Repository navigation
Conversation
…s not compiled can name
The flow analysis proves that a module variable is stored before every
read from the order in which the modules begin. collectModules() took
an entry point for a module that a name made at run time loads only
where the program itself loads files by name.
But the run time lets code that the build has not compiled name every
file that the host asks for: an entry point, and where names find files,
a file marked IsFoundByName or MayBeEvicted
(Modules::refusesRequestFromHost()). Such code is the text of eval or of
a Function constructor, and a file that the host runs under
admit=FilesRunByHost. Its import() begins one of those files when the
waiting jobs run, and a require() that it makes for itself through
process.getBuiltinModule("module") begins it at once. With two entry
points, the second one then runs while a module that the first one has
begun still runs, and reads a variable that the analysis took for
stored. The compiled read answered the value of a store that had not
run, or read a slot that held nothing and the program ended with a
segmentation fault.
modulesThatUnseenCodeNames() is that set, written once beside
modulesThatHostAsksFor(). A realm begins with one of those files. Where
one of them alone runs code or loads a file, that one has begun before
such code runs, and is left out: a file of data and a file whose body
only returns do not count. collectModules() reads the set where
unseenCodeCanRun(). It counts the files that a name finds also where the
host runs files outside the image, as the run time does.
Tests: 16 new files under tests/valueflow/results, of which 9 fail
without the change, and the native tests
module-programs/entry-point-that-text-loads-while-module-it-imports-runs,
module-programs/entry-point-that-file-of-host-loads-while-module-it-imports-runs
and module-programs/entry-point-that-text-loads-and-runs-with-waiting-jobs.
…nother file asks whether the variable is stored CopyClosureFactories gives a call in the body of a module a copy of the function that a `var` of an imported file holds, where the order of the loader says that the file has stored the variable. The pass runs before the flow analysis. A name that the build does not follow may begin the importer before that store: the variable answers undefined then, and the call throws a TypeError. The copy was called instead. The call now asks. In front of the call of the copy, the callee is compared with undefined. Where it is undefined, the call runs as the program writes it, and throws. Where flow proves the store, the comparison folds and the function is compiled as before. Tests: copy-closure-factories-held-by-a-variable-of-a-file-that-is-imported.ir pins the comparison. Of two new pipeline tests, ...-of-a-file-that-flow-proves-stored.ir has no comparison left, and ...-of-a-file-where-code-comes-in-at-any-place.ir keeps it. The native tests are module-programs/closure-factory-of-module-that-runs-while-text-loads-its-importer and module-programs/closure-factory-that-text-calls-for-with-waiting-jobs.
Status
|
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Unseen-code module discovery Source/JavaScriptCore/aot/ir/AOTIRProgram.*, Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsOrder.cpp, Source/JavaScriptCore/aot/valueflow/AOTValueFlowRulesHost.cpp, Source/JavaScriptCore/aot/tests/valueflow/results/a-file-that-the-host-runs-* |
The program identifies modules that unseen code may name. Value-flow result collection marks those modules as loaded by name. Fixtures cover host-run files, name-based discovery, and evictable files. |
Value flow with evaluated text Source/JavaScriptCore/aot/tests/valueflow/results/evaluated-text-* |
Added IR fixtures for evaluated text across entry points, including CommonJS and files found by name. The fixtures check module-variable initialization and report evaluated text as unseen code. |
Value flow across entry points Source/JavaScriptCore/aot/tests/valueflow/results/two-entry-points-* |
Added IR fixtures for module reads and stores across multiple entry points without evaluated text. |
Fallback for copied closure-factory calls Source/JavaScriptCore/aot/passes/AOTCopyClosureFactories.cpp, Source/JavaScriptCore/aot/tests/passes/copy-closure-factories-held-by-a-variable-of-a-file-*, Source/JavaScriptCore/aot/tests/passes/copy-closure-factories-a-helper-that-constructs-a-variable-of-a-file-that-is-imported.ir |
The pass checks imported callees that may still be undefined and preserves an as-written call path. The IR tests cover imported bindings, flow-proven stored bindings, and construction using an imported variable. |
Imports during module loading Source/JavaScriptCore/aot/tests/AOTModuleProgramTests.cpp |
Added module-program tests for dynamic imports, host-loaded files, waiting jobs, and reads of exports before initialization. The compile-time check now accepts one or more entry modules. |
Suggested reviewers: jarred-sumner
Merge Risk: 
Minimal · up to 6c6d8
-
No actionable merge-blocking risk was identified.
Pre-merge checks: 
3 · 
1
-
Failed checks: 1 warningCheck name Status Explanation Resolution Description check The description follows the WebKit-style structure of a bug-linked change with an explanation. It has Problem, Fix, Background, Downsides, and Notes sections that explain the root cause, the fix, the … Add the Bugzilla bug title and bug URL (https://bugs.webkit.org/show_bug.cgi?id=#####) at the top, following the template. Add the 'Reviewed by' line (use NOBODY (OOPS!) only as a placeholder until a reviewer is assigned). Add the list of c… Passed checks: 3 passed-
Full details: Title check
-
Explanation
The title names the main change: uncompiled code that begins a file by name is now counted in the module order (collectModules / modulesThatUnseenCodeNames) and in closure-factory copies. The description and objectives confirm this is the core of the PR, which fixes order proofs and adds undefined guards for copied var-held functions. The title is specific and matches the changeset, though it uses some internal jargon.
-
Full details: Description check
-
Explanation
The description follows the WebKit-style structure of a bug-linked change with an explanation. It has Problem, Fix, Background, Downsides, and Notes sections that explain the root cause, the fix, the test coverage, and the measured performance tradeoffs. It does not include the template's bug link (no Bugzilla URL or bug title), the 'Reviewed by' line, or the per-file ChangeLog-style list of changed paths. These are required by the repository template, so the description is missing key template elements, but the explanation of what changed and why is thorough.
Resolution
Add the Bugzilla bug title and bug URL (https://bugs.webkit.org/show_bug.cgi?id=#####) at the top, following the template. Add the 'Reviewed by' line (use NOBODY (OOPS!) only as a placeholder until a reviewer is assigned). Add the list of changed paths with the changed functions, for example 'Source/JavaScriptCore/aot/ir/AOTIRProgram.cpp: (modulesThatUnseenCodeNames)' and 'Source/JavaScriptCore/aot/passes/AOTCopyClosureFactories.cpp'. Move the 'Problem' and 'Fix' content into a commit-message-style explanation under the bug link, so the description follows the required template.
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
There was a problem hiding this comment.
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 two things in the new code and ruled them out: the two IRBuilder/FunctionAnalyses passes over the same caller in copyForCalls (AOTCopyClosureFactories.cpp) keep their ValueIDs valid because insertUntypedBefore only queues placement and compactFunction runs after both passes; and modulesThatUnseenCodeNames (AOTIRProgram.cpp) only dereferences begunFirst under numberThatRealmsBeginWith == 1, so a set with zero code-running modules is safe, while admitsAnyCode naming every module errs on the conservative side.
Extended reasoning...
The change touches the JavaScriptCore AOT compiler: a new program query in AOTIRProgram.{h,cpp}, a module-order widening in AOTValueFlowResultsOrder.cpp, and a two-pass callee test in the CopyClosureFactories pass, plus 18 new .ir expectation tests and native tests. It touches no auth, crypto, or injection surface. A confirmed inline finding exists and the pass change rewrites IR across two builder passes, so a human look is warranted rather than approval.
…`var`-held function that it is handed is stored
`inherits(Derived, Base)`, with
`function inherits(Below, Above) { Below.prototype = new Above() }`, gets
a copy of the helper in which a copy of Base makes the prototype object.
Where Base is a `var` of another file, the order of the loader said that
the variable is stored. A name that the build does not follow may begin
the importer before that store. The copy then read `prototype` of
undefined in front of the construction, and threw another TypeError
than `new Above()` does.
The construction in the copy now asks, as the call of a copied callee
does: where Above is undefined, `new Above()` runs as it is written.
Tests: copy-closure-factories-a-helper-that-constructs-a-variable-of-a-file-that-is-imported.ir
and the native test
module-programs/helper-that-constructs-variable-of-module-that-runs-while-text-loads-its-importer.
|
Preview build of 6c6d87d: |
There was a problem hiding this comment.
I reviewed the updated PR (through 6c6d87d) and found no bugs; the construction-in-the-copy case from my earlier inline comment is now tested the same way as the callee, with an IR test pinning it. Because this changes how flow's module-order proofs and CopyClosureFactories decide what is stored, a human look from the JSC/AOT side would still be worthwhile.
What was reviewed:
modulesThatUnseenCodeNames: the single-begun-module exclusion and the data/empty-body filter; a released body (hasBody()true, no blocks) falls through to "runs code", so noblocks.first()on an empty vector.- The second builder pass in
copyForCalls: one builder issuing several expansions thenremoveTypesOfValues/compactFunctionmatches the existingcallCopyWhereCalleeIsExpectedpath; the Undefined placeholder is inserted before the copy's closure so the test stands in front of everything the copy needs. - Unified-sources naming:
runsCodeOrLoadsFileandCallAsWrittenare unique across aot/.
Extended reasoning...
The change touches the AOT compiler's module-order inputs (AOTValueFlowResultsOrder.cpp, AOTIRProgram.{h,cpp}) and the CopyClosureFactories pass, adding a runtime StrictEq-against-undefined guard in front of calls of copies whose callee is a var of another file, plus 16 flow result tests, 3 pass tests and 6 native tests. It touches no auth, crypto or injection surface. The third commit addressed the one concern raised on the earlier push and the hunt ran dry with no findings, but the code is a non-trivial compiler semantics change under /Source/JavaScriptCore, which CODEOWNERS assigns to jsc-reviewers, so a human look is still appropriate rather than approval.
|
CI on the previous head (
I moved this pull request to draft until that job is explained. The failing test names from that job (or the |
|
Closing: #997 is merged. It replaces the middle and the back end of the ahead-of-time compiler, so this no longer applies to |
Problem
bun build --compile --aotexecutable with two entry points and onenew Functioncan end withpanic(main thread): Segmentation fault. Or it reads a module variable before its store.collectModules(),aot/valueflow/AOTValueFlowResultsOrder.cpp). Uncompiled code can begin a second entry point by name while an imported module still runs.CopyClosureFactorieshas the same gap: it calls a copy of avar-held function that is stillundefined. Plain Bun throws a TypeError.Fix
modulesThatUnseenCodeNames()(aot/ir/AOTIRProgram.cpp) holds the files that the run time lets such code name.collectModules()reads it whereunseenCodeCanRun().var-held function of another file is called or constructed behind a comparison withundefined. Flow folds it where it proves the store.testaot.Background
evalorFunction, or a file that the host runs from disk.Modules::refusesRequestFromHostlets it name an entry point and a file markedIsFoundByNameorMayBeEvicted.AOTCirclesOfModuleLoads.h: that pass runs before flow and sees no text, so text-free programs lost copies.Downsides
Notes
Where this comes from
No user reported it. I found it while I worked on the case that #961 does not reach (#988, on top of this one, holds that change). I wrote every program below.
The branch is three commits on
0ef2dfd320. It does not need #961.The crash program
bun build --compile --aot first.mjs main.mjs drains.mjs --outfile app && ./appbegins: helpers/runs: drains function/ends: helpers/ends: first/import of main: rejected, undefined is not an object (evaluating 'config.port')begins: helpers/runs: drains function/panic(main thread): Segmentation fault at address 0x583000000018, exit 139createRequire)main reads port: 8080 name: service-0/import of main: fulfilledPlain Bun runs the steps of the loader inside the
require(), somain.mjsruns whilehelpers.mjsruns. Node runs it later. The build follows plain Bun.RequireSitesBoundis 0 in this program before and after: no boundrequire()is involved.With
export var mode = "ready"inhelpers.mjsandconsole.log("main reads mode:", mode, typeof mode)inmain.mjs, the executable printedmain reads mode: ready stringbefore the store had run. Plain Bun and the executable with the change printundefined undefined. A build with--aot-validatenames the crash (AOT validation: ValueType f4689 v2 the empty value, exit 134) and is silent for the string.Other ways to the same fault, each with a test
require()for itself:process.getBuiltinModule("module").createRequire(url)("./second.mjs"). Noimport()and norequire()of the program is involved. Plain Bun printstext require threw: ReferenceError Cannot access 'value' before initialization.The executable printedsecond reads 8080.bun build --compile --aot main.mjswheremain.mjsstartsnew Worker(new URL("./worker.mjs", import.meta.url)). The front end makes the Worker file an entry point. The Worker realm begins withworker.mjs, and text there namesmain.mjs.bun test --aot --isolatewithBUN_AOT_TESTS_NAME_WHAT_THEY_USE=1builds withadmit=FilesRunByHost. A test file from disk imports an entry point from a callback. Before:panic(main thread): Segmentation fault,worker crashed: SIGSEGV. After: the test passes with plain Bun's lines.require()of the program begins the file that holds the variable.The set
modulesThatUnseenCodeNames(program, findsFilesByName, admitsAnyCode)stands besidemodulesThatHostAsksFor():IsFoundByNameorMayBeEvicted.admit=Inspector).export type ..) do not count. An entry point that has no code but imports a file counts.collectModules()passesfilesOfProgramAreLoadedByName(solution) || refusedCode.hostRunsFilesOutsideImageforfindsFilesByName, asFlowResults::findsFilesOfProgramByName()does. #988 reads the same set incallModules, so the two cannot disagree.The comment above
handLoadableModulesToText()and the header ofevaluated-text-loads-no-file-of-the-program.irsaid that text has norequire()of a file. Both now say what the run time does.Closure factories
export var once = run => { .. }inhelpers.js,const a = once(loadA)in the body ofmain.js: the pass gives the call a copy ofonce. It did so whereisStoredBeforeBodyStarts()said thathelpers.jshas run to its end. That still decides where a copy is made. The call now has this in front of it:The taken block calls the constant
undefined, so flow sees no call of the original function there. BehindApplyFlowResultsthe comparison isfalsewhere the store is proven, and behindCallModulesthe block is gone. Underadmit=Inspectornothing is proven and the comparison stays.The third commit does the same for a function that the call hands in.
inherits(Derived, Base)withfunction inherits(Below, Above) { Below.prototype = new Above() }gets a copy of the helper in which a copy ofBasemakes the prototype object. WhereBaseis avarof another file and is not stored yet, that copy readprototypeofundefinedand threwundefined is not an object (evaluating 'Above.prototype'). The construction in the copy now has the comparison in front of it, andnew Above()runs as it is written:undefined is not a constructor (evaluating 'new Above'), as in plain Bun.A test with
IsClosureOfwas the other form. On its other path the call as written keeps the variable as its callee, so flow would count the original function as a callee there. I did not build that form.Tests
Engine, release with assertions, x64, on
0ef2dfd320:testaot run aot/tests: 5864 passed, 0 failed, 42 todo. Without the two.cppedits: 5852 passed, 12 failed.testaot native: 3071 passed.module-programs/: 14 passed. Without the edits: 8 passed, 6 failed.tests/valueflow/results: 16 files (9 fail without the change, 7 are twins that keep the proof).tests/passes: 2 pipeline tests for the comparison and 1 pass test for the helper.copy-closure-factories-held-by-a-variable-of-a-file-that-is-imported.irnow pins the comparison.Bun level, on
0ef2dfd320with this PR and #988, under oven-sh/bun#47605 (22621ef913, which pins the #982 preview):test/aot/parallel/modules, run bytest/aot/run-parallel.ts: 11 pass and 1 is marked// Todo:(the order that [JSC] AOT: do not bind a require() of an ES module that uncompiled code can import #988 names). Without the two PRs 10 fail, one of them by SIGSEGV.require(), two Worker programs and the closure-factory program.test/aot/tools/same-output.ts(machine code, function by function), engine0ef2dfd320against both PRs: IDENTICAL for 289 programs ofperf/idiomsand 3,126 programs ofclosure-factories,modules,closed-worldandcommonjs.Bun level, earlier runs on the engine at
115632d802(#972) with #961 and the same source patches, under oven-sh/bun#47605 at64ddaea8aa:test/aotfiles with "test-run" in the name: 73 files, 538 tests. Run four at once, 1 file failed before and 2 other files failed after. Each of the three passes alone with both builds.test/aotfiles that setBUN_AOT_TESTS_NAME_WHAT_THEY_USE: 132 pass with both builds.Measurements
On the engine at
115632d802with #961, before the third commit, unless a line says another base.--explain=statisticsover 1,219 programs oftest/aot/parallel(all that namerequire, all 85 with more than one entry point, all ofclosure-factories/): 1 program changed a statistic,closed-world/evaluated-text-imports-an-entry-point(two entry points and a text, +1 call site, +8 bytes of shared code). No program with one entry point changed.CopyClosureFactoriesChangesis 20 before and after.test/aot/perf/compare-builds.tson the seven servers oftest/aot/servers/app(56 rows, one request of each route), before against both PRs: 0 slower, 0 faster.export var memo = ..called twice in the body of the importer, 1,000,000 turns of the returned closure: 13.0 instructions a turn with one entry point, with two, and with two and a text, before and after. The copy is kept in all of them. With the text the program runs 37,926 more instructions once (22,171,891 to 22,209,817).copy-closure-factories-held-by-a-variable-of-a-file-that-is-imported.ir: one byte differs, a count that goes from 48 to 49.node:worker_threads, a Worker file, onenew Function(process.env.TEMPLATE ?? ..), a shared ES module and a CommonJS file. Instructions per turn, before to after:export var limit = 10varthat only the run knowsexport const KC = 7exports.LEVELof the CommonJS fileexports.helper(i)bump()(++counter)new Registry().idMain already withdraws the same proofs where the program has one compiled
require(name)orimport(name), also with one entry point.bunbinary: 221,017,864 to 221,022,496 bytes.Open
The self-review raised 42 concerns. These are open, and #988 names the fifth (a debug assertion in the run time):
isStoredBeforeBodyStarts(),lieOnOneCircleOfLoads()andAOTCirclesOfModuleLoads.h. They no longer carry correctness. Without them, calls in files on a cycle and in files loaded byrequire()get copies too, which changes the code of programs that this PR leaves identical.markModulesEvaluatedByRequire()inAOTCoroutines.cpp, which has the rule in another form (found.bitCount() > 1) behind another gate.3toundefined|3). The rowexports.helper(i)above measures it.evaluated-text-loads-a-second-entry-point-that-reads-a-var.irpins thevar.Known and not repaired here: the module order gives up every proof below such a file, where the text can run at few places. A finer begin time is a change of its own.