1 //===-- tsan_report.cc ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //===----------------------------------------------------------------------===//
12 #include "tsan_report.h"
13 #include "tsan_platform.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"
22 ReportStack::ReportStack() : frames(nullptr), suppressable(false) {}
24 ReportStack *ReportStack::New() {
25 void *mem = internal_alloc(MBlockReportStack, sizeof(ReportStack));
26 return new(mem) ReportStack();
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) {}
33 ReportLocation *ReportLocation::New(ReportLocationType type) {
34 void *mem = internal_alloc(MBlockReportStack, sizeof(ReportLocation));
35 return new(mem) ReportLocation(type);
38 class Decorator: public __sanitizer::SanitizerCommonDecorator {
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(); }
48 ReportDesc::ReportDesc()
49 : tag(kExternalTagNone)
60 ReportMop::ReportMop()
64 ReportDesc::~ReportDesc() {
65 // FIXME(dvyukov): it must be leaking a lot of memory.
70 const int kThreadBufSize = 32;
71 const char *thread_name(char *buf, int tid) {
74 internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
78 static const char *ReportTypeString(ReportType typ, uptr tag) {
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";
92 case ReportTypeThreadLeak:
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
114 UNREACHABLE("missing case");
118 static const char *const kInterposedFunctionPrefix = "wrap_";
120 static const char *const kInterposedFunctionPrefix = "__interceptor_";
123 void PrintStack(const ReportStack *ent) {
124 if (ent == 0 || ent->frames == 0) {
125 Printf(" [failed to restore the stack]\n\n");
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, frame->info,
132 common_flags()->symbolize_vs_style,
133 common_flags()->strip_path_prefix, kInterposedFunctionPrefix);
134 Printf("%s\n", res.data());
139 static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
140 for (uptr i = 0; i < mset.Size(); i++) {
142 Printf(" (mutexes:");
143 const ReportMopMutex m = mset[i];
144 Printf(" %s M%llu", m.write ? "write" : "read", m.id);
145 Printf(i == mset.Size() - 1 ? ")" : ",");
149 static const char *MopDesc(bool first, bool write, bool atomic) {
150 return atomic ? (first ? (write ? "Atomic write" : "Atomic read")
151 : (write ? "Previous atomic write" : "Previous atomic read"))
152 : (first ? (write ? "Write" : "Read")
153 : (write ? "Previous write" : "Previous read"));
156 static const char *ExternalMopDesc(bool first, bool write) {
157 return first ? (write ? "Modifying" : "Read-only")
158 : (write ? "Previous modifying" : "Previous read-only");
161 static void PrintMop(const ReportMop *mop, bool first) {
163 char thrbuf[kThreadBufSize];
164 Printf("%s", d.Access());
165 if (mop->external_tag == kExternalTagNone) {
166 Printf(" %s of size %d at %p by %s",
167 MopDesc(first, mop->write, mop->atomic), mop->size,
168 (void *)mop->addr, thread_name(thrbuf, mop->tid));
170 const char *object_type = GetObjectTypeFromTag(mop->external_tag);
171 if (object_type == nullptr)
172 object_type = "external object";
173 Printf(" %s access of %s at %p by %s",
174 ExternalMopDesc(first, mop->write), object_type,
175 (void *)mop->addr, thread_name(thrbuf, mop->tid));
177 PrintMutexSet(mop->mset);
179 Printf("%s", d.Default());
180 PrintStack(mop->stack);
183 static void PrintLocation(const ReportLocation *loc) {
185 char thrbuf[kThreadBufSize];
186 bool print_stack = false;
187 Printf("%s", d.Location());
188 if (loc->type == ReportLocationGlobal) {
189 const DataInfo &global = loc->global;
190 if (global.size != 0)
191 Printf(" Location is global '%s' of size %zu at %p (%s+%p)\n\n",
192 global.name, global.size, global.start,
193 StripModuleName(global.module), global.module_offset);
195 Printf(" Location is global '%s' at %p (%s+%p)\n\n", global.name,
196 global.start, StripModuleName(global.module),
197 global.module_offset);
198 } else if (loc->type == ReportLocationHeap) {
199 char thrbuf[kThreadBufSize];
200 const char *object_type = GetObjectTypeFromTag(loc->external_tag);
202 Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
203 loc->heap_chunk_size, loc->heap_chunk_start,
204 thread_name(thrbuf, loc->tid));
206 Printf(" Location is %s of size %zu at %p allocated by %s:\n",
207 object_type, loc->heap_chunk_size, loc->heap_chunk_start,
208 thread_name(thrbuf, loc->tid));
211 } else if (loc->type == ReportLocationStack) {
212 Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
213 } else if (loc->type == ReportLocationTLS) {
214 Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
215 } else if (loc->type == ReportLocationFD) {
216 Printf(" Location is file descriptor %d created by %s at:\n",
217 loc->fd, thread_name(thrbuf, loc->tid));
220 Printf("%s", d.Default());
222 PrintStack(loc->stack);
225 static void PrintMutexShort(const ReportMutex *rm, const char *after) {
227 Printf("%sM%zd%s%s", d.Mutex(), rm->id, d.Default(), after);
230 static void PrintMutexShortWithAddress(const ReportMutex *rm,
233 Printf("%sM%zd (%p)%s%s", d.Mutex(), rm->id, rm->addr, d.Default(), after);
236 static void PrintMutex(const ReportMutex *rm) {
239 Printf("%s", d.Mutex());
240 Printf(" Mutex M%llu is already destroyed.\n\n", rm->id);
241 Printf("%s", d.Default());
243 Printf("%s", d.Mutex());
244 Printf(" Mutex M%llu (%p) created at:\n", rm->id, rm->addr);
245 Printf("%s", d.Default());
246 PrintStack(rm->stack);
250 static void PrintThread(const ReportThread *rt) {
252 if (rt->id == 0) // Little sense in describing the main thread.
254 Printf("%s", d.ThreadDescription());
255 Printf(" Thread T%d", rt->id);
256 if (rt->name && rt->name[0] != '\0')
257 Printf(" '%s'", rt->name);
258 char thrbuf[kThreadBufSize];
259 const char *thread_status = rt->running ? "running" : "finished";
260 if (rt->thread_type == ThreadType::Worker) {
261 Printf(" (tid=%zu, %s) is a GCD worker thread\n", rt->os_id, thread_status);
263 Printf("%s", d.Default());
266 Printf(" (tid=%zu, %s) created by %s", rt->os_id, thread_status,
267 thread_name(thrbuf, rt->parent_tid));
271 Printf("%s", d.Default());
272 PrintStack(rt->stack);
275 static void PrintSleep(const ReportStack *s) {
277 Printf("%s", d.Sleep());
278 Printf(" As if synchronized via sleep:\n");
279 Printf("%s", d.Default());
283 static ReportStack *ChooseSummaryStack(const ReportDesc *rep) {
284 if (rep->mops.Size())
285 return rep->mops[0]->stack;
286 if (rep->stacks.Size())
287 return rep->stacks[0];
288 if (rep->mutexes.Size())
289 return rep->mutexes[0]->stack;
290 if (rep->threads.Size())
291 return rep->threads[0]->stack;
295 static bool FrameIsInternal(const SymbolizedStack *frame) {
298 const char *file = frame->info.file;
299 const char *module = frame->info.module;
301 (internal_strstr(file, "tsan_interceptors.cc") ||
302 internal_strstr(file, "sanitizer_common_interceptors.inc") ||
303 internal_strstr(file, "tsan_interface_")))
305 if (module != 0 && (internal_strstr(module, "libclang_rt.tsan_")))
310 static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) {
311 while (FrameIsInternal(frames) && frames->next)
312 frames = frames->next;
316 void PrintReport(const ReportDesc *rep) {
318 Printf("==================\n");
319 const char *rep_typ_str = ReportTypeString(rep->typ, rep->tag);
320 Printf("%s", d.Warning());
321 Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str,
322 (int)internal_getpid());
323 Printf("%s", d.Default());
325 if (rep->typ == ReportTypeDeadlock) {
326 char thrbuf[kThreadBufSize];
327 Printf(" Cycle in lock order graph: ");
328 for (uptr i = 0; i < rep->mutexes.Size(); i++)
329 PrintMutexShortWithAddress(rep->mutexes[i], " => ");
330 PrintMutexShort(rep->mutexes[0], "\n\n");
331 CHECK_GT(rep->mutexes.Size(), 0U);
332 CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1),
334 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
336 PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()],
337 " acquired here while holding mutex ");
338 PrintMutexShort(rep->mutexes[i], " in ");
339 Printf("%s", d.ThreadDescription());
340 Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i]));
341 Printf("%s", d.Default());
342 if (flags()->second_deadlock_stack) {
343 PrintStack(rep->stacks[2*i]);
345 PrintMutexShort(rep->mutexes[i],
346 " previously acquired by the same thread here:\n");
347 PrintStack(rep->stacks[2*i+1]);
349 PrintStack(rep->stacks[i]);
351 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
352 "to get more informative warning message\n\n");
356 for (uptr i = 0; i < rep->stacks.Size(); i++) {
359 PrintStack(rep->stacks[i]);
363 for (uptr i = 0; i < rep->mops.Size(); i++)
364 PrintMop(rep->mops[i], i == 0);
367 PrintSleep(rep->sleep);
369 for (uptr i = 0; i < rep->locs.Size(); i++)
370 PrintLocation(rep->locs[i]);
372 if (rep->typ != ReportTypeDeadlock) {
373 for (uptr i = 0; i < rep->mutexes.Size(); i++)
374 PrintMutex(rep->mutexes[i]);
377 for (uptr i = 0; i < rep->threads.Size(); i++)
378 PrintThread(rep->threads[i]);
380 if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
381 Printf(" And %d more similar thread leaks.\n\n", rep->count - 1);
383 if (ReportStack *stack = ChooseSummaryStack(rep)) {
384 if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames))
385 ReportErrorSummary(rep_typ_str, frame->info);
388 if (common_flags()->print_module_map == 2) PrintModuleMap();
390 Printf("==================\n");
393 #else // #if !SANITIZER_GO
395 const int kMainThreadId = 1;
397 void PrintStack(const ReportStack *ent) {
398 if (ent == 0 || ent->frames == 0) {
399 Printf(" [failed to restore the stack]\n");
402 SymbolizedStack *frame = ent->frames;
403 for (int i = 0; frame; frame = frame->next, i++) {
404 const AddressInfo &info = frame->info;
405 Printf(" %s()\n %s:%d +0x%zx\n", info.function,
406 StripPathPrefix(info.file, common_flags()->strip_path_prefix),
407 info.line, (void *)info.module_offset);
411 static void PrintMop(const ReportMop *mop, bool first) {
413 Printf("%s at %p by ",
414 (first ? (mop->write ? "Write" : "Read")
415 : (mop->write ? "Previous write" : "Previous read")), mop->addr);
416 if (mop->tid == kMainThreadId)
417 Printf("main goroutine:\n");
419 Printf("goroutine %d:\n", mop->tid);
420 PrintStack(mop->stack);
423 static void PrintLocation(const ReportLocation *loc) {
425 case ReportLocationHeap: {
427 Printf("Heap block of size %zu at %p allocated by ",
428 loc->heap_chunk_size, loc->heap_chunk_start);
429 if (loc->tid == kMainThreadId)
430 Printf("main goroutine:\n");
432 Printf("goroutine %d:\n", loc->tid);
433 PrintStack(loc->stack);
436 case ReportLocationGlobal: {
438 Printf("Global var %s of size %zu at %p declared at %s:%zu\n",
439 loc->global.name, loc->global.size, loc->global.start,
440 loc->global.file, loc->global.line);
448 static void PrintThread(const ReportThread *rt) {
449 if (rt->id == kMainThreadId)
452 Printf("Goroutine %d (%s) created at:\n",
453 rt->id, rt->running ? "running" : "finished");
454 PrintStack(rt->stack);
457 void PrintReport(const ReportDesc *rep) {
458 Printf("==================\n");
459 if (rep->typ == ReportTypeRace) {
460 Printf("WARNING: DATA RACE");
461 for (uptr i = 0; i < rep->mops.Size(); i++)
462 PrintMop(rep->mops[i], i == 0);
463 for (uptr i = 0; i < rep->locs.Size(); i++)
464 PrintLocation(rep->locs[i]);
465 for (uptr i = 0; i < rep->threads.Size(); i++)
466 PrintThread(rep->threads[i]);
467 } else if (rep->typ == ReportTypeDeadlock) {
468 Printf("WARNING: DEADLOCK\n");
469 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
470 Printf("Goroutine %d lock mutex %d while holding mutex %d:\n",
471 999, rep->mutexes[i]->id,
472 rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
473 PrintStack(rep->stacks[2*i]);
475 Printf("Mutex %d was previously locked here:\n",
476 rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
477 PrintStack(rep->stacks[2*i + 1]);
481 Printf("==================\n");
486 } // namespace __tsan