← All reports

[2] Epoch-OSR BBQ recompile overwrites a retired callee, leaving an unrepatched call into freed Wasm code

HighJSC WebAssemblyUAF

A parked Wasm frame wakes up and branches into code the engine already freed.

d2b97df

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:

  1. A and B tier up to BBQ. A_BBQ1 has a baked-in bl to B_BBQ1.
  2. A_BBQ1 runs on Worker 1 and blocks in Atomics.wait() right before the bl to B_BBQ1.
  3. 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 CalleeGroup weak and resets the tier-up counter, which permits BBQ recompilation of A.
  4. 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.
  5. 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.
  6. B_BBQ1 is retired and gets GCed.
  7. Atomics.wait returns, and A_BBQ1 executes the bl to 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

- case OSRFor::Epilogue: {
- return nullptr;
- }
+ case OSRFor::Epilogue:
case OSRFor::Loop: {
+ // If there's a retired but not-yet-collected BBQCallee, use it. For both Loop and
+ // Epilogue OSR, it can be the case that such retired BBQCallees still need to be
+ // patched in some callers. Not using it means a new BBQ compilation can be triggered,
+ // which can overwrite the retired BBQCallee, leaving updateCallsitesToCallUs() unable
+ // to patch those callers.
return calleeGroup.tryGetBBQCalleeForLoopOSRConcurrently(instance->vm(), functionIndex);
}

Source/JavaScriptCore/wasm/WasmCalleeGroup.cpp

- case CompilationMode::BBQMode:
+ case CompilationMode::BBQMode: {
// The IPInt tier-up counter's compilation status already serializes BBQ plans for a function.
+ // IPInt OSR should never trigger a BBQ compile while there's a retired but still alive
+ // BBQCallee. In those cases, the retired BBQCallee is reused.
+ auto* slot = optimizedCalleesTuple(locker, functionIndex);
+ ASSERT(slot);
+ Locker bbqLocker { slot->m_bbqCallee.lock() };
+ RELEASE_ASSERT(!slot->m_bbqCallee.get());
break;
+ }

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:

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.

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.

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.