diff --git a/src/artifacts/MachOSliceArtifactReader.ts b/src/artifacts/MachOSliceArtifactReader.ts index f9e179981..9420bef26 100644 --- a/src/artifacts/MachOSliceArtifactReader.ts +++ b/src/artifacts/MachOSliceArtifactReader.ts @@ -8,6 +8,7 @@ import { type NativeCommandRunner, } from "../native/CommandRunner.js"; import { + lipoSubtypeBits, parseLipoArchitectures, type LipoArchitecture, } from "../native/parsers/lipo.js"; @@ -451,14 +452,27 @@ const lipoMatchesSlice = ( (reported.size === null || reported.size === observed.slice_size) && (reported.cpu_type_code === null || reported.cpu_type_code === observedCpuType) && - (reported.cpu_subtype_code === null || - reported.cpu_subtype_code === observedCpuSubtype) && + lipoSubtypeMatches(reported, observedCpuSubtype) && (reported.alignment === null || observed.fat_alignment_exponent === null || reported.alignment === 2 ** observed.fat_alignment_exponent) ); }; +/** + * Compare the complete subtype when lipo printed its capability byte, and + * otherwise only the subtype bits its record names. + */ +const lipoSubtypeMatches = ( + reported: LipoArchitecture, + observedCpuSubtype: number, +): boolean => { + if (reported.cpu_subtype_code !== null) + return reported.cpu_subtype_code === observedCpuSubtype; + const bits = lipoSubtypeBits(reported.cpu_subtype); + return bits === null || bits === (observedCpuSubtype & 0x00ffffff); +}; + /** * Newer lipo names an arm64e ABI variant `arm64e.` (for example * `arm64e.v1` for pointer-auth ABI version 1). The FAT table names that slice diff --git a/src/native/parsers/lipo.test.ts b/src/native/parsers/lipo.test.ts index 4de28398f..43f08d8b8 100644 --- a/src/native/parsers/lipo.test.ts +++ b/src/native/parsers/lipo.test.ts @@ -62,4 +62,31 @@ architecture arm64e }, ]); }); + + it.each([ + ["CPU_SUBTYPE_LIB64", 0x80000003], + ["0x80", 0x80000003], + ["0x0", 3], + ])("applies printed x86_64 capabilities %s", (capabilities, code) => { + expect( + parseLipoArchitectures( + `architecture x86_64\n cpusubtype CPU_SUBTYPE_X86_64_ALL\n capabilities ${capabilities}\n`, + )[0]?.cpu_subtype_code, + ).toBe(code); + }); + + it("leaves the subtype code unknown when lipo prints no capabilities", () => { + // macOS 26 lipo prints this blank line for LIB64 and plain x86_64 slices. + expect( + parseLipoArchitectures( + "architecture x86_64\n cputype CPU_TYPE_X86_64\n cpusubtype CPU_SUBTYPE_X86_ALL\n capabilities \n offset 16384\n", + ), + ).toMatchObject([ + { + cpu_subtype: "CPU_SUBTYPE_X86_ALL", + cpu_subtype_code: null, + capabilities: null, + }, + ]); + }); }); diff --git a/src/native/parsers/lipo.ts b/src/native/parsers/lipo.ts index 4fb13cda7..4f56d8b6b 100644 --- a/src/native/parsers/lipo.ts +++ b/src/native/parsers/lipo.ts @@ -128,7 +128,7 @@ const cpuTypeCode = (value: string | undefined): number | null => { return numeric ?? CPU_TYPES[value] ?? null; }; -/** Normalize the subtype and its separately printed ptrauth capability bits. */ +/** Normalize the subtype and its separately printed capability bits. */ const cpuSubtypeCode = ( value: string | undefined, capabilities: string | undefined, @@ -138,7 +138,11 @@ const cpuSubtypeCode = ( if (numeric !== null) return numeric; const subtype = CPU_SUBTYPES[value]; if (subtype === undefined) return null; - if (value !== "CPU_SUBTYPE_ARM64E") return subtype; + if (value !== "CPU_SUBTYPE_ARM64E") { + // Recent lipo prints an empty capabilities line, even for LIB64 slices. + const bits = capabilityBits(capabilities); + return bits === null ? null : ((bits << 24) | subtype) >>> 0; + } const version = /PTR_AUTH_VERSION\s+USERSPACE\s+(\d+)/u.exec( capabilities ?? "", )?.[1]; @@ -153,6 +157,20 @@ const cpuSubtypeCode = ( return (0x80000000 | (parsedVersion << 24) | subtype) >>> 0; }; +/** The subtype bits a lipo record names, without its capability byte. */ +export const lipoSubtypeBits = (value: string | null): number | null => { + if (value === null) return null; + const subtype = unsigned32(value) ?? CPU_SUBTYPES[value]; + return subtype === undefined ? null : subtype & 0x00ffffff; +}; + +/** lipo names the LIB64 capability and prints other capability bits in hex. */ +const capabilityBits = (capabilities: string | undefined): number | null => { + if (capabilities === "CPU_SUBTYPE_LIB64") return 0x80; + const bits = /^0x([\da-f]{1,2})$/iu.exec(capabilities ?? "")?.[1]; + return bits === undefined ? null : Number.parseInt(bits, 16); +}; + const unsigned32 = (value: string): number | null => { if (!/^(?:\d+|0x[\da-f]+)$/iu.test(value)) return null; const parsed = Number(value); diff --git a/tests/boundary/providers/native/artifactProvider.machOSlices.test.ts b/tests/boundary/providers/native/artifactProvider.machOSlices.test.ts index 812cc2df7..eedf1eb80 100644 --- a/tests/boundary/providers/native/artifactProvider.machOSlices.test.ts +++ b/tests/boundary/providers/native/artifactProvider.machOSlices.test.ts @@ -291,3 +291,61 @@ describe("artifact Mach-O arm64e variant slices", () => { }, ); }); + +// macOS 26 lipo leaves capabilities blank, even for a LIB64 x86_64 slice. +describe("artifact Mach-O LIB64 slices", () => { + const LIB64 = { type: 0x01000007, subtype: 0x80000003 } as const; + const lib64Slices = async (x86Subtype: string) => { + const root = await createTestTempDirectory("rea-slices-lib64-"); + const binary = join(root, "fat"); + const x86 = machoImage({ cpu: LIB64 }); + const arm = machoImage({ cpu: CPU.arm64 }); + await writeFile( + binary, + fatImage([ + { cpu: LIB64, bytes: x86 }, + { cpu: CPU.arm64, bytes: arm }, + ]), + ); + const reader = new MachOSliceArtifactReader( + binary, + {}, + lipoRunner( + [ + "architecture x86_64", + " cputype CPU_TYPE_X86_64", + ` cpusubtype ${x86Subtype}`, + " capabilities ", + " offset 4096", + ` size ${String(x86.length)}`, + " align 2^12 (4096)", + "architecture arm64", + " cputype CPU_TYPE_ARM64", + " cpusubtype CPU_SUBTYPE_ARM64_ALL", + " capabilities ", + " offset 8192", + ` size ${String(arm.length)}`, + " align 2^12 (4096)", + "", + ].join("\n"), + ), + ); + onTestFinished(() => reader.close()); + const entries = []; + for await (const entry of reader.entries()) entries.push(entry); + return entries.map(({ path, byteOffset }) => [path, byteOffset]); + }; + + it("matches an x86_64 LIB64 slice whose lipo capabilities are blank", async () => { + expect(await lib64Slices("CPU_SUBTYPE_X86_ALL")).toEqual([ + ["slices/x86_64", 4096], + ["slices/arm64", 8192], + ]); + }); + + it("still rejects a blank-capability subtype that names other bits", async () => { + await expect(lib64Slices("CPU_SUBTYPE_X86_64_H")).rejects.toMatchObject({ + reason: "integrity", + }); + }); +});