Repository navigation
fix(native): select one slice when universal Mach-O slices share a CPU type - #2040
Conversation
…U type On macOS 27 most system binaries are universal files whose slices share CPU_TYPE_ARM64: arm64e + arm64e.x1 (Calculator, TextEdit), x86_64 + arm64e + arm64e.x1 (/bin/ls), or arm64 + arm64e.x1 (ShortcutDroplet). Target admission names slices by CPU family, so these targets have availableArchitectures ["arm64"] while the file holds two arm64 slices. inspect_macho read that family count as a slice count and ran otool, nm, dyld_info, dwarfdump and vtool without -arch. ShortcutDroplet failed with "Malformed otool load-command output" (40 load commands against ncmds 20). Calculator reported both slices' imports (4138 instead of 2069), nm's "(for architecture ...)" header lines as exports, and no selected slice. selectArchitecture now also filters when lipo lists more than one slice. Its preference is unchanged: the family name, then arm64e, then the first arm64e.<variant>. inspect_native_dispatch_metadata matched FAT slices by CPU type only and refused /bin/ls and TextEdit as "Ambiguous FAT architecture slice". selectMachoSlice now ranks slices of the requested CPU type by base subtype in the same order: arm64 (0, then 1) or x86_64 (3), then arm64e (2), then other variants such as arm64e.x1 in FAT table order. It reports ambiguity only when the top-ranked slices share a subtype. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9c74fba85e
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| * FAT table order. | ||
| */ | ||
| const PREFERRED_CPU_SUBTYPES: Readonly<Record<number, readonly number[]>> = { | ||
| 0x0100000c: [0, 1, 2], |
There was a problem hiding this comment.
Match the arm64v8 fallback order across selectors
When a universal file contains an arm64v8 slice (base subtype 1) and an arm64e slice (base subtype 2), this ordering makes selectMachoSlice(..., "arm64") choose V8, while selectArchitecture in NativeMachoInspection.ts ignores arm64v8 and falls back to the exact arm64e name. The two public native inspections therefore read different physical slices of the same admitted target (where dispatch previously rejected the ambiguity), so this ranking should come from the same owned selection policy rather than introducing a conflicting subtype order.
AGENTS.md reference: AGENTS.md:L23-L23
Useful? React with 👍 / 👎.
dandgabr
left a comment
There was a problem hiding this comment.
Review corrections
- P2 — Keep native inspection and dispatch metadata on the same ARM64 slice.
selectMachoSlice()ranks ARM64 subtype 1 (arm64v8) ahead of subtype 2 (arm64e), butselectArchitecture()does not recognizearm64v8and prefers the namedarm64eslice. A universal file containing both can therefore produce dispatch metadata from one slice and runinspect_machotools against another. Align the subtype ranking with the names accepted byselectArchitecture()(or map subtype 1 explicitly) and add a mixed-slice regression.
|
@greptileai review |
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7456d81590
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| ): string | undefined => { | ||
| for (const { name } of policy.subtypes) | ||
| if (available.includes(name)) return name; | ||
| return available.find((name) => policy.variantName?.test(name)); |
There was a problem hiding this comment.
Rank arm64e variants by parsed subtype
When lipo reports an arm64e.v1 slice alongside arm64v8, this loop finds arm64v8 among the exact names before consulting variantName. The supported arm64e.v1 spelling is fresh evidence beyond the earlier exact-arm64e ordering thread: cpuSubtypeCode in src/native/parsers/lipo.ts decodes it as 0x81000002, so selectMachoSlice strips the capability byte and ranks it as base subtype 2 ahead of subtype 1, making inspect_macho and dispatch inspection analyze different physical slices. Rank the parsed lipo subtype instead of assigning every dotted variant the final fallback.
AGENTS.md reference: AGENTS.md:L19-L19
Useful? React with 👍 / 👎.
| export const baseMachoCpuSubtype = (subtype: number): number => | ||
| subtype & 0x00ffffff; |
There was a problem hiding this comment.
Reuse the domain CPU-subtype normalizer
baseMachoCpuSubtype duplicates the capability-mask normalization already exported as machoCpuSubtypeBase by src/domain/apple/machoCpuIdentity.ts. Keeping dispatch selection on this native-layer alias while artifact/load-command consumers use the domain owner establishes two normalization boundaries that can diverge when subtype handling changes; import the existing helper and remove this copy.
AGENTS.md reference: AGENTS.md:L23-L23
Useful? React with 👍 / 👎.
Summary
On macOS 27, most system binaries are universal files whose slices all have
CPU_TYPE_ARM64. Calculator and TextEdit are arm64e + arm64e.x1,/bin/lsis x86_64 + arm64e + arm64e.x1, and ShortcutDroplet is arm64 + arm64e.x1. Target admission names slices by CPU family, so these targets getavailableArchitectures: ["arm64"]even though the file holds two arm64 slices. Two native tools treated that one-entry family list as if it were the slice list:inspect_machodecided the file needed no-archand ran otool, nm, dyld_info, dwarfdump and vtool against both slices. ShortcutDroplet fails outright, and Calculator returns both slices' data mixed into one result.inspect_native_dispatch_metadatamatched FAT slices by CPU type alone and refuses/bin/lsand TextEdit as "Ambiguous FAT architecture slice".After this change, both tools pick exactly one slice, using the same preference order.
I didn't open an issue first. I found this while checking the other arm64e.x1 paths after #1765. That issue's naming fix landed in f598bab, and #1772 covers its dyld-resolution side. Neither touches the two functions changed here.
Problem and expected behavior
Smallest trigger:
rea inspect-macho /System/Library/CoreServices/ShortcutDroplet.app/Contents/MacOS/ShortcutDroplet. lipo lists two slices,arm64andarm64e.x1. Because the target has one CPU family,selectArchitecturereturned "unfiltered" and otool printed both slices. The parser then rejected 40 load commands againstncmds 20.The broken invariant: a slice tool must get
-archwhenever the file holds more than one slice. One slice per CPU family isn't guaranteed. Likewise,selectMachoSliceshould choose one slice when several share the requested CPU type, the way lipo's family names do, and report ambiguity only for slices it really can't tell apart.Change and scope
src/native/NativeMachoInspection.ts:selectArchitecturenow also filters when lipo listed more than one slice. Before, it only checked the family count. The existing preference is unchanged: the exact family name, thenarm64e, then the firstarm64e.<variant>. I kept the family check as well, so a file whose header has two families but whose lipo output lists fewer still gets today's "lipo did not list X" refusal. It doesn't fall back to an unfiltered run.src/native/AppleMachoSelection.ts:selectMachoSliceranks slices of the requested CPU type by base subtype, with the capability byte masked off. The order is arm64 (0, then 1) or x86_64 (3), then arm64e (2), then any other variant (arm64e.x1 is 12, x86_64h is 8) in FAT table order. It still throws "Ambiguous FAT architecture slice" when the top-ranked slices share a base subtype. So the identical-entry FAT64 test inAppleDispatchMetadata.test.tspasses unchanged.On the four binaries above, both tools now read the same slice: arm64e for Calculator, TextEdit and
/bin/ls, and arm64 for ShortcutDroplet.I fixed the slice decision in these two functions rather than admission. BinaryTarget's family grouping is intentional (see the comment in
selectArchitecture), and lipo and the FAT table are where the slice facts come from. Ghidra admission has the same root cause (below), so I've left it for a separate change.Not included:
src/ghidra/GhidraProvider.ts, theavailableArchitectures.length > 1check). An arm64e + arm64e.x1 file reports one CPU family, so it gets past the "analyze a thinned Mach-O slice instead" refusal, which then never fires for it. Testing a fix needs a real Ghidra, which I don't have set up.NativeMacOSProvider.ts(around line 997) doesn't acceptarm64e.x1.endianis alwaysnullon real universal binaries, becausefile -bdoesn't print an endianness for them.machoCpuSubtypeBasehelper. This PR keeps a local mask so the two don't conflict. Whichever lands second can switch over.Contract and boundary impact
selectArchitecture(tool arguments forinspect_macho) andselectMachoSlice(FAT selection for dispatch metadata). Admission and BinaryTarget are unchanged.inspect_machonow fillsselected_architecture, andinspect_native_dispatch_metadatareturns a result instead of an execution failure.inspect_machoevidence for these files now describes one slice. The provenance shows the-archarguments, and slice-based evidence locations now appear (Calculator has 7 locations, up from 2), with the "selected Mach-O slice is unknown" limitation gone.-archor--arch=added. Nothing else changes.docs/public/product-catalog.json), package, or installation impact: none.Evidence and regression coverage
Tests added or updated:
tests/boundary/providers/native/nativeSelectedArchitecture.test.ts: I moved the existing "fail any slice command without the expected-arch" runner into a shared helper and added two cases that use reallipo -detailed_infocaptures from Calculator and ShortcutDroplet, with the target set toavailableArchitectures: ["arm64"]as admission produces. The captures and their provenance are intests/fixtures/native-macos/lipo-same-cpu/.src/native/AppleMachoSelection.test.ts: FAT fixtures with the real macOS 27 subtypes. arm64e (0x80000002) + arm64e.x1 (0x8000000c) and arm64 (0x0) + arm64e.x1 run in both orders, plus the/bin/lslayout and x86_64h + x86_64. An arm64e + arm64e duplicate must still be reported as ambiguous.Base reproduction or other evidence: with only the tests applied on top of ee50ed1,
npm run test:focusedon the two test files gave 8 failed, 18 passed. The 6 selection cases threw "Ambiguous FAT architecture slice", and the 2 real-capture cases failed because otool ran without-arch. The duplicate case and the refactored existing cases passed.User-visible CLI/MCP output:
node scripts/rea.mjs <command> <path> --format jsonon macOS 27.0.1 (26A434), arm64, built from ee50ed1 before the change and from this branch after it:inspect-machoShortcutDropletunreadable_output: "Malformed otool load-command output" (2 Mach headers, 40 load commands,ncmds20 each)selected_architecture{ name: "arm64", container_file_offset: 16384 }, 20 load commands, every slice tool ran with-arch arm64inspect-machoCalculatorselected_architecture{ name: null, container_file_offset: null }, 4138 imports, and 2 of 3 exports were nm's… (for architecture arm64e):/(… arm64e.x1):header lines{ name: "arm64e", container_file_offset: 16384 }, 2069 imports, 1 export (__mh_execute_header)inspect-native-dispatch-metadata/bin/lsDYLD_CHAINED_PTR_ARM64Einspect-native-dispatch-metadataTextEditTextEdit's Swift conformance and vtable coverage is
partial, because of decoder limits that have nothing to do with slice selection. It was never reached before this change, since the run failed earlier.Remaining proof gaps:
arm64v8) rank right after plain arm64 in the dispatch selector. inspect_macho's name matching has noarm64v8rule, so the two selectors could still disagree on such a file. I didn't try to find one.endianitems above remain open.For evidence-bearing changes:
Validation performed
All runs used Node 24.18.0 and npm 11.16.0 on macOS 27.0.1, arm64.
npm ci: installed.npm run test:focused -- src/native/AppleMachoSelection.test.ts tests/boundary/providers/native/nativeSelectedArchitecture.test.tswith only the tests applied on ee50ed1: 8 failed, 18 passed (described above).npm run test:focused -- src/native/AppleMachoSelection.test.ts tests/boundary/providers/native/nativeSelectedArchitecture.test.ts src/native/AppleDispatchMetadata.test.ts src/native/NativeMachoInspection.test.ts tests/boundary/providers/native/nativeProvider.test.ts tests/boundary/providers/native/nativeMachoHeader.golden.test.ts tests/boundary/cli/nativeMachoEchoedPathCli.test.ts: 7 files, 97 tests passed.npx vitest run src/native tests/boundary/providers/native src/artifacts/apple src/domain/apple tests/boundary/cli/nativeMacho: 55 files, 518 tests passed.npm run test:fast: 392 files, 5035 passed, 7 skipped.npm run check:fast: passed.npm run check: passed (typecheck, lint and module boundaries, format check, knip, metadata, architecture guards).npm run verify:test-discovery: "Discovered all 901 test files exactly once."git diff --check: clean.npm run build:cached: passed.I didn't run
npm run docs:checkornpm run check:pr, since no contracts, docs or generated metadata changed.Compatibility, safety, and release
-archargument.Review checklist
type(scope): outcome.🤖 Generated with Claude Code