Skip to content

switchmultipred: recover multi-predecessor jump tables - #102

Merged
mahaloz merged 5 commits into
mainfrom
feat/switch-multipred
Jun 26, 2026
Merged

mahaloz merged 5 commits into
mainfrom
feat/switch-multipred

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

What

Implements proposal #36 (abnormal-switch-case-case3-0a21b8): recover a multi-predecessor jump-table switch that kuna currently truncates to a computed call.

angr's test_decompiling_abnormal_switch_case_case3 (memmove @ 0x18003c330, msvcr120.dll) recovers a clean 17-case switch. kuna lost the switch entirely and rendered the image-base-relative dispatch as 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], a 17-entry RVA table at 0x18003c410) whose BRANCHIND parent block has multiple predecessors with different bound guards (cmp $0x10 ; jbe at function entry; and $0x7 ; test ; jne on the backward-copy path that re-enters the dispatch). The single-predecessor guard walk in JumpBasic::analyzeGuards short-circuits to checkUnrolledGuard — which kuna left as an empty SEAM stub — so no dominating guard bounded the table, the normalized range stayed > maxtablesize, the basic model declined, and flow.rs truncated the indirect jump to a call.

How

New S2 jump-table recovery behind a default-OFF option switchmultipred:

  1. JumpBasic::checkUnrolledGuard ported from upstream (checkCommonCbranch + BlockBasic::findMultiequal + liftVerifyUnroll, extended to peel single-in/single-out trampoline blocks x86 compilers interpose between the bound guard and the multi-entry dispatch). The faithful lockstep path that fires when the SAME guard is duplicated.

  2. kuna_try_multipred_guard_table fallback in recoverModel for the x86 abnormal-switch shape, where the per-path guards are semantically different so the lockstep cannot collapse them: it locates the dispatch-index MULTIEQUAL, evaluates each predecessor guard as a function of its MULTIEQUAL input (the contiguous [0, N_K) toward-switch prefix), unions the per-path prefixes (N = max_K N_K), and re-bounds the table to [0, N). The existing image-base-relative readonly-LOAD table model then reads the N RVAs and the structurer emits the switch.

Before / after (on the target)

A bytechunk stage testcase (tests/stages/ghangr-switchmultipred-memmove.xml) loads the real memmove bytes at their VA (kuna's loader is ELF-only and cannot load the PE, per the proposal's go/no-go note):

default (option off) option switchmultipred on
dispatch (*(code *)(...))(a0,a1) computed call + Treating indirect jump as call warning recovered switch(...)
cases — 17 cases (case 0: ... case 0x10:), no default: — matches angr

Recovery log (only with the option on): [kuna switchmultipred] recovered multi-predecessor unrolled-guard jump table at 0x18003c40e: 3 predecessor guards, union bound [0,17)

Default + speed

Default OFF (upstream byte-identical): the gate short-circuits checkUnrolledGuard and the recoverModel fallback, so a binary with no multi-predecessor switch does zero extra work. On the target the on-cost is ~3% (0.32s -> 0.33s) — the difference is recovering the larger live function vs the truncated call, not analysis overhead.

Registration

Fully wired: options.rs KUNA_OPTION_NAMES, architecture.rs field + ctor + reset_defaults + set_kuna_option arm + build_arch_handle seam copy, seams.rs field + ctor, stages.toml surface + settable, kuna_stages count tests (surface 96->97, settable 61->62, field-backed live readers 26->27, trailing-comma 60->61), kuna-catalog.xml stage bounds, XML corpus file count (151->152), docs/assertions.md regenerated, docs/baseline-stages.json regenerated (227 keys). kuna catalog --check OK.

Gates

  • make test: PARITY OK 675/675 (default-off byte-identical)
  • make test-stages: PARITY OK (227/227, the 5 new switchmultipred asserts)
  • make rust-test: green
  • kuna catalog --check: OK

(Rebased onto current main over FID PR4 #101; combined counts reconciled.)

🤖 Generated with Claude Code

https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac

mahaloz and others added 5 commits June 26, 2026 21:04
angr's test_decompiling_abnormal_switch_case_case3 (memmove @ 0x18003c330,
msvcr120.dll) recovers a clean 17-case switch where kuna truncated the
image-base-relative dispatch to a computed (code *)() call.  The BRANCHIND's
parent block has multiple predecessors, each ending in its own bound guard
(entry `count <= 16`; back-copy `count & 7 != 0`); the single-predecessor guard
walk in analyzeGuards short-circuited to the empty checkUnrolledGuard SEAM, so
no dominating guard bounded the table.

This ports the upstream multi-predecessor unrolled-guard machinery
(JumpBasic::checkCommonCbranch + BlockBasic::findMultiequal + liftVerifyUnroll)
into checkUnrolledGuard (the faithful lockstep path for true-unrolled copies),
and adds a kuna_try_multipred_guard_table fallback in recoverModel for the x86
abnormal-switch shape where the per-path guards are semantically different: it
finds the dispatch MULTIEQUAL, evaluates each predecessor guard as a function of
its MULTIEQUAL input, and re-bounds the table to [0, max-over-paths) so the
existing readonly-LOAD image-base table model recovers the switch.

Gated behind `option switchmultipred` (default OFF, upstream byte-identical
when off).  Fully registered (options.rs, architecture.rs field/ctor/reset/
set_kuna_option + build_arch_handle seam copy, seams.rs, stages.toml surface +
settable, kuna_stages counts 96->97 / 60->61, assertions.md).  Stage testcase
tests/stages/ghangr-switchmultipred-memmove.xml: off = computed call, on =
recovered switch (17 cases 0..16, no default — matches angr).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
…ettable

Bump the kuna_stages count tests (surface 96->97, settable 60->61, trailing-comma
60, field-backed live readers 26->27 for switch_multi_pred), the kuna-catalog.xml
stage testcase bounds (use_when/change_kind counts, angr-provenance + opt-in-tool
lists), and regenerate docs/baseline-stages.json (227 keys) for the 5 new
switchmultipred assertions.  Add the default-off assertion in architecture/tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
…post-rebase

After rebasing onto main (FID PR4 #101 brought settables to 61), the combined
tree has 62 settables; the console 'stage catalog' + 'stage catalog returnpair'
emits 63 use_when / change_kind entries (62 + returnpair double-count), so bump
the KUNA-CATALOG #5/#9 max from 62 to 63.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
…ount (61->62)

The catalog_bytecompat gate compares the live emitter against the captured
tests/fixtures/stage_catalog.json byte-for-byte; recapture it (no program
loaded, no `current` field) so it includes the new switchmultipred settable
row, and bump fixture_has_all_*_settables 61->62.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
@mahaloz
mahaloz merged commit e002a17 into main Jun 26, 2026
mahaloz added a commit that referenced this pull request Jun 26, 2026
Reconcile the taildup feature with main after the FID PRs (#97-#101) and
switchmultipred (#102) landed:

- ElementId: taildup collided with switchsharedcase (both 4106 on the merged
  tree). Renumbered taildup -> 4108 (next free above ifelseflatten's 4107).
- Count unions (taildup +1 settable, switchmultipred +1, fid +1 vs merge-base):
  settable 60 -> 63, surface stays 97 (taildup adds no surface row),
  corpus files 151 -> 153, universalaction leaves -> 264 (252 C++ + 5 kuna),
  bytecompat fixture 63, stages.toml header counts.
- kuna-catalog.xml provenance: source_decompiler angr 22, structure-recovery 12
  (+taildup), opt-in-tool 11 (+switchmultipred), use_when/change_kind max 64
  (settables + returnpair double-count in `stage catalog`).
- Regenerated docs/assertions.md (no change) and docs/baseline-stages.json
  (230 passing keys).

Gates: catalog --check OK; make test PARITY OK 675/675 (baseline.json untouched);
make test-stages PARITY OK 230/230; make rust-test green.
@mahaloz
mahaloz deleted the feat/switch-multipred branch July 29, 2026 08:01
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