fix(artifacts): infer legacy Mach-O simulator platforms

This commit is contained in:
morluto
2026-10-10 05:13:53 +08:00
parent 5751f5caac
commit 5aa200285a
3 changed files with 169 additions and 6 deletions
@@ -1,3 +1,7 @@
import { createHash } from "node:crypto";
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
@@ -13,16 +17,124 @@ import {
machoImage,
readerOf,
rpathCommand,
versionMinCommand,
} from "./MachoImage.fixture.js";
import {
MAX_LOAD_COMMAND_BYTES,
hasMachoMagic,
readMachoImage,
} from "./MachoLoadCommandReader.js";
import { traceDylibResolution } from "./DylibResolutionReader.js";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
const read = (bytes: Uint8Array) =>
readMachoImage(readerOf(bytes), bytes.length);
// Legacy `LC_VERSION_MIN_*` commands carry no simulator flag: per dyld
// rules Intel images are simulator, ARM images are device (tvOS simulator
// is x86_64 only). A legacy simulator executable must not fail resolution
// against a modern simulator dylib.
it.each([
{ command: LC.VERSION_MIN_IPHONEOS, cpu: CPU.i386, platform: 7 },
{ command: LC.VERSION_MIN_IPHONEOS, cpu: CPU.x86_64, platform: 7 },
{ command: LC.VERSION_MIN_IPHONEOS, cpu: CPU.arm64, platform: 2 },
{ command: LC.VERSION_MIN_TVOS, cpu: CPU.x86_64, platform: 8 },
{ command: LC.VERSION_MIN_TVOS, cpu: CPU.arm64, platform: 3 },
{ command: LC.VERSION_MIN_WATCHOS, cpu: CPU.i386, platform: 9 },
{ command: LC.VERSION_MIN_WATCHOS, cpu: CPU.arm64, platform: 4 },
{ command: LC.VERSION_MIN_MACOSX, cpu: CPU.x86_64, platform: 1 },
] as const)(
"classifies legacy command $command on $cpu.type as platform $platform",
async ({ command, cpu, platform }) => {
const facts = await read(
machoImage({ cpu, commands: [versionMinCommand(command)] }),
);
if (facts.status !== "parsed") throw new Error(facts.status);
expect(facts.slices[0]).toMatchObject({ platform, platforms: [platform] });
},
);
it("uses legacy simulator and device platform facts when resolving dylibs", async () => {
const root = await createTestTempDirectory("rea-macho-legacy-platform-");
const dylibPath = "Frameworks/libThing.dylib";
const installName = "@rpath/libThing.dylib";
const intelExecutable = machoImage({
cpu: CPU.x86_64,
commands: [
versionMinCommand(LC.VERSION_MIN_IPHONEOS),
rpathCommand("@executable_path/Frameworks"),
dylibCommand(LC.LOAD_DYLIB, installName),
],
});
const armExecutable = machoImage({
cpu: CPU.arm64,
commands: [
versionMinCommand(LC.VERSION_MIN_IPHONEOS),
rpathCommand("@executable_path/Frameworks"),
dylibCommand(LC.LOAD_DYLIB, installName),
],
});
const simulatorDylib = fatImage([
{
cpu: CPU.x86_64,
bytes: machoImage({
cpu: CPU.x86_64,
fileType: FILE_TYPE.dylib,
commands: [
buildVersionCommand(7),
dylibCommand(LC.ID_DYLIB, installName),
],
}),
},
{
cpu: CPU.arm64,
bytes: machoImage({
cpu: CPU.arm64,
fileType: FILE_TYPE.dylib,
commands: [
buildVersionCommand(7),
dylibCommand(LC.ID_DYLIB, installName),
],
}),
},
]);
await mkdir(join(root, "Frameworks"), { recursive: true });
await writeFile(join(root, "Intel"), intelExecutable);
await writeFile(join(root, "Arm"), armExecutable);
await writeFile(join(root, dylibPath), simulatorDylib);
const trace = async (target: string, architecture: "arm64" | "x86_64") => {
const bytes = target === "Intel" ? intelExecutable : armExecutable;
return traceDylibResolution({
rootPath: root,
targetPath: join(root, target),
targetSha256: createHash("sha256").update(bytes).digest("hex"),
enumerateRoots: false,
parameters: { roots: [target], architecture },
});
};
const intelTrace = await trace("Intel", "x86_64");
const armTrace = await trace("Arm", "arm64");
expect(intelTrace.edges).toContainEqual(
expect.objectContaining({
loader: "Intel",
resolution: expect.objectContaining({
status: "resolved",
image: dylibPath,
}),
}),
);
expect(armTrace.edges).toContainEqual(
expect.objectContaining({
loader: "Arm",
candidates: expect.arrayContaining([
expect.objectContaining({ outcome: "platform-mismatch" }),
]),
}),
);
});
describe("Mach-O load command reader: decodes thin-image load commands", () => {
it("decodes dylib loading commands of a thin image", async () => {
const image = machoImage({