diff --git a/docs/desktop-application-capture.md b/docs/desktop-application-capture.md index d58df27535..83953ca6d2 100644 --- a/docs/desktop-application-capture.md +++ b/docs/desktop-application-capture.md @@ -278,19 +278,52 @@ at all for an archive package, whose export does not use it. REA reads DT_NEEDED from the resolved executable and from each library it finds, and looks each soname up as ld.so does: in the RPATH of the needing image and of each image up its chain of loaders to the executable when the needing image -has no RUNPATH, then in its RUNPATH (with `$ORIGIN` expanded and paths -normalized), and then in the `ldconfig -p` cache entries for the executable's -machine. Each candidate's ELF machine, class and byte order must match the +has no RUNPATH, then in its RUNPATH (with `$ORIGIN` expanded), and then in the +`ldconfig -p` cache entries for the executable's +machine. `$ORIGIN` and `${ORIGIN}` in DT_NEEDED itself are expanded relative +to the object declaring that dependency before full-path lookup and request +deduplication; the same spelling in two libraries can name two different files. +Lookup paths retain directory symlinks and subsequent `..` components until the +filesystem resolves them. The selected leaf name and its physical target are +queried for package ownership, keeping the original alias-qualified lookup +filename separate from canonical identity and trying its owner first. Distinct +ownership spellings participate in request identity even when they select the +same leaf. All observed owners of that leaf are retained; an unowned query +spelling does not create a warning when another spelling identifies its owner. +Distinct required links are retained even when they reach one physical object; +that object's dependencies are walked once through its first load path, which +supplies its own `$ORIGIN`. Link targets retain every ownership-qualified +spelling even when another route reaches the same physical link. Within one +inventory, repeated exact absolute lookup spellings reuse successful path +observations; unresolved probes remain retryable and later inventories observe +the filesystem afresh. Cancellation is checked between lookup metadata reads +and link-target traversals. An in-flight operating-system request is awaited. +Hard I/O failures keep their original error; expected refusals check cancellation +before becoming unresolved. Intermediate file and directory symlink entries +needed to reach a library are retained separately from the physical object. +This includes directory links reached through file-link targets and links to +objects inside the application's folder. The exact +usr-merge links supplied by the Ubuntu 24.04 computer base (`/bin`, `/sbin`, +`/lib`, and amd64 `/lib64` to their `/usr` counterparts) need no extra package; +packages merely registering those shared directories are not inferred as +library dependencies. Other link targets retain their own ownership. External +intermediate links need their own package owner. For the selected leaf, the +physical target is an ownership fallback when the leaf has no known owner. +A library is app-contained only when its required leaf and physical target are +inside the application's folder and its required lookup aliases are captured +there as well. +A combination with another dollar expression that REA does not expand remains +unresolved with a reason, rather than selecting a literal token-named file. +Each candidate's ELF machine, class and byte order must match the needing image before it is selected or its dependencies are walked. A foreign search-path candidate is skipped in favor of later directories or the cache; a cache architecture flag alone does not establish compatibility. If a candidate's identity cannot be read, that lookup stays unresolved, without selecting its package or guessing that a later candidate would load. A compatible identity remains available if a later header or dependency-table check fails; its package -is retained with an incompleteness reason. REA asks `dpkg-query -S` for each -selected library's package. Libraries in the executable's own folder belong to -the application: they are walked but not -listed, and neither is the application's own package. One batched +is retained with an incompleteness reason. REA asks `dpkg-query -S` for the +packages supplying external libraries and their required links. The inventory +lists each unowned path once and omits the application's own package. One batched `apt-cache policy` then keeps only the packages the Ubuntu 24.04 archive or a recorded vendor repository with a verified key offers, in any component its sources entry lists, since the export declares them all; the rest, such as diff --git a/src/desktop/LibraryPackageInventory.test.ts b/src/desktop/LibraryPackageInventory.test.ts index 2c0e4ac37f..4f099c02ba 100644 --- a/src/desktop/LibraryPackageInventory.test.ts +++ b/src/desktop/LibraryPackageInventory.test.ts @@ -1,6 +1,6 @@ import { desktopLibraryPolicy } from "../domain/desktop/libraryPolicy.js"; import { existsSync } from "node:fs"; -import { mkdir, realpath, symlink, writeFile } from "node:fs/promises"; +import { link, mkdir, realpath, symlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; @@ -10,6 +10,102 @@ import { inspectLibraryPackages } from "./LibraryPackageInventory.js"; import { elfImage } from "../domain/desktop/elfImage.fixture.js"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; +it.skipIf(process.platform === "win32").each(["/", "/.", "/.."])( + "preserves a dependency's trailing directory requirement: %s", + async (suffix) => { + const root = await createTestTempDirectory("rea-library-trailing-"); + const bin = join(root, "bin"), + lib = join(root, "lib"); + await Promise.all([mkdir(bin), mkdir(lib)]); + const executable = join(bin, "app"), + invalid = join(lib, "invalid.so"), + valid = join(lib, "valid.so"); + const request = invalid + suffix; + await Promise.all([ + writeFile(executable, elfImage({ needed: [request, valid] })), + writeFile(invalid, elfImage({})), + writeFile(valid, elfImage({})), + ]); + const queried: string[] = []; + const result = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + queried.push(...args.slice(1)); + return { + stdout: args + .slice(1) + .map( + (path) => + `${path === valid ? "valid-owner" : "invalid-owner"}: ${path}`, + ) + .join("\n"), + exitCode: 0, + }; + }, + undefined, + ); + expect(result.library_packages).toEqual(["valid-owner"]); + expect(result.library_packages_reason).toContain(request); + expect(result.missing_libraries).toEqual([]); + expect(queried).toContain(valid); + expect(queried).not.toContain(invalid); + }, +); + +it.skipIf(process.platform === "win32").each([true, false])( + "walks a DSO through its first load path only when aliases share an inode: %s", + async (hardLinked) => { + const root = await createTestTempDirectory("rea-library-hardlink-"); + const bin = join(root, "bin"), + first = join(root, "first"), + second = join(root, "second"); + await Promise.all([mkdir(bin), mkdir(first), mkdir(second)]); + const executable = join(bin, "app"), + a = join(first, "parent.so"), + b = join(second, "parent.so"); + const childA = join(first, "child.so"), + childB = join(second, "child.so"); + const parent = elfImage({ needed: ["$ORIGIN/child.so"] }); + await Promise.all([ + writeFile(executable, elfImage({ needed: [a, b] })), + writeFile(a, parent), + writeFile(childA, elfImage({})), + writeFile(childB, elfImage({})), + ]); + if (hardLinked) await link(a, b); + else await writeFile(b, parent); + const queried: string[] = []; + const result = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + queried.push(...args.slice(1)); + return { + stdout: args + .slice(1) + .map( + (path) => + `${path === childB ? "second-child" : "first-owner"}: ${path}`, + ) + .join("\n"), + exitCode: 0, + }; + }, + undefined, + ); + expect(result.library_packages).toEqual( + hardLinked ? ["first-owner"] : ["first-owner", "second-child"], + ); + expect(result.missing_libraries).toEqual([]); + expect(result.library_packages_reason).toBeUndefined(); + expect(queried).toEqual(expect.arrayContaining([a, b, childA])); + expect(queried.includes(childB)).toBe(!hardLinked); + }, +); + describe.skipIf(process.platform === "win32")( "literal ELF origin paths", () => { diff --git a/src/desktop/LibraryPackageInventory.ts b/src/desktop/LibraryPackageInventory.ts index 268e053d6e..0e8fa6ffdb 100644 --- a/src/desktop/LibraryPackageInventory.ts +++ b/src/desktop/LibraryPackageInventory.ts @@ -3,7 +3,8 @@ import { desktopLibraryPolicy, type DesktopLibraryPolicy, } from "../domain/desktop/libraryPolicy.js"; -import { realpath } from "node:fs/promises"; +import { lstat, readlink, realpath, stat } from "node:fs/promises"; +import { posix } from "node:path"; import type { DesktopPackage } from "../domain/desktop/desktopCapture.js"; import { @@ -17,6 +18,8 @@ import { libraryClosure, libraryPackageNames, parseLinkerCache, + type LibraryLookupAlias, + type LibraryFileProbe, } from "../domain/desktop/libraryPackages.js"; import { ownershipCandidates, @@ -24,13 +27,11 @@ import { parseDpkgOwners, } from "../domain/desktop/linuxPackages.js"; import { err, ok, type Result } from "../domain/result.js"; +import { abortIfNeeded } from "../resources/index.js"; +import { compositeKey } from "../domain/unicodeCodePointOrder.js"; import { withOwnedRegularFile } from "../filesystem/OwnedRegularFile.js"; import { readFileHandleRange } from "../filesystem/readFileHandleRange.js"; -import { - hostReadFailure, - isRefusedRead, - probeHostFile, -} from "./HostFileRead.js"; +import { hostReadFailure, isRefusedRead } from "./HostFileRead.js"; import { succeeded, type QueryRunner } from "./HostQuery.js"; /** The library fields of a Debian package's inventory. */ @@ -45,6 +46,141 @@ export type LibraryPackages = { const QUERY_BATCH = 200; +/** The Ubuntu 24.04 base-files package supplies these exact usr-merge links. */ +const suppliedByComputerBase = ( + path: string, + target: string, + machine: number, +): boolean => + (machine === 62 || machine === 183) && + (path === "/bin" || + path === "/sbin" || + path === "/lib" || + (machine === 62 && path === "/lib64")) && + (target === `usr${path}` || target === `/usr${path}`); + +/** Do not enqueue another lookup read after cancellation; keep failed I/O primary. */ +const lookupRead = async ( + signal: AbortSignal | undefined, + read: () => Promise, +): Promise => { + abortIfNeeded(signal); + const value = await read(); + abortIfNeeded(signal); + return value; +}; + +/** Library lookup distinguishes expected refusals from a failed physical read. */ +const probeLibraryFile = async ( + path: string, + signal: AbortSignal | undefined, +): Promise => { + try { + const facts = await lookupRead(signal, () => stat(path)); + return { kind: "file", isFile: facts.isFile() }; + } catch (cause: unknown) { + const code = (cause as NodeJS.ErrnoException).code; + if (code === "ENOENT" || code === "ENOTDIR") { + abortIfNeeded(signal); + return { kind: "file", isFile: false }; + } + if (isRefusedRead(cause)) { + abortIfNeeded(signal); + return { kind: "unknown", unreachable: code === "EACCES" }; + } + throw hostReadFailure(cause); + } +}; + +/** Remove dot traversal only after the filesystem resolves every preceding link. */ +const ownershipSpelling = async ( + path: string, + signal?: AbortSignal, +): Promise => { + let traversed = ""; + let spelling = "/"; + for (const part of path.split("/")) { + abortIfNeeded(signal); + if (part === "" || part === ".") continue; + traversed += `/${part}`; + spelling = + part === ".." + ? await lookupRead(signal, () => realpath(traversed)) + : posix.join(spelling, part); + } + return spelling; +}; + +/** Intermediate file/directory links needed to resolve a selected library. */ +const requiredAliases = async ( + path: string, + machine: number, + signal?: AbortSignal, +): Promise => { + const aliases = new Map(); + const visitedTargets = new Set(); + const activeLinks = new Set(); + const visit = async ( + lookup: string, + directoryLeaf: boolean, + fileLeaf = false, + ): Promise => { + abortIfNeeded(signal); + const ends: number[] = []; + for ( + let end = lookup.indexOf("/", lookup.startsWith("/") ? 1 : 0); + end !== -1; + end = lookup.indexOf("/", end + 1) + ) + ends.push(end); + ends.push(lookup.length); + for (const end of ends) { + const prefix = lookup.slice(0, end); + if (!(await lookupRead(signal, () => lstat(prefix))).isSymbolicLink()) + continue; + // Canonical parents identify the link entry, not its target. Keep ../ + // in the target spelling until the kernel resolves preceding links. + const alias = posix.join( + await lookupRead(signal, () => realpath(posix.dirname(prefix))), + posix.basename(prefix), + ); + const target = await lookupRead(signal, () => readlink(prefix)); + const directory = end !== lookup.length || directoryLeaf; + if ( + (directory || fileLeaf) && + !suppliedByComputerBase(alias, target, machine) + ) + aliases.set(prefix, { + path: alias, + ownershipPath: await ownershipSpelling(prefix, signal), + }); + const targetLookup = posix.isAbsolute(target) + ? target + : `${posix.dirname(prefix)}/${target}`; + // Target spelling owns the next ownership context. A physical link may + // be reached again through another alias without forming a cycle. + const targetKey = compositeKey([ + targetLookup, + directory ? "directory" : "file", + ]); + if (activeLinks.has(alias) || visitedTargets.has(targetKey)) continue; + visitedTargets.add(targetKey); + activeLinks.add(alias); + try { + // Selected leaves retain the existing DSO-owner fallback. Recursively + // reached links need independent ownership, including their leaf. + abortIfNeeded(signal); + await visit(targetLookup, directory, true); + } finally { + activeLinks.delete(alias); + } + } + }; + await visit(path, false); + abortIfNeeded(signal); + return [...aliases.values()]; +}; + const elfDependencies = async ( path: string, signal: AbortSignal | undefined, @@ -145,6 +281,51 @@ const incompleteReason = ( return reasons.length === 0 ? undefined : reasons.join("; "); }; +interface LibraryOwnershipLookup { + readonly kind: "leaf" | "alias"; + readonly path: string; + readonly candidates: readonly string[]; +} + +/** Reconcile equivalent query spellings without merging links with their targets. */ +const libraryOwnership = ( + located: readonly LibraryOwnershipLookup[], + knownOwners: ReadonlyMap, +) => { + const names: string[] = []; + // Distinct query spellings can address the same leaf entry. Keep every + // observed owner, but do not report that entry unowned if another spelling + // identifies it. Intermediate links remain independent of their targets. + const dispositions = new Map< + string, + { path: string; owned: boolean; unknown: boolean } + >(); + for (const library of located) { + const key = compositeKey([library.kind, library.path]); + let disposition = dispositions.get(key); + if (disposition === undefined) { + disposition = { path: library.path, owned: false, unknown: false }; + dispositions.set(key, disposition); + } + const found = library.candidates + .map((candidate) => knownOwners.get(candidate)) + .find((owner) => owner !== undefined && owner.length > 0); + if (found !== undefined) { + names.push(...found); + disposition.owned = true; + } else if ( + !library.candidates.every((candidate) => knownOwners.has(candidate)) + ) + disposition.unknown = true; + } + const unowned = [...dispositions.values()].filter(({ owned }) => !owned); + const unpackaged = unowned + .filter(({ unknown }) => !unknown) + .map(({ path }) => path); + const unresolved = unowned.some(({ unknown }) => unknown); + return { names, unpackaged, unresolved }; +}; + /** * The packages owning the shared libraries an executable loads, from its * DT_NEEDED closure resolved through RUNPATH, RPATH and `ldconfig -p`, with @@ -175,44 +356,74 @@ export const inspectLibraryPackages = async ( { path: executable, dependencies: root.value }, parseLinkerCache(cache), { + resolvePath: async (path) => { + try { + // Admit the full lookup before deriving its ownership leaf: splitting + // a regular file's trailing slash or dot component would erase ENOTDIR. + const probe = await probeLibraryFile(path, signal); + if (probe.kind !== "file" || !probe.isFile) return undefined; + const resolvedPath = await lookupRead(signal, () => realpath(path)); + const directory = await lookupRead(signal, () => + realpath(posix.dirname(path)), + ); + const selected = posix.join(directory, posix.basename(path)); + const identity = await lookupRead(signal, () => + stat(resolvedPath, { bigint: true }), + ); + return { + path: selected, + ownershipPath: await ownershipSpelling(path, signal), + resolvedPath, + fileIdentity: `${identity.dev}:${identity.ino}`, + requiredAliases: await requiredAliases( + path, + root.value.machine, + signal, + ), + }; + } catch (cause) { + if (isRefusedRead(cause)) { + abortIfNeeded(signal); + return undefined; + } + throw hostReadFailure(cause); + } + }, dependencies: (path, expected) => elfDependencies(path, signal, policy, expected), - isFile: probeHostFile, + isFile: (path) => probeLibraryFile(path, signal), }, policy.libraries, ); - const located = await Promise.all( - closure.libraries.map(async ({ path }) => ({ - path, - candidates: ownershipCandidates([ + const located: LibraryOwnershipLookup[] = [ + ...closure.libraries.map( + ({ path, resolvedPath, ownershipPath }) => ({ + kind: "leaf", path, - ...(await realpath(path).then( - (physical) => [physical], - () => [], - )), - ]), - })), - ); + candidates: ownershipCandidates([ownershipPath, path, resolvedPath]), + }), + ), + // An intermediate lookup alias is its own required entry. Ownership of + // its physical target cannot establish who supplies that link. + ...closure.requiredAliases.map( + ({ path, ownershipPath }) => ({ + kind: "alias", + path, + candidates: ownershipCandidates([ownershipPath, path]), + }), + ), + ]; const owners = await packageOwners( located.flatMap((library) => library.candidates), runQuery, signal, ); const knownOwners = owners.ok ? owners.value : owners.error.owners; - const names: string[] = []; - const unpackaged: string[] = []; - let ownershipUnresolved = false; - for (const library of located) { - const found = library.candidates - .map((candidate) => knownOwners.get(candidate)) - .find((owner) => owner !== undefined && owner.length > 0); - if (found !== undefined) names.push(...found); - else if ( - library.candidates.every((candidate) => knownOwners.has(candidate)) - ) - unpackaged.push(library.path); - else ownershipUnresolved = true; - } + const { + names, + unpackaged, + unresolved: ownershipUnresolved, + } = libraryOwnership(located, knownOwners); const reason = [ incompleteReason(closure, policy), owners.ok || !ownershipUnresolved ? undefined : owners.error.reason, @@ -222,7 +433,7 @@ export const inspectLibraryPackages = async ( return { library_packages: [...libraryPackageNames(names, ownPackage)], missing_libraries: [...closure.missing].sort(), - unpackaged_libraries: unpackaged.sort(), + unpackaged_libraries: [...new Set(unpackaged)].sort(), ...(reason.length === 0 ? {} : { library_packages_reason: reason }), }; }; diff --git a/src/domain/desktop/libraryPackages.test.ts b/src/domain/desktop/libraryPackages.test.ts index 781d1bcd53..a09361ec73 100644 --- a/src/domain/desktop/libraryPackages.test.ts +++ b/src/domain/desktop/libraryPackages.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import { posix } from "node:path"; import { err, ok } from "../result.js"; import { @@ -54,6 +55,13 @@ const host = ( > >, ) => ({ + resolvePath: async (path: string) => ({ + path: posix.normalize(path), + ownershipPath: posix.normalize(path), + resolvedPath: posix.normalize(path), + fileIdentity: posix.normalize(path), + requiredAliases: [], + }), dependencies: async (path: string, expected: ElfLibraryIdentity) => { const image = images[path]; if (typeof image !== "object") @@ -75,7 +83,7 @@ const host = ( }, isFile: async (path: string) => ({ kind: "file" as const, - isFile: path in images, + isFile: posix.normalize(path) in images, }), }); @@ -104,7 +112,7 @@ describe("parseLinkerCache", () => { }); }); -describe("libraryClosure", () => { +describe("libraryClosure denied probes", () => { it("continues past a denied directory to the one the linker can search", async () => { // A folder with denied search is not one the linker loads from, so a later // readable directory holding the soname is the object actually in use. @@ -115,6 +123,7 @@ describe("libraryClosure", () => { }, [], { + ...host({}), dependencies: async (path: string) => path === "/app/bin/app" ? ok(elf(["libcore.so"], [], ["/denied", "/native"])) @@ -136,77 +145,125 @@ describe("libraryClosure", () => { 10, ); expect(closure).toEqual({ - libraries: [{ soname: "libcore.so", path: "/native/libcore.so" }], + requiredAliases: [], + libraries: [ + { + soname: "libcore.so", + path: "/native/libcore.so", + ownershipPath: "/native/libcore.so", + resolvedPath: "/native/libcore.so", + }, + ], missing: [], unreadable: [], truncated: false, }); }); - it("keeps an unprobeable candidate unresolved instead of reporting it missing", async () => { - // Nothing else provides the soname, and the denied folder may hold it, so - // the walk records why it could not tell rather than claiming absence. - const closure = await libraryClosure( - { - path: "/app/bin/app", - dependencies: elf(["libcore.so"], [], ["/denied"]), - }, - [], - { - dependencies: async (path: string) => - path === "/app/bin/app" - ? ok(elf(["libcore.so"], [], ["/denied"])) - : err({ - kind: "unreadable" as const, - identity: null, - reason: "EACCES", - }), - isFile: async () => ({ - kind: "unknown" as const, - unreachable: true, - }), - }, - 10, - ); - expect(closure).toEqual({ - libraries: [], - missing: [], - unreadable: [{ path: "/denied/libcore.so", reason: "EACCES" }], - truncated: false, - }); - }); - - it("keeps a transiently unprobed candidate ahead of the linker cache", async () => { - // A search directory that failed transiently may still be the one the - // linker loads from, so its candidate keeps search order. - const closure = await libraryClosure( - { - path: "/app/bin/app", - dependencies: elf(["libcore.so"], [], ["/volatile"]), - }, - parseLinkerCache(LINKER_CACHE), - { - dependencies: async (path: string) => - path === "/app/bin/app" - ? ok(elf(["libcore.so"], [], ["/volatile"])) - : path === "/volatile/libcore.so" - ? ok(elf([])) + it.each([true, false])( + "keeps an unprobeable candidate unresolved with path resolution %s", + async (resolvable) => { + // Nothing else provides the soname, and the denied folder may hold it, so + // the walk records why it could not tell rather than claiming absence. + const closure = await libraryClosure( + { + path: "/app/bin/app", + dependencies: elf(["libcore.so"], [], ["/denied"]), + }, + [], + { + ...host({}), + resolvePath: async (path: string) => + path === "/app/bin/app" || resolvable + ? host({}).resolvePath(path) + : undefined, + dependencies: async (path: string) => + path === "/app/bin/app" + ? ok(elf(["libcore.so"], [], ["/denied"])) : err({ kind: "unreadable" as const, identity: null, - reason: "missing", + reason: "EACCES", }), - isFile: async (path: string) => - path === "/volatile/libcore.so" - ? { kind: "unknown" as const, unreachable: false } - : { kind: "file" as const, isFile: true }, - }, - 10, - ); - expect(closure).toMatchObject({ - libraries: [{ soname: "libcore.so", path: "/volatile/libcore.so" }], - }); - }); + isFile: async () => ({ + kind: "unknown" as const, + unreachable: true, + }), + }, + 10, + ); + expect(closure).toEqual({ + requiredAliases: [], + libraries: [], + missing: [], + unreadable: [ + { + path: "/denied/libcore.so", + reason: resolvable + ? "EACCES" + : "the library's filesystem path could not be resolved", + }, + ], + truncated: false, + }); + }, + ); +}); + +describe("libraryClosure", () => { + it.each([true, false])( + "keeps a transiently unprobed candidate ahead of the cache with path resolution %s", + async (resolvable) => { + // A search directory that failed transiently may still be the one the + // linker loads from, so its candidate keeps search order. + const closure = await libraryClosure( + { + path: "/app/bin/app", + dependencies: elf(["libcore.so"], [], ["/volatile"]), + }, + parseLinkerCache(LINKER_CACHE), + { + ...host({}), + resolvePath: async (path: string) => + path === "/app/bin/app" || resolvable + ? host({}).resolvePath(path) + : undefined, + dependencies: async (path: string) => + path === "/app/bin/app" + ? ok(elf(["libcore.so"], [], ["/volatile"])) + : path === "/volatile/libcore.so" + ? ok(elf([])) + : err({ + kind: "unreadable" as const, + identity: null, + reason: "missing", + }), + isFile: async (path: string) => + path === "/volatile/libcore.so" + ? { kind: "unknown" as const, unreachable: false } + : { kind: "file" as const, isFile: true }, + }, + 10, + ); + if (resolvable) + expect(closure).toMatchObject({ + libraries: [{ soname: "libcore.so", path: "/volatile/libcore.so" }], + }); + else + expect(closure).toEqual({ + requiredAliases: [], + libraries: [], + missing: [], + unreadable: [ + { + path: "/volatile/libcore.so", + reason: "the library's filesystem path could not be resolved", + }, + ], + truncated: false, + }); + }, + ); it("walks DT_NEEDED through RPATH and the cache for the executable's machine", async () => { const closure = await libraryClosure( @@ -224,13 +281,34 @@ describe("libraryClosure", () => { expect(closure).toEqual({ libraries: [ - { soname: "libnss3.so", path: "/lib/aarch64-linux-gnu/libnss3.so" }, - { soname: "libc.so.6", path: "/lib/aarch64-linux-gnu/libc.so.6" }, - { soname: "ld-linux-aarch64.so.1", path: "/lib/ld-linux-aarch64.so.1" }, - { soname: "libnspr4.so", path: "/lib/aarch64-linux-gnu/libnspr4.so" }, + { + soname: "libnss3.so", + path: "/lib/aarch64-linux-gnu/libnss3.so", + ownershipPath: "/lib/aarch64-linux-gnu/libnss3.so", + resolvedPath: "/lib/aarch64-linux-gnu/libnss3.so", + }, + { + soname: "libc.so.6", + path: "/lib/aarch64-linux-gnu/libc.so.6", + ownershipPath: "/lib/aarch64-linux-gnu/libc.so.6", + resolvedPath: "/lib/aarch64-linux-gnu/libc.so.6", + }, + { + soname: "ld-linux-aarch64.so.1", + path: "/lib/ld-linux-aarch64.so.1", + ownershipPath: "/lib/ld-linux-aarch64.so.1", + resolvedPath: "/lib/ld-linux-aarch64.so.1", + }, + { + soname: "libnspr4.so", + path: "/lib/aarch64-linux-gnu/libnspr4.so", + ownershipPath: "/lib/aarch64-linux-gnu/libnspr4.so", + resolvedPath: "/lib/aarch64-linux-gnu/libnspr4.so", + }, ], missing: ["libsecret-1.so.0"], unreadable: [], + requiredAliases: [], truncated: false, }); }); @@ -257,6 +335,7 @@ describe("libraryClosure", () => { reason: "the file could not be read", }, ], + requiredAliases: [], truncated: true, }); }); @@ -282,11 +361,22 @@ describe("libraryClosure candidate identity", () => { ); expect(closure).toEqual({ libraries: [ - { soname: "libcore.so", path: "/native/libcore.so" }, - { soname: "libhelper.so", path: "/native/libhelper.so" }, + { + soname: "libcore.so", + path: "/native/libcore.so", + ownershipPath: "/native/libcore.so", + resolvedPath: "/native/libcore.so", + }, + { + soname: "libhelper.so", + path: "/native/libhelper.so", + ownershipPath: "/native/libhelper.so", + resolvedPath: "/native/libhelper.so", + }, ], missing: [], unreadable: [], + requiredAliases: [], truncated: false, }); }, @@ -312,9 +402,17 @@ describe("libraryClosure candidate identity", () => { 10, ); expect(closure).toEqual({ - libraries: [{ soname: "libcore.so", path: "/cached/libcore.so" }], + libraries: [ + { + soname: "libcore.so", + path: "/cached/libcore.so", + ownershipPath: "/cached/libcore.so", + resolvedPath: "/cached/libcore.so", + }, + ], missing: [], unreadable: [], + requiredAliases: [], truncated: false, }); }); @@ -337,6 +435,7 @@ describe("libraryClosure unresolved candidates", () => { unreadable: [ { path: "/unreadable/libcore.so", reason: "cannot read header" }, ], + requiredAliases: [], truncated: false, }); }); @@ -355,6 +454,7 @@ describe("libraryClosure unresolved candidates", () => { unreadable: [ { path, reason: expect.stringContaining("incompatible ELF") }, ], + requiredAliases: [], truncated: false, }); }); @@ -377,7 +477,14 @@ describe("libraryClosure unresolved candidates", () => { 10, ); expect(closure).toMatchObject({ - libraries: [{ soname: "libcore.so", path: "/valid/libcore.so" }], + libraries: [ + { + soname: "libcore.so", + path: "/valid/libcore.so", + ownershipPath: "/valid/libcore.so", + resolvedPath: "/valid/libcore.so", + }, + ], missing: [], }); }); @@ -425,6 +532,7 @@ describe("libraryClosure unresolved candidates", () => { libraries: [], missing: ["libcore.so"], unreadable: [], + requiredAliases: [], truncated: false, }); }); @@ -450,10 +558,16 @@ describe("libraryClosure search paths", () => { ); expect(closure).toEqual({ libraries: [ - { soname: "liborigin.so", path: "/opt/$build-$&/lib/liborigin.so" }, + { + soname: "liborigin.so", + path: "/opt/$build-$&/lib/liborigin.so", + ownershipPath: "/opt/$build-$&/lib/liborigin.so", + resolvedPath: "/opt/$build-$&/lib/liborigin.so", + }, ], missing: ["libunsupported.so"], unreadable: [], + requiredAliases: [], truncated: false, }); }); @@ -470,7 +584,12 @@ describe("libraryClosure search paths", () => { ); expect(closure.libraries).toEqual([ - { soname: "libtool.so.1", path: "/usr/lib/libtool.so.1" }, + { + soname: "libtool.so.1", + path: "/usr/lib/libtool.so.1", + ownershipPath: "/usr/lib/libtool.so.1", + resolvedPath: "/usr/lib/libtool.so.1", + }, ]); }); @@ -503,8 +622,18 @@ describe("libraryClosure search paths", () => { expect(inherited).toMatchObject({ libraries: [ - { soname: "libcore.so", path: "/opt/app/lib/libcore.so" }, - { soname: "libhelper.so", path: "/opt/app/lib/libhelper.so" }, + { + soname: "libcore.so", + path: "/opt/app/lib/libcore.so", + ownershipPath: "/opt/app/lib/libcore.so", + resolvedPath: "/opt/app/lib/libcore.so", + }, + { + soname: "libhelper.so", + path: "/opt/app/lib/libhelper.so", + ownershipPath: "/opt/app/lib/libhelper.so", + resolvedPath: "/opt/app/lib/libhelper.so", + }, ], missing: [], }); @@ -556,3 +685,142 @@ describe("libraryPackageNames", () => { ).toEqual(["libc6", "libnspr4", "libnss3"]); }); }); + +describe("absolute dependency lookup reuse", () => { + it("reports truncation before reading a new absolute dependency beyond the limit", async () => { + const admitted = "/native/admitted.so"; + const beyondLimit = "/native/over-budget.so"; + const reads = host({ [admitted]: elf([]) }); + const result = await libraryClosure( + app(elf([admitted, beyondLimit])), + [], + { + ...reads, + resolvePath: async (path) => { + if (path === beyondLimit) throw new Error("metadata read failed"); + return reads.resolvePath(path); + }, + }, + 1, + ); + + expect(result).toMatchObject({ + libraries: [{ soname: admitted }], + unreadable: [], + truncated: true, + }); + }); + + it("resolves each exact successful spelling once across repeated declarations", async () => { + const paths = Array.from( + { length: 8 }, + (_, i) => `/native/lib${String(i)}.so`, + ); + const images = Object.fromEntries(paths.map((path) => [path, elf(paths)])); + const reads = host(images); + const calls: string[] = []; + const result = await libraryClosure( + app(elf(paths)), + [], + { + ...reads, + resolvePath: async (path) => { + calls.push(path); + return reads.resolvePath(path); + }, + }, + paths.length, + ); + expect(result.libraries.map((row) => row.path)).toEqual(paths); + expect(result.missing).toEqual([]); + expect(result.unreadable).toEqual([]); + expect(result.truncated).toBe(false); + expect(calls).toEqual(["/opt/app/bin/app", ...paths]); + }); + + it("retries an unresolved spelling instead of caching a transient failure", async () => { + const reads = host({ + "/native/parent.so": elf(["/alias/shared.so"]), + "/native/shared.so": elf([]), + }); + let attempts = 0; + const result = await libraryClosure( + app(elf(["/native/parent.so", "/alias/shared.so"])), + [], + { + ...reads, + resolvePath: async (path) => { + if (path !== "/alias/shared.so") return reads.resolvePath(path); + if (++attempts === 1) return undefined; + return { + ...(await reads.resolvePath("/native/shared.so")), + ownershipPath: path, + }; + }, + }, + 8, + ); + expect(attempts).toBe(2); + expect(result.libraries.map((row) => row.path)).toEqual([ + "/native/parent.so", + "/native/shared.so", + ]); + expect(result.unreadable).toHaveLength(1); + expect(result.unreadable[0]?.path).toBe("/alias/shared.so"); + }); + it("observes filesystem changes in the next inventory walk", async () => { + const reads = host({ + "/native/first.so": elf([]), + "/native/second.so": elf([]), + }); + let selected = "/native/first.so"; + const walk = () => + libraryClosure( + app(elf(["/alias/current.so"])), + [], + { + ...reads, + resolvePath: async (path) => + path === "/alias/current.so" + ? { ...(await reads.resolvePath(selected)), ownershipPath: path } + : reads.resolvePath(path), + }, + 8, + ); + expect((await walk()).libraries.map((row) => row.path)).toEqual([ + "/native/first.so", + ]); + selected = "/native/second.so"; + expect((await walk()).libraries.map((row) => row.path)).toEqual([ + "/native/second.so", + ]); + }); + it("selects a successful retry when its filesystem spelling stays unchanged", async () => { + const reads = host({ + "/native/parent.so": elf(["/native/shared.so"]), + "/native/shared.so": elf(["/native/child.so"]), + "/native/child.so": elf([]), + }); + let attempts = 0; + const result = await libraryClosure( + app(elf(["/native/parent.so", "/native/shared.so"])), + [], + { + ...reads, + resolvePath: async (path) => { + if (path === "/native/shared.so" && ++attempts === 1) + return undefined; + return reads.resolvePath(path); + }, + }, + 8, + ); + expect(result.libraries.map((row) => row.path)).toEqual([ + "/native/parent.so", + "/native/shared.so", + "/native/child.so", + ]); + expect(result.unreadable).toHaveLength(1); + expect(result.unreadable[0]?.path).toBe("/native/shared.so"); + }); +}); diff --git a/src/domain/desktop/libraryPackages.ts b/src/domain/desktop/libraryPackages.ts index c87596a7e9..eb00792046 100644 --- a/src/domain/desktop/libraryPackages.ts +++ b/src/domain/desktop/libraryPackages.ts @@ -1,6 +1,7 @@ import { posix } from "node:path"; import type { Result } from "../result.js"; +import { compositeKey } from "../unicodeCodePointOrder.js"; import { fromUbuntuNobleArchive } from "./appExportInstall.js"; import { isWithinAny } from "./desktopStateDiff.js"; import type { @@ -28,7 +29,7 @@ export const parseLinkerCache = (output: string): readonly LinkerCacheEntry[] => { soname: groups.soname, flags: (groups.flags ?? "").split(",").map((flag) => flag.trim()), - path: posix.normalize(groups.path), + path: groups.path, }, ]; }); @@ -68,6 +69,10 @@ interface LoadedObject { const ORIGIN_TOKEN = /\$(?:ORIGIN(?![A-Za-z0-9_])|\{ORIGIN\})/gu; +/** Expand only the declaration: dollars in the owner's directory are literal data. */ +const expandOrigin = (owner: string, declaration: string): string => + declaration.replace(ORIGIN_TOKEN, () => posix.dirname(owner)); + /** Absolute directories with `$ORIGIN` expanded to the owner's folder; relative and other dynamic-token paths are left out. */ const expanded = ( owner: string, @@ -76,9 +81,8 @@ const expanded = ( directories // Check tokens before expansion: dollars in the owner's directory are data. .filter((path) => !path.replace(ORIGIN_TOKEN, "").includes("$")) - .map((path) => path.replace(ORIGIN_TOKEN, () => posix.dirname(owner))) - .filter((path) => path.startsWith("/")) - .map((path) => posix.normalize(path)); + .map((path) => expandOrigin(owner, path)) + .filter((path) => path.startsWith("/")); /** * Where ld.so looks for an object's needed libraries before its cache: an @@ -98,38 +102,49 @@ const searchDirectories = (object: LoadedObject): readonly string[] => { ]; }; -/** Search order identifies candidates; existence alone does not select a loaded object. */ +/** Preserve filesystem lookup spelling until the host resolves its target. */ async function* libraryCandidates( - soname: string, + request: string, directories: readonly string[], machine: number, cache: readonly LinkerCacheEntry[], host: LibraryWalkHost, ): AsyncGenerator<{ path: string; unprobed: boolean }> { - if (soname.startsWith("/")) { - yield { path: posix.normalize(soname), unprobed: false }; + if (request.startsWith("/")) { + yield { path: request, unprobed: false }; return; } const unprobed: string[] = []; - if (!soname.includes("/")) + if (!request.includes("/")) for (const directory of directories) { - const candidate = posix.join(directory, soname); + const candidate = `${directory}/${request}`; const probe = await host.isFile(candidate); if (probe.kind === "file") { if (probe.isFile) yield { path: candidate, unprobed: false }; - } - // A folder the linker cannot search is not the object it loads with the - // same credentials. It is only evidence that the soname may be there. - else if (probe.unreachable) unprobed.push(candidate); - // Any other failed check may be transient, so the candidate keeps its - // search position instead of falling behind every cache entry. + } else if (probe.unreachable) unprobed.push(candidate); else yield { path: candidate, unprobed: false }; } - for (const path of cachedPaths(cache, soname, machine)) + for (const path of cachedPaths(cache, request, machine)) yield { path, unprobed: false }; for (const path of unprobed) yield { path, unprobed: true }; } +/** One required intermediate link: filesystem identity and ownership-query spelling. */ +export interface LibraryLookupAlias { + readonly path: string; + readonly ownershipPath: string; +} + +/** A selected leaf name and the target reached through the filesystem. */ +export interface LibraryPath { + readonly path: string; + readonly ownershipPath: string; + readonly resolvedPath: string; + /** Device and inode identify one loaded object across hard-link names. */ + readonly fileIdentity: string; + readonly requiredAliases: readonly LibraryLookupAlias[]; +} + /** What a host check of one candidate path established. */ export type LibraryFileProbe = | { readonly kind: "file"; readonly isFile: boolean } @@ -140,6 +155,8 @@ export type LibraryFileProbe = }; /** Host reads the closure walk needs. */ export interface LibraryWalkHost { + /** Resolve through the filesystem, including symlinks before subsequent `..` components. */ + readonly resolvePath: (path: string) => Promise; readonly dependencies: ( path: string, expected: ElfLibraryIdentity, @@ -149,10 +166,15 @@ export interface LibraryWalkHost { /** The shared libraries an executable loads at start, as the dynamic linker would find them. */ export interface LibraryClosure { - /** Libraries outside the application's own folder, by the soname that loaded them. */ + /** Required intermediate file/directory links outside the captured application folder. */ + readonly requiredAliases: readonly LibraryLookupAlias[]; + /** Required paths or targets outside the application's own folder, by their declared soname. */ readonly libraries: readonly { readonly soname: string; + /** Filesystem-checked ownership spelling, keeping directory aliases. */ + readonly ownershipPath: string; readonly path: string; + readonly resolvedPath: string; }[]; /** Sonames without a compatible candidate in the searched directories or linker cache. */ readonly missing: readonly string[]; @@ -168,7 +190,8 @@ export interface LibraryClosure { /** * Walk DT_NEEDED breadth first from the executable: each soname is looked up * in the RPATH and RUNPATH directories ld.so searches for the needing object, - * then the linker cache. Paths are normalized before any containment check. + * then the linker cache. Filesystem-resolved paths own identity and containment; + * original lookup paths retain the declaring object's ORIGIN semantics. * Libraries in the executable's own folder are the application's and are * walked but not listed. At most `limit` sonames are resolved. */ @@ -178,40 +201,137 @@ export const libraryClosure = async ( host: LibraryWalkHost, limit: number, ): Promise => { - const root = { ...executable, path: posix.normalize(executable.path) }; - const ownFolder = posix.dirname(root.path); - const queue: LoadedObject[] = [{ ...root, loader: undefined }]; + // Reuse admitted path facts only within this walk and by exact spelling. + // Failures stay retryable within the walk budget. Bound the cache separately + // because multiple spellings can resolve to the same request identity. + const resolvedLookups = new Map(); const seen = new Set(); const closure = { - libraries: [] as { soname: string; path: string }[], + requiredAliases: [] as LibraryLookupAlias[], + libraries: [] as { + soname: string; + ownershipPath: string; + path: string; + resolvedPath: string; + }[], missing: [] as string[], unreadable: [] as { path: string; reason: string }[], truncated: false, }; + const rootPath = await host.resolvePath(executable.path); + if (rootPath === undefined) { + closure.unreadable.push({ + path: executable.path, + reason: "the executable's filesystem path could not be resolved", + }); + return closure; + } + const root = { ...executable, path: rootPath.resolvedPath }; + const ownFolder = posix.dirname(root.path); + const queue: LoadedObject[] = [{ ...root, loader: undefined }]; + // Every required lookup can have its own package; the same resolved object + // supplies its dependency declarations only once, through its first load path. + const walkedObjects = new Set([rootPath.fileIdentity]); + const retainedAliases = new Set(); for (const object of queue) { const directories = searchDirectories(object); for (const soname of object.dependencies.needed) { - if (seen.has(soname)) continue; + // DT_NEEDED tokens belong to the declaring object, before request identity + // is shared across the closure. Identical source spellings can name two files. + const expandedRequest = expandOrigin(object.path, soname); + const unsupported = + expandedRequest !== soname && + soname.replace(ORIGIN_TOKEN, "").includes("$"); + const direct = !unsupported && expandedRequest.startsWith("/"); + let directPath = direct + ? resolvedLookups.get(expandedRequest) + : undefined; + if (direct && directPath === undefined) { + // Cached facts can still identify duplicates at capacity. A new + // filesystem lookup needs room in the dependency budget. + if (seen.size >= limit) return { ...closure, truncated: true }; + directPath = await host.resolvePath(expandedRequest); + if (directPath !== undefined && resolvedLookups.size < limit) + resolvedLookups.set(expandedRequest, directPath); + } + const request = directPath?.path ?? expandedRequest; + // An unresolved declaration cannot claim a known literal lookup's identity. + const key = + unsupported || (direct && directPath === undefined) + ? compositeKey(["unresolved", object.path, soname]) + : compositeKey([ + "lookup", + request, + directPath?.ownershipPath ?? request, + ...(directPath?.requiredAliases.flatMap((alias) => [ + alias.path, + alias.ownershipPath, + ]) ?? []), + ]); + if (seen.has(key)) continue; if (seen.size >= limit) return { ...closure, truncated: true }; - seen.add(soname); + seen.add(key); + if (unsupported) { + closure.unreadable.push({ + path: object.path, + reason: `DT_NEEDED ${JSON.stringify(soname)} contains an unsupported dynamic token or dollar expression alongside $ORIGIN`, + }); + continue; + } + if (direct && directPath === undefined) { + closure.unreadable.push({ + path: expandedRequest, + reason: "the library's filesystem path could not be resolved", + }); + continue; + } let selected: | { path: string; + resolvedPath: string; + fileIdentity: string; + lookupPath: string; + ownershipPath: string; + requiredAliases: readonly LibraryLookupAlias[]; dependencies: Result; } | undefined; let unresolved = false; - // A candidate the probe could not reach stays behind everything the walk - // did check, so a searchable candidate with the same soname still wins. + // Denied search directories remain behind reachable candidates. let unprobed: { path: string; reason: string } | undefined; - for await (const { path, unprobed: couldNotProbe } of libraryCandidates( - soname, + for await (const { + path: candidate, + unprobed: couldNotProbe, + } of libraryCandidates( + request, directories, object.dependencies.machine, cache, host, )) { - const dependencies = await host.dependencies(path, object.dependencies); + const resolved = request.startsWith("/") + ? directPath + : await host.resolvePath(candidate); + if (resolved === undefined) { + if (couldNotProbe) { + unprobed ??= { + path: candidate, + reason: "the library's filesystem path could not be resolved", + }; + continue; + } + closure.unreadable.push({ + path: candidate, + reason: "the library's filesystem path could not be resolved", + }); + unresolved = true; + break; + } + const { path, resolvedPath } = resolved; + const dependencies = await host.dependencies( + resolvedPath, + object.dependencies, + ); if (!dependencies.ok && dependencies.error.identity === null) { if (couldNotProbe) { unprobed ??= { path, reason: dependencies.error.reason }; @@ -227,7 +347,7 @@ export const libraryClosure = async ( break; } if (!dependencies.ok && dependencies.error.kind === "incompatible") { - if (soname.startsWith("/")) { + if (request.startsWith("/")) { closure.unreadable.push({ path, reason: dependencies.error.reason, @@ -237,7 +357,15 @@ export const libraryClosure = async ( } continue; } - selected = { path, dependencies }; + selected = { + path, + resolvedPath, + fileIdentity: resolved.fileIdentity, + lookupPath: request.startsWith("/") ? expandedRequest : candidate, + ownershipPath: resolved.ownershipPath, + requiredAliases: resolved.requiredAliases, + dependencies, + }; break; } if (selected === undefined) { @@ -245,12 +373,36 @@ export const libraryClosure = async ( else if (!unresolved) closure.missing.push(soname); continue; } - const { path, dependencies } = selected; - if (!isWithinAny(path, [ownFolder])) - closure.libraries.push({ soname, path }); - if (dependencies.ok) - queue.push({ path, dependencies: dependencies.value, loader: object }); - else closure.unreadable.push({ path, reason: dependencies.error.reason }); + const { path, resolvedPath, fileIdentity, lookupPath, dependencies } = + selected; + for (const alias of selected.requiredAliases) { + const key = compositeKey([alias.path, alias.ownershipPath]); + if (isWithinAny(alias.path, [ownFolder]) || retainedAliases.has(key)) + continue; + retainedAliases.add(key); + closure.requiredAliases.push(alias); + } + if ( + !isWithinAny(path, [ownFolder]) || + !isWithinAny(resolvedPath, [ownFolder]) + ) + closure.libraries.push({ + soname, + ownershipPath: selected.ownershipPath, + path, + resolvedPath, + }); + if (dependencies.ok) { + if (!walkedObjects.has(fileIdentity)) { + walkedObjects.add(fileIdentity); + queue.push({ + path: lookupPath, + dependencies: dependencies.value, + loader: object, + }); + } + } else + closure.unreadable.push({ path, reason: dependencies.error.reason }); } } return closure; diff --git a/tests/boundary/filesystem/desktopLibraryLookupCancellation.test.ts b/tests/boundary/filesystem/desktopLibraryLookupCancellation.test.ts new file mode 100644 index 0000000000..0ee699a9c8 --- /dev/null +++ b/tests/boundary/filesystem/desktopLibraryLookupCancellation.test.ts @@ -0,0 +1,181 @@ +import * as fs from "node:fs/promises"; +import { dirname, join } from "node:path"; +import { afterEach, expect, it, vi } from "vitest"; +import { inspectLibraryPackages } from "../../../src/desktop/LibraryPackageInventory.js"; +import { elfImage } from "../../../src/domain/desktop/elfImage.fixture.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; + +vi.mock("node:fs/promises", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + lstat: vi.fn(actual.lstat), + readlink: vi.fn(actual.readlink), + realpath: vi.fn(actual.realpath), + }; +}); +const actual = + await vi.importActual("node:fs/promises"); +afterEach(() => { + vi.mocked(fs.lstat).mockReset().mockImplementation(actual.lstat); + vi.mocked(fs.readlink).mockReset().mockImplementation(actual.readlink); + vi.mocked(fs.realpath).mockReset().mockImplementation(actual.realpath); +}); +const fixture = async () => { + const root = await createTestTempDirectory("rea-library-cancel-lookup-"); + const bin = join(root, "bin"), + lib = join(root, "lib"), + alias = join(root, "alias"); + await Promise.all([fs.mkdir(bin), fs.mkdir(lib)]); + await Promise.all([ + fs.mkdir(join(lib, "sub")), + fs.mkdir(join(lib, "nested")), + ]); + await fs.symlink("lib", alias); + await fs.writeFile(join(lib, "nested", "libchoice.so"), elfImage({})); + const executable = join(bin, "app"); + await fs.writeFile( + executable, + elfImage({ needed: [`${alias}/sub/../nested/libchoice.so`] }), + ); + return { executable, alias, dotLookup: `${alias}/sub/..` }; +}; + +it + .skipIf(process.platform === "win32") + .each( + (["realpath", "lstat", "readlink"] as const).flatMap((stage) => + ["EACCES", "EPERM", "EIO"].flatMap((code) => + [false, true].map((cancel) => ({ stage, code, cancel })), + ), + ), + )( + "preserves executable lookup refusal semantics: %j", + async ({ stage, code, cancel }) => { + const { executable } = await fixture(); + const selected = join(dirname(dirname(executable)), "entry"); + // Keep the selected executable itself a link, so every metadata stage can + // fail before the closure contains any located libraries or package queries. + await fs.symlink("bin/app", selected); + const controller = new AbortController(), + reason = new Error("cancel refusal"); + const failure = Object.assign(new Error("metadata failure"), { code }); + let injected = false; + const inject = (path: unknown) => { + if (path !== selected) return; + injected = true; + if (cancel) controller.abort(reason); + throw failure; + }; + if (stage === "realpath") + vi.mocked(fs.realpath).mockImplementation(async (...args) => { + inject(args[0]); + return actual.realpath(...args); + }); + else if (stage === "lstat") + vi.mocked(fs.lstat).mockImplementation(async (...args) => { + inject(args[0]); + return actual.lstat(...args); + }); + else + vi.mocked(fs.readlink).mockImplementation(async (...args) => { + inject(args[0]); + return actual.readlink(...args); + }); + const query = vi.fn(async (_command: string, _args: readonly string[]) => ({ + stdout: "", + exitCode: 0, + })); + const operation = inspectLibraryPackages( + selected, + "app", + query, + controller.signal, + ); + if (code === "EIO") await expect(operation).rejects.toBe(failure); + else if (cancel) + await expect(operation).rejects.toMatchObject({ + reason: "cancelled", + cause: reason, + }); + else { + const report = await operation; + expect(report.library_packages).toEqual([]); + expect(report.library_packages_reason).toBeDefined(); + } + expect(injected).toBe(true); + expect(query.mock.calls.every(([command]) => command === "ldconfig")).toBe( + true, + ); + }, +); + +it + .skipIf(process.platform === "win32") + .each(["lstat", "readlink", "realpath"] as const)( + "stops lookup reads after cancellation delivered with %s completion", + async (stage) => { + const { executable, alias, dotLookup } = await fixture(); + const controller = new AbortController(); + const reason = new Error("stop lookup"); + let readsAtAbort = -1; + const cancel = () => { + readsAtAbort = vi.mocked(fs.lstat).mock.calls.length; + controller.abort(reason); + }; + if (stage === "lstat") + vi.mocked(fs.lstat).mockImplementation(async (...args) => { + const value = await actual.lstat(...args); + if (args[0] === alias) cancel(); + return value; + }); + else if (stage === "readlink") + vi.mocked(fs.readlink).mockImplementation(async (...args) => { + const value = await actual.readlink(...args); + if (args[0] === alias) cancel(); + return value; + }); + else + vi.mocked(fs.realpath).mockImplementation(async (...args) => { + const value = await actual.realpath(...args); + if (args[0] === dotLookup) cancel(); + return value; + }); + await expect( + inspectLibraryPackages( + executable, + "app", + async () => ({ stdout: "", exitCode: 0 }), + controller.signal, + ), + ).rejects.toMatchObject({ reason: "cancelled", cause: reason }); + expect(readsAtAbort).toBeGreaterThanOrEqual(0); + expect(vi.mocked(fs.lstat).mock.calls.length).toBe(readsAtAbort); + }, +); + +it.skipIf(process.platform === "win32")( + "preserves a physical link-read failure racing cancellation", + async () => { + const { executable, alias } = await fixture(); + const controller = new AbortController(); + const failure = Object.assign(new Error("readlink I/O failure"), { + code: "EIO", + }); + vi.mocked(fs.readlink).mockImplementation(async (...args) => { + if (args[0] === alias) { + controller.abort(new Error("cancel while reading")); + throw failure; + } + return actual.readlink(...args); + }); + await expect( + inspectLibraryPackages( + executable, + "app", + async () => ({ stdout: "", exitCode: 0 }), + controller.signal, + ), + ).rejects.toBe(failure); + }, +); diff --git a/tests/boundary/filesystem/desktopLibraryLookupRetry.test.ts b/tests/boundary/filesystem/desktopLibraryLookupRetry.test.ts new file mode 100644 index 0000000000..0e24a52679 --- /dev/null +++ b/tests/boundary/filesystem/desktopLibraryLookupRetry.test.ts @@ -0,0 +1,139 @@ +import * as fs from "node:fs/promises"; +import { join } from "node:path"; +import { afterEach, expect, it, vi } from "vitest"; +import { inspectLibraryPackages } from "../../../src/desktop/LibraryPackageInventory.js"; +import { elfImage } from "../../../src/domain/desktop/elfImage.fixture.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; + +vi.mock("node:fs/promises", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, stat: vi.fn(actual.stat) }; +}); +const actual = + await vi.importActual("node:fs/promises"); +afterEach(() => vi.mocked(fs.stat).mockReset().mockImplementation(actual.stat)); + +it.skipIf(process.platform === "win32")( + "retains packages and descendants after a same-filename lookup recovers", + async () => { + const root = await createTestTempDirectory("rea-library-retry-lookup-"); + const bin = join(root, "bin"), + lib = join(root, "lib"); + await Promise.all([fs.mkdir(bin), fs.mkdir(lib)]); + const executable = join(bin, "app"); + const parent = join(lib, "parent.so"), + shared = join(lib, "shared.so"), + child = join(lib, "child.so"); + await Promise.all([ + fs.writeFile(executable, elfImage({ needed: [parent, shared] })), + fs.writeFile(parent, elfImage({ needed: [shared] })), + fs.writeFile(shared, elfImage({ needed: [child] })), + fs.writeFile(child, elfImage({})), + ]); + let attempts = 0; + vi.mocked(fs.stat).mockImplementation(async (...args) => { + if (args[0] === shared && ++attempts === 1) + throw Object.assign(new Error("transient lookup refusal"), { + code: "EACCES", + }); + return actual.stat(...args); + }); + const owners = new Map([ + [parent, "parent-owner"], + [shared, "shared-owner"], + [child, "child-owner"], + ]); + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + return { + stdout: args + .slice(1) + .flatMap((path) => { + const owner = owners.get(path); + return owner === undefined ? [] : [`${owner}: ${path}`]; + }) + .join("\n"), + exitCode: 0, + }; + }, + undefined, + ); + expect(attempts).toBeGreaterThanOrEqual(2); + expect(report).toMatchObject({ + library_packages: ["child-owner", "parent-owner", "shared-owner"], + missing_libraries: [], + unpackaged_libraries: [], + }); + expect(report.library_packages_reason).toBeDefined(); + }, +); + +it.skipIf(process.platform === "win32").each([ + { direct: true, cancel: false, io: true }, + { direct: true, cancel: true, io: true }, + { direct: false, cancel: false, io: true }, + { direct: false, cancel: true, io: true }, + { direct: false, cancel: true, io: false }, + { direct: false, cancel: false, io: false }, +])( + "preserves probe outcomes and cancellation across search paths: %j", + async ({ direct, cancel, io }) => { + const root = await createTestTempDirectory("rea-library-probe-outcome-"); + const bin = join(root, "bin"); + const dirs = [ + join(root, "first"), + join(root, "second"), + join(root, "third"), + ]; + await Promise.all([fs.mkdir(bin), ...dirs.map((path) => fs.mkdir(path))]); + const executable = join(bin, "app"), + first = join(dirs[0]!, "libmissing.so"); + await fs.writeFile( + executable, + elfImage({ + needed: [direct ? first : "libmissing.so"], + runpath: dirs.join(":"), + }), + ); + const controller = new AbortController(), + reason = new Error("cancel probe"); + const failure = Object.assign(new Error("physical stat failure"), { + code: "EIO", + }); + const probes: string[] = []; + vi.mocked(fs.stat).mockImplementation(async (...args) => { + if (typeof args[0] === "string" && args[0].endsWith("/libmissing.so")) { + probes.push(args[0]); + if (args[0] === first) { + if (cancel) controller.abort(reason); + if (io) throw failure; + } + } + return actual.stat(...args); + }); + const operation = inspectLibraryPackages( + executable, + "app", + async () => ({ stdout: "", exitCode: 0 }), + controller.signal, + ); + if (io) await expect(operation).rejects.toBe(failure); + else if (cancel) + await expect(operation).rejects.toMatchObject({ + reason: "cancelled", + cause: reason, + }); + else + expect(await operation).toEqual({ + library_packages: [], + missing_libraries: ["libmissing.so"], + unpackaged_libraries: [], + }); + expect(probes).toEqual( + io || cancel ? [first] : dirs.map((path) => join(path, "libmissing.so")), + ); + }, +); diff --git a/tests/boundary/filesystem/desktopNeededOrigin.test.ts b/tests/boundary/filesystem/desktopNeededOrigin.test.ts new file mode 100644 index 0000000000..03239c82e6 --- /dev/null +++ b/tests/boundary/filesystem/desktopNeededOrigin.test.ts @@ -0,0 +1,950 @@ +import { + copyFile, + lstat, + mkdir, + readlink, + realpath, + symlink, + writeFile, +} from "node:fs/promises"; +import { execFileOutput } from "../../../src/process/ExecFileOutput.js"; +import { dirname, join } from "node:path"; +import { expect, it } from "vitest"; +import { inspectLibraryPackages } from "../../../src/desktop/LibraryPackageInventory.js"; +import { elfImage } from "../../../src/domain/desktop/elfImage.fixture.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; + +it.skipIf(process.platform === "win32")( + "retains every required symlink package while walking its physical target once", + async () => { + const root = await createTestTempDirectory("rea-needed-link-owners-"); + const bin = join(root, "bin"), + first = join(root, "first"), + second = join(root, "second"); + await Promise.all([ + mkdir(bin), + mkdir(join(first, "deep"), { recursive: true }), + mkdir(second), + ]); + await symlink(join("first", "deep"), join(root, "bridge")); + const executable = join(bin, "app"); + const parent = join(bin, "libparent.so"); + const firstLink = join(first, "libfirst.so"); + const secondLink = join(second, "libsecond.so"); + const child = join(first, "libchild.so"); + const decoy = join(second, "libchild.so"); + await Promise.all([ + writeFile( + executable, + elfImage({ needed: ["$ORIGIN/../bridge/../libfirst.so", secondLink] }), + ), + writeFile(parent, elfImage({ needed: ["$ORIGIN/libchild.so"] })), + writeFile(child, elfImage({})), + writeFile(decoy, elfImage({})), + symlink(parent, firstLink), + symlink(parent, secondLink), + ]); + const owners = new Map([ + [join(root, "bridge"), "bridge-owner"], + [firstLink, "first-link-owner"], + [secondLink, "second-link-owner"], + [parent, "app"], + [child, "child-owner"], + [decoy, "decoy-owner"], + ]); + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => + command === "ldconfig" + ? { stdout: "", exitCode: 0 } + : { + stdout: args + .slice(1) + .flatMap((path) => { + const owner = owners.get(path); + return owner === undefined ? [] : [`${owner}: ${path}`]; + }) + .join("\n"), + exitCode: 0, + }, + undefined, + ); + expect(report).toEqual({ + library_packages: [ + "bridge-owner", + "child-owner", + "first-link-owner", + "second-link-owner", + ], + missing_libraries: [], + unpackaged_libraries: [], + }); + }, +); + +it.skipIf(process.platform === "win32")( + "lists an unowned link once when needed directly and through another link", + async () => { + const root = await createTestTempDirectory("rea-needed-unowned-links-"); + const bin = join(root, "bin"); + const lib = join(root, "lib"); + await Promise.all([mkdir(bin), mkdir(lib)]); + const executable = join(bin, "app"); + const outer = join(lib, "outer.so"); + const inner = join(lib, "inner.so"); + await Promise.all([ + writeFile(executable, elfImage({ needed: [outer, inner] })), + writeFile(join(lib, "payload.so"), elfImage({})), + symlink("payload.so", inner), + symlink("inner.so", outer), + ]); + + const report = await inspectLibraryPackages( + executable, + "app", + async (command) => ({ + stdout: "", + exitCode: command === "ldconfig" ? 0 : 1, + }), + undefined, + ); + expect(report.unpackaged_libraries).toEqual([inner, outer]); + }, +); + +it + .skipIf(process.platform === "win32") + .each(["needed", "rpath", "runpath", "cache"] as const)( + "preserves directory symlink traversal in %s lookups", + async (kind) => { + const root = await createTestTempDirectory("rea-needed-symlink-"); + const bin = join(root, "bin"); + const physical = join(root, "physical"); + await Promise.all([ + mkdir(bin), + mkdir(join(physical, "deep"), { recursive: true }), + ]); + await symlink(join("physical", "deep"), join(root, "bridge")); + const actual = join(physical, "libchoice.so"); + const decoy = join(root, "libchoice.so"); + const lookup = `${root}/bridge/../libchoice.so`; + expect(await realpath(`${root}/bridge/..`)).toBe(physical); + const executable = join(bin, "app"); + await Promise.all([ + writeFile(actual, elfImage({})), + writeFile(decoy, elfImage({})), + writeFile( + executable, + elfImage({ + needed: + kind === "needed" + ? ["$ORIGIN/../bridge/../libchoice.so", decoy] + : ["libchoice.so"], + ...(kind === "rpath" || kind === "runpath" + ? { [kind]: "$ORIGIN/../bridge/.." } + : {}), + }), + ), + ]); + const owners = new Map([ + [join(root, "bridge"), "bridge-owner"], + [actual, "actual-owner"], + [decoy, "decoy-owner"], + ]); + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") + return { + stdout: + kind === "cache" + ? `\tlibchoice.so (libc6,AArch64) => ${lookup}` + : "", + exitCode: 0, + }; + return { + stdout: args + .slice(1) + .flatMap((path) => + owners.has(path) ? [`${owners.get(path)}: ${path}`] : [], + ) + .join("\n"), + exitCode: 0, + }; + }, + undefined, + ); + expect(report).toEqual({ + library_packages: + kind === "needed" + ? ["actual-owner", "bridge-owner", "decoy-owner"] + : ["actual-owner", "bridge-owner"], + missing_libraries: [], + unpackaged_libraries: [], + }); + }, +); + +it.skipIf(process.platform === "win32")( + "retains the loaded object's lookup directory for its own ORIGIN declarations", + async () => { + const root = await createTestTempDirectory("rea-needed-alias-origin-"); + const bin = join(root, "bin"), + alias = join(root, "alias"), + physical = join(root, "physical"); + await Promise.all([bin, alias, physical].map((path) => mkdir(path))); + const executable = join(bin, "app"); + const parent = join(physical, "libparent.so"); + const loaded = join(alias, "libparent.so"); + const child = join(alias, "libchild.so"); + await Promise.all([ + writeFile(executable, elfImage({ needed: [loaded] })), + writeFile(parent, elfImage({ needed: ["$ORIGIN/libchild.so"] })), + writeFile(child, elfImage({})), + writeFile(join(physical, "libchild.so"), elfImage({})), + ]); + await symlink(parent, loaded); + const owners = new Map([ + [parent, "parent-owner"], + [child, "child-owner"], + [join(physical, "libchild.so"), "decoy-owner"], + ]); + const result = await inspectLibraryPackages( + executable, + "app", + async (command, args) => + command === "ldconfig" + ? { stdout: "", exitCode: 0 } + : { + stdout: args + .slice(1) + .flatMap((path) => + owners.has(path) ? [`${owners.get(path)}: ${path}`] : [], + ) + .join("\n"), + exitCode: 0, + }, + undefined, + ); + expect(result).toEqual({ + library_packages: ["child-owner", "parent-owner"], + missing_libraries: [], + unpackaged_libraries: [], + }); + }, +); + +it.skipIf(process.platform === "win32").each(["$ORIGIN", "${ORIGIN}"])( + "resolves %s DT_NEEDED in each declaring object before deduplicating requests", + async (token) => { + const root = await createTestTempDirectory("rea-needed-origin-"); + const base = join(root, "$build-$&"); + const bin = join(base, "bin"), + a = join(base, "a"), + b = join(base, "b"); + await Promise.all( + [bin, a, b].map((path) => mkdir(path, { recursive: true })), + ); + const executable = join(bin, "app"); + const owners = new Map([ + [join(a, "libparenta.so"), "parent-a"], + [join(b, "libparentb.so"), "parent-b"], + [join(a, "libliteral.so"), "literal-owner"], + [join(a, "libchild.so"), "child-a"], + [join(b, "libchild.so"), "child-b"], + ]); + await writeFile( + executable, + elfImage({ + needed: [ + `${token}/../a/libparenta.so`, + `${token}/../b/libparentb.so`, + join(a, "libliteral.so"), + ], + }), + ); + for (const [path, owner] of owners) + await writeFile( + path, + elfImage({ + needed: owner.startsWith("parent-") ? [`${token}/libchild.so`] : [], + }), + ); + const queried: string[] = []; + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + queried.push(...args.slice(1)); + return { + stdout: args + .slice(1) + .flatMap((path) => + owners.has(path) ? [`${owners.get(path)}: ${path}`] : [], + ) + .join("\n"), + exitCode: 0, + }; + }, + undefined, + ); + expect(report).toEqual({ + library_packages: [...owners.values()].sort(), + missing_libraries: [], + unpackaged_libraries: [], + }); + expect(new Set(queried)).toEqual(new Set(owners.keys())); + }, +); + +it.skipIf(process.platform === "win32")( + "keeps unresolved DT_NEEDED token combinations out of package ownership", + async () => { + const root = await createTestTempDirectory("rea-needed-unknown-"); + const bin = join(root, "bin"), + literal = join(bin, "$LIB"); + await mkdir(literal, { recursive: true }); + const executable = join(bin, "app"), + decoy = join(literal, "libchoice.so"); + await writeFile( + executable, + elfImage({ needed: ["$ORIGIN/$LIB/libchoice.so"] }), + ); + await writeFile(decoy, elfImage({})); + const queried: string[] = []; + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + queried.push(...args.slice(1)); + return { stdout: `decoy-owner: ${decoy}`, exitCode: 0 }; + }, + undefined, + ); + expect(report.library_packages).toEqual([]); + expect(report.missing_libraries).toEqual([]); + expect(report.library_packages_reason).toMatch( + /DT_NEEDED.*unsupported dynamic token/, + ); + expect(queried).toEqual([]); + }, +); + +it.skipIf(process.platform === "win32")( + "keeps an incompatible direct ORIGIN dependency unresolved instead of searching a fallback", + async () => { + const root = await createTestTempDirectory("rea-needed-incompatible-"); + const bin = join(root, "bin"), + foreign = join(root, "foreign"), + fallback = join(root, "fallback"); + await Promise.all([bin, foreign, fallback].map((path) => mkdir(path))); + const executable = join(bin, "app"), + direct = join(foreign, "libchoice.so"); + await writeFile( + executable, + elfImage({ + needed: ["$ORIGIN/../foreign/libchoice.so"], + runpath: fallback, + }), + ); + await writeFile(direct, elfImage({ machine: 62 })); + await writeFile(join(fallback, "libchoice.so"), elfImage({})); + const queried: string[] = []; + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + queried.push(...args.slice(1)); + return { stdout: "", exitCode: 0 }; + }, + undefined, + ); + expect(report.library_packages).toEqual([]); + expect(report.missing_libraries).toEqual([]); + expect(report.library_packages_reason).toContain( + "Incompatible ELF library", + ); + expect(report.library_packages_reason).toContain(direct); + expect(queried).toEqual([]); + }, +); + +it.skipIf(process.platform === "win32")( + "does not let an unresolved declaration suppress a known literal absolute lookup", + async () => { + const root = await createTestTempDirectory("rea-needed-literal-"); + const bin = join(root, "bin"), + literal = join(root, "$draft"); + await Promise.all([mkdir(bin), mkdir(literal)]); + const executable = join(bin, "app"), + library = join(literal, "libchoice.so"); + await writeFile( + executable, + elfImage({ needed: ["$ORIGIN/../$draft/libchoice.so", library] }), + ); + await writeFile(library, elfImage({})); + const queried: string[] = []; + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + queried.push(...args.slice(1)); + return { stdout: `literal-owner: ${library}`, exitCode: 0 }; + }, + undefined, + ); + expect(report.library_packages).toEqual(["literal-owner"]); + expect(report.missing_libraries).toEqual([]); + expect(report.library_packages_reason).toMatch( + /DT_NEEDED.*unsupported dynamic token/, + ); + expect(queried).toEqual([library]); + }, +); + +it.skipIf(process.platform === "win32").each([ + { + label: "external packaged", + external: true, + owner: "vendor", + expected: ["vendor"], + unpackaged: false, + }, + { + label: "external separately packaged", + external: true, + owner: "alias-owner", + expected: ["alias-owner"], + unpackaged: false, + }, + { + label: "external unowned", + external: true, + owner: undefined, + expected: [], + unpackaged: true, + }, + { + label: "captured internal", + external: false, + owner: "vendor", + expected: [], + unpackaged: false, + }, +])( + "retains required directory aliases ($label) when the DSO itself is captured", + async ({ external, owner, expected, unpackaged }) => { + const root = await createTestTempDirectory("rea-needed-directory-owner-"); + const bin = join(root, "app", "bin"); + await mkdir(bin, { recursive: true }); + const alias = join(external ? root : bin, "alias"); + const library = join(bin, "libinside.so"); + const executable = join(bin, "app"); + await symlink(bin, alias); + await writeFile(library, elfImage({})); + await writeFile( + executable, + elfImage({ needed: [`${alias}/libinside.so`] }), + ); + const owners = new Map([ + [library, "vendor"], + ...(owner === undefined ? [] : [[alias, owner] as const]), + ]); + const queried: string[] = []; + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + queried.push(...args.slice(1)); + return { + stdout: args + .slice(1) + .flatMap((path) => + owners.has(path) ? [`${owners.get(path)}: ${path}`] : [], + ) + .join("\n"), + exitCode: 0, + }; + }, + undefined, + ); + expect(report).toEqual({ + library_packages: expected, + missing_libraries: [], + unpackaged_libraries: unpackaged ? [alias] : [], + }); + expect(queried.includes(alias)).toBe(external); + }, +); + +it.skipIf(process.platform === "win32")( + "retains every package in a required directory-alias chain", + async () => { + const root = await createTestTempDirectory("rea-needed-directory-chain-"); + const bin = join(root, "app", "bin"); + await mkdir(bin, { recursive: true }); + const outer = join(root, "outer"); + const inner = join(root, "inner"); + await symlink("inner", outer); + await symlink("app/bin", inner); + const library = join(bin, "libinside.so"); + const executable = join(bin, "app"); + await writeFile(library, elfImage({})); + await writeFile( + executable, + elfImage({ needed: [`${outer}/libinside.so`] }), + ); + const owners = new Map([ + [outer, "outer-owner"], + [inner, "inner-owner"], + ]); + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => + command === "ldconfig" + ? { stdout: "", exitCode: 0 } + : { + stdout: args + .slice(1) + .flatMap((path) => + owners.has(path) ? [`${owners.get(path)}: ${path}`] : [], + ) + .join("\n"), + exitCode: 0, + }, + undefined, + ); + expect(report).toEqual({ + library_packages: ["inner-owner", "outer-owner"], + missing_libraries: [], + unpackaged_libraries: [], + }); + }, +); + +it.skipIf(process.platform === "win32")( + "retains distinct directory aliases before deduplicating the same loaded DSO", + async () => { + const root = await createTestTempDirectory( + "rea-needed-directory-distinct-", + ); + const bin = join(root, "app", "bin"); + await mkdir(bin, { recursive: true }); + const first = join(root, "first"), + second = join(root, "second"); + await symlink("app/bin", first); + await symlink("app/bin", second); + const library = join(bin, "libinside.so"); + const executable = join(bin, "app"); + await writeFile(library, elfImage({})); + await writeFile( + executable, + elfImage({ needed: [`${first}/libinside.so`, `${second}/libinside.so`] }), + ); + const owners = new Map([ + [first, "first-owner"], + [second, "second-owner"], + ]); + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => + command === "ldconfig" + ? { stdout: "", exitCode: 0 } + : { + stdout: args + .slice(1) + .flatMap((path) => + owners.has(path) ? [`${owners.get(path)}: ${path}`] : [], + ) + .join("\n"), + exitCode: 0, + }, + undefined, + ); + expect(report).toEqual({ + library_packages: ["first-owner", "second-owner"], + missing_libraries: [], + unpackaged_libraries: [], + }); + }, +); + +it.skipIf(process.platform === "win32").each([ + { relative: false, owned: true, chain: false }, + { relative: true, owned: true, chain: false }, + { relative: true, owned: false, chain: false }, + { relative: true, owned: true, chain: true }, +])( + "follows a file link into lookup aliases (relative=$relative, owned=$owned, chain=$chain)", + async ({ relative, owned, chain }) => { + const root = await createTestTempDirectory("rea-needed-leaf-directory-"); + const bin = join(root, "app"); + const external = join(root, "external"); + await mkdir(bin); + await mkdir(external); + const executable = join(bin, "app"); + const physical = join(bin, "libreal.so"); + const alias = join(root, "current"); + const leaf = join(external, "lib.so"); + await writeFile(physical, elfImage({})); + await symlink("app", alias); + const intermediate = join(root, "intermediate.so"); + if (chain) await symlink("current/libreal.so", intermediate); + await symlink( + chain + ? "../intermediate.so" + : relative + ? "../current/libreal.so" + : `${alias}/libreal.so`, + leaf, + ); + await writeFile(executable, elfImage({ needed: [leaf] })); + const owners = new Map([ + [leaf, "leaf-package"], + ...(owned ? [[alias, "alias-package"] as [string, string]] : []), + ...(chain + ? [[intermediate, "intermediate-package"] as [string, string]] + : []), + ]); + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => + command === "ldconfig" + ? { stdout: "", exitCode: 0 } + : { + stdout: args + .slice(1) + .flatMap((path) => + owners.has(path) ? [`${owners.get(path)}: ${path}`] : [], + ) + .join("\n"), + exitCode: 0, + }, + undefined, + ); + expect(report).toEqual({ + library_packages: [ + ...(owned ? ["alias-package"] : []), + ...(chain ? ["intermediate-package"] : []), + "leaf-package", + ], + missing_libraries: [], + unpackaged_libraries: owned ? [] : [alias], + }); + }, +); + +it.skipIf(process.platform === "win32").each([false, true])( + "queries the original alias-qualified library filename separately from identity (intermediate=%s)", + async (intermediate) => { + const root = await createTestTempDirectory("rea-needed-lookup-owner-"); + const bin = join(root, "bin"); + const physical = join(root, "release"); + await mkdir(bin); + await mkdir(physical); + const alias = join(root, "current"); + await symlink("release", alias); + const rawLookup = `${alias}/lib.so`; + const leaf = join(root, "leaf.so"); + const lookup = intermediate ? leaf : rawLookup; + if (intermediate) { + await symlink("libreal.so", join(physical, "lib.so")); + await writeFile(join(physical, "libreal.so"), elfImage({})); + await symlink(rawLookup, leaf); + } + const executable = join(bin, "app"); + if (!intermediate) await writeFile(join(physical, "lib.so"), elfImage({})); + await writeFile(executable, elfImage({ needed: [lookup] })); + const owners = new Map([ + [rawLookup, "payload-package"], + ...(intermediate ? [[leaf, "leaf-package"] as [string, string]] : []), + [alias, "alias-package"], + ]); + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => + command === "ldconfig" + ? { stdout: "", exitCode: 0 } + : { + stdout: args + .slice(1) + .flatMap((path) => + owners.has(path) ? [`${owners.get(path)}: ${path}`] : [], + ) + .join("\n"), + exitCode: 0, + }, + undefined, + ); + expect(report).toEqual({ + library_packages: intermediate + ? ["alias-package", "leaf-package", "payload-package"] + : ["alias-package", "payload-package"], + missing_libraries: [], + unpackaged_libraries: [], + }); + }, +); + +it.skipIf(process.platform !== "linux" || process.arch !== "x64")( + "does not install incidental owners of the Ubuntu base's exact usr-merge aliases", + async (context) => { + const libTarget = await readlink("/lib").catch(() => undefined); + if (libTarget !== "usr/lib" && libTarget !== "/usr/lib") { + context.skip(); + return; + } + const root = await createTestTempDirectory("rea-needed-usr-merge-"); + const executable = join(root, "app"); + await copyFile("/usr/bin/true", executable); + const queried: string[] = []; + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") + return { + ...(await execFileOutput("/sbin/ldconfig", ["-p"], { + timeout: 5000, + })), + exitCode: 0, + }; + queried.push(...args.slice(1)); + return { + stdout: args + .slice(1) + .flatMap((path) => + path === "/lib" || path === "/lib64" + ? [`incidental-a, incidental-b: ${path}`] + : [`libc6: ${path}`], + ) + .join("\n"), + exitCode: 0, + }; + }, + undefined, + ); + expect(report).toEqual({ + library_packages: ["libc6"], + missing_libraries: [], + unpackaged_libraries: [], + }); + expect(queried).not.toContain("/lib"); + expect(queried).not.toContain("/lib64"); + }, +); + +it + .skipIf(process.platform !== "linux" || process.arch !== "x64") + .for([true, false])( + "uses merged-system spellings for intermediate file links (owned=%s)", + async (owned, context) => { + let lookup: string | undefined; + let middle: string | undefined; + for (const name of ["libbrotlicommon.so", "libcom_err.so"]) { + const path = `/usr/lib/x86_64-linux-gnu/${name}`; + try { + if (!(await lstat(path)).isSymbolicLink()) continue; + const target = join(dirname(path), await readlink(path)); + if (!(await lstat(target)).isSymbolicLink()) continue; + lookup = path; + middle = target; + break; + } catch (cause) { + if ( + !( + cause instanceof Error && + "code" in cause && + ["ENOENT", "ENOTDIR", "EACCES"].includes(String(cause.code)) + ) + ) + throw cause; + } + } + if (lookup === undefined || middle === undefined) { + context.skip(); + return; + } + const alternate = middle.slice(4); + const root = await createTestTempDirectory("rea-needed-usr-link-"); + const executable = join(root, "app"); + await writeFile(executable, elfImage({ machine: 62, needed: [lookup] })); + const queried: string[] = []; + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") + return { + ...(await execFileOutput("/sbin/ldconfig", ["-p"], { + timeout: 5000, + })), + exitCode: 0, + }; + queried.push(...args.slice(1)); + return { + stdout: args + .slice(1) + .flatMap((path) => + path === middle || path === alternate + ? owned && path === alternate + ? [`runtime-package: ${path}`] + : [] + : [`other-package: ${path}`], + ) + .join("\n"), + exitCode: 0, + }; + }, + undefined, + ); + expect(report).toEqual({ + library_packages: owned + ? ["other-package", "runtime-package"] + : ["other-package"], + missing_libraries: [], + unpackaged_libraries: owned ? [] : [middle], + }); + expect(queried).toContain(alternate); + }, +); + +it.skipIf(process.platform === "win32").each([ + { reverse: false, physicalOwned: false }, + { reverse: true, physicalOwned: false }, + { reverse: false, physicalOwned: true }, + { reverse: true, physicalOwned: true }, +])( + "retains distinct ownership spellings of one selected leaf: %j", + async ({ reverse, physicalOwned }) => { + const root = await createTestTempDirectory("rea-needed-leaf-spellings-"); + const executable = join(root, "bin", "app"); + const release = join(root, "release"); + const current = join(root, "current"); + await mkdir(join(root, "bin")); + await mkdir(join(release, "sub"), { recursive: true }); + await symlink("release", current); + const physical = join(release, "libchoice.so"); + const alias = `${current}/libchoice.so`; + const declarations = [`${current}/sub/../libchoice.so`, alias]; + if (reverse) declarations.reverse(); + await writeFile(executable, elfImage({ needed: declarations })); + await writeFile(physical, elfImage({})); + const queried: string[] = []; + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + queried.push(...args.slice(1)); + return { + stdout: args + .slice(1) + .flatMap((path) => { + const owner = + path === alias + ? "alias-owner" + : path === current + ? "route-owner" + : physicalOwned && path === physical + ? "physical-owner" + : undefined; + return owner === undefined ? [] : [`${owner}: ${path}`]; + }) + .join("\n"), + exitCode: 0, + }; + }, + undefined, + ); + expect(report).toEqual({ + library_packages: physicalOwned + ? ["alias-owner", "physical-owner", "route-owner"] + : ["alias-owner", "route-owner"], + missing_libraries: [], + unpackaged_libraries: [], + }); + expect(queried).toContain(alias); + expect(queried).toContain(physical); + }, +); + +it.skipIf(process.platform === "win32").each([ + { first: "a", ownedFirst: false }, + { first: "b", ownedFirst: false }, + { first: "a", ownedFirst: true }, + { first: "b", ownedFirst: true }, +])( + "retains target ownership in each repeated link context: %j", + async ({ first, ownedFirst }) => { + const root = await createTestTempDirectory("rea-needed-link-context-"); + const bin = join(root, "bin"); + const real = join(root, "real"); + await mkdir(bin); + await mkdir(join(real, "sub"), { recursive: true }); + await Promise.all([ + symlink("real", join(root, "a")), + symlink("real", join(root, "b")), + symlink("y", join(real, "x")), + symlink("sub", join(real, "y")), + ]); + const second = first === "a" ? "b" : "a"; + const lookup = `${root}/${first}/x/../../${second}/x/lib.so`; + const payload = join(real, "sub", "lib.so"); + const executable = join(bin, "app"); + await writeFile(payload, elfImage({})); + await writeFile(executable, elfImage({ needed: [lookup] })); + expect(await realpath(lookup)).toBe(payload); + const yOwnerPath = join(root, ownedFirst ? first : second, "y"); + const owners = new Map([ + [join(root, "a"), "a-route"], + [join(root, "b"), "b-route"], + [join(root, first, "x"), "x-owner"], + [yOwnerPath, "y-owner"], + [payload, "payload-owner"], + ]); + const queried: string[] = []; + const report = await inspectLibraryPackages( + executable, + "app", + async (command, args) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + queried.push(...args.slice(1)); + return { + stdout: args + .slice(1) + .flatMap((path) => { + const owner = owners.get(path); + return owner === undefined ? [] : [`${owner}: ${path}`]; + }) + .join("\n"), + exitCode: 0, + }; + }, + undefined, + ); + expect(report).toEqual({ + library_packages: [ + "a-route", + "b-route", + "payload-owner", + "x-owner", + "y-owner", + ], + missing_libraries: [], + unpackaged_libraries: [], + }); + expect(queried).toContain(yOwnerPath); + }, +);