Repository navigation
AOT: fix wrong answers in Object.assign, folded sums, text requires, private RegExps and loading - #1084
AOT: fix wrong answers in Object.assign, folded sums, text requires, private RegExps and loading#1084Jarred-Sumner wants to merge 20 commits into
Conversation
|
Preview build of bfa5513: |
There was a problem hiding this comment.
I reviewed the four commits pushed since my last pass (the require()/text-form changes in AOTModules.cpp and AOTModuleEvaluation.cpp, and the RegExp escape change) and found no bugs; because the require() resolution rules in formThatRequireFinds and operationAOTRequireIfImported encode loader semantics that reading alone cannot confirm and the tests live in the Bun repo, a human look at those two paths would still be worthwhile.
What was reviewed:
- Folded-sum bailout: cursor is the butterfly at that point (same load as loadCursorAndEnd), registers.value is dead until the first loadValue, and (2^22-1) summands of |x| <= 2^31 plus an int32 initial value stay within +/-2^53; generateFoldFieldPairs only adds 32-bit overflow-checked pair results, so the unconditional bailout there is sound. The name is unique across aot/ for unified sources.
- operationAOTRequireIfImported: fileOfForm already gates on the form's kind being Data, so the only behavioral differences from the old reverseFind path are the intended ones (non-Data file now returns undefined; Data file without NamespaceIfImported still falls through to require).
- AOTHeldRegExps: both changes only add escapes under m_literalsOfFields, so the analysis becomes strictly more conservative.
- Not run: Tools/Scripts/aot/check-unified-sources.py and find-structures-decoded-behind-calls.py (no Structure* is decoded in the diff).
Extended reasoning...
The PR is 37 insertions across six files in the AOT compiler: a FieldIsOnlyAsked observation in LinkFlow::storesCopiedNamesOnPrototype, a JS_EXPORT_PRIVATE on hasGetterToCall, extra RegExp escapes for NewClosure/CallDirect under m_literalsOfFields, two "?text" rules in formThatRequireFinds, a fileOfForm-based rewrite in operationAOTRequireIfImported, and a publicLength bailout in the sum stubs. It touches no security-sensitive surface (no auth, injection, or data exposure). Four of the seven commits landed after the prior review and were not covered by it. Approve was not chosen because the module require() resolution semantics depend on Bun loader behavior that cannot be settled by reading and the covering tests are in a separate repository; defer rather than silence because the prior review covered only the first three commits.
There was a problem hiding this comment.
Beyond the inline finding, I also checked: the new loadedIfExists(*form)->wasImportedWhenRequired dereference in textRequiredInPlaceOf is guarded, since wasRequired(*form) short-circuits first and is only true when a LoadedModule exists; inSparseIndexingMode() is a plain JSObject member, so the now un-ifdef'd Object.assign bail-out compiles in the non-AOT lanes; and bailOutIfSumCouldRound is unique across aot/ and only clobbers registers.value before the fold loop, where it is free.
One note: the new stress/bun-object-assign-keeps-indexed-property-attributes.js is not listed in the fork-owned test table in JSTests/BUN-TEST-DIFFERENCES.md, unlike its sibling setter test. Not done here: running the new stress test (against a build with and without the fix, and the 200ms budget), and the optimized-lane Structure* script.
Extended reasoning...
The change spans twelve small commits across the AOT compiler (module/require resolution, JIT fold stubs, RegExp escape analysis, mention gathering, generator arguments) plus one engine-wide Object.assign fast-path bail-out with a new stress test. It touches no auth, injection, or data-exposure surface. One confirmed finding is posted inline on the new wasImportedWhenRequired bookkeeping; this note only records the other concrete concerns ruled out by reading and the checks that were not run.
| if (targetData.requireResult == RequireResult::NamespaceIfImported && !loaded.requiredValue) [[unlikely]] | ||
| loaded.wasImportedWhenRequired = hasBeenImported(target); |
There was a problem hiding this comment.
🔴 A program that host-requires a text form twice around an import of it can get a later untyped import() running the file as code, not its text module. wasImportedWhenRequired at AOTModuleEvaluation.cpp:5553-5554 is reassigned on every require while requiredValue is empty. A RequiredBy::Program require with isAnsweredAsByLoader never sets requiredValue (5635-5636), so a second host require after an import flips the flag to true. textRequiredInPlaceOf (AOTModules.cpp:4797) then treats the form as imported first. Fix: record the flag once at the form's first require, e.g. only when the LoadedModule is new or when neither requiredValue nor wasRequiredByHost is set, so loader-answered requires keep the first state too.
Why this was flagged
Bun's host calls requireModule (AOTModules.cpp:8948) with RequiredBy::Program for a Data form x?text whose data has requireResult NamespaceIfImported and loaderReturnsNamespace(). At AOTModuleEvaluation.cpp:5553-5554 loaded.requiredValue is empty, so wasImportedWhenRequired = hasBeenImported(target) = false. isAnsweredAsByLoader is true at 5555, so 5635-5636 skip storing requiredValue; 5638 sets wasRequiredByHost. An import statement or import() of x?text then makes hasBeenImported(target) true. A second host require reaches 5553 again with requiredValue still empty and overwrites the flag with true. A later import() of ./x without a type calls textRequiredInPlaceOf (AOTModules.cpp:4780): sharesEntry and wasRequired(*form) hold, but the new !loadedIfExists(*form)->wasImportedWhenRequired test at 4797 fails, so it returns module and the code file runs, where the base branch returned *form (the text module) whenever the form had been required.
Verification: At AOTModuleEvaluation.cpp:5553-5554 the !loaded.requiredValue guard is the only thing meant to make this a one-time snapshot. For host requires requiredValue is never stored (5635-5636) and the flag is recomputed on every host require. At AOTModules.cpp:4797, after one host require it returns the ?text form; after the second it returns module and start(module) runs the code file.
…een imported" Only a require() whose computed name spells the query reaches this twice without storing a value, and that require() answers wrongly by itself. With the change, a later import() of the path got the text in programs where it used to run the code as bun run does.
There was a problem hiding this comment.
Beyond the inline finding, I also checked two other parts of this push and ruled them out: the switch from the sparse-map test to inSparseIndexingMode() in ObjectConstructorInlines.h is sound because every putDirectIndex with attributes goes through ensureArrayStorageExistsAndEnterDictionaryIndexingMode, so an element with attributes always implies sparse mode; and the (1 << 22) - 1 summand bound in AOTStubsArrayCallbacks.cpp is safe because each step's int32 contribution is overflow-checked, keeping the 64-bit sum below 2 ** 53, and registers.cursor is the butterfly when the public length is read.
Extended reasoning...
The push touches the Object.assign fast path in plain JSC, AOT module loading for typed text/file requires, AOT RegExp privacy analysis, two AOT stub folds and a new JSTest; it touches no auth, crypto or injection surface. The two additional checks named in the body were made by reading the code and are not covered by the inline finding.
Findings marked 🟡 are optional suggestions and need no follow-up push.
Still open from earlier reviews (1):
- Unresolved: 1 blocking on lines changed since (possibly already fixed).
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.
There was a problem hiding this comment.
I reviewed the latest revision and didn't find any bugs; because the loader-semantics changes in AOTModules/AOTModuleEvaluation and the JIT stub change are intricate, a human look would still be worthwhile.
What was reviewed:
- The new
m_innermostRequireRootgate (set only forSynchronousEvaluationReason::Require) addresses the deferred-import sharing concern from the previous pass; arequire()under a type at AOTModuleEvaluation.cpp:577 now loads its own data while a deferred import keeps sharing the earlier import's entry. - Object.assign:
inSparseIndexingMode()is a sound replacement for the removedmayHaveElementsWithAttributes— everyputDirectIndexwith attributes goes throughensureArrayStorageExistsAndEnterDictionaryIndexingMode, which sets sparse mode, so attributed elements always land there. - Sum bailout: at most 2^22 summands (length plus the initial value) of |int32| < 2^31 keeps every partial sum within ±2^53,
registers.valueis free as scratch at that point, and field-pair products are int32 (overflow bails) so the same bound holds ingenerateFoldFieldPairs. - Not run: the new JSTest and a build.
Extended reasoning...
The change spans 14 files: AOT module loading semantics (require/import interplay for ?text/?file forms, import.meta.resolve query handling), a length-bound bailout in the array fold JIT stubs, link-layout and RegExp escape analysis tweaks, a private-name attribute change on class prototypes, and a plain-JSC Object.assign fix that routes targets in sparse indexing mode to the slow path, with a new JSTest. It touches no auth, injection or data-exposure surface. The latest commit addressed the inline finding from the previous pass and the bug hunt ran dry with no new findings, but the module-loading state machine and JIT stub edits are complex enough that approval without a human look is not warranted.
Still open from earlier reviews (1):
- Unresolved: 1 blocking on lines changed since (possibly already fixed).
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.
Wrong answers in programs compiled ahead of time.
Object.assignonto a prototype skipped a getter. Where the prototype refused the name, the source's getter was never run. Copying now counts as a read of every source value.reduce. The fold keeps a 64-bit sum, which is exact only while no step can pass 2 ** 53. Longer lists (over 2 ** 22 - 1 elements) now take the method.Bun.deepEqualsof objects that have accessors. This exports the check that the Bun side needs to take the generic path.require()beside text imports. A file imported as text and then required runs as code, and arequire()with type text finds the text's module or the loaded code. One case stays: the keys of a required file import still list__esModule.import()of data that an ES module required gets its own object, and animport()runs code whose text was imported before it answered arequire().Object.assign.import.meta.resolvekeeps a query as written, arequire()under a type loads its own data beside a pendingimport(), and a source's static names keep their place when copied onto compiled objects.Object.assignin plain JSC too. A store onto an element with attributes (non-enumerable, or not configurable) keeps them.Not run: ARM64, CI's suites. The tests come with the Bun change that pins this, except the
Object.assignJSTest, which is here.