annotate libsanitizer/sanitizer_common/sanitizer_deadlock_detector1.cc @ 117:f81c5aa9f14f

fix
author mir3636
date Tue, 28 Nov 2017 21:17:15 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- sanitizer_deadlock_detector1.cc -----------------------------------===//
kono
parents:
diff changeset
2 //
kono
parents:
diff changeset
3 // This file is distributed under the University of Illinois Open Source
kono
parents:
diff changeset
4 // License. See LICENSE.TXT for details.
kono
parents:
diff changeset
5 //
kono
parents:
diff changeset
6 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
7 //
kono
parents:
diff changeset
8 // Deadlock detector implementation based on NxN adjacency bit matrix.
kono
parents:
diff changeset
9 //
kono
parents:
diff changeset
10 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 #include "sanitizer_deadlock_detector_interface.h"
kono
parents:
diff changeset
13 #include "sanitizer_deadlock_detector.h"
kono
parents:
diff changeset
14 #include "sanitizer_allocator_internal.h"
kono
parents:
diff changeset
15 #include "sanitizer_placement_new.h"
kono
parents:
diff changeset
16 #include "sanitizer_mutex.h"
kono
parents:
diff changeset
17
kono
parents:
diff changeset
18 #if SANITIZER_DEADLOCK_DETECTOR_VERSION == 1
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 namespace __sanitizer {
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 typedef TwoLevelBitVector<> DDBV; // DeadlockDetector's bit vector.
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 struct DDPhysicalThread {
kono
parents:
diff changeset
25 };
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 struct DDLogicalThread {
kono
parents:
diff changeset
28 u64 ctx;
kono
parents:
diff changeset
29 DeadlockDetectorTLS<DDBV> dd;
kono
parents:
diff changeset
30 DDReport rep;
kono
parents:
diff changeset
31 bool report_pending;
kono
parents:
diff changeset
32 };
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 struct DD : public DDetector {
kono
parents:
diff changeset
35 SpinMutex mtx;
kono
parents:
diff changeset
36 DeadlockDetector<DDBV> dd;
kono
parents:
diff changeset
37 DDFlags flags;
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 explicit DD(const DDFlags *flags);
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 DDPhysicalThread *CreatePhysicalThread() override;
kono
parents:
diff changeset
42 void DestroyPhysicalThread(DDPhysicalThread *pt) override;
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 DDLogicalThread *CreateLogicalThread(u64 ctx) override;
kono
parents:
diff changeset
45 void DestroyLogicalThread(DDLogicalThread *lt) override;
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 void MutexInit(DDCallback *cb, DDMutex *m) override;
kono
parents:
diff changeset
48 void MutexBeforeLock(DDCallback *cb, DDMutex *m, bool wlock) override;
kono
parents:
diff changeset
49 void MutexAfterLock(DDCallback *cb, DDMutex *m, bool wlock,
kono
parents:
diff changeset
50 bool trylock) override;
kono
parents:
diff changeset
51 void MutexBeforeUnlock(DDCallback *cb, DDMutex *m, bool wlock) override;
kono
parents:
diff changeset
52 void MutexDestroy(DDCallback *cb, DDMutex *m) override;
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 DDReport *GetReport(DDCallback *cb) override;
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 void MutexEnsureID(DDLogicalThread *lt, DDMutex *m);
kono
parents:
diff changeset
57 void ReportDeadlock(DDCallback *cb, DDMutex *m);
kono
parents:
diff changeset
58 };
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 DDetector *DDetector::Create(const DDFlags *flags) {
kono
parents:
diff changeset
61 (void)flags;
kono
parents:
diff changeset
62 void *mem = MmapOrDie(sizeof(DD), "deadlock detector");
kono
parents:
diff changeset
63 return new(mem) DD(flags);
kono
parents:
diff changeset
64 }
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 DD::DD(const DDFlags *flags)
kono
parents:
diff changeset
67 : flags(*flags) {
kono
parents:
diff changeset
68 dd.clear();
kono
parents:
diff changeset
69 }
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 DDPhysicalThread* DD::CreatePhysicalThread() {
kono
parents:
diff changeset
72 return nullptr;
kono
parents:
diff changeset
73 }
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 void DD::DestroyPhysicalThread(DDPhysicalThread *pt) {
kono
parents:
diff changeset
76 }
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 DDLogicalThread* DD::CreateLogicalThread(u64 ctx) {
kono
parents:
diff changeset
79 DDLogicalThread *lt = (DDLogicalThread*)InternalAlloc(sizeof(*lt));
kono
parents:
diff changeset
80 lt->ctx = ctx;
kono
parents:
diff changeset
81 lt->dd.clear();
kono
parents:
diff changeset
82 lt->report_pending = false;
kono
parents:
diff changeset
83 return lt;
kono
parents:
diff changeset
84 }
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 void DD::DestroyLogicalThread(DDLogicalThread *lt) {
kono
parents:
diff changeset
87 lt->~DDLogicalThread();
kono
parents:
diff changeset
88 InternalFree(lt);
kono
parents:
diff changeset
89 }
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 void DD::MutexInit(DDCallback *cb, DDMutex *m) {
kono
parents:
diff changeset
92 m->id = 0;
kono
parents:
diff changeset
93 m->stk = cb->Unwind();
kono
parents:
diff changeset
94 }
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 void DD::MutexEnsureID(DDLogicalThread *lt, DDMutex *m) {
kono
parents:
diff changeset
97 if (!dd.nodeBelongsToCurrentEpoch(m->id))
kono
parents:
diff changeset
98 m->id = dd.newNode(reinterpret_cast<uptr>(m));
kono
parents:
diff changeset
99 dd.ensureCurrentEpoch(&lt->dd);
kono
parents:
diff changeset
100 }
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 void DD::MutexBeforeLock(DDCallback *cb,
kono
parents:
diff changeset
103 DDMutex *m, bool wlock) {
kono
parents:
diff changeset
104 DDLogicalThread *lt = cb->lt;
kono
parents:
diff changeset
105 if (lt->dd.empty()) return; // This will be the first lock held by lt.
kono
parents:
diff changeset
106 if (dd.hasAllEdges(&lt->dd, m->id)) return; // We already have all edges.
kono
parents:
diff changeset
107 SpinMutexLock lk(&mtx);
kono
parents:
diff changeset
108 MutexEnsureID(lt, m);
kono
parents:
diff changeset
109 if (dd.isHeld(&lt->dd, m->id))
kono
parents:
diff changeset
110 return; // FIXME: allow this only for recursive locks.
kono
parents:
diff changeset
111 if (dd.onLockBefore(&lt->dd, m->id)) {
kono
parents:
diff changeset
112 // Actually add this edge now so that we have all the stack traces.
kono
parents:
diff changeset
113 dd.addEdges(&lt->dd, m->id, cb->Unwind(), cb->UniqueTid());
kono
parents:
diff changeset
114 ReportDeadlock(cb, m);
kono
parents:
diff changeset
115 }
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 void DD::ReportDeadlock(DDCallback *cb, DDMutex *m) {
kono
parents:
diff changeset
119 DDLogicalThread *lt = cb->lt;
kono
parents:
diff changeset
120 uptr path[20];
kono
parents:
diff changeset
121 uptr len = dd.findPathToLock(&lt->dd, m->id, path, ARRAY_SIZE(path));
kono
parents:
diff changeset
122 if (len == 0U) {
kono
parents:
diff changeset
123 // A cycle of 20+ locks? Well, that's a bit odd...
kono
parents:
diff changeset
124 Printf("WARNING: too long mutex cycle found\n");
kono
parents:
diff changeset
125 return;
kono
parents:
diff changeset
126 }
kono
parents:
diff changeset
127 CHECK_EQ(m->id, path[0]);
kono
parents:
diff changeset
128 lt->report_pending = true;
kono
parents:
diff changeset
129 len = Min<uptr>(len, DDReport::kMaxLoopSize);
kono
parents:
diff changeset
130 DDReport *rep = &lt->rep;
kono
parents:
diff changeset
131 rep->n = len;
kono
parents:
diff changeset
132 for (uptr i = 0; i < len; i++) {
kono
parents:
diff changeset
133 uptr from = path[i];
kono
parents:
diff changeset
134 uptr to = path[(i + 1) % len];
kono
parents:
diff changeset
135 DDMutex *m0 = (DDMutex*)dd.getData(from);
kono
parents:
diff changeset
136 DDMutex *m1 = (DDMutex*)dd.getData(to);
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 u32 stk_from = -1U, stk_to = -1U;
kono
parents:
diff changeset
139 int unique_tid = 0;
kono
parents:
diff changeset
140 dd.findEdge(from, to, &stk_from, &stk_to, &unique_tid);
kono
parents:
diff changeset
141 // Printf("Edge: %zd=>%zd: %u/%u T%d\n", from, to, stk_from, stk_to,
kono
parents:
diff changeset
142 // unique_tid);
kono
parents:
diff changeset
143 rep->loop[i].thr_ctx = unique_tid;
kono
parents:
diff changeset
144 rep->loop[i].mtx_ctx0 = m0->ctx;
kono
parents:
diff changeset
145 rep->loop[i].mtx_ctx1 = m1->ctx;
kono
parents:
diff changeset
146 rep->loop[i].stk[0] = stk_to;
kono
parents:
diff changeset
147 rep->loop[i].stk[1] = stk_from;
kono
parents:
diff changeset
148 }
kono
parents:
diff changeset
149 }
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 void DD::MutexAfterLock(DDCallback *cb, DDMutex *m, bool wlock, bool trylock) {
kono
parents:
diff changeset
152 DDLogicalThread *lt = cb->lt;
kono
parents:
diff changeset
153 u32 stk = 0;
kono
parents:
diff changeset
154 if (flags.second_deadlock_stack)
kono
parents:
diff changeset
155 stk = cb->Unwind();
kono
parents:
diff changeset
156 // Printf("T%p MutexLock: %zx stk %u\n", lt, m->id, stk);
kono
parents:
diff changeset
157 if (dd.onFirstLock(&lt->dd, m->id, stk))
kono
parents:
diff changeset
158 return;
kono
parents:
diff changeset
159 if (dd.onLockFast(&lt->dd, m->id, stk))
kono
parents:
diff changeset
160 return;
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 SpinMutexLock lk(&mtx);
kono
parents:
diff changeset
163 MutexEnsureID(lt, m);
kono
parents:
diff changeset
164 if (wlock) // Only a recursive rlock may be held.
kono
parents:
diff changeset
165 CHECK(!dd.isHeld(&lt->dd, m->id));
kono
parents:
diff changeset
166 if (!trylock)
kono
parents:
diff changeset
167 dd.addEdges(&lt->dd, m->id, stk ? stk : cb->Unwind(), cb->UniqueTid());
kono
parents:
diff changeset
168 dd.onLockAfter(&lt->dd, m->id, stk);
kono
parents:
diff changeset
169 }
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 void DD::MutexBeforeUnlock(DDCallback *cb, DDMutex *m, bool wlock) {
kono
parents:
diff changeset
172 // Printf("T%p MutexUnLock: %zx\n", cb->lt, m->id);
kono
parents:
diff changeset
173 dd.onUnlock(&cb->lt->dd, m->id);
kono
parents:
diff changeset
174 }
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 void DD::MutexDestroy(DDCallback *cb,
kono
parents:
diff changeset
177 DDMutex *m) {
kono
parents:
diff changeset
178 if (!m->id) return;
kono
parents:
diff changeset
179 SpinMutexLock lk(&mtx);
kono
parents:
diff changeset
180 if (dd.nodeBelongsToCurrentEpoch(m->id))
kono
parents:
diff changeset
181 dd.removeNode(m->id);
kono
parents:
diff changeset
182 m->id = 0;
kono
parents:
diff changeset
183 }
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 DDReport *DD::GetReport(DDCallback *cb) {
kono
parents:
diff changeset
186 if (!cb->lt->report_pending)
kono
parents:
diff changeset
187 return nullptr;
kono
parents:
diff changeset
188 cb->lt->report_pending = false;
kono
parents:
diff changeset
189 return &cb->lt->rep;
kono
parents:
diff changeset
190 }
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 } // namespace __sanitizer
kono
parents:
diff changeset
193 #endif // #if SANITIZER_DEADLOCK_DETECTOR_VERSION == 1