about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/align.rs
blob: 2b6e83891d6eae684907f257158057ece130bdf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//@compile-flags: -Zmiri-permissive-provenance

/// This manually makes sure that we have a pointer with the proper alignment.
fn manual_alignment() {
    let x = &mut [0u8; 3];
    let base_addr = x as *mut _ as usize;
    let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr + 1 };
    let u16_ptr = base_addr_aligned as *mut u16;
    unsafe {
        *u16_ptr = 2;
    }
}

/// Test standard library `align_to`.
fn align_to() {
    const LEN: usize = 128;
    let buf = &[0u8; LEN];
    let (l, m, r) = unsafe { buf.align_to::<i32>() };
    assert!(m.len() * 4 >= LEN - 4);
    assert!(l.len() + r.len() <= 4);
}

fn main() {
    // Do this a couple times in a loop because it may work "by chance".
    for _ in 0..20 {
        manual_alignment();
        align_to();
    }
}