Skip to content

[JSC] bun:ffi: a symbolic breakpoint that matches an FFI function name does not abort - #720

Closed
robobun wants to merge 1 commit into
mainfrom
robobun/5f889c11/ffi-stub-own-construct-jitcode
Closed

robobun wants to merge 1 commit into
mainfrom
robobun/5f889c11/ffi-stub-own-construct-jitcode

Conversation

@robobun

@robobun robobun commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • Debugger.addSymbolicBreakpoint with a symbol that matches a live bun:ffi function name aborts: panic(main thread): abort() called, from the RELEASE_ASSERT at InspectorDebuggerAgent.cpp:1695 (or :1729). VS Code sends it for a function breakpoint.
  • JSFFIFunction::create (ffi/JSFFIFunction.cpp:131) gives the executable of a function with an IC stub the construct JITCode of base, the executable VM::getHostFunction() caches per name and arity. The debugger swaps a JITCode in place once per executable. The second one finds it already swapped.

Fix

  • The stub executable gets its own construct JITCode, made the way JITThunks::hostFunctionStub makes one. create() no longer makes the unused base when there is a stub.
  • Correct because each executable owns both JITCode objects, as didCreateNativeExecutable and ~ReplacedThunk assume.
  • Verified: the new testSymbolicBreakpointOnFFIFunction in ffi/tests/testFFI.cpp aborts at :1695 without the fix. Also all of testFFI and JSTests/stress/ffi-*.js (debug build).
  • Self-reviewed: 4 concerns raised, 3 addressed, 1 in Notes.

Background

  • A NativeExecutable holds one JITCode for call and one for construct. Each wraps the entry point a caller jumps to.
  • For a symbolic breakpoint on a native function, the debugger swaps in a thunk that calls the debugger first, and restores it later.

Downsides

  • One more NativeJITCode (40 bytes) per FFI function with an IC stub.
  • The same abort exists for WebAssembly: WebAssemblyFunction::create (upstream code) shares both JITCode objects. Two live functions named "0" (the function index) or "start" trigger it.
  • A call site linked before the breakpoint was added does not pause, as for every native function. A caller that the DFG or FTL compiles to CallFFI never pauses (no VM::isDebuggerHookInjected() check).
Notes
  • Repro in Bun 1.4.3-canary.1 (367d939d9), release build: dlopen("libz.so.1", { zlibVersion: { args: [], returns: "cstring" } }), keep the symbol, pause on a debugger statement under bun --inspect-wait, send Debugger.enable, then Debugger.addSymbolicBreakpoint with symbol: "zlibVersion". The process exits with SIGABRT before it answers. The debug adapter sends the command at packages/bun-debug-adapter-protocol/src/debugger/adapter.ts:1012 (oven-sh/bun). Web Inspector sends it for a symbolic breakpoint.
  • One function is enough while base is still alive (no GC since dlopen()). Two functions of one name and arity (one library opened twice, or one name in two linkSymbols() calls) always abort, because their executables keep the shared object alive.
  • Which assertion fails depends on the order. Breakpoint added after the functions exist: the second executable's cached arity-check entry no longer equals the entry of the swapped JITCode (:1695). Function made while the breakpoint is set: base is swapped when it is created, then the stub executable swaps the same object again and saves the debugger thunk as the original (:1729). Both reproduce in testFFI without the fix.
  • A function without a stub (hooks, useFFIICStub=0, a buffer_length argument) uses the shared executable from getHostFunction(), as before. The debugger handles that, because it keeps its records per executable.
  • Before this PR create() allocated base for every function with a stub and then dropped it: an 80-byte cell and two NativeJITCode objects, collected at the next GC.
  • The test connects a FrontendChannel to the global object's JSGlobalObjectInspectorController, sends the protocol messages, and resumes each pause from JSGlobalObjectDebugger::runWhilePausedCallback, the hook Bun uses. It has two functions of one name and a third made while the breakpoint is set. It checks that a call of each, and a construct, pauses with reason FunctionCall and returns the right value. It checks that the entry points are back after Debugger.removeSymbolicBreakpoint and after the frontend disconnects. It runs last in testFFI, because a debugger deletes all of the VM's code and a symbolic breakpoint sets VM::notifyDebuggerHookInjected() for the rest of the VM's life.
  • The new test also passes with JSC_useFFIICStub=0, JSC_useDFGJIT=0, JSC_useLLInt=0, JSC_useConcurrentJIT=0, JSC_collectContinuously=1, JSC_forceDebuggerBytecodeGeneration=1, JSC_slowPathAllocsBetweenGCs=5, JSC_forceICFailure=1 and JSC_validateExceptionChecks=1. The stress run was 545 test runs (33 files, every mode of run-jsc-stress-tests).
  • WebAssembly: wasm/js/WebAssemblyFunction.cpp:86 gives every function's executable both JITCode objects of one cached base. The name of an exported function is its function index (JSWebAssemblyInstance.cpp:539), and a start function is named "start" (WebAssemblyModuleRecord.cpp:840). Two instances of one module and a symbolic breakpoint on "0" abort at :1695 in the Bun build above. Upstream WebKit main has the same code. It needs its own change, because each function would need two more allocations.
  • The call-site limit: a call site keeps the entry point it was linked to. Checked in testFFI with an FFI function and with escape: a loop that ran before the breakpoint was added does not pause, a new call site does.
  • The CallFFI limit: for an ordinary host function the DFG and FTL stop making direct native calls once a symbolic breakpoint exists (DFGSpeculativeJIT64.cpp:1027, FTLLowerDFGToB3.cpp:14189). The CallFFI conversion (DFGByteCodeParser.cpp:2656, FFIDFG.cpp:44) has no such check, and CallFFI calls the target directly. So a hot caller of an FFI function stops pausing when it tiers up. Before this PR the process aborted before it got that far. That check needs its own change and test, and it touches code that [JSC] bun:ffi: JSFFIFunction::close() makes a call throw instead of jumping to an unloaded library #714 changes.
  • Self-review: it kept the shape of the fix. It asked this description to state the WebAssembly case as a confirmed abort with the right trigger names, and to state the call-site limit. Both are in. It also probed what the test says is per global object. The report of that concern did not reach me in full, so I acted on the probes: the test comments now say that a debugger deletes all of the VM's code, the test functions have a name that no other test uses, and the test frontend clears the pause callback when it disconnects.
  • CI of this PR ran testFFI with the new case on the Linux x64 and arm64 lto lanes. Bun built against the preview build autobuild-preview-pr-720-d997e370 (debug, ASAN): the dlopen() repro answers {"result":{}}, the next call pauses with reason FunctionCall and data {"name":"zlibVersion"}, and the script exits with code 0. Bump WebKit (oven-sh/WebKit#720 preview): a symbolic breakpoint that matches a bun:ffi function name no longer aborts bun#43813 pins that build and adds an inspector test for both orders.
  • [JSC] bun:ffi: JSFFIFunction::close() makes a call throw instead of jumping to an unloaded library #714 makes the stub an FFI::ICStubCode and does not touch create(). [JSC] FFI: compile C modules lowered to BIR with B3 #653 does not touch it either. The changes do not conflict.

…wn construct JITCode

JSFFIFunction::create() gives a function with an IC stub a NativeExecutable
of its own. That executable took its construct JITCode from the executable
that VM::getHostFunction() caches for the same name and arity. So those two
executables, and the executable of every other function of that name and
arity, shared one JITCode object.

For a symbolic breakpoint the debugger swaps the code of the JITCode objects
of every NativeExecutable whose name matches, in place, once per executable
(InspectorDebuggerAgent::didCreateNativeExecutable). The second executable
found the shared object already swapped and failed
RELEASE_ASSERT(nativeExecutable.generatedJITCodeWithArityCheckFor(kind) ==
jitCode->addressForCall(MustCheckArity)). A function made while the
breakpoint was set failed the RELEASE_ASSERT that follows the swap. So a
function breakpoint in VS Code, or a symbolic breakpoint in Web Inspector,
on the name of a dlopen() symbol aborted the process.

The stub executable now gets a construct JITCode of its own, made the way
JITThunks::hostFunctionStub() makes one. create() no longer asks for the
cached executable when there is a stub, because nothing else used it.

testFFI sends Debugger.addSymbolicBreakpoint through the global object's
inspector controller. It has two functions of one name and a third made
while the breakpoint is set. A call of each, and a construct, pauses and
returns. The entry points are back after the breakpoint is removed and
after the frontend disconnects.
@robobun

robobun commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status

@github-actions

Copy link
Copy Markdown

Preview build of d997e37: autobuild-preview-pr-720-d997e370

@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 finding, I also checked the new construct JITCode against what JITThunks::hostFunctionStub builds (JITThunks.cpp:342) — same NativeJITCode/createSelfManagedCodeRef/HostCallThunk construction, and the stub is only produced when Options::useJIT() holds (FFIICStub.cpp:331), so the ctiNativeConstruct precondition is met. The no-stub fallback still shares getHostFunction()'s cached executable, but that path never shared a JITCode object between two executables, so the debugger's per-executable swap is unaffected there.

Extended reasoning...

The change is confined to JSFFIFunction::create in Source/JavaScriptCore/ffi/JSFFIFunction.cpp (a fresh construct NativeJITCode per stub executable instead of borrowing the cached one) plus a new inspector-driven test in ffi/tests/testFFI.cpp; it touches no security-sensitive surface. The construct code mirrors JITThunks exactly and the VM-lifetime thunk makes the self-managed code ref safe. Not approving because an inline finding is posted and further verified findings were not, and the DFG/FTL CallFFI bypass of the breakpoint remains a behavior a human should weigh.

@coderabbitai

coderabbitai Bot commented Sep 22, 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: 01ee5d3b-45c0-4ccf-a172-edb2af46d790

📥 Commits

Reviewing files that changed from the base of the PR and between 564ac2a and d997e37.

📒 Files selected for processing (2)
  • Source/JavaScriptCore/ffi/JSFFIFunction.cpp
  • Source/JavaScriptCore/ffi/tests/testFFI.cpp

Included review availability: Your plan provides up to 5 included reviews per hour; 0 remain after this review.


Walkthrough

Changes

FFI symbolic breakpoints

Layer / File(s) Summary
Dedicated FFI constructor executable
Source/JavaScriptCore/ffi/JSFFIFunction.cpp
JSFFIFunction::create creates dedicated constructor JIT code and a NativeExecutable for FFI IC stubs. It uses vm.getHostFunction when no stub executable exists.
Remote-inspector symbolic-breakpoint coverage
Source/JavaScriptCore/ffi/tests/testFFI.cpp
The test validates breakpoint installation, pause metadata, call and construct JIT code restoration, support for existing and newly created functions, and cleanup after removal or frontend destruction.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to d997e

No verified merge-blocking issue remains in the available evidence; normal validation should still cover the FFI executable and inspector test paths.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 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: fixing an abort when a symbolic breakpoint matches a live bun:ffi function name.
Description check ✅ Passed The description gives a detailed problem statement, fix explanation, testing evidence, scope, limitations, and changed behavior. It does not include the required Bugzilla URL, Reviewed by NOBODY line,…

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.

@robobun

robobun commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator Author

Closing: the abort that this PR fixes no longer exists on main.

  • Upstream fixed it in the debugger: 5596b3e, "Web Inspector: isolate native debugger hooks from shared JIT code". Upgrade to upstream WebKit f59345e04a8d #889 merged it into this fork.
  • InspectorDebuggerAgent::didCreateNativeExecutable now gives each NativeExecutable a replacement JITCode (NativeExecutable::swapGeneratedJITCodeForDebugger). It no longer changes the code inside a JITCode that two executables share.
  • JSFFIFunction::create still shares the construct JITCode of base with the stub executable. That is safe now, so the change in this PR has no purpose left.
  • Checked with Bun. On v1.4.4 (a pin before Upgrade to upstream WebKit f59345e04a8d #889) a symbolic breakpoint on a dlopen() symbol aborts. On a debug build of Bun main (pin a7b59e6) the breakpoint is added, each call pauses with reason FunctionCall, the breakpoint is removed, and the script exits with code 0.
  • The WebAssembly case from the notes here (two live functions named "0") gives the same results: an abort on v1.4.4, correct behavior on main.
  • Not changed: the CallFFI conversion still has no VM::isDebuggerHookInjected() check at that pin. The limit from the review thread above still applies.
  • The testSymbolicBreakpointOnFFIFunction case from this PR is not in the tree. The Bun side, Bump WebKit (oven-sh/WebKit#720 preview): a symbolic breakpoint that matches a bun:ffi function name no longer aborts bun#43813, is closed for the same reason.

@robobun robobun closed this Oct 31, 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