mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
fix(javascript): refuse member-mutation work beyond a fixed allowance (#11144)
* fix(javascript): refuse member-mutation work beyond a fixed allowance Member-mutation indexing retained every scheduled alias state until its query finished, with no bound on the phase. Conditional self-selections before one member write (#8294) multiplied those states until the worker heap was exhausted. Charge each scheduled alias state to a per-phase work meter. Past 524,288 states the phase is refused with a resource_constraint that names the fixed work allowance. The application and web bundle workers carry the reported limit to the parent, which rebuilds the refusal with that allowance instead of heap advice. Refs #8294 #6127 #6344 #7170 #7510 #8298 * test(javascript): prove the member-mutation meter spans the phase
This commit is contained in:
1 parent
4eafd9dee5
commit
d07ab79f9b
12 files changed
+371
-34
No files matched your search
@@ -6,6 +6,7 @@ import {
|
||||
retainSemanticAmbientEffect,
|
||||
} from "./javascriptSemanticAmbientWrites.js";
|
||||
import { isIntrinsicMapLookup } from "./javascriptSemanticMapOperations.js";
|
||||
import { JavaScriptSemanticWorkMeter } from "./javascriptSemanticWorkAllowance.js";
|
||||
import {
|
||||
isIntrinsicTypedArrayConstruction,
|
||||
isTypedArrayNumericIndex,
|
||||
@@ -515,6 +516,9 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
const deferredMapReceivers = new Set<
|
||||
t.CallExpression | t.OptionalCallExpression
|
||||
>();
|
||||
// Alias states are retained until their query finishes; bound the phase
|
||||
// before a pathological alias history exhausts the worker heap.
|
||||
const work = new JavaScriptSemanticWorkMeter();
|
||||
// The binding pass already retained callable nodes. Ordinary data-only code
|
||||
// must not pay an alias walk for every member read or assignment.
|
||||
const accessorKeys = {
|
||||
@@ -923,6 +927,10 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
};
|
||||
};
|
||||
const pending: PendingValue[] = [];
|
||||
const schedule = (value: PendingValue): void => {
|
||||
work.charge();
|
||||
pending.push(value);
|
||||
};
|
||||
// The effect, mutation and assigned sources belong to this invocation.
|
||||
// Alias histories do not select a value: the binding/capture/projection do.
|
||||
// Keep small alternatives exact. Bound additional states across this whole
|
||||
@@ -949,7 +957,7 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
? resolveBinding(expression, expression.name)
|
||||
: undefined;
|
||||
if (binding === undefined) {
|
||||
pending.push(value);
|
||||
schedule(value);
|
||||
return;
|
||||
}
|
||||
const observablePath = observablePaths.normalize(
|
||||
@@ -1014,12 +1022,12 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
: { path, definiteReceiver, scheduled: false };
|
||||
joinedProjections.set(key, joined);
|
||||
if (!projectionsWidened) {
|
||||
pending.push({ ...value, exactKey: key });
|
||||
schedule({ ...value, exactKey: key });
|
||||
return;
|
||||
}
|
||||
if (joined.scheduled) return;
|
||||
joined.scheduled = true;
|
||||
pending.push({
|
||||
schedule({
|
||||
...value,
|
||||
path,
|
||||
definiteReceiver,
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
import { JavaScriptSemanticWorkMeter } from "./javascriptSemanticWorkAllowance.js";
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
/** One member write after a few self-selections: one alias query. */
|
||||
const query = (name: string): string =>
|
||||
[
|
||||
`function ${name}(input, env) { var m; if (env.first) m = env.first.info;`,
|
||||
...Array.from(
|
||||
{ length: 3 },
|
||||
(_, index) =>
|
||||
`if (/case${String(index)}/.test(input)) m = m[${String(index % 2)}];`,
|
||||
),
|
||||
"m[1] = input.length; return m; }",
|
||||
].join(" ");
|
||||
|
||||
const source = (count: number): string =>
|
||||
`${Array.from({ length: count }, (_, index) => query(`probe${String(index)}`)).join("\n")}\nmodule.exports = probe0;`;
|
||||
|
||||
/** The meters charged while analyzing each source, in order. */
|
||||
const chargedMeters = (sources: readonly string[]) => {
|
||||
const meters: JavaScriptSemanticWorkMeter[] = [];
|
||||
const charge = JavaScriptSemanticWorkMeter.prototype.charge;
|
||||
vi.spyOn(JavaScriptSemanticWorkMeter.prototype, "charge").mockImplementation(
|
||||
function (this: JavaScriptSemanticWorkMeter, units?: number) {
|
||||
if (!meters.includes(this)) meters.push(this);
|
||||
charge.call(this, units);
|
||||
},
|
||||
);
|
||||
return sources.map((text) => {
|
||||
const before = meters.length;
|
||||
analyzeJavaScriptSemantics(text);
|
||||
return meters.slice(before);
|
||||
});
|
||||
};
|
||||
|
||||
it("charges separate queries in one source against one meter", () => {
|
||||
const [meters] = chargedMeters([source(3)]);
|
||||
expect(meters).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("gives each source a fresh meter", () => {
|
||||
const [first, second] = chargedMeters([source(1), source(1)]);
|
||||
expect(first).toHaveLength(1);
|
||||
expect(second).toHaveLength(1);
|
||||
expect(second?.[0]).not.toBe(first?.[0]);
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { AnalysisResourceConstraintError } from "../analysisErrorCore.js";
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
import {
|
||||
JAVASCRIPT_MEMBER_MUTATION_WORK_ALLOWANCE,
|
||||
JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT,
|
||||
} from "./javascriptSemanticWorkAllowance.js";
|
||||
|
||||
/**
|
||||
* Conditional self-selections before one member write (#8294). Each selection
|
||||
* multiplies the alias histories the write must follow: 8 selections exhausted
|
||||
* a 1 GiB heap before the phase had a work allowance.
|
||||
*/
|
||||
const selections = (count: number): string =>
|
||||
[
|
||||
"function probe(input, env) { var m; if (env.first) m = env.first.info;",
|
||||
...Array.from(
|
||||
{ length: count },
|
||||
(_, index) =>
|
||||
`if (/case${String(index)}/.test(input)) m = m[${String(index % 2)}];`,
|
||||
),
|
||||
"m[1] = input.length; return m; } module.exports = probe;",
|
||||
].join(" ");
|
||||
|
||||
it("refuses member-mutation work beyond the fixed allowance", () => {
|
||||
let refusal: unknown;
|
||||
try {
|
||||
analyzeJavaScriptSemantics(selections(8));
|
||||
} catch (cause) {
|
||||
refusal = cause;
|
||||
}
|
||||
if (!(refusal instanceof AnalysisResourceConstraintError))
|
||||
throw new Error("Expected a work allowance refusal", { cause: refusal });
|
||||
expect(refusal).toMatchObject({
|
||||
resource: "cpu",
|
||||
retryable: false,
|
||||
reportedLimits: {
|
||||
[JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT]:
|
||||
JAVASCRIPT_MEMBER_MUTATION_WORK_ALLOWANCE,
|
||||
},
|
||||
fixedAllowance: {
|
||||
kind: "work",
|
||||
limit: JAVASCRIPT_MEMBER_MUTATION_WORK_ALLOWANCE,
|
||||
unit: "alias states",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("completes the same shape within the allowance", () => {
|
||||
expect(analyzeJavaScriptSemantics(selections(4)).coverage.status).toBe(
|
||||
"complete",
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import { AnalysisResourceConstraintError } from "../analysisErrorCore.js";
|
||||
import {
|
||||
fixedAllowanceOptions,
|
||||
type FixedAllowance,
|
||||
} from "../fixedAllowanceRemediation.js";
|
||||
|
||||
/**
|
||||
* Alias states one source's member-mutation phase may schedule. Each state is
|
||||
* retained until its query finishes, at roughly 1–2 KiB, so the allowance stays
|
||||
* well below the default 1 GiB worker heap. Published bundles up to 395 KB
|
||||
* schedule at most about 123,000 states.
|
||||
*/
|
||||
export const JAVASCRIPT_MEMBER_MUTATION_WORK_ALLOWANCE = 524_288;
|
||||
|
||||
/** Reported-limit key that names this allowance across worker boundaries. */
|
||||
export const JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT =
|
||||
"javascript_member_mutation_alias_state_limit";
|
||||
|
||||
const ALLOWANCE_NAME = "The JavaScript member-mutation work allowance";
|
||||
|
||||
/** Fixed allowance facts for a refusal rebuilt from reported limits. */
|
||||
export const javaScriptSemanticWorkAllowance = (
|
||||
limits: Readonly<Record<string, unknown>> | null | undefined,
|
||||
):
|
||||
| { readonly retryable: false; readonly fixedAllowance: FixedAllowance }
|
||||
| undefined => {
|
||||
const limit = limits?.[JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT];
|
||||
return typeof limit === "number"
|
||||
? fixedAllowanceOptions(ALLOWANCE_NAME, "work", {
|
||||
limit,
|
||||
unit: "alias states",
|
||||
})
|
||||
: undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* Work meter for one analysis phase. Charging past the fixed allowance refuses
|
||||
* the phase before its retained state can exhaust the worker heap; a partial
|
||||
* mutation index would let later reads keep stale values.
|
||||
*/
|
||||
export class JavaScriptSemanticWorkMeter {
|
||||
#used = 0;
|
||||
|
||||
constructor(
|
||||
readonly allowance: number = JAVASCRIPT_MEMBER_MUTATION_WORK_ALLOWANCE,
|
||||
) {}
|
||||
|
||||
charge(units = 1): void {
|
||||
this.#used += units;
|
||||
if (this.#used <= this.allowance) return;
|
||||
const limits = { [JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT]: this.allowance };
|
||||
throw new AnalysisResourceConstraintError(
|
||||
"analyze_javascript_semantics",
|
||||
"cpu",
|
||||
`JavaScript member-mutation analysis scheduled more than ${String(this.allowance)} alias states for one source`,
|
||||
limits,
|
||||
javaScriptSemanticWorkAllowance(limits),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user