Written by Claude Opus 5.5.
Where the unsafe is #
At the tip of #5 there are about 1,070 unsafe blocks, functions and impls:
| Crates | Sites | Mostly |
|---|---|---|
| dsv41/gpu, dsv41/prefill, inkling/gpu, qwen3.6/gpu, smollm3/gpu | about 670 | kernel launches and device pointers |
firecrest-cuda |
214 | driver bindings |
firecrest-quant |
151 | about 80 SIMD; about 40 thread pools that share buffers through raw pointers (pool.rs, and #5's workers, kpool); hostmem |
| others | about 35 | mmap, pinned memory, vision SIMD |
The crates that handle untrusted input already forbid(unsafe_code): server, tokenizer, core, sampling, policy, adapters, cli and the model/engine crates. That covers only their own code. Request data (token ids, images, and the MoE routing a prompt steers) still reaches unsafe code and kernels through the engines. So the checks at each engine's submit/forward, and the indices a kernel or a raw-pointer loop uses, are where safety is actually decided.
AGENTS.md currently allows unsafe for FFI, SIMD intrinsics and mmap or pinned memory. The pools fit none of these. In #5 they had the one soundness bug the review found: after a worker panics, the other threads can reach aliased &mut views from safe code, because the invariant depends on pub fields.
Proposal #
Rules (AGENTS.md, or CONTRIBUTING.md, #12):
- Allowed uses, and only these:
- FFI and kernel launches;
- mmap and pinned or registered host memory;
- SIMD intrinsics;
- shared-memory parallelism.
- SIMD:
- Lives in
simd/modules. - Has a scalar twin, tested against it: bit-identical where the code manipulates bits (quant decode, scale unpacking); within a stated tolerance for plain float reductions.
- Uses safe
#[target_feature]functions (current stable Rust) so that only the dispatch call and raw loads/stores areunsafe.
- Lives in
- Parallelism through raw pointers:
- One pool module for the whole workspace.
- Justified by a benchmark against a safe baseline (
std::thread::scope, rayon). - Its safety may not depend on public fields.
- A panic in a worker poisons the step, and no later phase runs.
- Miri or loom tests where feasible.
- Everywhere else: no
unsafe impl Send/Syncoutside those modules, and noget_uncheckedor unchecked arithmetic for speed without a measured gain stated in the commit. - Data-dependent indices (expert ids, top-k outputs, positions) are bounds-checked before they reach unsafe code or a kernel.
Tooling:
- Workspace lints:
unsafe_op_in_unsafe_fn = "deny"andclippy::undocumented_unsafe_blocks = "deny", now set per crate. forbid(unsafe_code)in every crate that has no unsafe.engines/smollm3/enginelacks it today.- A switch (feature or environment variable) that forces the scalar paths, so CI and Miri can run both. This is for testing, not a security feature: the GPU path can't be made safe by a flag.
Reduction work:
- Shrink the SIMD unsafe with safe
target_featurefunctions. - Merge
workers/kpoolandpool.rs. - Generate the kernel launch wrappers (#10). They are most of the remaining sites.