Commit 22219fb
authored
cargo-unit: per-unit panic-freedom check via relocation scan (#309)
Adds an opt-in `--deny-panics` policy check to cargo-unit that fails a
build when a workspace function can reach a panic. Refs #307.
## Approach
Detection is relocation-based. rustc lowers a panic call to a relocation
whose target is an undefined panic sink (`core::panicking::*`, plus the
`unwrap_failed` / `expect_failed` cold helpers that `unwrap`/`expect`
route through, confirmed against the relocations rustc emits at
opt-level 0 and 3). The new `scan-panics` subcommand reads symbols and
relocations with the `object` crate and attributes each panic call to
its containing function, with no disassembler and no demangler, so one
path covers ELF and Mach-O.
To cover generics, the scan reads the relocatable objects of every
workspace unit, not just library rlibs. A generic carries no relocation
in its defining rlib because it is codegened where it is monomorphized;
the bin or test object that instantiates it does carry the relocation,
and the symbol keeps the defining crate's mangled token. Each unit
therefore gets a `panic-objects` derivation (a recompile with `--emit
obj`, mirroring the clippy units), and findings are scoped to the whole
workspace crate set so a library generic monomorphized inside another
unit is attributed back to its crate.
This was the deliberate design after ruling out the naive alternative: a
trivial clean std binary already carries ~36 `panicking` symbols from
std's runtime, so a whole-artifact symbol grep is always-red for std
code. Relocation attribution scoped to the workspace crate set is the
signal that actually means "this workspace's code can panic."
## What it does
- `--deny-panics` renders `policyChecks.panicFreedom`: one scan
derivation per workspace unit, joined under one aggregate, so a touched
unit only re-scans itself.
- Each scan runs `nix-cargo-unit scan-panics --crate-name ...` over the
unit's `panic-objects` derivation, scoped to the workspace crate set.
- The scanner is the `cargoUnit` package, threaded through a new
`cargoUnit` template argument and asserted non-null, so enabling the
policy without wiring the scanner fails loudly instead of silently
passing.
## Validation
- 45 Rust tests pass (hermetic scanner tests synthesize objects via
`object::write`, no rustc dependency), clippy clean, fmt clean,
generated Nix parses.
- End-to-end on real crates: `unwrap`/`expect`/indexing are flagged with
their exact entrypoints; a generic `pub fn first<T>(xs:&[T])->T{xs[3]}`
invisible in its own rlib is caught via a consumer's objects and
attributed to its defining crate; a clean crate and a crate with its own
`panicking` module both pass.
## Known limitations (documented in code)
- Best-effort, not a soundness proof. The residual gap is a public
generic that no workspace bin/test ever instantiates (never codegened
anywhere here, and also unreachable from the workspace's own
entrypoints). The sound successor is call-graph reachability over the
linked, monomorphized binary (what `findpanics` does).
- The panic-sink catalog is curated; a panic routed through some other
std/alloc cold path is missed until its symbol is added.
- The `ix.cargoUnit.buildWorkspace` `policy` toggle that passes
`cargoUnit` and sets `--deny-panics` lives in the `ix` repo and is the
remaining integration step before this runs in a real workspace.1 parent 8012487 commit 22219fb
9 files changed
Lines changed: 946 additions & 47 deletions
File tree
- lib
- packages/nix-cargo-unit
- src
- templates
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
59 | 64 | | |
60 | 65 | | |
61 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
256 | 256 | | |
257 | 257 | | |
258 | 258 | | |
259 | | - | |
| 259 | + | |
| 260 | + | |
260 | 261 | | |
261 | 262 | | |
262 | 263 | | |
| |||
372 | 373 | | |
373 | 374 | | |
374 | 375 | | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
375 | 379 | | |
376 | 380 | | |
377 | 381 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
| |||
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
| 69 | + | |
66 | 70 | | |
67 | 71 | | |
68 | 72 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
31 | 50 | | |
32 | 51 | | |
33 | 52 | | |
| |||
62 | 81 | | |
63 | 82 | | |
64 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
65 | 89 | | |
66 | 90 | | |
67 | 91 | | |
| |||
103 | 127 | | |
104 | 128 | | |
105 | 129 | | |
| 130 | + | |
106 | 131 | | |
107 | 132 | | |
108 | 133 | | |
| |||
111 | 136 | | |
112 | 137 | | |
113 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
114 | 179 | | |
115 | 180 | | |
116 | 181 | | |
117 | 182 | | |
118 | 183 | | |
119 | 184 | | |
| 185 | + | |
120 | 186 | | |
121 | 187 | | |
0 commit comments