[2] Epoch-OSR BBQ recompile overwrites a retired callee, leaving an unrepatched call into freed Wasm code
A parked Wasm frame wakes up and branches into code the engine already freed.
Rated High on the shape of the primitive: a live JIT frame branching into callee code the heap has released. What keeps it from Critical is the setup. It needs a multi-step cross-worker tier-up schedule, plus a GC landing in the right window, and content cannot directly control either.
A JIT that frees old compiled code has to know every piece of live code that still branches into it, because a direct call baked into machine code is invisible to the garbage collector. In JSC's WebAssembly engine, functions climb from the IPInt interpreter to the BBQ baseline JIT to the OMG optimizing JIT. A CalleeGroup holds each compiled version; it is the per-module table of compiled code shared by every worker using that module. When a function tiers up, the engine repatches direct calls in every caller it can find, so each caller still holding such a branch must stay findable until it dies.
The angle: web content that shares one WebAssembly.Module across workers could steer a multi-step tier-up schedule so that a parked frame's next call jumps into freed JIT code. That is a control transfer into freed executable memory.
Let A and B be two Wasm functions in the same Module, where A calls B. The commit message gives this schedule:
- A and B tier up to BBQ. A_BBQ1 has a baked-in
blto B_BBQ1. - A_BBQ1 runs on Worker 1 and blocks in
Atomics.wait()right before theblto B_BBQ1. - A runs in Worker 2 and tiers up to OMG. A_BBQ1 is retired in favor of A_OMG. That makes A_BBQ1's reference in the
CalleeGroupweak and resets the tier-up counter, which permits BBQ recompilation of A. - Some old interpreter frames of A that are still on Worker 2's stack return. This can trigger BBQ compilation via epilogue OSR, which installs A_BBQ2 over the retired A_BBQ1.
- B tiers up to OMG. All calls to B_BBQ1 must be repatched; in reality that set is A_BBQ1, A_BBQ2 and A_OMG. Because A_BBQ2 overwrote A_BBQ1, the VM does not repatch A_BBQ1.
- B_BBQ1 is retired and gets GCed.
Atomics.waitreturns, and A_BBQ1 executes theblto B_BBQ1: use-after-free.
The fix reuses the retired BBQ callee in epilogue OSR, mirroring what loop OSR already did. The commit also adds $vm.wasmCalleeIDs for the stress test.
Source/JavaScriptCore/wasm/WasmIPIntSlowPaths.cpp
Source/JavaScriptCore/wasm/WasmCalleeGroup.cpp
Patch Details
The change has three parts. It repairs the epilogue-OSR arm of the tier-up slow path. It hardens the BBQ install path so overwriting an occupied slot crashes deterministically. It adds test-only observability and documentation around the free path.
In jitCompileAndSetHeuristics(), the getReplacement lambda's OSRFor::Epilogue arm used to return nullptr. It now falls through to OSRFor::Loop, so both call CalleeGroup::tryGetBBQCalleeForLoopOSRConcurrently(). That function returns the BBQ callee in the function's optimized-callees slot even when the slot holds it only weakly (retired). In that case it also registers the callee via vm.heap.reportWasmCalleePendingDestruction(), so the returning frame reuses the retired callee instead of starting a new compile.
In CalleeGroup::tryReserveCalleeForInstall(), the BBQMode case now takes the slot's m_bbqCallee lock and RELEASE_ASSERTs that the slot is empty. Any BBQ install over an existing BBQ callee, retired or not, becomes a crash instead of a silent overwrite.
The remaining changes are collateral:
- a comment in
Heap::releaseUnmarkedWasmCallees()noting thatcrossModifyingCodeFence()does not protect callers whose direct calls were never repatched; - the test-only
$vm.wasmCalleeIDs()helper inJSDollarVM.cpp(gatedallowIfNotFuzz), which reports BBQ/OMG callee identity and retired state; - the stress test
retired-bbq-caller-direct-call-repatch.js.
The long list of WasmIPIntSlowPaths.cpp functions marked "Deleted" in the changelog is generator noise; only the getReplacement switch changed.
A replace-in-place install over a retired-but-live entry erases the only record the fix-up sweep uses to find it, so a still-running object keeps a stale edge to a target that is later freed.
Background
Three execution tiers. JSC executes WebAssembly in three tiers. IPInt is an in-place interpreter, BBQ is a fast baseline JIT, and OMG is the B3-based optimizing JIT. A per-function tier-up counter decides when a function moves up a tier.
On-stack replacement. OSR (on-stack replacement) lets a frame running in a lower tier switch into compiled code without restarting the function. In IPInt, OSR checks happen at three points: the function prologue, loop back-edges, and the function epilogue (return).
CalleeGroup and the optimized-callees slot.
A WebAssembly.Module can be shared between workers. Its compiled code is therefore shared across threads and VMs, and it lives in one CalleeGroup per Module and memory mode. For each function, the group keeps an optimized-callees tuple. The tuple has one BBQ slot (m_bbqCallee, lock-protected, holding its callee either strongly or weakly) and an OMG callee.
Direct calls and repatching.
Wasm-to-Wasm calls are compiled as direct branch instructions to the callee's entrypoint (bl on ARM64). When a callee tiers up, updateCallsitesToCallUs() walks the known callers and repatches those branches to the new code.
Retirement.
Retirement happens when Options::freeRetiredWasmCode() is enabled. CalleeGroup::releaseBBQCallee() demotes a superseded BBQ callee to a weak reference and calls resetAndOptimizeSoon() on the IPInt tier-up counter. A retired callee survives only while something else keeps it alive. That can be a frame on some thread's stack, found by conservative scanning, or an explicit reportWasmCalleePendingDestruction(). Callees left unmarked at GC are released by Heap::releaseUnmarkedWasmCallees().
Atomics.wait.
Atomics.wait, like any long-blocking import call, suspends a worker thread while its Wasm frames remain on the stack.
Analysis
The bug is a use-after-free of JIT code, created by a cross-thread tier-install race on a shared CalleeGroup.
Worker 1 Worker 2 (or any worker)
------------------------ ---------------------------------------
A_BBQ1 parks in Atomics.wait,
just before bl B_BBQ1
A -> OMG: A_BBQ1 retired (slot now weak),
tier-up counter reset
old IPInt frame of A returns: Epilogue OSR
-> nullptr -> fresh BBQ compile
-> A_BBQ2 written over A_BBQ1's slot
B -> OMG: repatch A_BBQ2, A_OMG
(A_BBQ1 unreachable to the sweep)
B_BBQ1 retired -> releaseUnmarkedWasmCallees
wakes: bl B_BBQ1 ---------------> freed JIT code
Read the right column top to bottom. Retiring A_BBQ1 demoted its slot to weak and reset the tier-up counter, which re-opened BBQ compilation of A. Loop OSR already handled that state by reusing the retired callee. Epilogue OSR returned nullptr, so a returning IPInt frame past the threshold fell through to a fresh BBQ compile. That compile published A_BBQ2 into the same slot. tryReserveCalleeForInstall did not object, because the IPInt tier-up counter was the only serialization and the counter had just been reset.
The broken invariant: every compiled callee that still holds a direct call into another callee must stay discoverable by the repatching machinery until it is destroyed. Once A_BBQ1 dropped out of the slot, updateCallsitesToCallUs() could no longer find it, as the fix comment states. B's tier-up therefore repatched A_BBQ2 and A_OMG but left A_BBQ1's baked-in branch pointing at B_BBQ1.
In the left column, A_BBQ1 itself stays alive while Worker 1 is parked inside one of its frames; conservative-root scanning keeps retired callees that are live on a stack alive. B_BBQ1 has no live frame and no repatch-visible referrer, so it can be freed by Heap::releaseUnmarkedWasmCallees(). The new comment there says explicitly that the cross-modifying-code fence does not cover a live callee holding a not-yet-repatched direct call.
The fix is a sibling-path symmetry repair. That, together with the step-by-step schedule and the purpose-built $vm.wasmCalleeIDs helper, suggests code review of the tier-up lifecycle found the Loop/Epilogue asymmetry, and the race was then made deterministic for the regression test rather than surfaced by random fuzzing.
The stress test detects the bug when B returns a "reached BBQ" marker, meaning A_BBQ1 called a stale but still-executable B_BBQ1. Executing actually freed memory additionally requires a GC to release B_BBQ1 inside the window before Worker 1 wakes.
The bug is reachable from web content through a WebAssembly.Module shared across workers. An attacker would have to win the multi-step cross-thread schedule without the test's $vm.wasmCalleeIDs observability. The immediate effect is execution of freed JIT memory. If the range is not reused, the observable effect would be a crash on executing unmapped or garbage memory.
Escalation turns on what occupies B_BBQ1's executable range when the stale branch fires. If the JIT allocator could be steered to place other compiled code at that range before the parked A_BBQ1 frame resumes, the branch would land at an engine-chosen offset inside unrelated machine code. Whether that could become a useful gadget depends on two things this commit does not establish: the executable allocator's reuse policy, and how the entry ABI B_BBQ1 expected maps onto the replacement code. Those remain the audit questions. Controlled code execution is not an established path. The primitive is control-flow redirection, not a data read/write.
This vulnerability weakens the JIT's control-flow integrity in the WebContent process. The engine assumes every direct branch emitted into live compiled code targets live compiled code. Before the fix, ordinary web content could leave live A_BBQ1 code holding a branch into freed callee memory. If that executable range were reused for other compiled code, execution would continue at an engine-unintended point inside it. That makes this a renderer-plane control-flow hijack candidate rather than a mere crash, bounded by whatever code occupies the reclaimed range. Going beyond the renderer would need a separate escape.
Insight: The new RELEASE_ASSERT in tryReserveCalleeForInstall is the more durable half of the fix. It turns the whole class, "a BBQ install lands on a non-empty slot", into a deterministic crash regardless of which trigger path reached it. The Heap.cpp comment matters for the same reason. It records that freeing retired Wasm code depends on repatch completeness, not on reachability. Callee liveness is decided by scanning stacks for the callee's own frames, and incoming direct branches from other live code are not GC edges. Any path that makes a live caller invisible to updateCallsitesToCallUs() reopens the same bug.
Audit directions
- Single-slot registry overwritten while the previous occupant is retired but live. The overwrite erases the only record a later fix-up sweep uses.
- Narrow: audit every install path into
CalleeGroup's optimized-callees tuple (OMG install, OMG-for-OSR-entry install, the Prologuereplacement()SIMD path), and confirm each one either reuses or refuses a weakly held occupant. - Wider: apply the same check to JSC JS-tier
CodeBlockreplacement and jettison, and toGCAwareJITStubRoutineownership, where a new stub orCodeBlockreplaces an old one that is still on some stack. Ask whether incoming-call lists still enumerate the old one. - Widest: any JIT with code GC plus direct-call patching faces this invariant. HotSpot's nmethod not-entrant/zombie transition requires inline caches pointing at a dying nmethod to be cleaned before it is freed. V8's deoptimization and code flushing face the same "who still branches here" question.
- Code-review tell: a store into a strong/weak slot without first checking
get()on the weak side, or any replace-in-place step that happens before the repatch sweep has visited the old entry. - Code freed while still reachable through non-refcounted edges. Direct branches are such edges, and the design relies on a repatch sweep instead of reachability.
- Narrow: trace
Heap::releaseUnmarkedWasmCallees()and determine whether callee liveness ever considers live callers that branch into it. Grep for other consumers ofm_wasmIndirectCallEntrypoints,m_wasmToWasmExitStubsand JS-to-Wasm entry thunks that cache raw entrypoints. - Wider: the same shape covers any raw code pointer cached outside the refcount graph: call-profile caches, IC stubs that bake in a target, debugger breakpoints patched into code.
- Widest: wherever an edge is implicit in machine code and therefore invisible to the collector, soundness must rest on "the sweep visits every live holder". Audit for any state transition that removes a live holder from the sweep's enumeration domain.
- Code-review tell: a cached
CodePtrto another callee's entry stored somewhereupdateCallsitesToCallUs()does not walk. - Sibling trigger paths for one lifecycle transition, where only some honor a guard.
- Narrow: enumerate every path in
WasmIPIntSlowPaths.cppand the BBQ/OMG slow paths that can start a BBQ or OMG plan: prologue, loop and epilogue OSR, call-site tier-up, debugger-driven recompiles. Check that each consults the retired-callee state the Loop path already checked. - Wider: check for the same asymmetry in JS-tier OSR entry versus prologue tier-up in Baseline/DFG/FTL, where one entry respects a jettison or replacement guard that another skips.
- Widest: multi-trigger tier-up state machines exist in V8 (Maglev/Turbofan OSR versus function-entry tier-up via the feedback vector) and SpiderMonkey (Baseline-to-Ion OSR versus call entry). In each, ask whether every trigger checks the same "previous version still live" predicate before publishing a new version.
- Code-review tell: a
switch (osrFor)arm that returnsnullptrwhere a sibling arm returns an existing callee.