Files
rea/src/domain/javascript/javascriptSemanticOpaqueEffects.ts
T
morluto 9ac76d3d94 fix(javascript): invalidate stale facts across direct eval
Index unshadowed direct-eval sites once at the semantic owner. Distinguish
lexical rebinding, non-strict eval-introduced names, and possible mutation
of reachable objects. Reuse existing effect timing and capture proofs;
retain original definitions and module origins without inventing assignments
or parsing or executing eval payloads. Query object uncertainty conservatively
across the owning activation so aliases remain covered when names are shadowed
or leave block scope. Retain no binding-by-eval table.

Use the shared facts for values, read provenance, reference effects and child
argument shapes. Preserve indirect/optional/shadowed eval controls, unrelated
activations, immutable shapes, strict-mode bindings and prior primitive
captures. Remove obsolete reference plumbing and the source-wide eval blocker.

Validation: 306 focused checks across 16 files pass, including compiled CLI
and stdio MCP return projections, constructor uncertainty and child-process
consumers. Against the preceding SQLite commit, 9 of 23 selected checks fail
and 14 controls pass. Static/docs/test discovery (1559 files) pass. Two source
reviews found no remaining scoped blocker after alias and sloppy-name fixes.
No analyzed JavaScript, SQLite engine, heavy test or profiler was executed.
Conservative object reachability can intentionally lose exactness; large-case
opaque-query CPU and memory remain unmeasured.

Fixes #4875
2026-10-18 05:08:37 +08:00

207 lines
6.8 KiB
TypeScript

import * as t from "@babel/types";
import { unwrapJavaScriptExpression } from "./javascriptAstValues.js";
import {
semanticEffectTiming,
semanticBindingEffectFollowsCapture,
} from "./javascriptSemanticMutationInitializers.js";
import {
isUnshadowedGlobal,
resolveSemanticBindingState,
type JavaScriptSemanticAnalysisState,
type JavaScriptSemanticBindingState,
} from "./javascriptSemanticState.js";
/** One source owns sites and the minimal scope tree reaching them; O(AST) storage. */
export interface JavaScriptOpaqueScopeEffects {
readonly sites: WeakSet<t.Node>;
readonly sloppySites: WeakSet<t.Node>;
readonly byScope: Map<string, t.CallExpression[]>;
readonly children: Map<string, Set<string>>;
}
export const indexSemanticOpaqueEffects = (
candidates: readonly { node: t.CallExpression; scopeId: string }[],
state: JavaScriptSemanticAnalysisState,
): void => {
for (const { node, scopeId } of candidates) {
const callee = unwrapJavaScriptExpression(node.callee).node;
if (!isUnshadowedGlobal(callee, state, "eval")) continue;
state.opaqueEffects.sites.add(node);
if (!strictEval(node, state)) state.opaqueEffects.sloppySites.add(node);
const sites = state.opaqueEffects.byScope.get(scopeId) ?? [];
sites.push(node);
state.opaqueEffects.byScope.set(scopeId, sites);
let scope = state.scopesById.get(scopeId);
while (scope?.parentScopeId != null) {
const children =
state.opaqueEffects.children.get(scope.parentScopeId) ??
new Set<string>();
if (children.has(scope.scopeId)) break;
children.add(scope.scopeId);
state.opaqueEffects.children.set(scope.parentScopeId, children);
scope = state.scopesById.get(scope.parentScopeId);
}
}
};
/** Rebinding requires lexical visibility. Object references can remain reachable
* through aliases even where the original name is shadowed or out of block scope.
* Without a disjointness proof, consider their enclosing activation and closures.
* Queries retain no binding-by-eval table. */
export function* semanticOpaqueBindingEffects(
binding: JavaScriptSemanticBindingState,
state: JavaScriptSemanticAnalysisState,
kind: "binding" | "reference" = "binding",
): Generator<t.CallExpression> {
if (state.opaqueEffects.byScope.size === 0) return;
const pending = [
kind === "reference" ? activation(binding.scopeId, state) : binding.scopeId,
];
while (pending.length > 0) {
const scope = pending.pop()!;
for (const node of state.opaqueEffects.byScope.get(scope) ?? [])
if (
kind === "reference" ||
resolveSemanticBindingState(state, node, binding.name)?.bindingId ===
binding.bindingId
)
yield node;
for (const child of state.opaqueEffects.children.get(scope) ?? [])
pending.push(child);
}
}
export const semanticBindingMayHaveOpaqueEffect = (
binding: JavaScriptSemanticBindingState,
state: JavaScriptSemanticAnalysisState,
read?: t.Node,
kind: "binding" | "reference" = "binding",
): boolean => {
if (state.opaqueEffects.byScope.size === 0) return false;
for (const effect of semanticOpaqueBindingEffects(binding, state, kind))
if (
read === undefined ||
(semanticEffectTiming(read, effect, state) !== "after" &&
!semanticBindingEffectFollowsCapture(
binding,
read,
effect,
state.parentsByNode,
))
)
return true;
return false;
};
/** Opaque eval may introduce an otherwise unbound name in a visible activation. */
export const semanticOpaqueNameMayHaveChanged = (
read: t.Node,
state: JavaScriptSemanticAnalysisState,
binding?: JavaScriptSemanticBindingState,
): boolean => {
if (state.opaqueEffects.byScope.size === 0) return false;
const visible = new Set<string>();
let node: t.Node | undefined = read;
while (node !== undefined) {
const scope = state.scopeByNode.get(node);
if (scope !== undefined) {
let current: typeof scope | undefined = scope;
while (current !== undefined) {
visible.add(current.scopeId);
current =
current.parentScopeId === null
? undefined
: state.scopesById.get(current.parentScopeId);
}
break;
}
node = state.parentsByNode.get(node);
}
for (const [scopeId, sites] of state.opaqueEffects.byScope) {
let scope = state.scopesById.get(scopeId);
while (
scope !== undefined &&
scope.kind !== "function" &&
scope.kind !== "program"
)
scope =
scope.parentScopeId === null
? undefined
: state.scopesById.get(scope.parentScopeId);
if (
scope !== undefined &&
visible.has(scope.scopeId) &&
(binding === undefined ||
activation(binding.scopeId, state) !== scope.scopeId) &&
sites.some(
(effect) =>
state.opaqueEffects.sloppySites.has(effect) &&
(binding === undefined ||
resolveSemanticBindingState(state, effect, binding.name)
?.bindingId === binding.bindingId) &&
semanticEffectTiming(read, effect, state) !== "after",
)
)
return true;
}
return false;
};
/** Declaration assignability is independent of observed explicit writes. */
export const semanticBindingCanBeRebound = (
binding: JavaScriptSemanticBindingState,
): boolean =>
binding.mutable ||
(binding.definitions.some(
({ kind }) => kind === "function" || kind === "class",
) &&
binding.initializers.some(
({ node }) => t.isFunctionDeclaration(node) || t.isClassDeclaration(node),
));
export const semanticOpaqueBindingReadMayHaveChanged = (
binding: JavaScriptSemanticBindingState,
read: t.Node,
state: JavaScriptSemanticAnalysisState,
): boolean =>
(semanticBindingCanBeRebound(binding) &&
semanticBindingMayHaveOpaqueEffect(binding, state, read)) ||
semanticOpaqueNameMayHaveChanged(read, state, binding);
const activation = (
scopeId: string,
state: JavaScriptSemanticAnalysisState,
): string => {
let scope = state.scopesById.get(scopeId);
while (
scope !== undefined &&
scope.kind !== "function" &&
scope.kind !== "program" &&
scope.parentScopeId !== null
)
scope = state.scopesById.get(scope.parentScopeId);
return scope?.scopeId ?? scopeId;
};
const strictEval = (
site: t.Node,
state: JavaScriptSemanticAnalysisState,
): boolean => {
for (
let node: t.Node | undefined = site;
node !== undefined;
node = state.parentsByNode.get(node)
) {
if (t.isClass(node) || (t.isProgram(node) && node.sourceType === "module"))
return true;
const directives = t.isProgram(node)
? node.directives
: t.isFunction(node) && t.isBlockStatement(node.body)
? node.body.directives
: [];
if (directives.some(({ value }) => value.value === "use strict"))
return true;
}
return false;
};