Skip to content

fix(native): select one slice when universal Mach-O slices share a CPU type - #2040

Merged
morluto merged 4 commits into
morluto:mainfrom
hemanthrayuduu:fix/macho-same-cpu-slices
Oct 13, 2026
Merged

morluto merged 4 commits into
morluto:mainfrom
hemanthrayuduu:fix/macho-same-cpu-slices

Conversation

@hemanthrayuduu

Copy link
Copy Markdown
Contributor

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/ls is x86_64 + arm64e + arm64e.x1, and ShortcutDroplet is arm64 + arm64e.x1. Target admission names slices by CPU family, so these targets get availableArchitectures: ["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_macho decided the file needed no -arch and 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_metadata matched FAT slices by CPU type alone and refuses /bin/ls and 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, arm64 and arm64e.x1. Because the target has one CPU family, selectArchitecture returned "unfiltered" and otool printed both slices. The parser then rejected 40 load commands against ncmds 20.

The broken invariant: a slice tool must get -arch whenever the file holds more than one slice. One slice per CPU family isn't guaranteed. Likewise, selectMachoSlice should 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: selectArchitecture now 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, then arm64e, then the first arm64e.<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: selectMachoSlice ranks 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 in AppleDispatchMetadata.test.ts passes 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:

  • Ghidra admission (src/ghidra/GhidraProvider.ts, the availableArchitectures.length > 1 check). 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.
  • The codesign per-slice regex in NativeMacOSProvider.ts (around line 997) doesn't accept arm64e.x1.
  • A FAT file whose only x86_64 slice is x86_64h. inspect_macho's lipo-name matching has no rule for it.
  • endian is always null on real universal binaries, because file -b doesn't print an endianness for them.
  • fix(apple): preserve slice identity and macOS boundary diagnostics #1772 adds a shared machoCpuSubtypeBase helper. This PR keeps a local mask so the two don't conflict. Whichever lands second can switch over.

Contract and boundary impact

  • Semantic owner and earliest changed stage: slice selection in the macOS native provider. That's selectArchitecture (tool arguments for inspect_macho) and selectMachoSlice (FAT selection for dispatch metadata). Admission and BinaryTarget are unchanged.
  • CLI and MCP/tool-catalog contract: no schema changes. For these files, inspect_macho now fills selected_architecture, and inspect_native_dispatch_metadata returns a result instead of an execution failure.
  • Provider, bridge, target-format, or platform compatibility: native macOS provider only. Thin files, one-slice FAT files and FAT files with one slice per family take the same path as before.
  • Evidence, artifact, provenance, or reconstruction contract: inspect_macho evidence for these files now describes one slice. The provenance shows the -arch arguments, and slice-based evidence locations now appear (Calculator has 7 locations, up from 2), with the "selected Mach-O slice is unknown" limitation gone.
  • Process execution, authorization, cleanup, or containment impact: same tools, now with -arch or --arch= added. Nothing else changes.
  • Generated metadata (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 real lipo -detailed_info captures from Calculator and ShortcutDroplet, with the target set to availableArchitectures: ["arm64"] as admission produces. The captures and their provenance are in tests/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/ls layout 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:focused on 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 json on macOS 27.0.1 (26A434), arm64, built from ee50ed1 before the change and from this branch after it:

    Target Before After
    inspect-macho ShortcutDroplet exit 1, unreadable_output: "Malformed otool load-command output" (2 Mach headers, 40 load commands, ncmds 20 each) exit 0, selected_architecture { name: "arm64", container_file_offset: 16384 }, 20 load commands, every slice tool ran with -arch arm64
    inspect-macho Calculator exit 0, but selected_architecture { name: null, container_file_offset: null }, 4138 imports, and 2 of 3 exports were nm's … (for architecture arm64e): / (… arm64e.x1): header lines exit 0, { name: "arm64e", container_file_offset: 16384 }, 2069 imports, 1 export (__mh_execute_header)
    inspect-native-dispatch-metadata /bin/ls exit 1: "Ambiguous FAT architecture slice (FAT header byte order: big-endian)" exit 0. No ObjC or Swift records, as you'd expect from a C tool. Chained fixups coverage DYLD_CHAINED_PTR_ARM64E
    inspect-native-dispatch-metadata TextEdit exit 1, same error exit 0: 38 ObjC classes, 7 protocols, 84 ivars, 341 dispatch implementations, 34 Swift dispatch slots, 728 relative pointers. All 3190 file offsets fall between 22060 and 217776, inside the arm64e slice (16384–427424) and not arm64e.x1 (from 442368)

    TextEdit'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:

    • I only ran this on one macOS 27.0.1 arm64 host. On an x64 host, admission picks the x86_64 slice, which this change doesn't affect, and I didn't test that.
    • Slices with subtype 1 (lipo calls them arm64v8) rank right after plain arm64 in the dispatch selector. inspect_macho's name matching has no arm64v8 rule, so the two selectors could still disagree on such a file. I didn't try to find one.
    • The Ghidra, codesign, x86_64h-only and endian items above remain open.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

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.ts with 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.
  • The CLI runs in the table above, before and after.

I didn't run npm run docs:check or npm run check:pr, since no contracts, docs or generated metadata changed.

Compatibility, safety, and release

  • Breaking changes or migration steps: none. One behavior change sits outside the bug, and I got it from reading the code; no test covers it. A FAT file with several 32-bit slices of one CPU type (for example armv7 + armv7s) used to fail with "Ambiguous FAT architecture slice". Now the first slice is selected, and dispatch metadata fails with the existing "Unsupported 32-bit Mach-O metadata" error instead.
  • Real Hopper/Ghidra, browser, or OS coverage: real macOS 27.0.1 Apple tools via the CLI runs above. I didn't run Hopper or Ghidra, and neither is touched.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: no new processes or paths. The same Apple tools run with an added -arch argument.

Review checklist

  • This PR addresses a concrete problem or an agreed enhancement.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

🤖 Generated with Claude Code

…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]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T08:38:29.793008Z 7456d81 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/native/AppleMachoSelection.ts Outdated
* FAT table order.
*/
const PREFERRED_CPU_SUBTYPES: Readonly<Record<number, readonly number[]>> = {
0x0100000c: [0, 1, 2],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 dandgabr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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), but selectArchitecture() does not recognize arm64v8 and prefers the named arm64e slice. A universal file containing both can therefore produce dispatch metadata from one slice and run inspect_macho tools against another. Align the subtype ranking with the names accepted by selectArchitecture() (or map subtype 1 explicitly) and add a mixed-slice regression.

@morluto

morluto commented Oct 13, 2026

Copy link
Copy Markdown
Owner

@greptileai review

@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new blocking issue was established.

Summary

The PR makes native Mach-O inspection choose one slice when several slices share a CPU family.

  • Mach-O inspection tools now read one preferred slice from universal files.
  • Dispatch metadata picks a preferred slice when CPU types repeat.

No new actionable issues were established. No previous review threads were supplied. The merge bases differ, so this review used the full PR diff. GitHub access was unavailable, so whether another PR supersedes this one could not be checked.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Target CPU family] --> B[Shared slice preferences]
  B --> C[lipo slice names]
  B --> D[FAT CPU subtypes]
  C --> E[Choose tool architecture]
  E --> F[Run Apple tools on selected slice]
  D --> G[Check ranges and rank slices]
  G --> H{Best slice duplicated?}
  H -->|Yes| I[Reject ambiguity]
  H -->|No| J[Check selected header and read metadata]
Loading

Reviews (2) · Last reviewed commit: "chore: preserve upstream process capture..." · Reviewed by Greptile

@morluto
morluto merged commit 96fc84f into morluto:main Oct 13, 2026
20 checks passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment on lines +32 to +33
export const baseMachoCpuSubtype = (subtype: number): number =>
subtype & 0x00ffffff;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants