Data race on lifoBlocksToFree during compacting GC causes null deref [@ GCRuntime::freeFromBackgroundThread]
Categories
(Core :: JavaScript: GC, defect)
Tracking
()
People
(Reporter: bugmon, Unassigned)
Details
(5 keywords)
Attachments
(2 files)
Summary
Data race in SpiderMonkey's GC background-free infrastructure causing a SingleLinkedList invariant violation (head_ == null, last_ != null) and subsequent null-pointer dereference. The crash occurs at js/src/ds/LifoAlloc.h:387 in popFirst(), triggered when GCRuntime::freeFromBackgroundThread() (Sweeping.cpp:585) operates on a LifoAlloc that received corrupted state from the shared GCRuntime::lifoBlocksToFree via transferFrom(). Root cause: the compacting-GC path directly consumes lifoBlocksToFree without joining the already-scheduled BackgroundFreeTask, and/or GCParallelTask double-scheduling when helper threads are saturated by off-thread baseline batching.
Steps to Reproduce
Shell flags required: --fuzzing-safe --baseline-batching=on --baseline-queue-capacity=64
Run test.js under the SpiderMonkey JS shell. The testcase:
- Lowers GC heap budget (
gcparam("maxBytes", 20MB)) - Generates 80 unique
eval'd closures per round → batches them for off-thread Baseline compilation (saturating helper threads) - Allocates heavy garbage including
"x".repeat(10)strings to populatelifoBlocksToFreewith oversize chunks - Calls
gc(gc, "shrinking")repeatedly → enters the compacting-GC code path
The crash reproduced on 3/3 independent runs within a few seconds. Removing "shrinking" from the gc() call eliminates the crash, confirming the compacting-GC path is a required participant.
Affected Code
File: js/src/ds/LifoAlloc.h, line 387 — crash site
UniquePtr<T, D> popFirst() {
MOZ_ASSERT(head_); // no-op in release build
UniquePtr<T, D> result = std::move(head_); // result.get() == nullptr
head_ = std::move(result->next_); // ★ line 387: nullptr->next_ → SEGV@0x0
...
}
The loop guard in LifoAlloc::freeAll() (LifoAlloc.cpp:125) is while (!oversize_.empty()), but empty() checks last_ not head_:
bool empty() const { return !last_; } // LifoAlloc.h — tests wrong field for this purpose
When the invariant bool(head_) == bool(last_) is broken by concurrent mutation, empty() returns false, the loop is entered, and popFirst() dereferences a null head_.
Register evidence (from the crash dump):
| Register | Value | Meaning |
|---|---|---|
rax |
0x0 |
result.release() → the moved-from head_ was null |
r13 |
0x0 |
head_ field value |
rbx |
0x7a0e66bf1220 |
&oversize_ (base of local lifoBlocks list) |
r12 |
0x7a0e66bf1230 |
&oversize_.head_ (+0x10) |
r15 |
0x7a0e66bf1238 |
&oversize_.last_ (+0x18) — still non-null |
File: js/src/gc/Sweeping.cpp, line 585 — victim consumer
void GCRuntime::freeFromBackgroundThread(AutoLockHelperThreadState& lock) {
do {
LifoAlloc lifoBlocks(...); // stack-local on helper thread
lifoBlocks.transferFrom(&lifoBlocksToFree.ref()); // ← copies corrupt {head_,last_} pair
...
AutoUnlockHelperThreadState unlock(lock); // lock DROPPED
lifoBlocks.freeAll(); // ★ line 585 → crash
} while (hasBuffersForBackgroundFree()); // lock reacquired for check
}
transferFrom() → prependAll() → steal() copies head_ and last_ as two independent, non-atomic stores and performs no invariant validation, so a torn {null, non-null} state propagates silently.
File: js/src/gc/Compacting.cpp, line 864 — racing consumer
// inside updateRuntimePointersToRelocatedCells — only reached during shrinking GC
{
AutoLockHelperThreadState lock;
lifoBlocksToFree.ref().freeAll(); // ★ directly mutates shared list WITHOUT freeTask.join()
}
Diagnostic evidence of concurrency: A second AddressSanitizer:DEADLYSIGNAL is interleaved into the ASan report (immediately after Thread T4 created by T0 here:). This is the signature of another thread faulting while ASan is mid-report on T4 — proving two threads crashed on related state near-simultaneously.
Exploit Chain
- JS entry:
gc(gc, "shrinking")invoked from content script under lowmaxBytes. - Helper saturation:
--baseline-batching=on --baseline-queue-capacity=64+ 80 eval'd closures fill all helper threads with long-runningBaselineCompileTasks. TheBackgroundFreeTasksits inQueuedstate or is delayed. - Free-queue population: Memory pressure forces minor GCs;
ICStubSpace::freeAllAfterMinorGC/ICScript::prepareForDestructionqueue oversizeBumpChunks intolifoBlocksToFree.freeTaskis started. - Helper thread T4 picks up
freeTask→GCParallelTask::runHelperThreadTask→freeFromBackgroundThread: takes lock,transferFrom(), drops lock, entersfreeAll(). - Main thread T0 enters compacting phase →
updateRuntimePointersToRelocatedCells→Compacting.cpp:864: takes lock (T4 released it), callsfreeAll()directly onlifoBlocksToFreewithout joiningfreeTask. - Invariant torn:
popFirst()performs two sequential non-atomic stores (head_ = …, then conditionallast_ = nullptr). Interleaving across the T4/T0 lock-drop-reacquire boundary — or double-scheduling offreeTaskitself whenGCParallelTask::wasStarted()fails to recognizeQueuedstate — leavesoversize_withhead_ = null, last_ = stale. - Corruption propagation: On T4's next loop iteration,
transferFrom()copies the broken pair into the locallifoBlocks. - Crash:
while (!oversize_.empty())seeslast_ != null→ enters →popFirst()readshead_ == null→nullptr->next_→ SEGV at0x000000000000.
Security Impact
| Property | Assessment |
|---|---|
| Severity | Moderate–High |
| Immediate effect | Helper-thread null-deref → process crash (DoS) |
| Escalation potential | The surviving last_ is a dangling raw pointer to a BumpChunk disconnected from head_. Any path reaching oversize_.last()->... (e.g. LifoAlloc::isEmpty()) before the crash would perform a use-after-free. Depending on interleaving, the race could also manifest as double-free of BumpChunks rather than null-deref. |
| Attacker control | Content JavaScript can reliably trigger via memory-pressure loops + closures + gc(). No shell-only primitives needed. |
| Preconditions | Off-thread baseline batching enabled; shrinking/compacting GC must occur; ≥2 helper threads. Batching is on the shipping track, and shrinking GCs fire organically under real memory pressure. |
Suggested Fix
Primary fix — join freeTask before the compacting phase directly drains lifoBlocksToFree:
// js/src/gc/Compacting.cpp (inside updateRuntimePointersToRelocatedCells)
- {
- AutoLockHelperThreadState lock;
- lifoBlocksToFree.ref().freeAll();
- }
+ // BackgroundFreeTask::freeFromBackgroundThread() drops the helper-thread lock
+ // between transferFrom() and its loop condition. Holding the lock here is
+ // insufficient: the task's next iteration can observe a torn oversize_ list.
+ // Join the task before directly consuming lifoBlocksToFree.
+ freeTask.join();
+ {
+ AutoLockHelperThreadState lock;
+ lifoBlocksToFree.ref().freeAll();
+ }
Defense-in-depth — close the Queued-state gap in GCParallelTask:
// js/src/gc/GCParallelTask.h
bool wasStarted(const AutoLockHelperThreadState& lock) const {
- return isDispatched(lock) || isRunning(lock);
+ return isQueued(lock) || isDispatched(lock) || isRunning(lock);
}
This prevents startOrRunIfIdle() from double-scheduling a task that is queued but not yet dispatched when all helper threads are busy.
| Reporter | ||
Comment 1•8 months ago
|
||
Updated•8 months ago
|
Updated•8 months ago
|
Comment 3•8 months ago
|
||
Comment 4•8 months ago
|
||
This looks very similar to bug 2022997, which also purported to find bugs involving lifoBlocksToFree. In that case I concluded that the real problem was bug 2022337.
I failed to reproduce this locally, even using rr chaos mode. I tried running with the problem from bug 2022337 reinserted; that didn't fail normally, but did fail in chaos mode. In that case, head_ is 0x0 but last_ is 0x3, which is the bogus value written by jitcode. This is consistent with the description above:
When the invariant bool(head_) == bool(last_) is broken by concurrent mutation, empty() returns false, the loop is entered, and popFirst() dereferences a null head_.
Much like bug 2022997, I find it difficult to follow Claude's proposed chain of events. I once again suspect that Claude is having a hard time reasoning about (or remembering?) the bug in jitcode clobbering the LifoAlloc's memory, and tries to invent a story that doesn't include the jitcode bug. That story doesn't make any sense, because it's not a true story.
It looks to me like all the locking here is working just fine. Jon, can you take a quick look at this to make sure I'm not missing anything obvious? Assuming I'm not, I think we should be fine to close this as a duplicate of 2022997.
Comment 5•8 months ago
|
||
(In reply to Iain Ireland [:iain] from comment #4)
Thanks for looking into this Iain.
The locking is fine here. lifoBlocksToFree is wrapped in HelperThreadLockData and we take the helper thread lock whenever we access it.
I'll dupe to bug 2022997.
Comment 6•8 months ago
|
||
Can we add the commit the crash was reproduced at to these bugs in the future?
Updated•8 months ago
|
Updated•8 months ago
|
Comment 8•8 months ago
|
||
(In reply to Jon Coppeard (:jonco) from comment #6)
Can we add the commit the crash was reproduced at to these bugs in the future?
Updated•8 months ago
|
Updated•1 month ago
|
Description
•