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