From 76585bc08506ba154e267b09590e7f2210380e64 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:52:49 -0500 Subject: [PATCH] test(ghidra): consolidate PE PDB provenance coverage (#12919) --- src/ghidra/GhidraPeProfile.test.ts | 150 +++++++++------------ src/ghidra/GhidraProvider.behavior.test.ts | 108 +++++---------- 2 files changed, 99 insertions(+), 159 deletions(-) diff --git a/src/ghidra/GhidraPeProfile.test.ts b/src/ghidra/GhidraPeProfile.test.ts index ff8711c91f..ccfbc9c73d 100644 --- a/src/ghidra/GhidraPeProfile.test.ts +++ b/src/ghidra/GhidraPeProfile.test.ts @@ -16,7 +16,7 @@ const target = { executableRole: "application", managed: false, codeView: { - pdbPath: "public.pdb", + pdbPath: "C:\\symbols\\public.pdb", pdbFileName: "public.pdb", guid: "01234567-89ab-cdef-0123-456789abcdef", age: 10, @@ -63,8 +63,8 @@ it.each(["file", "guid", "age"] as const)( "retains declared identity when only reported %s is omitted", (attribute) => { const pdb: GhidraSessionInfo["pdb"] = { - file: "program.pdb", - guid: "reported-guid", + file: "public.pdb", + guid: "0123456789abcdef0123456789abcdef", age: "a", signature: null, version: "RSDS", @@ -132,27 +132,31 @@ it.each([ { file: "public.pdb", guid: "unrecognized-guid", age: "a" }, { file: "public.pdb", guid: "0123456789abcdef0123456789abcdef", age: "ten" }, ])("retains non-equivalent declared PDB identity for %j", (identity) => { - for (const loaded of [false, null, true]) { - const pdb = { ...absentAttributes, ...identity, version: "RSDS", loaded }; - const caveats = ghidraPePdbLimitations(target, pdb); - expect(caveats[0]).toBe(ghidraPdbLimitations(pdb)[0]); - expect(caveats[0]).toContain(identity.file); - expect(caveats[0]).toContain(identity.guid); - expect(caveats[0]).toContain(`age (hex) ${identity.age}`); - expect(caveats).toHaveLength(2); - expect(caveats[1]).toContain('PDB "public.pdb"'); - expect(caveats[1]).toContain("01234567-89ab-cdef-0123-456789abcdef"); - expect(caveats[1]).toContain("age (decimal) 10"); - expect(caveats[1]).toContain("equivalence is not established"); - expect(pdb).toMatchObject({ ...identity, loaded }); - } + const pdb = { + ...absentAttributes, + ...identity, + version: "RSDS", + loaded: true, + }; + const caveats = ghidraPePdbLimitations(target, pdb); + expect(caveats).toEqual([ + ...ghidraPdbLimitations(pdb), + expect.stringContaining('PDB "public.pdb"'), + ]); + expect(caveats[0]).toContain(identity.file); + expect(caveats[0]).toContain(identity.guid); + expect(caveats[0]).toContain(`age (hex) ${identity.age}`); + expect(caveats[1]).toContain("01234567-89ab-cdef-0123-456789abcdef"); + expect(caveats[1]).toContain("age (decimal) 10"); + expect(caveats[1]).toContain("equivalence is not established"); + expect(pdb).toMatchObject({ ...identity, loaded: true }); }); it.each([ - "0123456789ABCDEF0123456789ABCDEF", - "01234567-89ab-cdef-0123-456789abcdef", -])("avoids duplicate identity for normalized matching GUID %s", (guid) => { - const pdb = { ...absentAttributes, file: "public.pdb", guid, age: "a" }; + ["public.pdb", "0123456789ABCDEF0123456789ABCDEF"], + ["C:\\symbols\\public.pdb", "01234567-89ab-cdef-0123-456789abcdef"], +])("recognizes the declared locator %s and normalized GUID", (file, guid) => { + const pdb = { ...absentAttributes, file, guid, age: "a" }; expect(ghidraPePdbLimitations(target, pdb)).toEqual( ghidraPdbLimitations(pdb), ); @@ -209,42 +213,39 @@ it("reports NB10 declared age without inventing a GUID", () => { expect(caveats[0]).not.toContain("GUID"); }); -it.each([false, null, true])( - "keeps an omitted NB10 GUID unknown with loaded = %s", - (loaded) => { - const pdbPath = "a".repeat(4070) + "\\legacy.pdb"; - const legacy = { - ...target, - codeView: { - pdbPath, - pdbFileName: "legacy.pdb", - guid: null, - age: 2, - }, - }; - const pdb = { - ...absentAttributes, - file: pdbPath, - age: "2", - signature: "01234567", - version: "NB10", - loaded, - }; - expect(ghidraPePdbLimitations(legacy, pdb)).toEqual( - ghidraPdbLimitations(pdb), - ); - const omitted: GhidraSessionInfo["pdb"] = { - ...pdb, - resource_limit: { omitted_attributes: ["guid"], utf16_budget: 4096 }, - }; - const caveats = ghidraPePdbLimitations(legacy, omitted); - expect(caveats).toEqual([ - ...ghidraPdbLimitations(omitted), - expect.stringContaining('PDB "legacy.pdb" (age (decimal) 2)'), - ]); - expect(caveats.at(-1)).toContain("equivalence is not established"); - }, -); +it("keeps a budget-omitted NB10 GUID unknown", () => { + const pdbPath = "a".repeat(4070) + "\\legacy.pdb"; + const legacy = { + ...target, + codeView: { + pdbPath, + pdbFileName: "legacy.pdb", + guid: null, + age: 2, + }, + }; + const pdb = { + ...absentAttributes, + file: pdbPath, + age: "2", + signature: "01234567", + version: "NB10", + loaded: null, + }; + expect(ghidraPePdbLimitations(legacy, pdb)).toEqual( + ghidraPdbLimitations(pdb), + ); + const omitted: GhidraSessionInfo["pdb"] = { + ...pdb, + resource_limit: { omitted_attributes: ["guid"], utf16_budget: 4096 }, + }; + const caveats = ghidraPePdbLimitations(legacy, omitted); + expect(caveats).toEqual([ + ...ghidraPdbLimitations(omitted), + expect.stringContaining('PDB "legacy.pdb" (age (decimal) 2)'), + ]); + expect(caveats.at(-1)).toContain("equivalence is not established"); +}); it.each(["signature", "version"] as const)( "keeps matching NB10 identity when only %s is omitted", @@ -293,50 +294,27 @@ it("adds no PDB warning for a non-PE target", () => { expect(ghidraPePdbLimitations(elf, undefined)).toEqual([]); }); -it("bounds an escaped inline locator while preserving its declared source value", () => { - const name = '"'.repeat(5000) + ".pdb"; - const selected = { - ...target, - codeView: { ...target.codeView!, pdbFileName: name }, - }; - const caveats = ghidraPePdbLimitations(selected, undefined).join(" "); - expect(Buffer.byteLength(caveats)).toBeLessThan(1024); - expect(caveats).toContain("locator omitted"); - expect(caveats).toContain("01234567-89ab-cdef-0123-456789abcdef"); - expect(selected.codeView.pdbFileName).toBe(name); -}); - it.each([ - ["long ASCII", "a".repeat(171)], + ["ASCII beyond the conservative shortcut", "a".repeat(171)], ["ASCII at the byte budget", "a".repeat(1022)], ["ASCII beyond the byte budget", "a".repeat(1023)], ["quotes at the byte budget", '"'.repeat(511)], ["quotes beyond the byte budget", '"'.repeat(512)], - ["short control escapes", "\n".repeat(511)], - ["short control escapes beyond the byte budget", "\n".repeat(512)], - ["six-byte control escapes", "\u0001".repeat(170)], - ["six-byte control escapes beyond the byte budget", "\u0001".repeat(171)], - ["UTF-8 BMP text", "中".repeat(340)], - ["UTF-8 BMP text beyond the byte budget", "中".repeat(341)], - ["surrogate pairs", "😀".repeat(255)], - ["surrogate pairs beyond the byte budget", "😀".repeat(256)], - ["unpaired high surrogates", "\ud800".repeat(170)], - ["unpaired high surrogates beyond the byte budget", "\ud800".repeat(171)], - ["unpaired low surrogates", "\udfff".repeat(170)], - ["unpaired low surrogates beyond the byte budget", "\udfff".repeat(171)], + ["multibyte text beyond the byte budget", "中".repeat(341)], ])("uses actual JSON UTF-8 size for %s", (_, name) => { const selected = { ...target, - codeView: { ...target.codeView!, pdbFileName: name }, + codeView: { ...target.codeView, pdbFileName: name }, }; const caveat = ghidraPePdbLimitations(selected, undefined).join(" "); const encoded = JSON.stringify(name); if (Buffer.byteLength(encoded) <= 1024) { expect(caveat).toContain(`PDB ${encoded} (`); - expect(caveat).not.toContain("locator omitted"); } else { - expect(caveat).toContain("locator omitted"); + expect(caveat).toContain("1024-byte resource budget"); expect(caveat).not.toContain(encoded); + expect(Buffer.byteLength(caveat)).toBeLessThan(1024); } + expect(caveat).toContain("01234567-89ab-cdef-0123-456789abcdef"); expect(selected.codeView.pdbFileName).toBe(name); }); diff --git a/src/ghidra/GhidraProvider.behavior.test.ts b/src/ghidra/GhidraProvider.behavior.test.ts index 8bd6ddfdcd..e34c735da4 100644 --- a/src/ghidra/GhidraProvider.behavior.test.ts +++ b/src/ghidra/GhidraProvider.behavior.test.ts @@ -113,6 +113,7 @@ import { GHIDRA_MUTATING_OPERATIONS, GHIDRA_SESSION_CAPABILITIES, type GhidraSessionInfo, + ghidraPdbLimitations, } from "./GhidraSessionValues.js"; import type { Result } from "../domain/result.js"; import { GhidraSessionError } from "./GhidraSessionError.js"; @@ -4222,8 +4223,15 @@ describe("Ghidra containing-procedure absence", () => { }); describe("Ghidra PDB coverage", () => { + const reference = { + file: "C:\\build\\fixture.pdb", + guid: "01234567-89AB-CDEF-0123-456789ABCDEF", + age: "a", + signature: null, + version: "RSDS", + }; it.each([ - { name: "missing bridge report", pdb: undefined, warns: true }, + { name: "missing bridge report", pdb: undefined }, { name: "report without a PDB reference", pdb: { @@ -4234,59 +4242,19 @@ describe("Ghidra PDB coverage", () => { version: null, loaded: null, }, - warns: false, }, - ])("distinguishes $name in direct PE searches", async ({ pdb, warns }) => { - const ghidra = provider(installationHost(), () => ({ - start: () => - Promise.resolve( - ok(pdb === undefined ? sessionInfo() : { ...sessionInfo(), pdb }), - ), - callTool: () => Promise.resolve(ok([])), - close: () => Promise.resolve(ok(null)), - })); - const target = executableTarget("pe", "x86_64"); - const resolved = await ghidra.resolveAnalysisProfile(target); - if (!resolved.ok || resolved.value.profile === null) - throw new Error("Expected a PE profile"); - const client = ghidra.createClient(target, resolved.value.profile); - try { - const searched = await client.execute("search_procedures", { - pattern: "add_numbers", - }); - if (!searched.ok) throw searched.error; - expect(searched.value.result).toEqual([]); - const warnings = searched.value.limitations.filter((limitation) => - limitation.includes("PDB"), - ); - expect(warnings).toEqual( - warns - ? [ - "Ghidra did not report PDB attributes; debug-symbol coverage is unknown.", - ] - : [], - ); - } finally { - await client.close(); - } - }); - const pdb = { - file: "C:\\build\\fixture.pdb", - guid: "01234567-89AB-CDEF-0123-456789ABCDEF", - age: "a", - signature: null, - version: "RSDS", - loaded: false as boolean | null, - }; - it.each([false, null, true])( - "carries PDB load state %s into direct search and health Evidence", - async (loaded) => { + { name: "refused load", pdb: { ...reference, loaded: false } }, + { name: "unknown load", pdb: { ...reference, loaded: null } }, + { name: "reported load", pdb: { ...reference, loaded: true } }, + ])( + "carries $name into a direct PE search and subsequent health", + async ({ pdb }) => { let starts = 0; const ghidra = provider(installationHost(), () => ({ start: () => { starts += 1; return Promise.resolve( - ok({ ...sessionInfo(), pdb: { ...pdb, loaded } }), + ok(pdb === undefined ? sessionInfo() : { ...sessionInfo(), pdb }), ); }, callTool: () => Promise.resolve(ok([])), @@ -4297,33 +4265,27 @@ describe("Ghidra PDB coverage", () => { if (!resolved.ok || resolved.value.profile === null) throw new Error("Expected a PE profile"); const client = ghidra.createClient(target, resolved.value.profile); - // No preceding health call: a name search must establish symbol coverage itself. - const searched = await client.execute("search_procedures", { - pattern: "add_numbers", - }); - expect(starts).toBe(1); - if (!searched.ok) throw searched.error; - expect(searched.value.result).toEqual([]); - const health = await client.execute("health", {}); - if (!health.ok) throw health.error; - expect(health.value.result).toMatchObject({ pdb: { ...pdb, loaded } }); - for (const observation of [searched.value, health.value]) { - const limitations = observation.limitations.join("\n"); - if (loaded === true) { - expect(limitations).toContain("loaded PDB"); - expect(limitations).toContain("not an independent PDB identity"); - expect(limitations).toContain("empty name search does not prove"); - } else { - expect(limitations).toContain(pdb.file); - expect(limitations).toContain(pdb.guid); - expect(limitations).toContain("age (hex) a"); - expect(limitations).toContain("list_procedures"); - expect(limitations).toContain( - loaded === null ? "unknown" : "not loaded", - ); + try { + const searched = await client.execute("search_procedures", { + pattern: "add_numbers", + }); + if (!searched.ok) throw searched.error; + expect(starts).toBe(1); + expect(searched.value.result).toEqual([]); + const health = await client.execute("health", {}); + if (!health.ok) throw health.error; + if (pdb === undefined) + expect(health.value.result).not.toHaveProperty("pdb"); + else expect(health.value.result).toHaveProperty("pdb", pdb); + const expected = ghidraPdbLimitations(pdb); + for (const observation of [searched.value, health.value]) { + expect( + observation.limitations.filter((item) => item.includes("PDB")), + ).toEqual(expected); } + } finally { + await client.close(); } - await client.close(); }, ); });