]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass/align.rs
Auto merge of #104915 - weihanglo:update-cargo, r=ehuss
[rust.git] / src / tools / miri / tests / pass / align.rs
1 //@compile-flags: -Zmiri-permissive-provenance
2
3 /// This manually makes sure that we have a pointer with the proper alignment.
4 fn manual_alignment() {
5     let x = &mut [0u8; 3];
6     let base_addr = x as *mut _ as usize;
7     let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr + 1 };
8     let u16_ptr = base_addr_aligned as *mut u16;
9     unsafe {
10         *u16_ptr = 2;
11     }
12 }
13
14 /// Test standard library `align_to`.
15 fn align_to() {
16     const LEN: usize = 128;
17     let buf = &[0u8; LEN];
18     let (l, m, r) = unsafe { buf.align_to::<i32>() };
19     assert!(m.len() * 4 >= LEN - 4);
20     assert!(l.len() + r.len() <= 4);
21 }
22
23 fn main() {
24     // Do this a couple times in a loop because it may work "by chance".
25     for _ in 0..20 {
26         manual_alignment();
27         align_to();
28     }
29 }