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