blob: cc8e5a0c48bd49b51430e2b335bd0cbe4d888693 [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) {
Alexey Samsonov3741ab82014-10-26 06:23:07 +000022 BufferedStackTrace stack;
Dmitry Vyukov54a03032014-03-04 11:39:56 +000023 thr->ignore_interceptors = true;
Alexey Samsonov3741ab82014-10-26 06:23:07 +000024 stack.Unwind(1000, 0, 0, 0, 0, 0, false);
Dmitry Vyukov54a03032014-03-04 11:39:56 +000025 thr->ignore_interceptors = false;
Alexey Samsonov3741ab82014-10-26 06:23:07 +000026 if (stack.size <= skip)
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000027 return 0;
Alexey Samsonov3741ab82014-10-26 06:23:07 +000028 return StackDepotPut(StackTrace(stack.trace + skip, stack.size - skip));
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000029}
30
31static void PrintStackTrace(Thread *thr, u32 stk) {
Alexey Samsonov6e7af812014-10-26 05:43:17 +000032 StackTrace stack = StackDepotGet(stk);
Dmitry Vyukov54a03032014-03-04 11:39:56 +000033 thr->ignore_interceptors = true;
Alexey Samsonov6e7af812014-10-26 05:43:17 +000034 stack.Print();
Dmitry Vyukov54a03032014-03-04 11:39:56 +000035 thr->ignore_interceptors = false;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000036}
37
38static void ReportDeadlock(Thread *thr, DDReport *rep) {
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000039 if (rep == 0)
40 return;
Dmitry Vyukov51f5b5f2014-04-11 17:54:27 +000041 BlockingMutexLock lock(&ctx->report_mutex);
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 Vyukov69bd9ca2014-03-06 12:02:17 +000045 Printf("Thread %d locks mutex %llu while holding mutex %llu:\n",
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000046 rep->loop[i].thr_ctx, rep->loop[i].mtx_ctx1, rep->loop[i].mtx_ctx0);
Kostya Serebryanye7846202014-03-17 15:16:25 +000047 PrintStackTrace(thr, rep->loop[i].stk[1]);
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000048 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 Vyukov512a18e2014-02-28 14:52:20 +000053 }
54 Printf("==============================\n");
55}
56
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000057Callback::Callback(Thread *thr)
58 : thr(thr) {
59 lt = thr->dd_lt;
60 pt = thr->dd_pt;
61}
62
63u32 Callback::Unwind() {
64 return CurrentStackTrace(thr, 3);
65}
66
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000067void InitializeFlags(Flags *f, const char *env) {
68 internal_memset(f, 0, sizeof(*f));
69
70 // Default values.
71 f->second_deadlock_stack = false;
72
Chandler Carruth6173e862015-01-02 09:59:38 +000073 CommonFlags *cf = common_flags();
Alexey Samsonov2f8c8d52014-12-19 21:40:04 +000074 SetCommonFlagsDefaults();
Chandler Carruth6173e862015-01-02 09:59:38 +000075 // Override some common flags defaults.
76 cf->allow_addr2line = true;
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000077
78 // Override from command line.
Alexander Potapenko12964362014-03-20 12:52:52 +000079 ParseFlag(env, &f->second_deadlock_stack, "second_deadlock_stack", "");
Alexey Samsonov2f8c8d52014-12-19 21:40:04 +000080 ParseCommonFlagsFromString(env);
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000081}
82
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000083void Initialize() {
84 static u64 ctx_mem[sizeof(Context) / sizeof(u64) + 1];
85 ctx = new(ctx_mem) Context();
86
87 InitializeInterceptors();
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000088 InitializeFlags(flags(), GetEnv("DSAN_OPTIONS"));
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000089 ctx->dd = DDetector::Create(flags());
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000090}
91
92void ThreadInit(Thread *thr) {
93 static atomic_uintptr_t id_gen;
94 uptr id = atomic_fetch_add(&id_gen, 1, memory_order_relaxed);
95 thr->dd_pt = ctx->dd->CreatePhysicalThread();
96 thr->dd_lt = ctx->dd->CreateLogicalThread(id);
97}
98
99void ThreadDestroy(Thread *thr) {
100 ctx->dd->DestroyPhysicalThread(thr->dd_pt);
101 ctx->dd->DestroyLogicalThread(thr->dd_lt);
102}
103
104void MutexBeforeLock(Thread *thr, uptr m, bool writelock) {
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000105 if (thr->ignore_interceptors)
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000106 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000107 Callback cb(thr);
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000108 {
109 MutexHashMap::Handle h(&ctx->mutex_map, m);
110 if (h.created())
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000111 ctx->dd->MutexInit(&cb, &h->dd);
112 ctx->dd->MutexBeforeLock(&cb, &h->dd, writelock);
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000113 }
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000114 ReportDeadlock(thr, ctx->dd->GetReport(&cb));
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000115}
116
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000117void MutexAfterLock(Thread *thr, uptr m, bool writelock, bool trylock) {
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000118 if (thr->ignore_interceptors)
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000119 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000120 Callback cb(thr);
121 {
122 MutexHashMap::Handle h(&ctx->mutex_map, m);
123 if (h.created())
124 ctx->dd->MutexInit(&cb, &h->dd);
125 ctx->dd->MutexAfterLock(&cb, &h->dd, writelock, trylock);
126 }
127 ReportDeadlock(thr, ctx->dd->GetReport(&cb));
128}
129
130void MutexBeforeUnlock(Thread *thr, uptr m, bool writelock) {
131 if (thr->ignore_interceptors)
132 return;
133 Callback cb(thr);
134 {
135 MutexHashMap::Handle h(&ctx->mutex_map, m);
136 ctx->dd->MutexBeforeUnlock(&cb, &h->dd, writelock);
137 }
138 ReportDeadlock(thr, ctx->dd->GetReport(&cb));
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000139}
140
141void MutexDestroy(Thread *thr, uptr m) {
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000142 if (thr->ignore_interceptors)
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000143 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000144 Callback cb(thr);
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000145 MutexHashMap::Handle h(&ctx->mutex_map, m, true);
146 if (!h.exists())
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000147 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000148 ctx->dd->MutexDestroy(&cb, &h->dd);
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000149}
150
151} // namespace __dsan