A MISRA-style standard for unsafe Rust · Example rule
UR-09.3

Volatile access to
memory-mapped hardware

One example where unsafe Rust behaves exactly like C: the optimizer may legally delete your hardware-polling loop, and nothing in the compiler stops you.

Why a formal, statically enforced rule is the only defense.

The scenario

Polling a status register through a raw pointer

Direct MMIO dereference is undefined-adjacent territory, so the developer wraps it in unsafe. Compiles clean. Zero warnings.

const STATUS_REG: *const u32 = 0x4000_0000 as *const u32; fn wait_until_ready() { // spin until the hardware sets bit 0 while unsafe { STATUS_REG.read() } & 1 == 0 {} }

The read is a non-volatile read: from the optimizer's point of view it has no side effects.

The failure

Both miscompilations are legal for the optimizer

Hoisted read (loop-invariant code motion)

let cached = STATUS_REG.read(); while cached & 1 == 0 {} // spins forever

The register is read once, then the loop spins on the cached value regardless of hardware state.

Deleted loop (no side effects)

// entire loop removed: return; // falls straight through

An infinite loop with no side effects may be assumed unreachable and removed entirely.

Result: passes host unit tests, then hangs or skips the wait on target, depending on the optimization level.

Why this is legal

The optimizer's contract, inherited from C

STATUS_REG.read() // non-volatile: optimizer may cache, hoist, delete STATUS_REG.read_volatile() // volatile: must execute on every iteration

Two correct-looking one-liners inside unsafe. The wrong one is a latent field failure.

The fix

One method name separates correct from broken

fn wait_until_ready() { while unsafe { STATUS_REG.read_volatile() } & 1 == 0 {} }
The MISRA-style rule

UR-09.3, structured like MISRA C Mandatory

AspectRequirement
RuleA memory-mapped hardware location shall only be accessed through volatile operations.
CategoryMandatory: cannot be permanently waived by team signoff.
RationaleNon-volatile accesses to hardware addresses may be optimized away, hoisted, or cached, producing behavior divergent from the hardware contract.
DecidabilityFully statically decidable.
EnforcementAny pointer whose provenance includes an integer-to-pointer cast (as *const / as *mut) shall be dereferenced only via read_volatile/write_volatile-class intrinsics. Direct deref of such pointers is a violation, rejected at build time by a CI lint.
DeviationPermitted only for RAM-backed buffers provably not device memory, documented per deviation request.
Takeaway

Why a standard, not good intentions

What the language gives you

unsafe means "the programmer has manually verified the contract". It marks responsibility; it does not transfer knowledge.

What the standard gives you

A mandatory rule with a syntactic fingerprint (integer-to-pointer cast feeding a non-volatile deref) that a static check rejects before the build.

The volatile contract is invisible in the code. Only a written, enforced rule can make it visible.

This is why embedded C teams adopted MISRA, and why an unsafe-Rust standard needs the same structure: mandatory rules, static enforcement, formal deviation process.

MISRA for Unsafe Rust · Rule UR-09.3 example
1 / 7
← Swipe to navigate →