At any moment during execution, a V8 thread's call stack contains an interleaved mix of frames: C++ entry frames, Ignition bytecode interpreter frames, Sparkplug baseline frames, Maglev and TurboFan optimized frames, builtin continuation frames, and WebAssembly frames.
Four critical subsystems in V8 rely on walking this heterogeneous stack accurately:
SafepointTable iteration and Conservative Stack Scanning).Isolate::Throw): Walks from the throwing PC outward until a frame's HandlerTable catches the exception.TURBOFAN_JS or MAGLEV frame on the physical stack with one or more reconstructed INTERPRETED and BUILTIN_CONTINUATION frames.Error.stack, DevTools): Samples the call stack asynchronously from a signal handler or walks frames synchronously to symbolize function names and line numbers.fp) Chain and Frame Type MarkersV8 maintains a contiguous linked list of Frame Pointers (fp / rbp on x64 / x29 on ARM64) across all generated frames. At fp + 0, every frame stores the caller‘s saved fp, and at fp + kSystemPointerSize, it stores the caller’s return pc:
Higher Memory Addresses (Caller Frames) +-----------------------------------------------------------------------+ | Caller Parameter N ... Parameter 0 / Receiver | +-----------------------------------------------------------------------+ | Return Address (saved PC in caller) [ fp + kPCOnStackSize ]| +=======================================================================+ | Saved Caller Frame Pointer (prev FP) [ fp + 0 ] | <-- Current FP +-----------------------------------------------------------------------+ | Frame Type Marker (Smi: JAVASCRIPT, EXIT, ...) [ fp - kPointerSize ] | +-----------------------------------------------------------------------+ | Context (JS frames) / Typed-Frame Slot 0 [ fp - 2*kPointerSize]| +-----------------------------------------------------------------------+ | JSFunction (JS frames) / Typed-Frame Slot 1 [ fp - 3*kPointerSize]| +-----------------------------------------------------------------------+ | Actual Arg Count (JS frames) / Frame Slots [ fp - 4*kPointerSize]| +-----------------------------------------------------------------------+ Lower Memory Addresses (Current Stack Pointer SP)
How does StackFrame::ComputeType know what kind of frame sits at a given fp?
[fp + TypedFrameConstants::kFrameTypeOffset]:StackFrame::Type marker (StackFrame::TypeToMarker(type), encoded with a low tag bit of 0) at [fp + TypedFrameConstants::kFrameTypeOffset] ([fp - kSystemPointerSize] without an embedded constant pool).INTERPRETED, BASELINE, MAGLEV, TURBOFAN_JS, plus JS-linkage builtins and compiled JS-to-Wasm TURBOFAN_STUB_WITH_CONTEXT wrappers):StackFrame::JAVASCRIPT at [fp + StandardFrameConstants::kFrameTypeOffset], followed by a dedicated Tagged<Context> slot at [fp + StandardFrameConstants::kContextOffset], Tagged<JSFunction> at [fp + StandardFrameConstants::kFunctionOffset], and argc at [fp + StandardFrameConstants::kArgCOffset].StackFrame::JAVASCRIPT across all JS tiers means in-place On-Stack Replacement (Ignition -> Sparkplug/Maglev/TurboFan and Sparkplug -> Maglev/TurboFan) and DiscardBaselineCodeVisitor never need to rewrite the frame type marker slot.marker == TypeToMarker(StackFrame::JAVASCRIPT) confirms a Standard JavaScript frame, V8 distinguishes the concrete JS tier via the PC / CodeKind:INTERPRETED (InterpretedFrame): Identified because the return pc (or current PC) lies inside an Ignition bytecode handler builtin (Isolate::builtins()->IsBytecodeHandlerPc(pc)) or InterpreterEntryTrampoline. It adds [fp + UnoptimizedFrameConstants::kBytecodeArrayFromFp], [fp + kBytecodeOffsetOrFeedbackCellFromFp], and [fp + kFeedbackVectorFromFp].BASELINE / MAGLEV / TURBOFAN_JS / BUILTIN / TURBOFAN_STUB_WITH_CONTEXT: Identified by looking up the Code object for pc and checking code->kind() (CodeKind::BASELINE, CodeKind::MAGLEV, CodeKind::TURBOFAN_JS, JS-linkage CodeKind::BUILTIN, or CodeKind::JS_TO_WASM_FUNCTION).ENTRY, CONSTRUCT, EXIT, INTERNAL, BUILTIN_CONTINUATION, WASM, etc.):StackFrame::TypeToMarker(type) (!= JAVASCRIPT) directly in [fp + TypedFrameConstants::kFrameTypeOffset], so ComputeStackFrameType can classify them without ever confusing a corrupted or unboxed slot in a JS frame with a TypedFrame marker (or vice versa).StackFrame::Type Taxonomy (src/execution/frames.h)FRAME_TYPE_LIST in src/execution/frames.h defines the concrete StackFrame subclasses:
| Category | StackFrame::Type Enum (StackFrame Class) | Role |
|---|---|---|
| C++ -> JS Entry | ENTRY (EntryFrame)CONSTRUCT_ENTRY (ConstructEntryFrame) | Pushed by JSEntryTrampoline when C++ invokes JS (Execution::Call / Construct). Saves C++ callee-saved registers and links to the previous c_entry_fp. |
| JS -> C++ Exit | EXIT (ExitFrame)BUILTIN_EXIT (BuiltinExitFrame)API_CALLBACK_EXIT (ApiCallbackExitFrame)API_ACCESSOR_EXIT (ApiAccessorExitFrame) | Pushed by CEntry or API stubs when JS calls into a C++ RUNTIME_FUNCTION, C++ builtin, or v8::FunctionCallback. Records Isolate::c_entry_fp() so stack walking can jump across arbitrary C++ compiler frames! |
| JavaScript Execution Tiers | INTERPRETED (InterpretedFrame)BASELINE (BaselineFrame)MAGLEV (MaglevFrame)TURBOFAN_JS (TurbofanJSFrame) | The four JavaScript execution tiers (UnoptimizedJSFrame base for Ignition/Sparkplug; OptimizedJSFrame base for Maglev/TurboFan). |
| Construct & Argument Adapters | CONSTRUCT (ConstructFrame)FAST_CONSTRUCT (FastConstructFrame) | Wraps new Foo() calls to enforce ECMAScript constructor return-value semantics (returning the allocated receiver if the constructor returns a primitive). |
| Deopt & Builtin Continuations | IRREGEXP (IrregexpFrame)INTERNAL (InternalFrame)TURBOFAN_STUB_WITH_CONTEXT (TurbofanStubWithContextFrame)BUILTIN_CONTINUATION (BuiltinContinuationFrame)JAVASCRIPT_BUILTIN_CONTINUATION (JavaScriptBuiltinContinuationFrame)JAVASCRIPT_BUILTIN_CONTINUATION_WITH_CATCH (...WithCatchFrame) | Internal helper frames and synthetic frames reconstructed by the Deoptimizer when an inlined builtin deoptimizes mid-operation. |
| WebAssembly & JSPI | WASM (WasmFrame), WASM_TO_JS, JS_TO_WASM, WASM_INTERPRETER_ENTRY, WASM_DEBUG_BREAK, C_WASM_ENTRY, WASM_EXIT, WASM_LIFTOFF_SETUP, WASM_SEGMENT_START (WasmSegmentStartFrame) | WebAssembly execution, JS/Wasm boundary wrappers, and JS Promise Integration (JSPI) stack-switching segment headers. |
c_entry_fp and handler Chain)When V8 is executing inside C++ (e.g., Runtime_LoadIC_Miss), the C++ compiler's stack frames on top of ExitFrame do not have V8 frame headers. How does StackFrameIterator start from C++ and find the topmost JS frame?
ExitFrame, the stub stores the ExitFrame's fp in Isolate::c_entry_fp().EntryFrame), the EntryFrame saves the outer c_entry_fp on the stack (EntryFrameConstants::kDirectCallerFPOffset) and links into ThreadLocalTop.StackFrameIterator starts at Isolate::c_entry_fp(), walks fp links upward through JS frames until it hits an EntryFrame, and then jumps across the C++ gap directly to the previous saved c_entry_fp!StackFrameIterator vs StackFrameIteratorForProfiler (src/execution/frames.h)V8 provides two distinct frame iterators for very different operating environments:
StackFrameIterator (Synchronous, Exact In-Runtime Walking)Used by GC root scanning, Isolate::Throw, Deoptimizer, and synchronous Error.stack capture.
fp chain.FrameSummary / JavaScriptFrameIterator): A single physical MAGLEV or TURBOFAN_JS frame may represent multiple inlined JS functions. Calling frame->Summarize(&frames) decodes the SafepointEntry's deoptimization_index in DeoptimizationData to expand the single physical frame into a vector of logical FrameSummary records (innermost to outermost).TdzHole): When a derived class constructor throws before super() (or while a builtin/API constructor frame is on the stack), frame->receiver() is tdz_hole_value() (TdzHole). All three stack-trace receiver consumers -- CallSiteBuilder::AppendFrame (src/execution/isolate.cc), StringStream::PrintPrototype (src/strings/string-stream.cc), and DebugStackTraceIterator::GetReceiver (src/debug/debug-stack-trace-iterator.cc) -- must check IsTdzHole (rather than IsTheHole) to avoid dereferencing TdzHole as a JSReceiver.ArgumentsMarker): While Deoptimizer::MaterializeHeapObjects() is allocating escape-analyzed objects for reconstructed stack frames, a frame's closure (function) or receiver slot may temporarily hold arguments_marker(). Stack walkers reachable from allocation hooks or crash printing (SamplingHeapProfiler::AddStack, AllocationTracker::AllocationEvent, JavaScriptFrame::Print, and StringStream::PrintPrototype) must check IsJSFunction(frame->unchecked_function()) and IsArgumentsMarker(receiver) before dereferencing them.StackFrameIteratorForProfiler (Asynchronous Signal-Handler Safe Walking)Used by V8's sampling CPU profiler (TickSample / CpuProfiler), which interrupts the target thread at completely arbitrary machine instructions via an OS signal (SIGPROF on POSIX) or thread suspension.
Why can't a signal handler use StackFrameIterator?
push rbp; mov rbp, rsp has finished) or epilogue (after pop rbp before ret), or inside a frameless stub (IsNoFrameBytecodeHandlerPc).StackFrameIteratorForProfiler):IsAligned(..., kSystemPointerSize)) alongside stack bounds for every candidate sp and fp (IsValidStackAddress / IsValidFramePointer / IsValidState), preventing misaligned reads if SIGPROF interrupts a signal handler (such as the Wasm trap handler) or frame-pointer-less C++ helper while js_entry_sp is active and c_entry_fp is null.IsValidFrameType before dereferencing frame markers.top_frame_type_ and registers from TickSample (pc, fp, sp, lr) to recover the caller when stopped mid-prologue/epilogue.[!CAUTION] Never Add
V8_EXPORT_PRIVATEtoStackFrameIteratorForProfiler! As documented insrc/execution/frames.h,StackFrameIteratorForProfilerruns inside an asynchronous POSIX signal handler (SIGPROF) on the interrupted thread‘s stack -- which may be right at the edge of a stack overflow! IfStackFrameIteratorForProfilermethods were exported across shared-library boundaries (V8_EXPORT_PRIVATEin component builds), calling them would invoke the dynamic linker’s lazy PLT resolver (_dl_runtime_resolve), which allocates a large stack frame and crashes with a stack overflow inside the signal handler.
Historically, every V8 C++ runtime function had to wrap all heap pointers in indirect Handle<T> slots allocated in a HandleScope table because the GC only knew how to scan HandleScopes and tagged slots described by SafepointTables in JIT frames.
Modern V8 combines Precise Frame Scanning with Conservative Stack Scanning (CSS) (src/heap/base/stack.h, ConservativeStackVisitor):
StackFrame::Iterate):InterpretedFrame and BaselineFrame, every bytecode register slot on the stack is guaranteed to hold a valid tagged value (initialized to undefined or stale_register / tdz_hole), so the GC visits all parameter and register slots directly.MaglevFrame and TurbofanJSFrame, local stack slots and CPU registers may hold raw unboxed 32-bit/64-bit integers or floats that look like heap pointers! Therefore, the compiler emits a SafepointTable (or MaglevSafepointTable) recording a bitmap of which stack spill slots (VisitSpillSlots) and callee-saved registers hold tagged pointers at each call PC.ConservativeStackVisitor):c_entry_fp segments), modern V8 allows DirectHandle<T> -- a zero-indirection wrapper holding a direct Tagged<T> pointer on the C++ CPU stack or in a CPU register!ConservativeStackVisitor scans raw words on the C++ stack, checks whether each word falls within a valid V8 heap page in the pointer-compression cage and points to a valid HeapObject allocation boundary (MarkingBitmap), and conservatively marks and pins those objects so they are not moved while C++ holds direct stack pointers to them.src/execution/frames.h, src/execution/frames-inl.h, & src/execution/frames.cc: StackFrame hierarchy, StackFrameIterator, StackFrameIteratorForProfiler, and FrameSummary.src/execution/frame-constants.h: Architecture-independent and architecture-specific frame slot offsets (StandardFrameConstants, UnoptimizedFrameConstants, TypedFrameConstants).src/codegen/safepoint-table.h & src/codegen/maglev-safepoint-table.h: Safepoint bitmaps for tagged spill slots and registers in optimized frames.src/heap/conservative-stack-visitor.h & .cc: Conservative Stack Scanning (CSS) for DirectHandle<T>.