审查 Rust 不安全代码的内存安全
逐项检查 unsafe 代码的安全不变量与调用路径,标出未定义行为风险并提供修复和验证结果。
完整提示词
<context>
Unsafe Rust is sound only if no sequence of safe calls can cause undefined behaviour, so the question is never "does this block work in our tests" but "can safe code calling this API break it". The common unsound patterns: a safe function that wraps unsafe code without checking the precondition (an index, a length, alignment, a non-null pointer); `slice::from_raw_parts` with a length from untrusted input or a lifetime longer than the data; `transmute` between types with different layout or invalid bit patterns (bool, enums, references); `set_len` before the elements are initialised; `mem::uninitialized` or `assume_init` on data that is not initialised; creating two `&mut` to the same data; `Send` or `Sync` implemented for types holding raw pointers or `Rc`; panics in the middle of an unsafe update leaving a broken invariant observable in `Drop`; and FFI signatures that mismatch the C side or let the C code keep a pointer after Rust frees it. Miri detects many of these when tests exercise the code; it does not support most FFI calls.
</context>
<task>
Audit the unsafe code in {{crate}}.
1. List every `unsafe` block, `unsafe fn`, `unsafe impl` and `extern` block (`grep -rn "unsafe"` and `cargo geiger` if installed). Record what each relies on.
2. For each, write the safety invariant it needs, then check that every safe caller path guarantees it. Mark the block sound, unsound (with a safe-code example that triggers UB), or unclear.
3. Check `unsafe impl Send/Sync`, `Drop` implementations near unsafe code, and panic safety in functions that temporarily break invariants.
4. For FFI, compare Rust signatures with the C headers (types, nullability, ownership, who frees), and check that callbacks cannot unwind across the boundary.
5. Replace unsafe code that has a safe equivalent with the same performance (`split_at_mut`, `chunks_exact`, `bytemuck` or `zerocopy` if already a dependency, `MaybeUninit` APIs used correctly). Make functions with unchecked preconditions `unsafe fn` or add the check.
6. Add a `// SAFETY:` comment to every remaining unsafe block stating the invariant and why it holds, and enable `clippy::undocumented_unsafe_blocks` and `unsafe_op_in_unsafe_fn` if the crate accepts it.
7. Add tests for each unsound case found, then run `cargo test`, `cargo clippy`, and `cargo +nightly miri test` on the crate (skipping FFI tests Miri cannot run, and saying which).
</task>
<constraints>
- An unsound finding needs a concrete safe-code trigger; otherwise mark it "unclear" with the question to resolve.
- Do not change the public API without listing the change and its semver impact.
- Do not add new dependencies to remove unsafe without asking.
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## Unsafe inventory
Table: location, kind, purpose, invariant, verdict.
## Findings
For each unsound case: location, the safe code that triggers UB, and the fix.
## Changes
One line per file: `path`, what changed.
## Verification
Commands run (tests, clippy, Miri) and actual results, including what Miri skipped.
## Remaining unsafe
Count before and after, and why each remaining block is needed.
</output_format>可替换内容
- {{crate}}
- {{> guardrails/investigate-before-answering}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、Rust代码审计、内存安全、Miri测试