Skip to content

unrolledguard: recover unrolled-guard jump tables (MSVC memcpy) - #105

Merged
mahaloz merged 2 commits into
mainfrom
feat/unrolledguard
Jun 26, 2026
Merged

mahaloz merged 2 commits into
mainfrom
feat/unrolledguard

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

option unrolledguard — recover interleaved unrolled-guard jump tables (MSVC optimized memcpy)

Closes the gap behind proposal #50 (test_decompiling_optimized_memcpy, 0x42cca0).

angr recovers 7 jump-table switches in this MSVC CRT optimized Duff's-device memcpy; on main kuna recovers 1 and degrades the other six indirect jumps to /* WARNING: Treating indirect jump as call */ computed (code **)() calls.

Root cause — REVISED from the proposal

The proposal hypothesized a JumpBasic::checkUnrolledGuard guard-bound gap. Two instrumentation traces overturned that. The failing tables never reach recover_model_basic/analyze_guards/check_unrolled_guard — switchmultipred on leaves the output unchanged. They fail earlier, in the per-table partial-flow rebuild:

  • kuna recovers the interleaved tables one at a time, each in its own fresh partial clone.
  • A later table's clone re-clones an already-recovered sibling table into its jumpvec, and that partial's collect_edges then calls target() on a sibling case body (e.g. 0x42ce24, the case-0 body of the 0x42ce14 table) that was never decoded into this partial's visited snapshot (it is decoded into the parent flow only after the recovery pass returns).
  • target() throws Could not find op at target address -> the partial pipeline errors -> FailNormal -> truncate_indirect_jump -> CALLIND.

Upstream Ghidra avoids this by building one shared partial and running collectEdges once while the sibling tables are still empty (addresstable filled later, in recoverAddresses), so it iterates 0 case targets and never calls target() on an undecoded body. Verified 0x42ce24 is legitimate code (a case body, not table data or an over-read) and the recovered table is correctly sized.

Fix

option unrolledguard (default-off opt-in): inside a jump-table-recovery partial clone (jumptablerecovery_on) with the gate on, FlowInfo::collect_edges skips a recovered sibling table's case-target edge whose op was not decoded into this partial, instead of propagating the target() error -- reproducing upstream's partial CFG (the "assume no branches out" shape the findJumpTable==0 partial path already uses). A recovery log fires only with the gate on. A flag-gated flow.rs logic change (no new module/ElementId), like switchmultipred.

Before / after (0x42cca0, self-contained bytechunk; the i386 PE loader can't open the file directly)

switches Treating indirect jump as call
unrolledguard off (default) 1 7
unrolledguard on 16 0
angr reference 7 --

(kuna recovers each Duff's-device table as its own switch, hence 16; angr clusters them into 7. The load-bearing result is 0 computed calls with the gate on.)

Default + speed

Default off. unrolledguard on recovers 16 jump tables vs 1 (each fully table-emulated; the function decompiles much larger), ~3.9x slower for this function (148 ms -> 579 ms, 10 samples). Proportional to the recovered content; never touches the default path. make test stays PARITY OK (byte-identical with the gate off, 0/675 ablation).

Gates

Registration

Architecture flag unrolled_guard (decl/ctor/reset/parse-arm/seam-copy) + ArchSeam field; KUNA_OPTION_NAMES; stages.toml surface + settable; count tests bumped (settable 63->64, surface 97->98, live_value 27->28, json comma 62->63, fixture 63->64, corpus 153->154); regenerated stage_catalog.json fixture + docs/assertions.md + docs/baseline-stages.json; kuna-catalog.xml catalog-count assertions. Stage test tests/stages/ghangr-optimized-memcpy-6301a9.xml (off = computed calls, on = recovered switches). docs/baseline.json untouched.

🤖 Generated with Claude Code

mahaloz and others added 2 commits June 26, 2026 23:20
…ptimized memcpy)

angr's test_decompiling_optimized_memcpy (0x42cca0, an MSVC CRT optimized Duff's-device
memcpy) recovers 7 jump-table switches; kuna recovered 1 and degraded the other 6 to
"Treating indirect jump as call" computed (code **)() calls.

Root cause (REVISED from proposal #50): the failures are NOT in JumpBasic guard analysis
(checkUnrolledGuard / switchmultipred is fully ported and irrelevant here). They fail in
the per-table partial-flow rebuild. kuna recovers the interleaved tables one at a time,
each in its own fresh partial clone; a later table's clone re-clones an already-recovered
SIBLING table into its jumpvec, and that partial's collect_edges then calls target() on a
sibling case body (e.g. 0x42ce24, the case-0 body of the 0x42ce14 table) that was never
decoded into THIS partial's visited snapshot (it is decoded into the parent flow only after
the recovery pass returns). target() throws "Could not find op at target address", the
partial pipeline errors -> FailNormal -> truncate_indirect_jump -> CALLIND. Upstream avoids
this by building ONE shared partial and running collectEdges once while the sibling tables
are still empty (addresstable filled later, in recoverAddresses), so it iterates 0 case
targets and never calls target() on an undecoded body.

Two instrumentation traces (a fork inheriting full context + a fresh general-purpose
subagent) verified: 0x42ce24 is legitimate CODE (a case body), the recovered table is
correctly sized (4 entries, no over-read), and the divergence is the partial-flow lifecycle
(per-table rebuild vs upstream's shared partial), not a guard-bound or table-sizing port gap.

Fix (option unrolledguard, default-off opt-in): inside a jump-table-recovery partial clone
(funcdata flag jumptablerecovery_on) with the gate on, FlowInfo::collect_edges SKIPS a
recovered sibling table's case-target edge whose op was not decoded into this partial,
instead of propagating the target() error -- reproducing upstream's partial CFG (the same
"assume no branches out" shape the findJumpTable==0 partial path already uses). The
interleaved tables then all recover and the dispatches render as switches. A recovery log
fires only with the gate on.

Result on 0x42cca0 (bytechunk): off = 1 switch + 7 computed calls; on = 16 switches, 0
computed calls. Default-off (recovering 16 tables vs 1 makes the function decompile much
larger, ~3.9x slower for this function; opt-in keeps the default path untouched). make test
stays PARITY OK (byte-identical with the gate off, 0/675 ablation).

Full registration: architecture flag (unrolled_guard) decl/ctor/reset/parse-arm/seam-copy,
ArchSeam field, KUNA_OPTION_NAMES, stages.toml surface + settable, count tests
(settable 63->64, surface 97->98, live_value 27->28, json },\n 62->63, fixture 63->64
settables, corpus 153->154), regenerated stage_catalog.json fixture + docs/assertions.md +
docs/baseline-stages.json, and kuna-catalog.xml catalog-count assertions (#5/#6/#8/#9). No
new module/ElementId (a flag-gated flow.rs logic change, like switchmultipred). Stage test
tests/stages/ghangr-optimized-memcpy-6301a9.xml (off = computed calls, on = recovered
switches). docs/baseline.json untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
Union both opt-in settables atop main: dedupitetail (4109, #104) +
unrolledguard (no ElementId, #105). Resolved by UNION (no registration
dropped):
- stages.toml: 65 settables, 98 surfaces; header counts updated
- kuna_stages/tests.rs: settable_count_is_65, surface_count_is_98, 64
  trailing commas in the static catalog
- catalog_bytecompat.rs: 65 "option" rows
- xml.rs corpus count: 155 (83 datatests + 72 stage testcases)
- kuna-catalog.xml provenance: #6 source_decompiler angr=24, #7
  structure-recovery=13 (+dedupitetail), #8 opt-in-tool=12 (+unrolledguard),
  #5/#9 use_when/change_kind max bumped 65->66 (65 settables + returnpair dup)
- baseline-stages.json + assertions.md regenerated (235/235)

Gates: catalog --check OK; make test PARITY OK 675/675; make test-stages
PARITY OK 235/235; make rust-test green.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
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