Repository navigation
Conversation
…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.
|
Status
|
|
Preview build of d997e37: |
There was a problem hiding this comment.
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.
|
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 configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Essentials Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 5 included reviews per hour; 0 remain after this review. WalkthroughChangesFFI symbolic breakpoints
Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to 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)
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 Comment |
|
Closing: the abort that this PR fixes no longer exists on main.
|
Problem
Debugger.addSymbolicBreakpointwith a symbol that matches a livebun:ffifunction name aborts:panic(main thread): abort() called, from theRELEASE_ASSERTatInspectorDebuggerAgent.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 constructJITCodeofbase, the executableVM::getHostFunction()caches per name and arity. The debugger swaps aJITCodein place once per executable. The second one finds it already swapped.Fix
JITCode, made the wayJITThunks::hostFunctionStubmakes one.create()no longer makes the unusedbasewhen there is a stub.JITCodeobjects, asdidCreateNativeExecutableand~ReplacedThunkassume.testSymbolicBreakpointOnFFIFunctioninffi/tests/testFFI.cppaborts at:1695without the fix. Also all of testFFI andJSTests/stress/ffi-*.js(debug build).Background
NativeExecutableholds oneJITCodefor call and one for construct. Each wraps the entry point a caller jumps to.Downsides
NativeJITCode(40 bytes) per FFI function with an IC stub.WebAssemblyFunction::create(upstream code) shares bothJITCodeobjects. Two live functions named"0"(the function index) or"start"trigger it.CallFFInever pauses (noVM::isDebuggerHookInjected()check).Notes
dlopen("libz.so.1", { zlibVersion: { args: [], returns: "cstring" } }), keep the symbol, pause on adebuggerstatement underbun --inspect-wait, sendDebugger.enable, thenDebugger.addSymbolicBreakpointwithsymbol: "zlibVersion". The process exits with SIGABRT before it answers. The debug adapter sends the command atpackages/bun-debug-adapter-protocol/src/debugger/adapter.ts:1012(oven-sh/bun). Web Inspector sends it for a symbolic breakpoint.baseis still alive (no GC sincedlopen()). Two functions of one name and arity (one library opened twice, or one name in twolinkSymbols()calls) always abort, because their executables keep the shared object alive.JITCode(:1695). Function made while the breakpoint is set:baseis 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.useFFIICStub=0, abuffer_lengthargument) uses the shared executable fromgetHostFunction(), as before. The debugger handles that, because it keeps its records per executable.create()allocatedbasefor every function with a stub and then dropped it: an 80-byte cell and twoNativeJITCodeobjects, collected at the next GC.FrontendChannelto the global object'sJSGlobalObjectInspectorController, sends the protocol messages, and resumes each pause fromJSGlobalObjectDebugger::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 reasonFunctionCalland returns the right value. It checks that the entry points are back afterDebugger.removeSymbolicBreakpointand after the frontend disconnects. It runs last in testFFI, because a debugger deletes all of the VM's code and a symbolic breakpoint setsVM::notifyDebuggerHookInjected()for the rest of the VM's life.JSC_useFFIICStub=0,JSC_useDFGJIT=0,JSC_useLLInt=0,JSC_useConcurrentJIT=0,JSC_collectContinuously=1,JSC_forceDebuggerBytecodeGeneration=1,JSC_slowPathAllocsBetweenGCs=5,JSC_forceICFailure=1andJSC_validateExceptionChecks=1. The stress run was 545 test runs (33 files, every mode ofrun-jsc-stress-tests).wasm/js/WebAssemblyFunction.cpp:86gives every function's executable bothJITCodeobjects of one cachedbase. 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:1695in the Bun build above. Upstream WebKit main has the same code. It needs its own change, because each function would need two more allocations.escape: a loop that ran before the breakpoint was added does not pause, a new call site does.CallFFIlimit: 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). TheCallFFIconversion (DFGByteCodeParser.cpp:2656,FFIDFG.cpp:44) has no such check, andCallFFIcalls 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.autobuild-preview-pr-720-d997e370(debug, ASAN): thedlopen()repro answers{"result":{}}, the next call pauses with reasonFunctionCalland 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.FFI::ICStubCodeand does not touchcreate(). [JSC] FFI: compile C modules lowered to BIR with B3 #653 does not touch it either. The changes do not conflict.