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
StackTopto 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
ORaiseTopafter 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
StackTopits push left, and return to that.StackTopwould 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,
ScanRootssets toundefinedevery pointer pastStackTop, 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
CoverRegsrecords the most registers an optimized region of it covers withStackTop(itsORegspastMaxRegs), set when the region is emitted. The end of a frame is its base plus the larger ofMaxRegsandCoverRegs. - A raise past every frame's end writes what it exposes.
XJitFieldInitsets the slots betweenStackTopand the initializer's frame toundefined, 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
StackTopover 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
StackTopand the furthest frame end, which is small beside the scan itself. - A value written past
StackTopand 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.jsandscripts/engine/regress/jit-field-init-gap.jsreproduce the two paths. The Engine workflow runs both under small nurseries with--gc-verifyon every pull request, and the first on i386-win32 too.