Skip to content

Commit 4d65aa8

Browse files
committed
expand and better explain alignment check tests
1 parent 69e8318 commit 4d65aa8

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

tests/compile-fail/unaligned_ptr_cast1.rs renamed to tests/compile-fail/unaligned_ptr1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
fn main() {
55
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
66
let x = &x[0] as *const _ as *const u32;
7-
// This must fail because alignment is violated
7+
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
88
let _x = unsafe { *x }; //~ ERROR tried to access memory with alignment 2, but alignment 4 is required
99
}

tests/compile-fail/unaligned_ptr2.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This should fail even without validation.
2+
// compile-flags: -Zmiri-disable-validation
3+
4+
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 tried to access memory with alignment 1, but alignment 4 is required
10+
}

tests/compile-fail/unaligned_ptr_cast2.rs renamed to tests/compile-fail/unaligned_ptr3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
fn main() {
55
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;
6+
let x = &x[0] as *const _ as *const *const u8; // cast to ptr-to-ptr, so that we load a ptr
77
// This must fail because alignment is violated. Test specifically for loading pointers,
88
// which have special code in miri's memory.
99
let _x = unsafe { *x };

tests/compile-fail/unaligned_ptr_cast_zst.rs renamed to tests/compile-fail/unaligned_ptr_zst.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// This should fail even without validation
2+
// compile-flags: -Zmiri-disable-validation
3+
14
fn main() {
25
let x = &2u16;
36
let x = x as *const _ as *const [u32; 0];

0 commit comments

Comments
 (0)