2018-07-22 14:44:20 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "Lock.h"
|
|
|
|
|
|
|
|
#include "ChunkAllocator.h"
|
|
|
|
#include "InfallibleVector.h"
|
|
|
|
#include "SpinLock.h"
|
|
|
|
#include "Thread.h"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace recordreplay {
|
|
|
|
|
2018-10-26 00:46:07 +03:00
|
|
|
// The total number of locks that have been created. Each Lock is given a
|
|
|
|
// non-zero id based on this counter.
|
2018-07-22 14:44:20 +03:00
|
|
|
static Atomic<size_t, SequentiallyConsistent, Behavior::DontPreserve> gNumLocks;
|
|
|
|
|
|
|
|
struct LockAcquires {
|
|
|
|
// List of thread acquire orders for the lock. This is protected by the lock
|
|
|
|
// itself.
|
|
|
|
Stream* mAcquires;
|
|
|
|
|
|
|
|
// During replay, the next thread id to acquire the lock. Writes to this are
|
|
|
|
// protected by the lock itself, though reads may occur on other threads.
|
|
|
|
Atomic<size_t, SequentiallyConsistent, Behavior::DontPreserve> mNextOwner;
|
|
|
|
|
|
|
|
static const size_t NoNextOwner = 0;
|
|
|
|
|
|
|
|
void ReadAndNotifyNextOwner(Thread* aCurrentThread) {
|
|
|
|
MOZ_RELEASE_ASSERT(IsReplaying());
|
|
|
|
if (mAcquires->AtEnd()) {
|
|
|
|
mNextOwner = NoNextOwner;
|
|
|
|
} else {
|
|
|
|
mNextOwner = mAcquires->ReadScalar();
|
|
|
|
if (mNextOwner != aCurrentThread->Id()) {
|
|
|
|
Thread::Notify(mNextOwner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Acquires for each lock, indexed by the lock ID.
|
|
|
|
static ChunkAllocator<LockAcquires> gLockAcquires;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Locking Interface
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Table mapping native lock pointers to the associated Lock structure, for
|
|
|
|
// every recorded lock in existence.
|
|
|
|
typedef std::unordered_map<void*, Lock*> LockMap;
|
|
|
|
static LockMap* gLocks;
|
|
|
|
static ReadWriteSpinLock gLocksLock;
|
|
|
|
|
2018-10-26 00:46:07 +03:00
|
|
|
static Lock* CreateNewLock(Thread* aThread, size_t aId) {
|
|
|
|
LockAcquires* info = gLockAcquires.Create(aId);
|
|
|
|
info->mAcquires = gRecordingFile->OpenStream(StreamName::Lock, aId);
|
|
|
|
|
|
|
|
if (IsReplaying()) {
|
|
|
|
info->ReadAndNotifyNextOwner(aThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Lock(aId);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:12:51 +03:00
|
|
|
/* static */
|
|
|
|
void Lock::New(void* aNativeLock) {
|
2018-09-03 21:08:45 +03:00
|
|
|
Thread* thread = Thread::Current();
|
2018-10-17 19:04:29 +03:00
|
|
|
RecordingEventSection res(thread);
|
|
|
|
if (!res.CanAccessEvents()) {
|
2018-07-22 14:44:20 +03:00
|
|
|
Destroy(aNativeLock); // Clean up any old lock, as below.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->Events().RecordOrReplayThreadEvent(ThreadEvent::CreateLock);
|
|
|
|
|
|
|
|
size_t id;
|
|
|
|
if (IsRecording()) {
|
|
|
|
id = gNumLocks++;
|
|
|
|
}
|
|
|
|
thread->Events().RecordOrReplayScalar(&id);
|
|
|
|
|
2018-10-26 00:46:07 +03:00
|
|
|
Lock* lock = CreateNewLock(thread, id);
|
2018-07-22 14:44:20 +03:00
|
|
|
|
|
|
|
// Tolerate new locks being created with identical pointers, even if there
|
2018-09-03 21:08:45 +03:00
|
|
|
// was no explicit Destroy() call for the old one.
|
2018-07-22 14:44:20 +03:00
|
|
|
Destroy(aNativeLock);
|
|
|
|
|
|
|
|
AutoWriteSpinLock ex(gLocksLock);
|
|
|
|
thread->BeginDisallowEvents();
|
|
|
|
|
|
|
|
if (!gLocks) {
|
|
|
|
gLocks = new LockMap();
|
|
|
|
}
|
|
|
|
|
2018-10-26 00:46:07 +03:00
|
|
|
gLocks->insert(LockMap::value_type(aNativeLock, lock));
|
2018-07-22 14:44:20 +03:00
|
|
|
|
|
|
|
thread->EndDisallowEvents();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:12:51 +03:00
|
|
|
/* static */
|
|
|
|
void Lock::Destroy(void* aNativeLock) {
|
2018-07-22 14:44:20 +03:00
|
|
|
Lock* lock = nullptr;
|
|
|
|
{
|
|
|
|
AutoWriteSpinLock ex(gLocksLock);
|
|
|
|
if (gLocks) {
|
|
|
|
LockMap::iterator iter = gLocks->find(aNativeLock);
|
|
|
|
if (iter != gLocks->end()) {
|
|
|
|
lock = iter->second;
|
|
|
|
gLocks->erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete lock;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:12:51 +03:00
|
|
|
/* static */
|
|
|
|
Lock* Lock::Find(void* aNativeLock) {
|
2018-07-22 14:44:20 +03:00
|
|
|
MOZ_RELEASE_ASSERT(IsRecordingOrReplaying());
|
|
|
|
|
2018-10-30 02:23:33 +03:00
|
|
|
Maybe<AutoReadSpinLock> ex;
|
|
|
|
ex.emplace(gLocksLock);
|
2018-07-22 14:44:20 +03:00
|
|
|
|
|
|
|
if (gLocks) {
|
|
|
|
LockMap::iterator iter = gLocks->find(aNativeLock);
|
|
|
|
if (iter != gLocks->end()) {
|
|
|
|
// Now that we know the lock is recorded, check whether thread events
|
|
|
|
// should be generated right now. Doing things in this order avoids
|
|
|
|
// reentrancy issues when initializing the thread-local state used by
|
|
|
|
// these calls.
|
2018-10-22 00:03:34 +03:00
|
|
|
Lock* lock = iter->second;
|
|
|
|
if (AreThreadEventsPassedThrough()) {
|
2018-07-22 14:44:20 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-22 00:03:34 +03:00
|
|
|
if (HasDivergedFromRecording()) {
|
|
|
|
// When diverged from the recording, don't allow uses of locks that are
|
|
|
|
// held by idling threads that have not diverged from the recording.
|
|
|
|
// This will cause the process to deadlock, so rewind instead.
|
2018-10-31 23:21:22 +03:00
|
|
|
if (lock->mOwner && Thread::GetById(lock->mOwner)->ShouldIdle() &&
|
|
|
|
Thread::CurrentIsMainThread()) {
|
2018-10-30 02:23:33 +03:00
|
|
|
ex.reset();
|
2018-10-22 00:03:34 +03:00
|
|
|
EnsureNotDivergedFromRecording();
|
|
|
|
Unreachable();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return lock;
|
2018-07-22 14:44:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-30 05:42:32 +03:00
|
|
|
void Lock::Enter() {
|
2018-09-03 21:08:45 +03:00
|
|
|
Thread* thread = Thread::Current();
|
2018-10-17 19:04:29 +03:00
|
|
|
|
|
|
|
RecordingEventSection res(thread);
|
|
|
|
if (!res.CanAccessEvents()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-22 14:44:20 +03:00
|
|
|
|
|
|
|
// Include an event in each thread's record when a lock acquire begins. This
|
|
|
|
// is not required by the replay but is used to check that lock acquire order
|
|
|
|
// is consistent with the recording and that we will fail explicitly instead
|
|
|
|
// of deadlocking.
|
|
|
|
thread->Events().RecordOrReplayThreadEvent(ThreadEvent::Lock);
|
|
|
|
thread->Events().CheckInput(mId);
|
|
|
|
|
|
|
|
LockAcquires* acquires = gLockAcquires.Get(mId);
|
|
|
|
if (IsRecording()) {
|
|
|
|
acquires->mAcquires->WriteScalar(thread->Id());
|
|
|
|
} else {
|
2018-10-17 19:04:29 +03:00
|
|
|
// Wait until this thread is next in line to acquire the lock, or until it
|
|
|
|
// has been instructed to diverge from the recording.
|
|
|
|
while (thread->Id() != acquires->mNextOwner &&
|
|
|
|
!thread->MaybeDivergeFromRecording()) {
|
2018-07-22 14:44:20 +03:00
|
|
|
Thread::Wait();
|
|
|
|
}
|
2018-10-22 00:03:34 +03:00
|
|
|
if (!thread->HasDivergedFromRecording()) {
|
|
|
|
mOwner = thread->Id();
|
|
|
|
}
|
2018-08-30 05:42:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Lock::Exit() {
|
2018-10-17 19:04:29 +03:00
|
|
|
Thread* thread = Thread::Current();
|
|
|
|
if (IsReplaying() && !thread->HasDivergedFromRecording()) {
|
2018-10-22 00:03:34 +03:00
|
|
|
mOwner = 0;
|
|
|
|
|
2018-08-30 05:42:32 +03:00
|
|
|
// Notify the next owner before releasing the lock.
|
|
|
|
LockAcquires* acquires = gLockAcquires.Get(mId);
|
2018-10-17 19:04:29 +03:00
|
|
|
acquires->ReadAndNotifyNextOwner(thread);
|
2018-07-22 14:44:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:12:51 +03:00
|
|
|
/* static */
|
|
|
|
void Lock::LockAquiresUpdated(size_t aLockId) {
|
2018-10-26 00:46:07 +03:00
|
|
|
LockAcquires* acquires = gLockAcquires.MaybeGet(aLockId);
|
|
|
|
if (acquires && acquires->mAcquires &&
|
|
|
|
acquires->mNextOwner == LockAcquires::NoNextOwner) {
|
|
|
|
acquires->ReadAndNotifyNextOwner(Thread::Current());
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 14:44:20 +03:00
|
|
|
|
2018-10-26 00:46:07 +03:00
|
|
|
// We use a set of Locks to record and replay the order in which atomic
|
|
|
|
// accesses occur. Each lock describes the acquire order for a disjoint set of
|
|
|
|
// values; this is done to reduce contention between threads, and ensures that
|
|
|
|
// when the same value pointer is used in two ordered atomic accesses, those
|
|
|
|
// accesses will replay in the same order as they did while recording.
|
|
|
|
// Instead of using platform mutexes, we manage the Locks directly to avoid
|
|
|
|
// overhead in Lock::Find. Atomics accesses are a major source of recording
|
|
|
|
// overhead, which we want to minimize.
|
|
|
|
static const size_t NumAtomicLocks = 89;
|
|
|
|
static Lock** gAtomicLocks;
|
|
|
|
|
|
|
|
// While recording, these locks prevent multiple threads from simultaneously
|
|
|
|
// owning the same atomic lock.
|
|
|
|
static SpinLock* gAtomicLockOwners;
|
2018-07-22 14:44:20 +03:00
|
|
|
|
2019-02-26 01:12:51 +03:00
|
|
|
/* static */
|
|
|
|
void Lock::InitializeLocks() {
|
2018-10-26 00:46:07 +03:00
|
|
|
Thread* thread = Thread::Current();
|
2018-07-22 14:44:20 +03:00
|
|
|
|
2018-10-26 00:46:07 +03:00
|
|
|
gNumLocks = 1;
|
|
|
|
gAtomicLocks = new Lock*[NumAtomicLocks];
|
|
|
|
for (size_t i = 0; i < NumAtomicLocks; i++) {
|
|
|
|
gAtomicLocks[i] = CreateNewLock(thread, gNumLocks++);
|
|
|
|
}
|
|
|
|
if (IsRecording()) {
|
|
|
|
gAtomicLockOwners = new SpinLock[NumAtomicLocks];
|
|
|
|
PodZero(gAtomicLockOwners, NumAtomicLocks);
|
2018-07-22 14:44:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2018-10-26 00:46:07 +03:00
|
|
|
MOZ_EXPORT void RecordReplayInterface_InternalBeginOrderedAtomicAccess(
|
|
|
|
const void* aValue) {
|
2018-07-22 14:44:20 +03:00
|
|
|
MOZ_RELEASE_ASSERT(IsRecordingOrReplaying());
|
2018-10-26 00:46:07 +03:00
|
|
|
|
|
|
|
Thread* thread = Thread::Current();
|
|
|
|
|
|
|
|
// Determine which atomic lock to use for this access.
|
|
|
|
size_t atomicId;
|
|
|
|
{
|
|
|
|
RecordingEventSection res(thread);
|
|
|
|
if (!res.CanAccessEvents()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->Events().RecordOrReplayThreadEvent(ThreadEvent::AtomicAccess);
|
|
|
|
|
|
|
|
atomicId = IsRecording() ? (HashGeneric(aValue) % NumAtomicLocks) : 0;
|
|
|
|
thread->Events().RecordOrReplayScalar(&atomicId);
|
|
|
|
MOZ_RELEASE_ASSERT(atomicId < NumAtomicLocks);
|
2018-07-22 14:44:20 +03:00
|
|
|
}
|
2018-10-26 00:46:07 +03:00
|
|
|
|
|
|
|
// When recording, hold a spin lock so that no other thread can access this
|
|
|
|
// same atomic until this access ends. When replaying, we don't need to hold
|
|
|
|
// any actual lock, as the atomic access cannot race and the Lock structure
|
|
|
|
// ensures that accesses happen in the same order.
|
|
|
|
if (IsRecording()) {
|
|
|
|
gAtomicLockOwners[atomicId].Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
gAtomicLocks[atomicId]->Enter();
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(thread->AtomicLockId().isNothing());
|
|
|
|
thread->AtomicLockId().emplace(atomicId);
|
2018-07-22 14:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_EXPORT void RecordReplayInterface_InternalEndOrderedAtomicAccess() {
|
|
|
|
MOZ_RELEASE_ASSERT(IsRecordingOrReplaying());
|
2018-10-26 00:46:07 +03:00
|
|
|
|
|
|
|
Thread* thread = Thread::Current();
|
|
|
|
if (!thread || thread->PassThroughEvents() ||
|
|
|
|
thread->HasDivergedFromRecording()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(thread->AtomicLockId().isSome());
|
|
|
|
size_t atomicId = thread->AtomicLockId().ref();
|
|
|
|
thread->AtomicLockId().reset();
|
|
|
|
|
|
|
|
if (IsRecording()) {
|
|
|
|
gAtomicLockOwners[atomicId].Unlock();
|
2018-07-22 14:44:20 +03:00
|
|
|
}
|
2018-10-26 00:46:07 +03:00
|
|
|
|
|
|
|
gAtomicLocks[atomicId]->Exit();
|
2018-07-22 14:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
} // namespace recordreplay
|
|
|
|
} // namespace mozilla
|