Skip to content

analyze: audit:pool-views catches intern-pool views held across interning; sweep of the 33 sites (#1323) - #1328

Merged
ehartford merged 8 commits into
mainfrom
fix-1323-intern-views
Sep 22, 2026
Merged

ehartford merged 8 commits into
mainfrom
fix-1323-intern-views

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1323. Based on #1322 (fix-derive-x-match-observes), which owns the 17 ComptimeTransform sites; merge that first, then this rebases to main.

The tool: with analyze <file> audit:pool-views (in audit:all)

InternPool.resolve_symbol returns &self.state.symbol_texts[sym] — a reference to the str header slot inside a Vec[str]. The bytes live in the never-moving InternStringArena; the header slot is what the next push frees. Any let name = intern.resolve(sym) still read after something interns is a use-after-free inside the compiler (release binary segfaulted in ct_generate_debug_derive, #1322).

Why the language did not catch it. The same program over a plain struct (type Pool { texts: Vec[str] }, fn resolve(i) -> &str: self.texts[i], then p.intern(..), then use) is rejected at check time: cannot mutate p while name is a live view into it. InternPool is a Copy handle over *mut InternPoolState, so the view-invalidation check never sees the aliasing. That is the root cause; the audit polices the class until the structure is fixed (see the decision below).

Criterion, derived from live MIR and the live call graph, never from a name:

  • pool field F: the field in <place>.F[i] whose element some body returns a ref to (through copy chains to _0);
  • producer: that body, or a body whose _0 is a producer's call result — found: InternPool.resolve_symbol/resolve, Sema.pool_resolve_symbol/pool_resolve, MirBuilder.symbol_text, AstPool.get_string, Ci{Type,Expr,Stmt,Decl}Pool.get_string, Sema.source_text_view_for_file_id/source_text_for_decl_node, Sema.dyn_impl_row_sig/dyn_impl_row_mono_sym, and (before the fix below) AstPool.impl_type_params_start/count, impl_trait_type_args_start/count;
  • grower: a body that passes a place ending in F as the receiver of a mut fn/move fn call or a mutating container intrinsic (symbol_texts.push), or assigns the field; plus everything reaching one over the MIR call graph, per field (growing symbol_texts never poisons a view into impl_type_params);
  • hit: a local holding a producer's result (through copy chains) that is read, passed, or written through after a call reaching a grower of its field with no reassignment between, on any CFG path (forward dataflow, union at joins).

Each violation names the function, binding, producer, source line, the poisoning call and the direct grower it reaches. WITH_ANALYZE_TRACE=pool-views prints the per-block state. Not covered, by design and stated in docs/deep-debugging-tools.md: a view stored into an aggregate, a view from a builtin (Vec.get), a view into a container the function owns.

A tool that stayed silent is a bug — it happened here: the first cut queued a CFG successor only when its in-state grew, so a block after a clean call was never audited and the fixture's planted defect passed with violations=0 ok. Every block is now visited at least once; the fixture (deep-debug lane in build.w) requires the planted dangling named and the clean safe/reads_only silent, and audit:all over the same fixture must fail too.

Cost on src/main.w: 99 s, 1.5 GB (facts collection dominates).

The sweep

analyze src/main.w audit:pool-views on the base: 37 hits (33 once growth is tracked per pool field; the 4 dropped were AstPool views "poisoned" by calls that only reach intern_str). After: 0. One commit per module:

Alternative I evaluated — decision needed, not taken here

Making resolve return an owned str is wrong: it allocates on every pool_resolve on every hot path, and most of the ~200 bindings never intern. A str header copy (bytes are arena-stable) would be a second owner of the same allocation — §2.3 transport is not duplication — so that is out too.

The structural fix is making the header store stable: symbol_texts as a chunked store (fixed pages of str headers, allocated like InternStringArena's byte pages, never reallocated) so &symbol_texts[sym] is valid for the pool's lifetime and the class cannot exist for InternPool. resolve's contract (-> &str) stays. The audit still guards the other pools (AstPool.get_string, Ci*Pool.get_string, dyn_impl_row_*). I have not changed InternPool; that is Eric's call.

Verification

  • with check src/main.w: ok. with build :dev: green (stage1 built with the audit and the fixes).
  • analyze src/main.w audit:pool-views with that stage1: violations=0 ok; fixture: violations=1 FAILED naming dangling only.
  • run --debug-alloc test/behavior/derive_enum_debug_clone.w: expected output, leak count=0.
  • Not run: :fixpoint, :test, and the deep-debug lane itself (deep-debug-tool-tests needs the release build) — the fixture and the audit were exercised by hand with the same command the lane runs.

Eric Hartford added 8 commits September 22, 2026 14:48
…ave grown (#1323)

`InternPool.resolve_symbol` returns `&self.state.symbol_texts[sym]`; a
binding of that view read after anything interns is a use-after-free inside
the compiler (the release binary segfaulted in ct_generate_debug_derive,
#1322). The pool sits behind a Copy `*mut InternPoolState` handle, so the
ordinary "cannot mutate while a view is live" check never sees the aliasing:
the same program over a plain struct is rejected at check time.

The audit derives every role from the live MIR and call graph, never from a
name: a pool field is the F in `<place>.F[i]` whose element some body
returns a ref to; a producer is that body or a wrapper returning its result
(InternPool.resolve, Sema.pool_resolve, AstPool.get_string, …); a grower
mutates a place ending in a pool field in place (mut-receiver call, mutating
container intrinsic) or assigns it, plus everything reaching one over the
call graph, per field; a hit is a local holding a producer's result that is
read, passed or written through after a call reaching a grower of its field
with no reassignment between, on any CFG path (forward dataflow, union at
joins). It is part of audit:all.

The first cut queued a successor only when its in-state grew, so a block
after a clean call was never audited and the fixture's planted defect passed
silently; every block is now visited at least once. WITH_ANALYZE_TRACE=
pool-views prints the per-block state. The deep-debug lane runs the audit
over a fixture with one defect (`dangling`) and two clean shapes (`safe`,
`reads_only`) and requires the defect named and the clean cases silent.

On src/main.w: 7 fields, 18 producers, 23 direct growers, 33 hits, 99 s and
1.5 GB.
…t return i32, not a view (#1323)

Unannotated, D43 tail inference gave these accessors the element place's
type `&i32` — a view into impl_type_params / impl_trait_type_args, which
add_impl_type_params / add_impl_trait_type_args push to. audit:pool-views
flagged the views held across resolve_type_expr and
resolve_impl_trait_arg_for_source in Sema.bind_type_params_from_type_expr
(SemaCheck.w:17409), Sema.select_blanket_impl_for_generic_inst (17735) and
Sema.install_trait_default_type_args (SemaDecl.w:2104, 2106). A start or a
count is a value; the signature now says so.
audit:pool-views: eval_str_method_call `method` read at 3431 after
eval_expr; function_decl_summary_value and type_decl_summary_value `name`
passed at 4221 / 4268 after empty_vec_for_field — all reach
InternPool.intern_str.
audit:pool-views: check_index `base_name` read at 12092 after check_expr;
check_method_call_parts `mc_method_name_raw` passed at 21467 after
check_builtin_method_call_arg; check_typeinfo_module_call `method_name`
read at 22476 after resolve_type_level_arg_expr — all reach
Sema.pool_intern.
audit:pool-views: collect_fn_decl `fn_name_str` passed at 1654 after
resolve_type_expr; collect_impl_decl `trait_name` read at 2481 after
warn_large_copy_type — both reach Sema.pool_intern.
audit:pool-views: `name_str` passed at 5388 after compute_fn_abi, which
reaches InternPool.intern_str.
…ve_type (#1323)

audit:pool-views: mir_emit_atomic_fiber_intrinsic_call `asm_constr` passed
at 10186 after resolve_type, which reaches InternPool.intern_str.
audit:pool-views: ensure_monomorphized_trait_vtable `type_name`,
`trait_name`, `method_name` read at 1054 after InternPool.intern;
ensure_generic_inst_trait_vtable `trait_text` read at 1122 after
ensure_concrete_mir_function — all reach InternPool.intern_str.
@ehartford
ehartford force-pushed the fix-1323-intern-views branch from 5b7f879 to fea60e3 Compare September 22, 2026 19:10
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery on fea60e3e (rebased on main after #1322; seed-driven): build, :fixpoint rc=0; :test red only in main's two remaining known reds — behav_distinct_compat (#1332) and fizzbuzz (#1325). None in this change's paths. Marking ready.

@ehartford
ehartford marked this pull request as ready for review September 22, 2026 19:11
@ehartford
ehartford merged commit ae609cd into main Sep 22, 2026
@ehartford
ehartford deleted the fix-1323-intern-views branch September 22, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intern-pool text views held across interning: an audit for the shape

1 participant