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_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 *Warning() { return Red(); }
42 const char *EndWarning() { return Default(); }
43 const char *Access() { return Blue(); }
44 const char *EndAccess() { return Default(); }
45 const char *ThreadDescription() { return Cyan(); }
46 const char *EndThreadDescription() { return Default(); }
47 const char *Location() { return Green(); }
48 const char *EndLocation() { return Default(); }
49 const char *Sleep() { return Yellow(); }
50 const char *EndSleep() { return Default(); }
51 const char *Mutex() { return Magenta(); }
52 const char *EndMutex() { return Default(); }
55 ReportDesc::ReportDesc()
56 : stacks(MBlockReportStack)
57 , mops(MBlockReportMop)
58 , locs(MBlockReportLoc)
59 , mutexes(MBlockReportMutex)
60 , threads(MBlockReportThread)
61 , unique_tids(MBlockReportThread)
66 ReportMop::ReportMop()
67 : mset(MBlockReportMutex) {
70 ReportDesc::~ReportDesc() {
71 // FIXME(dvyukov): it must be leaking a lot of memory.
76 const int kThreadBufSize = 32;
77 const char *thread_name(char *buf, int tid) {
80 internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
84 static const char *ReportTypeString(ReportType typ) {
85 if (typ == ReportTypeRace)
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)
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)";
114 void PrintStack(const ReportStack *ent) {
115 if (ent == 0 || ent->frames == 0) {
116 Printf(" [failed to restore the stack]\n\n");
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());
129 static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
130 for (uptr i = 0; i < mset.Size(); i++) {
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 ? ")" : ",");
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"));
146 static void PrintMop(const ReportMop *mop, bool first) {
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);
156 Printf("%s", d.EndAccess());
157 PrintStack(mop->stack);
160 static void PrintLocation(const ReportLocation *loc) {
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));
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));
185 Printf("%s", d.EndLocation());
187 PrintStack(loc->stack);
190 static void PrintMutexShort(const ReportMutex *rm, const char *after) {
192 Printf("%sM%zd%s%s", d.Mutex(), rm->id, d.EndMutex(), after);
195 static void PrintMutexShortWithAddress(const ReportMutex *rm,
198 Printf("%sM%zd (%p)%s%s", d.Mutex(), rm->id, rm->addr, d.EndMutex(), after);
201 static void PrintMutex(const ReportMutex *rm) {
204 Printf("%s", d.Mutex());
205 Printf(" Mutex M%llu is already destroyed.\n\n", rm->id);
206 Printf("%s", d.EndMutex());
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);
215 static void PrintThread(const ReportThread *rt) {
217 if (rt->id == 0) // Little sense in describing the main thread.
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));
230 Printf("%s", d.EndThreadDescription());
231 PrintStack(rt->stack);
234 static void PrintSleep(const ReportStack *s) {
236 Printf("%s", d.Sleep());
237 Printf(" As if synchronized via sleep:\n");
238 Printf("%s", d.EndSleep());
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;
254 static bool FrameIsInternal(const SymbolizedStack *frame) {
257 const char *file = frame->info.file;
259 (internal_strstr(file, "tsan_interceptors.cc") ||
260 internal_strstr(file, "sanitizer_common_interceptors.inc") ||
261 internal_strstr(file, "tsan_interface_"));
264 static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) {
265 while (FrameIsInternal(frames) && frames->next)
266 frames = frames->next;
270 void PrintReport(const ReportDesc *rep) {
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());
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),
288 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
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]);
299 PrintMutexShort(rep->mutexes[i],
300 " previously acquired by the same thread here:\n");
301 PrintStack(rep->stacks[2*i+1]);
303 PrintStack(rep->stacks[i]);
305 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
306 "to get more informative warning message\n\n");
310 for (uptr i = 0; i < rep->stacks.Size(); i++) {
313 PrintStack(rep->stacks[i]);
317 for (uptr i = 0; i < rep->mops.Size(); i++)
318 PrintMop(rep->mops[i], i == 0);
321 PrintSleep(rep->sleep);
323 for (uptr i = 0; i < rep->locs.Size(); i++)
324 PrintLocation(rep->locs[i]);
326 if (rep->typ != ReportTypeDeadlock) {
327 for (uptr i = 0; i < rep->mutexes.Size(); i++)
328 PrintMutex(rep->mutexes[i]);
331 for (uptr i = 0; i < rep->threads.Size(); i++)
332 PrintThread(rep->threads[i]);
334 if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
335 Printf(" And %d more similar thread leaks.\n\n", rep->count - 1);
337 if (ReportStack *stack = ChooseSummaryStack(rep)) {
338 if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames)) {
339 const AddressInfo &info = frame->info;
340 ReportErrorSummary(rep_typ_str, info.file, info.line, info.function);
344 Printf("==================\n");
347 #else // #ifndef SANITIZER_GO
349 const int kMainThreadId = 1;
351 void PrintStack(const ReportStack *ent) {
352 if (ent == 0 || ent->frames == 0) {
353 Printf(" [failed to restore the stack]\n");
356 SymbolizedStack *frame = ent->frames;
357 for (int i = 0; frame; frame = frame->next, i++) {
358 const AddressInfo &info = frame->info;
359 Printf(" %s()\n %s:%d +0x%zx\n", info.function,
360 StripPathPrefix(info.file, common_flags()->strip_path_prefix),
361 info.line, (void *)info.module_offset);
365 static void PrintMop(const ReportMop *mop, bool first) {
368 (first ? (mop->write ? "Write" : "Read")
369 : (mop->write ? "Previous write" : "Previous read")));
370 if (mop->tid == kMainThreadId)
371 Printf("main goroutine:\n");
373 Printf("goroutine %d:\n", mop->tid);
374 PrintStack(mop->stack);
377 static void PrintThread(const ReportThread *rt) {
378 if (rt->id == kMainThreadId)
381 Printf("Goroutine %d (%s) created at:\n",
382 rt->id, rt->running ? "running" : "finished");
383 PrintStack(rt->stack);
386 void PrintReport(const ReportDesc *rep) {
387 Printf("==================\n");
388 if (rep->typ == ReportTypeRace) {
389 Printf("WARNING: DATA RACE");
390 for (uptr i = 0; i < rep->mops.Size(); i++)
391 PrintMop(rep->mops[i], i == 0);
392 for (uptr i = 0; i < rep->threads.Size(); i++)
393 PrintThread(rep->threads[i]);
394 } else if (rep->typ == ReportTypeDeadlock) {
395 Printf("WARNING: DEADLOCK\n");
396 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
397 Printf("Goroutine %d lock mutex %d while holding mutex %d:\n",
398 999, rep->mutexes[i]->id,
399 rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
400 PrintStack(rep->stacks[2*i]);
402 Printf("Mutex %d was previously locked here:\n",
403 rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
404 PrintStack(rep->stacks[2*i + 1]);
408 Printf("==================\n");
413 } // namespace __tsan