Skip to content

AOT: fix wrong answers in Object.assign, folded sums, text requires, private RegExps and loading - #1084

Open
Jarred-Sumner wants to merge 20 commits into
mainfrom
claude/aot-wrong-answers-sums-getters
Open

Jarred-Sumner wants to merge 20 commits into
mainfrom
claude/aot-wrong-answers-sums-getters

Conversation

@Jarred-Sumner

@Jarred-Sumner Jarred-Sumner commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Wrong answers in programs compiled ahead of time.

  • Object.assign onto 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.
  • A folded sum of int32 numbers could differ from 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.deepEquals of 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 a require() 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.
  • A RegExp in a private field, compiled through an alias in a closure. The field's literal is no longer treated as private once a closure captures it.
  • Loading. An import() of data that an ES module required gets its own object, and an import() runs code whose text was imported before it answered a require().
  • More of loading and Object.assign. import.meta.resolve keeps a query as written, a require() under a type loads its own data beside a pending import(), and a source's static names keep their place when copied onto compiled objects.
  • Builds. Elements that only a storage test names are placed, and generators keep constant arguments, so two builds no longer stop.
  • Object.assign in plain JSC too. A store onto an element with attributes (non-enumerable, or not configurable) keeps them.
Check (Linux x64) Before After
Programs for the fixes wrong values as plain Bun
138 real programs, instructions 1 1.0021 (geometric mean, median 1.0000)
Programs that differ from plain in the test kit 3 2, none new

Not run: ARM64, CI's suites. The tests come with the Bun change that pins this, except the Object.assign JSTest, which is here.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Preview build of bfa5513: autobuild-preview-pr-1084-bfa55130

@Jarred-Sumner Jarred-Sumner changed the title AOT: fix three wrong answers in Object.assign, folded sums and accessor objects AOT: fix wrong answers in Object.assign, folded sums, text requires and private RegExps Nov 20, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Jarred-Sumner Jarred-Sumner changed the title AOT: fix wrong answers in Object.assign, folded sums, text requires and private RegExps AOT: fix wrong answers in Object.assign, folded sums, text requires, private RegExps and loading Nov 20, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +5553 to +5554
if (targetData.requireResult == RequireResult::NamespaceIfImported && !loaded.requiredValue) [[unlikely]]
loaded.wasImportedWhenRequired = hasBeenImported(target);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 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.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_innermostRequireRoot gate (set only for SynchronousEvaluationReason::Require) addresses the deferred-import sharing concern from the previous pass; a require() 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 removed mayHaveElementsWithAttributes — every putDirectIndex with attributes goes through ensureArrayStorageExistsAndEnterDictionaryIndexingMode, 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.value is free as scratch at that point, and field-pair products are int32 (overflow bails) so the same bound holds in generateFoldFieldPairs.
  • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant