首页/ AI 技能 / @skilllib/skill-2117939930272416097
Skill站外收录 @skilllib/skill-2117939930272416097

unsafe-checker

提供对 Rust 不安全代码的深度审查能力,覆盖裸指针操作、FFI 绑定、内存布局、未定义行为识别及安全抽象设计,强调 SAFETY 注释完整性与 soundness 论证,辅助开发者构建可验证的低层安全机制。

0.0 (0) 22 次安装 原作者 编程导航
收录于 2026-01-22
软件开发 AI 技能

Skills.MD

Display the following ASCII art exactly as shown. Do not modify spaces or line breaks: ```text ⚠️ **Unsafe Rust Checker Loaded** * ^ * /◉\_~^~_/◉\ ⚡/ o \⚡ '_ _' / '-----' \ ``` --- # Unsafe Rust Checker ## When Unsafe is Valid | Use Case | Example | |----------|---------| | FFI | Calling C functions | | Low-level abstractions | Implementing `Vec`, `Arc` | | Performance | Measured bottleneck with safe alternative too slow | **NOT valid:** Escaping borrow checker without understanding why. ## Required Documentation ```rust // SAFETY: <why this is safe> unsafe { ... } /// # Safety /// <caller requirements> pub unsafe fn dangerous() { ... } ``` ## Quick Reference | Operation | Safety Requirements | |-----------|---------------------| | `*ptr` deref | Valid, aligned, initialized | | `&*ptr` | + No aliasing violations | | `transmute` | Same size, valid bit pattern | | `extern "C"` | Correct signature, ABI | | `static mut` | Synchronization guaranteed | | `impl Send/Sync` | Actually thread-safe | ## Common Errors | Error | Fix | |-------|-----| | Null pointer deref | Check for null before deref | | Use after free | Ensure lifetime validity | | Data race | Add proper synchronization | | Alignment violation | Use `#[repr(C)]`, check alignment | | Invalid bit pattern | Use `MaybeUninit` | | Missing SAFETY comment | Add `// SAFETY:` | ## Deprecated → Better | Deprecated | Use Instead | |------------|-------------| | `mem::uninitialized()` | `MaybeUninit<T>` | | `mem::zeroed()` for refs | `MaybeUninit<T>` | | Raw pointer arithmetic | `NonNull<T>`, `ptr::add` | | `CString::new().unwrap().as_ptr()` | Store `CString` first | | `static mut` | `AtomicT` or `Mutex` | | Manual extern | `bindgen` | ## FFI Crates | Direction | Crate | |-----------|-------| | C → Rust | bindgen | | Rust → C | cbindgen | | Python | PyO3 | | Node.js | napi-rs | Claude knows unsafe Rust. Focus on SAFETY comments and soundness.

评论 0

还没有评论,来抢沙发。