From fd8beaf5c4abac980d5730a4e19ec6c1879907b6 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 13 Apr 2020 17:51:22 +0200 Subject: [PATCH] add option to disable alignment checks --- src/bin/miri.rs | 5 +++++ src/eval.rs | 4 ++++ src/machine.rs | 17 +++++++++++++++-- .../unaligned_pointers/alignment.rs | 2 +- tests/run-pass/disable-alignment-check.rs | 11 +++++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 tests/run-pass/disable-alignment-check.rs diff --git a/src/bin/miri.rs b/src/bin/miri.rs index 14d78053c0fd..1ceb6e621a47 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -128,6 +128,7 @@ fn main() { // Parse our arguments and split them across `rustc` and `miri`. let mut validate = true; let mut stacked_borrows = true; + let mut check_alignment = true; let mut communicate = false; let mut ignore_leaks = false; let mut seed: Option = None; @@ -152,6 +153,9 @@ fn main() { "-Zmiri-disable-stacked-borrows" => { stacked_borrows = false; } + "-Zmiri-disable-alignment-check" => { + check_alignment = false; + } "-Zmiri-disable-isolation" => { communicate = true; } @@ -243,6 +247,7 @@ fn main() { let miri_config = miri::MiriConfig { validate, stacked_borrows, + check_alignment, communicate, ignore_leaks, excluded_env_vars, diff --git a/src/eval.rs b/src/eval.rs index 094be194f178..b360b1bd8bbc 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -19,6 +19,8 @@ pub struct MiriConfig { pub validate: bool, /// Determines if Stacked Borrows is enabled. pub stacked_borrows: bool, + /// Determines if alignment checking is enabled. + pub check_alignment: bool, /// Determines if communication with the host environment is enabled. pub communicate: bool, /// Determines if memory leaks should be ignored. @@ -40,6 +42,7 @@ impl Default for MiriConfig { MiriConfig { validate: true, stacked_borrows: true, + check_alignment: true, communicate: false, ignore_leaks: false, excluded_env_vars: vec![], @@ -72,6 +75,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( config.stacked_borrows, config.tracked_pointer_tag, config.tracked_alloc_id, + config.check_alignment, ), ); // Complete initialization. diff --git a/src/machine.rs b/src/machine.rs index 72635f7bf57b..54dfb49d798b 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -118,10 +118,19 @@ pub struct MemoryExtra { /// An allocation ID to report when it is being allocated /// (helps for debugging memory leaks). tracked_alloc_id: Option, + + /// Controls whether alignment of memory accesses is being checked. + check_alignment: bool, } impl MemoryExtra { - pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option, tracked_alloc_id: Option) -> Self { + pub fn new( + rng: StdRng, + stacked_borrows: bool, + tracked_pointer_tag: Option, + tracked_alloc_id: Option, + check_alignment: bool, + ) -> Self { let stacked_borrows = if stacked_borrows { Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(tracked_pointer_tag)))) } else { @@ -133,6 +142,7 @@ impl MemoryExtra { extern_statics: FxHashMap::default(), rng: RefCell::new(rng), tracked_alloc_id, + check_alignment, } } @@ -299,7 +309,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> { const GLOBAL_KIND: Option = Some(MiriMemoryKind::Global); - const CHECK_ALIGN: bool = true; + #[inline(always)] + fn enforce_alignment(memory_extra: &MemoryExtra) -> bool { + memory_extra.check_alignment + } #[inline(always)] fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool { diff --git a/tests/compile-fail/unaligned_pointers/alignment.rs b/tests/compile-fail/unaligned_pointers/alignment.rs index bac1b92075a7..b732a949af87 100644 --- a/tests/compile-fail/unaligned_pointers/alignment.rs +++ b/tests/compile-fail/unaligned_pointers/alignment.rs @@ -2,7 +2,7 @@ fn main() { // miri always gives allocations the worst possible alignment, so a `u8` array is guaranteed // to be at the virtual location 1 (so one byte offset from the ultimate alignemnt location 0) let mut x = [0u8; 20]; - let x_ptr: *mut u8 = &mut x[0]; + let x_ptr: *mut u8 = x.as_mut_ptr(); let y_ptr = x_ptr as *mut u64; unsafe { *y_ptr = 42; //~ ERROR accessing memory with alignment 1, but alignment diff --git a/tests/run-pass/disable-alignment-check.rs b/tests/run-pass/disable-alignment-check.rs new file mode 100644 index 000000000000..2fb0dd8369df --- /dev/null +++ b/tests/run-pass/disable-alignment-check.rs @@ -0,0 +1,11 @@ +// compile-flags: -Zmiri-disable-alignment-check + +fn main() { + let mut x = [0u8; 20]; + let x_ptr: *mut u8 = x.as_mut_ptr(); + // At least one of these is definitely unaligned. + unsafe { + *(x_ptr as *mut u64) = 42; + *(x_ptr.add(1) as *mut u64) = 42; + } +}