Unsafe Code and Safety Invariants
CONCEPTS:Rust Unsafe Code
Question Variations
- "What does `unsafe` allow in Rust?"
- "Does an unsafe block turn off all compiler checks?"
- "What makes a safe wrapper around unsafe code sound?"
- "When should a function be marked `unsafe`?"
Why This Is Asked
unsafe is essential for some low-level Rust abstractions but shifts proof obligations from the compiler to the programmer. This question assesses whether you can keep unsafe code minimal, state its invariants, and expose a sound safe interface around it.
Key Concepts
- Unsafe blocks permit a limited set of unchecked operations.
- Unsafe operations include raw-pointer dereference and calling unsafe functions.
- Unsafe code must uphold Rust’s aliasing, validity, lifetime, and thread-safety invariants.
- A safe abstraction should encapsulate unsafe implementation details and document assumptions.
Question Variations
- “What does
unsafeallow in Rust?” - “Does an unsafe block turn off all compiler checks?”
- “What makes a safe wrapper around unsafe code sound?”
- “When should a function be marked
unsafe?”