From 473b3ee10e8e0cef1a5dbcca7c294220c287fb29 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:58:10 -0500 Subject: [PATCH] fix(javascript): preserve nested array iterator dependencies (#12558) * fix(javascript): preserve nested array iterator dependencies * fix(javascript): retain reflected array prototype identity * fix(javascript): guard native arrays with ambient prototype effects Reuse the shared effect-log classifier first prepared for dense inline spread analysis. * test(javascript): keep array prototype alias fixture with sources --- ...vaScriptSemanticGraphDestructuring.test.ts | 128 ++++++++++++++++++ .../javascriptSemanticAmbientWrites.test.ts | 24 ++++ .../javascriptSemanticAmbientWrites.ts | 5 +- .../javascript/javascriptSemanticEffectLog.ts | 33 +++++ .../javascriptSemanticMemberMutations.ts | 31 ++--- .../javascript/javascriptSemanticObjects.ts | 13 +- .../javascriptSemanticReadPresence.ts | 27 +++- .../javascriptDestructuringEvidence.test.ts | 28 +++- 8 files changed, 256 insertions(+), 33 deletions(-) diff --git a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts index 63f23e08b8..907b98a884 100644 --- a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts @@ -1778,3 +1778,131 @@ it.each([ ).toEqual(path); }, ); + +it.each([ + [ + "inline array", + 'const pairs=[["first"]];const [[selected]]=pairs;pairs[0][0]="later";', + ["0", "0"], + "resolved", + ], + [ + "const alias", + 'const pairs=[["first"]];const alias=pairs;const [[selected]]=alias;pairs[0][0]="later";', + ["0", "0"], + "resolved", + ], + [ + "assignment", + 'const pairs=[["first"]];let selected;[[selected]]=pairs;pairs[0][0]="later";', + ["0", "0"], + "resolved", + ], + [ + "projected array RHS", + 'const values={pairs:[["first"]]};const [[selected]]=values.pairs;values.pairs[0][0]="later";', + ["pairs", "0", "0"], + "resolved", + ], + [ + "outer elision", + 'const pairs=[0,["first"]];const [,[selected]]=pairs;pairs[1][0]="later";', + ["1", "0"], + "resolved", + ], + [ + "outer hole before the selected inner array", + 'const pairs=[,["first"]];const [,[selected]]=pairs;pairs[1][0]="later";', + ["1", "0"], + "candidate", + ], + [ + "prior write through alias", + 'const pairs=[["first"]];const alias=pairs;alias[0][0]="earlier";const [[selected]]=pairs;', + ["0", "0"], + "candidate", + ], + [ + "prior ordinary default", + 'const pairs=[undefined,["first"]];const [unused=(pairs[1][0]="earlier"),[selected]]=pairs;', + ["1", "0"], + "candidate", + ], + [ + "inner custom iterator", + 'const pairs=[["first"]];pairs[0][Symbol.iterator]=function*(){yield "other";};const [[selected]]=pairs;', + ["0", "0"], + "candidate", + ], + [ + "Object.getPrototypeOf aliases the shared array prototype", + 'const proto=Object.getPrototypeOf([]);proto[Symbol.iterator]=function*(){};const pairs=[["first"]];const [[selected]]=pairs;pairs[0][0]="later";', + ["0", "0"], + "candidate", + ], + [ + "an earlier call writes through Reflect.getPrototypeOf", + 'change();function change(){const proto=Reflect.getPrototypeOf([]);proto[Symbol.iterator]=function*(){};}const pairs=[["first"]];const [[selected]]=pairs;pairs[0][0]="later";', + ["0", "0"], + "candidate", + ], + [ + "an unrelated property on the shared array prototype", + 'const proto=Reflect.getPrototypeOf([]);proto.note=1;const pairs=[["first"]];const [[selected]]=pairs;pairs[0][0]="later";', + ["0", "0"], + "resolved", + ], + [ + "a lexical Object replacement returns a fresh receiver", + 'const Object={getPrototypeOf:()=>({})};const proto=Object.getPrototypeOf([]);proto[Symbol.iterator]=function*(){};const pairs=[["first"]];const [[selected]]=pairs;pairs[0][0]="later";', + ["0", "0"], + "resolved", + ], + [ + "shared next changed before acquisition", + 'const pairs=[["first"]];Object.getPrototypeOf([].values()).next=()=>({value:"other",done:false});const [[selected]]=pairs;pairs[0][0]="later";', + ["0", "0"], + "candidate", + ], + [ + "shared next changed after acquisition", + 'const pairs=[["first"]];const [[selected]]=pairs;Object.getPrototypeOf([].values()).next=()=>({value:"other",done:false});pairs[0][0]="later";', + ["0", "0"], + "resolved", + ], + [ + "prior inner close can change this array", + 'const pairs=[[0],["second"]];pairs[0][1]=pairs[1];Object.getPrototypeOf([].values()).return=function(){const value=this.next().value;if(value)delete value[0];return {};};const [[used],[selected]]=pairs;', + ["1", "0"], + "candidate", + ], + [ + "proof stops at the inner close", + 'const pairs=[[0],"next"];const [[used],selected]=pairs;pairs[1]="later";', + ["1"], + "candidate", + ], + [ + "third iterator", + 'const pairs=[[["first"]]];const [[[selected]]]=pairs;pairs[0][0][0]="later";', + ["0", "0", "0"], + "candidate", + ], +] as const)( + "composes one native array inside another for %s", + (_name, code, path, resolution) => { + const graph = graphForJavaScript(code); + const selected = graph.nodes.find( + (n) => n.kind === "binding" && n.label === "selected", + ); + const relation = graph.relations.find( + (r) => + r.relation === "destructures" && r.target_node_id === selected?.node_id, + ); + expect(relation).toMatchObject({ resolution }); + expect( + graph.nodes.find((n) => n.node_id === relation?.source_node_id) + ?.properties.property_path, + ).toEqual(path); + }, +); diff --git a/src/domain/javascript/javascriptSemanticAmbientWrites.test.ts b/src/domain/javascript/javascriptSemanticAmbientWrites.test.ts index 686ef86535..e7a921c8d4 100644 --- a/src/domain/javascript/javascriptSemanticAmbientWrites.test.ts +++ b/src/domain/javascript/javascriptSemanticAmbientWrites.test.ts @@ -460,6 +460,30 @@ function expectAmbientEffectPaths({ }); } +it.each([ + { + source: + "const proto=Object.getPrototypeOf([]);proto[Symbol.iterator]=function*(){};", + writes: [["global", "Array", "prototype", null]], + escapes: [], + }, + { + source: + "change();function change(){const proto=Reflect.getPrototypeOf([]);proto[Symbol.iterator]=function*(){};}", + writes: [["global", "Array", "prototype", null]], + escapes: [], + }, + { + source: + "const Object={getPrototypeOf:()=>({})};const proto=Object.getPrototypeOf([]);proto[Symbol.iterator]=function*(){};", + writes: [], + escapes: [], + }, +])( + "keeps native array prototype identity through $source", + expectAmbientEffectPaths, +); + it.each([ { source: "(function(g){})(this);", writes: [], escapes: [] }, { source: "(function(){})(this);", writes: [], escapes: [] }, diff --git a/src/domain/javascript/javascriptSemanticAmbientWrites.ts b/src/domain/javascript/javascriptSemanticAmbientWrites.ts index ced6fcb84d..685cbde8ea 100644 --- a/src/domain/javascript/javascriptSemanticAmbientWrites.ts +++ b/src/domain/javascript/javascriptSemanticAmbientWrites.ts @@ -848,7 +848,10 @@ export const createAmbientPathResolver = ( continueWith(property.value, suffix.slice(1)); } } else if (t.isArrayExpression(root)) { - if (suffix === undefined) { + if (possibleWrites && suffix?.[0] === "__proto__") { + retain(["global", "Array", "prototype", ...suffix.slice(1)]); + resolvedIdentity = true; + } else if (suffix === undefined) { for (const element of root.elements) if (t.isSpreadElement(element)) continueWith(element.argument); else if (t.isExpression(element)) continueWith(element); diff --git a/src/domain/javascript/javascriptSemanticEffectLog.ts b/src/domain/javascript/javascriptSemanticEffectLog.ts index 3a4d831a09..a84d4d92f2 100644 --- a/src/domain/javascript/javascriptSemanticEffectLog.ts +++ b/src/domain/javascript/javascriptSemanticEffectLog.ts @@ -11,6 +11,7 @@ import { unwrapJavaScriptExpression, } from "./javascriptAstValues.js"; import { + semanticArrayIndex, semanticPropertyPathKeyMatches, type JavaScriptSemanticPropertyPath, } from "./javascriptSemanticPropertyPaths.js"; @@ -377,6 +378,38 @@ export const semanticEffectMayFollow = ( type AmbientEffect = JavaScriptSemanticAnalysisState["ambientWriteEffects"][number]; +/** Which effects on the native prototype chain can alter array iteration. */ +export const semanticArrayPrototypeIterationMayChange = ( + path: JavaScriptSemanticPropertyPath, +): boolean => { + let offset = 0; + while (path[offset] === "constructor" && path[offset + 1] === "prototype") + offset += 2; + const key = path[offset]; + return ( + key === undefined || + key === null || + typeof key === "object" || + semanticArrayIndex(key) !== null || + key === "__proto__" + ); +}; + +/** Seed the iteration guard from the earlier, alias-aware ambient effect index. */ +export const semanticAmbientArrayIterationUnknown = ( + state: JavaScriptSemanticAnalysisState, +): boolean => { + const changesIteration = ({ path }: AmbientEffect): boolean => + path[0] === "global" && + (path[1] === "Array" || path[1] === "Object" || path[1] === null) && + (path[2] === "prototype" || path[2] === null) && + semanticArrayPrototypeIterationMayChange(path.slice(3)); + return ( + state.ambientWriteEffects.some(changesIteration) || + state.ambientEscapeEffects.some(changesIteration) + ); +}; + type MemberWrite = JavaScriptSemanticBindingState["memberWrites"][number]; /** Matching effects per queried path; the effect lists only grow, so their diff --git a/src/domain/javascript/javascriptSemanticMemberMutations.ts b/src/domain/javascript/javascriptSemanticMemberMutations.ts index ed46601e98..7a4139c6f8 100644 --- a/src/domain/javascript/javascriptSemanticMemberMutations.ts +++ b/src/domain/javascript/javascriptSemanticMemberMutations.ts @@ -42,6 +42,8 @@ import { import { bindingHasMemberWrites, bindingProjectedContentsMayChangeAt, + semanticAmbientArrayIterationUnknown, + semanticArrayPrototypeIterationMayChange, } from "./javascriptSemanticEffectLog.js"; import { semanticContainer } from "./javascriptSemanticSlots.js"; import { @@ -126,23 +128,6 @@ interface MutationSelectionContext { type ValueEffect = "write" | "escape"; -/** Only replacing or possibly selecting an iterator-bearing prototype slot changes iteration. */ -const arrayPrototypeIterationMayChange = (path: PropertyPath): boolean => { - // Array.prototype and Object.prototype are on native arrays' prototype - // chain. Their default constructor.prototype pairs return to the same owner. - let offset = 0; - while (path[offset] === "constructor" && path[offset + 1] === "prototype") - offset += 2; - const key = path[offset]; - return ( - key === undefined || - key === null || - typeof key === "object" || - semanticArrayIndex(key) !== null || - key === "__proto__" - ); -}; - /** Preserve uncertainty from property writes and references exposed to calls, yielding during traversal and fixpoint iteration. */ /** Local methods use the same slots, prototype shadowing and array indices as aliases. */ @@ -782,7 +767,7 @@ export function* collectSemanticMemberMutationsSteps( recordRequiredEffect("node:process", path.slice(2), "write", node); const selections = new Map(); const coveringPaths = new Map(); - state.arrayIterationUnknown = false; + state.arrayIterationUnknown = semanticAmbientArrayIterationUnknown(state); // Definition collection finishes before this phase, so a scope's bindings no // longer change and one node resolves to one binding. Repeated visits of the // same node otherwise repeat the same scope walk. @@ -1135,14 +1120,16 @@ export function* collectSemanticMemberMutationsSteps( if ( (["Array", "Object"].includes(expression.name) && (current.path[0] === "prototype" || current.path[0] === null) && - arrayPrototypeIterationMayChange(current.path.slice(1))) || + semanticArrayPrototypeIterationMayChange( + current.path.slice(1), + )) || (["globalThis", "global", "window", "self"].includes( expression.name, ) && (["Array", "Object"].includes(String(current.path[0])) || current.path[0] === null) && (current.path[1] === "prototype" || current.path[1] === null) && - arrayPrototypeIterationMayChange(current.path.slice(2))) + semanticArrayPrototypeIterationMayChange(current.path.slice(2))) ) state.arrayIterationUnknown = true; if (lookup?.kind !== "uncertain-binding") continue; @@ -1669,10 +1656,10 @@ export function* collectSemanticMemberMutationsSteps( t.isArrayExpression(expression) && ((path[0] === "__proto__" && (effect === "escape" || path.length > 1) && - arrayPrototypeIterationMayChange(path.slice(1))) || + semanticArrayPrototypeIterationMayChange(path.slice(1))) || (path[0] === "constructor" && path[1] === "prototype" && - arrayPrototypeIterationMayChange(path.slice(2)))) + semanticArrayPrototypeIterationMayChange(path.slice(2)))) ) state.arrayIterationUnknown = true; const references = referencedValues( diff --git a/src/domain/javascript/javascriptSemanticObjects.ts b/src/domain/javascript/javascriptSemanticObjects.ts index 09f1923c3a..03c93ff62b 100644 --- a/src/domain/javascript/javascriptSemanticObjects.ts +++ b/src/domain/javascript/javascriptSemanticObjects.ts @@ -195,8 +195,8 @@ const collectDestructuring = ( if (t.isObjectPattern(pattern)) { // A nested pattern needs a proved object even if it has no children. if ( - (rootArray && !t.isObjectExpression(selected)) || - (!rootArray && ownSlot !== sourcePresence?.read) + ownSlot !== sourcePresence?.read || + (rootArray && !t.isObjectExpression(selected)) ) ownSlot = undefined; for (const property of pattern.properties.toReversed()) { @@ -221,12 +221,17 @@ const collectDestructuring = ( if (t.isArrayPattern(pattern)) { if (pattern !== root) { ownSlot = - !rootArray && ownSlot !== undefined && ownSlot === sourcePresence?.read && + (!rootArray || path.length === 1) && t.isArrayExpression(selected) && propertyPath !== null - ? sourcePresence?.readNestedArray?.(propertyPath, path, pattern) + ? sourcePresence?.readNestedArray?.( + selected, + propertyPath, + path, + pattern, + ) : undefined; // IteratorClose can run after these elements and before the next // sibling. Reaching this boundary ends the current presence proof. diff --git a/src/domain/javascript/javascriptSemanticReadPresence.ts b/src/domain/javascript/javascriptSemanticReadPresence.ts index d88325ca2d..0aba17a820 100644 --- a/src/domain/javascript/javascriptSemanticReadPresence.ts +++ b/src/domain/javascript/javascriptSemanticReadPresence.ts @@ -14,6 +14,7 @@ import { semanticArrayIndex } from "./javascriptSemanticPropertyPaths.js"; import { semanticKnownArrayIteration } from "./javascriptSemanticReferenceSources.js"; import { semanticSlotAtPath } from "./javascriptSemanticSlots.js"; import { evaluateSemanticExpression } from "./javascriptSemanticValues.js"; +import type { JavaScriptSemanticValue } from "./javascriptSemanticValueTypes.js"; import { resolveSemanticBindingState, semanticStableInitializer, @@ -34,6 +35,7 @@ type ReadOwnData = ( interface ReadPresence { readonly read: ReadOwnData; readonly readNestedArray?: ( + literal: t.ArrayExpression, path: readonly string[], fromRhs: readonly string[], acquisition: t.ArrayPattern, @@ -150,15 +152,20 @@ export const createSemanticReadPresenceQuery = ( ) return undefined; const { value } = origin; + let rhsSnapshot: JavaScriptSemanticValue | undefined; const arraySlots = ( + literal: t.ArrayExpression, arrayPath: readonly string[], fromRhs: readonly string[], acquisition: t.Node | undefined, ): ReadOwnData | undefined => { - const literal = ownDataAtPath(value, arrayPath); - if (!t.isArrayExpression(literal)) return undefined; - const base = evaluateSemanticExpression(capture, state, capture, "query"); - const selectedArray = semanticSlotAtPath(base, fromRhs); + rhsSnapshot ??= evaluateSemanticExpression( + capture, + state, + capture, + "query", + ); + const selectedArray = semanticSlotAtPath(rhsSnapshot, fromRhs); const array = selectedArray.value; if ( selectedArray.presence !== "present" || @@ -211,12 +218,22 @@ export const createSemanticReadPresenceQuery = ( ? [] : undefined); if (arrayPath !== undefined) { + const literal = ownDataAtPath(value, arrayPath); + if (!t.isArrayExpression(literal)) return undefined; const read = arraySlots( + literal, arrayPath, [], iterationPath === undefined ? undefined : capture, ); - return read === undefined ? undefined : { read }; + return read === undefined + ? undefined + : { + read, + ...(iterationPath === undefined + ? {} + : { readNestedArray: arraySlots }), + }; } if (!t.isObjectExpression(value)) return undefined; const read: ReadOwnData = (path, beforeIterator) => { diff --git a/tests/boundary/cli/javascriptDestructuringEvidence.test.ts b/tests/boundary/cli/javascriptDestructuringEvidence.test.ts index b1f5a308c0..d30cabf5ff 100644 --- a/tests/boundary/cli/javascriptDestructuringEvidence.test.ts +++ b/tests/boundary/cli/javascriptDestructuringEvidence.test.ts @@ -26,6 +26,17 @@ const iteratorCloseSource = ` const { useCustom = (holder.parent.items = custom), items: [read], next: { mode: projectedAfterAcquisition } } = holder.parent; `; +const arrayPrototypeAliasSource = ` + change(); + function change() { + const proto = Reflect.getPrototypeOf([]); + proto[Symbol.iterator] = function* () {}; + } + const pairs = [["first"]]; + const [[aliasedPrototypeElement]] = pairs; + pairs[0][0] = "later"; + `; + const expectedDependencies = [ ["declared", ["a"]], ["a", ["a"]], @@ -38,6 +49,8 @@ const expectedDependencies = [ ["projected", ["items", "0"]], ["arrayObject", ["0", "mode"]], ["nestedElement", ["items", "0"]], + ["innerElement", ["0", "0"]], + ["aliasedPrototypeElement", ["0", "0"], "candidate"], ["selected", ["0"], "candidate"], ["beforeClose", ["label"]], ["consumed", ["items", "0", "mode"]], @@ -93,6 +106,9 @@ cliTest( const nestedSource = { items: ["nested"] }; const { items: [nestedElement] } = nestedSource; nestedSource.items[0] = "later"; + const nestedPairs = [["read-time"]]; + const [[innerElement]] = nestedPairs; + nestedPairs[0][0] = "later"; const box = { items: [] }; const iterator = box.items.values(); const prototype = Reflect.getPrototypeOf(iterator); @@ -105,6 +121,10 @@ cliTest( `, ); await writeFile(join(root, "iterator-close.js"), iteratorCloseSource); + await writeFile( + join(root, "array-prototype-alias.js"), + arrayPrototypeAliasSource, + ); const output = await cli.run({ arguments: ["analyze-javascript-application", root, "--json"], }); @@ -150,6 +170,8 @@ cliTest( "projected", "arrayObject", "nestedElement", + "innerElement", + "aliasedPrototypeElement", "selected", "beforeClose", "consumed", @@ -182,7 +204,11 @@ cliTest( ]), }, }; - if (name === "selected" || name === "afterClose") + if ( + name === "selected" || + name === "afterClose" || + name === "aliasedPrototypeElement" + ) expect(trace.json).not.toMatchObject(relation); else expect(trace.json).toMatchObject(relation); }