Skip to content

Commit 4294ae2

Browse files
committed
[JSC] Rebuild assertIsMutatorOrMutatorIsStopped on the GC protocol's roles
https://bugs.webkit.org/show_bug.cgi?id=330335 rdar://191952601 Reviewed by Keith Miller. BlockDirectory's bit vectors are guarded by m_bitvectorLock, a reader-writer lock. Reading or writing a single bit takes only its shared side, mutate(), and only resizing the vectors takes its exclusive side, grow(). Some callers skip even mutate(), in places where no other thread can resize the vectors. That relies on two invariants: - One thread at a time has access to the heap, and only that thread resizes the vectors, in addBlock() and removeBlock(). - While the world is stopped, no thread acts as the access holder: a thread with access can act only as the conductor, which does not allocate. So nothing resizes the vectors. So the access holder may read and write single bits without taking mutate(), and so may the conductor and its marking helpers while the world is stopped. Only part of the first invariant was checked: access is a single bit, which acquireAccessSlow() release-asserts is clear, and addBlock() asserted the API lock, a stand-in for access. removeBlock() and the unlocked readers relied on assertIsMutatorOrMutatorIsStopped(), which accepted any thread while the world was stopped, including compiler threads, so the second invariant was not checked at all. With the world running, if no thread owned the API lock, it accepted any thread as long as some thread had access, which is the normal state for WebKit's main thread and workers. Replace it with checks of the roles: - assertIsAccessHolder() now also requires the world to be running, so addBlock() and removeBlock(), which assert it, fail if anything resizes the vectors during a stopped phase. - assertIsExclusiveAccessHolder() also requires that no other thread has access, using a debug-only count of threads with access, and grants the access capability exclusively. Code that relies on a single mutator calls it: LocalAllocator::tryAllocateIn(), MarkedSpace::freeBlock() and the unlocked bit accesses. Later changes will mark the rest of the code that assumes a single mutator the same way. - BlockDirectory::assertMayAccessBitsUnlocked() accepts the exclusive access holder, or the conductor or a marking helper while the world is stopped. - Heap::assertIsExclusiveAccessHolderOrConductorWithWorldStopped() covers MarkedBlock::Handle::sweep(), which the conductor also runs when it resumes allocation. A concurrent sweeper thread, which BlockDirectory's inUse claims prepare for, holds none of these roles, so it must take mutate(). If it ever frees blocks itself, removeBlock()'s assert fails, since the first invariant would no longer hold. Canonical link: https://commits.webkit.org/325970@main
1 parent 2fdab78 commit 4294ae2

15 files changed

Lines changed: 79 additions & 46 deletions

‎Source/JavaScriptCore/heap/BlockDirectory.cpp‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "AlignedMemoryAllocator.h"
3030
#include "BlockDirectoryInlines.h"
31+
#include "Collector.h"
3132
#include "Heap.h"
3233
#include "HeapInlines.h"
3334
#include "MarkedSpaceInlines.h"
@@ -152,6 +153,7 @@ MarkedBlock::Handle* BlockDirectory::tryAllocateBlock(JSC::Heap& heap)
152153
void BlockDirectory::addBlock(MarkedBlock::Handle* block)
153154
{
154155
ASSERT(markedSpace().heap().vm().currentThreadIsHoldingAPILock());
156+
markedSpace().heap().assertIsAccessHolder();
155157

156158
Locker locker { m_bitvectorLock.grow() };
157159
unsigned index;
@@ -193,7 +195,7 @@ void BlockDirectory::addBlock(MarkedBlock::Handle* block)
193195

194196
void BlockDirectory::removeBlock(MarkedBlock::Handle* block, WillDeleteBlock willDelete)
195197
{
196-
markedSpace().heap().assertIsMutatorOrMutatorIsStopped();
198+
markedSpace().heap().assertIsAccessHolder();
197199
Locker locker { m_bitvectorLock.grow() };
198200
ASSERT(block->directory() == this);
199201
ASSERT(m_blocks[block->index()].first == block);
@@ -231,7 +233,7 @@ void BlockDirectory::stopAllocating()
231233
});
232234

233235
#if ASSERT_ENABLED
234-
assertIsMutatorOrMutatorIsStopped();
236+
assertMayAccessBitsUnlocked();
235237
if (!inUseBitsView().isEmpty()) [[unlikely]] {
236238
dataLogLn("Not all inUse bits are clear at stopAllocating");
237239
dataLogLn(*this);
@@ -488,7 +490,7 @@ RefPtr<SharedTask<MarkedBlock::Handle*()>> BlockDirectory::parallelNotEmptyBlock
488490
if (m_done)
489491
return nullptr;
490492
Locker locker { m_lock };
491-
m_directory.assertIsMutatorOrMutatorIsStopped();
493+
m_directory.assertMayAccessBitsUnlocked();
492494
m_index = m_directory.m_bits.markingNotEmpty().findBit(m_index, true);
493495
if (m_index >= m_directory.m_blocks.size()) {
494496
m_done = true;
@@ -541,9 +543,13 @@ MarkedSpace& BlockDirectory::markedSpace() const
541543
}
542544

543545
#if ASSERT_ENABLED
544-
void BlockDirectory::assertIsMutatorOrMutatorIsStopped() const
546+
void BlockDirectory::assertMayAccessBitsUnlocked() const
545547
{
546-
markedSpace().heap().assertIsMutatorOrMutatorIsStopped();
548+
JSC::Heap& heap = markedSpace().heap();
549+
if (heap.worldIsStopped())
550+
ASSERT(heap.collector().currentThreadIsConductor() || Thread::currentSingleton().gcThreadType() == GCThreadType::Helper);
551+
else
552+
heap.assertIsExclusiveAccessHolder();
547553
}
548554

549555
void BlockDirectory::assertWorldIsStopped() const WTF_IGNORES_THREAD_SAFETY_ANALYSIS

‎Source/JavaScriptCore/heap/BlockDirectory.h‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ class BlockDirectory {
105105
void removeBlock(MarkedBlock::Handle*, WillDeleteBlock = WillDeleteBlock::No);
106106

107107
#if ASSERT_ENABLED
108-
JS_EXPORT_PRIVATE void assertIsMutatorOrMutatorIsStopped() const WTF_ASSERTS_ACQUIRED_SHARED_LOCK(m_bitvectorLock);
108+
JS_EXPORT_PRIVATE void assertMayAccessBitsUnlocked() const WTF_ASSERTS_ACQUIRED_SHARED_LOCK(m_bitvectorLock);
109109
#else
110-
ALWAYS_INLINE void assertIsMutatorOrMutatorIsStopped() const WTF_ASSERTS_ACQUIRED_SHARED_LOCK(m_bitvectorLock) { }
110+
ALWAYS_INLINE void assertMayAccessBitsUnlocked() const WTF_ASSERTS_ACQUIRED_SHARED_LOCK(m_bitvectorLock) { }
111111
#endif
112112
MutateGrowLock& bitvectorLock() LIFETIME_BOUND WTF_RETURNS_LOCK(m_bitvectorLock) { return m_bitvectorLock; }
113113

@@ -255,11 +255,12 @@ class BlockDirectory {
255255
// (assertWorldIsStopped()).
256256
// - Exception: notifyNeedsDestruction() may set a block's destructible bit without holding inUse;
257257
// see assertMayNotifyNeedsDestruction().
258-
// - Exception: growth only happens on the mutator, so the mutator, or anyone while the world is
259-
// stopped, may read the bits without mutate(); see assertIsMutatorOrMutatorIsStopped().
258+
// - Exception: growth only happens on the thread with access (addBlock() and removeBlock() assert it),
259+
// and one thread at a time has access, so that thread, or the conductor and its marking helpers while
260+
// the world is stopped, may read and write single bits without mutate(); see assertMayAccessBitsUnlocked().
260261
BlockDirectoryBits m_bits WTF_GUARDED_BY_LOCK(m_bitvectorLock); // Don't access this directly use one of the accessors above.
261262
MutateGrowLock m_bitvectorLock;
262-
[[no_unique_address]] BlockOwnership m_blockOwnership;
263+
NO_UNIQUE_ADDRESS BlockOwnership m_blockOwnership;
263264
Lock m_localAllocatorsLock;
264265
CellAttributes m_attributes;
265266

‎Source/JavaScriptCore/heap/BlockDirectoryInlines.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace JSC {
3333

3434
inline void BlockDirectory::forEachBlock(NOESCAPE const std::invocable<MarkedBlock::Handle*> auto& functor)
3535
{
36-
assertIsMutatorOrMutatorIsStopped();
36+
assertMayAccessBitsUnlocked();
3737
liveBitsView().forEachSetBit(
3838
[&] (size_t index) {
3939
functor(m_blocks[index].first);
@@ -42,7 +42,7 @@ inline void BlockDirectory::forEachBlock(NOESCAPE const std::invocable<MarkedBlo
4242

4343
inline void BlockDirectory::forEachNotEmptyBlock(NOESCAPE const std::invocable<MarkedBlock::Handle*> auto& functor)
4444
{
45-
assertIsMutatorOrMutatorIsStopped();
45+
assertMayAccessBitsUnlocked();
4646
markingNotEmptyBitsView().forEachSetBit(
4747
[&] (size_t index) {
4848
functor(m_blocks[index].first);

‎Source/JavaScriptCore/heap/Collector.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ void Collector::checkIsConductor(GCConductor conn)
934934
heap.checkConn(conn == GCConductor::Mutator && &heap == m_conductingHeap ? GCConductor::Mutator : GCConductor::Collector);
935935
});
936936
if (conn == GCConductor::Mutator)
937-
m_conductingHeap->assertIsAccessHolder();
937+
ASSERT(m_conductingHeap->currentThreadAcquiredAccess());
938938
else
939939
assertIsCollectorThread();
940940
}
@@ -960,7 +960,7 @@ void Collector::assertIsCollectorThread() const
960960
bool Collector::currentThreadIsConductor() const
961961
{
962962
if (m_conductingHeap)
963-
return m_conductingHeap->currentThreadIsAccessHolder();
963+
return m_conductingHeap->currentThreadAcquiredAccess();
964964
return m_collectorThreadUID.loadRelaxed() == Thread::currentSingleton().uid();
965965
}
966966
#endif

‎Source/JavaScriptCore/heap/Collector.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ class Collector {
122122

123123
void dump(PrintStream&) const;
124124

125+
#if ASSERT_ENABLED
126+
// For asserts that accept more than one role, which thread-safety analysis cannot express.
127+
bool currentThreadIsConductor() const;
128+
#endif
129+
125130
private:
126131
class CollectorThread;
127132
friend class Heap;
@@ -136,8 +141,6 @@ class Collector {
136141
void assertIsConductor(GCConductor) WTF_ASSERTS_ACQUIRED_CAPABILITY(conductorRole);
137142
void assertIsConductorWithWorldStopped(GCConductor) WTF_ASSERTS_ACQUIRED_CAPABILITY(conductorRole) WTF_ASSERTS_ACQUIRED_CAPABILITY(conductorWithWorldStoppedRole);
138143
void assertIsCollectorThread() const WTF_ASSERTS_ACQUIRED_CAPABILITY(m_collectorThreadRole);
139-
// For asserts that accept either of two roles, which thread-safety analysis cannot express.
140-
bool currentThreadIsConductor() const;
141144
#else
142145
ALWAYS_INLINE void assertIsConductor(GCConductor) WTF_ASSERTS_ACQUIRED_CAPABILITY(conductorRole) { }
143146
ALWAYS_INLINE void assertIsConductorWithWorldStopped(GCConductor) WTF_ASSERTS_ACQUIRED_CAPABILITY(conductorRole) WTF_ASSERTS_ACQUIRED_CAPABILITY(conductorWithWorldStoppedRole) { }
@@ -297,7 +300,7 @@ class Collector {
297300
Box<Lock> m_threadLock;
298301
const Ref<AutomaticThreadCondition> m_threadCondition; // The mutator must not wait on this. It would cause a deadlock.
299302
const RefPtr<AutomaticThread> m_thread;
300-
[[no_unique_address]] CollectorThreadRole m_collectorThreadRole;
303+
NO_UNIQUE_ADDRESS CollectorThreadRole m_collectorThreadRole;
301304
#if ASSERT_ENABLED
302305
// For assertIsCollectorThread(). Set on each start, since an idle AutomaticThread exits and a later wake starts a
303306
// new OS thread.

‎Source/JavaScriptCore/heap/Heap.cpp‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,26 @@ void Heap::assertIsAccessHolder() const
501501
ASSERT(currentThreadIsAccessHolder());
502502
}
503503

504+
void Heap::assertIsExclusiveAccessHolder() const
505+
{
506+
assertIsAccessHolder();
507+
ASSERT(m_accessHolderCount.loadRelaxed() == 1);
508+
}
509+
510+
void Heap::assertIsExclusiveAccessHolderOrConductorWithWorldStopped() const
511+
{
512+
if (worldIsStopped())
513+
ASSERT(m_collector->currentThreadIsConductor());
514+
else
515+
assertIsExclusiveAccessHolder();
516+
}
517+
504518
bool Heap::currentThreadIsAccessHolder() const
519+
{
520+
return !worldIsStopped() && currentThreadAcquiredAccess();
521+
}
522+
523+
bool Heap::currentThreadAcquiredAccess() const
505524
{
506525
if (!hasAccess())
507526
return false;
@@ -512,18 +531,6 @@ bool Heap::currentThreadIsAccessHolder() const
512531
}
513532
return m_accessHolderThreadUID.loadRelaxed() == Thread::currentSingleton().uid();
514533
}
515-
516-
void Heap::assertIsMutatorOrMutatorIsStopped() const
517-
{
518-
if (worldIsStopped())
519-
return;
520-
if (auto owner = vm().apiLock().ownerThread())
521-
ASSERT(owner->get() == &Thread::currentSingleton());
522-
else {
523-
// FIXME: It feels like heap access should be tied to holding the API lock.
524-
ASSERT(hasAccess());
525-
}
526-
}
527534
#endif
528535

529536
void Heap::releaseDelayedReleasedObjects()

‎Source/JavaScriptCore/heap/Heap.h‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,20 @@ class Heap {
381381
bool needCollectionEpilogue() const { return m_worldState.load() & needCollectionEpilogueBit; }
382382
bool worldIsStopped() const { return m_worldIsStopped; }
383383
#if ASSERT_ENABLED
384-
JS_EXPORT_PRIVATE void assertIsMutatorOrMutatorIsStopped() const;
384+
// Asserts that the current thread has access to the heap and that the world is running. While the world is stopped,
385+
// the thread with access can act only as the conductor, not the access holder.
386+
// Note that this acquires the capability shared, so it does not establish that the current thread is the only one
387+
// with access; code that relies on that should call assertIsExclusiveAccessHolder() instead.
388+
JS_EXPORT_PRIVATE void assertIsAccessHolder() const WTF_ASSERTS_ACQUIRED_SHARED_CAPABILITY(m_accessHolderRole);
389+
// Like assertIsAccessHolder(), but also asserts that no other thread has access to the heap.
390+
// Note that this acquires the capability exclusively, so code that calls this relies on being the only thread
391+
// with access.
392+
JS_EXPORT_PRIVATE void assertIsExclusiveAccessHolder() const WTF_ASSERTS_ACQUIRED_CAPABILITY(m_accessHolderRole);
393+
JS_EXPORT_PRIVATE void assertIsExclusiveAccessHolderOrConductorWithWorldStopped() const;
385394
#else
386-
ALWAYS_INLINE void assertIsMutatorOrMutatorIsStopped() const { }
395+
ALWAYS_INLINE void assertIsAccessHolder() const WTF_ASSERTS_ACQUIRED_SHARED_CAPABILITY(m_accessHolderRole) { }
396+
ALWAYS_INLINE void assertIsExclusiveAccessHolder() const WTF_ASSERTS_ACQUIRED_CAPABILITY(m_accessHolderRole) { }
397+
ALWAYS_INLINE void assertIsExclusiveAccessHolderOrConductorWithWorldStopped() const { }
387398
#endif
388399

389400
// We're always busy on the collection threads. On the main thread, this returns true if we're
@@ -688,11 +699,10 @@ class Heap {
688699
void noteAccessAcquired();
689700
void noteAccessReleased() WTF_REQUIRES_SHARED_CAPABILITY(m_accessHolderRole);
690701
#if ASSERT_ENABLED
691-
JS_EXPORT_PRIVATE void assertIsAccessHolder() const WTF_ASSERTS_ACQUIRED_SHARED_CAPABILITY(m_accessHolderRole);
692702
// For asserts that accept either of two roles, which thread-safety analysis cannot express.
693703
bool currentThreadIsAccessHolder() const;
694-
#else
695-
ALWAYS_INLINE void assertIsAccessHolder() const WTF_ASSERTS_ACQUIRED_SHARED_CAPABILITY(m_accessHolderRole) { }
704+
// Whether the current thread acquired access, whether or not the world is currently stopped.
705+
bool currentThreadAcquiredAccess() const;
696706
#endif
697707

698708
bool handleNeedCollectionEpilogue(unsigned) WTF_REQUIRES_SHARED_CAPABILITY(m_accessHolderRole);
@@ -939,10 +949,12 @@ class Heap {
939949
static constexpr unsigned needCollectionEpilogueBit = 1u << 3u;
940950
static constexpr unsigned mutatorWaitingBit = 1u << 4u; // Allows the mutator to use this as a condition variable.
941951
Atomic<unsigned> m_worldState;
942-
[[no_unique_address]] HeapAccessHolderRole m_accessHolderRole;
952+
NO_UNIQUE_ADDRESS HeapAccessHolderRole m_accessHolderRole;
943953
#if ASSERT_ENABLED
944954
// For assertIsAccessHolder(): the thread that acquired access, or 0.
945955
Atomic<uint32_t> m_accessHolderThreadUID { 0 };
956+
// For assertIsExclusiveAccessHolder(): how many threads have access.
957+
Atomic<unsigned> m_accessHolderCount { 0 };
946958
#endif
947959
bool m_worldIsStopped { false };
948960

‎Source/JavaScriptCore/heap/HeapInlines.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,15 @@ inline void Heap::noteAccessAcquired()
241241
{
242242
#if ASSERT_ENABLED
243243
m_accessHolderThreadUID.storeRelaxed(Thread::currentSingleton().uid());
244+
m_accessHolderCount.exchangeAdd(1);
244245
#endif
245246
}
246247

247248
inline void Heap::noteAccessReleased()
248249
{
249250
#if ASSERT_ENABLED
250251
m_accessHolderThreadUID.storeRelaxed(0);
252+
m_accessHolderCount.exchangeSub(1);
251253
#endif
252254
}
253255

‎Source/JavaScriptCore/heap/IsoCellSet.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Ref<SharedTask<MarkedBlock::Handle*()>> IsoCellSet::parallelNotEmptyMarkedBlockS
6060
{
6161
if (m_done)
6262
return nullptr;
63-
m_directory.assertIsMutatorOrMutatorIsStopped();
63+
m_directory.assertMayAccessBitsUnlocked();
6464
Locker locker { m_lock };
6565
auto bits = m_directory.markingNotEmptyBitsView() & m_set.m_blocksWithBits;
6666
m_index = bits.findBit(m_index, true);

‎Source/JavaScriptCore/heap/IsoCellSetInlines.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ template<typename Func>
7979
void IsoCellSet::forEachMarkedCell(NOESCAPE const Func& func)
8080
{
8181
BlockDirectory& directory = m_subspace.m_directory;
82-
directory.assertIsMutatorOrMutatorIsStopped();
82+
directory.assertMayAccessBitsUnlocked();
8383
(directory.markingNotEmptyBitsView() & m_blocksWithBits).forEachSetBit(
8484
[&] (unsigned blockIndex) {
8585
MarkedBlock::Handle* block = directory.m_blocks[blockIndex].first;

0 commit comments

Comments
 (0)