Skip to content

Commit 035e003

Browse files
committed
make another test more robust against random alignment
1 parent db159b8 commit 035e003

File tree

6 files changed

+39
-29
lines changed

6 files changed

+39
-29
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
// error-pattern: but alignment 8 is required
2+
13
fn main() {
24
let mut x = [0u8; 20];
35
let x_ptr: *mut u8 = x.as_mut_ptr();
46
// At least one of these is definitely unaligned.
5-
// Currently, we guarantee to complain about the first one already (https://github.com/rust-lang/miri/issues/1074).
67
unsafe {
7-
*(x_ptr as *mut u64) = 42; //~ ERROR accessing memory with alignment 1, but alignment
8+
*(x_ptr as *mut u64) = 42;
89
*(x_ptr.add(1) as *mut u64) = 42;
910
}
10-
panic!("unreachable in miri");
1111
}

tests/compile-fail/unaligned_pointers/reference_to_packed.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ struct Foo {
1010
}
1111

1212
fn main() {
13-
let foo = Foo {
14-
x: 42,
15-
y: 99,
16-
};
17-
let p = unsafe { &foo.x };
18-
let i = *p; //~ ERROR alignment 4 is required
13+
for _ in 0..10 { // Try many times as this might work by chance.
14+
let foo = Foo {
15+
x: 42,
16+
y: 99,
17+
};
18+
let p = unsafe { &foo.x };
19+
let i = *p; //~ ERROR alignment 4 is required
20+
}
1921
}

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)