1 //@compile-flags: -Zmiri-permissive-provenance
2 #![feature(strict_provenance)]
6 /// Ensure we can expose the address of a pointer that is out-of-bounds
7 fn ptr_roundtrip_out_of_bounds() {
9 let x_ptr = &x as *const i32;
11 let x_usize = x_ptr.wrapping_offset(128).expose_addr();
13 let ptr = ptr::from_exposed_addr::<i32>(x_usize).wrapping_offset(-128);
14 assert_eq!(unsafe { *ptr }, 3);
17 /// Ensure that we can move between allocations after casting back to a ptr
18 fn ptr_roundtrip_confusion() {
22 let x_ptr = &x as *const i32;
23 let y_ptr = &y as *const i32;
25 let x_usize = x_ptr.expose_addr();
26 let y_usize = y_ptr.expose_addr();
28 let ptr = ptr::from_exposed_addr::<i32>(y_usize);
29 let ptr = ptr.with_addr(x_usize);
30 assert_eq!(unsafe { *ptr }, 0);
33 /// Ensure we can cast back a different integer than the one we got when exposing.
34 fn ptr_roundtrip_imperfect() {
36 let x_ptr = &x as *const u8;
38 let x_usize = x_ptr.expose_addr() + 128;
40 let ptr = ptr::from_exposed_addr::<u8>(x_usize).wrapping_offset(-128);
41 assert_eq!(unsafe { *ptr }, 3);
44 /// Ensure that we can roundtrip through a pointer with an address of 0
45 fn ptr_roundtrip_null() {
47 let x_ptr = x as *const i32;
48 let x_null_ptr = x_ptr.with_addr(0); // addr 0, but still the provenance of x
49 let null = x_null_ptr.expose_addr();
52 let x_null_ptr_copy = ptr::from_exposed_addr::<i32>(null); // just a roundtrip, so has provenance of x (angelically)
53 let x_ptr_copy = x_null_ptr_copy.with_addr(x_ptr.addr()); // addr of x and provenance of x
54 assert_eq!(unsafe { *x_ptr_copy }, 42);
58 ptr_roundtrip_out_of_bounds();
59 ptr_roundtrip_confusion();
60 ptr_roundtrip_imperfect();