Skip to content

[PROPOSAL] angr-abnormal-switch-case-case3-0a21b8: multi-predecessor unrolled-guard jump-table recovery - #36

Closed
mahaloz wants to merge 1 commit into
mainfrom
feat/angr-abnormal-switch-case-case3-0a21b8
Closed

mahaloz wants to merge 1 commit into
mainfrom
feat/angr-abnormal-switch-case-case3-0a21b8

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

[PROPOSAL] abnormal-switch-case-case3 — multi-predecessor unrolled-guard jump-table recovery

Scope: LARGE — needs human go/no-go before any implementation worker is spent.

The problem

angr's test_decompiling_abnormal_switch_case_case3 (memmove @ 0x18003c330,
msvcr120.dll) recovers a clean switch (count) with 17 cases. kuna loses the
switch and emits an indirect call:

                    /* WARNING: Treating indirect jump as call */
v7 = (void *)(*(code *)((uint8)*(uint4 *)(a2 * 4 + 0x18003c410) + 0x180000000))(a0,a1);

The dispatch is an image-base-relative jump table
(target = 0x180000000 + table[index*4], 17-entry RVA table at 0x18003c410)
whose BRANCHIND block has two predecessors with two different bound guards
(cmp $0x10 ; jbe at function entry; and $0x7 ; test ; jne on the
backward-copy path that re-enters the dispatch at 0x18003c3fc). See
analysis.md for the full asm + decompilation evidence.

Why it is not a one-Action feature (Hard Rule 7)

The recovery does not fail for lack of a small helper — it fails inside the
core S2 jump-table guard analysis:

  • JumpBasicModel::analyze_guards (s2_lift/jumptable.rs:1896) bounds the table
    by walking backward through single-predecessor blocks for the dominating
    guard. With size_in() > 1 it short-circuits to check_unrolled_guard.
  • check_unrolled_guard (s2_lift/jumptable.rs:2080) is an empty SEAM stub:
    // SEAM(structuring): checkCommonCbranch + findMultiequal + liftVerifyUnroll.
  • No bound ⇒ recover_model_basic returns false
    (s2_lift/jumptable.rs:2113) ⇒ flow.rs:2719 truncates the indirect jump to
    a call.

Neither gated precedent fits: kuna_loweredswitch.rs manufactures a table from
a comparison cascade (none here); kuna_switchmodbound presupposes the guard
walk already melded a path (it did not). Closing the gap means porting the
upstream multi-predecessor unrolled-guard machinery
into the
check_unrolled_guard SEAM — modifying JumpBasicModel core, not a single
gated early-return. This trips Hard Rule 7 on two counts (new S2 recovery
infrastructure; touches switch-recovery core beyond a gated early-return).

angr reference

angr's JumpTableResolver / "abnormal switch case" handling resolves jump
tables whose bound check is split across multiple predecessor guards. The kuna
analog is upstream Ghidra's JumpBasic::checkUnrolledGuard →
BlockBasic::findMultiequal + JumpBasic::checkCommonCbranch +
liftVerifyUnroll (decompiler/cpp/jumptable.cc), which kuna left as the SEAM
stub at jumptable.rs:2080.

Implementation plan (multi-step)

  1. Port check_unrolled_guard in s2_lift/jumptable.rs:
    • checkCommonCbranch — when the rootblock has multiple predecessors, find a
      CBRANCH common to the guard structure of each predecessor path.
    • BlockBasic::findMultiequal (s2_lift/blockbasic.rs or block.rs) — pair
      up the MULTIEQUAL inputs across predecessors so the switch variable's value
      range is the union of per-path guards.
    • liftVerifyUnroll — verify the lifted (unrolled) guard genuinely bounds the
      index on every path before accepting the bound.
  2. Bound the table from the unioned guard so find_normalized yields
    jrange.size <= maxtablesize, then let the existing readonly-LOAD
    image-base-relative table model (jumptable.rs:2011) read the 17 RVAs.
  3. Gate the whole behavior behind a new option abnormalswitch on|off
    (default OFF while developing; ablation decides the ship default), so default
    output stays byte-identical when off.
  4. Test harness prerequisite (go/no-go): kuna's loader is ELF-only and
    cannot load this PE. The end-to-end testcase must use a raw bytechunk
    (function bytes 0x18003c330..0x18003c895 at their real addresses, proven to
    reproduce the gap in analysis.md). The standard pipeline before/after demo
    (kuna decompile <pe>) will NOT run on this target.

Speed / risk assessment

  • Risk: HIGH. analyze_guards / find_normalized are core to every
    jump-table recovery in the engine. A faithful port must keep the 675/675
    datatest parity intact (several datatests exercise jump tables). The behavior
    must be option-gated so default output is byte-identical until the ablation
    proves it clean.
  • Speed: guard analysis runs once per BRANCHIND during S2; the unrolled-guard
    walk is bounded (maxbranch=2, maxpullback=2). Expected negligible per-table
    cost, but must be measured on a jump-table-heavy corpus before any default-on.
  • Surface: estimated 2 ported-core files (jumptable.rs, a block helper) +
    options.rs/architecture.rs/stages.toml registration anchors, plus the
    bytechunk test. Larger than one new module.

Proposed option

option abnormalswitch on|off — "Recover image-base-relative jump tables whose
bound guard is split across multiple dispatch predecessors (angr's abnormal
switch case)." source_decompiler=angr,
inspiration="test_decompiling_abnormal_switch_case_case3; JumpTableResolver / abnormal switch case; 0x18003c330",
change_kind=structure-recovery.

Recommendation

File this draft and pause for human go/no-go. On approval, re-dispatch an
implementation worker on feat/angr-abnormal-switch-case-case3-0a21b8 to port
checkUnrolledGuard/findMultiequal/liftVerifyUnroll behind
option abnormalswitch, with the bytechunk harness for the end-to-end test.

…rd jump-table recovery

angr's test_decompiling_abnormal_switch_case_case3 (memmove @ 0x18003c330 in
msvcr120.dll) recovers a clean switch(count) with 17 cases. kuna loses the
switch and emits an indirect call ("Treating indirect jump as call"): the
image-base-relative jump table's BRANCHIND block has two predecessors with two
different bound guards (cmp $0x10 at entry; and $0x7 on the backward-copy path
re-entering the dispatch). JumpBasicModel::analyze_guards (jumptable.rs:1896)
only walks single-predecessor blocks, so it short-circuits into the empty
check_unrolled_guard SEAM (jumptable.rs:2080) and recovery is declined.

Closing the gap requires porting upstream's multi-predecessor unrolled-guard
machinery (checkCommonCbranch + findMultiequal + liftVerifyUnroll) into the
core S2 jump-table recovery -- large per Hard Rule 7, so this is a proposal,
not an implementation. Secondary blocker: kuna's loader is ELF-only and cannot
load the PE target (reproduced via raw bytechunk).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mahaloz

mahaloz commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Closing — implemented by #102 (switchmultipred), default-on since #110

Re-triaged against today's main by running kuna on the actual target and ablating. Both blockers this proposal flagged are gone.

The loader go/no-go is stale. The proposal said kuna's ELF-only loader could not open msvcr120.dll, so the demo needed a bespoke bytechunk harness. PR #40 (PE linked-exe loading, merged ~30 min after this proposal was filed) removed that. Today:

$ kuna decompile .../x86_64/windows/msvcr120.dll 0x18003c330 --addr
real 0m43.5s   exit 0   408 lines   (real memmove body, not a stub)

The gap itself is closed by default.

proposal-era kuna today, defaults angr 9.2.213
switch 0 1 1
case labels 0 17 (case 0 … case 0x10) 17
Treating indirect jump as call 1 0 0

Ablation is causal — --option switchmultipred off reproduces this proposal's bug verbatim:

$ kuna decompile msvcr120.dll 0x18003c330 --addr --option switchmultipred off
367 lines, 0 switch, 0 cases, 1 warning:
    /* WARNING: Treating indirect jump as call */
    return (void *)(*(code *)((uint8)v4 + 0x180000000))(a0,a1);

Case bodies 6/7/8/9/0x10 were spot-diffed against angr's — structurally identical tail copies (case 8 = one 8-byte copy, case 0x10 = 16-byte copy), so the recovered table is the right table, not an over-bound one.

Why #102 is this proposal's implementation under a different name. #102's body opens "Implements proposal #36 (abnormal-switch-case-case3-0a21b8)", names 0x18003c330 / 0x18003c410, and the shipped option carries inspiration = "test_decompiling_abnormal_switch_case_case3; JumpTableResolver / abnormal switch case; 0x18003c330". The check_unrolled_guard SEAM stub this proposal wanted filled is now a real body at decompiler/crates/kuna-decomp/src/p2_lift/jumptable.rs:2437 (call site :2071), with stage test tests/stages/ghangr-switchmultipred-memmove.xml and 7 switchmultipred keys in docs/baseline-stages.json. #110 flipped it default-on — stronger than this proposal asked for (it proposed abnormalswitch default-off, "ablation decides the ship default").

Residual (out of scope here): kuna emits 1 goto (label_18003c6c0, in the large-copy path outside the switch) where angr emits 0, and 408 loc vs angr's 467. Neither is part of this proposal's stated criterion — this was a jump-table-recovery proposal and that criterion is fully met.

@mahaloz mahaloz closed this Jul 29, 2026
@mahaloz
mahaloz deleted the feat/angr-abnormal-switch-case-case3-0a21b8 branch August 9, 2026 02:45
mahaloz added a commit that referenced this pull request Sep 19, 2026
… arguments through fillinMap; pin the outgoing-slot cases

A ninth double goes on the stack behind the four integer registers a
"%f ... %d" call leaves unused, and fillinMap's positional rules ended the
open-tail call's list there, so the audit withdrew the override. The declared
(fixed-position) trials are now kept active.

fmtslots_x86_64 (clang -O2) passes 30-32 pin show9's nine doubles and two
sscanf destinations in the first outgoing stack slot, which the closed
prototype folded to the value stored before the call (return 6; return 0;).
The round-6 binary fails #31-#36.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
mahaloz added a commit that referenced this pull request Sep 19, 2026
… arguments through fillinMap; pin the outgoing-slot cases

A ninth double goes on the stack behind the four integer registers a
"%f ... %d" call leaves unused, and fillinMap's positional rules ended the
open-tail call's list there, so the audit withdrew the override. The declared
(fixed-position) trials are now kept active.

fmtslots_x86_64 (clang -O2) passes 30-32 pin show9's nine doubles and two
sscanf destinations in the first outgoing stack slot, which the closed
prototype folded to the value stored before the call (return 6; return 0;).
The round-6 binary fails #31-#36.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
mahaloz added a commit to borancar/kuna that referenced this pull request Nov 14, 2026
MSVC /O1 writes mov ebx,3 as push 3; pop ebx. That pop into a
callee-saved register is not the epilogue, and ending the cleanup scan
there dropped the arguments of a cdecl call whose cleanup MSVC deferred
past it (pkgen2: sub_42ea6e(v1,v2) -> (v1), and the next
zend_register_long_constant took the orphaned pair). A pop right after a
push is now passed over. The fixture gains consts (rebuilt from its
recipe), stage Noelo-Lab#36/Noelo-Lab#37; spec 04 also states the remaining limit (a frame
torn down with no callee-saved restore first).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
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