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 };
}),