Rust Update, clean up MMappedMemory
Ensure the code compiles with the latest rust nightly version, and fixes some unsafety in MMappedMemory
This commit is contained in:
parent
c12a3ba29b
commit
8d78c70dae
8
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
8
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="ClangTidy" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="clangTidyChecks" value="-*,bugprone-argument-comment,bugprone-assert-side-effect,bugprone-bad-signal-to-kill-thread,bugprone-branch-clone,bugprone-copy-constructor-init,bugprone-dangling-handle,bugprone-dynamic-static-initializers,bugprone-fold-init-type,bugprone-forward-declaration-namespace,bugprone-forwarding-reference-overload,bugprone-inaccurate-erase,bugprone-incorrect-roundings,bugprone-integer-division,bugprone-lambda-function-name,bugprone-macro-parentheses,bugprone-macro-repeated-side-effects,bugprone-misplaced-operator-in-strlen-in-alloc,bugprone-misplaced-pointer-arithmetic-in-alloc,bugprone-misplaced-widening-cast,bugprone-move-forwarding-reference,bugprone-multiple-statement-macro,bugprone-no-escape,bugprone-not-null-terminated-result,bugprone-parent-virtual-call,bugprone-posix-return,bugprone-reserved-identifier,bugprone-sizeof-container,bugprone-sizeof-expression,bugprone-spuriously-wake-up-functions,bugprone-string-constructor,bugprone-string-integer-assignment,bugprone-string-literal-with-embedded-nul,bugprone-suspicious-enum-usage,bugprone-suspicious-include,bugprone-suspicious-memset-usage,bugprone-suspicious-missing-comma,bugprone-suspicious-semicolon,bugprone-suspicious-string-compare,bugprone-swapped-arguments,bugprone-terminating-continue,bugprone-throw-keyword-missing,bugprone-too-small-loop-variable,bugprone-undefined-memory-manipulation,bugprone-undelegated-constructor,bugprone-unhandled-self-assignment,bugprone-unused-raii,bugprone-unused-return-value,bugprone-use-after-move,bugprone-virtual-near-miss,cert-dcl21-cpp,cert-dcl58-cpp,cert-err34-c,cert-err52-cpp,cert-err58-cpp,cert-err60-cpp,cert-flp30-c,cert-msc50-cpp,cert-msc51-cpp,cert-str34-c,cppcoreguidelines-interfaces-global-init,cppcoreguidelines-narrowing-conversions,cppcoreguidelines-pro-type-member-init,cppcoreguidelines-pro-type-static-cast-downcast,cppcoreguidelines-slicing,google-default-arguments,google-explicit-constructor,google-runtime-operator,hicpp-exception-baseclass,hicpp-multiway-paths-covered,misc-misplaced-const,misc-new-delete-overloads,misc-no-recursion,misc-non-copyable-objects,misc-throw-by-value-catch-by-reference,misc-unconventional-assign-operator,misc-uniqueptr-reset-release,modernize-avoid-bind,modernize-concat-nested-namespaces,modernize-deprecated-headers,modernize-deprecated-ios-base-aliases,modernize-loop-convert,modernize-make-shared,modernize-make-unique,modernize-pass-by-value,modernize-raw-string-literal,modernize-redundant-void-arg,modernize-replace-auto-ptr,modernize-replace-disallow-copy-and-assign-macro,modernize-replace-random-shuffle,modernize-return-braced-init-list,modernize-shrink-to-fit,modernize-unary-static-assert,modernize-use-auto,modernize-use-bool-literals,modernize-use-emplace,modernize-use-equals-default,modernize-use-equals-delete,modernize-use-nodiscard,modernize-use-noexcept,modernize-use-nullptr,modernize-use-override,modernize-use-transparent-functors,modernize-use-uncaught-exceptions,mpi-buffer-deref,mpi-type-mismatch,openmp-use-default-none,performance-faster-string-find,performance-for-range-copy,performance-implicit-conversion-in-loop,performance-inefficient-algorithm,performance-inefficient-string-concatenation,performance-inefficient-vector-operation,performance-move-const-arg,performance-move-constructor-init,performance-no-automatic-move,performance-noexcept-move-constructor,performance-trivially-destructible,performance-type-promotion-in-math-fn,performance-unnecessary-copy-initialization,performance-unnecessary-value-param,portability-simd-intrinsics,readability-avoid-const-params-in-decls,readability-const-return-type,readability-container-size-empty,readability-convert-member-functions-to-static,readability-delete-null-pointer,readability-deleted-default,readability-inconsistent-declaration-parameter-name,readability-make-member-function-const,readability-misleading-indentation,readability-misplaced-array-index,readability-non-const-parameter,readability-redundant-control-flow,readability-redundant-declaration,readability-redundant-function-ptr-dereference,readability-redundant-smartptr-get,readability-redundant-string-cstr,readability-redundant-string-init,readability-simplify-subscript-expr,readability-static-accessed-through-instance,readability-static-definition-in-anonymous-namespace,readability-string-compare,readability-uniqueptr-delete-release,readability-use-anyofallof,bugprone-implicit-widening-of-multiplication-result,bugprone-unhandled-exception-at-new,concurrency-thread-canceltype-asynchronous" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
@ -212,7 +212,7 @@ impl<T: TimingChannelPrimitives> TopologyAwareTimingChannel<T> {
|
||||
pub fn new_with_core_pairs(
|
||||
core_pairs: impl Iterator<Item = (usize, usize)> + Clone,
|
||||
) -> Result<(Self, usize, usize), TopologyAwareError> {
|
||||
let m = MMappedMemory::new(PAGE_LEN, false);
|
||||
let m = MMappedMemory::new(PAGE_LEN, false, |i| i as u8);
|
||||
let array: &[u8] = m.slice();
|
||||
|
||||
let t = Default::default();
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use cache_utils::cache_info::get_cache_info;
|
||||
use cache_utils::complex_addressing::cache_slicing;
|
||||
use cpuid::MicroArchitecture;
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use cache_utils::frequency::get_freq_cpufreq_kernel;
|
||||
use cache_utils::rdtsc_fence;
|
||||
use libc::sched_getcpu;
|
||||
|
@ -1,8 +1,10 @@
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use cache_utils::flush;
|
||||
use cache_utils::mmap::MMappedMemory;
|
||||
|
||||
pub fn main() {
|
||||
let m = MMappedMemory::new(2 << 20, true);
|
||||
let m = MMappedMemory::new(2 << 20, true, |i| i as u8);
|
||||
let array = m.slice();
|
||||
loop {
|
||||
unsafe {
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use cache_utils::calibration::only_flush;
|
||||
use cache_utils::frequency::get_freq_cpufreq_kernel;
|
||||
use cache_utils::rdtsc_fence;
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use cache_utils::calibration::only_reload;
|
||||
use cache_utils::frequency::get_freq_cpufreq_kernel;
|
||||
use cache_utils::rdtsc_fence;
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use cache_utils::calibration::{
|
||||
accumulate, calibrate_fixed_freq_2_thread, calibration_result_to_ASVP, flush_and_reload,
|
||||
get_cache_slicing, load_and_flush, map_values, only_flush, only_reload, reduce,
|
||||
@ -18,15 +20,17 @@ use std::process::Command;
|
||||
use std::str::from_utf8;
|
||||
|
||||
unsafe fn multiple_access(p: *const u8) {
|
||||
maccess::<u8>(p);
|
||||
maccess::<u8>(p);
|
||||
arch_x86::_mm_mfence();
|
||||
maccess::<u8>(p);
|
||||
arch_x86::_mm_mfence();
|
||||
maccess::<u8>(p);
|
||||
arch_x86::_mm_mfence();
|
||||
maccess::<u8>(p);
|
||||
maccess::<u8>(p);
|
||||
unsafe {
|
||||
maccess::<u8>(p);
|
||||
maccess::<u8>(p);
|
||||
arch_x86::_mm_mfence();
|
||||
maccess::<u8>(p);
|
||||
arch_x86::_mm_mfence();
|
||||
maccess::<u8>(p);
|
||||
arch_x86::_mm_mfence();
|
||||
maccess::<u8>(p);
|
||||
maccess::<u8>(p);
|
||||
}
|
||||
}
|
||||
|
||||
const SIZE: usize = 2 << 20;
|
||||
@ -105,7 +109,7 @@ fn main() {
|
||||
|
||||
println!("Number of cores per socket: {}", core_per_socket);
|
||||
|
||||
let m = MMappedMemory::new(SIZE, true);
|
||||
let m = MMappedMemory::new(SIZE, true, |i: usize| i as u8);
|
||||
let array = m.slice();
|
||||
|
||||
let cache_line_size = 64;
|
||||
|
@ -10,10 +10,9 @@ use nix::unistd::Pid;
|
||||
use nix::Error;
|
||||
use std::cmp::min;
|
||||
use std::ptr::null_mut;
|
||||
//use std::sync::atomic::Ordering;
|
||||
use std::mem::forget;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::vec::Vec;
|
||||
use turn_lock::TurnHandle;
|
||||
|
||||
pub struct CalibrateOperation2T<'a, T> {
|
||||
@ -111,7 +110,7 @@ fn calibrate_fixed_freq_2_thread_impl<I: Iterator<Item = (usize, usize)>, T>(
|
||||
},
|
||||
);
|
||||
|
||||
let mut helper_turn_handle = Arc::new(Mutex::new(turn_handles.pop().unwrap()));
|
||||
let helper_turn_handle = Arc::new(Mutex::new(turn_handles.pop().unwrap()));
|
||||
let mut main_turn_handle = turn_handles.pop().unwrap();
|
||||
|
||||
let mut params = main_turn_handle.wait();
|
||||
|
@ -9,17 +9,6 @@ use core::arch::x86_64 as arch_x86;
|
||||
#[cfg(feature = "no_std")]
|
||||
use polling_serial::{serial_print as print, serial_println as println};
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
use nix::unistd::Pid;
|
||||
//#[cfg(feature = "use_std")]
|
||||
//use nix::Error::Sys;
|
||||
#[cfg(feature = "use_std")]
|
||||
use nix::Error;
|
||||
#[cfg(feature = "use_std")]
|
||||
use std::sync::Arc;
|
||||
#[cfg(feature = "use_std")]
|
||||
use std::thread;
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
pub use crate::calibrate_2t::*;
|
||||
|
||||
@ -28,11 +17,8 @@ use crate::calibration::Verbosity::*;
|
||||
use alloc::vec;
|
||||
use alloc::vec::Vec;
|
||||
use core::cmp::min;
|
||||
use core::ptr::null_mut;
|
||||
use core::sync::atomic::{spin_loop_hint, AtomicBool, AtomicPtr, Ordering};
|
||||
use itertools::Itertools;
|
||||
|
||||
use atomic::Atomic;
|
||||
use core::hash::Hash;
|
||||
use core::ops::{Add, AddAssign};
|
||||
#[cfg(feature = "no_std")]
|
||||
@ -71,44 +57,52 @@ impl CalibrationOptions {
|
||||
}
|
||||
|
||||
pub unsafe fn only_reload(p: *const u8) -> u64 {
|
||||
let t = rdtsc_fence();
|
||||
maccess(p);
|
||||
rdtsc_fence() - t
|
||||
let t = unsafe { rdtsc_fence() };
|
||||
unsafe { maccess(p) };
|
||||
(unsafe { rdtsc_fence() } - t)
|
||||
}
|
||||
|
||||
pub unsafe fn flush_and_reload(p: *const u8) -> u64 {
|
||||
flush(p);
|
||||
only_reload(p)
|
||||
unsafe {
|
||||
flush(p);
|
||||
only_reload(p)
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn reload_and_flush(p: *const u8) -> u64 {
|
||||
let r = only_reload(p);
|
||||
flush(p);
|
||||
let r = unsafe { only_reload(p) };
|
||||
unsafe { flush(p) };
|
||||
r
|
||||
}
|
||||
|
||||
pub unsafe fn only_flush(p: *const u8) -> u64 {
|
||||
let t = rdtsc_fence();
|
||||
flush(p);
|
||||
rdtsc_fence() - t
|
||||
let t = unsafe { rdtsc_fence() };
|
||||
unsafe { flush(p) };
|
||||
(unsafe { rdtsc_fence() } - t)
|
||||
}
|
||||
|
||||
pub unsafe fn load_and_flush(p: *const u8) -> u64 {
|
||||
maccess(p);
|
||||
only_flush(p)
|
||||
unsafe {
|
||||
maccess(p);
|
||||
only_flush(p)
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn flush_and_flush(p: *const u8) -> u64 {
|
||||
flush(p);
|
||||
only_flush(p)
|
||||
unsafe {
|
||||
flush(p);
|
||||
only_flush(p)
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn l3_and_reload(p: *const u8) -> u64 {
|
||||
flush(p);
|
||||
arch_x86::_mm_mfence();
|
||||
arch_x86::_mm_prefetch(p as *const i8, arch_x86::_MM_HINT_T2);
|
||||
arch_x86::__cpuid_count(0, 0);
|
||||
only_reload(p)
|
||||
unsafe {
|
||||
flush(p);
|
||||
arch_x86::_mm_mfence();
|
||||
arch_x86::_mm_prefetch::<{ arch_x86::_MM_HINT_T2 }>(p as *const i8);
|
||||
arch_x86::__cpuid_count(0, 0);
|
||||
only_reload(p)
|
||||
}
|
||||
}
|
||||
|
||||
pub const PAGE_SHIFT: usize = 12;
|
||||
|
@ -1,6 +1,7 @@
|
||||
#![cfg_attr(feature = "no_std", no_std)]
|
||||
#![feature(ptr_internals)]
|
||||
#![allow(clippy::missing_safety_doc)]
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use static_assertions::assert_cfg;
|
||||
|
||||
@ -29,23 +30,23 @@ use core::ptr;
|
||||
|
||||
// rdtsc no fence
|
||||
pub unsafe fn rdtsc_nofence() -> u64 {
|
||||
arch_x86::_rdtsc()
|
||||
unsafe { arch_x86::_rdtsc() }
|
||||
}
|
||||
// rdtsc (has mfence before and after)
|
||||
pub unsafe fn rdtsc_fence() -> u64 {
|
||||
arch_x86::_mm_mfence();
|
||||
let tsc: u64 = arch_x86::_rdtsc();
|
||||
arch_x86::_mm_mfence();
|
||||
unsafe { arch_x86::_mm_mfence() };
|
||||
let tsc: u64 = unsafe { arch_x86::_rdtsc() };
|
||||
unsafe { arch_x86::_mm_mfence() };
|
||||
tsc
|
||||
}
|
||||
|
||||
pub unsafe fn maccess<T>(p: *const T) {
|
||||
ptr::read_volatile(p);
|
||||
unsafe { ptr::read_volatile(p) };
|
||||
}
|
||||
|
||||
// flush (cflush)
|
||||
pub unsafe fn flush(p: *const u8) {
|
||||
arch_x86::_mm_clflush(p);
|
||||
unsafe { arch_x86::_mm_clflush(p) };
|
||||
}
|
||||
|
||||
pub fn noop<T>(_: *const T) {}
|
||||
|
@ -33,7 +33,7 @@ struct Page {
|
||||
}
|
||||
*/
|
||||
pub fn main() {
|
||||
let m = MMappedMemory::new(SIZE, true);
|
||||
let m = MMappedMemory::new(SIZE, true, |i| i as u8);
|
||||
let array = m.slice();
|
||||
|
||||
let old = sched_getaffinity(Pid::from_raw(0)).unwrap();
|
||||
|
@ -2,8 +2,9 @@
|
||||
|
||||
use core::borrow::{Borrow, BorrowMut};
|
||||
use core::ffi::c_void;
|
||||
use core::mem::size_of;
|
||||
use core::mem::{size_of, MaybeUninit};
|
||||
use core::ops::{Deref, DerefMut};
|
||||
use core::ptr;
|
||||
use core::ptr::null_mut;
|
||||
use core::ptr::Unique;
|
||||
use core::slice::{from_raw_parts, from_raw_parts_mut};
|
||||
@ -19,14 +20,18 @@ use nix::sys::mman;
|
||||
#define HUGETLB_FLAG_ENCODE_1MB (20 << HUGETLB_FLAG_ENCODE_SHIFT)
|
||||
#define HUGETLB_FLAG_ENCODE_2MB (21 << HUGETLB_FLAG_ENCODE_SHIFT)
|
||||
*/
|
||||
|
||||
/** Safety issue : if T is non triviably constructable and destructable this is dangerous */
|
||||
pub struct MMappedMemory<T> {
|
||||
pointer: Unique<T>,
|
||||
size: usize,
|
||||
}
|
||||
|
||||
impl<T> MMappedMemory<T> {
|
||||
pub fn try_new(size: usize, huge: bool) -> Result<MMappedMemory<T>, nix::Error> {
|
||||
pub fn try_new(
|
||||
size: usize,
|
||||
huge: bool,
|
||||
initializer: impl Fn(usize) -> T,
|
||||
) -> Result<MMappedMemory<T>, nix::Error> {
|
||||
assert_ne!(size_of::<T>(), 0);
|
||||
if let Some(p) = unsafe {
|
||||
let p = mman::mmap(
|
||||
@ -46,14 +51,48 @@ impl<T> MMappedMemory<T> {
|
||||
let pointer_T = p as *mut T;
|
||||
Unique::new(pointer_T)
|
||||
} {
|
||||
Ok(MMappedMemory { pointer: p, size })
|
||||
let mut s = MMappedMemory { pointer: p, size };
|
||||
for i in 0..s.size {
|
||||
unsafe { ptr::write(s.pointer.as_ptr().add(i), initializer(i)) };
|
||||
}
|
||||
Ok(s)
|
||||
} else {
|
||||
Err(nix::Error::Sys(EINVAL))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(size: usize, huge: bool) -> MMappedMemory<T> {
|
||||
Self::try_new(size, huge).unwrap()
|
||||
/*
|
||||
pub fn try_new_uninit(
|
||||
size: usize,
|
||||
huge: bool,
|
||||
) -> Result<MMappedMemory<MaybeUninit<T>>, nix::Error> {
|
||||
assert_ne!(size_of::<T>(), 0);
|
||||
if let Some(p) = unsafe {
|
||||
let p = mman::mmap(
|
||||
null_mut(),
|
||||
size * size_of::<T>(),
|
||||
mman::ProtFlags::PROT_READ | mman::ProtFlags::PROT_WRITE,
|
||||
mman::MapFlags::MAP_PRIVATE
|
||||
| mman::MapFlags::MAP_ANONYMOUS
|
||||
| if huge {
|
||||
mman::MapFlags::MAP_HUGETLB
|
||||
} else {
|
||||
mman::MapFlags::MAP_ANONYMOUS
|
||||
},
|
||||
-1,
|
||||
0,
|
||||
)?;
|
||||
let pointer_T = p as *mut T;
|
||||
Unique::new(pointer_T)
|
||||
} {
|
||||
let mut s = MMappedMemory { pointer: p, size };
|
||||
Ok(s)
|
||||
} else {
|
||||
Err(nix::Error::Sys(EINVAL))
|
||||
}
|
||||
}
|
||||
*/
|
||||
pub fn new(size: usize, huge: bool, init: impl Fn(usize) -> T) -> MMappedMemory<T> {
|
||||
Self::try_new(size, huge, init).unwrap()
|
||||
}
|
||||
|
||||
pub fn slice(&self) -> &[T] {
|
||||
@ -67,6 +106,9 @@ impl<T> MMappedMemory<T> {
|
||||
|
||||
impl<T> Drop for MMappedMemory<T> {
|
||||
fn drop(&mut self) {
|
||||
for i in 0..self.size {
|
||||
unsafe { ptr::drop_in_place(self.pointer.as_ptr().add(i)) };
|
||||
}
|
||||
unsafe {
|
||||
mman::munmap(self.pointer.as_ptr() as *mut c_void, self.size).unwrap();
|
||||
}
|
||||
|
@ -165,13 +165,10 @@ pub fn benchmark_channel<T: 'static + Send + CovertChannel>(
|
||||
let old_affinity = set_affinity(&channel.main_core()).unwrap();
|
||||
|
||||
let size = num_pages * PAGE_SIZE;
|
||||
let mut m = MMappedMemory::new(size, false);
|
||||
let mut m = MMappedMemory::new(size, false, |i| (i / PAGE_SIZE) as u8);
|
||||
let mut receiver_turn_handles = Vec::new();
|
||||
let mut transmit_turn_handles = Vec::new();
|
||||
|
||||
for i in 0..num_pages {
|
||||
m.slice_mut()[i * PAGE_SIZE] = i as u8;
|
||||
}
|
||||
let array: &[u8] = m.slice();
|
||||
for i in 0..num_pages {
|
||||
let addr = &array[i * PAGE_SIZE] as *const u8;
|
||||
|
@ -1,6 +1,4 @@
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
#![feature(const_generics)]
|
||||
#![feature(const_evaluatable_checked)]
|
||||
|
||||
use crate::Probe::{Flush, FullFlush, Load};
|
||||
use basic_timing_cache_channel::{TopologyAwareError, TopologyAwareTimingChannel};
|
||||
@ -166,15 +164,14 @@ impl<const GS: usize> Prober<GS> {
|
||||
};
|
||||
|
||||
for i in 0..num_pages {
|
||||
let mut p = match MMappedMemory::<u8>::try_new(PAGE_LEN * GS, false) {
|
||||
let mut p = match MMappedMemory::<u8>::try_new(PAGE_LEN * GS, false, |j| {
|
||||
(j / CACHE_LINE_LEN + i * PAGE_CACHELINE_LEN) as u8
|
||||
}) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
return Err(ProberError::NoMem(e));
|
||||
}
|
||||
};
|
||||
for j in 0..(PAGE_LEN * GS) {
|
||||
p[j] = (i * PAGE_CACHELINE_LEN + j) as u8;
|
||||
}
|
||||
let page_addresses = ((0..(PAGE_LEN * GS)).step_by(CACHE_LINE_LEN))
|
||||
.map(|offset| &p[offset] as *const u8);
|
||||
let ff_page_handles = unsafe { ff_channel.calibrate(page_addresses.clone()) }.unwrap();
|
||||
|
Loading…
x
Reference in New Issue
Block a user