fix(apple): validate dylib candidates and preserve search uncertainty (#1100)

* fix(apple): validate dylib candidates and preserve search uncertainty

* fix(apple): scope embedded dyld uncertainty by search phase

* fix(apple): scope dyld prefixes to eligible paths and platforms

* fix(apple): ignore embedded process settings on library roots
This commit is contained in:
morluto
2026-10-09 01:43:52 +08:00
committed by GitHub
parent 3b2ec69c54
commit 7914e36ab9
9 changed files with 819 additions and 20 deletions
@@ -8,10 +8,12 @@ import {
LC,
buildVersionCommand,
dylibCommand,
dyldEnvironmentCommand,
machoImage,
rpathCommand,
} from "../../../src/artifacts/apple/MachoImage.fixture.js";
import { dylibResolutionResultSchema } from "../../../src/domain/apple/dylibResolution.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
@@ -254,3 +256,113 @@ describe.skipIf(process.getuid?.() === 0)(
);
},
);
cliTest(
"reports embedded path overrides as conditional without changing diagnostic-only resolution",
async ({ cli }) => {
for (const [setting, expected] of [
["DYLD_LIBRARY_PATH=/external", "conditional"],
["DYLD_PRINT_LIBRARIES=1", "resolved"],
["DYLD_FALLBACK_LIBRARY_PATH=/external", "resolved"],
["DYLD_FRAMEWORK_PATH=/external", "resolved"],
["DYLD_ROOT_PATH=/external", "resolved"],
["DYLD_OVERLAY_PATH=/external", "resolved"],
] as const) {
const app = await fixtureApp();
await writeFiles(app, {
"Contents/MacOS/App": machoImage({
commands: [
dyldEnvironmentCommand(setting),
buildVersionCommand(1),
rpathCommand("@executable_path/../Absent"),
rpathCommand("@executable_path/../Frameworks"),
dylibCommand(LC.LOAD_DYLIB, "@rpath/libcore.dylib"),
],
}),
});
const result = await cli.run({
arguments: [
"trace-dylib-resolution",
app,
"--root",
"Contents/MacOS/App",
"--json",
],
environment: ENVIRONMENT,
});
expect(result.exitCode, JSON.stringify(result.json)).toBe(0);
const trace = dylibResolutionResultSchema.parse(
parseEvidence(result.json).normalized_result,
);
expect(trace.edges[0]?.resolution.status).toBe(expected);
expect(trace.coverage.status).toBe(
expected === "conditional" ? "partial" : "complete",
);
expect(
trace.images.find(({ path }) => path === "Contents/MacOS/App")
?.slices[0]?.dyld_environment,
).toEqual([setting]);
if (expected === "conditional")
expect(trace.findings[0]?.explanation).toContain(
"does not establish which image dyld will load",
);
}
},
);
cliTest(
"retains definitive load findings for an explicitly selected app library root",
async ({ cli }) => {
const app = await fixtureApp();
const settings = [
"DYLD_LIBRARY_PATH=/external",
"DYLD_INSERT_LIBRARIES=/external/injected.dylib",
];
await writeFiles(app, {
"Contents/Frameworks/libcore.dylib": machoImage({
fileType: FILE_TYPE.dylib,
commands: [
buildVersionCommand(1),
...settings.map(dyldEnvironmentCommand),
dylibCommand(LC.LOAD_DYLIB, "@loader_path/child.dylib"),
dylibCommand(LC.LOAD_DYLIB, "@loader_path/missing.dylib"),
],
}),
"Contents/Frameworks/child.dylib": machoImage({
fileType: FILE_TYPE.dylib,
commands: [buildVersionCommand(1)],
}),
});
const result = await cli.run({
arguments: [
"trace-dylib-resolution",
app,
"--root",
"Contents/Frameworks/libcore.dylib",
"--json",
],
environment: ENVIRONMENT,
});
expect(result.exitCode, JSON.stringify(result.json)).toBe(0);
const trace = dylibResolutionResultSchema.parse(
parseEvidence(result.json).normalized_result,
);
expect(trace.edges.map(({ resolution }) => resolution.status)).toEqual([
"resolved",
"unresolved",
]);
expect(trace.coverage.status).toBe("complete");
expect(trace.findings.map(({ kind }) => kind)).toContain(
"required-load-unresolved",
);
expect(
trace.images.find(
({ path }) => path === "Contents/Frameworks/libcore.dylib",
)?.slices[0]?.dyld_environment,
).toEqual(settings);
expect(
trace.findings.find(({ kind }) => kind === "dyld-environment-present")
?.explanation,
).toContain("entries in this non-executable root are observations");
},
);
@@ -11,6 +11,7 @@ import {
LC,
buildVersionCommand,
dylibCommand,
dyldEnvironmentCommand,
machoImage,
rpathCommand,
} from "../../../src/artifacts/apple/MachoImage.fixture.js";
@@ -204,3 +205,121 @@ it.skipIf(process.getuid?.() === 0)(
});
},
);
it.each([
"DYLD_LIBRARY_PATH=/external",
"DYLD_PRINT_RPATHS=1",
"DYLD_FALLBACK_LIBRARY_PATH=/external",
"DYLD_FRAMEWORK_PATH=/external",
"DYLD_ROOT_PATH=/external",
"DYLD_OVERLAY_PATH=/external",
])("preserves %s and its resolution semantics through MCP", async (setting) => {
const root = await createTestTempDirectory("rea-dylib-environment-mcp-");
const program = join(root, "program");
await writeFile(
program,
machoImage({
commands: [
dyldEnvironmentCommand(setting),
buildVersionCommand(1),
dylibCommand(LC.LOAD_DYLIB, "@loader_path/child.dylib"),
],
}),
);
await writeFile(
join(root, "child.dylib"),
machoImage({
fileType: FILE_TYPE.dylib,
commands: [buildVersionCommand(1)],
}),
);
await withClient(async (client) => {
const opened = await client.callTool({
name: "open_binary",
arguments: { path: program },
});
expect(opened.isError).not.toBe(true);
const result = await client.callTool({
name: "trace_dylib_resolution",
arguments: {},
});
expect(result.isError, JSON.stringify(result.structuredContent)).not.toBe(
true,
);
const trace = dylibResolutionResultSchema.parse(
structuredResult(result.structuredContent),
);
const overrides = setting.startsWith("DYLD_LIBRARY_PATH=");
expect(trace.edges[0]?.resolution.status).toBe(
overrides ? "conditional" : "resolved",
);
expect(trace.coverage.status).toBe(overrides ? "partial" : "complete");
expect(
trace.images.find(({ path }) => path === "program")?.slices[0]
?.dyld_environment,
).toEqual([setting]);
});
});
it("ignores process settings on an explicitly selected library root through MCP", async () => {
const root = await createTestTempDirectory("rea-dylib-root-mcp-");
const path = join(root, "library.dylib");
const settings = [
"DYLD_LIBRARY_PATH=/external",
"DYLD_INSERT_LIBRARIES=/external/injected.dylib",
];
await writeFile(
path,
machoImage({
fileType: FILE_TYPE.dylib,
commands: [
buildVersionCommand(1),
...settings.map(dyldEnvironmentCommand),
dylibCommand(LC.LOAD_DYLIB, "@loader_path/child.dylib"),
dylibCommand(LC.LOAD_DYLIB, "@loader_path/missing.dylib"),
],
}),
);
await writeFile(
join(root, "child.dylib"),
machoImage({
fileType: FILE_TYPE.dylib,
commands: [buildVersionCommand(1)],
}),
);
await withClient(async (client) => {
const opened = await client.callTool({
name: "open_binary",
arguments: { path },
});
expect(opened.isError, JSON.stringify(opened.structuredContent)).not.toBe(
true,
);
const result = await client.callTool({
name: "trace_dylib_resolution",
arguments: { roots: ["library.dylib"] },
});
expect(result.isError, JSON.stringify(result.structuredContent)).not.toBe(
true,
);
const trace = dylibResolutionResultSchema.parse(
structuredResult(result.structuredContent),
);
expect(trace.edges.map(({ resolution }) => resolution.status)).toEqual([
"resolved",
"unresolved",
]);
expect(trace.coverage.status).toBe("complete");
expect(trace.findings.map(({ kind }) => kind)).toContain(
"required-load-unresolved",
);
expect(
trace.images.find(({ path }) => path === "library.dylib")?.slices[0]
?.dyld_environment,
).toEqual(settings);
expect(
trace.findings.find(({ kind }) => kind === "dyld-environment-present")
?.explanation,
).toContain("entries in this non-executable root are observations");
});
});