Sub topic 9
Pointer provenance: why a pointer's origin matters
This chapter assumes you can read basic unsafe Rust, use raw pointers, and run cargo +nightly miri run as shown in earlier chapters.
A pointer is more than an address
On most hardware a pointer is just a number. Rust's abstract machine, which Miri implements, treats it as more. Every pointer also carries provenance, which records which allocation it was derived from and what access it was granted.
A pointer may only touch memory inside the allocation it came from. Having the right address is not enough. If two allocations sit next to each other in memory, a pointer from the first cannot be used to read the second, even when its numeric value equals the second one's start.
Compilers rely on this rule. If every pointer is tied to one allocation, the optimizer can assume that writing through a pointer from a cannot change b. Without provenance, it could assume almost nothing.
Seeing a provenance violation
This program builds a pointer to b by computing an offset from a:
fn main() {
let a = [1u8, 2, 3, 4];
let b = [5u8, 6, 7, 8];
let pa = a.as_ptr();
let pb = b.as_ptr();
let offset = (pb as usize).wrapping_sub(pa as usize);
let p = pa.wrapping_add(offset);
let v = unsafe { *p };
println!("{v}");
}Natively, this often prints 5, because p holds the address of b[0]. Miri reports an out-of-bounds access instead. p was derived from a, so it may only access a's four bytes, and the report names the allocation that is too small for the offset.
The fix is to derive the pointer from the allocation you want to access. Use pb for b, and keep arithmetic from pa inside a.
Pointers and integers
Casting a pointer to an integer discards nothing by itself, but the integer cannot carry provenance. Converting it back needs a rule. Rust offers two approaches:
| Approach | Methods | Behavior |
|---|---|---|
| Strict provenance | `addr`, `with_addr`, `map_addr` | The new pointer always inherits provenance from an existing pointer you name. |
| Exposed provenance | `expose_provenance`, `with_exposed_provenance` | Exposing a pointer marks its allocation as reachable by integer casts. A later integer-to-pointer conversion may pick it up. |
This round trip is valid because the pointer was exposed first:
fn main() {
let x = 5u32;
let addr = (&x as *const u32).expose_provenance();
let p = std::ptr::with_exposed_provenance::<u32>(addr);
println!("{}", unsafe { *p });
}Miri accepts integer-to-pointer casts by default, so it will not flag code that merely round-trips an address. To find code that depends on guessing provenance, run with MIRIFLAGS="-Zmiri-strict-provenance". Miri then rejects integer-to-pointer casts that have no pointer to inherit from.
Provenance and aliasing
Provenance also carries permissions. A pointer made from &mut x gets a different permission than one made from &x or from a raw pointer. The next chapter's aliasing models, Stacked Borrows and Tree Borrows, track those permissions. When a new reference invalidates an older pointer, the older pointer still has the right address but has lost its provenance for that access.
Exercise
In the first example, p has the same address as b.as_ptr(). Why does Miri still reject *p, and what is the smallest change that makes the read valid?
Show answer
p was derived from a.as_ptr(), so its provenance covers only a's four bytes, and address equality with b does not widen it. The read is valid if the pointer comes from b: read *pb instead of *p.