2016-10-21 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libsanitizer / tsan / tsan_report.cc
blob119b1ec1da9c2d36a3b49391a7c880d2164320b4
1 //===-- tsan_report.cc ----------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
11 #include "tsan_report.h"
12 #include "tsan_platform.h"
13 #include "tsan_rtl.h"
14 #include "sanitizer_common/sanitizer_placement_new.h"
15 #include "sanitizer_common/sanitizer_report_decorator.h"
16 #include "sanitizer_common/sanitizer_stacktrace_printer.h"
18 namespace __tsan {
20 ReportStack::ReportStack() : frames(nullptr), suppressable(false) {}
22 ReportStack *ReportStack::New() {
23 void *mem = internal_alloc(MBlockReportStack, sizeof(ReportStack));
24 return new(mem) ReportStack();
27 ReportLocation::ReportLocation(ReportLocationType type)
28 : type(type), global(), heap_chunk_start(0), heap_chunk_size(0), tid(0),
29 fd(0), suppressable(false), stack(nullptr) {}
31 ReportLocation *ReportLocation::New(ReportLocationType type) {
32 void *mem = internal_alloc(MBlockReportStack, sizeof(ReportLocation));
33 return new(mem) ReportLocation(type);
36 class Decorator: public __sanitizer::SanitizerCommonDecorator {
37 public:
38 Decorator() : SanitizerCommonDecorator() { }
39 const char *Warning() { return Red(); }
40 const char *EndWarning() { return Default(); }
41 const char *Access() { return Blue(); }
42 const char *EndAccess() { return Default(); }
43 const char *ThreadDescription() { return Cyan(); }
44 const char *EndThreadDescription() { return Default(); }
45 const char *Location() { return Green(); }
46 const char *EndLocation() { return Default(); }
47 const char *Sleep() { return Yellow(); }
48 const char *EndSleep() { return Default(); }
49 const char *Mutex() { return Magenta(); }
50 const char *EndMutex() { return Default(); }
53 ReportDesc::ReportDesc()
54 : stacks(MBlockReportStack)
55 , mops(MBlockReportMop)
56 , locs(MBlockReportLoc)
57 , mutexes(MBlockReportMutex)
58 , threads(MBlockReportThread)
59 , unique_tids(MBlockReportThread)
60 , sleep()
61 , count() {
64 ReportMop::ReportMop()
65 : mset(MBlockReportMutex) {
68 ReportDesc::~ReportDesc() {
69 // FIXME(dvyukov): it must be leaking a lot of memory.
72 #ifndef SANITIZER_GO
74 const int kThreadBufSize = 32;
75 const char *thread_name(char *buf, int tid) {
76 if (tid == 0)
77 return "main thread";
78 internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
79 return buf;
82 static const char *ReportTypeString(ReportType typ) {
83 if (typ == ReportTypeRace)
84 return "data race";
85 if (typ == ReportTypeVptrRace)
86 return "data race on vptr (ctor/dtor vs virtual call)";
87 if (typ == ReportTypeUseAfterFree)
88 return "heap-use-after-free";
89 if (typ == ReportTypeVptrUseAfterFree)
90 return "heap-use-after-free (virtual call vs free)";
91 if (typ == ReportTypeThreadLeak)
92 return "thread leak";
93 if (typ == ReportTypeMutexDestroyLocked)
94 return "destroy of a locked mutex";
95 if (typ == ReportTypeMutexDoubleLock)
96 return "double lock of a mutex";
97 if (typ == ReportTypeMutexBadUnlock)
98 return "unlock of an unlocked mutex (or by a wrong thread)";
99 if (typ == ReportTypeMutexBadReadLock)
100 return "read lock of a write locked mutex";
101 if (typ == ReportTypeMutexBadReadUnlock)
102 return "read unlock of a write locked mutex";
103 if (typ == ReportTypeSignalUnsafe)
104 return "signal-unsafe call inside of a signal";
105 if (typ == ReportTypeErrnoInSignal)
106 return "signal handler spoils errno";
107 if (typ == ReportTypeDeadlock)
108 return "lock-order-inversion (potential deadlock)";
109 return "";
112 #if SANITIZER_MAC
113 static const char *const kInterposedFunctionPrefix = "wrap_";
114 #else
115 static const char *const kInterposedFunctionPrefix = "__interceptor_";
116 #endif
118 void PrintStack(const ReportStack *ent) {
119 if (ent == 0 || ent->frames == 0) {
120 Printf(" [failed to restore the stack]\n\n");
121 return;
123 SymbolizedStack *frame = ent->frames;
124 for (int i = 0; frame && frame->info.address; frame = frame->next, i++) {
125 InternalScopedString res(2 * GetPageSizeCached());
126 RenderFrame(&res, common_flags()->stack_trace_format, i, frame->info,
127 common_flags()->symbolize_vs_style,
128 common_flags()->strip_path_prefix, kInterposedFunctionPrefix);
129 Printf("%s\n", res.data());
131 Printf("\n");
134 static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
135 for (uptr i = 0; i < mset.Size(); i++) {
136 if (i == 0)
137 Printf(" (mutexes:");
138 const ReportMopMutex m = mset[i];
139 Printf(" %s M%llu", m.write ? "write" : "read", m.id);
140 Printf(i == mset.Size() - 1 ? ")" : ",");
144 static const char *MopDesc(bool first, bool write, bool atomic) {
145 return atomic ? (first ? (write ? "Atomic write" : "Atomic read")
146 : (write ? "Previous atomic write" : "Previous atomic read"))
147 : (first ? (write ? "Write" : "Read")
148 : (write ? "Previous write" : "Previous read"));
151 static void PrintMop(const ReportMop *mop, bool first) {
152 Decorator d;
153 char thrbuf[kThreadBufSize];
154 Printf("%s", d.Access());
155 Printf(" %s of size %d at %p by %s",
156 MopDesc(first, mop->write, mop->atomic),
157 mop->size, (void*)mop->addr,
158 thread_name(thrbuf, mop->tid));
159 PrintMutexSet(mop->mset);
160 Printf(":\n");
161 Printf("%s", d.EndAccess());
162 PrintStack(mop->stack);
165 static void PrintLocation(const ReportLocation *loc) {
166 Decorator d;
167 char thrbuf[kThreadBufSize];
168 bool print_stack = false;
169 Printf("%s", d.Location());
170 if (loc->type == ReportLocationGlobal) {
171 const DataInfo &global = loc->global;
172 if (global.size != 0)
173 Printf(" Location is global '%s' of size %zu at %p (%s+%p)\n\n",
174 global.name, global.size, global.start,
175 StripModuleName(global.module), global.module_offset);
176 else
177 Printf(" Location is global '%s' at %p (%s+%p)\n\n", global.name,
178 global.start, StripModuleName(global.module),
179 global.module_offset);
180 } else if (loc->type == ReportLocationHeap) {
181 char thrbuf[kThreadBufSize];
182 Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
183 loc->heap_chunk_size, loc->heap_chunk_start,
184 thread_name(thrbuf, loc->tid));
185 print_stack = true;
186 } else if (loc->type == ReportLocationStack) {
187 Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
188 } else if (loc->type == ReportLocationTLS) {
189 Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
190 } else if (loc->type == ReportLocationFD) {
191 Printf(" Location is file descriptor %d created by %s at:\n",
192 loc->fd, thread_name(thrbuf, loc->tid));
193 print_stack = true;
195 Printf("%s", d.EndLocation());
196 if (print_stack)
197 PrintStack(loc->stack);
200 static void PrintMutexShort(const ReportMutex *rm, const char *after) {
201 Decorator d;
202 Printf("%sM%zd%s%s", d.Mutex(), rm->id, d.EndMutex(), after);
205 static void PrintMutexShortWithAddress(const ReportMutex *rm,
206 const char *after) {
207 Decorator d;
208 Printf("%sM%zd (%p)%s%s", d.Mutex(), rm->id, rm->addr, d.EndMutex(), after);
211 static void PrintMutex(const ReportMutex *rm) {
212 Decorator d;
213 if (rm->destroyed) {
214 Printf("%s", d.Mutex());
215 Printf(" Mutex M%llu is already destroyed.\n\n", rm->id);
216 Printf("%s", d.EndMutex());
217 } else {
218 Printf("%s", d.Mutex());
219 Printf(" Mutex M%llu (%p) created at:\n", rm->id, rm->addr);
220 Printf("%s", d.EndMutex());
221 PrintStack(rm->stack);
225 static void PrintThread(const ReportThread *rt) {
226 Decorator d;
227 if (rt->id == 0) // Little sense in describing the main thread.
228 return;
229 Printf("%s", d.ThreadDescription());
230 Printf(" Thread T%d", rt->id);
231 if (rt->name && rt->name[0] != '\0')
232 Printf(" '%s'", rt->name);
233 char thrbuf[kThreadBufSize];
234 Printf(" (tid=%zu, %s) created by %s",
235 rt->pid, rt->running ? "running" : "finished",
236 thread_name(thrbuf, rt->parent_tid));
237 if (rt->stack)
238 Printf(" at:");
239 Printf("\n");
240 Printf("%s", d.EndThreadDescription());
241 PrintStack(rt->stack);
244 static void PrintSleep(const ReportStack *s) {
245 Decorator d;
246 Printf("%s", d.Sleep());
247 Printf(" As if synchronized via sleep:\n");
248 Printf("%s", d.EndSleep());
249 PrintStack(s);
252 static ReportStack *ChooseSummaryStack(const ReportDesc *rep) {
253 if (rep->mops.Size())
254 return rep->mops[0]->stack;
255 if (rep->stacks.Size())
256 return rep->stacks[0];
257 if (rep->mutexes.Size())
258 return rep->mutexes[0]->stack;
259 if (rep->threads.Size())
260 return rep->threads[0]->stack;
261 return 0;
264 static bool FrameIsInternal(const SymbolizedStack *frame) {
265 if (frame == 0)
266 return false;
267 const char *file = frame->info.file;
268 return file != 0 &&
269 (internal_strstr(file, "tsan_interceptors.cc") ||
270 internal_strstr(file, "sanitizer_common_interceptors.inc") ||
271 internal_strstr(file, "tsan_interface_"));
274 static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) {
275 while (FrameIsInternal(frames) && frames->next)
276 frames = frames->next;
277 return frames;
280 void PrintReport(const ReportDesc *rep) {
281 Decorator d;
282 Printf("==================\n");
283 const char *rep_typ_str = ReportTypeString(rep->typ);
284 Printf("%s", d.Warning());
285 Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str,
286 (int)internal_getpid());
287 Printf("%s", d.EndWarning());
289 if (rep->typ == ReportTypeDeadlock) {
290 char thrbuf[kThreadBufSize];
291 Printf(" Cycle in lock order graph: ");
292 for (uptr i = 0; i < rep->mutexes.Size(); i++)
293 PrintMutexShortWithAddress(rep->mutexes[i], " => ");
294 PrintMutexShort(rep->mutexes[0], "\n\n");
295 CHECK_GT(rep->mutexes.Size(), 0U);
296 CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1),
297 rep->stacks.Size());
298 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
299 Printf(" Mutex ");
300 PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()],
301 " acquired here while holding mutex ");
302 PrintMutexShort(rep->mutexes[i], " in ");
303 Printf("%s", d.ThreadDescription());
304 Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i]));
305 Printf("%s", d.EndThreadDescription());
306 if (flags()->second_deadlock_stack) {
307 PrintStack(rep->stacks[2*i]);
308 Printf(" Mutex ");
309 PrintMutexShort(rep->mutexes[i],
310 " previously acquired by the same thread here:\n");
311 PrintStack(rep->stacks[2*i+1]);
312 } else {
313 PrintStack(rep->stacks[i]);
314 if (i == 0)
315 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
316 "to get more informative warning message\n\n");
319 } else {
320 for (uptr i = 0; i < rep->stacks.Size(); i++) {
321 if (i)
322 Printf(" and:\n");
323 PrintStack(rep->stacks[i]);
327 for (uptr i = 0; i < rep->mops.Size(); i++)
328 PrintMop(rep->mops[i], i == 0);
330 if (rep->sleep)
331 PrintSleep(rep->sleep);
333 for (uptr i = 0; i < rep->locs.Size(); i++)
334 PrintLocation(rep->locs[i]);
336 if (rep->typ != ReportTypeDeadlock) {
337 for (uptr i = 0; i < rep->mutexes.Size(); i++)
338 PrintMutex(rep->mutexes[i]);
341 for (uptr i = 0; i < rep->threads.Size(); i++)
342 PrintThread(rep->threads[i]);
344 if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
345 Printf(" And %d more similar thread leaks.\n\n", rep->count - 1);
347 if (ReportStack *stack = ChooseSummaryStack(rep)) {
348 if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames))
349 ReportErrorSummary(rep_typ_str, frame->info);
352 Printf("==================\n");
355 #else // #ifndef SANITIZER_GO
357 const int kMainThreadId = 1;
359 void PrintStack(const ReportStack *ent) {
360 if (ent == 0 || ent->frames == 0) {
361 Printf(" [failed to restore the stack]\n");
362 return;
364 SymbolizedStack *frame = ent->frames;
365 for (int i = 0; frame; frame = frame->next, i++) {
366 const AddressInfo &info = frame->info;
367 Printf(" %s()\n %s:%d +0x%zx\n", info.function,
368 StripPathPrefix(info.file, common_flags()->strip_path_prefix),
369 info.line, (void *)info.module_offset);
373 static void PrintMop(const ReportMop *mop, bool first) {
374 Printf("\n");
375 Printf("%s by ",
376 (first ? (mop->write ? "Write" : "Read")
377 : (mop->write ? "Previous write" : "Previous read")));
378 if (mop->tid == kMainThreadId)
379 Printf("main goroutine:\n");
380 else
381 Printf("goroutine %d:\n", mop->tid);
382 PrintStack(mop->stack);
385 static void PrintThread(const ReportThread *rt) {
386 if (rt->id == kMainThreadId)
387 return;
388 Printf("\n");
389 Printf("Goroutine %d (%s) created at:\n",
390 rt->id, rt->running ? "running" : "finished");
391 PrintStack(rt->stack);
394 void PrintReport(const ReportDesc *rep) {
395 Printf("==================\n");
396 if (rep->typ == ReportTypeRace) {
397 Printf("WARNING: DATA RACE");
398 for (uptr i = 0; i < rep->mops.Size(); i++)
399 PrintMop(rep->mops[i], i == 0);
400 for (uptr i = 0; i < rep->threads.Size(); i++)
401 PrintThread(rep->threads[i]);
402 } else if (rep->typ == ReportTypeDeadlock) {
403 Printf("WARNING: DEADLOCK\n");
404 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
405 Printf("Goroutine %d lock mutex %d while holding mutex %d:\n",
406 999, rep->mutexes[i]->id,
407 rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
408 PrintStack(rep->stacks[2*i]);
409 Printf("\n");
410 Printf("Mutex %d was previously locked here:\n",
411 rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
412 PrintStack(rep->stacks[2*i + 1]);
413 Printf("\n");
416 Printf("==================\n");
419 #endif
421 } // namespace __tsan