1 //===-- tsan_report.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 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "tsan_report.h"
14 #include "tsan_platform.h"
15 #include "tsan_rtl.h"
16 #include "sanitizer_common/sanitizer_report_decorator.h"
17
18 namespace __tsan {
19
20 class Decorator: public __sanitizer::SanitizerCommonDecorator {
21 public:
Decorator()22 Decorator() : SanitizerCommonDecorator() { }
Warning()23 const char *Warning() { return Red(); }
EndWarning()24 const char *EndWarning() { return Default(); }
Access()25 const char *Access() { return Blue(); }
EndAccess()26 const char *EndAccess() { return Default(); }
ThreadDescription()27 const char *ThreadDescription() { return Cyan(); }
EndThreadDescription()28 const char *EndThreadDescription() { return Default(); }
Location()29 const char *Location() { return Green(); }
EndLocation()30 const char *EndLocation() { return Default(); }
Sleep()31 const char *Sleep() { return Yellow(); }
EndSleep()32 const char *EndSleep() { return Default(); }
Mutex()33 const char *Mutex() { return Magenta(); }
EndMutex()34 const char *EndMutex() { return Default(); }
35 };
36
ReportDesc()37 ReportDesc::ReportDesc()
38 : stacks(MBlockReportStack)
39 , mops(MBlockReportMop)
40 , locs(MBlockReportLoc)
41 , mutexes(MBlockReportMutex)
42 , threads(MBlockReportThread)
43 , unique_tids(MBlockReportThread)
44 , sleep()
45 , count() {
46 }
47
ReportMop()48 ReportMop::ReportMop()
49 : mset(MBlockReportMutex) {
50 }
51
~ReportDesc()52 ReportDesc::~ReportDesc() {
53 // FIXME(dvyukov): it must be leaking a lot of memory.
54 }
55
56 #ifndef TSAN_GO
57
58 const int kThreadBufSize = 32;
thread_name(char * buf,int tid)59 const char *thread_name(char *buf, int tid) {
60 if (tid == 0)
61 return "main thread";
62 internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
63 return buf;
64 }
65
ReportTypeString(ReportType typ)66 static const char *ReportTypeString(ReportType typ) {
67 if (typ == ReportTypeRace)
68 return "data race";
69 if (typ == ReportTypeVptrRace)
70 return "data race on vptr (ctor/dtor vs virtual call)";
71 if (typ == ReportTypeUseAfterFree)
72 return "heap-use-after-free";
73 if (typ == ReportTypeThreadLeak)
74 return "thread leak";
75 if (typ == ReportTypeMutexDestroyLocked)
76 return "destroy of a locked mutex";
77 if (typ == ReportTypeMutexDoubleLock)
78 return "double lock of a mutex";
79 if (typ == ReportTypeMutexBadUnlock)
80 return "unlock of an unlocked mutex (or by a wrong thread)";
81 if (typ == ReportTypeMutexBadReadLock)
82 return "read lock of a write locked mutex";
83 if (typ == ReportTypeMutexBadReadUnlock)
84 return "read unlock of a write locked mutex";
85 if (typ == ReportTypeSignalUnsafe)
86 return "signal-unsafe call inside of a signal";
87 if (typ == ReportTypeErrnoInSignal)
88 return "signal handler spoils errno";
89 if (typ == ReportTypeDeadlock)
90 return "lock-order-inversion (potential deadlock)";
91 return "";
92 }
93
PrintStack(const ReportStack * ent)94 void PrintStack(const ReportStack *ent) {
95 if (ent == 0) {
96 Printf(" [failed to restore the stack]\n\n");
97 return;
98 }
99 for (int i = 0; ent; ent = ent->next, i++) {
100 Printf(" #%d %s %s:%d", i, ent->func, ent->file, ent->line);
101 if (ent->col)
102 Printf(":%d", ent->col);
103 if (ent->module && ent->offset)
104 Printf(" (%s+%p)\n", ent->module, (void*)ent->offset);
105 else
106 Printf(" (%p)\n", (void*)ent->pc);
107 }
108 Printf("\n");
109 }
110
PrintMutexSet(Vector<ReportMopMutex> const & mset)111 static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
112 for (uptr i = 0; i < mset.Size(); i++) {
113 if (i == 0)
114 Printf(" (mutexes:");
115 const ReportMopMutex m = mset[i];
116 Printf(" %s M%llu", m.write ? "write" : "read", m.id);
117 Printf(i == mset.Size() - 1 ? ")" : ",");
118 }
119 }
120
MopDesc(bool first,bool write,bool atomic)121 static const char *MopDesc(bool first, bool write, bool atomic) {
122 return atomic ? (first ? (write ? "Atomic write" : "Atomic read")
123 : (write ? "Previous atomic write" : "Previous atomic read"))
124 : (first ? (write ? "Write" : "Read")
125 : (write ? "Previous write" : "Previous read"));
126 }
127
PrintMop(const ReportMop * mop,bool first)128 static void PrintMop(const ReportMop *mop, bool first) {
129 Decorator d;
130 char thrbuf[kThreadBufSize];
131 Printf("%s", d.Access());
132 Printf(" %s of size %d at %p by %s",
133 MopDesc(first, mop->write, mop->atomic),
134 mop->size, (void*)mop->addr,
135 thread_name(thrbuf, mop->tid));
136 PrintMutexSet(mop->mset);
137 Printf(":\n");
138 Printf("%s", d.EndAccess());
139 PrintStack(mop->stack);
140 }
141
PrintLocation(const ReportLocation * loc)142 static void PrintLocation(const ReportLocation *loc) {
143 Decorator d;
144 char thrbuf[kThreadBufSize];
145 bool print_stack = false;
146 Printf("%s", d.Location());
147 if (loc->type == ReportLocationGlobal) {
148 Printf(" Location is global '%s' of size %zu at %p (%s+%p)\n\n",
149 loc->name, loc->size, loc->addr, loc->module, loc->offset);
150 } else if (loc->type == ReportLocationHeap) {
151 char thrbuf[kThreadBufSize];
152 Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
153 loc->size, loc->addr, thread_name(thrbuf, loc->tid));
154 print_stack = true;
155 } else if (loc->type == ReportLocationStack) {
156 Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
157 } else if (loc->type == ReportLocationTLS) {
158 Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
159 } else if (loc->type == ReportLocationFD) {
160 Printf(" Location is file descriptor %d created by %s at:\n",
161 loc->fd, thread_name(thrbuf, loc->tid));
162 print_stack = true;
163 }
164 Printf("%s", d.EndLocation());
165 if (print_stack)
166 PrintStack(loc->stack);
167 }
168
PrintMutexShort(const ReportMutex * rm,const char * after)169 static void PrintMutexShort(const ReportMutex *rm, const char *after) {
170 Decorator d;
171 Printf("%sM%zd%s%s", d.Mutex(), rm->id, d.EndMutex(), after);
172 }
173
PrintMutexShortWithAddress(const ReportMutex * rm,const char * after)174 static void PrintMutexShortWithAddress(const ReportMutex *rm,
175 const char *after) {
176 Decorator d;
177 Printf("%sM%zd (%p)%s%s", d.Mutex(), rm->id, rm->addr, d.EndMutex(), after);
178 }
179
PrintMutex(const ReportMutex * rm)180 static void PrintMutex(const ReportMutex *rm) {
181 Decorator d;
182 if (rm->destroyed) {
183 Printf("%s", d.Mutex());
184 Printf(" Mutex M%llu is already destroyed.\n\n", rm->id);
185 Printf("%s", d.EndMutex());
186 } else {
187 Printf("%s", d.Mutex());
188 Printf(" Mutex M%llu (%p) created at:\n", rm->id, rm->addr);
189 Printf("%s", d.EndMutex());
190 PrintStack(rm->stack);
191 }
192 }
193
PrintThread(const ReportThread * rt)194 static void PrintThread(const ReportThread *rt) {
195 Decorator d;
196 if (rt->id == 0) // Little sense in describing the main thread.
197 return;
198 Printf("%s", d.ThreadDescription());
199 Printf(" Thread T%d", rt->id);
200 if (rt->name && rt->name[0] != '\0')
201 Printf(" '%s'", rt->name);
202 char thrbuf[kThreadBufSize];
203 Printf(" (tid=%zu, %s) created by %s",
204 rt->pid, rt->running ? "running" : "finished",
205 thread_name(thrbuf, rt->parent_tid));
206 if (rt->stack)
207 Printf(" at:");
208 Printf("\n");
209 Printf("%s", d.EndThreadDescription());
210 PrintStack(rt->stack);
211 }
212
PrintSleep(const ReportStack * s)213 static void PrintSleep(const ReportStack *s) {
214 Decorator d;
215 Printf("%s", d.Sleep());
216 Printf(" As if synchronized via sleep:\n");
217 Printf("%s", d.EndSleep());
218 PrintStack(s);
219 }
220
ChooseSummaryStack(const ReportDesc * rep)221 static ReportStack *ChooseSummaryStack(const ReportDesc *rep) {
222 if (rep->mops.Size())
223 return rep->mops[0]->stack;
224 if (rep->stacks.Size())
225 return rep->stacks[0];
226 if (rep->mutexes.Size())
227 return rep->mutexes[0]->stack;
228 if (rep->threads.Size())
229 return rep->threads[0]->stack;
230 return 0;
231 }
232
SkipTsanInternalFrames(ReportStack * ent)233 ReportStack *SkipTsanInternalFrames(ReportStack *ent) {
234 while (FrameIsInternal(ent) && ent->next)
235 ent = ent->next;
236 return ent;
237 }
238
PrintReport(const ReportDesc * rep)239 void PrintReport(const ReportDesc *rep) {
240 Decorator d;
241 Printf("==================\n");
242 const char *rep_typ_str = ReportTypeString(rep->typ);
243 Printf("%s", d.Warning());
244 Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str,
245 (int)internal_getpid());
246 Printf("%s", d.EndWarning());
247
248 if (rep->typ == ReportTypeDeadlock) {
249 char thrbuf[kThreadBufSize];
250 Printf(" Cycle in lock order graph: ");
251 for (uptr i = 0; i < rep->mutexes.Size(); i++)
252 PrintMutexShortWithAddress(rep->mutexes[i], " => ");
253 PrintMutexShort(rep->mutexes[0], "\n\n");
254 CHECK_GT(rep->mutexes.Size(), 0U);
255 CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1),
256 rep->stacks.Size());
257 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
258 Printf(" Mutex ");
259 PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()],
260 " acquired here while holding mutex ");
261 PrintMutexShort(rep->mutexes[i], " in ");
262 Printf("%s", d.ThreadDescription());
263 Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i]));
264 Printf("%s", d.EndThreadDescription());
265 if (flags()->second_deadlock_stack) {
266 PrintStack(rep->stacks[2*i]);
267 Printf(" Mutex ");
268 PrintMutexShort(rep->mutexes[i],
269 " previously acquired by the same thread here:\n");
270 PrintStack(rep->stacks[2*i+1]);
271 } else {
272 PrintStack(rep->stacks[i]);
273 if (i == 0)
274 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
275 "to get more informative warning message\n\n");
276 }
277 }
278 } else {
279 for (uptr i = 0; i < rep->stacks.Size(); i++) {
280 if (i)
281 Printf(" and:\n");
282 PrintStack(rep->stacks[i]);
283 }
284 }
285
286 for (uptr i = 0; i < rep->mops.Size(); i++)
287 PrintMop(rep->mops[i], i == 0);
288
289 if (rep->sleep)
290 PrintSleep(rep->sleep);
291
292 for (uptr i = 0; i < rep->locs.Size(); i++)
293 PrintLocation(rep->locs[i]);
294
295 if (rep->typ != ReportTypeDeadlock) {
296 for (uptr i = 0; i < rep->mutexes.Size(); i++)
297 PrintMutex(rep->mutexes[i]);
298 }
299
300 for (uptr i = 0; i < rep->threads.Size(); i++)
301 PrintThread(rep->threads[i]);
302
303 if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
304 Printf(" And %d more similar thread leaks.\n\n", rep->count - 1);
305
306 if (ReportStack *ent = SkipTsanInternalFrames(ChooseSummaryStack(rep)))
307 ReportErrorSummary(rep_typ_str, ent->file, ent->line, ent->func);
308
309 Printf("==================\n");
310 }
311
312 #else // #ifndef TSAN_GO
313
314 const int kMainThreadId = 1;
315
PrintStack(const ReportStack * ent)316 void PrintStack(const ReportStack *ent) {
317 if (ent == 0) {
318 Printf(" [failed to restore the stack]\n");
319 return;
320 }
321 for (int i = 0; ent; ent = ent->next, i++) {
322 Printf(" %s()\n %s:%d +0x%zx\n",
323 ent->func, ent->file, ent->line, (void*)ent->offset);
324 }
325 }
326
PrintMop(const ReportMop * mop,bool first)327 static void PrintMop(const ReportMop *mop, bool first) {
328 Printf("\n");
329 Printf("%s by ",
330 (first ? (mop->write ? "Write" : "Read")
331 : (mop->write ? "Previous write" : "Previous read")));
332 if (mop->tid == kMainThreadId)
333 Printf("main goroutine:\n");
334 else
335 Printf("goroutine %d:\n", mop->tid);
336 PrintStack(mop->stack);
337 }
338
PrintThread(const ReportThread * rt)339 static void PrintThread(const ReportThread *rt) {
340 if (rt->id == kMainThreadId)
341 return;
342 Printf("\n");
343 Printf("Goroutine %d (%s) created at:\n",
344 rt->id, rt->running ? "running" : "finished");
345 PrintStack(rt->stack);
346 }
347
PrintReport(const ReportDesc * rep)348 void PrintReport(const ReportDesc *rep) {
349 Printf("==================\n");
350 if (rep->typ == ReportTypeRace) {
351 Printf("WARNING: DATA RACE");
352 for (uptr i = 0; i < rep->mops.Size(); i++)
353 PrintMop(rep->mops[i], i == 0);
354 for (uptr i = 0; i < rep->threads.Size(); i++)
355 PrintThread(rep->threads[i]);
356 } else if (rep->typ == ReportTypeDeadlock) {
357 Printf("WARNING: DEADLOCK\n");
358 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
359 Printf("Goroutine %d lock mutex %d while holding mutex %d:\n",
360 999, rep->mutexes[i]->id,
361 rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
362 PrintStack(rep->stacks[2*i]);
363 Printf("\n");
364 Printf("Mutex %d was previously locked here:\n",
365 rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
366 PrintStack(rep->stacks[2*i + 1]);
367 Printf("\n");
368 }
369 }
370 Printf("==================\n");
371 }
372
373 #endif
374
375 } // namespace __tsan
376