google/rust-skills/unsafe-rust-review

Claude Code and agent skill by google/rust-skills

Trust tier 4
  • vendor
Source check pending

Checking that the SKILL.md file is still in the repository.

Write and review unsafe Rust `# Safety` documentation and safety comments (e.g., `// safety:` or `/// safety:` in any capitalization) as proof obligations grounded in the Rust Reference, standard library documentation, trusted opted-in dependency contracts, and explicit project invariants.

Install

npx vskill@latest install google/rust-skills/unsafe-rust-review

SKILL.md

The same SKILL.md was published earlier as google/rust-skills/unsafe-rust-review.

Unsafe Rust Safety Documentation and Commenting Skill

Mission

Act as an extremely strict unsafe Rust author and reviewer. Treat every # Safety section as an English-language theorem or lemma, and every safety comment (e.g., // safety: or /// safety: in any capitalization) as an English-language proof.

The goal is not reassuring prose. The goal is logic bulletproof-ness. A reviewer should be able to mechanically translate the prose into proof obligations and check each obligation against authoritative Rust axioms, documented dependency contracts, project-local invariants, type-system facts, and local code facts.

The governing standard is:

Every unsafe boundary creates proof obligations, and every unsafe operation
must locally prove that those obligations are discharged.

An unsafe fn or unsafe trait states extra conditions that callers or implementers must satisfy. An unsafe {} block, unsafe impl, or unsafe attribute use is the code author’s assertion that the relevant conditions have been satisfied at that exact program point.

Unsafe documentation and comments must therefore be written in the style of formal proof:

  • Define the proposition being proved.
  • Enumerate all required assumptions.
  • Classify each assumption by source.
  • Show that the assumptions imply the required unsafe contract.
  • State the postconditions and state transitions caused by the unsafe

operation.

  • Reject every implicit, circular, stale, unverifiable, or folklore premise.

Conventions

Every section carries a stable ` [area.slug] ID after its heading. Cite sections by ID, never by number or title: numbers shift when a list grows, and titles get reworded. Areas: model, evidence, fact, conv (conventions), doc (# Safety criteria), comment (comment criteria), auth (authoring guidance), topics, axiom, rej (reject patterns), dep (dependency rules), ref (reference creation), hazard` (high-risk hazard checklists).

Activation criteria

Use this skill whenever authoring or reviewing Rust code that contains, exposes, wraps, depends on, or reasons about any unsafe surface, including:

  • unsafe fn
  • unsafe trait
  • unsafe trait methods
  • unsafe impl
  • unsafe extern blocks or functions
  • unsafe attributes such as no_mangle, export_name, link_section, or

naked

  • unsafe {} blocks
  • raw pointer dereferences
  • calls to unsafe functions
  • accesses to union fields
  • accesses to mutable statics or extern statics
  • inline assembly
  • FFI boundaries
  • target_feature-sensitive code
  • manual allocation, deallocation, ownership reconstruction, or layout

manipulation

  • manual initialization or drop management
  • pin projection or pinned destruction logic
  • unsafe abstractions over safe callback, trait, iterator, comparator, or

closure inputs

  • safe API surfaces that can establish, expose, or break unsafe invariants,

including public fields, constructors, safe methods, safe trait methods, and macro-generated APIs

  • macro-generated or build-generated unsafe code or safe API surfaces
  • feature-gated, cfg-gated, target-specific, allocator-specific,

SIMD-specific, or debug/release-specific unsafe code paths

Use the strictest reasonable interpretation of all requirements. When style or review advice conflicts, choose the more demanding rule unless it conflicts with the Rust Reference or standard library documentation.

In review, findings come from [fact.classify], doc.*, comment.*, rej.*, dep.*, ref.*, and hazard.*. The remaining sections define the model and the conventions proofs cite, and auth.* says how to write well; a departure from auth.* is not a finding.

Core model [model]

Safety documentation is a theorem [model.theorem]

A # Safety section on an unsafe API states a theorem of the following shape:

text
If the caller or implementer satisfies preconditions P1 ... Pn,
then invoking or implementing this unsafe API preserves Rust soundness.

The documentation must say exactly who is responsible for the preconditions and how long each precondition must hold.

Safety comments are proofs [model.proof]

A safety comment (which may use // SAFETY:, // safety:, // Safety:, or their doc comment equivalents like /// safety:) immediately adjacent to an unsafe operation proves a theorem of the following shape:

text
The unsafe operation O requires obligations Q1 ... Qn.
At this program point, facts F1 ... Fm hold.
Facts F1 ... Fm imply Q1 ... Qn.
Therefore O's unsafe contract is discharged.
After O, state changes S1 ... Sk hold.

The proof must be local enough that a maintainer can audit it without searching through the entire program, except for explicitly named project-local invariants, dependency contracts, upstream # Safety preconditions, and the guarantees documented by the crate-controlled or trusted-dependency functions it calls or installs (an unsafe fn's # Safety postconditions, or a safe fn's # Safety-usable invariant; never the docs of caller-provided code).

Soundness target [model.soundness]

Unsafe code is sound only if safe Rust code using the abstraction cannot trigger undefined behavior without crossing an unsafe contract. The abstraction may have logic bugs, panic, leak memory, or produce incorrect values, but it must not allow safe callers to cause UB merely by using safe APIs in type-correct but adversarial ways.

Authority and evidence hierarchy [evidence]

Use this hierarchy when writing or reviewing safety documentation.

1. Core Rust axioms [evidence.axioms]

The only core axioms for Rust language and standard-library semantics are:

  1. The Rust Reference.
  2. The Rust standard library documentation.

All claims about undefined behavior, validity, aliasing, layout, references, pointer arithmetic, initialization, drop, standard-library unsafe contracts, and standard-library safe API semantics must bottom out in these sources.

Do not use folklore as an axiom. Do not rely on current compiler behavior, LLVM behavior, Miri behavior, blog posts, examples, or "how Rust usually works" unless the fact is entailed by the Rust Reference or standard library documentation.

2. Trusted opted-in dependency contracts [evidence.deps]

It is acceptable to assume that safe dependencies intentionally selected by the code author work as documented.

For example, if this crate intentionally depends on a library or uses a standard-library safe API, the safety proof may assume that the dependency's safe APIs perform their documented safe behavior. In particular, it is acceptable to rely on a dependency function such as Vec::sort actually performing the sorting operation documented by the standard library, subject to that API's own documented assumptions and inputs.

This rule is a trust-boundary rule, not a Rust-language axiom:

  • Treat non-std dependency behavior as a DEPENDENCY LEMMA, not as a core

Rust axiom.

  • Prefer pinned versions and exact documentation links when relying on non-std

dependency semantics.

  • Do not let dependency documentation invent Rust layout, aliasing, validity,

provenance, or UB rules that are not supported by the Reference or std docs.

  • Do not use a dependency lemma to discharge an unsafe contract unless the

dependency API actually guarantees the needed fact.

  • Do not silently rely on a dependency's undocumented behavior.
  • If the dependency API is unsafe, discharge its unsafe contract exactly like

any other unsafe API.

3. Caller-provided safe code is not trusted for semantic correctness [evidence.caller-code]

The "safe dependencies work as documented" assumption does not apply to caller-provided safe code.

Caller-provided safe code includes, without limitation:

  • safe function arguments
  • closures
  • callbacks
  • function pointers supplied by the caller
  • iterator implementations supplied by the caller
  • safe trait implementations supplied by the caller
  • implementations of Ord, Eq, Hash, Iterator, ExactSizeIterator,

Drop, Deref, Borrow, AsRef, Default, Clone, or other safe traits

  • values whose behavior is mediated by caller-controlled interior mutability
  • safe methods reachable through generic type parameters
  • safe code transitively invoked by a dependency through caller-supplied

callbacks or trait methods

Model caller-provided safe code as adversarial but type-correct. It may:

  • panic at any point where panicking is possible;
  • fail to terminate;
  • return arbitrary values permitted by its type signature;
  • violate safe trait laws such as ordering, equality, hashing, length hints,

or clone consistency;

  • mutate reachable state through Cell, RefCell, atomics, locks,

UnsafeCell, global state, or reentrant calls;

  • call back into this crate;
  • observe intermediate states that are exposed to it;
  • drop values at inconvenient times;
  • use safe APIs in unexpected order;
  • exploit every behavior allowed by safe Rust and the public API surface.

Do not assume caller-provided safe code "works as documented" for memory-safety proofs. Safe trait documentation may describe logic contracts, but unsafe code must not rely on a safe trait law for memory safety unless one of the following is true:

  1. the trait is unsafe and the needed law is part of its # Safety contract;
  2. the property is checked dynamically before unsafe reliance;
  3. every possible implementation that can reach the unsafe code is

crate-controlled or comes from a trusted opted-in dependency, and downstream/caller code cannot add a new implementation that violates the law;

  1. the trait is sealed, and sealing is airtight: downstream crates cannot name,

implement, forge, or indirectly satisfy the sealing condition, including through blanket impls, public fields, public constructors, macros, or re-exported marker types;

  1. the type system enforces the property independently of caller honesty.

An unsafe API may explicitly place a caller obligation on the behavior of an otherwise safe implementation. In that case, do not assume the safe trait law for arbitrary caller-provided implementations; instead, quote the unsafe API's precondition and prove that every concrete implementation reachable in this call satisfies it through crate control, a trusted dependency contract, sealing, type-system enforcement, or direct inspection. Pin::new_unchecked is a key example: its caller must establish the required behavior of the concrete pointer type's Deref, DerefMut, and Drop implementations. (See also [conv.safety-usable].)

A sealed-trait proof must be mechanical, not aspirational. Documentation saying "do not implement this trait" is not sealing. A public supertrait, public marker type, public token constructor, public blanket impl, re-exported sealing mechanism, or downstream-invoked macro may reopen the implementation set. If downstream safe code can choose, generate, or influence an implementation that violates the law, the implementation is caller-provided for proof purposes.

When relying on a trusted dependency's safe trait implementation, rely on a concrete implementation for a concrete type or a documented closed implementation set. Do not generalize from "this dependency implements the trait correctly for its types" to "an arbitrary caller-provided T: Trait satisfies the law."

The abstraction must remain memory-safe even if caller-provided safe code is semantically wrong. Violating safe trait laws may be a logic error by the caller, but it must not become UB caused by this unsafe abstraction.

Example: sorting

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files. Certified 1 Nov 2026.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
53
Repository forks
6
7-day trend score
20

Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill CERTIFIED badge
[![vskill CERTIFIED](https://verified-skill.com/api/v1/skills/google/rust-skills/unsafe-rust-review/badge)](https://verified-skill.com/skills/google/rust-skills/unsafe-rust-review)

Something wrong with this skill or its page? Tell us.