Skip to content

Commit

Permalink
make sure we catch alignment problems even with intrptrcast
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jun 29, 2019
1 parent c1645f6 commit 0bb50ad
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/compile-fail/intptrcast_alignment_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Validation makes this fail in the wrong place
// compile-flags: -Zmiri-disable-validation -Zmiri-seed=0000000000000000

// Even with intptrcast and without validation, we want to be *sure* to catch bugs
// that arise from pointers being insufficiently aligned. The only way to achieve
// that is not not let programs exploit integer information for alignment, so here
// we test that this is indeed the case.
fn main() {
let x = &mut [0u8; 3];
let base_addr = x as *mut _ as usize;
let u16_ref = unsafe { if base_addr % 2 == 0 {
&mut *(base_addr as *mut u16)
} else {
&mut *((base_addr+1) as *mut u16)
} };
*u16_ref = 2; //~ ERROR tried to access memory with alignment 1, but alignment 2 is required
println!("{:?}", x);
}

0 comments on commit 0bb50ad

Please sign in to comment.