diff --git a/scripts/verify/architecture-guards.mjs b/scripts/verify/architecture-guards.mjs index e1093b50b5..3eea22d2b7 100644 --- a/scripts/verify/architecture-guards.mjs +++ b/scripts/verify/architecture-guards.mjs @@ -80,6 +80,29 @@ check( check("NUL-joined list key (use compositeKey)", files, /\.join\(["']\\0["']\)/); +// Pushed effect invalidation (docs/adr/javascript/pushed-effect-invalidation.md). +// Opaque-effect checks are private to javascriptSemanticEffectLog.ts, so the +// compiler keeps readers on its questions, and only the log and its record +// module read the recorded sites. A method read on an intrinsic +// instance asks one owner, which checks the method, the instance's prototype +// link and the intrinsic prototypes. +check( + "reader-side read of recorded opaque effects (ask javascriptSemanticEffectLog.ts or the javascriptSemanticEffectRecord.ts accessors)", + files, + /\.opaqueEffects\b/u, + [ + "src/domain/javascript/javascriptSemanticEffectLog.ts", + "src/domain/javascript/javascriptSemanticEffectRecord.ts", + ], +); + +check( + "reader-side prototype-link member check (use isIntrinsicInstanceMethodCall)", + files, + /mayObserve\([^;]*\["__proto__"\]/u, + ["src/domain/javascript/javascriptSemanticIntrinsicInstances.ts"], +); + check( "direct transparent-wrapper unwrap outside javascriptAstValues (use unwrapJavaScriptExpression)", files, @@ -324,6 +347,18 @@ const checkCounted = (label, key, selected, pattern, owners) => { ); }; +// Pushed effect invalidation (docs/adr/javascript/pushed-effect-invalidation.md). +// Recorded effects belong to the effect log. A reader that reads an effect +// index itself decides on its own which effect kinds and proofs apply; new +// readers ask the log instead, and each migrated reader lowers this count. +checkCounted( + "read of a JavaScript effect index outside the effect log (ask javascriptSemanticEffectLog.ts)", + "javascript-effect-index-read", + files, + /\.(?:bindingWrites|memberWrites|escapedPaths|ambientWriteEffects|ambientEscapeEffects|ambientBindingWrites|ambientWrites|ambientEscapes)\b/gu, + ["src/domain/javascript/javascriptSemanticEffectLog.ts"], +); + // Coverage status has one vocabulary, coverageStatusSchema in // src/domain/coverage.ts. A local enum that contains "complete" either copies // it or drops `unknown`, which forces an unknown producer to pick a side. diff --git a/scripts/verify/baselines/javascript-effect-index-read.json b/scripts/verify/baselines/javascript-effect-index-read.json new file mode 100644 index 0000000000..1157c05043 --- /dev/null +++ b/scripts/verify/baselines/javascript-effect-index-read.json @@ -0,0 +1,10 @@ +{ + "src/domain/javascript/javascriptRouteScan.ts": 1, + "src/domain/javascript/javascriptSemanticAmbientWrites.ts": 25, + "src/domain/javascript/javascriptSemanticAnalysis.ts": 4, + "src/domain/javascript/javascriptSemanticDataEffectHelpers.ts": 3, + "src/domain/javascript/javascriptSemanticMemberMutations.ts": 5, + "src/domain/javascript/javascriptSemanticMutationInitializers.ts": 3, + "src/domain/javascript/javascriptSemanticProjectedSources.ts": 3, + "src/domain/javascript/javascriptSemanticProjection.ts": 1 +} diff --git a/src/domain/javascript/electronStaticOptionObjects.ts b/src/domain/javascript/electronStaticOptionObjects.ts index 9c0e7dc219..25619fb9eb 100644 --- a/src/domain/javascript/electronStaticOptionObjects.ts +++ b/src/domain/javascript/electronStaticOptionObjects.ts @@ -58,8 +58,7 @@ export const resolveElectronOptionObject = ( const candidate = optionObjectCandidate(input, index); if (candidate === undefined) return undefined; for (const reference of candidate.references) { - if (index.referenceMayHaveChangedAt(site, reference, propertyPath)) - return undefined; + if (index.mayObserve(site, reference, propertyPath)) return undefined; } return candidate.object; }; diff --git a/src/domain/javascript/electronStaticRenderer.ts b/src/domain/javascript/electronStaticRenderer.ts index 4b68c08e21..a5139439a6 100644 --- a/src/domain/javascript/electronStaticRenderer.ts +++ b/src/domain/javascript/electronStaticRenderer.ts @@ -126,8 +126,7 @@ export const resolveElectronBrowserWindowReceiver = ( // Keep every alias's consumed path and check at the original invocation site. completeSemanticSteps(index.indexMemberMutationFactsSteps()); for (const alias of aliases) - if (index.referenceMayHaveChangedAt(site, alias.receiver, alias.path)) - return undefined; + if (index.mayObserve(site, alias.receiver, alias.path)) return undefined; return { owner: receiver, path, diff --git a/src/domain/javascript/javascriptCommonJsExportReceivers.ts b/src/domain/javascript/javascriptCommonJsExportReceivers.ts index 9b1c869210..896055fa89 100644 --- a/src/domain/javascript/javascriptCommonJsExportReceivers.ts +++ b/src/domain/javascript/javascriptCommonJsExportReceivers.ts @@ -13,47 +13,25 @@ import { } from "./javascriptSemanticState.js"; import { isUnmodifiedGlobalReference } from "./javascriptSemanticGlobalReference.js"; import { traverseJavaScriptValueAstSteps } from "./javascriptSemanticTraversal.js"; -import { semanticPropertyPathKeyMatches } from "./javascriptSemanticPropertyPaths.js"; -import { createSemanticModuleInterop } from "./javascriptModuleInterop.js"; +import { semanticModuleInteropWithAmbientWrites } from "./javascriptModuleInterop.js"; +import { + ambientPathWriteSites, + bindingMemberEffectMayPrecede, +} from "./javascriptSemanticEffectLog.js"; import { evaluateSemanticExpression } from "./javascriptSemanticValues.js"; import type { JavaScriptSemanticPropertyPath } from "./javascriptSemanticPropertyPaths.js"; import { semanticProjectedSourcesAtRead } from "./javascriptSemanticProjectedSources.js"; import { isUnshadowedGlobal, semanticBindingHasPriorValueWrite, - semanticEffectTiming, } from "./javascriptSemanticMutationInitializers.js"; import { javaScriptRuntimeContext } from "./javascriptSourceParser.js"; -const exportInvocations = new WeakMap< - JavaScriptSemanticAnalysisState, - ReturnType ->(); - /** Share the intrinsic owner's positional and capture proofs across CJS consumers. */ const commonJsExportInvocation = ( node: t.CallExpression, state: JavaScriptSemanticAnalysisState, -) => { - let interop = exportInvocations.get(state); - if (interop === undefined) { - interop = createSemanticModuleInterop(state); - for (const { node: write, path } of state.ambientWriteEffects) - if (write !== undefined && path[0] === "global") - interop.retainWrite(write, [ - "intrinsic", - ...path - .slice(1) - .map((key) => - typeof key === "string" || typeof key === "number" - ? String(key) - : null, - ), - ]); - exportInvocations.set(state, interop); - } - return interop.intrinsicInvocation(node); -}; +) => semanticModuleInteropWithAmbientWrites(state).intrinsicInvocation(node); type ReceiverStatus = "connected" | "detached" | "uncertain"; export interface CommonJsExportReceiverEffect { @@ -99,22 +77,7 @@ const copiedOwnFields = ( if (t.isIdentifier(node)) { const binding = resolveSemanticBindingState(state, node, node.name); if (binding === undefined) return unknown; - if ( - binding.memberWrites.some( - (effect) => - semanticEffectTiming(source, effect.node, state) !== "after", - ) || - binding.escapedPaths.some( - (effect) => - effect.node !== undefined && - !( - (effect.node.start ?? -1) <= (source.start ?? -1) && - (effect.node.end ?? -1) >= (source.end ?? -1) - ) && - semanticEffectTiming(source, effect.node, state) !== "after", - ) - ) - return unknown; + if (bindingMemberEffectMayPrecede(state, binding, source)) return unknown; const selected = semanticProjectedSourcesAtRead( binding, node, @@ -445,16 +408,12 @@ export function* indexCommonJsExportReceiversSteps( const publicCallWrites = new WeakSet(); // Consume the shared write owner's resolved aliases, intrinsic invocation // and keyed mutator facts instead of reinterpreting call names here. - for (const { node, path } of state.ambientWriteEffects) { - if ( - (t.isCallExpression(node) || t.isOptionalCallExpression(node)) && - path[0] === "commonjs-module" && - path.length <= 2 && - (path.length === 1 || - semanticPropertyPathKeyMatches(path[1] ?? null, "exports")) - ) + for (const node of ambientPathWriteSites(state, [ + "commonjs-module", + "exports", + ])) + if (t.isCallExpression(node) || t.isOptionalCallExpression(node)) publicCallWrites.add(node); - } const valueOf = (node: t.Node): Identity => { const value = unwrapJavaScriptExpression(node).node; // An opaque result may alias another object, but reading the current diff --git a/src/domain/javascript/javascriptElectronAppPathChanges.ts b/src/domain/javascript/javascriptElectronAppPathChanges.ts index db56a6d084..ab79ce6c2b 100644 --- a/src/domain/javascript/javascriptElectronAppPathChanges.ts +++ b/src/domain/javascript/javascriptElectronAppPathChanges.ts @@ -1,4 +1,5 @@ import * as t from "@babel/types"; +import { semanticMemberWriteSources } from "./javascriptSemanticEffectLog.js"; import { semanticStaticPropertyKey, @@ -242,11 +243,10 @@ export const createSemanticElectronAppPathChangeQuery = ( : [...source.projection, ...path], }); } - for (const effect of binding.memberWrites) - for (const source of effect.assignedSources ?? []) { - if (semanticReferenceSourcesInactive(source, state)) continue; - enqueue({ ...current, node: source.node, path: undefined }); - } + for (const source of semanticMemberWriteSources(binding)) { + if (semanticReferenceSourcesInactive(source, state)) continue; + enqueue({ ...current, node: source.node, path: undefined }); + } continue; } for (const source of sources) diff --git a/src/domain/javascript/javascriptModuleInterop.ts b/src/domain/javascript/javascriptModuleInterop.ts index 3a5cbc0831..106e6376e3 100644 --- a/src/domain/javascript/javascriptModuleInterop.ts +++ b/src/domain/javascript/javascriptModuleInterop.ts @@ -30,7 +30,12 @@ import { unwrapJavaScriptExpression, } from "./javascriptAstValues.js"; import { semanticKnownArrayIterationAt } from "./javascriptSemanticReferenceSources.js"; -import { semanticOpaqueBindingReadMayHaveChanged } from "./javascriptSemanticOpaqueEffects.js"; +import { + opaqueCodeMayRebindBinding, + semanticAmbientIntrinsicWrites, + semanticAmbientWriteGeneration, +} from "./javascriptSemanticEffectLog.js"; +import { semanticDirectEvalRecorded } from "./javascriptSemanticEffectRecord.js"; // Match the complete Rolldown runtime operations, including its live getters. // Names and formatting are irrelevant; lexical roles, operators and keys are not. @@ -756,7 +761,7 @@ export const createSemanticModuleInterop = ( return result; }, aliasMayHaveChanged: (binding: JavaScriptSemanticBindingState): boolean => { - if (state.opaqueEffects.byScope.size === 0) return false; + if (!semanticDirectEvalRecorded(state)) return false; const cached = aliasChangedCache.get(binding.bindingId); if (cached !== undefined) return cached; origin(binding); @@ -768,13 +773,7 @@ export const createSemanticModuleInterop = ( seen.add(current.bindingId); const next = aliasTargets.get(current.bindingId); if (next === undefined) break; - if ( - semanticOpaqueBindingReadMayHaveChanged( - next.targetBinding, - next.node, - state, - ) - ) { + if (opaqueCodeMayRebindBinding(next.targetBinding, state, next.node)) { changed = true; break; } @@ -912,3 +911,29 @@ const controlBodyOperation = (node: t.Node): t.Node => { } return node; }; + +const ambientWriteInterops = new WeakMap< + JavaScriptSemanticAnalysisState, + { + readonly generation: number; + readonly interop: ReturnType; + } +>(); + +/** + * Module interop that retains every recorded ambient global write, so an + * intrinsic invocation is resolved only while its intrinsic is unreplaced at + * the call. It is rebuilt when a later ambient write is recorded. + */ +export const semanticModuleInteropWithAmbientWrites = ( + state: JavaScriptSemanticAnalysisState, +): ReturnType => { + const generation = semanticAmbientWriteGeneration(state); + const prior = ambientWriteInterops.get(state); + if (prior?.generation === generation) return prior.interop; + const interop = createSemanticModuleInterop(state); + for (const { site, path } of semanticAmbientIntrinsicWrites(state)) + interop.retainWrite(site, path); + ambientWriteInterops.set(state, { generation, interop }); + return interop; +}; diff --git a/src/domain/javascript/javascriptNetworkRequests.ts b/src/domain/javascript/javascriptNetworkRequests.ts index 41dbe94c71..f93e654bb0 100644 --- a/src/domain/javascript/javascriptNetworkRequests.ts +++ b/src/domain/javascript/javascriptNetworkRequests.ts @@ -1,18 +1,11 @@ import { completeSemanticSteps } from "./javascriptSemanticTraversal.js"; -import { - semanticStaticPropertyName, - unwrapJavaScriptExpression, -} from "./javascriptAstValues.js"; -import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; -import { semanticOpaqueGlobalMayHaveChanged } from "./javascriptSemanticOpaqueEffects.js"; -import { - resolveSemanticBindingState, - semanticStableInitializer, - type JavaScriptBindingIndex, - type JavaScriptSemanticAnalysisState, +import type { + JavaScriptBindingIndex, + JavaScriptSemanticAnalysisState, } from "./javascriptSemanticState.js"; import * as t from "@babel/types"; import { isUnmodifiedGlobalReference } from "./javascriptSemanticGlobalReference.js"; +import { isIntrinsicInstanceMethodCall } from "./javascriptSemanticIntrinsicInstances.js"; export const isFetchCall = ( node: t.Node, @@ -39,69 +32,15 @@ export const isBrowserNetworkRequest = ( const isXhrOpenRequest = ( node: t.Node, bindings: JavaScriptSemanticAnalysisState, - index?: Pick, -): boolean | undefined => { - if (!t.isCallExpression(node) && !t.isOptionalCallExpression(node)) - return false; - const callee = unwrapJavaScriptExpression(node.callee).node; - if ( - (!t.isMemberExpression(callee) && !t.isOptionalMemberExpression(callee)) || - semanticStaticPropertyName(callee.property, callee.computed) !== "open" - ) - return false; - if ( - ["XMLHttpRequest", "Object"].some((owner) => - ["open", "__proto__"].some( - (member) => - bindings.ambientWrites.covers([ - "global", - owner, - "prototype", - member, - ]) || - bindings.ambientEscapes.covers([ - "global", - owner, - "prototype", - member, - ]), - ), - ) - ) - return false; - let receiver = unwrapJavaScriptExpression(callee.object).node; - const seen = new Set(); - while (t.isIdentifier(receiver)) { - if ( - index !== undefined && - (index.referenceMayHaveChangedAt(node, receiver, ["open"]) || - index.referenceMayHaveChangedAt(node, receiver, ["__proto__"])) - ) - return false; - const binding = resolveSemanticBindingState( - bindings, - receiver, - receiver.name, - ); - if (binding === undefined || seen.has(binding.bindingId)) return false; - seen.add(binding.bindingId); - const initializer = semanticStableInitializer(binding, bindings); - if ( - initializer === undefined || - initializer.projection.length !== 0 || - !semanticBindingAtRead(binding, receiver, bindings).hasDefiniteInitializer - ) - return false; - receiver = unwrapJavaScriptExpression(initializer.node).node; - } - if ( - !t.isNewExpression(receiver) || - !isUnmodifiedGlobalReference(receiver.callee, bindings, "XMLHttpRequest") || - semanticOpaqueGlobalMayHaveChanged(callee, bindings) - ) - return false; - return index === undefined && seen.size > 0 ? undefined : true; -}; + index?: Pick, +): boolean | undefined => + (t.isCallExpression(node) || t.isOptionalCallExpression(node)) && + isIntrinsicInstanceMethodCall( + node, + bindings, + { constructor: "XMLHttpRequest", methods: ["open"] }, + index, + ); /** Resolve once through the shared mutation index when lexical proof needs it. */ export const createXhrOpenRequestQuery = diff --git a/src/domain/javascript/javascriptRouteScan.ts b/src/domain/javascript/javascriptRouteScan.ts index 8766aca9e8..ed6f3b5c0b 100644 --- a/src/domain/javascript/javascriptRouteScan.ts +++ b/src/domain/javascript/javascriptRouteScan.ts @@ -1080,7 +1080,7 @@ export class JavaScriptRouteScan { if ( initializer === undefined || initializer.projection.length !== 0 || - (site !== undefined && index.referenceMayHaveChangedAt(site, node)) + (site !== undefined && index.mayObserve(site, node)) ) return; node = initializer.node; diff --git a/src/domain/javascript/javascriptSemanticAmbientWrites.ts b/src/domain/javascript/javascriptSemanticAmbientWrites.ts index ee6cabec83..c5d530bdaf 100644 --- a/src/domain/javascript/javascriptSemanticAmbientWrites.ts +++ b/src/domain/javascript/javascriptSemanticAmbientWrites.ts @@ -10,6 +10,7 @@ import { electronMemberPath, } from "./javascriptElectronMemberWrites.js"; import { createSemanticModuleInterop } from "./javascriptModuleInterop.js"; +import { ambientEffectCovers } from "./javascriptSemanticEffectLog.js"; import { resolveSemanticBindingState, semanticStableInitializer, @@ -23,10 +24,7 @@ import { } from "./javascriptSemanticMutationInitializers.js"; import { semanticIterationSources } from "./javascriptSemanticIterationSources.js"; import type { SemanticIterationSource } from "./javascriptSemanticIterationFacts.js"; -import { - joinSemanticPropertyPaths, - semanticPropertyPathKeyMatches, -} from "./javascriptSemanticPropertyPaths.js"; +import { joinSemanticPropertyPaths } from "./javascriptSemanticPropertyPaths.js"; import { createSemanticReturnedReferences } from "./javascriptSemanticReturnedReferences.js"; import { GLOBAL_OBJECT_NAMES, @@ -53,15 +51,6 @@ const canonicalAmbientPath = ( type AmbientPath = readonly (string | null)[]; -const ambientEffectCovers = ( - effect: JavaScriptSemanticAnalysisState["ambientWriteEffects"][number], - target: readonly string[], -): boolean => - effect.path.length <= target.length && - effect.path.every((key, index) => - semanticPropertyPathKeyMatches(key, target[index]!), - ); - const retainedAmbientEffects = new WeakMap< JavaScriptSemanticAnalysisState, Map> diff --git a/src/domain/javascript/javascriptSemanticAnalysis.ts b/src/domain/javascript/javascriptSemanticAnalysis.ts index eff53b5a19..5339125a47 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.ts @@ -3,14 +3,22 @@ import { type JavaScriptSemanticPrecisionInput, } from "./javascriptSemanticPrecision.js"; import { semanticPatternPropertyReadSources } from "./javascriptSemanticIterationSources.js"; +import { + createSemanticOpaqueEffects, + recordSemanticGlobalCodeReference, + semanticDirectEvalRecorded, + semanticGlobalCodeSites, +} from "./javascriptSemanticEffectRecord.js"; import { createSemanticOpaqueGlobalObserver, indexSemanticOpaqueEffects, - semanticOpaqueBindingEffects, - semanticBindingCanBeRebound, - semanticOpaqueBindingReadMayHaveChanged, - semanticBindingMayHaveOpaqueEffect, -} from "./javascriptSemanticOpaqueEffects.js"; + opaqueCodeMayRebindBinding, + semanticReferenceEffectsAtSite, + semanticReferenceEffectsMayHaveChanged, + semanticReadMayObserveEffect, + semanticReferenceRoot, + type SemanticAmbientRootPaths, +} from "./javascriptSemanticEffectLog.js"; import type { JavaScriptOpenReceiverFact } from "./javascriptSemanticReceiverFacts.js"; import * as t from "@babel/types"; @@ -50,7 +58,6 @@ import { type JavaScriptSemanticBindingState, type JavaScriptSemanticScopeState, type JavaScriptBindingIndex, - type JavaScriptSemanticReferenceEffectsAtSite, currentSemanticScope, semanticPrototypeOwners, type SemanticPrototypeOwner, @@ -87,7 +94,6 @@ import { } from "./javascriptSemanticWorkAllowance.js"; import { semanticBindingAtRead, - semanticEffectTiming, semanticEffectTimingWithinActivation, } from "./javascriptSemanticMutationInitializers.js"; import { @@ -131,11 +137,7 @@ import { import { isJavaScriptRpcClientFactory } from "./javascriptRpcEndpoints.js"; import { createSemanticModuleInterop } from "./javascriptModuleInterop.js"; -import { - SemanticPropertyPathCoverage, - semanticPropertyPathKeyMatches, - type JavaScriptSemanticPropertyPath, -} from "./javascriptSemanticPropertyPaths.js"; +import { SemanticPropertyPathCoverage } from "./javascriptSemanticPropertyPaths.js"; import { indexCommonJsExportReceiversSteps } from "./javascriptCommonJsExportReceivers.js"; import { createSemanticElectronAppPathChangeQuery } from "./javascriptElectronAppPathChanges.js"; import type { JavaScriptModuleResolutionMode } from "./javascriptModuleResolutionMode.js"; @@ -217,8 +219,11 @@ export const analyzeParsedJavaScriptReferences = ( const indexedAmbientWriteStates = new WeakSet(); -const indexedMemberMutationStates = - new WeakSet(); +/** The ambient-write owner resolves the global roots a reference may alias. */ +const ambientRootPaths = + (state: JavaScriptSemanticAnalysisState): SemanticAmbientRootPaths => + (root) => + createAmbientPathResolver(state, false)(root).paths; function* indexSemanticAmbientWritesSteps( state: JavaScriptSemanticAnalysisState, @@ -233,7 +238,7 @@ function* indexSemanticAmbientWritesSteps( }), (node) => observePossibleCommonJsDelegateWrite(node, state), ); - if (state.opaqueEffects.globalSites.length > 0) + if (semanticGlobalCodeSites(state).length > 0) clearSemanticValueCaches(state); indexedAmbientWriteStates.add(state); } @@ -242,183 +247,24 @@ function* indexSemanticAmbientWritesSteps( function* indexSemanticMemberMutationFactsSteps( state: JavaScriptSemanticAnalysisState, ): Generator { - if (indexedMemberMutationStates.has(state)) return; + if (state.memberMutationFactsComplete) return; yield* indexSemanticAmbientWritesSteps(state); yield* collectSemanticMemberMutationsSteps( state.program, state, (site, reference, path) => - referenceEffectsMayHaveChanged(state, site, reference, path), + semanticReferenceEffectsMayHaveChanged( + state, + ambientRootPaths(state), + site, + reference, + path, + ), ); state.memberMutationFactsComplete = true; - indexedMemberMutationStates.add(state); invalidateSemanticRequireDelegateWrites(state); } -/** One owner projects source-site effects for complete reports or early exit. */ -const visitSemanticReferenceEffectsAtSite = ( - state: JavaScriptSemanticAnalysisState, - site: t.Node, - referenceNode: t.Node, - visit: ( - effect: JavaScriptSemanticReferenceEffectsAtSite["effects"][number], - ) => boolean, - requestedPath?: JavaScriptSemanticPropertyPath, - effectTiming: typeof semanticEffectTiming = semanticEffectTiming, -): Omit => { - const { root, path: inferredPath } = semanticReferenceRoot(referenceNode); - const memberEffectsIndexed = indexedMemberMutationStates.has(state); - if (!t.isIdentifier(root)) - return { - bindingId: null, - resolution: "unknown", - memberEffectsIndexed, - }; - const lookup = semanticBindingLookupAt(state, root, root.name); - const binding = "binding" in lookup ? lookup.binding : undefined; - const blocked = lookup.kind !== "binding" && lookup.kind !== "ambient"; - const resolution = - lookup.kind === "binding" ? "resolved" : blocked ? "unknown" : "unbound"; - const path = requestedPath ?? inferredPath; - const metadata: Omit = { - bindingId: binding?.bindingId ?? null, - resolution, - memberEffectsIndexed, - }; - const retain = ( - kind: JavaScriptSemanticReferenceEffectsAtSite["effects"][number]["kind"], - node: t.Node | undefined, - effectPath?: JavaScriptSemanticPropertyPath, - ): boolean => { - if (effectPath !== undefined && !semanticPathsOverlap(path, effectPath)) - return true; - return visit({ - kind, - ...(effectPath === undefined ? {} : { path: effectPath }), - timing: effectTiming(site, node, state), - }); - }; - if (binding !== undefined) { - if (semanticBindingCanBeRebound(binding)) - for (const node of semanticOpaqueBindingEffects(binding, state)) - if (!retain("binding-write", node)) return metadata; - for (const node of semanticOpaqueBindingEffects( - binding, - state, - "reference", - )) - if (!retain("escape", node, [])) return metadata; - for (const node of state.bindingWrites.get(binding.bindingId) ?? []) - if (!retain("binding-write", node)) return metadata; - // The mutation producer also queries its growing facts during convergence. - // Completion stays explicit in metadata; callers cannot infer readiness. - for (const effect of binding.memberWrites) - if (!retain("member-write", effect.node, effect.path)) return metadata; - for (const effect of binding.escapedPaths) - if (!retain("escape", effect.node, effect.path)) return metadata; - } else if (lookup.kind === "ambient" || lookup.kind === "uncertain-ambient") { - for (const node of state.ambientBindingWrites.get(root.name) ?? []) - if (!retain("ambient-write", node)) return metadata; - const ambientRoots = createAmbientPathResolver(state, false)(root).paths; - for (const ambientRoot of ambientRoots) { - const selectedPath = [...ambientRoot, ...path]; - for (const [kind, effects] of [ - ["member-write", state.ambientWriteEffects], - ["escape", state.ambientEscapeEffects], - ] as const) - for (const effect of effects) - if ( - semanticPathsOverlap(selectedPath, effect.path) && - !retain(kind, effect.node, effect.path.slice(ambientRoot.length)) - ) - return metadata; - } - } - return metadata; -}; - -/** Query current producer facts; public consumers additionally require completion. */ -const referenceEffectsMayHaveChanged = ( - state: JavaScriptSemanticAnalysisState, - site: t.Node, - referenceNode: t.Node, - propertyPath?: JavaScriptSemanticPropertyPath, -): boolean => { - let changed = false; - const metadata = visitSemanticReferenceEffectsAtSite( - state, - site, - referenceNode, - (effect) => { - changed = effect.timing !== "after"; - return !changed; - }, - propertyPath, - ); - return metadata.resolution !== "resolved" || changed; -}; - -/** Preserve the complete diagnostic contract for consumers needing every effect. */ -const semanticReferenceEffectsAtSite = ( - state: JavaScriptSemanticAnalysisState, - site: t.Node, - referenceNode: t.Node, - requestedPath?: JavaScriptSemanticPropertyPath, - effectTiming: typeof semanticEffectTiming = semanticEffectTiming, -): JavaScriptSemanticReferenceEffectsAtSite => { - const effects: JavaScriptSemanticReferenceEffectsAtSite["effects"][number][] = - []; - const metadata = visitSemanticReferenceEffectsAtSite( - state, - site, - referenceNode, - (effect) => { - effects.push(effect); - return true; - }, - requestedPath, - effectTiming, - ); - return { ...metadata, effects }; -}; - -const semanticReferenceRoot = ( - node: t.Node, -): { readonly root: t.Node; readonly path: JavaScriptSemanticPropertyPath } => { - const path: (string | number | null)[] = []; - let current = unwrapJavaScriptExpression(node).node; - while ( - t.isMemberExpression(current) || - t.isOptionalMemberExpression(current) - ) { - const key = semanticStaticPropertyKey(current.property, current.computed); - path.unshift(key ?? null); - current = unwrapJavaScriptExpression(current.object).node; - } - return { root: current, path }; -}; - -const semanticPathsOverlap = ( - left: JavaScriptSemanticPropertyPath, - right: JavaScriptSemanticPropertyPath, -): boolean => { - for (let index = 0; index < Math.min(left.length, right.length); index++) { - const leftKey = left[index]; - const rightKey = right[index]; - if (leftKey === undefined || rightKey === undefined) return true; - const leftMatchesRight = - typeof rightKey === "string" || typeof rightKey === "number" - ? semanticPropertyPathKeyMatches(leftKey, String(rightKey)) - : true; - const rightMatchesLeft = - typeof leftKey === "string" || typeof leftKey === "number" - ? semanticPropertyPathKeyMatches(rightKey, String(leftKey)) - : true; - if (!leftMatchesRight && !rightMatchesLeft) return false; - } - return true; -}; - /** Recover semantics from an already parsed JavaScript artifact. */ export const analyzeParsedJavaScriptSemantics = ( file: ParsedJavaScriptSource, @@ -596,6 +442,7 @@ export function* indexJavaScriptBindingsWithReceiversSteps( effectsForReferenceAt(site, referenceNode, propertyPath) { return semanticReferenceEffectsAtSite( state, + ambientRootPaths(state), site, referenceNode, propertyPath, @@ -604,19 +451,20 @@ export function* indexJavaScriptBindingsWithReceiversSteps( effectsForReferenceWithinActivationAt(site, referenceNode, propertyPath) { return semanticReferenceEffectsAtSite( state, + ambientRootPaths(state), site, referenceNode, propertyPath, semanticEffectTimingWithinActivation, ); }, - referenceMayHaveChangedAt(site, referenceNode, propertyPath) { - if (!indexedMemberMutationStates.has(state)) return true; - return referenceEffectsMayHaveChanged( + mayObserve(read, target, path) { + return semanticReadMayObserveEffect( state, - site, - referenceNode, - propertyPath, + ambientRootPaths(state), + read, + target, + path, ); }, *referencesSteps() { @@ -932,11 +780,11 @@ export function* indexJavaScriptElectronIdentitySteps( const rootExport = (root: t.Node): readonly string[] | undefined => { // Possible origins must still cover writes through uncertain aliases. // Admit positive reads separately, without caching their temporal validity. - if (state.opaqueEffects.byScope.size > 0 && t.isIdentifier(root)) { + if (semanticDirectEvalRecorded(state) && t.isIdentifier(root)) { const binding = resolveSemanticBindingState(state, root, root.name); if ( binding !== undefined && - (semanticOpaqueBindingReadMayHaveChanged(binding, root, state) || + (opaqueCodeMayRebindBinding(binding, state, root) || interop.aliasMayHaveChanged(binding)) ) return undefined; @@ -958,9 +806,8 @@ export function* indexJavaScriptElectronIdentitySteps( if (!originAvailableAtRead(binding, root)) return undefined; } if ( - state.opaqueEffects.byScope.size > 0 && - (semanticOpaqueBindingReadMayHaveChanged(binding, root, state) || - interop.aliasMayHaveChanged(binding)) + opaqueCodeMayRebindBinding(binding, state, root) || + interop.aliasMayHaveChanged(binding) ) return undefined; let candidate = candidatesByBinding.get(binding.bindingId); @@ -1202,11 +1049,7 @@ const bindingOrigin = ( : visited.has(alias.bindingId) || exhausted || !originAvailableAt(alias, source, state) || - [binding, alias].some( - (hop) => - semanticBindingCanBeRebound(hop) && - semanticBindingMayHaveOpaqueEffect(hop, state), - ) + [binding, alias].some((hop) => opaqueCodeMayRebindBinding(hop, state)) ? undefined : bindingOrigin(alias, state, visited.add(binding.bindingId)); const projection = initializer.projection; @@ -1236,14 +1079,7 @@ const createState = ( arrayIterationUnknown: false, memberMutationFactsComplete: false, program, - opaqueEffects: { - mayReferenceGlobalCode: false, - sites: new WeakSet(), - sloppySites: new WeakSet(), - byScope: new Map(), - children: new Map(), - globalSites: [], - }, + opaqueEffects: createSemanticOpaqueEffects(), staticImportResolutionMode, valueResourceLimits: new Set(), getterResourceLimits: new Set(), @@ -1431,7 +1267,7 @@ function* collectDefinitionsSteps( t.isThisExpression(node) || (t.isIdentifier(node) && globalCodeRoots.has(node.name)) ) - state.opaqueEffects.mayReferenceGlobalCode = true; + recordSemanticGlobalCodeReference(state); if ( t.isCallExpression(node) && t.isIdentifier(unwrapJavaScriptExpression(node.callee).node, { @@ -1650,7 +1486,7 @@ function* collectDefinitionsSteps( ]); } indexSemanticOpaqueEffects(opaqueCandidates, state); - if (state.opaqueEffects.byScope.size > 0) clearSemanticValueCaches(state); + if (semanticDirectEvalRecorded(state)) clearSemanticValueCaches(state); } /** Preserve uncertainty when source can change or expose a built-in prototype. */ diff --git a/src/domain/javascript/javascriptSemanticAnalysisSteps.test.ts b/src/domain/javascript/javascriptSemanticAnalysisSteps.test.ts index 227cbee9e0..b40fdde318 100644 --- a/src/domain/javascript/javascriptSemanticAnalysisSteps.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysisSteps.test.ts @@ -36,7 +36,7 @@ it("bounds repeated capture checks while preserving unindexed and escaped unknow const first = sites[0]; if (first === undefined || !t.isIdentifier(first.arguments[0])) throw new Error("Expected capture"); - expect(index.referenceMayHaveChangedAt(first, first.arguments[0])).toBe(true); + expect(index.mayObserve(first, first.arguments[0])).toBe(true); completeSemanticSteps(index.indexMemberMutationFactsSteps()); const binding = index.bindingForReferenceAt(first.arguments[0]); if (binding === undefined) throw new Error("Expected local binding"); @@ -51,7 +51,7 @@ it("bounds repeated capture checks while preserving unindexed and escaped unknow const changed = sites.map((site) => { const reference = site.arguments[0]; if (!t.isIdentifier(reference)) throw new Error("Expected local reference"); - return index.referenceMayHaveChangedAt(site, reference, ["webPreferences"]); + return index.mayObserve(site, reference, ["webPreferences"]); }); expect(changed).toEqual([ false, diff --git a/src/domain/javascript/javascriptSemanticAsyncEffects.ts b/src/domain/javascript/javascriptSemanticAsyncEffects.ts index 10aec8401f..73ebcf2c73 100644 --- a/src/domain/javascript/javascriptSemanticAsyncEffects.ts +++ b/src/domain/javascript/javascriptSemanticAsyncEffects.ts @@ -19,12 +19,12 @@ import { } from "./javascriptSemanticState.js"; import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; import { traverseJavaScriptValueAst } from "./javascriptSemanticTraversal.js"; -import { isUnmodifiedGlobalReference } from "./javascriptSemanticGlobalReference.js"; -import { createGlobalReferenceReader } from "./javascriptSemanticAmbientWrites.js"; import { - semanticOpaqueGlobalMayHaveChanged, - semanticOpaqueNameMayHaveChanged, -} from "./javascriptSemanticOpaqueEffects.js"; + isIntrinsicGlobalReference, + isUnmodifiedGlobalReference, +} from "./javascriptSemanticGlobalReference.js"; +import { opaqueCodeMayChangeGlobalRead } from "./javascriptSemanticEffectLog.js"; +import { createGlobalReferenceReader } from "./javascriptSemanticAmbientWrites.js"; import { range } from "./javascriptStaticAnalysisHelpers.js"; import { builtinDataEffectIdentity, @@ -378,8 +378,7 @@ const schedulesWithNonFunctionCallback = ( if ( t.isIdentifier(value) && ["undefined", "NaN", "Infinity"].includes(value.name) && - isUnmodifiedGlobalReference(value, state, value.name) && - !semanticOpaqueNameMayHaveChanged(value, state) + isIntrinsicGlobalReference(value, state, value.name) ) return true; // A class passes Node's own typeof check; invoking it is a separate failure, @@ -416,8 +415,7 @@ const schedulesWithInvalidDelay = ( t.isIdentifier(value.callee) && delayConstructors.get(value.callee.name)?.(value.callee).status === "unmodified" && - !semanticOpaqueNameMayHaveChanged(value.callee, state) && - !semanticOpaqueGlobalMayHaveChanged(value.callee, state) + !opaqueCodeMayChangeGlobalRead(value.callee, state, value.callee.name) ); }; diff --git a/src/domain/javascript/javascriptSemanticCallResolution.ts b/src/domain/javascript/javascriptSemanticCallResolution.ts index 57d94066eb..2885083e36 100644 --- a/src/domain/javascript/javascriptSemanticCallResolution.ts +++ b/src/domain/javascript/javascriptSemanticCallResolution.ts @@ -1,5 +1,6 @@ import { javascriptSourceRangeContains } from "./javascriptSourceCoordinates.js"; import * as t from "@babel/types"; +import { bindingAssignmentMayPrecede } from "./javascriptSemanticEffectLog.js"; import type { JavaScriptSemanticArgumentFlow, @@ -7,10 +8,7 @@ import type { } from "./javascriptSemanticIr.js"; import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js"; import { semanticCallableIdForNode } from "./javascriptSemanticProjection.js"; -import { - semanticBindingAtRead, - semanticEffectTiming, -} from "./javascriptSemanticMutationInitializers.js"; +import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; import { unwrapJavaScriptExpression, runtimeFunctionParameters, @@ -203,11 +201,8 @@ const resolveBindingCallables = ( // replacement. Until projected callable resolution is supported here, // preserve known candidates without claiming they cover every target. const complete = - !binding.referenceInitializers.some( - (initializer) => - initializer.assignment && - semanticEffectTiming(site, initializer.node, context.state) !== "after", - ) && candidates.every(({ complete }) => complete); + !bindingAssignmentMayPrecede(context.state, binding, site) && + candidates.every(({ complete }) => complete); const resolution = { callableIds, complete, diff --git a/src/domain/javascript/javascriptSemanticCalls.ts b/src/domain/javascript/javascriptSemanticCalls.ts index 1442a921fc..b676874401 100644 --- a/src/domain/javascript/javascriptSemanticCalls.ts +++ b/src/domain/javascript/javascriptSemanticCalls.ts @@ -32,6 +32,7 @@ import { range } from "./javascriptStaticAnalysisHelpers.js"; import { semanticCallableResultKind } from "./javascriptSemanticCallableResults.js"; import { assignedSemanticResultBindings } from "./javascriptSemanticDataEffectHelpers.js"; import { semanticPrimitiveSet } from "./javascriptSemanticPrimitives.js"; +import { isSemanticDirectEvalSite } from "./javascriptSemanticEffectRecord.js"; /** Local call, flow, capture, and unsupported-frontier facts. */ export interface JavaScriptSemanticCallAnalysis { @@ -454,7 +455,7 @@ const collectDynamicScope = ( ): void => { const reason = t.isWithStatement(node) ? "`with` introduces a runtime binding environment; names inside are not statically resolvable." - : state.opaqueEffects.sites.has(node) + : isSemanticDirectEvalSite(node, state) ? "`eval` can change bindings and reachable objects at runtime; its effects are not statically resolvable." : null; if (reason === null) return; diff --git a/src/domain/javascript/javascriptSemanticDataEffects.ts b/src/domain/javascript/javascriptSemanticDataEffects.ts index eb3253eebe..9d86ae2253 100644 --- a/src/domain/javascript/javascriptSemanticDataEffects.ts +++ b/src/domain/javascript/javascriptSemanticDataEffects.ts @@ -1,4 +1,8 @@ import * as t from "@babel/types"; +import { + bindingAssignmentMayPrecede, + bindingInitializersMayPrecede, +} from "./javascriptSemanticEffectLog.js"; import type { JavaScriptSemanticBoundaryOperation, @@ -22,10 +26,7 @@ import { type JavaScriptSemanticAnalysisState, type JavaScriptSemanticBindingState, } from "./javascriptSemanticState.js"; -import { - semanticBindingAtRead, - semanticEffectTiming, -} from "./javascriptSemanticMutationInitializers.js"; +import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; import { builtinDataEffectIdentity, dataEffectArgumentBindingId as argumentBindingId, @@ -318,8 +319,10 @@ const soleWriteReachesRead = ( ): boolean => { if (binding === undefined || request === undefined) return false; const snapshot = semanticBindingAtRead(binding, read, context.state); - const initializers = snapshot.binding.initializers.filter( - ({ node }) => semanticEffectTiming(read, node, context.state) !== "after", + const initializers = bindingInitializersMayPrecede( + context.state, + snapshot.binding, + read, ); const [initializer] = initializers; if (initializers.length !== 1 || initializer === undefined) return false; @@ -330,11 +333,7 @@ const soleWriteReachesRead = ( source = unwrapJavaScriptExpression(source.argument).node; return ( source === request && - !snapshot.binding.referenceInitializers.some( - ({ assignment, node }) => - assignment && - semanticEffectTiming(read, node, context.state) !== "after", - ) && + !bindingAssignmentMayPrecede(context.state, snapshot.binding, read) && (semanticStableInitializer(binding, context.state) !== undefined || snapshot.hasDefiniteInitializer) ); diff --git a/src/domain/javascript/javascriptSemanticEffectLog.ts b/src/domain/javascript/javascriptSemanticEffectLog.ts new file mode 100644 index 0000000000..6d3ea7d5ce --- /dev/null +++ b/src/domain/javascript/javascriptSemanticEffectLog.ts @@ -0,0 +1,879 @@ +/** + * The effect log: one owner of the questions readers ask about recorded + * effects (docs/adr/javascript/pushed-effect-invalidation.md). Readers ask + * whether a read may observe an effect; the timing, capture and path proofs + * stay here. Recorded opaque sites live in javascriptSemanticEffectRecord.ts. + */ +import * as t from "@babel/types"; +import { javaScriptProgramStrictness } from "./javascriptSourceParser.js"; +import { + semanticStaticPropertyKey, + unwrapJavaScriptExpression, +} from "./javascriptAstValues.js"; +import { + semanticPropertyPathKeyMatches, + type JavaScriptSemanticPropertyPath, +} from "./javascriptSemanticPropertyPaths.js"; +import { + invalidateSemanticEscapedPath, + invalidateSemanticMutationPaths, +} from "./javascriptSemanticMutationValues.js"; +import type { JavaScriptSemanticValue } from "./javascriptSemanticValueTypes.js"; +import { + isUnshadowedGlobal, + semanticBindingEffectFollowsCapture, + semanticEscapeFollowsCapture, + semanticEffectTiming, + semanticBindingEffectFollowsRead, + semanticReadPrecedesEffectWithinActivation, + semanticTopLevelEffectFollowsCapture, +} from "./javascriptSemanticMutationInitializers.js"; +import { + resolveSemanticBindingState, + semanticBindingLookupAt, + type JavaScriptSemanticAnalysisState, + type JavaScriptSemanticBindingState, + type JavaScriptSemanticReferenceEffectsAtSite, +} from "./javascriptSemanticState.js"; + +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(); + if (children.has(scope.scopeId)) break; + children.add(scope.scopeId); + state.opaqueEffects.children.set(scope.parentScopeId, children); + scope = state.scopesById.get(scope.parentScopeId); + } + } +}; + +/** The existing ambient invocation walk owns global effects; ordinary sources + * neither retain a second call list nor create another alias resolver. */ +export const createSemanticOpaqueGlobalObserver = ( + state: JavaScriptSemanticAnalysisState, + dependencies: { + createAmbientPathResolver: ( + state: JavaScriptSemanticAnalysisState, + observeGlobals: boolean, + ) => ( + node: t.Node, + site: t.Node | undefined, + ) => { + paths: readonly (readonly (string | null)[])[]; + callerPaths: readonly (readonly (string | null)[])[]; + }; + createSemanticModuleInterop: ( + state: JavaScriptSemanticAnalysisState, + options?: { + intrinsicPath: (node: t.Node) => readonly string[] | undefined; + }, + ) => { + intrinsicPath: (node: t.Node) => readonly string[] | undefined; + intrinsicInvocation: ( + node: + | t.CallExpression + | t.OptionalCallExpression + | t.NewExpression + | t.TaggedTemplateExpression, + ) => { readonly path: readonly string[] } | undefined; + }; + }, +): ((node: t.Node) => void) | undefined => { + if (!state.opaqueEffects.mayReferenceGlobalCode) return undefined; + const ambientPaths = dependencies.createAmbientPathResolver(state, true); + let invocationSite: t.Node | undefined; + const interop = dependencies.createSemanticModuleInterop(state, { + intrinsicPath: (node) => { + const { paths, callerPaths } = ambientPaths(node, invocationSite); + const path = + paths.find((path) => path[1] === "eval" || path[1] === "Function") ?? + callerPaths.find( + (path) => path[1] === "eval" || path[1] === "Function", + ) ?? + paths[0]; + if (path?.[0] !== "global" || path.some((key) => key === null)) + return undefined; + // Keep the global callable owner for generic call/apply/bind resolution. + return path[1] === "eval" || path[1] === "Function" + ? ["intrinsic", ...(path as readonly string[])] + : ["intrinsic", ...(path.slice(1) as readonly string[])]; + }, + }); + return (node) => { + if ( + !t.isCallExpression(node) && + !t.isOptionalCallExpression(node) && + !t.isNewExpression(node) && + !t.isTaggedTemplateExpression(node) + ) + return; + invocationSite = node; + const path = interop.intrinsicInvocation(node)?.path; + // Constructed code may escape and activate a classic-script global. Keep + // its creation as a conservative timing bound, not proof of execution. + if ( + path?.join(".") === "intrinsic.global.eval" || + path?.join(".") === "intrinsic.global.Function" + ) + state.opaqueEffects.globalSites.push(node); + }; +}; + +/** Global code can affect globals, but cannot rebind its caller's locals. */ +const semanticOpaqueGlobalMayHaveChanged = ( + read: t.Node, + state: JavaScriptSemanticAnalysisState, +): boolean => + state.opaqueEffects.globalSites.some( + (effect) => semanticEffectTiming(read, effect, state) !== "after", + ); + +/** 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. */ +function* semanticOpaqueBindingEffects( + binding: JavaScriptSemanticBindingState, + state: JavaScriptSemanticAnalysisState, + kind: "binding" | "reference" = "binding", +): Generator { + 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); + } +} + +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, + ) && + // Rebinding a freshly initialized local is activation-specific; + // mutations to the referenced object may persist across calls. + !( + kind === "binding" && + semanticBindingEffectFollowsRead( + binding, + read, + effect, + state.parentsByNode, + ) + )) + ) + return true; + return false; +}; + +/** Opaque eval may introduce an otherwise unbound name in a visible activation. */ +const semanticOpaqueNameMayHaveChanged = ( + read: t.Node, + state: JavaScriptSemanticAnalysisState, + binding?: JavaScriptSemanticBindingState, +): boolean => { + if (state.opaqueEffects.byScope.size === 0) return false; + const visible = new Set(); + 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" && + // A sloppy-eval name belongs to this activation, not a prior call. + !semanticReadPrecedesEffectWithinActivation( + read, + effect, + state.parentsByNode, + ), + ) + ) + return true; + } + return false; +}; + +/** + * Non-writable, non-configurable global properties. Global code cannot replace + * them; only a binding that shadows the name can. + */ +const IMMUTABLE_GLOBALS = new Set(["undefined", "NaN", "Infinity"]); + +/** + * Whether opaque code may have changed what an intrinsic global read observes. + * A sloppy direct eval in an activation visible from `reference` may introduce + * a binding for its root name; global code (`eval`, `Function`) that may run + * before `observedAt` may replace a writable global or its prototype. Pass + * `observedAt` when the intrinsic behavior is used later than the reference is + * read, such as a method called on an instance constructed earlier. + */ +export const opaqueCodeMayChangeGlobalRead = ( + reference: t.Node, + state: JavaScriptSemanticAnalysisState, + root: string, + observedAt: t.Node = reference, +): boolean => + semanticOpaqueNameMayHaveChanged(reference, state) || + (!IMMUTABLE_GLOBALS.has(root) && + semanticOpaqueGlobalMayHaveChanged(observedAt, state)); + +/** + * Whether a source-visible ambient effect recorded anywhere in the source may + * have changed `path` (a `global`, `import-meta` or module-tagged property + * path): a write to it or to one of its prefixes. With `"contents"`, an escape + * of it or of a prefix to code whose effects are not evaluated counts too. + * The answer ignores timing, so it holds at every read site. + */ +export const ambientPathMayHaveChanged = ( + state: JavaScriptSemanticAnalysisState, + path: JavaScriptSemanticPropertyPath, + observes: "value" | "contents" = "value", +): boolean => + state.ambientWrites.covers(path) || + (observes === "contents" && state.ambientEscapes.covers(path)); + +/** Whether a recorded ambient effect's path is `target` or one of its prefixes. */ +export const ambientEffectCovers = ( + effect: JavaScriptSemanticAnalysisState["ambientWriteEffects"][number], + target: readonly string[], +): boolean => + effect.path.length <= target.length && + effect.path.every((key, index) => + semanticPropertyPathKeyMatches(key, target[index]!), + ); + +/** + * How a read proves that an effect runs after it. `"activation"` is + * `semanticEffectTiming`, which orders effects within and across activations; + * `"top-level"` orders only top-level statements of the program. + */ +export type SemanticEffectOrdering = "activation" | "top-level"; + +const effectMayPrecedeRead = ( + state: JavaScriptSemanticAnalysisState, + read: t.Node, + effect: t.Node | undefined, + ordering: SemanticEffectOrdering, +): boolean => + ordering === "activation" + ? semanticEffectTiming(read, effect, state) !== "after" + : !semanticTopLevelEffectFollowsCapture(read, effect, state.parentsByNode); + +type AmbientEffect = + JavaScriptSemanticAnalysisState["ambientWriteEffects"][number]; + +/** Matching effects per queried path; the effect lists only grow, so their + * lengths tell when a retained selection is stale. */ +const ambientPathSelections = new WeakMap< + JavaScriptSemanticAnalysisState, + { + writes: number; + escapes: number; + readonly byPath: Map< + string, + { readonly writes: AmbientEffect[]; readonly escapes: AmbientEffect[] } + >; + } +>(); + +const ambientPathEffects = ( + state: JavaScriptSemanticAnalysisState, + path: readonly string[], +): { readonly writes: AmbientEffect[]; readonly escapes: AmbientEffect[] } => { + let cache = ambientPathSelections.get(state); + if ( + cache === undefined || + cache.writes !== state.ambientWriteEffects.length || + cache.escapes !== state.ambientEscapeEffects.length + ) { + cache = { + writes: state.ambientWriteEffects.length, + escapes: state.ambientEscapeEffects.length, + byPath: new Map(), + }; + ambientPathSelections.set(state, cache); + } + const key = JSON.stringify(path); + let selected = cache.byPath.get(key); + if (selected === undefined) { + selected = { + writes: state.ambientWriteEffects.filter((effect) => + ambientEffectCovers(effect, path), + ), + escapes: state.ambientEscapeEffects.filter((effect) => + ambientEffectCovers(effect, path), + ), + }; + cache.byPath.set(key, selected); + } + return selected; +}; + +/** + * `mayObserve` for an ambient member path: whether a recorded write to `path` + * or to one of its prefixes may run before `read`. With `"contents"`, an + * escape of it to code whose effects are not evaluated counts too. + */ +export const ambientPathMayObserveAt = ( + state: JavaScriptSemanticAnalysisState, + read: t.Node, + path: readonly string[], + observes: "value" | "contents" = "value", + ordering: SemanticEffectOrdering = "activation", +): boolean => { + const { writes, escapes } = ambientPathEffects(state, path); + const mayPrecede = ({ node }: AmbientEffect): boolean => + effectMayPrecedeRead(state, read, node, ordering); + return ( + writes.some(mayPrecede) || + (observes === "contents" && escapes.some(mayPrecede)) + ); +}; + +/** + * `mayObserve` for an unbound name: whether a recorded assignment to the + * ambient binding `name` may run before `read`. + */ +export const ambientBindingMayObserveWriteAt = ( + state: JavaScriptSemanticAnalysisState, + read: t.Node, + name: string, + ordering: SemanticEffectOrdering = "activation", +): boolean => + (state.ambientBindingWrites.get(name) ?? []).some((write) => + effectMayPrecedeRead(state, read, write, ordering), + ); + +/** + * A count that grows whenever an ambient write is recorded. A cache that + * depends on ambient writes stores it and recomputes when it differs. + */ +export const semanticAmbientWriteGeneration = ( + state: JavaScriptSemanticAnalysisState, +): number => state.ambientWriteEffects.length; + +/** + * A count that grows whenever a member write or escape is recorded on + * `binding`. A cache that depends on the binding's member effects stores it + * and recomputes when it differs. + */ +export const semanticBindingEffectGeneration = ( + binding: JavaScriptSemanticBindingState, +): number => binding.memberWrites.length + binding.escapedPaths.length; + +/** + * Recorded ambient writes to `global` paths, as intrinsic paths with + * non-literal keys as null. A write recorded without a site is placed at the + * program, so it may precede every read. + */ +export function* semanticAmbientIntrinsicWrites( + state: JavaScriptSemanticAnalysisState, +): Generator<{ + readonly site: t.Node; + readonly path: readonly (string | null)[]; +}> { + for (const { node, path } of state.ambientWriteEffects) + if (path[0] === "global") + yield { + site: node ?? state.program, + path: [ + "intrinsic", + ...path + .slice(1) + .map((key) => + key === null || typeof key === "object" ? null : String(key), + ), + ], + }; +} + +/** Sites of recorded ambient writes to `path` or to one of its prefixes. */ +export const ambientPathWriteSites = ( + state: JavaScriptSemanticAnalysisState, + path: readonly string[], +): readonly (t.Node | undefined)[] => + ambientPathEffects(state, path).writes.map(({ node }) => node); + +/** + * Whether a member write to `binding`'s object, or an escape of it, may run + * before `read`. An escape whose site encloses `read` is the call that reads + * it, so it does not count. + */ +export const bindingMemberEffectMayPrecede = ( + state: JavaScriptSemanticAnalysisState, + binding: JavaScriptSemanticBindingState, + read: t.Node, +): boolean => + binding.memberWrites.some( + (effect) => semanticEffectTiming(read, effect.node, state) !== "after", + ) || + binding.escapedPaths.some( + (effect) => + effect.node !== undefined && + !( + (effect.node.start ?? -1) <= (read.start ?? -1) && + (effect.node.end ?? -1) >= (read.end ?? -1) + ) && + semanticEffectTiming(read, effect.node, state) !== "after", + ); + +/** + * Whether an assignment to `binding` recorded as a reference initializer + * (a pattern or other non-ordinary write) may run before `read`. + */ +export const bindingAssignmentMayPrecede = ( + state: JavaScriptSemanticAnalysisState, + binding: JavaScriptSemanticBindingState, + read: t.Node, +): boolean => + binding.referenceInitializers.some( + ({ assignment, node }) => + assignment && semanticEffectTiming(read, node, state) !== "after", + ); + +/** The initializers of `binding` that may run before `read`. */ +export const bindingInitializersMayPrecede = ( + state: JavaScriptSemanticAnalysisState, + binding: JavaScriptSemanticBindingState, + read: t.Node, +): JavaScriptSemanticBindingState["initializers"] => + binding.initializers.filter( + ({ node }) => semanticEffectTiming(read, node, state) !== "after", + ); + +/** Whether a recorded write to `.prototype` may run before `read`. */ +export const prototypeMutationMayPrecede = ( + state: JavaScriptSemanticAnalysisState, + owner: string, + read: t.Node, +): boolean => + state.prototypeMutationSites.some( + (effect) => + effect.owner === owner && + semanticEffectTiming(read, effect.node, state) !== "after", + ); + +/** The sources recorded member writes assigned into `binding`'s object. */ +export function* semanticMemberWriteSources( + binding: JavaScriptSemanticBindingState, +): Generator[number]> { + for (const effect of binding.memberWrites) + yield* effect.assignedSources ?? []; +} + +/** Declaration assignability is independent of observed explicit writes. */ +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), + )); + +/** + * Whether opaque code (a direct eval visible to the binding) may have rebound + * it. With a read, only effects that may precede that read count, and a sloppy + * direct eval may also have introduced a shadowing name visible from it. + * Without one, any visible direct eval counts. + */ +export const opaqueCodeMayRebindBinding = ( + binding: JavaScriptSemanticBindingState, + state: JavaScriptSemanticAnalysisState, + read?: t.Node, +): boolean => + (semanticBindingCanBeRebound(binding) && + semanticBindingMayHaveOpaqueEffect(binding, state, read)) || + (read !== undefined && + semanticOpaqueNameMayHaveChanged(read, state, binding)); + +/** + * Whether opaque code may have mutated the object a binding references after + * it was captured; objects stay reachable through aliases where the name is + * shadowed or out of scope. + */ +const opaqueCodeMayReachBindingReference = ( + binding: JavaScriptSemanticBindingState, + state: JavaScriptSemanticAnalysisState, + capturePoint: t.Node | undefined, +): boolean => + semanticBindingMayHaveOpaqueEffect(binding, state, capturePoint, "reference"); + +/** + * The value of `binding`'s reference as a read captured at `capturePoint` + * observes it, after the recorded effects that may precede the capture: + * member writes invalidate their paths, escapes invalidate the escaped object + * references, and a direct eval that may reach the reference makes an object + * or array unknown. Without a capture point, every recorded effect counts. + */ +export const applySemanticBindingReferenceEffects = ( + value: JavaScriptSemanticValue, + binding: JavaScriptSemanticBindingState, + state: JavaScriptSemanticAnalysisState, + capturePoint: t.Node | undefined, +): JavaScriptSemanticValue => { + let projected = invalidateSemanticMutationPaths( + value, + binding.memberWrites + .filter( + ({ node }) => + capturePoint === undefined || + node === undefined || + !semanticBindingEffectFollowsCapture( + binding, + capturePoint, + node, + state.parentsByNode, + ), + ) + .map(({ path }) => path), + ); + for (const escape of binding.escapedPaths) { + // Escapes only invalidate object references; other lattice values survive. + if (projected.status !== "object" && projected.status !== "array") break; + if ( + capturePoint !== undefined && + semanticEscapeFollowsCapture( + binding, + capturePoint, + escape.node, + state.parentsByNode, + ) + ) + continue; + projected = invalidateSemanticEscapedPath(projected, escape.path); + } + if ( + (projected.status === "object" || projected.status === "array") && + opaqueCodeMayReachBindingReference(binding, state, capturePoint) + ) + projected = { + status: "unknown", + reason: "Direct eval may have mutated this object reference.", + }; + return projected; +}; + +/** + * Opaque sites that may affect a binding, for a report of every effect: direct + * evals that may rebind it when its declaration is assignable, then every + * direct eval that may reach the object it references (an escape of its whole + * value). Timing and path proofs stay with the caller's effect report. + */ +function* semanticOpaqueReferenceEffects( + binding: JavaScriptSemanticBindingState, + state: JavaScriptSemanticAnalysisState, +): Generator<{ + readonly kind: "binding-write" | "escape"; + readonly node: t.CallExpression; +}> { + if (semanticBindingCanBeRebound(binding)) + for (const node of semanticOpaqueBindingEffects(binding, state)) + yield { kind: "binding-write", node }; + for (const node of semanticOpaqueBindingEffects(binding, state, "reference")) + yield { kind: "escape", node }; +} + +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) && javaScriptProgramStrictness(node) === "strict") + ) + return true; + const directives = + t.isFunction(node) && t.isBlockStatement(node.body) + ? node.body.directives + : []; + if (directives.some(({ value }) => value.value === "use strict")) + return true; + } + return false; +}; + +/** Ambient root paths a global reference may alias; the ambient-write owner + * resolves them, so the effect log takes the resolver from its caller. */ +export type SemanticAmbientRootPaths = ( + root: t.Node, +) => readonly (readonly (string | null)[])[]; + +/** One owner projects source-site effects for complete reports or early exit. */ +const visitSemanticReferenceEffectsAtSite = ( + state: JavaScriptSemanticAnalysisState, + ambientPaths: SemanticAmbientRootPaths, + site: t.Node, + referenceNode: t.Node, + visit: ( + effect: JavaScriptSemanticReferenceEffectsAtSite["effects"][number], + ) => boolean, + requestedPath?: JavaScriptSemanticPropertyPath, + effectTiming: typeof semanticEffectTiming = semanticEffectTiming, +): Omit => { + const { root, path: inferredPath } = semanticReferenceRoot(referenceNode); + const memberEffectsIndexed = state.memberMutationFactsComplete; + if (!t.isIdentifier(root)) + return { + bindingId: null, + resolution: "unknown", + memberEffectsIndexed, + }; + const lookup = semanticBindingLookupAt(state, root, root.name); + const binding = "binding" in lookup ? lookup.binding : undefined; + const blocked = lookup.kind !== "binding" && lookup.kind !== "ambient"; + const resolution = + lookup.kind === "binding" ? "resolved" : blocked ? "unknown" : "unbound"; + const path = requestedPath ?? inferredPath; + const metadata: Omit = { + bindingId: binding?.bindingId ?? null, + resolution, + memberEffectsIndexed, + }; + const retain = ( + kind: JavaScriptSemanticReferenceEffectsAtSite["effects"][number]["kind"], + node: t.Node | undefined, + effectPath?: JavaScriptSemanticPropertyPath, + ): boolean => { + if (effectPath !== undefined && !semanticPathsOverlap(path, effectPath)) + return true; + return visit({ + kind, + ...(effectPath === undefined ? {} : { path: effectPath }), + timing: effectTiming(site, node, state), + }); + }; + if (binding !== undefined) { + for (const { kind, node } of semanticOpaqueReferenceEffects(binding, state)) + if (!retain(kind, node, kind === "escape" ? [] : undefined)) + return metadata; + for (const node of state.bindingWrites.get(binding.bindingId) ?? []) + if (!retain("binding-write", node)) return metadata; + // The mutation producer also queries its growing facts during convergence. + // Completion stays explicit in metadata; callers cannot infer readiness. + for (const effect of binding.memberWrites) + if (!retain("member-write", effect.node, effect.path)) return metadata; + for (const effect of binding.escapedPaths) + if (!retain("escape", effect.node, effect.path)) return metadata; + } else if (lookup.kind === "ambient" || lookup.kind === "uncertain-ambient") { + for (const node of state.ambientBindingWrites.get(root.name) ?? []) + if (!retain("ambient-write", node)) return metadata; + const ambientRoots = ambientPaths(root); + for (const ambientRoot of ambientRoots) { + const selectedPath = [...ambientRoot, ...path]; + for (const [kind, effects] of [ + ["member-write", state.ambientWriteEffects], + ["escape", state.ambientEscapeEffects], + ] as const) + for (const effect of effects) + if ( + semanticPathsOverlap(selectedPath, effect.path) && + !retain(kind, effect.node, effect.path.slice(ambientRoot.length)) + ) + return metadata; + } + } + return metadata; +}; + +/** Query current producer facts; public consumers additionally require completion. */ +export const semanticReferenceEffectsMayHaveChanged = ( + state: JavaScriptSemanticAnalysisState, + ambientPaths: SemanticAmbientRootPaths, + site: t.Node, + referenceNode: t.Node, + propertyPath?: JavaScriptSemanticPropertyPath, +): boolean => { + let changed = false; + const metadata = visitSemanticReferenceEffectsAtSite( + state, + ambientPaths, + site, + referenceNode, + (effect) => { + changed = effect.timing !== "after"; + return !changed; + }, + propertyPath, + ); + return metadata.resolution !== "resolved" || changed; +}; + +/** + * `mayObserve(read, target, path)` (ADR decision 2): whether reading `target` + * at `path` (default: the member path `target` spells) at `read` may observe + * a recorded effect. Before the member-mutation phase completes, every read + * may observe one (decision 4). + */ +export const semanticReadMayObserveEffect = ( + state: JavaScriptSemanticAnalysisState, + ambientPaths: SemanticAmbientRootPaths, + read: t.Node, + target: t.Node, + path?: JavaScriptSemanticPropertyPath, +): boolean => + !state.memberMutationFactsComplete || + semanticReferenceEffectsMayHaveChanged( + state, + ambientPaths, + read, + target, + path, + ); + +/** Preserve the complete diagnostic contract for consumers needing every effect. */ +export const semanticReferenceEffectsAtSite = ( + state: JavaScriptSemanticAnalysisState, + ambientPaths: SemanticAmbientRootPaths, + site: t.Node, + referenceNode: t.Node, + requestedPath?: JavaScriptSemanticPropertyPath, + effectTiming: typeof semanticEffectTiming = semanticEffectTiming, +): JavaScriptSemanticReferenceEffectsAtSite => { + const effects: JavaScriptSemanticReferenceEffectsAtSite["effects"][number][] = + []; + const metadata = visitSemanticReferenceEffectsAtSite( + state, + ambientPaths, + site, + referenceNode, + (effect) => { + effects.push(effect); + return true; + }, + requestedPath, + effectTiming, + ); + return { ...metadata, effects }; +}; + +export const semanticReferenceRoot = ( + node: t.Node, +): { readonly root: t.Node; readonly path: JavaScriptSemanticPropertyPath } => { + const path: (string | number | null)[] = []; + let current = unwrapJavaScriptExpression(node).node; + while ( + t.isMemberExpression(current) || + t.isOptionalMemberExpression(current) + ) { + const key = semanticStaticPropertyKey(current.property, current.computed); + path.unshift(key ?? null); + current = unwrapJavaScriptExpression(current.object).node; + } + return { root: current, path }; +}; + +const semanticPathsOverlap = ( + left: JavaScriptSemanticPropertyPath, + right: JavaScriptSemanticPropertyPath, +): boolean => { + for (let index = 0; index < Math.min(left.length, right.length); index++) { + const leftKey = left[index]; + const rightKey = right[index]; + if (leftKey === undefined || rightKey === undefined) return true; + const leftMatchesRight = + typeof rightKey === "string" || typeof rightKey === "number" + ? semanticPropertyPathKeyMatches(leftKey, String(rightKey)) + : true; + const rightMatchesLeft = + typeof leftKey === "string" || typeof leftKey === "number" + ? semanticPropertyPathKeyMatches(rightKey, String(leftKey)) + : true; + if (!leftMatchesRight && !rightMatchesLeft) return false; + } + return true; +}; diff --git a/src/domain/javascript/javascriptSemanticEffectRecord.ts b/src/domain/javascript/javascriptSemanticEffectRecord.ts new file mode 100644 index 0000000000..f28d019963 --- /dev/null +++ b/src/domain/javascript/javascriptSemanticEffectRecord.ts @@ -0,0 +1,49 @@ +import type * as t from "@babel/types"; +import type { + JavaScriptOpaqueScopeEffects, + JavaScriptSemanticAnalysisState, +} from "./javascriptSemanticState.js"; + +/** + * Storage of the opaque effects (`eval`, `Function`) recorded for one source. + * Only this module and the effect log (javascriptSemanticEffectLog.ts) read + * the recorded sites; the timing producer, which the effect log builds on, + * asks the accessors below + * (docs/adr/javascript/pushed-effect-invalidation.md). + */ +export const createSemanticOpaqueEffects = + (): JavaScriptOpaqueScopeEffects => ({ + mayReferenceGlobalCode: false, + sites: new WeakSet(), + sloppySites: new WeakSet(), + byScope: new Map(), + children: new Map(), + globalSites: [], + }); + +/** Source evidence, collected with declarations, that global code may run. */ +export const recordSemanticGlobalCodeReference = ( + state: JavaScriptSemanticAnalysisState, +): void => { + state.opaqueEffects.mayReferenceGlobalCode = true; +}; + +/** Whether the source contains a direct `eval` that may rebind names. */ +export const semanticDirectEvalRecorded = ( + state: JavaScriptSemanticAnalysisState, +): boolean => state.opaqueEffects.byScope.size > 0; + +/** Whether `node` is a recorded direct `eval` call. */ +export const isSemanticDirectEvalSite = ( + node: t.Node, + state: JavaScriptSemanticAnalysisState, +): boolean => state.opaqueEffects.sites.has(node); + +/** + * Global-code sites (`eval`, `Function`) observed so far, in observation + * order. The ambient walk appends while timing queries run, so a consumer + * may keep its position in the list and read only later entries. + */ +export const semanticGlobalCodeSites = ( + state: JavaScriptSemanticAnalysisState, +): readonly t.Node[] => state.opaqueEffects.globalSites; diff --git a/src/domain/javascript/javascriptSemanticExpressionShapes.ts b/src/domain/javascript/javascriptSemanticExpressionShapes.ts index cde0cda017..2e80608d12 100644 --- a/src/domain/javascript/javascriptSemanticExpressionShapes.ts +++ b/src/domain/javascript/javascriptSemanticExpressionShapes.ts @@ -1,7 +1,5 @@ -import { - semanticOpaqueBindingReadMayHaveChanged, - semanticOpaqueNameMayHaveChanged, -} from "./javascriptSemanticOpaqueEffects.js"; +import { opaqueCodeMayRebindBinding } from "./javascriptSemanticEffectLog.js"; +import { opaqueCodeMayChangeGlobalRead } from "./javascriptSemanticEffectLog.js"; import * as t from "@babel/types"; import { @@ -89,7 +87,7 @@ export const createSemanticExpressionShapeResolver = ( } if ( namedProofBlocked() || - semanticOpaqueBindingReadMayHaveChanged(binding, node, state) + opaqueCodeMayRebindBinding(binding, state, node) ) return "unknown"; const initializer = semanticStableInitializer(binding, state); @@ -110,7 +108,7 @@ export const createSemanticExpressionShapeResolver = ( isNullishJavaScriptSyntax(node) || (isUnshadowedGlobal(node, state, "undefined") && !namedProofBlocked() && - !semanticOpaqueNameMayHaveChanged(node, state)) + !opaqueCodeMayChangeGlobalRead(node, state, "undefined")) ); }, }; diff --git a/src/domain/javascript/javascriptSemanticGlobalReference.ts b/src/domain/javascript/javascriptSemanticGlobalReference.ts index 8f991523fc..7a8b36f359 100644 --- a/src/domain/javascript/javascriptSemanticGlobalReference.ts +++ b/src/domain/javascript/javascriptSemanticGlobalReference.ts @@ -3,6 +3,10 @@ import { electronMemberPath } from "./javascriptElectronMemberWrites.js"; import { isUnshadowedGlobal } from "./javascriptSemanticMutationInitializers.js"; import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js"; import { isImportMetaUrl } from "./javascriptStaticAnalysisHelpers.js"; +import { + ambientPathMayHaveChanged, + opaqueCodeMayChangeGlobalRead, +} from "./javascriptSemanticEffectLog.js"; export const GLOBAL_OBJECT_NAMES = ["globalThis", "global", "window", "self"]; @@ -40,17 +44,37 @@ export const isUnmodifiedGlobalReference = ( ): boolean => { const path: readonly [string, ...string[]] = typeof name === "string" ? [name] : name; - if (state.ambientWrites.covers(["global", ...path])) return false; + if (ambientPathMayHaveChanged(state, ["global", ...path])) return false; const namespace = globalReferenceNamespace(callee, state, path); return ( namespace !== undefined && - (namespace === null || !state.ambientWrites.covers(["global", namespace])) + (namespace === null || + !ambientPathMayHaveChanged(state, ["global", namespace])) ); }; +/** + * An intrinsic global read: an unmodified global reference that no opaque code + * may have rebound or replaced by the time its behavior is observed. + */ +export const isIntrinsicGlobalReference = ( + reference: t.Node, + state: JavaScriptSemanticAnalysisState, + name: string | readonly [string, ...string[]], + observedAt: t.Node = reference, +): boolean => + isUnmodifiedGlobalReference(reference, state, name) && + !opaqueCodeMayChangeGlobalRead( + reference, + state, + typeof name === "string" ? name : name[0], + observedAt, + ); + /** A module URL base is known only while its property has no visible write. */ export const isUnmodifiedImportMetaUrl = ( node: t.Node | undefined, state: JavaScriptSemanticAnalysisState, ): boolean => - isImportMetaUrl(node) && !state.ambientWrites.covers(["import-meta", "url"]); + isImportMetaUrl(node) && + !ambientPathMayHaveChanged(state, ["import-meta", "url"]); diff --git a/src/domain/javascript/javascriptSemanticIntrinsicInstances.ts b/src/domain/javascript/javascriptSemanticIntrinsicInstances.ts new file mode 100644 index 0000000000..62a0413625 --- /dev/null +++ b/src/domain/javascript/javascriptSemanticIntrinsicInstances.ts @@ -0,0 +1,137 @@ +import * as t from "@babel/types"; + +import { + semanticStaticPropertyName, + unwrapJavaScriptExpression, +} from "./javascriptAstValues.js"; +import { isIntrinsicGlobalReference } from "./javascriptSemanticGlobalReference.js"; +import { ambientPathMayHaveChanged } from "./javascriptSemanticEffectLog.js"; +import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; +import { + resolveSemanticBindingState, + semanticStableInitializer, + type JavaScriptBindingIndex, + type JavaScriptSemanticAnalysisState, +} from "./javascriptSemanticState.js"; + +/** An intrinsic constructor whose instances expose methods a reader relies on. */ +interface JavaScriptIntrinsicInstanceMethods { + readonly constructor: string; + readonly methods: readonly string[]; + /** Instance members the constructor itself calls when given arguments. */ + readonly constructionHooks?: readonly string[]; +} + +/** + * Whether a source-visible write or escape may have replaced a member on the + * intrinsic prototype, on `Object.prototype`, or the prototype link itself. + */ +export const intrinsicPrototypeMemberMayHaveChanged = ( + state: JavaScriptSemanticAnalysisState, + constructor: string, + member: string, +): boolean => + [constructor, "Object"].some((owner) => + [member, "__proto__"].some((key) => + ambientPathMayHaveChanged( + state, + ["global", owner, "prototype", key], + "contents", + ), + ), + ); + +/** + * Prove that a call invokes an unchanged intrinsic method on an instance that + * `new ()` created and stable aliases carried to the call. Every + * alias in the receiver walk asks the shared member-effect owner whether the + * method or the instance's prototype link may have changed before the call. + * Without `index`, a walk through an alias returns undefined: the proof needs + * the shared member-mutation facts. + */ +export const isIntrinsicInstanceMethodCall = ( + node: t.CallExpression | t.OptionalCallExpression | t.NewExpression, + bindings: JavaScriptSemanticAnalysisState, + intrinsic: JavaScriptIntrinsicInstanceMethods, + index?: Pick, +): boolean | undefined => { + const callee = unwrapJavaScriptExpression(node.callee).node; + if (!t.isMemberExpression(callee) && !t.isOptionalMemberExpression(callee)) + return false; + const method = semanticStaticPropertyName(callee.property, callee.computed); + if (method === undefined || !intrinsic.methods.includes(method)) return false; + if ( + intrinsicPrototypeMemberMayHaveChanged( + bindings, + intrinsic.constructor, + method, + ) + ) + return false; + let receiver = unwrapJavaScriptExpression(callee.object).node; + const seen = new Set(); + while (t.isIdentifier(receiver)) { + if ( + index !== undefined && + (index.mayObserve(node, receiver, [method]) || + index.mayObserve(node, receiver, ["__proto__"])) + ) + return false; + const binding = resolveSemanticBindingState( + bindings, + receiver, + receiver.name, + ); + if (binding === undefined || seen.has(binding.bindingId)) return false; + seen.add(binding.bindingId); + const initializer = semanticStableInitializer(binding, bindings); + if ( + initializer === undefined || + initializer.projection.length !== 0 || + !semanticBindingAtRead(binding, receiver, bindings).hasDefiniteInitializer + ) + return false; + receiver = unwrapJavaScriptExpression(initializer.node).node; + } + if ( + !t.isNewExpression(receiver) || + !isIntrinsicGlobalReference( + receiver.callee, + bindings, + intrinsic.constructor, + callee, + ) + ) + return false; + // A hook the constructor invokes can install its own methods on the + // instance before the constructed value is read. + if ( + receiver.arguments.length > 0 && + (intrinsic.constructionHooks ?? []).some((hook) => + intrinsicPrototypeMemberMayHaveChanged( + bindings, + intrinsic.constructor, + hook, + ), + ) + ) + return false; + return index === undefined && seen.size > 0 ? undefined : true; +}; + +/** Map construction from an iterable calls the instance's `set` hook. */ +export const isIntrinsicMapLookup = ( + node: t.CallExpression | t.OptionalCallExpression | t.NewExpression, + bindings: JavaScriptSemanticAnalysisState, + index?: Pick, +): boolean | undefined => + isIntrinsicInstanceMethodCall( + node, + bindings, + { + constructor: "Map", + methods: ["get", "delete"], + constructionHooks: ["set"], + }, + index, + ); diff --git a/src/domain/javascript/javascriptSemanticMapOperations.ts b/src/domain/javascript/javascriptSemanticMapOperations.ts deleted file mode 100644 index 469ff7c04e..0000000000 --- a/src/domain/javascript/javascriptSemanticMapOperations.ts +++ /dev/null @@ -1,73 +0,0 @@ -import * as t from "@babel/types"; - -import { - semanticStaticPropertyName, - unwrapJavaScriptExpression, -} from "./javascriptAstValues.js"; -import { isUnmodifiedGlobalReference } from "./javascriptSemanticGlobalReference.js"; -import { semanticOpaqueGlobalMayHaveChanged } from "./javascriptSemanticOpaqueEffects.js"; -import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; -import { - resolveSemanticBindingState, - semanticStableInitializer, - type JavaScriptBindingIndex, - type JavaScriptSemanticAnalysisState, -} from "./javascriptSemanticState.js"; - -/** Prove an unchanged Map operation; undefined requests shared member-mutation facts. */ -export const isIntrinsicMapLookup = ( - node: t.CallExpression | t.OptionalCallExpression | t.NewExpression, - bindings: JavaScriptSemanticAnalysisState, - index?: Pick, -): boolean | undefined => { - const callee = unwrapJavaScriptExpression(node.callee).node; - if (!t.isMemberExpression(callee) && !t.isOptionalMemberExpression(callee)) - return false; - const method = semanticStaticPropertyName(callee.property, callee.computed); - if (method !== "get" && method !== "delete") return false; - const prototypeMemberMayHaveChanged = (member: string): boolean => - ["Map", "Object"].some((owner) => { - const path = ["global", owner, "prototype", member]; - return ( - bindings.ambientWrites.covers(path) || - bindings.ambientEscapes.covers(path) - ); - }); - if (prototypeMemberMayHaveChanged(method)) return false; - let receiver = unwrapJavaScriptExpression(callee.object).node; - const seen = new Set(); - while (t.isIdentifier(receiver)) { - if ( - index !== undefined && - (index.referenceMayHaveChangedAt(node, receiver, [method]) || - index.referenceMayHaveChangedAt(node, receiver, ["__proto__"])) - ) - return false; - const binding = resolveSemanticBindingState( - bindings, - receiver, - receiver.name, - ); - if (binding === undefined || seen.has(binding.bindingId)) return false; - seen.add(binding.bindingId); - const initializer = semanticStableInitializer(binding, bindings); - if ( - initializer === undefined || - initializer.projection.length !== 0 || - !semanticBindingAtRead(binding, receiver, bindings).hasDefiniteInitializer - ) - return false; - receiver = unwrapJavaScriptExpression(initializer.node).node; - } - if ( - !t.isNewExpression(receiver) || - !isUnmodifiedGlobalReference(receiver.callee, bindings, "Map") - ) - return false; - // Iterable construction reads and invokes the instance's set hook. A changed - // adder can install its own get/delete before the constructed value is read. - if (receiver.arguments.length > 0 && prototypeMemberMayHaveChanged("set")) - return false; - if (semanticOpaqueGlobalMayHaveChanged(callee, bindings)) return false; - return index === undefined && seen.size > 0 ? undefined : true; -}; diff --git a/src/domain/javascript/javascriptSemanticMemberMutations.ts b/src/domain/javascript/javascriptSemanticMemberMutations.ts index 765ae7372e..973633deb5 100644 --- a/src/domain/javascript/javascriptSemanticMemberMutations.ts +++ b/src/domain/javascript/javascriptSemanticMemberMutations.ts @@ -5,7 +5,7 @@ import { createGlobalPathReader, retainSemanticAmbientEffect, } from "./javascriptSemanticAmbientWrites.js"; -import { isIntrinsicMapLookup } from "./javascriptSemanticMapOperations.js"; +import { isIntrinsicMapLookup } from "./javascriptSemanticIntrinsicInstances.js"; import { JavaScriptSemanticWorkMeter } from "./javascriptSemanticWorkAllowance.js"; import { isIntrinsicTypedArrayConstruction, @@ -511,7 +511,7 @@ const accessorEffectOperands = ( export function* collectSemanticMemberMutationsSteps( program: t.Program, state: JavaScriptSemanticAnalysisState, - referenceMayHaveChangedAt: JavaScriptBindingIndex["referenceMayHaveChangedAt"], + mayObserve: JavaScriptBindingIndex["mayObserve"], ): Generator { const deferredMapReceivers = new Set< t.CallExpression | t.OptionalCallExpression @@ -2060,11 +2060,7 @@ export function* collectSemanticMemberMutationsSteps( let receiverIndex = 0; for (const node of deferredMapReceivers) { if (receiverIndex++ % 64 === 0) yield; - if ( - isIntrinsicMapLookup(node, state, { referenceMayHaveChangedAt }) === - true - ) - continue; + if (isIntrinsicMapLookup(node, state, { mayObserve }) === true) continue; deferredMapReceivers.delete(node); markReceiver(node.callee, node); } diff --git a/src/domain/javascript/javascriptSemanticMutationInitializers.ts b/src/domain/javascript/javascriptSemanticMutationInitializers.ts index cdc464c200..9a1e441417 100644 --- a/src/domain/javascript/javascriptSemanticMutationInitializers.ts +++ b/src/domain/javascript/javascriptSemanticMutationInitializers.ts @@ -9,6 +9,10 @@ import { javaScriptThisOwner, } from "./javascriptSourceParser.js"; import { semanticPropertyReadSources } from "./javascriptSemanticIterationSources.js"; +import { + isSemanticDirectEvalSite, + semanticGlobalCodeSites, +} from "./javascriptSemanticEffectRecord.js"; import { delayedInstanceFieldValue, @@ -1300,7 +1304,8 @@ const moduleFunctionFirstRead = ( // scope-owned sites are complete before ambient indexing starts. if ( t.isCallExpression(node) && - state?.opaqueEffects.sites.has(node) + state !== undefined && + isSemanticDirectEvalSite(node, state) ) { if (unknownPropertyRead !== null) unknownPropertyRead = @@ -1470,7 +1475,7 @@ const moduleFunctionFirstRead = ( } // Callee resolution can query timing during the ambient walk. Source reads // stay fixed; consume each newly observed opaque call once without rescanning. - const sites = state?.opaqueEffects.globalSites ?? []; + const sites = state === undefined ? [] : semanticGlobalCodeSites(state); while (retained.opaqueSitesIndexed < sites.length) { const site = sites[retained.opaqueSitesIndexed++]; const position = diff --git a/src/domain/javascript/javascriptSemanticOpaqueEffects.ts b/src/domain/javascript/javascriptSemanticOpaqueEffects.ts deleted file mode 100644 index b4905f7bc2..0000000000 --- a/src/domain/javascript/javascriptSemanticOpaqueEffects.ts +++ /dev/null @@ -1,304 +0,0 @@ -import * as t from "@babel/types"; -import { javaScriptProgramStrictness } from "./javascriptSourceParser.js"; -import { unwrapJavaScriptExpression } from "./javascriptAstValues.js"; -import { - isUnshadowedGlobal, - semanticEffectTiming, - semanticBindingEffectFollowsCapture, - semanticBindingEffectFollowsRead, - semanticReadPrecedesEffectWithinActivation, -} from "./javascriptSemanticMutationInitializers.js"; -import { - resolveSemanticBindingState, - type JavaScriptSemanticAnalysisState, - type JavaScriptSemanticBindingState, -} from "./javascriptSemanticState.js"; - -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(); - if (children.has(scope.scopeId)) break; - children.add(scope.scopeId); - state.opaqueEffects.children.set(scope.parentScopeId, children); - scope = state.scopesById.get(scope.parentScopeId); - } - } -}; - -/** The existing ambient invocation walk owns global effects; ordinary sources - * neither retain a second call list nor create another alias resolver. */ -export const createSemanticOpaqueGlobalObserver = ( - state: JavaScriptSemanticAnalysisState, - dependencies: { - createAmbientPathResolver: ( - state: JavaScriptSemanticAnalysisState, - observeGlobals: boolean, - ) => ( - node: t.Node, - site: t.Node | undefined, - ) => { - paths: readonly (readonly (string | null)[])[]; - callerPaths: readonly (readonly (string | null)[])[]; - }; - createSemanticModuleInterop: ( - state: JavaScriptSemanticAnalysisState, - options?: { - intrinsicPath: (node: t.Node) => readonly string[] | undefined; - }, - ) => { - intrinsicPath: (node: t.Node) => readonly string[] | undefined; - intrinsicInvocation: ( - node: - | t.CallExpression - | t.OptionalCallExpression - | t.NewExpression - | t.TaggedTemplateExpression, - ) => { readonly path: readonly string[] } | undefined; - }; - }, -): ((node: t.Node) => void) | undefined => { - if (!state.opaqueEffects.mayReferenceGlobalCode) return undefined; - const ambientPaths = dependencies.createAmbientPathResolver(state, true); - let invocationSite: t.Node | undefined; - const interop = dependencies.createSemanticModuleInterop(state, { - intrinsicPath: (node) => { - const { paths, callerPaths } = ambientPaths(node, invocationSite); - const path = - paths.find((path) => path[1] === "eval" || path[1] === "Function") ?? - callerPaths.find( - (path) => path[1] === "eval" || path[1] === "Function", - ) ?? - paths[0]; - if (path?.[0] !== "global" || path.some((key) => key === null)) - return undefined; - // Keep the global callable owner for generic call/apply/bind resolution. - return path[1] === "eval" || path[1] === "Function" - ? ["intrinsic", ...(path as readonly string[])] - : ["intrinsic", ...(path.slice(1) as readonly string[])]; - }, - }); - return (node) => { - if ( - !t.isCallExpression(node) && - !t.isOptionalCallExpression(node) && - !t.isNewExpression(node) && - !t.isTaggedTemplateExpression(node) - ) - return; - invocationSite = node; - const path = interop.intrinsicInvocation(node)?.path; - // Constructed code may escape and activate a classic-script global. Keep - // its creation as a conservative timing bound, not proof of execution. - if ( - path?.join(".") === "intrinsic.global.eval" || - path?.join(".") === "intrinsic.global.Function" - ) - state.opaqueEffects.globalSites.push(node); - }; -}; - -/** Global code can affect globals, but cannot rebind its caller's locals. */ -export const semanticOpaqueGlobalMayHaveChanged = ( - read: t.Node, - state: JavaScriptSemanticAnalysisState, -): boolean => - state.opaqueEffects.globalSites.some( - (effect) => semanticEffectTiming(read, effect, state) !== "after", - ); - -/** 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 { - 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, - ) && - // Rebinding a freshly initialized local is activation-specific; - // mutations to the referenced object may persist across calls. - !( - kind === "binding" && - semanticBindingEffectFollowsRead( - 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(); - 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" && - // A sloppy-eval name belongs to this activation, not a prior call. - !semanticReadPrecedesEffectWithinActivation( - read, - effect, - state.parentsByNode, - ), - ) - ) - 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) && javaScriptProgramStrictness(node) === "strict") - ) - return true; - const directives = - t.isFunction(node) && t.isBlockStatement(node.body) - ? node.body.directives - : []; - if (directives.some(({ value }) => value.value === "use strict")) - return true; - } - return false; -}; diff --git a/src/domain/javascript/javascriptSemanticReferenceSources.ts b/src/domain/javascript/javascriptSemanticReferenceSources.ts index 3a97d3a157..50ac54517f 100644 --- a/src/domain/javascript/javascriptSemanticReferenceSources.ts +++ b/src/domain/javascript/javascriptSemanticReferenceSources.ts @@ -2,7 +2,6 @@ import type * as t from "@babel/types"; import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js"; import { semanticArrayIndex, - semanticPropertyPathKeyMatches, type JavaScriptSemanticPropertyPath as PropertyPath, } from "./javascriptSemanticPropertyPaths.js"; import { @@ -11,7 +10,7 @@ import { } from "./javascriptSemanticValues.js"; import { semanticSlotAtPath } from "./javascriptSemanticSlots.js"; import type { JavaScriptSemanticValue } from "./javascriptSemanticValueTypes.js"; -import { semanticEffectTiming } from "./javascriptSemanticMutationInitializers.js"; +import { ambientPathMayObserveAt } from "./javascriptSemanticEffectLog.js"; /** * Node's wrapper module inherits require. A copy cannot read that delegate @@ -20,21 +19,13 @@ import { semanticEffectTiming } from "./javascriptSemanticMutationInitializers.j export const semanticCommonJsRequireOwnEnumerableAt = ( capture: t.Node, state: JavaScriptSemanticAnalysisState, -): boolean => { - const selected = ["commonjs-module", "require"]; - const mayAffect = ( - effect: JavaScriptSemanticAnalysisState["ambientWriteEffects"][number], - ) => - effect.path.length <= selected.length && - effect.path.every((key, index) => - semanticPropertyPathKeyMatches(key, selected[index] ?? ""), - ) && - semanticEffectTiming(capture, effect.node, state) !== "after"; - return ( - state.ambientWriteEffects.some(mayAffect) || - state.ambientEscapeEffects.some(mayAffect) +): boolean => + ambientPathMayObserveAt( + state, + capture, + ["commonjs-module", "require"], + "contents", ); -}; interface ReferenceSource { readonly node: t.Node; diff --git a/src/domain/javascript/javascriptSemanticRequireOrigin.ts b/src/domain/javascript/javascriptSemanticRequireOrigin.ts index ba7068aefe..8431984a43 100644 --- a/src/domain/javascript/javascriptSemanticRequireOrigin.ts +++ b/src/domain/javascript/javascriptSemanticRequireOrigin.ts @@ -25,7 +25,6 @@ import { semanticBindingAtRead, semanticBindingHasPriorValueWrite, semanticEffectTiming, - semanticTopLevelEffectFollowsCapture, } from "./javascriptSemanticMutationInitializers.js"; import { semanticStaticPropertyKey, @@ -39,7 +38,14 @@ import { staticSourceRelativeUrlPath, stringValue, } from "./javascriptStaticAnalysisHelpers.js"; -import { createSemanticModuleInterop } from "./javascriptModuleInterop.js"; +import { + createSemanticModuleInterop, + semanticModuleInteropWithAmbientWrites, +} from "./javascriptModuleInterop.js"; +import { + ambientBindingMayObserveWriteAt, + ambientPathMayObserveAt, +} from "./javascriptSemanticEffectLog.js"; import { createSemanticReturnedReferences } from "./javascriptSemanticReturnedReferences.js"; import { semanticIterationSources } from "./javascriptSemanticIterationSources.js"; import { @@ -177,9 +183,7 @@ const requireLoaderValueAt = ( semanticResolutionBlocked(state, loader, "require") ) return "unknown"; - return (state.ambientBindingWrites.get("require") ?? []).some( - (write) => semanticEffectTiming(node, write, state) !== "after", - ) + return ambientBindingMayObserveWriteAt(state, node, "require") ? "unknown" : commonJsLoaderAt(node, state); } @@ -677,35 +681,6 @@ const selectedRequireSourcesAt = ( : undefined; }; -const loaderInvocationInterops = new WeakMap< - JavaScriptSemanticAnalysisState, - { - readonly writes: number; - readonly interop: ReturnType; - } ->(); - -const loaderInvocationInterop = (state: JavaScriptSemanticAnalysisState) => { - const prior = loaderInvocationInterops.get(state); - if (prior?.writes === state.ambientWriteEffects.length) return prior.interop; - const interop = createSemanticModuleInterop(state); - for (const effect of state.ambientWriteEffects) - if (effect.path[0] === "global") - interop.retainWrite(effect.node ?? state.program, [ - "intrinsic", - ...effect.path - .slice(1) - .map((key) => - key === null || typeof key === "object" ? null : String(key), - ), - ]); - loaderInvocationInterops.set(state, { - writes: state.ambientWriteEffects.length, - interop, - }); - return interop; -}; - export interface JavaScriptRequireCallFact { readonly operation: "load" | "resolve"; readonly loader: JavaScriptRequireLoaderFact; @@ -887,7 +862,7 @@ export const semanticRequireCallAt = ( : { sourceRelativePath: selected.sourceRelativePath }), }; } - const interop = loaderInvocationInterop(state); + const interop = semanticModuleInteropWithAmbientWrites(state); if (!t.isNewExpression(node)) { // Invocation normalization retains the selected path identity. Keep its // filename alongside that path rather than selecting the outer call again. @@ -1001,12 +976,6 @@ const isWrapperRequireInitializerCall = ( ); }; -// Ambient indexing finishes before these derived facts are consumed. Keep only -// replacement sites, rather than rescanning unrelated global member writes. -const globalProcessReplacements = new WeakMap< - JavaScriptSemanticAnalysisState, - readonly (t.Node | undefined)[] ->(); type ModuleSourceMatch = "possible" | "no-match" | "incomplete"; type ModuleRequireWrite = { readonly node: t.Node; @@ -1570,7 +1539,7 @@ export const semanticModuleRequireCallAt = ( if (isCommonJsRequireDelegate(target, state, includeUnknown, node)) return true; if (t.isNewExpression(node)) return false; - const interop = loaderInvocationInterop(state); + const interop = semanticModuleInteropWithAmbientWrites(state); return ( interop.intrinsicInvocation(node, (callee) => isCommonJsRequireDelegate(callee, state, false, node) @@ -1649,31 +1618,14 @@ const objectMayProvideRequire = (node: t.Node | undefined): boolean => export const semanticOriginalGlobalProcessAt = ( node: t.Node, state: JavaScriptSemanticAnalysisState, -): boolean => { - let replacements = globalProcessReplacements.get(state); - if (replacements === undefined) { - replacements = [ - ...state.ambientWriteEffects - .filter( - ({ path }) => - path.length <= 2 && - path.every((key, index) => - semanticPropertyPathKeyMatches( - key, - ["global", "process"][index]!, - ), - ), - ) - .map(({ node: write }) => write), - ...(state.ambientBindingWrites.get("process") ?? []), - ]; - globalProcessReplacements.set(state, replacements); - } - return !replacements.some( - (write) => - !semanticTopLevelEffectFollowsCapture(node, write, state.parentsByNode), - ); -}; +): boolean => + !ambientPathMayObserveAt( + state, + node, + ["global", "process"], + "value", + "top-level", + ) && !ambientBindingMayObserveWriteAt(state, node, "process", "top-level"); /** Node's unbound process reference shares the cached process exports object. */ export const semanticGlobalProcessOrigin = ( @@ -1741,10 +1693,7 @@ const isConditionalRequireAt = ( ))) ) return false; - return !(state.ambientBindingWrites.get("require") ?? []).some( - (write) => - !semanticTopLevelEffectFollowsCapture(node, write, state.parentsByNode), - ); + return !ambientBindingMayObserveWriteAt(state, node, "require", "top-level"); }; /** Recover an unshadowed literal require origin and its exact member path. */ @@ -1786,14 +1735,7 @@ export const semanticRequireOriginCandidate = ( (binding === undefined && (javaScriptRuntimeContext(state.program) === "module" || semanticResolutionBlocked(state, current.callee, "require"))) || - (state.ambientBindingWrites.get("require") ?? []).some( - (write) => - !semanticTopLevelEffectFollowsCapture( - current, - write, - state.parentsByNode, - ), - ) + ambientBindingMayObserveWriteAt(state, current, "require", "top-level") ) return undefined; const specifier = stringValue(current.arguments[0]); diff --git a/src/domain/javascript/javascriptSemanticResources.ts b/src/domain/javascript/javascriptSemanticResources.ts index e1cace0a3c..04ac1643b3 100644 --- a/src/domain/javascript/javascriptSemanticResources.ts +++ b/src/domain/javascript/javascriptSemanticResources.ts @@ -21,11 +21,11 @@ import { unwrapJavaScriptExpression, } from "./javascriptAstValues.js"; import { isUnmodifiedGlobalReference } from "./javascriptSemanticGlobalReference.js"; -import { semanticOpaqueBindingReadMayHaveChanged } from "./javascriptSemanticOpaqueEffects.js"; import { - semanticBindingAtRead, - semanticEffectTiming, -} from "./javascriptSemanticMutationInitializers.js"; + opaqueCodeMayRebindBinding, + prototypeMutationMayPrecede, +} from "./javascriptSemanticEffectLog.js"; +import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; import { resolveSemanticBindingState, type JavaScriptSemanticAnalysisState, @@ -457,10 +457,10 @@ const collectReleases = ( const candidates = binding === undefined ? [] : (byBinding.get(binding.bindingId) ?? []); if (binding === undefined || candidates.length === 0) return; - const opaqueReceiver = semanticOpaqueBindingReadMayHaveChanged( + const opaqueReceiver = opaqueCodeMayRebindBinding( binding, - member.object, context.state, + member.object, ); const receiver = candidates.some(({ resultIsPromise }) => resultIsPromise) ? semanticBindingAtRead(binding, member.object, context.state).binding @@ -996,11 +996,7 @@ const rejectsSocketHostMember = ( staticJavaScriptTruthiness(value) === false; // Node reads an absent member through the prototype chain, so a write to // Object.prototype that may run first can supply a path or socket. - const inheritable = state.prototypeMutationSites.some( - (effect) => - effect.owner === "Object" && - semanticEffectTiming(node, effect.node, state) !== "after", - ); + const inheritable = prototypeMutationMayPrecede(state, "Object", node); for (const member of specifier === "tls" ? ["path", "socket"] : ["path"]) { const value = effective(member); if (value === "unknown" || (value !== undefined && !falsy(value))) diff --git a/src/domain/javascript/javascriptSemanticReturnedReferences.ts b/src/domain/javascript/javascriptSemanticReturnedReferences.ts index 6062bb972c..268acdf38b 100644 --- a/src/domain/javascript/javascriptSemanticReturnedReferences.ts +++ b/src/domain/javascript/javascriptSemanticReturnedReferences.ts @@ -1,4 +1,10 @@ import * as t from "@babel/types"; +import { + ambientPathMayObserveAt, + semanticAmbientWriteGeneration, + semanticBindingEffectGeneration, + semanticMemberWriteSources, +} from "./javascriptSemanticEffectLog.js"; import { semanticStaticPropertyKey, @@ -14,10 +20,7 @@ import { resolveJavaScriptGenericInvocation, type JavaScriptSourceInvocationContext, } from "./javascriptIntrinsicInvocation.js"; -import { - semanticBindingAtRead, - semanticEffectTiming, -} from "./javascriptSemanticMutationInitializers.js"; +import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; import { semanticReferenceSourcesInactive, semanticKnownArrayIteration, @@ -97,11 +100,10 @@ export const createSemanticReturnedReferences = ( readonly result: SemanticReturnedReferenceResult; readonly invocation: t.Node | undefined; readonly iterationUnknown: boolean; - readonly ambientWrites: number; + readonly ambientGeneration: number; readonly dependencies: readonly { readonly binding: JavaScriptSemanticBindingState; - readonly writes: number; - readonly escapes: number; + readonly generation: number; }[]; } >(); @@ -111,18 +113,14 @@ export const createSemanticReturnedReferences = ( existing !== undefined && existing.invocation === invocation && existing.iterationUnknown === state.arrayIterationUnknown && - existing.ambientWrites === state.ambientWriteEffects.length && + existing.ambientGeneration === semanticAmbientWriteGeneration(state) && existing.dependencies.every( - ({ binding, writes, escapes }) => - binding.memberWrites.length === writes && - binding.escapedPaths.length === escapes, + ({ binding, generation }) => + semanticBindingEffectGeneration(binding) === generation, ) ) return existing.result; - const dependencies = new Map< - JavaScriptSemanticBindingState, - { readonly writes: number; readonly escapes: number } - >(); + const dependencies = new Map(); const functions = new Set(); const work: Candidate[] = []; // A transition depends on the source read and current projection, not the @@ -152,20 +150,14 @@ export const createSemanticReturnedReferences = ( const sourceGenericInvocation = invocation !== undefined && javaScriptGenericInvocationMethod(invocation.callee) !== undefined; - const inheritedInvocationChanged = (method: "call" | "apply"): boolean => { - if (invocation === undefined) return false; - const inheritedPath = ["global", "Function", "prototype", method]; - return state.ambientWriteEffects.some( - (effect) => - effect.path.length <= inheritedPath.length && - effect.path.every( - (key, index) => - key === null || - semanticPropertyPathKeyMatches(key, inheritedPath[index] ?? ""), - ) && - semanticEffectTiming(invocation, effect.node, state) !== "after", - ); - }; + const inheritedInvocationChanged = (method: "call" | "apply"): boolean => + invocation !== undefined && + ambientPathMayObserveAt(state, invocation, [ + "global", + "Function", + "prototype", + method, + ]); // Exact finite alternatives retain unrelated keys and cross-path // correlations. Growing/shrinking cycles still converge at any depth; // selector-budget widening retains the complete may-target closure. @@ -317,10 +309,7 @@ export const createSemanticReturnedReferences = ( result.unresolvedInvocation = true; continue; } - dependencies.set(binding, { - writes: binding.memberWrites.length, - escapes: binding.escapedPaths.length, - }); + dependencies.set(binding, semanticBindingEffectGeneration(binding)); if (current.path !== undefined) { if ( sourceGenericInvocation && @@ -413,10 +402,9 @@ export const createSemanticReturnedReferences = ( }); } if (current.path === undefined) - for (const effect of binding.memberWrites) - for (const source of effect.assignedSources ?? []) - if (!semanticReferenceSourcesInactive(source, state)) - pending.push({ node: source.node, path: undefined }); + for (const source of semanticMemberWriteSources(binding)) + if (!semanticReferenceSourcesInactive(source, state)) + pending.push({ node: source.node, path: undefined }); } else if ( t.isMemberExpression(node) || t.isOptionalMemberExpression(node) @@ -737,10 +725,10 @@ export const createSemanticReturnedReferences = ( result, invocation, iterationUnknown: state.arrayIterationUnknown, - ambientWrites: state.ambientWriteEffects.length, - dependencies: [...dependencies].map(([binding, counts]) => ({ + ambientGeneration: semanticAmbientWriteGeneration(state), + dependencies: [...dependencies].map(([binding, generation]) => ({ binding, - ...counts, + generation, })), }); if (result.precision === "widened") diff --git a/src/domain/javascript/javascriptSemanticState.ts b/src/domain/javascript/javascriptSemanticState.ts index 81bc675bae..e1a29d3a12 100644 --- a/src/domain/javascript/javascriptSemanticState.ts +++ b/src/domain/javascript/javascriptSemanticState.ts @@ -258,11 +258,16 @@ export interface JavaScriptBindingIndex { referenceNode: t.Node, propertyPath?: JavaScriptSemanticPropertyPath, ): JavaScriptSemanticReferenceEffectsAtSite; - /** Stop at the first relevant prior/uncertain effect without retaining a report. */ - referenceMayHaveChangedAt( - site: t.Node, - referenceNode: t.Node, - propertyPath?: JavaScriptSemanticPropertyPath, + /** + * The effect log's read query: whether reading `target` (at `path`, or the + * member path `target` spells) at `read` may observe a recorded effect. It + * stops at the first such effect, and answers true until member-mutation + * facts are complete. + */ + mayObserve( + read: t.Node, + target: t.Node, + path?: JavaScriptSemanticPropertyPath, ): boolean; referencesSteps(): Generator; } diff --git a/src/domain/javascript/javascriptSemanticTypedArrays.ts b/src/domain/javascript/javascriptSemanticTypedArrays.ts index 7be386c6c3..ef32ac396b 100644 --- a/src/domain/javascript/javascriptSemanticTypedArrays.ts +++ b/src/domain/javascript/javascriptSemanticTypedArrays.ts @@ -1,8 +1,8 @@ import * as t from "@babel/types"; import { unwrapJavaScriptExpression } from "./javascriptAstValues.js"; -import { isUnmodifiedGlobalReference } from "./javascriptSemanticGlobalReference.js"; -import { semanticOpaqueGlobalMayHaveChanged } from "./javascriptSemanticOpaqueEffects.js"; +import { isIntrinsicGlobalReference } from "./javascriptSemanticGlobalReference.js"; +import { ambientPathMayHaveChanged } from "./javascriptSemanticEffectLog.js"; import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js"; const typedArrayConstructors = [ @@ -25,11 +25,10 @@ export const isIntrinsicTypedArrayConstruction = ( node: t.NewExpression, state: JavaScriptSemanticAnalysisState, ): boolean => - !semanticOpaqueGlobalMayHaveChanged(node, state) && typedArrayConstructors.some( (name) => - isUnmodifiedGlobalReference(node.callee, state, name) && - !state.ambientEscapes.covers(["global", name]), + isIntrinsicGlobalReference(node.callee, state, name, node) && + !ambientPathMayHaveChanged(state, ["global", name], "contents"), ); /** A successful evaluation of these expressions produces a Number, not a key object. */ diff --git a/src/domain/javascript/javascriptSemanticValues.ts b/src/domain/javascript/javascriptSemanticValues.ts index 363c9cb3a6..37a3cafc4e 100644 --- a/src/domain/javascript/javascriptSemanticValues.ts +++ b/src/domain/javascript/javascriptSemanticValues.ts @@ -1,9 +1,8 @@ import type { JavaScriptSemanticPrecision } from "./javascriptSemanticPrecision.js"; import { - semanticBindingMayHaveOpaqueEffect, - semanticBindingCanBeRebound, - semanticOpaqueBindingReadMayHaveChanged, -} from "./javascriptSemanticOpaqueEffects.js"; + applySemanticBindingReferenceEffects, + opaqueCodeMayRebindBinding, +} from "./javascriptSemanticEffectLog.js"; import * as t from "@babel/types"; import { semanticKnownSymbolKey } from "./javascriptSemanticPropertyKeys.js"; import { logicalRightOperand } from "./javascriptSymbolicControlFlow.js"; @@ -15,8 +14,6 @@ import { import { semanticCapturePosition, - semanticEscapeFollowsCapture, - semanticBindingEffectFollowsCapture, semanticBindingPrecedesCapture, semanticBindingAtRead, } from "./javascriptSemanticMutationInitializers.js"; @@ -26,11 +23,6 @@ import { semanticContainer, } from "./javascriptSemanticSlots.js"; -import { - invalidateSemanticEscapedPath, - invalidateSemanticMutationPaths, -} from "./javascriptSemanticMutationValues.js"; - import type { JavaScriptBindingProvenance } from "./javascriptSemanticIr.js"; import type { JavaScriptSemanticProperty, @@ -233,8 +225,7 @@ export const evaluateSemanticBinding = ( binding: JavaScriptSemanticBindingState, state: JavaScriptSemanticAnalysisState, ): JavaScriptSemanticValue => - semanticBindingCanBeRebound(binding) && - semanticBindingMayHaveOpaqueEffect(binding, state) + opaqueCodeMayRebindBinding(binding, state) ? { status: "unknown", reason: "Direct eval may have changed this lexical binding.", @@ -297,8 +288,7 @@ export const evaluateSemanticProvenance = ( dependence: { lowest: CALLER_INDEPENDENT }, memo: createEvaluationMemo(state), }); - return semanticBindingCanBeRebound(binding) && - semanticBindingMayHaveOpaqueEffect(binding, state) + return opaqueCodeMayRebindBinding(binding, state) ? semanticAmbiguousProvenance( provenance.origins, "Direct eval may have changed this runtime value.", @@ -551,50 +541,12 @@ const evaluateBinding = ( ? callerDependence : Math.min(callerDependence, context.dependence.lowest); } - let projected = invalidateSemanticMutationPaths( + const projected = applySemanticBindingReferenceEffects( value, - binding.memberWrites - .filter( - ({ node }) => - context.capturePoint === undefined || - node === undefined || - !semanticBindingEffectFollowsCapture( - binding, - context.capturePoint, - node, - context.state.parentsByNode, - ), - ) - .map(({ path }) => path), + binding, + context.state, + context.capturePoint, ); - for (const escape of binding.escapedPaths) { - // Escapes only invalidate object references; other lattice values survive. - if (projected.status !== "object" && projected.status !== "array") break; - if ( - context.capturePoint !== undefined && - semanticEscapeFollowsCapture( - binding, - context.capturePoint, - escape.node, - context.state.parentsByNode, - ) - ) - continue; - projected = invalidateSemanticEscapedPath(projected, escape.path); - } - if ( - (projected.status === "object" || projected.status === "array") && - semanticBindingMayHaveOpaqueEffect( - binding, - context.state, - context.capturePoint, - "reference", - ) - ) - projected = { - status: "unknown", - reason: "Direct eval may have mutated this object reference.", - }; if ( callerIndependent && independent !== undefined && @@ -708,10 +660,10 @@ const evaluateExpressionValue = ( } const atRead = read.binding; if ( - semanticOpaqueBindingReadMayHaveChanged( + opaqueCodeMayRebindBinding( binding, - context.capturePoint ?? node, context.state, + context.capturePoint ?? node, ) ) return { @@ -1410,7 +1362,7 @@ const provenanceForBindingAtRead = ( read: t.Identifier, context: EvaluationContext, ): JavaScriptBindingProvenance => { - if (semanticOpaqueBindingReadMayHaveChanged(binding, read, context.state)) + if (opaqueCodeMayRebindBinding(binding, context.state, read)) return semanticAmbiguousProvenance( provenanceForBinding(binding, context).origins, "Direct eval may have changed this runtime value.", diff --git a/src/domain/javascript/javascriptStaticAnalysis.mapEndpoints.test.ts b/src/domain/javascript/javascriptStaticAnalysis.mapEndpoints.test.ts index b8c1436c16..16869ca09f 100644 --- a/src/domain/javascript/javascriptStaticAnalysis.mapEndpoints.test.ts +++ b/src/domain/javascript/javascriptStaticAnalysis.mapEndpoints.test.ts @@ -63,6 +63,7 @@ it.each([ 'let records = new Map(); records = client; records.get("/accounts")', 'function inspect(Map) { new Map().get("/accounts"); }', 'Map.prototype.get = request; const records = new Map(); records.get("/accounts")', + 'Map.prototype.__proto__ = { get: fetch }; new Map().get("/accounts")', 'const records = new Map(); records.__proto__ = { get: fetch }; records.get("/accounts")', 'const records = new Map(); const alias = records; alias.__proto__ = { get: fetch }; records.get("/accounts")', 'const records = new Map(); records["__proto__"] = { delete: fetch }; records.delete("/accounts")', diff --git a/src/domain/javascript/javascriptStaticAnalysis.mapOpaqueEffects.test.ts b/src/domain/javascript/javascriptStaticAnalysis.mapOpaqueEffects.test.ts index b2c468a185..a240391ee5 100644 --- a/src/domain/javascript/javascriptStaticAnalysis.mapOpaqueEffects.test.ts +++ b/src/domain/javascript/javascriptStaticAnalysis.mapOpaqueEffects.test.ts @@ -28,6 +28,9 @@ it.each([ 'function invoke() { run("Map.prototype.get = fetch"); } const run = eval; invoke(); new Map().get("/accounts");', 'function getRun() { return eval; } getRun()("Map.prototype.get = fetch"); new Map().get("/accounts");', 'Reflect.apply(eval, null, ["Map.prototype.get = fetch"]); new Map().get("/accounts");', + // The lookup, not the constructor reference, bounds global-code timing. + 'const map = new Map(); eval("Map.prototype.get = fetch"); map.get("/accounts");', + 'const map = new Map(); Function("Map.prototype.get = fetch")(); map.get("/accounts");', ])( "keeps a Map lookup after possible opaque mutation as a candidate: %s", (source) => { @@ -47,6 +50,7 @@ it.each([ 'function inspect(eval) { const run = eval; run("Map.prototype.get = fetch"); return new Map().get("/accounts"); }', 'let run = eval; run = () => {}; run("Map.prototype.get = fetch"); new Map().get("/accounts");', 'eval.bind(null); new Map().get("/accounts");', + 'const map = new Map(); map.get("/accounts"); eval("Map.prototype.get = fetch");', ])( "preserves a Map proof when eval cannot precede the lookup: %s", (source) => { diff --git a/src/domain/javascript/javascriptStaticAnalysis.partialEndpoints.test.ts b/src/domain/javascript/javascriptStaticAnalysis.partialEndpoints.test.ts index d516dd49b0..48808339a5 100644 --- a/src/domain/javascript/javascriptStaticAnalysis.partialEndpoints.test.ts +++ b/src/domain/javascript/javascriptStaticAnalysis.partialEndpoints.test.ts @@ -232,6 +232,37 @@ it("resolves an unchanged URL object's toString target", () => { ]); }); +// Coercing a URL object reads its conversion methods through URL.prototype +// and its prototype chain; an explicit toString() call reads one of them. +it.each([ + ["URL.prototype escapes", "configure(URL.prototype);", "socket.toString()"], + [ + "Object.prototype escapes", + "configure(Object.prototype);", + "socket.toString()", + ], + [ + "URL.prototype's prototype link changes", + "URL.prototype.__proto__ = proxy;", + "socket", + ], +])("keeps a URL object target partial after %s", (_, effect, target) => { + expect( + endpoints(` + ${effect} + const socket = new URL("wss://api.example.test/socket"); + new WebSocket(${target}); + `), + ).toEqual([ + { + kind: "network", + value: target, + mechanism: "call:WebSocket", + resolution: "partial", + }, + ]); +}); + it("keeps a URL object partial when its base is unresolved", () => { expect( endpoints(` diff --git a/src/domain/javascript/javascriptStaticAnalysisCalls.ts b/src/domain/javascript/javascriptStaticAnalysisCalls.ts index 2edbc99c8b..a52cb8b991 100644 --- a/src/domain/javascript/javascriptStaticAnalysisCalls.ts +++ b/src/domain/javascript/javascriptStaticAnalysisCalls.ts @@ -3,7 +3,10 @@ import { isFetchCall, } from "./javascriptNetworkRequests.js"; import * as t from "@babel/types"; -import { isIntrinsicMapLookup } from "./javascriptSemanticMapOperations.js"; +import { + intrinsicPrototypeMemberMayHaveChanged, + isIntrinsicMapLookup, +} from "./javascriptSemanticIntrinsicInstances.js"; import { traverseJavaScriptValueAst } from "./javascriptSemanticTraversal.js"; import { semanticIdentifierRole } from "./javascriptSemanticProjection.js"; @@ -781,7 +784,7 @@ const resolvedRequestTarget = ( // A toString result is selected before the enclosing request invocation. const expression = unwrapJavaScriptExpression(node).node; const capture = reference === expression ? site : expression; - if (index.referenceMayHaveChangedAt(capture, reference)) return undefined; + if (index.mayObserve(capture, reference)) return undefined; if (!requestInitializerPrecedesSite(reference, capture, bindings, index)) return undefined; const evaluated = evaluateSemanticExpression(node, bindings); @@ -849,7 +852,7 @@ const urlObjectRequestTarget = ( isToString = true; } } - if (!t.isIdentifier(target) || index.referenceMayHaveChangedAt(site, target)) + if (!t.isIdentifier(target) || index.mayObserve(site, target)) return undefined; const binding = index.bindingForReferenceAt(target); if (binding === undefined || seen.has(binding.bindingId)) return undefined; @@ -884,10 +887,8 @@ const urlObjectRequestTarget = ( if (!isUnmodifiedGlobalReference(initialized.callee, bindings, "URL")) return undefined; if ( - ["URL", "Object"].some((owner) => - ["toString", "valueOf"].some((method) => - bindings.ambientWrites.covers(["global", owner, "prototype", method]), - ), + ["toString", "valueOf"].some((method) => + intrinsicPrototypeMemberMayHaveChanged(bindings, "URL", method), ) ) return undefined; @@ -1002,7 +1003,7 @@ export const finishSqliteStorageCalls = ( for (const { node, name } of context.accumulator.pendingSqliteCalls) { const callee = unwrapJavaScriptExpression(node.callee).node; const provenance = evaluateSemanticExpressionProvenance(callee, bindings); - const changed = index.referenceMayHaveChangedAt(node, callee); + const changed = index.mayObserve(node, callee); if (!changed && t.isIdentifier(callee) && provenance.status === "local") continue; const origin = diff --git a/src/domain/webBundleAnalyzer.test.ts b/src/domain/webBundleAnalyzer.test.ts index b1912b419f..0e30b438a9 100644 --- a/src/domain/webBundleAnalyzer.test.ts +++ b/src/domain/webBundleAnalyzer.test.ts @@ -2226,6 +2226,10 @@ it.each([ ["XMLHttpRequest.prototype.open = replacement;", false], ["const alias = xhr; alias.open = replacement;", false], ["mutate(xhr);", false], + // Global code between construction and the call may patch the prototype, + // so the call, not the constructor reference, bounds its timing. + ['eval("XMLHttpRequest.prototype.open = replacement");', false], + ['Function("XMLHttpRequest.prototype.open = replacement")();', false], ])( "preserves XHR receiver proof across visible mutation: %s", (mutation, observed) => { diff --git a/src/domain/webBundleAnalyzerInspection.ts b/src/domain/webBundleAnalyzerInspection.ts index 54b64fcffd..4b5e041fda 100644 --- a/src/domain/webBundleAnalyzerInspection.ts +++ b/src/domain/webBundleAnalyzerInspection.ts @@ -23,7 +23,7 @@ import { } from "./javascript/javascriptSemanticTraversal.js"; import { indexJavaScriptBindingsWithReceiversSteps } from "./javascript/javascriptSemanticAnalysis.js"; import type { JavaScriptOpenReceiverFact } from "./javascript/javascriptSemanticReceiverFacts.js"; -import { isIntrinsicMapLookup } from "./javascript/javascriptSemanticMapOperations.js"; +import { isIntrinsicMapLookup } from "./javascript/javascriptSemanticIntrinsicInstances.js"; import { semanticStaticPropertyKey, isNullishJavaScriptSyntax,