mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
refactor(domain): move the lipo detailed-info parser into src/domain/apple (#11460)
This commit is contained in:
1 parent
5196c90992
commit
d068f164d3
19 files changed
+790
-790
No files matched your search
@@ -22,7 +22,7 @@
|
||||
"apktool -> artifacts": 2,
|
||||
"artifacts -> dotnet": 3,
|
||||
"artifacts -> javascript": 1,
|
||||
"artifacts -> native": 2,
|
||||
"artifacts -> native": 1,
|
||||
"browser -> artifacts": 6,
|
||||
"browser -> electron": 5,
|
||||
"browser -> javascript": 1,
|
||||
|
||||
@@ -7,6 +7,8 @@ import {
|
||||
type ArtifactReader,
|
||||
type ArtifactReaderTraversal,
|
||||
type ArtifactTraversalOptions,
|
||||
streamChunkToBuffer,
|
||||
readArtifactFileChunks,
|
||||
} from "../resources/index.js";
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
import { Readable } from "node:stream";
|
||||
@@ -16,11 +18,6 @@ import {
|
||||
XcrunCommandRunner,
|
||||
type NativeCommandRunner,
|
||||
} from "../native/CommandRunner.js";
|
||||
import {
|
||||
parseLipoOutputResult,
|
||||
LipoFieldRetentionError,
|
||||
type LipoArchitecture,
|
||||
} from "../native/parsers/lipo.js";
|
||||
import type { MachoSlice } from "../domain/apple/dylibResolution.js";
|
||||
import {
|
||||
machoArchitectureFamily,
|
||||
@@ -31,14 +28,15 @@ import {
|
||||
readMachoImage,
|
||||
type ReadAt,
|
||||
} from "../domain/apple/machoLoadCommandReader.js";
|
||||
import {
|
||||
streamChunkToBuffer,
|
||||
readArtifactFileChunks,
|
||||
} from "../resources/index.js";
|
||||
import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js";
|
||||
import { ArtifactFileSource } from "./ArtifactFileSource.js";
|
||||
import { readFileHandleRange } from "../filesystem/readFileHandleRange.js";
|
||||
import type { ArtifactIntegrityPolicyName } from "../domain/artifactIntegrityPolicy.js";
|
||||
import {
|
||||
parseLipoOutputResult,
|
||||
LipoFieldRetentionError,
|
||||
type LipoArchitecture,
|
||||
} from "../domain/apple/lipo.js";
|
||||
|
||||
/** Read-only universal Mach-O slice reader backed by native lipo metadata. */
|
||||
export class MachOSliceArtifactReader implements ArtifactReader {
|
||||
|
||||
@@ -0,0 +1,382 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseLipoFixture } from "../../../tests/fixtures/lipo.js";
|
||||
|
||||
const fatRecords = `fat_magic 0xcafebabe
|
||||
nfat_arch 2
|
||||
architecture arm64
|
||||
cputype CPU_TYPE_ARM64
|
||||
cpusubtype CPU_SUBTYPE_ARM64_ALL
|
||||
capabilities 0x0
|
||||
offset 4096
|
||||
size 32
|
||||
align 2^12 (4096)
|
||||
architecture x86_64
|
||||
cputype CPU_TYPE_X86_64
|
||||
cpusubtype CPU_SUBTYPE_X86_64_ALL
|
||||
capabilities 0x0
|
||||
offset 8192
|
||||
size 32
|
||||
align 2^12 (4096)
|
||||
`;
|
||||
|
||||
describe("lipo numeric field observations", () => {
|
||||
it.each([
|
||||
{ field: "offset", source: "-1" },
|
||||
{ field: "size", source: "9007199254740992" },
|
||||
{ field: "align", source: "2^12 (2048)" },
|
||||
{ field: "align", source: "2^53" },
|
||||
{ field: "offset", source: "" },
|
||||
] as const)(
|
||||
"retains an explicit uninterpretable $field source: $source",
|
||||
({ field, source }) => {
|
||||
const sourceLine = `\t${field} ${source} `;
|
||||
const architecture = parseLipoFixture(
|
||||
`architecture arm64\n cputype 16777228\n${sourceLine}\n`,
|
||||
).architectures[0];
|
||||
expect(architecture?.cpu_type_code).toBe(16777228);
|
||||
expect(architecture?.fields[field]).toEqual({
|
||||
kind: "uninterpretable",
|
||||
sourceLines: [sourceLine],
|
||||
});
|
||||
expect(architecture?.limitations).toHaveLength(1);
|
||||
expect(architecture?.limitations[0]).toContain(sourceLine);
|
||||
expect(architecture?.limitations[0]).toContain(field);
|
||||
expect(architecture?.limitations[0]).toMatch(
|
||||
/lipo.*arm64.*could not be interpreted/iu,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it("parses each valid numeric field once while preserving its exact source line", () => {
|
||||
const architecture = parseLipoFixture(
|
||||
"architecture arm64\n offset 4096 \n size 48\n align 2^12 (4096)\n",
|
||||
).architectures[0];
|
||||
expect(architecture).toMatchObject({
|
||||
file_offset: 4096,
|
||||
size: 48,
|
||||
alignment: 4096,
|
||||
limitations: [],
|
||||
fields: {
|
||||
offset: {
|
||||
kind: "parsed",
|
||||
sourceLines: [" offset 4096 "],
|
||||
value: 4096,
|
||||
},
|
||||
size: { kind: "parsed", sourceLines: [" size 48"], value: 48 },
|
||||
align: {
|
||||
kind: "parsed",
|
||||
sourceLines: [" align 2^12 (4096)"],
|
||||
value: 4096,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it.each(["offset", "size", "align"] as const)(
|
||||
"retains every repeated %s declaration and requires their parsed values to agree",
|
||||
(field) => {
|
||||
const first = field === "align" ? "2^12 (4096)" : "4096";
|
||||
const conflicting = field === "align" ? "2^13 (8192)" : "8192";
|
||||
for (const second of [first, conflicting, "unsupported"]) {
|
||||
const sourceLines = [` ${field} ${first}`, `\t${field} ${second} `];
|
||||
const row = parseLipoFixture(
|
||||
`architecture arm64\n${sourceLines.join("\n")}\n`,
|
||||
).architectures[0];
|
||||
expect(row?.fields[field]).toEqual(
|
||||
second === first
|
||||
? { kind: "parsed", sourceLines, value: 4096 }
|
||||
: { kind: "uninterpretable", sourceLines },
|
||||
);
|
||||
if (second === first) expect(row?.limitations).toEqual([]);
|
||||
else {
|
||||
expect(row?.limitations).toHaveLength(1);
|
||||
for (const line of sourceLines)
|
||||
expect(row?.limitations[0]).toContain(line);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
type: "constructor",
|
||||
subtype: "CPU_SUBTYPE_ARM64_ALL",
|
||||
typeCode: null,
|
||||
subtypeCode: 0,
|
||||
subtypeBase: 0,
|
||||
},
|
||||
{
|
||||
type: "CPU_TYPE_ARM64",
|
||||
subtype: "toString",
|
||||
typeCode: 0x0100000c,
|
||||
subtypeCode: null,
|
||||
subtypeBase: null,
|
||||
},
|
||||
])(
|
||||
"preserves unknown CPU names without inventing codes",
|
||||
({ type, subtype, typeCode, subtypeCode, subtypeBase }) => {
|
||||
const { architectures } = parseLipoFixture(
|
||||
fatRecords
|
||||
.replace("CPU_TYPE_ARM64", type)
|
||||
.replace("CPU_SUBTYPE_ARM64_ALL", subtype),
|
||||
);
|
||||
expect(architectures[0]).toMatchObject({
|
||||
cpu_type: type,
|
||||
cpu_type_code: typeCode,
|
||||
cpu_subtype: subtype,
|
||||
cpu_subtype_code: subtypeCode,
|
||||
cpu_subtype_base: subtypeBase,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
describe("lipo architecture facts", () => {
|
||||
it("preserves Apple's declared and hidden ARM64 architecture scopes", () => {
|
||||
const output = parseLipoFixture(`fat_magic 0xcafebabe
|
||||
nfat_arch 1 (+1 hidden)
|
||||
architecture armv7
|
||||
cputype CPU_TYPE_ARM
|
||||
cpusubtype CPU_SUBTYPE_ARM_V7
|
||||
offset 4096
|
||||
size 28
|
||||
align 2^12 (4096)
|
||||
architecture arm64 (hidden)
|
||||
cputype CPU_TYPE_ARM64
|
||||
cpusubtype CPU_SUBTYPE_ARM64_ALL
|
||||
offset 8192
|
||||
size 32
|
||||
align 2^12 (4096)
|
||||
`);
|
||||
expect(output).toMatchObject({
|
||||
declaredArchitectureCount: 1,
|
||||
hiddenArchitectureCount: 1,
|
||||
architectures: [
|
||||
{ name: "armv7", hidden: false },
|
||||
{ name: "arm64", hidden: true, file_offset: 8192, size: 32 },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("normalizes symbolic CPU fields and arm64e capability bits", () => {
|
||||
const { format, architectures } = parseLipoFixture(`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(format).toBe("fat");
|
||||
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("normalizes the CPU names lipo prints for i386 slices", () => {
|
||||
// cctools misc/lipo.c print_cputype() spells i386 with I386 names.
|
||||
expect(
|
||||
parseLipoFixture(`architecture i386
|
||||
cputype CPU_TYPE_I386
|
||||
cpusubtype CPU_SUBTYPE_I386_ALL
|
||||
capabilities 0x0
|
||||
offset 16384
|
||||
size 28
|
||||
align 2^14 (16384)
|
||||
`).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
name: "i386",
|
||||
cpu_type_code: 7,
|
||||
cpu_subtype_code: 3,
|
||||
cpu_subtype_base: 3,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps unknown symbolic CPU values as source text without a code", () => {
|
||||
expect(
|
||||
parseLipoFixture(
|
||||
"architecture future\n cputype CPU_TYPE_FUTURE\n cpusubtype CPU_SUBTYPE_FUTURE\n offset 4096\n size 64\n align 2^12\n",
|
||||
).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_type: "CPU_TYPE_FUTURE",
|
||||
cpu_type_code: null,
|
||||
cpu_subtype: "CPU_SUBTYPE_FUTURE",
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["CPU_SUBTYPE_LIB64", 0x80000003],
|
||||
["0x80", 0x80000003],
|
||||
["0x0", 3],
|
||||
])("applies printed x86_64 capabilities %s", (capabilities, code) => {
|
||||
expect(
|
||||
parseLipoFixture(
|
||||
`architecture x86_64\n cpusubtype CPU_SUBTYPE_X86_64_ALL\n capabilities ${capabilities}\n`,
|
||||
).architectures[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(
|
||||
parseLipoFixture(
|
||||
"architecture x86_64\n cputype CPU_TYPE_X86_64\n cpusubtype CPU_SUBTYPE_X86_ALL\n capabilities \n offset 16384\n",
|
||||
).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_subtype: "CPU_SUBTYPE_X86_ALL",
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: 3,
|
||||
capabilities: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ capabilities: "PTR_AUTH_VERSION USERSPACE 1", code: 0x81000002 },
|
||||
{ capabilities: "", code: null },
|
||||
])(
|
||||
"retains an arm64e base subtype with capabilities '$capabilities'",
|
||||
({ capabilities, code }) => {
|
||||
expect(
|
||||
parseLipoFixture(
|
||||
`architecture arm64e.v1\n cputype CPU_TYPE_ARM64\n cpusubtype CPU_SUBTYPE_ARM64E\n capabilities ${capabilities}\n`,
|
||||
).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_type_code: 0x0100000c,
|
||||
cpu_subtype_code: code,
|
||||
cpu_subtype_base: 2,
|
||||
},
|
||||
]);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("lipo FAT operand framing", () => {
|
||||
it.each([
|
||||
"/fixture/\narchitecture forged\n offset 123\n size 64",
|
||||
"/fixture/\nfat_magic 0xcafebabe\nnfat_arch 1\narchitecture forged",
|
||||
"/fixture/\nNon-fat file: fake is architecture: forged",
|
||||
"/fixture/first\nsecond\nthird",
|
||||
"/fixture/first\r\narchitecture forged\r\n offset 123",
|
||||
"/fixture/[app](arm64).*+$^?\\name",
|
||||
])(
|
||||
"keeps the complete echoed operand out of FAT slice records: %j",
|
||||
(path) => {
|
||||
const output = `Fat header in: ${path}\n${fatRecords}`;
|
||||
|
||||
const parsed = parseLipoFixture(output, path);
|
||||
expect(parsed).toEqual(parseLipoFixture(fatRecords));
|
||||
expect(parsed.architectures.map(({ name }) => name)).toEqual([
|
||||
"arm64",
|
||||
"x86_64",
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps a CRLF pathname separate from a CRLF record terminator", () => {
|
||||
const path = "/fixture/first\r\narchitecture forged\r\n size 64";
|
||||
const output = `Fat header in: ${path}\r\n${fatRecords.replaceAll("\n", "\r\n")}`;
|
||||
|
||||
const parsed = parseLipoFixture(output, path);
|
||||
expect(parsed).toEqual(parseLipoFixture(fatRecords));
|
||||
expect(parsed.architectures).toMatchObject([
|
||||
{ name: "arm64", file_offset: 4096, size: 32 },
|
||||
{ name: "x86_64", file_offset: 8192, size: 32 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("preserves ordinary FAT metadata with and without an operand", () => {
|
||||
const path = "/fixture/app";
|
||||
const output = `Fat header in: ${path}\n${fatRecords}`;
|
||||
|
||||
expect(parseLipoFixture(output, path)).toEqual(parseLipoFixture(output));
|
||||
});
|
||||
|
||||
it("does not require a FAT path header in captured fixtures", () => {
|
||||
expect(parseLipoFixture(fatRecords, "/fixture/app")).toEqual(
|
||||
parseLipoFixture(fatRecords),
|
||||
);
|
||||
});
|
||||
|
||||
it("does not strip a header for a different operand that shares a prefix", () => {
|
||||
const path = "/fixture/app-extra\narchitecture forged";
|
||||
const output = `Fat header in: ${path}\n${fatRecords}`;
|
||||
|
||||
expect(parseLipoFixture(output, "/fixture/app")).toEqual(
|
||||
parseLipoFixture(output),
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves thin-file parsing when the operand contains FAT-like lines", () => {
|
||||
const path = "/fixture/\nFat header in: fake\narchitecture forged";
|
||||
const output = `Non-fat file: ${path} is architecture: arm64\n`;
|
||||
|
||||
expect(parseLipoFixture(output, path)).toEqual({
|
||||
format: "thin",
|
||||
declaredArchitectureCount: null,
|
||||
hiddenArchitectureCount: null,
|
||||
architectures: [
|
||||
{
|
||||
name: "arm64",
|
||||
hidden: false,
|
||||
cpu_type: null,
|
||||
cpu_subtype: null,
|
||||
cpu_type_code: null,
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
capabilities: null,
|
||||
file_offset: null,
|
||||
size: null,
|
||||
alignment: null,
|
||||
fields: {
|
||||
offset: { kind: "absent" },
|
||||
size: { kind: "absent" },
|
||||
align: { kind: "absent" },
|
||||
},
|
||||
limitations: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(parseLipoFixture(output, path)).toEqual(parseLipoFixture(output));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,373 @@
|
||||
import { z } from "zod";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import { malformedInput, ParseFailureError } from "../parseFailure.js";
|
||||
import { NATIVE_COMMAND_OUTPUT_BUDGET_BYTES } from "../native/nativeCommandOutput.js";
|
||||
import { machoCpuSubtypeBase } from "./machoCpuIdentity.js";
|
||||
|
||||
const architectureSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
hidden: z.boolean(),
|
||||
cpu_type: z.string().nullable(),
|
||||
cpu_subtype: z.string().nullable(),
|
||||
cpu_type_code: z.int().nonnegative().nullable(),
|
||||
cpu_subtype_code: z.int().nonnegative().nullable(),
|
||||
cpu_subtype_base: z.int().nonnegative().nullable(),
|
||||
capabilities: z.string().nullable(),
|
||||
file_offset: z.int().min(0).nullable(),
|
||||
size: z.int().min(0).nullable(),
|
||||
alignment: z.int().min(0).nullable(),
|
||||
});
|
||||
|
||||
export type LipoFieldObservation =
|
||||
| { readonly kind: "absent" }
|
||||
| {
|
||||
readonly kind: "parsed";
|
||||
readonly sourceLines: readonly string[];
|
||||
readonly value: number;
|
||||
}
|
||||
| {
|
||||
readonly kind: "uninterpretable";
|
||||
readonly sourceLines: readonly string[];
|
||||
};
|
||||
|
||||
export type LipoArchitecture = z.infer<typeof architectureSchema> & {
|
||||
readonly fields: Readonly<
|
||||
Record<"offset" | "size" | "align", LipoFieldObservation>
|
||||
>;
|
||||
readonly limitations: readonly string[];
|
||||
};
|
||||
|
||||
/** Capacity refusal before retaining expanded per-declaration source facts. */
|
||||
export class LipoFieldRetentionError extends Error {
|
||||
readonly _tag = "LipoFieldRetentionError";
|
||||
constructor(readonly requiredBytes: number) {
|
||||
super(
|
||||
"lipo numeric field observations exceed the retained native output budget",
|
||||
);
|
||||
this.name = this._tag;
|
||||
}
|
||||
}
|
||||
|
||||
export interface LipoOutput {
|
||||
readonly format: "thin" | "fat";
|
||||
/** `nfat_arch` counts visible rows; Apple may append one hidden ARM64 row. */
|
||||
readonly declaredArchitectureCount: number | null;
|
||||
readonly hiddenArchitectureCount: number | null;
|
||||
readonly architectures: LipoArchitecture[];
|
||||
}
|
||||
|
||||
/** Parse `lipo -detailed_info` framing and deterministic slice metadata. */
|
||||
const parseLipoOutput = (output: string, operandPath?: string): LipoOutput => {
|
||||
// The echoed FAT operand is one value, even when its pathname contains lines.
|
||||
if (operandPath !== undefined) {
|
||||
const header = `Fat header in: ${operandPath}`;
|
||||
if (output.startsWith(header)) {
|
||||
if (output.startsWith("\r\n", header.length))
|
||||
output = output.slice(header.length + 2);
|
||||
else if (output.startsWith("\n", header.length))
|
||||
output = output.slice(header.length + 1);
|
||||
}
|
||||
}
|
||||
// lipo echoes a thin file's pathname verbatim, including embedded newlines.
|
||||
// Match its final architecture field before splitting records into lines.
|
||||
const thinArchitecture =
|
||||
/(?:^|\r?\n)Non-fat file:[\s\S]* is architecture: ([^\s]+)\s*$/u.exec(
|
||||
output,
|
||||
)?.[1];
|
||||
if (thinArchitecture !== undefined)
|
||||
return {
|
||||
format: "thin",
|
||||
declaredArchitectureCount: null,
|
||||
hiddenArchitectureCount: null,
|
||||
architectures: [
|
||||
{
|
||||
...architectureSchema.parse({
|
||||
name: thinArchitecture,
|
||||
hidden: false,
|
||||
cpu_type: null,
|
||||
cpu_subtype: null,
|
||||
cpu_type_code: null,
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
capabilities: null,
|
||||
file_offset: null,
|
||||
size: null,
|
||||
alignment: null,
|
||||
}),
|
||||
fields: {
|
||||
offset: { kind: "absent" },
|
||||
size: { kind: "absent" },
|
||||
align: { kind: "absent" },
|
||||
},
|
||||
limitations: [],
|
||||
},
|
||||
],
|
||||
};
|
||||
const architectures: LipoArchitecture[] = [];
|
||||
let declaredArchitectureCount: number | null = null;
|
||||
let hiddenArchitectureCount: number | null = null;
|
||||
let current: Record<string, string> | undefined;
|
||||
let observations: Partial<
|
||||
Record<
|
||||
"offset" | "size" | "align",
|
||||
{ readonly sourceLine: string; readonly value: number | null }[]
|
||||
>
|
||||
> = {};
|
||||
let retainedFieldBytes = 0;
|
||||
const admitFieldBytes = (bytes: number): void => {
|
||||
retainedFieldBytes += bytes;
|
||||
if (retainedFieldBytes > NATIVE_COMMAND_OUTPUT_BUDGET_BYTES)
|
||||
throw new LipoFieldRetentionError(retainedFieldBytes);
|
||||
};
|
||||
const flush = (): void => {
|
||||
if (current === undefined) return;
|
||||
const name = current.architecture ?? current["Non-fat file"];
|
||||
if (name !== undefined) {
|
||||
const observe = (
|
||||
key: "offset" | "size" | "align",
|
||||
): LipoFieldObservation => {
|
||||
const observed = observations[key];
|
||||
if (observed === undefined) return { kind: "absent" };
|
||||
const sourceLines = observed.map(({ sourceLine }) => sourceLine);
|
||||
const value = observed[0]?.value;
|
||||
return value === null ||
|
||||
value === undefined ||
|
||||
observed.some((candidate) => candidate.value !== value)
|
||||
? { kind: "uninterpretable", sourceLines }
|
||||
: { kind: "parsed", sourceLines, value };
|
||||
};
|
||||
const fields = {
|
||||
offset: observe("offset"),
|
||||
size: observe("size"),
|
||||
align: observe("align"),
|
||||
};
|
||||
const value = (field: LipoFieldObservation): number | null =>
|
||||
field.kind === "parsed" ? field.value : null;
|
||||
const limitations = Object.entries(fields).flatMap(
|
||||
([field, observation]) => {
|
||||
if (observation.kind !== "uninterpretable") return [];
|
||||
admitFieldBytes(name.length * 14 + 256);
|
||||
return [
|
||||
`lipo ${field} for ${name} could not be interpreted; ${observation.sourceLines.length === 1 ? "source line: " : "source lines:\n"}${observation.sourceLines.join("\n")}`,
|
||||
];
|
||||
},
|
||||
);
|
||||
architectures.push({
|
||||
...architectureSchema.parse({
|
||||
name: name.includes(" is architecture: ")
|
||||
? (name.split(" is architecture: ").at(-1) ?? name)
|
||||
: name.replace(/ \(hidden\)$/u, ""),
|
||||
hidden: name.endsWith(" (hidden)"),
|
||||
cpu_type: current.cputype ?? null,
|
||||
cpu_subtype: current.cpusubtype ?? null,
|
||||
cpu_type_code: cpuTypeCode(current.cputype),
|
||||
cpu_subtype_code: cpuSubtypeCode(
|
||||
current.cpusubtype,
|
||||
current.capabilities,
|
||||
),
|
||||
cpu_subtype_base: cpuSubtypeBase(current.cpusubtype),
|
||||
capabilities: current.capabilities ?? null,
|
||||
file_offset: value(fields.offset),
|
||||
size: value(fields.size),
|
||||
alignment: value(fields.align),
|
||||
}),
|
||||
fields,
|
||||
limitations,
|
||||
});
|
||||
}
|
||||
current = undefined;
|
||||
observations = {};
|
||||
};
|
||||
for (const rawLine of output.split(/\r?\n/u)) {
|
||||
const line = rawLine.trim();
|
||||
const count = /^nfat_arch\s+(\d+)(?:\s+\(\+(\d+) hidden\))?$/u.exec(line);
|
||||
if (count?.[1] !== undefined) {
|
||||
declaredArchitectureCount = integer(count[1]);
|
||||
hiddenArchitectureCount = integer(count[2] ?? "0");
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("Non-fat file:")) {
|
||||
flush();
|
||||
current = { "Non-fat file": line };
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("architecture ")) {
|
||||
flush();
|
||||
current = { architecture: line.slice("architecture ".length).trim() };
|
||||
continue;
|
||||
}
|
||||
const numericField = /^\s*(offset|size|align)(?:\s+(.*))?$/u.exec(rawLine);
|
||||
if (
|
||||
numericField?.[1] === "offset" ||
|
||||
numericField?.[1] === "size" ||
|
||||
numericField?.[1] === "align"
|
||||
) {
|
||||
current ??= {};
|
||||
const field = numericField[1];
|
||||
// UTF-16 source, joined and rendered strings, worst-case escaped JSON,
|
||||
// and per-observation bookkeeping fit the existing native output allowance.
|
||||
// Charge before storing another observation; every source line is retained
|
||||
// at most once per field and rendered at most once, never per duplicate pair.
|
||||
admitFieldBytes(rawLine.length * 18 + 128);
|
||||
const sourceValue = (numericField[2] ?? "").trim();
|
||||
const observed = {
|
||||
sourceLine: rawLine,
|
||||
value:
|
||||
field === "align" ? alignment(sourceValue) : integer(sourceValue),
|
||||
};
|
||||
(observations[field] ??= []).push(observed);
|
||||
continue;
|
||||
}
|
||||
const match = /^(cputype|cpusubtype|capabilities)\s+(.+)$/u.exec(line);
|
||||
if (match?.[1] !== undefined && match[2] !== undefined) {
|
||||
current ??= {};
|
||||
current[match[1]] = match[2].trim();
|
||||
}
|
||||
}
|
||||
flush();
|
||||
if (architectures.length === 0)
|
||||
throw malformedInput(
|
||||
"lipo-output",
|
||||
"lipo output contained no architectures",
|
||||
);
|
||||
return {
|
||||
format: "fat",
|
||||
declaredArchitectureCount,
|
||||
hiddenArchitectureCount,
|
||||
architectures,
|
||||
};
|
||||
};
|
||||
|
||||
/** Classify expected lipo parse failures once for all command adapters. */
|
||||
export const parseLipoOutputResult = (
|
||||
output: string,
|
||||
operandPath?: string,
|
||||
): Result<
|
||||
LipoOutput,
|
||||
ParseFailureError | z.ZodError | LipoFieldRetentionError
|
||||
> => {
|
||||
try {
|
||||
return ok(parseLipoOutput(output, operandPath));
|
||||
} catch (cause: unknown) {
|
||||
if (
|
||||
!(cause instanceof ParseFailureError) &&
|
||||
!(cause instanceof z.ZodError) &&
|
||||
!(cause instanceof LipoFieldRetentionError)
|
||||
)
|
||||
throw cause;
|
||||
return err(cause);
|
||||
}
|
||||
};
|
||||
|
||||
const integer = (value: string | undefined): number | null => {
|
||||
if (value === undefined || !/^\d+$/u.test(value)) return null;
|
||||
const parsed = Number.parseInt(value, 10);
|
||||
return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : null;
|
||||
};
|
||||
|
||||
const CPU_TYPES: Readonly<Record<string, number>> = {
|
||||
CPU_TYPE_X86: 7,
|
||||
CPU_TYPE_I386: 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_I386_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 ownCpuCode = (
|
||||
codes: Readonly<Record<string, number>>,
|
||||
name: string,
|
||||
): number | undefined => (Object.hasOwn(codes, name) ? codes[name] : undefined);
|
||||
|
||||
const cpuTypeCode = (value: string | undefined): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const numeric = unsigned32(value);
|
||||
return numeric ?? ownCpuCode(CPU_TYPES, value) ?? null;
|
||||
};
|
||||
|
||||
/** Normalize the subtype and its separately printed 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 = ownCpuCode(CPU_SUBTYPES, value);
|
||||
if (subtype === undefined) return null;
|
||||
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];
|
||||
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;
|
||||
};
|
||||
|
||||
/** The subtype bits a lipo record names, without its capability byte. */
|
||||
const cpuSubtypeBase = (value: string | undefined): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const subtype = unsigned32(value) ?? ownCpuCode(CPU_SUBTYPES, value);
|
||||
return subtype === undefined ? null : machoCpuSubtypeBase(subtype);
|
||||
};
|
||||
|
||||
/** 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);
|
||||
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);
|
||||
const powerText = exponent?.[1];
|
||||
if (powerText !== undefined) {
|
||||
const power = Number.parseInt(powerText, 10);
|
||||
if (power > 52) return null;
|
||||
const alignment = 2 ** power;
|
||||
const reported = exponent?.[2];
|
||||
return reported === undefined || integer(reported) === alignment
|
||||
? alignment
|
||||
: null;
|
||||
}
|
||||
return integer(value);
|
||||
};
|
||||
@@ -0,0 +1,2 @@
|
||||
/** Retain complete native command output up to a bounded aggregate size. */
|
||||
export const NATIVE_COMMAND_OUTPUT_BUDGET_BYTES = 64 * 1024 * 1024;
|
||||
@@ -20,14 +20,13 @@ import type {
|
||||
} from "../process/ProviderProcess.js";
|
||||
import { utf8HeadExcerpt } from "../domain/utf8HeadExcerpt.js";
|
||||
import { DEFAULT_NATIVE_COMMAND_TIMEOUT_MS } from "../domain/native/nativeDeadlines.js";
|
||||
import { NATIVE_COMMAND_OUTPUT_BUDGET_BYTES } from "../domain/native/nativeCommandOutput.js";
|
||||
import {
|
||||
selectArtifactCommandPath,
|
||||
type ArtifactCommandPath,
|
||||
} from "../process/ArtifactCommandPath.js";
|
||||
import { ArtifactReaderFailure } from "../resources/index.js";
|
||||
|
||||
/** Retain complete command output up to a bounded aggregate size. */
|
||||
export const NATIVE_COMMAND_OUTPUT_BUDGET_BYTES = 64 * 1024 * 1024;
|
||||
/**
|
||||
* Failure diagnostics keep only an excerpt of each stream. Successful
|
||||
* analysis needs complete output, but a failed command's error would
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { selectLipoArchitecture } from "./MachoArchitectureSelection.js";
|
||||
import type { LipoArchitecture } from "./parsers/lipo.js";
|
||||
import type { LipoArchitecture } from "../domain/apple/lipo.js";
|
||||
|
||||
const record = (
|
||||
name: string,
|
||||
|
||||
@@ -7,11 +7,11 @@ import {
|
||||
} from "../domain/apple/machoCpuIdentity.js";
|
||||
import { keyedBy, uniqueKeyed } from "../domain/keyedCollection.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import type { LipoArchitecture } from "./parsers/lipo.js";
|
||||
import type {
|
||||
AdmittedMachoSlice,
|
||||
MachoSliceSelectionRefusal,
|
||||
} from "./metadata/MachoSliceSelection.js";
|
||||
import type { LipoArchitecture } from "../domain/apple/lipo.js";
|
||||
|
||||
/** Apple tool names for the domain-owned host subtype preference. */
|
||||
const preferredSubtypes = <Subtype extends number>(
|
||||
|
||||
@@ -25,10 +25,10 @@ import {
|
||||
nativeDeadlineRecovery,
|
||||
} from "../domain/native/nativeDeadlines.js";
|
||||
import {
|
||||
NATIVE_COMMAND_OUTPUT_BUDGET_BYTES,
|
||||
NATIVE_FAILURE_DIAGNOSTIC_BYTES,
|
||||
type NativeCommandFailure,
|
||||
} from "./CommandRunner.js";
|
||||
import { NATIVE_COMMAND_OUTPUT_BUDGET_BYTES } from "../domain/native/nativeCommandOutput.js";
|
||||
|
||||
/** Operation-owned text for a command deadline or unavailable requirement. */
|
||||
export interface NativeCommandFailurePresentation {
|
||||
|
||||
@@ -122,7 +122,6 @@ import {
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import {
|
||||
NativeCommandFailure,
|
||||
NATIVE_COMMAND_OUTPUT_BUDGET_BYTES,
|
||||
XcrunCommandRunner,
|
||||
failureCapture,
|
||||
type NativeCommandCleanupFailure,
|
||||
@@ -173,6 +172,7 @@ import {
|
||||
import { NativeUiInterruptionControl } from "./NativeUiInterruptionControl.js";
|
||||
import type { ResolvedAppBundleResourceRoot } from "../domain/apple/bundleResourceRoot.js";
|
||||
import { UnsupportedBinaryPlistUidError } from "../domain/apple/plistScalars.js";
|
||||
import { NATIVE_COMMAND_OUTPUT_BUDGET_BYTES } from "../domain/native/nativeCommandOutput.js";
|
||||
|
||||
type SignatureRuntimeRootFactory = () => Promise<
|
||||
Pick<PrivateRuntimeRoot, "path" | "close">
|
||||
|
||||
@@ -16,10 +16,7 @@ import { machoRuntimeSymbolName } from "../domain/native/machoSymbolName.js";
|
||||
import { keyedBy, uniqueKeyed } from "../domain/keyedCollection.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import type { MachoSliceSelectionRefusal } from "./metadata/MachoSliceSelection.js";
|
||||
import {
|
||||
NATIVE_COMMAND_OUTPUT_BUDGET_BYTES,
|
||||
type NativeCommandCapture,
|
||||
} from "./CommandRunner.js";
|
||||
import type { NativeCommandCapture } from "./CommandRunner.js";
|
||||
import { chainedImportNames } from "./AppleMachoFixups.js";
|
||||
import {
|
||||
architectureBaseName,
|
||||
@@ -31,7 +28,7 @@ import {
|
||||
parseDyldExports,
|
||||
parseDyldImports,
|
||||
} from "./parsers/dyldInfo.js";
|
||||
import { parseLipoCapture, type LipoArchitecture } from "./parsers/lipo.js";
|
||||
import { parseLipoCapture } from "./parsers/lipo.js";
|
||||
import { withoutEchoedPathHeader } from "./parsers/echoedPath.js";
|
||||
import {
|
||||
projectMachoBinary,
|
||||
@@ -59,6 +56,8 @@ import {
|
||||
unusableNativeCommandOutput,
|
||||
} from "./NativeMachoCommandDiagnostics.js";
|
||||
import { invariantViolation } from "../domain/internalInvariant.js";
|
||||
import { NATIVE_COMMAND_OUTPUT_BUDGET_BYTES } from "../domain/native/nativeCommandOutput.js";
|
||||
import type { LipoArchitecture } from "../domain/apple/lipo.js";
|
||||
|
||||
interface NativeMachoObservation {
|
||||
readonly result: JsonValue;
|
||||
|
||||
@@ -3,8 +3,8 @@ import {
|
||||
assertSelectedMachoSlice,
|
||||
resolveSelectedMachoSlice,
|
||||
} from "./NativeMachoSelectedSlice.js";
|
||||
import { parseLipoOutputResult } from "./parsers/lipo.js";
|
||||
import type { MachoCommandNames } from "./MachoCommandNames.js";
|
||||
import { parseLipoOutputResult } from "../domain/apple/lipo.js";
|
||||
|
||||
it("binds the lipo row to one FAT entry and its selected thin header", async () => {
|
||||
const bytes = Buffer.alloc(256);
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
type MachoCommandNames,
|
||||
} from "./MachoCommandNames.js";
|
||||
import type { NativeMachoSource } from "./NativeMachoSource.js";
|
||||
import type { LipoArchitecture } from "./parsers/lipo.js";
|
||||
import type { LipoArchitecture } from "../domain/apple/lipo.js";
|
||||
|
||||
export interface ResolvedMachoSlice {
|
||||
readonly offset: number;
|
||||
|
||||
@@ -1,25 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { parseLipoCapture } from "./lipo.js";
|
||||
import { parseLipoFixture } from "../../../tests/fixtures/lipo.js";
|
||||
|
||||
const fatRecords = `fat_magic 0xcafebabe
|
||||
nfat_arch 2
|
||||
architecture arm64
|
||||
cputype CPU_TYPE_ARM64
|
||||
cpusubtype CPU_SUBTYPE_ARM64_ALL
|
||||
capabilities 0x0
|
||||
offset 4096
|
||||
size 32
|
||||
align 2^12 (4096)
|
||||
architecture x86_64
|
||||
cputype CPU_TYPE_X86_64
|
||||
cpusubtype CPU_SUBTYPE_X86_64_ALL
|
||||
capabilities 0x0
|
||||
offset 8192
|
||||
size 32
|
||||
align 2^12 (4096)
|
||||
`;
|
||||
|
||||
it("retains native command capture when shared lipo parsing rejects records", () => {
|
||||
const capture = {
|
||||
@@ -60,364 +41,3 @@ it("retains native command capture when shared lipo parsing rejects records", ()
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
describe("lipo numeric field observations", () => {
|
||||
it.each([
|
||||
{ field: "offset", source: "-1" },
|
||||
{ field: "size", source: "9007199254740992" },
|
||||
{ field: "align", source: "2^12 (2048)" },
|
||||
{ field: "align", source: "2^53" },
|
||||
{ field: "offset", source: "" },
|
||||
] as const)(
|
||||
"retains an explicit uninterpretable $field source: $source",
|
||||
({ field, source }) => {
|
||||
const sourceLine = `\t${field} ${source} `;
|
||||
const architecture = parseLipoFixture(
|
||||
`architecture arm64\n cputype 16777228\n${sourceLine}\n`,
|
||||
).architectures[0];
|
||||
expect(architecture?.cpu_type_code).toBe(16777228);
|
||||
expect(architecture?.fields[field]).toEqual({
|
||||
kind: "uninterpretable",
|
||||
sourceLines: [sourceLine],
|
||||
});
|
||||
expect(architecture?.limitations).toHaveLength(1);
|
||||
expect(architecture?.limitations[0]).toContain(sourceLine);
|
||||
expect(architecture?.limitations[0]).toContain(field);
|
||||
expect(architecture?.limitations[0]).toMatch(
|
||||
/lipo.*arm64.*could not be interpreted/iu,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it("parses each valid numeric field once while preserving its exact source line", () => {
|
||||
const architecture = parseLipoFixture(
|
||||
"architecture arm64\n offset 4096 \n size 48\n align 2^12 (4096)\n",
|
||||
).architectures[0];
|
||||
expect(architecture).toMatchObject({
|
||||
file_offset: 4096,
|
||||
size: 48,
|
||||
alignment: 4096,
|
||||
limitations: [],
|
||||
fields: {
|
||||
offset: {
|
||||
kind: "parsed",
|
||||
sourceLines: [" offset 4096 "],
|
||||
value: 4096,
|
||||
},
|
||||
size: { kind: "parsed", sourceLines: [" size 48"], value: 48 },
|
||||
align: {
|
||||
kind: "parsed",
|
||||
sourceLines: [" align 2^12 (4096)"],
|
||||
value: 4096,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it.each(["offset", "size", "align"] as const)(
|
||||
"retains every repeated %s declaration and requires their parsed values to agree",
|
||||
(field) => {
|
||||
const first = field === "align" ? "2^12 (4096)" : "4096";
|
||||
const conflicting = field === "align" ? "2^13 (8192)" : "8192";
|
||||
for (const second of [first, conflicting, "unsupported"]) {
|
||||
const sourceLines = [` ${field} ${first}`, `\t${field} ${second} `];
|
||||
const row = parseLipoFixture(
|
||||
`architecture arm64\n${sourceLines.join("\n")}\n`,
|
||||
).architectures[0];
|
||||
expect(row?.fields[field]).toEqual(
|
||||
second === first
|
||||
? { kind: "parsed", sourceLines, value: 4096 }
|
||||
: { kind: "uninterpretable", sourceLines },
|
||||
);
|
||||
if (second === first) expect(row?.limitations).toEqual([]);
|
||||
else {
|
||||
expect(row?.limitations).toHaveLength(1);
|
||||
for (const line of sourceLines)
|
||||
expect(row?.limitations[0]).toContain(line);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
type: "constructor",
|
||||
subtype: "CPU_SUBTYPE_ARM64_ALL",
|
||||
typeCode: null,
|
||||
subtypeCode: 0,
|
||||
subtypeBase: 0,
|
||||
},
|
||||
{
|
||||
type: "CPU_TYPE_ARM64",
|
||||
subtype: "toString",
|
||||
typeCode: 0x0100000c,
|
||||
subtypeCode: null,
|
||||
subtypeBase: null,
|
||||
},
|
||||
])(
|
||||
"preserves unknown CPU names without inventing codes",
|
||||
({ type, subtype, typeCode, subtypeCode, subtypeBase }) => {
|
||||
const { architectures } = parseLipoFixture(
|
||||
fatRecords
|
||||
.replace("CPU_TYPE_ARM64", type)
|
||||
.replace("CPU_SUBTYPE_ARM64_ALL", subtype),
|
||||
);
|
||||
expect(architectures[0]).toMatchObject({
|
||||
cpu_type: type,
|
||||
cpu_type_code: typeCode,
|
||||
cpu_subtype: subtype,
|
||||
cpu_subtype_code: subtypeCode,
|
||||
cpu_subtype_base: subtypeBase,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
describe("lipo architecture facts", () => {
|
||||
it("preserves Apple's declared and hidden ARM64 architecture scopes", () => {
|
||||
const output = parseLipoFixture(`fat_magic 0xcafebabe
|
||||
nfat_arch 1 (+1 hidden)
|
||||
architecture armv7
|
||||
cputype CPU_TYPE_ARM
|
||||
cpusubtype CPU_SUBTYPE_ARM_V7
|
||||
offset 4096
|
||||
size 28
|
||||
align 2^12 (4096)
|
||||
architecture arm64 (hidden)
|
||||
cputype CPU_TYPE_ARM64
|
||||
cpusubtype CPU_SUBTYPE_ARM64_ALL
|
||||
offset 8192
|
||||
size 32
|
||||
align 2^12 (4096)
|
||||
`);
|
||||
expect(output).toMatchObject({
|
||||
declaredArchitectureCount: 1,
|
||||
hiddenArchitectureCount: 1,
|
||||
architectures: [
|
||||
{ name: "armv7", hidden: false },
|
||||
{ name: "arm64", hidden: true, file_offset: 8192, size: 32 },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("normalizes symbolic CPU fields and arm64e capability bits", () => {
|
||||
const { format, architectures } = parseLipoFixture(`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(format).toBe("fat");
|
||||
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("normalizes the CPU names lipo prints for i386 slices", () => {
|
||||
// cctools misc/lipo.c print_cputype() spells i386 with I386 names.
|
||||
expect(
|
||||
parseLipoFixture(`architecture i386
|
||||
cputype CPU_TYPE_I386
|
||||
cpusubtype CPU_SUBTYPE_I386_ALL
|
||||
capabilities 0x0
|
||||
offset 16384
|
||||
size 28
|
||||
align 2^14 (16384)
|
||||
`).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
name: "i386",
|
||||
cpu_type_code: 7,
|
||||
cpu_subtype_code: 3,
|
||||
cpu_subtype_base: 3,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps unknown symbolic CPU values as source text without a code", () => {
|
||||
expect(
|
||||
parseLipoFixture(
|
||||
"architecture future\n cputype CPU_TYPE_FUTURE\n cpusubtype CPU_SUBTYPE_FUTURE\n offset 4096\n size 64\n align 2^12\n",
|
||||
).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_type: "CPU_TYPE_FUTURE",
|
||||
cpu_type_code: null,
|
||||
cpu_subtype: "CPU_SUBTYPE_FUTURE",
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["CPU_SUBTYPE_LIB64", 0x80000003],
|
||||
["0x80", 0x80000003],
|
||||
["0x0", 3],
|
||||
])("applies printed x86_64 capabilities %s", (capabilities, code) => {
|
||||
expect(
|
||||
parseLipoFixture(
|
||||
`architecture x86_64\n cpusubtype CPU_SUBTYPE_X86_64_ALL\n capabilities ${capabilities}\n`,
|
||||
).architectures[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(
|
||||
parseLipoFixture(
|
||||
"architecture x86_64\n cputype CPU_TYPE_X86_64\n cpusubtype CPU_SUBTYPE_X86_ALL\n capabilities \n offset 16384\n",
|
||||
).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_subtype: "CPU_SUBTYPE_X86_ALL",
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: 3,
|
||||
capabilities: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ capabilities: "PTR_AUTH_VERSION USERSPACE 1", code: 0x81000002 },
|
||||
{ capabilities: "", code: null },
|
||||
])(
|
||||
"retains an arm64e base subtype with capabilities '$capabilities'",
|
||||
({ capabilities, code }) => {
|
||||
expect(
|
||||
parseLipoFixture(
|
||||
`architecture arm64e.v1\n cputype CPU_TYPE_ARM64\n cpusubtype CPU_SUBTYPE_ARM64E\n capabilities ${capabilities}\n`,
|
||||
).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_type_code: 0x0100000c,
|
||||
cpu_subtype_code: code,
|
||||
cpu_subtype_base: 2,
|
||||
},
|
||||
]);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("lipo FAT operand framing", () => {
|
||||
it.each([
|
||||
"/fixture/\narchitecture forged\n offset 123\n size 64",
|
||||
"/fixture/\nfat_magic 0xcafebabe\nnfat_arch 1\narchitecture forged",
|
||||
"/fixture/\nNon-fat file: fake is architecture: forged",
|
||||
"/fixture/first\nsecond\nthird",
|
||||
"/fixture/first\r\narchitecture forged\r\n offset 123",
|
||||
"/fixture/[app](arm64).*+$^?\\name",
|
||||
])(
|
||||
"keeps the complete echoed operand out of FAT slice records: %j",
|
||||
(path) => {
|
||||
const output = `Fat header in: ${path}\n${fatRecords}`;
|
||||
|
||||
const parsed = parseLipoFixture(output, path);
|
||||
expect(parsed).toEqual(parseLipoFixture(fatRecords));
|
||||
expect(parsed.architectures.map(({ name }) => name)).toEqual([
|
||||
"arm64",
|
||||
"x86_64",
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps a CRLF pathname separate from a CRLF record terminator", () => {
|
||||
const path = "/fixture/first\r\narchitecture forged\r\n size 64";
|
||||
const output = `Fat header in: ${path}\r\n${fatRecords.replaceAll("\n", "\r\n")}`;
|
||||
|
||||
const parsed = parseLipoFixture(output, path);
|
||||
expect(parsed).toEqual(parseLipoFixture(fatRecords));
|
||||
expect(parsed.architectures).toMatchObject([
|
||||
{ name: "arm64", file_offset: 4096, size: 32 },
|
||||
{ name: "x86_64", file_offset: 8192, size: 32 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("preserves ordinary FAT metadata with and without an operand", () => {
|
||||
const path = "/fixture/app";
|
||||
const output = `Fat header in: ${path}\n${fatRecords}`;
|
||||
|
||||
expect(parseLipoFixture(output, path)).toEqual(parseLipoFixture(output));
|
||||
});
|
||||
|
||||
it("does not require a FAT path header in captured fixtures", () => {
|
||||
expect(parseLipoFixture(fatRecords, "/fixture/app")).toEqual(
|
||||
parseLipoFixture(fatRecords),
|
||||
);
|
||||
});
|
||||
|
||||
it("does not strip a header for a different operand that shares a prefix", () => {
|
||||
const path = "/fixture/app-extra\narchitecture forged";
|
||||
const output = `Fat header in: ${path}\n${fatRecords}`;
|
||||
|
||||
expect(parseLipoFixture(output, "/fixture/app")).toEqual(
|
||||
parseLipoFixture(output),
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves thin-file parsing when the operand contains FAT-like lines", () => {
|
||||
const path = "/fixture/\nFat header in: fake\narchitecture forged";
|
||||
const output = `Non-fat file: ${path} is architecture: arm64\n`;
|
||||
|
||||
expect(parseLipoFixture(output, path)).toEqual({
|
||||
format: "thin",
|
||||
declaredArchitectureCount: null,
|
||||
hiddenArchitectureCount: null,
|
||||
architectures: [
|
||||
{
|
||||
name: "arm64",
|
||||
hidden: false,
|
||||
cpu_type: null,
|
||||
cpu_subtype: null,
|
||||
cpu_type_code: null,
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
capabilities: null,
|
||||
file_offset: null,
|
||||
size: null,
|
||||
alignment: null,
|
||||
fields: {
|
||||
offset: { kind: "absent" },
|
||||
size: { kind: "absent" },
|
||||
align: { kind: "absent" },
|
||||
},
|
||||
limitations: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(parseLipoFixture(output, path)).toEqual(parseLipoFixture(output));
|
||||
});
|
||||
});
|
||||
+7
-378
@@ -1,274 +1,16 @@
|
||||
import { fixedAllowanceOptions } from "../../domain/fixedAllowanceRemediation.js";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
AnalysisOutputError,
|
||||
AnalysisResourceConstraintError,
|
||||
} from "../../domain/analysisErrorCore.js";
|
||||
import { err, ok, type Result } from "../../domain/result.js";
|
||||
import { err, type Result } from "../../domain/result.js";
|
||||
import {
|
||||
malformedInput,
|
||||
ParseFailureError,
|
||||
} from "../../domain/parseFailure.js";
|
||||
import {
|
||||
NATIVE_COMMAND_OUTPUT_BUDGET_BYTES,
|
||||
type NativeCommandCapture,
|
||||
} from "../CommandRunner.js";
|
||||
import { machoCpuSubtypeBase } from "../../domain/apple/machoCpuIdentity.js";
|
||||
|
||||
const architectureSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
hidden: z.boolean(),
|
||||
cpu_type: z.string().nullable(),
|
||||
cpu_subtype: z.string().nullable(),
|
||||
cpu_type_code: z.int().nonnegative().nullable(),
|
||||
cpu_subtype_code: z.int().nonnegative().nullable(),
|
||||
cpu_subtype_base: z.int().nonnegative().nullable(),
|
||||
capabilities: z.string().nullable(),
|
||||
file_offset: z.int().min(0).nullable(),
|
||||
size: z.int().min(0).nullable(),
|
||||
alignment: z.int().min(0).nullable(),
|
||||
});
|
||||
|
||||
export type LipoFieldObservation =
|
||||
| { readonly kind: "absent" }
|
||||
| {
|
||||
readonly kind: "parsed";
|
||||
readonly sourceLines: readonly string[];
|
||||
readonly value: number;
|
||||
}
|
||||
| {
|
||||
readonly kind: "uninterpretable";
|
||||
readonly sourceLines: readonly string[];
|
||||
};
|
||||
|
||||
export type LipoArchitecture = z.infer<typeof architectureSchema> & {
|
||||
readonly fields: Readonly<
|
||||
Record<"offset" | "size" | "align", LipoFieldObservation>
|
||||
>;
|
||||
readonly limitations: readonly string[];
|
||||
};
|
||||
|
||||
/** Capacity refusal before retaining expanded per-declaration source facts. */
|
||||
export class LipoFieldRetentionError extends Error {
|
||||
readonly _tag = "LipoFieldRetentionError";
|
||||
constructor(readonly requiredBytes: number) {
|
||||
super(
|
||||
"lipo numeric field observations exceed the retained native output budget",
|
||||
);
|
||||
this.name = this._tag;
|
||||
}
|
||||
}
|
||||
|
||||
interface LipoOutput {
|
||||
readonly format: "thin" | "fat";
|
||||
/** `nfat_arch` counts visible rows; Apple may append one hidden ARM64 row. */
|
||||
readonly declaredArchitectureCount: number | null;
|
||||
readonly hiddenArchitectureCount: number | null;
|
||||
readonly architectures: LipoArchitecture[];
|
||||
}
|
||||
|
||||
/** Parse `lipo -detailed_info` framing and deterministic slice metadata. */
|
||||
const parseLipoOutput = (output: string, operandPath?: string): LipoOutput => {
|
||||
// The echoed FAT operand is one value, even when its pathname contains lines.
|
||||
if (operandPath !== undefined) {
|
||||
const header = `Fat header in: ${operandPath}`;
|
||||
if (output.startsWith(header)) {
|
||||
if (output.startsWith("\r\n", header.length))
|
||||
output = output.slice(header.length + 2);
|
||||
else if (output.startsWith("\n", header.length))
|
||||
output = output.slice(header.length + 1);
|
||||
}
|
||||
}
|
||||
// lipo echoes a thin file's pathname verbatim, including embedded newlines.
|
||||
// Match its final architecture field before splitting records into lines.
|
||||
const thinArchitecture =
|
||||
/(?:^|\r?\n)Non-fat file:[\s\S]* is architecture: ([^\s]+)\s*$/u.exec(
|
||||
output,
|
||||
)?.[1];
|
||||
if (thinArchitecture !== undefined)
|
||||
return {
|
||||
format: "thin",
|
||||
declaredArchitectureCount: null,
|
||||
hiddenArchitectureCount: null,
|
||||
architectures: [
|
||||
{
|
||||
...architectureSchema.parse({
|
||||
name: thinArchitecture,
|
||||
hidden: false,
|
||||
cpu_type: null,
|
||||
cpu_subtype: null,
|
||||
cpu_type_code: null,
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
capabilities: null,
|
||||
file_offset: null,
|
||||
size: null,
|
||||
alignment: null,
|
||||
}),
|
||||
fields: {
|
||||
offset: { kind: "absent" },
|
||||
size: { kind: "absent" },
|
||||
align: { kind: "absent" },
|
||||
},
|
||||
limitations: [],
|
||||
},
|
||||
],
|
||||
};
|
||||
const architectures: LipoArchitecture[] = [];
|
||||
let declaredArchitectureCount: number | null = null;
|
||||
let hiddenArchitectureCount: number | null = null;
|
||||
let current: Record<string, string> | undefined;
|
||||
let observations: Partial<
|
||||
Record<
|
||||
"offset" | "size" | "align",
|
||||
{ readonly sourceLine: string; readonly value: number | null }[]
|
||||
>
|
||||
> = {};
|
||||
let retainedFieldBytes = 0;
|
||||
const admitFieldBytes = (bytes: number): void => {
|
||||
retainedFieldBytes += bytes;
|
||||
if (retainedFieldBytes > NATIVE_COMMAND_OUTPUT_BUDGET_BYTES)
|
||||
throw new LipoFieldRetentionError(retainedFieldBytes);
|
||||
};
|
||||
const flush = (): void => {
|
||||
if (current === undefined) return;
|
||||
const name = current.architecture ?? current["Non-fat file"];
|
||||
if (name !== undefined) {
|
||||
const observe = (
|
||||
key: "offset" | "size" | "align",
|
||||
): LipoFieldObservation => {
|
||||
const observed = observations[key];
|
||||
if (observed === undefined) return { kind: "absent" };
|
||||
const sourceLines = observed.map(({ sourceLine }) => sourceLine);
|
||||
const value = observed[0]?.value;
|
||||
return value === null ||
|
||||
value === undefined ||
|
||||
observed.some((candidate) => candidate.value !== value)
|
||||
? { kind: "uninterpretable", sourceLines }
|
||||
: { kind: "parsed", sourceLines, value };
|
||||
};
|
||||
const fields = {
|
||||
offset: observe("offset"),
|
||||
size: observe("size"),
|
||||
align: observe("align"),
|
||||
};
|
||||
const value = (field: LipoFieldObservation): number | null =>
|
||||
field.kind === "parsed" ? field.value : null;
|
||||
const limitations = Object.entries(fields).flatMap(
|
||||
([field, observation]) => {
|
||||
if (observation.kind !== "uninterpretable") return [];
|
||||
admitFieldBytes(name.length * 14 + 256);
|
||||
return [
|
||||
`lipo ${field} for ${name} could not be interpreted; ${observation.sourceLines.length === 1 ? "source line: " : "source lines:\n"}${observation.sourceLines.join("\n")}`,
|
||||
];
|
||||
},
|
||||
);
|
||||
architectures.push({
|
||||
...architectureSchema.parse({
|
||||
name: name.includes(" is architecture: ")
|
||||
? (name.split(" is architecture: ").at(-1) ?? name)
|
||||
: name.replace(/ \(hidden\)$/u, ""),
|
||||
hidden: name.endsWith(" (hidden)"),
|
||||
cpu_type: current.cputype ?? null,
|
||||
cpu_subtype: current.cpusubtype ?? null,
|
||||
cpu_type_code: cpuTypeCode(current.cputype),
|
||||
cpu_subtype_code: cpuSubtypeCode(
|
||||
current.cpusubtype,
|
||||
current.capabilities,
|
||||
),
|
||||
cpu_subtype_base: cpuSubtypeBase(current.cpusubtype),
|
||||
capabilities: current.capabilities ?? null,
|
||||
file_offset: value(fields.offset),
|
||||
size: value(fields.size),
|
||||
alignment: value(fields.align),
|
||||
}),
|
||||
fields,
|
||||
limitations,
|
||||
});
|
||||
}
|
||||
current = undefined;
|
||||
observations = {};
|
||||
};
|
||||
for (const rawLine of output.split(/\r?\n/u)) {
|
||||
const line = rawLine.trim();
|
||||
const count = /^nfat_arch\s+(\d+)(?:\s+\(\+(\d+) hidden\))?$/u.exec(line);
|
||||
if (count?.[1] !== undefined) {
|
||||
declaredArchitectureCount = integer(count[1]);
|
||||
hiddenArchitectureCount = integer(count[2] ?? "0");
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("Non-fat file:")) {
|
||||
flush();
|
||||
current = { "Non-fat file": line };
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("architecture ")) {
|
||||
flush();
|
||||
current = { architecture: line.slice("architecture ".length).trim() };
|
||||
continue;
|
||||
}
|
||||
const numericField = /^\s*(offset|size|align)(?:\s+(.*))?$/u.exec(rawLine);
|
||||
if (
|
||||
numericField?.[1] === "offset" ||
|
||||
numericField?.[1] === "size" ||
|
||||
numericField?.[1] === "align"
|
||||
) {
|
||||
current ??= {};
|
||||
const field = numericField[1];
|
||||
// UTF-16 source, joined and rendered strings, worst-case escaped JSON,
|
||||
// and per-observation bookkeeping fit the existing native output allowance.
|
||||
// Charge before storing another observation; every source line is retained
|
||||
// at most once per field and rendered at most once, never per duplicate pair.
|
||||
admitFieldBytes(rawLine.length * 18 + 128);
|
||||
const sourceValue = (numericField[2] ?? "").trim();
|
||||
const observed = {
|
||||
sourceLine: rawLine,
|
||||
value:
|
||||
field === "align" ? alignment(sourceValue) : integer(sourceValue),
|
||||
};
|
||||
(observations[field] ??= []).push(observed);
|
||||
continue;
|
||||
}
|
||||
const match = /^(cputype|cpusubtype|capabilities)\s+(.+)$/u.exec(line);
|
||||
if (match?.[1] !== undefined && match[2] !== undefined) {
|
||||
current ??= {};
|
||||
current[match[1]] = match[2].trim();
|
||||
}
|
||||
}
|
||||
flush();
|
||||
if (architectures.length === 0)
|
||||
throw malformedInput(
|
||||
"lipo-output",
|
||||
"lipo output contained no architectures",
|
||||
);
|
||||
return {
|
||||
format: "fat",
|
||||
declaredArchitectureCount,
|
||||
hiddenArchitectureCount,
|
||||
architectures,
|
||||
};
|
||||
};
|
||||
|
||||
/** Classify expected lipo parse failures once for all command adapters. */
|
||||
export const parseLipoOutputResult = (
|
||||
output: string,
|
||||
operandPath?: string,
|
||||
): Result<
|
||||
LipoOutput,
|
||||
ParseFailureError | z.ZodError | LipoFieldRetentionError
|
||||
> => {
|
||||
try {
|
||||
return ok(parseLipoOutput(output, operandPath));
|
||||
} catch (cause: unknown) {
|
||||
if (
|
||||
!(cause instanceof ParseFailureError) &&
|
||||
!(cause instanceof z.ZodError) &&
|
||||
!(cause instanceof LipoFieldRetentionError)
|
||||
)
|
||||
throw cause;
|
||||
return err(cause);
|
||||
}
|
||||
};
|
||||
LipoFieldRetentionError,
|
||||
parseLipoOutputResult,
|
||||
type LipoOutput,
|
||||
} from "../../domain/apple/lipo.js";
|
||||
import { NATIVE_COMMAND_OUTPUT_BUDGET_BYTES } from "../../domain/native/nativeCommandOutput.js";
|
||||
import type { NativeCommandCapture } from "../CommandRunner.js";
|
||||
|
||||
/** Translate classified parse failure into native command output failure. */
|
||||
export const parseLipoCapture = (
|
||||
@@ -322,116 +64,3 @@ export const parseLipoCapture = (
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
const integer = (value: string | undefined): number | null => {
|
||||
if (value === undefined || !/^\d+$/u.test(value)) return null;
|
||||
const parsed = Number.parseInt(value, 10);
|
||||
return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : null;
|
||||
};
|
||||
|
||||
const CPU_TYPES: Readonly<Record<string, number>> = {
|
||||
CPU_TYPE_X86: 7,
|
||||
CPU_TYPE_I386: 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_I386_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 ownCpuCode = (
|
||||
codes: Readonly<Record<string, number>>,
|
||||
name: string,
|
||||
): number | undefined => (Object.hasOwn(codes, name) ? codes[name] : undefined);
|
||||
|
||||
const cpuTypeCode = (value: string | undefined): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const numeric = unsigned32(value);
|
||||
return numeric ?? ownCpuCode(CPU_TYPES, value) ?? null;
|
||||
};
|
||||
|
||||
/** Normalize the subtype and its separately printed 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 = ownCpuCode(CPU_SUBTYPES, value);
|
||||
if (subtype === undefined) return null;
|
||||
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];
|
||||
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;
|
||||
};
|
||||
|
||||
/** The subtype bits a lipo record names, without its capability byte. */
|
||||
const cpuSubtypeBase = (value: string | undefined): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const subtype = unsigned32(value) ?? ownCpuCode(CPU_SUBTYPES, value);
|
||||
return subtype === undefined ? null : machoCpuSubtypeBase(subtype);
|
||||
};
|
||||
|
||||
/** 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);
|
||||
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);
|
||||
const powerText = exponent?.[1];
|
||||
if (powerText !== undefined) {
|
||||
const power = Number.parseInt(powerText, 10);
|
||||
if (power > 52) return null;
|
||||
const alignment = 2 ** power;
|
||||
const reported = exponent?.[2];
|
||||
return reported === undefined || integer(reported) === alignment
|
||||
? alignment
|
||||
: null;
|
||||
}
|
||||
return integer(value);
|
||||
};
|
||||
@@ -17,15 +17,13 @@ import {
|
||||
nativeMachoTargetForFile,
|
||||
} from "../../../fixtures/nativeCommands.js";
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
import { NATIVE_COMMAND_OUTPUT_BUDGET_BYTES } from "../../../../src/native/CommandRunner.js";
|
||||
import {
|
||||
LipoFieldRetentionError,
|
||||
parseLipoCapture,
|
||||
} from "../../../../src/native/parsers/lipo.js";
|
||||
import { parseLipoCapture } from "../../../../src/native/parsers/lipo.js";
|
||||
import {
|
||||
ArtifactReaderFailure,
|
||||
type ArtifactEntry,
|
||||
} from "../../../../src/resources/ArtifactReader.js";
|
||||
import { NATIVE_COMMAND_OUTPUT_BUDGET_BYTES } from "../../../../src/domain/native/nativeCommandOutput.js";
|
||||
import { LipoFieldRetentionError } from "../../../../src/domain/apple/lipo.js";
|
||||
|
||||
it.each(["absent", "valid", "uninterpretable"] as const)(
|
||||
"preserves %s lipo fields through native observations and structural slice inventory",
|
||||
|
||||
@@ -16,7 +16,6 @@ import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js
|
||||
|
||||
import { NativeMacOSProvider } from "../../../../src/native/NativeMacOSProvider.js";
|
||||
import {
|
||||
NATIVE_COMMAND_OUTPUT_BUDGET_BYTES,
|
||||
NATIVE_FAILURE_DIAGNOSTIC_BYTES,
|
||||
NativeCommandFailure,
|
||||
XcrunCommandRunner,
|
||||
@@ -56,6 +55,7 @@ import {
|
||||
nativeResolvedToolFixture,
|
||||
} from "../../../fixtures/nativeCommands.js";
|
||||
import { releaseOwnedCleanup } from "../../../support/cleanupRelease.js";
|
||||
import { NATIVE_COMMAND_OUTPUT_BUDGET_BYTES } from "../../../../src/domain/native/nativeCommandOutput.js";
|
||||
|
||||
/** The file runner identifies selected .plist inputs as XML. */
|
||||
const writeFixturePlist = async (path: string): Promise<void> => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { readPlistJson } from "../../../../src/native/parsers/plist.js";
|
||||
import { parseLipoOutputResult } from "../../../../src/native/parsers/lipo.js";
|
||||
import { parseLipoOutputResult } from "../../../../src/domain/apple/lipo.js";
|
||||
|
||||
it.each([
|
||||
"line\nbreak",
|
||||
|
||||
Vendored
+1
-1
@@ -1,4 +1,4 @@
|
||||
import { parseLipoOutputResult } from "../../src/native/parsers/lipo.js";
|
||||
import { parseLipoOutputResult } from "../../src/domain/apple/lipo.js";
|
||||
|
||||
/** Unwrap the production parser boundary for fixture setup and error assertions. */
|
||||
export const parseLipoFixture = (output: string, operandPath?: string) => {
|
||||
|
||||
Reference in new issue
Block a user