diff --git a/src/native/elf/KernelModuleParser.test.ts b/src/native/elf/KernelModuleParser.test.ts index cbe02623e9..f1b370603a 100644 --- a/src/native/elf/KernelModuleParser.test.ts +++ b/src/native/elf/KernelModuleParser.test.ts @@ -447,10 +447,10 @@ it("charges many sections sharing one huge __ksymtab name to the entry budget", const bytes = kernelModuleFixture({ sharedNameSections: { name, count: 8192 }, }); - const started = performance.now(); expect(() => parse(bytes)).toThrow(ArtifactReaderFailure); + // The shared name decodes once; per-section decoding exceeds the 16 MiB + // section-name budget and refuses this module. const report = parse(bytes, { max_entries: 65_536 }); - expect(performance.now() - started).toBeLessThan(5_000); const huge = report.ksymtab?.sections.slice(1) ?? []; expect(huge).toHaveLength(8192); expect( diff --git a/src/native/initializers/NativeInitializersParsers.test.ts b/src/native/initializers/NativeInitializersParsers.test.ts index 6329cd821b..edd8286199 100644 --- a/src/native/initializers/NativeInitializersParsers.test.ts +++ b/src/native/initializers/NativeInitializersParsers.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it, vi } from "vitest"; +import { describe, expect, it, onTestFinished, vi } from "vitest"; import { deflateSync } from "node:zlib"; import { readNativeInitializers } from "./NativeInitializersProvider.js"; @@ -6,6 +6,7 @@ import { readNativeInitializers } from "./NativeInitializersProvider.js"; const mapping = vi.hoisted(() => ({ scans: vi.fn(), indexes: vi.fn(), + lookups: { count: 0 }, })); vi.mock("../../domain/binaryContainer.js", async (importOriginal) => { const actual = @@ -22,10 +23,51 @@ vi.mock("../../domain/binaryContainer.js", async (importOriginal) => { ...args: Parameters ) => { mapping.indexes(...args); - return actual.createFileMappingIndex(...args); + const index = actual.createFileMappingIndex(...args); + return { + ...index, + offset: (...lookup: Parameters) => { + mapping.lookups.count++; + return index.offset(...lookup); + }, + }; }, }; }); +/** Element reads of every parsed ELF section table, so tests can count section walks. */ +const sectionReads = vi.hoisted(() => ({ count: 0, budget: Infinity })); +vi.mock( + "../../domain/native/nativeSectionDirectory.js", + async (importOriginal) => { + const actual = + await importOriginal< + typeof import("../../domain/native/nativeSectionDirectory.js") + >(); + return { + ...actual, + readElfDirectoryFromBuffer: ( + ...args: Parameters + ) => { + const directory = actual.readElfDirectoryFromBuffer(...args); + return { + ...directory, + sections: new Proxy(directory.sections, { + get(target, key, receiver) { + // Past the budget a walk fails at once instead of finishing. + if ( + typeof key === "string" && + /^\d+$/u.test(key) && + ++sectionReads.count > sectionReads.budget + ) + throw new Error("Section table read budget exceeded"); + return Reflect.get(target, key, receiver) as unknown; + }, + }), + }; + }, + }; + }, +); import { elf32RelativeInitializers, elf64PieInitializers, @@ -538,9 +580,13 @@ describe("ELF incomplete inputs", () => { const bytes = elf64PieInitializers(); // DT_INIT_ARRAYSZ is the fourth dynamic entry; 2^50 slots is still a safe integer. bytes.writeBigUInt64LE(8n << 50n, 0x300 + 3 * 16 + 8); - const started = performance.now(); + mapping.lookups.count = 0; const report = await read(bytes); - expect(performance.now() - started).toBeLessThan(10_000); + // Each admitted slot is located once; the rest of the image adds a few + // hundred lookups (66194 here). An unclamped slot loop locates every slot. + expect(mapping.lookups.count).toBeLessThan( + MAX_NATIVE_INITIALIZER_ENTRIES + 1024, + ); expect(report.status).toBe("incomplete"); expect(report.coverage).toMatchObject({ status: "partial", @@ -950,10 +996,15 @@ describe("ELF section lookups", () => { bytes.writeBigUInt64LE(BigInt(bytes.length), 64 + 40); bytes.writeBigUInt64LE(BigInt(base.length), 0x300 + 2 * 16 + 8); bytes.writeBigUInt64LE(BigInt(slots * 8), 0x300 + 3 * 16 + 8); - const started = performance.now(); + // Each consumer walks the table once (about 6 x 65535 reads); a + // per-lookup walk would read it once per slot. + sectionReads.count = 0; + sectionReads.budget = 12 * 65535; + onTestFinished(() => { + sectionReads.budget = Infinity; + }); const report = await read(bytes); - // A per-lookup header walk is about 4e9 comparisons here; the index is a few hundred ms. - expect(performance.now() - started).toBeLessThan(10_000); + expect(sectionReads.count).toBeLessThanOrEqual(sectionReads.budget); expect(report.coverage.examined_entries).toBe(slots); const slot = report.entries.find((entry) => entry.kind === "init_array"); expect(slot).toMatchObject({ diff --git a/src/native/pe/PeLoadConfigParser.test.ts b/src/native/pe/PeLoadConfigParser.test.ts index 60c9002130..d7bf1de97f 100644 --- a/src/native/pe/PeLoadConfigParser.test.ts +++ b/src/native/pe/PeLoadConfigParser.test.ts @@ -1,5 +1,5 @@ import { ArtifactReaderFailure } from "../../resources/ArtifactReader.js"; -import { describe, expect, it } from "vitest"; +import { describe, expect, it, onTestFinished, vi } from "vitest"; import { inspectPeLoadConfigInputSchema, peLoadConfigSchema, @@ -12,6 +12,37 @@ import { type PeLoadConfigFixtureOptions, } from "./PeLoadConfig.fixture.js"; +/** Element reads of every parsed image's section and mapping tables. */ +const tableReads = vi.hoisted(() => ({ count: 0, budget: Infinity })); +vi.mock("../../domain/native/peImage.js", async (importOriginal) => { + const actual = + await importOriginal(); + const counted = (table: readonly T[]): readonly T[] => + new Proxy(table, { + get(target, key, receiver) { + // Past the budget a walk fails at once instead of finishing. + if ( + typeof key === "string" && + /^\d+$/u.test(key) && + ++tableReads.count > tableReads.budget + ) + throw new Error("Image table read budget exceeded"); + return Reflect.get(target, key, receiver) as unknown; + }, + }); + return { + ...actual, + readPeImage: (bytes: Buffer) => { + const image = actual.readPeImage(bytes); + return { + ...image, + sections: counted(image.sections), + mappings: counted(image.mappings), + }; + }, + }; +}); + const parse = ( options: PeLoadConfigFixtureOptions = {}, max_callbacks = 64, @@ -177,9 +208,15 @@ it("retains a callback in virtual-only section storage with unknown file offset" ).toMatchObject({ rva: 0x2200, file_offset: null, section: ".text" }); }); -it("resolves callback sections in bounded time for the largest section table", async () => { +it("resolves callback sections without rescanning the largest section table", async () => { const bytes = peManySectionsImage(65535, 4096); - const started = performance.now(); + // Building the section index reads each entry about twice (131071 here); a + // per-callback scan would read about 4096 x 65535 entries. + tableReads.count = 0; + tableReads.budget = 4 * 65535; + onTestFinished(() => { + tableReads.budget = Infinity; + }); const report = await parsePeLoadConfig( bytes, { path: "/sample.exe", sha256: "a".repeat(64), bytes: bytes.length }, @@ -188,8 +225,7 @@ it("resolves callback sections in bounded time for the largest section table", a max_callbacks: 4096, }), ); - // A per-callback scan of every section takes several seconds here. - expect(performance.now() - started).toBeLessThan(1_000); + expect(tableReads.count).toBeLessThanOrEqual(tableReads.budget); expect(report.tls?.callbacks).toHaveLength(4096); expect(report.tls?.callbacks.every(({ section }) => section === ".v")).toBe( true,