Skip to content

[AUTOMATED] switchextent: bound a Swift enum's jump table by the enum's case count - #1877

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

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

Conversation

@coleleavitt

Copy link
Copy Markdown
Contributor

[AUTOMATED]

A Swift switch over a multi-payload enum indexes its jump table with the case swift_getEnumCaseMultiPayload returns and checks no range. When that table is its function's last, there is no next table to bound it, and the dispatch prints as a computed call that ends the function:

$ llvm-mc -triple=aarch64 -filetype=obj decompiler/crates/kuna-analysis/tests/fixtures/swiftenumcase_aarch64.s -o s.o
$ ld.lld --image-base=0x8000 -z max-page-size=0x1000 -Ttext=0x10000 -e dispatch s.o -o swiftenumcase_aarch64
$ kuna decompile swiftenumcase_aarch64 dispatch
  v1 = Shape_accessor(0);
  consume(a0);
  (*(void (*)(void))(dat_11088[swift_getEnumCaseMultiPayload(a0,v1) & 0xffffffff] * 4UL + 0x1004c))(); // jump-as-call

With this change the same function prints switch(swift_getEnumCaseMultiPayload(a0,v1) & 0xffffffff) with its three cases. (The fixture binary is committed, so kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/swiftenumcase_aarch64 dispatch works too.)

Fix

  • swiftmetadata records each local enum's case count by its metadata accessor: payload cases (low 24 bits of the descriptor word at +20) plus empty cases (+24). The count is exact, so a function's last table is bounded too.
  • The switchextent extent scan now follows calls. An accessor's result, kept in a callee-saved register, is the enum's metadata. A swift_getEnumCaseMultiPayload call whose second argument holds it returns a case. A table indexed by that case gets the count. Without metadata from a local accessor, the next-table rule applies as before.
  • The scan's 16-value register set now drops its oldest value when it is full, instead of refusing new values. Before, a run of constants early in a function could hide its later tables. This also bounds last_two's first table in kuna-switchnext-a64.xml, which now prints two switches instead of computed calls (new assertion Render unknown types as real C types (realtypes, DIV-6) #6, cleanup: Rust-first restructure + terminology refresh #3 tightened).

Tests

  • kuna-cli/tests/switchextent_cli.rs::a_swift_enum_table_has_its_enums_case_count: three cases with the change. It stays a computed call with switchextent off or swiftmetadata off, and on the unpatched tree.
  • Datatests 675/675 unchanged. Stage corpus 4939/4939.
  • On an iOS Swift app, across 251 functions that dispatch on this call's result: computed-call dispatches go from 247 to 14, and switches from 142 to 407. One 13 KB function goes from 1,345 lines with 24 cases to 1,918 lines with 48. No function lost a switch. decompile-all on a 180 KB Swift framework prints the same output at the same speed.

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

coleleavitt and others added 13 commits November 21, 2026 21:09
…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.
…(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]
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
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
@mahaloz

mahaloz commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] I pushed two commits on top of your head:

  • a merge of current main, with no conflicts;
  • one paragraph in chapter 1's swiftmetadata section (docs/spec/01-program-prep.md). It says the pass now also reads an enum's payload and empty case counts from its descriptor and hands them to the engine, keyed by the metadata accessor. The PR had described this only in chapter 2.

There are no code changes. I checked the case-count bound against the jump tables swiftc emits (swiftc -S): generic multi-payload enums at -O/-Osize, with library evolution, @frozen, shared-body and default cases, and two enums in one function, on x86-64 and AArch64. On libswift_RegexParser and libswift_StringProcessing, every count matched the table length. Enums whose accessor lives in another module, fixed-layout multi-payload enums and single-payload enums are left as before.

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 merged commit cba1aa1 into Noelo-Lab:main Nov 22, 2026
@coleleavitt
coleleavitt deleted the fix/enumswitch 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