Skip to content

[JSC] AOT: a host keeps what its stores replace in compiled code, and where - #1050

Closed
robobun wants to merge 2 commits into
mainfrom
robobun/6e8f4bfe/read-export-before-mock-replaces-it
Closed

robobun wants to merge 2 commits into
mainfrom
robobun/6e8f4bfe/read-export-before-mock-replaces-it

Conversation

@robobun

@robobun robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Superseded by #1059. Its commits 6 and 8 fix this leak by name, on the load that the record is bound to. This draft stays as the variant that keeps the place. What it does not cover is in #1059 (comment).

Problem

Fix

  • First commit, no behaviour change: the walk of Modules::tryReplaceExport() is a template that stores or answers the place.
  • keepCompiledExportValue() writes the value of the place into a table of the host, with the place. A name that the store refuses throws here.
  • AOT::storeIntoKeptPlace(), readKeptPlace() and putBackKeptExport() work on that place and resolve no name. hasCompiledExportLeftKeptPlace() tells a reader that the export leads elsewhere now.
  • Verified: native tests kept-exports/* (5 new, all 2,183 pass). The Bun half is oven-sh/bun@6c39792332b, a branch with no pull request.

Background

  • A file runs from source when its cache entry was deleted or a plugin gave its text. Its re-exports resolve to compiled files.
  • A module of a test run has one instance per load.
  • Considered a put-back by name (the first version here): it stored functions of one load into the next.

Downsides

  • AOT::mockExport takes 2,329 instructions for the fifth export, before and after. A spy store takes 2 more (2,426 to 2,428, x64 release).
  • .text of Bun grows by 11,776 bytes with both halves.
  • export * as ns in such a file is still refused.
Notes

Repro. Four files, bun test --parallel=1 --no-isolate --aot, on canary 1.4.5-canary.1+9098897a1:

// dep.ts
export const greet = () => "real";
// barrel.ts
export * from "./dep";
// a.test.ts
import { mock, test, vi } from "bun:test";
import { join } from "node:path";
import { greet as direct } from "./dep";
test("a", async () => {
  const path = join(import.meta.dir, "bar" + "rel.ts");
  await Bun.write(path, 'export * from "./dep";\n');
  const barrel = await import(path);
  mock.module(path, () => ({ greet: () => "barrel" }));
  vi.doUnmock(path);
  console.log("2", barrel.greet(), direct());
});
// b.test.ts
import { test } from "bun:test";
import { greet } from "./dep";
test("b", () => console.log("3", greet()));

Plain prints 2 real real and 3 real. With --aot it prints 2 barrel barrel and 3 barrel.

The place under a private name. The table belongs to the mock in Bun, and Bun moves it from one mock to the next. A record beside it would have to move at the same sites. A record in the engine would need the lifetime of the mock.

A later keep. A name that leads to another place at a later keep gets its old place put back first, and the table keeps the new place.

Tables of exports. A table that went with the run of its module is left alone: what a store left in it went with it. A table that is set aside while a mock stands in place of its module (capturedExportsBeforeMockInPlace) is written where it is, with the namespace and the variable kept beside it. A store into a kept place is not kept a second time by the bracket of a mock or by a mock of a library module over the same table.

No value yet. The table stands for an export that has no value yet. readKeptPlace() reads the place again once the body has run. A variable does not go back to no value. A table entry that its source had yet to give gets that value at the put-back, as restoreCapturedExports() does.

A reader. What the table keeps under a name is the value of its place. After the module that holds the export was loaded again, the export leads to the new load. hasCompiledExportLeftKeptPlace() walks from the name as the keep does and compares the place. A table that is set aside for a mock in place of its module is still the table of the export. One that went with the run of its module is not.

Measured (x64, release, no LTO, Bun at the head of oven-sh/bun#48926, this branch against 260715a):

  • AOT::mockExport, a mock of a compiled file: 8,080 to 8,081 instructions for the first export, 2,329 to 2,329 for the fifth.
  • AOT::tryReplaceExport(JSCell*), a spy on a compiled namespace: 3,685 to 3,687 for the store, 2,426 to 2,428 for the restore. The two are in the walk (105 to 107 of its own).
  • overrideExportValue(): 790 bytes before and after. A store into a JS module variable takes 495 instructions, 118 of its own, before and after.
  • The walk: 5,519 bytes in four functions before. After, the form that stores is 5,535 bytes in three, and the form that finds is 4,430.
  • .text of Bun 121,279,008 to 121,290,784 bytes with both halves.
  • A mock and its end over 100 exports of a barrel that runs from source: resolveExport() 200 calls before and after, overrideExportValue() 100 to 0, 100 walks before and after, 200 stores into a remembered place.

Hardened lanes. A hardened build excludes code outside the image, so a host reads no export there that the program does not let it replace. The native tests read only such exports through the namespace. They also pass with exclude=CodeOutsideImage.

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Preview build of 0a3726b: autobuild-preview-pr-1050-0a3726b1

@robobun
robobun force-pushed the robobun/6e8f4bfe/read-export-before-mock-replaces-it branch from 1488ede to 4a4b3f0 Compare November 14, 2026 10:26
@robobun robobun changed the title [JSC] AOT: a host can read the export that a store would replace [JSC] AOT: a host keeps what its stores replace in compiled code, and where Nov 14, 2026
@robobun
robobun force-pushed the robobun/6e8f4bfe/read-export-before-mock-replaces-it branch 3 times, most recently from bd7e05d to 7fca096 Compare November 14, 2026 15:22
…e and for a find

Modules::tryReplaceExport() walks from a name to the place that holds the
value of an export, and stores there. The walk is a template over what is
done with the place at its end:

- StoreIntoPlace stores the value, and keeps what the bracket of a mock of
  a compiled file keeps. tryReplaceExport() and tryReplaceCapturedExport()
  use it, and store what they stored before.
- FindPlace stores nothing and answers a ReplaceablePlace: a variable of
  the program, a field of a module instance, an entry of a table of
  captured exports or of exports that a mock added, or a namespace that a
  file hands on. findPlaceToReplaceExport() uses it.

tryReplaceExportAddedByMock() is a part of the walk now.

No behaviour changes. AOT::mockExport takes 2,329 instructions for the
fifth export of a mock of a compiled file, as before (x64, release).
… where

JSModuleNamespaceObject::overrideExportValue() stores into compiled code for
a module that runs from source and hands on names of compiled files. Outside
the bracket of a mock of a compiled file nothing kept what such a store
replaced. So under `bun test --aot` the end of a module mock of such a
module put nothing back, and the next test file read the mock.

- JSModuleNamespaceObject::keepCompiledExportValue() finds the place that
  overrideExportValue() would store into and writes its value into a table
  that the caller owns, by export name. The table stands for an export that
  has no value yet. Under a private name the table remembers the place.
  Both members go through one classification of the resolved record. Where
  overrideExportValue() would throw, the keep throws, before any store.
- AOT::storeIntoKeptPlace(), AOT::readKeptPlace() and
  AOT::putBackKeptExport() work on the remembered place. They resolve no
  name. So a store and its put-back reach the same module instance, also
  after the module was loaded again.
- JSModuleNamespaceObject::hasCompiledExportLeftKeptPlace() says whether an
  export leads to another place by now. What the table keeps for it is then
  the value of the load before, and a reader takes the export where it is.
- A store into a kept place is not kept a second time by the bracket of a
  mock, or by a mock of a library module over the same table.
- A table of exports that went with the run of its module is left alone. A
  table that is set aside while a mock stands in place of its module is
  written where it is, with the namespace and the variable kept beside it.
- A name that leads to another place at a later keep gets its old place put
  back first.

Tests: the native tests kept-exports/* (a variable, a field of two module
instances, a table of exports, an export that has no value yet).
@robobun

robobun commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator Author

Closing: #1059 has the engine's part of this fix in its commits 6 and 8, by name and on the load that the record is bound to. This draft stays readable as the variant that keeps the place. What it does not cover, and one change that is not in it, are in #1059 (comment).

@robobun robobun closed this Nov 15, 2026
Jarred-Sumner added a commit that referenced this pull request Nov 15, 2026
…f checks (#1059)

Eight fixes of Bun's ahead-of-time compiler (`bun build --compile --aot` and `bun test --aot`, both
experimental). Bun's side is a pull request that pins this one. This follows #1055.

14 files, +204 -30, all in Source/JavaScriptCore/aot/.

- A CommonJS file that throws while import() calls of it are in flight runs again behind them, as Bun
  runs it: a compiled program threw the first run's error. The same where the file calls import() of
  itself and throws, and where several retries at once come behind a required ES module that failed.

- A name that two `export *` give, one of them through a CommonJS file that an earlier test file has
  loaded, is ambiguous in a test run. A compiled test run handed out one of the two.

- With --aot-harden=proof-checks a build stopped where a list has a getter that stores into a list and
  a read that the compiler remembers is made again to be checked, and where a pattern takes every name
  of an object away. A program stopped with a failed type check where closures hold a variable that a
  loop stores ahead of its reads.

- `for (const x of list) switch (x) { case "a": n += x.name; }` ended the build by a signal.

- What a mock of a file that runs from source replaces in compiled code can be read by the host that
  puts it back, such a mock reaches the names of a CommonJS file, and the mock is read, stored and put
  back on the load that the source file's names are bound to.

Known: where a CommonJS file behind such a mock is loaded a second time, the mock can reach the readers
of its first load, and a namespace that was imported from a compiled barrel reads the second load.
#1050 is a draft of the same by the place that the mock wrote.
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.

1 participant