rust-unsafe-review
Skill0 starsChanged 3 days agoMITView on GitHub
Review unsafe Rust for soundness - inventory every unsafe block, unsafe fn, unsafe impl, extern block and static mut, check SAFETY comments and Safety doc sections, examine risky operations (transmute, from_raw_parts, set_len, assume_init, mem zeroed or uninitialized, const-to-mut pointer casts, get_unchecked, manual Send and Sync impls), and verify with Miri, sanitizers and cargo-geiger style counts. Includes a scanner that produces the inventory and pre-flags undocumented unsafe and dangerous APIs. Use when the user asks to audit or review unsafe code, FFI bindings or a crate before depending on it, sees Miri or "undefined behavior" reports, adds unsafe for performance, or wants to forbid unsafe_code.
In this repository9 other files it gives its agents.
- AGENTS.mdAGENTS.md
- rust-async-reviewSkill
- rust-borrow-checkerSkill
- rust-crates-publishSkill
- rust-dependency-auditSkill
- rust-error-handlingSkill
- rust-lint-ciSkill
- rust-testing-benchesSkill
- rust-workspace-setupSkill
The file
Files
Unsafe code review
Goal: every unsafe has a written, checkable argument for why it is sound, the unsafe
surface is as small as possible, and Miri runs on the tests that exercise it.
Paths below are relative to this skill's folder (in Claude Code: ${CLAUDE_SKILL_DIR}).
When to use
- "Review the unsafe code in this crate", "is this transmute OK?", FFI wrappers
- Deciding whether a dependency with unsafe code is trustworthy
- Miri, AddressSanitizer or "undefined behavior" reports
- Adding
#![forbid(unsafe_code)]orundocumented_unsafe_blocksto a project
When not to use
- Safe-code borrow errors:
rust-borrow-checker(do not add unsafe to silence them) - Data races in async code with locks:
rust-async-review - Vulnerable dependency versions:
rust-dependency-audit(cargo audit)
Workflow
- Inventory (read-only):
Report counts per crate and which crates already forbidpython3 scripts/unsafe_audit.py . --inventory python3 scripts/unsafe_audit.py . --json > unsafe-inventory.jsonunsafe_code. Crates with zero unsafe should getunsafe_code = "forbid"in[lints.rust]. - Triage findings:
highfirst (manualSend/Sync, casting*constto*mut,mem::uninitialized), then risky operations, then missing SAFETY comments. - Review each unsafe site with the checklist in references/checklist.md. For every operation write down the precondition (from the std docs "Safety" section) and where it is established (bounds check, constructor invariant, type system). If you cannot point to it, the code is unsound or under-documented: report it as such.
- Shrink the surface: replace unsafe with safe equivalents where they exist
(references/checklist.md) such as
bytemuck/zerocopyfor casts,slice::chunks_exact,MaybeUninitAPIs,std::ptr::addr_of_mut!/&raw mut. Wrap remaining unsafe in small private functions with a safe public API that enforces the invariants. - Document:
// SAFETY: ...directly above each block,/// # Safetyon everypub unsafe fnandunsafe trait. Enable lints:unsafe_op_in_unsafe_fn = "deny",clippy::undocumented_unsafe_blocks = "warn",clippy::missing_safety_doc = "warn". - Verify dynamically (ask before installing toolchains):
Miri cannot run FFI calls; cover the Rust side with tests that do not cross the boundary.rustup +nightly component add miri cargo +nightly miri test # UB in tests: aliasing, alignment, uninit MIRIFLAGS="-Zmiri-strict-provenance" cargo +nightly miri test RUSTFLAGS="-Zsanitizer=address" cargo +nightly test --target x86_64-unknown-linux-gnu - Re-run
python3 scripts/unsafe_audit.py . --fail-on mediumand report the result.
Decision points
- Unsafe "for performance": require a benchmark showing the gain (see
rust-testing-benches); bounds-check elimination via iterators orassert!before the loop often gives the same speed safely. unsafe impl Send/Sync: only if every field is safe to send/share or access is synchronised; raw pointers to non-thread-safe data make it unsound.static mut: replace with atomics,Mutex,OnceLockorLazyLock. Edition 2024 denies references tostatic mut(static_mut_refs).- FFI: check nullability, ownership (who frees), lifetimes of returned pointers,
CStrvsCString, and that panics cannot unwind acrossextern "C"(useextern "C-unwind"deliberately or catch the panic).
Output
Unsafe inventory: 7 blocks, 2 unsafe fns, 1 unsafe impl, 1 static mut (crate ring-buf)
UNSOUND src/lib.rs:14 unsafe impl Send for Ring<T> - holds *mut T to data shared with ...
FIX src/lib.rs:19 set_len(cap) before init -> use Vec::resize_with / MaybeUninit
DOC 5 blocks without SAFETY comments (comments proposed in the diff)
Miri: cargo +nightly miri test -> 0 UB after fixes (or: not run, nightly not installed)Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

