fix(native): accept universal binaries with LIB64 x86_64 slices (#1982)
* fix(native): apply printed lipo capability bits to CPU subtypes lipo -detailed_info prints a slice's capability byte separately from its symbolic subtype: CPU_SUBTYPE_LIB64 for x86_64 and ppc64, and other bits as hex. Only the arm64e pointer-auth form was applied, so a LIB64 x86_64 slice normalized to subtype 3 while its FAT entry carries 0x80000003, and slice validation rejected the universal binary as an integrity mismatch. Fold the printed capability byte into the subtype code. * fix(native): leave lipo CPU subtypes unknown without capability bits The lipo shipped with macOS 26 prints an empty capabilities line for x86_64 slices whose FAT capability byte is 0x80 as well as 0x00, so a symbolic subtype alone does not identify the slice's CPU subtype. The parser assumed no capability bits and still reported subtype 3 for LIB64 slices, which made universal applications such as Sublime Text fail slice validation. Report the subtype code as unknown when lipo prints no capability byte, as arm64e already does; slice validation then relies on the remaining physical facts. * test(native): cover printed and blank lipo capability bits Fold CPU_SUBTYPE_LIB64 and hex capability bytes into x86_64 subtype codes, and keep the code unknown for the blank capabilities line that macOS 26 lipo prints. * test(native): match a LIB64 x86_64 slice with blank lipo capabilities Read a universal image whose x86_64 FAT entry carries 0x80000003 while lipo reports CPU_SUBTYPE_X86_ALL with a blank capabilities line, as macOS 26 lipo does, and expect both slices instead of an integrity failure. * fix(native): compare named lipo subtype bits without capability bytes A blank capabilities line leaves the subtype code unknown, and slice matching then skipped the subtype entirely. A FAT entry whose named subtype differs, such as CPU_SUBTYPE_ARM64_V8 recorded by lipo as CPU_SUBTYPE_ARM64_ALL, was accepted. Keep comparing the complete subtype when lipo printed its capability byte, and otherwise compare the subtype bits its record names. * test(native): share LIB64 slice reader setup Read the LIB64 universal fixture through one helper that takes the x86_64 subtype lipo reports, so further cases reuse the same image and lipo record. The existing expectation is unchanged. * test(native): reject a blank-capability lipo subtype naming other bits With the capabilities line blank, a lipo record naming CPU_SUBTYPE_X86_64_H for the x86_64 LIB64 slice still disagrees with the FAT entry's subtype bits and fails slice validation.
This commit is contained in:
@@ -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.<variant>` (for example
|
||||
* `arm64e.v1` for pointer-auth ABI version 1). The FAT table names that slice
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user