]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/unaligned_pointers/intptrcast_alignment_check.rs
Rollup merge of #101717 - Pointerbender:unsafecell-memory-layout, r=Amanieu
[rust.git] / src / tools / miri / tests / fail / unaligned_pointers / intptrcast_alignment_check.rs
1 //@compile-flags: -Zmiri-symbolic-alignment-check -Zmiri-permissive-provenance
2 // With the symbolic alignment check, even with intptrcast and without
3 // validation, we want to be *sure* to catch bugs that arise from pointers being
4 // insufficiently aligned. The only way to achieve that is not not let programs
5 // exploit integer information for alignment, so here we test that this is
6 // indeed the case.
7 //
8 // See https://github.com/rust-lang/miri/issues/1074.
9 fn main() {
10     let x = &mut [0u8; 3];
11     let base_addr = x as *mut _ as usize;
12     // Manually make sure the pointer is properly aligned.
13     let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr + 1 };
14     let u16_ptr = base_addr_aligned as *mut u16;
15     unsafe { *u16_ptr = 2 }; //~ERROR: memory with alignment 1, but alignment 2 is required
16     println!("{:?}", x);
17 }