From d5b15297acc5f585b308700d27c0f1420154a14e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 18 Aug 2020 10:11:54 +0200 Subject: [PATCH] forgot to add alignment test loop in one test --- rust-version | 2 +- .../unaligned_pointers/alignment.rs | 2 ++ .../unaligned_pointers/atomic_unaligned.rs | 2 +- .../unaligned_pointers/dyn_alignment.rs | 22 ++++++++++--------- .../intptrcast_alignment_check.rs | 3 ++- .../unaligned_pointers/reference_to_packed.rs | 2 +- .../unaligned_pointers/unaligned_ptr1.rs | 2 +- .../unaligned_pointers/unaligned_ptr2.rs | 14 ++++++------ .../unaligned_pointers/unaligned_ptr3.rs | 3 +-- .../unaligned_pointers/unaligned_ptr_zst.rs | 11 +++++----- 10 files changed, 34 insertions(+), 29 deletions(-) diff --git a/rust-version b/rust-version index e322ff61d1b..6d85c7fb388 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -8cdc94e84040ce797fd33d0a7cfda4ec4f2f2421 +515c9fa505e18a65d7f61bc3e9eb833b79a68618 diff --git a/tests/compile-fail/unaligned_pointers/alignment.rs b/tests/compile-fail/unaligned_pointers/alignment.rs index e4d7621b8b1..ff31fc6c293 100644 --- a/tests/compile-fail/unaligned_pointers/alignment.rs +++ b/tests/compile-fail/unaligned_pointers/alignment.rs @@ -1,6 +1,8 @@ // error-pattern: but alignment 4 is required fn main() { + // No retry needed, this fails reliably. + let mut x = [0u8; 20]; let x_ptr: *mut u8 = x.as_mut_ptr(); // At least one of these is definitely unaligned. diff --git a/tests/compile-fail/unaligned_pointers/atomic_unaligned.rs b/tests/compile-fail/unaligned_pointers/atomic_unaligned.rs index 77eff5087da..7e0704ac6fc 100644 --- a/tests/compile-fail/unaligned_pointers/atomic_unaligned.rs +++ b/tests/compile-fail/unaligned_pointers/atomic_unaligned.rs @@ -8,6 +8,6 @@ fn main() { let zptr = &z as *const _ as *const u64; unsafe { ::std::intrinsics::atomic_load(zptr); - //~^ ERROR accessing memory with alignment 4, but alignment 8 is required + //~^ERROR accessing memory with alignment 4, but alignment 8 is required } } diff --git a/tests/compile-fail/unaligned_pointers/dyn_alignment.rs b/tests/compile-fail/unaligned_pointers/dyn_alignment.rs index a40db99a72a..91d9ec475b1 100644 --- a/tests/compile-fail/unaligned_pointers/dyn_alignment.rs +++ b/tests/compile-fail/unaligned_pointers/dyn_alignment.rs @@ -6,14 +6,16 @@ struct MuchAlign; fn main() { - let buf = [0u32; 256]; - // `buf` is sufficiently aligned for `layout.align` on a `dyn Debug`, but not - // for the actual alignment required by `MuchAlign`. - // We craft a wide reference `&dyn Debug` with the vtable for `MuchAlign`. That should be UB, - // as the reference is not aligned to its dynamic alignment requirements. - let mut ptr = &MuchAlign as &dyn std::fmt::Debug; - // Overwrite the data part of `ptr` so it points to `buf`. - unsafe { (&mut ptr as *mut _ as *mut *const u8).write(&buf as *const _ as *const u8); } - // Re-borrow that. This should be UB. - let _ptr = &*ptr; //~ ERROR alignment 256 is required + for _ in 0..10 { // Try many times as this might work by chance. + let buf = [0u32; 256]; + // `buf` is sufficiently aligned for `layout.align` on a `dyn Debug`, but not + // for the actual alignment required by `MuchAlign`. + // We craft a wide reference `&dyn Debug` with the vtable for `MuchAlign`. That should be UB, + // as the reference is not aligned to its dynamic alignment requirements. + let mut ptr = &MuchAlign as &dyn std::fmt::Debug; + // Overwrite the data part of `ptr` so it points to `buf`. + unsafe { (&mut ptr as *mut _ as *mut *const u8).write(&buf as *const _ as *const u8); } + // Re-borrow that. This should be UB. + let _ptr = &*ptr; //~ERROR alignment 256 is required + } } diff --git a/tests/compile-fail/unaligned_pointers/intptrcast_alignment_check.rs b/tests/compile-fail/unaligned_pointers/intptrcast_alignment_check.rs index 3865d457863..9872a493c02 100644 --- a/tests/compile-fail/unaligned_pointers/intptrcast_alignment_check.rs +++ b/tests/compile-fail/unaligned_pointers/intptrcast_alignment_check.rs @@ -9,8 +9,9 @@ fn main() { let x = &mut [0u8; 3]; let base_addr = x as *mut _ as usize; + // Manually make sure the pointer is properly aligned. let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr+1 }; let u16_ptr = base_addr_aligned as *mut u16; - unsafe { *u16_ptr = 2; } //~ ERROR memory with alignment 1, but alignment 2 is required + unsafe { *u16_ptr = 2; } //~ERROR memory with alignment 1, but alignment 2 is required println!("{:?}", x); } diff --git a/tests/compile-fail/unaligned_pointers/reference_to_packed.rs b/tests/compile-fail/unaligned_pointers/reference_to_packed.rs index 998394c6c70..6fa95211853 100644 --- a/tests/compile-fail/unaligned_pointers/reference_to_packed.rs +++ b/tests/compile-fail/unaligned_pointers/reference_to_packed.rs @@ -16,6 +16,6 @@ fn main() { y: 99, }; let p = unsafe { &foo.x }; - let i = *p; //~ ERROR alignment 4 is required + let i = *p; //~ERROR alignment 4 is required } } diff --git a/tests/compile-fail/unaligned_pointers/unaligned_ptr1.rs b/tests/compile-fail/unaligned_pointers/unaligned_ptr1.rs index 43e6fd67d24..1d72e5170b7 100644 --- a/tests/compile-fail/unaligned_pointers/unaligned_ptr1.rs +++ b/tests/compile-fail/unaligned_pointers/unaligned_ptr1.rs @@ -6,6 +6,6 @@ fn main() { let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error. let x = &x[0] as *const _ as *const u32; // This must fail because alignment is violated: the allocation's base is not sufficiently aligned. - let _x = unsafe { *x }; //~ ERROR memory with alignment 2, but alignment 4 is required + let _x = unsafe { *x }; //~ERROR memory with alignment 2, but alignment 4 is required } } diff --git a/tests/compile-fail/unaligned_pointers/unaligned_ptr2.rs b/tests/compile-fail/unaligned_pointers/unaligned_ptr2.rs index f4ed8d47b53..49612e2b8a0 100644 --- a/tests/compile-fail/unaligned_pointers/unaligned_ptr2.rs +++ b/tests/compile-fail/unaligned_pointers/unaligned_ptr2.rs @@ -2,11 +2,11 @@ // compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows fn main() { - for _ in 0..10 { // Try many times as this might work by chance. - let x = [2u32, 3]; // Make it big enough so we don't get an out-of-bounds error. - let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32; - // This must fail because alignment is violated: the offset is not sufficiently aligned. - // Also make the offset not a power of 2, that used to ICE. - let _x = unsafe { *x }; //~ ERROR memory with alignment 1, but alignment 4 is required - } + // No retry needed, this fails reliably. + + let x = [2u32, 3]; // Make it big enough so we don't get an out-of-bounds error. + let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32; + // This must fail because alignment is violated: the offset is not sufficiently aligned. + // Also make the offset not a power of 2, that used to ICE. + let _x = unsafe { *x }; //~ERROR memory with alignment 1, but alignment 4 is required } diff --git a/tests/compile-fail/unaligned_pointers/unaligned_ptr3.rs b/tests/compile-fail/unaligned_pointers/unaligned_ptr3.rs index 61c2a3cde89..ecab83b05a0 100644 --- a/tests/compile-fail/unaligned_pointers/unaligned_ptr3.rs +++ b/tests/compile-fail/unaligned_pointers/unaligned_ptr3.rs @@ -7,7 +7,6 @@ fn main() { let x = &x[0] as *const _ as *const *const u8; // cast to ptr-to-ptr, so that we load a ptr // This must fail because alignment is violated. Test specifically for loading pointers, // which have special code in miri's memory. - let _x = unsafe { *x }; - //~^ ERROR but alignment + let _x = unsafe { *x }; //~ERROR but alignment } } diff --git a/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs b/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs index beba47359b5..2b6ff3f71c6 100644 --- a/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs +++ b/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs @@ -2,9 +2,10 @@ // compile-flags: -Zmiri-disable-validation fn main() { - let x = &2u8; - let x = x as *const _ as *const [u32; 0]; - // This must fail because alignment is violated. Test specifically for loading ZST. - let _x = unsafe { *x }; - //~^ ERROR alignment 4 is required + for _ in 0..10 { // Try many times as this might work by chance. + let x = &2u8; + let x = x as *const _ as *const [u32; 0]; + // This must fail because alignment is violated. Test specifically for loading ZST. + let _x = unsafe { *x }; //~ERROR alignment 4 is required + } } -- 2.44.0