Repository navigation
[AUTOMATED] switchextent: bound a Swift enum's jump table by the enum's case count - #1877
Merged
Merged
Conversation
…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]
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
…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
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
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
Contributor
|
[AUTOMATED] I pushed two commits on top of your head:
There are no code changes. I checked the case-count bound against the jump tables swiftc emits ( |
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
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0179iMKNhsvezm24JAGcKtoV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[AUTOMATED]
A Swift
switchover a multi-payload enum indexes its jump table with the caseswift_getEnumCaseMultiPayloadreturns 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:With this change the same function prints
switch(swift_getEnumCaseMultiPayload(a0,v1) & 0xffffffff)with its three cases. (The fixture binary is committed, sokuna decompile decompiler/crates/kuna-analysis/tests/fixtures/swiftenumcase_aarch64 dispatchworks too.)Fix
swiftmetadatarecords 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.switchextentextent scan now follows calls. An accessor's result, kept in a callee-saved register, is the enum's metadata. Aswift_getEnumCaseMultiPayloadcall 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.last_two's first table inkuna-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 withswitchextent offorswiftmetadata off, and on the unpatched tree.decompile-allon a 180 KB Swift framework prints the same output at the same speed.Model:
anthropic/claude-opus-5-5:high