From a86a6687181ec8eb37860a838c850e72b15f7936 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 16 Nov 2026 07:37:59 +0800 Subject: [PATCH] fix(javascript): count escapes of Function.prototype before trusting call and apply (#11501) * fix(javascript): count escapes of Function.prototype before trusting call and apply * fix(javascript): key cached returned references on ambient escapes too * fix(javascript): track arguments of directly invoked functions through their parameters (#11538) * fix(javascript): track arguments of directly invoked functions through their parameters Passing a value to a function expression invoked where it is defined, such as a UMD header (function(root, factory){...})(this, ...), recorded an escape of the argument to unevaluated code although the callee's body is source code whose effects are indexed. javascriptSemanticDirectInvocations owns which arguments reach such a callee only through a plain parameter; the ambient path resolver and member mutation indexing follow that parameter back to its argument, and neither records an escape for it. Spread and rest lists, pattern or default parameters, arguments and direct eval keep the escape, as do unknown callees. Refs #11499 * fix(javascript): escape values thrown out of a function and scan arguments once A value thrown out of a function or class body reaches handlers the throw-to-catch index does not link to it. Arguments of a directly invoked function used to hide this behind the call escape; they no longer do, and captured values had the same gap. The binding pass records such throws, and ambient and member effect indexing escape their values at the throw. Which functions name 'arguments' is now found in one program walk instead of one subtree scan per directly invoked function. * perf(javascript): find shadowed direct-invocation parameters in one pass * fix(javascript): settle Object and Reflect static call receivers in one owner (#11543) * fix(javascript): settle Object and Reflect static call receivers in one owner Ambient write indexing skipped the receiver escape of any resolved Object or Reflect static call, even when source code had replaced the method, while member mutation indexing escaped the constructor for every such call. The second escape (global.Object contents) made each later Function.prototype.call/apply check uncertain in bundles that call Object.defineProperty or Object.freeze. Ambient indexing now settles these receivers once all ambient writes are known: the constructor escapes only when a write or escape may replace the method before the call, and member mutation reuses that decision through retainedGlobalCallReceivers. Refs #11535 * fix(javascript): retain the receiver only for native Object and Reflect statics A resolved Object member that is not one of the language's own static methods (for example one Object inherits from Function.prototype, or an unknown name) may be user code that receives the constructor, so its call keeps the receiver escape. * fix(javascript): look up intrinsic and builtin method tables by own key only NATIVE_STATIC_METHODS and BUILTIN_RECEIVER_METHODS were plain objects, so an owner such as globalThis.constructor or a module named 'constructor' or 'toString' found an inherited Object.prototype function and analysis threw a TypeError on '.has'. Both are Maps now. * fix(javascript): escape values thrown uncaught at the top level (#11556) A value thrown uncaught at the top level reaches the host's uncaught-exception handlers (process.on('uncaughtException'), window.onerror), which may change it before a timer or callback reads it. The binding pass recorded escaping throws only when the walk reached a function or class body; a throw that reaches the program without a handler now escapes the same way. * fix(javascript): follow direct-invocation arguments through returned references and function arguments properties A callable argument of a directly invoked function no longer escapes at the call, but the shared returned-reference owner could not follow the parameter to it, so patch(get()) inside the callee lost the escape of the value get returns. The returned-reference walk now follows the parameter to its argument like the write and escape walkers. A sloppy function's arguments property (f["arguments"], g.caller.arguments from a function it calls) exposes its activation's arguments. A non-strict callee is no longer tracked through its parameters when the program reads an arguments or caller property by a static key. The strictness reader moves beside javaScriptProgramStrictness so both modules share it. --- .../javascript/javascriptModuleInterop.ts | 4 +- .../javascriptSemanticAmbientWrites.test.ts | 133 ++++++++++ .../javascriptSemanticAmbientWrites.ts | 119 ++++++++- ...tSemanticAnalysis.builtin_defaults.test.ts | 146 +++++++++++ .../javascript/javascriptSemanticAnalysis.ts | 38 +-- .../javascriptSemanticDataEffectHelpers.ts | 17 +- .../javascriptSemanticDirectInvocations.ts | 232 ++++++++++++++++++ .../javascript/javascriptSemanticEffectLog.ts | 13 +- .../javascriptSemanticIntrinsicInstances.ts | 21 +- .../javascriptSemanticMemberMutations.ts | 31 ++- .../javascriptSemanticReturnedReferences.ts | 30 ++- .../javascript/javascriptSemanticState.ts | 14 +- .../javascript/javascriptSourceParser.ts | 22 ++ ...ptStaticAnalysisReturnedInvocation.test.ts | 74 ++++++ 14 files changed, 826 insertions(+), 68 deletions(-) create mode 100644 src/domain/javascript/javascriptSemanticDirectInvocations.ts diff --git a/src/domain/javascript/javascriptModuleInterop.ts b/src/domain/javascript/javascriptModuleInterop.ts index 106e6376e3..ac8c5e99bb 100644 --- a/src/domain/javascript/javascriptModuleInterop.ts +++ b/src/domain/javascript/javascriptModuleInterop.ts @@ -33,7 +33,7 @@ import { semanticKnownArrayIterationAt } from "./javascriptSemanticReferenceSour import { opaqueCodeMayRebindBinding, semanticAmbientIntrinsicWrites, - semanticAmbientWriteGeneration, + semanticAmbientEffectGeneration, } from "./javascriptSemanticEffectLog.js"; import { semanticDirectEvalRecorded } from "./javascriptSemanticEffectRecord.js"; @@ -928,7 +928,7 @@ const ambientWriteInterops = new WeakMap< export const semanticModuleInteropWithAmbientWrites = ( state: JavaScriptSemanticAnalysisState, ): ReturnType => { - const generation = semanticAmbientWriteGeneration(state); + const generation = semanticAmbientEffectGeneration(state); const prior = ambientWriteInterops.get(state); if (prior?.generation === generation) return prior.interop; const interop = createSemanticModuleInterop(state); diff --git a/src/domain/javascript/javascriptSemanticAmbientWrites.test.ts b/src/domain/javascript/javascriptSemanticAmbientWrites.test.ts index 54c73730ba..bf74a1f4d7 100644 --- a/src/domain/javascript/javascriptSemanticAmbientWrites.test.ts +++ b/src/domain/javascript/javascriptSemanticAmbientWrites.test.ts @@ -385,3 +385,136 @@ it.each([ expect(statuses).toEqual(expected); }, ); + +// An argument of a directly invoked source function expression reaches only +// that function's parameter, whose effects are indexed like any other source +// effect. Only arguments the body may reach another way escape. +function expectAmbientEffectPaths({ + source, + writes, + escapes, +}: { + readonly source: string; + readonly writes: readonly (readonly unknown[])[]; + readonly escapes: readonly (readonly unknown[])[]; +}): void { + const parsed = parseJavaScriptSource(source, "app.js"); + if (parsed === null) throw new Error("Expected ambient effect fixture"); + const index = completeSemanticSteps( + indexJavaScriptBindingsWithReceiversSteps(parsed.program), + ); + completeSemanticSteps(index.indexMemberMutationFactsSteps()); + const paths = (effects: readonly { readonly path: unknown }[]) => [ + ...new Set(effects.map(({ path }) => JSON.stringify(path))), + ]; + expect({ + writes: paths(index.bindings.ambientWriteEffects), + escapes: paths(index.bindings.ambientEscapeEffects), + }).toEqual({ + writes: writes.map((path) => JSON.stringify(path)), + escapes: escapes.map((path) => JSON.stringify(path)), + }); +} + +it.each([ + { source: "(function(g){})(this);", writes: [], escapes: [] }, + { source: "(function(){})(this);", writes: [], escapes: [] }, + { + source: "(function(g){g.x=1})(this);", + writes: [["global", "x"]], + escapes: [], + }, + { + source: "(function(g){var h=g;h.y=2})(this);", + writes: [["global", "y"]], + escapes: [], + }, + { + source: "(function(g){later(function(){g.w=1})})(this);", + writes: [["global", "w"]], + escapes: [], + }, + { + source: "(function(g){return g})(this).z=1;", + writes: [["global", "z"]], + escapes: [], + }, + { + source: "(function(g,y){y(1)})(this,function(g){});", + writes: [], + escapes: [], + }, + { + source: "(function(g){patch(g)})(this);", + writes: [], + escapes: [["global", null]], + }, + { + source: "(function(g){arguments[0].x=1})(this);", + writes: [], + escapes: [["global", null]], + }, + { + source: "(function(g){})(...[this]);", + writes: [], + escapes: [["global", null]], + }, + { + source: "(function({x}){})(this);", + writes: [], + escapes: [["global", null]], + }, + { + source: "(function(g=d){})(this);", + writes: [], + escapes: [["global", null]], + }, + { + source: "(function(...r){})(this);", + writes: [], + escapes: [["global", null]], + }, + { source: "unknown(this);", writes: [], escapes: [["global", null]] }, + { + source: "(function(g){g.x=1})(globalThis);", + writes: [["global", "x"]], + escapes: [], + }, + { + source: "(function(m){m.exports=1})(module);", + writes: [["commonjs-module", "exports"]], + escapes: [], + }, + { + source: "(function(g){patch(g)})(window);", + writes: [], + escapes: [["global", null]], + }, + { + source: "(function(g){throw g})(this);", + writes: [], + escapes: [["global", null]], + }, +])( + "tracks arguments of directly invoked functions through parameters: $source", + expectAmbientEffectPaths, +); + +// A value thrown uncaught at the top level reaches the host's +// uncaught-exception handlers, which may change it. +it.each([ + ...[ + "throw this;", + "{ throw this; }", + "try { throw this; } finally {}", + "try {} catch { throw this; }", + ].map((source) => ({ + source, + writes: [], + escapes: [["global", null]], + })), + { source: "try { throw this; } catch {}", writes: [], escapes: [] }, +])( + "escapes values thrown uncaught at the top level: $source", + expectAmbientEffectPaths, +); diff --git a/src/domain/javascript/javascriptSemanticAmbientWrites.ts b/src/domain/javascript/javascriptSemanticAmbientWrites.ts index b5412a972b..8752a14ed6 100644 --- a/src/domain/javascript/javascriptSemanticAmbientWrites.ts +++ b/src/domain/javascript/javascriptSemanticAmbientWrites.ts @@ -5,6 +5,10 @@ import { semanticStaticPropertyKey, unwrapJavaScriptExpression, } from "./javascriptAstValues.js"; +import { + semanticArgumentStaysInSource, + semanticDirectInvocationArgument, +} from "./javascriptSemanticDirectInvocations.js"; import { collectJavaScriptIdentityWritesSteps, electronMemberPath, @@ -12,6 +16,7 @@ import { import { createSemanticModuleInterop } from "./javascriptModuleInterop.js"; import { ambientEffectCovers, + ambientPathMayObserveAt, semanticEffectMayFollow, semanticEffectMayPrecede, } from "./javascriptSemanticEffectLog.js"; @@ -41,6 +46,62 @@ import { } from "./javascriptSemanticReferenceSources.js"; import { semanticCopiedReferencePath } from "./javascriptSemanticCopiedReferencePaths.js"; +/** + * Own static methods of `Object` and `Reflect` in the language. A native one + * ignores its receiver; any other member, including one `Object` inherits + * from `Function.prototype`, may be user code that receives the constructor. + */ +const NATIVE_STATIC_METHODS: ReadonlyMap> = new Map( + [ + [ + "Object", + new Set([ + "assign", + "create", + "defineProperties", + "defineProperty", + "entries", + "freeze", + "fromEntries", + "getOwnPropertyDescriptor", + "getOwnPropertyDescriptors", + "getOwnPropertyNames", + "getOwnPropertySymbols", + "getPrototypeOf", + "groupBy", + "hasOwn", + "is", + "isExtensible", + "isFrozen", + "isSealed", + "keys", + "preventExtensions", + "seal", + "setPrototypeOf", + "values", + ]), + ], + [ + "Reflect", + new Set([ + "apply", + "construct", + "defineProperty", + "deleteProperty", + "get", + "getOwnPropertyDescriptor", + "getPrototypeOf", + "has", + "isExtensible", + "ownKeys", + "preventExtensions", + "set", + "setPrototypeOf", + ]), + ], + ], +); + /** Global self references retain object identity, rather than adding a property. */ const canonicalAmbientPath = ( path: readonly (string | null)[], @@ -367,6 +428,13 @@ export const createAmbientPathResolver = ( : stable === undefined ? [] : [stable]; + // A directly invoked function's parameter starts as its argument, + // which therefore did not escape at the call. + const argument = possibleWrites + ? semanticDirectInvocationArgument(root, binding, state) + : undefined; + if (argument !== undefined) + queue({ node: argument, readsAtSelf: true, suffix }); for (const initializer of initializers) { if (semanticReferenceSourcesInactive(initializer, state)) continue; // Pattern assignments share the source selection owner with member @@ -604,6 +672,12 @@ export function* indexJavaScriptAmbientWritesSteps( browserReceiver: boolean; projection?: AmbientProjection; }[] = []; + // Object/Reflect static calls, settled once every ambient write is known. + const staticReceivers: { + node: t.Node; + target: t.Node; + method: readonly string[]; + }[] = []; const stores = new WeakMap< t.Node, Map @@ -673,10 +747,14 @@ export function* indexJavaScriptAmbientWritesSteps( const retainEscape = (target: t.Node, browserReceiver = false) => { escapes.push({ target, node, browserReceiver }); }; - for (const argument of t.isTaggedTemplateExpression(node) + for (const [index, argument] of (t.isTaggedTemplateExpression(node) ? node.quasi.expressions - : node.arguments) - if (t.isNode(argument)) + : node.arguments + ).entries()) + if ( + t.isNode(argument) && + !semanticArgumentStaysInSource(node, index, state) + ) retainEscape( t.isSpreadElement(argument) ? argument.argument : argument, ); @@ -691,16 +769,23 @@ export function* indexJavaScriptAmbientWritesSteps( : undefined; if ( !t.isNewExpression(node) && - (t.isMemberExpression(callee) || - t.isOptionalMemberExpression(callee)) && - !( - invocation?.kind === "known" && - ["Object", "Reflect"].includes(invocation.path[1] ?? "") - ) + (t.isMemberExpression(callee) || t.isOptionalMemberExpression(callee)) ) { - if (browserMethod !== undefined) - browserReceiverMethods.add(browserMethod); - retainEscape(callee.object, browserMethod !== undefined); + const owner = invocation?.path[1]; + const method = + invocation?.kind === "known" && + invocation.path.length === 3 && + owner !== undefined && + NATIVE_STATIC_METHODS.get(owner)?.has(invocation.path[2] ?? "") + ? invocation.path.slice(1) + : undefined; + if (method !== undefined) + staticReceivers.push({ node, target: callee.object, method }); + else { + if (browserMethod !== undefined) + browserReceiverMethods.add(browserMethod); + retainEscape(callee.object, browserMethod !== undefined); + } } }, ); @@ -760,6 +845,8 @@ export function* indexJavaScriptAmbientWritesSteps( } yield; } + for (const node of state.escapingThrows) + escapes.push({ node, target: node.argument, browserReceiver: false }); for (const source of storedReferences) { if (source.target === undefined) continue; const receiver = ambientWrites(source.target.object, source.mutation); @@ -844,6 +931,14 @@ export function* indexJavaScriptAmbientWritesSteps( continue; retainEscapeEffect(effect); } + // A native static method ignores its receiver. A source write or escape + // that may replace the method before this call makes it user code, which + // receives the constructor. (Its prototype chain is not consulted: the + // method is an own property.) + for (const { node, target, method } of staticReceivers) + if (ambientPathMayObserveAt(state, node, ["global", ...method], "contents")) + retainEscapeEffect({ node, target, browserReceiver: false }); + else state.retainedGlobalCallReceivers.add(target); function retainEscapeEffect({ node, target, diff --git a/src/domain/javascript/javascriptSemanticAnalysis.builtin_defaults.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.builtin_defaults.test.ts index df258b32e6..2275b862a1 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.builtin_defaults.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.builtin_defaults.test.ts @@ -885,3 +885,149 @@ it.each([ expect(ir.configurationOperations).toEqual([]); }, ); + +// A directly invoked source function receives its arguments through its +// parameters, whose effects are indexed like any other source effect. Each +// row states why `fs.readFileSync` is or is not still the original method. +it.each([ + { + source: `(function(p){})(fs);`, + keys: ["kept"], + why: "the body never uses the argument", + }, + { + source: `(function(p){ p.readFileSync = custom })({}, fs);`, + keys: ["kept"], + why: "no parameter binds the second argument and the body never reads arguments", + }, + { + source: `(function(p, p){ p.readFileSync = custom })(fs, other);`, + keys: ["kept"], + why: "the later duplicate parameter binds the other argument", + }, + { + source: `(function(p){ p.readFileSync = custom })(fs);`, + keys: [], + why: "a write through the parameter reaches the argument", + }, + { + source: `(function(p){ var q = p; q.readFileSync = custom })(fs);`, + keys: [], + why: "a local alias of the parameter reaches the argument", + }, + { + source: `(function(p){ later(() => { p.readFileSync = custom }) })(fs);`, + keys: [], + why: "a closure over the parameter reaches the argument", + }, + { + source: `(function(p){ return p })(fs).readFileSync = custom;`, + keys: [], + why: "the returned parameter is the argument", + }, + { + source: `(function(p){ patch(p) })(fs);`, + keys: [], + why: "the parameter escapes to unknown code", + }, + { + source: `(function(p){ store.v = p })(fs);`, + keys: [], + why: "the parameter is stored in an unknown object", + }, + { + source: `(function(p){ arguments[0].readFileSync = custom })(fs);`, + keys: [], + why: "the arguments object reaches the argument", + }, + { + source: `(function(...r){ r[0].readFileSync = custom })(fs);`, + keys: [], + why: "a rest parameter is a new array holding the argument", + }, + { + source: `(function(p){ eval(src) })(fs);`, + keys: [], + why: "direct eval may reach the parameter", + }, + { + source: `const box = {fs}; (function(b){ b.fs.readFileSync = custom })(box);`, + keys: [], + why: "a write below the parameter reaches the argument's child", + }, + { + source: `unknown(fs);`, + keys: [], + why: "an unknown callee still receives an escape", + }, + { + source: `(function(get){ patch(get()) })(() => fs);`, + keys: [], + why: "a callable argument's result reaches the parameter's callers", + }, + { + source: `(function(get){ get().readFileSync = custom })(() => fs);`, + keys: [], + why: "a write through a callable argument's result reaches its value", + }, + { + source: `(function f(p){ patch(f["arguments"][0]) })(fs);`, + keys: [], + why: "a sloppy function's arguments property reaches the argument", + }, + { + source: `(function f(p){ patch(f.arguments[0]) })(fs);`, + keys: [], + why: "a sloppy function's arguments property reaches the argument", + }, + { + source: `function g(){ patch(g.caller.arguments[0]) } (function(p){ g() })(fs);`, + keys: [], + why: "a called sloppy function reaches its caller's arguments", + }, + { + source: `function g(){ patch(g.caller) } (function(p){ "use strict"; g() })(fs);`, + keys: ["kept"], + why: "a strict function exposes no arguments property", + }, + { + source: `try { (function(p){ throw p })(fs); } catch (e) { patch(e) }`, + keys: [], + why: "a value thrown out of the callee reaches its caller's handler", + }, + { + source: `try { (function(){ throw fs })(); } catch (e) { patch(e) }`, + keys: [], + why: "a captured value thrown out of a function reaches the caller's handler", + }, +])( + "tracks direct invocation arguments through parameters: $source ($why)", + ({ source, keys }) => { + expect( + analyzeCommonJsSemanticFixture( + `const fs=require('fs'); ${source} fs.readFileSync('kept');`, + ).configurationOperations.map(({ key }) => key), + ).toEqual(keys); + }, +); + +it("treats a value thrown uncaught at the top level as reaching host handlers", () => { + // `process.on("uncaughtException", ...)` may change the thrown module + // before the timer callback reads it. + expect( + analyzeCommonJsSemanticFixture( + `const fs=require('fs'); setTimeout(() => fs.readFileSync('later')); throw fs;`, + ).configurationOperations.map(({ key }) => key), + ).toEqual([]); +}); + +it.each(["constructor", "toString"])( + "analyzes a module named like an Object.prototype key: %s", + (specifier) => { + expect( + analyzeCommonJsSemanticFixture( + `const m=require("${specifier}"); m.has("x");`, + ).configurationOperations, + ).toEqual([]); + }, +); diff --git a/src/domain/javascript/javascriptSemanticAnalysis.ts b/src/domain/javascript/javascriptSemanticAnalysis.ts index 5339125a47..9fa98409c1 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.ts @@ -130,7 +130,7 @@ import { } from "./javascriptSemanticCoverage.js"; import { semanticResourceLimitReason } from "./javascriptSemanticResourceLimits.js"; import { - javaScriptProgramStrictness, + javaScriptStrictnessAt, parseJavaScriptSource, type ParsedJavaScriptSource, } from "./javascriptSourceParser.js"; @@ -1107,6 +1107,7 @@ const createState = ( ambientWrites: new SemanticPropertyPathCoverage(), escapingReferenceStores: new WeakSet(), retainedGlobalCallReceivers: new WeakSet(), + escapingThrows: [], ambientEscapes: new SemanticPropertyPathCoverage(), ambientEscapeEffects: [], ambientWriteEffects: [], @@ -1370,6 +1371,10 @@ function* collectDefinitionsSteps( } } for (const { node, ancestors } of thrownValues) { + // A value not caught in its own function or class body reaches a + // caller's handler, and one thrown uncaught at the top level reaches the + // host's uncaught-exception handlers; neither is linked to the throw. + let caught = false; let child: t.Node = node; for (let index = ancestors.length - 1; index >= 0; index--) { const parent = ancestors[index]!; @@ -1399,16 +1404,20 @@ function* collectDefinitionsSteps( referenceOnly: true, entryBody: parent.handler.body, }); + caught = true; break; } child = parent; } + if (!caught) state.escapingThrows.push(node); } for (const { node, scope } of legacyBlockFunctions) { if ( node.id == null || - semanticStrictnessAt(state.parentsByNode.get(node) ?? node, state) === - "strict" + javaScriptStrictnessAt( + state.parentsByNode.get(node) ?? node, + state.parentsByNode, + ) === "strict" ) continue; let outer = @@ -1957,31 +1966,10 @@ const mappedArgumentsOwner = ( return { scope, parameters: mapped, - strictness: semanticStrictnessAt(owner, state), + strictness: javaScriptStrictnessAt(owner, state.parentsByNode), }; }; -const semanticStrictnessAt = ( - owner: t.Node, - state: JavaScriptSemanticAnalysisState, -): MappedArgumentsOwner["strictness"] => { - for ( - let current: t.Node | undefined = owner; - current !== undefined; - current = state.parentsByNode.get(current) - ) { - if (t.isClass(current)) return "strict"; - if ( - t.isFunction(current) && - t.isBlockStatement(current.body) && - current.body.directives.some(({ value }) => value.value === "use strict") - ) - return "strict"; - if (t.isProgram(current)) return javaScriptProgramStrictness(current); - } - return "unknown"; -}; - /** A mapped arguments property write is another write to its formal binding. */ const bindMappedArgumentWrite = ( target: t.Node, diff --git a/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts b/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts index 871bd6bea5..9c4a2e408d 100644 --- a/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts +++ b/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts @@ -712,8 +712,12 @@ const affectsBuiltinPath = ( semanticPropertyPathKeyMatches(key, selected[index]!), ); -const BUILTIN_RECEIVER_METHODS: Readonly>> = - { +// A Map, so a specifier named like an Object.prototype key finds nothing. +const BUILTIN_RECEIVER_METHODS: ReadonlyMap< + string, + ReadonlySet +> = new Map( + Object.entries({ path: new Set(["resolve", "join", "dirname"]), "path/posix": new Set(["resolve", "join", "dirname"]), url: new Set(["fileURLToPath"]), @@ -747,7 +751,8 @@ const BUILTIN_RECEIVER_METHODS: Readonly>> = "clearInterval", "clearImmediate", ]), - }; + }), +); interface BuiltinEffectIndex { readonly writesByModule: Map< @@ -966,9 +971,9 @@ const computeOriginalBuiltinReceiver = ( })); if ( method === null || - !BUILTIN_RECEIVER_METHODS[ - posixReceiver ? "path/posix" : origin.specifier - ]?.has(method) || + !BUILTIN_RECEIVER_METHODS.get( + posixReceiver ? "path/posix" : origin.specifier, + )?.has(method) || (receiver !== undefined && bindingMemberEffectMayPrecedeReads( state, diff --git a/src/domain/javascript/javascriptSemanticDirectInvocations.ts b/src/domain/javascript/javascriptSemanticDirectInvocations.ts new file mode 100644 index 0000000000..3453b7b021 --- /dev/null +++ b/src/domain/javascript/javascriptSemanticDirectInvocations.ts @@ -0,0 +1,232 @@ +/** + * Arguments of a directly invoked source function expression. The callee's + * body is source code whose effects are indexed like any other, so an + * argument it can reach only through a plain parameter does not escape at + * the call: effect owners follow the parameter to the argument instead. + */ +import * as t from "@babel/types"; + +import { + runtimeFunctionParameters, + semanticStaticPropertyKey, + unwrapJavaScriptExpression, +} from "./javascriptAstValues.js"; +import { semanticDirectEvalRecorded } from "./javascriptSemanticEffectRecord.js"; +import { traverseJavaScriptAst } from "./javascriptSemanticTraversal.js"; +import { javaScriptStrictnessAt } from "./javascriptSourceParser.js"; +import type { + JavaScriptSemanticAnalysisState, + JavaScriptSemanticBindingState, +} from "./javascriptSemanticState.js"; + +/** + * A call that directly invokes a source function expression, as in a UMD + * header `(function (root, factory) { ... })(this, ...)`. `bound[i]` is the + * parameter that argument `i` binds, `null` when nothing in the body can + * reach it, and `undefined` when the body may reach it other than through a + * plain parameter (a spread or rest list, `arguments`, a sloppy function's + * `arguments` property or direct `eval`). + */ +interface DirectInvocation { + readonly call: t.CallExpression; + readonly bound: readonly (t.Identifier | null | undefined)[]; +} + +const directInvocations = new WeakMap< + JavaScriptSemanticAnalysisState, + WeakMap +>(); + +interface ArgumentsReaders { + /** Functions whose subtree names `arguments`. */ + readonly functions: WeakSet; + /** Whether any member read names `arguments` or `caller` by a static key. */ + readonly functionProperties: boolean; +} + +const argumentsReaders = new WeakMap< + JavaScriptSemanticAnalysisState, + ArgumentsReaders +>(); + +/** + * Whether `callee`'s activation arguments may be read other than by its + * parameters: its subtree names `arguments`, or it is not strict and the + * program reads a function's `arguments`/`caller` property (`f["arguments"]`, + * `g.caller.arguments` from a function it calls). One program walk marks + * every function around each `arguments` identifier (nested arrows + * included) and notes those property reads. + */ +const argumentsMayBeRead = ( + callee: t.Function, + state: JavaScriptSemanticAnalysisState, +): boolean => { + let readers = argumentsReaders.get(state); + if (readers === undefined) { + const functions = new WeakSet(); + let functionProperties = false; + traverseJavaScriptAst(state.program, { + enter: (node) => { + if ( + (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) && + FUNCTION_ARGUMENT_PROPERTIES.has( + semanticStaticPropertyKey(node.property, node.computed) ?? "", + ) + ) + functionProperties = true; + if (!t.isIdentifier(node, { name: "arguments" })) return; + for ( + let current = state.parentsByNode.get(node); + current !== undefined && !functions.has(current); + current = state.parentsByNode.get(current) + ) + if (t.isFunction(current)) functions.add(current); + }, + }); + readers = { functions, functionProperties }; + argumentsReaders.set(state, readers); + } + return ( + readers.functions.has(callee) || + (readers.functionProperties && + javaScriptStrictnessAt(callee, state.parentsByNode) !== "strict") + ); +}; + +/** Sloppy-function properties that expose an active call's arguments. */ +const FUNCTION_ARGUMENT_PROPERTIES: ReadonlySet = new Set([ + "arguments", + "caller", +]); + +/** The call that invokes function expression `callee` where it is defined. */ +const directCall = ( + callee: t.Node, + parents: WeakMap, +): t.CallExpression | undefined => { + if (!t.isFunctionExpression(callee) && !t.isArrowFunctionExpression(callee)) + return undefined; + let call = parents.get(callee); + while (call !== undefined && unwrapJavaScriptExpression(call).node === callee) + call = parents.get(call); + return t.isCallExpression(call) && + unwrapJavaScriptExpression(call.callee).node === callee + ? call + : undefined; +}; + +const computeDirectInvocation = ( + callee: t.Function, + state: JavaScriptSemanticAnalysisState, +): DirectInvocation | undefined => { + const call = directCall(callee, state.parentsByNode); + if (call === undefined) return undefined; + if (call.arguments.length === 0) return { call, bound: [] }; + // Only plain parameter lists bind every argument by position alone and + // share one scope with the body. + const parameters = runtimeFunctionParameters(callee); + const rest = parameters.findIndex((parameter) => t.isRestElement(parameter)); + if ( + parameters.some( + (parameter, index) => !t.isIdentifier(parameter) && index !== rest, + ) || + semanticDirectEvalRecorded(state) || + argumentsMayBeRead(callee, state) + ) + return undefined; + const spread = call.arguments.findIndex((argument) => + t.isSpreadElement(argument), + ); + // A later duplicate parameter name shadows earlier ones. + const lastPosition = new Map(); + parameters.forEach((parameter, index) => { + if (t.isIdentifier(parameter)) lastPosition.set(parameter.name, index); + }); + const bound = call.arguments.map((_, index) => { + if ((spread >= 0 && index >= spread) || (rest >= 0 && index >= rest)) + return undefined; + const parameter = parameters[index]; + if (!t.isIdentifier(parameter)) return null; + return lastPosition.get(parameter.name) === index ? parameter : null; + }); + return { call, bound }; +}; + +const directInvocation = ( + callee: t.Node, + state: JavaScriptSemanticAnalysisState, +): DirectInvocation | undefined => { + if (!t.isFunction(callee)) return undefined; + let byCallee = directInvocations.get(state); + if (byCallee === undefined) { + byCallee = new WeakMap(); + directInvocations.set(state, byCallee); + } + let cached = byCallee.get(callee); + if (cached === undefined) { + cached = computeDirectInvocation(callee, state) ?? null; + byCallee.set(callee, cached); + } + return cached ?? undefined; +}; + +/** + * Whether argument `index` of `call` reaches the callee only through a + * parameter whose effects are followed to the argument, so passing it is not + * an escape to code whose effects are not evaluated. + */ +export const semanticArgumentStaysInSource = ( + call: t.Node, + index: number, + state: JavaScriptSemanticAnalysisState, +): boolean => { + if (!t.isCallExpression(call)) return false; + const invocation = directInvocation( + unwrapJavaScriptExpression(call.callee).node, + state, + ); + return invocation?.call === call && invocation.bound[index] !== undefined; +}; + +const parameterArguments = new WeakMap< + JavaScriptSemanticBindingState, + t.Expression | null +>(); + +/** + * The argument a directly invoked function's parameter `binding` receives; + * `reference` is any reference to the binding inside that function. A body + * declaration of the parameter's name shares its binding, so the argument + * stays among that binding's values. + */ +export const semanticDirectInvocationArgument = ( + reference: t.Node, + binding: JavaScriptSemanticBindingState, + state: JavaScriptSemanticAnalysisState, +): t.Expression | undefined => { + const cached = parameterArguments.get(binding); + if (cached !== undefined) return cached ?? undefined; + let argument: t.Expression | undefined; + if (state.scopesById.get(binding.scopeId)?.kind === "function") + for ( + let current = state.parentsByNode.get(reference); + current !== undefined; + current = state.parentsByNode.get(current) + ) { + if ( + !t.isFunction(current) || + state.scopeByNode.get(current)?.scopeId !== binding.scopeId + ) + continue; + const invocation = directInvocation(current, state); + const index = + invocation?.bound.findIndex( + (parameter) => parameter?.name === binding.name, + ) ?? -1; + const selected = invocation?.call.arguments[index]; + if (index >= 0 && t.isExpression(selected)) argument = selected; + break; + } + parameterArguments.set(binding, argument ?? null); + return argument; +}; diff --git a/src/domain/javascript/javascriptSemanticEffectLog.ts b/src/domain/javascript/javascriptSemanticEffectLog.ts index eee8b6268c..603d95b268 100644 --- a/src/domain/javascript/javascriptSemanticEffectLog.ts +++ b/src/domain/javascript/javascriptSemanticEffectLog.ts @@ -441,12 +441,17 @@ export const ambientBindingMayObserveWriteAt = ( ); /** - * A count that grows whenever an ambient write is recorded. A cache that - * depends on ambient writes stores it and recomputes when it differs. + * A count that grows whenever an ambient write is recorded, or with + * `"contents"` also an ambient escape: the effects `ambientPathMayObserveAt` + * reads with the same argument. A cache that depends on them stores it and + * recomputes when it differs. */ -export const semanticAmbientWriteGeneration = ( +export const semanticAmbientEffectGeneration = ( state: JavaScriptSemanticAnalysisState, -): number => state.ambientWriteEffects.length; + observes: "value" | "contents" = "value", +): number => + state.ambientWriteEffects.length + + (observes === "contents" ? state.ambientEscapeEffects.length : 0); /** * A count that grows whenever a member write or escape is recorded on diff --git a/src/domain/javascript/javascriptSemanticIntrinsicInstances.ts b/src/domain/javascript/javascriptSemanticIntrinsicInstances.ts index 62a0413625..3d624e1d22 100644 --- a/src/domain/javascript/javascriptSemanticIntrinsicInstances.ts +++ b/src/domain/javascript/javascriptSemanticIntrinsicInstances.ts @@ -5,7 +5,10 @@ import { unwrapJavaScriptExpression, } from "./javascriptAstValues.js"; import { isIntrinsicGlobalReference } from "./javascriptSemanticGlobalReference.js"; -import { ambientPathMayHaveChanged } from "./javascriptSemanticEffectLog.js"; +import { + ambientPathMayHaveChanged, + ambientPathMayObserveAt, +} from "./javascriptSemanticEffectLog.js"; import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; import { resolveSemanticBindingState, @@ -25,20 +28,22 @@ interface JavaScriptIntrinsicInstanceMethods { /** * Whether a source-visible write or escape may have replaced a member on the * intrinsic prototype, on `Object.prototype`, or the prototype link itself. + * With `read`, only effects that may run before that read count; without it, + * the answer holds at every read site. */ export const intrinsicPrototypeMemberMayHaveChanged = ( state: JavaScriptSemanticAnalysisState, constructor: string, member: string, + read?: t.Node, ): boolean => [constructor, "Object"].some((owner) => - [member, "__proto__"].some((key) => - ambientPathMayHaveChanged( - state, - ["global", owner, "prototype", key], - "contents", - ), - ), + [member, "__proto__"].some((key) => { + const path = ["global", owner, "prototype", key]; + return read === undefined + ? ambientPathMayHaveChanged(state, path, "contents") + : ambientPathMayObserveAt(state, read, path, "contents"); + }), ); /** diff --git a/src/domain/javascript/javascriptSemanticMemberMutations.ts b/src/domain/javascript/javascriptSemanticMemberMutations.ts index db900f0100..0798b5248e 100644 --- a/src/domain/javascript/javascriptSemanticMemberMutations.ts +++ b/src/domain/javascript/javascriptSemanticMemberMutations.ts @@ -82,6 +82,10 @@ import { semanticSelectedReferenceSlot, } from "./javascriptSemanticReferenceSources.js"; import { semanticCopiedReferencePath } from "./javascriptSemanticCopiedReferencePaths.js"; +import { + semanticArgumentStaysInSource, + semanticDirectInvocationArgument, +} from "./javascriptSemanticDirectInvocations.js"; import { semanticArrayIndex, joinSemanticPropertyPaths, @@ -1426,6 +1430,23 @@ export function* collectSemanticMemberMutationsSteps( ]), }); } + // A directly invoked function's parameter starts as its argument, + // which therefore did not escape at the call. + const argument = semanticDirectInvocationArgument( + expression, + binding, + state, + ); + if (argument !== undefined) + enqueue({ + node: argument, + path, + bindings: nested, + definiteReceiver: false, + originAt: argument, + memberReadAt: current.memberReadAt, + capturedSelections: current.capturedSelections, + }); for (const initializer of retainsInitializers ? origins.initializers : []) @@ -1808,8 +1829,11 @@ export function* collectSemanticMemberMutationsSteps( ): void => { for (const source of returnedReferences(node.callee).references) markEscaped(source.node, node, source.projection); - for (const argument of node.arguments) - if (t.isSpreadElement(argument)) + for (const [index, argument] of node.arguments.entries()) + // A directly invoked source function receives it through a parameter, + // which `markValue` follows back to the argument. + if (semanticArgumentStaysInSource(node, index, state)) continue; + else if (t.isSpreadElement(argument)) deferIterable({ node: argument.argument, projection: [], @@ -1954,6 +1978,9 @@ export function* collectSemanticMemberMutationsSteps( } }, }); + // A value thrown out of its function or the program reaches handlers the + // throw-to-catch index does not link to it. + for (const node of state.escapingThrows) markEscaped(node.argument, node); // A later effect can make a previously defined destructuring source uncertain. // Reconsider defaults and iterable origins until their effects are stable. while ( diff --git a/src/domain/javascript/javascriptSemanticReturnedReferences.ts b/src/domain/javascript/javascriptSemanticReturnedReferences.ts index 268acdf38b..4238e07dda 100644 --- a/src/domain/javascript/javascriptSemanticReturnedReferences.ts +++ b/src/domain/javascript/javascriptSemanticReturnedReferences.ts @@ -1,10 +1,10 @@ import * as t from "@babel/types"; import { - ambientPathMayObserveAt, - semanticAmbientWriteGeneration, + semanticAmbientEffectGeneration, semanticBindingEffectGeneration, semanticMemberWriteSources, } from "./javascriptSemanticEffectLog.js"; +import { intrinsicPrototypeMemberMayHaveChanged } from "./javascriptSemanticIntrinsicInstances.js"; import { semanticStaticPropertyKey, @@ -48,6 +48,7 @@ import { semanticAliasSelectionChoices, type SemanticAliasSelectionPath, } from "./javascriptSemanticAliasSelections.js"; +import { semanticDirectInvocationArgument } from "./javascriptSemanticDirectInvocations.js"; import { resolveSemanticBindingState, type JavaScriptSemanticAnalysisState, @@ -113,7 +114,8 @@ export const createSemanticReturnedReferences = ( existing !== undefined && existing.invocation === invocation && existing.iterationUnknown === state.arrayIterationUnknown && - existing.ambientGeneration === semanticAmbientWriteGeneration(state) && + existing.ambientGeneration === + semanticAmbientEffectGeneration(state, "contents") && existing.dependencies.every( ({ binding, generation }) => semanticBindingEffectGeneration(binding) === generation, @@ -152,12 +154,12 @@ export const createSemanticReturnedReferences = ( javaScriptGenericInvocationMethod(invocation.callee) !== undefined; const inheritedInvocationChanged = (method: "call" | "apply"): boolean => invocation !== undefined && - ambientPathMayObserveAt(state, invocation, [ - "global", + intrinsicPrototypeMemberMayHaveChanged( + state, "Function", - "prototype", method, - ]); + invocation, + ); // 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. @@ -310,6 +312,18 @@ export const createSemanticReturnedReferences = ( continue; } dependencies.set(binding, semanticBindingEffectGeneration(binding)); + // A directly invoked function's parameter starts as its argument, + // which therefore did not escape at the call. + const argument = semanticDirectInvocationArgument(node, binding, state); + if (argument !== undefined) + enqueue( + argument, + current.path, + current.path?.length === 0 ? undefined : (current.readAt ?? node), + current.capturedSelections, + current.invocation, + current.classKind, + ); if (current.path !== undefined) { if ( sourceGenericInvocation && @@ -725,7 +739,7 @@ export const createSemanticReturnedReferences = ( result, invocation, iterationUnknown: state.arrayIterationUnknown, - ambientGeneration: semanticAmbientWriteGeneration(state), + ambientGeneration: semanticAmbientEffectGeneration(state, "contents"), dependencies: [...dependencies].map(([binding, generation]) => ({ binding, generation, diff --git a/src/domain/javascript/javascriptSemanticState.ts b/src/domain/javascript/javascriptSemanticState.ts index e1a29d3a12..105f758486 100644 --- a/src/domain/javascript/javascriptSemanticState.ts +++ b/src/domain/javascript/javascriptSemanticState.ts @@ -216,8 +216,20 @@ export interface JavaScriptSemanticAnalysisState { readonly ambientBindingWrites: Map; /** Source-local writes, tagged by global, import.meta or CJS module identity. */ readonly ambientWrites: SemanticPropertyPathCoverage; - /** Browser receivers proven to be only the ambient global identity. */ + /** + * Call receivers that ambient indexing proved the call does not expose: + * browser receivers that are only the ambient global identity, and the + * constructor of an unchanged native `Object`/`Reflect` static method. + * Member mutation indexing skips their receiver escape. + */ readonly retainedGlobalCallReceivers: WeakSet; + /** + * Throw statements whose value leaves the enclosing function or class + * body, or the program (reaching host uncaught-exception handlers). Their + * handlers are not linked to the thrown value, so effect indexing treats + * the value as escaping at the throw. + */ + readonly escapingThrows: t.ThrowStatement[]; /** Store targets whose ownership is unknown or ambient, as classified by ambient indexing. */ readonly escapingReferenceStores: WeakSet; /** Ambient object identities passed to calls whose effects are not evaluated. */ diff --git a/src/domain/javascript/javascriptSourceParser.ts b/src/domain/javascript/javascriptSourceParser.ts index 13902e897a..cd119c84b0 100644 --- a/src/domain/javascript/javascriptSourceParser.ts +++ b/src/domain/javascript/javascriptSourceParser.ts @@ -53,6 +53,28 @@ export const javaScriptProgramStrictness = ( : "unknown"; }; +/** Runtime strictness of the code at `node`: classes, directives, then the program. */ +export const javaScriptStrictnessAt = ( + node: t.Node, + parents: WeakMap, +): "strict" | "sloppy" | "unknown" => { + for ( + let current: t.Node | undefined = node; + current !== undefined; + current = parents.get(current) + ) { + if (t.isClass(current)) return "strict"; + if ( + t.isFunction(current) && + t.isBlockStatement(current.body) && + current.body.directives.some(({ value }) => value.value === "use strict") + ) + return "strict"; + if (t.isProgram(current)) return javaScriptProgramStrictness(current); + } + return "unknown"; +}; + const hasRuntimeModuleDeclaration = (program: t.Program): boolean => program.body.some((node) => { if (t.isTSImportEqualsDeclaration(node)) diff --git a/src/domain/javascript/javascriptStaticAnalysisReturnedInvocation.test.ts b/src/domain/javascript/javascriptStaticAnalysisReturnedInvocation.test.ts index b73bb63bd3..796011fe32 100644 --- a/src/domain/javascript/javascriptStaticAnalysisReturnedInvocation.test.ts +++ b/src/domain/javascript/javascriptStaticAnalysisReturnedInvocation.test.ts @@ -6,6 +6,7 @@ import { parseJavaScriptSource } from "./javascriptSourceParser.js"; import { indexJavaScriptBindingsWithReceiversSteps } from "./javascriptSemanticAnalysis.js"; import { completeSemanticSteps } from "./javascriptSemanticTraversal.js"; import { createSemanticReturnedReferences } from "./javascriptSemanticReturnedReferences.js"; +import { retainSemanticAmbientEffect } from "./javascriptSemanticAmbientWrites.js"; it.each([ ["const get=()=>module;get.call(null)", true], @@ -98,6 +99,41 @@ it.each([ false, ], ["Function.prototype.extra=unknown;const get=()=>({});get.call(null)", false], + ["patch(Function.prototype);const get=()=>({});get.call(null)", true], + ["patch(Function.prototype);const get=()=>({});get.apply(null,[])", true], + ["patch(Function);const get=()=>({});get.call(null)", true], + [ + "patch(Function.prototype);const get=()=>module;get.call=()=>({});get.call(null)", + false, + ], + // A native static method does not expose its constructor receiver. + ["Object.freeze({});const get=()=>({});get.call(null)", false], + [ + 'Object.defineProperty(o,"a",{value:1});const get=()=>({});get.call(null)', + false, + ], + ["Reflect.ownKeys({});const get=()=>({});get.call(null)", false], + // A replaced static method is user code that receives the constructor. + [ + 'Object.defineProperty=custom;Object.defineProperty(o,"a",{value:1});const get=()=>({});get.call(null)', + true, + ], + [ + "Object.custom=custom;Object.custom({});const get=()=>({});get.call(null)", + true, + ], + // A member Object inherits is user code that receives the constructor. + [ + "Function.prototype.custom=custom;Object.custom();const get=()=>({});get.call(null)", + true, + ], + ["Object.custom();const get=()=>({});get.call(null)", true], + // An owner named like an Object.prototype key is looked up safely. + ["globalThis.constructor.keys({});const get=()=>({});get.call(null)", false], + [ + "const get=()=>({});get.call(null);Object.defineProperty=custom;Object.defineProperty(o,'a',{})", + false, + ], ])( "retains inherited invocation uncertainty without revoking own slots: %s", (receiver, expected) => { @@ -132,6 +168,15 @@ it("keeps an earlier native invocation before a later prototype write", () => { expect(analysis.references).toEqual([]); }); +it("keeps an earlier native invocation before a later prototype escape", () => { + const analysis = analyzeJavaScriptStaticSource(` + const get=()=>({}); + get.call(null).require('./unused.cjs'); + patch(Function.prototype); + `); + expect(analysis.references).toEqual([]); +}); + it("does not treat a shadowed Function namespace as native prototype mutation", () => { const analysis = analyzeJavaScriptStaticSource(` function local(Function){ @@ -143,6 +188,35 @@ it("does not treat a shadowed Function namespace as native prototype mutation", expect(analysis.references).toEqual([]); }); +it("recomputes a cached invocation after a later recorded prototype escape", () => { + const parsed = parseJavaScriptSource("const get=()=>({});get.call(null);"); + if (parsed === null) throw new Error("Expected valid fixture"); + const index = completeSemanticSteps( + indexJavaScriptBindingsWithReceiversSteps(parsed.program), + ); + completeSemanticSteps(index.indexAmbientWritesSteps()); + completeSemanticSteps(index.indexMemberMutationFactsSteps()); + const statement = parsed.program.body.at(-1); + if ( + !t.isExpressionStatement(statement) || + !t.isCallExpression(statement.expression) + ) + throw new Error("Expected final invocation"); + const call = statement.expression; + const returned = createSemanticReturnedReferences( + index.bindings, + "direct-call", + ); + expect(returned(call.callee, call).unresolvedInvocation).toBeUndefined(); + retainSemanticAmbientEffect( + index.bindings, + "escape", + ["global", "Function", "prototype"], + parsed.program, + ); + expect(returned(call.callee, call).unresolvedInvocation).toBe(true); +}); + it("retains a finite shared callable alias graph", () => { const aliases = Array.from( { length: 64 },