Skip to content

Main reds: derive generators held an intern-pool view across interning (release-only segfault); da_match_place_observes never allocated - #1322

Merged
ehartford merged 2 commits into
mainfrom
fix-derive-x-match-observes
Sep 22, 2026
Merged

ehartford merged 2 commits into
mainfrom
fix-derive-x-match-observes

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Two main reds after #1311 and #1314 merged, each green alone. Neither is an interaction of the two rules; both are independent defects that the merged tree exposed.

1. derive_enum_debug_clone.w — the release compiler segfaults compiling it (out/release/bin/with check … rc 139, no output; the seed-built stage1 compiles it fine, hence red only in the battery). lldb on main's release binary, Sema.ct_generate_debug_derive +1432: ldp x8, x9, [x20] faults with x20 = [sp+0x28] — the type_name &str — feeding type_name ++ ".debug_str". InternPool.resolve returns a view into symbol_texts: Vec[str]; every derive generator bound it (let type_name = intern.resolve(sym)) and then interned new strings (ct_build_string_lit, intern.intern) before the last use, so the table grew, the old buffer was freed, and the tail read through the dangling view (the live buffer, x27, is in a different region; the old page reads as zeros). The current compiler generation lowers that let as a true D27 view where the older seed copied the header — that is the "layout" difference between stage1 and release; the number of derives run in one module is what put the growth inside this function. Fix: own the text at all 17 such bindings (intern.resolve(sym).clone(), one with -p rewrite; the spelling SemaDecl.w:907 already uses). No generator logic change; the derived bodies still bind payloads positionally and the match-observes rule is untouched.

2. da_match_place_observes.w — the program never allocates. Its payloads were all-literal ++ chains, which try_fold_literal_str_concat folds to one constant (_1 = const "sym…" in --dump-mir); with no allocation with_debug_alloc_report_leaks returns before printing (dbg_base == 0), and the lane fails on want: 'leak count=0' with no report line to read. A two-parameter helper (fn heap(a: str, b: str): a ++ b) builds the payloads at runtime (str_concat_n in the MIR) with every asserted length unchanged; run --debug-alloc now prints leak count=0 and the lane driver passes it.

Verified on this branch with a full src/main build (release binary): test passes derive_enum_debug_clone, derive_debug_wide_primitives, atomic_store_element_view, behav_match_place_observes, spec_ss11_8_derive, spec_ss11_8_derive_builder, spec_ss17_3_user_defined_derive, and every *derive*/*match* file under test/behavior and test/spec; the debug-alloc driver passes the fixture; :move-audit and :drop-audit both green. :fixpoint/:test not run.

Follow-up worth a tool, not blind edits: the same let x = pool.resolve(sym) view shape appears ~200 times across Sema/Codegen/Parser; most never intern before the last use, but nothing checks that. Filed as an issue.

Eric Hartford added 2 commits September 22, 2026 12:52
…ve generators held a view across interning

`out/release/bin/with check test/behavior/derive_enum_debug_clone.w`
segfaulted (rc 139, no output); the same test passed on the stage1 built
by the pinned seed, so main went red only in the battery (behavior-tests
lane, release binary) after #1311 and #1314 merged.

Root cause (lldb on the cffb1cb release binary, frame
Sema.ct_generate_debug_derive +1432):

    ->  ldp x8, x9, [x20]        ; x20 = [sp+0x28], the `type_name` &str
        ...
        bl  with_str_concat_n    ; type_name ++ ".debug_str"
    EXC_BAD_ACCESS address == x20; x27 (the live symbol_texts buffer)
    is in another region; the page under x20 reads as zeros.

`InternPool.resolve` returns `&str`, a view into `symbol_texts: Vec[str]`
(InternPool.w:136). Every generator bound it — `let type_name =
intern.resolve(type_name_sym)` — then interned new texts
(`ct_build_string_lit`, `intern.intern`) before its last use, so the
table grew, the old buffer was freed, and the tail read through the
dangling view. Which compiler generation lowers that `let` as a true
D27 view (the current one) versus a header copy (the older seed) is
the "layout" that made it release-only; which derives run in one
module is what made the growth land inside this function.

Fix: own the text at every such binding (17 sites, one `with -p`
rewrite: `intern.resolve(sym).clone()`), the same spelling
SemaDecl.w:907 already uses for its own pool text. No generator logic
changes.

Evidence: the rebuilt release binary compiles and passes
derive_enum_debug_clone.w and the derive/match suites; :move-audit and
:drop-audit ran on the batch (results in the PR).
…ocator report exists

The fixture's payloads were all-literal `++` chains (`"one" ++ "two"`),
which MirLower.try_fold_literal_str_concat folds to one constant
(`_1 = const "sym…"` in --dump-mir), so the program never allocated,
`with_debug_alloc_report_leaks` returned before printing (dbg_base == 0),
and the lane failed on `want: 'leak count=0'` with no report line to
read. The match-observes rule under test was never exercised over heap
payloads, which is the point of the fixture.

A two-parameter helper (`fn heap(a: str, b: str): a ++ b`) keeps every
asserted length and produces a runtime concat (`str_concat_n` in the
MIR); `run --debug-alloc` now prints `leak count=0` and the lane driver
passes the fixture.
@ehartford

Copy link
Copy Markdown
Contributor Author

Isolation battery on 422fd532 (rebased on main 980474c; seed-driven): build, :fixpoint, :move-audit, :drop-audit rc=0; the two tests this PR fixes (derive_enum_debug_clone, da_match_place_observes) pass; :test red only in main's own remaining reds — behav_distinct_compat (#1319's gap, fix forward in progress) and fizzbuzz (#1325). Mergeable on its own.

@ehartford
ehartford merged commit 742649a into main Sep 22, 2026
@ehartford
ehartford deleted the fix-derive-x-match-observes branch September 22, 2026 18:44
ehartford pushed a commit that referenced this pull request Sep 22, 2026
…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.
ehartford added a commit that referenced this pull request Sep 22, 2026
…ning; sweep of the 33 sites (#1323) (#1328)

* analyze: audit:pool-views finds a pool view read after the pool may have 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.

* Ast: impl_type_params_start/count and impl_trait_type_args_start/count 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.

* ComptimeEval: own the pool texts held across evaluation (#1323)

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.

* SemaCheck: own the pool texts held across checking (#1323)

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.

* SemaDecl: own the pool texts held across collection (#1323)

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.

* Codegen: declare_extern_fn owns name_str across compute_fn_abi (#1323)

audit:pool-views: `name_str` passed at 5388 after compute_fn_abi, which
reaches InternPool.intern_str.

* CodegenDispatch: the inline-asm constraint text is owned across resolve_type (#1323)

audit:pool-views: mir_emit_atomic_fiber_intrinsic_call `asm_constr` passed
at 10186 after resolve_type, which reaches InternPool.intern_str.

* CodegenTraits: own the vtable names held across interning (#1323)

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.

---------

Co-authored-by: Eric Hartford <[email protected]>
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.

1 participant