2014-02-28 18:48:13 +08:00
|
|
|
//===-- sanitizer_deadlock_detector1.cc -----------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Deadlock detector implementation based on NxN adjacency bit matrix.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_deadlock_detector_interface.h"
|
|
|
|
#include "sanitizer_deadlock_detector.h"
|
|
|
|
#include "sanitizer_allocator_internal.h"
|
|
|
|
#include "sanitizer_placement_new.h"
|
|
|
|
#include "sanitizer_mutex.h"
|
|
|
|
|
2014-03-04 20:52:20 +08:00
|
|
|
#if SANITIZER_DEADLOCK_DETECTOR_VERSION == 1
|
|
|
|
|
2014-02-28 18:48:13 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
|
|
|
|
typedef TwoLevelBitVector<> DDBV; // DeadlockDetector's bit vector.
|
|
|
|
|
|
|
|
struct DDPhysicalThread {
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DDLogicalThread {
|
|
|
|
u64 ctx;
|
|
|
|
DeadlockDetectorTLS<DDBV> dd;
|
|
|
|
DDReport rep;
|
2014-03-05 21:41:21 +08:00
|
|
|
bool report_pending;
|
2014-02-28 18:48:13 +08:00
|
|
|
};
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
struct DD : public DDetector {
|
2014-02-28 18:48:13 +08:00
|
|
|
SpinMutex mtx;
|
|
|
|
DeadlockDetector<DDBV> dd;
|
2014-03-19 23:00:38 +08:00
|
|
|
DDFlags flags;
|
2014-02-28 18:48:13 +08:00
|
|
|
|
2014-03-19 23:00:38 +08:00
|
|
|
explicit DD(const DDFlags *flags);
|
2014-02-28 18:48:13 +08:00
|
|
|
|
2015-04-11 10:44:24 +08:00
|
|
|
DDPhysicalThread *CreatePhysicalThread() override;
|
|
|
|
void DestroyPhysicalThread(DDPhysicalThread *pt) override;
|
2014-02-28 18:48:13 +08:00
|
|
|
|
2015-04-11 10:44:24 +08:00
|
|
|
DDLogicalThread *CreateLogicalThread(u64 ctx) override;
|
|
|
|
void DestroyLogicalThread(DDLogicalThread *lt) override;
|
2014-02-28 18:48:13 +08:00
|
|
|
|
2015-04-11 10:44:24 +08:00
|
|
|
void MutexInit(DDCallback *cb, DDMutex *m) override;
|
|
|
|
void MutexBeforeLock(DDCallback *cb, DDMutex *m, bool wlock) override;
|
|
|
|
void MutexAfterLock(DDCallback *cb, DDMutex *m, bool wlock,
|
|
|
|
bool trylock) override;
|
|
|
|
void MutexBeforeUnlock(DDCallback *cb, DDMutex *m, bool wlock) override;
|
|
|
|
void MutexDestroy(DDCallback *cb, DDMutex *m) override;
|
2014-03-05 21:41:21 +08:00
|
|
|
|
2015-04-11 10:44:24 +08:00
|
|
|
DDReport *GetReport(DDCallback *cb) override;
|
2014-02-28 18:48:13 +08:00
|
|
|
|
|
|
|
void MutexEnsureID(DDLogicalThread *lt, DDMutex *m);
|
2014-03-19 20:49:46 +08:00
|
|
|
void ReportDeadlock(DDCallback *cb, DDMutex *m);
|
2014-02-28 18:48:13 +08:00
|
|
|
};
|
|
|
|
|
2014-03-18 21:13:47 +08:00
|
|
|
DDetector *DDetector::Create(const DDFlags *flags) {
|
|
|
|
(void)flags;
|
2014-03-05 21:41:21 +08:00
|
|
|
void *mem = MmapOrDie(sizeof(DD), "deadlock detector");
|
2014-03-19 23:00:38 +08:00
|
|
|
return new(mem) DD(flags);
|
2014-02-28 18:48:13 +08:00
|
|
|
}
|
|
|
|
|
2014-03-19 23:00:38 +08:00
|
|
|
DD::DD(const DDFlags *flags)
|
|
|
|
: flags(*flags) {
|
2014-02-28 18:48:13 +08:00
|
|
|
dd.clear();
|
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
DDPhysicalThread* DD::CreatePhysicalThread() {
|
2014-02-28 18:48:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
void DD::DestroyPhysicalThread(DDPhysicalThread *pt) {
|
2014-02-28 18:48:13 +08:00
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
DDLogicalThread* DD::CreateLogicalThread(u64 ctx) {
|
2014-02-28 18:48:13 +08:00
|
|
|
DDLogicalThread *lt = (DDLogicalThread*)InternalAlloc(sizeof(*lt));
|
|
|
|
lt->ctx = ctx;
|
|
|
|
lt->dd.clear();
|
2014-03-05 21:41:21 +08:00
|
|
|
lt->report_pending = false;
|
2014-02-28 18:48:13 +08:00
|
|
|
return lt;
|
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
void DD::DestroyLogicalThread(DDLogicalThread *lt) {
|
2014-02-28 18:48:13 +08:00
|
|
|
lt->~DDLogicalThread();
|
|
|
|
InternalFree(lt);
|
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
void DD::MutexInit(DDCallback *cb, DDMutex *m) {
|
2014-02-28 18:48:13 +08:00
|
|
|
m->id = 0;
|
2014-03-05 21:41:21 +08:00
|
|
|
m->stk = cb->Unwind();
|
2014-02-28 18:48:13 +08:00
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
void DD::MutexEnsureID(DDLogicalThread *lt, DDMutex *m) {
|
2014-02-28 18:48:13 +08:00
|
|
|
if (!dd.nodeBelongsToCurrentEpoch(m->id))
|
|
|
|
m->id = dd.newNode(reinterpret_cast<uptr>(m));
|
|
|
|
dd.ensureCurrentEpoch(<->dd);
|
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
void DD::MutexBeforeLock(DDCallback *cb,
|
|
|
|
DDMutex *m, bool wlock) {
|
|
|
|
DDLogicalThread *lt = cb->lt;
|
2014-03-13 21:21:30 +08:00
|
|
|
if (lt->dd.empty()) return; // This will be the first lock held by lt.
|
2014-03-14 16:06:15 +08:00
|
|
|
if (dd.hasAllEdges(<->dd, m->id)) return; // We already have all edges.
|
2014-02-28 18:48:13 +08:00
|
|
|
SpinMutexLock lk(&mtx);
|
|
|
|
MutexEnsureID(lt, m);
|
2014-03-13 21:21:30 +08:00
|
|
|
if (dd.isHeld(<->dd, m->id))
|
|
|
|
return; // FIXME: allow this only for recursive locks.
|
|
|
|
if (dd.onLockBefore(<->dd, m->id)) {
|
2014-03-17 22:41:36 +08:00
|
|
|
// Actually add this edge now so that we have all the stack traces.
|
2014-03-21 21:00:18 +08:00
|
|
|
dd.addEdges(<->dd, m->id, cb->Unwind(), cb->UniqueTid());
|
2014-03-19 20:49:46 +08:00
|
|
|
ReportDeadlock(cb, m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DD::ReportDeadlock(DDCallback *cb, DDMutex *m) {
|
|
|
|
DDLogicalThread *lt = cb->lt;
|
|
|
|
uptr path[10];
|
|
|
|
uptr len = dd.findPathToLock(<->dd, m->id, path, ARRAY_SIZE(path));
|
|
|
|
CHECK_GT(len, 0U); // Hm.. cycle of 10 locks? I'd like to see that.
|
|
|
|
CHECK_EQ(m->id, path[0]);
|
|
|
|
lt->report_pending = true;
|
|
|
|
DDReport *rep = <->rep;
|
|
|
|
rep->n = len;
|
|
|
|
for (uptr i = 0; i < len; i++) {
|
|
|
|
uptr from = path[i];
|
|
|
|
uptr to = path[(i + 1) % len];
|
|
|
|
DDMutex *m0 = (DDMutex*)dd.getData(from);
|
|
|
|
DDMutex *m1 = (DDMutex*)dd.getData(to);
|
|
|
|
|
2014-03-21 21:00:18 +08:00
|
|
|
u32 stk_from = -1U, stk_to = -1U;
|
|
|
|
int unique_tid = 0;
|
|
|
|
dd.findEdge(from, to, &stk_from, &stk_to, &unique_tid);
|
|
|
|
// Printf("Edge: %zd=>%zd: %u/%u T%d\n", from, to, stk_from, stk_to,
|
|
|
|
// unique_tid);
|
|
|
|
rep->loop[i].thr_ctx = unique_tid;
|
2014-03-19 20:49:46 +08:00
|
|
|
rep->loop[i].mtx_ctx0 = m0->ctx;
|
|
|
|
rep->loop[i].mtx_ctx1 = m1->ctx;
|
2014-03-19 23:00:38 +08:00
|
|
|
rep->loop[i].stk[0] = stk_to;
|
|
|
|
rep->loop[i].stk[1] = stk_from;
|
2014-02-28 18:48:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 21:21:30 +08:00
|
|
|
void DD::MutexAfterLock(DDCallback *cb, DDMutex *m, bool wlock, bool trylock) {
|
|
|
|
DDLogicalThread *lt = cb->lt;
|
2014-03-19 23:00:38 +08:00
|
|
|
u32 stk = 0;
|
|
|
|
if (flags.second_deadlock_stack)
|
|
|
|
stk = cb->Unwind();
|
2014-03-19 22:19:31 +08:00
|
|
|
// Printf("T%p MutexLock: %zx stk %u\n", lt, m->id, stk);
|
2014-03-17 22:41:36 +08:00
|
|
|
if (dd.onFirstLock(<->dd, m->id, stk))
|
2014-03-13 21:21:30 +08:00
|
|
|
return;
|
2014-03-17 22:41:36 +08:00
|
|
|
if (dd.onLockFast(<->dd, m->id, stk))
|
2014-03-14 17:22:01 +08:00
|
|
|
return;
|
|
|
|
|
2014-03-13 21:21:30 +08:00
|
|
|
SpinMutexLock lk(&mtx);
|
|
|
|
MutexEnsureID(lt, m);
|
|
|
|
if (wlock) // Only a recursive rlock may be held.
|
|
|
|
CHECK(!dd.isHeld(<->dd, m->id));
|
2014-03-14 15:09:01 +08:00
|
|
|
if (!trylock)
|
2014-03-21 21:00:18 +08:00
|
|
|
dd.addEdges(<->dd, m->id, stk ? stk : cb->Unwind(), cb->UniqueTid());
|
2014-03-17 22:41:36 +08:00
|
|
|
dd.onLockAfter(<->dd, m->id, stk);
|
2014-03-13 21:21:30 +08:00
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
void DD::MutexBeforeUnlock(DDCallback *cb, DDMutex *m, bool wlock) {
|
2014-03-13 21:21:30 +08:00
|
|
|
// Printf("T%p MutexUnLock: %zx\n", cb->lt, m->id);
|
2014-03-05 21:41:21 +08:00
|
|
|
dd.onUnlock(&cb->lt->dd, m->id);
|
2014-02-28 18:48:13 +08:00
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
void DD::MutexDestroy(DDCallback *cb,
|
2014-02-28 18:48:13 +08:00
|
|
|
DDMutex *m) {
|
2014-02-28 18:59:33 +08:00
|
|
|
if (!m->id) return;
|
2014-02-28 18:48:13 +08:00
|
|
|
SpinMutexLock lk(&mtx);
|
|
|
|
if (dd.nodeBelongsToCurrentEpoch(m->id))
|
|
|
|
dd.removeNode(m->id);
|
|
|
|
m->id = 0;
|
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:21 +08:00
|
|
|
DDReport *DD::GetReport(DDCallback *cb) {
|
|
|
|
if (!cb->lt->report_pending)
|
|
|
|
return 0;
|
|
|
|
cb->lt->report_pending = false;
|
|
|
|
return &cb->lt->rep;
|
|
|
|
}
|
|
|
|
|
2014-02-28 18:48:13 +08:00
|
|
|
} // namespace __sanitizer
|
2014-03-04 20:52:20 +08:00
|
|
|
#endif // #if SANITIZER_DEADLOCK_DETECTOR_VERSION == 1
|