Skip to content

[AUTOMATED] Print a search tree of jump tables as one switch (option switchmerge) - #1863

Merged
mahaloz merged 23 commits into
Noelo-Lab:mainfrom
coleleavitt:fix/switch-default-holes
Nov 22, 2026
Merged

mahaloz merged 23 commits into
Noelo-Lab:mainfrom
coleleavitt:fix/switch-default-holes

Conversation

@coleleavitt

Copy link
Copy Markdown
Contributor

[AUTOMATED] A sparse switch that the compiler lowers to two range-checked jump tables on one value prints as two nested switches, and the holes of a table print as case labels of an arm that only reaches the default code. A reader sees two switches and a goto where the source has one switch.

echo 080401511f1d0071a8000054090500102a79a8b829010a8b20011fd6088001511f1d0071a8030054290500102a79a8b829010a8b20011fd6200c80521a000014400c805218000014800c805216000014a00c805214000014e00c805212000014000d805210000014000e80520e000014200e80520c000014600e80520a000014800e805208000014c00e805206000014e00e8052040000140004001100001952c0035fd600781f53c0035fd68cffffff94ffffffecffffff9cffffffa4ffffffecffffffacffffffb4ffffff9cffffffa4ffffffccffffffacffffffb4ffffffccffffffbcffffffc4ffffff | xxd -r -p > f.bin
kuna decompile f.bin --raw-image --target AARCH64:LE:64:v8A:default --base 0x100000 0x100000 --addr
    case 0x43:
    case 0x46:
label_100098:
      return a0 + 1U ^ 0x80;
    ...
    default:
      switch(a0) {
        case 0x60:
        ...
        default:
          goto label_100098;

With this change it prints one switch(a0) with the twelve value labels and a single default: return a0 + 1U ^ 0x80;.

  • New option switchmerge (P8, default on, C output only, p8_structure/kuna_switchmerge.rs) plans the arms each switch prints, on the final structured tree, and changes no code. A default: arm that starts with a switch on the same variable takes that switch's arms, when the inner switch prints nothing before its switch, has no label, repeats no value, and leaves the way the arm leaves. An inner switch followed by more code must have no default: and no arm that leaves it; that code stays the default: arm.
  • An arm that is only a goto to the code default: runs prints no labels. If default: is that goto, the arm holding the code prints default:.
  • An arm moves or is dropped only when the arm before it cannot fall into it. A switch with a table entry that goes straight to its exit is never merged into, since that value would take the merged default:. The goto census places break; and labels for the printed switch.

Tests: new stage test kuna-switchmerge.xml (both shapes, plus an off run that keeps the nested form; it fails without the change). 0/675 datatest assertions move. On cmark-gfm's cmark_utf8proc_case_fold (arm64) the labels go from 2161 to 1837 and the switches from 17 to 12, with the same wall time.

Model: anthropic/claude-opus-5-5:high

…old to its last copy

The extent fold that names a function's jump tables walked each
instruction's p-code linearly. AArch64 csel/csinc lift to a conditional
branch over one copy, so the fold took the last copy as the register's
value: a dispatch whose index a csel picks from constants read as a load
from one address, and its table was never named. The byte-masked last
table of the function before it then had no table to end at and kept
230 phantom rows. A Swift enum-to-string switch on Perplexity iOS
(sub_1028ab1c8) printed its case bodies' mov/movk string-literal chains
as `v1 & 0xffff0000ffffffff | 0x676100000000` joins.

Both folds (decompiler and discovery Listing) now treat what an
instruction writes after a branch inside it as unknown.

This is part of option switchextent (default on). The stage test is
kuna-switchnextcsel-a64.
…itch

A sparse source switch compiles to several range-checked jump tables on one
selector: a failed range check goes to the next table and the holes go to the
default code. kuna printed each table as its own switch nested in the previous
one's default arm, and every hole as a case label of an arm that only jumps to
the code default runs.

New option switchmerge (P8, default on, C output only) plans each switch's
printed arms on the final structured tree: a default arm that starts with a
switch on the same selector takes that switch's arms, and an arm that only
jumps to the default code prints no labels. The goto census places break and
labels for the printed switch. 0/675 datatest assertions move; with the
option off the output is byte-identical to before.

cmark-gfm cmark_utf8proc_case_fold (arm64): 2161 -> 1837 case labels,
17 -> 12 switches; GED vs source 2726 -> 1744.

[AUTOMATED]
…(objcsdk)

A for-in loop sends countByEnumeratingWithState:objects:count: with the
address of a stack NSFastEnumerationState. The header reader spelled the
NSFastEnumerationState * parameter as a class pointer and could not spell
the (id [_Nonnull]) buffer or NSDictionary's generic (K []) form, so the
selector had no prototype, and the encoding decoder made every ^{...}
pointer a void *. The state printed as eight loose locals and the loop
read *(objc_object **)(v8 + v7 * 8) for state.itemsPtr[i].

The reader now lays out a pointer to a typedef'd struct with its member
names (^{NSFastEnumerationState="state"Q"itemsPtr"^@...}), spells an array
parameter as a pointer, a scalar pointer with its pointee and a generic
parameter as id; the decoder turns a named struct layout behind a pointer
into a pointer to that struct. The typelocked parameter then types the
stack object (gatherOpen), so the state is one local.

Perplexity 26.46: 3/3 for-in loops of a 390-function sample and 14/14 of
AppAuthCore print one NSFastEnumerationState and state.itemsPtr[i].
…ort's address

A Mach-O __got slot of a data import (a Swift witness table, type metadata or conformance descriptor, an Objective-C class object) holds the import's address. Code that passes that address on, to swift_getWitnessTable or as a message receiver, printed the slot as dat_<slot>. With importdata on, a read of the value the loader put in a slot machodataslots marks as data now renders &<import>; function-import slots and values the function stored are unchanged.

[AUTOMATED]
coleleavitt and others added 13 commits November 22, 2026 03:22
A Swift switch over a multi-payload enum indexes its jump table with the
case swift_getEnumCaseMultiPayload returns, with no range check. When the
table is its function's last, or the extent scan dropped the next table,
switchextent had no bound and the dispatch stayed a computed call.

The swiftmetadata pass now records each local enum's case count (payload
cases plus empty cases, from the enum descriptor) by its metadata
accessor. The extent scan follows an accessor's result in a preserved
register into swift_getEnumCaseMultiPayload and the returned case into a
table, and the re-bound uses that count. The scan's register set also
drops its oldest value when full instead of refusing new ones, so a run
of constants early in a function no longer hides later tables.

[AUTOMATED]
…an in-instruction branch as unknown

Chapter 1 describes the discovery Listing's switchextent fold, which this
change also touches; say there that a csel/csinc result is unknown to it.
Rewrap the chapter 2 line the change left at 130 columns.

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

Chapter 1's swiftmetadata section lists what the pass reads from a nominal
type descriptor; an enum's payload and empty case counts are now among them,
handed to the engine by the enum's metadata accessor for switchextent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
A data-import slot's value read by a mask, shift, multiply or division
printed `&OBJC_CLASS_$_NSObject >> 0x20` and `&Swift.Int.type_metadata | 1`,
which C rejects. Such a read, and a read narrower than the slot, keep
`dat_<slot>` as before; an addition still reads `&<import> + 8`. The check
returns early when the image marked no data-import slot.

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

The plan drops or moves an arm only when the arm printed before it cannot
run into it, and it judged that from the case's structured out-edges. Two
kinds of arm run into the next arm with no such edge: one whose trailing
jump goes to the arm after it in the switch's own order, which gotonext
prints as nothing, and one whose tail the cross-jump reverter duplicated
(the duplicated leaf carries no edges). Dropping the hole arm after either
sent the arm's values into a different case. falls_through now asks
whether the arm's printed code can run past its end: only a leaf whose
basic block has no successor and a BlockGoto whose jump prints cannot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
A struct decoded from an encoding padded every layout to a pointer and
placed a nested struct's members with no trailing padding. Now that a
pointer to a struct types the stack object a caller passes, a 12-byte
`{Tri2=iii}`, a 4-byte `{Pair16=ss}` or a 3-byte `{Three=ccc}` claimed
eight bytes too many and swallowed the neighbouring spill into a char array
(`*(objc_object **)&v2[0xc] = a0`), and `{Outer={Inner=ic}ci}` put `c` at 5,
not 8. The size is now padded to the widest member's alignment and a nested
struct is aligned and padded as a unit, so those stack objects read
`v2.x + v2.z`, `v2.a + v2.b` and `v2.c1 + v2.c3`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
… not on the fold's 16-value cap

pick indexed its table with `ldrb w11, [x9, x8]`. SLEIGH lifts that index
through an INT_LEFT by 0, which the fold never folds, so a constant index
never made pick's load address constant there. What hid pick's table was
the fold's 16-value cap: the csel/csinc chain's copies filled it, and the
dispatch's last values were refused. With the join, fewer values are
tracked, so the test passed for that reason, and Noelo-Lab#1877's change to drop
the oldest value passes it too, without this change.

pick now adds the index to the table's address (`add x9, x9, x8;
ldrb w11, [x9]`), so a constant index really makes the load one from a
constant address. The stage test now fails on main and with Noelo-Lab#1877 alone,
and passes with the join. Fixture rebuilt with LLVM 21.1.8, README row,
stage bytes and comment updated; assertions unchanged. The chapter 2
sentence now names that shape.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
The decoder reads a bitfield code `b<n>` as a 4-byte uint, so a layout
such as `^{Packed=b8b4b20[8S]}` gave each bitfield its own word: 36 bytes
for a 20-byte struct, with the array at the wrong offset. The encoding does
not say how the bits pack, so such a struct is not laid out; the pointer
stays `void *`, as before this change.

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

mahaloz commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

[AUTOMATED] Pushed three commits on top of this branch:

  • 3596a5d merges current main (no conflicts).
  • ba2e6a6 fixes how the plan decides whether an arm runs into the next one. That check used to read the case's structured out-edges. Two kinds of arm run into the next arm without such an edge: one whose trailing jump to the next arm is left out by gotonext, and one whose tail the cross-jump reverter copied into it (the copied leaf has no edges). If the arm after one of these was dropped or moved, the values of the arm before it ran a different case. falls_through now asks whether the arm's printed code can run past its end (runs_past_end). The spec paragraph is updated too.
  • abe66b7: an inner switch followed by more code in default: also stays nested when it has a bare case k: break; arm. Merged, that break; would leave the outer switch and skip the code.

I found the first issue by recompiling decompiled switches and running them against their source, on gcc/clang -O2/-O3 builds for x86-64, AArch64 and ARM. It shows up most easily together with #1872's hole drops: on clang -O2 AArch64, case 0x90: v1 = ...; fell into case 0xa1:. With these fixes, 0 of the 385 functions this PR changes behave differently from their source.

…nested

An inner switch that is followed by more code in the default arm merges
only when no arm leaves it, since its unlisted values must reach that code.
The check covered fall-through exits and breaks inside arms but not a bare
`case k: break;` arm (a goto case to the switch's exit), which in the merged
switch would leave the outer switch and skip the code. Such a switch now
stays nested. 0 of 8,588 corpus functions change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
@mahaloz
mahaloz merged commit 4553cb0 into Noelo-Lab:main Nov 22, 2026
mahaloz pushed a commit that referenced this pull request Nov 22, 2026
…ad guarded tables past jumptablemax (#1872)

Extends switchmerge (stacked on #1863). An arm that is only a goto or a break
now prints no labels when the default code reaches its target for each of its
values before it prints a statement. Every test and switch on the selector is
decided with the value, and the walk stops at a back edge. An `if (v == c) S`
with no else at the head of a default arm prints as `case c: S`. An
`if (v relop K)` whose two branches are switches on the selector prints as one
switch when every value takes its own branch and the two defaults do the same.
The planner reads each arm's labels in one pass over the table. jumptablemax
stays 1024, but a table that a guard bounds to at most 65536 entries is read to
that bound. It is refused when it reaches more than jumptablemax distinct
destinations, or when it admits more than four values per table slot it reads.
For example, gcc's `if (x <= 0x28ff) goto tbl[x >> 8]` stays a computed call.
New stage tests kuna-switchmerge-values, kuna-switchmerge-split and
kuna-jumptableslots. 0/675 datatest assertions move.

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