Safe Rust promises no undefined behavior (UB). Unsafe code must keep that promise by hand, or the whole program loses it.
Undefined behavior to know
- Use-after-free and dangling references
- Data races
- Out-of-bounds reads and writes
- Producing an invalid value (e.g. a
boolthat isn't 0 or 1, a null reference) - Breaking aliasing rules (a
&mut Tthat isn't unique)
Tools
- Miri: runs tests in an interpreter that detects many kinds of UB (
cargo +nightly miri test) - Sanitizers: AddressSanitizer, ThreadSanitizer (nightly
-Zsanitizer)
To learn
- Stacked Borrows / Tree Borrows (the aliasing models Miri checks)
- Common UB patterns in real crates