raw
current commit
261004.7 · deps: update V8 to 15.2.124.38

Stack Walking in V8

Stack walking is the process of traversing the call stack to inspect active frames. V8 uses stack walking for several critical features:

Frame Types

V8 has a rich set of frame types, as execution can transition between interpreted JavaScript, optimized JavaScript, WebAssembly, and C++ host code.

Key frame types (defined in src/execution/frames.h):

The Stack Frame Iterator

The primary mechanism for stack walking in C++ is the StackFrameIterator class (and its variants like DebuggableStackFrameIterator).

for (StackFrameIterator it(isolate); !it.done(); it.Advance()) {
  StackFrame* frame = it.frame();
  // Inspect frame...
}

How it Works:

  1. Starting Point: The iterator typically starts from the current top of the stack (retrieved from the isolate’s saved state or current CPU registers).
  2. Frame Identification: V8 identifies the type of frame by looking at the Frame Marker (a special value stored in the frame) or by looking up the PC (Program Counter) in the instruction stream of active code objects.
  3. Navigation: The iterator uses the saved Frame Pointer (FP) to find the caller’s frame. Standard V8 frames maintain a consistent link chain of FPs.

Standard Frame Layout

Most V8 frames follow a standard layout (though optimized frames may vary to save space):

                      +---------------------------+
                      |  Caller's Frame Pointer   |  <- Higher addresses
                      +---------------------------+
                      |  Caller's Return Address  |
                      +---------------------------+
                      |  Context / Marker         |
                      +---------------------------+
                      |  Function / Code Object   |  <- Current FP points here
                      +---------------------------+
                      |  ... Locals / Stack ...   |  <- SP points to the top
                      +---------------------------+

GC Stack Scanning

During Garbage Collection, V8 must scan the stack to find all root pointers to heap objects. This process relies on the stack walking infrastructure.

Precise vs. Conservative Scanning

V8 primarily uses Precise Stack Scanning. It knows exactly which slots on the stack contain tagged pointers and which contain raw data (like integers or floats).

Handling Arguments

Arguments to functions can be passed on the stack or in registers, depending on the calling convention and execution tier.

Safe Stack Walking

Stack walking must be done safely, especially when handling signals for the CPU profiler or during GC when the stack might be in an inconsistent state.


See Also