Skip to content

emit-c emits empty stub labels for out-of-range BB references instead of failing #982

Closed

Description

@eas4ai

Classification

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.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    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.

    Tested revision and compiler

    item value
    source main @ 5809ac1 (2026-09-14)
    compiler with build of 5809ac1 in a detached worktree, self-reports with v0.15.2.1
    platform macOS Darwin 25.6.0, arm64

    Evidence

    with build test/behavior/behav_labeled_tail_goto.w --emit-c -o out.c (a 20-line program plus the embedded prelude):

    pattern count
    real labels ^bb[0-9]+:$ 114,383
    stub labels ^bb[0-9]+: ;$ (only src/CCodegen.w:9429 emits this text) 58,682

    One concrete function, StringBuilder.with_capacity (5 blocks, bb0..bb4), gets bb5: ; and bb6: ; appended after its return. Its MIR (--dump-mir) is:

    bb3: call const fn sym41(copy _2) -> [return: _5, next: bb4];
    

    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-9450 max_referenced_bb: if d2 > max_bb / if d3 > max_bb unconditionally, for every TermKind. Per src/Mir.w:205-209, for TK_CALL d2 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:

    1. 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.
    2. 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.
    3. 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).
    4. A negative test constructs an out-of-range target through the MIR validation harness (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-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.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    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:

    $ with build main.w --emit-c -o main.c   # from a source checkout
    # main.c, the end of main():
    bb1:
        /* drop(_1); */
        return;
    bb2: ;
    bb3: ;
    …
    bb7: ;
    

    post-release: emit-C is a manual bootstrap lane (see #955), and failing loudly here means first fixing the producers that reference those blocks.

  3. added a commit that references this issue on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions