From 6a41a29963f4623d399c66b916b95711207bbc8c Mon Sep 17 00:00:00 2001 From: clyde Date: Sun, 11 Oct 2026 01:00:27 +0530 Subject: [PATCH] fix(apple): preserve directory requirements in dylib paths (#1623) --- src/domain/apple/dyldPaths.ts | 9 ++- src/domain/apple/dylibResolution.test.ts | 43 ++++++++++++++ src/domain/apple/dylibResolution.ts | 3 +- tests/boundary/cli/dylibResolutionCli.test.ts | 59 +++++++++++++++++++ 4 files changed, 110 insertions(+), 4 deletions(-) diff --git a/src/domain/apple/dyldPaths.ts b/src/domain/apple/dyldPaths.ts index 765bb15d1..e853b9a8a 100644 --- a/src/domain/apple/dyldPaths.ts +++ b/src/domain/apple/dyldPaths.ts @@ -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("/"); diff --git a/src/domain/apple/dylibResolution.test.ts b/src/domain/apple/dylibResolution.test.ts index 1b262970a..2d63ce393 100644 --- a/src/domain/apple/dylibResolution.test.ts +++ b/src/domain/apple/dylibResolution.test.ts @@ -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( diff --git a/src/domain/apple/dylibResolution.ts b/src/domain/apple/dylibResolution.ts index 86dc4902e..871cb7601 100644 --- a/src/domain/apple/dylibResolution.ts +++ b/src/domain/apple/dylibResolution.ts @@ -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 }; }), diff --git a/tests/boundary/cli/dylibResolutionCli.test.ts b/tests/boundary/cli/dylibResolutionCli.test.ts index c92d265b2..1f6449993 100644 --- a/tests/boundary/cli/dylibResolutionCli.test.ts +++ b/tests/boundary/cli/dylibResolutionCli.test.ts @@ -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", () => {