Skip to content

[JSC] Let Bun report a Structure heap reservation that failed - #760

Open
robobun wants to merge 2 commits into
mainfrom
robobun/77588891/report-structure-heap-reservation-failure
Open

robobun wants to merge 2 commits into
mainfrom
robobun/77588891/report-structure-heap-reservation-failure

Conversation

@robobun

@robobun robobun commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Pairs with oven-sh/bun#39967, which defines the function.

Problem

  • StructureMemoryManager::StructureMemoryManager() aborts when it cannot reserve the Structure heap. RELEASE_ASSERT(g_jscConfig.startOfStructureHeap, ...) fires when the OS refuses all 8 sizes. RELEASE_ASSERT(mi_manage_os_memory_ex(...)) fires when mimalloc refuses the range that was reserved.
  • The cause is a limit of the process (ulimit -v, ulimit -d, vm.overcommit_memory=2). In Bun the abort prints "Bun has crashed" and sends a crash report (Sentry BUN-4NF7).

Fix

  • The constructor calls Bun__structureHeapReservationFailed(largestTriedBytes, smallestTriedBytes, reservedBytes) at both failures, when the embedder defines it. Bun prints an error with the sizes and the limits of the process, then exits with status 1.
  • The declaration is weak, like the other Bun hooks. The jsc shell does not define the function, so it keeps both assertions. A definition that returns reaches RELEASE_ASSERT_NOT_REACHED().
  • WebKitCompilerFlags.cmake lists the symbol with the other hooks. The macOS link of jsc needs the -U entry for a symbol that stays undefined.
  • Verified with the bun-webkit-linux-amd64-lto lane of this pull request (2a0f26a). The jsc shell still aborts with status 134 at both assertions. A bun release build with crash_handler: report an exhausted address space during JSC initialization as an error, not a crash bun#39967 prints the error and exits 1 there, at each of 16 ulimit -v and 14 ulimit -d limits where the base aborts.

Background

Downsides

  • In the bun release build (ThinLTO, x64) the constructor grows from 914 to 975 bytes. It runs 96 instructions when the first size fits (95 at the base, one more register move) and 188 in both builds under ulimit -v 400000 (gdb step count, 3 of 3 runs identical).

StructureMemoryManager reserves the Structure heap at startup. When the
OS refuses every size, or mimalloc refuses the range that was reserved,
a RELEASE_ASSERT aborts the process. In Bun the abort shows as a crash
report, but the cause is a limit of the process (ulimit -v, ulimit -d,
vm.overcommit_memory=2).

The constructor now calls Bun__structureHeapReservationFailed with the
sizes it tried, when Bun defines that function. The function does not
return. The JSC shell does not define it and keeps the assertions.

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline findings, I also checked the mappedHeapSize * 2 passed as smallestTriedBytes at StructureAlignedMemoryAllocator.cpp:134 — after the 8-iteration loop exits without a reservation, mappedHeapSize has been halved once past the last size attempted, so the doubling recovers the correct value.

Extended reasoning...

The change adds a weak noreturn Bun hook called at two Structure heap reservation failure points in Source/JavaScriptCore/heap/StructureAlignedMemoryAllocator.cpp and lists the symbol in both linker undefined-symbol lists in Source/cmake/WebKitCompilerFlags.cmake; it touches no security-sensitive surface. Two inline findings were already reported, so this note only records the size-arithmetic check that was ruled out.

Comment thread Source/JavaScriptCore/heap/StructureAlignedMemoryAllocator.cpp Outdated
Comment thread Source/cmake/WebKitCompilerFlags.cmake
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Preview build of 2a0f26a: autobuild-preview-pr-760-2a0f26a3

@coderabbitai

coderabbitai Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Essentials

Run ID: 85641751-b84f-4f6c-8858-5872544fb727

📥 Commits

Reviewing files that changed from the base of the PR and between d524dcc and 2a0f26a.

📒 Files selected for processing (1)
  • Source/JavaScriptCore/heap/StructureAlignedMemoryAllocator.cpp

Included review availability: This review used your included allowance. Your plan provides up to 5 included reviews per hour; 1 remain after this review.


Walkthrough

Bun builds now report structure-heap reservation failures through an optional callback. Apple and non-Apple Bun event-loop linker flags retain the callback symbol.

Changes

Structure-heap failure reporting

Layer / File(s) Summary
Bun structure-heap failure reporting
Source/JavaScriptCore/heap/StructureAlignedMemoryAllocator.cpp, Source/cmake/WebKitCompilerFlags.cmake
The allocator calls the optional Bun callback when reservation attempts fail or mimalloc cannot manage the mapped region. The existing assertions remain. Both Apple and non-Apple Bun event-loop linker flags retain the callback symbol.

Suggested reviewers: mcatanzaro

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 2a0f2

No verified defect requires holding this change. The allocator retains its assertion if the failure-reporting hook returns; normal checks should cover the affected link targets.

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the problem, fix, affected behavior, testing, and performance impact. It does not follow the repository template because it omits a Bugzilla URL, the required review line, and… Add the associated Bugzilla bug URL, include the required “Reviewed by NOBODY (OOPS!).” line or actual reviewer information, and list each changed file with the relevant functions or classes. Ensure the pull request is also linked from Bugz…
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the main change: Bun can report Structure heap reservation failures in JSC.
Full details: Description check

Explanation

The description explains the problem, fix, affected behavior, testing, and performance impact. It does not follow the repository template because it omits a Bugzilla URL, the required review line, and the template-style changed-file and function list.

Resolution

Add the associated Bugzilla bug URL, include the required “Reviewed by NOBODY (OOPS!).” line or actual reviewer information, and list each changed file with the relevant functions or classes. Ensure the pull request is also linked from Bugzilla and has the required labels and assignment if applicable.

  • Fix all pre-merge checks with AI
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.

Warning

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.


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

The declaration is no longer noreturn. A definition that returns now
reaches RELEASE_ASSERT_NOT_REACHED, where before the behavior was
undefined.

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

Still open from earlier reviews (1):

  • Unresolved: 1 minor or pre-existing.

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.

2 participants