Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 1 | //===-- 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 Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 12 | #include "sanitizer_common/sanitizer_placement_new.h" |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 13 | #include "sanitizer_common/sanitizer_flags.h" |
| 14 | #include "sanitizer_common/sanitizer_stacktrace.h" |
| 15 | #include "sanitizer_common/sanitizer_stackdepot.h" |
| 16 | |
| 17 | namespace __dsan { |
| 18 | |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 19 | static Context *ctx; |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 20 | |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 21 | static u32 CurrentStackTrace(Thread *thr, uptr skip) { |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 22 | StackTrace trace; |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 23 | thr->ignore_interceptors = true; |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 24 | trace.Unwind(1000, 0, 0, 0, 0, 0, false); |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 25 | thr->ignore_interceptors = false; |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 26 | if (trace.size <= skip) |
| 27 | return 0; |
| 28 | return StackDepotPut(trace.trace + skip, trace.size - skip); |
| 29 | } |
| 30 | |
| 31 | static void PrintStackTrace(Thread *thr, u32 stk) { |
| 32 | uptr size = 0; |
| 33 | const uptr *trace = StackDepotGet(stk, &size); |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 34 | thr->ignore_interceptors = true; |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 35 | StackTrace::PrintStack(trace, size); |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 36 | thr->ignore_interceptors = false; |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static void ReportDeadlock(Thread *thr, DDReport *rep) { |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 40 | if (rep == 0) |
| 41 | return; |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 42 | Printf("==============================\n"); |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 43 | Printf("WARNING: lock-order-inversion (potential deadlock)\n"); |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 44 | for (int i = 0; i < rep->n; i++) { |
Dmitry Vyukov | 69bd9ca | 2014-03-06 12:02:17 +0000 | [diff] [blame] | 45 | Printf("Thread %d locks mutex %llu while holding mutex %llu:\n", |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 46 | rep->loop[i].thr_ctx, rep->loop[i].mtx_ctx1, rep->loop[i].mtx_ctx0); |
Kostya Serebryany | e784620 | 2014-03-17 15:16:25 +0000 | [diff] [blame] | 47 | PrintStackTrace(thr, rep->loop[i].stk[1]); |
Dmitry Vyukov | 3cd028c | 2014-03-18 13:13:47 +0000 | [diff] [blame] | 48 | if (rep->loop[i].stk[0]) { |
| 49 | Printf("Mutex %llu was acquired here:\n", |
| 50 | rep->loop[i].mtx_ctx0); |
| 51 | PrintStackTrace(thr, rep->loop[i].stk[0]); |
| 52 | } |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 53 | } |
| 54 | Printf("==============================\n"); |
Dmitry Vyukov | b72bc2e | 2014-03-18 12:53:05 +0000 | [diff] [blame] | 55 | Die(); |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 58 | Callback::Callback(Thread *thr) |
| 59 | : thr(thr) { |
| 60 | lt = thr->dd_lt; |
| 61 | pt = thr->dd_pt; |
| 62 | } |
| 63 | |
| 64 | u32 Callback::Unwind() { |
| 65 | return CurrentStackTrace(thr, 3); |
| 66 | } |
| 67 | |
Dmitry Vyukov | 3cd028c | 2014-03-18 13:13:47 +0000 | [diff] [blame] | 68 | void InitializeFlags(Flags *f, const char *env) { |
| 69 | internal_memset(f, 0, sizeof(*f)); |
| 70 | |
| 71 | // Default values. |
| 72 | f->second_deadlock_stack = false; |
| 73 | |
| 74 | SetCommonFlagsDefaults(f); |
| 75 | // Override some common flags defaults. |
| 76 | f->allow_addr2line = true; |
| 77 | |
| 78 | // Override from command line. |
Alexander Potapenko | 1296436 | 2014-03-20 12:52:52 +0000 | [diff] [blame] | 79 | ParseFlag(env, &f->second_deadlock_stack, "second_deadlock_stack", ""); |
Dmitry Vyukov | 3cd028c | 2014-03-18 13:13:47 +0000 | [diff] [blame] | 80 | ParseCommonFlagsFromString(f, env); |
| 81 | |
| 82 | // Copy back to common flags. |
| 83 | *common_flags() = *f; |
| 84 | } |
| 85 | |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 86 | void Initialize() { |
| 87 | static u64 ctx_mem[sizeof(Context) / sizeof(u64) + 1]; |
| 88 | ctx = new(ctx_mem) Context(); |
| 89 | |
| 90 | InitializeInterceptors(); |
Dmitry Vyukov | 3cd028c | 2014-03-18 13:13:47 +0000 | [diff] [blame] | 91 | InitializeFlags(flags(), GetEnv("DSAN_OPTIONS")); |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 92 | common_flags()->symbolize = true; |
Dmitry Vyukov | 3cd028c | 2014-03-18 13:13:47 +0000 | [diff] [blame] | 93 | ctx->dd = DDetector::Create(flags()); |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void ThreadInit(Thread *thr) { |
| 97 | static atomic_uintptr_t id_gen; |
| 98 | uptr id = atomic_fetch_add(&id_gen, 1, memory_order_relaxed); |
| 99 | thr->dd_pt = ctx->dd->CreatePhysicalThread(); |
| 100 | thr->dd_lt = ctx->dd->CreateLogicalThread(id); |
| 101 | } |
| 102 | |
| 103 | void ThreadDestroy(Thread *thr) { |
| 104 | ctx->dd->DestroyPhysicalThread(thr->dd_pt); |
| 105 | ctx->dd->DestroyLogicalThread(thr->dd_lt); |
| 106 | } |
| 107 | |
| 108 | void MutexBeforeLock(Thread *thr, uptr m, bool writelock) { |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 109 | if (thr->ignore_interceptors) |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 110 | return; |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 111 | Callback cb(thr); |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 112 | { |
| 113 | MutexHashMap::Handle h(&ctx->mutex_map, m); |
| 114 | if (h.created()) |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 115 | ctx->dd->MutexInit(&cb, &h->dd); |
| 116 | ctx->dd->MutexBeforeLock(&cb, &h->dd, writelock); |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 117 | } |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 118 | ReportDeadlock(thr, ctx->dd->GetReport(&cb)); |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 121 | void MutexAfterLock(Thread *thr, uptr m, bool writelock, bool trylock) { |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 122 | if (thr->ignore_interceptors) |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 123 | return; |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 124 | Callback cb(thr); |
| 125 | { |
| 126 | MutexHashMap::Handle h(&ctx->mutex_map, m); |
| 127 | if (h.created()) |
| 128 | ctx->dd->MutexInit(&cb, &h->dd); |
| 129 | ctx->dd->MutexAfterLock(&cb, &h->dd, writelock, trylock); |
| 130 | } |
| 131 | ReportDeadlock(thr, ctx->dd->GetReport(&cb)); |
| 132 | } |
| 133 | |
| 134 | void MutexBeforeUnlock(Thread *thr, uptr m, bool writelock) { |
| 135 | if (thr->ignore_interceptors) |
| 136 | return; |
| 137 | Callback cb(thr); |
| 138 | { |
| 139 | MutexHashMap::Handle h(&ctx->mutex_map, m); |
| 140 | ctx->dd->MutexBeforeUnlock(&cb, &h->dd, writelock); |
| 141 | } |
| 142 | ReportDeadlock(thr, ctx->dd->GetReport(&cb)); |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void MutexDestroy(Thread *thr, uptr m) { |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 146 | if (thr->ignore_interceptors) |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 147 | return; |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 148 | Callback cb(thr); |
Dmitry Vyukov | 54a0303 | 2014-03-04 11:39:56 +0000 | [diff] [blame] | 149 | MutexHashMap::Handle h(&ctx->mutex_map, m, true); |
| 150 | if (!h.exists()) |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 151 | return; |
Dmitry Vyukov | 9b410fb | 2014-03-05 13:41:21 +0000 | [diff] [blame] | 152 | ctx->dd->MutexDestroy(&cb, &h->dd); |
Dmitry Vyukov | 512a18e | 2014-02-28 14:52:20 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace __dsan |