1 //===-- tsan_report.cpp ---------------------------------------------------===//
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 class Decorator
: public __sanitizer::SanitizerCommonDecorator
{
24 Decorator() : SanitizerCommonDecorator() { }
25 const char *Access() { return Blue(); }
26 const char *ThreadDescription() { return Cyan(); }
27 const char *Location() { return Green(); }
28 const char *Sleep() { return Yellow(); }
29 const char *Mutex() { return Magenta(); }
32 ReportDesc::ReportDesc()
33 : tag(kExternalTagNone
)
44 ReportMop::ReportMop()
48 ReportDesc::~ReportDesc() {
49 // FIXME(dvyukov): it must be leaking a lot of memory.
54 const int kThreadBufSize
= 32;
55 const char *thread_name(char *buf
, Tid tid
) {
58 internal_snprintf(buf
, kThreadBufSize
, "thread T%d", tid
);
62 static const char *ReportTypeString(ReportType typ
, uptr tag
) {
66 case ReportTypeVptrRace
:
67 return "data race on vptr (ctor/dtor vs virtual call)";
68 case ReportTypeUseAfterFree
:
69 return "heap-use-after-free";
70 case ReportTypeVptrUseAfterFree
:
71 return "heap-use-after-free (virtual call vs free)";
72 case ReportTypeExternalRace
: {
73 const char *str
= GetReportHeaderFromTag(tag
);
74 return str
? str
: "race on external object";
76 case ReportTypeThreadLeak
:
78 case ReportTypeMutexDestroyLocked
:
79 return "destroy of a locked mutex";
80 case ReportTypeMutexDoubleLock
:
81 return "double lock of a mutex";
82 case ReportTypeMutexInvalidAccess
:
83 return "use of an invalid mutex (e.g. uninitialized or destroyed)";
84 case ReportTypeMutexBadUnlock
:
85 return "unlock of an unlocked mutex (or by a wrong thread)";
86 case ReportTypeMutexBadReadLock
:
87 return "read lock of a write locked mutex";
88 case ReportTypeMutexBadReadUnlock
:
89 return "read unlock of a write locked mutex";
90 case ReportTypeSignalUnsafe
:
91 return "signal-unsafe call inside of a signal";
92 case ReportTypeErrnoInSignal
:
93 return "signal handler spoils errno";
94 case ReportTypeDeadlock
:
95 return "lock-order-inversion (potential deadlock)";
96 case ReportTypeMutexHeldWrongContext
:
97 return "mutex held in the wrong context";
98 // No default case so compiler warns us if we miss one
100 UNREACHABLE("missing case");
103 void PrintStack(const ReportStack
*ent
) {
104 if (ent
== 0 || ent
->frames
== 0) {
105 Printf(" [failed to restore the stack]\n\n");
108 SymbolizedStack
*frame
= ent
->frames
;
109 for (int i
= 0; frame
&& frame
->info
.address
; frame
= frame
->next
, i
++) {
110 InternalScopedString res
;
111 StackTracePrinter::GetOrInit()->RenderFrame(
112 &res
, common_flags()->stack_trace_format
, i
, frame
->info
.address
,
113 &frame
->info
, common_flags()->symbolize_vs_style
,
114 common_flags()->strip_path_prefix
);
115 Printf("%s\n", res
.data());
120 static void PrintMutexSet(Vector
<ReportMopMutex
> const& mset
) {
121 for (uptr i
= 0; i
< mset
.Size(); i
++) {
123 Printf(" (mutexes:");
124 const ReportMopMutex m
= mset
[i
];
125 Printf(" %s M%u", m
.write
? "write" : "read", m
.id
);
126 Printf(i
== mset
.Size() - 1 ? ")" : ",");
130 static const char *MopDesc(bool first
, bool write
, bool atomic
) {
131 return atomic
? (first
? (write
? "Atomic write" : "Atomic read")
132 : (write
? "Previous atomic write" : "Previous atomic read"))
133 : (first
? (write
? "Write" : "Read")
134 : (write
? "Previous write" : "Previous read"));
137 static const char *ExternalMopDesc(bool first
, bool write
) {
138 return first
? (write
? "Modifying" : "Read-only")
139 : (write
? "Previous modifying" : "Previous read-only");
142 static void PrintMop(const ReportMop
*mop
, bool first
) {
144 char thrbuf
[kThreadBufSize
];
145 Printf("%s", d
.Access());
146 if (mop
->external_tag
== kExternalTagNone
) {
147 Printf(" %s of size %d at %p by %s",
148 MopDesc(first
, mop
->write
, mop
->atomic
), mop
->size
,
149 (void *)mop
->addr
, thread_name(thrbuf
, mop
->tid
));
151 const char *object_type
= GetObjectTypeFromTag(mop
->external_tag
);
152 if (object_type
== nullptr)
153 object_type
= "external object";
154 Printf(" %s access of %s at %p by %s",
155 ExternalMopDesc(first
, mop
->write
), object_type
,
156 (void *)mop
->addr
, thread_name(thrbuf
, mop
->tid
));
158 PrintMutexSet(mop
->mset
);
160 Printf("%s", d
.Default());
161 PrintStack(mop
->stack
);
164 static void PrintLocation(const ReportLocation
*loc
) {
166 char thrbuf
[kThreadBufSize
];
167 bool print_stack
= false;
168 Printf("%s", d
.Location());
169 if (loc
->type
== ReportLocationGlobal
) {
170 const DataInfo
&global
= loc
->global
;
171 if (global
.size
!= 0)
172 Printf(" Location is global '%s' of size %zu at %p (%s+0x%zx)\n\n",
173 global
.name
, global
.size
, reinterpret_cast<void *>(global
.start
),
174 StripModuleName(global
.module
), global
.module_offset
);
176 Printf(" Location is global '%s' at %p (%s+0x%zx)\n\n", global
.name
,
177 reinterpret_cast<void *>(global
.start
),
178 StripModuleName(global
.module
), global
.module_offset
);
179 } else if (loc
->type
== ReportLocationHeap
) {
180 char thrbuf
[kThreadBufSize
];
181 const char *object_type
= GetObjectTypeFromTag(loc
->external_tag
);
183 Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
184 loc
->heap_chunk_size
,
185 reinterpret_cast<void *>(loc
->heap_chunk_start
),
186 thread_name(thrbuf
, loc
->tid
));
188 Printf(" Location is %s of size %zu at %p allocated by %s:\n",
189 object_type
, loc
->heap_chunk_size
,
190 reinterpret_cast<void *>(loc
->heap_chunk_start
),
191 thread_name(thrbuf
, loc
->tid
));
194 } else if (loc
->type
== ReportLocationStack
) {
195 Printf(" Location is stack of %s.\n\n", thread_name(thrbuf
, loc
->tid
));
196 } else if (loc
->type
== ReportLocationTLS
) {
197 Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf
, loc
->tid
));
198 } else if (loc
->type
== ReportLocationFD
) {
199 Printf(" Location is file descriptor %d %s by %s at:\n", loc
->fd
,
200 loc
->fd_closed
? "destroyed" : "created",
201 thread_name(thrbuf
, loc
->tid
));
204 Printf("%s", d
.Default());
206 PrintStack(loc
->stack
);
209 static void PrintMutexShort(const ReportMutex
*rm
, const char *after
) {
211 Printf("%sM%d%s%s", d
.Mutex(), rm
->id
, d
.Default(), after
);
214 static void PrintMutexShortWithAddress(const ReportMutex
*rm
,
217 Printf("%sM%d (%p)%s%s", d
.Mutex(), rm
->id
,
218 reinterpret_cast<void *>(rm
->addr
), d
.Default(), after
);
221 static void PrintMutex(const ReportMutex
*rm
) {
223 Printf("%s", d
.Mutex());
224 Printf(" Mutex M%u (%p) created at:\n", rm
->id
,
225 reinterpret_cast<void *>(rm
->addr
));
226 Printf("%s", d
.Default());
227 PrintStack(rm
->stack
);
230 static void PrintThread(const ReportThread
*rt
) {
232 if (rt
->id
== kMainTid
) // Little sense in describing the main thread.
234 Printf("%s", d
.ThreadDescription());
235 Printf(" Thread T%d", rt
->id
);
236 if (rt
->name
&& rt
->name
[0] != '\0')
237 Printf(" '%s'", rt
->name
);
238 char thrbuf
[kThreadBufSize
];
239 const char *thread_status
= rt
->running
? "running" : "finished";
240 if (rt
->thread_type
== ThreadType::Worker
) {
241 Printf(" (tid=%llu, %s) is a GCD worker thread\n", rt
->os_id
,
244 Printf("%s", d
.Default());
247 Printf(" (tid=%llu, %s) created by %s", rt
->os_id
, thread_status
,
248 thread_name(thrbuf
, rt
->parent_tid
));
252 Printf("%s", d
.Default());
253 PrintStack(rt
->stack
);
256 static void PrintSleep(const ReportStack
*s
) {
258 Printf("%s", d
.Sleep());
259 Printf(" As if synchronized via sleep:\n");
260 Printf("%s", d
.Default());
264 static ReportStack
*ChooseSummaryStack(const ReportDesc
*rep
) {
265 if (rep
->mops
.Size())
266 return rep
->mops
[0]->stack
;
267 if (rep
->stacks
.Size())
268 return rep
->stacks
[0];
269 if (rep
->mutexes
.Size())
270 return rep
->mutexes
[0]->stack
;
271 if (rep
->threads
.Size())
272 return rep
->threads
[0]->stack
;
276 static bool FrameIsInternal(const SymbolizedStack
*frame
) {
279 const char *file
= frame
->info
.file
;
280 const char *module
= frame
->info
.module
;
282 (internal_strstr(file
, "tsan_interceptors_posix.cpp") ||
283 internal_strstr(file
, "tsan_interceptors_memintrinsics.cpp") ||
284 internal_strstr(file
, "sanitizer_common_interceptors.inc") ||
285 internal_strstr(file
, "tsan_interface_")))
287 if (module
!= 0 && (internal_strstr(module
, "libclang_rt.tsan_")))
292 static SymbolizedStack
*SkipTsanInternalFrames(SymbolizedStack
*frames
) {
293 while (FrameIsInternal(frames
) && frames
->next
)
294 frames
= frames
->next
;
298 void PrintReport(const ReportDesc
*rep
) {
300 Printf("==================\n");
301 const char *rep_typ_str
= ReportTypeString(rep
->typ
, rep
->tag
);
302 Printf("%s", d
.Warning());
303 Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str
,
304 (int)internal_getpid());
305 Printf("%s", d
.Default());
307 if (rep
->typ
== ReportTypeErrnoInSignal
)
308 Printf(" Signal %u handler invoked at:\n", rep
->signum
);
310 if (rep
->typ
== ReportTypeDeadlock
) {
311 char thrbuf
[kThreadBufSize
];
312 Printf(" Cycle in lock order graph: ");
313 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++)
314 PrintMutexShortWithAddress(rep
->mutexes
[i
], " => ");
315 PrintMutexShort(rep
->mutexes
[0], "\n\n");
316 CHECK_GT(rep
->mutexes
.Size(), 0U);
317 CHECK_EQ(rep
->mutexes
.Size() * (flags()->second_deadlock_stack
? 2 : 1),
319 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++) {
321 PrintMutexShort(rep
->mutexes
[(i
+ 1) % rep
->mutexes
.Size()],
322 " acquired here while holding mutex ");
323 PrintMutexShort(rep
->mutexes
[i
], " in ");
324 Printf("%s", d
.ThreadDescription());
325 Printf("%s:\n", thread_name(thrbuf
, rep
->unique_tids
[i
]));
326 Printf("%s", d
.Default());
327 if (flags()->second_deadlock_stack
) {
328 PrintStack(rep
->stacks
[2*i
]);
330 PrintMutexShort(rep
->mutexes
[i
],
331 " previously acquired by the same thread here:\n");
332 PrintStack(rep
->stacks
[2*i
+1]);
334 PrintStack(rep
->stacks
[i
]);
336 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
337 "to get more informative warning message\n\n");
341 for (uptr i
= 0; i
< rep
->stacks
.Size(); i
++) {
344 PrintStack(rep
->stacks
[i
]);
348 for (uptr i
= 0; i
< rep
->mops
.Size(); i
++)
349 PrintMop(rep
->mops
[i
], i
== 0);
352 PrintSleep(rep
->sleep
);
354 for (uptr i
= 0; i
< rep
->locs
.Size(); i
++)
355 PrintLocation(rep
->locs
[i
]);
357 if (rep
->typ
!= ReportTypeDeadlock
) {
358 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++)
359 PrintMutex(rep
->mutexes
[i
]);
362 for (uptr i
= 0; i
< rep
->threads
.Size(); i
++)
363 PrintThread(rep
->threads
[i
]);
365 if (rep
->typ
== ReportTypeThreadLeak
&& rep
->count
> 1)
366 Printf(" And %d more similar thread leaks.\n\n", rep
->count
- 1);
368 if (ReportStack
*stack
= ChooseSummaryStack(rep
)) {
369 if (SymbolizedStack
*frame
= SkipTsanInternalFrames(stack
->frames
))
370 ReportErrorSummary(rep_typ_str
, frame
->info
);
373 if (common_flags()->print_module_map
== 2)
376 Printf("==================\n");
379 #else // #if !SANITIZER_GO
381 const Tid kMainGoroutineId
= 1;
383 void PrintStack(const ReportStack
*ent
) {
384 if (ent
== 0 || ent
->frames
== 0) {
385 Printf(" [failed to restore the stack]\n");
388 SymbolizedStack
*frame
= ent
->frames
;
389 for (int i
= 0; frame
; frame
= frame
->next
, i
++) {
390 const AddressInfo
&info
= frame
->info
;
391 Printf(" %s()\n %s:%d +0x%zx\n", info
.function
,
392 StripPathPrefix(info
.file
, common_flags()->strip_path_prefix
),
393 info
.line
, info
.module_offset
);
397 static void PrintMop(const ReportMop
*mop
, bool first
) {
399 Printf("%s at %p by ",
400 (first
? (mop
->write
? "Write" : "Read")
401 : (mop
->write
? "Previous write" : "Previous read")),
402 reinterpret_cast<void *>(mop
->addr
));
403 if (mop
->tid
== kMainGoroutineId
)
404 Printf("main goroutine:\n");
406 Printf("goroutine %d:\n", mop
->tid
);
407 PrintStack(mop
->stack
);
410 static void PrintLocation(const ReportLocation
*loc
) {
412 case ReportLocationHeap
: {
414 Printf("Heap block of size %zu at %p allocated by ", loc
->heap_chunk_size
,
415 reinterpret_cast<void *>(loc
->heap_chunk_start
));
416 if (loc
->tid
== kMainGoroutineId
)
417 Printf("main goroutine:\n");
419 Printf("goroutine %d:\n", loc
->tid
);
420 PrintStack(loc
->stack
);
423 case ReportLocationGlobal
: {
425 Printf("Global var %s of size %zu at %p declared at %s:%zu\n",
426 loc
->global
.name
, loc
->global
.size
,
427 reinterpret_cast<void *>(loc
->global
.start
), loc
->global
.file
,
436 static void PrintThread(const ReportThread
*rt
) {
437 if (rt
->id
== kMainGoroutineId
)
440 Printf("Goroutine %d (%s) created at:\n",
441 rt
->id
, rt
->running
? "running" : "finished");
442 PrintStack(rt
->stack
);
445 void PrintReport(const ReportDesc
*rep
) {
446 Printf("==================\n");
447 if (rep
->typ
== ReportTypeRace
) {
448 Printf("WARNING: DATA RACE");
449 for (uptr i
= 0; i
< rep
->mops
.Size(); i
++)
450 PrintMop(rep
->mops
[i
], i
== 0);
451 for (uptr i
= 0; i
< rep
->locs
.Size(); i
++)
452 PrintLocation(rep
->locs
[i
]);
453 for (uptr i
= 0; i
< rep
->threads
.Size(); i
++)
454 PrintThread(rep
->threads
[i
]);
455 } else if (rep
->typ
== ReportTypeDeadlock
) {
456 Printf("WARNING: DEADLOCK\n");
457 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++) {
458 Printf("Goroutine %d lock mutex %u while holding mutex %u:\n", 999,
460 rep
->mutexes
[(i
+ 1) % rep
->mutexes
.Size()]->id
);
461 PrintStack(rep
->stacks
[2*i
]);
463 Printf("Mutex %u was previously locked here:\n",
464 rep
->mutexes
[(i
+ 1) % rep
->mutexes
.Size()]->id
);
465 PrintStack(rep
->stacks
[2*i
+ 1]);
469 Printf("==================\n");
474 } // namespace __tsan