From 72664e42aa1f66ac57891cc45f80cc925e261c19 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 19 Jul 2017 11:28:35 -0700 Subject: [PATCH] No longer check aligment and non-NULLness on `&` This breaks creating unaligned raw pointers via `&packed.field as *const _`, which needs to be legal. Also it doesn't seem like LLVM still relies on this, see * https://github.com/solson/miri/issues/244#issuecomment-315563640 * https://internals.rust-lang.org/t/rules-for-alignment-and-non-nullness-of-references/5430/16 We probably want to handle this invariant like the others that validation is concerned with, and only check it on function boundaries for now. --- src/eval_context.rs | 4 ---- tests/compile-fail/int_ptr_cast.rs | 5 ----- tests/compile-fail/int_ptr_cast2.rs | 5 ----- tests/compile-fail/reference_to_packed.rs | 4 ++-- tests/compile-fail/unaligned_ptr_cast.rs | 2 +- 5 files changed, 3 insertions(+), 17 deletions(-) delete mode 100644 tests/compile-fail/int_ptr_cast.rs delete mode 100644 tests/compile-fail/int_ptr_cast2.rs diff --git a/src/eval_context.rs b/src/eval_context.rs index 2f28063ff86d..ff09e5db9490 100644 --- a/src/eval_context.rs +++ b/src/eval_context.rs @@ -682,10 +682,6 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> { bug!("attempted to take a reference to an enum downcast lvalue"), }; - // Check alignment and non-NULLness. - let (_, align) = self.size_and_align_of_dst(ty, val)?; - self.memory.check_align(ptr, align)?; - self.write_value(val, dest, dest_ty)?; } diff --git a/tests/compile-fail/int_ptr_cast.rs b/tests/compile-fail/int_ptr_cast.rs deleted file mode 100644 index ae5f65a7166c..000000000000 --- a/tests/compile-fail/int_ptr_cast.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let x = 2usize as *const u32; - // This must fail because alignment is violated - let _ = unsafe { &*x }; //~ ERROR: tried to access memory with alignment 2, but alignment 4 is required -} diff --git a/tests/compile-fail/int_ptr_cast2.rs b/tests/compile-fail/int_ptr_cast2.rs deleted file mode 100644 index 1897066f7bcc..000000000000 --- a/tests/compile-fail/int_ptr_cast2.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let x = 0usize as *const u32; - // This must fail because the pointer is NULL - let _ = unsafe { &*x }; //~ ERROR: invalid use of NULL pointer -} diff --git a/tests/compile-fail/reference_to_packed.rs b/tests/compile-fail/reference_to_packed.rs index 4cf353298b9e..5ca733a64df2 100644 --- a/tests/compile-fail/reference_to_packed.rs +++ b/tests/compile-fail/reference_to_packed.rs @@ -11,6 +11,6 @@ fn main() { x: 42, y: 99, }; - let p = &foo.x; //~ ERROR tried to access memory with alignment 1, but alignment 4 is required - let i = *p; + let p = &foo.x; + let i = *p; //~ ERROR tried to access memory with alignment 1, but alignment 4 is required } diff --git a/tests/compile-fail/unaligned_ptr_cast.rs b/tests/compile-fail/unaligned_ptr_cast.rs index fcab430f8fcb..8ad1b323250c 100644 --- a/tests/compile-fail/unaligned_ptr_cast.rs +++ b/tests/compile-fail/unaligned_ptr_cast.rs @@ -2,5 +2,5 @@ fn main() { let x = &2u16; let x = x as *const _ as *const u32; // This must fail because alignment is violated - let _ = unsafe { &*x }; //~ ERROR: tried to access memory with alignment 2, but alignment 4 is required + let _x = unsafe { *x }; //~ ERROR: tried to access memory with alignment 2, but alignment 4 is required }