Browse documentation

A collection clears the dead pointers past the value stack's top

Date: 2026-11-29 Area: gc, engine Related: Garbage Collector, #2325

Context

A collection scans the value stack up to StackTop. The slots past it are dead registers: those of the frames that returned, and those of a live frame above its current call window. A collection neither scans nor updates them, so a pointer among them that points into the nursery, or at an old cell a major collection frees, is stale afterwards.

That is harmless until something raises StackTop over the slot again without writing it, and several paths do:

  • A return sets StackTop to the end of the caller's registers, which can be below the end of a frame further out (a small function called from a large one). The next return to the larger frame raises it again.
  • An unwind to a handler, and the optimizing tier's ORaiseTop after an embedded call, raise it to a frame's end.
  • A compiled field initializer's frame (XJitFieldInit) is placed past the constructor's registers.

Each path had been fixed on its own where it showed up: a native call clears the caller's registers past its arguments when it collected (ClearDeadAfterNative, the JIT's native stub), and an optimized region's entry clears the inlined callees' registers it covers (XOptCoverRegs). Returns had no such clear, and Response.prototype.text's tests crashed under a 16 KB nursery: TextDecoder.prototype.decode raised StackTop back over a register into which a deeper native call had written its this, and the next collection followed it into the reused nursery.

Two other ways were weighed:

  • Clear at every return that raises StackTop. About thirty sites set it, some of them in emitted machine code, and each future path would need the same care.
  • Give each frame the StackTop its push left, and return to that. StackTop would then never drop below an outer frame's end, but every push and return, in the interpreter, the baseline JIT's emitted code and the optimizing tier, would carry a new frame field.

Decision

  • The collection clears them. Before it scans, ScanRoots sets to undefined every pointer past StackTop, up to the end of the registers of the frame that reaches furthest (ClearDeadPastStackTop). Values that are not pointers stay: nothing goes stale in them.
  • A frame's end includes its optimized regions' registers. A template's CoverRegs records the most registers an optimized region of it covers with StackTop (its ORegs past MaxRegs), set when the region is emitted. The end of a frame is its base plus the larger of MaxRegs and CoverRegs.
  • A raise past every frame's end writes what it exposes. XJitFieldInit sets the slots between StackTop and the initializer's frame to undefined, as a frame push does for its own registers.
  • The per-path clears stay. After a native call that collected, they also drop what the call's frames left in the caller's dead registers, which the caller would otherwise keep alive.

Consequences

  • No path that raises StackTop over a frame's dead registers meets a stale pointer, including paths added later.
  • Each collection walks the frames once more and touches the slots between StackTop and the furthest frame end, which is small beside the scan itself.
  • A value written past StackTop and expected to survive a collection loses its pointers. Nothing may rely on that already: such a pointer was stale after any collection that moved or freed its cell.
  • scripts/engine/regress/return-dead-registers.js and scripts/engine/regress/jit-field-init-gap.js reproduce the two paths. The Engine workflow runs both under small nurseries with --gc-verify on every pull request, and the first on i386-win32 too.