Skip to content

[JSC] AOT: an accessor that unseen code may have put above a module object gets that module object - #994

Closed
robobun wants to merge 1 commit into
mainfrom
robobun/98ec500b/aot-accessor-above-module-object
Closed

robobun wants to merge 1 commit into
mainfrom
robobun/98ec500b/aot-accessor-above-module-object

Conversation

@robobun

@robobun robobun commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • An --aot executable throws TypeError: inner.secret is not a function where plain bun runs. new Function text puts a getter above every CommonJS module object. A compiled file reads that name off its module, and the getter stores into module.exports.
  • HeapRule::callAccessors (aot/valueflow/AOTValueFlowRulesHeapLookups.cpp:1031) hands a receiver to an accessor without a table row only if the receiver is no built-in object. A module object is one.

Fix

  • Where unseen code runs, that arm also hands over the program's module objects (new set whatIsNewToUnseenCode).
  • Verified: 15 new IR tests that name no host entry above module objects. 12 fail on main's engine and pass here. All 5876 IR tests pass.
  • Self-reviewed: 40 concerns raised, 37 addressed. Not taken: a guard for machine code and a fuzz generator (Notes).

Background

Downsides

  • Where text is evaluated, a file that looks up an unlisted key on module loses the proofs about its exports. A spread of module costs the same. On the old pin, 289 benchmarks and 8 servers compile to identical code.
  • Not covered: an N-API addon that lays the getter (same TypeError), a trap above require functions, a redefined id above module objects.
  • No user reported this. On main it hides where the program uses process.env or node:http.
Notes

The program

inner.cjs:

exports.secret = function secret() { return "secret of inner"; };
exports.isHot = () => (module.hot ? "hot" : "cold");

main.cjs:

const inner = require("./inner.cjs");
const report = new Function(`
  const caught = [];
  const Module = process.getBuiltinModule("node:module");
  const above = Object.getPrototypeOf(new Module("made"));
  Object.defineProperty(above, "hot", { configurable: true, get() { caught.push(this); } });
  return () => { for (const m of caught) m.exports.secret = () => "replaced"; return caught.length; };
`)();
inner.isHot();
console.log(report(), inner.secret());

Plain bun prints 1 replaced. The executable throws TypeError: inner.secret is not a function. (In 'inner.secret()', 'inner.secret' is undefined). With BUN_JSC_validateAOTRuntime=1 it aborts with AOT validation: ImpossibleMutation. Indirect eval and vm.runInThisContext give the same result.

What the flow analysis said

valueflow-why escaped-builtin=ModuleObject prints held: those of 0 of 2 modules on main, and the callee of inner.secret() is the one function. With the change it prints 1 of 2, and the callee set holds the unknown value.

Why the guard

Without unseen code, what a lookup finds under an unlisted name above a module object is native code of the host. The host has every module object already. The test a-name-no-row-lists-read-off-a-module-object-hands-it-to-nobody-where-no-text-is-evaluated.ir keeps that. Without the guard, module.hot would hand out the module object in every program.

Why require and import.meta stay out

Text that gets a require function of the program calls it with a name. An executable answers that with the documented error for a dynamic path, and require.main answers undefined where no compiled code names a way. The test a-name-no-row-lists-read-off-a-require-function-hands-it-to-nobody-beside-evaluated-text.ir pins this.

The tests

All 15 are in aot/tests/valueflow/modules. Run with the testaot and jsc that CI built: for main b83d8a1 the 12 print held: those of 0 of 2 modules. For this head the folder gives 147 passed, and all of aot/tests gives 5876 passed, 0 failed, 42 todo.

  • A name: read (four orders of the flow rules), stored, read without text, read off a require function.
  • An index: read, stored, read without text.
  • A key that nobody knows, Reflect.get, Object.assign, Object.prototype.toString (it reads Symbol.toStringTag), and a module object as the scope of a with statement.

The first version of this pull request also gave the stand-in host a row for the object above module objects, and three tests that needed it. They are gone: Bun's CI runs these tests against Bun's table, and that table does not state the link today.

Where main already answers right

On main the forms give the right answer when the program uses the environment object: process.env, Bun.env, or a library module that reads it, such as node:http and node:os. That object has native accessors without rows, and the link above a module object is unstated. With the object in use, valueflow-why escaped-builtin=ModuleObject answers escaped by Lookup "hot" on main. Of 29 programs that fail on Bun main, 26 print plain Bun's line there with void process.env.PORT; added. process.pid, process.versions, node:fs and node:path do not have that effect.

What a copy of module costs

({ ...module }), a rest pattern and Object.entries(module) read own properties only, so they enter no accessor above. The flow analysis looks the keys up through the chain, finds the stand-in, and now hands the module object out where text is evaluated (held: 1 of 2, on main 0 of 2). The answer is safe and less exact. Object.keys(module) hands nothing out.

What stays open

  • An N-API addon that gets Module or new Module() from the program and lays the getter. The executable still throws inner.secret is not a function. Bun's table counts a loader of machine code as no unseen code. mayMachineCodeRun in AOTValueFlowRulesHost.cpp is the predicate that this arm would need. No program of the suites loads an addon and reads an unlisted name off module, so that guard has no measurement.
  • A trap above the require functions.
  • A redefined id, filename, path or loaded on the object above module objects. A module object lists those names itself, so a lookup on one never goes above it. That needs the link in the table. node:module: make Module the class of CommonJS module objects bun#47848 makes Module.prototype the object above module objects, makes those accessors non-configurable and plans the row.
  • Not open on this base: a getter that the program itself lays there under a new name, through module.__proto__ or new Module().__proto__. It is wrong with the engine at 4d45755. The flow analysis of main already answers f3 f5 for the call of the replaced export.

What the self-review changed

The first version of this pull request was based on 4d45755. It had a second rule for index keys, and the stand-in host had a row for the object above module objects.

  • The row is gone, with the three tests that needed it. node:module: make Module the class of CommonJS module objects bun#47848 replaces the object that it stood for.
  • The index rule is gone. Its six tests for an object below a built-in object pass on main without it. The three index tests on a module object stay: with an unstated link they come to the arm.
  • Five tests are new: a key that nobody knows, Reflect.get, Object.assign, Object.prototype.toString and a with scope.
  • Not taken: a guard that also counts machine code (two concerns, see above), and a generator of these forms for Bun's fuzz runner (one concern). The forms are 15 IR tests here and 15 programs in Bun.

Measurements

No Bun consumes WebKit main yet (oven-sh/bun#47605 moves the pin). The numbers on programs are from Bun main 675ee5b97b with the engine at 4d45755, against the same with this change. Release builds without LTO, linux-x64.

  • test/aot/tools/same-output.ts: IDENTICAL for 289 programs of test/aot/perf/idioms and for 8 servers of test/aot/servers/app. All 8 servers raise the flag UnseenCodeRuns.
  • 15 programs where text lays a getter, a setter or a Proxy above the module objects (AOT: programs where evaluated text puts an accessor above the module objects bun#47912): 15 fail before and pass after, also with --aot-validate and the checks of the run time on. Before, each of the 15 stops at an AOT validation line.
  • 1017 existing programs of Bun's test/aot/parallel that use modules, loaders and node:vm: 1012 pass, 4 are todo and 1 fails, before and after.
  • AnalyzeFlow instructions, least of 5 builds: hono 2,986.6 M against 2,993.9 M, express 9,799.2 M against 9,799.4 M. Two readings of one build differ by up to 0.9%.
  • The Bun executable has the same size (136,886,544 bytes).

Order

  1. This change.
  2. Bun pins a build with it. The 15 programs of AOT: programs where evaluated text puts an accessor above the module objects bun#47912 then lose their // Todo: line.
  3. The link above module objects, with node:module: make Module the class of CommonJS module objects bun#47848.

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Preview build of 1a85295: autobuild-preview-pr-994-1a852959

@robobun
robobun force-pushed the robobun/98ec500b/aot-accessor-above-module-object branch 2 times, most recently from c0c9ed1 to 11955b5 Compare November 4, 2026 06:32
…bject gets that module object

Text that a program evaluates runs in the global scope. From there it comes to
the object that a host keeps above every CommonJS module object, and it can lay
an accessor on that object under a name or at an index that nothing locks. A
file that looks the key up on its own `module` enters the accessor with its
module object, and `exports` of that object is what the file exports.

The flow analysis takes an accessor that no row describes for native code of
the host (the stand-in %UndeclaredNativeCode%), and hands it a receiver only if
the receiver is no built-in object. A module object is one, so it stayed in the
program, and so did the layout that compiled code had proven for its exports.

Where unseen code runs, the stand-in arm of HeapRule::callAccessors now passes
the receiver through a second set: what is no built-in object, and the module
objects of the program. A require function and an import.meta are not in it:
neither leads to a module object. Where no unseen code runs the arm is as it
was.

The tests read and store a name, an index and a key that nobody knows on a
module object, and come to the arm through Reflect.get, Object.assign,
Object.prototype.toString and the scope of a with statement. They name no
entry of a host above the module objects, so they hold for a table that states
that link and for one that does not.
@robobun
robobun force-pushed the robobun/98ec500b/aot-accessor-above-module-object branch from 11955b5 to 1a85295 Compare November 4, 2026 10:39
@robobun
robobun marked this pull request as ready for review November 4, 2026 11:46
@robobun

robobun commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

Status

This pull request is rebased on main (b83d8a1) and holds the engine change alone. CI is green on 1a85295, and the Test lanes ran testaot.

How I reproduced it:

# main's engine, CI testaot of b83d8a12ae
testaot run Source/JavaScriptCore/aot/tests/valueflow/modules/a-name-no-row-lists-read-off-a-module-object-hands-it-to-evaluated-text.ir --show-output
#   held: those of 0 of 2 modules
# this head, CI testaot of 1a85295985
#   held: those of 1 of 2 modules

The programs for Bun's suite are in oven-sh/bun#47912.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline finding, I also checked the subspan(1) in the new whatIsNewToUnseenCode construction (AOTValueFlowHeapTables.cpp:745) for an out-of-bounds slice when a program has no module objects — appendObjectsOf always appends the canonical realm object first, so the span is never empty. I also checked that doesUnseenCodeRun() in the swapped arm is the same host-node predicate the rest of this rule file already uses, so the arm re-evaluates when that node flips rather than depending on rule order.

Extended reasoning...

The change is three source lines in the AOT value-flow analysis (a new ValueSetID in the heap-tables macro list, its construction in the ValueFlowHeapTables constructor, and a conditional in HeapRule::callAccessors) plus fifteen .ir tests; it touches no auth, crypto or injection surface. The one posted finding is about the N-API loader path the author documents as deliberately out of scope, which is a scope decision for a maintainer of aot/ to weigh. The concrete candidates I examined (span bounds, predicate ordering) were ruled out from the code.

if (m_context.contains(functions, m_tables.undescribedNativeCode())) {
passToAccessorNobodyKnows(receiver, m_tables.whatIsNewToNativeCode());
// The stand-in is native code of the host, to which no built-in object is new. Where unseen code runs it is also an accessor that such code has laid on an object above the receiver.
passToAccessorNobodyKnows(receiver, doesUnseenCodeRun() ? m_tables.whatIsNewToUnseenCode() : m_tables.whatIsNewToNativeCode());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟣 pre-existing, not blocking: Pre-existing, left open by this partial fix: a compiled program whose N-API addon lays a getter above module objects still throws TypeError: inner.secret is not a function where plain bun runs. The new arm at AOTValueFlowRulesHeapLookups.cpp:1032 hands module objects out only when doesUnseenCodeRun() is true, but a loader of machine code raises no UnseenCodeRuns flag. The sibling rule exposeModuleObjectsOfEntryPoints at AOTValueFlowRulesHost.cpp:1346 already treats machine code as reaching module objects via mayMachineCodeRun. Fix: make the hand-over condition at 1032 cover every way foreign code can reach the object above module objects, i.e. also when mayMachineCodeRun (AOTValueFlowRulesHost.cpp:1333) holds, matching the guard at 1346.
A small fix can ride a push you are already making; otherwise a short reply is enough.

Why this was flagged

A program loads an N-API addon and the addon, calling back into JS, defines a getter under an unlisted name on the object above module objects; a compiled file reads that name off its own module. The getter stores into module.exports. At AOTValueFlowRulesHeapLookups.cpp:1032 the lookup resolves to undescribedNativeCode, but doesUnseenCodeRun() is false because no text is evaluated, so passToAccessorNobodyKnows gets whatIsNewToNativeCode, which excludes every module object. The exported closure stays proven in the program, the executable constant-folds the callee and throws TypeError: inner.secret is not a function while plain bun runs. The base branch fails the same way; the diff fixes only the evaluated-text variant. The sibling guard at AOTValueFlowRulesHost.cpp:1346 already ORs isHostFlagRaised(UnseenCodeRuns) with mayMachineCodeRun(context) for exposing main-module objects, so the codebase treats machine code as able to reach module objects; this arm does not.

Verification: Pre-existing; acknowledged in diff: the code does nothing for machine code, and the base already fails by the same route. AOTValueFlowRulesHeapLookups.cpp:1032 chooses whatIsNewToNativeCode unless doesUnseenCodeRun(), which (AOTValueFlowRulesHeapInternal.h:155) reads only ValueFlowHostNode::UnseenCodeRuns. A loader of machine code never raises that flag (AOTValueFlowRulesHost.cpp:1069-1077).

@coderabbitai

coderabbitai Bot commented Nov 4, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The AOT value-flow accessor rule now uses a value set that includes program module objects when unseen code runs. New CommonJS IR fixtures check module-object access and escape results across evaluated-text, delayed-rule, and non-evaluated-text cases.

Changes

Unseen-code module object flow

Layer / File(s) Summary
Unseen-code accessor value set
Source/JavaScriptCore/aot/valueflow/AOTValueFlowHeapTables.cpp, Source/JavaScriptCore/aot/valueflow/AOTValueFlowHeapTables.h, Source/JavaScriptCore/aot/valueflow/AOTValueFlowRulesHeapLookups.cpp
The heap tables add whatIsNewToUnseenCode, combining the native-code set with module objects starting at index 1. The accessor rule selects this set when unseen code runs.
Module-object access with evaluated text
Source/JavaScriptCore/aot/tests/valueflow/modules/a-key-nobody-knows-*.ir, .../modules/a-module-object-*.ir, .../modules/a-name-no-row-lists-read-off-a-module-object-hands-it-to-evaluated-text.ir, .../modules/a-name-no-row-lists-stored-into-*.ir, .../modules/a-name-read-off-a-module-object-through-reflect-get-*.ir, .../modules/an-index-*-hands-it-to-evaluated-text.ir, .../modules/exports-of-a-commonjs-module-stay-in-the-program-beside-evaluated-text.ir
The fixtures check value-flow output for module-object reads, writes, and calls involving evaluated text. The comments distinguish named main-module access from calling an exported function.
Delayed rules and non-evaluated-text cases
.../modules/a-name-no-row-lists-read-off-a-module-object-hands-it-to-evaluated-text-with-*.ir, .../modules/a-name-no-row-lists-read-off-a-module-object-hands-it-to-nobody-where-no-text-is-evaluated.ir, .../modules/a-name-no-row-lists-read-off-a-require-function-hands-it-to-nobody-beside-evaluated-text.ir, .../modules/an-index-read-off-a-module-object-hands-it-to-nobody-where-no-text-is-evaluated.ir
The fixtures check delayed flow-rule configurations and module-object or ModuleRequire access without evaluated text. Their expected output covers escape attribution and held-module reporting.

Possibly related PRs

  • oven-sh/WebKit#987: Both PRs change AOT assumptions about which modules unseen code can reach.
  • oven-sh/WebKit#979: Both PRs update AOT value-flow handling for unseen code and module objects.
  • oven-sh/WebKit#978: This PR continues the unseen-code flow changes by adding module objects to accessor inputs.
Merge Risk: Minimal · up to 1a852

No actionable merge-blocking issue is established from the available evidence. Merge after normal checks.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly identifies the main change: unseen-code accessors can receive a module object. It is specific and related to the changeset.
Description check Passed The description is detailed and covers the problem, fix, affected behavior, tests, performance results, limitations, and implementation details. It does not include the template's explicit Bugzilla li…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is detailed and covers the problem, fix, affected behavior, tests, performance results, limitations, and implementation details. It does not include the template's explicit Bugzilla link, reviewer line, or changed-file list, but it is mostly complete and directly relevant.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share
Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 65f9c35d-34f3-4f92-af90-b4b5cd6a8c45
Commits

Reviewing files that changed from the base of the PR and between b83d8a1 and 1a85295.

Files selected for processing: 19
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-key-nobody-knows-read-off-a-module-object-hands-it-to-evaluated-text.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-module-object-as-the-scope-of-a-with-statement-hands-it-to-evaluated-text.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-module-object-that-object-assign-stores-into-hands-it-to-evaluated-text.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-module-object-that-object-prototype-to-string-is-called-on-hands-it-to-evaluated-text.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-name-no-row-lists-read-off-a-module-object-hands-it-to-evaluated-text-with-all-flow-rules-delayed.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-name-no-row-lists-read-off-a-module-object-hands-it-to-evaluated-text-with-flow-rules-delayed.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-name-no-row-lists-read-off-a-module-object-hands-it-to-evaluated-text-with-other-flow-rules-delayed.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-name-no-row-lists-read-off-a-module-object-hands-it-to-evaluated-text.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-name-no-row-lists-read-off-a-module-object-hands-it-to-nobody-where-no-text-is-evaluated.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-name-no-row-lists-read-off-a-require-function-hands-it-to-nobody-beside-evaluated-text.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-name-no-row-lists-stored-into-a-module-object-hands-it-to-evaluated-text.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/a-name-read-off-a-module-object-through-reflect-get-hands-it-to-evaluated-text.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/an-index-read-off-a-module-object-hands-it-to-evaluated-text.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/an-index-read-off-a-module-object-hands-it-to-nobody-where-no-text-is-evaluated.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/an-index-stored-into-a-module-object-hands-it-to-evaluated-text.ir
  • Source/JavaScriptCore/aot/tests/valueflow/modules/exports-of-a-commonjs-module-stay-in-the-program-beside-evaluated-text.ir
  • Source/JavaScriptCore/aot/valueflow/AOTValueFlowHeapTables.cpp
  • Source/JavaScriptCore/aot/valueflow/AOTValueFlowHeapTables.h
  • Source/JavaScriptCore/aot/valueflow/AOTValueFlowRulesHeapLookups.cpp

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

Closing: #997 is merged. It replaces the middle and the back end of the ahead-of-time compiler, so this no longer applies to main and would conflict with it. If the problem still shows on main, please open a new pull request against it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants