Repository navigation
AOT: miscompilations and crashes found since #967, builds that never ended, faster loops - #971
Conversation
…ended, faster loops
Wrong answers of compiled programs:
- A list of numbers with one `undefined` in it, iterated by two loops. The first adds
the numbers up, the second counts the `undefined`s: it counted 0 instead of 1.
- In a generator, a value read before a `yield*` and used after it was wiped when a
parameter that held an object was set to `null` or `undefined` in the same loop: 12
instead of 22. A second program never ended.
- A `RegExp` made from text out of a list was compiled from the text the build had
predicted, even when the list was read under two names in one step and then changed.
- `main.mjs` imports `m.mjs`, which imports `x.cjs`, which calls `require()` on `m.mjs`
under a name made at run time: `main.mjs` ran after `x.cjs` had ended, not before the
`require()` returned.
- An `import()` made while other `import()` calls were waiting, and whose graph shares a
module with one of them, was held back behind all of them.
Crashes of compiled programs:
- A segfault in eleven lines: a loop reads and stores a static field that holds an
object, after the field was deleted while a Proxy was on the class's prototype chain
and was defined again through the trap.
- A segfault at address 0 after an out-of-memory error that the program catches: a rope
of 2^30 two-byte characters that cannot be flattened, ahead of a short loop in a `try`
with `catch` and `finally`.
Builds that failed or never ended:
- Thirteen lines did not build in two minutes: two nested loops of eight iterations, a
swap in a `try`, an xor in its `finally`. B3's `Value::returnsBool()` recursed into both
operands without remembering what it had asked, so its cost was exponential.
- One function spent 214 of 246 seconds in type propagation: its queue was last in,
first out.
- The compiler aborted on `for (;;) { if (source.take === never) return ..; const got =
source.take(..); .. }`. A loop that reads the same property of an unchanging object on
every iteration gets a version that asks ahead of the loop, and a type test on that read
was turned into a cast of the copy. Where both sides of the test's branch leave the
loop, the branch still hands on what the test proved, so its condition has to stay a
test.
- Fourteen lines stopped the build with `InvalidGuard`: a read of a typed array at the
top of an iteration relied on the bounds check at the end of the previous one, and the
loop's counter was then replaced by a constant.
- Quadratic time in large functions: three places in Air's register and stack
allocators, two in the ahead-of-time compiler's own passes (one is flow analysis), and the
lookup of a regular expression's symbols (arm64: twice as many alternatives cost 9.5
times as much, now 1.74 times).
`bun test --aot`, following oven-sh/bun#44833:
- An `import` that reaches a mock whose factory threw gets the error that the loader
kept. `require()` of a mocked module returns what the factory returned. A later mock
of a CommonJS file doesn't change what an earlier `require()` returned.
- The engine's own tests no longer depend on how many built-ins Bun has.
Speed:
- New B3 pass, promoteLoopMemory: `a[k] += v[j]` and `o.total += x` reloaded on every
iteration what the previous one had stored. The word is read once ahead of the loop
and carried in a Phi. The stores stay where they are. Off by default; only the ahead-of-time
compiler's policy turns it on.
- `map.has(k)` followed by `map.get(k)` searches once. Keys that are objects are searched
inline in loops.
- A generator function made for each call: 2,585 to 175 instructions. tslib's
`__awaiter`: 3,876 to 1,441 per iteration.
- Helpers that Babel and TypeScript emit: `Reflect.construct` behind tests, a built-in
bound and called in one function, descriptors read and defined again, `for..in` copies.
- Remainders and quotients of whole numbers stay integers. A loop polls with one
faulting load on arm64, macOS x64 and Windows arm64 too.
…orted on
`import { test, vi } from "vitest"; test("t", () => { vi.useFakeTimers(); })` under
`bun test --aot` ended in `abort()`. The code that throws "was not compiled into this
executable" looked the function up in the table of built-ins to ask whether it is an
accessor's getter, and a function that the table leaves out has no record there. It asks
`contains()` first now, so the error is thrown.
…lowed to its call
`bun = require("bun"); copy = {}; copy[key] = bun[key]; copy.plugin(options)`: the build
now sees that `copy.plugin` may be `Bun.plugin`. The test, `plugin-copy-late-mark.ir`,
comes with Bun's half.
…totype`, and to a method found on `Bun.s3`
`TypeError.prototype.list = [1, 2]`, then `catch (e) { put(e.constructor.prototype, { length: 3 }) }`
with `put(where, what) { where.list = what }`, then `TypeError.prototype.list.length` read in
a function: the read took the list for what the build had seen stored by name, and the
program ended in a segfault. The same with `Bun.S3Client.prototype.file.list = [1, 2];
Bun.s3.file.list = { length: 3 }`. Where no list was involved, the read answered
`undefined`. Both stores are now seen by a read that names the object.
| bool isWrittenByFileOfProduct(VM& vm) | ||
| { | ||
| // Compiled code calls a built-in function itself, or through an operation that enters the VM. A refusal is rare, so the walk that lists every kind of frame will do. | ||
| constexpr size_t framesLookedAt = 32; | ||
| Vector<StackFrame> frames; | ||
| vm.interpreter.getStackTrace(nullptr, frames, 0, framesLookedAt); |
There was a problem hiding this comment.
🔴 Debug builds of a test-run program now crash on a RELEASE_ASSERT, instead of raising the read-only error, when a locked built-in is written below a host frame. isWrittenByFileOfProduct at AOTErrorMessages.cpp:3417 calls getStackTrace with a null owner. Every StackFrame it appends with a callee runs vm.writeBarrier(nullptr, cell), which fails ASSERT_GC_OBJECT_LOOKS_VALID under ENABLE(GC_VALIDATION). Fix: capture the frames without an owner-barriered StackFrame (walk with CompiledFrameIterator or StackVisitor and read only the source of each compiled frame), or pass a real owner cell; the same walk also now runs for every refused locked write, including silent sloppy-mode ones, so it should stay off that hot path.
Why this was flagged
A program built with replaced builtins admitted (admitsReplacedBuiltins) writes to a locked built-in, e.g. Reflect.set(Array.prototype, 'x', 1) in a test, or a sloppy assignment inside a callback below a native function. JSObject.cpp:102-104 computes lockedPropertyPolicy, which calls lockedObjectErrorMessage and reflectThrowsOnLockedProperty; both now call isWrittenByFileOfProduct (AOTBuiltinCells.cpp:799, AOTBuiltinCells.cpp:825). That function calls vm.interpreter.getStackTrace(nullptr, frames, 0, 32) at AOTErrorMessages.cpp:3417. With compiled code present Interpreter.cpp:856 goes to AOT::tryGetStackTrace, which at AOTStackTrace.cpp:1553 does results.constructAndAppend(vm, owner, callee) for a host frame on top and at AOTStackTrace.cpp:1484 for a compiled frame with a recorded callee, and appendFramesBelow lists native frames the same way (StackFrame.cpp:42-44). StackFrame constructs WriteBarrier(vm, owner, callee), whose setEarlyValue (WriteBarrierInlines.h:52-56) calls vm.writeBarrier(owner, value) with owner == nullptr.
Verification: normal (debug builds only; release builds are unaffected) — triggered whenever a program compiled with AdmittedCode::ReplacedBuiltins writes/deletes a locked built-in while any frame with a callee is on the walked stack (e.g. below a host function such as Reflect.set, or a native that invoked the callback, or any host/bytecode frame below the compiled run).
| void CoroutineTable::willMakePrototypeOfGeneratorFunction(VM& vm, JSObject* function) | ||
| { | ||
| if (!m_structureOfGeneratorsWithoutPrototype) | ||
| return; | ||
| uint32_t place = placeOfGeneratorFunctionState(uncheckedDowncast<Closure>(function)->closureNumber()); | ||
| uint8_t& state = m_instance.coroutines.statesOfGeneratorFunctions[place]; | ||
| if (state != static_cast<uint8_t>(GeneratorFunctionState::MakesGeneratorsWithoutPrototype)) | ||
| return; | ||
| state = static_cast<uint8_t>(GeneratorFunctionState::MakesGeneratorsWithPrototype); | ||
| DeferGC deferGC(vm); | ||
| Vector<Generator*> found; | ||
| { | ||
| HeapIterationScope iterationScope(vm.heap); | ||
| vm.heap.objectSpace().forEachLiveCell(iterationScope, [&](HeapCell* cell, HeapCell::Kind kind) { | ||
| if (!isJSCellKind(kind) || static_cast<JSCell*>(cell)->classInfo() != Generator::info()) | ||
| return IterationStatus::Continue; | ||
| auto* generator = uncheckedDowncast<Generator>(static_cast<JSCell*>(cell)); | ||
| // One without a frame was left behind where the stack ran out in front of the body that makes the frame. Nobody has it. | ||
| if (!generator->takesPrototypeFromFunction() || !generator->savedFrame() || generator->structure()->realm() != m_instance.globalObject) | ||
| return IterationStatus::Continue; | ||
| // Each image of the realm has a table of its own. | ||
| if (placeOfGeneratorFunctionState(generator->closureNumberOfFunction()) == place && generator->instanceOfFunction() == &m_instance) | ||
| found.append(generator); | ||
| return IterationStatus::Continue; | ||
| }); | ||
| } | ||
| for (Generator* generator : found) | ||
| generator->takePrototypeFromFunction(vm); |
There was a problem hiding this comment.
🟡 (optional) Programs with a large heap stall for a whole-heap walk the first time a generator reaches a generic JSC lookup, which the base never does. Generators made by the SyncWithHeapFrame path carry a Structure with OverridesGetPrototype, so JSObject::getPropertySlot (JSObjectInlines.h:267) calls Generator::getPrototype for any miss on the generator itself; that runs ensurePrototypeProperty, and willMakePrototypeOfGeneratorFunction at AOTCoroutineTable.cpp:111-124 iterates every live cell with forEachLiveCell under HeapIterationScope, which also stops all allocators. Fix: convert the generators of a place without walking the heap, so Object.getPrototypeOf, instanceof, Array.from(gen) and console.log(gen) cost no more than on the base.
Why this was flagged
A sync generator function whose closure was never asked for prototype makes its generators through the SyncWithHeapFrame path (AOTJobs.cpp:887-889); each has the Structure made at AOTCoroutineTable.cpp:488-491 with OverridesGetPrototype. Uncompiled code then does any lookup that misses on the generator object: Array.from(gen()), Object.getPrototypeOf(gen), gen instanceof X, or JSON.stringify(gen). JSObject::getPropertySlot at JSObjectInlines.h:267 sees overridesGetPrototype and calls Generator::getPrototype (AOTCoroutineTable.cpp:502-510), which calls ensurePrototypeProperty (AOTClosure.cpp:392-398) -> willMakePrototypeOfGeneratorFunction. With the place in state MakesGeneratorsWithoutPrototype that function takes a HeapIterationScope and runs vm.heap.objectSpace().forEachLiveCell over every block and precise allocation (MarkedSpaceInlines.h:46-60). On a heap of hundreds of MB that is tens to hundreds of milliseconds on the JS thread, once per place. On the base branch the generator was created with its function's prototype at NewGenerator (AOTJobs.cpp:891) and the same lookups are ordinary structure-chain reads with no walk. No safeguard limits the walk: the state check at AOTCoroutineTable.cpp:107 only skips places that never made such a generator.
Verification: Source/JavaScriptCore/aot/runtime/AOTCoroutineTable.cpp:99-100 ("one walk for a place in the table at most. JSGlobalObject::haveABadTime() is the model"); the stated bound (at most one walk per 4096-way place) is accurate, but each walk is a full object-space iteration whose cost scales with the entire live heap, and the trigger is ordinary code rather than the rare pattern haveABadTime guards.
| auto callers = std::ranges::equal_range(m_directCalls, activation.function, { }, &DirectCall::callee); | ||
| m_callersAreTooManyForOneWalk |= !m_testsBeingAnswered && m_valuesFollowed < m_budget && callers.size() > m_budget - m_valuesFollowed; |
There was a problem hiding this comment.
🟡 (optional) Builds of programs with a helper that has thousands of direct callers and a RegExp call inside it can take many times longer, up to callers times the walk budget. m_callersAreTooManyForOneWalk at AOTRegExpRequests.cpp:2664 is set exactly when callers exceed the remaining budget, and line 241 then sends the site to exploreForEachCaller, which resets m_valuesFollowed per caller (line 393) so each of N callers spends a fresh 16384 budget. m_leftWhereBudgetRanOut (line 744) only short-cuts a value whose remaining budget is at or below a previous run-out, which a fresh per-caller budget never is. Fix: keep a total budget across the per-caller walks, or cap the number of callers walked separately, so the cost stays bounded by the single-walk budget times a small constant.
Why this was flagged
Condition: a function with more direct callers than the budget left (the test at AOTRegExpRequests.cpp:2664 is callers.size() > m_budget - m_valuesFollowed) contains a RegExp request whose text or flags come from a parameter. Before this change m_didNotMultiplyAlternatives alone gated the per-caller fallback, so a walk over all callers simply ran out and the site was given up at the single budget. Now line 241 and line 409 send it to exploreForEachCaller, which per caller restores the budget (line 393, pre-existing) and walks again, so the total is callers x 16384 values. The check on m_leftWhereBudgetRanOut at lines 744-749 fires only when leftAtFirst <= the remembered remaining budget; each per-caller walk starts with a larger remaining budget than the shared walk that ran out, so the shortcut does not apply and each caller walks in full. Consequence: build-time blow-up of the AOT compiler for that program, with no cap. Remedy: a total budget across callers or a bound on how many callers get a walk of their own.
Verification: Line 2664 sets m_callersAreTooManyForOneWalk precisely when the all-callers loop will run out. Lines 241 and 409 now gate the per-caller fallback on it; on the base only m_didNotMultiplyAlternatives did, so the base spent one budget and gave the site up. exploreForEachCaller lines 393-398 give every one of the N top-level callers a fresh 16384 budget.
| if (!found || !(found->variablesBeforeMock || found->valueBeforeMock || (withExports && withExports->exportsBeforeMock))) | ||
| return; |
There was a problem hiding this comment.
🟣 pre-existing, not blocking: Test code that mocks a program file before anything loads it and then restores the mock keeps getting the mock from require() and import for the rest of the process. endMockLaidOverFile at AOTModuleLinking.cpp:925-926 returns before didEndMock(file) and before clearing exportsOfMock when nothing was saved under the mock. answerRequire at AOTModuleEvaluation.cpp:3436 still sees isMocked(target) and answers answerOfRequireToMock. Fix: on every end of a mock, clear the m_mocks bit and exportsOfMock first, and only then return when there is nothing to restore; the same ordering applies to endMockOfLibraryModule's early returns.
A small fix can ride a push you are already making; otherwise a short reply is enough.
Why this was flagged
A test calls mock.module("./file", factory) before any import or require of ./file, later ends the mock, then requires ./file. The host calls didEndMock (AOTModules.cpp:5232), which routes a program file to endMockLaidOverFile (AOTModules.cpp:5242). At AOTModuleLinking.cpp:925 found->variablesBeforeMock, found->valueBeforeMock and exportsBeforeMock are all null because keepWhatMockIsLaidOver (AOTModuleLinking.cpp:905) only saves what a file that already ran held, so the function returns at line 926 without reaching didEndMock(file) at line 928 or clearing loaded.exportsOfMock at line 929. Modules::didEndMock (AOTModuleLinking.cpp:397) is the only thing that drops the m_mocks bit, so isMocked(file) stays true. The next require() enters answerRequire, where AOTModuleEvaluation.cpp:3436 tests isMocked(target) and answers the stale mock's exports via answerOfRequireToMock instead of running the file. The base had no didEndMock hook at all, so this is the new restore path failing for the population of mocks laid before first load.
Verification: The base has no didEndMock host entry at all (AOTModuleHost.h:247 is added by this diff), so on the base a module mock never ends on any path. For an ahead-of-load mock, variablesBeforeMock, exportsBeforeMock and valueBeforeMock stay null, so endMockLaidOverFile returns at AOTModuleLinking.cpp:925-926 before didEndMock(file) at :928, isMocked stays true, and answerRequire (AOTModuleEvaluation.cpp:3436) keeps answering the mock.
There was a problem hiding this comment.
Findings marked 🟡 are optional suggestions and need no follow-up push.
Still open from earlier reviews (4):
- 🔴
Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp:3417—Debug builds of a test-run program now crash on a RELEASE_ASSERT, instead of raising the read-only error, when a locked… - Also unresolved: 3 minor or pre-existing.
If you have decided not to act on one of these findings, resolve its thread (a reply alone leaves it open) and the next review stops counting it. To review this commit again now, use Re-run on its "Claude Code Review" check.
| m_out.appendTo(hasCounters); | ||
| TypedPointer counter = m_out.address(heap, checkedCells, CheckedFrameCells::offsetOfBirthsAt(m_id.index(), context.valueID().index())); | ||
| LValue births = m_out.load32(counter); | ||
| LValue isLeftOut = m_out.bitAnd(m_out.aboveOrEqual(births, CheckedFrameCells::numberOfBirthsThatAreAllChecked), m_out.notZero32(m_out.bitAnd(births, CheckedFrameCells::intervalOfCheckedBirths - 1))); | ||
| m_out.branch(isLeftOut, normalEdge(staysInFrame), rareEdge(mayBeChecked)); | ||
|
|
||
| m_out.appendTo(staysInFrame); | ||
| m_out.store32(m_out.add(births, 1), counter); | ||
| ValueFromBlock inFrame = m_out.anchor(birth); |
There was a problem hiding this comment.
🟡 nit (optional): Validated runs no longer retire the previous checked cell of a frame slot at a birth that is left out, so a stale reference to it inside the same frame stays readable where the base made it fault. The inline path at AOTCellAllocationEmitter.cpp:316-318 hands back the frame slot after the 1024th birth without the work CheckedFrameCells::make does first. Fix: before taking the staysInFrame edge, retire the slot's live granule (what AOTCheckedFrameCells.cpp:227-236 does) and the granules of frames already left, or route left-out births through a cheaper operation that does both; the counter bump alone is not enough.
Why this was flagged
A build that validates references (generator().validatesOrChecksProofs() at AOTCellAllocationEmitter.cpp:286) runs a loop that allocates an object in the same frame slot each turn. On the base every birth went through CheckedFrameCells::make, whose first steps (AOTCheckedFrameCells.cpp:223-236) call didLeaveFramesBelow and read the granule index back from the slot to end the earlier granule, so the earlier guarded cell became inaccessible at the very next birth and a reference kept from the previous turn faulted on use. With this change a birth whose counter satisfies isLeftOut (AOTCellAllocationEmitter.cpp:313) takes the staysInFrame edge, bumps the counter and returns the slot itself; the program then overwrites the slot word with the new object's header, so the next make() at AOTCheckedFrameCells.cpp:227 no longer finds the index and the earlier granule stays Live until didLeaveFrameOf runs at frame exit. A reference that outlived its birth is therefore not detected within the frame. Only validation builds are affected.
Verification: nit — triggers in an image built with --aot-validate / CheckProofsAtRunTime when a frame-sunk allocation runs more than 1024 times in one frame. The inline path at AOTCellAllocationEmitter.cpp:313-318 hands back the frame slot without calling MakeCheckedFrameCell, so the previous granule, which CheckedFrameCells::make (AOTCheckedFrameCells.cpp:221-236) retired on the base, stays live and readable.
The pass and its tests stay in B3. The ahead-of-time compiler's policy no longer turns
it on.
What broke, in a loop:
holder.n = (holder.n + 1) | 0;
if (j === when) other.n = 50;
if (holder instanceof Other) bump(holder);
with `function bump(o: Other) { o.n = (o.n + o.k) | 0 }`. Where `holder` and `other` are
one object, a `Plain`, the store of 50 was lost: the function answered 7:29 instead of
55:317. The copy of `bump` in the loop never runs for that object, but its access named a
narrower heap for the word, and the pass let that one heap decide that nobody else
writes it.
There was a problem hiding this comment.
Code review found no new issues
No new issues were found in this update; 5 findings from earlier reviews are still open above.
Still open from earlier reviews (5):
- 🔴
Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp:3417—Debug builds of a test-run program now crash on a RELEASE_ASSERT, instead of raising the read-only error, when a locked… - Also unresolved: 4 minor or pre-existing.
If you have decided not to act on one of these findings, resolve its thread (a reply alone leaves it open) and the next review stops counting it. To review this commit again now, use Re-run on its "Claude Code Review" check.
|
Preview build of cc1ea90: |
…s, a bounds test on split() (#972) * AOT: miscompilations and crashes found since #967, builds that never ended, faster loops Wrong answers of compiled programs: - A list of numbers with one `undefined` in it, iterated by two loops. The first adds the numbers up, the second counts the `undefined`s: it counted 0 instead of 1. - In a generator, a value read before a `yield*` and used after it was wiped when a parameter that held an object was set to `null` or `undefined` in the same loop: 12 instead of 22. A second program never ended. - A `RegExp` made from text out of a list was compiled from the text the build had predicted, even when the list was read under two names in one step and then changed. - `main.mjs` imports `m.mjs`, which imports `x.cjs`, which calls `require()` on `m.mjs` under a name made at run time: `main.mjs` ran after `x.cjs` had ended, not before the `require()` returned. - An `import()` made while other `import()` calls were waiting, and whose graph shares a module with one of them, was held back behind all of them. Crashes of compiled programs: - A segfault in eleven lines: a loop reads and stores a static field that holds an object, after the field was deleted while a Proxy was on the class's prototype chain and was defined again through the trap. - A segfault at address 0 after an out-of-memory error that the program catches: a rope of 2^30 two-byte characters that cannot be flattened, ahead of a short loop in a `try` with `catch` and `finally`. Builds that failed or never ended: - Thirteen lines did not build in two minutes: two nested loops of eight iterations, a swap in a `try`, an xor in its `finally`. B3's `Value::returnsBool()` recursed into both operands without remembering what it had asked, so its cost was exponential. - One function spent 214 of 246 seconds in type propagation: its queue was last in, first out. - The compiler aborted on `for (;;) { if (source.take === never) return ..; const got = source.take(..); .. }`. A loop that reads the same property of an unchanging object on every iteration gets a version that asks ahead of the loop, and a type test on that read was turned into a cast of the copy. Where both sides of the test's branch leave the loop, the branch still hands on what the test proved, so its condition has to stay a test. - Fourteen lines stopped the build with `InvalidGuard`: a read of a typed array at the top of an iteration relied on the bounds check at the end of the previous one, and the loop's counter was then replaced by a constant. - Quadratic time in large functions: three places in Air's register and stack allocators, two in the ahead-of-time compiler's own passes (one is flow analysis), and the lookup of a regular expression's symbols (arm64: twice as many alternatives cost 9.5 times as much, now 1.74 times). `bun test --aot`, following oven-sh/bun#44833: - An `import` that reaches a mock whose factory threw gets the error that the loader kept. `require()` of a mocked module returns what the factory returned. A later mock of a CommonJS file doesn't change what an earlier `require()` returned. - The engine's own tests no longer depend on how many built-ins Bun has. Speed: - New B3 pass, promoteLoopMemory: `a[k] += v[j]` and `o.total += x` reloaded on every iteration what the previous one had stored. The word is read once ahead of the loop and carried in a Phi. The stores stay where they are. Off by default; only the ahead-of-time compiler's policy turns it on. - `map.has(k)` followed by `map.get(k)` searches once. Keys that are objects are searched inline in loops. - A generator function made for each call: 2,585 to 175 instructions. tslib's `__awaiter`: 3,876 to 1,441 per iteration. - Helpers that Babel and TypeScript emit: `Reflect.construct` behind tests, a built-in bound and called in one function, descriptors read and defined again, `for..in` copies. - Remainders and quotients of whole numbers stay integers. A loop polls with one faulting load on arm64, macOS x64 and Windows arm64 too. * AOT: a library function that a test run leaves out is refused, not aborted on `import { test, vi } from "vitest"; test("t", () => { vi.useFakeTimers(); })` under `bun test --aot` ended in `abort()`. The code that throws "was not compiled into this executable" looked the function up in the table of built-ins to ask whether it is an accessor's getter, and a function that the table leaves out has no record there. It asks `contains()` first now, so the error is thrown. * AOT: a member copied off `require("bun")` under a computed key is followed to its call `bun = require("bun"); copy = {}; copy[key] = bun[key]; copy.plugin(options)`: the build now sees that `copy.plugin` may be `Bun.plugin`. The test, `plugin-copy-late-mark.ir`, comes with Bun's half. * AOT: two segfaults: a store through a caught error's `constructor.prototype`, and to a method found on `Bun.s3` `TypeError.prototype.list = [1, 2]`, then `catch (e) { put(e.constructor.prototype, { length: 3 }) }` with `put(where, what) { where.list = what }`, then `TypeError.prototype.list.length` read in a function: the read took the list for what the build had seen stored by name, and the program ended in a segfault. The same with `Bun.S3Client.prototype.file.list = [1, 2]; Bun.s3.file.list = { length: 3 }`. Where no list was involved, the read answered `undefined`. Both stores are now seen by a read that names the object. * AOT: promoteLoopMemory is not switched on yet The pass and its tests stay in B3. The ahead-of-time compiler's policy no longer turns it on. What broke, in a loop: holder.n = (holder.n + 1) | 0; if (j === when) other.n = 50; if (holder instanceof Other) bump(holder); with `function bump(o: Other) { o.n = (o.n + o.k) | 0 }`. Where `holder` and `other` are one object, a `Plain`, the store of 50 was lost: the function answered 7:29 instead of 55:317. The copy of `bump` in the loop never runs for that object, but its access named a narrower heap for the word, and the pass let that one heap decide that nobody else writes it. * AOT: fix crashes and miscompilations found since #971, free Map/Set index slots, turn promoteLoopMemory back on Stacked on #971. The stored programs and tests for everything listed here come with oven-sh/bun#47605. Crashes and failed builds: - `TypeError.list = [1, 2]; JSON.parse("0", () => Reflect.set)(e.constructor, "list", { length: 3 })` with `e` a caught error, then `TypeError.list.length` in a function: the compiled program segfaulted. The same happened with a hidden `Object.assign` or `Object.defineProperty`, and when the writer was called as a member of a hidden object. A write through a value the compiler can't identify is now also applied to built-in objects, even when flow analysis sees no path to them. - `Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key))` with a `key` object whose `toString()` returns "a" and then "b": plain prints 3, compiled code segfaulted. With methods the wrong one ran; with getters it threw "'a' was left out of the executable". - Every `--aot` executable aborted before its first statement on a kernel with 64 KB pages (RHEL 8 aarch64, Oracle's UEK, the 64k kernels for NVIDIA Grace). - The compiler aborted with "Stopped while compiling run in ConvertCountersToIntegers" on `for (..) { if (j === w) other.n = j; holder.n = (holder.n + 1) | 0; if (holder instanceof Q) bump(holder); }`. - `vi.doUnmock("./k.js")` while a plugin's async `onLoad` is still loading another import segfaulted under `bun test --aot`. - `bun test --aot` aborted under `validateExceptionChecks`, which CI's ASAN lane sets, when a mock was installed in a layered run: `vi.doMock("./m08.js", async () => { await sleep(80); return { value: "mocked" }; }); import("./m08.js")`. Miscompilations: - The code that creates, fills and stores a module variable's value is deferred until the variable is first read. An assignment to a plain object or function created by that initializer was deferred with it. But a program may call `Object.freeze(Object.prototype)` or `Object.defineProperty(Object.prototype, "toString", { writable: false })`, and after that an assignment to an inherited name fails: a TypeError in strict code, silently dropped in sloppy code. Imports run first and the main module hardens afterwards, so plain did the store before the freeze and compiled code did it after. main has this bug too. - A loop's stores to a variable were delayed to the loop's exit edges, even across an operation that throws only when memory runs out (`text.charCodeAt()` on a rope). That error can be caught, and then the loop is left through none of those edges. - A Map or Set lookup with the same key on every iteration was hoisted out of the loop, even when a `try` covered the lookup but not the loop's preheader, and even when the loop might not run: `for (let i = 0; i < 0; i++) map.has(rope)` threw a RangeError. - A loop that does `const list = []`, pushes, reads `this.state.type`, reads `list[0]` and stores the list into a node at the end of the iteration: a value read back out of an open object was taken for this iteration's list. Also with the push on one arm of an `if`. - `seen.push([a])` in one loop and `for (const kept of seen) show(kept)` in the next: the inner list was attributed to `show()`. - `const o = Object.create(Blob.prototype, {}); o.constructor = { get name() { return "Stored" } }; o.text()`: plain throws ERR_INVALID_THIS ".. received an instance of Stored"; compiled code threw "'name' was left out of the executable". The same for `Buffer.concat(v)` when `v` is an object that isn't an array. - `const { module } = await WebAssembly.instantiate(..)`: the compiler didn't know what the promise resolves with. Modules: - `for (3 times) try { require("./t.cjs") } catch {}` where t.cjs throws: the body ran once and the first error was thrown three times. It now runs again each time, as in node and `bun run`. - `Object.isExtensible(require("./e3.mjs"))` after an import of the same file: plain true, compiled false. - `require("./m3.mjs"); Object.keys(require("./d.json"))` where m3.mjs imports d.json: plain `a,default`, compiled `a`. - Loading a file that only a plugin can provide, under a name computed at run time. - A test registers `Bun.plugin` with an `onLoad` that matches slow.js and then does `await import("./user.js")`, where user.js imports k.js and slow.js: "Cannot access 'x' before initialization" for k.js's export. Quadratic behavior: - `map.delete(k); map.set(k, v)` took a new slot in the table's index each time and never freed the old one, so each iteration cost the size of the table. A deletion now frees the slot when no search can miss it. This costs 5 more instructions on a set and on a delete. - `class Below extends mix(Base)`: the whole caller was walked for every such call. At 3,200 classes that step went from 8,306.6 M instructions to 119.7 M; the generated code is byte-identical. - Two more build steps that grew with the square of the program: choosing representations around a web of values, and finding who reads a parameter that is only passed on. Speed: - The AOT policy turns promoteLoopMemory on again, with the fix for the aliased store that got it turned off in #971. What guards it: 14 stored programs that each gave a wrong answer with an earlier version of the pass, a fuzzer family written for it, 8,815 programs with no differing result, and a review by someone who didn't write it and tried to break it: 0 of 7,728 programs wrong. - The low word of a sum that was built from doubles is computed as a 32-bit sum. Outside aot/: 13 files. The JIT compiles b3/ too; the changes below run there only where noted. - b3/B3PromoteLoopMemory.cpp and its testb3 cases: the pass. It runs only when `loops.promotesMemory` is set: false by default, set only by the AOT policy. - b3/B3LikelyPaths.{h,cpp}: how the nearest common dominator over likely paths is found. Used by hoistLoopInvariantValues() under `loops.hoistsOnlyFromLikelyPaths`: false by default, set only by the AOT policy. - b3/B3ReduceStrength.cpp and testb3_bun_reductions.cpp: `Trunc(Op(a, b))` becomes `Op(Trunc(a), Trunc(b))` when this walk built the operation from doubles. Under `narrowsIntegers`: false by default, set only by the AOT policy. - b3/B3LoopRangeChecks.cpp, b3/B3Policy.h: a switch that lets a test sabotage the compiler (`omitsLimitOfClampedBound`, false by default) and a client callback (null by default). Loops are versioned only under `loops.versions`: set only by the AOT policy. - b3/B3CompilationReport.h: 16 more counters in a Procedure's report. That is 64 more bytes per Procedure, in the JIT too; nothing there increments them. - heap/HeapSubspaceTypes.h: an #include under the existing `#if ENABLE(AOT)`, for the generators' own subspace. - llint/LLIntEntrypoint.cpp, runtime/VM.cpp: inside `#if ENABLE(AOT)`: code that runs in the interpreter is also counted in a process without an image, under an option that tests set. - runtime/ExceptionHelpers.cpp: inside `#if ENABLE(AOT)`: a clearer error for a global function declaration that reuses the name of a global that compiled code treats as a constant. * AOT: a bounds test on the result of split() or splice() compared against the wrong length function f(h: string | null): string[] { if (h == null) return []; const accepted: { type: string; count: number }[] = []; for (const part of h.split(",")) { const [type, ...params] = part.split(";").map(v => v.trim()); accepted.push({ type, count: params.length }); } return accepted.map(e => e.type); } Built with --aot-validate, f("a/b") and f("c/d, e/f") returned [] instead of ["a/b"] and ["c/d", "e/f"]: the whole for-of loop was removed. No error, exit code 0. main has this bug too. When only the first few elements and the length of an array returned by split, match, exec or splice are used, allocation sinking never builds the array. It becomes a virtual object whose shape counts only the elements that are kept separately. evaluateBoundsTest() compared an index against that count as if it were the array's length. Nothing in this function reads a constant index, so the count was 0, every index was "out of bounds", the edge into the loop body was dead, and the body held the only use that would have asked for the real array. For such an array, an index past the shape's count is now unknown instead of false. The wrong answer has only been seen under --aot-validate, where a type check keeps the array alive. At default flags, iterating over split()'s result becomes a loop over indexOf and substring and no array is made. But the rule itself was reached at default flags too, so a wrong answer there can't be ruled out for earlier builds. The same shape with splice() was wrong as well. The generated code of ten servers and the TypeScript compiler benchmark is unchanged at default flags. The tests come with oven-sh/bun#47605: test/aot/parallel/strings/split-list-walked.ts and test/aot/parallel/arrays/spliced-list-walked.ts. * AOT: include what AOTValueFlowSolveTestCommands.cpp uses The file uses StringPrintStream and makeString() without including their headers. It compiled only where the unified build put it next to a file that does. The -aot lanes failed with "unknown type name 'StringPrintStream'" and "use of undeclared identifier 'makeString'". * AOT: don't name a variable `small` in AOTValueFlowCoreTests.cpp The Windows SDK's rpcndr.h has `#define small char`, so `ValueSets small(1u << 10);` didn't compile on the Windows -aot lanes ("cannot combine with previous 'type-name' declaration specifier"). * AOT: a module whose mock ended before its body ran has not begun to load Under `bun test --aot`, two test files each mock the same ES module before it loads (`vi.mock("./dep.js", ..)` hoisted, or `mock.module()` in a test ahead of the `import()`). A third file, which mocks nothing, read the second file's mock, by name and through the namespace object, and dep.js never ran. Plain `bun test` runs dep.js and reads the real values. When the first mock ended, its module still counted as having begun to load, because that goes by the group it was loaded with, which is never taken back. So the second mock's factory didn't wait for the first import. It ran at once, and its values were laid by name over a module whose body had never run. Modules::hasBegunToLoad() now answers no for such a module. The bit it reads is set only where a mock that stood in place of a module ends, so an executable never gets here. The test comes with oven-sh/bun#47605: test/aot/mock-second-file.test.ts. * AOT: check for an exception after a mock in place is taken back AOT::didEndMock() had no throw scope, and went on after setMockInPlace(null), which can throw. Under `validateExceptionChecks`, which CI's ASAN lane sets, `bun test --aot` aborted in two test files: "setMockInPlace @ AOTModules.cpp .. unchecked as of captureExports @ AOTModuleLinking.cpp". // a.test.js vi.mock("./dep.js", () => ({ value: "mock" })); // the mock ends with its test file; b.test.js then imports ./dep.js * AOT: build the same program at one thread as at many The `typescript` package built at `--aot-threads=1` and at 4 gave two different executables from the same front-end output. One line in one function of 16,981 differed: for one property read, the predicted receivers were empty at one thread and listed at two or more. No proof differed, and 4, 4 again and 16 threads agreed, so it was never run to run. findOnAllThreadsAndDo(), in the pass that computes predictions after the solver's rounds, was the only place in the flow analysis with a test for exactly one thread. With one worker each item of a wave ran in place and saw what the items before it had done. With more, all steps of a batch are computed from the state at its start and replayed in order. Both end at a fixpoint, but the pass has caps, so not always at the same one. One thread now takes the same path as many. Batch and share sizes don't depend on the thread count. At two or more threads nothing changes. Six thread counts from 1 to 32, eight times each: one hash, the one that 4 and 16 threads gave before. test/aot/same-program-however-the-threads-fall.test.ts in oven-sh/bun covers it. * AOT: a read copied for another kind of array storage kept its proof across a store through a second name function clean(src, dst, n) { for (let i = 0; i < n; i++) { if (src[i] < 0) dst[i] = NaN; // a bad sample else dst[i] = src[i] * 2; } } clean("1,2,3,4,5,6".split(","), [], 6); const samples = [1.5, -1.5, 2.5, 3.5, 4.5, 6.5]; clean(samples, samples, 6); console.log(samples.join(" ")); // 3 NaN 5.5 7 9 13, should be 3 NaN 5 7 9 13 Silent, at default flags, in plain JavaScript and in TypeScript. main has this bug. PlanArrayCapacities copies the reads behind a test of an array's kind of storage for the other kind, as far as the test's proof goes. addReadsOfOtherStorage() copied the stores between those reads with them and never asked whether the copy's proof outlives such a store. A store through another operand that may be the same array ends it. The guards are now asked again behind each copy, and a copy that may lose its proof is taken back. New: --aot-sabotage=CopiedReadsKeepTheirProof, the statistics ReadsCopiedForOtherStorage and CopiesOfReadsTakenBack, two IR tests. The 30 stored programs come with oven-sh/bun#47605. * AOT: SplitBehindEntryChecks kept value ids across a retyping that renumbers the values function outer(p: any, parent: number | null): string { const head = parent === null ? "" : "<a " + parent + ">" + "<b " + parent + ">"; return head + (p as Kind).name; } // called hot as outer(given, null) An assertion build of the compiler aborted on `id.index() < values.size()`. A release build read outside the function's table of values. No release build has been seen to give other code. typeValuesFromNever() may cut dead blocks, and then it compacts the function and gives every value a new id. sayWhatOperandsOfFormerChecksHaveProven() went on with the ids it had taken before. The retyping is now handed a Renumbering: the checks' ids are mapped, and those cut with the dead code are dropped. The bound at the head of that function is a RELEASE_ASSERT. * AOT: the second import() of a CommonJS file whose mock factory throws asks the factory again mock.module("./lib.cjs", () => { throw new Error("boom " + ++n); }); // import(), require(), import(), require(), each caught Under `bun test --aot` as one program the errors read boom 1, 2, 1, 3. Plain `bun test` has 1, 2, 3, 4. An import() of a CommonJS file begins at the file's ES face. The walk then comes to the file itself, which isn't the root, so the throw counted as "behind an import statement" and the error was kept on the file for the loader. The file behind the root's face now counts as the root. * AOT: a mock factory that takes importOriginal and fails is asked again at the next import In plain `bun test` a factory that takes `importOriginal`, sync or async, is laid over the original. Where it fails the loader keeps no record: the next import asks it again, and the module that imports the mocked one then loads. A factory without the parameter keeps its error. Compiled code kept the error for both kinds. deferMock() is now told which kind the mock is, and runMocksBelow() keeps no error for the first kind. Where such a factory's promise rejects a turn later, the mock waits for the next load again instead of being taken away. The test comes with oven-sh/bun#47605: test/aot/mock-factory-retry.test.ts. * AOT: vi.doUnmock() in a test, behind a hoisted vi.mock() of a module that never ran vi.mock("./dep.js", () => ({ value: "mock", fn() { return "mock-fn"; } })); import { value, fn } from "./dep.js"; test("t", () => { vi.doUnmock("./dep.js"); fn(); value; }); Under `bun test --aot` the imported function answered `real-fn` silently, and consts, `ns.value` and `Object.keys(ns)` threw "before initialization". In plain `bun test` whoever was linked keeps the mock. An ended mock that stood in place of a module is now let go when a load next comes to that module. At the end of the test file, while a graph is being loaded, and for .cjs and library modules it is let go at once, as before. So it is where a module that a require() has already answered imports the mocked module: a compiled require() of a module that has run asks nobody, so no load would ever come by. * AOT: `export * from "<a CommonJS package>"` in a mocked module, in layered test runs // lib.mjs export * from "driver"; export let v = 1; // a.test.js mock.module("./lib.mjs", () => ({ v: "mock v" })); await import("./lib.mjs"); In a layered `bun test --aot` run (`--parallel=2 --aot-memory-limit=1KB` is enough) this failed with `TypeError: The module ".../driver/index.js" is not loaded`. The same error came in a second test file that only imports a user of a module which the first file had mocked. A regression of this branch. Three places went behind the `export *` of a mocked module to take names, and evaluateModulesOwedBelow() ran a module that a mock had kept from running without asking the loader for what only that module imports. A mock that came ahead of the load now stands for the whole module. * AOT: the first require() of an ES module gave its namespace object a Structure shared with others import { x } from "./a.mjs"; // read hot const all = ["./a.mjs", "./b.mjs", "./c.mjs"].map(p => import.meta.require(p)); function readX(ns) { return ns.x; } // over the three objects, hot In a layered `bun test --aot` run readX() came to answer a's x for b. Silent. A regression of this branch, found in review. willRequireESModule() set the Structure that all extensible namespace objects share, also on an object that had a Structure of its own, and hasOwnStructure() went on answering true. A property cache keyed on the shared Structure then carried the first module's binding to the next. Such an object now gets a new extensible Structure of its own. * AOT: the compiler aborted on `o.self = o` where o may be a subclass of Map class C extends Map {} function see(o) { return o.self; } function run(o = new C()) { see(o); o.self = o; } run({}); run(); "Stopped while compiling run in PlaceTypeTests." Also over Set, WeakMap, WeakSet, Array and Error. main has this bug. Behind a store's tests, rewriteTestedPropertyAccess() takes the last TypeTest for the test of the stored value, which says nothing about the receiver, and leaves it out. Here the stored value is the receiver, so its only test was left out and the access couldn't be resolved. The test is now left out only where the receiver hasn't passed it too. * AOT: an import() waits for the async factory of a mock below it vi.mock("./a", async importOriginal => ({ ...(await importOriginal()), a: "mocked-a" })); import { a } from "./a"; // and user.ts: export const atLoad = a; Under `bun test --aot` as one program: "Cannot access 'a' before initialization", or the real value, and the next test file "never finished loading". Which of the three came went by when a thread had read the original from disk. A hoisted vi.mock() turns the test file's import statements into such import() calls, so Vitest's commonest partial mock met it. main has this bug. startImport() called the waiting mock's factory, held the import for one turn and delivered it whatever the factory had done by then. Only the loader's own fetch waited. Where factories wait, the import now goes on behind them, and is rejected with what one of them throws. * AOT: a bounds test on the kept prefix of split()'s result compares against the real length Follows "a bounds test on the result of split() or splice() compared against the wrong length". That commit made an index past the kept elements unknown instead of false. The other side had the same flaw: an index inside the shape's count was taken for in bounds, though the array may be shorter than the number of elements that the function reads. The test becomes `position < the array's real length`. New: --aot-sabotage=ListsOfBuiltinsEndWithWhatIsKept and the statistics ListsOfBuiltinsNeverBuilt and ObjectsOfBuiltinsNeverBuilt. It costs 0.15% on one router idiom. * AOT: three IR tests check the new counters of arrays and objects that are never built * AOT: a ring of `export *` with a package in it hung the link of a layered test run // index.ts: export * from "./a"; export * from "./b"; export * from "pkg"; // a.ts, b.ts: export * from "./index"; import { missing } from "./index"; In a layered `bun test --aot` run the link never ended (485,000 frames when it was killed). It should throw the SyntaxError for the missing export, as it does as one program and in an executable. A regression of this branch, found in review. Modules::isMockedInItsImage() called itself for every dynamic `export *` with no set of visited modules. It is one loop with a BitVector now, and asks each module once. * AOT: a name that a barrel hands on from a mocked CommonJS file is the mock's // barrel.js: export { name } from "./lib.cjs"; vi.mock("./lib.cjs", () => ({ name: "mock" })); import { name } from "./barrel.js"; Under `bun test --aot` as one program `name`, `ns.name`, Reflect.get and `(await import("./barrel.js")).name` read `undefined`. Object.getOwnPropertyDescriptor and a spread of the same object were right. findNamespaceExport() leaves a member to the namespace object where the module that declares the variable may be mocked. The names of a CommonJS file are variables of its ES face, and the mark sits on the file, so the read was bound to a variable that nothing fills while the mock stands. Both are asked now. * AOT: a function of a built-in module replaced by a writer the build can't see import found from "node:module"; JSON.parse("0", () => Reflect.set)(found, "SourceMap", mine); function use() { return found.SourceMap(); } use(); // plain: mine's result; compiled: SIGSEGV With `buffer.atob`, `Buffer`, `Blob` and all of `node:util/types` the write went through, a read saw it, and the call by the module's name silently reached the original. main has this bug. Flow analysis reasoned that start-up locks every member of a built-in that no statement writes. Nobody locks the object of a module, and nobody should: stubs and instrumentation replace such members. A holder that is a module's object, has no lock under the name and has escaped to code nobody sees now also answers what was stored through the unknown object, so the call is tested and not bound. * AOT: a variable read stale behind a method picked by a name known only at run time const handlers = { open(done) { return { run() { done({ ok: true }); } }; }, close(done) { return { run() { done({ ok: false }); } }; }, }; const type = JSON.parse('{"type":"open"}').type; let result = null; handlers[type](r => { result = r; }).run(); console.log(JSON.stringify(result)); // null, should be {"ok":true} Silent, at default flags. Neither the verifier nor --aot-validate sees it; --aot-check=MovedLoads does. main has this bug. In cold code Select fuses a property read and a call into CallMethod. That operation's effects are "unknown code", without its known callees. "Unknown code" is what functions entered by unknown code do, and a function whose callers flow analysis all knows isn't among those. So B3 was told that the call doesn't write the variable's word, and forwarded the earlier store to the load. The read and the call are now fused only where the call's effects lie within those of unknown code. New: --aot-sabotage=FusedCallsEnterOnlyUnknownCode. * AOT: a setter laid by Object.defineProperty that stores through `this` import "node:util"; class C { z = 0; } function run(given) { const o = given ?? new C(); Object.defineProperty(o, "p", { set(v) { this.z = v; } }); o.p = 1; return o.z; } console.log(run(), run([])); // 0 1, should be 1 1 Silent. Where a TypeScript class declares `z?: number` the executable aborted. Twenty built-in modules arm it by being imported anywhere in the program. main has this bug. The library's text holds a function that lets accessors escape, so the setter escapes, rightly. For a store with several receivers the setters found are then {unknown}, which covers the known setter, and the branch that hands each receiver to the setters on its own chain asked the covered view and was skipped. The instance was neither handed to the setter nor made to escape, so its field counted as stored once. For a store, where unknown stands among the setters found, the receivers that haven't escaped are now told apart, and each is handed to the setters laid on its own chain. A getter that writes through `this` and is run by a spread had the same hole in the rule for copies. What flow analysis hands the optimizer is unchanged on express, fastify and the typescript package. * AOT: the test of a branch that passes on a proof stays in the branch's block function f(x, n) { let fuel = 3, b = x, c = 1n, e = x, a = x; const gt = { get v() { a = b; return 1; } }; for (let i = 0; i < n; i++) c = e; while (c === b && fuel-- > 0) { gt.v; } b++; return e + 1; } f(1, 2); f("a", 2); "Stopped while compiling f in SimplifyControlFlow", and the step that the message advises to switch off isn't the one at fault. main has this bug; a neighbour with a value out of Map.get built on main and aborted on this branch. HoistLoopInvariantReads lifted the loop's test out of the block of its Branch, which passes a proof on its taken edge. PlaceTypeTests then copied a part of the loop, and the SSA repair turned the Branch's condition into a block parameter, which proves nothing. Of 58 varied forms 23 didn't build and none built and answered wrongly. Such a test is no longer hoisted. Those loops now make an unchanging test in every iteration: 1,031 functions of the public corpus change, 3,361 bytes fewer. * AOT: a class is no longer recognized by its prototype's own `constructor` property const Base = t => class { constructor() { this.a = t; } }; const Heir = S => class extends S { constructor() { super(); this.b = 5; } }; for (let i = 0; i < 2; i++) { const D = Heir(Base(i)); delete D.prototype.constructor; console.log(i, JSON.stringify(new D())); // the second iteration: SIGSEGV } With class fields it is silent: `{"a":1}` for `{"a":1,"b":5}`. Besides `delete`: Reflect.deleteProperty, an accessor, Object.defineProperties, and a mixin helper that copies another prototype's own properties. Of 3,432 generated forms 472 were wrong, 164 by SIGSEGV. main has this bug. From a class's second evaluation on, where no member guard is left, its class object and prototype object are left out of the table of holders. Asked later whose prototype an object is, ClassTable::classOfHolderLeftOut() read the object's own `constructor`. With that gone the answer was "nobody's", and the base's constructor kept the object it had allocated in the base's shape. A prototype object that is left out now names its class object under a private name of the class table: read-only, not enumerable, not deletable, and seen by nothing that a program can ask. * AOT: include what AOTCallRules.cpp uses matchMethodCall() now asks context.build().isSabotaged(..), and BuildContext was an incomplete type there. Bun's build precompiles a prefix header that brings AOTBuildContext.h in; WebKit's own CMake build does not.
#967, #971 and #972 changed how an import() waits for its text. Two files of aot/runtime had conflicts. - TextThatWaits has the new fields of main and then the async context of the call. - deliverFirstTextThatWaits() installs that async context around the call of startImport() that main makes. - The jobs that main queues in startImport() and decideHowCountedImportGoesOn() still carry the async context of the import().
main has eight commits since 4d45755, the commit this branch started at: #967, #971, #972, #974, #978, #979, #982 and #983. Two files conflict, each in one place. Both sides changed a line that calls one of WTF's atomic functions: this branch gave it the MemoryOrder template argument (WebKit/WebKit@6ab0aebc81), main changed the code beside it. - aot/analysis/AOTCallGraph.cpp: main's new loop over body.expectedFunctionTypes, with atomicLoad<MemoryOrder::Relaxed>() and atomicStore<MemoryOrder::Relaxed>(). - aot/runtime/AOTInstance.cpp: main's `if constexpr (hasFaultingLoopPolls)` in setPollsEnabled(), behind atomicStore<MemoryOrder::Relaxed>(). No conflict, and the same change: aot/valueflow/AOTFlowApply.cpp has three calls in the old form that main added. They take the template argument. The other 21 files that both sides changed merge as they are.
… bit is free
property-lookup/absence-filter-names-what-stops-stores asked the filter of a plain object
whether a store under "label" passes. The filter has 128 bits for all the names of an
image, so names share bits. The accessor __proto__ of Object.prototype stops stores above
every plain object, and a name with the bit of __proto__ does not pass: that is what the
filter is for, and the test failed at its first check,
ASSERTION FAILED: ofBelow && ofBelow->letsStoresPass(*number)
The number of a name goes by how often the program and the library use each name, and
the library of testaot in Bun's build is Bun's table of built-ins. So the two numbers move
with Bun's sources and with the platform. In the build of oven-sh/bun#47811 at 205a11bbaf
(Buildkite 137387) the test failed on seven lanes and passed on Linux with glibc. There
"label" has the number 1241 and __proto__ has 89: both are bit 89. With the table of
Bun's main one merge later they are 1241 and 1599, and the test passes.
The change is the one that main has for this test since #971 (8a7c63a), word for word:
the test takes the first of the four names of its program that the filter lets pass.
With the table of 205a11bbaf that is "count", and the test passes there.
… bit is free
property-lookup/absence-filter-names-what-stops-stores asked the filter of a plain object
whether a store under "label" passes. The filter has 128 bits for all the names of an
image, so names share bits. The accessor __proto__ of Object.prototype stops stores above
every plain object, and a name with the bit of __proto__ does not pass: that is what the
filter is for, and the test failed at its first check,
ASSERTION FAILED: ofBelow && ofBelow->letsStoresPass(*number)
The number of a name goes by how often the program and the library use each name, and
the library of testaot in Bun's build is Bun's table of built-ins. So the two numbers move
with Bun's sources and with the platform. In the build of oven-sh/bun#47811 at 205a11bbaf
(Buildkite 137387) the test failed on seven lanes and passed on Linux with glibc. There
"label" has the number 1241 and __proto__ has 89: both are bit 89. With the table of
Bun's main one merge later they are 1241 and 1599, and the test passes.
The change is the one that main has for this test since #971 (8a7c63a), word for word:
the test takes the first of the four names of its program that the filter lets pass.
With the table of 205a11bbaf that is "count", and the test passes there.
…grade-to-fd64f10cbda1 This branch is main with the upgrade to upstream 1d4bed4 (#992). The branch that is merged in is the same upgrade on the engine of Bun's main, and on top of it the merge of upstream fd64f10 (1b14e09) and what the fork needed behind it. The two branches part at 2edbf27. Three files conflict, all of them tests: - b3/testb3_bun_placement.cpp and b3/testb3_bun_vectorize_loops.cpp: main and the merged branch repaired the same two tests (an argument that is made where it is used, and a disassembly that reads past the code). Main's text stays. - aot/tests/AOTListedFieldsTests.cpp: each side adds tests at the same place. Both stay. aot/tests/AOTPropertyLookupTests.cpp merges without a conflict: the merged branch took main's change of #971 word for word.
…en-sh#44833's mocks Pins WebKit to autobuild-preview-pr-971-cc1ea906 (oven-sh/WebKit#971). That pull request has the engine's half of what is listed here and its own list of miscompilations and crashes; their stored programs and tests come with this commit. Hangs and failed builds: - esbuild's `transformSync()` and `buildSync()` never returned in a compiled program. `var threads; try { threads = require("worker_threads") } catch {}`, then `var start = given => new given.Worker(__filename, ..)` and `start(threads)`: the search for the files that Workers start from knew the module behind an import, a direct `require()` and a `const`, not behind a variable that is assigned or a parameter. The Worker could not be loaded, and the caller slept in `Atomics.wait()`. - A package that says `new Worker(__filename)` got `this executable can't load a module from a dynamic path`, in an executable and in a test run built as one program. - `mock.module("node:os", ..)` in a.test.js, `await import("node:os")` in b.test.js: the promise never settled and the test timed out. Since oven-sh#44833 plain `bun test` takes a file's mocks away when the file ends, and nobody told the engine. - `internal compiler error (LinkAndEmit): UndefinedLayout`, in two ways. `child.ts` has `import "unbuilt"`, which can't be resolved, and `export class Child extends Base`, and another file says `await import("./child")`: in a test run no test file ran. Or a package with `"sideEffects": false` has `export class Unused extends Base {}` beside `export const used = 1`, and main.ts imports `used` and, with `import type`, `Unused`. The layout of a class whose statement is not compiled named a parent that nothing defines. - A build that never ended in the checker: a conditional type such as `T extends infer R ? R : never` was weighed by how it is written, not by what `T` stands for. - `import.meta.glob()` was not expanded under `--aot`. It is now, by Bun's own parser: lazy, eager, `import: "name"`, `?raw`, `Object.keys()` of it, in a CommonJS file. Wrong answers of compiled programs: - `bottom[name]`, with `class Top { [name]() {} }`, `class Middle extends Top` and `class Bottom extends Middle`, had its type if `Bottom` was asked about first and no type if `Middle` was. Which came first went by the order of the files. - `Object.getOwnPropertyDescriptor(require("node:_http_agent"), "globalAgent")` showed a getter and a setter. It is a value made at load, as in plain Bun. - `export { default as t } from "./outside.txt" with { type: "toml" }`, for a file that is not in the program, lost its type, while `import` kept it. - In a `.mjs` or `.mts` file `this` at the top is `undefined`. The name alone decides: a `.cjs` file in a package with `"type": "module"` still exports through `this`. - A stack trace taken while `then` is read by `await thenable` listed the async function, which has suspended by then. A class whose decorated members need a constructor was shown as `new Task (unknown:1:11)`, not at its `class`. `bun test --aot`: - `vi.mock()` and `jest.mock()` are hoisted above the imports, as oven-sh#44833 does for plain `bun test`. With `import { captured } from "./side"; vi.mock("./mod", ..)`, where side.ts imports mod.ts, mod.ts ran and `captured` was the real value. - `require()` of a mocked module answers the object that the factory gave, for `node:` names too. - `mock.module("node:dns", f1); await import("node:dns"); mock.module("node:dns", f2); await import("node:dns")` answered the first mock again where a run is built in layers. Five mocks in a row gave 1, 1, 3, 4, 5. - A compiled run of one empty test held 166 functions of the test runner's own code. It holds 2 again. `process.mainModule` follows the test file. - A file of the project that writes on a built-in (`Buffer.isEncoding = ..`) now throws in the test run as it does in the executable. A green run said nothing about that line. - A file that only Workers load has no `expect` or `test`, as in plain Bun. - `// require("./x.cjs")` in a comment no longer decides whether x.cjs runs as CommonJS. - A test that writes state.js and imports it afterwards gets the new text in more forms: a path from `process.cwd()`, and a helper in another file that takes the path or joins a name to its own folder. - A type imported from a module in a layer below no longer costs the importing file its types. - `--shard` with `--timings` spreads the files that are held apart for their memory over the shards, and the walk ahead of the deal asks for each name of a folder once. Build time and memory: - What a file uses of the library's functions was kept in four sets of 131,131 bits, each cleared and walked for every file. They are two sorted lists now. The two blocks of a module that is set aside are compressed in one go, not one by one. - `class C1 extends C0 { m1() {} }`, 800 in a row: every inherited method was compared with itself in the class above, 380 of 540 MB. - A build that is not a test run walked every expression of every file for writes on built-ins and threw the result away. Speed: - A sloppy function that only reads `arguments` and has a closure over a parameter (Babel's `_objectSpread`) no longer makes the arguments object at every call. - `Array.from(map)` and `Array.from(map.entries())`: 672 instructions an entry, now 132. - 65 host functions have a signature that says what they answer. Without one, a call counted as making an instance of every host class. Tests: - `heap-limit`: each of the eleven cases that use up the address space runs twenty times at each limit, and one run of the twenty may end in a `malloc` that is not allowed to fail. Plain Bun dies there every time. - The stored programs that print what the collector lets go are each built alone. What an executable with many entry points copies into what goes by everything it holds, so two of them failed in CI by the company they were dealt. Both are stored with that company. - The test of how a run is cut to fit its memory is skipped under sanitizers, like its eleven siblings. - A stepped idiom fails when it reads more than 2% and more than 2 instructions over its pin. It was held exactly. Two pins that had been raised go back to where they were. - A stepped row can be marked `"todo"`: it is held to a higher number, and fails once its pins are met again. Two `EventEmitter.emit` idioms carry it, and `"owed"` in the counted pins: the fix for the compiler's abort costs them five times their instructions until the narrower fix is in the engine. - Two rows of the flow-schedule test are restated to what they measure. - Six stored programs, `pass-conditions/carried-word-*`, each built alone: a loop adds to `holder.n`, stores to `other.n` once, and under `holder instanceof Other` calls a function, a method or a setter that writes `n` too, with `holder` and `other` one object. B3's new pass promoteLoopMemory lost the store. The engine has the pass switched off for now, and these guard its return. - Three more counted idioms are marked `"owed"`. Their pins stay where they are.
Fixes for
bun build --compile --aotandbun test --aotfound since #967. The wrong answers and the crashes come first. 320 files, +8,875 −918; all but 21 of them are underSource/JavaScriptCore/aot.Wrong answers of compiled programs
undefinedin it, iterated by two loops. The first addsthe numbers up, the second counts the
undefineds: it counted 0 instead of 1.yield*and used after it was wiped when aparameter that held an object was set to
nullorundefinedin the same loop: 12instead of 22. A second program never ended.
RegExpmade from text out of a list was compiled from the text the build hadpredicted, even when the list was read under two names in one step and then changed.
main.mjsimportsm.mjs, which importsx.cjs, which callsrequire()onm.mjsunder a name made at run time:
main.mjsran afterx.cjshad ended, not before therequire()returned.import()made while otherimport()calls were waiting, and whose graph shares amodule with one of them, was held back behind all of them.
bun = require("bun"); copy = {}; copy[key] = bun[key]; copy.plugin(options): a membercopied off
require("bun")under a computed key is followed to its call. The build nowsees that
copy.pluginmay beBun.plugin.Crashes of compiled programs
object, after the field was deleted while a Proxy was on the class's prototype chain
and was defined again through the trap.
of 2^30 two-byte characters that cannot be flattened, ahead of a short loop in a
trywith
catchandfinally.TypeError.prototype.list = [1, 2], thencatch (e) { put(e.constructor.prototype, { length: 3 }) }withput(where, what) { where.list = what }, thenTypeError.prototype.list.lengthread ina function: the read took the list for what the build had seen stored by name. The same
with
Bun.S3Client.prototype.file.list = [1, 2]; Bun.s3.file.list = { length: 3 }.Where no list was involved, the read answered
undefined. Both stores are now seen bya read that names the object.
Builds that failed or never ended
swap in a
try, an xor in itsfinally. B3'sValue::returnsBool()recursed into bothoperands without remembering what it had asked, so its cost was exponential.
first out.
for (;;) { if (source.take === never) return ..; const got = source.take(..); .. }. A loop that reads the same property of an unchanging object onevery iteration gets a version that asks ahead of the loop, and a type test on that read
was turned into a cast of the copy. Where both sides of the test's branch leave the
loop, the branch still hands on what the test proved, so its condition has to stay a
test.
InvalidGuard: a read of a typed array at thetop of an iteration relied on the bounds check at the end of the previous one, and the
loop's counter was then replaced by a constant.
allocators, two in the ahead-of-time compiler's own passes (one is flow analysis), and the
lookup of a regular expression's symbols (arm64: twice as many alternatives cost 9.5
times as much, now 1.74 times).
bun test --aot, following oven-sh/bun#44833importthat reaches a mock whose factory threw gets the error that the loaderkept.
require()of a mocked module returns what the factory returned. A later mockof a CommonJS file doesn't change what an earlier
require()returned.import { test, vi } from "vitest"; test("t", () => { vi.useFakeTimers(); })ended inabort(). The code that throws "was not compiled into this executable" looked thefunction up in the table of built-ins, and a function that the table leaves out has no
record there. The error is thrown now; making the call work follows.
Speed
a[k] += v[j]and
o.total += xreload on every iteration what the previous one stored; the pass readsthe word once ahead of the loop and carries it in a Phi, and the stores stay where they
are. The first commit here had the ahead-of-time compiler's policy turn it on, and the
last one takes that back: with
holder.n = (holder.n + 1) | 0; if (j === 1) other.n = 50; if (holder instanceof Other) holder.bump();in a loop andholderandotheroneobject, the store of 50 was lost. It never reached
main. The pass comes back with itsfix in the next engine PR.
map.has(k)followed bymap.get(k)searches once. Keys that are objects are searchedinline in loops.
__awaiter: 3,876 to 1,441 per iteration.Reflect.constructbehind tests, a built-inbound and called in one function, descriptors read and defined again,
for..incopies.faulting load on arm64, macOS x64 and Windows arm64 too.
Known costs, fixed in the next engine PR
everyday-emitters/five-argumentsandthree-arguments(emit()in a loop) cost fivetimes their instructions, 4.8 to 25 per iteration. Only the condition of a branch that
stays in the loop has to stay a test. The narrower form is written and tested.
asks. The first
gen instanceof X,Object.getPrototypeOf(gen),console.log(gen)orread of the function's own
.prototypeconverts that function's live generators by onewalk over the whole heap, once per generator function: about 6 ms with 27 MB of live
heap and 30 to 45 ms with 105 MB (
heapUsed, on a busy machine). Compiledfor..of,spread, destructuring,
.next()andJSON.stringify(gen)don't reach it. The fix givesgenerators a subspace of their own, so the walk follows their number.
NDEBUGnot defined, so GC validation is on),bun test --aotassertswhere a write to a locked built-in is refused with a host function on the stack, e.g.
Reflect.set(Buffer, "isEncoding", f). The check that decides whose file wrote it takesa stack trace without an owner cell, and the write barrier of a frame asserts that it has
one. Release and ASAN builds raise the read-only error as intended. The same check walks
the stack up to three times per refused write. The fix walks once and builds no frames.
Not AOT-specific
Twenty-one files outside
Source/JavaScriptCore/aot. The new pass is off: no policy turns it on yet. The changes inB3Value.cppand Air's allocators are not behind a switch: the JIT runs them too. The one inB3LowerToAir.cppis behindselection.extendsInstructionSelection, which is off by default.A new B3 pass, off by default, and nothing turns it on yet.
b3/B3PromoteLoopMemory.{h,cpp},Sources.txt: the pass. It keeps a word that a loop reads and writes at one address in a value across iterations.b3/B3Generate.cpp: the pass is one more entry in the pipeline's table. It runs only where the procedure has loops and its policy saysloops.promotesMemory.b3/B3Policy.{h,cpp}: that switch,falseby default, and a second one that only a test of the pass sets. 43 switches instead of 42.b3/B3CompilationReport.h: four more counters, 19 instead of 15.b3/testb3_bun_promote_loop_memory.cpp(new),b3/testb3_bun.{h,cpp},b3/testb3_bun_policy.cpp,b3/testb3_bun_random_program.h,shell/CMakeLists.txt: its tests.testPipelinePromotesLoopMemoryOnlyWhereAskedholds that the default pipeline does not run it.Compile time. The output is the same: the machine code of 1,155,808 functions out of 342 programs, compared before and after the register allocator's changes, is identical.
b3/B3Value.cpp,b3/testb3_bun_analyses.cpp:Value::returnsBool()asked both operands ofBitAnd,BitOr,BitXorandSelectagain and again. It now remembers what it has asked. Same answers; the test builds the chain that took exponential time.b3/air/AirAllocateRegistersByGreedy.cpp,b3/air/AirLimits.h: in a function with hundreds of calls, choosing a register to split a value around calls no longer walks every call in the value's live range once per register. From 256 clobber points on, for a value that no clobbering instruction names, a register is skipped when prefix sums of the least each call can cost already exceed the best split found, and a walk stops after 16 sites if the register is held by something that can't be evicted. The calls that clobber a whole set of registers are listed once per set and found by binary search.b3/air/AirAllocateStackByGraphColoring.cpp,WTF/wtf/InterferenceGraph.h: two quadratic loops in the stack allocators of functions with thousands of slots. Where ranges of a slot are held against a cell, each side now skips what lies ahead of the other's next range. At a move between two slots the live set is added in one step, by a new overloadaddAllBut(u, alsoNot, others). Every slot gets the cell it got before.One instruction on arm64, behind a switch that only the ahead-of-time compiler's policy sets.
b3/B3LowerToAir.cpp: an Upsilon that hands the constant 0 to a Phi that is kept zero-extended isMove32 %xzr, %tmpinstead ofMove %xzr, %tmp. It is the same machine instruction, but Air now sees a 32-bit definition, so theMove32that widened a loop's counter goes away:for (let i = 0; i < count; i++) sum += i * 2 + 1takes 5 instructions per iteration instead of 6. The set of such Phis is filled only where the procedure's policy saysselection.extendsInstructionSelection(falseinB3Policy.h), so the code the JIT tiers emit does not change.Under
ENABLE(AOT).runtime/Structure.cpp: four lines invalidateFlags(), which exists only in builds with assertions. AStructurewhose class setsOverridesGetPrototypeitself is no longer held to what its method table says, so the assertion does not fire for the generator functions that get their prototype object only when somebody asks for it.Testing
testb3whole (219,069 checks) on the text in which the new pass landed.--aotand compared with plain Bun: 11 of 11..irfiles underaot/testshere, and programs undertest/aotin the Bun pull request that follows from the same snapshot.main52 fail.vitestprogram: exit 1 with the error; one commit earlier, exit 134.test/aot/parallelin the Bun pull request, each compiled and compared with plain Bun: 8,688 pass, 12 skip themselves, and 2 importbun:internal-for-testing, which this build refuses. The IR tests underaot/testson the head: 5,772 pass, 0 fail. The head links, and it compiles for Linux, macOS and Windows.aot/, compiled with-fsyntax-only -mms-bitfields: everystatic_asserton the size of a struct holds under the Microsoft bit-field layout too. 0 errors.