Skip to content

Switch-default d2==0 means abort() in C but bb0 in LLVM (unenforced convention) #984

Closed

Description

@eas4ai

Classification

Latent backend-divergence defect (audit finding CCGEN-003).

Source chain

For TK_SWITCH_INT, the C backend treats default d2 == 0 as 'no default → abort()' (src/CCodegen.w:8170,8185) while the LLVM backend treats d2 as a plain BB index with if d2 >= 0 (src/CodegenDispatch.w:15538), i.e. d2==0 is a real default-bb0. All current producers pass nonzero defaults so the backends agree today — but nothing enforces the convention, and one default=bb0 lowering silently diverges the backends (C aborts where LLVM branches).

Repair boundary

File with CCGEN-001's fix or separately: a single documented MIR contract for the switch default (sentinel vs index) enforced at construction or validated by both backends.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced (latent, as the report says; established from source)

    Verdict. The two backends read the TK_SWITCH_INT default payload d2 under different conventions, and nothing in MIR construction, the MIR printer, or the validators pins either one. That is exactly the report's claim. It is latent today: every switchInt producer in MirLower.w passes a freshly allocated block as the default, bb0 is the entry block and is never re-targeted, and a dump of several test programs shows no otherwise: bb0. So there is no runtime divergence to run; the defect is the missing contract, which direct source evidence establishes. A third reading I found while checking: for a negative d2 the LLVM backend branches to its unreachable block while the C backend would emit goto bb-1, so the sentinel space disagrees in both directions.

    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 (used only for --dump-mir)
    platform macOS Darwin 25.6.0, arm64

    Evidence

    • C backend, src/CCodegen.w:8157-8180 emit_switch_term: if d2 != 0: goto bb{d2} else: abort(), in both the single-target and the table path. Zero is a sentinel meaning "no default".
    • LLVM backend, src/CodegenDispatch.w:15403-15408: default_bb = mir_default_unreachable_bb_value(); if d2 >= 0: default_bb = mir_bb_values[d2]. Zero is bb0; negative is the sentinel.
    • MIR printer, src/Mir.w:172-203: prints otherwise: bb{d2} unconditionally, so a dump would render the C sentinel as a real branch to bb0.
    • Producers: all 12 terminate(TermKind.TK_SWITCH_INT, ..., <block>, 0) sites in src/MirLower.w (lines 4523 to 6992) pass a real block (else_bb, exit_bb, fail_bb, skip_bb, rhs_bb, end_bb), never a sentinel.
    • Validators: validate_mir_body (src/MirCore.w:2256) checks array lengths only; no check relates d2 to block_count or to a documented sentinel (the same gap as emit-c emits empty stub labels for out-of-range BB references instead of failing #982).
    • grep -c "otherwise: bb0]" over --dump-mir of behav_labeled_tail_goto.w, spec_ss14_11_collection_await.w, and behav_std_build_api.w: 0, 0, 0.

    Spec / decisions

    Not a language-spec matter. AGENTS.md's single-source-of-truth rule for ABI (D6, FnAbi) is the closest principle: a per-backend re-derivation of a MIR encoding is the pattern that produced the T*/T** divergence. No decision-log entry documents the switch-default encoding.

    Related PRs

    None since 450733e touch emit_switch_term or the LLVM switch lowering's default handling (b9e0e65 touched other terminators). #982's review already proposes a terminator-target validator; this is the same fix's second acceptance criterion.

    Would I champion a fix? Yes, folded into #982's validator work.

    Acceptance criteria:

    1. One documented encoding in src/MirCore.w next to TermKind: either "d2 is always a valid block index" (then the C backend drops the != 0 sentinel and abort() path) or "d2 < 0 means no default" (then the C backend tests < 0 and emits abort() or __builtin_unreachable(), and the LLVM backend's existing >= 0 test is the same rule).
    2. validate_mir_body rejects a switchInt whose d2 is outside [0, block_count) unless it equals the documented sentinel; --validate-all reports it.
    3. The MIR printer renders the sentinel distinctly (e.g. otherwise: unreachable) so dumps cannot show a phantom bb0 branch.
    4. A MIR validation test (src/MirValidationTests.w) covers default = 0, default = -1, and default = block_count.

    Untested

    No runtime probe is possible without hand-built MIR; #989 (hostile-MIR testing) is the prerequisite for a negative test that actually executes both backends on a bb0 default.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced in source; no program reaches it today. The C backend still treats a switch default of 0 as "no default" (emit_switch_term, src/CCodegen.w:9179-9190: if d2 != 0: goto bb{d2} else abort();), while the LLVM backend treats any d2 >= 0 as a block index (src/CodegenDispatch.w:16086-16089). No validator states the convention.

    post-release: latent, and it belongs with the emit-C campaign (#955, #982).

  3. added this to the post-v0.16.0 milestone on Oct 28, 2026
  4. added a commit that references this issue on Nov 11, 2026
  5. ehartford commented on Nov 11, 2026

    @ehartford
    Contributor

    Fixed in #3255 (6a905fd). Root cause: MirCore's structural validator accepted a switch default of 0, which LLVM read as bb0 and emit-C as abort(). validate_mir_body now requires the default in 1..bb_count and CCodegen.emit_switch_term always emits the jump, so the convention is enforced in one place. Negative case: test/internals/mir_structural_validator_test.w.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions