Skip to content

[AUTOMATED] Bound an unchecked jump table at the function's next table (option switchextent, default on) - #1850

Merged
mahaloz merged 18 commits into
Noelo-Lab:mainfrom
coleleavitt:fix/jtbound
Nov 21, 2026
Merged

mahaloz merged 18 commits into
Noelo-Lab:mainfrom
coleleavitt:fix/jtbound

Conversation

@coleleavitt

Copy link
Copy Markdown
Contributor

[AUTOMATED]

cargo build -p kuna-cli
F=decompiler/crates/kuna-analysis/tests/fixtures/switchextent_aarch64
kuna decompile $F unchecked
kuna decompile $F unchecked --option switchextent off
kuna xrefs $F --from 0x100b8

unchecked dispatches twice on a call's result through two halfword offset tables laid out back to back, with no range check on either index. Before: both dispatches print as (*(void (*)(void))(dat_11160[...] * 4UL + 0x100bc))(); // jump-as-call and the function's flow ends there; kuna xrefs --from 0x100b8 files no jump. After: the first table ends where the second begins and recovers as switch(pick(a0) & 0xffffffff) with three cases; the second is the function's last table and stays a computed call; the walk files a jump to each of the three case bodies. With --option switchextent off the function prints as before.

A compiler that knows its selector is in range checks nothing before the dispatch: Swift's switch over a multi-payload enum indexes the table straight with the case swift_getEnumCaseMultiPayload returned, and clang does the same for a C switch whose default is __builtin_unreachable(). The basic model finds no guard, the selector's whole 32-bit range exceeds jumptablemax, and the dispatch is treated as a call that ends the function. On a Swift iOS app's main binary, 1,077 such dispatches in 640 functions; a 13 KB copy helper decompiled to its first 518 lines and now to 1,345 with its 27-case enum switch.

The rule: a function's jump tables are emitted together, back to back in one read-only section, so a table runs exactly to the lowest table of the same function above it. When a table's recovery fails and the function has not been scanned, the flow decodes the function's extent once (entry to the next known entry), folds registers through the small lattice the dispatch shapes need, and names the table of every computed jump the fold proves reads one. With two or more tables named, the recovery runs again and the basic model re-bounds the unguarded table to the entries that fit before the next one; the run-on cut still trims rows that leave the function. A function's last table has no such bound and is left as it was: an object the function merely reads may sit anywhere in the section, and a bound taken from one could run the table on into another function's.

The discovery Listing never read AArch64's compact tables at all (adr base, byte/halfword/word offsets, lsl #2), so a range-checked switch's case bodies were gaps the aggressive finder cut into functions: cmark-gfm's re2c scanner _scan_html_block_start listed as 104 bytes plus five sub_ functions is one function of 4,484 bytes again, and the framework's 442 functions are 407. The Listing now reads those tables through the same register fold the x86 delta tables use, bounded by the range check, by a check that branches into the dispatch block (clang's second table of a two-range switch), or by the next-table rule above. kuna decompile-all on that framework: 272–380 s before, 154–179 s after (three runs each, shared machine).

Option switchextent (P2 switch-model, default on; 0/675 datatest assertions change). Stage test tests/stages/kuna-switchextent-a64.xml and kuna-cli/tests/switchextent_cli.rs over the new fixture switchextent_aarch64 (source and build command in the fixtures README). Spec: docs/spec/01-program-prep.md (AArch64 compact tables) and docs/spec/02-lift-and-flow.md (the extent bound).

Model: anthropic/claude-fable-5-1:high

coleleavitt and others added 12 commits November 21, 2026 11:39
…allee fills itself

Add option `calleefilledarg` (P4, default on). `ParamListStandard::forceNoUse`
applies the convention's positional rule at every call: the first argument
register nobody fills ends the list, so a trial scored definitely-not-used
turned every actively staged register after it inactive. That is right for a
C callee and wrong for a trampoline that loads the slot itself: an
Objective-C image linked with -objc_stubs_fast sends each message through a
`__objc_stubs` entry that loads the selector into x1 and jumps through a GOT
slot, and the caller stages the real arguments in x2 onward, so every
`for (x in ...)` loop printed its countByEnumeratingWithState:objects:count:
send with the receiver alone, the enumeration state never escaped, and its
zero initialiser folded through the call.

The evidence is the callee's entry-liveness probe `calleedeadarg` already
caches: the callee is a trampoline (every path leaves through an indirect
transfer, none returns or calls a named target, nothing is read on entry)
and it writes the register before any read on every path. A
definitely-not-used register trial at such a callee, and a hole
`buildTrialMap` backfills on such a register, are stepped over by
`forceNoUse` and never materialised by `forceInactiveChain`; the chain rule
is untouched. Anything else keeps the positional rule.

Stage test `tests/stages/kuna-calleefilledarg.xml` (the trampoline shape, a
control stub that fills nothing, and `option calleefilledarg off`); three
`fillinMap` unit tests. 675/675 datatest assertions unchanged.
…literal (option cstringconst)

A constant prints as a quoted string only when its type is a pointer to a
character type, and a global symbol is needed for it to get that type; the
string pass plants none at a literal shorter than four characters, so `" ("`
or `".SH"` in the middle of a __cstring section printed as the integer
0x28abe or the address &dat_28a62 when it reached the printer as an argument
of a call through an untyped function pointer or as a void * chosen between
two values. A Mach-O S_CSTRING_LITERALS or ELF SHF_STRINGS|SHF_MERGE section
holds nothing but string literals; a pointer-wide constant inside one now
reads as the literal there, in the pointer arm and for a call argument.
Default on; off keeps the integer. Inert without a section table.

Fixture macho_cstringconst_arm64 (a dylib at base 0) and kuna-console
verify_cstringconst.rs pin both shapes on and off. Catalog counts, the
catalog fixture, docs/options.md and the spec chapter follow the new row.
…ent)

[AUTOMATED]

A compiler that knows its selector is in range checks nothing before the
dispatch: Swift's enum `switch` indexes a halfword offset table straight
with the case `swift_getEnumCaseMultiPayload` returned, and clang does the
same for a C `switch` whose `default` is `__builtin_unreachable()`. The
basic model found no guard, the range exceeded `jumptablemax`, and the
dispatch printed as `(*(code *)(v * 4 + base))(); // jump-as-call`, ending
the function's flow. The discovery Listing never read AArch64's compact
tables at all (`adr` base, byte/halfword offsets, `lsl #2`), so their case
bodies were gaps the aggressive finder cut into `sub_` functions.

A function's jump tables are emitted back to back in one read-only
section, so a table runs to the lowest table of the same function above
it. The flow decodes the function's extent once and names the table of
each computed jump the fold proves reads one; the model re-bounds an
unguarded table to the entries before the next. A function's last table
stays as it was. The Listing reads AArch64 compact tables through the
switchtable fold (new `Val::Relative`), bounded by the range check, by a
check that branches into the dispatch block, or by the same next-table
rule.

Option `switchextent` (P2, default on; 0/675 datatests change).
Stage test tests/stages/kuna-switchextent-a64.xml and kuna-cli
switchextent_cli over the new fixture switchextent_aarch64.
… with a bound

[AUTOMATED]

Decode the function's extent lazily, when a table's recovery fails and the
function has not been scanned, and re-run the recovery only when the scan
names a second table to bound the first by. A failure for its own reasons
costs one extent scan and no second pipeline, so the retry-failure flow
keeps its recovery count. Live-value and tier counts take the new option.
…unction (machodataslots)

The LC_DYSYMTAB indirect-symbol walk named every __got / __la_symbol_ptr
entry as a function import, so a slot holding a Swift type's metadata or
nominal type descriptor, a protocol witness table, an Objective-C class
object or a C++ static data member was a size-0 entry of `kuna functions`:
7,865 of a 72 MB Swift app's 270,680 entries. A slot whose import's mangling
says it is data now stays out of the import stream and is named through the
symbol-table-only channel `elfgotimports` uses for an ELF GLOB_DAT slot, so
`importdata` still renders a read through it as the import and the slot
never joins the function inventory. A plain C import carries no such mark
and stays a callable slot.

Fixture macho_dataslots_arm64 and kuna-console verify_machodataslots.rs
cover the inventory and both importdata renderings; is_data_import has unit
tests for the ObjC, Swift and Itanium classifications.
… slots by name, memoize the classifier

Three follow-ups to the data-import split of the Mach-O indirect-symbol walk:

- The Swift metadata reader built its import map from the import stream only,
  so on an image with classic LC_DYLD_INFO binds (no chained fixups to fall
  back on) every relative pointer through a __got slot of an imported
  protocol or method descriptor went unresolved, and a stripped image lost
  its protocol-witness names (`<main.Point:Swift.Hashable>.hashValue.getter`
  -> `sub_100000980`). The map now also takes the data-import slots, exactly
  the names it read before.
- The slots' names were symbol-table only, so `kuna xrefs --to`, `kuna read`
  and `kuna disassemble` no longer accepted `OBJC_CLASS_$_NSObject`. An
  import_slot_names entry whose slot is painted externref is now a data
  alias, which also lets an ELF GOT data import (`stdout`) be selected by
  name.
- The walk runs on every read of the import stream (about twenty times a
  load), and each run demangled every Swift and C++ slot name again: up to
  +60% user time for `kuna functions` on an import-heavy image. The
  demangled answer is memoized per thread (std HashMap, capped).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
…, and count only indirect jumps as a trampoline's exits

The positional rule is right for every C callee, trampoline or not: a C
function that fills rdi and rsi and tail-calls through a function pointer
(`void tramp2(void) { g_fp(g_x, 1); }`) takes its parameters in order, so
the idiv remainder a clang -O0 caller leaves in rdx is no argument of it,
yet the pass printed `tramp2((a0 - a1) % 7)`. Only a selector stub has no
parameter in the register it fills, so the pass now requires the callee to
be named `objc_msgSend$<sel>` (the linker's and the objc analyzer's name).

is_trampoline also counted a user op, an indirect call and an undecodable
instruction as an exit: ChibiOS chSemWait (`movs r2,#0x30; msr basepri,r2;
...`) read as a trampoline that fills r2, and its caller printed
`chSemWait(&sem,a1,v2,a3)`. Only BRANCHIND cuts count now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
…pin the C-trampoline case, Objective-C docs

The stage fixture's stub is now `objc_msgSend$countByEnumeratingWithState:objects:count:`.
New tests/stages/kuna-calleefilledarg-ctramp.xml (x86-64, clang 14) pins
that a C trampoline filling rsi, or rdi and rsi, keeps the positional rule
and no idiv remainder reaches the call. phases.toml row, spec paragraph and
stages README describe the Objective-C target; options.md, phase_catalog.json,
the corpus count (850) and docs/baseline-stages.json (4928) re-derived.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
…eral's first byte and not for a declared scalar

The integer arm printed any pointer-wide call argument inside __cstring as the
literal there. In a dylib at base 0 a size can land inside the section:
setvbuf(fp, NULL, 0, 0x4000) printed as setvbuf(fp, NULL, 0, "\"") in an
x86_64 libexpat build. The arm now needs the constant to start a literal (the
section's first byte, or the byte after a NUL) and the callee's prototype not
to lock that argument to an integer, boolean or float type. The cheap range
check runs before the use walk, and the walk no longer revisits an (op,
varnode) pair. The pointer arm is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
…xture; describe them in the row and spec

macho_cstringconst_guard_arm64 (recipe in the fixtures README) hands setvbuf
a declared size_t at the first byte of "hello world" and passes a value two
bytes into it through an untyped function pointer; both stay integers with
the option on, while the same pointer's real literal arguments print as
literals. The phases.toml summary, docs/spec/09-emission.md, docs/options.md
and the catalog fixture follow.

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

The flow's extent bound took the lowest table above an unchecked one
wherever it lay; the discovery Listing's reader already keeps the next
table to the same read-only section. A table in another section says
nothing about where this one ends, so the decompiler now declines it
too (ArchContext::in_section_of) and the dispatch stays a computed call,
as it was without the option.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
@mahaloz

mahaloz commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] Pushed f33e726 (11 lines + spec prose): the flow's extent bound (JumpBasicModel::kuna_try_extent_bound_table) now takes the next table only when it lies in the same load-image section as the unchecked one, as the Listing's FunctionTables::next_after already does. Before, a function whose second table sat in another section (e.g. tab_second moved to its own .mytabs section in the switchextent_aarch64.s recipe) still bounded the first table at it. Nothing else changes: the fixture, the stage test and the decbench/fixture outputs are byte-identical.

mahaloz and others added 5 commits November 21, 2026 18:59
cstringconst joins calleefilledarg from main: 231 settables, tiers
48/124/59, 84 live fields, 230 catalog records, KUNA-CATALOG Noelo-Lab#15 239.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
switchextent joins calleefilledarg and cstringconst from main: 232
settables, tiers 48/125/59, 85 live fields, 231 catalog records,
KUNA-CATALOG Noelo-Lab#15 240.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
@mahaloz
mahaloz merged commit aff3221 into Noelo-Lab:main Nov 21, 2026
mahaloz added a commit to coleleavitt/kuna that referenced this pull request Nov 21, 2026
Resolves the kuna_switchtable.rs and listing/mod.rs conflicts with Noelo-Lab#1850
(switchextent). Main's `Shape` match keeps the `Val::Slot` arm. Main's
`Scaled`/`Relative` values replace `Halves`/`Short`, and `Val::Slot` is added
after them. Main's new `TableRead` literals now use `..TableRead::default()`
for the `slot` field. kuna_switchflow.rs gets the signature changes only:
`guard_cases(.., false)` for the fall-through guard, `mask_limit` through a
`decoded` closure, and `Val::Entry { .., .. }`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
@coleleavitt
coleleavitt deleted the fix/jtbound branch November 22, 2026 20:13
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.

2 participants