2012-11-22 23:03:11 +01:00
|
|
|
//===-- tsan_rtl_mutex.cc -------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of ThreadSanitizer (TSan), a race detector.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
#include <sanitizer_common/sanitizer_deadlock_detector_interface.h>
|
|
|
|
#include <sanitizer_common/sanitizer_stackdepot.h>
|
|
|
|
|
2012-11-22 23:03:11 +01:00
|
|
|
#include "tsan_rtl.h"
|
|
|
|
#include "tsan_flags.h"
|
|
|
|
#include "tsan_sync.h"
|
|
|
|
#include "tsan_report.h"
|
|
|
|
#include "tsan_symbolize.h"
|
|
|
|
#include "tsan_platform.h"
|
|
|
|
|
|
|
|
namespace __tsan {
|
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
void ReportDeadlock(ThreadState *thr, uptr pc, DDReport *r);
|
|
|
|
|
|
|
|
struct Callback : DDCallback {
|
|
|
|
ThreadState *thr;
|
|
|
|
uptr pc;
|
|
|
|
|
|
|
|
Callback(ThreadState *thr, uptr pc)
|
|
|
|
: thr(thr)
|
|
|
|
, pc(pc) {
|
|
|
|
DDCallback::pt = thr->dd_pt;
|
|
|
|
DDCallback::lt = thr->dd_lt;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual u32 Unwind() {
|
|
|
|
return CurrentStackId(thr, pc);
|
|
|
|
}
|
|
|
|
virtual int UniqueTid() {
|
|
|
|
return thr->unique_id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void DDMutexInit(ThreadState *thr, uptr pc, SyncVar *s) {
|
|
|
|
Callback cb(thr, pc);
|
|
|
|
ctx->dd->MutexInit(&cb, &s->dd);
|
|
|
|
s->dd.ctx = s->GetId();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ReportMutexMisuse(ThreadState *thr, uptr pc, ReportType typ,
|
|
|
|
uptr addr, u64 mid) {
|
2014-09-23 19:59:53 +02:00
|
|
|
// In Go, these misuses are either impossible, or detected by std lib,
|
|
|
|
// or false positives (e.g. unlock in a different thread).
|
|
|
|
if (kGoMode)
|
|
|
|
return;
|
2014-05-22 09:09:21 +02:00
|
|
|
ThreadRegistryLock l(ctx->thread_registry);
|
|
|
|
ScopedReport rep(typ);
|
|
|
|
rep.AddMutex(mid);
|
2014-11-13 21:41:38 +01:00
|
|
|
VarSizeStackTrace trace;
|
|
|
|
ObtainCurrentStack(thr, pc, &trace);
|
|
|
|
rep.AddStack(trace, true);
|
2014-05-22 09:09:21 +02:00
|
|
|
rep.AddLocation(addr, 1);
|
2014-09-23 19:59:53 +02:00
|
|
|
OutputReport(thr, rep);
|
2014-05-22 09:09:21 +02:00
|
|
|
}
|
|
|
|
|
2012-11-22 23:03:11 +01:00
|
|
|
void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
|
|
|
|
bool rw, bool recursive, bool linker_init) {
|
|
|
|
DPrintf("#%d: MutexCreate %zx\n", thr->tid, addr);
|
|
|
|
StatInc(thr, StatMutexCreate);
|
2013-02-13 11:46:01 +01:00
|
|
|
if (!linker_init && IsAppMem(addr)) {
|
|
|
|
CHECK(!thr->is_freeing);
|
|
|
|
thr->is_freeing = true;
|
|
|
|
MemoryWrite(thr, pc, addr, kSizeLog1);
|
|
|
|
thr->is_freeing = false;
|
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetOrCreateAndLock(thr, pc, addr, true);
|
2012-11-22 23:03:11 +01:00
|
|
|
s->is_rw = rw;
|
|
|
|
s->is_recursive = recursive;
|
|
|
|
s->is_linker_init = linker_init;
|
2014-09-23 19:59:53 +02:00
|
|
|
if (kCppMode && s->creation_stack_id == 0)
|
|
|
|
s->creation_stack_id = CurrentStackId(thr, pc);
|
2012-11-22 23:03:11 +01:00
|
|
|
s->mtx.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MutexDestroy(ThreadState *thr, uptr pc, uptr addr) {
|
|
|
|
DPrintf("#%d: MutexDestroy %zx\n", thr->tid, addr);
|
|
|
|
StatInc(thr, StatMutexDestroy);
|
|
|
|
#ifndef TSAN_GO
|
|
|
|
// Global mutexes not marked as LINKER_INITIALIZED
|
|
|
|
// cause tons of not interesting reports, so just ignore it.
|
|
|
|
if (IsGlobalVar(addr))
|
|
|
|
return;
|
|
|
|
#endif
|
2013-02-13 11:46:01 +01:00
|
|
|
if (IsAppMem(addr)) {
|
|
|
|
CHECK(!thr->is_freeing);
|
|
|
|
thr->is_freeing = true;
|
|
|
|
MemoryWrite(thr, pc, addr, kSizeLog1);
|
|
|
|
thr->is_freeing = false;
|
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetIfExistsAndLock(addr);
|
|
|
|
if (s == 0)
|
|
|
|
return;
|
|
|
|
if (common_flags()->detect_deadlocks) {
|
|
|
|
Callback cb(thr, pc);
|
|
|
|
ctx->dd->MutexDestroy(&cb, &s->dd);
|
|
|
|
ctx->dd->MutexInit(&cb, &s->dd);
|
|
|
|
}
|
|
|
|
bool unlock_locked = false;
|
2012-11-22 23:03:11 +01:00
|
|
|
if (flags()->report_destroy_locked
|
|
|
|
&& s->owner_tid != SyncVar::kInvalidTid
|
|
|
|
&& !s->is_broken) {
|
|
|
|
s->is_broken = true;
|
2014-09-23 19:59:53 +02:00
|
|
|
unlock_locked = true;
|
|
|
|
}
|
|
|
|
u64 mid = s->GetId();
|
|
|
|
u32 last_lock = s->last_lock;
|
|
|
|
if (!unlock_locked)
|
|
|
|
s->Reset(thr); // must not reset it before the report is printed
|
|
|
|
s->mtx.Unlock();
|
|
|
|
if (unlock_locked) {
|
2013-11-04 22:33:31 +01:00
|
|
|
ThreadRegistryLock l(ctx->thread_registry);
|
2012-11-22 23:03:11 +01:00
|
|
|
ScopedReport rep(ReportTypeMutexDestroyLocked);
|
2014-09-23 19:59:53 +02:00
|
|
|
rep.AddMutex(mid);
|
2014-11-13 21:41:38 +01:00
|
|
|
VarSizeStackTrace trace;
|
|
|
|
ObtainCurrentStack(thr, pc, &trace);
|
|
|
|
rep.AddStack(trace);
|
2014-09-23 19:59:53 +02:00
|
|
|
FastState last(last_lock);
|
2013-01-10 13:44:08 +01:00
|
|
|
RestoreStack(last.tid(), last.epoch(), &trace, 0);
|
2014-11-13 21:41:38 +01:00
|
|
|
rep.AddStack(trace, true);
|
2014-09-23 19:59:53 +02:00
|
|
|
rep.AddLocation(addr, 1);
|
|
|
|
OutputReport(thr, rep);
|
|
|
|
}
|
|
|
|
if (unlock_locked) {
|
|
|
|
SyncVar *s = ctx->metamap.GetIfExistsAndLock(addr);
|
|
|
|
if (s != 0) {
|
|
|
|
s->Reset(thr);
|
|
|
|
s->mtx.Unlock();
|
|
|
|
}
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
thr->mset.Remove(mid);
|
|
|
|
// s will be destroyed and freed in MetaMap::FreeBlock.
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
void MutexLock(ThreadState *thr, uptr pc, uptr addr, int rec, bool try_lock) {
|
2013-11-04 22:33:31 +01:00
|
|
|
DPrintf("#%d: MutexLock %zx rec=%d\n", thr->tid, addr, rec);
|
|
|
|
CHECK_GT(rec, 0);
|
2012-11-22 23:03:11 +01:00
|
|
|
if (IsAppMem(addr))
|
2013-02-13 11:46:01 +01:00
|
|
|
MemoryReadAtomic(thr, pc, addr, kSizeLog1);
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetOrCreateAndLock(thr, pc, addr, true);
|
2012-11-22 23:03:11 +01:00
|
|
|
thr->fast_state.IncrementEpoch();
|
2013-01-10 13:44:08 +01:00
|
|
|
TraceAddEvent(thr, thr->fast_state, EventTypeLock, s->GetId());
|
2014-05-22 09:09:21 +02:00
|
|
|
bool report_double_lock = false;
|
2012-11-22 23:03:11 +01:00
|
|
|
if (s->owner_tid == SyncVar::kInvalidTid) {
|
|
|
|
CHECK_EQ(s->recursion, 0);
|
|
|
|
s->owner_tid = thr->tid;
|
|
|
|
s->last_lock = thr->fast_state.raw();
|
|
|
|
} else if (s->owner_tid == thr->tid) {
|
|
|
|
CHECK_GT(s->recursion, 0);
|
2014-05-22 09:09:21 +02:00
|
|
|
} else if (flags()->report_mutex_bugs && !s->is_broken) {
|
|
|
|
s->is_broken = true;
|
|
|
|
report_double_lock = true;
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
if (s->recursion == 0) {
|
|
|
|
StatInc(thr, StatMutexLock);
|
2013-12-05 10:18:38 +01:00
|
|
|
AcquireImpl(thr, pc, &s->clock);
|
|
|
|
AcquireImpl(thr, pc, &s->read_clock);
|
2012-11-22 23:03:11 +01:00
|
|
|
} else if (!s->is_recursive) {
|
|
|
|
StatInc(thr, StatMutexRecLock);
|
|
|
|
}
|
2013-11-04 22:33:31 +01:00
|
|
|
s->recursion += rec;
|
2013-01-10 13:44:08 +01:00
|
|
|
thr->mset.Add(s->GetId(), true, thr->fast_state.epoch());
|
2014-09-23 19:59:53 +02:00
|
|
|
if (common_flags()->detect_deadlocks && (s->recursion - rec) == 0) {
|
2014-05-22 09:09:21 +02:00
|
|
|
Callback cb(thr, pc);
|
|
|
|
if (!try_lock)
|
|
|
|
ctx->dd->MutexBeforeLock(&cb, &s->dd, true);
|
|
|
|
ctx->dd->MutexAfterLock(&cb, &s->dd, true, try_lock);
|
|
|
|
}
|
|
|
|
u64 mid = s->GetId();
|
2012-11-22 23:03:11 +01:00
|
|
|
s->mtx.Unlock();
|
2014-05-22 09:09:21 +02:00
|
|
|
// Can't touch s after this point.
|
|
|
|
if (report_double_lock)
|
|
|
|
ReportMutexMisuse(thr, pc, ReportTypeMutexDoubleLock, addr, mid);
|
2014-09-23 19:59:53 +02:00
|
|
|
if (common_flags()->detect_deadlocks) {
|
2014-05-22 09:09:21 +02:00
|
|
|
Callback cb(thr, pc);
|
|
|
|
ReportDeadlock(thr, pc, ctx->dd->GetReport(&cb));
|
|
|
|
}
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, bool all) {
|
|
|
|
DPrintf("#%d: MutexUnlock %zx all=%d\n", thr->tid, addr, all);
|
2012-11-22 23:03:11 +01:00
|
|
|
if (IsAppMem(addr))
|
2013-02-13 11:46:01 +01:00
|
|
|
MemoryReadAtomic(thr, pc, addr, kSizeLog1);
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetOrCreateAndLock(thr, pc, addr, true);
|
2012-11-22 23:03:11 +01:00
|
|
|
thr->fast_state.IncrementEpoch();
|
2013-01-10 13:44:08 +01:00
|
|
|
TraceAddEvent(thr, thr->fast_state, EventTypeUnlock, s->GetId());
|
2013-11-04 22:33:31 +01:00
|
|
|
int rec = 0;
|
2014-05-22 09:09:21 +02:00
|
|
|
bool report_bad_unlock = false;
|
2014-09-23 19:59:53 +02:00
|
|
|
if (kCppMode && (s->recursion == 0 || s->owner_tid != thr->tid)) {
|
2014-05-22 09:09:21 +02:00
|
|
|
if (flags()->report_mutex_bugs && !s->is_broken) {
|
2012-11-22 23:03:11 +01:00
|
|
|
s->is_broken = true;
|
2014-05-22 09:09:21 +02:00
|
|
|
report_bad_unlock = true;
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-11-04 22:33:31 +01:00
|
|
|
rec = all ? s->recursion : 1;
|
|
|
|
s->recursion -= rec;
|
2012-11-22 23:03:11 +01:00
|
|
|
if (s->recursion == 0) {
|
|
|
|
StatInc(thr, StatMutexUnlock);
|
|
|
|
s->owner_tid = SyncVar::kInvalidTid;
|
2013-12-05 10:18:38 +01:00
|
|
|
ReleaseStoreImpl(thr, pc, &s->clock);
|
2012-11-22 23:03:11 +01:00
|
|
|
} else {
|
|
|
|
StatInc(thr, StatMutexRecUnlock);
|
|
|
|
}
|
|
|
|
}
|
2013-01-10 13:44:08 +01:00
|
|
|
thr->mset.Del(s->GetId(), true);
|
2014-09-23 19:59:53 +02:00
|
|
|
if (common_flags()->detect_deadlocks && s->recursion == 0 &&
|
|
|
|
!report_bad_unlock) {
|
2014-05-22 09:09:21 +02:00
|
|
|
Callback cb(thr, pc);
|
|
|
|
ctx->dd->MutexBeforeUnlock(&cb, &s->dd, true);
|
|
|
|
}
|
|
|
|
u64 mid = s->GetId();
|
2012-11-22 23:03:11 +01:00
|
|
|
s->mtx.Unlock();
|
2014-05-22 09:09:21 +02:00
|
|
|
// Can't touch s after this point.
|
|
|
|
if (report_bad_unlock)
|
|
|
|
ReportMutexMisuse(thr, pc, ReportTypeMutexBadUnlock, addr, mid);
|
2014-09-23 19:59:53 +02:00
|
|
|
if (common_flags()->detect_deadlocks && !report_bad_unlock) {
|
2014-05-22 09:09:21 +02:00
|
|
|
Callback cb(thr, pc);
|
|
|
|
ReportDeadlock(thr, pc, ctx->dd->GetReport(&cb));
|
|
|
|
}
|
2013-11-04 22:33:31 +01:00
|
|
|
return rec;
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
void MutexReadLock(ThreadState *thr, uptr pc, uptr addr, bool trylock) {
|
2012-11-22 23:03:11 +01:00
|
|
|
DPrintf("#%d: MutexReadLock %zx\n", thr->tid, addr);
|
|
|
|
StatInc(thr, StatMutexReadLock);
|
|
|
|
if (IsAppMem(addr))
|
2013-02-13 11:46:01 +01:00
|
|
|
MemoryReadAtomic(thr, pc, addr, kSizeLog1);
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetOrCreateAndLock(thr, pc, addr, false);
|
2012-11-22 23:03:11 +01:00
|
|
|
thr->fast_state.IncrementEpoch();
|
2013-01-10 13:44:08 +01:00
|
|
|
TraceAddEvent(thr, thr->fast_state, EventTypeRLock, s->GetId());
|
2014-05-22 09:09:21 +02:00
|
|
|
bool report_bad_lock = false;
|
2012-11-22 23:03:11 +01:00
|
|
|
if (s->owner_tid != SyncVar::kInvalidTid) {
|
2014-05-22 09:09:21 +02:00
|
|
|
if (flags()->report_mutex_bugs && !s->is_broken) {
|
|
|
|
s->is_broken = true;
|
|
|
|
report_bad_lock = true;
|
|
|
|
}
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
2013-12-05 10:18:38 +01:00
|
|
|
AcquireImpl(thr, pc, &s->clock);
|
2012-11-22 23:03:11 +01:00
|
|
|
s->last_lock = thr->fast_state.raw();
|
2013-01-10 13:44:08 +01:00
|
|
|
thr->mset.Add(s->GetId(), false, thr->fast_state.epoch());
|
2014-09-23 19:59:53 +02:00
|
|
|
if (common_flags()->detect_deadlocks && s->recursion == 0) {
|
2014-05-22 09:09:21 +02:00
|
|
|
Callback cb(thr, pc);
|
|
|
|
if (!trylock)
|
|
|
|
ctx->dd->MutexBeforeLock(&cb, &s->dd, false);
|
|
|
|
ctx->dd->MutexAfterLock(&cb, &s->dd, false, trylock);
|
|
|
|
}
|
|
|
|
u64 mid = s->GetId();
|
2012-11-22 23:03:11 +01:00
|
|
|
s->mtx.ReadUnlock();
|
2014-05-22 09:09:21 +02:00
|
|
|
// Can't touch s after this point.
|
|
|
|
if (report_bad_lock)
|
|
|
|
ReportMutexMisuse(thr, pc, ReportTypeMutexBadReadLock, addr, mid);
|
2014-09-23 19:59:53 +02:00
|
|
|
if (common_flags()->detect_deadlocks) {
|
2014-05-22 09:09:21 +02:00
|
|
|
Callback cb(thr, pc);
|
|
|
|
ReportDeadlock(thr, pc, ctx->dd->GetReport(&cb));
|
|
|
|
}
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr) {
|
|
|
|
DPrintf("#%d: MutexReadUnlock %zx\n", thr->tid, addr);
|
|
|
|
StatInc(thr, StatMutexReadUnlock);
|
|
|
|
if (IsAppMem(addr))
|
2013-02-13 11:46:01 +01:00
|
|
|
MemoryReadAtomic(thr, pc, addr, kSizeLog1);
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetOrCreateAndLock(thr, pc, addr, true);
|
2012-11-22 23:03:11 +01:00
|
|
|
thr->fast_state.IncrementEpoch();
|
2013-01-10 13:44:08 +01:00
|
|
|
TraceAddEvent(thr, thr->fast_state, EventTypeRUnlock, s->GetId());
|
2014-05-22 09:09:21 +02:00
|
|
|
bool report_bad_unlock = false;
|
2012-11-22 23:03:11 +01:00
|
|
|
if (s->owner_tid != SyncVar::kInvalidTid) {
|
2014-05-22 09:09:21 +02:00
|
|
|
if (flags()->report_mutex_bugs && !s->is_broken) {
|
|
|
|
s->is_broken = true;
|
|
|
|
report_bad_unlock = true;
|
|
|
|
}
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
2013-12-05 10:18:38 +01:00
|
|
|
ReleaseImpl(thr, pc, &s->read_clock);
|
2014-09-23 19:59:53 +02:00
|
|
|
if (common_flags()->detect_deadlocks && s->recursion == 0) {
|
2014-05-22 09:09:21 +02:00
|
|
|
Callback cb(thr, pc);
|
|
|
|
ctx->dd->MutexBeforeUnlock(&cb, &s->dd, false);
|
|
|
|
}
|
|
|
|
u64 mid = s->GetId();
|
2012-11-22 23:03:11 +01:00
|
|
|
s->mtx.Unlock();
|
2014-05-22 09:09:21 +02:00
|
|
|
// Can't touch s after this point.
|
|
|
|
thr->mset.Del(mid, false);
|
|
|
|
if (report_bad_unlock)
|
|
|
|
ReportMutexMisuse(thr, pc, ReportTypeMutexBadReadUnlock, addr, mid);
|
2014-09-23 19:59:53 +02:00
|
|
|
if (common_flags()->detect_deadlocks) {
|
2014-05-22 09:09:21 +02:00
|
|
|
Callback cb(thr, pc);
|
|
|
|
ReportDeadlock(thr, pc, ctx->dd->GetReport(&cb));
|
|
|
|
}
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr) {
|
|
|
|
DPrintf("#%d: MutexReadOrWriteUnlock %zx\n", thr->tid, addr);
|
|
|
|
if (IsAppMem(addr))
|
2013-02-13 11:46:01 +01:00
|
|
|
MemoryReadAtomic(thr, pc, addr, kSizeLog1);
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetOrCreateAndLock(thr, pc, addr, true);
|
2013-01-10 13:44:08 +01:00
|
|
|
bool write = true;
|
2014-05-22 09:09:21 +02:00
|
|
|
bool report_bad_unlock = false;
|
2012-11-22 23:03:11 +01:00
|
|
|
if (s->owner_tid == SyncVar::kInvalidTid) {
|
|
|
|
// Seems to be read unlock.
|
2013-01-10 13:44:08 +01:00
|
|
|
write = false;
|
2012-11-22 23:03:11 +01:00
|
|
|
StatInc(thr, StatMutexReadUnlock);
|
|
|
|
thr->fast_state.IncrementEpoch();
|
2013-01-10 13:44:08 +01:00
|
|
|
TraceAddEvent(thr, thr->fast_state, EventTypeRUnlock, s->GetId());
|
2013-12-05 10:18:38 +01:00
|
|
|
ReleaseImpl(thr, pc, &s->read_clock);
|
2012-11-22 23:03:11 +01:00
|
|
|
} else if (s->owner_tid == thr->tid) {
|
|
|
|
// Seems to be write unlock.
|
2013-01-10 13:44:08 +01:00
|
|
|
thr->fast_state.IncrementEpoch();
|
|
|
|
TraceAddEvent(thr, thr->fast_state, EventTypeUnlock, s->GetId());
|
2012-11-22 23:03:11 +01:00
|
|
|
CHECK_GT(s->recursion, 0);
|
|
|
|
s->recursion--;
|
|
|
|
if (s->recursion == 0) {
|
|
|
|
StatInc(thr, StatMutexUnlock);
|
|
|
|
s->owner_tid = SyncVar::kInvalidTid;
|
2013-12-05 10:18:38 +01:00
|
|
|
ReleaseImpl(thr, pc, &s->clock);
|
2012-11-22 23:03:11 +01:00
|
|
|
} else {
|
|
|
|
StatInc(thr, StatMutexRecUnlock);
|
|
|
|
}
|
|
|
|
} else if (!s->is_broken) {
|
|
|
|
s->is_broken = true;
|
2014-05-22 09:09:21 +02:00
|
|
|
report_bad_unlock = true;
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
2013-01-10 13:44:08 +01:00
|
|
|
thr->mset.Del(s->GetId(), write);
|
2014-09-23 19:59:53 +02:00
|
|
|
if (common_flags()->detect_deadlocks && s->recursion == 0) {
|
2014-05-22 09:09:21 +02:00
|
|
|
Callback cb(thr, pc);
|
|
|
|
ctx->dd->MutexBeforeUnlock(&cb, &s->dd, write);
|
|
|
|
}
|
|
|
|
u64 mid = s->GetId();
|
2012-11-22 23:03:11 +01:00
|
|
|
s->mtx.Unlock();
|
2014-05-22 09:09:21 +02:00
|
|
|
// Can't touch s after this point.
|
|
|
|
if (report_bad_unlock)
|
|
|
|
ReportMutexMisuse(thr, pc, ReportTypeMutexBadUnlock, addr, mid);
|
2014-09-23 19:59:53 +02:00
|
|
|
if (common_flags()->detect_deadlocks) {
|
2014-05-22 09:09:21 +02:00
|
|
|
Callback cb(thr, pc);
|
|
|
|
ReportDeadlock(thr, pc, ctx->dd->GetReport(&cb));
|
|
|
|
}
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 10:18:38 +01:00
|
|
|
void MutexRepair(ThreadState *thr, uptr pc, uptr addr) {
|
|
|
|
DPrintf("#%d: MutexRepair %zx\n", thr->tid, addr);
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetOrCreateAndLock(thr, pc, addr, true);
|
2013-12-05 10:18:38 +01:00
|
|
|
s->owner_tid = SyncVar::kInvalidTid;
|
|
|
|
s->recursion = 0;
|
|
|
|
s->mtx.Unlock();
|
|
|
|
}
|
|
|
|
|
2012-11-22 23:03:11 +01:00
|
|
|
void Acquire(ThreadState *thr, uptr pc, uptr addr) {
|
|
|
|
DPrintf("#%d: Acquire %zx\n", thr->tid, addr);
|
2013-12-05 10:18:38 +01:00
|
|
|
if (thr->ignore_sync)
|
|
|
|
return;
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetOrCreateAndLock(thr, pc, addr, false);
|
2013-12-05 10:18:38 +01:00
|
|
|
AcquireImpl(thr, pc, &s->clock);
|
2012-11-22 23:03:11 +01:00
|
|
|
s->mtx.ReadUnlock();
|
|
|
|
}
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
static void UpdateClockCallback(ThreadContextBase *tctx_base, void *arg) {
|
|
|
|
ThreadState *thr = reinterpret_cast<ThreadState*>(arg);
|
|
|
|
ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
|
|
|
|
if (tctx->status == ThreadStatusRunning)
|
|
|
|
thr->clock.set(tctx->tid, tctx->thr->fast_state.epoch());
|
|
|
|
else
|
|
|
|
thr->clock.set(tctx->tid, tctx->epoch1);
|
|
|
|
}
|
|
|
|
|
2012-11-23 15:46:25 +01:00
|
|
|
void AcquireGlobal(ThreadState *thr, uptr pc) {
|
2013-12-05 10:18:38 +01:00
|
|
|
DPrintf("#%d: AcquireGlobal\n", thr->tid);
|
|
|
|
if (thr->ignore_sync)
|
|
|
|
return;
|
2014-05-22 09:09:21 +02:00
|
|
|
ThreadRegistryLock l(ctx->thread_registry);
|
|
|
|
ctx->thread_registry->RunCallbackForEachThreadLocked(
|
2013-11-04 22:33:31 +01:00
|
|
|
UpdateClockCallback, thr);
|
2012-11-23 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
2012-11-22 23:03:11 +01:00
|
|
|
void Release(ThreadState *thr, uptr pc, uptr addr) {
|
|
|
|
DPrintf("#%d: Release %zx\n", thr->tid, addr);
|
2013-12-05 10:18:38 +01:00
|
|
|
if (thr->ignore_sync)
|
|
|
|
return;
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetOrCreateAndLock(thr, pc, addr, true);
|
2013-12-05 10:18:38 +01:00
|
|
|
thr->fast_state.IncrementEpoch();
|
|
|
|
// Can't increment epoch w/o writing to the trace as well.
|
|
|
|
TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
|
|
|
|
ReleaseImpl(thr, pc, &s->clock);
|
2012-11-22 23:03:11 +01:00
|
|
|
s->mtx.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseStore(ThreadState *thr, uptr pc, uptr addr) {
|
|
|
|
DPrintf("#%d: ReleaseStore %zx\n", thr->tid, addr);
|
2013-12-05 10:18:38 +01:00
|
|
|
if (thr->ignore_sync)
|
|
|
|
return;
|
2014-09-23 19:59:53 +02:00
|
|
|
SyncVar *s = ctx->metamap.GetOrCreateAndLock(thr, pc, addr, true);
|
2013-12-05 10:18:38 +01:00
|
|
|
thr->fast_state.IncrementEpoch();
|
|
|
|
// Can't increment epoch w/o writing to the trace as well.
|
|
|
|
TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
|
|
|
|
ReleaseStoreImpl(thr, pc, &s->clock);
|
2012-11-22 23:03:11 +01:00
|
|
|
s->mtx.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TSAN_GO
|
2013-11-04 22:33:31 +01:00
|
|
|
static void UpdateSleepClockCallback(ThreadContextBase *tctx_base, void *arg) {
|
|
|
|
ThreadState *thr = reinterpret_cast<ThreadState*>(arg);
|
|
|
|
ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
|
|
|
|
if (tctx->status == ThreadStatusRunning)
|
|
|
|
thr->last_sleep_clock.set(tctx->tid, tctx->thr->fast_state.epoch());
|
|
|
|
else
|
|
|
|
thr->last_sleep_clock.set(tctx->tid, tctx->epoch1);
|
|
|
|
}
|
|
|
|
|
2012-11-22 23:03:11 +01:00
|
|
|
void AfterSleep(ThreadState *thr, uptr pc) {
|
2013-12-05 10:18:38 +01:00
|
|
|
DPrintf("#%d: AfterSleep %zx\n", thr->tid);
|
|
|
|
if (thr->ignore_sync)
|
|
|
|
return;
|
2012-11-22 23:03:11 +01:00
|
|
|
thr->last_sleep_stack_id = CurrentStackId(thr, pc);
|
2014-05-22 09:09:21 +02:00
|
|
|
ThreadRegistryLock l(ctx->thread_registry);
|
|
|
|
ctx->thread_registry->RunCallbackForEachThreadLocked(
|
2013-11-04 22:33:31 +01:00
|
|
|
UpdateSleepClockCallback, thr);
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-05 10:18:38 +01:00
|
|
|
void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c) {
|
|
|
|
if (thr->ignore_sync)
|
|
|
|
return;
|
2014-05-22 09:09:21 +02:00
|
|
|
thr->clock.set(thr->fast_state.epoch());
|
2014-09-23 19:59:53 +02:00
|
|
|
thr->clock.acquire(&thr->clock_cache, c);
|
2013-12-05 10:18:38 +01:00
|
|
|
StatInc(thr, StatSyncAcquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c) {
|
|
|
|
if (thr->ignore_sync)
|
|
|
|
return;
|
2014-05-22 09:09:21 +02:00
|
|
|
thr->clock.set(thr->fast_state.epoch());
|
2013-12-05 10:18:38 +01:00
|
|
|
thr->fast_synch_epoch = thr->fast_state.epoch();
|
2014-09-23 19:59:53 +02:00
|
|
|
thr->clock.release(&thr->clock_cache, c);
|
2013-12-05 10:18:38 +01:00
|
|
|
StatInc(thr, StatSyncRelease);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c) {
|
|
|
|
if (thr->ignore_sync)
|
|
|
|
return;
|
2014-05-22 09:09:21 +02:00
|
|
|
thr->clock.set(thr->fast_state.epoch());
|
2013-12-05 10:18:38 +01:00
|
|
|
thr->fast_synch_epoch = thr->fast_state.epoch();
|
2014-09-23 19:59:53 +02:00
|
|
|
thr->clock.ReleaseStore(&thr->clock_cache, c);
|
2013-12-05 10:18:38 +01:00
|
|
|
StatInc(thr, StatSyncRelease);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c) {
|
|
|
|
if (thr->ignore_sync)
|
|
|
|
return;
|
2014-05-22 09:09:21 +02:00
|
|
|
thr->clock.set(thr->fast_state.epoch());
|
2013-12-05 10:18:38 +01:00
|
|
|
thr->fast_synch_epoch = thr->fast_state.epoch();
|
2014-09-23 19:59:53 +02:00
|
|
|
thr->clock.acq_rel(&thr->clock_cache, c);
|
2013-12-05 10:18:38 +01:00
|
|
|
StatInc(thr, StatSyncAcquire);
|
|
|
|
StatInc(thr, StatSyncRelease);
|
|
|
|
}
|
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
void ReportDeadlock(ThreadState *thr, uptr pc, DDReport *r) {
|
|
|
|
if (r == 0)
|
|
|
|
return;
|
|
|
|
ThreadRegistryLock l(ctx->thread_registry);
|
|
|
|
ScopedReport rep(ReportTypeDeadlock);
|
|
|
|
for (int i = 0; i < r->n; i++) {
|
|
|
|
rep.AddMutex(r->loop[i].mtx_ctx0);
|
|
|
|
rep.AddUniqueTid((int)r->loop[i].thr_ctx);
|
|
|
|
rep.AddThread((int)r->loop[i].thr_ctx);
|
|
|
|
}
|
|
|
|
uptr dummy_pc = 0x42;
|
|
|
|
for (int i = 0; i < r->n; i++) {
|
|
|
|
for (int j = 0; j < (flags()->second_deadlock_stack ? 2 : 1); j++) {
|
|
|
|
u32 stk = r->loop[i].stk[j];
|
|
|
|
if (stk) {
|
2014-11-13 21:41:38 +01:00
|
|
|
rep.AddStack(StackDepotGet(stk), true);
|
2014-05-22 09:09:21 +02:00
|
|
|
} else {
|
|
|
|
// Sometimes we fail to extract the stack trace (FIXME: investigate),
|
|
|
|
// but we should still produce some stack trace in the report.
|
2014-11-13 21:41:38 +01:00
|
|
|
rep.AddStack(StackTrace(&dummy_pc, 1), true);
|
2014-05-22 09:09:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
OutputReport(thr, rep);
|
2014-05-22 09:09:21 +02:00
|
|
|
}
|
|
|
|
|
2012-11-22 23:03:11 +01:00
|
|
|
} // namespace __tsan
|