mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 03:33:25 +00:00
test: bound folder collapse, echo search and ELF section admission by work, not elapsed time (#10783)
* test(domain): bound folder collapse and echo search by work, not elapsed time Two quadratic-regression tests in src/domain asserted elapsed time. Count the comparisons and text searches instead, failing at a linear bound so a pairwise or per-member search stops early instead of running on. Refs #10513 * test(native): bound hostile ELF section admission by array work, not time The 65,000-section case asserted it finished within 5 s, which a loaded runner can miss and a fast one can pass while quadratic. Count array elements walked through iteration and the element-visiting methods, and stop at 64 per section; the parser walks about 19. A per-candidate rescan of the admitted spans now fails at the bound.
This commit is contained in:
1 parent
dd7597a108
commit
1b327b7cb9
3 files changed
+133
-33
No files matched your search
@@ -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<typeof create>;
|
||||
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)}`] },
|
||||
]);
|
||||
|
||||
@@ -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<string, DesktopStatePath>();
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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 = <T>(
|
||||
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";
|
||||
|
||||
Reference in new issue
Block a user