← All reports

[JSC] WeakBlock::destroy shouldn't be called while marking

Clearing a Weak handle could free memory a GC marker was still reading

Component: JavaScriptCore GC | b1f6566

Source/JavaScriptCore/heap/Heap.cpp

- if (isConcurrent == IsConcurrent::No) {
- unsigned maxPooled = maxPooledWeakBlocks();
- while (m_pooledWeakBlockCount > maxPooled)
- WeakBlock::destroy(*this, takeWeakBlockFromPool());
- }
+ if (isConcurrent == IsConcurrent::No && !m_objectSpace.isMarking())
+ trimWeakBlockPool();
+}
+
+void Heap::trimWeakBlockPool()
+{
+ unsigned maxPooled = maxPooledWeakBlocks();
+ while (m_pooledWeakBlockCount > maxPooled)
+ WeakBlock::destroy(*this, takeWeakBlockFromPool());
}
...
+void Heap::trimPooledWeakBlocksAfterMarking()
+{
+ ASSERT(isInPhase(CollectorPhase::End));
+ Locker locker { m_weakBlockLock };
+ trimWeakBlockPool();
+}

Source/JavaScriptCore/heap/WeakBlock.cpp

+ RELEASE_ASSERT(!heap.m_objectSpace.isMarking());

JSC's Weak<T> handles are backed by WeakImpl slots stored in WeakBlocks, and emptied blocks go back to a heap-level pool guarded by m_weakBlockLock. Concurrent GC marker threads read a Weak<>'s WeakImpl* in SlotVisitor::append without taking that lock, while the mutator keeps running and can clear or reassign a Weak, freeing its slot. When that was the last live slot, the block was returned to the pool and trimmed immediately with WeakBlock::destroy, so a marker could still be holding a pointer into a block that had just been freed.

Emptied blocks are now pooled but not destroyed while m_objectSpace.isMarking(). A new Heap::trimPooledWeakBlocksAfterMarking() runs from endMarking() under m_weakBlockLock and trims the pool back to maxPooledWeakBlocks(). sweepSynchronously() destroys all pooled blocks only when no marking is in progress, and WeakBlock::destroy gains a RELEASE_ASSERT against destroy-during-marking. A JSC stress test using new createWeakHolder/setWeakHolderValue helpers in jsc.cpp runs under --collectContinuously.

Before:                                  After:
Mutator               Marker             Mutator                Marker
 Weak::clear()         visit(owner)       Weak::clear()          visit(owner)
  └─ last slot freed    └─ load WeakImpl*  └─ returnToPool         └─ load WeakImpl*
     └─ trim → destroy        │               └─ isMarking(): skip     (block pooled, valid)
        (block freed) ──────► read (UAF)   Heap::endMarking()
                                            └─ trimPooledWeakBlocksAfterMarking()

This closes a use-after-free read of GC metadata, a mutator/marker race reachable from web content through WebCore's common Weak<>-in-marked-fields idiom. It was introduced by the WeakBlock redesign in 321344@main, and the fix plus a stress test reproducing it roughly 1 in 6 runs are public in 326262@main, so builds containing the former but not the latter remain affected.

The fix is a policy check (isMarking()), not a change to how markers read blocks, and the new RELEASE_ASSERT makes any residual destroy-during-marking path a deterministic crash, which is easy to fuzz for. Audit the other block-freeing call sites (destroyAllPooledWeakBlocks, WeakSet shrink and teardown) for paths that bypass returnWeakBlockToPool. Check whether isMarking() is read consistently when the mutator starts the next collection, since the new sweepSynchronously comment notes collectNow() can already be in the next cycle. More broadly, look at other lockless WeakImpl* readers (finalizer state checks, WeakGCMap, WeakHandleOwner callbacks) that rely on the same lifetime assumption, and at pooled-block reuse, where a stale WeakImpl* would read a recycled slot rather than freed memory. --weakBlockPoolDivisor=0 plus --collectContinuously is a good starting harness.