Repository navigation
Conversation
…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]>
Closing — implemented by #102 (
|
| 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.
… 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
… 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
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
[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 cleanswitch (count)with 17 cases. kuna loses theswitch and emits an indirect call:
The dispatch is an image-base-relative jump table
(
target = 0x180000000 + table[index*4], 17-entry RVA table at0x18003c410)whose
BRANCHINDblock has two predecessors with two different bound guards(
cmp $0x10 ; jbeat function entry;and $0x7 ; test ; jneon thebackward-copy path that re-enters the dispatch at
0x18003c3fc). Seeanalysis.mdfor 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 tableby walking backward through single-predecessor blocks for the dominating
guard. With
size_in() > 1it short-circuits tocheck_unrolled_guard.check_unrolled_guard(s2_lift/jumptable.rs:2080) is an empty SEAM stub:// SEAM(structuring): checkCommonCbranch + findMultiequal + liftVerifyUnroll.recover_model_basicreturnsfalse(
s2_lift/jumptable.rs:2113) ⇒flow.rs:2719truncates the indirect jump toa call.
Neither gated precedent fits:
kuna_loweredswitch.rsmanufactures a table froma comparison cascade (none here);
kuna_switchmodboundpresupposes the guardwalk already melded a path (it did not). Closing the gap means porting the
upstream multi-predecessor unrolled-guard machinery into the
check_unrolled_guardSEAM — modifyingJumpBasicModelcore, not a singlegated 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 jumptables 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 SEAMstub at
jumptable.rs:2080.Implementation plan (multi-step)
check_unrolled_guardins2_lift/jumptable.rs:checkCommonCbranch— when the rootblock has multiple predecessors, find aCBRANCH common to the guard structure of each predecessor path.
BlockBasic::findMultiequal(s2_lift/blockbasic.rsorblock.rs) — pairup 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 theindex on every path before accepting the bound.
find_normalizedyieldsjrange.size <= maxtablesize, then let the existing readonly-LOADimage-base-relative table model (
jumptable.rs:2011) read the 17 RVAs.option abnormalswitch on|off(default OFF while developing; ablation decides the ship default), so default
output stays byte-identical when off.
cannot load this PE. The end-to-end testcase must use a raw bytechunk
(function bytes
0x18003c330..0x18003c895at their real addresses, proven toreproduce the gap in
analysis.md). The standard pipeline before/after demo(
kuna decompile <pe>) will NOT run on this target.Speed / risk assessment
analyze_guards/find_normalizedare core to everyjump-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.
walk is bounded (
maxbranch=2,maxpullback=2). Expected negligible per-tablecost, but must be measured on a jump-table-heavy corpus before any default-on.
jumptable.rs, a block helper) +options.rs/architecture.rs/stages.tomlregistration anchors, plus thebytechunk test. Larger than one new module.
Proposed option
option abnormalswitch on|off— "Recover image-base-relative jump tables whosebound 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-0a21b8to portcheckUnrolledGuard/findMultiequal/liftVerifyUnrollbehindoption abnormalswitch, with the bytechunk harness for the end-to-end test.