diff --git a/src/application/javascript/JavaScriptWrappedCallPathCancellation.test.ts b/src/application/javascript/JavaScriptWrappedCallPathCancellation.test.ts index df1b605b37..3ad0457e8a 100644 --- a/src/application/javascript/JavaScriptWrappedCallPathCancellation.test.ts +++ b/src/application/javascript/JavaScriptWrappedCallPathCancellation.test.ts @@ -1,6 +1,7 @@ import { expect, it } from "vitest"; import { completeJavaScriptAnalysisSteps } from "./JavaScriptAnalysisControl.js"; import { collectElectronWrappedCallPathsSteps } from "../../domain/javascript/electronWrappedCallPaths.js"; +import { analyzeParsedJavaScriptStaticSource } from "../../../tests/fixtures/javascriptStaticAnalysis.js"; import { indexJavaScriptBindingsWithReceiversSteps, indexJavaScriptElectronIdentitySteps, @@ -29,6 +30,7 @@ it("cancels actual call-path work while its first seed indexes a long block", as source, accumulator, identity, + () => index.indexMemberMutationFactsSteps(), ); // The first candidate has not yet been expanded: this checkpoint is inside // its block indexing, before the walker's completed batch is yielded. @@ -54,8 +56,149 @@ it("cancels actual call-path work while its first seed indexes a long block", as source, completed, identity, + () => index.indexMemberMutationFactsSteps(), ), ), ).toContainEqual(expect.objectContaining({ entry: "first", conditions: [] })); expect(completed.electronCallPathLimits).toBeUndefined(); }); + +it("cancels while a late initializer requests member-mutation indexing", async () => { + const source = ` + const name = "electron"; + const holder = {}; + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => holder.load(name); + register(); + `; + const parsed = parseJavaScriptSource(source); + if (parsed === null) throw new Error("Expected parsed source"); + const index = completeSemanticSteps( + indexJavaScriptBindingsWithReceiversSteps(parsed.program), + ); + const identity = completeSemanticSteps( + indexJavaScriptElectronIdentitySteps(parsed.program, index.bindings), + ); + const accumulator = createJavaScriptAnalysisAccumulator(source.length); + let indexingStarted = false; + function* indexMemberMutationFacts(): Generator { + indexingStarted = true; + yield; + yield* index.indexMemberMutationFactsSteps(); + } + const work = collectElectronWrappedCallPathsSteps( + parsed.program, + index.bindings, + source, + accumulator, + identity, + indexMemberMutationFacts, + ); + + while (!indexingStarted) { + const next = work.next(); + expect(next.done).toBe(false); + } + expect(index.bindings.memberMutationFactsComplete).toBe(false); + + const controller = new AbortController(); + const reason = { request: "cancel mutation indexing" }; + controller.abort(reason); + await expect( + completeJavaScriptAnalysisSteps(work, controller.signal), + ).rejects.toMatchObject({ reason: "cancelled", cause: reason }); + expect(work.next()).toEqual({ done: true, value: undefined }); + expect(index.bindings.memberMutationFactsComplete).toBe(false); +}); + +it("counts unknowns once when mutation indexing retries a seed", () => { + const source = ` + const holder = {}; + holder.load = require; + function register(value) { + switch (value) { + case choose(): + break; + case "run": + load().ipcMain.on("ping", () => {}); + } + } + var load = () => holder.load("electron"); + register("run"); + `; + const collectUnknowns = (eager: boolean) => { + const parsed = parseJavaScriptSource(source); + if (parsed === null) throw new Error("Expected parsed source"); + const index = completeSemanticSteps( + indexJavaScriptBindingsWithReceiversSteps(parsed.program), + ); + if (eager) completeSemanticSteps(index.indexMemberMutationFactsSteps()); + const identity = completeSemanticSteps( + indexJavaScriptElectronIdentitySteps(parsed.program, index.bindings), + ); + const accumulator = createJavaScriptAnalysisAccumulator(source.length); + let indexRequests = 0; + completeSemanticSteps( + collectElectronWrappedCallPathsSteps( + parsed.program, + index.bindings, + source, + accumulator, + identity, + () => { + indexRequests += 1; + return index.indexMemberMutationFactsSteps(); + }, + ), + ); + return { indexRequests, unknownFindings: accumulator.unknownFindings }; + }; + + const eager = collectUnknowns(true); + const retried = collectUnknowns(false); + + expect(retried.indexRequests).toBe(1); + expect(retried.unknownFindings).toBe(eager.unknownFindings); +}); + +it("preserves unknown handler counts when mutation indexing retries a seed", () => { + const source = ` + const e = require("electron"); + run("start", e.ipcMain.on); + function run(tag, on) { + on("other", unknownHandler); + format("value"); + } + var format = value => value; + `; + // An established CommonJS loader keeps the candidate-loader unknown out of + // the comparison, so both counts describe the call-path walk alone. + const parsed = parseJavaScriptSource(source, "main.cjs"); + if (parsed === null) throw new Error("Expected parsed source"); + const index = completeSemanticSteps( + indexJavaScriptBindingsWithReceiversSteps(parsed.program), + ); + completeSemanticSteps(index.indexMemberMutationFactsSteps()); + const identity = completeSemanticSteps( + indexJavaScriptElectronIdentitySteps(parsed.program, index.bindings), + ); + const accumulator = createJavaScriptAnalysisAccumulator(source.length); + completeSemanticSteps( + collectElectronWrappedCallPathsSteps( + parsed.program, + index.bindings, + source, + accumulator, + identity, + () => index.indexMemberMutationFactsSteps(), + ), + ); + + const analysis = analyzeParsedJavaScriptStaticSource(source, parsed); + + expect(analysis.analysis_coverage?.semantics.unknown_findings).toBe( + accumulator.unknownFindings, + ); +}); diff --git a/src/domain/javascript/electronWrappedCallPaths.ts b/src/domain/javascript/electronWrappedCallPaths.ts index 2eba39ebc4..305aac7d22 100644 --- a/src/domain/javascript/electronWrappedCallPaths.ts +++ b/src/domain/javascript/electronWrappedCallPaths.ts @@ -1,6 +1,10 @@ import * as t from "@babel/types"; import { runtimeFunctionParameters } from "./javascriptAstValues.js"; import { JavaScriptSymbolicBindings } from "./javascriptSymbolicBindings.js"; +import { + referencesElectronModule, + referencesUnresolvedModule, +} from "./javascriptElectronMemberWrites.js"; import { symbolicVariants, symbolicTruth, @@ -260,15 +264,24 @@ export function* collectElectronWrappedCallPathsSteps( source: string, accumulator: JavaScriptAnalysisAccumulator, identity: JavaScriptElectronIdentity, + indexMemberMutationFactsSteps: () => Generator, ): Generator { // Bounds repeated interprocedural expansion independently of bundle bytes. // Exhaustion is reported as unknown coverage, never as a negative finding. let remainingVisits = state.precision.wrapper_visits; + // Unknowns met on the walk are attributed to the file only where the walk + // could have reached Electron; see the end of this function. + let unknownFindings = 0; const prefixes = new ElectronCallPathBlockPrefixes( () => --remainingVisits >= 0, state.parentsByNode, ); - const resolver = new JavaScriptSymbolicBindings(state, program, identity); + const resolver = new JavaScriptSymbolicBindings( + state, + program, + identity, + () => false, + ); const paths: ElectronCallPath[] = []; const seen = new Set(); const unconditionalRegistrations = new Set(); @@ -324,7 +337,7 @@ export function* collectElectronWrappedCallPathsSteps( : source.slice(part.start, part.end); const discriminant = text(node.discriminant); if (discriminant === undefined) { - accumulator.unknownFindings += 1; + unknownFindings += 1; return context; } const choices: string[] = []; @@ -364,7 +377,7 @@ export function* collectElectronWrappedCallPathsSteps( } const match = compare(branch.test, "==="); if (match === undefined || !append(choices, match)) { - accumulator.unknownFindings += 1; + unknownFindings += 1; return context; } } @@ -375,7 +388,7 @@ export function* collectElectronWrappedCallPathsSteps( if (branch.test == null) continue; const mismatch = compare(branch.test, "!=="); if (mismatch === undefined || !append(unmatched, mismatch)) { - accumulator.unknownFindings += 1; + unknownFindings += 1; return context; } } @@ -383,10 +396,10 @@ export function* collectElectronWrappedCallPathsSteps( } // Earlier case bodies may terminate or change state before reaching this // case. The selection disjunction is necessary but not a complete path. - if (fallthrough) accumulator.unknownFindings += 1; + if (fallthrough) unknownFindings += 1; const expression = choices.map((choice) => `(${choice})`).join(" || "); if (expression.length > 512) { - accumulator.unknownFindings += 1; + unknownFindings += 1; return context; } if (expression === "") return context; @@ -427,7 +440,7 @@ export function* collectElectronWrappedCallPathsSteps( value: SymbolicValue, ): PathContext | null => { if (value.conditionUnresolved) { - accumulator.unknownFindings += 1; + unknownFindings += 1; return null; } const authorityContext = @@ -821,7 +834,7 @@ export function* collectElectronWrappedCallPathsSteps( }> { if (active.has(value)) return; if (active.size >= state.precision.wrapper_depth) { - accumulator.unknownFindings += 1; + unknownFindings += 1; (accumulator.electronCallPathLimits ??= new Set()).add("wrapper_depth"); return; } @@ -914,7 +927,7 @@ export function* collectElectronWrappedCallPathsSteps( const body = resolver.callBody(fn); if (body === undefined || context.active.has(fn.node)) return; if (fn.conditionUnresolved === true) { - accumulator.unknownFindings += 1; + unknownFindings += 1; return; } const name = @@ -957,7 +970,7 @@ export function* collectElectronWrappedCallPathsSteps( }; const first = args[0]; if (first?.kind !== "literal" || typeof first.value !== "string") { - accumulator.unknownFindings += 1; + unknownFindings += 1; return open(); } const channelContext = metadataContext(context, first); @@ -1130,11 +1143,9 @@ export function* collectElectronWrappedCallPathsSteps( // Its call chain can never deduplicate a later path. perInvocationHandler ? undefined : { calls: context.calls, slot }, ); - if (descriptor.mode !== "send" && descriptor.mode !== "invoke" && !fn) - accumulator.unknownFindings += 1; } } else if (guardedOnly) { - accumulator.unknownFindings += 1; + unknownFindings += 1; // A guarded registration is a path candidate even when its callback has // no observable body. Keep its predicate instead of promoting it to an // unconditional IPC fact or dropping the registration entirely. @@ -1213,9 +1224,9 @@ export function* collectElectronWrappedCallPathsSteps( worldValue === null ? null : electronStaticSymbolicValue(worldValue, worldNode); - if (worldId?.status === "dynamic") accumulator.unknownFindings += 1; + if (worldId?.status === "dynamic") unknownFindings += 1; if (key?.kind !== "literal" || typeof key.value !== "string" || !object) { - accumulator.unknownFindings += 1; + unknownFindings += 1; return unresolved(); } const keyContext = metadataContext(worldContext, key); @@ -1233,7 +1244,7 @@ export function* collectElectronWrappedCallPathsSteps( guardedObject || guardedDispatch; if (guarded) { - accumulator.unknownFindings += 1; + unknownFindings += 1; unresolved(); } else if (site !== undefined) { // Each invocation binds its own key and object, so one helper's exposure @@ -1274,7 +1285,7 @@ export function* collectElectronWrappedCallPathsSteps( prior.value.world_id?.status === "literal" && worldId?.status === "literal" ) - accumulator.unknownFindings += 1; + unknownFindings += 1; prior.value = merged; } } @@ -1525,8 +1536,8 @@ export function* collectElectronWrappedCallPathsSteps( } if (index < owner.cases.length) context = switchCaseContext(owner, index, context); - else accumulator.unknownFindings += 1; - } else accumulator.unknownFindings += 1; + else unknownFindings += 1; + } else unknownFindings += 1; } else if (t.isLogicalExpression(parent) && child === parent.right) { const right = symbolicLogicalRight( parent.operator, @@ -1578,7 +1589,15 @@ export function* collectElectronWrappedCallPathsSteps( for (;;) { const batch = traversal.next(); for (const node of seeds) { - yield* seed(node); + for (;;) { + const priorUnknownFindings = unknownFindings; + resolver.memberMutationFactsRequired = false; + yield* seed(node); + if (!resolver.memberMutationFactsRequired) break; + unknownFindings = priorUnknownFindings; + yield* indexMemberMutationFactsSteps(); + resolver.memberMutationFactsIndexed(); + } if (remainingVisits < 0) break; } seeds.length = 0; @@ -1590,7 +1609,7 @@ export function* collectElectronWrappedCallPathsSteps( } recordBridgeExposures(); if (remainingVisits < 0 || resolver.truncated) { - accumulator.unknownFindings += 1; + unknownFindings += 1; const limits = (accumulator.electronCallPathLimits ??= new Set()); if (remainingVisits < 0) limits.add("wrapper_visits"); for (const limit of resolver.resourceLimits) limits.add(limit); @@ -1622,6 +1641,29 @@ export function* collectElectronWrappedCallPathsSteps( }, }; } - if (resolver.unresolved) accumulator.unknownFindings += 1; + unknownFindings += accumulator.ipc.filter( + ({ value }) => + value.resolution_basis === "wrapper-substitution" && + value.mode !== "send" && + value.mode !== "invoke" && + value.handler_kind !== "inline-function", + ).length; + if (resolver.unresolved) unknownFindings += 1; + // Every path starts at an Electron exposure or IPC registration, and the + // resolver reaches an Electron value only through a require or import of + // the Electron module. Where none of that happened, no read gave up before + // an initializer that could supply a loader, and no budget ran out, the + // walk could not have hidden a path, so its unresolved reads describe no + // fact this analysis could have found. + if ( + identity.bindings.size > 0 || + resolver.reachedElectron || + resolver.unresolvedRequire || + resolver.unresolvedLoader || + (accumulator.electronCallPathLimits?.size ?? 0) > 0 || + referencesElectronModule(accumulator.references) || + referencesUnresolvedModule(accumulator.references) + ) + accumulator.unknownFindings += unknownFindings; return paths; } diff --git a/src/domain/javascript/electronWrappedCallPaths.unknown_scope.test.ts b/src/domain/javascript/electronWrappedCallPaths.unknown_scope.test.ts new file mode 100644 index 0000000000..5f7978a3ed --- /dev/null +++ b/src/domain/javascript/electronWrappedCallPaths.unknown_scope.test.ts @@ -0,0 +1,1250 @@ +import { describe, expect, it } from "vitest"; +import { parseJavaScriptSource } from "./javascriptSourceParser.js"; +import { + analyzeJavaScriptStaticSource, + analyzeParsedJavaScriptStaticSource, +} from "../../../tests/fixtures/javascriptStaticAnalysis.js"; + +// A bare `require` in a source without a declared runtime context is only a +// candidate loader, and that candidate counts its own unknown. These fixtures +// exercise the call-path walk, so parse them as established CommonJS. The +// `import.meta` fixtures keep module syntax and stay on the bare analyzer. +const analyze = (source: string) => { + const parsed = parseJavaScriptSource(source, "main.cjs"); + if (parsed === null) throw new Error("fixture did not parse"); + return analyzeParsedJavaScriptStaticSource(source, parsed); +}; + +// A hoisted function reads a binding whose initializer appears later in the +// module, the shape production bundles such as react-dom take for timers. +const LATER_INITIALIZED_READ = ` +function schedule(callback) { + return scheduleTimeout(callback, 0); +} +var scheduleTimeout = "function" === typeof setTimeout ? setTimeout : void 0; +module.exports = { schedule }; +`; +const UNKNOWN_LIMITATION = expect.stringContaining("remain unknown"); + +describe("wrapped call-path unknown attribution", () => { + it("keeps a module without Electron identity complete when a hoisted read precedes its initializer", () => { + const result = analyze(LATER_INITIALIZED_READ); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + expect(result.electron.call_paths).toBeUndefined(); + }); + + it("keeps the same read unknown beside Electron identity, where an isolated-world exposure could hide", () => { + const result = analyze( + `const e = require("electron");\ne.app.quit();\n${LATER_INITIALIZED_READ}`, + ); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps a module complete when only a worker URL names electron", () => { + const result = analyzeJavaScriptStaticSource( + `const worker = new Worker(new URL("electron", import.meta.url));\n${LATER_INITIALIZED_READ}`, + ); + + expect(result.references).toContainEqual( + expect.objectContaining({ kind: "worker", specifier: "electron" }), + ); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps bridge findings and call paths when the Electron module name is a resolved variable", () => { + const result = analyze(` + const name = "electron"; + const { contextBridge, ipcRenderer } = require(name); + contextBridge.exposeInMainWorld("api", { ping: () => ipcRenderer.send("ping") }); + `); + + expect(result.references).toContainEqual( + expect.objectContaining({ kind: "require", specifier: null }), + ); + expect(result.electron.context_bridge_apis).toContainEqual( + expect.objectContaining({ api_key: "api" }), + ); + expect(result.electron.call_paths).toContainEqual( + expect.objectContaining({ + entry_kind: "bridge-member", + entry: "api.ping", + terminal: "electron.ipcRenderer.send", + }), + ); + }); + + it("keeps IPC registrations and call paths through a require alias", () => { + const result = analyze(` + const load = require; + const { ipcMain } = load("electron"); + ipcMain.on("ping", (event) => event.sender.send("pong")); + `); + + expect(result.references).toContainEqual( + expect.objectContaining({ kind: "require", specifier: "electron" }), + ); + expect(result.electron.ipc).toContainEqual( + expect.objectContaining({ + side: "main", + operation: "on", + channel: "ping", + }), + ); + expect(result.electron.call_paths).toContainEqual( + expect.objectContaining({ + entry_kind: "ipc-handler", + entry: "ping", + terminal: "ipc-event.sender.send", + }), + ); + }); + + it("keeps the unknown when a hoisted read precedes a loader alias that could supply Electron", () => { + const result = analyze(` + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var load = require; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer loader proof", () => { + it("keeps the unknown when a late loader alias resolves through another binding", () => { + const result = analyze(` + const req = require; + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var load = req; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late loader resolves to an ambient loader alias", () => { + const result = analyze(` + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var load = __non_webpack_require__; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late loader reads a changed import.meta member", () => { + const result = analyzeJavaScriptStaticSource(` + import.meta.load = __non_webpack_require__; + const name = "electron"; + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => import.meta.load(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it.each([ + ["directly", "() => import.meta"], + ["through a local", "() => { const meta = import.meta; return meta; }"], + ])( + "keeps the unknown when a late function returns a changed import.meta %s", + (_form, initializer) => { + const result = analyzeJavaScriptStaticSource(` + import.meta.load = __non_webpack_require__; + function register() { + load().load("electron").ipcMain.on("ping", () => {}); + } + var load = ${initializer}; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }, + ); + + it("keeps the unknown when a late loader alias resolves through two bindings", () => { + const result = analyze(` + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var base = require; + var load = base; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late loader alias comes from a function call", () => { + const result = analyze(` + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var load = pick(); + function pick() { + return require; + } + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late loader alias resolves to a host member", () => { + const result = analyze(` + const req = window.require; + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var load = req; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late initializer can hand out require", () => { + const result = analyze(` + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var load = typeof require === "function" ? require : function () {}; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the module complete when a later function only returns a string containing electron", () => { + const result = analyze(` + function register() { + format("value"); + } + var format = (value) => value + "electron"; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the module complete when the later initializer of the called alias cannot load a module", () => { + const result = analyze(` + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var load = (name) => ({ ipcMain: { on() { return name; } } }); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer selected value proof", () => { + it.each([ + ["conditional", "true ? value => value : value => value"], + ["logical", "(value => value) || (value => value)"], + ["sequence", "(0, value => value)"], + ])( + "keeps the module complete through a %s callable selection", + (_kind, initializer) => { + const result = analyze(` + function run() { + format("value"); + } + var format = ${initializer}; + run(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }, + ); + + it("keeps the module complete when a late function returns import.meta.url", () => { + const result = analyzeJavaScriptStaticSource(` + function run() { + moduleUrl(); + } + var moduleUrl = () => import.meta.url; + run(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the module complete when a late function returns an unchanged import.meta", () => { + const result = analyzeJavaScriptStaticSource(` + function run() { + meta().url; + } + var meta = () => import.meta; + run(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer loader proof", () => { + it("keeps the unknown when a late function has a loader default parameter", () => { + const result = analyze(` + function register() { + load()("electron").ipcMain.on("ping", () => {}); + } + var load = (req = require) => req; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late forwarding wrapper closes over a loader", () => { + const result = analyze(` + const req = require; + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var load = (name) => req(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer loader input proof", () => { + it("keeps the unknown when a late forwarding wrapper receives a loader parameter", () => { + const result = analyze(` + const name = "electron"; + function register() { + load(require).ipcMain.on("ping", () => {}); + } + var load = (loader) => loader(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late forwarding wrapper reads a catch binding", () => { + const result = analyze(` + const name = "electron"; + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => { + try { + run(); + } catch (loader) { + return loader(name); + } + }; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late forwarding wrapper reads arguments", () => { + const result = analyze(` + const name = "electron"; + function register() { + load(require).ipcMain.on("ping", () => {}); + } + var load = function () { + return arguments[0](name); + }; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late forwarding wrapper receives a loader in rest arguments", () => { + const result = analyze(` + const req = require; + const moduleName = "electron"; + function register() { + load("safe", req).ipcMain.on("ping", () => {}); + } + var load = (...loaders) => loaders[1](moduleName); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the module complete when a late forwarding wrapper calls a loader-free module function", () => { + const result = analyze(` + function identity(value) { + return value; + } + function register() { + load("value"); + } + var load = (value) => identity(value); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late function returns its loader receiver", () => { + const result = analyze(` + function register() { + load.call(require)("electron").ipcMain.on("ping", () => {}); + } + var load = function () { return this; }; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late function reads a loader from its receiver", () => { + const result = analyze(` + const name = "electron"; + function register() { + load.call({ r: require }).ipcMain.on("ping", () => {}); + } + var load = function () { return this.r(name); }; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late function reads a loader from a changed object", () => { + const result = analyze(` + const name = "electron"; + const holder = {}; + holder.load = require; + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => holder.load(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when an alias changes a late function's object", () => { + const result = analyze(` + const name = "electron"; + const holder = {}; + const alias = holder; + alias.load = require; + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => holder.load(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer loader input edge cases", () => { + it("keeps the unknown when a spread shifts a loader into a later parameter", () => { + const result = analyze(` + const args = [0, require]; + function register() { + load(...args, "safe")("electron").ipcMain.on("ping", () => {}); + } + var load = (first, second) => second; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a spread shifts a loader into rest parameters", () => { + const result = analyze(` + const args = [0, require]; + function register() { + load(...args, "safe")[0]("electron").ipcMain.on("ping", () => {}); + } + var load = (first, ...rest) => rest; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late function returns new.target", () => { + const result = analyze(` + function register() { + Reflect.construct(load, [], require)("electron").ipcMain.on("x", () => {}); + } + var load = function () { return new.target; }; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer mutation proof", () => { + it("keeps the unknown when a called function changes a late function's object", () => { + const result = analyze(` + const name = "electron"; + const holder = {}; + function install(target) { + target.load = require; + } + install(holder); + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => holder.load(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late loader can come from a changed prototype", () => { + const result = analyze(` + const name = "electron"; + Object.prototype.load = require; + function register() { + load(name).ipcMain.on("ping", () => {}); + } + var load = ({}).load; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a safe argument inherits a loader member", () => { + const result = analyze(` + Object.prototype.load = require; + function register() { + late({})("electron").ipcMain.on("ping", () => {}); + } + var late = value => value.load; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps a safe literal member helper complete", () => { + const result = analyze(` + function run() { + size("value"); + } + var size = value => value.length; + run(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a destructuring projection inherits a loader", () => { + const result = analyze(` + Object.prototype.load = require; + function register() { + load("electron").ipcMain.on("x", () => {}); + } + var { load } = {}; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a late function reads a loader through super", () => { + const result = analyze(` + const name = "electron"; + Object.prototype.r = require; + function register() { + load().ipcMain.on("x", () => {}); + } + var load = ({ m() { return super.r(name); } }).m; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when an ambient value receives a loader member", () => { + const result = analyze(` + const name = "electron"; + String.load = require; + function register() { + load().ipcMain.on("x", () => {}); + } + var load = () => String.load(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when an ambient value receives a computed loader member", () => { + const result = analyze(` + const key = "load"; + const name = "electron"; + String.load = require; + function register() { + load().ipcMain.on("x", () => {}); + } + var load = () => String[key](name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when a qualified write replaces an ambient value", () => { + const result = analyze(` + globalThis.String = require; + function register() { + load()("electron").ipcMain.on("ping", () => {}); + } + var load = () => String; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown for an inherited loader on a function value", () => { + const result = analyze(` + const name = "electron"; + Object.prototype.load = require; + function register() { + load().ipcMain.on("x", () => {}); + } + var load = () => (() => {}).load(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer local projection proof", () => { + it("keeps the unknown when a function-local destructuring projection inherits a loader", () => { + const result = analyze(` + Object.prototype.r = require; + const name = "electron"; + function register() { + load().ipcMain.on("x", () => {}); + } + var load = () => { const { r } = {}; return r(name); }; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps a function-local destructuring of a safe literal complete", () => { + const result = analyze(` + function run() { + first(); + } + var first = () => { const { length } = "value"; return length; }; + run(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer ambient value proof", () => { + it.each([ + ["directly", "() => setTimeout"], + ["through a local", "() => { const timer = setTimeout; return timer; }"], + ["through a sequence", "() => (0, setTimeout)"], + ])( + "keeps uncertainty when a late function returns a changed timer %s", + (_form, initializer) => { + const result = analyze(` + setTimeout.load = require; + const name = "electron"; + function register() { + load().load(name).ipcMain.on("ping", () => {}); + } + var load = ${initializer}; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }, + ); + + it("keeps a late scheduler complete when it only invokes the changed timer", () => { + const result = analyze(` + setTimeout.label = "scheduler"; + function schedule() { + later(); + } + var later = () => setTimeout(() => {}, 0); + schedule(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer ambient prototype proof", () => { + it("keeps the unknown when an ambient value inherits a loader member", () => { + const result = analyze(` + Object.prototype.load = require; + const name = "electron"; + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => String.load(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps the unknown when an ambient value inherits a function loader member", () => { + const result = analyze(` + Function.prototype.load = require; + const name = "electron"; + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => String.load(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it.each([ + [ + "Object.prototype through a sequence", + "Object.prototype.load = require;", + "(0, String.load)", + ], + [ + "Object.prototype through a logical expression", + "Object.prototype.load = require;", + "(true && String.load)", + ], + [ + "Function.prototype through a sequence", + "Function.prototype.load = require;", + "(0, String.load)", + ], + [ + "a computed Object.prototype write through a sequence", + 'Object.prototype["load"] = require;', + "(0, String.load)", + ], + [ + "String.__proto__ through a sequence", + "String.__proto__.load = require;", + "(0, String.load)", + ], + [ + "Object.defineProperty through a sequence", + 'Object.defineProperty(Object.prototype, "load", { value: require });', + "(0, String.load)", + ], + ])( + "keeps the unknown when %s supplies an indirect ambient loader", + (_source, write, callee) => { + const result = analyze(` + ${write} + const name = "electron"; + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => ${callee}(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }, + ); + + it("keeps an indirect ambient member call complete without a prototype write", () => { + const result = analyze(` + const name = "electron"; + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => (0, String.load)(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps a safe ambient member helper complete", () => { + const result = analyze(` + const { app } = require("electron"); + String.fromCharCode(65); + app.getName(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer literal prototype proof", () => { + it.each(["[]", '("")', "(0)", "(false)", "/value/", "(0n)"])( + "keeps the unknown when Object.prototype can supply a late loader through %s", + (literal) => { + const result = analyze(` + Object.prototype.load = require; + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var load = ${literal}.load; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }, + ); + + it.each([ + ["Array", "Array.prototype", "[]"], + ["String", "String.prototype", '("")'], + ])( + "keeps the unknown when %s.prototype can supply a late loader", + (_owner, prototype, literal) => { + const result = analyze(` + const name = "electron"; + ${prototype}.load = require; + function register() { + load(name).ipcMain.on("ping", () => {}); + } + var load = ${literal}.load; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }, + ); + + it("keeps the unknown when a late function reads a changed prototype", () => { + const result = analyze(` + const name = "electron"; + Object.prototype.load = require; + function register() { + load().ipcMain.on("ping", () => {}); + } + var load = () => ({}).load(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it.each([ + ["an object literal", "() => ({})"], + ["a number", "() => 1"], + ["a local object", "() => { const value = {}; return value; }"], + ["a promise", "async () => null"], + ])( + "keeps the unknown when a late function returns %s whose prototype supplies a loader", + (_form, initializer) => { + const result = analyze(` + Object.prototype.load = require; + function register() { + load().load("electron").ipcMain.on("ping", () => {}); + } + var load = ${initializer}; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }, + ); + + it("keeps a late function returning a literal complete without a prototype write", () => { + const result = analyze(` + function run() { + make().size; + } + var make = () => ({}); + run(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer precision", () => { + it.each([ + [ + "clearTimeout", + "cancelTimeout", + 'typeof clearTimeout === "function" ? clearTimeout : undefined', + ], + [ + "queueMicrotask", + "scheduleMicrotask", + 'typeof queueMicrotask === "function" ? queueMicrotask : undefined', + ], + ])("keeps a late %s shim complete", (_ambient, local, initializer) => { + const result = analyze(` + function run(value) { + return ${local}(value); + } + var ${local} = ${initializer}; + run(() => {}); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps an alias rebinding separate from its original object", () => { + const result = analyze(` + const base = {}; + let alias = base; + alias = {}; + function run() { + format("value"); + } + var format = (value) => base; + run(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); + + it.each([ + "alias = {}; alias.other = 1;", + "alias = []; alias.other = 1;", + "alias = () => {}; alias.other = 1;", + "alias = function () {}; alias.other = 1;", + "alias = class {}; alias.other = 1;", + "alias = new (class {})(); alias.other = 1;", + "alias = /value/; alias.other = 1;", + "alias = {}; alias[key] = 1;", + "alias = {}; const replacement = alias; replacement.other = 1;", + "const replacement = {}; alias = replacement; alias.other = 1;", + ])("keeps bridge methods after replacement alias writes: %s", (writes) => { + const result = analyze(` + const { contextBridge, ipcRenderer } = require("electron"); + const base = { ping: () => ipcRenderer.send("ping") }; + let alias = base; + ${writes} + contextBridge.exposeInMainWorld("api", base); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.electron.context_bridge_apis).toContainEqual( + expect.objectContaining({ + api_key: "api", + members: expect.arrayContaining(["ping"]), + }), + ); + expect(result.electron.call_paths).toContainEqual( + expect.objectContaining({ + entry: "api.ping", + terminal: "electron.ipcRenderer.send", + }), + ); + }); + + it("retains uncertainty when a constructed replacement returns the original object", () => { + const result = analyze(` + const { contextBridge, ipcRenderer } = require("electron"); + const base = { ping: () => ipcRenderer.send("ping") }; + let alias = base; + alias = new (class { constructor() { return base; } })(); + alias.other = 1; + contextBridge.exposeInMainWorld("api", base); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it.each([ + "const replacement = base;", + "let replacement = {}; replacement = base;", + "const { replacement } = { replacement: base };", + ])( + "retains uncertainty when a replacement binding can hold the original object: %s", + (replacement) => { + const result = analyze(` + const { contextBridge, ipcRenderer } = require("electron"); + const base = { ping: () => ipcRenderer.send("ping") }; + ${replacement} + let alias = base; + alias = replacement; + alias.other = 1; + contextBridge.exposeInMainWorld("api", base); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }, + ); + + it.each([ + "alias.load = require; alias = {};", + "const captured = alias; alias = {}; captured.load = require;", + "if (flag) alias = {}; alias.load = require;", + "({ value: alias } = { value: holder }); alias.load = require;", + ])("retains original alias mutation uncertainty: %s", (writes) => { + const result = analyze(` + const name = "electron"; + const holder = {}; + let alias = holder; + ${writes} + function register() { load().ipcMain.on("ping", () => {}); } + var load = () => holder.load(name); + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("keeps a sequence-wrapped late callee complete", () => { + const result = analyze(` + function run() { + (0, format)("value"); + } + var format = (value) => value; + run(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); + }); +}); + +describe("later initializer alias write bounds", () => { + // 64 million alias and write pairs, as many as 8,000 of each. Member writes + // to one object cost quadratic time in the ambient write index, which this + // bound does not exercise, so the pairs come from more aliases. + it("indexes 32,000 aliases and 2,000 writes without exhausting the worker heap", () => { + const aliases = Array.from( + { length: 32_000 }, + (_, index) => `const alias${index} = value;`, + ).join("\n"); + const writes = Array.from( + { length: 2_000 }, + (_, index) => `value.property${index} = ${index};`, + ).join("\n"); + const result = analyze(` + const value = {}; + ${aliases} + ${writes} + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + }); + + it("indexes one very large alias group without exceeding the argument limit", () => { + const aliases = Array.from( + { length: 150_000 }, + (_, index) => `const alias${index} = value;`, + ).join("\n"); + const result = analyze(` + const value = {}; + ${aliases} + value.property = 1; + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + }); +}); + +it("keeps require.resolve-only Electron references outside the call-path gate", () => { + const result = analyze(` + require.resolve("electron"); + function run() { + format("value"); + } + var format = value => value; + run(); + `); + + expect(result.references).toContainEqual( + expect.objectContaining({ + kind: "require", + operation: "resolve", + specifier: "electron", + }), + ); + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("complete"); + expect(result.limitations).not.toContainEqual(UNKNOWN_LIMITATION); +}); + +describe("later initializer loader proof bounds", () => { + it("bounds the loader proof for a long late alias chain", () => { + const aliases = Array.from({ length: 512 }, (_, index) => + index === 0 ? "var a0 = require;" : `var a${index} = a${index - 1};`, + ).join("\n"); + const result = analyze(` + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + ${aliases} + var load = a511; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); + + it("bounds the loader proof for a deeply nested late initializer", () => { + const initializer = Array.from( + { length: 512 }, + (_, index) => `value${index}`, + ).join(" + "); + const result = analyze(` + function register() { + load("electron").ipcMain.on("ping", () => {}); + } + var load = ${initializer}; + register(); + `); + + expect(result.parse_error_count).toBe(0); + expect(result.parse_status).toBe("partial"); + expect(result.limitations).toContainEqual(UNKNOWN_LIMITATION); + }); +}); diff --git a/src/domain/javascript/javascriptElectronMemberWrites.ts b/src/domain/javascript/javascriptElectronMemberWrites.ts index 99510650c4..66927f3146 100644 --- a/src/domain/javascript/javascriptElectronMemberWrites.ts +++ b/src/domain/javascript/javascriptElectronMemberWrites.ts @@ -24,6 +24,43 @@ export const ELECTRON_REFERENCE_KINDS: ReadonlySet = new Set([ export const ELECTRON_MODULE = /^electron(?:\/(?:common|main|renderer|utility))?$/u; +type ModuleReference = { + readonly value: { + readonly kind: string; + readonly loader?: string; + readonly loader_authority?: "candidate"; + readonly operation: "load" | "resolve"; + readonly specifier: string | null; + }; +}; + +/** Whether any loaded package reference imports or requires Electron. */ +export const referencesElectronModule = ( + references: readonly ModuleReference[], +): boolean => + references.some( + ({ value }) => + value.operation === "load" && + ELECTRON_REFERENCE_KINDS.has(value.kind) && + value.loader !== "factory-loader" && + // A candidate loader only observes the literal; the CommonJS loader + // behind it is not established, so it does not prove Electron. + value.loader_authority !== "candidate" && + value.specifier !== null && + ELECTRON_MODULE.test(value.specifier), + ); + +/** Whether any static import or require leaves its module name unresolved. */ +export const referencesUnresolvedModule = ( + references: readonly ModuleReference[], +): boolean => + references.some( + ({ value }) => + value.operation === "load" && + ELECTRON_REFERENCE_KINDS.has(value.kind) && + value.specifier === null, + ); + /** * Electron's CommonJS exports define no `default` member, so a leading * `default` segment can only denote the module object itself, as an ES module diff --git a/src/domain/javascript/javascriptSemanticEffectLog.ts b/src/domain/javascript/javascriptSemanticEffectLog.ts index cab5204ce0..e235299536 100644 --- a/src/domain/javascript/javascriptSemanticEffectLog.ts +++ b/src/domain/javascript/javascriptSemanticEffectLog.ts @@ -312,6 +312,27 @@ export const ambientPathMayHaveChanged = ( state.ambientWrites.covers(path) || (observes === "contents" && state.ambientEscapes.covers(path)); +/** + * Whether a source-visible ambient write or escape recorded anywhere in the + * source reaches `path`, one of its prefixes or any member below it. A reader + * that depends on every member of an ambient object asks this. The answer + * ignores timing, so it holds at every read site. + */ +export const ambientPathOrMemberMayHaveChanged = ( + state: JavaScriptSemanticAnalysisState, + path: JavaScriptSemanticPropertyPath, +): boolean => + state.ambientWrites.touches(path) || state.ambientEscapes.touches(path); + +/** + * Whether an assignment to the ambient binding `name` is recorded anywhere in + * the source. The answer ignores timing, so it holds at every read site. + */ +export const ambientBindingHasWrites = ( + state: JavaScriptSemanticAnalysisState, + name: string, +): boolean => state.ambientBindingWrites.has(name); + /** Whether a recorded ambient effect's path is `target` or one of its prefixes. */ export const ambientEffectCovers = ( effect: JavaScriptSemanticAnalysisState["ambientWriteEffects"][number], @@ -713,6 +734,15 @@ export const bindingEffectsConfinedToReceiverCalls = ( path.length === 0 && node !== undefined && matchesCall(node), ); +/** + * Whether an escape of `binding`'s object is recorded at a site other than + * `except`, the call a reader is already evaluating. + */ +export const bindingHasEscapesOutside = ( + binding: JavaScriptSemanticBindingState, + except: t.Node | undefined, +): boolean => binding.escapedPaths.some((effect) => effect.node !== except); + /** * The nodes recorded member writes to `binding`'s object involve, in record * order: each write's site (undefined when it was recorded without one), then diff --git a/src/domain/javascript/javascriptSemanticPropertyPaths.test.ts b/src/domain/javascript/javascriptSemanticPropertyPaths.test.ts index ec4971b96a..f1df784d59 100644 --- a/src/domain/javascript/javascriptSemanticPropertyPaths.test.ts +++ b/src/domain/javascript/javascriptSemanticPropertyPaths.test.ts @@ -51,6 +51,26 @@ it("preserves numeric key identity and absent capture identity", () => { expect(coverage.covers([1], ["captured"])).toBe(false); }); +it("touches a prefix through members retained below it", () => { + const coverage = new SemanticPropertyPathCoverage(); + expect(coverage.retain(["global", "Object", "prototype", "load"])).toBe(true); + expect(coverage.covers(["global", "Object", "prototype"])).toBe(false); + expect(coverage.touches(["global", "Object", "prototype"])).toBe(true); + expect(coverage.touches(["global", "Object", "prototype", "load"])).toBe( + true, + ); + expect(coverage.touches(["global", "Object", "prototype", "other"])).toBe( + false, + ); + expect(coverage.touches(["global", "Array", "prototype"])).toBe(false); + expect(coverage.touches(["import-meta"])).toBe(false); + expect(coverage.retain(["import-meta", "url"], ["site", undefined])).toBe( + true, + ); + expect(coverage.touches(["import-meta"], ["site"])).toBe(true); + expect(coverage.touches(["import-meta"], ["other"])).toBe(false); +}); + it("lets an escaped root cover every deeper capture", () => { const coverage = new SemanticPropertyPathCoverage(); expect(coverage.retain([])).toBe(true); diff --git a/src/domain/javascript/javascriptSemanticPropertyPaths.ts b/src/domain/javascript/javascriptSemanticPropertyPaths.ts index 4c86dffb08..09cfe3a0ae 100644 --- a/src/domain/javascript/javascriptSemanticPropertyPaths.ts +++ b/src/domain/javascript/javascriptSemanticPropertyPaths.ts @@ -147,6 +147,22 @@ export class SemanticPropertyPathCoverage { covers( path: JavaScriptSemanticPropertyPath, contexts?: readonly (string | undefined)[], + ): boolean { + return this.#reaches(path, contexts, false); + } + + /** Whether a retained prefix covers this path or any member below it. */ + touches( + path: JavaScriptSemanticPropertyPath, + contexts?: readonly (string | undefined)[], + ): boolean { + return this.#reaches(path, contexts, true); + } + + #reaches( + path: JavaScriptSemanticPropertyPath, + contexts: readonly (string | undefined)[] | undefined, + below: boolean, ): boolean { const pending = [{ node: this.#root, offset: 0 }]; while (pending.length > 0) { @@ -154,7 +170,16 @@ export class SemanticPropertyPathCoverage { if (current === undefined) break; if (current.node.terminal) return true; const key = path[current.offset]; - if (key === undefined) continue; + if (key === undefined) { + // Retained prefixes only create nodes along their own path, so any + // child here belongs to a member below the queried path. + if ( + below && + (current.node.exact.size > 0 || current.node.selectors.size > 0) + ) + return true; + continue; + } const context = contexts?.[current.offset]; if (key !== null && typeof key !== "object") { const exact = current.node.exact.get( diff --git a/src/domain/javascript/javascriptStaticAnalysis.ts b/src/domain/javascript/javascriptStaticAnalysis.ts index d8db46a393..5836caadfd 100644 --- a/src/domain/javascript/javascriptStaticAnalysis.ts +++ b/src/domain/javascript/javascriptStaticAnalysis.ts @@ -19,8 +19,7 @@ import { RETURNED_REFERENCE_WIDENING_LIMITATION } from "./javascriptSemanticCove import { collectElectronWrappedCallPathsSteps } from "./electronWrappedCallPaths.js"; import { ELECTRON_IDENTITY_LIMITATION, - ELECTRON_MODULE, - ELECTRON_REFERENCE_KINDS, + referencesElectronModule, } from "./javascriptElectronMemberWrites.js"; import { traverseJavaScriptAstSteps } from "./javascriptSemanticTraversal.js"; @@ -233,6 +232,7 @@ export function* analyzeParsedJavaScriptStaticSourceSteps( source, accumulator, electronIdentity, + () => index.indexMemberMutationFactsSteps(), ); addSourceMapDirectives(source, file.comments ?? [], accumulator); return finalizeStaticAnalysis(source, file, accumulator, bindings, routes); @@ -268,24 +268,7 @@ const finalizeStaticAnalysis = ( const parserErrors = recoveredJavaScriptSyntaxErrorCount(file.errors); const wrapperRedeclarations = commonJsWrapperRedeclarationCount(file.errors); const typeOnlySideEffect = hasUncertainTypeOnlyModuleSideEffect(file.program); - // A `worker` or `service-worker` reference records a URL relative to the - // importing source, so `new Worker(new URL("electron", import.meta.url))` is - // not an Electron module. Read the same kinds the Electron boundary owner uses. - // Only a loaded module establishes Electron applicability: `require.resolve` - // locates the module without loading it, so no boundary fact is expected. - const electronObserved = accumulator.references.some( - ({ value }) => - value.specifier !== null && - value.operation === "load" && - ELECTRON_REFERENCE_KINDS.has(value.kind) && - ELECTRON_MODULE.test(value.specifier) && - value.loader_authority !== "candidate" && - // A `factory-loader` reference names a recovered bundle module, so its - // specifier is a module key inside the artifact rather than a package. A - // real external load in the same file keeps its `commonjs-loader` - // identity, so it is still counted. - value.loader !== "factory-loader", - ); + const electronObserved = referencesElectronModule(accumulator.references); const resourceLimits = [...bindings.valueResourceLimits].sort(); const controls = semanticPrecisionControls( resourceLimits, diff --git a/src/domain/javascript/javascriptSymbolicBindings.test.ts b/src/domain/javascript/javascriptSymbolicBindings.test.ts index c45df0622e..2096bdefb5 100644 --- a/src/domain/javascript/javascriptSymbolicBindings.test.ts +++ b/src/domain/javascript/javascriptSymbolicBindings.test.ts @@ -14,7 +14,7 @@ import { type SymbolicValue, } from "./javascriptSymbolicValues.js"; -const resolverFor = (source: string) => { +const resolverFor = (source: string, indexMemberMutationFacts?: () => void) => { const parsed = parseJavaScriptSource(source); if (parsed === null) throw new Error("symbolic source fixture did not parse"); const state = completeSemanticSteps( @@ -22,7 +22,12 @@ const resolverFor = (source: string) => { ); return { parsed, - resolver: new JavaScriptSymbolicBindings(state, parsed.program), + resolver: new JavaScriptSymbolicBindings( + state, + parsed.program, + undefined, + indexMemberMutationFacts, + ), }; }; @@ -33,8 +38,9 @@ const inspectFixture = ( resolver: JavaScriptSymbolicBindings, node: t.CallExpression, ) => SymbolicValue = (resolver, node) => resolver.evaluate(node.arguments[0]), + indexMemberMutationFacts?: () => void, ) => { - const { parsed, resolver } = resolverFor(source); + const { parsed, resolver } = resolverFor(source, indexMemberMutationFacts); const values: SymbolicValue[] = []; traverseJavaScriptAst(parsed.program, { enter: (node) => { @@ -49,6 +55,26 @@ const inspectFixture = ( return { parsed, resolver, values }; }; +it("requests member-mutation facts only for a late initializer proof", () => { + const indexMemberMutationFacts = vi.fn(); + inspectFixture( + `function run() { inspect(format("value")); } + var format = value => value; + run();`, + undefined, + indexMemberMutationFacts, + ); + expect(indexMemberMutationFacts).toHaveBeenCalledOnce(); + + indexMemberMutationFacts.mockClear(); + inspectFixture( + `const value = "ready"; inspect(value);`, + undefined, + indexMemberMutationFacts, + ); + expect(indexMemberMutationFacts).not.toHaveBeenCalled(); +}); + const inspectedValues = (source: string): SymbolicValue[] => inspectFixture(source).values; @@ -60,6 +86,54 @@ const inspectOne = (source: string): SymbolicValue => { return value; }; +it("bounds a late loader proof across 15,000 aliases", () => { + const aliases = Array.from({ length: 15_000 }, (_, index) => + index === 0 ? "var a0 = require;" : `var a${index} = a${index - 1};`, + ).join("\n"); + const { resolver, values } = inspectFixture(` + function run() { inspect(load); } + ${aliases} + var load = a14999; + `); + + expect(values).toEqual([expect.objectContaining({ kind: "unknown" })]); + expect(resolver.unresolvedLoader).toBe(true); + expect(resolver.truncated).toBe(true); +}); + +it("bounds a late loader proof across 15,000 expression nodes", () => { + // Build the accepted AST directly so this tests the resolver's depth bound + // independently of the source parser's own stack capacity. + let initializer: t.Expression = t.identifier("value0"); + for (let index = 1; index < 15_000; index += 1) + initializer = t.binaryExpression( + "+", + initializer, + t.identifier(`value${index}`), + ); + const read = t.identifier("load"); + const program = t.program([ + t.functionDeclaration( + t.identifier("run"), + [], + t.blockStatement([ + t.expressionStatement( + t.callExpression(t.identifier("inspect"), [read]), + ), + ]), + ), + t.variableDeclaration("var", [ + t.variableDeclarator(t.identifier("load"), initializer), + ]), + ]); + const state = completeSemanticSteps(indexJavaScriptBindingsSteps(program)); + const resolver = new JavaScriptSymbolicBindings(state, program); + + expect(resolver.evaluate(read)).toMatchObject({ kind: "unknown" }); + expect(resolver.unresolvedLoader).toBe(true); + expect(resolver.truncated).toBe(true); +}); + it("substitutes numeric modules, named exports and captured wrapper arguments", () => { const { resolver, values } = inspectFixture(`(() => { const modules = { diff --git a/src/domain/javascript/javascriptSymbolicBindings.ts b/src/domain/javascript/javascriptSymbolicBindings.ts index 11378984b2..e66569714d 100644 --- a/src/domain/javascript/javascriptSymbolicBindings.ts +++ b/src/domain/javascript/javascriptSymbolicBindings.ts @@ -15,6 +15,7 @@ import { createSemanticExpressionShapeResolver } from "./javascriptSemanticExpre import { semanticRequireOriginCandidate } from "./javascriptSemanticRequireOrigin.js"; import { javaScriptRuntimeContext } from "./javascriptSourceParser.js"; import { createGlobalReferenceReader } from "./javascriptSemanticAmbientWrites.js"; +import { semanticCallableResultExpressions } from "./javascriptSemanticCallableResults.js"; import { javascriptBootstrapAt } from "./javascriptBootstrap.js"; import { ELECTRON_MODULE } from "./javascriptElectronMemberWrites.js"; import { @@ -22,19 +23,32 @@ import { type JavaScriptElectronIdentity, } from "./javascriptSemanticAnalysis.js"; import { semanticArrayIndex } from "./javascriptSemanticPropertyPaths.js"; +import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; +import { + ambientBindingHasWrites, + ambientPathMayHaveChanged, + ambientPathOrMemberMayHaveChanged, + bindingHasEscapesOutside, + bindingHasMemberWrites, +} from "./javascriptSemanticEffectLog.js"; import { symbolicLogicalRight } from "./javascriptSymbolicControlFlow.js"; import { moduleFactory, modulePropertyName, } from "./javascriptStaticAnalysisHelpers.js"; +import { isUnmodifiedImportMetaUrl } from "./javascriptSemanticGlobalReference.js"; import { completeSemanticSteps, traverseJavaScriptAst, } from "./javascriptSemanticTraversal.js"; import { resolveSemanticBindingState, + semanticPrototypeOwners, semanticResolutionBlocked, + semanticStableInitializer, type JavaScriptSemanticAnalysisState, + type JavaScriptSemanticBindingState, + type SemanticPrototypeOwner, } from "./javascriptSemanticState.js"; import { symbolicCondition, @@ -71,6 +85,7 @@ interface SymbolicReadBoundary { readonly edge: "start" | "end"; } type SymbolicNamespace = Extract; +type LoaderFreeArgument = boolean | t.Expression; const emptyEnvironment: SymbolicEnvironment = { values: new Map() }; @@ -85,14 +100,69 @@ const containsOwnedReturn = (root: t.Node): boolean => { return found; }; +const LOADER_FREE_AMBIENT_VALUES: ReadonlySet = new Set([ + "Boolean", + "String", + "clearTimeout", + "queueMicrotask", + "setTimeout", + "undefined", +]); + +const literalPrototypeOwner = ( + node: t.Node, +): SemanticPrototypeOwner | undefined => { + if (t.isObjectExpression(node)) return "Object"; + if (t.isArrayExpression(node)) return "Array"; + if (t.isStringLiteral(node) || t.isTemplateLiteral(node)) return "String"; + if (t.isFunction(node) || t.isClass(node)) return "Function"; + if (t.isNumericLiteral(node)) return "Number"; + if (t.isBooleanLiteral(node)) return "Boolean"; + if (t.isRegExpLiteral(node)) return "RegExp"; + if (t.isBigIntLiteral(node)) return "BigInt"; + return undefined; +}; + +const isImportMeta = (node: t.Node): node is t.MetaProperty => + t.isMetaProperty(node) && + node.meta.name === "import" && + node.property.name === "meta"; + +const isFreshConstruction = (node: t.Node): boolean => { + if (!t.isNewExpression(node)) return false; + const constructor = unwrapJavaScriptExpression(node.callee).node; + if ( + !t.isClass(constructor) || + constructor.superClass !== null || + constructor.decorators?.length + ) + return false; + const method = constructor.body.body.find( + (member): member is t.ClassMethod => + t.isClassMethod(member) && member.kind === "constructor", + ); + return ( + method === undefined || + semanticCallableResultExpressions(method).every( + ({ node: result }) => result === null, + ) + ); +}; + +const isFreshObject = (node: t.Node): boolean => + t.isObjectExpression(node) || + t.isArrayExpression(node) || + t.isFunction(node) || + (t.isClass(node) && !node.decorators?.length) || + t.isRegExpLiteral(node) || + isFreshConstruction(node); + /** Demand-driven substitutions for local bindings, closures and literal module tables. */ export class JavaScriptSymbolicBindings { readonly #writes = new Map(); + readonly #aliasMemberWrites = new Map, PropertyWrite[]>(); readonly #environments = new WeakMap(); - readonly #cache = new WeakMap< - SymbolicEnvironment, - WeakMap - >(); + #cache = new WeakMap>(); readonly #active = new Set(); readonly #activeFunctions = new Set(); readonly #exportAliases = new Map(); @@ -114,11 +184,21 @@ export class JavaScriptSymbolicBindings { ReadonlyMap | null >(); readonly #mutated = new Set(); + readonly #mutatedAliasGroups = new Set>(); readonly #electronIdentity: JavaScriptElectronIdentity; + readonly #indexMemberMutationFacts: (() => boolean | void) | undefined; readonly #namespaceOwners = new Map(); readonly #moduleFactories = new WeakSet(); readonly #unsafeExports = new Set(); readonly #aliasesByBinding = new Map(); + readonly #aliasSourcesByBinding = new Map(); + readonly #bindingAliasGroups = new Map>(); + readonly #prototypeMutationOwners: ReadonlySet; + readonly #loaderFreeBindings = new Map(); + readonly #loaderFreeBindingsInProgress = new Set(); + #loaderFreeFunctionCalls = new WeakMap>(); + #loaderProofDepth = 0; + #memberMutationFactsReady: boolean; readonly #bootstrapTables: Array<{ readonly tableAliases: ReadonlySet; readonly modules: Map; @@ -131,6 +211,14 @@ export class JavaScriptSymbolicBindings { return this.resourceLimits.size > 0; } unresolved = false; + /** An evaluated require named an Electron module. */ + reachedElectron = false; + /** An evaluated require left its module name unresolved. */ + unresolvedRequire = false; + /** A read gave up before an initializer that may supply a module loader. */ + unresolvedLoader = false; + /** A loader-free proof needs the yielding member-mutation index. */ + memberMutationFactsRequired = false; constructor( readonly state: JavaScriptSemanticAnalysisState, @@ -138,9 +226,17 @@ export class JavaScriptSymbolicBindings { electronIdentity = completeSemanticSteps( indexJavaScriptElectronIdentitySteps(program, state), ), + indexMemberMutationFacts?: () => boolean | void, ) { this.#wrapperVisits = new Budget(state.precision.wrapper_visits); this.#electronIdentity = electronIdentity; + this.#indexMemberMutationFacts = indexMemberMutationFacts; + this.#memberMutationFactsReady = + state.memberMutationFactsComplete || + indexMemberMutationFacts === undefined; + this.#prototypeMutationOwners = new Set( + state.prototypeMutationSites.map(({ owner }) => owner), + ); const shapes = createSemanticExpressionShapeResolver(state); const defineProperty = createGlobalReferenceReader(state, [ "Object", @@ -393,7 +489,7 @@ export class JavaScriptSymbolicBindings { } if (value.kind === "namespace") { if ( - this.#mutated.has(value.bindingId) || + this.#bindingMutated(value.bindingId) || this.#unsafeExports.has(`${value.bindingId}:${key}`) ) return symbolicUnknown("ambiguous export writes"); @@ -570,7 +666,7 @@ export class JavaScriptSymbolicBindings { ); if (value.kind === "record") return 0; if (value.kind === "namespace") { - if (this.#mutated.has(value.bindingId)) return 1; + if (this.#bindingMutated(value.bindingId)) return 1; const available = this.#namespaceWrites(value); if (available === undefined) return 1; if (available.some((write) => write.receiverUncertain)) return 1; @@ -656,6 +752,15 @@ export class JavaScriptSymbolicBindings { }); } + /** Retry provisional reads after the yielding mutation index completes. */ + memberMutationFactsIndexed(): void { + this.memberMutationFactsRequired = false; + this.#memberMutationFactsReady = true; + this.#cache = new WeakMap(); + this.#loaderFreeBindings.clear(); + this.#loaderFreeFunctionCalls = new WeakMap(); + } + #evaluate(node: t.Node, env: SymbolicEnvironment): SymbolicValue { if ( t.isStringLiteral(node) || @@ -808,13 +913,16 @@ export class JavaScriptSymbolicBindings { const electronOrigin = this.#electronIdentity.exportPath(node) ?? this.#electronIdentity.candidateExportPath(node); - if (electronOrigin === undefined && this.#mutated.has(binding.bindingId)) + if (electronOrigin === undefined && this.#bindingMutated(binding.bindingId)) return symbolicUnknown("dynamic member writes"); for (let e: SymbolicEnvironment | undefined = env; e; e = e.parent) { const value = e.values.get(binding.bindingId); if (value) return value; } - if (electronOrigin === undefined && this.#writes.has(binding.bindingId)) + if ( + electronOrigin === undefined && + this.#bindingHasWrites(binding.bindingId) + ) return symbolicUnknown("overwritten object members"); const initializer = binding.initializers.length === 1 ? binding.initializers[0] : undefined; @@ -831,6 +939,12 @@ export class JavaScriptSymbolicBindings { initializer.node.end > point) ) { this.unresolved = true; + const invocation = this.#calleeInvocation(node); + if ( + !this.#isLoaderFreeBinding(binding, invocation) && + !this.memberMutationFactsRequired + ) + this.unresolvedLoader = true; return symbolicUnknown("initializer not available at this read"); } if (this.state.conditionallySkippedExpressions.has(initializer.node)) @@ -846,6 +960,538 @@ export class JavaScriptSymbolicBindings { return value; } + #isLoaderFreeBinding( + binding: JavaScriptSemanticBindingState, + invocation?: t.CallExpression | t.OptionalCallExpression, + callerParameterLoaderFree?: ReadonlyMap, + proofInvocation?: t.CallExpression | t.OptionalCallExpression, + ): boolean { + if (!this.#memberMutationFactsReady) { + if (this.#indexMemberMutationFacts?.() === false) { + this.memberMutationFactsRequired = true; + return false; + } + this.#memberMutationFactsReady = true; + } + if ( + this.#bindingMutated(binding.bindingId) || + this.#bindingHasWrites(binding.bindingId) || + bindingHasMemberWrites(binding) || + bindingHasEscapesOutside(binding, proofInvocation) + ) + return false; + if (invocation === undefined && proofInvocation === undefined) { + const cached = this.#loaderFreeBindings.get(binding.bindingId); + if (cached !== undefined) return cached; + } + if (this.#loaderFreeBindingsInProgress.has(binding.bindingId)) return false; + if (this.#loaderProofDepth >= this.state.precision.wrapper_depth) { + this.resourceLimits.add("wrapper_depth"); + return false; + } + const initializer = + binding.definitions.length === 1 && + ["variable", "function", "class"].includes( + binding.definitions[0]?.kind ?? "", + ) && + binding.initializers.length === 1 + ? binding.initializers[0] + : undefined; + if (initializer === undefined) { + if (invocation === undefined && proofInvocation === undefined) + this.#loaderFreeBindings.set(binding.bindingId, false); + return false; + } + this.#loaderFreeBindingsInProgress.add(binding.bindingId); + this.#loaderProofDepth += 1; + try { + let projected = initializer.node; + for (const key of initializer.projection) { + if ( + key === null || + (!t.isExpression(projected) && !t.isSuper(projected)) + ) + return false; + projected = t.memberExpression( + projected, + typeof key === "number" + ? t.numericLiteral(key) + : t.stringLiteral(key), + true, + ); + } + const loaderFree = this.#isLoaderFree( + projected, + 0, + invocation, + callerParameterLoaderFree, + ); + if (invocation === undefined && proofInvocation === undefined) + this.#loaderFreeBindings.set(binding.bindingId, loaderFree); + return loaderFree; + } finally { + this.#loaderProofDepth -= 1; + this.#loaderFreeBindingsInProgress.delete(binding.bindingId); + } + } + + #isLoaderFree( + node: t.Node, + depth = 0, + invocation?: t.CallExpression | t.OptionalCallExpression, + callerParameterLoaderFree?: ReadonlyMap, + ): boolean { + if (!this.#chargeLoaderProof()) return false; + if (depth >= this.state.precision.wrapper_depth) { + this.resourceLimits.add("wrapper_depth"); + return false; + } + if (t.isTemplateLiteral(node)) + return ( + node.quasis.every( + (quasi) => + this.#chargeLoaderProof() && + !ELECTRON_MODULE.test(quasi.value.cooked ?? quasi.value.raw), + ) && + node.expressions.every((expression) => + this.#isLoaderFree(expression, depth + 1), + ) + ); + if (t.isLiteral(node)) return true; + if (t.isIdentifier(node)) { + const binding = resolveSemanticBindingState(this.state, node, node.name); + return binding !== undefined + ? this.#isLoaderFreeBinding( + binding, + invocation, + callerParameterLoaderFree, + ) + : this.#isLoaderFreeAmbientReference(node); + } + if (t.isUnaryExpression(node)) + return ( + node.operator === "typeof" || + this.#isLoaderFree(node.argument, depth + 1) + ); + if (t.isBinaryExpression(node)) + return ( + this.#isLoaderFree(node.left, depth + 1) && + this.#isLoaderFree(node.right, depth + 1) + ); + if (t.isLogicalExpression(node)) + return ( + this.#isLoaderFree( + node.left, + depth + 1, + invocation, + callerParameterLoaderFree, + ) && + this.#isLoaderFree( + node.right, + depth + 1, + invocation, + callerParameterLoaderFree, + ) + ); + if (t.isConditionalExpression(node)) + return ( + this.#isLoaderFree(node.test, depth + 1) && + this.#isLoaderFree( + node.consequent, + depth + 1, + invocation, + callerParameterLoaderFree, + ) && + this.#isLoaderFree( + node.alternate, + depth + 1, + invocation, + callerParameterLoaderFree, + ) + ); + if (t.isSequenceExpression(node)) + return node.expressions.every((expression, index) => + this.#isLoaderFree( + expression, + depth + 1, + index === node.expressions.length - 1 ? invocation : undefined, + callerParameterLoaderFree, + ), + ); + if (t.isObjectExpression(node)) + return node.properties.every((property) => { + if (!this.#chargeLoaderProof()) return false; + if (t.isSpreadElement(property)) + return this.#isLoaderFree(property.argument, depth + 1); + if (t.isObjectMethod(property)) + return ( + (!property.computed || + this.#isLoaderFree(property.key, depth + 1)) && + this.#isLoaderFreeFunction(property) + ); + return ( + (!property.computed || this.#isLoaderFree(property.key, depth + 1)) && + this.#isLoaderFree(property.value, depth + 1) + ); + }); + if (t.isArrayExpression(node)) + return node.elements.every( + (element) => + element === null || + (t.isSpreadElement(element) + ? this.#chargeLoaderProof() && + this.#isLoaderFree(element.argument, depth + 1) + : this.#isLoaderFree(element, depth + 1)), + ); + if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) + return this.#isLoaderFreeLiteralMember(node, depth) ?? false; + if (t.isFunction(node)) + return this.#isLoaderFreeFunction( + node, + invocation, + callerParameterLoaderFree, + ); + return false; + } + + #isLoaderFreeFunction( + node: t.Function, + invocation?: t.CallExpression | t.OptionalCallExpression, + callerParameterLoaderFree?: ReadonlyMap, + ): boolean { + const functionScope = this.state.scopeByNode.get(node); + if (functionScope === undefined) return false; + const parameterLoaderFree = new Map(); + const parameterStates: string[] = []; + const argumentLoaderFree = ( + argument: t.CallExpression["arguments"][number] | undefined, + ): LoaderFreeArgument => { + if (argument === undefined || !t.isExpression(argument)) return false; + if (t.isIdentifier(argument)) { + const binding = resolveSemanticBindingState( + this.state, + argument, + argument.name, + ); + if (binding?.kind === "parameter") + return callerParameterLoaderFree?.get(binding.bindingId) ?? false; + } + return this.#isLoaderFree( + argument, + 0, + undefined, + callerParameterLoaderFree, + ) + ? argument + : false; + }; + const firstSpread = + invocation?.arguments.findIndex(t.isSpreadElement) ?? -1; + runtimeFunctionParameters(node).forEach((parameter, index) => { + const target = t.isRestElement(parameter) + ? parameter.argument + : parameter; + if (!t.isIdentifier(target)) { + parameterStates.push("0"); + return; + } + const binding = resolveSemanticBindingState( + this.state, + target, + target.name, + ); + const loaderFree: LoaderFreeArgument = t.isRestElement(parameter) + ? firstSpread < 0 && + invocation !== undefined && + invocation.arguments + .slice(index) + .every((argument) => argumentLoaderFree(argument) !== false) + : firstSpread >= 0 && index >= firstSpread + ? false + : argumentLoaderFree(invocation?.arguments[index]); + if (binding !== undefined) + parameterLoaderFree.set(binding.bindingId, loaderFree); + parameterStates.push( + loaderFree === false + ? "0" + : loaderFree === true + ? "1" + : `1:${loaderFree.start ?? "?"}:${loaderFree.end ?? "?"}`, + ); + }); + const signature = parameterStates.join("|"); + if (invocation !== undefined) { + const cached = this.#loaderFreeFunctionCalls.get(node)?.get(signature); + if (cached !== undefined) return cached; + } + let loaderFree = true; + traverseJavaScriptAst(node, { + skip: () => !loaderFree, + enter: (child, parent, readAncestors) => { + if (!this.#chargeLoaderProof()) { + loaderFree = false; + return; + } + const literalMemberProof = + t.isMemberExpression(child) || t.isOptionalMemberExpression(child) + ? (this.#isLoaderFreeLiteralMember(child, 0) ?? + this.#isLoaderFreeImportMetaMember(child)) + : undefined; + if ( + (t.isIdentifier(child) && + parent !== null && + t.isReferenced(child, parent, readAncestors().at(-2)) && + !this.#isLoaderFreeFunctionReference( + child, + functionScope.scopeId, + parameterLoaderFree, + invocation, + )) || + t.isThisExpression(child) || + t.isSuper(child) || + t.isImport(child) || + t.isImportExpression(child) || + literalMemberProof === false || + (t.isStringLiteral(child) && + ELECTRON_MODULE.test(child.value) && + !(t.isBinaryExpression(parent) && parent.operator === "+")) + ) + loaderFree = false; + }, + }); + if (loaderFree) + loaderFree = semanticCallableResultExpressions(node).every( + ({ node: result }) => + this.#chargeLoaderProof() && this.#isLoaderFreeResult(node, result), + ); + if (invocation !== undefined) { + const cached = this.#loaderFreeFunctionCalls.get(node) ?? new Map(); + cached.set(signature, loaderFree); + this.#loaderFreeFunctionCalls.set(node, cached); + } + return loaderFree; + } + + /** + * The caller selects members on a late function's result after this proof, + * so a result whose prototype chain or `import.meta` gained members can + * supply a loader without a member expression inside the function. + */ + #isLoaderFreeResult(fn: t.Function, result: t.Node | null): boolean { + if ((fn.async || fn.generator) && this.#inheritedMembersChanged("Object")) + return false; + if (result === null) return true; + const value = unwrapJavaScriptExpression(result).node; + if ( + t.isNullLiteral(value) || + t.isUnaryExpression(value, { operator: "void" }) || + (t.isIdentifier(value) && + value.name === "undefined" && + resolveSemanticBindingState(this.state, value, value.name) === + undefined && + !semanticResolutionBlocked(this.state, value, value.name)) + ) + return true; + if (isImportMeta(value)) return !this.#importMetaMembersChanged(); + const owner = literalPrototypeOwner(value); + if (owner !== undefined) return !this.#inheritedMembersChanged(owner); + return ( + !semanticPrototypeOwners.some((candidate) => + this.#inheritedMembersChanged(candidate), + ) && !this.#importMetaMembersChanged() + ); + } + + /** Whether any member a value of this prototype inherits may have changed. */ + #inheritedMembersChanged(owner: SemanticPrototypeOwner): boolean { + const owners: readonly SemanticPrototypeOwner[] = + owner === "Object" ? [owner] : [owner, "Object"]; + return owners.some((prototypeOwner) => { + const path = ["global", prototypeOwner, "prototype"] as const; + return ( + this.#prototypeMutationOwners.has(prototypeOwner) || + ambientPathOrMemberMayHaveChanged(this.state, path) + ); + }); + } + + #importMetaMembersChanged(): boolean { + return ambientPathOrMemberMayHaveChanged(this.state, ["import-meta"]); + } + + #isLoaderFreeFunctionReference( + node: t.Identifier, + functionScopeId: string, + parameterLoaderFree: ReadonlyMap, + proofInvocation?: t.CallExpression | t.OptionalCallExpression, + ): boolean { + const binding = resolveSemanticBindingState(this.state, node, node.name); + if (binding === undefined) return this.#isLoaderFreeAmbientReference(node); + if (binding.kind === "parameter") { + const argument = parameterLoaderFree.get(binding.bindingId) ?? false; + const parent = this.state.parentsByNode.get(node); + const memberReceiver = + (t.isMemberExpression(parent) || + t.isOptionalMemberExpression(parent)) && + parent.object === node; + if (!memberReceiver) return argument !== false; + if (argument === false || argument === true) return false; + const member = this.#parameterMemberExpression(node, argument); + return member !== undefined && this.#isLoaderFree(member); + } + if (binding.kind === "catch") return false; + const invocation = this.#calleeInvocation(node); + // The walk visits a local's inline initializer, but a destructuring + // projection reads a member without a member expression to inspect. + return ( + (this.#scopeContains(binding.scopeId, functionScopeId) && + binding.initializers.every( + ({ projection }) => projection.length === 0, + )) || + this.#isLoaderFreeBinding( + binding, + invocation, + parameterLoaderFree, + proofInvocation, + ) + ); + } + + #isLoaderFreeAmbientReference(node: t.Identifier): boolean { + if ( + semanticResolutionBlocked(this.state, node, node.name) || + ambientBindingHasWrites(this.state, node.name) || + !LOADER_FREE_AMBIENT_VALUES.has(node.name) + ) + return false; + const path = ["global", node.name] as const; + if (ambientPathMayHaveChanged(this.state, path, "contents")) return false; + const parent = this.state.parentsByNode.get(node); + if ( + !(t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) || + parent.object !== node + ) { + // Direct invocation and typeof use the global itself. A value read can + // hand the object to a later member selection, including through an alias. + return ( + this.#calleeInvocation(node) !== undefined || + t.isUnaryExpression(parent, { operator: "typeof" }) || + !ambientPathOrMemberMayHaveChanged(this.state, path) + ); + } + const key = semanticStaticPropertyKey(parent.property, parent.computed); + if (key === null) return false; + const memberPath = [...path, key] as const; + const inheritedMemberPaths = [ + [...path, "__proto__", key], + ["global", "Function", "prototype", key], + ["global", "Object", "prototype", key], + ] as const; + return ( + !ambientPathMayHaveChanged(this.state, memberPath, "contents") && + inheritedMemberPaths.every( + (inheritedPath) => + !ambientPathMayHaveChanged(this.state, inheritedPath, "contents"), + ) + ); + } + + #parameterMemberExpression( + node: t.Identifier, + argument: LoaderFreeArgument, + ): t.Expression | undefined { + if (argument === false || argument === true) return undefined; + let source: t.Expression = node; + let projected = argument; + let parent = this.state.parentsByNode.get(source); + if ( + !(t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) || + parent.object !== source + ) + return undefined; + do { + projected = t.isOptionalMemberExpression(parent) + ? t.optionalMemberExpression( + projected, + parent.property, + parent.computed, + parent.optional, + ) + : t.memberExpression(projected, parent.property, parent.computed); + source = parent; + parent = this.state.parentsByNode.get(source); + } while ( + (t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) && + parent.object === source + ); + return projected; + } + + #chargeLoaderProof(): boolean { + return this.#spendWrapperVisits(1); + } + + #calleeInvocation( + node: t.Node, + ): t.CallExpression | t.OptionalCallExpression | undefined { + let callee = node; + let parent = this.state.parentsByNode.get(callee); + while ( + t.isSequenceExpression(parent) && + parent.expressions.at(-1) === callee + ) { + callee = parent; + parent = this.state.parentsByNode.get(callee); + } + return (t.isCallExpression(parent) || t.isOptionalCallExpression(parent)) && + parent.callee === callee + ? parent + : undefined; + } + + #isLoaderFreeLiteralMember( + node: t.MemberExpression | t.OptionalMemberExpression, + depth: number, + ): boolean | undefined { + const object = unwrapJavaScriptExpression(node.object).node; + const owner = literalPrototypeOwner(object); + if (owner === undefined) return undefined; + const key = semanticStaticPropertyKey(node.property, node.computed); + const ownObjectProperty = + key !== null && + t.isObjectExpression(object) && + object.properties.some( + (property) => + !t.isSpreadElement(property) && + !isObjectPrototypeSetter(property, key) && + semanticStaticPropertyKey(property.key, property.computed) === key, + ); + const inheritedMemberChanged = + key !== null && + (owner === "Object" ? [owner] : [owner, "Object"]).some( + (prototypeOwner) => { + const path = ["global", prototypeOwner, "prototype", key] as const; + return ambientPathMayHaveChanged(this.state, path, "contents"); + }, + ); + return ( + this.#isLoaderFree(object, depth + 1) && + (!node.computed || this.#isLoaderFree(node.property, depth + 1)) && + (ownObjectProperty || + (!inheritedMemberChanged && + !this.#prototypeMutationOwners.has(owner) && + !this.#prototypeMutationOwners.has("Object"))) + ); + } + + #isLoaderFreeImportMetaMember( + node: t.MemberExpression | t.OptionalMemberExpression, + ): boolean | undefined { + const object = unwrapJavaScriptExpression(node.object).node; + if (!isImportMeta(object)) return undefined; + return isUnmodifiedImportMetaUrl(node, this.state); + } + #readBoundary( bindingScopeId: string, environment: SymbolicEnvironment, @@ -1036,7 +1682,7 @@ export class JavaScriptSymbolicBindings { read?: SymbolicPropertyRead, selectedKey?: string, ): readonly PropertyWrite[] | undefined { - const writes = this.#writes.get(value.bindingId) ?? []; + const writes = this.#bindingWrites(value.bindingId); const completedAt = value.environment.completedAt; const boundary = read === undefined ? undefined : this.#namespaceReadBoundary(value, read); @@ -1264,18 +1910,21 @@ export class JavaScriptSymbolicBindings { value.path.join(".") === "require" ) { const spec = args[0]; - result = - spec?.kind === "literal" && typeof spec.value === "string" - ? { - kind: "external", - path: [spec.value], - ...(semanticRequireOriginCandidate(node, this.state)?.origin - .loaderAuthority === "candidate" || - value.loaderAuthority === "candidate" - ? { loaderAuthority: "candidate" as const } - : {}), - } - : symbolicUnknown("dynamic require"); + if (spec?.kind === "literal" && typeof spec.value === "string") { + if (ELECTRON_MODULE.test(spec.value)) this.reachedElectron = true; + result = { + kind: "external", + path: [spec.value], + ...(semanticRequireOriginCandidate(node, this.state)?.origin + .loaderAuthority === "candidate" || + value.loaderAuthority === "candidate" + ? { loaderAuthority: "candidate" as const } + : {}), + }; + } else { + this.unresolvedRequire = true; + result = symbolicUnknown("dynamic require"); + } } else if ( value.kind !== "function" || this.#activeFunctions.has(value.node) @@ -1538,12 +2187,103 @@ export class JavaScriptSymbolicBindings { initializer.node.name, ); if (!source) continue; + if (!this.#sharesInitialAliases(source, initializer.node)) continue; const aliases = this.#aliasesByBinding.get(source.bindingId) ?? []; aliases.push(binding.bindingId); this.#aliasesByBinding.set(source.bindingId, aliases); + const sources = this.#aliasSourcesByBinding.get(binding.bindingId) ?? []; + sources.push(source.bindingId); + this.#aliasSourcesByBinding.set(binding.bindingId, sources); } } + #sharesInitialAliases( + binding: JavaScriptSemanticBindingState, + read: t.Node, + ): boolean { + if (!binding.definitions.some(({ kind }) => kind === "assignment")) + return true; + const selected = semanticBindingAtRead( + binding, + read, + this.state.parentsByNode, + ); + // A definite fresh replacement has its own object identity. Captures of + // the original value and uncertain rebindings retain their shared writes. + return ( + selected.status !== "ordered" || + !selected.hasDefiniteInitializer || + selected.binding.initializers.length === 0 || + selected.binding.initializers.some( + (initializer) => + initializer === binding.initializers[0] || + initializer.projection.length > 0 || + !this.#isFreshValue(initializer.node), + ) + ); + } + + /** A fresh allocation, inline or held by a binding that only ever received one. */ + #isFreshValue(node: t.Node): boolean { + if (isFreshObject(node)) return true; + if (!t.isIdentifier(node)) return false; + const binding = resolveSemanticBindingState(this.state, node, node.name); + const initializer = semanticStableInitializer(binding, this.state); + return ( + binding !== undefined && + binding.referenceInitializers.length === 0 && + initializer !== undefined && + initializer.entryBody === undefined && + initializer.projection.length === 0 && + isFreshObject(initializer.node) + ); + } + + #bindingAliasGroup(bindingId: string): ReadonlySet { + const cached = this.#bindingAliasGroups.get(bindingId); + if (cached !== undefined) return cached; + const group = new Set(); + const pending = [bindingId]; + while (pending.length > 0) { + const current = pending.pop()!; + if (group.has(current)) continue; + group.add(current); + for (const alias of this.#aliasesByBinding.get(current) ?? []) + pending.push(alias); + for (const source of this.#aliasSourcesByBinding.get(current) ?? []) + pending.push(source); + } + for (const member of group) this.#bindingAliasGroups.set(member, group); + return group; + } + + #bindingHasWrites(bindingId: string): boolean { + if ((this.#writes.get(bindingId)?.length ?? 0) > 0) return true; + const group = this.#bindingAliasGroups.get(bindingId); + return ( + group !== undefined && + (this.#aliasMemberWrites.get(group)?.length ?? 0) > 0 + ); + } + + #bindingWrites(bindingId: string): readonly PropertyWrite[] { + const own = this.#writes.get(bindingId) ?? []; + const group = this.#bindingAliasGroups.get(bindingId); + const shared = + group === undefined ? [] : (this.#aliasMemberWrites.get(group) ?? []); + if (own.length === 0) return shared; + if (shared.length === 0) return own; + return [...own, ...shared].sort( + (left, right) => (left.site.start ?? -1) - (right.site.start ?? -1), + ); + } + + #bindingMutated(bindingId: string): boolean { + if (this.#mutated.has(bindingId)) return true; + const group = this.#bindingAliasGroups.get(bindingId); + return group !== undefined && this.#mutatedAliasGroups.has(group); + } + #invalidateBootstrapTable( tableAliases: ReadonlySet, modules: Map, @@ -1551,11 +2291,11 @@ export class JavaScriptSymbolicBindings { let invalidateAll = false; const invalidSlots = new Set(); for (const bindingId of tableAliases) { - if (this.#mutated.has(bindingId)) invalidateAll = true; + if (this.#bindingMutated(bindingId)) invalidateAll = true; for (const definition of this.state.bindingsById.get(bindingId) ?.definitions ?? []) if (definition.kind === "assignment") invalidateAll = true; - for (const write of this.#writes.get(bindingId) ?? []) { + for (const write of this.#bindingWrites(bindingId)) { if (write.path.length === 0) invalidateAll = true; else invalidSlots.add(write.path[0]!); } @@ -1592,10 +2332,14 @@ export class JavaScriptSymbolicBindings { } if (dynamic && t.isIdentifier(root)) { const binding = resolveSemanticBindingState(this.state, root, root.name); - if (binding) - this.#mutated.add( - this.#exportAliases.get(binding.bindingId) ?? binding.bindingId, - ); + if (binding) { + const alias = this.#exportAliases.get(binding.bindingId); + if (alias === undefined && this.#sharesInitialAliases(binding, root)) + this.#mutatedAliasGroups.add( + this.#bindingAliasGroup(binding.bindingId), + ); + else this.#mutated.add(alias ?? binding.bindingId); + } return; } if (t.isIdentifier(root)) this.#addWrite(root, path, value, site); @@ -1620,7 +2364,6 @@ export class JavaScriptSymbolicBindings { } if (this.#moduleBindings.has(binding.bindingId) && path[0] !== "exports") return; - const key = alias ?? binding.bindingId; const selected = this.#moduleBindings.has(binding.bindingId) ? path.slice(1) : path; @@ -1640,20 +2383,19 @@ export class JavaScriptSymbolicBindings { t.isSwitchCase(parent) || t.isTryStatement(parent) ) { - if (selected.length === 0) this.#mutated.add(key); - else this.#unsafeExports.add(`${key}:${selected[0] ?? ""}`); + if (selected.length === 0) this.#mutated.add(alias); + else this.#unsafeExports.add(`${alias}:${selected[0] ?? ""}`); return; } } } - const writes = this.#writes.get(key) ?? []; const exportValueReceiver = alias !== undefined && selected.length === 0 && t.isAssignmentExpression(value, { operator: "=" }) ? this.#exportValueReceiver(value.right, alias) : undefined; - writes.push({ + const write = { path: selected, value, site, @@ -1665,7 +2407,21 @@ export class JavaScriptSymbolicBindings { : ("exports" as const), }), ...(exportValueReceiver === undefined ? {} : { exportValueReceiver }), - }); + }; + if ( + alias === undefined && + selected.length > 0 && + this.#sharesInitialAliases(binding, root) + ) { + const group = this.#bindingAliasGroup(binding.bindingId); + const writes = this.#aliasMemberWrites.get(group) ?? []; + writes.push(write); + this.#aliasMemberWrites.set(group, writes); + return; + } + const key = alias ?? binding.bindingId; + const writes = this.#writes.get(key) ?? []; + writes.push(write); this.#writes.set(key, writes); } } diff --git a/tests/boundary/cli/javascriptApplicationCoverage.test.ts b/tests/boundary/cli/javascriptApplicationCoverage.test.ts new file mode 100644 index 0000000000..a441e9013a --- /dev/null +++ b/tests/boundary/cli/javascriptApplicationCoverage.test.ts @@ -0,0 +1,92 @@ +import { expect } from "vitest"; + +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; +import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js"; + +cliTest( + "reports complete coverage for a parsed module without Electron identity whose hoisted read precedes its initializer", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-js-coverage-hoisted-read-"); + await writeFixtureFiles(root, { + "index.js": [ + "function schedule(callback) {", + " return scheduleTimeout(callback, 0);", + "}", + 'var scheduleTimeout = "function" === typeof setTimeout ? setTimeout : void 0;', + "module.exports = { schedule };", + "", + ].join("\n"), + }); + const application = await analyzeJavaScriptDirectory("CLI", cli, root); + + expect(application.statistics).toMatchObject({ + parsed_javascript_files: 1, + parse_failures: 0, + }); + const source = application.graph.nodes + .flatMap((node) => node.observations) + .find((observation) => observation.properties.file_kind === "javascript"); + expect(source?.properties.parse_status).toBe("complete"); + expect(application.graph.coverage).toEqual({ + status: "complete", + truncated: false, + omitted_count: 0, + limits: [], + }); + }, +); + +for (const surface of ["CLI", "stdio MCP"] as const) { + cliTest( + `retains inherited loader uncertainty through ${surface}`, + async ({ cli }) => { + const root = await createTestTempDirectory("rea-js-inherited-loader-"); + await writeFixtureFiles(root, { + "index.js": `Object.prototype.load = require; + function register() { load("electron").ipcMain.on("ping", () => {}); } + var load = [].load; + register();`, + }); + const application = await analyzeJavaScriptDirectory(surface, cli, root); + const source = application.graph.nodes + .flatMap((node) => node.observations) + .find( + (observation) => observation.properties.file_kind === "javascript", + ); + + expect(source?.properties.parse_status).toBe("partial"); + expect(application.graph.coverage.status).toBe("partial"); + }, + ); + + cliTest( + `retains bridge paths after alias replacement through ${surface}`, + async ({ cli }) => { + const root = await createTestTempDirectory("rea-js-replaced-alias-"); + await writeFixtureFiles(root, { + "preload.js": `const { contextBridge, ipcRenderer } = require("electron"); + const base = { ping: () => ipcRenderer.send("ping") }; + let alias = base; + alias = {}; + alias.other = 1; + contextBridge.exposeInMainWorld("api", base);`, + }); + const application = await analyzeJavaScriptDirectory(surface, cli, root); + const observations = application.graph.nodes.flatMap( + (node) => node.observations, + ); + + expect(observations).toContainEqual( + expect.objectContaining({ + properties: expect.objectContaining({ + entry: "api.ping", + entry_kind: "bridge-member", + step_kind: "terminal", + }), + }), + ); + }, + ); +} diff --git a/tests/boundary/mcp/agentKnownAnswerMcp.test.ts b/tests/boundary/mcp/agentKnownAnswerMcp.test.ts index 7bf2e32601..b56e650270 100644 --- a/tests/boundary/mcp/agentKnownAnswerMcp.test.ts +++ b/tests/boundary/mcp/agentKnownAnswerMcp.test.ts @@ -384,7 +384,7 @@ const assertNotesAnswerFailures = ( patch: { value: { paths: ["csv.js", "main.js", "preload.js", "renderer.js"], - coverage: "complete-within-source", + coverage: "partial", }, }, failure: "value_mismatch", diff --git a/tests/evaluation/model-evals/AgentEvaluationFixtures.ts b/tests/evaluation/model-evals/AgentEvaluationFixtures.ts index dfa6adb02e..94e8c48ad9 100644 --- a/tests/evaluation/model-evals/AgentEvaluationFixtures.ts +++ b/tests/evaluation/model-evals/AgentEvaluationFixtures.ts @@ -265,9 +265,9 @@ const notesClaims = ( id: "feature_modules", expectedValue: { paths: ["csv.js", "main.js", "preload.js", "renderer.js"], - // The renderer's exportNotes().catch(...) leaves a Promise member - // unresolved in static analysis, so the graph reports partial coverage. - coverage: "partial", + // The renderer's unresolved Promise member cannot hide an Electron + // call path, so it does not make this feature trace partial. + coverage: "complete-within-source", }, source: { ...derived,