Skip to content

analysis(arm): Stage 2 raw Thumb-prologue seeding — discovery parity 70→90% (betaflight +860 fns) - #173

Merged
mahaloz merged 1 commit into
mainfrom
feat/arm-raw-prologue-seed
Jul 14, 2026
Merged

mahaloz merged 1 commit into
mainfrom
feat/arm-raw-prologue-seed

Conversation

@mahaloz

@mahaloz mahaloz commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Stage 2 of the ARM Cortex-M discovery fix (after #172 enabled AIF): adds angr-style raw unpaired Thumb-prologue seeding to push stripped-firmware function discovery toward angr-parity. Goal: near-100% function equality with angr.

What

raw_thumb_prologue_seeds (kuna-analysis aif/mod.rs) mirrors angr CFGFast._func_addrs_from_prologues() over ArchARMCortexM.thumb_prologs: after the first recursive-descent walk, scan every exec section at 2-byte alignment for the two canonical LR-saving Thumb prologues — PUSH {..,lr} (0xB5xx) and PUSH.W {..,lr} (0xE92D+LR) — with no epilogue/prepattern/fingerprint requirement (unlike funcstart_patterns' <patternpairs>), then re-seed the walk so it expands each into a full function + discovers its callees. This catches the functions preceded by data/literal-pools/padding that the pair-matcher never seeds.

Precision guards (how angr/Ghidra keep this clean): gap-only via Listing::is_undefined (reuses the walk's coverage, never splits a discovered body), check_valid_subroutine (decoded in the painted Thumb TMode), address-ordered body-claim dedup. Gated on the existing non-x86-64 funcstart_patterns path — no new stage-model option.

Discovery parity |kuna ∩ angr| / |angr| (Stage-1 → Stage-2)

binary before after Δ real fns
crazyflie cf2.elf 82.0% 87.6% +157
betaflight STM32F405 70.3% 90.5% +860
cleanflight DALRCF405 76.2% 89.0% +294
libopencm3 cdcacm 90.8% 90.8% saturated

The canonical-PUSH residual is essentially eliminated (only 5/12/10 PUSH functions still missed). Remaining residual is non-PUSH entry shapes (a future Stage 3).

Precision / safety

~99.5% of newly-discovered functions decompile brace-balanced; the only 2 new imbalances are real angr functions tripping a pre-existing printer bug (not false starts). Per-binary errors DROPPED (crazyflie 68→2, betaflight 14→6, cleanflight 18→4). make test 675/675 byte-identical, make test-stages 261/261, make rust-test 0 failures (+ a prologue-matcher unit test + an ARM CLI e2e test). x86-64 untouched.

🤖 Generated with Claude Code

https://claude.ai/code/session_018DV2MXxQAAWK7xLPBmsq9J

…pped Cortex-M discovery

Stage 1 (#172) enabled the AIF gap-walk on non-x86-64 decompile-all
(crazyflie 45% -> 82% of angr's discovered set). The residual: functions
that START WITH A CANONICAL THUMB PUSH but are (a) never seeded by Ghidra
`<patternpairs>` (no matching epilogue prepattern before them — they sit
after literal pools / data / padding), (b) unreachable by the direct-BL
recursive-descent walk, and (c) skipped by AIF's cursor-advancing
fingerprint gap-walk in dense back-to-back prologue clusters. betaflight
STM32F405 alone missed ~483 such functions.

Stage 2 mirrors angr's `CFGFast._func_addrs_from_prologues()`
(angr/analyses/cfg/cfg_fast.py:2607) over `ArchARMCortexM.thumb_prologs`
(archinfo/arch_arm.py:401: `{rb"[\x00-\xff]\xb5",
rb"\x2d\xe9[\x00-\xff][\x00-\xff]"}`): after the first walk, scan every
executable section at 2-byte alignment for the two canonical LR-saving
Thumb prologues — `PUSH {..,lr}` (0xB5xx) and `PUSH.W {..,lr}` (0xE92D
with the LR bit) — with NO epilogue/prepattern/fingerprint requirement, and
re-seed the recursive-descent walk with the survivors so it expands each
into a full function and discovers its callees
(kuna-analysis/src/analyzers/aif/mod.rs::raw_thumb_prologue_seeds, wired in
passes.rs::run_listing_consumers).

Precision (angr measured raw prologues at ~93%; the guards close the gap):
 (i)   gap-only — skip any candidate the walk already covers
       (`Listing::is_undefined` — reuses the walk's coverage), so a
       prologue-shaped byte-pair inside a discovered body never splits it;
 (ii)  `check_valid_subroutine` — the SAME validity predicate AIF uses
       (>2 insns, reaches a clean RET / computed jump / adds-info call, no
       undecodable byte, no out-of-image flow), decoding in the
       already-painted Thumb TMode (cortexm_thumb_paints);
 (iii) body-claim dedup — address-ordered scan claims each accepted body,
       so a lower-address entry is seen before any interior prologue.
Requiring the LR bit on PUSH.W (0xE92D..0x4???) keeps non-LR STMDB spills
out. SUB SP (0xB0xx) was measured (only ~7 of crazyflie's residual) and
left out as too false-start-prone.

Discovery parity |kuna ∩ angr| / |angr| (decompile-all default path):
  crazyflie   cf2.elf                82.0% -> 87.6%  (2280 -> 2437; +157)
  betaflight  STM32F405.elf          70.3% -> 90.5%  (2986 -> 3846; +860)
  cleanflight DALRCF405.elf          76.2% -> 89.0%  (1754 -> 2048; +294)
  libopencm3  cdcacm.elf             90.8% -> 90.8%  (saturated; no change)
The canonical-PUSH residual is essentially eliminated (only 5/12/10 PUSH
functions still missed on crazyflie/betaflight/cleanflight — the rest of
the residual is non-PUSH entry shapes, a future Stage 3).

Precision: of the newly-discovered functions, ~99.5% are brace-balanced
clean decompilations; per-function errors DROPPED on every binary
(crazyflie 68->2, betaflight 14->6, cleanflight 18->4). The only 2 new
brace-unbalanced functions (betaflight) are REAL angr functions (tune2Rx,
tuneRx2) that trip a pre-existing printer bug — confirmed intrinsic (same
imbalance when decompiled standalone), not discovery garbage.

Gating: folded into the existing `funcstart_patterns`
(`analysis_funcstart_patterns`) non-x86-64 discovery gate — no new
stage-model option — and ARM-gated inside `raw_thumb_prologue_seeds`, so
x86-64 / console / datatest are byte-identical and every non-ARM binary is
a strict no-op.

Gates: `make test` 675/675 PARITY OK (byte-identical), `make test-stages`
PARITY OK (261/261), `make rust-test` 0 failures. New tests:
aif::thumb_lr_prologue_matcher (unit) +
arm_decompile_all_raw_thumb_prologue_seed_non_destructive (CLI e2e).

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018DV2MXxQAAWK7xLPBmsq9J
@mahaloz
mahaloz merged commit 1e235cf into main Jul 14, 2026
@mahaloz
mahaloz deleted the feat/arm-raw-prologue-seed 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