StoreBuffer::mayHavePointersToDeadCells_ lost across a semispace minor GC re-adding dead-cell edges (Crash [@ JS::Value::unboxGCPointer] / GC use-after-free)
Categories
(Core :: JavaScript: GC, defect, P3)
Tracking
()
People
(Reporter: decoder, Assigned: jonco)
References
(Blocks 1 open bug)
Details
(4 keywords)
Attachments
(2 files, 1 external file)
Summary
StoreBuffer::mayHavePointersToDeadCells_ (js/src/gc/StoreBuffer.h / StoreBuffer.cpp) is the coupling flag for a mandatory deferred handoff: it records that the store buffer may contain edges into dead tenured cells (added by brain transplants while a major GC is in flight). GCRuntime::prepareForSweepSlice() reads it and evicts the nursery (flushing every store buffer edge) before any sweeping, so that no store buffer entry can outlive the cell it points into:
// js/src/gc/Sweeping.cpp:2688
// Clear out whole cell store buffer entries to unreachable cells.
if (storeBuffer().mayHavePointersToDeadCells()) {
collectNurseryFromMajorGC(sliceReason);
}
With the semispace nursery enabled, a minor GC that runs while a major GC is in flight resets this flag while silently carrying the guarded dead-cell edges over into the fresh store buffer. The protective eviction is then skipped, the dead cell is swept and finalized, and the next minor GC traces a stale ValueEdge into the freed cell — a GC use-after-free (8-byte read, and a controlled-ish forwarded-pointer write, into freed/reallocated GC heap memory).
Tested on SpiderMonkey JS shell m-c-asan-debug, product_version 20260603-5cffec5992d3.
Root cause
The flag is cleared whenever the store buffer is reset — StoreBuffer::clear() (js/src/gc/StoreBuffer.cpp:347-364) and, on the normal minor GC path, by the store buffer swap in Nursery::traceRoots (js/src/gc/Nursery.cpp:1656-1667): a fresh StoreBuffer (constructed with mayHavePointersToDeadCells_ = false, js/src/gc/StoreBuffer.cpp:212-226) is std::swaped in via the move constructor/assignment (js/src/gc/StoreBuffer.cpp:228-262), and the old buffer is traced and discarded.
The clearing path assumes a minor GC fully retires all buffered edges, making false correct afterwards. That is wrong with the semispace nursery (--gc-param=semispaceNurseryEnabled=1): while tracing the old buffer, any edge whose target stays in the nursery (young cells survive one extra cycle in semispace mode) is re-added to the new store buffer:
// js/src/gc/Tenuring.cpp:845 (ValueEdge::trace)
TenuringTracer::AutoPromotedAnyToNursery promotedToNursery(mover);
mover.traverse(edge);
if (promotedToNursery) {
mover.runtime()->gc.storeBuffer().putValue(edge); // re-added
}
The dead-cell edges the flag was guarding are carried into the new buffer, but nothing propagates the flag — only a partial state transfer is performed. After this minor GC the engine believes the store buffer is "clean": prepareForSweepSlice() (js/src/gc/Sweeping.cpp:2688-2691) skips the mandatory eviction, the in-flight major GC sweeps and finalizes the dead cell, and the store buffer keeps an interior pointer into the freed cell. The next minor GC traces the stale edge.
Key locations:
- js/src/gc/StoreBuffer.cpp:228-262 — move ctor/assignment used by the swap, installs
mayHavePointersToDeadCells_ = falsein the live buffer. - js/src/gc/StoreBuffer.cpp:347-364 —
StoreBuffer::clear()resets the flag with the same assumption. - js/src/gc/Tenuring.cpp:845-871 (ValueEdge/WasmAnyRefEdge), 836-842 (CellPtrEdge), 500-507 (whole cells) — re-add paths that preserve edges across a semispace minor GC without preserving the flag.
- js/src/gc/Sweeping.cpp:2688-2691, 2733 — consumers of the broken invariant.
Reproduction
Debug ASan build:
js --fuzzing-safe --gc-param=semispaceNurseryEnabled=1 poc.js
poc.js (attached): for each round it (1) creates a cross-compartment wrapper W for a transplantableObject(), tenures it, then drops it so it is reachable only via the weak wrapper map; (2) runs a zone GC of only the wrapper's zone (schedulezone+startgc) so nothing marks W — it is doomed; (3) calls transplant(gTarget) while the zone GC is marking, so ProxyObject::swap writes the nursery-allocated new identity into the doomed wrapper's private slot (creates the ValueEdge into the dead cell and sets mayHavePointersToDeadCells_); (4) minorgc() — the bug — re-adds the dead-cell edge to the fresh store buffer while the flag is reset; (5) finishes the major GC with gcslice(), sweeping/finalizing the dead wrapper; (6) gcparam("maxBytes", ...) joins background finalization so the swept cell is poisoned; (7) minorgc() traces the stale edge → crash. Reproduces 5/5 on the debug ASan build.
Instrumented runs (gcparam("minorGCNumber")) confirm the broken invariant directly: transplant-during-GC with no intervening minor GC performs exactly 1 protective nursery eviction (flag honored); with a semispace minor GC in between, 0 protective evictions (flag lost), while hardware watchpoints confirm the dead-cell edge is still in the store buffer and is traced (read + write through the stale pointer) by the next minor GC after the cell is swept.
Crash signature
Assertion failure: (asBits_ & js::gc::CellAlignMask) == 0 (GC pointer is not aligned. Is this memory corruption?), at dist/include/js/Value.h:713
#1 JS::Value::unboxGCPointer<JSObject, ...>() Value.h:712
#2 JS::Value::toObject() const Value.h:923
#5 js::CheckTracedThing<JS::Value>(JSTracer*, JS::Value const&) gc/Marking.cpp:201
#6 js::gc::TenuringTracer::traverse(JS::Value*) gc/Tenuring.cpp:269
#7 js::gc::StoreBuffer::ValueEdge::trace(js::gc::TenuringTracer&) const gc/Tenuring.cpp:852
#8 js::gc::StoreBuffer::MonoTypeBuffer<ValueEdge>::trace(...) gc/Tenuring.cpp:384
#9 js::Nursery::traceRoots(...) gc/Nursery.cpp:1674
The minor GC traces a store buffer ValueEdge whose slot lives inside a swept (finalized, JS_SWEPT_TENURED_PATTERN-poisoned) cell; the poisoned slot fails the GC-pointer alignment assertion — the GC is consuming freed memory. In release builds the stale read/write proceeds silently into freed or reallocated GC memory (the opt ASan build does not crash because the Value-shaped poisoning is debug-only). Full output in crash_output.txt.
Suggested fix
Either re-set mayHavePointersToDeadCells_ on the new store buffer whenever edges are re-added during a minor GC that runs while JS::RuntimeHeapIsMajorCollecting(), or simply preserve the old buffer's flag across the swap in Nursery::traceRoots when a major GC is in progress.
Notes on configuration
The bug requires the semispace nursery, which is off by default on all channels (GC param semispaceNurseryEnabled / browser pref javascript.options.mem.gc_experimental_semispace_nursery, both default false) — hence unsupported-config. The re-add of buffered edges across a minor GC only occurs in semispace mode, so the flag-loss is specific to that configuration. The remaining ingredients are all ordinary browser behavior: JS_TransplantObject is the window/remote-proxy navigation machinery, zone GCs are the browser's default GC granularity, and the shell gcslice()/startgc() calls only make incremental-GC timing deterministic.
Files
poc.js— testcase (run with--fuzzing-safe --gc-param=semispaceNurseryEnabled=1).crash_output.txt— full assertion + ASan output (debug ASan build).
| Reporter | ||
Comment 1•5 months ago
|
||
| Reporter | ||
Comment 2•5 months ago
|
||
Updated•5 months ago
|
| Assignee | ||
Updated•5 months ago
|
| Assignee | ||
Comment 3•5 months ago
|
||
Semispace nursery collections that don't tenure everything may leave these
pointers in the store buffer so we need to preserve the flag. We could make
this more precise but there's not much point.
Comment 5•4 months ago
|
||
Updated•4 months ago
|
Updated•3 months ago
|
Updated•21 days ago
|
Description
•