]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/intptrcast_alignment_check.rs
Auto merge of #852 - lzutao:rustup, r=RalfJung
[rust.git] / tests / compile-fail / intptrcast_alignment_check.rs
1 // Validation makes this fail in the wrong place
2 // compile-flags: -Zmiri-disable-validation
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 }