Repository navigation
Conversation
|
Preview build of 0a3726b: |
robobun
force-pushed
the
robobun/6e8f4bfe/read-export-before-mock-replaces-it
branch
from
November 14, 2026 10:26
1488ede to
4a4b3f0
Compare
robobun
force-pushed
the
robobun/6e8f4bfe/read-export-before-mock-replaces-it
branch
3 times, most recently
from
November 14, 2026 15:22
bd7e05d to
7fca096
Compare
…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
force-pushed
the
robobun/6e8f4bfe/read-export-before-mock-replaces-it
branch
from
November 14, 2026 15:33
7fca096 to
0a3726b
Compare
This was referenced Nov 14, 2026
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). |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
bun test --aot: a module mock of a file that runs from source leaves its values in compiled code. The next test file reads the mock, and all tests pass.overrideExportValue()stores into compiled code for such a file (JSModuleNamespaceObject.cpp:678,:685, since AOT: module instances in test runs, code without types, generators, BigInt, smaller builds #1036). The engine keeps what a store replaces only for a mock of a compiled file (AOTModules.cpp:1471).Fix
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()andputBackKeptExport()work on that place and resolve no name.hasCompiledExportLeftKeptPlace()tells a reader that the export leads elsewhere now.kept-exports/*(5 new, all 2,183 pass). The Bun half is oven-sh/bun@6c39792332b, a branch with no pull request.Background
Downsides
AOT::mockExporttakes 2,329 instructions for the fifth export, before and after. A spy store takes 2 more (2,426 to 2,428, x64 release)..textof Bun grows by 11,776 bytes with both halves.export * as nsin 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:Plain prints
2 real realand3 real. With--aotit prints2 barrel barreland3 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, asrestoreCapturedExports()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..textof Bun 121,279,008 to 121,290,784 bytes with both halves.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.