Files
reverse-engineering-anything/src/domain/javascript/javascriptSemanticAmbientWrites.test.ts
T
morluto a86a668718 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.
2026-11-16 07:37:59 +08:00

521 lines
16 KiB
TypeScript

import * as t from "@babel/types";
import { expect, it } from "vitest";
import { analyzeJavaScriptStaticSource } from "../../../tests/fixtures/javascriptStaticAnalysis.js";
import { indexJavaScriptBindingsWithReceiversSteps } from "./javascriptSemanticAnalysis.js";
import { indexJavaScriptBindingsSteps } from "../../../tests/fixtures/javascriptSemanticIndex.js";
import {
completeSemanticSteps,
traverseJavaScriptAst,
} from "./javascriptSemanticTraversal.js";
import {
createGlobalPathReader,
createGlobalReferenceReader,
} from "./javascriptSemanticAmbientWrites.js";
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
it.each([
[
"await value; Object.prototype.then=null; await value;",
["unmodified", "null"],
],
[
"Object.prototype.then=function(resolve){resolve()}; await value;",
["function"],
],
["if(flag) Object.prototype.then=null; await value;", ["unknown"]],
["mutate(Object.prototype); await value;", ["unknown"]],
["await value; mutate(Object.prototype);", ["unmodified"]],
["Array.prototype.then=null; await value;", ["unmodified"]],
] as const)(
"reads intrinsic property facts at implicit await sites: %s",
(source, expected) => {
const parsed = parseJavaScriptSource(source, "app.mjs");
if (parsed === null) throw new Error("Expected implicit read fixture");
const state = completeSemanticSteps(
indexJavaScriptBindingsSteps(parsed.program),
);
const read = createGlobalPathReader(state, ["Object", "prototype", "then"]);
const statuses: string[] = [];
traverseJavaScriptAst(parsed.program, {
enter(node) {
if (!t.isAwaitExpression(node)) return;
const fact = read(node);
statuses.push(
fact.status === "replacement"
? t.isNullLiteral(fact.value)
? "null"
: t.isFunction(fact.value)
? "function"
: "other"
: fact.status,
);
},
});
expect(statuses).toEqual(expected);
},
);
it("keeps wrapper module aliases distinct from global-object module properties", () => {
const parsed = parseJavaScriptSource(
`
const wrapper = module;
Object.defineProperty(wrapper, "exports", { value: exports });
Object.defineProperty(globalThis.module, "exports", { value: exports });
`,
"app.cjs",
);
if (parsed === null) throw new Error("Expected parsed source");
const state = completeSemanticSteps(
indexJavaScriptBindingsSteps(parsed.program),
);
expect(state.ambientWriteEffects.map(({ path }) => path)).toEqual([
["commonjs-module", "exports"],
["global", "module", "exports"],
]);
});
it.each([
["const copy={...module};consume(copy);", false],
["const copy={...{child:module}};consume(copy);", true],
[
"const original={child:module};const copy={...original};consume(copy);",
true,
],
])(
"exposes copied children without escaping the source container: %s",
(body, moduleEscaped) => {
const parsed = parseJavaScriptSource(`function copy(){${body}}`);
if (parsed === null) throw new Error("Expected copied escape fixture");
const state = completeSemanticSteps(
indexJavaScriptBindingsSteps(parsed.program),
);
expect(
state.ambientEscapeEffects.some(
({ path }) =>
path.length === 2 &&
path[0] === "commonjs-module" &&
path[1] === null,
),
).toBe(moduleEscaped);
},
);
it.each([
{
name: "reconverging projections with different alias-cycle histories",
setup: `var x; const a = { foo: x, bar: globalThis }; x = a.bar;
const y = flag ? x : a.foo; y.URL = replacement;`,
specifier: null,
},
{
name: "growing self-reference projections with an ambient initializer",
setup: `let alias = globalThis; alias = alias.globalThis;
alias.URL = replacement;`,
specifier: null,
},
{
name: "distinct properties selecting both ambient identities",
setup: `const box = { left: globalThis, right: import.meta };
const selected = flag ? box.left : box.right; selected.URL = replacement;`,
specifier: null,
},
{
name: "cyclic containers with a reachable ambient property",
setup: `var alias; const box = { next: alias, target: import.meta };
alias = box; const selected = flag ? alias.target : alias.next.target;
selected.url = "file:///other/entry.mjs";`,
specifier: null,
},
{
name: "cycles containing no ambient identity",
setup: `var box; box = { next: box };
const selected = flag ? box : box.next; selected.URL = replacement;`,
specifier: "./worker.js",
},
{
name: "unrelated writes through widened self-reference projections",
setup: `let alias = globalThis; alias = alias.globalThis;
alias.unrelated = replacement;`,
specifier: "./worker.js",
},
{
name: "an exact empty projection into a shallow copy",
setup: `const copy = { ...globalThis };
const selected = flag ? copy : copy; selected.URL = replacement;`,
specifier: "./worker.js",
},
])("preserves $name", ({ setup, specifier }) => {
const source = `import { Worker } from "node:worker_threads";
${setup}
new Worker(new URL("./worker.js", import.meta.url));`;
const worker = analyzeJavaScriptStaticSource(source).references.find(
(reference) => reference.kind === "worker",
);
expect(worker?.specifier).toBe(specifier);
});
it("retains getter-derived ambient escapes once per invoked read site", () => {
const source = `const box={loader:require,get trigger(){this.loader.resolve=this.loader;return 0}};
box.trigger;
box.trigger;`;
const parsed = parseJavaScriptSource(source);
if (parsed === null) throw new Error("Expected getter fixture");
const index = completeSemanticSteps(
indexJavaScriptBindingsWithReceiversSteps(parsed.program),
);
completeSemanticSteps(index.indexMemberMutationFactsSteps());
const effects = index.bindings.ambientEscapeEffects.filter(
({ path }) =>
JSON.stringify(path) === JSON.stringify(["global", "require", null]),
);
expect(
effects.map(({ node }) =>
node === undefined ? null : source.slice(node.start ?? 0, node.end ?? 0),
),
).toEqual(["box.trigger", "box.trigger"]);
expect(effects[0]?.node).not.toBe(effects[1]?.node);
});
const globalFetchWriteSites = (body: string): string[] => {
const source = `var t={},k=[];${body}`;
const parsed = parseJavaScriptSource(source);
if (parsed === null) throw new Error("Expected stored-slot fixture");
const state = completeSemanticSteps(
indexJavaScriptBindingsSteps(parsed.program),
);
return state.ambientWriteEffects
.filter(
({ path }) =>
JSON.stringify(path) === JSON.stringify(["global", "fetch"]),
)
.map(({ node }) =>
node === undefined ? "" : source.slice(node.start ?? 0, node.end ?? 0),
);
};
it.each([
{
name: "an object read from a replaced slot",
body: "var g={};t[k[0]]=g;t[k[1]]=window;g.fetch=replacement;",
sites: [],
},
{
name: "a literal property value whose slot is replaced",
body: "var o={};var h={p:o};h.p=window;o.fetch=replacement;h.p.fetch=other;",
sites: ["h.p.fetch=other"],
},
{
name: "a global read back through an unknown slot",
body: "t[k[0]]=window;t[k[1]].fetch=replacement;",
sites: ["t[k[1]].fetch=replacement"],
},
{
name: "a global read into an alias of an unknown slot",
body: "t[k[0]]=window;var r=t[k[1]];r.fetch=replacement;",
sites: ["r.fetch=replacement"],
},
{
name: "a nested store through an unknown slot",
body: "t[k[0]]={};t[k[1]].x=window;t[k[2]].x.fetch=replacement;",
sites: ["t[k[2]].x.fetch=replacement"],
},
{
name: "a store through an instance prototype",
body: "function F(){}var x=new F();x.__proto__.slot=window;F.prototype.slot.fetch=replacement;",
sites: ["F.prototype.slot.fetch=replacement"],
},
{
name: "a store through an instance constructor",
body: "function F(){}var x=new F();x.constructor.slot=window;F.slot.fetch=replacement;",
sites: ["F.slot.fetch=replacement"],
},
{
name: "a store into a rest copy",
body: "var src={};var {...copy}=src;copy.slot=window;copy.slot.fetch=replacement;",
sites: ["copy.slot.fetch=replacement"],
},
])("resolves stored references for $name", ({ body, sites }) => {
expect(globalFetchWriteSites(body)).toEqual(sites);
});
it("keeps an object read from a replaced slot as a URL base proof", () => {
const worker =
analyzeJavaScriptStaticSource(`import { Worker } from "node:worker_threads";
const t={},k=[];const g={};t[k[0]]=g;t[k[1]]=globalThis;g.URL=replacement;
new Worker(new URL("./worker.js", import.meta.url));`).references.find(
(reference) => reference.kind === "worker",
);
expect(worker?.specifier).toBe("./worker.js");
});
it("indexes unknown-key stores of fresh objects in linear stored-value work", () => {
// Count reads of the stored values' own syntax during ambient indexing only.
const work = (count: number): number => {
const source = `var t={},k=[];${Array.from({ length: count }, (_, i) => `t[k[${String(i)}]]={v:${String(i)}};`).join("")}`;
const parsed = parseJavaScriptSource(source);
if (parsed === null) throw new Error("Expected stored-object fixture");
const index = completeSemanticSteps(
indexJavaScriptBindingsWithReceiversSteps(parsed.program),
);
let counting = false;
let reads = 0;
for (const statement of parsed.program.body) {
if (
!t.isExpressionStatement(statement) ||
!t.isAssignmentExpression(statement.expression)
)
continue;
const value = statement.expression.right;
const type = value.type;
Object.defineProperty(value, "type", {
enumerable: true,
configurable: true,
get: () => {
if (counting) reads += 1;
return type;
},
});
}
counting = true;
completeSemanticSteps(index.indexAmbientWritesSteps());
counting = false;
expect(index.bindings.ambientWriteEffects).toEqual([]);
return reads;
};
// Each stored value is examined a bounded number of times. Re-reading every
// earlier value from every later store grows at least quadratically.
expect(work(64)).toBeLessThan(3 * work(32));
});
it.each([
[
"before and after a direct replacement",
"inspect(Object.defineProperty);Object.defineProperty=()=>{};inspect(Object.defineProperty);",
["unmodified", "replacement"],
],
[
"an unrelated property",
"Object.assign=()=>{};inspect(Object.defineProperty);",
["unmodified"],
],
[
"a conditional replacement",
"if(flag)Object.defineProperty=()=>{};inspect(Object.defineProperty);",
["unknown"],
],
[
"a replaced parent",
"Object=input;inspect(Object.defineProperty);",
["unknown"],
],
[
"a lexical shadow",
"const Object={defineProperty(){}};inspect(Object.defineProperty);",
["unrelated"],
],
[
"an unproven alias write",
"const copy=Object;copy.defineProperty=()=>{};inspect(Object.defineProperty);",
["unknown"],
],
[
"a later write in a possibly repeated activation",
"function run(){inspect(Object.defineProperty);Object.defineProperty=()=>{};}",
["unknown"],
],
[
"competing unconditional replacements before a read",
"function run(){Object.defineProperty=()=>{};Object.defineProperty=()=>{};inspect(Object.defineProperty);}",
["replacement"],
],
[
"a repeated activation with a later write",
"function run(){inspect(Object.defineProperty);Object.defineProperty=()=>{};}run();run();",
["unknown"],
],
[
"a replaced global namespace",
"globalThis={Object:{}};globalThis.Object.defineProperty=()=>{};inspect(Object.defineProperty);",
["unknown"],
],
[
"a namespaced replacement read directly",
"globalThis.Object.defineProperty=()=>{};inspect(Object.defineProperty);",
["replacement"],
],
[
"a direct replacement read through the namespace",
"Object.defineProperty=()=>{};inspect(globalThis.Object.defineProperty);",
["replacement"],
],
[
"an escaped global owner",
"mutate(Object);inspect(Object.defineProperty);",
["unknown"],
],
[
"a namespaced global",
"inspect(globalThis.Object.defineProperty);globalThis.Object.defineProperty=()=>{};inspect(globalThis.Object.defineProperty);",
["unmodified", "replacement"],
],
])(
"retains ordered global-reference facts for %s",
(_label, source, expected) => {
const parsed = parseJavaScriptSource(source, "app.cjs");
if (parsed === null) throw new Error("Expected global-reference fixture");
const state = completeSemanticSteps(
indexJavaScriptBindingsSteps(parsed.program),
);
const read = createGlobalReferenceReader(state, [
"Object",
"defineProperty",
]);
const statuses: string[] = [];
traverseJavaScriptAst(parsed.program, {
enter(node) {
if (
t.isCallExpression(node) &&
t.isIdentifier(node.callee, { name: "inspect" }) &&
t.isNode(node.arguments[0])
)
statuses.push(read(node.arguments[0]).status);
},
});
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,
);