Skip to content

Commit 4711df2

Browse files
committed
make another test more robust against random alignment
1 parent db159b8 commit 4711df2

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

tests/compile-fail/unaligned_pointers/unaligned_ptr1.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33

44
fn main() {
5-
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
6-
let x = &x[0] as *const _ as *const u32;
7-
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
8-
let _x = unsafe { *x }; //~ ERROR memory with alignment 2, but alignment 4 is required
5+
for _ in 0..10 { // Try many times as this might work by chance.
6+
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
7+
let x = &x[0] as *const _ as *const u32;
8+
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
9+
let _x = unsafe { *x }; //~ ERROR memory with alignment 2, but alignment 4 is required
10+
}
911
}

tests/compile-fail/unaligned_pointers/unaligned_ptr2.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33

44
fn main() {
5-
let x = [2u32, 3]; // Make it big enough so we don't get an out-of-bounds error.
6-
let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32;
7-
// This must fail because alignment is violated: the offset is not sufficiently aligned.
8-
// Also make the offset not a power of 2, that used to ICE.
9-
let _x = unsafe { *x }; //~ ERROR memory with alignment 1, but alignment 4 is required
5+
for _ in 0..10 { // Try many times as this might work by chance.
6+
let x = [2u32, 3]; // Make it big enough so we don't get an out-of-bounds error.
7+
let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32;
8+
// This must fail because alignment is violated: the offset is not sufficiently aligned.
9+
// Also make the offset not a power of 2, that used to ICE.
10+
let _x = unsafe { *x }; //~ ERROR memory with alignment 1, but alignment 4 is required
11+
}
1012
}

tests/compile-fail/unaligned_pointers/unaligned_ptr3.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33

44
fn main() {
5-
let x = [2u16, 3, 4, 5]; // Make it big enough so we don't get an out-of-bounds error.
6-
let x = &x[0] as *const _ as *const *const u8; // cast to ptr-to-ptr, so that we load a ptr
7-
// This must fail because alignment is violated. Test specifically for loading pointers,
8-
// which have special code in miri's memory.
9-
let _x = unsafe { *x };
10-
//~^ ERROR memory with alignment 2, but alignment
5+
for _ in 0..10 { // Try many times as this might work by chance.
6+
let x = [2u16, 3, 4, 5]; // Make it big enough so we don't get an out-of-bounds error.
7+
let x = &x[0] as *const _ as *const *const u8; // cast to ptr-to-ptr, so that we load a ptr
8+
// This must fail because alignment is violated. Test specifically for loading pointers,
9+
// which have special code in miri's memory.
10+
let _x = unsafe { *x };
11+
//~^ ERROR but alignment
12+
}
1113
}

tests/compile-fail/unaligned_pointers/unaligned_ptr_addr_of.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
use std::ptr;
55

66
fn main() {
7-
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
8-
let x = &x[0] as *const _ as *const u32;
9-
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
10-
// The deref is UB even if we just put the result into a raw pointer.
11-
let _x = unsafe { ptr::raw_const!(*x) }; //~ ERROR memory with alignment 2, but alignment 4 is required
7+
for _ in 0..10 { // Try many times as this might work by chance.
8+
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
9+
let x = &x[0] as *const _ as *const u32;
10+
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
11+
// The deref is UB even if we just put the result into a raw pointer.
12+
let _x = unsafe { ptr::raw_const!(*x) }; //~ ERROR memory with alignment 2, but alignment 4 is required
13+
}
1214
}

0 commit comments

Comments
 (0)