fix(apple): validate physical slices and dylib platform compatibility
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseLipoArchitectures } from "./lipo.js";
|
||||
|
||||
describe("lipo architecture facts", () => {
|
||||
it("normalizes symbolic CPU fields and arm64e capability bits", () => {
|
||||
const architectures = parseLipoArchitectures(`Fat header in: /bin/ls
|
||||
fat_magic 0xcafebabe
|
||||
nfat_arch 2
|
||||
architecture x86_64
|
||||
cputype CPU_TYPE_X86_64
|
||||
cpusubtype CPU_SUBTYPE_X86_64_ALL
|
||||
capabilities 0x0
|
||||
offset 16384
|
||||
size 48112
|
||||
align 2^14 (16384)
|
||||
architecture arm64e
|
||||
cputype CPU_TYPE_ARM64
|
||||
cpusubtype CPU_SUBTYPE_ARM64E
|
||||
capabilities PTR_AUTH_VERSION USERSPACE 0
|
||||
offset 65536
|
||||
size 89088
|
||||
align 2^14 (16384)
|
||||
`);
|
||||
expect(architectures).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "x86_64",
|
||||
cpu_type: "CPU_TYPE_X86_64",
|
||||
cpu_type_code: 0x01000007,
|
||||
cpu_subtype: "CPU_SUBTYPE_X86_64_ALL",
|
||||
cpu_subtype_code: 3,
|
||||
capabilities: "0x0",
|
||||
file_offset: 16384,
|
||||
size: 48112,
|
||||
alignment: 16384,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "arm64e",
|
||||
cpu_type: "CPU_TYPE_ARM64",
|
||||
cpu_type_code: 0x0100000c,
|
||||
cpu_subtype: "CPU_SUBTYPE_ARM64E",
|
||||
cpu_subtype_code: 0x80000002,
|
||||
capabilities: "PTR_AUTH_VERSION USERSPACE 0",
|
||||
file_offset: 65536,
|
||||
size: 89088,
|
||||
alignment: 16384,
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps unknown symbolic CPU values as source text without a code", () => {
|
||||
expect(
|
||||
parseLipoArchitectures(
|
||||
"architecture future\n cputype CPU_TYPE_FUTURE\n cpusubtype CPU_SUBTYPE_FUTURE\n offset 4096\n size 64\n align 2^12\n",
|
||||
),
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_type: "CPU_TYPE_FUTURE",
|
||||
cpu_type_code: null,
|
||||
cpu_subtype: "CPU_SUBTYPE_FUTURE",
|
||||
cpu_subtype_code: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -4,6 +4,9 @@ const architectureSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
cpu_type: z.string().nullable(),
|
||||
cpu_subtype: z.string().nullable(),
|
||||
cpu_type_code: z.number().int().nonnegative().nullable(),
|
||||
cpu_subtype_code: z.number().int().nonnegative().nullable(),
|
||||
capabilities: z.string().nullable(),
|
||||
file_offset: z.number().int().min(0).nullable(),
|
||||
size: z.number().int().min(0).nullable(),
|
||||
alignment: z.number().int().min(0).nullable(),
|
||||
@@ -25,6 +28,9 @@ export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
name: thinArchitecture,
|
||||
cpu_type: null,
|
||||
cpu_subtype: null,
|
||||
cpu_type_code: null,
|
||||
cpu_subtype_code: null,
|
||||
capabilities: null,
|
||||
file_offset: null,
|
||||
size: null,
|
||||
alignment: null,
|
||||
@@ -43,6 +49,12 @@ export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
: name,
|
||||
cpu_type: current.cputype ?? null,
|
||||
cpu_subtype: current.cpusubtype ?? null,
|
||||
cpu_type_code: cpuTypeCode(current.cputype),
|
||||
cpu_subtype_code: cpuSubtypeCode(
|
||||
current.cpusubtype,
|
||||
current.capabilities,
|
||||
),
|
||||
capabilities: current.capabilities ?? null,
|
||||
file_offset: integer(current.offset),
|
||||
size: integer(current.size),
|
||||
alignment: alignment(current.align),
|
||||
@@ -62,7 +74,10 @@ export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
current = { architecture: line.slice("architecture ".length).trim() };
|
||||
continue;
|
||||
}
|
||||
const match = /^(cputype|cpusubtype|offset|size|align)\s+(.+)$/u.exec(line);
|
||||
const match =
|
||||
/^(cputype|cpusubtype|capabilities|offset|size|align)\s+(.+)$/u.exec(
|
||||
line,
|
||||
);
|
||||
if (match?.[1] !== undefined && match[2] !== undefined) {
|
||||
current ??= {};
|
||||
current[match[1]] = match[2].trim();
|
||||
@@ -80,6 +95,72 @@ const integer = (value: string | undefined): number | null => {
|
||||
return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : null;
|
||||
};
|
||||
|
||||
const CPU_TYPES: Readonly<Record<string, number>> = {
|
||||
CPU_TYPE_X86: 7,
|
||||
CPU_TYPE_X86_64: 0x01000007,
|
||||
CPU_TYPE_ARM: 12,
|
||||
CPU_TYPE_ARM64: 0x0100000c,
|
||||
CPU_TYPE_ARM64_32: 0x0200000c,
|
||||
CPU_TYPE_POWERPC: 18,
|
||||
CPU_TYPE_POWERPC64: 0x01000012,
|
||||
};
|
||||
|
||||
const CPU_SUBTYPES: Readonly<Record<string, number>> = {
|
||||
CPU_SUBTYPE_X86_ALL: 3,
|
||||
CPU_SUBTYPE_X86_64_ALL: 3,
|
||||
CPU_SUBTYPE_X86_64_H: 8,
|
||||
CPU_SUBTYPE_ARM64_ALL: 0,
|
||||
CPU_SUBTYPE_ARM64_V8: 1,
|
||||
CPU_SUBTYPE_ARM64E: 2,
|
||||
CPU_SUBTYPE_ARM64_32_ALL: 0,
|
||||
CPU_SUBTYPE_ARM64_32_V8: 1,
|
||||
CPU_SUBTYPE_ARM_V7: 9,
|
||||
CPU_SUBTYPE_ARM_V7S: 11,
|
||||
CPU_SUBTYPE_ARM_V7K: 12,
|
||||
CPU_SUBTYPE_POWERPC_ALL: 0,
|
||||
CPU_SUBTYPE_POWERPC_970: 100,
|
||||
};
|
||||
|
||||
/** Normalize known lipo names while retaining the producer's original text. */
|
||||
const cpuTypeCode = (value: string | undefined): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const numeric = unsigned32(value);
|
||||
return numeric ?? CPU_TYPES[value] ?? null;
|
||||
};
|
||||
|
||||
/** Normalize the subtype and its separately printed ptrauth capability bits. */
|
||||
const cpuSubtypeCode = (
|
||||
value: string | undefined,
|
||||
capabilities: string | undefined,
|
||||
): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const numeric = unsigned32(value);
|
||||
if (numeric !== null) return numeric;
|
||||
const subtype = CPU_SUBTYPES[value];
|
||||
if (subtype === undefined) return null;
|
||||
if (value !== "CPU_SUBTYPE_ARM64E") return subtype;
|
||||
const version = /PTR_AUTH_VERSION\s+USERSPACE\s+(\d+)/u.exec(
|
||||
capabilities ?? "",
|
||||
)?.[1];
|
||||
if (version === undefined) return null;
|
||||
const parsedVersion = Number(version);
|
||||
if (
|
||||
!Number.isInteger(parsedVersion) ||
|
||||
parsedVersion < 0 ||
|
||||
parsedVersion > 15
|
||||
)
|
||||
return null;
|
||||
return (0x80000000 | (parsedVersion << 24) | subtype) >>> 0;
|
||||
};
|
||||
|
||||
const unsigned32 = (value: string): number | null => {
|
||||
if (!/^(?:\d+|0x[\da-f]+)$/iu.test(value)) return null;
|
||||
const parsed = Number(value);
|
||||
return Number.isInteger(parsed) && parsed >= 0 && parsed <= 0xffffffff
|
||||
? parsed >>> 0
|
||||
: null;
|
||||
};
|
||||
|
||||
const alignment = (value: string | undefined): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const exponent = /^2\^(\d+)(?: \((\d+)\))?$/u.exec(value);
|
||||
|
||||
Reference in New Issue
Block a user