Repository navigation
[PROPOSAL] angr-optimized-memcpy-6301a9: recover unrolled-guard jump tables (MSVC optimized memcpy) - #50
[PROPOSAL] angr-optimized-memcpy-6301a9: recover unrolled-guard jump tables (MSVC optimized memcpy)#50mahaloz wants to merge 1 commit into
Conversation
…s (MSVC optimized memcpy)
angr's test_decompiling_optimized_memcpy (0x42cca0, MSVC CRT memcpy, Duff's device)
recovers 7 jump-table switches; kuna recovers 1 and degrades the other 6 indirect
jumps to "Treating indirect jump as call". Root cause: the 6 failing tables are
bounded by a Duff's-device unrolled/duplicated cbranch guard, and
JumpBasic::check_unrolled_guard (s2_lift/jumptable.rs) is a // SEAM(structuring) stub
whose BlockBasic SSA deps (findMultiequal/liftVerifyUnroll/noInterveningStatement/
unblockedMulti) are // SEAM(W7) in substrate/block.rs.
A decider subagent ruled this LARGE under Hard Rule 7 (core S2/S7 recovery edits
beyond a gated early-return + new SSA infrastructure across >3 anchor files; not a
self-contained Action and not cleanly default-off-gateable). Per the proposal fork
this commit adds the analysis + proposal + partial record only; no implementation.
docs/features/optimized-memcpy-6301a9/{analysis,proposal,record,angr-vs-kuna}.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Closing — implemented by #105 (
|
| defaults today | --option unrolledguard on |
angr 9.2.213 | |
|---|---|---|---|
switch tokens |
1 | 16 | 7 |
Treating indirect jump as call |
7 | 0 | 0 |
| loc | 164 | 462 | — |
| wall time | 2.3 s | 4.2 s | — |
The defaults column reproduces this proposal's baseline exactly (1 recovered switch, the rest degraded). With the option on, every one of the seven dispatch tables named in the degraded output is recovered: 0x42ceb4, 0x42cfb0 (×2), 0x42cf60, 0x42cda8, 0x42cd28, 0x42ce24.
The switch-token count is 16 rather than angr's literal 7 because kuna emits one switch per structural position of the same seven Duff's-device dispatches — this is not tail duplication (the count is unchanged at 16/0 with taildup and dedupitetail both forced off), and the emitted shape matches angr's reference: switch(a2) { case 0..3: goto label_42cfc0/c8/d8/ec; default: <nested switch on & 3> }.
Default-off is what this proposal sanctioned. unrolledguard ships default-off / destructive_as_default. Plan step 5 of this proposal states verbatim: "measure decompile wall-time off vs on … if over the speed budget, ship opt-in." Measured 2.3 s → 4.2 s on this function — over the ≤5% budget, so opt-in is the disposition the proposal itself specified. #105 also delivered the two-pass stage test the plan asked for (pass 1 asserts the degradation, pass 2 asserts the recovery).
Residual (cosmetic): the switch-token count differs from angr's literal 7 for the structural-copy reason above; the same seven tables are recovered either way.
[PROPOSAL] optimized-memcpy-6301a9 — recover unrolled-guard jump tables (MSVC optimized memcpy)
Status: proposal (large feature — needs human go/no-go before implementation)
angr testcase:
test_decompiling_optimized_memcpy::0x42cca0Binary:
i386/windows/736cb27201273f6c4f83da362c9595b50d12333362e02bc7a77dd327cc6b045aProposed option:
unrolledguard on|offThe problem
On the MSVC CRT optimized memcpy (
0x42cca0, Duff's device), angr recovers 7jump-table switches; kuna recovers 1 and degrades the other 6 to
/* WARNING: Treating indirect jump as call */. Full analysis + side-by-side:analysis.md/angr-vs-kuna.txt.The six failing tables share one root cause (see analysis): their bound comes from a
Duff's-device unrolled/duplicated
cbranchguard at a block-merge point, andkuna's
JumpBasic::check_unrolled_guard(s2_lift/jumptable.rs:2075) is a documented// SEAM(structuring)stub. The value-set / index-transform / table-emulationpath is already ported and works — only the unrolled-guard bound is missing.
angr reference
angr's
JumpTableResolver(inangr/analyses/cfg) resolves these as bounded jumptables; the structurer then emits
switchper table. The kuna-equivalent is Ghidra'sJumpBasic::checkUnrolledGuard/checkCommonCbranch(jumptable.cc:1257-ish) plusthe
BlockBasicSSA helpers it depends on.Why this is LARGE (Hard Rule 7)
A decider subagent investigated and confirmed large:
kuna_*.rsAction/Rule. The fix edits S2jumptable-recovery core (
analyzeGuards/findNormalizedinjumptable.rs) beyonda single gated early-return.
BlockBasicSSA helpersfindMultiequal/noInterveningStatement/unblockedMulti/liftVerifyUnroll(all
// SEAM(W7)insubstrate/block.rs).jumptable.rs,block.rs,funcdata,plus option/arch wiring) and is not cleanly gateable default-off (it changes core
recovery, not an opt-in rendering pass).
kuna_loweredswitch.rs-style manufacturing Actionthat reads the absolute tables from the loadimage — is even larger: it duplicates
working recovery, and the Duff's-device targets are mid-block, forcing CFG block
splitting plus its own bounds inference.
Proposed implementation plan (multi-step)
BlockBasicSSA helpersfindMultiequal/noInterveningStatement/
unblockedMulti/liftVerifyUnrollinsubstrate/block.rs(faithful C++transcription; standard W7 SSA-helper effort).
checkCommonCbranch+ fillcheck_unrolled_guardinjumptable.rstodetect the duplicated guard across the unrolled merge and contribute the bounding
range constraint into
analyzeGuards.option unrolledguard on|off(
change_kind = structure-recovery), default-off while developing; correct thestale
jumptable.rsmodule-header SEAM note that misdescribes the chain as unported.tests/stages/ghangr-optimized-memcpy-6301a9.xml: the bytechunk of0x42cca0+ its in-.texttables; pass 1 (option unrolledguard off) asserts theTreating indirect jump as calldegradation andswitch(count == 1; pass 2(default/on) asserts the recovered switches (target: 7, matching angr).
kuna test --all --baseline docs/baseline.jsonmust stayPARITY OK; measure decompile wall-time off vs on (jump-table recovery + extra SSA
walks can be non-trivial; if over the speed budget, ship opt-in).
Speed / risk assessment
recovered switch and the broader datatest jump-table corpus.
make test/make test-stagesparity is the hard gate. Validate the bound across all sixtransformed tables (negative stride
-4, offset(a2-4)*4, masks&3).should be done carefully/faithfully, not pattern-matched to this one function.
size_in()>1guard blocks; expected to be modest, but must be measured (Hard Rule 6).
Recommended option name
unrolledguard(the construct is "jump table bounded by an unrolled/duplicated cbranchguard"),
change_kind = structure-recovery,source_decompiler = angr,inspiration = "test_decompiling_optimized_memcpy; JumpBasic::checkUnrolledGuard / angr JumpTableResolver; 0x42cca0".🤖 Generated with Claude Code