This document defines the policy governing all use of unsafe Rust in VeridianOS.
Unsafe code is treated as a controlled architectural mechanism, not a convenience.
Any violation of this policy renders the system architecturally incorrect, regardless of observed behavior.
Unsafe Rust exists in VeridianOS only to enforce higher-level safety, isolation, and correctness invariants that cannot be expressed in safe Rust alone.
Unsafe code is never used:
- for convenience
- for stylistic preference
- for premature optimization
- to bypass architectural constraints
Unsafe code is permitted only in the following contexts:
- Hardware interaction (e.g., MMIO, CPU instructions)
- Low-level memory management required to enforce ownership and isolation
- Boot and early initialization code
- Mechanisms that implement or enforce architectural invariants
All other code must be written in safe Rust.
- Unsafe blocks must be as small as possible
- Broad
unsafe fnorunsafe implusage is strongly discouraged - Large unsafe regions are considered design failures
Unsafe code must be isolated to clearly identified modules or files.
Unsafe behavior must not “leak” across module boundaries.
Every unsafe block must include a comment that explicitly states:
- Which architectural invariant(s) it upholds
- Why safe Rust is insufficient
- What assumptions are being relied upon
Example (illustrative):
// SAFETY:
// Upholds I-8 (Memory Ownership) by ensuring this mapping is created
// only by the kernel during early boot.
// Safe Rust cannot express this invariant due to raw pointer usage.
// Assumes identity-mapped physical memory during this phase.
unsafe {
...
}Undocumented unsafe code is incorrect by definition.
Unsafe code must explicitly reference the invariant(s) defined in docs/INVARIANTS.md that it supports.
If no invariant can be named, the unsafe code is not permitted.
Unsafe code is subject to stricter review than safe code.
Review must verify:
- correctness of assumptions
- completeness of documentation
- absence of alternative safe designs
- preservation of all referenced invariants
Unsafe code is assumed guilty until proven necessary.
The following patterns are explicitly disallowed:
- Unsafe code for performance alone
- Unsafe abstractions that hide unsafety behind “safe-looking” APIs
- Unsafe access justified by “this is internal” reasoning
- Unsafe code copied from external sources without re-verification
Unsafe code is expected to shrink over time, not grow.
As Rust evolves or better abstractions become available:
- unsafe code must be reevaluated
- safe replacements should be preferred
- invariants must remain preserved
Future enforcement mechanisms may include:
- linting rules that flag undocumented unsafe
- tests asserting invariant preservation
- static analysis or formal verification of unsafe regions
The architecture is designed to support these extensions.
Unsafe Rust in VeridianOS is:
- rare
- deliberate
- justified
- documented
- invariant-bound
Unsafe code exists to protect correctness, not to compromise it.
Any unsafe code that does not meet this policy is incorrect by definition.