Skip to content

Commit 0bb50ad

Browse files
committed
make sure we catch alignment problems even with intrptrcast
1 parent c1645f6 commit 0bb50ad

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Validation makes this fail in the wrong place
2+
// compile-flags: -Zmiri-disable-validation -Zmiri-seed=0000000000000000
3+
4+
// Even with intptrcast and without validation, we want to be *sure* to catch bugs
5+
// that arise from pointers being insufficiently aligned. The only way to achieve
6+
// that is not not let programs exploit integer information for alignment, so here
7+
// we test that this is indeed the case.
8+
fn main() {
9+
let x = &mut [0u8; 3];
10+
let base_addr = x as *mut _ as usize;
11+
let u16_ref = unsafe { if base_addr % 2 == 0 {
12+
&mut *(base_addr as *mut u16)
13+
} else {
14+
&mut *((base_addr+1) as *mut u16)
15+
} };
16+
*u16_ref = 2; //~ ERROR tried to access memory with alignment 1, but alignment 2 is required
17+
println!("{:?}", x);
18+
}

0 commit comments

Comments
 (0)