about summary refs log tree commit diff
path: root/src/tools/miri/tests/fail/tree_borrows/outside-range.rs
blob: 8450e1c168d9a86dbe5f9003d4d1a81fd185a623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//@compile-flags: -Zmiri-tree-borrows

// Check that in the case of locations outside the range of a pointer,
// protectors trigger if and only if the location has already been accessed
fn main() {
    unsafe {
        let data = &mut [0u8, 1, 2, 3];
        let raw = data.as_mut_ptr();
        stuff(&mut *raw, raw);
    }
}

unsafe fn stuff(x: &mut u8, y: *mut u8) {
    let xraw = x as *mut u8;
    // No issue here: location 1 is not accessed
    *y.add(1) = 42;
    // Still no issue: location 2 is not invalidated
    let _val = *xraw.add(2);
    // However protector triggers if location is both accessed and invalidated
    let _val = *xraw.add(3);
    *y.add(3) = 42; //~ ERROR: /write access through .* is forbidden/
}