1 // compile-flags: -Zmiri-strict-provenance
2 #![feature(strict_provenance)]
6 // This is the example from
7 // <https://github.com/rust-lang/unsafe-code-guidelines/issues/286#issuecomment-1085144431>.
9 unsafe fn deref(left: *const u8, right: *const u8) {
10 let left_int: usize = mem::transmute(left); //~ERROR expected plain (non-pointer) bytes
11 let right_int: usize = mem::transmute(right);
12 if left_int == right_int {
13 // The compiler is allowed to replace `left_int` by `right_int` here...
14 let left_ptr: *const u8 = mem::transmute(left_int);
15 // ...which however means here it could be dereferencing the wrong pointer.
21 let ptr1 = &0u8 as *const u8;
22 let ptr2 = &1u8 as *const u8;
24 // Two pointers with the same address but different provenance.
25 deref(ptr1, ptr2.with_addr(ptr1.addr()));