rust-ffi-safe-wrappers
Design and review Rust FFI boundaries that keep raw declarations isolated behind safe ownership, lifetime, error, and cleanup wrappers. Use when wri…
它会碰到什么
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
Rust FFI Safe Wrappers
Use this skill to wrap foreign interfaces in Rust APIs that make ownership,
lifetimes, errors, and cleanup explicit. FFI code should be unsafe at the raw
boundary and safe for normal Rust callers.
Core Workflow
- Separate raw declarations from safe wrappers. Put raw
unsafe extern
bindings in a small private module.
- Verify ABI, type layout, calling convention, ownership, nullability,
threading, and error conventions against the foreign header or docs.
- Use
unsafe extern "C"blocks for modern Rust, especially Rust 2024 edition
code. Leave safety-conditional items unqualified or explicitly unsafe, and
mark individual extern items safe only when calling them is safe for all
Rust inputs.
- Use
#[repr(C)]for structs and enums that cross the ABI boundary. Do not
expose Rust-only layout types across C.
- Use
CStringfor owned nul-terminated strings sent to C andCStrfor
borrowed C strings. Never treat arbitrary C memory as Rust-owned.
- Wrap raw handles in Rust types with private fields and
Dropcleanup using
the matching foreign deallocator.
- Prevent Rust panics from crossing
extern "C"boundaries. Catch panics or
expose an ABI that permits unwinding only when deliberately using
"C-unwind".
Wrapper Rules
Read references/ffi-boundary-patterns.md before adding or reviewing FFI
bindings.
- Prefer
core::ffiorstd::ffiC types over guessing integer sizes. - Convert foreign error codes into Rust
Resultat the wrapper boundary. - Represent nullable handles as
Option<NonNull<T>>internally when useful,
but expose safe Rust types to callers.
- Tie borrowed values to owner lifetimes with normal references or
PhantomData when the compiler cannot see the relationship.
- Make initialization and shutdown idempotent with
OnceLock,LazyLock, or
Once when the foreign library requires process-wide setup.
- Use
bindgenfor large or changing C headers andcbindgenwhen exporting a
Rust API to C, but still review generated unsafe signatures.
Rust 2024 Syntax Checks
unsafe extern "C" {
pub fn library_open(path: *const core::ffi::c_char) -> *mut RawHandle;
pub fn library_close(handle: *mut RawHandle);
// Mark an item `safe` only when calling it is valid for all Rust inputs:
// pub safe fn library_version() -> core::ffi::c_int;
}
// SAFETY: This exported symbol name is unique for this library.
#[unsafe(no_mangle)]
pub extern "C" fn plugin_version() -> core::ffi::c_int {
1
}
Unsafe attributes such as no_mangle, export_name, and link_section need
the unsafe(...) form in Rust 2024 edition code. Include a SAFETY comment
where symbol uniqueness or linker behavior is part of the contract.
Review Checklist
- Raw bindings are private or clearly separated from safe wrappers.
- Every pointer parameter has a documented nullability and ownership rule.
- Every allocation is freed by the same side or an explicitly matching
deallocator.
- Strings and buffers preserve length, encoding, and nul-byte requirements.
- Panics, callbacks, and threads crossing the boundary have explicit behavior.
想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
plugins/LVTD-LLC/skills/skills/rust-ffi-safe-wrappers/SKILL.md