blob: 840a25a2ab7746af3c701ed43918e647733255eb [file] [log] [blame]
Dmitry Vyukov512a18e2014-02-28 14:52:20 +00001//===-- dd_rtl.cc ---------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "dd_rtl.h"
11#include "sanitizer_common/sanitizer_common.h"
Dmitry Vyukov54a03032014-03-04 11:39:56 +000012#include "sanitizer_common/sanitizer_placement_new.h"
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000013#include "sanitizer_common/sanitizer_flags.h"
14#include "sanitizer_common/sanitizer_stacktrace.h"
15#include "sanitizer_common/sanitizer_stackdepot.h"
16
17namespace __dsan {
18
Dmitry Vyukov54a03032014-03-04 11:39:56 +000019static Context *ctx;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000020
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000021static u32 CurrentStackTrace(Thread *thr, uptr skip) {
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000022 StackTrace trace;
Dmitry Vyukov54a03032014-03-04 11:39:56 +000023 thr->ignore_interceptors = true;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000024 trace.Unwind(1000, 0, 0, 0, 0, 0, false);
Dmitry Vyukov54a03032014-03-04 11:39:56 +000025 thr->ignore_interceptors = false;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000026 if (trace.size <= skip)
27 return 0;
28 return StackDepotPut(trace.trace + skip, trace.size - skip);
29}
30
31static void PrintStackTrace(Thread *thr, u32 stk) {
32 uptr size = 0;
33 const uptr *trace = StackDepotGet(stk, &size);
Dmitry Vyukov54a03032014-03-04 11:39:56 +000034 thr->ignore_interceptors = true;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000035 StackTrace::PrintStack(trace, size);
Dmitry Vyukov54a03032014-03-04 11:39:56 +000036 thr->ignore_interceptors = false;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000037}
38
39static void ReportDeadlock(Thread *thr, DDReport *rep) {
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000040 if (rep == 0)
41 return;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000042 Printf("==============================\n");
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000043 Printf("WARNING: lock-order-inversion (potential deadlock)\n");
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000044 for (int i = 0; i < rep->n; i++) {
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000045 Printf("Thread %d locks mutex %llu under mutex %llu:\n",
46 rep->loop[i].thr_ctx, rep->loop[i].mtx_ctx1, rep->loop[i].mtx_ctx0);
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000047 PrintStackTrace(thr, rep->loop[i].stk);
48 }
49 Printf("==============================\n");
50}
51
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000052Callback::Callback(Thread *thr)
53 : thr(thr) {
54 lt = thr->dd_lt;
55 pt = thr->dd_pt;
56}
57
58u32 Callback::Unwind() {
59 return CurrentStackTrace(thr, 3);
60}
61
62void Initialize() {
63 static u64 ctx_mem[sizeof(Context) / sizeof(u64) + 1];
64 ctx = new(ctx_mem) Context();
65
66 InitializeInterceptors();
67 ParseCommonFlagsFromString(flags(), GetEnv("DSAN_OPTIONS"));
68 //common_flags()->allow_addr2line = true;
69 common_flags()->symbolize = true;
70 ctx->dd = DDetector::Create();
71}
72
73void ThreadInit(Thread *thr) {
74 static atomic_uintptr_t id_gen;
75 uptr id = atomic_fetch_add(&id_gen, 1, memory_order_relaxed);
76 thr->dd_pt = ctx->dd->CreatePhysicalThread();
77 thr->dd_lt = ctx->dd->CreateLogicalThread(id);
78}
79
80void ThreadDestroy(Thread *thr) {
81 ctx->dd->DestroyPhysicalThread(thr->dd_pt);
82 ctx->dd->DestroyLogicalThread(thr->dd_lt);
83}
84
85void MutexBeforeLock(Thread *thr, uptr m, bool writelock) {
Dmitry Vyukov54a03032014-03-04 11:39:56 +000086 if (thr->ignore_interceptors)
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000087 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000088 Callback cb(thr);
Dmitry Vyukov54a03032014-03-04 11:39:56 +000089 {
90 MutexHashMap::Handle h(&ctx->mutex_map, m);
91 if (h.created())
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000092 ctx->dd->MutexInit(&cb, &h->dd);
93 ctx->dd->MutexBeforeLock(&cb, &h->dd, writelock);
Dmitry Vyukov54a03032014-03-04 11:39:56 +000094 }
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000095 ReportDeadlock(thr, ctx->dd->GetReport(&cb));
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000096}
97
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000098void MutexAfterLock(Thread *thr, uptr m, bool writelock, bool trylock) {
Dmitry Vyukov54a03032014-03-04 11:39:56 +000099 if (thr->ignore_interceptors)
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000100 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000101 Callback cb(thr);
102 {
103 MutexHashMap::Handle h(&ctx->mutex_map, m);
104 if (h.created())
105 ctx->dd->MutexInit(&cb, &h->dd);
106 ctx->dd->MutexAfterLock(&cb, &h->dd, writelock, trylock);
107 }
108 ReportDeadlock(thr, ctx->dd->GetReport(&cb));
109}
110
111void MutexBeforeUnlock(Thread *thr, uptr m, bool writelock) {
112 if (thr->ignore_interceptors)
113 return;
114 Callback cb(thr);
115 {
116 MutexHashMap::Handle h(&ctx->mutex_map, m);
117 ctx->dd->MutexBeforeUnlock(&cb, &h->dd, writelock);
118 }
119 ReportDeadlock(thr, ctx->dd->GetReport(&cb));
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000120}
121
122void MutexDestroy(Thread *thr, uptr m) {
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000123 if (thr->ignore_interceptors)
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000124 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000125 Callback cb(thr);
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000126 MutexHashMap::Handle h(&ctx->mutex_map, m, true);
127 if (!h.exists())
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000128 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000129 ctx->dd->MutexDestroy(&cb, &h->dd);
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000130}
131
132} // namespace __dsan