1 //===-- tsan_report.cc ----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
12 //===----------------------------------------------------------------------===//
13 #include "tsan_report.h"
14 #include "tsan_platform.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"
23 ReportStack::ReportStack() : frames(nullptr), suppressable(false) {}
25 ReportStack *ReportStack::New() {
26 void *mem = internal_alloc(MBlockReportStack, sizeof(ReportStack));
27 return new(mem) ReportStack();
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) {}
34 ReportLocation *ReportLocation::New(ReportLocationType type) {
35 void *mem = internal_alloc(MBlockReportStack, sizeof(ReportLocation));
36 return new(mem) ReportLocation(type);
39 class Decorator: public __sanitizer::SanitizerCommonDecorator {
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(); }
49 ReportDesc::ReportDesc()
50 : tag(kExternalTagNone)
61 ReportMop::ReportMop()
65 ReportDesc::~ReportDesc() {
66 // FIXME(dvyukov): it must be leaking a lot of memory.
71 const int kThreadBufSize = 32;
72 const char *thread_name(char *buf, int tid) {
75 internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
79 static const char *ReportTypeString(ReportType typ, uptr tag) {
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";
93 case ReportTypeThreadLeak:
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
115 UNREACHABLE("missing case");
119 static const char *const kInterposedFunctionPrefix = "wrap_";
121 static const char *const kInterposedFunctionPrefix = "__interceptor_";
124 void PrintStack(const ReportStack *ent) {
125 if (ent == 0 || ent->frames == 0) {
126 Printf(" [failed to restore the stack]\n\n");
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());
140 static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
141 for (uptr i = 0; i < mset.Size(); i++) {
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 ? ")" : ",");
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"));
157 static const char *ExternalMopDesc(bool first, bool write) {
158 return first ? (write ? "Modifying" : "Read-only")
159 : (write ? "Previous modifying" : "Previous read-only");
162 static void PrintMop(const ReportMop *mop, bool first) {
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));
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));
178 PrintMutexSet(mop->mset);
180 Printf("%s", d.Default());
181 PrintStack(mop->stack);
184 static void PrintLocation(const ReportLocation *loc) {
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);
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);
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));
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));
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));
221 Printf("%s", d.Default());
223 PrintStack(loc->stack);
226 static void PrintMutexShort(const ReportMutex *rm, const char *after) {
228 Printf("%sM%zd%s%s", d.Mutex(), rm->id, d.Default(), after);
231 static void PrintMutexShortWithAddress(const ReportMutex *rm,
234 Printf("%sM%zd (%p)%s%s", d.Mutex(), rm->id, rm->addr, d.Default(), after);
237 static void PrintMutex(const ReportMutex *rm) {
240 Printf("%s", d.Mutex());
241 Printf(" Mutex M%llu is already destroyed.\n\n", rm->id);
242 Printf("%s", d.Default());
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);
251 static void PrintThread(const ReportThread *rt) {
253 if (rt->id == 0) // Little sense in describing the main thread.
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);
264 Printf("%s", d.Default());
267 Printf(" (tid=%zu, %s) created by %s", rt->os_id, thread_status,
268 thread_name(thrbuf, rt->parent_tid));
272 Printf("%s", d.Default());
273 PrintStack(rt->stack);
276 static void PrintSleep(const ReportStack *s) {
278 Printf("%s", d.Sleep());
279 Printf(" As if synchronized via sleep:\n");
280 Printf("%s", d.Default());
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;
296 static bool FrameIsInternal(const SymbolizedStack *frame) {
299 const char *file = frame->info.file;
300 const char *module = frame->info.module;
302 (internal_strstr(file, "tsan_interceptors.cc") ||
303 internal_strstr(file, "sanitizer_common_interceptors.inc") ||
304 internal_strstr(file, "tsan_interface_")))
306 if (module != 0 && (internal_strstr(module, "libclang_rt.tsan_")))
311 static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) {
312 while (FrameIsInternal(frames) && frames->next)
313 frames = frames->next;
317 void PrintReport(const ReportDesc *rep) {
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());
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),
335 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
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]);
346 PrintMutexShort(rep->mutexes[i],
347 " previously acquired by the same thread here:\n");
348 PrintStack(rep->stacks[2*i+1]);
350 PrintStack(rep->stacks[i]);
352 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
353 "to get more informative warning message\n\n");
357 for (uptr i = 0; i < rep->stacks.Size(); i++) {
360 PrintStack(rep->stacks[i]);
364 for (uptr i = 0; i < rep->mops.Size(); i++)
365 PrintMop(rep->mops[i], i == 0);
368 PrintSleep(rep->sleep);
370 for (uptr i = 0; i < rep->locs.Size(); i++)
371 PrintLocation(rep->locs[i]);
373 if (rep->typ != ReportTypeDeadlock) {
374 for (uptr i = 0; i < rep->mutexes.Size(); i++)
375 PrintMutex(rep->mutexes[i]);
378 for (uptr i = 0; i < rep->threads.Size(); i++)
379 PrintThread(rep->threads[i]);
381 if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
382 Printf(" And %d more similar thread leaks.\n\n", rep->count - 1);
384 if (ReportStack *stack = ChooseSummaryStack(rep)) {
385 if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames))
386 ReportErrorSummary(rep_typ_str, frame->info);
389 if (common_flags()->print_module_map == 2) PrintModuleMap();
391 Printf("==================\n");
394 #else // #if !SANITIZER_GO
396 const int kMainThreadId = 1;
398 void PrintStack(const ReportStack *ent) {
399 if (ent == 0 || ent->frames == 0) {
400 Printf(" [failed to restore the stack]\n");
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);
412 static void PrintMop(const ReportMop *mop, bool first) {
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");
420 Printf("goroutine %d:\n", mop->tid);
421 PrintStack(mop->stack);
424 static void PrintLocation(const ReportLocation *loc) {
426 case ReportLocationHeap: {
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");
433 Printf("goroutine %d:\n", loc->tid);
434 PrintStack(loc->stack);
437 case ReportLocationGlobal: {
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);
449 static void PrintThread(const ReportThread *rt) {
450 if (rt->id == kMainThreadId)
453 Printf("Goroutine %d (%s) created at:\n",
454 rt->id, rt->running ? "running" : "finished");
455 PrintStack(rt->stack);
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]);
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]);
482 Printf("==================\n");
487 } // namespace __tsan