You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
emit-c emits empty stub labels for out-of-range BB references instead of failing #982
Silent-miscompile shape in the C backend (audit finding CCGEN-001). The fail-loud mechanism exists and is bypassed on this path.
Source chain
emit_fn_body (src/CCodegen.w:9402-9407) emits EMPTY stub labels (bbN: ;) for BB indices referenced beyond block_count; control reaching one falls through into }. Zero-block functions DO self.fail (:9383), and unknown term/stmt/const/operand kinds all fail+poison with the source discarded (emit_module:399-400, honored by Compilation.emit_c:1485) — this path bypasses all of it. Origin: April-2026 workaround ('switch/goto targets exceed emitted BB range', e17d62c); the underlying MIR inconsistency was never resolved at the source. Reachability at HEAD unverified (needs MIR-level proof) — but a workaround that generates fall-through functions must either prove its trigger dead or fail.
Repair boundary
self.fail('terminator references out-of-range BB') in place of the stub loop; prove or remove the trigger.
Review: reproduced, and the trigger is now explained
Verdict. The central claim is established on current main: emit_fn_body emits empty bbN: ; stub labels instead of failing, and nothing in the MIR validators checks that terminator targets are in range. The open question in the report ("reachability at HEAD unverified") is answered: the stub loop fires in almost every function, because max_referenced_bb reads the d2/d3 payload of every terminator kind as a block index, and for a call terminator d2 is the destination place, not a block. The April-2026 "switch/goto targets exceed emitted BB range" symptom that motivated the workaround is consistent with this same misread; I found no function whose MIR actually references an out-of-range block.
return: _5 is place 5 and the C output declares with_vec _5 and StringBuilder _6; those two place indices are what became "referenced blocks". No goto in the emitted C targets bb5 or bb6.
Root cause (read in source, confirmed by the function above)
src/CCodegen.w:9434-9450max_referenced_bb: if d2 > max_bb / if d3 > max_bb unconditionally, for every TermKind. Per src/Mir.w:205-209, for TK_CALLd2 is the destination place and only d3 is a block. Conversely, the genuine block targets of TK_GOTO (d0) and TK_DROP_AND_GOTO (d1) are never scanned, so a real out-of-range target there would be neither stubbed nor diagnosed; the C compiler would report an undeclared label, which is the only reason this is not a silent miscompile in practice today.
src/CCodegen.w:9425-9430: the stub loop, with no self.fail. The zero-block case two lines up does fail, as the report says.
validate_mir_body (src/MirCore.w:2256-2290) checks only that the per-block arrays have equal lengths; it never checks switch table targets, next blocks, or goto targets against block_count. --validate-all therefore cannot catch a dangling block reference. That is the silent validator the report asks for.
Origin: e17d62c (2026-04-04) added both local_receives_ref and this scanner while driving the emit-c error count from 41 to 26; the commit message attributes the undeclared-label errors to "switch/goto targets exceed emitted BB range" without a MIR-level cause. Given that the scanner misreads call payloads, the labels it "fixed" were most likely its own misreads, so the workaround has been generating dead labels rather than papering over a real MIR inconsistency.
Spec / decisions
AGENTS.md §No Silent Fallbacks forbids emitting output that "compiles but behaves differently from the source" and forbids continuing after an untranslatable construct. A fall-through label for a block that does not exist is that class; the fact that today's stubs are unreachable is luck, not a guarantee. No decision-log entry covers the C backend's handling of this.
Related PRs
None since 450733e touch max_referenced_bb or the stub loop. No open PR does.
Would I champion a fix? Yes.
Acceptance criteria:
validate_mir_body checks every terminator's block operands (goto d0, drop_and_goto d1, switch table targets and otherwise, call d3) against block_count, so --validate-all reports a dangling target with the function and block named.
max_referenced_bb and the stub loop are deleted; emit_fn_body calls self.fail("terminator references out-of-range BB") if the validator is bypassed and a target is out of range.
Emitting C for behav_labeled_tail_goto.w produces zero bbN: ; lines, and the emit-c self-host lane stays green (which will also prove there was never a genuine out-of-range reference).
I did not scan the C for the whole compiler (src/main.w --emit-c fails on an unrelated c_void type note at HEAD), only for behaviour tests with their embedded prelude. Linux and Windows not executed; the backend is platform-independent With.
Activity
ehartford commentedon Sep 16, 2026
Review: reproduced, and the trigger is now explained
Verdict. The central claim is established on current
main:emit_fn_bodyemits emptybbN: ;stub labels instead of failing, and nothing in the MIR validators checks that terminator targets are in range. The open question in the report ("reachability at HEAD unverified") is answered: the stub loop fires in almost every function, becausemax_referenced_bbreads thed2/d3payload of every terminator kind as a block index, and for acallterminatord2is the destination place, not a block. The April-2026 "switch/goto targets exceed emitted BB range" symptom that motivated the workaround is consistent with this same misread; I found no function whose MIR actually references an out-of-range block.Tested revision and compiler
main@ 5809ac1 (2026-09-14)with buildof 5809ac1 in a detached worktree, self-reportswith v0.15.2.1Evidence
with build test/behavior/behav_labeled_tail_goto.w --emit-c -o out.c(a 20-line program plus the embedded prelude):^bb[0-9]+:$^bb[0-9]+: ;$(onlysrc/CCodegen.w:9429emits this text)One concrete function,
StringBuilder.with_capacity(5 blocks,bb0..bb4), getsbb5: ;andbb6: ;appended after itsreturn. Its MIR (--dump-mir) is:return: _5is place 5 and the C output declareswith_vec _5andStringBuilder _6; those two place indices are what became "referenced blocks". Nogotoin the emitted C targetsbb5orbb6.Root cause (read in source, confirmed by the function above)
src/CCodegen.w:9434-9450max_referenced_bb:if d2 > max_bb/if d3 > max_bbunconditionally, for everyTermKind. Persrc/Mir.w:205-209, forTK_CALLd2is the destination place and onlyd3is a block. Conversely, the genuine block targets ofTK_GOTO(d0) andTK_DROP_AND_GOTO(d1) are never scanned, so a real out-of-range target there would be neither stubbed nor diagnosed; the C compiler would report an undeclared label, which is the only reason this is not a silent miscompile in practice today.src/CCodegen.w:9425-9430: the stub loop, with noself.fail. The zero-block case two lines up does fail, as the report says.validate_mir_body(src/MirCore.w:2256-2290) checks only that the per-block arrays have equal lengths; it never checksswitchtable targets,nextblocks, orgototargets againstblock_count.--validate-alltherefore cannot catch a dangling block reference. That is the silent validator the report asks for.local_receives_refand this scanner while driving the emit-c error count from 41 to 26; the commit message attributes the undeclared-label errors to "switch/goto targets exceed emitted BB range" without a MIR-level cause. Given that the scanner misreads call payloads, the labels it "fixed" were most likely its own misreads, so the workaround has been generating dead labels rather than papering over a real MIR inconsistency.Spec / decisions
AGENTS.md §No Silent Fallbacks forbids emitting output that "compiles but behaves differently from the source" and forbids continuing after an untranslatable construct. A fall-through label for a block that does not exist is that class; the fact that today's stubs are unreachable is luck, not a guarantee. No decision-log entry covers the C backend's handling of this.
Related PRs
None since 450733e touch
max_referenced_bbor the stub loop. No open PR does.Would I champion a fix? Yes.
Acceptance criteria:
validate_mir_bodychecks every terminator's block operands (goto d0,drop_and_goto d1,switchtable targets andotherwise,call d3) againstblock_count, so--validate-allreports a dangling target with the function and block named.max_referenced_bband the stub loop are deleted;emit_fn_bodycallsself.fail("terminator references out-of-range BB")if the validator is bypassed and a target is out of range.behav_labeled_tail_goto.wproduces zerobbN: ;lines, and the emit-c self-host lane stays green (which will also prove there was never a genuine out-of-range reference).src/MirValidationTests.w) and asserts the validator's message; MIR validator negative coverage: validate_mir_body and use-after-kill have no malformed-MIR cases #989 (hostile-MIR testing) is the same gap from the other side.Untested
I did not scan the C for the whole compiler (
src/main.w --emit-cfails on an unrelatedc_voidtype note at HEAD), only for behaviour tests with their embedded prelude. Linux and Windows not executed; the backend is platform-independent With.ehartford commentedon Oct 25, 2026
Triage 2026-10-25,
with v0.15.3.0(main a261b65): reproduced. Emitting C for a three-line program leaves empty stub labels after the last real block:post-release: emit-C is a manual bootstrap lane (see #955), and failing loudly here means first fixing the producers that reference those blocks.
emit-c successors have one owner (#982); missing bundles say so (#1076)…