From 798dc5a78ac9b0016c9b5c193bf8c7dea73ef908 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 2 Mar 2022 13:06:28 -0500 Subject: [PATCH] Make sure we notice when a u16 is loaded at offset 1 into a u8 allocation --- .../unaligned_pointers/unaligned_ptr4.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/compile-fail/unaligned_pointers/unaligned_ptr4.rs diff --git a/tests/compile-fail/unaligned_pointers/unaligned_ptr4.rs b/tests/compile-fail/unaligned_pointers/unaligned_ptr4.rs new file mode 100644 index 000000000000..10766746bd42 --- /dev/null +++ b/tests/compile-fail/unaligned_pointers/unaligned_ptr4.rs @@ -0,0 +1,12 @@ +// This should fail even without validation or Stacked Borrows. +// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows + +fn main() { + // Make sure we notice when a u16 is loaded at offset 1 into a u8 allocation. + // (This would be missed if u8 allocations are *always* at odd addresses.) + for _ in 0..10 { // Try many times as this might work by chance. + let x = [0u8; 4]; + let ptr = x.as_ptr().wrapping_offset(1).cast::(); + let _val = unsafe { *ptr }; //~ERROR but alignment + } +}