Merge origin/main into feat/dylib-resolution
Follow #952's Apple ownership: the Mach-O load-command reader, dylib resolution adapter and fixture move to src/artifacts/apple/, the resolver, dyld path semantics and findings to src/domain/apple/, and the dylib lane helper to scripts/verify/apple/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash } from "node:crypto";
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
import {
|
||||
artifactCli,
|
||||
artifactMcpResult,
|
||||
withArtifactMcp,
|
||||
} from "../../lib/artifact-e2e.mjs";
|
||||
|
||||
if (process.platform !== "darwin")
|
||||
throw new Error(
|
||||
"Asset catalog E2E requires macOS, Xcode actool and assetutil",
|
||||
);
|
||||
const exec = promisify(execFile);
|
||||
await exec("/usr/bin/xcrun", ["--find", "actool"]);
|
||||
await exec("/usr/bin/xcrun", ["--find", "clang"]);
|
||||
await exec("/usr/bin/assetutil", ["--version"]);
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-asset-e2e-"));
|
||||
try {
|
||||
const app = join(root, "Fixture.app");
|
||||
const resources = join(app, "Contents", "Resources");
|
||||
await mkdir(resources, { recursive: true });
|
||||
await mkdir(join(app, "Contents", "MacOS"), { recursive: true });
|
||||
await exec("/usr/bin/xcrun", [
|
||||
"clang",
|
||||
fileURLToPath(
|
||||
new URL(
|
||||
"../../../tests/conformance/interface-builder/fixture.c",
|
||||
import.meta.url,
|
||||
),
|
||||
),
|
||||
"-o",
|
||||
join(app, "Contents", "MacOS", "Fixture"),
|
||||
]);
|
||||
await writeFile(
|
||||
join(app, "Contents", "Info.plist"),
|
||||
'<?xml version="1.0"?><plist version="1.0"><dict><key>CFBundleExecutable</key><string>Fixture</string><key>CFBundleIdentifier</key><string>dev.rea.asset-fixture</string></dict></plist>',
|
||||
);
|
||||
await exec("/usr/bin/xcrun", [
|
||||
"actool",
|
||||
fileURLToPath(
|
||||
new URL(
|
||||
"../../../tests/conformance/native/Assets.xcassets",
|
||||
import.meta.url,
|
||||
),
|
||||
),
|
||||
"--compile",
|
||||
resources,
|
||||
"--platform",
|
||||
"macosx",
|
||||
"--minimum-deployment-target",
|
||||
"12.0",
|
||||
]);
|
||||
const raw = JSON.parse(
|
||||
(
|
||||
await exec("/usr/bin/assetutil", [
|
||||
"--info",
|
||||
join(resources, "Assets.car"),
|
||||
])
|
||||
).stdout,
|
||||
);
|
||||
const sha256 = createHash("sha256")
|
||||
.update(await readFile(join(resources, "Assets.car")))
|
||||
.digest("hex");
|
||||
const cli = await artifactCli("inspect-asset-catalog", app);
|
||||
assert.deepEqual(
|
||||
cli.records.map(({ metadata }) => metadata),
|
||||
raw,
|
||||
);
|
||||
assert.equal(cli.catalogs[0].sha256, sha256);
|
||||
assert.ok(
|
||||
cli.records.some(
|
||||
({ asset_name, metadata }) =>
|
||||
asset_name === "FixtureColor" && metadata.AssetType === "Color",
|
||||
),
|
||||
);
|
||||
await withArtifactMcp(app, async (client) => {
|
||||
assert.deepEqual(
|
||||
await artifactMcpResult(client, "inspect_asset_catalog"),
|
||||
cli,
|
||||
);
|
||||
const first = await artifactMcpResult(client, "inspect_asset_catalog", {
|
||||
limit: 1,
|
||||
});
|
||||
assert.equal(first.next_offset, 1);
|
||||
const next = await artifactMcpResult(client, "inspect_asset_catalog", {
|
||||
offset: first.next_offset,
|
||||
});
|
||||
assert.deepEqual([...first.records, ...next.records], cli.records);
|
||||
const invalid = await client.callTool({
|
||||
name: "inspect_asset_catalog",
|
||||
arguments: { offset: -1 },
|
||||
});
|
||||
assert.equal(invalid.isError, true);
|
||||
});
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({ ok: true, mocked: false, cli: true, stdio_mcp: true, records: raw.length, pagination: true, malformed_input_rejected: true })}\n`,
|
||||
);
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
import { parseBinaryTarget } from "../../../dist/application/BinaryTargetResolver.js";
|
||||
import { inspectAppleDispatchMetadata } from "../../../dist/native/AppleDispatchMetadata.js";
|
||||
|
||||
if (process.platform !== "darwin")
|
||||
throw new Error(
|
||||
"Apple dispatch verification requires macOS with a host Objective-C compiler and Foundation SDK",
|
||||
);
|
||||
const exec = promisify(execFile);
|
||||
const source = (name) =>
|
||||
fileURLToPath(
|
||||
new URL(`../../../tests/conformance/native/${name}`, import.meta.url),
|
||||
);
|
||||
|
||||
/** Both link modes: legacy LC_DYLD_INFO binds and the default chained fixups. */
|
||||
const LINK_MODES = [
|
||||
{
|
||||
mode: "dyld-info",
|
||||
flags: ["-Wl,-no_fixup_chains"],
|
||||
fixups: "LC_DYLD_INFO bind opcodes",
|
||||
swift: true,
|
||||
},
|
||||
{ mode: "chained", flags: [], fixups: "chained fixups:", swift: true },
|
||||
// arm64e uses authenticated chained pointers (DYLD_CHAINED_PTR_ARM64E_*).
|
||||
...(process.arch === "arm64"
|
||||
? [
|
||||
{
|
||||
mode: "chained-arm64e",
|
||||
flags: ["-arch", "arm64e"],
|
||||
fixups: "chained fixups: DYLD_CHAINED_PTR_ARM64E",
|
||||
swift: false,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
];
|
||||
|
||||
const fixupCoverage = (metadata) =>
|
||||
metadata.coverage.find(({ facet }) => facet === "pointer_fixups");
|
||||
|
||||
const inspect = async (path) => {
|
||||
const target = await parseBinaryTarget(path);
|
||||
if (!target.ok) throw target.error;
|
||||
return {
|
||||
target: target.value,
|
||||
metadata: (await inspectAppleDispatchMetadata(target.value, 5000)).result,
|
||||
};
|
||||
};
|
||||
|
||||
const checkObjc = (metadata, mode) => {
|
||||
const fixture = metadata.objc_classes.find(
|
||||
({ name, is_meta_class: meta }) => name === "ReaDispatchFixture" && !meta,
|
||||
);
|
||||
const category = metadata.objc_categories.find(
|
||||
({ name }) => name === "ReaDispatchAdditions",
|
||||
);
|
||||
const objcCoverage = metadata.coverage.find(
|
||||
({ facet }) => facet === "objc_class_method_ivar_metadata",
|
||||
);
|
||||
if (
|
||||
!metadata.objc_dispatch_implementations.some(
|
||||
(item) =>
|
||||
item.class_name === "ReaDispatchFixture" &&
|
||||
item.selector === "performAction:" &&
|
||||
item.implementation_address !== null,
|
||||
) ||
|
||||
!metadata.objc_dispatch_implementations.some(
|
||||
(item) =>
|
||||
item.selector === "fixtureVersion" && item.method_type === "class",
|
||||
) ||
|
||||
!metadata.objc_ivars.some(
|
||||
(item) => item.name === "state" && item.offset !== null,
|
||||
) ||
|
||||
!metadata.objc_protocol_records.some(
|
||||
(protocol) =>
|
||||
protocol.name === "ReaDispatchProtocol" &&
|
||||
protocol.methods.some(
|
||||
(method) => method.selector === "performAction:",
|
||||
) &&
|
||||
protocol.optional_methods.some(
|
||||
(method) => method.selector === "optionalFixtureValue",
|
||||
),
|
||||
) ||
|
||||
// The superclass lives in libobjc, so it comes from a bind fixup.
|
||||
fixture?.super_class !== "NSObject" ||
|
||||
fixture.decode.status !== "decoded" ||
|
||||
!fixture.properties.some(
|
||||
(property) =>
|
||||
property.name === "fixtureName" &&
|
||||
property.type_encoding === '@"NSString"' &&
|
||||
property.attributes.some(({ name }) => name === "C"),
|
||||
) ||
|
||||
category?.class_name !== "NSString" ||
|
||||
category.class_source !== "external" ||
|
||||
!category.instance_methods.includes("reaFixtureLength") ||
|
||||
!metadata.objc_dispatch_implementations.some(
|
||||
(item) =>
|
||||
item.class_name === "NSString" &&
|
||||
item.category === "ReaDispatchAdditions" &&
|
||||
item.selector === "reaFixtureLength",
|
||||
) ||
|
||||
objcCoverage?.status !== "complete" ||
|
||||
fixupCoverage(metadata)?.reason?.startsWith(mode.fixups) !== true
|
||||
)
|
||||
throw new Error(
|
||||
`Apple metadata fixture drifted (${mode.mode}): ${JSON.stringify(metadata)}`,
|
||||
);
|
||||
};
|
||||
|
||||
const checkSwift = (metadata, mode) => {
|
||||
if (
|
||||
!metadata.swift_dispatch_slots.some(
|
||||
(slot) =>
|
||||
slot.table_kind === "class_vtable" &&
|
||||
slot.owner === "ReaVtableFixture" &&
|
||||
slot.decode.status === "decoded",
|
||||
)
|
||||
)
|
||||
throw new Error(
|
||||
`Swift class vtable fixture unavailable (${mode.mode}): ${JSON.stringify(metadata.coverage)}`,
|
||||
);
|
||||
if (
|
||||
!metadata.swift_conformances.some(
|
||||
(item) =>
|
||||
item.type_name === "ReaWitnessFixture" &&
|
||||
item.protocol_name === "ReaWitnessFixtureProtocol",
|
||||
) ||
|
||||
!metadata.swift_dispatch_slots.some(
|
||||
(item) =>
|
||||
item.owner === "ReaWitnessFixture: ReaWitnessFixtureProtocol" &&
|
||||
item.implementation_address !== null,
|
||||
) ||
|
||||
fixupCoverage(metadata)?.reason?.startsWith(mode.fixups) !== true
|
||||
)
|
||||
throw new Error(
|
||||
`Swift metadata fixture drifted (${mode.mode}): ${JSON.stringify(metadata)}`,
|
||||
);
|
||||
};
|
||||
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-dispatch-"));
|
||||
try {
|
||||
const results = [];
|
||||
for (const mode of LINK_MODES) {
|
||||
const objcPath = join(root, `fixture-${mode.mode}`);
|
||||
await exec("/usr/bin/xcrun", [
|
||||
"clang",
|
||||
"-O2",
|
||||
"-framework",
|
||||
"Foundation",
|
||||
...mode.flags,
|
||||
source("dispatch.m"),
|
||||
"-o",
|
||||
objcPath,
|
||||
]);
|
||||
for (const variant of ["symbols", "stripped"]) {
|
||||
if (variant === "stripped")
|
||||
await exec("/usr/bin/strip", ["-x", objcPath]);
|
||||
const { target, metadata } = await inspect(objcPath);
|
||||
checkObjc(metadata, mode);
|
||||
results.push({
|
||||
variant: `objc-${mode.mode}-${variant}`,
|
||||
target_sha256: target.sha256,
|
||||
classes: metadata.objc_classes.length,
|
||||
categories: metadata.objc_categories.length,
|
||||
implementations: metadata.objc_dispatch_implementations.length,
|
||||
fixups: fixupCoverage(metadata)?.reason,
|
||||
});
|
||||
}
|
||||
if (!mode.swift) continue;
|
||||
const swiftPath = join(root, `libReaWitnessFixture-${mode.mode}.dylib`);
|
||||
await exec("/usr/bin/xcrun", [
|
||||
"swiftc",
|
||||
"-emit-library",
|
||||
"-O",
|
||||
...mode.flags
|
||||
.map((flag) => flag.replace(/^-Wl,/u, "-Xlinker\0"))
|
||||
.flatMap((flag) => flag.split("\0")),
|
||||
source("dispatch.swift"),
|
||||
"-o",
|
||||
swiftPath,
|
||||
]);
|
||||
for (const variant of ["symbols", "stripped"]) {
|
||||
if (variant === "stripped")
|
||||
await exec("/usr/bin/strip", ["-x", swiftPath]);
|
||||
const { target, metadata } = await inspect(swiftPath);
|
||||
checkSwift(metadata, mode);
|
||||
results.push({
|
||||
variant: `swift-${mode.mode}-${variant}`,
|
||||
target_sha256: target.sha256,
|
||||
conformances: metadata.swift_conformances.length,
|
||||
slots: metadata.swift_dispatch_slots.length,
|
||||
fixups: fixupCoverage(metadata)?.reason,
|
||||
});
|
||||
}
|
||||
}
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({ ok: true, target_executed: false, fixtures: results })}\n`,
|
||||
);
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { parseBinaryTarget } from "../../../dist/application/BinaryTargetResolver.js";
|
||||
import { ArtifactProvider } from "../../../dist/artifacts/ArtifactProvider.js";
|
||||
|
||||
const exec = promisify(execFile);
|
||||
const fixtureRoot = await mkdtemp(join(tmpdir(), "rea-interface-builder-"));
|
||||
const appPath = join(fixtureRoot, "Fixture.app");
|
||||
const resourcesPath = join(appPath, "Contents", "Resources");
|
||||
const executablePath = join(appPath, "Contents", "MacOS", "Fixture");
|
||||
const nibPath = join(resourcesPath, "Main.nib");
|
||||
const sourceRoot = fileURLToPath(
|
||||
new URL("../../../tests/conformance/interface-builder/", import.meta.url),
|
||||
);
|
||||
|
||||
try {
|
||||
await mkdir(join(appPath, "Contents", "MacOS"), { recursive: true });
|
||||
await mkdir(resourcesPath, { recursive: true });
|
||||
await writeFile(
|
||||
join(appPath, "Contents", "Info.plist"),
|
||||
`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0"><dict><key>CFBundleExecutable</key><string>Fixture</string><key>CFBundleIdentifier</key><string>dev.rea.interface-builder-fixture</string></dict></plist>\n`,
|
||||
);
|
||||
await exec("xcrun", [
|
||||
"ibtool",
|
||||
"--errors",
|
||||
"--warnings",
|
||||
"--compile",
|
||||
nibPath,
|
||||
join(sourceRoot, "Main.xib"),
|
||||
]);
|
||||
await exec("cc", [join(sourceRoot, "fixture.c"), "-o", executablePath]);
|
||||
|
||||
const target = await parseBinaryTarget(appPath);
|
||||
if (!target.ok) throw target.error;
|
||||
const execution = await new ArtifactProvider()
|
||||
.createClient(target.value)
|
||||
.execute("decode_interface_builder", {}, {});
|
||||
if (!execution.ok) throw execution.error;
|
||||
|
||||
const decoded = execution.value.result;
|
||||
assert.equal(decoded.documents.length, 1);
|
||||
assert.equal(
|
||||
decoded.documents[0]?.relative_path,
|
||||
"Contents/Resources/Main.nib",
|
||||
);
|
||||
assert.equal(decoded.documents[0]?.document_kind, "nib");
|
||||
assert.equal(decoded.documents[0]?.hierarchy_complete, true);
|
||||
assert.ok(decoded.documents[0]?.object_count > 0);
|
||||
assert.ok(decoded.documents[0]?.connection_count >= 2);
|
||||
|
||||
const button = decoded.graph.nodes.find(
|
||||
(node) => node.kind === "control" && node.name === "NSButton",
|
||||
);
|
||||
const action = decoded.graph.nodes.find(
|
||||
(node) => node.kind === "action" && node.name === "runAction:",
|
||||
);
|
||||
assert.ok(button, "compiled button control was not decoded");
|
||||
assert.ok(action, "compiled action selector was not decoded");
|
||||
assert.ok(
|
||||
decoded.graph.edges.some(
|
||||
(edge) =>
|
||||
edge.relation === "target_action" &&
|
||||
edge.resolution === "observed" &&
|
||||
(edge.from === action.id || edge.to === action.id),
|
||||
),
|
||||
"compiled target/action connection was not represented as observed",
|
||||
);
|
||||
assert.ok(
|
||||
decoded.graph.coverage.some(
|
||||
(facet) =>
|
||||
facet.facet === "archive_decode" && facet.status === "complete",
|
||||
),
|
||||
);
|
||||
assert.equal(decoded.graph.truncated, false);
|
||||
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({
|
||||
ok: true,
|
||||
fixture: "compiled-appkit-nib",
|
||||
provider: execution.value.provider,
|
||||
target_sha256: decoded.target_sha256,
|
||||
archive_sha256: decoded.documents[0].archive_sha256,
|
||||
objects: decoded.documents[0].object_count,
|
||||
connections: decoded.documents[0].connection_count,
|
||||
recovered_control: button.name,
|
||||
recovered_action: action.name,
|
||||
hierarchy_complete: decoded.documents[0].hierarchy_complete,
|
||||
truncated: decoded.graph.truncated,
|
||||
})}\n`,
|
||||
);
|
||||
} finally {
|
||||
await rm(fixtureRoot, { recursive: true, force: true });
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash } from "node:crypto";
|
||||
import {
|
||||
artifactCli,
|
||||
artifactMcpResult,
|
||||
withArtifactMcp,
|
||||
} from "../../lib/artifact-e2e.mjs";
|
||||
|
||||
if (process.platform !== "darwin")
|
||||
throw new Error(
|
||||
"Real Foundation keyed archive verification requires macOS and Xcode Swift tools",
|
||||
);
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-keyed-fixture-"));
|
||||
try {
|
||||
const archive = join(root, "model.plist");
|
||||
await promisify(execFile)("/usr/bin/xcrun", [
|
||||
"swift",
|
||||
"-module-cache-path",
|
||||
join(root, "modules"),
|
||||
fileURLToPath(
|
||||
new URL(
|
||||
"../../../tests/conformance/native/keyed-archive.swift",
|
||||
import.meta.url,
|
||||
),
|
||||
),
|
||||
archive,
|
||||
]);
|
||||
const graph = await artifactCli("inspect-keyed-archive", archive);
|
||||
const digest = createHash("sha256")
|
||||
.update(await readFile(archive))
|
||||
.digest("hex");
|
||||
assert.equal(graph.archive_sha256, digest);
|
||||
assert.equal(graph.target_sha256, digest);
|
||||
await withArtifactMcp(archive, async (client) => {
|
||||
assert.deepEqual(
|
||||
await artifactMcpResult(client, "inspect_keyed_archive"),
|
||||
graph,
|
||||
);
|
||||
const page = await artifactMcpResult(client, "inspect_keyed_archive", {
|
||||
offset: 1,
|
||||
limit: 1,
|
||||
});
|
||||
assert.deepEqual(page.objects, [graph.objects[1]]);
|
||||
assert.equal(page.next_offset, 2);
|
||||
const invalid = await client.callTool({
|
||||
name: "inspect_keyed_archive",
|
||||
arguments: { path: "../other.plist" },
|
||||
});
|
||||
assert.equal(invalid.isError, true);
|
||||
});
|
||||
const xml = join(root, "model.xml.plist");
|
||||
await promisify(execFile)("/usr/bin/plutil", [
|
||||
"-convert",
|
||||
"xml1",
|
||||
"-o",
|
||||
xml,
|
||||
archive,
|
||||
]);
|
||||
const xmlGraph = await artifactCli("inspect-keyed-archive", xml);
|
||||
const expected = JSON.parse(
|
||||
await readFile(
|
||||
new URL(
|
||||
"../../../tests/fixtures/golden/keyed-archive/graph.json",
|
||||
import.meta.url,
|
||||
),
|
||||
"utf8",
|
||||
),
|
||||
);
|
||||
assert.deepEqual(
|
||||
Object.fromEntries(
|
||||
Object.entries(xmlGraph).filter(
|
||||
([key]) =>
|
||||
!["archive_path", "archive_sha256", "target_sha256"].includes(key),
|
||||
),
|
||||
),
|
||||
expected,
|
||||
);
|
||||
assert.deepEqual(
|
||||
graph.references.map(({ source, path, target, status }) => ({
|
||||
source,
|
||||
path,
|
||||
target,
|
||||
status,
|
||||
})),
|
||||
xmlGraph.references.map(({ source, path, target, status }) => ({
|
||||
source,
|
||||
path,
|
||||
target,
|
||||
status,
|
||||
})),
|
||||
);
|
||||
if (
|
||||
graph?.archive_format !== "binary-plist" ||
|
||||
!graph.references.some(
|
||||
(link) =>
|
||||
link.source !== null &&
|
||||
link.source === link.target &&
|
||||
link.status === "resolved",
|
||||
) ||
|
||||
!graph.objects.some((item) => item.class_name?.includes("ReaArchiveRecord"))
|
||||
)
|
||||
throw new Error(
|
||||
`Real Foundation archive graph drifted: ${JSON.stringify(graph)}`,
|
||||
);
|
||||
const record = graph.objects.find((item) =>
|
||||
item.class_name?.includes("ReaArchiveRecord"),
|
||||
);
|
||||
if (
|
||||
graph.references.filter(
|
||||
(link) => link.target === record.id && link.source !== record.id,
|
||||
).length < 2
|
||||
)
|
||||
throw new Error("Real shared object identity was not preserved");
|
||||
const uidRoot = join(root, "uid-root.plist");
|
||||
await promisify(execFile)("/usr/bin/xcrun", [
|
||||
"swift",
|
||||
"-module-cache-path",
|
||||
join(root, "modules"),
|
||||
fileURLToPath(
|
||||
new URL(
|
||||
"../../../tests/conformance/native/keyed-archive-uid-root.swift",
|
||||
import.meta.url,
|
||||
),
|
||||
),
|
||||
uidRoot,
|
||||
]);
|
||||
const uidRootXml = join(root, "uid-root.xml.plist");
|
||||
await promisify(execFile)("/usr/bin/plutil", [
|
||||
"-convert",
|
||||
"xml1",
|
||||
"-o",
|
||||
uidRootXml,
|
||||
uidRoot,
|
||||
]);
|
||||
for (const path of [uidRoot, uidRootXml]) {
|
||||
const uidGraph = await artifactCli("inspect-keyed-archive", path);
|
||||
assert.deepEqual(
|
||||
uidGraph.references.map(({ source, path, target, status }) => ({
|
||||
source,
|
||||
path,
|
||||
target,
|
||||
status,
|
||||
})),
|
||||
[{ source: null, path: ["UID"], target: 1, status: "resolved" }],
|
||||
);
|
||||
}
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({ ok: true, mocked: false, cli: true, stdio_mcp: true, xml_golden: true, uid_named_root: true, format: graph.archive_format, objects: graph.total_objects, references: graph.total_references, shared_identity: true, cyclic_identity: true, target_classes_instantiated_by_reader: false })}\n`,
|
||||
);
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import {
|
||||
artifactCliEvidence,
|
||||
artifactMcpResult,
|
||||
withArtifactMcp,
|
||||
} from "../../lib/artifact-e2e.mjs";
|
||||
import {
|
||||
buildMacosBundleFixture,
|
||||
preflightMacosBundleFixture,
|
||||
} from "../../fixtures/apple/macos-bundle.mjs";
|
||||
import { verifyDylibResolution } from "./macos-dylib-e2e.mjs";
|
||||
|
||||
const exec = promisify(execFile);
|
||||
|
||||
await preflightMacosBundleFixture();
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-macos-bundle-"));
|
||||
|
||||
/** Bundle roles relative to the application root, independent of the container. */
|
||||
const anatomy = (projection, applicationRoot) => {
|
||||
const relative = (path) =>
|
||||
applicationRoot === "."
|
||||
? path
|
||||
: path === applicationRoot
|
||||
? "."
|
||||
: path.slice(applicationRoot.length + 1);
|
||||
const paths = (components) =>
|
||||
components.map(({ path }) => relative(path)).sort();
|
||||
return {
|
||||
bundles: projection.bundles.map((bundle) => ({
|
||||
path: relative(bundle.path),
|
||||
role: bundle.role,
|
||||
layout: bundle.layout,
|
||||
parent: bundle.parent_path === null ? null : relative(bundle.parent_path),
|
||||
info_plist: bundle.info_plist_path && relative(bundle.info_plist_path),
|
||||
executables: bundle.executable_candidates.map(relative),
|
||||
signing: bundle.signing_paths.map(relative),
|
||||
})),
|
||||
privileged_helpers: paths(projection.components.privileged_helpers),
|
||||
launchd_plists: projection.components.launchd_plists
|
||||
.map(({ path, domain }) => `${domain}:${relative(path)}`)
|
||||
.sort(),
|
||||
helpers: paths(projection.components.helpers),
|
||||
};
|
||||
};
|
||||
|
||||
const FRAMEWORK = "Contents/Frameworks/Core.framework";
|
||||
const EXPECTED_BUNDLES = [
|
||||
[".", "application", "macos-deep", null],
|
||||
[FRAMEWORK, "framework", "versioned-framework", "."],
|
||||
[
|
||||
`${FRAMEWORK}/Versions/A/XPCServices/FwSvc.xpc`,
|
||||
"xpc-service",
|
||||
"macos-deep",
|
||||
FRAMEWORK,
|
||||
],
|
||||
["Contents/Library/LoginItems/Login.app", "login-item", "macos-deep", "."],
|
||||
["Contents/PlugIns/Ext.appex", "app-extension", "macos-deep", "."],
|
||||
["Contents/XPCServices/Svc.xpc", "xpc-service", "macos-deep", "."],
|
||||
];
|
||||
|
||||
try {
|
||||
const { app } = await buildMacosBundleFixture(root);
|
||||
const zip = join(root, "MacFixture.zip");
|
||||
await exec("/usr/bin/ditto", ["-c", "-k", "--keepParent", app, zip]);
|
||||
const staging = join(root, "dmg-source");
|
||||
await mkdir(staging);
|
||||
await exec("/usr/bin/ditto", [app, join(staging, "MacFixture.app")]);
|
||||
await symlink("/Applications", join(staging, "Applications"));
|
||||
const dmg = join(root, "MacFixture.dmg");
|
||||
// The default APFS image attaches as an image disk plus a synthesized
|
||||
// container, which exercises detach of devices sharing one image.
|
||||
await exec("/usr/bin/hdiutil", [
|
||||
"create",
|
||||
"-quiet",
|
||||
"-srcfolder",
|
||||
staging,
|
||||
"-volname",
|
||||
"ReaFixture",
|
||||
"-format",
|
||||
"UDZO",
|
||||
dmg,
|
||||
]);
|
||||
|
||||
const containers = [
|
||||
{ target: app, format: "directory", applicationRoot: "." },
|
||||
{ target: zip, format: "zip", applicationRoot: "MacFixture.app" },
|
||||
{
|
||||
target: dmg,
|
||||
format: "dmg",
|
||||
applicationRoot: "MacFixture.dmg/ReaFixture/MacFixture.app",
|
||||
},
|
||||
];
|
||||
const anatomies = [];
|
||||
for (const { target, format, applicationRoot } of containers) {
|
||||
const inventory = await artifactCliEvidence("inspect-artifact", target);
|
||||
const input = join(root, `${format}-projection.json`);
|
||||
await writeFile(input, JSON.stringify({ inventory_evidence: [inventory] }));
|
||||
const evidence = await artifactCliEvidence(
|
||||
"project-apple-application-graph",
|
||||
input,
|
||||
);
|
||||
const projection = evidence.normalized_result;
|
||||
assert.equal(evidence.subject?.format, format);
|
||||
assert.equal(projection.root_format, format);
|
||||
assert.deepEqual(projection.platforms, ["macos"]);
|
||||
assert.deepEqual(projection.application_roots, [applicationRoot]);
|
||||
assert.equal(projection.coverage.status, "complete-within-inventory");
|
||||
const observed = anatomy(projection, applicationRoot);
|
||||
assert.deepEqual(
|
||||
observed.bundles.map(({ path, role, layout, parent }) => [
|
||||
path,
|
||||
role,
|
||||
layout,
|
||||
parent,
|
||||
]),
|
||||
EXPECTED_BUNDLES,
|
||||
`${format} bundle roles drifted`,
|
||||
);
|
||||
assert.deepEqual(observed.privileged_helpers, [
|
||||
"Contents/Library/LaunchServices/com.example.rea.helper",
|
||||
]);
|
||||
assert.deepEqual(observed.launchd_plists, [
|
||||
"agent:Contents/Library/LaunchAgents/com.example.rea.agent.plist",
|
||||
"daemon:Contents/Library/LaunchDaemons/com.example.rea.helper.plist",
|
||||
]);
|
||||
assert.deepEqual(observed.helpers, ["Contents/Helpers/rea-tool"]);
|
||||
assert.ok(
|
||||
projection.symlinks.some((path) =>
|
||||
path.endsWith(`${FRAMEWORK}/Versions/Current`),
|
||||
),
|
||||
"Versions/Current symlink was not reported",
|
||||
);
|
||||
anatomies.push(observed);
|
||||
if (format === "directory")
|
||||
await withArtifactMcp(app, async (client) => {
|
||||
assert.deepEqual(
|
||||
await artifactMcpResult(client, "project_apple_application_graph", {
|
||||
inventory_evidence: [inventory],
|
||||
}),
|
||||
projection,
|
||||
);
|
||||
});
|
||||
}
|
||||
assert.deepEqual(anatomies[1], anatomies[0], "ZIP anatomy differs");
|
||||
assert.deepEqual(anatomies[2], anatomies[0], "DMG anatomy differs");
|
||||
const { stdout: attached } = await exec("/usr/bin/hdiutil", ["info"]);
|
||||
assert.ok(!attached.includes(dmg), "DMG remained attached after inventory");
|
||||
const dylibs = await verifyDylibResolution(app);
|
||||
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({
|
||||
ok: true,
|
||||
mocked: false,
|
||||
cli: true,
|
||||
stdio_mcp: true,
|
||||
containers: containers.map(({ format }) => format),
|
||||
bundles: EXPECTED_BUNDLES.length,
|
||||
dmg_detached: true,
|
||||
dylib_resolution: dylibs,
|
||||
})}\n`,
|
||||
);
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { realpath } from "node:fs/promises";
|
||||
import { join, relative } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import {
|
||||
artifactCliEvidence,
|
||||
artifactMcpResult,
|
||||
withArtifactMcp,
|
||||
} from "../../lib/artifact-e2e.mjs";
|
||||
|
||||
const exec = promisify(execFile);
|
||||
|
||||
const MAIN = "Contents/MacOS/MacFixture";
|
||||
const SERVICE = "Contents/XPCServices/Svc.xpc/Contents/MacOS/Svc";
|
||||
const CORE = "Contents/Frameworks/Core.framework/Versions/A/Core";
|
||||
const DEPENDENCY_COMMANDS = new Set([
|
||||
"LC_LOAD_DYLIB",
|
||||
"LC_LOAD_WEAK_DYLIB",
|
||||
"LC_REEXPORT_DYLIB",
|
||||
"LC_LAZY_LOAD_DYLIB",
|
||||
"LC_LOAD_UPWARD_DYLIB",
|
||||
]);
|
||||
|
||||
/** Dependencies, rpaths and install name of one slice as Apple's otool reports them. */
|
||||
const otoolLoadCommands = async (path, architecture) => {
|
||||
const { stdout } = await exec(
|
||||
"/usr/bin/xcrun",
|
||||
["otool", "-arch", architecture, "-l", path],
|
||||
{ maxBuffer: 64 * 1024 * 1024 },
|
||||
);
|
||||
const result = { dependencies: [], rpaths: [], install_name: null };
|
||||
for (const block of stdout.split(/\nLoad command \d+\n/u)) {
|
||||
const command = /^\s*cmd (\S+)/mu.exec(block)?.[1];
|
||||
const name = /^\s*name (.+?) \(offset \d+\)$/mu.exec(block)?.[1];
|
||||
if (command === "LC_RPATH")
|
||||
result.rpaths.push(/^\s*path (.+?) \(offset \d+\)$/mu.exec(block)?.[1]);
|
||||
else if (command === "LC_ID_DYLIB") result.install_name = name;
|
||||
else if (DEPENDENCY_COMMANDS.has(command))
|
||||
result.dependencies.push(`${command} ${name}`);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
/** In-bundle images dyld actually loaded, in load order. */
|
||||
const loadedImages = async (app, executable) => {
|
||||
const { stderr } = await exec(join(app, executable), [], {
|
||||
env: { PATH: "/usr/bin:/bin", DYLD_PRINT_LIBRARIES: "1" },
|
||||
});
|
||||
const appRoot = await realpath(app);
|
||||
const loaded = [];
|
||||
for (const line of stderr.split("\n")) {
|
||||
const path = /^dyld\[\d+\]: <[0-9A-F-]+> (.+)$/u.exec(line)?.[1];
|
||||
if (path === undefined) continue;
|
||||
// Shared-cache libraries are reported by path but are not files on disk.
|
||||
const real = await realpath(path).catch(() => undefined);
|
||||
if (real?.startsWith(`${appRoot}/`) === true)
|
||||
loaded.push(relative(appRoot, real));
|
||||
}
|
||||
return loaded;
|
||||
};
|
||||
|
||||
/** Images a process root loads according to the trace, in first-load order. */
|
||||
const predictedImages = (trace, root) => {
|
||||
const loaded = [root];
|
||||
for (const edge of trace.edges)
|
||||
if (
|
||||
edge.root === root &&
|
||||
["resolved", "conditional"].includes(edge.resolution.status) &&
|
||||
!loaded.includes(edge.resolution.image)
|
||||
)
|
||||
loaded.push(edge.resolution.image);
|
||||
return loaded;
|
||||
};
|
||||
|
||||
const findEdge = (trace, root, loader, installName) => {
|
||||
const edge = trace.edges.find(
|
||||
(candidate) =>
|
||||
candidate.root === root &&
|
||||
candidate.loader === loader &&
|
||||
candidate.install_name === installName,
|
||||
);
|
||||
assert.ok(edge, `missing edge ${root}: ${loader} -> ${installName}`);
|
||||
return edge;
|
||||
};
|
||||
|
||||
const verifyResolutionShapes = (trace) => {
|
||||
const core = findEdge(
|
||||
trace,
|
||||
MAIN,
|
||||
MAIN,
|
||||
"@rpath/Core.framework/Versions/A/Core",
|
||||
);
|
||||
assert.deepEqual(
|
||||
core.candidates.map(({ source, outcome }) => [source, outcome]),
|
||||
[
|
||||
["rpath", "absent"],
|
||||
["rpath", "resolved"],
|
||||
],
|
||||
);
|
||||
assert.deepEqual(core.resolution, { status: "resolved", image: CORE });
|
||||
const weak = findEdge(trace, MAIN, MAIN, "@rpath/libgone.dylib");
|
||||
assert.equal(weak.weak, true);
|
||||
assert.equal(weak.resolution.status, "unresolved");
|
||||
const delayed = findEdge(trace, MAIN, MAIN, "@rpath/libdelay.dylib");
|
||||
assert.equal(delayed.encoding, "dylib_use_command");
|
||||
assert.equal(delayed.delayed_init, true);
|
||||
const chain = findEdge(trace, MAIN, CORE, "@rpath/libchain.dylib");
|
||||
assert.equal(
|
||||
chain.resolution.image,
|
||||
"Contents/Frameworks/Core.framework/Versions/A/Libraries/libchain.dylib",
|
||||
"the loading image's own LC_RPATH must be searched first",
|
||||
);
|
||||
const service = findEdge(
|
||||
trace,
|
||||
SERVICE,
|
||||
SERVICE,
|
||||
"@rpath/Core.framework/Versions/A/Core",
|
||||
);
|
||||
assert.equal(service.candidates[0]?.outcome, "outside-target");
|
||||
assert.deepEqual(service.resolution, { status: "conditional", image: CORE });
|
||||
const system = trace.edges.filter(({ install_name: name }) =>
|
||||
name.startsWith("/usr/lib/"),
|
||||
);
|
||||
assert.ok(system.length > 0);
|
||||
assert.ok(
|
||||
system.every(({ resolution }) => resolution.status === "undetermined"),
|
||||
);
|
||||
const kinds = trace.findings.map(({ kind, image }) => `${kind}:${image}`);
|
||||
assert.ok(kinds.includes(`weak-load-unresolved:${MAIN}`));
|
||||
assert.ok(kinds.includes(`earlier-rpath-candidate-absent:${MAIN}`));
|
||||
};
|
||||
|
||||
const verifyParserConformance = async (app, trace) => {
|
||||
let slices = 0;
|
||||
for (const image of trace.images)
|
||||
for (const slice of image.slices) {
|
||||
const expected = await otoolLoadCommands(
|
||||
join(app, image.path),
|
||||
slice.architecture,
|
||||
);
|
||||
const edges = trace.edges.filter(
|
||||
({ loader, architecture, root }) =>
|
||||
loader === image.path &&
|
||||
architecture === slice.architecture &&
|
||||
root ===
|
||||
trace.edges.find(
|
||||
(edge) =>
|
||||
edge.loader === image.path &&
|
||||
edge.architecture === slice.architecture,
|
||||
)?.root,
|
||||
);
|
||||
assert.deepEqual(
|
||||
{
|
||||
dependencies: edges.map(
|
||||
({ command, install_name: name }) => `${command} ${name}`,
|
||||
),
|
||||
rpaths: slice.rpaths,
|
||||
install_name: slice.install_name,
|
||||
},
|
||||
expected,
|
||||
`${image.path} (${slice.architecture}) differs from otool -l`,
|
||||
);
|
||||
slices += 1;
|
||||
}
|
||||
return slices;
|
||||
};
|
||||
|
||||
/** Verify trace_dylib_resolution against otool and the real dyld on the fixture app. */
|
||||
export async function verifyDylibResolution(app) {
|
||||
const trace = (await artifactCliEvidence("trace-dylib-resolution", app))
|
||||
.normalized_result;
|
||||
await withArtifactMcp(app, async (client) => {
|
||||
assert.deepEqual(
|
||||
await artifactMcpResult(client, "trace_dylib_resolution"),
|
||||
trace,
|
||||
);
|
||||
const invalid = await client.callTool({
|
||||
name: "trace_dylib_resolution",
|
||||
arguments: { roots: ["../outside"] },
|
||||
});
|
||||
assert.equal(invalid.isError, true);
|
||||
});
|
||||
assert.equal(trace.coverage.status, "complete");
|
||||
assert.deepEqual(trace.roots.map(({ image }) => image).sort(), [
|
||||
"Contents/Frameworks/Core.framework/Versions/A/XPCServices/FwSvc.xpc/Contents/MacOS/FwSvc",
|
||||
"Contents/Helpers/rea-tool",
|
||||
"Contents/Library/LaunchServices/com.example.rea.helper",
|
||||
"Contents/Library/LoginItems/Login.app/Contents/MacOS/Login",
|
||||
MAIN,
|
||||
"Contents/PlugIns/Ext.appex/Contents/MacOS/Ext",
|
||||
SERVICE,
|
||||
]);
|
||||
verifyResolutionShapes(trace);
|
||||
const slices = await verifyParserConformance(app, trace);
|
||||
for (const root of [MAIN, SERVICE])
|
||||
assert.deepEqual(
|
||||
await loadedImages(app, root),
|
||||
predictedImages(trace, root),
|
||||
`${root} load order differs from DYLD_PRINT_LIBRARIES`,
|
||||
);
|
||||
return {
|
||||
roots: trace.roots.length,
|
||||
edges: trace.edges.length,
|
||||
otool_slices: slices,
|
||||
dyld_runtime_roots: 2,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user