agentleFS
jscott3201jscott3201/yet-more-skills/skills/rust-unsafe-ffi/SKILL.md

rust-unsafe-ffi

Skill4 starsChanged 2 months agoApache-2.0View on GitHub

Audit Rust unsafe blocks, raw pointers, FFI, layout, and synchronization soundness. Use when a boundary exists or is proposed; never bypass a repository ban on unsafe code.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Prefer small unsafe islands behind clear safe abstractions.

    line 29
  2. Distinctive:

    Read tool limits when choosing evidence.

    line 41

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Rust Unsafe and FFI Review

Begin with the repository's unsafe policy. Where unsafe code is forbidden, preserve that policy and pursue safe APIs or an approved dependency boundary. This skill is an audit workflow, not permission to add unsafe code, relax lints, or assert thread safety.

Find the actual unsafe boundary

Inspect unsafe blocks/functions/impls, FFI declarations, generated code, dependency wrappers, allocation owners, callbacks, and the safe entry points exposing them. Distinguish project-authored unsafe code from generated/dependency internals without assuming either is sound merely because it compiles.

For each operation, identify the caller obligations, invariants established before entry, and invariants the implementation must maintain. A safe wrapper must uphold its guarantees for all inputs reachable through its safe API, including error paths, panic/unwind behavior, reentrancy, and concurrent calls.

Review memory and representation

Check provenance, bounds, allocation lifetime, alignment, initialization, aliasing/exclusivity, valid values, and drop ownership. A non-null pointer is not automatically valid to dereference. A buffer's capacity is not its initialized length. A zeroed byte pattern is not a valid instance of every Rust type. Do not infer stable FFI layout from ordinary Rust struct layout.

Review raw slice construction and pointer arithmetic at zero-length and overflow boundaries. Verify that pointer/length pairs describe the same live allocation. Preserve the distinction between an unaligned read and creating an invalid aligned reference. Check that owning pointers are released exactly once by the matching allocator/foreign API.

For concurrent code, inspect Send/Sync assumptions, atomics, interior mutability, and foreign thread affinity. An unsafe impl is a semantic promise, not a way to satisfy a compiler error. Keep publication/visibility and object lifetime reasoning separate.

Review control flow across FFI

Define how panics and foreign exceptions interact with the chosen ABI. Do not assume catch_unwind catches aborts or arbitrary foreign exceptions. Keep callbacks' lifetimes, user-data ownership, thread constraints, and reentrancy explicit. Do not hold a Rust lock across a callback that can re-enter the protected API unless the design handles it.

Prefer small unsafe islands behind clear safe abstractions. Put a useful safety explanation next to the operation, stating the established invariant rather than repeating that it is safe. Do not expand an unsafe block to cover unrelated work or perform speculative unsafe optimization.

Build complementary evidence

Use targeted Miri tests for supported pure-Rust paths when the toolchain is available and permitted. Use sanitizers or native FFI tests for relevant unsupported/external paths, and a bounded concurrency model when appropriate. These tools find classes of failures on exercised paths; passing them is not a proof of soundness. Do not disable isolation or widen permissions merely to force a test through.

Exercise malformed inputs, zero/large sizes, failed construction, early drop, callback reentry, and concurrent teardown. Keep the safe alternative as a differential baseline when useful.

Return

List the safety invariant, evidence, concrete unsound path if found, and the smallest repair. State tooling/platform gaps explicitly. A speculative concern without a reachable violation should be labeled as such, not promoted to a confirmed defect.

Read tool limits when choosing evidence.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.