Skip to content

[AUTOMATED] fix(analysis): opt-in flowmode keeps a blx target's Thumb mode off A32 functions that flow proves - #795

Merged
mahaloz merged 9 commits into
mainfrom
fix/arm-blx-context
Oct 3, 2026
Merged

mahaloz merged 9 commits into
mainfrom
fix/arm-blx-context

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

In a stripped ARM image, an A32 function placed after a Thumb helper that some caller reaches with blx decodes as Thumb, even when it is reached with bl from A32 code. This image is push {lr}; bl 0x2000080; blx 0x2000040; pop {pc}, a Thumb helper at 0x2000040 and an A32 add r0,r0,#1; bx lr at 0x2000080, with no symbols:

echo 'f0VMRgEBAQAAAAAAAAAAAAIAKAABAAAAAAAAAjQAAADwAAAAAAAABTQAIAABACgAAwACAAEAAABUAAAAAAAAAgAAAAKIAAAAiAAAAAUAAAAEAAAAAEAt6R0AAOsMAAD6AIC96AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAcgcEcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAIDiHv8v4QAudGV4dAAuc2hzdHJ0YWIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABAAAABgAAAAAAAAJUAAAAiAAAAAAAAAAAAAAABAAAAAAAAAAHAAAAAwAAAAAAAAAAAAAA3AAAABEAAAAAAAAAAAAAAAQAAAAAAAAA' | base64 -d > interwork.elf
kuna decompile interwork.elf 0x2000080
void sub_2000080(void) // warn: Function flows into unmapped memory
{
  halt_missing(); // warn: Function flow reaches unmapped memory: r0x02000082 flows to r0x02000586
}

The blx writes Thumb into the context database from its target up to the next explicit mode change, which in a stripped image is the end of memory. With this branch, --option flowmode on prints return a0 + 1;.

Fix

  • New option flowmode: off (default), on, aftercall. The Listing walk is unchanged. With on, kuna then decodes again the code whose mode the image proves, with every context write masked: from an even e_entry or even function symbol (A32), across branches and the fall-through of ordinary instructions, into bl targets in the caller's mode and blx targets in the other.
  • The proof never continues past an unconditional call (a callee that returns does not mean this call does) or past a user-defined p-code operation such as svc or bkpt (the exception need not come back). A function is painted only when every instruction it can run is proven, and two proofs that disagree on an address paint nothing. The function inventory is untouched.
  • It runs only where no metadata paints TMode (no mapping symbol, no Thumb function symbol, no Cortex-M vector table), the build attributes allow A32, and no --isa is given.
  • aftercall also continues after a call whose callee is proven to return. That fixes the issue's own layout (blx first, then bl), but paints the wrong mode after a call that never returns at its site.
  • Off by default because both values still trust an ordinary fall-through and a branch target. When a compiler ends a function with __builtin_unreachable() after a store or a conditional branch (clang -O0 does this for noreturn functions), the proof runs into the next function and paints a static Thumb function there in A32. Not fixed either way: the same leak from a Thumb function symbol ([AUTOMATED] ARM: a Thumb function symbol's mode reaches the A32 function after it when the image has no mapping symbols #842).

Tests

  • tests/stages/kuna-flowmode.xml: the default prints halt_missing, on fixes the image above, and the issue's layout needs aftercall.
  • kuna-cli/tests/arm_interwork_cli.rs runs the interworking images under on and aftercall. New fixture arm_exception_end_le32: A32 exports end in svc #0 or bkpt #1 and __builtin_unreachable(), each followed by a static Thumb function, which keeps its decode under every value.
  • decompile-all with the default is byte-identical to main on 14 ARM ELFs, including those where on paints. On the 10 in-scope images where the pass has something to correct, on changes 2 functions and aftercall 6, all A32 code main prints as Thumb garbage. kuna functions, interleaved min of 11 on 5 images: -0.5% to +2.5%.

Part of #780

🤖 Generated with Claude Code

https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt

@mahaloz
mahaloz force-pushed the fix/arm-blx-context branch from 16e85f3 to 8ad6fe7 Compare October 1, 2026 20:47
@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Oct 1, 2026
@mahaloz
mahaloz force-pushed the fix/arm-blx-context branch 5 times, most recently from ede94cd to 71f9b39 Compare October 2, 2026 05:34
@mahaloz
mahaloz force-pushed the fix/arm-blx-context branch 3 times, most recently from 2b8ca5e to 04a97f2 Compare October 2, 2026 21:09
@mahaloz mahaloz changed the title [AUTOMATED] fix(analysis): keep a blx target's Thumb mode off the functions after it [AUTOMATED] fix(analysis): keep a blx target's Thumb mode off A32 functions that flow proves Oct 2, 2026
@mahaloz mahaloz changed the title [AUTOMATED] fix(analysis): keep a blx target's Thumb mode off A32 functions that flow proves [AUTOMATED] fix(analysis): opt-in flowmode keeps a blx target's Thumb mode off A32 functions that flow proves Oct 2, 2026
@mahaloz
mahaloz force-pushed the fix/arm-blx-context branch 2 times, most recently from c3026a6 to 8a0cf43 Compare October 2, 2026 22:44
mahaloz and others added 9 commits October 3, 2026 00:11
…eft in the other mode

Main's Listing walk now runs unchanged. Afterwards kuna decodes again,
without writing the mode, the code an even e_entry or even function
symbol proves the mode of, following branches, fall-through, bl/blx call
targets, and the instruction after a call only once the callee is proven
to return. Proven instructions whose mode the database disagrees with are
painted at the commit; conflicting proofs paint nothing. Only images with
no load-time TMode paint take the pass. The even-function-symbol paints
and the evidence walk are gone. New option flowmode (default on).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
… counts

Two-pass stage test (option off shows halt_missing). CLI tests for a call
proven to return, a call that never returns, conflicting proofs, and
three stripped armel shapes where a noreturn call or an unsized A32
export sits before a function of the other mode. The veneer fixture goes
with the walk it tested.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
…y default

A callee that returns does not show that a particular call to it returns:
after `report_bad(x); __builtin_unreachable();`, or a call through a
noreturn alias of a function that can return, the compiler emits nothing,
and the proof then ran on into the next function in the caller's mode. A
Thumb function placed there, which the walk decodes correctly, was painted
A32.

flowmode now takes on|aftercall|off. The default no longer follows the
fall-through of an unconditional call; aftercall keeps the previous rule
(follow it once the callee is proven to return) for images where that is
known to hold, such as GH-780's own layout. Only functions whose every
reachable instruction is proven are painted, and the proof decodes mask
every context word, so IT-block and LRset commits no longer reach the
database either.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
…and is off by default

An A32 export that ends in `svc #0` or `bkpt` and then
`__builtin_unreachable()` is followed directly by the next function. The
proof treated the exception's fall-through as executed, carried A32 into a
static Thumb function placed there, and painted it A32. The instruction
after any user-defined p-code operation (CALLOTHER) is now never proven, and
the function that runs one is not complete, so it is not painted.

The proof still trusts the fall-through of an ordinary instruction and the
target of a branch, which a compiler can also leave unreachable (clang -O0
ends a noreturn function that stores and then reaches
__builtin_unreachable() with the store). The option is therefore off by
default; on and aftercall stay available.

New fixture arm_exception_end_le32 (svc and bkpt shapes) with a CLI test;
the other interworking tests now run under on and aftercall explicitly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
Both #826 and this branch moved the settable counts from 250 to 251, which
merged as one bump. Re-derived on a fresh build: 252 settables.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
@mahaloz
mahaloz force-pushed the fix/arm-blx-context branch from 8a0cf43 to 3357db7 Compare October 3, 2026 00:16
@mahaloz
mahaloz merged commit ec8d0d1 into main Oct 3, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/arm-blx-context branch October 3, 2026 00:32
mahaloz added a commit to phix33/kuna that referenced this pull request Oct 3, 2026
Brings in Noelo-Lab#795 (flowmode) and Noelo-Lab#864. Counts re-derived on a fresh build:
253 settables, tiers 81/105/67, 252 catalog rows, corpus 389; the stages
baseline is main's plus the three STACK STORE keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant