Compare declared CodeView identity with recognized Program GUID and hexadecimal age formats. Preserve both sources when fields conflict or equivalence cannot be established. Keep known Program identity alongside loaded-state attestation caveats at the projection owner, and revise PE snapshot coverage to program-info-v4. Validation: 246 associated tests across 15 suites, runtime build, typecheck and changed-file lint pass; two Windows-only cases skip on Linux. Five conflict/unknown-format cases reproduce before the comparison fix; their loaded-state variants also expose the original Program projection dropping identity fields.
110 lines
4.0 KiB
TypeScript
110 lines
4.0 KiB
TypeScript
import type {
|
|
BinaryTarget,
|
|
PeCodeViewRecord,
|
|
} from "../domain/binaryTargetTypes.js";
|
|
import {
|
|
ghidraPdbLimitations,
|
|
type GhidraSessionInfo,
|
|
} from "./GhidraSessionValues.js";
|
|
|
|
const PDB_LOCATOR_INLINE_BYTE_BUDGET = 1024;
|
|
|
|
/** Establish equivalence only for the declared fields and recognized formats. */
|
|
const reportedPdbMatchesDeclaration = (
|
|
declared: PeCodeViewRecord,
|
|
reported: NonNullable<GhidraSessionInfo["pdb"]>,
|
|
): boolean => {
|
|
if (
|
|
(reported.file !== declared.pdbPath &&
|
|
reported.file !== declared.pdbFileName) ||
|
|
reported.age === null ||
|
|
!/^(?:0x)?[0-9a-f]+$/iu.test(reported.age) ||
|
|
Number.parseInt(reported.age, 16) !== declared.age
|
|
)
|
|
return false;
|
|
if (declared.guid === null) return reported.guid === null;
|
|
if (reported.guid === null) return false;
|
|
let guid = reported.guid;
|
|
if (guid.startsWith("{") && guid.endsWith("}")) guid = guid.slice(1, -1);
|
|
if (
|
|
!/^(?:[0-9a-f]{32}|[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12})$/iu.test(
|
|
guid,
|
|
)
|
|
)
|
|
return false;
|
|
return (
|
|
guid.replaceAll("-", "").toLowerCase() ===
|
|
declared.guid.replaceAll("-", "").toLowerCase()
|
|
);
|
|
};
|
|
|
|
/** Measure JSON quotes, escapes and UTF-8 without allocating the encoded string. */
|
|
const pdbLocatorFitsInlineBudget = (value: string): boolean => {
|
|
if (value.length * 6 + 2 <= PDB_LOCATOR_INLINE_BYTE_BUDGET) return true;
|
|
if (value.length + 2 > PDB_LOCATOR_INLINE_BYTE_BUDGET) return false;
|
|
let bytes = 2;
|
|
for (let index = 0; index < value.length; index += 1) {
|
|
const point = value.codePointAt(index) ?? 0;
|
|
if (point > 0xffff) {
|
|
bytes += 4;
|
|
index += 1;
|
|
} else if (point >= 0xd800 && point <= 0xdfff) {
|
|
// JSON.stringify escapes unpaired UTF-16 surrogates as six ASCII bytes.
|
|
bytes += 6;
|
|
} else if (point < 0x20) {
|
|
bytes +=
|
|
point === 8 ||
|
|
point === 9 ||
|
|
point === 10 ||
|
|
point === 12 ||
|
|
point === 13
|
|
? 2
|
|
: 6;
|
|
} else if (point === 0x22 || point === 0x5c) {
|
|
bytes += 2;
|
|
} else {
|
|
bytes += point < 0x80 ? 1 : point < 0x800 ? 2 : 3;
|
|
}
|
|
if (bytes > PDB_LOCATOR_INLINE_BYTE_BUDGET) return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
/** Use reported Program facts first, retaining declared PE facts as a fallback. */
|
|
export const ghidraPePdbLimitations = (
|
|
target: BinaryTarget,
|
|
pdb: GhidraSessionInfo["pdb"],
|
|
): readonly string[] => {
|
|
if (target.format !== "pe") return [];
|
|
const reported = ghidraPdbLimitations(pdb);
|
|
if (
|
|
target.codeView === undefined ||
|
|
(pdb !== undefined && reportedPdbMatchesDeclaration(target.codeView, pdb))
|
|
)
|
|
return reported;
|
|
const { pdbFileName, guid, age } = target.codeView;
|
|
// Include JSON escaping in the inline budget before allocating that representation.
|
|
const locator = pdbLocatorFitsInlineBudget(pdbFileName)
|
|
? JSON.stringify(pdbFileName)
|
|
: `(declared locator omitted from this inline caveat: ${PDB_LOCATOR_INLINE_BYTE_BUDGET}-byte resource budget)`;
|
|
const identity =
|
|
guid === null
|
|
? `age (decimal) ${age}`
|
|
: `GUID ${guid}, age (decimal) ${age}`;
|
|
const state =
|
|
pdb === undefined
|
|
? "Ghidra did not report PDB attributes; load state unknown"
|
|
: pdb.loaded === false
|
|
? "Ghidra reports not loaded"
|
|
: pdb.loaded === true
|
|
? "Ghidra reports a loaded PDB; this declared identity is not an independent matching-PDB attestation"
|
|
: "Ghidra reports load state unknown";
|
|
const comparison =
|
|
pdb !== undefined &&
|
|
[pdb.file, pdb.guid, pdb.age].some((value) => value !== null)
|
|
? " Reported and declared PDB observations are retained separately; equivalence is not established."
|
|
: "";
|
|
const declared = `The PE CodeView record references PDB ${locator} (${identity}); ${state}.${comparison} Original PDB names and types may be missing. Use list_procedures and address-based analysis to inspect recovered functions. REA does not admit a caller-selected PDB companion.`;
|
|
return pdb === undefined ? [declared] : [...reported, declared];
|
|
};
|