Repository navigation
unrolledguard: recover unrolled-guard jump tables (MSVC memcpy) - #105
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
mainkuna 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::checkUnrolledGuardguard-bound gap. Two instrumentation traces overturned that. The failing tables never reachrecover_model_basic/analyze_guards/check_unrolled_guard—switchmultipred onleaves the output unchanged. They fail earlier, in the per-table partial-flow rebuild:collect_edgesthen callstarget()on a sibling case body (e.g.0x42ce24, the case-0 body of the0x42ce14table) that was never decoded into this partial'svisitedsnapshot (it is decoded into the parent flow only after the recovery pass returns).target()throwsCould 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
collectEdgesonce while the sibling tables are still empty (addresstablefilled later, inrecoverAddresses), so it iterates 0 case targets and never callstarget()on an undecoded body. Verified0x42ce24is 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_edgesskips a recovered sibling table's case-target edge whose op was not decoded into this partial, instead of propagating thetarget()error -- reproducing upstream's partial CFG (the "assume no branches out" shape thefindJumpTable==0partial path already uses). A recovery log fires only with the gate on. A flag-gatedflow.rslogic change (no new module/ElementId), likeswitchmultipred.Before / after (
0x42cca0, self-contained bytechunk; the i386 PE loader can't open the file directly)Treating indirect jump as callunrolledguard off(default)unrolledguard on(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 onrecovers 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 teststays PARITY OK (byte-identical with the gate off, 0/675 ablation).Gates
make test-- PARITY OK, 675/675.make test-stages-- PARITY OK, 232/232 (adds the 2 newunrolledguardassertions + the renamed catalog Render unknown types as real C types (realtypes, DIV-6) #6/Perf/actionpool cursor #8 rows).make rust-test-- green.kuna catalog --check-- OK.Registration
Architecture flag
unrolled_guard(decl/ctor/reset/parse-arm/seam-copy) +ArchSeamfield;KUNA_OPTION_NAMES;stages.tomlsurface + settable; count tests bumped (settable 63->64, surface 97->98, live_value 27->28, json comma 62->63, fixture 63->64, corpus 153->154); regeneratedstage_catalog.jsonfixture +docs/assertions.md+docs/baseline-stages.json;kuna-catalog.xmlcatalog-count assertions. Stage testtests/stages/ghangr-optimized-memcpy-6301a9.xml(off = computed calls, on = recovered switches).docs/baseline.jsonuntouched.🤖 Generated with Claude Code