[JSC] UAF of JIT code: DFG/FTL DirectTailCall to a host function leaves the caller's CodeBlock unrooted during the call
CVE: CVE-2026-86986 · Safari 27.2 · Released October 26, 2026 Impact: Processing maliciously crafted web content may lead to an unexpected Safari crash Apple's description: A use-after-free issue was addressed with improved memory management. Credit: an anonymous researcher
High. A tail-call shortcut let the GC free the exact machine code a pending ret was aimed at, a control-flow UAF that page script can reach. Past the crash Apple describes, escalation depends on the executable allocator reusing that region before the host call returns.
JavaScriptCore's optimizing JITs may discard machine code at almost any garbage collection. When an assumption baked into optimized code stops holding, the GC jettisons that code and can release its executable memory. The safety net is that any CodeBlock whose code is still live on the stack is found through its frame and kept alive. Proper tail calls put pressure on that net. A strict-mode return f(x) rewrites the caller's frame into the callee's frame before the callee runs. If the emitted sequence still returns into the caller's code afterward, something other than the now-destroyed frame has to keep that code allocated.
The angle: A web page can arrange for a garbage collection inside a builtin call to free the optimized CodeBlock code the builtin is about to return into, sending renderer control flow into freed executable memory.
Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp
Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp
Patch Details
The fix deletes one fast path from each optimizing tier. Both deletions remove the same idea.
In the DFG, SpeculativeJIT::emitCall had an inline native-call path. When the callee of a direct call is a compile-time-constant NativeExecutable with no intrinsic, and no debugger hook is injected, the DFG emits an emitCallTarget lambda instead of going through call linking. That lambda does an inline function prologue, writes nullptr into the new frame's CallFrameSlot::codeBlock, makes a C call to the host function, and runs the epilogue. Inside that path, an if (isTail) block handled tail position as follows:
- Store the call-site index.
- Run
CallFrameShuffler(...).prepareForTailCall(). - Invoke
emitCallTarget(). - Emit
ret().
The patch adds !isTail to the guarding condition and deletes the isTail block, so tail calls never enter the inline path.
In the FTL, the DirectTailCall lowering inside LowerDFGToB3 had an equivalent block: if (nativeFunction && !vm->isDebuggerHookInjected()). It stored the call-site index into the high word of argumentCountIncludingThis, ran prepareForTailCall(), invoked emitCallTarget(), and emitted jit.ret(). The diff's hunk header attributes this code to compileCompareStrictEq, but that is a diff-context artifact. The code is the direct-call lowering, immediately before m_directCallLinkInfos.add(...) and setCallType(CallLinkInfo::DirectTailCall). The patch deletes the block outright.
After the patch, a DirectTailCall to a NoIntrinsic host function in either tier falls through to the ordinary DirectCallLinkInfo path. Non-tail DirectCalls to host functions keep the inline fast path.
Before (tail position): After (tail position):
DirectTailCall → host fn DirectTailCall → host fn
└─► prepareForTailCall() └─► DirectCallLinkInfo path
└─► call hostFn (inline) └─► native call thunk
└─► ret → caller code (kept alive via callee)
The commit also adds JSTests/stress/direct-tail-call-to-host-function-keeps-caller-code-alive.js, which runs under --sweepSynchronously=1 --zeroExecutableMemoryOnFree=1. The Analysis section walks through it.
Background
Proper tail calls.
In JSC strict-mode code, a call in tail position (return f(x)) reuses the caller's stack frame instead of pushing a new one. Once the callee starts, the caller's frame no longer exists as a distinct frame.
CodeBlocks and executable memory.
A CodeBlock is JSC's per-function, per-tier compilation unit. Its JIT machine code lives in executable memory owned by the CodeBlock's JITCode. When a CodeBlock is finalized, that executable memory can be released back to the allocator.
CodeBlock liveness during GC. During collection, JSC keeps a CodeBlock alive if it is reachable through ordinary heap references or through frames on the stack. For frames, the GC reads each frame's codeBlock header slot, and finds those slots via a conservative stack scan. CodeBlocks that are not found this way are dead, and their code becomes eligible for freeing.
Frozen weak references and jettison. DFG and FTL code embeds assumptions about specific objects, such as the current value of a global, and holds those objects as frozen weak references. If a GC finds such a referent dead, it invalidates (jettisons) the optimized CodeBlock that depended on it. The code can then be freed. A separate invalidation route is watchpoints: an inferred-value watchpoint on a global fires when the global is reassigned, and that also jettisons dependent code.
Host functions.
A host (native) function is a builtin implemented in C++. One example is stringProtoFuncRepeat, which backs String.prototype.repeat. JIT code invokes host functions with a real C call. Because they can allocate, they can trigger a GC partway through.
DirectCall and DirectTailCall.
These are DFG nodes for calls whose callee is a compile-time constant. They are normally linked through a DirectCallLinkInfo. For NoIntrinsic host-function callees in non-tail position, the DFG and FTL instead emit the inline C call described in Patch Details.
CallFrameShuffler::prepareForTailCall().
This is the JIT helper that rewrites the current frame in place into the callee's layout before control transfers to the callee. It moves the arguments, the header slots, and the callee-save registers.
Analysis
This is a use-after-free of JIT executable memory: a return into a code region that the GC freed while a ret targeting it was still pending.
caller's DFG/FTL code host function + GC
─────────────────────── ────────────────────────────────
prepareForTailCall()
caller frame → callee frame
codeBlock slot := nullptr
call hostFn ─────────────────────► stringProtoFuncRepeat(n)
[ret addr → caller's JIT code] allocates MBs → full GC
caller CodeBlock: no frame root
weak ref dead → jettison → free
◄───────────────── ret
ret lands in freed JIT memory ✗
Read the left column from the top. prepareForTailCall() overwrites the caller's frame with the callee's frame. Then emitCallTarget() runs its inline prologue and executes emitPutToCallFrameHeader(nullptr, CallFrameSlot::codeBlock). From this point on, no frame on the stack names the caller's CodeBlock: the caller's frame has been consumed, and the new frame says "no CodeBlock".
Nothing has jumped to the callee yet, though. The emitted code performs a real C call into the host function from inside the caller's own JIT code. That call pushes a return address, shown as the bracketed arrow in the diagram, which points back into the caller's machine code. The ret emitted after emitCallTarget() is also part of that code. So the caller has given up its frame but still has code on the return path.
The GC does not account for that. Per the commit message and the test's comment, JSC's conservative stack scan does not treat return addresses into CodeBlock JIT code as roots. The scan looks for codeBlock-slot words, and the only one in play here holds nullptr. The right column then shows a GC starting inside the host function and finding the caller's CodeBlock unrooted.
A missing root alone does not free code; something has to kill the CodeBlock. That is the job of the frozen weak references. If any of them is dead at that moment, the GC jettisons the caller's code. With synchronous sweeping, it frees the code immediately. When the host function finishes, its ret transfers control into executable memory that no longer belongs to the caller.
The stress test is built to make each of these conditions hold deterministically. It is worth reading as a recipe, because every line removes one thing that would otherwise rescue the caller by accident:
- Defuse the watchpoint route.
globalThis.his assigned twice up front, which invalidates the global's inferred-value watchpoint. Later, nullinghdoes not jettison the caller through a watchpoint. The jettison has to come from the GC finding the frozen weak reference dead. - Train the tail caller. Each generation builds a fresh strict-mode
tail(n)vianew Function. The fresh executables keep earlier jettisons from counting against the reoptimization heuristics.tailcallsglobalThis.h(n)on oddnand ends withreturn "a".repeat(n), which is a DirectTailCall to the NoIntrinsic host functionstringProtoFuncRepeat. Four thousand iterations get it into the DFG/FTL withh's current function frozen as a weak reference. - Kill the weak referent.
globalThis.h = nullleaves the frozen function object reachable only from the optimized code's weak reference. - Scrub the stack.
scrub(2000)recurses through float and integer work to overwrite stale stack words that might conservatively root the caller. The test also keepstail'sargCountIncludingThisno larger than the host callee's frame (both are 2). That way the shuffle fully overwrites the old frame header, and no leftover codeBlock-slot word survives to root the caller. - Allocate inside the host call.
tail((1 << 20) + (k << 18))takes the even branch, which skipsh, and asksrepeatfor megabytes of string. That forces a full GC insidestringProtoFuncRepeat, while the shuffled frame no longer roots the caller. - Make the free observable.
--zeroExecutableMemoryOnFree=1turns the stale return into a deterministic crash instead of a silent run through whatever bytes remain.
That last flag also says something about default builds. Forcing zeroing implies that, by default, the freed region keeps stale bytes or may be reused, and that determines what an attacker would be working with. As a primitive, a return into freed JIT executable memory is a control-flow transfer. Its ceiling depends on what occupies the region when ret executes. If the region is unmapped or zeroed, as the test forces, the effect would be a crash, which matches Apple's stated impact. If the executable allocator hands the region to other JIT code before the host function returns, control would resume at an arbitrary offset inside that unrelated code. That could make the bug a renderer control-flow hijack candidate. It does not give a read/write primitive on its own.
The trigger requires nothing beyond ordinary page script. An attacker needs a strict-mode function that DFG or FTL compiles with a constant host-function callee in tail position, a host call that allocates enough to force a GC, and one of the caller's frozen weak references dead at that moment. Escalation from a crash to controlled code execution would depend on executable-memory reuse and on hardening properties that the available source context does not establish. The bug is confined to the WebContent process, where JSC JIT code runs. Anything beyond renderer-plane impact would need a separate sandbox escape.
The fix does not teach the GC to treat return addresses as roots. It changes the call so that no caller-owned code sits on the return path while the host function runs. With the inline path closed to tail calls, a DirectTailCall to a host function goes through the DirectCallLinkInfo route. Per the commit message, that route's native call thunk is kept alive via the callee, so a GC during the host call no longer leaves the code it will return into without an owner.
Non-tail DirectCalls keep the inline fast path. In that case the caller's frame is not consumed by a shuffle before the C call, and losing that frame is exactly what the tail variant did.
Once the tail-call shuffle nulled the frame's codeBlock slot, a raw return address was the only thing keeping the caller's code alive, and the GC does not count return addresses.
Insight
Takeaway: whenever a JIT emits call rather than jmp after destroying its own frame, ask what keeps the code on the return path alive, because a return PC is not a GC root.
Audit directions
-
JIT code returned into while no frame metadata roots its owner. The invariant: any code on the return path of a GC-capable call must be reachable from a GC root other than the raw return PC.
- Narrow: grep
JavaScriptCore/dfg,ftl,jit, andwasmforCallFrameShuffler(...).prepareForTailCall()followed by acall,nearCall, or C-call emission and then aretinstead of a jump. Also flagemitPutToCallFrameHeader(nullptr, CallFrameSlot::codeBlock)ahead of any operation that can GC. Match tell: anycallafter a shuffle whose return lands in the current function's own code. - Wider: IC stubs and thunks that call GC-capable operations and rely on PC-based marking, such as GCAwareJITStubRoutine-style routines, Wasm-to-JS and JS-to-Wasm wrappers, and varargs tail-call thunks. For each, check whether every code object on the return path is rooted by something other than its return address. Match tell: a stub whose memory is freed by a jettison or finalizer that the operation it calls can trigger.
- Widest: any runtime that garbage-collects or discards machine code: V8 code space with lazy deopt, SpiderMonkey JitCode discarding, .NET code pitching, LuaJIT trace flushing. Ask how each keeps in-flight code alive, and whether frame-elision paths bypass that mechanism.
- Narrow: grep
-
Frame elision erasing metadata that stack walkers rely on. Tail calls, OSR, and frame shuffling all rewrite frame headers. The GC root scan, the exception unwinder, and the profiler all consume those headers. The invariant: every such consumer sees a consistent picture at every point where it can run.
- Narrow: review every
CallFrameShuffleruser, and every site that stores the call-site index into the high word ofargumentCountIncludingThisafter a shuffle. Check what ShadowChicken, the sampling profiler, and exception unwinding observe if they run during the subsequent callee. Match tell: a codeBlock slot that isnullptror stale while the PC still lies inside a CodeBlock's code. - Wider: OSR exit and entry ramps, and Wasm tail-call (
return_call) lowering, where frames are rebuilt in place. Check whether a GC or unwinder can observe the half-rewritten state. - Widest: any engine implementing proper tail calls: Wasm tail calls in V8 and SpiderMonkey, Scheme and Erlang VMs, and tail-call-capable LLVM backends with GC statepoints. Ask whether elided frames still contribute stack maps or safepoint info for any code that remains on the return path.
- Narrow: review every
-
Specialized fast paths that drop the general path's lifetime guarantees. Here the inline host-call path skipped the
DirectCallLinkInforoute, whose thunk is kept alive via the callee. The invariant: a fast path must preserve every liveness and rooting property of the slow path it replaces, not just its calling convention.- Narrow: audit the remaining non-tail inline native-call path in
SpeculativeJIT::emitCalland in the FTL direct-call lowering. Also audit other DFG/FTL inline host-call shortcuts, such as CallDOM and intrinsic-free direct calls underisDebuggerHookInjectedtoggles. Compare what each one roots against the CallLinkInfo-based path. Match tell: a fast path where the code object on the return path is owned by something that a GC inside the call can finalize. - Wider: inline caches that call getters, setters, or host functions directly from stub code instead of through a shared thunk. Check whether a jettison triggered inside the call can free the stub.
- Widest: any JIT or interpreter that inlines calls into foreign or native code, such as V8 fast API calls and SpiderMonkey JitInfo-based DOM calls. Ask whether the inlined path keeps the same GC-visible ownership as the generic call trampoline.
- Narrow: audit the remaining non-tail inline native-call path in