Repository navigation
missing-function-call-1101b1: recover guard-bounded GCC PIC jump tables (option switchguardbound) - #60
Conversation
…es (option switchguardbound) Closes the angr `test_decompiling_missing_function_call` gap (binary `adams`, `main`): kuna failed to recover a getopt() jump-table switch and emitted `/* WARNING: Treating indirect jump as call */`, collapsing the whole switch and its surrounding loop into one bogus computed `(*(code *)…)()` call. Root cause (instrumented): the table at 0x96ac is range-guarded by `sub 0x36; ja` (idx LEu 0x36, 55 entries) with the index spilled to the stack. Jump-table recovery runs early on the partial function, before the flag idiom is simplified and the spill collapsed, so JumpBasic::analyzeGuards cannot bound the index (jrange.size = 2^31) and recoverAddresses throws "Too many branches". Mechanism: a new gated JumpBasic model-extension `kuna_try_guard_bound_table` (sibling of GH-9191 switchmodbound), invoked from recover_model_basic after the normal/modulo models fail. It EVALUATES the guard boolean as a function of the index varnode (resolving register-reused siblings like m = v + 0x3f via a linear-offset map) and takes N = the first value whose routing flips from 0's, composing the simplifier's `idx GT 0x35 && m != 0x75` form to N = 0x37 (incl. the boundary 'u' case); then re-binds the index to [0, N) so the normal buildAddresses/structurer emit the switch. Option `switchguardbound on|off`, default OFF opt-in (LLM-discoverable). Ablation flag-on over the 675 datatests: 0 assertions changed; PARITY OK. Speed +34% on the target (full switch+loop recovery vs truncation; gate-off byte-identical) so forced opt-in per the speed gate (no DIV entry). New module kuna_switchguardbound.rs (ELEM 4022); two-pass stage test tests/stages/ghangr-missing-function-call-1101b1.xml. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
We need the following update on this:
|
…ction-call-1101b1 # Conflicts: # decompiler/crates/kuna-base/src/xml.rs # decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_stages/tests.rs # decompiler/crates/kuna-decomp/tests/catalog_bytecompat.rs # docs/PROGRESS.md # docs/baseline-stages.json # tests/stages/kuna-catalog.xml
…loop recovery), keep default-OFF; document declaration-duplication
Reviewer asked why `option switchguardbound` is "incredibly slow" and to document
the over-declared-variables observation. Investigation (no source change needed):
Speed (target adams/main, slope-isolated per-decompile cost, which cancels the
~190 ms fixed process-spawn/load/SLEIGH-mmap that the PR-body +34 % wall-clock
figure was diluted by):
switchguardbound off ≈ 23.9 ms
switchguardbound on ≈ 63.2 ms (+164 %, ~2.6x)
Root cause is downstream and INHERENT, not the heuristic. Instrumented:
kuna_try_guard_bound_table runs 3x at ~7 µs each (~22 µs total, <0.1 % of the
ON<->OFF gap); 0x with the gate off (confirmed). The cost is that the gate-ON path
keeps the real 55-entry switch + getopt while-loop and runs CFG-build / dataflow /
structuring / type-inference over a ~2.6x larger function, instead of truncating
the BRANCHIND to one CALLIND. Ruled out (cf. the prior ActionPool O(N^2)
regression): no O(n^2)/O(n^3), no per-op / per-CFG-node recomputation, no repeated
dominator/region rebuild; scan_guard_tree is already triple-bounded (64-step walk,
4096-step eval budget, v-loop capped at maxtablesize=1024) and resolves in µs.
No localized algorithmic fix exists — the extra time is the decompiler correctly
doing more work. Kept DEFAULT-OFF opt-in (speed_forced_off=true) per the +5 % speed
gate, matching its over-bound risk profile. Output byte-identical with the gate off
(675/675 datatests unchanged) -> no docs/divergences.md entry.
"Too many declared variables on top": NOT caused by this feature. It is a
pre-existing render artifact already mitigated on main by `dedupvardecls` (DIV-7,
default-on, landed after this branch). With dedupvardecls off the ON output renders
67x int4 t / 34x int4 m / 34x int4 c / 33x int4 fd; with it on (default) the
declaration block is ~12 clean lines. Cause: one stack slot maps onto many
un-merged same-named scalar HighVariables (one per loop/switch-phi SSA merge point);
the printer emits one decl per HighVariable. dedupvardecls de-dups identical
rendered lines (render-side) — it hides the symptom, the engine still carries N
HighVariables. The residual `int4 t` x2 is a legitimate sub-piece split
(t._0_4_ / t._4_4_ of the 8-byte -0x28 slot), correctly not collapsed. Follow-up
(separate agent): the s6_merge HighVariable-merge pass. Full write-up in
docs/features/missing-function-call-1101b1/{analysis.md,record.json}.
Gates: make test PARITY OK (675/675), make test-stages PARITY OK (176/176),
make rust-test green, kuna catalog --check OK.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
Perf review + variable-duplication follow-upMerged Why it's slow — measured, and it's inherent (not the heuristic)Re-measured with a slope method that cancels the ~190 ms fixed process-spawn/load/SLEIGH-mmap cost the PR-body +34 % wall-clock figure was diluted by (load once in one
Root cause is downstream and inherent, not the new code. Instrumented: There is no localized algorithmic fix → the option stays DEFAULT-OFF, opt-in ( "WAY too many defined variables on top"This is not caused by
Hypothesis: one stack slot maps onto many distinct same-named scalar Full write-up: |
Resolve union conflicts with main (which landed #60 switchguardbound + #68/#60 region work). Both opt-in/default-off knobs coexist: - stages.toml: union [[settable]]/[[surface]] -> surface 91->93, settable 45->47 (29 stage-model + 15 analysis + 3 loader); header counts updated. - kuna_stages/tests.rs: surface_count 93, settable_count 47, live_value present-for-23 (switch_guard_bound + tail_call_jumps both field-backed), catalog-json trailing-commas 46. - catalog_bytecompat.rs: fixture_has_all_47_settables; stage_catalog.json fixture auto-unioned (byte-for-byte test green). - xml.rs corpus count 138->139 (83 datatests + 56 stage tests: both the missing-function-call and tee-o2-tail-jumps stage tests present). - kuna-catalog.xml: source_decompiler=angr 12, structure-recovery 7, opt-in-tool 8. - PROGRESS.md: union both session entries. - tailcalljump ElementId stays 4101 (no collision; gotoreduce=4100 on main). Regenerated docs/assertions.md + docs/baseline-stages.json (180/180). Gates: kuna catalog --check OK; make test PARITY OK 675/675; make test-stages PARITY OK 180/180. docs/baseline.json untouched. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
Resolve union conflicts with main (which since this branch's base landed gotoreduce, loopbreak_recovery/DIV-10, switchguardbound/#60, tailcalljump/#59, macho-arm64e, and more). branchflip (S8 readability, opt-in/default-off, ElementId-free) coexists with all of them: - stages.toml: union [[settable]]/[[surface]] -> surface 93->94, settable 47->48 (30 stage-model + 15 analysis + 3 loader); branchflip inserted after stackguard, before loopbreak_recovery; header counts updated. - options.rs KUNA_OPTION_NAMES: union (branchflip + loopbreak_recovery). - architecture.rs: union the set_kuna_option arm (branchflip on_off! + loopbreak_recovery); branch_flip field/ctor/reset/seam-copy all preserved. - kuna_stages/tests.rs: surface_count 94, settable_count 48, live_value present-for-23 / suppressed-for-25 (branchflip is NOT field-backed), catalog-json trailing-commas 47. - catalog_bytecompat.rs: fixture_has_all_48_settables; stage_catalog.json fixture regenerated from the binary (byte-for-byte test green). - verify_w8_fw_universalaction.rs: nonblank 261 (252 C++ + branchflip + gotoreduce + 7 headers); list_action_decompile_oracle.txt re-numbered for the branchflip + gotoreduce leaves (byte-equal test green). - xml.rs corpus count 139->140 (83 datatests + 57 stage tests). - kuna-catalog.xml: source_decompiler=angr 13, structure-recovery 7, opt-in-tool 9. - PROGRESS.md: union both session sets. No DIV entry (opt-in/default-off; output byte-identical when off). Regenerated docs/assertions.md + docs/baseline-stages.json (183/183). Gates: kuna catalog --check OK; make test PARITY OK 675/675; make test-stages PARITY OK 183/183. docs/baseline.json untouched. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
…eldb type_match 9 -> 35 of 525, +26 with 0 lost) (#266) * [AUTOMATED] feat(p1): cppsig - apply the demangled C++ signature (leveldb type_match 9 -> 35 of 525, +26 with 0 lost) kuna applied no demangled C++ signature at all. The demangler reduces a mangled symbol to its qualified NAME only - a hard requirement of the `::`-scope splitter - and `demangle_raw`, the full c++filt form, had shipped with NO production caller since the demangler landed, its own header recording the signature application as "a deferred follow-up". The cost is a STRIPPED C++ shared library, where the exported `.dynsym` mangled name is the only signature source left. On stripped google/leveldb -O0, functions whose first parameter is a named pointer ran ghidra 188 / ida 150 / binja 60 / angr 0 / kuna 0 across 527 functions. New `analyzers/demangle/kuna_cppsig.rs` parses the demangled STRING, the same shape as upstream's GnuDemanglerParser, and parks a prototype by entry address. A C++ reference maps to a pointer; a by-value aggregate, array, function pointer or overloaded operator refuses the whole signature. The return type is deliberately NOT applied - Itanium encodes one only for a template function, so upstream returns null and keeps what the analysis recovered; kuna expresses that as `outtype: None`, read as "lock the INPUT half only". Three-valued because Itanium mangles a static member function exactly like a non-static one: `proven` (the default) applies only what the mangling entails and measures precision 1.0000 / recall 0.7093 on 1329 symbols with DWARF ground truth; `inferred` decides the rest from class evidence at precision 0.9278 / recall 1.0000. Ghidra 12.1's own `this` decision on the same binary is 0.85. Benchmark, decbench leveldb O0 (n=525) with the fixed scorer (decbench origin/main @325046f, PR #60): type_match perfects 9 -> 35 at the default (+26, zero lost), 38 under `inferred`. Rivals on the same scorer: ghidra 44, ida 35, binja 11, angr 8 - kuna moves from last to second. Plain C is untouched: 11 binaries / 1,351 functions, 0 changed. Gated `cppsig`, default `proven` (DIV-62); the pass runs at load, so both certainty tiers are stashed apart and the mode applies at the analysis commit. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01McQC7imaNfzj5k5AVumSYN * [AUTOMATED] docs(cppsig): complete the feature bundle with analysis.md + plan.md The standing requirement is a docs/features/<slug>/ bundle of analysis.md, plan.md and record.json; only record.json was written. Content is the measured material from the PR body: the leveldb type_match standing, the precision/recall table behind the three-valued `this` rule, the three defects the whole-binary sweep found, and the refusals. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01McQC7imaNfzj5k5AVumSYN --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: mahaloz <[email protected]>
missing-function-call: recover a guard-bounded GCC PIC jump table (
option switchguardbound)Closes one gap where angr's decompiler beats kuna: angr
test_decompiling_missing_function_call(binaryadams, functionmain, x86-64 GCC PIE).angr recovers
main's getopt() dispatch as a properswitch(6 cases + default). kunafails to recover the jump table and emits
/* WARNING: Treating indirect jump as call */,collapsing the entire switch and the surrounding getopt loop into one bogus computed
(*(code *)…)()call — so every case body (strdup/fprintf/atoi/…) vanishes. The angrtest asserts exactly this is wrong (
filenopresent, the-e/strdup case present).See
analysis.mdfor the full instrumented root-cause.Root cause
The table at
0x96acis a textbook GCC PIC jump table whose index is range-guarded bysub 0x36; ja DEFAULT(idx LEu 0x36, 55 entries) and spilled to the stack before thetable load. kuna's jump-table recovery runs early on the partial/truncated function, before
the
sub;jaflag idiom is simplified and before the stack spill is collapsed, soJumpBasic::analyzeGuardscannot bound the index (jrange.size = 2^31 GT maxtablesize).The basic model is declined →
recoverAddressesthrows "Too many branches" →truncateIndirectJumprewrites the BRANCHIND into a CALLIND.Mechanism
A new option-gated
JumpBasicmodel-extensionkuna_try_guard_bound_table(S2switch-model), sibling of the GH-9191
switchmodboundmodulo bound, invoked fromrecover_model_basicafter the normal model (and the modulo extension) fail. It EVALUATESthe guard boolean as a function of the index varnode
v(resolving register-reused siblingslike
m = v + 0x3fvia a linear-offset map) and takesN= the firstvwhose routingflips from
v = 0's — composing the simplifier'sidx GT 0x35 and m != 0x75form to thecorrect
N = 0x37(55, including the boundary'u'/userlist case at idx0x36, which anaive constant scan would miss). It re-binds the table index to
[0, N); the normalbuildAddresses+ structurer then emit the switch.Option
option switchguardbound on|off(default OFF, opt-in;kuna catalogdiscoverable).Flip per decompilation:
kuna decompile ./adams main --option switchguardbound on.Ablation / parity / speed
the heuristic over-bounds no corpus switch.
make testPARITY OK (675/675, byte-identical when off);make test-stagesPARITY OK (164/164, incl. the new test).main, off vs on): off ≈ 192.9 ms, on ≈ 259.1 ms (+34.3%, over the +5%budget — the ON cost is doing the full switch+loop recovery instead of truncating to a
call; the gate-OFF path is unchanged). Per the speed gate the option ships default-OFF
opt-in (
speed_forced_off; no DIV entry — output byte-identical when off), matching theswitchmodboundprecedent + risk profile (the guard-to-index correspondence is assertedacross a memory spill it cannot prove in dataflow).
A two-pass stage test
tests/stages/ghangr-missing-function-call-1101b1.xmlpins bothstates (off = the computed-call bug; on = the recovered switch incl. the boundary
case 0x36).make rust-testis green except the pre-existingverify_w10_proto_unlockfailure (aremoved-C++-oracle test that fails identically on this branch without these changes —
confirmed via
git stash).🤖 Generated with Claude Code
Before / after —
maininadamsReal captured kuna output on the function this feature was built for, with
option switchguardboundflipped. Ported from angr — test_decompiling_missing_function_call; angr jump-table / VSA guard bounding; main.switchguardbound off(default)switchguardbound onkuna — before (
option switchguardbound off)kuna — after (
option switchguardbound on)angr reference (the goal, v9.2.213)