diff --git a/src/native/pe/PeImportParser.ts b/src/native/pe/PeImportParser.ts index 5fdcc66931..d628b1ec94 100644 --- a/src/native/pe/PeImportParser.ts +++ b/src/native/pe/PeImportParser.ts @@ -294,7 +294,9 @@ export const parsePeImports = async ( } const hintNameRva = Number(thunk); try { - const hintAt = map(hintNameRva, 3); + // The two-byte hint and the separately mapped name may occupy adjacent + // RVA sections with different file translations. + const hintAt = map(hintNameRva, 2); const hint = reader.u16(hintAt); const name = cString(hintNameRva + 2); retain(name); diff --git a/tests/acceptance/peImportHintBoundaryCli.test.ts b/tests/acceptance/peImportHintBoundaryCli.test.ts new file mode 100644 index 0000000000..3f20d0abef --- /dev/null +++ b/tests/acceptance/peImportHintBoundaryCli.test.ts @@ -0,0 +1,69 @@ +import { createHash } from "node:crypto"; +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect } from "vitest"; +import { BINARY_DIAGNOSTICS_TOOL_CONTRACTS } from "../../src/contracts/native/binaryDiagnosticsToolContracts.js"; +import { peImportHintBoundaryImage } from "../fixtures/binaryDiagnostics/peImports.js"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; +import { cliTest } from "../support/cli/cliFixture.js"; + +const contract = BINARY_DIAGNOSTICS_TOOL_CONTRACTS.find( + (item) => item.name === "inspect_pe_imports", +); +if (contract === undefined) + throw new Error("Missing inspect_pe_imports contract"); + +const cases = ([32, 64] as const).flatMap((bits) => + (["standard", "delay"] as const).flatMap((kind) => + (["none", "hint", "name"] as const).map((gap) => ({ bits, kind, gap })), + ), +); + +cliTest.for(cases)( + "reads a PE$bits $kind hint/name section boundary only with both fields mapped (gap: $gap)", + async ({ bits, kind, gap }, { cli }) => { + const root = await createTestTempDirectory("rea-pe-hint-boundary-"); + const path = join(root, "imports.dll"); + const bytes = peImportHintBoundaryImage(bits, kind, gap); + await writeFile(path, bytes); + const response = await cli.run({ + arguments: ["inspect-pe-imports", path, "--json"], + }); + expect(response.exitCode, response.stderr).toBe(0); + const report = contract.outputSchema.parse(response.json).normalized_result; + expect(report.artifact.sha256).toBe( + createHash("sha256").update(bytes).digest("hex"), + ); + expect(report.bits).toBe(bits); + const rows = kind === "standard" ? report.imports : report.delay_imports; + expect(rows).toHaveLength(1); + expect(rows[0]?.name).toBe("KERNEL32.dll"); + if (gap === "none") { + expect(rows[0]?.functions).toEqual([ + expect.objectContaining({ + name: "GetProcAddress", + hint: 7, + ordinal: null, + thunk_hex: "1ffe", + }), + ]); + expect(report.coverage.status).toBe("complete"); + expect(report.issues).toEqual([]); + } else { + expect(rows[0]?.functions).toEqual([]); + expect(report.coverage.status).toBe("partial"); + expect(report.issues).toContainEqual( + expect.objectContaining({ + code: "name_unreadable", + location: "0x1ffe", + }), + ); + } + // Independently decoded with pefile 2024.8.26 on these same fixture bytes. + expect(report.imphash.md5).toBe( + gap === "none" && kind === "standard" + ? "7a7803027531302026dedc7b5f6025f2" + : null, + ); + }, +); diff --git a/tests/fixtures/binaryDiagnostics/peImports.ts b/tests/fixtures/binaryDiagnostics/peImports.ts index 5c775b72d4..d09ff99295 100644 --- a/tests/fixtures/binaryDiagnostics/peImports.ts +++ b/tests/fixtures/binaryDiagnostics/peImports.ts @@ -170,6 +170,50 @@ export const peImportImage = ( return split; }; +/** A two-byte hint ends one section; its name begins at the next RVA in another file span. */ +export const peImportHintBoundaryImage = ( + bits: 32 | 64, + kind: "standard" | "delay", + gap: "none" | "hint" | "name" = "none", +): Buffer => { + const dll = { + name: "KERNEL32.dll", + functions: [{ name: "GetProcAddress", hint: 7 }], + }; + const sectionBytes = 0x2000; + const splitAt = 0x1000; + const bytes = peImportImage({ + bits, + sectionBytes, + splitAt, + ...(kind === "standard" ? { dlls: [dll] } : { dlls: [], delayDlls: [dll] }), + }); + const descriptor = peImportFixtureOffset(SECTION_RVA); + const hintRva = SECTION_RVA + splitAt - 2; + const ilt = bytes.readUInt32LE(descriptor + (kind === "standard" ? 0 : 16)); + const iat = bytes.readUInt32LE(descriptor + (kind === "standard" ? 16 : 12)); + for (const table of [ilt, iat]) { + const at = peImportFixtureOffset(table); + if (bits === 64) bytes.writeBigUInt64LE(BigInt(hintRva), at); + else bytes.writeUInt32LE(hintRva, at); + } + bytes.writeUInt16LE(7, peImportFixtureOffset(hintRva)); + bytes.write( + "GetProcAddress\0", + SECTION_OFFSET + sectionBytes + SPLIT_GAP_BYTES, + "ascii", + ); + // Aligned PE section layout; a DLL need not declare an entry point. + bytes.writeUInt16LE(0x2002, 150); + bytes.writeUInt32LE(0x1000, 152 + 32); + bytes.writeUInt32LE(0x200, 152 + 36); + bytes.writeUInt32LE(gap === "name" ? 0x4000 : 0x3000, 152 + 56); + const section = 152 + (bits === 64 ? 112 : 96) + 16 * 8; + if (gap === "hint") bytes.writeUInt32LE(splitAt - 1, section + 16); + if (gap === "name") bytes.writeUInt32LE(0x3000, section + 40 + 12); + return bytes; +}; + /** PE image whose optional header admits no import data directory. */ export const peWithoutImportTableFixture = (bits: 32 | 64 = 64): Buffer => peImportImage({ bits, importDirectory: false, delayDlls: [] });