refactor(domain): charge remaining fixed allowances through Budget (#11319)

* refactor(domain): charge the Interface Builder, NativeAOT, managed and runtime budgets through Budget

* test(browser): show a refused runtime retention charge leaves the room intact

* refactor(domain): charge the remaining fixed allowances through Budget (#11383)

* refactor(domain): charge CUDA metadata and SQLite reply budgets through Budget

CudaMetadataBudget (visits, section reads, temporary index) and the SQLite
OutputBudget kept hand-written allowances whose field names escaped the
budget-arithmetic guard; the SQLite reply budget also charged each part
before checking it. Both now charge Budget, which refuses an oversized
charge without changing the remainder.

The guard now also counts, inside any hand-written ...Budget class, a field
that is adjusted by hand and compared with < or >, whatever its name.
Unchecked tallies (a yield counter, a reservation ledger) stay uncounted.
Firmware arena cursors are write offsets, not allowances: they are renamed
#arenaWriteOffset and leave the guard's scope. Baseline 17 -> 15.

* refactor(domain): move cache, re-priced, checkpointed and ledger retention onto Budget

Budget gains chargeEvicting (evict the oldest cache entries until a charge
fits) and restore (refund everything charged since a used mark, never
re-charging). With them:

- the Bun metadata range cache evicts through chargeEvicting and charges
  physical reads through a second Budget;
- Playwright scenario events refund an event's old cost and try-charge its
  re-priced cost, and admission charges with tryCharge;
- WebNetworkProjectionAccumulator keeps one shared allowance plus retained
  and temporary shares whose checkpoints restore by refunding;
- EvidenceLedger commits its admitted delta (charge, or refund when a
  replacement shrinks) and clears with restore(0);
- the web bundle worker charges an admitted result, and on the completed-
  snapshot path retains facts only when they still fit;
- the Pyc nested code walk charges steps and retained references.

budget-arithmetic 15 -> 4 (JavaScriptAnalysisWorker only).

* build(verify): declare the budget-arithmetic guard's counter for typed tests
This commit is contained in:
morluto authored and GitHub committed 2026-11-16 03:08:28 +08:00
1 parent aba42b47b9
commit 64c119df47
30 files changed
+593 -346

No files matched your search

+33 -27
View File
@@ -25,6 +25,7 @@ import {
type PythonPycCodeObject,
type PythonPycConst,
} from "../domain/python/pythonPyc.js";
import { Budget } from "../domain/budget.js";
import { malformedInput, unsupportedInput } from "../domain/parseFailure.js";
import { BinaryReader } from "../native/metadata/BinaryContainer.js";
import {
@@ -342,8 +343,9 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
* bound. Absent while a container is still on the explicit walk stack.
*/
readonly #nestedCache = new WeakMap<MarshalValue, readonly CodeNode[]>();
#nestedSteps = 0;
#nestedRetained = 0;
/** Walk steps and retained code references share max_objects * max_depth. */
readonly #nestedSteps: Budget;
readonly #nestedRetained: Budget;
constructor(
bytes: Buffer,
@@ -364,6 +366,8 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
previewLimit: PYTHON_PYC_CONST_PREVIEW_LIMIT,
publishContainers: "before-items",
});
this.#nestedSteps = new Budget(input.max_objects * input.max_depth);
this.#nestedRetained = new Budget(input.max_objects * input.max_depth);
}
protected wrap(value: MarshalCoreValue<MarshalValue>): MarshalValue {
@@ -756,7 +760,7 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
if (this.#memoizeNested(value, added)) {
for (const entry of added) {
this.#nestedCache.delete(entry.value);
this.#nestedRetained -= entry.retained;
this.#nestedRetained.refund(entry.retained);
}
const walked = this.#walkNested(value);
this.#nestedCache.set(value, walked);
@@ -801,12 +805,12 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
});
continue;
}
const retainedBefore = this.#nestedRetained;
const retainedBefore = this.#nestedRetained.used;
const codes = this.#combineNested(frame.children);
this.#nestedCache.set(frame.value, codes);
added.push({
value: frame.value,
retained: this.#nestedRetained - retainedBefore,
retained: this.#nestedRetained.used - retainedBefore,
});
gray.delete(frame.value);
frames.pop();
@@ -882,31 +886,33 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
}
#takeNestedStep(): void {
this.#nestedSteps += 1;
const limit = this.input.max_objects * this.input.max_depth;
if (this.#nestedSteps > limit)
throw new PythonPycResourceFailure(
"objects",
{
max_objects: this.input.max_objects,
max_depth: this.input.max_depth,
},
`Nested code walk exceeds ${limit} steps (max_objects * max_depth).`,
);
this.#nestedSteps.charge(
1,
() =>
new PythonPycResourceFailure(
"objects",
{
max_objects: this.input.max_objects,
max_depth: this.input.max_depth,
},
`Nested code walk exceeds ${String(this.#nestedSteps.maximum)} steps (max_objects * max_depth).`,
),
);
}
#retainNested(codes: readonly CodeNode[]): void {
this.#nestedRetained += codes.length;
const limit = this.input.max_objects * this.input.max_depth;
if (this.#nestedRetained > limit)
throw new PythonPycResourceFailure(
"objects",
{
max_objects: this.input.max_objects,
max_depth: this.input.max_depth,
},
`Nested code walk retains more than ${limit} code references.`,
);
this.#nestedRetained.charge(
codes.length,
() =>
new PythonPycResourceFailure(
"objects",
{
max_objects: this.input.max_objects,
max_depth: this.input.max_depth,
},
`Nested code walk retains more than ${String(this.#nestedRetained.maximum)} code references.`,
),
);
}
/**