fix(native): select one slice when universal Mach-O slices share a CPU type (#2040)
* fix(native): select one slice when universal Mach-O slices share a CPU 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 <noreply@anthropic.com> * fix(native): share Mach-O slice preference across inspections * chore: preserve upstream process capture test during merge --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
morluto
parent
b69d20a300
commit
96fc84f1fb
@@ -1,7 +1,13 @@
|
||||
import type { FixupCommands } from "./AppleMachoFixups.js";
|
||||
import {
|
||||
MACHO_ARCHITECTURE_POLICIES,
|
||||
baseMachoCpuSubtype,
|
||||
machoSubtypeRank,
|
||||
} from "./MachoArchitectureSelection.js";
|
||||
import {
|
||||
readFatSliceDeclarations,
|
||||
validateFatSlice,
|
||||
type FatSliceDeclaration,
|
||||
} from "../domain/apple/machoContainer.js";
|
||||
|
||||
/** One mapped Mach-O segment with file-backed VA range. */
|
||||
@@ -90,32 +96,44 @@ export const selectMachoSlice = (
|
||||
const headerEnd = 8 + count * stride;
|
||||
if (count > 128 || headerEnd > bytes.length)
|
||||
throw malformed("Malformed FAT architecture table");
|
||||
const cpu = MACHO_CPU_TYPE[architecture];
|
||||
if (cpu === undefined) throw invalid("Requested FAT architecture is absent");
|
||||
const policy = MACHO_ARCHITECTURE_POLICIES[architecture];
|
||||
if (policy === undefined)
|
||||
throw invalid("Requested FAT architecture is absent");
|
||||
const declarations = readFatSliceDeclarations(bytes.subarray(8, headerEnd), {
|
||||
count,
|
||||
wide: fat64,
|
||||
littleEndian,
|
||||
});
|
||||
if (declarations.status !== "parsed") throw malformed(declarations.reason);
|
||||
let selected: MachoSlice | undefined;
|
||||
let best: FatSliceDeclaration | undefined;
|
||||
let bestRank = Number.POSITIVE_INFINITY;
|
||||
let ambiguous = false;
|
||||
for (const declaration of declarations.slices) {
|
||||
const issue = validateFatSlice(declaration, bytes.length, headerEnd);
|
||||
if (issue !== null) throw malformed(issue);
|
||||
if (declaration.cpuType !== cpu) continue;
|
||||
if (selected !== undefined)
|
||||
throw invalid("Ambiguous FAT architecture slice");
|
||||
selected = {
|
||||
slice: declaration.offset,
|
||||
sliceEnd: declaration.offset + declaration.size,
|
||||
fatDeclaration: {
|
||||
...declaration,
|
||||
tableEnd: headerEnd,
|
||||
},
|
||||
};
|
||||
if (declaration.cpuType !== policy.cpuType) continue;
|
||||
const rank = machoSubtypeRank(policy, declaration.cpuSubtype);
|
||||
if (best !== undefined && rank >= bestRank) {
|
||||
// Equal ranks keep FAT table order unless the slices share a subtype.
|
||||
if (
|
||||
rank === bestRank &&
|
||||
baseMachoCpuSubtype(declaration.cpuSubtype) ===
|
||||
baseMachoCpuSubtype(best.cpuSubtype)
|
||||
)
|
||||
ambiguous = true;
|
||||
continue;
|
||||
}
|
||||
best = declaration;
|
||||
bestRank = rank;
|
||||
ambiguous = false;
|
||||
}
|
||||
if (selected === undefined)
|
||||
throw invalid("Requested FAT architecture is absent");
|
||||
if (best === undefined) throw invalid("Requested FAT architecture is absent");
|
||||
if (ambiguous) throw invalid("Ambiguous FAT architecture slice");
|
||||
const selected: MachoSlice = {
|
||||
slice: best.offset,
|
||||
sliceEnd: best.offset + best.size,
|
||||
fatDeclaration: { ...best, tableEnd: headerEnd },
|
||||
};
|
||||
if (selected.slice + 4 > bytes.length)
|
||||
throw malformed("Truncated Mach-O header");
|
||||
const sliceMagic = bytes.readUInt32LE(selected.slice);
|
||||
@@ -126,13 +144,6 @@ export const selectMachoSlice = (
|
||||
return selected;
|
||||
};
|
||||
|
||||
/** CPU types admitted by `machArchitecture` in the domain binary target parser. */
|
||||
const MACHO_CPU_TYPE: Readonly<Record<string, number>> = {
|
||||
x86: 7,
|
||||
x86_64: 0x01000007,
|
||||
arm: 12,
|
||||
arm64: 0x0100000c,
|
||||
};
|
||||
const MH_MAGIC = 0xfeedface;
|
||||
const MH_CIGAM = 0xcefaedfe;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user