Week 13 - Unsafe Rust: Raw Pointers, NonNull, MaybeUninit, UB¶
13.1 Conceptual Core¶
unsafeis not "turn off the borrow checker." It unlocks five extra capabilities:- Dereference raw pointers.
- Call
unsafe fns. - Implement
unsafe traits. - Access
static mut(now discouraged in favor ofSyncUnsafeCell). - Access fields of
unions. - The borrow checker, lifetime checker, type checker-all still run.
unsafewidens ability, it does not weaken checks. - The discipline is safety contracts: every
unsafe fnand everyunsafe { ... }block must be paired with a comment articulating the invariants the caller is asserting. The community standard is// SAFETY: ...comments, scanned byclippy::undocumented_unsafe_blocks.
13.2 Mechanical Detail¶
*const Tvs*mut T-the variance differs (*mut Tis invariant inT), the legality of forming references differs (you may not produce&Tfrom a*const Taliasing a&mut T), but otherwise they behave the same. The mut/const distinction is documentation, not enforcement.NonNull<T>-a wrapper around*mut Twith the niche optimization (the null bit pattern is forbidden). Use it for FFI handles and as the storage primitive inBox/Rc/Arc.MaybeUninit<T>-the way to manipulate uninitialized memory legally.mem::uninitializedis deprecated for soundness reasons;MaybeUninitis the replacement. InternalizeMaybeUninit::write,assume_init,assume_init_ref.- Provenance: a pointer carries not just an address but a provenance tag indicating which allocation it derives from.
ptr::with_addrandptr::map_addrare the safe ways to manipulate addresses without losing provenance. Read the strict-provenance proposal (std::ptrmodule docs). - The Rustonomicon's UB list: dangling references, null references, misaligned references, mutable aliasing, type confusion (transmuting padding), data races. Memorize.
13.3 Lab-"A Sound Vec"¶
Re-implement Vec<T> from scratch (the Nomicon's chapter 9 walk-through is the reference). Requirements:
- RawVec allocator wrapper handling growth.
- ZST (zero-sized type) handling-Vec<()> must work without ever allocating.
- Drop correct under panic in T::drop.
- Iteration via IntoIter with proper drop on partial consumption.
- Pass Miri on every public method.
13.4 Idiomatic & Clippy Drill¶
clippy::undocumented_unsafe_blocks,clippy::multiple_unsafe_ops_per_block,clippy::transmute_ptr_to_ref,clippy::cast_ptr_alignment. Each maps to a documented UB class.
13.5 Production Hardening Slice¶
- Run your
Veclab undercargo +nightly miri test -Zmiri-strict-provenance. Document each Miri diagnostic and the fix in aSAFETY_LOG.md. This document is the deliverable, not the code.