about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/ptr_int_from_exposed.rs
blob: 98f8f15608e1db46d3cfd48261b3d751063fcb56 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//@compile-flags: -Zmiri-permissive-provenance

use std::ptr;

/// Ensure we can expose the address of a pointer that is out-of-bounds
fn ptr_roundtrip_out_of_bounds() {
    let x: i32 = 3;
    let x_ptr = &x as *const i32;

    let x_usize = x_ptr.wrapping_offset(128).expose_provenance();

    let ptr = ptr::with_exposed_provenance::<i32>(x_usize).wrapping_offset(-128);
    assert_eq!(unsafe { *ptr }, 3);
}

/// Ensure that we can move between allocations after casting back to a ptr
fn ptr_roundtrip_confusion() {
    let x: i32 = 0;
    let y: i32 = 1;

    let x_ptr = &x as *const i32;
    let y_ptr = &y as *const i32;

    let x_usize = x_ptr.expose_provenance();
    let y_usize = y_ptr.expose_provenance();

    let ptr = ptr::with_exposed_provenance::<i32>(y_usize);
    let ptr = ptr.with_addr(x_usize);
    assert_eq!(unsafe { *ptr }, 0);
}

/// Ensure we can cast back a different integer than the one we got when exposing.
fn ptr_roundtrip_imperfect() {
    let x: u8 = 3;
    let x_ptr = &x as *const u8;

    let x_usize = x_ptr.expose_provenance() + 128;

    let ptr = ptr::with_exposed_provenance::<u8>(x_usize).wrapping_offset(-128);
    assert_eq!(unsafe { *ptr }, 3);
}

/// Ensure that we can roundtrip through a pointer with an address of 0
fn ptr_roundtrip_null() {
    let x = &42;
    let x_ptr = x as *const i32;
    let x_null_ptr = x_ptr.with_addr(0); // addr 0, but still the provenance of x
    let null = x_null_ptr.expose_provenance();
    assert_eq!(null, 0);

    let x_null_ptr_copy = ptr::with_exposed_provenance::<i32>(null); // just a roundtrip, so has provenance of x (angelically)
    let x_ptr_copy = x_null_ptr_copy.with_addr(x_ptr.addr()); // addr of x and provenance of x
    assert_eq!(unsafe { *x_ptr_copy }, 42);
}

fn ptr_roundtrip_offset_from() {
    let arr = [0; 5];
    let begin = arr.as_ptr();
    let end = begin.wrapping_add(arr.len());
    let end_roundtrip = ptr::with_exposed_provenance::<i32>(end.expose_provenance());
    unsafe { end_roundtrip.offset_from(begin) };
}

fn main() {
    ptr_roundtrip_out_of_bounds();
    ptr_roundtrip_confusion();
    ptr_roundtrip_imperfect();
    ptr_roundtrip_null();
    ptr_roundtrip_offset_from();
}