]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/tsan/rtl/tsan_report.cc
Upgrade our copy of clang, llvm and lldb to 3.6.0 release.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / tsan / rtl / tsan_report.cc
1 //===-- tsan_report.cc ----------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "tsan_report.h"
14 #include "tsan_platform.h"
15 #include "tsan_rtl.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 *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(); }
53 };
54
55 ReportDesc::ReportDesc()
56     : stacks(MBlockReportStack)
57     , mops(MBlockReportMop)
58     , locs(MBlockReportLoc)
59     , mutexes(MBlockReportMutex)
60     , threads(MBlockReportThread)
61     , unique_tids(MBlockReportThread)
62     , sleep()
63     , count() {
64 }
65
66 ReportMop::ReportMop()
67     : mset(MBlockReportMutex) {
68 }
69
70 ReportDesc::~ReportDesc() {
71   // FIXME(dvyukov): it must be leaking a lot of memory.
72 }
73
74 #ifndef SANITIZER_GO
75
76 const int kThreadBufSize = 32;
77 const char *thread_name(char *buf, int tid) {
78   if (tid == 0)
79     return "main thread";
80   internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
81   return buf;
82 }
83
84 static const char *ReportTypeString(ReportType typ) {
85   if (typ == ReportTypeRace)
86     return "data race";
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)
94     return "thread leak";
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)";
111   return "";
112 }
113
114 void PrintStack(const ReportStack *ent) {
115   if (ent == 0 || ent->frames == 0) {
116     Printf("    [failed to restore the stack]\n\n");
117     return;
118   }
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());
125   }
126   Printf("\n");
127 }
128
129 static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
130   for (uptr i = 0; i < mset.Size(); i++) {
131     if (i == 0)
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 ? ")" : ",");
136   }
137 }
138
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"));
144 }
145
146 static void PrintMop(const ReportMop *mop, bool first) {
147   Decorator d;
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);
155   Printf(":\n");
156   Printf("%s", d.EndAccess());
157   PrintStack(mop->stack);
158 }
159
160 static void PrintLocation(const ReportLocation *loc) {
161   Decorator d;
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));
175     print_stack = true;
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));
183     print_stack = true;
184   }
185   Printf("%s", d.EndLocation());
186   if (print_stack)
187     PrintStack(loc->stack);
188 }
189
190 static void PrintMutexShort(const ReportMutex *rm, const char *after) {
191   Decorator d;
192   Printf("%sM%zd%s%s", d.Mutex(), rm->id, d.EndMutex(), after);
193 }
194
195 static void PrintMutexShortWithAddress(const ReportMutex *rm,
196                                        const char *after) {
197   Decorator d;
198   Printf("%sM%zd (%p)%s%s", d.Mutex(), rm->id, rm->addr, d.EndMutex(), after);
199 }
200
201 static void PrintMutex(const ReportMutex *rm) {
202   Decorator d;
203   if (rm->destroyed) {
204     Printf("%s", d.Mutex());
205     Printf("  Mutex M%llu is already destroyed.\n\n", rm->id);
206     Printf("%s", d.EndMutex());
207   } else {
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);
212   }
213 }
214
215 static void PrintThread(const ReportThread *rt) {
216   Decorator d;
217   if (rt->id == 0)  // Little sense in describing the main thread.
218     return;
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));
227   if (rt->stack)
228     Printf(" at:");
229   Printf("\n");
230   Printf("%s", d.EndThreadDescription());
231   PrintStack(rt->stack);
232 }
233
234 static void PrintSleep(const ReportStack *s) {
235   Decorator d;
236   Printf("%s", d.Sleep());
237   Printf("  As if synchronized via sleep:\n");
238   Printf("%s", d.EndSleep());
239   PrintStack(s);
240 }
241
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;
251   return 0;
252 }
253
254 static bool FrameIsInternal(const SymbolizedStack *frame) {
255   if (frame == 0)
256     return false;
257   const char *file = frame->info.file;
258   return file != 0 &&
259          (internal_strstr(file, "tsan_interceptors.cc") ||
260           internal_strstr(file, "sanitizer_common_interceptors.inc") ||
261           internal_strstr(file, "tsan_interface_"));
262 }
263
264 static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) {
265   while (FrameIsInternal(frames) && frames->next)
266     frames = frames->next;
267   return frames;
268 }
269
270 void PrintReport(const ReportDesc *rep) {
271   Decorator d;
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());
278
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),
287              rep->stacks.Size());
288     for (uptr i = 0; i < rep->mutexes.Size(); i++) {
289       Printf("  Mutex ");
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]);
298         Printf("  Mutex ");
299         PrintMutexShort(rep->mutexes[i],
300                         " previously acquired by the same thread here:\n");
301         PrintStack(rep->stacks[2*i+1]);
302       } else {
303         PrintStack(rep->stacks[i]);
304         if (i == 0)
305           Printf("    Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
306                  "to get more informative warning message\n\n");
307       }
308     }
309   } else {
310     for (uptr i = 0; i < rep->stacks.Size(); i++) {
311       if (i)
312         Printf("  and:\n");
313       PrintStack(rep->stacks[i]);
314     }
315   }
316
317   for (uptr i = 0; i < rep->mops.Size(); i++)
318     PrintMop(rep->mops[i], i == 0);
319
320   if (rep->sleep)
321     PrintSleep(rep->sleep);
322
323   for (uptr i = 0; i < rep->locs.Size(); i++)
324     PrintLocation(rep->locs[i]);
325
326   if (rep->typ != ReportTypeDeadlock) {
327     for (uptr i = 0; i < rep->mutexes.Size(); i++)
328       PrintMutex(rep->mutexes[i]);
329   }
330
331   for (uptr i = 0; i < rep->threads.Size(); i++)
332     PrintThread(rep->threads[i]);
333
334   if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
335     Printf("  And %d more similar thread leaks.\n\n", rep->count - 1);
336
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);
341     }
342   }
343
344   Printf("==================\n");
345 }
346
347 #else  // #ifndef SANITIZER_GO
348
349 const int kMainThreadId = 1;
350
351 void PrintStack(const ReportStack *ent) {
352   if (ent == 0 || ent->frames == 0) {
353     Printf("  [failed to restore the stack]\n");
354     return;
355   }
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);
362   }
363 }
364
365 static void PrintMop(const ReportMop *mop, bool first) {
366   Printf("\n");
367   Printf("%s by ",
368       (first ? (mop->write ? "Write" : "Read")
369              : (mop->write ? "Previous write" : "Previous read")));
370   if (mop->tid == kMainThreadId)
371     Printf("main goroutine:\n");
372   else
373     Printf("goroutine %d:\n", mop->tid);
374   PrintStack(mop->stack);
375 }
376
377 static void PrintThread(const ReportThread *rt) {
378   if (rt->id == kMainThreadId)
379     return;
380   Printf("\n");
381   Printf("Goroutine %d (%s) created at:\n",
382     rt->id, rt->running ? "running" : "finished");
383   PrintStack(rt->stack);
384 }
385
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]);
401       Printf("\n");
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]);
405       Printf("\n");
406     }
407   }
408   Printf("==================\n");
409 }
410
411 #endif
412
413 }  // namespace __tsan