feat(apple): inspect dyld shared caches and resolve system paths
macOS 11 and later ship system libraries only inside the dyld shared cache. trace_dylib_resolution therefore left every /usr/lib and /System load undetermined, and REA had no way to see what a cache contains. inspect_dyld_shared_cache (CLI inspect-dyld-shared-cache) reads a cache file set selected by path, without an open target. Its pure TypeScript reader parses modern and legacy dyld_cache_header layouts and reports: - UUID, architecture, platform, OS version, Mac Catalyst alternate, cache type, shared region, max slide; - every VM mapping; - each subcache by file suffix, checked by UUID. A missing or mismatched subcache makes coverage partial; - the complete image list. For each requested install path it reads that image's load commands through the cache mappings with the existing Mach-O reader. trace_dylib_resolution accepts an optional shared_cache. Absolute candidates found in a cache that serves the process's CPU family resolve with the new outcome and status `shared-cache`. That status also takes precedence over later in-bundle fallbacks such as a back-deployed libswiftCore. The result names the cache UUID and OS version it used. verify:dyld-shared-cache checks every image in the host cache (3,649 on macOS 26.6), with dependencies and their re-export, weak, upward and delay-init attributes, against `dyld_info -all_dyld_cache -linked_dylibs`. It also checks CLI/MCP parity, and that each shared-cache edge in the fixture trace is a library dyld actually loads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a0ec3e2016
commit
ade11fa02d
@@ -38,7 +38,7 @@ export async function artifactCliEvidence(command, target, arguments_ = []) {
|
||||
return evidence;
|
||||
}
|
||||
|
||||
/** Run a real stdio MCP subprocess, owning and closing its transport. */
|
||||
/** Run a real stdio MCP subprocess, owning and closing its transport; open `target` unless it is null. */
|
||||
export async function withArtifactMcp(target, verify) {
|
||||
const transport = new StdioClientTransport({
|
||||
command: process.execPath,
|
||||
@@ -49,11 +49,14 @@ export async function withArtifactMcp(target, verify) {
|
||||
const client = new Client({ name: "artifact-real-e2e", version: "1" });
|
||||
try {
|
||||
await client.connect(transport);
|
||||
const opened = await client.callTool({
|
||||
name: "open_binary",
|
||||
arguments: { path: target },
|
||||
});
|
||||
assert.notEqual(opened.isError, true, JSON.stringify(opened));
|
||||
// Target-free tools pass `null` and need no open session.
|
||||
if (target !== null) {
|
||||
const opened = await client.callTool({
|
||||
name: "open_binary",
|
||||
arguments: { path: target },
|
||||
});
|
||||
assert.notEqual(opened.isError, true, JSON.stringify(opened));
|
||||
}
|
||||
await verify(client);
|
||||
} finally {
|
||||
await client.close();
|
||||
|
||||
@@ -211,7 +211,8 @@ export const providerCatalog = (sources) => {
|
||||
contracts: sources.applicationContracts.APPLICATION_TOOL_CONTRACTS.filter(
|
||||
({ name }) =>
|
||||
name !== "project_android_application_graph" &&
|
||||
name !== "project_apple_application_graph",
|
||||
name !== "project_apple_application_graph" &&
|
||||
name !== "inspect_dyld_shared_cache",
|
||||
),
|
||||
},
|
||||
{
|
||||
@@ -223,7 +224,9 @@ export const providerCatalog = (sources) => {
|
||||
{
|
||||
identity: sources.artifactProviders.APPLE_APPLICATION_PROVIDER,
|
||||
contracts: sources.applicationContracts.APPLICATION_TOOL_CONTRACTS.filter(
|
||||
({ name }) => name === "project_apple_application_graph",
|
||||
({ name }) =>
|
||||
name === "project_apple_application_graph" ||
|
||||
name === "inspect_dyld_shared_cache",
|
||||
),
|
||||
},
|
||||
{
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { access, mkdtemp, realpath, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { DyldSharedCache } from "../dist/artifacts/DyldSharedCacheReader.js";
|
||||
import {
|
||||
artifactCli,
|
||||
artifactMcpResult,
|
||||
withArtifactMcp,
|
||||
} from "./lib/artifact-e2e.mjs";
|
||||
import {
|
||||
buildMacosBundleFixture,
|
||||
preflightMacosBundleFixture,
|
||||
} from "./lib/macos-bundle-fixture.mjs";
|
||||
|
||||
const exec = promisify(execFile);
|
||||
|
||||
await preflightMacosBundleFixture();
|
||||
|
||||
/** The cache this host's dyld uses for native processes. */
|
||||
const hostCache = async () => {
|
||||
const names =
|
||||
process.arch === "arm64"
|
||||
? ["dyld_shared_cache_arm64e"]
|
||||
: ["dyld_shared_cache_x86_64h", "dyld_shared_cache_x86_64"];
|
||||
for (const directory of [
|
||||
"/System/Volumes/Preboot/Cryptexes/OS/System/Library/dyld",
|
||||
"/System/Library/dyld",
|
||||
])
|
||||
for (const name of names)
|
||||
try {
|
||||
await access(join(directory, name));
|
||||
return join(directory, name);
|
||||
} catch {
|
||||
// Try the next documented location.
|
||||
}
|
||||
throw new Error("No dyld shared cache was found in the documented locations");
|
||||
};
|
||||
|
||||
/** `dyld_info -all_dyld_cache -linked_dylibs`: path -> "attributes|load path" lines. */
|
||||
const dyldInfoImages = async () => {
|
||||
const { stdout } = await exec(
|
||||
"/usr/bin/xcrun",
|
||||
["dyld_info", "-all_dyld_cache", "-linked_dylibs"],
|
||||
{ maxBuffer: 256 * 1024 * 1024 },
|
||||
);
|
||||
const images = new Map();
|
||||
let current;
|
||||
for (const line of stdout.split("\n")) {
|
||||
const head = /^(\/.+) \[(\S+)\]:$/u.exec(line);
|
||||
if (head !== null) {
|
||||
current = [];
|
||||
images.set(head[1], current);
|
||||
continue;
|
||||
}
|
||||
const dependency = /^\s{8}(.*?)\s*(\/\S.*)$/u.exec(line);
|
||||
if (dependency !== null && current !== undefined)
|
||||
current.push(
|
||||
`${dependency[1].split(/\s+/u).filter(Boolean).sort().join(",")}|${dependency[2]}`,
|
||||
);
|
||||
}
|
||||
return images;
|
||||
};
|
||||
|
||||
const attributes = (dependency) =>
|
||||
[
|
||||
dependency.reexport ? "re-export" : null,
|
||||
dependency.weak ? "weak-link" : null,
|
||||
dependency.upward ? "upward" : null,
|
||||
dependency.delayed_init ? "delay-init" : null,
|
||||
]
|
||||
.filter((value) => value !== null)
|
||||
.sort()
|
||||
.join(",");
|
||||
|
||||
const cachePath = await hostCache();
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-dyld-cache-"));
|
||||
try {
|
||||
const expected = await dyldInfoImages();
|
||||
const cache = await DyldSharedCache.open(cachePath);
|
||||
let compared = 0;
|
||||
try {
|
||||
assert.deepEqual(
|
||||
cache.images.map(({ path }) => path).sort(),
|
||||
[...expected.keys()].sort(),
|
||||
"image list differs from dyld_info",
|
||||
);
|
||||
for (const [path, dependencies] of expected) {
|
||||
const read = await cache.imageFacts(path);
|
||||
assert.equal(read?.facts.status, "parsed", path);
|
||||
assert.deepEqual(
|
||||
read.facts.slices[0].dependencies.map(
|
||||
(dependency) =>
|
||||
`${attributes(dependency)}|${dependency.install_name}`,
|
||||
),
|
||||
dependencies,
|
||||
`${path} dependencies differ from dyld_info`,
|
||||
);
|
||||
compared += 1;
|
||||
}
|
||||
} finally {
|
||||
await cache.close();
|
||||
}
|
||||
|
||||
const inspected = await artifactCli("inspect-dyld-shared-cache", cachePath, [
|
||||
"--image",
|
||||
"/usr/lib/libSystem.B.dylib",
|
||||
]);
|
||||
assert.equal(inspected.images_total, expected.size);
|
||||
assert.equal(inspected.coverage.status, "complete");
|
||||
assert.equal(inspected.inspected_images[0].status, "parsed");
|
||||
await withArtifactMcp(null, async (client) => {
|
||||
const viaMcp = await artifactMcpResult(
|
||||
client,
|
||||
"inspect_dyld_shared_cache",
|
||||
{
|
||||
cache_path: cachePath,
|
||||
images: ["/usr/lib/libSystem.B.dylib"],
|
||||
},
|
||||
);
|
||||
assert.deepEqual(viaMcp, inspected);
|
||||
});
|
||||
|
||||
const { app } = await buildMacosBundleFixture(root);
|
||||
const trace = await artifactCli("trace-dylib-resolution", app, [
|
||||
"--shared-cache",
|
||||
cachePath,
|
||||
]);
|
||||
assert.equal(trace.shared_cache.uuid, inspected.uuid);
|
||||
const main = "Contents/MacOS/MacFixture";
|
||||
const cached = trace.edges.filter(
|
||||
({ root: edgeRoot, resolution }) =>
|
||||
edgeRoot === main && resolution.status === "shared-cache",
|
||||
);
|
||||
assert.ok(
|
||||
cached.some(
|
||||
({ install_name: name }) => name === "/usr/lib/libSystem.B.dylib",
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
trace.edges.every(({ install_name: name, resolution }) =>
|
||||
name.startsWith("/usr/lib/") || name.startsWith("/System/")
|
||||
? resolution.status === "shared-cache"
|
||||
: true,
|
||||
),
|
||||
"a system install path was not found in the host cache",
|
||||
);
|
||||
const { stderr } = await exec(join(app, main), [], {
|
||||
env: { PATH: "/usr/bin:/bin", DYLD_PRINT_LIBRARIES: "1" },
|
||||
});
|
||||
const loaded = new Set(
|
||||
stderr
|
||||
.split("\n")
|
||||
.map((line) => /^dyld\[\d+\]: <[0-9A-F-]+> (.+)$/u.exec(line)?.[1])
|
||||
.filter((path) => path !== undefined),
|
||||
);
|
||||
for (const { resolution } of cached)
|
||||
assert.ok(
|
||||
loaded.has(resolution.image),
|
||||
`${resolution.image} was not loaded by dyld`,
|
||||
);
|
||||
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({
|
||||
ok: true,
|
||||
mocked: false,
|
||||
cli: true,
|
||||
stdio_mcp: true,
|
||||
cache: {
|
||||
architecture: inspected.architecture,
|
||||
os_version: inspected.os_version,
|
||||
images: inspected.images_total,
|
||||
},
|
||||
images_matching_dyld_info: compared,
|
||||
trace_shared_cache_edges: cached.length,
|
||||
dyld_loaded_every_cached_edge: true,
|
||||
host: await realpath(cachePath),
|
||||
})}\n`,
|
||||
);
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
Reference in New Issue
Block a user