Summary
On macOS, FPThreadsSharedMutex (FRWLock) runs pthread_rwlock_init on its member without clearing it first. Apple’s pthread_rwlock_init reads the existing bytes. If they hold an initialized lock whose lock and unlock counts disagree, it returns EBUSY and the constructor’s checkf fires. Reused heap memory regularly holds such stale lock bytes, so a lock that is a member of a heap object occasionally lands on them. We hit it in Chaos (TRigidParticles::SleepDataLock, during UWorld::CreatePhysicsScene) and in Mass (FMassArchetypeSharedFragmentValues, during a TEDS command flush).
What Type of Bug are you experiencing?
Foundation (C++ Tools, Profiling, & Pipeline)
Steps to Reproduce
- On a Mac, in any C++ project, run this (for example from an automation test):
alignas(16) uint8 Bytes[sizeof(FRWLock)] = {};
pthread_rwlock_t* Stale = reinterpret_cast<pthread_rwlock_t*>(Bytes); // FRWLock's only member
pthread_rwlock_init(Stale, nullptr);
pthread_rwlock_wrlock(Stale); // leave the bytes of a lock that is in use
new (Bytes) FRWLock; // default-initialized, as a class member is
- The
FRWLockconstructor asserts.
new (Bytes) FRWLock() with parentheses does not reproduce it, because value-initialization zeroes the bytes first.
Expected Result
The lock initializes. The memory was never a lock of this object, so its old contents shouldn’t matter.
Observed Result
Assertion failed: Err == 0 [File:Runtime/Core/Public/HAL/PThreadsSharedMutex.h] [Line: 32] pthread_rwlock_init failed with error: 16
Affects Versions
5.8
Platform(s)
Mac
Additional Notes
- macOS reads the lock’s counts from a 16-byte-aligned slot inside the 200-byte
pthread_rwlock_t, computed from the lock’s own address. So an unlocked lock that was moved 8 bytes off its 16-byte alignment is refused too. - Stale lock bytes are common in freed memory. One run of our automation suite frees about 25,000 locks that were never destroyed, mostly old copies left when a container is reallocated.
- One engine source of refused ones: the constructor of Chaos’s
TAABBTree::FWorkSnapshotleavesAverageCenter,ScaledCenterVariance,NodeLevel,NewNodeIdx,BestBoundsCurIdxandSplitInfos[].WorkSnapshotIdxuninitialized. An old signature survives in those fields while bounds are written over the words read as counts. An allocator probe caught 7 suchWorkPoolblocks freed in a single run. - Rate: 1 in 413 full runs of our editor automation suite.
- Packaged Development builds don’t hit it, because
FMallocPoisonProxyfills each new allocation. The editor and Shipping builds do. Windows (SRWLOCK) and Linux (glibc) never read the old bytes. - Suggested fix: clear the mutex before initializing it in
FPThreadsSharedMutex’s constructor, for example withFMemory::Memzero(Mutex). InitializingFWorkSnapshot’s fields would also remove one source. - Earlier report of the Mass/TEDS occurrence: [UE 5.8.4][Editor][Mac] Crash: pthread rwlock init fails with EBUSY (16) copying Mass shared fragment values during a TEDS command flush