feat(artifacts): trace Mach-O dylib resolution in bundles
Add trace_dylib_resolution (CLI trace-dylib-resolution) to the artifact family. For the active Mach-O, or every executable in the active .app (the main app, XPC services, extensions, login items, helpers), it reports which file each dylib load reaches and how. A pure TypeScript reader decodes FAT and Mach-O load commands: LC_LOAD/WEAK/REEXPORT/LAZY/UPWARD_DYLIB, including the newer dylib_use_command encoding with weak, re-export, upward and delayed-init flags, plus LC_ID_DYLIB, LC_RPATH, LC_DYLD_ENVIRONMENT and code-signature presence. Malformed tables are reported with the failing command instead of being guessed. Resolution follows dyld: - @executable_path comes from each process root; - @loader_path comes from the loading image, or from the owner of the LC_RPATH entry being expanded; - @rpath searches the loader's rpaths first, then up the load chain; - dependencies load dependents-first in load-command order, once per process, and a matching install name reuses an already loaded image; - symlinks are followed inside the analyzed root, and escapes stop. Every candidate path is reported with its outcome. Each edge resolves to resolved, conditional (an outside-root candidate such as /usr/lib/swift precedes), unresolved, or undetermined. Absolute and shared-cache system paths are never checked against the host. Derived findings cover unresolved required or weak loads, absent earlier @rpath candidates (with library validation left unknown), and LC_DYLD_ENVIRONMENT. verify:macos-bundle now also: - checks every traced image against otool -l; - asserts each resolution status on a clang-built fixture that adds a missing Overrides rpath, an absolute rpath, and a delay-init library; - compares the predicted load order of two process roots with what dyld loads under DYLD_PRINT_LIBRARIES. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
55249704c5
commit
11b7f918c2
@@ -0,0 +1,125 @@
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { ArtifactProvider } from "../../../src/artifacts/ArtifactProvider.js";
|
||||
import {
|
||||
FILE_TYPE,
|
||||
LC,
|
||||
dylibCommand,
|
||||
machoImage,
|
||||
rpathCommand,
|
||||
} from "../../../src/artifacts/MachoImage.fixture.js";
|
||||
import { dylibResolutionResultSchema } from "../../../src/domain/dylibResolution.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
const withClient = async (
|
||||
verify: (client: Client) => Promise<void>,
|
||||
): Promise<void> => {
|
||||
const session = createTestBinarySession(new ArtifactProvider());
|
||||
const server = createServer(session, session);
|
||||
const client = new Client({ name: "dylib-mcp-test", version: "1" });
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
try {
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
await verify(client);
|
||||
} finally {
|
||||
await client.close();
|
||||
await server.close();
|
||||
}
|
||||
};
|
||||
|
||||
const structuredResult = (value: unknown): unknown =>
|
||||
z.object({ result: z.unknown() }).parse(value).result;
|
||||
|
||||
it("traces dylib resolution for an opened app bundle and rejects a changed target", async () => {
|
||||
const app = join(
|
||||
await createTestTempDirectory("rea-dylib-mcp-"),
|
||||
"Fixture.app",
|
||||
);
|
||||
await mkdir(join(app, "Contents/MacOS"), { recursive: true });
|
||||
await mkdir(join(app, "Contents/Frameworks"), { recursive: true });
|
||||
await writeFile(
|
||||
join(app, "Contents/Info.plist"),
|
||||
"<plist><dict><key>CFBundleExecutable</key><string>App</string></dict></plist>",
|
||||
);
|
||||
const executable = join(app, "Contents/MacOS/App");
|
||||
await writeFile(
|
||||
executable,
|
||||
machoImage({
|
||||
commands: [
|
||||
rpathCommand("@executable_path/../Frameworks"),
|
||||
dylibCommand(LC.LOAD_DYLIB, "@rpath/libcore.dylib"),
|
||||
],
|
||||
}),
|
||||
);
|
||||
await writeFile(
|
||||
join(app, "Contents/Frameworks/libcore.dylib"),
|
||||
machoImage({ fileType: FILE_TYPE.dylib }),
|
||||
);
|
||||
await withClient(async (client) => {
|
||||
const opened = await client.callTool({
|
||||
name: "open_binary",
|
||||
arguments: { path: app },
|
||||
});
|
||||
expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
|
||||
const called = await client.callTool({
|
||||
name: "trace_dylib_resolution",
|
||||
arguments: {},
|
||||
});
|
||||
expect(called.isError, JSON.stringify(called.structuredContent)).not.toBe(
|
||||
true,
|
||||
);
|
||||
const trace = dylibResolutionResultSchema.parse(
|
||||
structuredResult(called.structuredContent),
|
||||
);
|
||||
expect(trace.edges).toEqual([
|
||||
expect.objectContaining({
|
||||
install_name: "@rpath/libcore.dylib",
|
||||
resolution: {
|
||||
status: "resolved",
|
||||
image: "Contents/Frameworks/libcore.dylib",
|
||||
},
|
||||
}),
|
||||
]);
|
||||
const escaping = await client.callTool({
|
||||
name: "trace_dylib_resolution",
|
||||
arguments: { roots: ["Contents/../../outside"] },
|
||||
});
|
||||
expect(escaping.isError).toBe(true);
|
||||
|
||||
await writeFile(executable, machoImage({}));
|
||||
const changed = await client.callTool({
|
||||
name: "trace_dylib_resolution",
|
||||
arguments: {},
|
||||
});
|
||||
expect(changed.isError).toBe(true);
|
||||
expect(JSON.stringify(changed.structuredContent)).toContain("integrity");
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects targets that are not Mach-O", async () => {
|
||||
const directory = await createTestTempDirectory("rea-dylib-mcp-plist-");
|
||||
const path = join(directory, "Info.plist");
|
||||
await writeFile(path, "<plist><dict/></plist>");
|
||||
await withClient(async (client) => {
|
||||
const opened = await client.callTool({
|
||||
name: "open_binary",
|
||||
arguments: { path },
|
||||
});
|
||||
expect(opened.isError).not.toBe(true);
|
||||
const called = await client.callTool({
|
||||
name: "trace_dylib_resolution",
|
||||
arguments: {},
|
||||
});
|
||||
expect(called.isError).toBe(true);
|
||||
expect(JSON.stringify(called.structuredContent)).toContain("unavailable");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user