Skip to content

Fix null butterfly dereference in DFG graph dump - #28562

Closed
robobun wants to merge 4 commits into
mainfrom
farm/13c4c691/fix-null-butterfly-dump
Closed

robobun wants to merge 4 commits into
mainfrom
farm/13c4c691/fix-null-butterfly-dump

Conversation

@robobun

@robobun robobun commented Mar 25, 2026

Copy link
Copy Markdown
Collaborator

What

Fix undefined behavior in JSCJSValue::dumpInContextAssumingStructure where butterfly()->base(structure) is called without checking if butterfly() returns null.

Why

When the DFG compiler dumps its graph (e.g. during validation failures), it prints JSConstant nodes that reference heap objects. For objects with only inline properties (no indexed storage, no out-of-line properties), butterfly() returns nullptr. The dump code unconditionally dereferences this null pointer to call ->base(structure), triggering undefined behavior caught by UBSAN:

Butterfly.h:182:21: runtime error: member call on null pointer of type 'JSC::Butterfly *'

The call chain is:

DFGGraph::dump → FrozenValue::dumpInContext → JSValue::dumpInContextAssumingStructure
  → butterfly()->base(structure)  // butterfly() is null
    → indexingHeader()            // this is null → UB

Fix

Add a null check on the butterfly pointer before calling ->base(). When null, just print the pointer value without the base offset.

This is an upstream WebKit fix (the patch is in patches/webkit/). The change is in vendor/WebKit/Source/JavaScriptCore/runtime/JSCJSValue.cpp.

Fuzzilli fingerprint: Butterfly.h:182:21:member call on null pointer of type 'JSC::Butterfl

@robobun

robobun commented Mar 25, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 11:25 AM PT - Mar 25th, 2026

❌ @robobun, your commit e81788e has 3 failures in Build #41952 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 28562

That installs a local version of the PR into your bun-28562 executable, so you can run:

bun-28562 --bun

@coderabbitai

coderabbitai Bot commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@robobun has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 5 minutes and 9 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 22e0cebe-9ba0-40aa-9099-2ebc9e3911f6

📥 Commits

Reviewing files that changed from the base of the PR and between add1be46c573bde2465254ff0c21dfef2f69fd60 and e81788e.

📒 Files selected for processing (2)
  • patches/webkit/fix-null-butterfly-dump.patch
  • test/js/bun/jsc/dfg-null-butterfly-dump.test.ts

Walkthrough

Patch updates JavaScriptCore’s dump logic to avoid dereferencing a null butterfly pointer and adds a regression test that exercises TypedArray-backed objects to ensure the dump handles null butterflies without crashing.

Changes

Cohort / File(s) Summary
Null Butterfly Dump Fix
patches/webkit/fix-null-butterfly-dump.patch
Modify JSValue::dumpInContextAssumingStructure to cache the object's butterfly pointer and only access/print the butterfly base when the pointer is non-null; otherwise print the pointer without dereferencing.
Regression Test
test/js/bun/jsc/dfg-null-butterfly-dump.test.ts
Add a test that spawns a Bun process running code which creates TypedArray-backed objects (including SharedArrayBuffer growth paths) to trigger null-butterfly scenarios and asserts the child process prints ok and exits with code 0.
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and accurately describes the main change: fixing null butterfly dereference in DFG graph dump, which matches the core issue addressed in the PR.
Description check ✅ Passed The description includes both required sections: 'What' explains the fix with technical details, and 'Why' provides comprehensive context including code chains, error messages, and UBSAN details, though 'How did you verify your code works?' is missing.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@test/js/bun/jsc/dfg-null-butterfly-dump.test.ts`:
- Around line 36-37: Remove the two assertions that check stderr for absence of
runtime strings: delete the lines containing expect(stderr).not.toContain("null
pointer") and expect(stderr).not.toContain("runtime error") from the test
(dfg-null-butterfly-dump.test.ts) so the test no longer asserts absence of
panic/runtime text; leave the remaining stderr expectations intact.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: dd0a10d9-3518-4351-a214-a60179fd5aee

📥 Commits

Reviewing files that changed from the base of the PR and between 1879e9e and 2e6013d83db44e1ceb5a9edc8aa3adbb13ccab52.

📒 Files selected for processing (2)
  • patches/webkit/fix-null-butterfly-dump.patch
  • test/js/bun/jsc/dfg-null-butterfly-dump.test.ts

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — straightforward null-guard fix in debug/dump-only code path with a regression test.

Extended reasoning...

Overview

This PR adds a WebKit patch (patches/webkit/fix-null-butterfly-dump.patch) that guards a null pointer dereference in JSValue::dumpInContextAssumingStructure, and a regression test in test/js/bun/jsc/dfg-null-butterfly-dump.test.ts. Only two files are changed.

Security Risks

None. The affected code path is the DFG graph dump/debug printer, which runs during validation failures and diagnostic output — not in any production execution path. There is no user-controlled input reaching this code.

Level of Scrutiny

Low. The fix is a one-liner null check added to a diagnostic print function. The logic is self-evident: butterfly() may return nullptr for objects with only inline properties, and the fix avoids dereferencing it. The change is explicitly an upstream WebKit fix applied via the patches mechanism.

Other Factors

No bugs were found by the automated hunting system. The regression test exercises the exact scenario described (DFG validation with objects that have null butterflies) and checks for absence of UBSAN output and a clean exit code. The change is well-scoped and carries no design decisions requiring human judgment.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The C++ patch is correct, but please address the outstanding reviewer comment about the test — the expect(stderr).not.toContain(...) assertions violate the project guideline against checking for absence of panic/runtime-error strings in test output.

Extended reasoning...

Overview

This PR adds a WebKit patch (patches/webkit/fix-null-butterfly-dump.patch) that fixes a null butterfly dereference in JSValue::dumpInContextAssumingStructure, and a regression test at test/js/bun/jsc/dfg-null-butterfly-dump.test.ts.

The Patch

The fix is minimal and clearly correct: cache the butterfly() result, guard with a null check, and only print base= when non-null. This is exactly the right approach for a UBSAN-detected null-dereference in a debug/dump path. No security concerns; no logic changes to production code paths.

The Test

The test has an outstanding issue from coderabbitai (unaddressed by the author): lines 36–37 assert expect(stderr).not.toContain("null pointer") and expect(stderr).not.toContain("runtime error"). Per the project coding guidelines, tests must never check for the absence of panic/runtime-error strings — these assertions will never catch failures in normal CI builds because UBSAN output requires special sanitizer build flags. This is a genuine pattern violation, not a style nit.

Level of Scrutiny

The fix itself is low risk and routine. I would normally approve it. However, there is an open reviewer comment that the author has not yet addressed, and the test pattern violation is a real concern per project guidelines.

Other Factors

The build failures mentioned by robobun reference commit 7c289b8, which is the prior commit on master — they are unrelated to this PR. There are no security-sensitive code paths involved.

robobun and others added 4 commits March 25, 2026 16:42
JSCJSValue::dumpInContextAssumingStructure unconditionally dereferences
the return value of butterfly() when printing JSObject constants.
Objects with only inline properties have a null butterfly, causing
undefined behavior when the DFG graph dump is triggered (e.g. during
validation failures).

Add a null check before calling butterfly->base(structure).
@robobun
robobun force-pushed the farm/13c4c691/fix-null-butterfly-dump branch from bd5007c to e81788e Compare March 25, 2026 16:42

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — the stderr pipe deadlock concern from my previous review has been addressed, and the null butterfly fix is correct.

Extended reasoning...

Overview

This PR adds a WebKit patch (patches/webkit/fix-null-butterfly-dump.patch) that fixes a null pointer dereference in JSValue::dumpInContextAssumingStructure, and a regression test (test/js/bun/jsc/dfg-null-butterfly-dump.test.ts). The patch is minimal: cache the butterfly pointer, null-check before dereferencing, and fall through to printing without the base offset if null.

Security risks

None. This is a diagnostic/dump code path only reached during DFG graph validation. No auth, crypto, or permission code is touched.

Level of scrutiny

Low. The fix is a textbook null guard on a single pointer dereference in debug/dump output code. The patch matches the description exactly. The test correctly uses stderr: "ignore", asserts stdout === "ok\n", and checks exitCode === 0.

Other factors

My previous review flagged a potential pipe deadlock (the test had stderr: "pipe" but never consumed it). That was addressed in commit bd5007c. The current diff already reflects stderr: "ignore". No further issues remain. No bugs were found by the automated bug hunting system.

Comment on lines +1 to +12
--- a/Source/JavaScriptCore/runtime/JSCJSValue.cpp
+++ b/Source/JavaScriptCore/runtime/JSCJSValue.cpp
@@ -311,7 +311,11 @@ void JSValue::dumpInContextAssumingStructure(PrintStream& out, DumpContext* cont
else if (structure->classInfoForCells()->isSubClassOf(JSObject::info())) {
out.print("Object: ", RawPointer(asCell()));
- out.print(" with butterfly ", RawPointer(asObject(asCell())->butterfly()), "(base=", RawPointer(asObject(asCell())->butterfly()->base(structure)), ")");
+ auto* butterfly = asObject(asCell())->butterfly();
+ if (butterfly)
+ out.print(" with butterfly ", RawPointer(butterfly), "(base=", RawPointer(butterfly->base(structure)), ")");
+ else
+ out.print(" with butterfly ", RawPointer(butterfly));
out.print(" (Structure ", inContext(*structure, context), ")");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔴 The patch patches/webkit/fix-null-butterfly-dump.patch is never applied by the build system — it exists only as documentation. WebKit is integrated as a prebuilt binary via SetupWebKit.cmake, which does not invoke GitClone.cmake's patch-application logic used by all other vendored libraries. Additionally, WEBKIT_VERSION in SetupWebKit.cmake was last updated three days before this PR was opened, meaning the current prebuilt binary cannot contain this fix. To actually ship the fix, it must be committed to the oven-sh/WebKit fork, new prebuilt binaries must be built, and WEBKIT_VERSION must be updated.

Extended reasoning...

How the build system applies patches

Every vendored C/C++ library in the Bun repo that has patches under patches/<name>/ uses register_repository() in its cmake file (e.g., highway, libarchive, libuv, lshpack, brotli, zlib). register_repository() calls into cmake/scripts/GitClone.cmake, which — at line 44 — iterates over patches/${GIT_NAME}/ and applies each .patch file to the cloned source. This is the one and only mechanism for applying patches in this repo.

Why WebKit is different — and why that breaks the fix

WebKit is not cloned via register_repository(). Instead, cmake/tools/SetupWebKit.cmake handles it via two completely separate code paths: (a) the default path downloads a prebuilt binary tarball from oven-sh/WebKit GitHub releases; (b) the WEBKIT_LOCAL=1 path configures cmake from vendor/WebKit source. Neither path invokes GitClone.cmake or reads from patches/webkit/. No other script in the repository references patches/webkit/ — the directory was created fresh by this PR with no prior precedent.

The WEBKIT_VERSION timestamp seals it

The WEBKIT_VERSION hash (fc9f2fa7272fec64905df6a9c78e15d7912f14ca) in SetupWebKit.cmake was set by PR #28371, merged on March 22, 2026. This PR was opened on March 25, 2026 — three days later. The prebuilt binary that Bun downloads during a normal build was therefore produced before this fix was written. Even if the patch were applied to the oven-sh/WebKit fork today, the binary pointed to by the current WEBKIT_VERSION would still be unpatched.

Addressing the refutation

One verifier argued this is intentional — patches/webkit/ is documentation of upstream changes made to the fork, and WEBKIT_VERSION might already point to a commit containing the fix. This does not hold up:

  1. CONTRIBUTING.md explicitly states: "if you make changes to our WebKit fork (oven-sh/webkit), you will also have to change SetupWebKit.cmake to point to the commit hash." This PR does not update WEBKIT_VERSION.
  2. The three-day timestamp gap is dispositive: the current prebuilt binary was produced before this fix was written, so it cannot contain the fix.
  3. No established pattern: patches/webkit/ is newly created by this PR; there is no repo history of it serving as a documentation-only changelog.

Impact

The PR title and description claim to fix a UBSAN null-pointer dereference (butterfly()->base(structure) called with null butterfly). Any Bun binary built from this repo will still contain the unfixed WebKit code. The regression test may pass vacuously if the specific JIT path that triggers the null butterfly dump is not exercised on the unpatched binary, masking the incomplete fix.

Step-by-step proof

  1. Developer builds Bun from this PR: cmake runs SetupWebKit.cmake.
  2. SetupWebKit.cmake downloads the prebuilt tarball identified by WEBKIT_VERSION = fc9f2fa7272fec64905df6a9c78e15d7912f14ca (built March 22, 2026).
  3. No code path reads patches/webkit/fix-null-butterfly-dump.patch or applies it.
  4. The resulting libJavaScriptCore in the tarball still contains the original butterfly()->base(structure) call with no null check.
  5. The built bun binary still triggers UBSAN when DFG dumps a JSConstant with a null butterfly.

Correct fix

Apply the change in the patch to the oven-sh/WebKit fork, trigger a new prebuilt WebKit build, and update WEBKIT_VERSION in SetupWebKit.cmake to the new commit hash — as CONTRIBUTING.md requires.

@github-actions

Copy link
Copy Markdown
Contributor

Closing this PR because it has been inactive for more than 90 days.

@github-actions github-actions Bot closed this Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant