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
+392 -3
View File
@@ -160,7 +160,7 @@ describe("dyld path expansion", () => {
}),
[plugin]: parsed(
slice({
file_type: "bundle",
file_type: "dylib",
dependencies: [dependency("@rpath/libshared.dylib")],
}),
),
@@ -316,13 +316,15 @@ describe("dyld resolution outcomes: deriving path and weak-load outcomes", () =>
trace.findings.map(({ kind, edge_index: index }) => [kind, index]),
).toEqual([
["earlier-rpath-candidate-absent", 0],
["weak-load-unresolved", 1],
["required-load-unresolved", 2],
["dyld-environment-present", null],
]);
expect(trace.findings[0]?.explanation).toContain(
"Contents/MacOS/../Overrides/libfound.dylib",
);
expect(trace.findings[0]?.explanation).toContain(
"Unmodeled environment overrides",
);
expect(trace.coverage.status).toBe("partial");
});
});
@@ -716,3 +718,390 @@ describe("conditional loads", () => {
).toContain(`${VENDOR} loads only conditionally; if it loads,`);
});
});
it("propagates embedded search-path uncertainty through found, missing, lazy and descendant edges", async () => {
const child = "Contents/MacOS/child.dylib";
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
dyld_environment: ["DYLD_LIBRARY_PATH=/tmp"],
dependencies: [
dependency("@executable_path/child.dylib"),
dependency("@executable_path/missing.dylib"),
dependency("@executable_path/lazy.dylib", {
command: "LC_LAZY_LOAD_DYLIB",
}),
],
}),
[child]: parsed(
slice({ dependencies: [dependency("@loader_path/gone.dylib")] }),
),
}),
{ roots: [MAIN] },
);
expect(trace.edges.map(({ resolution }) => resolution.status)).toEqual([
"conditional",
"undetermined",
"undetermined",
"undetermined",
]);
expect(trace.edges.at(-1)?.loader_conditional).toBe(true);
expect(trace.findings.map(({ kind }) => kind)).toEqual([
"dyld-environment-present",
]);
});
it.each([
"DYLD_PRINT_LIBRARIES=1",
"DYLD_PRINT_RPATHS=1",
"DYLD_LIBRARY_PATH_LOG=/tmp",
"DYLD_LIBRARY_PATH",
"DYLD_IMAGE_SUFFIX=",
"DYLD_INSERT_LIBRARIES=",
])(
"retains %s as an observation without adding path uncertainty",
async (setting) => {
const child = "Contents/MacOS/child.dylib";
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
dyld_environment: [setting],
dependencies: [dependency("@executable_path/child.dylib")],
}),
[child]: parsed(slice()),
}),
{ roots: [MAIN] },
);
expect(trace.edges[0]?.resolution.status).toBe("resolved");
expect(trace.coverage.status).toBe("complete");
expect(
trace.images.find(({ path }) => path === MAIN)?.slices[0]
?.dyld_environment,
).toEqual([setting]);
expect(trace.findings.map(({ kind }) => kind)).toContain(
"dyld-environment-present",
);
},
);
it.each(["execute", "bundle", "other"] as const)(
"does not resolve a %s image as a dylib dependency",
async (file_type) => {
const child = "Contents/MacOS/child.dylib";
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
dependencies: [dependency("@executable_path/child.dylib")],
}),
[child]: parsed(slice({ file_type })),
}),
{ roots: [MAIN] },
);
expect(trace.edges[0]).toMatchObject({
candidates: [{ outcome: "not-loadable" }],
resolution: { status: "unresolved", image: null },
});
expect(trace.edges).toHaveLength(1);
},
);
it.each([
"@loader_path/child.dylib",
"@executable_path/child.dylib",
"@rpath/child.dylib",
])(
"keeps a found %s definitive under library fallback paths",
async (install_name) => {
const child = "Contents/MacOS/child.dylib";
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
dyld_environment: ["DYLD_FALLBACK_LIBRARY_PATH=/fallback"],
rpaths: ["@executable_path"],
dependencies: [dependency(install_name)],
}),
[child]: parsed(
slice({
dependencies: [dependency("@loader_path/grandchild.dylib")],
}),
),
"Contents/MacOS/grandchild.dylib": parsed(slice()),
}),
{ roots: [MAIN] },
);
expect(trace.edges.map(({ resolution }) => resolution.status)).toEqual([
"resolved",
"resolved",
]);
expect(
trace.edges.every(({ loader_conditional }) => !loader_conditional),
).toBe(true);
expect(trace.coverage.status).toBe("complete");
},
);
it.each([
[
"DYLD_FRAMEWORK_PATH=/override",
"@loader_path/child.dylib",
"resolved",
"complete",
],
[
"DYLD_FRAMEWORK_PATH=/override",
"@loader_path/Foo.framework/Libraries/child.dylib",
"resolved",
"complete",
],
[
"DYLD_FRAMEWORK_PATH=/override",
"@loader_path/Foo.framework/Bar",
"resolved",
"complete",
],
[
"DYLD_FRAMEWORK_PATH=/override",
"@loader_path/Foo.framework/Foo",
"conditional",
"partial",
],
[
"DYLD_FRAMEWORK_PATH=/override",
"@loader_path/Foo.framework/Versions/A/Foo",
"conditional",
"partial",
],
[
"DYLD_LIBRARY_PATH=/override",
"@loader_path/Foo.framework/Foo",
"resolved",
"complete",
],
[
"DYLD_LIBRARY_PATH=/override",
"@loader_path/Foo.framework/Libraries/child.dylib",
"conditional",
"partial",
],
[
"DYLD_FALLBACK_FRAMEWORK_PATH=/fallback",
"@loader_path/Foo.framework/Foo",
"resolved",
"complete",
],
[
"DYLD_FALLBACK_FRAMEWORK_PATH=/fallback",
"@loader_path/child.dylib",
"resolved",
"complete",
],
] as const)(
"scopes %s to the image kind and reached search phase of %s",
async (setting, install_name, status, coverage) => {
const child = install_name.replace("@loader_path", "Contents/MacOS");
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
dyld_environment: [setting],
dependencies: [dependency(install_name)],
}),
[child]: parsed(slice()),
}),
{ roots: [MAIN] },
);
expect(trace.edges[0]?.resolution).toEqual({ status, image: child });
expect(trace.coverage.status).toBe(coverage);
},
);
it.each([
[
"DYLD_FALLBACK_LIBRARY_PATH=/fallback",
"@loader_path/missing.dylib",
"undetermined",
"partial",
],
[
"DYLD_FALLBACK_FRAMEWORK_PATH=/fallback",
"@loader_path/Foo.framework/Foo",
"undetermined",
"partial",
],
[
"DYLD_FALLBACK_FRAMEWORK_PATH=/fallback",
"@loader_path/missing.dylib",
"unresolved",
"complete",
],
[
"DYLD_FALLBACK_LIBRARY_PATH=/fallback",
"@loader_path/Foo.framework/Foo",
"unresolved",
"complete",
],
] as const)(
"retains the applicable fallback uncertainty for %s and %s",
async (setting, install_name, status, coverage) => {
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
dyld_environment: [setting],
dependencies: [dependency(install_name)],
}),
}),
{ roots: [MAIN] },
);
expect(trace.edges[0]?.resolution).toEqual({ status, image: null });
expect(trace.coverage.status).toBe(coverage);
},
);
it.each([
["DYLD_ROOT_PATH=/root", [1], "@loader_path/child.dylib"],
["DYLD_ROOT_PATH=/root", [7], "@loader_path/child.dylib"],
["DYLD_ROOT_PATH=/root", [8], "@executable_path/child.dylib"],
["DYLD_ROOT_PATH=/root", [9], "@rpath/child.dylib"],
["DYLD_ROOT_PATH=/root", [12], "@rpath/child.dylib"],
["DYLD_OVERLAY_PATH=/overlay", [1], "@rpath/child.dylib"],
["DYLD_OVERLAY_PATH=/overlay", [7], "@loader_path/child.dylib"],
["DYLD_ROOT_PATH=", [7], "@loader_path/child.dylib"],
["DYLD_OVERLAY_PATH=", [1], "@loader_path/child.dylib"],
] as const)(
"limits prefix setting %s on platforms %j for %s",
async (setting, platforms, install_name) => {
const child = "Contents/MacOS/child.dylib";
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
platforms: [...platforms],
dyld_environment: [setting],
rpaths: ["@executable_path"],
dependencies: [dependency(install_name)],
}),
[child]: parsed(slice({ platforms: [...platforms] })),
}),
{ roots: [MAIN] },
);
expect(trace.edges[0]?.resolution.status).toBe("resolved");
expect(trace.coverage.status).toBe("complete");
expect(
trace.images.find(({ path }) => path === MAIN)?.slices[0]
?.dyld_environment,
).toEqual([setting]);
},
);
it.each([
["DYLD_ROOT_PATH=/root", [1], "complete"],
["DYLD_ROOT_PATH=/root", [2], "complete"],
["DYLD_ROOT_PATH=/root", [6], "complete"],
["DYLD_ROOT_PATH=/root", [7], "partial"],
["DYLD_ROOT_PATH=/root", [8], "partial"],
["DYLD_ROOT_PATH=/root", [9], "partial"],
["DYLD_ROOT_PATH=/root", [12], "partial"],
["DYLD_ROOT_PATH=/root", [], "partial"],
["DYLD_ROOT_PATH=/root", [99], "partial"],
["DYLD_OVERLAY_PATH=/overlay", [1], "partial"],
] as const)(
"retains only possible absolute-path prefix uncertainty for %s on %j",
async (setting, platforms, coverage) => {
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
platforms: [...platforms],
dyld_environment: [setting],
dependencies: [dependency("/usr/lib/child.dylib")],
}),
}),
{ roots: [MAIN] },
);
expect(trace.edges[0]?.resolution.status).toBe("undetermined");
expect(trace.coverage.status).toBe(coverage);
},
);
it.each(["dylib", "bundle", "other"] as const)(
"ignores embedded process settings from a %s root while retaining load findings",
async (file_type) => {
const root = "Contents/MacOS/root";
const child = "Contents/MacOS/child.dylib";
const settings = [
"DYLD_LIBRARY_PATH=/external",
"DYLD_INSERT_LIBRARIES=/external/injected.dylib",
];
const trace = await traceDylibLoading(
memoryView({
[root]: parsed(
slice({
file_type,
dyld_environment: settings,
dependencies: [
dependency("@loader_path/child.dylib"),
dependency("@loader_path/missing.dylib"),
dependency("@loader_path/lazy.dylib", {
command: "LC_LAZY_LOAD_DYLIB",
}),
],
}),
),
[child]: parsed(
slice({
dependencies: [dependency("@loader_path/descendant.dylib")],
}),
),
}),
{ roots: [root] },
);
expect(trace.edges.map(({ resolution }) => resolution.status)).toEqual([
"resolved",
"unresolved",
"unresolved",
"unresolved",
]);
expect(
trace.edges.every(({ loader_conditional }) => !loader_conditional),
).toBe(true);
expect(trace.coverage.status).toBe("complete");
expect(
trace.findings.filter(({ kind }) => kind === "required-load-unresolved"),
).toHaveLength(2);
expect(
trace.findings.find(({ kind }) => kind === "required-load-unresolved")
?.explanation,
).toContain("load this required dependency");
expect(
trace.findings.filter(({ kind }) => kind === "lazy-load-unresolved"),
).toHaveLength(1);
expect(
trace.images.find(({ path }) => path === root)?.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");
},
);
it("keeps inserted-library uncertainty scoped to the executable's process", async () => {
const library = "Contents/MacOS/library.dylib";
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
dyld_environment: ["DYLD_INSERT_LIBRARIES=/external/injected.dylib"],
}),
[library]: parsed(
slice({
dyld_environment: ["DYLD_INSERT_LIBRARIES=/external/ignored.dylib"],
}),
),
}),
{ roots: [library, MAIN] },
);
expect(trace.coverage.status).toBe("partial");
const limitation = trace.limitations.find((value) =>
value.includes("LC_DYLD_ENVIRONMENT"),
);
expect(limitation).toContain(MAIN);
expect(limitation).not.toContain(library);
});