diff --git a/src/domain/analysisView/environmentStateWriteCopies.test.ts b/src/domain/analysisView/environmentStateWriteCopies.test.ts index 6720c41f1f..70b0d8792d 100644 --- a/src/domain/analysisView/environmentStateWriteCopies.test.ts +++ b/src/domain/analysisView/environmentStateWriteCopies.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import type { JsonObject, StateEntity } from "./environmentStateRules.js"; import { @@ -99,19 +99,41 @@ it("finds the echoed members a wide body's constants hold without searching each `constant text ${String(index)}`, ]), ); - const started = performance.now(); - const result = create( - { - id: 1, - ...body, - ...constants, - last: `see ${body[`e${String(n - 1)}`]!}`, - }, - body, - () => [], - ); - // Searching every text for every echoed member is quadratic in the body. - expect(performance.now() - started).toBeLessThan(5_000); + // Each text is read once for the members it holds, with a few searches per + // field besides. Searching every text for every echoed member makes n^2 + // searches; the spies stop such a search at the bound instead of letting it + // run on. + const allowed = 16 * n; + let searches = 0; + const searched = (method: "includes" | "indexOf") => { + const original = String.prototype[method] as ( + ...args: unknown[] + ) => unknown; + return vi.spyOn(String.prototype, method).mockImplementation(function ( + this: string, + ...args: unknown[] + ) { + if (++searches > allowed) throw new Error("texts searched per member"); + return original.apply(this, args); + } as never); + }; + const spies = [searched("includes"), searched("indexOf")]; + let result: ReturnType; + try { + result = create( + { + id: 1, + ...body, + ...constants, + last: `see ${body[`e${String(n - 1)}`]!}`, + }, + body, + () => [], + ); + } finally { + for (const spy of spies) spy.mockRestore(); + } + expect(searches).toBeLessThanOrEqual(allowed); expect(result.fixed).toEqual([ { member: `e${String(n - 1)}`, sent: body[`e${String(n - 1)}`] }, ]); diff --git a/src/domain/desktop/appExportUnowned.test.ts b/src/domain/desktop/appExportUnowned.test.ts index 45763d853b..7581154dff 100644 --- a/src/domain/desktop/appExportUnowned.test.ts +++ b/src/domain/desktop/appExportUnowned.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { heldOpenLimitation, @@ -96,7 +96,7 @@ describe("unownedChangedFolders", () => { expect(unownedFoldersLimitation([])).toBeUndefined(); }); - it("collapses tens of thousands of separate folders in time that grows with their number, not its square", () => { + it("collapses tens of thousands of separate folders with work that grows with their number, not its square", () => { const count = 20_000; const files = new Map(); for (let n = 0; n < count; n += 1) @@ -106,17 +106,39 @@ describe("unownedChangedFolders", () => { files.set("Projects/p1-old/notes.txt", file("unrelated")); files.set("Owned/a.txt", file("document")); - const started = performance.now(); - const folders = unownedChangedFolders(files, ["Owned/b"]); - const elapsedMs = performance.now() - started; + // A path is compared with the folders it lies in, by lookup or prefix: + // a few comparisons per path and folder level. Comparing each folder with + // every other makes count^2 of them; the spies stop such a walk at the + // bound instead of letting it run on. + const allowed = 24 * files.size; + let comparisons = 0; + const counted = (original: (...args: never[]) => unknown) => + function (this: unknown, ...args: never[]) { + if (++comparisons > allowed) + throw new Error("folders compared pairwise"); + return original.apply(this, args); + } as never; + const { has } = Set.prototype; + const { startsWith } = String.prototype; + const spies = [ + vi.spyOn(Set.prototype, "has").mockImplementation(counted(has)), + vi + .spyOn(String.prototype, "startsWith") + .mockImplementation(counted(startsWith)), + ]; + let folders: readonly string[]; + try { + folders = unownedChangedFolders(files, ["Owned/b"]); + } finally { + for (const spy of spies) spy.mockRestore(); + } + expect(comparisons).toBeLessThanOrEqual(allowed); expect(folders).toHaveLength(count + 2); expect(folders).toEqual( expect.arrayContaining(["Projects/p1", "Projects/p1-old", "Owned/a.txt"]), ); expect(folders).not.toContain("Projects/p1/sub"); - // Comparing each folder with every other took tens of seconds at this count. - expect(elapsedMs).toBeLessThan(2_000); }); }); diff --git a/src/native/hardening/BinaryHardeningParsers.test.ts b/src/native/hardening/BinaryHardeningParsers.test.ts index 3f7db3d8e1..0f0694f27c 100644 --- a/src/native/hardening/BinaryHardeningParsers.test.ts +++ b/src/native/hardening/BinaryHardeningParsers.test.ts @@ -1089,18 +1089,74 @@ it("leaves GNU property rows unknown behind a compressed note section", () => { }); }); -it( - "admits a hostile section table without rescanning every span", - { tags: ["long-running"] }, - () => { - const started = performance.now(); - const image = readElfHardening( - elfHardeningFixture({ disjointNoteSections: 65000 }), - ); - expect(performance.now() - started).toBeLessThan(5000); - expect(findRow(image.rows, "cet_ibt_shstk").state).toBe("unknown"); - }, -); +/** + * Count the array elements an operation walks, through iteration and the + * element-visiting array methods. Throws once past `allowed`, so a quadratic + * walk stops at the bound instead of running on. The methods are patched + * directly: a spy's own bookkeeping iterates arrays and would count itself. + */ +const countArrayVisits = ( + allowed: number, + run: () => T, +): { readonly result: T; readonly visited: number } => { + const keys: readonly (string | symbol)[] = [ + "every", + "filter", + "find", + "findIndex", + "findLast", + "findLastIndex", + "flatMap", + "forEach", + "includes", + "indexOf", + "map", + "reduce", + "some", + "sort", + "toSorted", + Symbol.iterator, + ]; + const prototype = Array.prototype as unknown as Record< + PropertyKey, + (this: readonly unknown[], ...args: unknown[]) => unknown + >; + const originals = keys.map((key) => [key, prototype[key]!] as const); + let visited = 0; + let counting = true; + for (const [key, original] of originals) + prototype[key] = function (...args) { + if (counting && (visited += this.length) > allowed) { + counting = false; + throw new Error("array elements walked past bound"); + } + return original.apply(this, args); + }; + let result: T; + let walked: number; + try { + result = run(); + } finally { + counting = false; + walked = visited; + for (const [key, original] of originals) prototype[key] = original; + } + return { result, visited: walked }; +}; + +it("admits a hostile section table without rescanning every span", () => { + // Each section header is walked a fixed number of times while sections are + // parsed, sorted and indexed. Rescanning the note spans for every span + // walks sections^2 elements. + const sections = 65_000; + const bytes = elfHardeningFixture({ disjointNoteSections: sections }); + const allowed = 64 * sections; + const { result: image, visited } = countArrayVisits(allowed, () => + readElfHardening(bytes), + ); + expect(visited).toBeLessThanOrEqual(allowed); + expect(findRow(image.rows, "cet_ibt_shstk").state).toBe("unknown"); +}); it("cites a non-ASCII import name by its source bytes", () => { const name = "__\u00ff_chk";