diff --git a/src/desktop/LibraryPackageInventory.test.ts b/src/desktop/LibraryPackageInventory.test.ts index 1238ca9f47..e17db241a5 100644 --- a/src/desktop/LibraryPackageInventory.test.ts +++ b/src/desktop/LibraryPackageInventory.test.ts @@ -11,7 +11,7 @@ import { inspectLibraryPackages } from "./LibraryPackageInventory.js"; import { elfImage } from "../domain/desktop/elfImage.fixture.js"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; -it("reports truncated ELF dependency tables without suggesting more capture headroom", async () => { +it("reports why the selected executable could not supply library dependencies", async () => { const root = await createTestTempDirectory("rea-library-table-framing-"); const executable = join(root, "app"); const needed = Array.from( @@ -37,13 +37,20 @@ it("reports truncated ELF dependency tables without suggesting more capture head "ELF dynamic table reached library-policy.table_bytes=112 bytes", ); image.writeBigUInt64LE(BigInt(image.length), 64 + 56 + 8); - await writeFile(executable, image); - expect(await inspect()).toEqual({ - library_packages: [], - missing_libraries: [], - unpackaged_libraries: [], - library_packages_reason: `the libraries of ${executable} were not read: ELF dynamic table is truncated`, - }); + const script = + "#!/bin/sh\n# An authored shell launcher with room for an ELF header probe.\nexit 0\n"; + for (const [bytes, reason] of [ + [image, "ELF dynamic table is truncated"], + [script, "not an ELF image"], + ] as const) { + await writeFile(executable, bytes); + expect(await inspect()).toEqual({ + library_packages: [], + missing_libraries: [], + unpackaged_libraries: [], + library_packages_reason: `the libraries of ${executable} were not read: ${reason}`, + }); + } }); it.skipIf(process.platform === "win32").each(["/", "/.", "/.."])( @@ -296,24 +303,6 @@ describe.skipIf( expect(libraries.library_packages).toContain("libc6"); expect(libraries.missing_libraries).toEqual([]); }); - - it("explains a launcher script instead of reading it as ELF", async () => { - const libraries = await inspectLibraryPackages( - "/usr/bin/ldd", - "libc-bin", - runHostQuery, - undefined, - ); - - expect(libraries).toEqual({ - library_packages: [], - missing_libraries: [], - unpackaged_libraries: [], - library_packages_reason: expect.stringMatching( - /\/usr\/bin\/ldd.*not read.*not an ELF image/iu, - ), - }); - }); }); it.skipIf(process.platform === "win32")( @@ -331,19 +320,23 @@ it.skipIf(process.platform === "win32")( writeFile(executable, elfImage({ needed: paths })), ...paths.map((path) => writeFile(path, elfImage({}))), ]); - let batches = 0; + const batches: string[][] = []; const result = await inspectLibraryPackages( executable, "app", - async (command) => { + async (command, args) => { if (command === "ldconfig") return { stdout: "", exitCode: 0 }; - batches++; - if (batches === 1) return { stdout: `owner: ${paths[0]}`, exitCode: 1 }; + const [option, ...queried] = args; + expect(command).toBe("dpkg-query"); + expect(option).toBe("-S"); + batches.push(queried); + if (batches.length === 1) + return { stdout: `owner: ${paths[0]}`, exitCode: 1 }; return { stdout: "", exitCode: 2 }; }, undefined, ); - expect(batches).toBe(2); + expect(batches).toEqual([paths.slice(0, 200), paths.slice(200)]); expect(result.library_packages).toEqual(["owner"]); expect(result.unpackaged_libraries).toContain(paths[1]); expect(result.unpackaged_libraries).not.toContain(paths[200]); @@ -458,58 +451,63 @@ it.skipIf(process.platform === "win32")( ); it.skipIf(process.platform === "win32")( - "recovers a 513-library closure with the caller-selected capture policy", + "recovers the next transitive library by raising the selected closure limit", async () => { const root = await createTestTempDirectory("rea-library-policy-"); const bin = join(root, "bin"); const lib = join(root, "lib"); await Promise.all([mkdir(bin), mkdir(lib)]); - const paths = Array.from({ length: 513 }, (_, index) => - join(lib, `lib${index}.so`), - ); const executable = join(bin, "app"); + const paths = [ + join(lib, "first.so"), + join(lib, "middle.so"), + join(lib, "last.so"), + ] as const; + const [first, middle, last] = paths; await Promise.all([ - writeFile(executable, elfImage({ needed: [paths[0] ?? ""] })), - ...paths.map((path, index) => - writeFile( - path, - elfImage({ - needed: - paths[index + 1] === undefined ? [] : [paths[index + 1] ?? ""], - }), - ), - ), + writeFile(executable, elfImage({ needed: [first] })), + writeFile(first, elfImage({ needed: [middle] })), + writeFile(middle, elfImage({ needed: [last] })), + writeFile(last, elfImage({})), ]); - const query = async (command: string, args: readonly string[]) => ({ - stdout: - command === "ldconfig" - ? "" - : args - .slice(1) - .map((path) => `owner-${paths.indexOf(path)}: ${path}`) - .join("\n"), - exitCode: 0, - }); - const limited = await inspectLibraryPackages( - executable, - "app", - query, - undefined, - ); - expect(limited.library_packages).toHaveLength(512); + + const ownershipQueries: string[][] = []; + const query = async (command: string, args: readonly string[]) => { + if (command === "ldconfig") return { stdout: "", exitCode: 0 }; + expect(command).toBe("dpkg-query"); + const [option, ...queried] = args; + expect(option).toBe("-S"); + ownershipQueries.push(queried); + return { + stdout: queried + .map( + (path) => + `owner-${paths.findIndex((item) => item === path)}: ${path}`, + ) + .join("\n"), + exitCode: 0, + }; + }; + const inspect = (libraries: number) => + inspectLibraryPackages( + executable, + "app", + query, + undefined, + desktopLibraryPolicy({ libraries }), + ); + const limited = await inspect(2); + expect(limited.library_packages).toEqual(["owner-0", "owner-1"]); expect(limited.library_packages_reason).toContain( - "library-policy.libraries=512", + "library-policy.libraries=2", ); expect(limited.library_packages_reason).toContain("--library-policy"); - const recovered = await inspectLibraryPackages( - executable, - "app", - query, - undefined, - desktopLibraryPolicy({ libraries: 513 }), - ); - expect(recovered.library_packages).toHaveLength(513); - expect(recovered.library_packages).toContain("owner-512"); - expect(recovered.library_packages_reason).toBeUndefined(); + const recovered = await inspect(3); + expect(recovered).toEqual({ + library_packages: ["owner-0", "owner-1", "owner-2"], + missing_libraries: [], + unpackaged_libraries: [], + }); + expect(ownershipQueries).toEqual([[first, middle], paths]); }, ); diff --git a/src/domain/desktop/libraryPackages.test.ts b/src/domain/desktop/libraryPackages.test.ts index 72c9385aa7..08dbfd7dae 100644 --- a/src/domain/desktop/libraryPackages.test.ts +++ b/src/domain/desktop/libraryPackages.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest"; import { posix } from "node:path"; import { err, ok } from "../result.js"; +import { desktopLibraryPolicy } from "./libraryPolicy.js"; import { elfLibraryIdentityMatches, type ElfLibraryIdentity, @@ -112,98 +113,108 @@ describe("parseLinkerCache", () => { }); }); -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. - const closure = await libraryClosure( - { - path: "/app/bin/app", - dependencies: elf(["libcore.so"], [], ["/denied", "/native"]), - }, - [], - { - ...host({}), - dependencies: async (path: string) => - path === "/app/bin/app" - ? ok(elf(["libcore.so"], [], ["/denied", "/native"])) - : path === "/native/libcore.so" - ? ok(elf([])) - : err({ - kind: "unreadable" as const, - identity: null, - reason: "EACCES", - }), - isFile: async (path: string) => - path === "/denied/libcore.so" - ? { kind: "unknown" as const, unreachable: true } - : { - kind: "file" as const, - isFile: path === "/native/libcore.so", - }, - }, - 10, - ); - expect(closure).toEqual({ - requiredAliases: [], - libraries: [ - { - soname: "libcore.so", - path: "/native/libcore.so", - ownershipPath: "/native/libcore.so", - resolvedPath: "/native/libcore.so", - }, - ], - missing: [], - unreadable: [], - truncated: false, - }); - }); - - 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. +describe("libraryClosure probe authority", () => { + const soname = "libcore.so"; + const candidate = "/volatile/libcore.so"; + const native = "/native/libcore.so"; + const cached = "/cache/libcore.so"; + const unknownPath = "the library's filesystem path could not be resolved"; + it.each([ + { + scenario: "denial before another search directory", + denied: true, + resolves: false, + fallback: "directory", + selected: native, + reason: null, + }, + { + scenario: "denial before a cache entry", + denied: true, + resolves: false, + fallback: "cache", + selected: cached, + reason: null, + }, + { + scenario: "denial with an unreadable file", + denied: true, + resolves: true, + fallback: null, + selected: null, + reason: "EACCES", + }, + { + scenario: "denial with unresolved physical identity", + denied: true, + resolves: false, + fallback: null, + selected: null, + reason: unknownPath, + }, + { + scenario: "transient probe with recovered identity", + denied: false, + resolves: true, + fallback: "cache", + selected: candidate, + reason: null, + }, + { + scenario: "transient probe with unresolved identity", + denied: false, + resolves: false, + fallback: "cache", + selected: null, + reason: unknownPath, + }, + ] as const)( + "preserves library selection after $scenario", + async ({ denied, resolves, fallback, selected, reason }) => { + const files = host({ + [candidate]: denied ? { identity: null, reason: "EACCES" } : elf([]), + [native]: elf([]), + [cached]: elf([]), + }); const closure = await libraryClosure( + app( + elf( + [soname], + [], + fallback === "directory" ? ["/volatile", "/native"] : ["/volatile"], + ), + ), + fallback === "cache" + ? [{ soname, flags: ["AArch64"], path: cached }] + : [], { - 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: "EACCES", - }), - isFile: async () => ({ - kind: "unknown" as const, - unreachable: true, - }), + ...files, + resolvePath: (path) => + path === candidate && !resolves + ? Promise.resolve(undefined) + : files.resolvePath(path), + isFile: (path) => + path === candidate + ? Promise.resolve({ kind: "unknown", unreachable: denied }) + : files.isFile(path), }, 10, ); expect(closure).toEqual({ requiredAliases: [], - libraries: [], + libraries: + selected === null + ? [] + : [ + { + soname, + path: selected, + resolvedPath: selected, + ownershipPath: selected, + }, + ], missing: [], - unreadable: [ - { - path: "/denied/libcore.so", - reason: resolvable - ? "EACCES" - : "the library's filesystem path could not be resolved", - }, - ], + unreadable: reason === null ? [] : [{ path: candidate, reason }], truncated: false, }); }, @@ -211,60 +222,6 @@ describe("libraryClosure denied probes", () => { }); 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( typora, @@ -703,31 +660,51 @@ describe("libraryPackageNames", () => { }); }); -describe("absolute dependency lookup reuse", () => { - it("reports truncation before reading a new absolute dependency beyond the limit", async () => { - const admitted = "/native/admitted.so"; +it.each([ + { + label: "selected limit", + policy: desktopLibraryPolicy({ libraries: 1 }), + count: 1, + }, + { label: "default", policy: desktopLibraryPolicy(), count: 512 }, +])( + "stops before touching the next transitive library at the $label", + async ({ policy, count }) => { + const admitted = Array.from( + { length: count }, + (_, index) => `/native/lib${index}.so`, + ); const beyondLimit = "/native/over-budget.so"; - const reads = host({ [admitted]: elf([]) }); + const images = Object.fromEntries( + admitted.map((path, index) => [ + path, + elf([admitted[index + 1] ?? beyondLimit]), + ]), + ); + const reads = host(images); const result = await libraryClosure( - app(elf([admitted, beyondLimit])), + app(elf(admitted.slice(0, 1))), [], { ...reads, resolvePath: async (path) => { - if (path === beyondLimit) throw new Error("metadata read failed"); + if (path === beyondLimit) + throw new Error("read a dependency beyond the selected limit"); return reads.resolvePath(path); }, }, - 1, + policy.libraries, ); - + expect(result.libraries.map(({ soname }) => soname)).toEqual(admitted); expect(result).toMatchObject({ - libraries: [{ soname: admitted }], + missing: [], unreadable: [], truncated: true, }); - }); + }, +); +describe("absolute dependency lookup reuse", () => { it("resolves each exact successful spelling once across repeated declarations", async () => { const paths = Array.from( { length: 8 },