← [ ABORT TO HUD ]
SEQ. 1
SEQ. 2

Raw Pointers, Pointer Provenance & Strict Aliasing

Unsafe Rust, Miri & Soundness Invariants18 min140 BASE XP⌨ HANDS-ON LAB

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:

  1. Dereferencing raw pointers (*const T, *mut T)
  2. Calling unsafe functions or foreign FFI exports (e.g. CUDA Driver API)
  3. Implementing unsafe traits (e.g. Send and Sync)
  4. Mutating mutable static variables or accessing union fields
  5. Accessing fields of #[repr(packed)] structs
Undefined Behavior (UB) is Absolute: If code triggers UB, the compiler is legally permitted under the ISO/LLVM spec to reorder instructions, delete branches, or execute arbitrary operations. Sound unsafe code must uphold safety invariants under ALL conceivable inputs.

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.

⌨ HANDS-ON LABAudit Pointer Aliasing & Undefined Behavior with Miri
⭐ +160 XP

Run Miri interpreter with Stacked Borrows validation to catch pointer provenance violations and out-of-bounds access.

1Run Miri test suite to verify Stacked Borrows compliance across raw pointer operations.
2Check strict provenance lints to eliminate loose integer-to-pointer casts.
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 2 — type "hint" if stuck
SYNAPSE VERIFICATION
QUERY 1 // 1
What does Miri detect that the standard rustc compiler cannot detect alone?
Undefined Behavior in unsafe code, including aliasing violations, use-after-free, and unaligned pointer reads
Syntax errors in declarative macro patterns
Missing documentation comments on public trait methods
Unused imports and dead code warnings