Skip to content

Commit cf89f15

Browse files
committed
[JSC] Assert the invariants of Heap::m_worldState where the protocol relies on them
https://bugs.webkit.org/show_bug.cgi?id=329038 rdar://191078586 Reviewed by Keith Miller. The mutator-collector protocol relies on rules about each heap's world state, only some of which are asserted. Assert the rest where the code depends on them: - A mutator holds the conn, or waits for the collector, only while it has heap access, and a stopped mutator has neither: in acquireAccessSlow, releaseAccessSlow, resumeTheMutator, setMutatorWaiting, clearMutatorWaiting and requestCollection. - End sets the epilogue bit once per collection, while the mutator is stopped or conducting, and the heap's next collection begins only after the epilogue has run: in setNeedCollectionEpilogue, beginCollectionInEachHeap and finishChangingPhase. - While the world is stopped, each member heap's mutator is stopped or conducting: in Collector::stopThePeriphery. finishChangingPhase no longer calls handleNeedCollectionEpilogue() when a mutator conducts into a stopped phase, since the bit is never set there; it asserts that instead. Also add Heap::needCollectionEpilogue(), turn the log-then-crash checks in releaseAccessSlow, stopThePeriphery and resumeThePeriphery into release asserts that report the world state, and say why the epilogue must run when it does. Canonical link: https://commits.webkit.org/324850@main
1 parent cd20093 commit cf89f15

3 files changed

Lines changed: 41 additions & 24 deletions

File tree

‎Source/JavaScriptCore/heap/Collector.cpp‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ GCRequest::Ticket Collector::requestCollection(Heap& requester, GCRequest reques
235235
// right now. This is an optimization that prevents the collector thread from ever starting in most
236236
// cases.
237237
ASSERT(m_lastServedTicket <= m_lastGrantedTicket);
238+
ASSERT(requester.hasHeapAccess());
239+
ASSERT(!(requester.m_worldState.load() & Heap::stoppedBit));
238240
// It's okay for a heap to request a GC while its membership is pending, but it can't be the conductor yet.
239241
if ((m_lastServedTicket == m_lastGrantedTicket) && !m_threadIsWorking && hasHeap(requester)) {
240242
dataLogLnIf(CollectorInternal::verbose, "Taking the conn.");
@@ -482,6 +484,8 @@ void Collector::beginCollectionInEachHeap(CollectionScope scope, MonotonicTime s
482484
heap.m_currentGCStartTime = startTime;
483485
if (!Options::seedOfVMRandomForFuzzer())
484486
heap.vm().random().setSeed(cryptographicallyRandomNumber<uint32_t>());
487+
// The previous collection's epilogue must have run before starting the next collection.
488+
RELEASE_ASSERT(!heap.needCollectionEpilogue());
485489
heap.willStartCollection(scope);
486490
heap.beginMarking();
487491
});
@@ -733,6 +737,8 @@ NEVER_INLINE bool Collector::finishChangingPhase(GCConductor conn)
733737
if (suspendedBefore != suspendedAfter) {
734738
if (suspendedBefore) {
735739
RELEASE_ASSERT(!suspendedAfter);
740+
// End sets the epilogue bit just before resuming into NotRunning, and nothing else sets it during a collection.
741+
RELEASE_ASSERT(heap().needCollectionEpilogue() == (m_currentPhase == CollectorPhase::End));
736742

737743
resumeThePeriphery();
738744
if (conn == GCConductor::Collector)
@@ -753,7 +759,8 @@ NEVER_INLINE bool Collector::finishChangingPhase(GCConductor conn)
753759
}
754760
} else {
755761
sanitizeStackForVM(heap().vm());
756-
heap().handleNeedCollectionEpilogue();
762+
// A mutator runs any pending epilogue before it starts conducting, and only End sets the bit.
763+
RELEASE_ASSERT(!heap().needCollectionEpilogue());
757764
}
758765
stopThePeriphery();
759766
}
@@ -766,7 +773,12 @@ NEVER_INLINE bool Collector::finishChangingPhase(GCConductor conn)
766773
void Collector::stopThePeriphery()
767774
{
768775
m_isCompilerThreadsSuspended = suspendCompilerThreads();
769-
forEachHeap([](Heap& heap) {
776+
forEachHeap([&](Heap& heap) {
777+
unsigned worldState = heap.m_worldState.load();
778+
if (&heap == m_conductingHeap)
779+
RELEASE_ASSERT(worldState & Heap::mutatorHasConnBit, worldState);
780+
else
781+
RELEASE_ASSERT(worldState & Heap::stoppedBit, worldState);
770782
heap.stopThePeriphery();
771783
});
772784
m_collectorSlotVisitor->setMutatorsAreStopped(true);

‎Source/JavaScriptCore/heap/Heap.cpp‎

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,10 +1363,7 @@ void Heap::checkConn(GCConductor conn)
13631363

13641364
void Heap::stopThePeriphery()
13651365
{
1366-
if (m_worldIsStopped) {
1367-
dataLog("FATAL: world already stopped.\n");
1368-
RELEASE_ASSERT_NOT_REACHED();
1369-
}
1366+
RELEASE_ASSERT(!m_worldIsStopped);
13701367

13711368
if (m_mutatorDidRun)
13721369
m_mutatorExecutionVersion++;
@@ -1392,10 +1389,7 @@ void Heap::resumeThePeriphery()
13921389

13931390
m_barriersExecuted = 0;
13941391

1395-
if (!m_worldIsStopped) {
1396-
dataLog("Fatal: collector does not believe that the world is stopped.\n");
1397-
RELEASE_ASSERT_NOT_REACHED();
1398-
}
1392+
RELEASE_ASSERT(m_worldIsStopped);
13991393
m_worldIsStopped = false;
14001394
}
14011395

@@ -1442,6 +1436,7 @@ NEVER_INLINE void Heap::resumeTheMutator()
14421436
RELEASE_ASSERT(oldState & stoppedBit, oldState);
14431437
RELEASE_ASSERT(!(oldState & hasAccessBit), oldState);
14441438
RELEASE_ASSERT(!(oldState & mutatorHasConnBit), oldState);
1439+
RELEASE_ASSERT(!(oldState & mutatorWaitingBit), oldState);
14451440

14461441
if (m_worldState.compareExchangeWeak(oldState, oldState & ~stoppedBit)) {
14471442
dataLogLnIf(HeapInternal::verbose, "CASing and returning.");
@@ -1537,8 +1532,10 @@ void Heap::acquireAccessSlow()
15371532
{
15381533
for (;;) {
15391534
unsigned oldState = m_worldState.load();
1540-
RELEASE_ASSERT(!(oldState & hasAccessBit));
1541-
1535+
RELEASE_ASSERT(!(oldState & hasAccessBit), oldState);
1536+
RELEASE_ASSERT(!(oldState & mutatorHasConnBit), oldState);
1537+
RELEASE_ASSERT(!(oldState & mutatorWaitingBit), oldState);
1538+
15421539
if (oldState & stoppedBit) {
15431540
if (HeapInternal::verboseStop) {
15441541
dataLogLn("Stopping in acquireAccess!");
@@ -1564,15 +1561,10 @@ void Heap::releaseAccessSlow()
15641561
{
15651562
for (;;) {
15661563
unsigned oldState = m_worldState.load();
1567-
if (!(oldState & hasAccessBit)) {
1568-
dataLog("FATAL: Attempting to release access but the mutator does not have access.\n");
1569-
RELEASE_ASSERT_NOT_REACHED();
1570-
}
1571-
if (oldState & stoppedBit) {
1572-
dataLog("FATAL: Attempting to release access but the mutator is stopped.\n");
1573-
RELEASE_ASSERT_NOT_REACHED();
1574-
}
1575-
1564+
RELEASE_ASSERT(oldState & hasAccessBit, oldState);
1565+
RELEASE_ASSERT(!(oldState & stoppedBit), oldState);
1566+
RELEASE_ASSERT(!(oldState & mutatorWaitingBit), oldState);
1567+
15761568
if (handleNeedCollectionEpilogue(oldState))
15771569
continue;
15781570

@@ -1629,6 +1621,9 @@ void Heap::relinquishConn()
16291621
while (relinquishConn(m_worldState.load())) { }
16301622
}
16311623

1624+
// The epilogue clears VM caches that hold cells marking did not keep alive, so a mutator runs it before returning from
1625+
// the heap. It also finishes the collection's own bookkeeping, which the heap's next collection would overwrite, so it
1626+
// must run before the next collection begins.
16321627
NEVER_INLINE bool Heap::handleNeedCollectionEpilogue(unsigned oldState)
16331628
{
16341629
RELEASE_ASSERT(oldState & hasAccessBit);
@@ -1653,7 +1648,10 @@ void Heap::handleNeedCollectionEpilogue()
16531648

16541649
void Heap::setNeedCollectionEpilogue()
16551650
{
1656-
m_worldState.exchangeOr(needCollectionEpilogueBit);
1651+
unsigned oldState = m_worldState.exchangeOr(needCollectionEpilogueBit);
1652+
// Only End sets this, once per collection, while the mutator is stopped or conducting.
1653+
RELEASE_ASSERT(!(oldState & needCollectionEpilogueBit), oldState);
1654+
RELEASE_ASSERT((oldState & stoppedBit) || (oldState & mutatorHasConnBit), oldState);
16571655
ParkingLot::unparkAll(&m_worldState);
16581656
m_stopIfNecessaryTimer->scheduleSoon();
16591657
}
@@ -1674,12 +1672,18 @@ void Heap::waitWhileNeedCollectionEpilogue()
16741672

16751673
void Heap::setMutatorWaiting()
16761674
{
1677-
m_worldState.exchangeOr(mutatorWaitingBit);
1675+
unsigned oldState = m_worldState.exchangeOr(mutatorWaitingBit);
1676+
RELEASE_ASSERT(oldState & hasAccessBit, oldState);
1677+
RELEASE_ASSERT(!(oldState & stoppedBit), oldState);
16781678
}
16791679

16801680
void Heap::clearMutatorWaiting()
16811681
{
1682-
m_worldState.exchangeAnd(~mutatorWaitingBit);
1682+
unsigned oldState = m_worldState.exchangeAnd(~mutatorWaitingBit);
1683+
if (oldState & mutatorWaitingBit) {
1684+
RELEASE_ASSERT(oldState & hasAccessBit, oldState);
1685+
RELEASE_ASSERT(!(oldState & stoppedBit), oldState);
1686+
}
16831687
}
16841688

16851689
void Heap::runCollectionEpilogue()

‎Source/JavaScriptCore/heap/Heap.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ class Heap {
371371
MutatorState mutatorState() const { return m_mutatorState; }
372372
std::optional<CollectionScope> collectionScope() const { return m_collectionScope; }
373373
bool hasHeapAccess() const { return m_worldState.load() & hasAccessBit; }
374+
bool needCollectionEpilogue() const { return m_worldState.load() & needCollectionEpilogueBit; }
374375
bool worldIsStopped() const { return m_worldIsStopped; }
375376
bool worldIsRunning() const { return !worldIsStopped(); }
376377
#if ASSERT_ENABLED

0 commit comments

Comments
 (0)