xref: /freebsd/contrib/llvm-project/compiler-rt/lib/tsan/rtl/tsan_report.cpp (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
1 //===-- tsan_report.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "tsan_report.h"
13 #include "tsan_platform.h"
14 #include "tsan_rtl.h"
15 #include "sanitizer_common/sanitizer_file.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 
22 ReportStack::ReportStack() : frames(nullptr), suppressable(false) {}
23 
24 ReportStack *ReportStack::New() {
25   void *mem = internal_alloc(MBlockReportStack, sizeof(ReportStack));
26   return new(mem) ReportStack();
27 }
28 
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 
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:
40   Decorator() : SanitizerCommonDecorator() { }
41   const char *Access()     { return Blue(); }
42   const char *ThreadDescription()    { return Cyan(); }
43   const char *Location()   { return Green(); }
44   const char *Sleep()   { return Yellow(); }
45   const char *Mutex()   { return Magenta(); }
46 };
47 
48 ReportDesc::ReportDesc()
49     : tag(kExternalTagNone)
50     , stacks()
51     , mops()
52     , locs()
53     , mutexes()
54     , threads()
55     , unique_tids()
56     , sleep()
57     , count() {
58 }
59 
60 ReportMop::ReportMop()
61     : mset() {
62 }
63 
64 ReportDesc::~ReportDesc() {
65   // FIXME(dvyukov): it must be leaking a lot of memory.
66 }
67 
68 #if !SANITIZER_GO
69 
70 const int kThreadBufSize = 32;
71 const char *thread_name(char *buf, int tid) {
72   if (tid == 0)
73     return "main thread";
74   internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
75   return buf;
76 }
77 
78 static const char *ReportTypeString(ReportType typ, uptr tag) {
79   switch (typ) {
80     case ReportTypeRace:
81       return "data race";
82     case ReportTypeVptrRace:
83       return "data race on vptr (ctor/dtor vs virtual call)";
84     case ReportTypeUseAfterFree:
85       return "heap-use-after-free";
86     case ReportTypeVptrUseAfterFree:
87       return "heap-use-after-free (virtual call vs free)";
88     case ReportTypeExternalRace: {
89       const char *str = GetReportHeaderFromTag(tag);
90       return str ? str : "race on external object";
91     }
92     case ReportTypeThreadLeak:
93       return "thread leak";
94     case ReportTypeMutexDestroyLocked:
95       return "destroy of a locked mutex";
96     case ReportTypeMutexDoubleLock:
97       return "double lock of a mutex";
98     case ReportTypeMutexInvalidAccess:
99       return "use of an invalid mutex (e.g. uninitialized or destroyed)";
100     case ReportTypeMutexBadUnlock:
101       return "unlock of an unlocked mutex (or by a wrong thread)";
102     case ReportTypeMutexBadReadLock:
103       return "read lock of a write locked mutex";
104     case ReportTypeMutexBadReadUnlock:
105       return "read unlock of a write locked mutex";
106     case ReportTypeSignalUnsafe:
107       return "signal-unsafe call inside of a signal";
108     case ReportTypeErrnoInSignal:
109       return "signal handler spoils errno";
110     case ReportTypeDeadlock:
111       return "lock-order-inversion (potential deadlock)";
112     // No default case so compiler warns us if we miss one
113   }
114   UNREACHABLE("missing case");
115 }
116 
117 #if SANITIZER_MAC
118 static const char *const kInterposedFunctionPrefix = "wrap_";
119 #else
120 static const char *const kInterposedFunctionPrefix = "__interceptor_";
121 #endif
122 
123 void PrintStack(const ReportStack *ent) {
124   if (ent == 0 || ent->frames == 0) {
125     Printf("    [failed to restore the stack]\n\n");
126     return;
127   }
128   SymbolizedStack *frame = ent->frames;
129   for (int i = 0; frame && frame->info.address; frame = frame->next, i++) {
130     InternalScopedString res(2 * GetPageSizeCached());
131     RenderFrame(&res, common_flags()->stack_trace_format, i,
132                 frame->info.address, &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->thread_type == ThreadType::Worker) {
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_posix.cpp") ||
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)
390     DumpProcessMap();
391 
392   Printf("==================\n");
393 }
394 
395 #else  // #if !SANITIZER_GO
396 
397 const int kMainThreadId = 1;
398 
399 void PrintStack(const ReportStack *ent) {
400   if (ent == 0 || ent->frames == 0) {
401     Printf("  [failed to restore the stack]\n");
402     return;
403   }
404   SymbolizedStack *frame = ent->frames;
405   for (int i = 0; frame; frame = frame->next, i++) {
406     const AddressInfo &info = frame->info;
407     Printf("  %s()\n      %s:%d +0x%zx\n", info.function,
408         StripPathPrefix(info.file, common_flags()->strip_path_prefix),
409         info.line, (void *)info.module_offset);
410   }
411 }
412 
413 static void PrintMop(const ReportMop *mop, bool first) {
414   Printf("\n");
415   Printf("%s at %p by ",
416       (first ? (mop->write ? "Write" : "Read")
417              : (mop->write ? "Previous write" : "Previous read")), mop->addr);
418   if (mop->tid == kMainThreadId)
419     Printf("main goroutine:\n");
420   else
421     Printf("goroutine %d:\n", mop->tid);
422   PrintStack(mop->stack);
423 }
424 
425 static void PrintLocation(const ReportLocation *loc) {
426   switch (loc->type) {
427   case ReportLocationHeap: {
428     Printf("\n");
429     Printf("Heap block of size %zu at %p allocated by ",
430         loc->heap_chunk_size, loc->heap_chunk_start);
431     if (loc->tid == kMainThreadId)
432       Printf("main goroutine:\n");
433     else
434       Printf("goroutine %d:\n", loc->tid);
435     PrintStack(loc->stack);
436     break;
437   }
438   case ReportLocationGlobal: {
439     Printf("\n");
440     Printf("Global var %s of size %zu at %p declared at %s:%zu\n",
441         loc->global.name, loc->global.size, loc->global.start,
442         loc->global.file, loc->global.line);
443     break;
444   }
445   default:
446     break;
447   }
448 }
449 
450 static void PrintThread(const ReportThread *rt) {
451   if (rt->id == kMainThreadId)
452     return;
453   Printf("\n");
454   Printf("Goroutine %d (%s) created at:\n",
455     rt->id, rt->running ? "running" : "finished");
456   PrintStack(rt->stack);
457 }
458 
459 void PrintReport(const ReportDesc *rep) {
460   Printf("==================\n");
461   if (rep->typ == ReportTypeRace) {
462     Printf("WARNING: DATA RACE");
463     for (uptr i = 0; i < rep->mops.Size(); i++)
464       PrintMop(rep->mops[i], i == 0);
465     for (uptr i = 0; i < rep->locs.Size(); i++)
466       PrintLocation(rep->locs[i]);
467     for (uptr i = 0; i < rep->threads.Size(); i++)
468       PrintThread(rep->threads[i]);
469   } else if (rep->typ == ReportTypeDeadlock) {
470     Printf("WARNING: DEADLOCK\n");
471     for (uptr i = 0; i < rep->mutexes.Size(); i++) {
472       Printf("Goroutine %d lock mutex %d while holding mutex %d:\n",
473           999, rep->mutexes[i]->id,
474           rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
475       PrintStack(rep->stacks[2*i]);
476       Printf("\n");
477       Printf("Mutex %d was previously locked here:\n",
478           rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
479       PrintStack(rep->stacks[2*i + 1]);
480       Printf("\n");
481     }
482   }
483   Printf("==================\n");
484 }
485 
486 #endif
487 
488 }  // namespace __tsan
489