fix(apple): preserve directory requirements in dylib paths (#1623)

This commit is contained in:
clyde
2026-10-11 03:30:27 +08:00
committed by GitHub
parent c12a532b9c
commit 6a41a29963
4 changed files with 110 additions and 4 deletions
+7 -2
View File
@@ -25,6 +25,7 @@ export const resolveTreePath = async (
let hops = 0;
while (pending.length > 0) {
const segment = pending.shift() ?? "";
if (segment === ".") continue;
if (segment === "..") {
if (resolved.length === 0) return { kind: "escapes" };
resolved.pop();
@@ -50,8 +51,12 @@ export const resolveTreePath = async (
: { kind: "not-file" };
};
const segmentsOf = (path: string): string[] =>
path.split("/").filter((segment) => segment !== "" && segment !== ".");
const segmentsOf = (path: string): string[] => {
const segments = path.split("/").filter((segment) => segment !== "");
// a trailing slash requires a directory, just like a final dot component.
if (path.endsWith("/")) segments.push(".");
return segments;
};
export const directoryOf = (path: string): string => {
const index = path.lastIndexOf("/");
+43
View File
@@ -250,6 +250,49 @@ describe("dyld path expansion", () => {
});
});
it("keeps directory requirements in symlink targets", async () => {
const names = [
"@loader_path/alias-slash",
"@loader_path/alias-dot",
"@loader_path/alias-directory/lib.dylib",
];
const trace = await traceDylibLoading(
memoryView(
{
[MAIN]: executable({
dependencies: names.map((name) => dependency(name)),
}),
"Contents/MacOS/lib.dylib": parsed(slice()),
},
{
"Contents/MacOS/alias-slash": "lib.dylib/",
"Contents/MacOS/alias-dot": "lib.dylib/.",
"Contents/MacOS/alias-directory": "./",
},
),
{ roots: [MAIN] },
);
expect(
trace.edges.map(({ install_name, candidates, resolution }) => ({
install_name,
outcome: candidates[0]?.outcome,
resolution,
})),
).toEqual(
names.map((name, index) => ({
install_name: name,
outcome: index < 2 ? "absent" : "resolved",
resolution:
index < 2
? { status: "unresolved", image: null }
: { status: "resolved", image: "Contents/MacOS/lib.dylib" },
})),
);
expect(
trace.findings.map(({ kind, edge_index }) => [kind, edge_index]),
).toEqual([0, 1].map((index) => ["required-load-unresolved", index]));
});
describe("dyld resolution outcomes: deriving path and weak-load outcomes", () => {
it("keeps paths outside the root undetermined and in-root fallbacks conditional", async () => {
const trace = await traceDylibLoading(
+1 -2
View File
@@ -4,7 +4,6 @@ import {
compatibleSlice,
directoryOf,
expandPrefix,
joinPath,
resolveTreePath,
type Expansion,
} from "./dyldPaths.js";
@@ -275,7 +274,7 @@ const candidateTemplates = (
path:
base.scope === "outside"
? `${base.path.replace(/\/+$/u, "")}/${rest}`
: joinPath(base.path, rest),
: `${base.path}${base.path === "" ? "" : "/"}${rest}`,
};
return { expansion, source: "rpath" as const, rpath, rpathOwner: owner };
}),
@@ -252,6 +252,65 @@ describe("trace-dylib-resolution CLI", () => {
);
});
cliTest(
"retains directory requirements when resolving dylib install names",
async ({ cli }) => {
const directory = await createTestTempDirectory("rea-dylib-path-cli-");
const names = [
"@loader_path/lib/libhelper.dylib/",
"@loader_path/lib/libhelper.dylib/.",
"@rpath/libhelper.dylib/",
"@rpath/libhelper.dylib/.",
"@rpath/",
"@loader_path/lib/./libhelper.dylib",
"@loader_path/lib//libhelper.dylib",
];
await writeFiles(directory, {
tool: machoImage({
commands: [
buildVersionCommand(1),
rpathCommand("@loader_path/lib/libhelper.dylib"),
rpathCommand("@loader_path/lib"),
...names.map((name) => dylibCommand(LC.LOAD_DYLIB, name)),
],
}),
"lib/libhelper.dylib": machoImage({
fileType: FILE_TYPE.dylib,
commands: [buildVersionCommand(1)],
}),
});
const result = await cli.run({
arguments: ["trace-dylib-resolution", join(directory, "tool"), "--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(({ install_name, candidates, resolution }) => ({
install_name,
outcome: candidates[0]?.outcome,
resolution,
})),
).toEqual(
names.map((name, index) => ({
install_name: name,
outcome: index < 5 ? "absent" : "resolved",
resolution:
index < 5
? { status: "unresolved", image: null }
: { status: "resolved", image: "lib/libhelper.dylib" },
})),
);
expect(
trace.findings.map(({ kind, edge_index }) => [kind, edge_index]),
).toEqual(
[0, 1, 2, 3, 4].map((index) => ["required-load-unresolved", index]),
);
},
);
describe.skipIf(process.getuid?.() === 0)(
"trace-dylib-resolution permissions",
() => {