1 //===-- tsan_report.cc ----------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //===----------------------------------------------------------------------===//
11 #include "tsan_report.h"
12 #include "tsan_platform.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"
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
{
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
)
64 ReportMop::ReportMop()
65 : mset(MBlockReportMutex
) {
68 ReportDesc::~ReportDesc() {
69 // FIXME(dvyukov): it must be leaking a lot of memory.
74 const int kThreadBufSize
= 32;
75 const char *thread_name(char *buf
, int tid
) {
78 internal_snprintf(buf
, kThreadBufSize
, "thread T%d", tid
);
82 static const char *ReportTypeString(ReportType typ
) {
83 if (typ
== ReportTypeRace
)
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
)
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)";
113 static const char *const kInterposedFunctionPrefix
= "wrap_";
115 static const char *const kInterposedFunctionPrefix
= "__interceptor_";
118 void PrintStack(const ReportStack
*ent
) {
119 if (ent
== 0 || ent
->frames
== 0) {
120 Printf(" [failed to restore the stack]\n\n");
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());
134 static void PrintMutexSet(Vector
<ReportMopMutex
> const& mset
) {
135 for (uptr i
= 0; i
< mset
.Size(); i
++) {
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
) {
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
);
161 Printf("%s", d
.EndAccess());
162 PrintStack(mop
->stack
);
165 static void PrintLocation(const ReportLocation
*loc
) {
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
);
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
));
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
));
195 Printf("%s", d
.EndLocation());
197 PrintStack(loc
->stack
);
200 static void PrintMutexShort(const ReportMutex
*rm
, const char *after
) {
202 Printf("%sM%zd%s%s", d
.Mutex(), rm
->id
, d
.EndMutex(), after
);
205 static void PrintMutexShortWithAddress(const ReportMutex
*rm
,
208 Printf("%sM%zd (%p)%s%s", d
.Mutex(), rm
->id
, rm
->addr
, d
.EndMutex(), after
);
211 static void PrintMutex(const ReportMutex
*rm
) {
214 Printf("%s", d
.Mutex());
215 Printf(" Mutex M%llu is already destroyed.\n\n", rm
->id
);
216 Printf("%s", d
.EndMutex());
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
) {
227 if (rt
->id
== 0) // Little sense in describing the main thread.
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
));
240 Printf("%s", d
.EndThreadDescription());
241 PrintStack(rt
->stack
);
244 static void PrintSleep(const ReportStack
*s
) {
246 Printf("%s", d
.Sleep());
247 Printf(" As if synchronized via sleep:\n");
248 Printf("%s", d
.EndSleep());
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
;
264 static bool FrameIsInternal(const SymbolizedStack
*frame
) {
267 const char *file
= frame
->info
.file
;
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
;
280 void PrintReport(const ReportDesc
*rep
) {
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),
298 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++) {
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
]);
309 PrintMutexShort(rep
->mutexes
[i
],
310 " previously acquired by the same thread here:\n");
311 PrintStack(rep
->stacks
[2*i
+1]);
313 PrintStack(rep
->stacks
[i
]);
315 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
316 "to get more informative warning message\n\n");
320 for (uptr i
= 0; i
< rep
->stacks
.Size(); i
++) {
323 PrintStack(rep
->stacks
[i
]);
327 for (uptr i
= 0; i
< rep
->mops
.Size(); i
++)
328 PrintMop(rep
->mops
[i
], i
== 0);
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");
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
) {
376 (first
? (mop
->write
? "Write" : "Read")
377 : (mop
->write
? "Previous write" : "Previous read")));
378 if (mop
->tid
== kMainThreadId
)
379 Printf("main goroutine:\n");
381 Printf("goroutine %d:\n", mop
->tid
);
382 PrintStack(mop
->stack
);
385 static void PrintThread(const ReportThread
*rt
) {
386 if (rt
->id
== kMainThreadId
)
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
]);
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]);
416 Printf("==================\n");
421 } // namespace __tsan