Skip to content

JSBigInt: raise maxLengthBits from 1 << 20 to 1 << 30 - #484

Closed
robobun wants to merge 1 commit into
mainfrom
farm/e033e942/bigint-max-length
Closed

robobun wants to merge 1 commit into
mainfrom
farm/e033e942/bigint-max-length

Conversation

@robobun

@robobun robobun commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Problem

JSC caps a BigInt at maxLengthBits = 1 << 20 bits (JSBigInt.h:527). Any operation past 1 million bits throws RangeError: Out of memory: BigInt generated from this operation is too big. V8 allows 1 << 30 bits, so BigInt workloads that run on Node.js and Deno fail on Bun far from any real memory limit. oven-sh/bun#39964 hits this with a 3.5 million bit Fibonacci computation.

Fix

  • Raise maxLengthBits to 1 << 30, the same cap as V8. maxLength becomes 2^24 digits, 128MB per BigInt.
  • The comment above the constant already states the implementation supports maxInt / digitBits digits (about 2^31 bits). I audited every maxLength and maxLengthBits use in JSBigInt.cpp: the four static_asserts hold at 1 << 30, all length arithmetic stays in range (lengths stay under 2^25, length * digitBits stays under 2^31), and both toString paths already guard the result size against JSString::MaxLength.
  • Verified with a debug JSCOnly build: products and shifts across the old cap, the 2^n fast path, asUintN at wide widths, a value at exactly 2^30 bits, and one past it (throws).

Tests

  • big-int-out-of-memory-tests.js, bigint-inc-dec-in-place.js, bigint-exponential-oom.js: scaled to the new boundary, pass.
  • The three bigint-oom-in-codegen tests and eval-huge-big-int-memory-overflow.js build their literals at runtime and move one hex digit past the new cap, marked memoryHog.
  • bigint-oom-import.js imports a checked-in 1.05 million bit literal. A source file cannot reasonably exceed 1 << 30 bits, so it now asserts the import succeeds.
  • Sweep of all 409 BigInt stress tests: no new failures against a baseline build (10 fail on the baseline too, they need harness requireOptions flags).

@coderabbitai

coderabbitai Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

  • Run on-demand review

On-demand reviews are free for the next 27 days. After that, they cost $0.25 per reviewed file.

Or wait 18 minutes for your next included review.

View limit details

Limit details: You’ve used all 5 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 319cb563-a462-4ab0-a93f-f92a7237db14

📥 Commits

Reviewing files that changed from the base of the PR and between c148a12 and 0de7d20.

📒 Files selected for processing (9)
  • JSTests/stress/big-int-out-of-memory-tests.js
  • JSTests/stress/bigint-exponential-oom.js
  • JSTests/stress/bigint-inc-dec-in-place.js
  • JSTests/stress/bigint-oom-import.js
  • JSTests/stress/bigint-oom-in-codegen-array-literal-context.js
  • JSTests/stress/bigint-oom-in-codegen-binary-conditional-context.js
  • JSTests/stress/bigint-oom-in-codegen-conditional-context.js
  • JSTests/stress/eval-huge-big-int-memory-overflow.js
  • Source/JavaScriptCore/runtime/JSBigInt.h

Comment @coderabbitai help to get the list of available commands.

Comment on lines +86 to +88
// maxLength boundary: maxLengthBits = 1 << 30.
{
const maxLengthBits = 1048576n;
const maxLengthBits = 1073741824n;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Three tests scaled to the new 1 << 30 boundary — bigint-inc-dec-in-place.js, bigint-exponential-oom.js, and big-int-out-of-memory-tests.js — should get //@ memoryHog! like the codegen tests did. They now allocate 128MB BigInts (the first holds several live at once), so they'll exceed the 200ms stress-test limit and may OOM on $memoryLimited runners (600MB cap) since without memoryHog! they run non-exclusively and aren't skipped there.

Extended reasoning...

What the issue is

This PR raises JSBigInt::maxLengthBits from 1 << 20 to 1 << 30 and scales the boundary tests accordingly — a 1024× increase from ~128KB BigInts to ~128MB BigInts. The PR correctly adds //@ memoryHog! to the three bigint-oom-in-codegen-*.js tests (which now build ~256MB source strings), but three other tests scaled by the same factor were left without it:

Test Directive before Directive after Now allocates
bigint-inc-dec-in-place.js (none — runs in every config) (none) many 128MB values simultaneously
bigint-exponential-oom.js (none — runs in every config) (none) 32MB and 128MB values
big-int-out-of-memory-tests.js //@ runDefault(...) unchanged 128MB all-ones + intermediates

Since the same PR annotates the codegen tests, this looks like an oversight rather than a deliberate choice.

Why it matters

JSTests/README.md:19 requires stress tests to run in under 200ms in all configurations. Tools/Scripts/run-jsc-stress-tests:967-971 shows that memoryHog! does two things: it calls exclusive! (so the test doesn't run concurrently with other tests competing for RAM) and it skips the test entirely when $memoryLimited is set. On memory-limited runners, JSCTEST_memoryLimit is set to 600MB (line 3296), so a test that holds several 128MB BigInts live at once can hit a real allocator OOM and fail spuriously.

Step-by-step: bigint-inc-dec-in-place.js

This is the worst of the three because it has no //@ directive, so it runs in every stress configuration. Walking through the maxLength block at lines 86–112 with maxLengthBits = 1073741824n:

  1. pow2(maxLengthBits - 1n) → a 128MB BigInt.
  2. ... - 1n, << 1n, | 1n → each produces a fresh 128MB BigInt; the last is bound to max and stays live for the whole block.
  3. shouldBe(dec(max), max - 1n) → two more 128MB values, plus a 16M-digit !== comparison.
  4. shouldBe(inc(max - 1n), max) → two more 128MB values.
  5. shouldBe(inc(-max), -(max - 1n)) → three more 128MB values.
  6. nearMax = max - pow2(maxLengthBits - 1n) → two more 128MB values; nearMax also stays live.
  7. shouldBe(inc(nearMax), nearMax + 1n) → two more.

With max and nearMax pinned by locals plus at least two temporaries per shouldBe, peak live-set is comfortably ≥4×128MB before GC can reclaim intermediates — over 512MB, and this runs in every config in parallel with other tests. On a $memoryLimited runner with a 600MB cap this can OOM outright; on any runner it will take multiple seconds, not <200ms.

bigint-exponential-oom.js is lighter (it materializes 2n ** 0xfffffffn ≈ 32MB and 2n ** 1073741823n = 128MB, each shifted right), but still has no directive so it runs in every config. big-int-out-of-memory-tests.js builds a 128MB all-ones value via (1n << 1073741823n) - 1n then << 1n | 1n (several 128MB intermediates) and only has //@ runDefault, which limits it to one config but doesn't set exclusive! or skip on memory-limited platforms.

Fix

Add //@ memoryHog! to the top of all three files (for big-int-out-of-memory-tests.js, add it alongside the existing //@ runDefault(...)). Alternatively, if you'd rather keep bigint-inc-dec-in-place.js fast in every config, the maxLength-boundary block could be split into its own memoryHog! file so the rest of the test still runs everywhere.

Severity

This is test-suite hygiene — the runtime change itself is correct and the tests pass — so it's a nit, but worth fixing to avoid CI flakiness on memory-limited bots and to keep stress-suite wall-clock in check.

@github-actions

github-actions Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Preview Builds

Commit Release Date
0de7d205 autobuild-preview-pr-484-0de7d205 2026-08-24 11:54:36 UTC
7cc6d891 autobuild-preview-pr-484-7cc6d891 2026-08-23 09:02:16 UTC
6cf2a37b autobuild-preview-pr-484-6cf2a37b 2026-08-21 15:38:31 UTC

@robobun
robobun force-pushed the farm/e033e942/bigint-max-length branch from 6cf2a37 to 7cc6d89 Compare August 23, 2026 07:45
robobun added a commit to oven-sh/bun that referenced this pull request Aug 23, 2026
JSC capped a BigInt at 1 million bits, so workloads that run on Node.js
(V8 allows 1 << 30 bits) failed with RangeError: Out of memory: BigInt
generated from this operation is too big. oven-sh/WebKit#484 raises
maxLengthBits to 1 << 30.

Fixes #39964
JSBigInt::maxLengthBits was 1 million bits. V8 allows 1 << 30 bits, so
BigInt workloads that run on Node.js failed here with RangeError: Out
of memory: BigInt generated from this operation is too big, far from
any real memory limit. The implementation supports maxInt / digitBits
digits, so the constant can go up without code changes: every length
check, the four static_asserts, and both toString paths (which already
guard against JSString::MaxLength) hold at 1 << 30.

Scale the JSTests that encode the old limit. The codegen OOM tests
build their literals at runtime and move one hex digit past the new
cap. bigint-oom-import.js imports a checked-in 1.05 million bit
literal that cannot reasonably grow past 1 << 30 bits, so it now
asserts the import succeeds.
@robobun
robobun force-pushed the farm/e033e942/bigint-max-length branch from 7cc6d89 to 0de7d20 Compare August 24, 2026 11:12
robobun added a commit to oven-sh/bun that referenced this pull request Aug 24, 2026
JSC capped a BigInt at 1 million bits, so workloads that run on Node.js
(V8 allows 1 << 30 bits) failed with RangeError: Out of memory: BigInt
generated from this operation is too big. oven-sh/WebKit#484 raises
maxLengthBits to 1 << 30.

Fixes #39964
@robobun

robobun commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as superseded by #507, which is merged. #507 raises maxLengthBits to 1 << 30 and moves the boundary tests, and it names this PR as superseded. It also ports V8's sub-quadratic multiply, divide, toString and BigInt(string) and adds an interruption check. The higher cap needs those, so raising the constant alone, as this PR does, is not enough.

The bun-side WebKit bump for #507 is oven-sh/bun#40299. Issue oven-sh/bun#39964 is fixed by #507.

@robobun robobun closed this Aug 25, 2026
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