Raw Pointers, Pointer Provenance & Strict Aliasing
What Unsafe Rust Actually Does
A common misconception is that writing unsafe turns off the Rust compiler. In reality, unsafe simply gives the software engineer access to five superpowers:
- Dereferencing raw pointers (
*const T,*mut T) - Calling unsafe functions or foreign FFI exports (e.g. CUDA Driver API)
- Implementing unsafe traits (e.g.
SendandSync) - Mutating mutable static variables or accessing
unionfields - Accessing fields of
#[repr(packed)]structs
Pointer Provenance & Stacked Borrows
Modern compilers do not treat memory pointers as raw integers. Every pointer carries invisible provenance: metadata tracking which memory allocation it originated from and what borrowing permissions it possesses. Violating the Stacked Borrows or Tree Borrows model triggers instant UB:
// UNSOUND EXAMPLE: Violating aliasing rules through integer casting
let mut val: u32 = 10;
let ptr = &mut val as *mut u32;
// Deriving a shared reference
let ref_val = &val;
// Writing through ptr while ref_val is live violates the Stacked Borrows stack!
unsafe { *ptr = 20; }
println!("{}", ref_val); // UB: ref_val was invalidated by write through ptr!
Validating Invariants with Miri
Miri is an interpreter that executes Rust intermediate representation (MIR) and validates every memory access against the Stacked Borrows model. Running cargo miri test detects aliasing violations, memory leaks, and unaligned reads before code ever touches production.
Run Miri interpreter with Stacked Borrows validation to catch pointer provenance violations and out-of-bounds access.