Unsafe Rust Authoring and Audit
Treat each safety contract as an English-language theorem and each safety
comment as its proof. Reject hand-waving, folklore, hidden assumptions, and
proof by testing.
Establish the Exact Claim
Unless the user specifies a narrower claim, establish:
For the exact audited source snapshot, every supported compilation
configuration, every valid in-scope use in a context satisfying all
out-of-scope safety obligations preserves freedom from Rust undefined behavior
under the documented Rust abstract semantics, and every mandatory in-scope
documented postcondition holds, assuming only the explicitly recorded trusted
computing base (TCB).
Interpret valid use as follows:
- For a safe API, quantify over every well-typed safe use. Impose no hidden
safety precondition.
- For an unsafe API, quantify over every use satisfying all documented initial,
ongoing, and terminal safety obligations.
- For a binary or other entrypoint, quantify over executions satisfying the
explicitly recorded deployment assumptions. Do not transfer those
assumptions silently to a safe library API.
Prove every documented postcondition of each unsafe API in scope and every
documented guarantee consumed by an in-scope soundness proof. Include broader
safe-API robustness only when the user or audit scope requests it.
Prove source-level Rust soundness first. State claims about a particular
compiler backend, binary, platform, security property, probability, or
deployment separately with their additional premises.
Use Only Applicable Premises
- Bottom out Rust-language and standard-library facts in exact applicable text
from versioned Rust Reference or standard-library documentation.
- Quote and link the smallest sufficient set of passages whose propositions,
together with justified inference steps, entail the fact. Open each citation
and verify its wording, qualifications, version, and scope.
- Attach an applicability domain to every claim and premise, whether stated
locally or inherited from an identified project policy or canonical entry. A
derivation proves only the cases covered by all premises it consumes.
- Apply a guarantee documented for an older Rust release to a later stable
release only when an exact applicable Rust backwards-compatibility
commitment preserves that exact proposition throughout the later release's
relevant domain. An API's stability badge does not by itself preserve every
behavioral statement in its current documentation. Record a
non-authoritative compatibility premise explicitly in the TCB. Never infer
an earlier-version guarantee merely from later documentation.
- Do not promote this skill, the Rustonomicon, Unsafe Code Guidelines, RFCs,
blogs, issue discussions, implementation behavior, Miri, or common practice
to Rust axioms. Use them to discover risks and authoritative text, or record
the exact additional proposition as a TCB assumption.
- Trust a deliberately selected safe dependency API to behave as documented
only when that exact trust is explicit in the TCB. Do not extend this
exception to caller-controlled safe code, callbacks, values, or safe trait
implementations.
- Audit a third-party unsafe API through to admissible premises or record its
exact implementation and contract as an additional TCB assumption.
When no admissible direct or derived proof can be completed because
authoritative documentation is ambiguous or insufficient, identify the
smallest missing proposition. Do not repair it with intuition. Report a
documentation gap and suggest an upstream improvement when appropriate.
Compose Proofs Locally and Literally
- Identify the controlling contract independently of the existing safety
comment. Distinguish normative contract text from examples, rationale,
implementation comments, and inferred design intent.
- Read the controlling contract according to its actual text. Decompose every
applicable conjunction, implication, quantifier, temporal clause,
precondition, and postcondition into separately reviewable obligations. Do
not replace a literal requirement with an operationally similar property.
Give every normative clause a disposition even when no known consumer uses
it.
- Reify every fact used nonlocally as a named contract or invariant carried by a
type, field, function boundary, guard, typestate, lock, token, or other
locally checkable mechanism. A function contract about global state is an
acceptable degenerate case.
- Prove that each state transition establishes, preserves, transfers,
deliberately suspends under an explicit obligation, or discharges every
applicable invariant. At each consumer, prove that the current invariant
entails the exact needed precondition.
- Trace dataflow across calls and time rather than limiting review to lexical
unsafe blocks. Account for every producer, transition, and consumer.
- For new code, place invariant-bearing representation in the smallest
practical leaf module, keep safely accessible representation fields private
to it, and treat safe code outside that module—including the rest of the same
crate—as untrusted.
Follow the Proof Workflow
- Frame the claim. Record the artifact identity, exact scope, valid uses or
executions, Rust and dependency versions, supported configuration set,
mandatory postconditions, TCB, and exclusions.
- Inventory the surface. Enumerate every in-scope safe and unsafe API
surface, obligation site, invariant producer/transition/consumer, and
generated or expanded artifact across the supported set.
- State every obligation. Obtain each controlling contract, decompose it
literally, and state the exact proposition and applicability to prove.
- Construct the derivation. Derive every conjunct from checked local facts,
named invariants, applicable authoritative axioms, tool-derived theorems, or
explicit TCB entries. Unfold definitions and seek indirect multi-premise
derivations; absence of one direct sentence is not itself a documentation
gap. Justify every intermediate inference.
- Close composition. Ensure every literal contract clause and safe surface
has a disposition, every premise consumed by unsafe code has an admissible
source, and every supported configuration region is proved by an abstract
argument or exhaustive partition. Try to falsify the contract reading,
inference chain, and coverage before concluding
PROVED.
- Report exactly. Keep unresolved obligations visible and state the
smallest missing implication. Record proofs, TCB, coverage, findings,
postcondition failures, documentation gaps, and residual scope without
optimism.
Do not require a concrete UB counterexample to reject an incomplete proof. A
missing, ambiguous, circular, or inapplicable derivation is sufficient for
UNPROVED.
Write and Review Proof-Grade Documentation
Read proof-obligations.md before authoring or
reviewing an unsafe contract, invariant, SAFETY comment, or local proof.
Keep each proof adjacent to the smallest cohesive unsafe operation or assertion.
State the exact operation and its preconditions, cite checked facts and named
invariants, show the derivation, and prove resulting postconditions and
invariant state on every applicable exit.
When existing code can be validated only by reconstructing a material
derivation absent from its safety comment, do not accept it silently. Include
the reconstructed derivation—or the smallest missing portion—in the review,
with its citations and applicability. Classify implementation correctness
separately from proof-documentation quality. If changes are authorized, improve
the adjacent proof; otherwise provide proposed wording. Do not use a
reconstructed implementation proof to invent or strengthen a caller-facing
contract retroactively.
Close API and Configuration Boundaries
Read
api-boundaries-and-evolution.md
for fields, constructors, methods, traits, sealing, macros, public or hidden
APIs, robustness, or contract evolution.
Apply this mandatory safe-surface checklist: public fields, constructors, safe
methods, safe trait methods, and macro-generated APIs all count as safe API
surfaces. Include language-reachable #[doc(hidden)] safe items for soundness
even when excluded from documentation or compatibility promises.
Treat caller-provided safe code as adversarial within the behaviors permitted
by safe Rust and its types. Seal a trait or make it unsafe when soundness
requires an unenforced implementer behavior.
Read
configurations-and-generated-code.md
for every full audit and whenever conditional compilation, targets, generated
code, FFI, assembly, SIMD, allocators, linking, or build tooling is relevant.
Every supported combination of compilation options that can ship downstream
must be sound. Use parametric proofs or exhaustive partitions when literal
enumeration would explode; do not substitute a tested sample.
Evaluate Trust and Evidence
Read tcb-and-evidence.md for every full audit
and whenever a proof uses dependencies, external specifications, tools,
testing, formal verification, environmental restrictions, or cryptographic or
probabilistic assumptions.
Judge evidence by the exact proposition it establishes, its artifact and model,
its quantified domain and bounds, its premises, and its residual trust—not by a
label such as testing, static analysis, model checking, or formal verification.
Use Exact Verdicts
Read audit-reporting.md before delivering a
persistent or full audit.
- PROVED: Every obligation for the exact named claim is discharged over its
complete applicability, relative to the stated TCB.
- UNPROVED: At least one required derivation, premise, applicability or
coverage argument, postcondition proof, or citation is missing, ambiguous,
circular, or unverifiable.
- UNSOUND: A valid use or in-scope execution is proved to reach undefined
behavior.
- CONTRACT-BROKEN: A documented postcondition is proved false even though
undefined behavior need not occur.
Apply verdicts separately to soundness, documented postconditions, and
conditional application claims. State exact scope, applicability, and TCB
beside every verdict. Never substitute “looks sound,” “probably sound,” or test
success.
For a persistent audit, complete:
- tcb-audit-log-template.md
- unsafe-code-audit-report-template.md
For an inline review, provide the equivalent material compactly. Reuse an
existing canonical project log rather than creating a competing trust model.
1---2name: unsafe-rust-53description: Author, document, review, or audit unsafe Rust with proof-grade rigor. Use for unsafe blocks and functions, unsafe traits and impls, raw pointers, FFI, inline assembly, intrinsics, layout or validity reasoning, concurrency and atomics, SIMD and target features, allocators, invariant-bearing fields, safety comments or `# Safety` documentation, soundness reviews, TCB audits, generated unsafe code, and changes to safety or behavioral contracts.4---56# Unsafe Rust Authoring and Audit78Treat each safety contract as an English-language theorem and each safety9comment as its proof. Reject hand-waving, folklore, hidden assumptions, and10proof by testing.1112## Establish the Exact Claim1314Unless the user specifies a narrower claim, establish:1516> For the exact audited source snapshot, every supported compilation17> configuration, every valid in-scope use in a context satisfying all18> out-of-scope safety obligations preserves freedom from Rust undefined behavior19> under the documented Rust abstract semantics, and every mandatory in-scope20> documented postcondition holds, assuming only the explicitly recorded trusted21> computing base (TCB).2223Interpret valid use as follows:2425- For a safe API, quantify over every well-typed safe use. Impose no hidden26 safety precondition.27- For an unsafe API, quantify over every use satisfying all documented initial,28 ongoing, and terminal safety obligations.29- For a binary or other entrypoint, quantify over executions satisfying the30 explicitly recorded deployment assumptions. Do not transfer those31 assumptions silently to a safe library API.3233Prove every documented postcondition of each unsafe API in scope and every34documented guarantee consumed by an in-scope soundness proof. Include broader35safe-API robustness only when the user or audit scope requests it.3637Prove source-level Rust soundness first. State claims about a particular38compiler backend, binary, platform, security property, probability, or39deployment separately with their additional premises.4041## Use Only Applicable Premises4243- Bottom out Rust-language and standard-library facts in exact applicable text44 from versioned Rust Reference or standard-library documentation.45- Quote and link the smallest sufficient set of passages whose propositions,46 together with justified inference steps, entail the fact. Open each citation47 and verify its wording, qualifications, version, and scope.48- Attach an applicability domain to every claim and premise, whether stated49 locally or inherited from an identified project policy or canonical entry. A50 derivation proves only the cases covered by all premises it consumes.51- Apply a guarantee documented for an older Rust release to a later stable52 release only when an exact applicable Rust backwards-compatibility53 commitment preserves that exact proposition throughout the later release's54 relevant domain. An API's stability badge does not by itself preserve every55 behavioral statement in its current documentation. Record a56 non-authoritative compatibility premise explicitly in the TCB. Never infer57 an earlier-version guarantee merely from later documentation.58- Do not promote this skill, the Rustonomicon, Unsafe Code Guidelines, RFCs,59 blogs, issue discussions, implementation behavior, Miri, or common practice60 to Rust axioms. Use them to discover risks and authoritative text, or record61 the exact additional proposition as a TCB assumption.62- Trust a deliberately selected safe dependency API to behave as documented63 only when that exact trust is explicit in the TCB. Do not extend this64 exception to caller-controlled safe code, callbacks, values, or safe trait65 implementations.66- Audit a third-party unsafe API through to admissible premises or record its67 exact implementation and contract as an additional TCB assumption.6869When no admissible direct or derived proof can be completed because70authoritative documentation is ambiguous or insufficient, identify the71smallest missing proposition. Do not repair it with intuition. Report a72documentation gap and suggest an upstream improvement when appropriate.7374## Compose Proofs Locally and Literally7576- Identify the controlling contract independently of the existing safety77 comment. Distinguish normative contract text from examples, rationale,78 implementation comments, and inferred design intent.79- Read the controlling contract according to its actual text. Decompose every80 applicable conjunction, implication, quantifier, temporal clause,81 precondition, and postcondition into separately reviewable obligations. Do82 not replace a literal requirement with an operationally similar property.83 Give every normative clause a disposition even when no known consumer uses84 it.85- Reify every fact used nonlocally as a named contract or invariant carried by a86 type, field, function boundary, guard, typestate, lock, token, or other87 locally checkable mechanism. A function contract about global state is an88 acceptable degenerate case.89- Prove that each state transition establishes, preserves, transfers,90 deliberately suspends under an explicit obligation, or discharges every91 applicable invariant. At each consumer, prove that the current invariant92 entails the exact needed precondition.93- Trace dataflow across calls and time rather than limiting review to lexical94 unsafe blocks. Account for every producer, transition, and consumer.95- For new code, place invariant-bearing representation in the smallest96 practical leaf module, keep safely accessible representation fields private97 to it, and treat safe code outside that module—including the rest of the same98 crate—as untrusted.99100## Follow the Proof Workflow1011021. **Frame the claim.** Record the artifact identity, exact scope, valid uses or103 executions, Rust and dependency versions, supported configuration set,104 mandatory postconditions, TCB, and exclusions.1052. **Inventory the surface.** Enumerate every in-scope safe and unsafe API106 surface, obligation site, invariant producer/transition/consumer, and107 generated or expanded artifact across the supported set.1083. **State every obligation.** Obtain each controlling contract, decompose it109 literally, and state the exact proposition and applicability to prove.1104. **Construct the derivation.** Derive every conjunct from checked local facts,111 named invariants, applicable authoritative axioms, tool-derived theorems, or112 explicit TCB entries. Unfold definitions and seek indirect multi-premise113 derivations; absence of one direct sentence is not itself a documentation114 gap. Justify every intermediate inference.1155. **Close composition.** Ensure every literal contract clause and safe surface116 has a disposition, every premise consumed by unsafe code has an admissible117 source, and every supported configuration region is proved by an abstract118 argument or exhaustive partition. Try to falsify the contract reading,119 inference chain, and coverage before concluding `PROVED`.1206. **Report exactly.** Keep unresolved obligations visible and state the121 smallest missing implication. Record proofs, TCB, coverage, findings,122 postcondition failures, documentation gaps, and residual scope without123 optimism.124125Do not require a concrete UB counterexample to reject an incomplete proof. A126missing, ambiguous, circular, or inapplicable derivation is sufficient for127`UNPROVED`.128129## Write and Review Proof-Grade Documentation130131Read [proof-obligations.md](references/proof-obligations.md) before authoring or132reviewing an unsafe contract, invariant, `SAFETY` comment, or local proof.133134Keep each proof adjacent to the smallest cohesive unsafe operation or assertion.135State the exact operation and its preconditions, cite checked facts and named136invariants, show the derivation, and prove resulting postconditions and137invariant state on every applicable exit.138139When existing code can be validated only by reconstructing a material140derivation absent from its safety comment, do not accept it silently. Include141the reconstructed derivation—or the smallest missing portion—in the review,142with its citations and applicability. Classify implementation correctness143separately from proof-documentation quality. If changes are authorized, improve144the adjacent proof; otherwise provide proposed wording. Do not use a145reconstructed implementation proof to invent or strengthen a caller-facing146contract retroactively.147148## Close API and Configuration Boundaries149150Read151[api-boundaries-and-evolution.md](references/api-boundaries-and-evolution.md)152for fields, constructors, methods, traits, sealing, macros, public or hidden153APIs, robustness, or contract evolution.154155Apply this mandatory safe-surface checklist: public fields, constructors, safe156methods, safe trait methods, and macro-generated APIs all count as safe API157surfaces. Include language-reachable `#[doc(hidden)]` safe items for soundness158even when excluded from documentation or compatibility promises.159160Treat caller-provided safe code as adversarial within the behaviors permitted161by safe Rust and its types. Seal a trait or make it unsafe when soundness162requires an unenforced implementer behavior.163164Read165[configurations-and-generated-code.md](references/configurations-and-generated-code.md)166for every full audit and whenever conditional compilation, targets, generated167code, FFI, assembly, SIMD, allocators, linking, or build tooling is relevant.168Every supported combination of compilation options that can ship downstream169must be sound. Use parametric proofs or exhaustive partitions when literal170enumeration would explode; do not substitute a tested sample.171172## Evaluate Trust and Evidence173174Read [tcb-and-evidence.md](references/tcb-and-evidence.md) for every full audit175and whenever a proof uses dependencies, external specifications, tools,176testing, formal verification, environmental restrictions, or cryptographic or177probabilistic assumptions.178179Judge evidence by the exact proposition it establishes, its artifact and model,180its quantified domain and bounds, its premises, and its residual trust—not by a181label such as testing, static analysis, model checking, or formal verification.182183## Use Exact Verdicts184185Read [audit-reporting.md](references/audit-reporting.md) before delivering a186persistent or full audit.187188- **PROVED:** Every obligation for the exact named claim is discharged over its189 complete applicability, relative to the stated TCB.190- **UNPROVED:** At least one required derivation, premise, applicability or191 coverage argument, postcondition proof, or citation is missing, ambiguous,192 circular, or unverifiable.193- **UNSOUND:** A valid use or in-scope execution is proved to reach undefined194 behavior.195- **CONTRACT-BROKEN:** A documented postcondition is proved false even though196 undefined behavior need not occur.197198Apply verdicts separately to soundness, documented postconditions, and199conditional application claims. State exact scope, applicability, and TCB200beside every verdict. Never substitute “looks sound,” “probably sound,” or test201success.202203For a persistent audit, complete:204205- [tcb-audit-log-template.md](assets/tcb-audit-log-template.md)206- [unsafe-code-audit-report-template.md](assets/unsafe-code-audit-report-template.md)207208For an inline review, provide the equivalent material compactly. Reuse an209existing canonical project log rather than creating a competing trust model.