fix(javascript): bound primitive string growth and repeated evaluation

This commit is contained in:
morluto
2026-10-09 00:49:31 +08:00
parent 770d2f60aa
commit 4a1837d9d5
10 changed files with 298 additions and 25 deletions
@@ -219,7 +219,7 @@ describe("logical primitive unions", () => {
});
});
describe("semantic expression resource bounds", () => {
describe("semantic primitive allocation and binding reuse", () => {
it("returns an explicit unknown before expanding a large addition product", () => {
const term = '(choice ? "a" : "b")';
const expression = Array.from({ length: 20 }, () => term).join(" + ");
@@ -238,6 +238,102 @@ describe("semantic expression resource bounds", () => {
});
});
it("bounds derived single-string growth before concatenation or key serialization", () => {
const declarations = ['const value0 = "x";'];
for (let index = 1; index <= 100; index += 1) {
const previous = `value${String(index - 1)}`;
declarations.push(
`const value${String(index)} = ${previous} + ${previous};`,
);
}
declarations.push("const answer = value100;");
const ir = analyzeJavaScriptSemantics(declarations.join("\n"));
expect(topLevelBinding(ir, "answer").value).toMatchObject({
status: "unknown",
resourceLimit: "primitive-bytes",
reason: expect.stringMatching(/primitive string-byte budget exceeded/i),
});
expect(ir.coverage).toMatchObject({
status: "partial",
omittedCount: null,
resourceLimits: ["primitive-bytes"],
});
});
it("memoizes by resolved binding without crossing shadowing or mutation", () => {
const ir = analyzeJavaScriptSemantics(`
const shared = "outer";
function readsOuter() { return shared; }
function readsShadow() { const shared = "inner"; return shared; }
function readsMutation() {
let shared = "before";
shared = "after";
return shared;
}
`);
expect(topLevelBinding(ir, "shared").value).toEqual({
status: "literal",
value: "outer",
});
expect(
ir.callables.find(({ name }) => name === "readsOuter")?.returnSites[0]
?.value,
).toEqual({
status: "literal",
value: "outer",
});
expect(
ir.callables.find(({ name }) => name === "readsShadow")?.returnSites[0]
?.value,
).toEqual({
status: "literal",
value: "inner",
});
expect(
ir.callables.find(({ name }) => name === "readsMutation")?.returnSites[0]
?.value.status,
).toBe("ambiguous");
});
it("retains a large derived string while its aggregate JSON estimate fits", () => {
const part = "x".repeat(32_768);
const ir = analyzeJavaScriptSemantics(
`const left = ${JSON.stringify(part)}; const right = ${JSON.stringify(part)}; const answer = left + right;`,
);
expect(topLevelBinding(ir, "answer").value).toMatchObject({
status: "literal",
value: expect.stringMatching(/^x+$/u),
});
const value = topLevelBinding(ir, "answer").value;
if (value.status !== "literal" || typeof value.value !== "string")
throw new Error("Expected exact derived string");
expect(value.value.length).toBe(65_536);
expect(ir.coverage.status).toBe("complete");
});
it("retains exact candidates for a near-budget Cartesian workload", () => {
const left = "l".repeat(85);
const right = "r".repeat(85);
const expression = Array.from(
{ length: 7 },
() =>
`(${JSON.stringify(left)} + (${`choice ? ${JSON.stringify(left)} : ${JSON.stringify(right)}`}))`,
).join(" + ");
const value = valueOf(expression);
expect(value.status).toBe("union");
if (value.status !== "union")
throw new Error("Expected exact primitive union");
expect(value.values).toHaveLength(128);
expect(
value.values.every(
(candidate) =>
typeof candidate === "string" && candidate.length === 1_190,
),
).toBe(true);
});
});
describe("semantic expression depth bounds", () => {
it("returns an explicit unknown for a deeply nested unary AST without throwing", () => {
const parsed = parseJavaScriptSource("const answer = true;");
const declaration = parsed?.program.body[0];
@@ -9,6 +9,7 @@ import { semanticPrimitiveKey } from "./javascriptSemanticProvenance.js";
import { readExactJavaScriptLiteral } from "./javascriptAstValues.js";
import {
SEMANTIC_PRIMITIVE_CANDIDATE_LIMIT,
exceedsSemanticPrimitiveStringByteBudget,
semanticResourceLimitUnknown,
} from "./javascriptSemanticResourceLimits.js";
@@ -23,20 +24,29 @@ export const MAX_SEMANTIC_PRIMITIVE_CANDIDATES =
export const semanticPrimitiveSet = (
values: readonly JavaScriptSemanticPrimitive[],
): JavaScriptSemanticValue => {
const byKey = new Map<string, JavaScriptSemanticPrimitive>();
const uniqueValues = new Set<JavaScriptSemanticPrimitive>();
for (const value of values) {
if (typeof value === "number" && !Number.isFinite(value))
return {
status: "unknown",
reason: "Nonfinite numbers are outside the JSON primitive lattice.",
};
byKey.set(semanticPrimitiveKey(value), value);
if (byKey.size > MAX_SEMANTIC_PRIMITIVE_CANDIDATES)
uniqueValues.add(value);
if (uniqueValues.size > MAX_SEMANTIC_PRIMITIVE_CANDIDATES)
return semanticResourceLimitUnknown("primitive-candidates");
}
const unique = [...byKey.values()].sort((left, right) =>
compareCodePoints(semanticPrimitiveKey(left), semanticPrimitiveKey(right)),
);
if (
exceedsSemanticPrimitiveStringByteBudget(
[...uniqueValues].flatMap((value) =>
typeof value === "string" ? [value] : [],
),
)
)
return semanticResourceLimitUnknown("primitive-bytes");
const unique = [...uniqueValues]
.map((value) => ({ key: semanticPrimitiveKey(value), value }))
.sort((left, right) => compareCodePoints(left.key, right.key))
.map(({ value }) => value);
const only = unique[0];
return unique.length === 1 && only !== undefined
? { status: "literal", value: only }
@@ -3,16 +3,71 @@ import type {
JavaScriptSemanticValue,
} from "./javascriptSemanticValueTypes.js";
/** Maximum distinct alternatives retained in one normalized primitive value. */
export const SEMANTIC_PRIMITIVE_CANDIDATE_LIMIT = 256;
/** Maximum active expression nesting during inert semantic evaluation. */
export const SEMANTIC_EXPRESSION_DEPTH_LIMIT = 256;
/** Bound normalized strings and transient keys independently of transport limits. */
export const SEMANTIC_PRIMITIVE_JSON_BYTES_LIMIT = 1_048_576;
const RESOURCE_LIMIT_REASON: Record<JavaScriptSemanticResourceLimit, string> = {
"primitive-candidates":
"Primitive candidate budget exceeded (maximum 256 alternatives).",
"primitive-bytes":
"Primitive string-byte budget exceeded (maximum 1048576 estimated JSON bytes).",
"expression-depth":
"Semantic expression depth budget exceeded (maximum 256 nested expressions).",
};
/** Check UTF-16 length against worst-case JSON escaping without flattening. */
const jsonStringBytesFromLength = (codeUnits: number): number =>
codeUnits * 6 + 2;
/** Bound string payload before JSON key serialization or candidate creation. */
export const exceedsSemanticPrimitiveStringByteBudget = (
strings: Iterable<string>,
): boolean => {
let total = 0;
for (const value of strings) {
total += jsonStringBytesFromLength(value.length);
if (total > SEMANTIC_PRIMITIVE_JSON_BYTES_LIMIT) return true;
}
return false;
};
/** Bound all outputs of a primitive addition before concatenating any. */
export const exceedsSemanticPrimitiveAdditionByteBudget = (
left: readonly (string | number | boolean | null)[],
right: readonly (string | number | boolean | null)[],
): boolean => {
let total = 0;
for (const leftValue of left) {
for (const rightValue of right) {
if (typeof leftValue !== "string" && typeof rightValue !== "string")
continue;
const length = String(leftValue).length + String(rightValue).length;
total += jsonStringBytesFromLength(length);
if (total > SEMANTIC_PRIMITIVE_JSON_BYTES_LIMIT) return true;
}
}
return false;
};
/** Bound template interpolation outputs before creating concatenated strings. */
export const exceedsSemanticTemplateByteBudget = (
prefixes: readonly string[],
suffixes: readonly string[],
): boolean => {
let total = 0;
for (const prefix of prefixes) {
for (const suffix of suffixes) {
total += jsonStringBytesFromLength(prefix.length + suffix.length);
if (total > SEMANTIC_PRIMITIVE_JSON_BYTES_LIMIT) return true;
}
}
return false;
};
/** Make an explicit unknown value tagged with the semantic resource bound. */
export const semanticResourceLimitUnknown = (
resourceLimit: JavaScriptSemanticResourceLimit,
@@ -4,6 +4,7 @@ export type JavaScriptSemanticPrimitive = string | number | boolean | null;
/** Resource bound that prevented an exact semantic value. */
export type JavaScriptSemanticResourceLimit =
| "primitive-candidates"
| "primitive-bytes"
| "expression-depth";
type JavaScriptSemanticObjectValue = {
@@ -32,6 +32,8 @@ import {
import {
SEMANTIC_EXPRESSION_DEPTH_LIMIT,
SEMANTIC_PRIMITIVE_CANDIDATE_LIMIT,
exceedsSemanticPrimitiveAdditionByteBudget,
exceedsSemanticTemplateByteBudget,
semanticResourceLimitUnknown,
semanticResourceLimitReason,
} from "./javascriptSemanticResourceLimits.js";
@@ -40,8 +42,24 @@ interface EvaluationContext {
readonly state: JavaScriptSemanticAnalysisState;
readonly bindings: ReadonlySet<string>;
readonly expressionDepth: number;
readonly primitiveBindingValues: Map<string, JavaScriptSemanticValue>;
}
const primitiveBindingValuesByState = new WeakMap<
JavaScriptSemanticAnalysisState,
Map<string, JavaScriptSemanticValue>
>();
const primitiveBindingValuesFor = (
state: JavaScriptSemanticAnalysisState,
): Map<string, JavaScriptSemanticValue> => {
const existing = primitiveBindingValuesByState.get(state);
if (existing !== undefined) return existing;
const values = new Map<string, JavaScriptSemanticValue>();
primitiveBindingValuesByState.set(state, values);
return values;
};
// Cap expression recursion independently from graph nodes and primitive unions.
const isSemanticResourceLimit = (
value: JavaScriptSemanticValue,
@@ -56,14 +74,24 @@ export const evaluateSemanticBinding = (
binding: JavaScriptSemanticBindingState,
state: JavaScriptSemanticAnalysisState,
): JavaScriptSemanticValue =>
evaluateBinding(binding, { state, bindings: new Set(), expressionDepth: 0 });
evaluateBinding(binding, {
state,
bindings: new Set(),
expressionDepth: 0,
primitiveBindingValues: primitiveBindingValuesFor(state),
});
/** Evaluate one arbitrary inert expression in the established lexical state. */
export const evaluateSemanticExpression = (
node: t.Node,
state: JavaScriptSemanticAnalysisState,
): JavaScriptSemanticValue =>
evaluateExpression(node, { state, bindings: new Set(), expressionDepth: 0 });
evaluateExpression(node, {
state,
bindings: new Set(),
expressionDepth: 0,
primitiveBindingValues: primitiveBindingValuesFor(state),
});
/** Follow module provenance through destructuring, members, and aliases. */
export const evaluateSemanticProvenance = (
@@ -74,6 +102,7 @@ export const evaluateSemanticProvenance = (
state,
bindings: new Set(),
expressionDepth: 0,
primitiveBindingValues: primitiveBindingValuesFor(state),
});
const evaluateBinding = (
@@ -82,6 +111,8 @@ const evaluateBinding = (
): JavaScriptSemanticValue => {
if (context.bindings.has(binding.bindingId))
return { status: "cycle", reason: `Alias cycle at ${binding.name}.` };
const cached = context.primitiveBindingValues.get(binding.bindingId);
if (cached !== undefined) return cached;
if (binding.initializers.length === 0)
return {
status: "unknown",
@@ -100,7 +131,19 @@ const evaluateBinding = (
evaluateExpression(initializer.node, nested),
initializer.projection,
);
return binding.mutatedPaths.reduce(invalidateSemanticMutationPath, value);
const projected = binding.mutatedPaths.reduce(
invalidateSemanticMutationPath,
value,
);
if (
projected.status === "literal" ||
projected.status === "union" ||
(isSemanticResourceLimit(projected) &&
(projected.resourceLimit === "primitive-bytes" ||
projected.resourceLimit === "primitive-candidates"))
)
context.primitiveBindingValues.set(binding.bindingId, projected);
return projected;
};
const evaluateExpression = (
@@ -155,6 +198,8 @@ const evaluateTemplate = (
// Semantic template evaluation retains its established raw fallback for
// synthetic AST inputs; exact atom readers remain cooked-only.
const text = quasi?.value.cooked ?? quasi?.value.raw ?? "";
if (exceedsSemanticTemplateByteBudget(candidates, [text]))
return semanticResourceLimitUnknown("primitive-bytes");
candidates = candidates.map((prefix) => `${prefix}${text}`);
const expression = node.expressions[index];
if (expression === undefined) continue;
@@ -169,8 +214,11 @@ const evaluateTemplate = (
}
if (candidates.length > SEMANTIC_PRIMITIVE_CANDIDATE_LIMIT / values.length)
return semanticResourceLimitUnknown("primitive-candidates");
const suffixes = values.map((value) => String(value));
if (exceedsSemanticTemplateByteBudget(candidates, suffixes))
return semanticResourceLimitUnknown("primitive-bytes");
candidates = candidates.flatMap((prefix) =>
values.map((value) => `${prefix}${String(value)}`),
suffixes.map((suffix) => `${prefix}${suffix}`),
);
}
return primitiveSet(candidates);
@@ -347,6 +395,8 @@ const addPrimitiveValues = (
}
if (left.length > MAX_SEMANTIC_PRIMITIVE_CANDIDATES / right.length)
return semanticResourceLimitUnknown("primitive-candidates");
if (exceedsSemanticPrimitiveAdditionByteBudget(left, right))
return semanticResourceLimitUnknown("primitive-bytes");
const values = left.flatMap((leftValue) =>
right.map((rightValue) =>
typeof leftValue === "string" || typeof rightValue === "string"
@@ -563,6 +613,7 @@ const nestedContext = (
bindingId?: string,
): EvaluationContext => ({
state: context.state,
primitiveBindingValues: context.primitiveBindingValues,
expressionDepth: context.expressionDepth + 1,
bindings:
bindingId === undefined