1 //===-- tsan_report.cpp ---------------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of ThreadSanitizer (TSan), a race detector. 10 // 11 //===----------------------------------------------------------------------===// 12 #include "tsan_report.h" 13 #include "tsan_platform.h" 14 #include "tsan_rtl.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" 19 20 namespace __tsan { 21 22 class Decorator: public __sanitizer::SanitizerCommonDecorator { 23 public: 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(); } 30 }; 31 32 ReportDesc::ReportDesc() 33 : tag(kExternalTagNone) 34 , stacks() 35 , mops() 36 , locs() 37 , mutexes() 38 , threads() 39 , unique_tids() 40 , sleep() 41 , count() { 42 } 43 44 ReportMop::ReportMop() 45 : mset() { 46 } 47 48 ReportDesc::~ReportDesc() { 49 // FIXME(dvyukov): it must be leaking a lot of memory. 50 } 51 52 #if !SANITIZER_GO 53 54 const int kThreadBufSize = 32; 55 const char *thread_name(char *buf, Tid tid) { 56 if (tid == kMainTid) 57 return "main thread"; 58 internal_snprintf(buf, kThreadBufSize, "thread T%d", tid); 59 return buf; 60 } 61 62 static const char *ReportTypeString(ReportType typ, uptr tag) { 63 switch (typ) { 64 case ReportTypeRace: 65 return "data race"; 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"; 75 } 76 case ReportTypeThreadLeak: 77 return "thread leak"; 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 // No default case so compiler warns us if we miss one 97 } 98 UNREACHABLE("missing case"); 99 } 100 101 #if SANITIZER_APPLE 102 static const char *const kInterposedFunctionPrefix = "wrap_"; 103 #else 104 static const char *const kInterposedFunctionPrefix = "__interceptor_"; 105 #endif 106 107 void PrintStack(const ReportStack *ent) { 108 if (ent == 0 || ent->frames == 0) { 109 Printf(" [failed to restore the stack]\n\n"); 110 return; 111 } 112 SymbolizedStack *frame = ent->frames; 113 for (int i = 0; frame && frame->info.address; frame = frame->next, i++) { 114 InternalScopedString res; 115 RenderFrame(&res, common_flags()->stack_trace_format, i, 116 frame->info.address, &frame->info, 117 common_flags()->symbolize_vs_style, 118 common_flags()->strip_path_prefix, kInterposedFunctionPrefix); 119 Printf("%s\n", res.data()); 120 } 121 Printf("\n"); 122 } 123 124 static void PrintMutexSet(Vector<ReportMopMutex> const& mset) { 125 for (uptr i = 0; i < mset.Size(); i++) { 126 if (i == 0) 127 Printf(" (mutexes:"); 128 const ReportMopMutex m = mset[i]; 129 Printf(" %s M%u", m.write ? "write" : "read", m.id); 130 Printf(i == mset.Size() - 1 ? ")" : ","); 131 } 132 } 133 134 static const char *MopDesc(bool first, bool write, bool atomic) { 135 return atomic ? (first ? (write ? "Atomic write" : "Atomic read") 136 : (write ? "Previous atomic write" : "Previous atomic read")) 137 : (first ? (write ? "Write" : "Read") 138 : (write ? "Previous write" : "Previous read")); 139 } 140 141 static const char *ExternalMopDesc(bool first, bool write) { 142 return first ? (write ? "Modifying" : "Read-only") 143 : (write ? "Previous modifying" : "Previous read-only"); 144 } 145 146 static void PrintMop(const ReportMop *mop, bool first) { 147 Decorator d; 148 char thrbuf[kThreadBufSize]; 149 Printf("%s", d.Access()); 150 if (mop->external_tag == kExternalTagNone) { 151 Printf(" %s of size %d at %p by %s", 152 MopDesc(first, mop->write, mop->atomic), mop->size, 153 (void *)mop->addr, thread_name(thrbuf, mop->tid)); 154 } else { 155 const char *object_type = GetObjectTypeFromTag(mop->external_tag); 156 if (object_type == nullptr) 157 object_type = "external object"; 158 Printf(" %s access of %s at %p by %s", 159 ExternalMopDesc(first, mop->write), object_type, 160 (void *)mop->addr, thread_name(thrbuf, mop->tid)); 161 } 162 PrintMutexSet(mop->mset); 163 Printf(":\n"); 164 Printf("%s", d.Default()); 165 PrintStack(mop->stack); 166 } 167 168 static void PrintLocation(const ReportLocation *loc) { 169 Decorator d; 170 char thrbuf[kThreadBufSize]; 171 bool print_stack = false; 172 Printf("%s", d.Location()); 173 if (loc->type == ReportLocationGlobal) { 174 const DataInfo &global = loc->global; 175 if (global.size != 0) 176 Printf(" Location is global '%s' of size %zu at %p (%s+0x%zx)\n\n", 177 global.name, global.size, reinterpret_cast<void *>(global.start), 178 StripModuleName(global.module), global.module_offset); 179 else 180 Printf(" Location is global '%s' at %p (%s+0x%zx)\n\n", global.name, 181 reinterpret_cast<void *>(global.start), 182 StripModuleName(global.module), global.module_offset); 183 } else if (loc->type == ReportLocationHeap) { 184 char thrbuf[kThreadBufSize]; 185 const char *object_type = GetObjectTypeFromTag(loc->external_tag); 186 if (!object_type) { 187 Printf(" Location is heap block of size %zu at %p allocated by %s:\n", 188 loc->heap_chunk_size, 189 reinterpret_cast<void *>(loc->heap_chunk_start), 190 thread_name(thrbuf, loc->tid)); 191 } else { 192 Printf(" Location is %s of size %zu at %p allocated by %s:\n", 193 object_type, loc->heap_chunk_size, 194 reinterpret_cast<void *>(loc->heap_chunk_start), 195 thread_name(thrbuf, loc->tid)); 196 } 197 print_stack = true; 198 } else if (loc->type == ReportLocationStack) { 199 Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid)); 200 } else if (loc->type == ReportLocationTLS) { 201 Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid)); 202 } else if (loc->type == ReportLocationFD) { 203 Printf(" Location is file descriptor %d %s by %s at:\n", loc->fd, 204 loc->fd_closed ? "destroyed" : "created", 205 thread_name(thrbuf, loc->tid)); 206 print_stack = true; 207 } 208 Printf("%s", d.Default()); 209 if (print_stack) 210 PrintStack(loc->stack); 211 } 212 213 static void PrintMutexShort(const ReportMutex *rm, const char *after) { 214 Decorator d; 215 Printf("%sM%d%s%s", d.Mutex(), rm->id, d.Default(), after); 216 } 217 218 static void PrintMutexShortWithAddress(const ReportMutex *rm, 219 const char *after) { 220 Decorator d; 221 Printf("%sM%d (%p)%s%s", d.Mutex(), rm->id, 222 reinterpret_cast<void *>(rm->addr), d.Default(), after); 223 } 224 225 static void PrintMutex(const ReportMutex *rm) { 226 Decorator d; 227 Printf("%s", d.Mutex()); 228 Printf(" Mutex M%u (%p) created at:\n", rm->id, 229 reinterpret_cast<void *>(rm->addr)); 230 Printf("%s", d.Default()); 231 PrintStack(rm->stack); 232 } 233 234 static void PrintThread(const ReportThread *rt) { 235 Decorator d; 236 if (rt->id == kMainTid) // Little sense in describing the main thread. 237 return; 238 Printf("%s", d.ThreadDescription()); 239 Printf(" Thread T%d", rt->id); 240 if (rt->name && rt->name[0] != '\0') 241 Printf(" '%s'", rt->name); 242 char thrbuf[kThreadBufSize]; 243 const char *thread_status = rt->running ? "running" : "finished"; 244 if (rt->thread_type == ThreadType::Worker) { 245 Printf(" (tid=%llu, %s) is a GCD worker thread\n", rt->os_id, 246 thread_status); 247 Printf("\n"); 248 Printf("%s", d.Default()); 249 return; 250 } 251 Printf(" (tid=%llu, %s) created by %s", rt->os_id, thread_status, 252 thread_name(thrbuf, rt->parent_tid)); 253 if (rt->stack) 254 Printf(" at:"); 255 Printf("\n"); 256 Printf("%s", d.Default()); 257 PrintStack(rt->stack); 258 } 259 260 static void PrintSleep(const ReportStack *s) { 261 Decorator d; 262 Printf("%s", d.Sleep()); 263 Printf(" As if synchronized via sleep:\n"); 264 Printf("%s", d.Default()); 265 PrintStack(s); 266 } 267 268 static ReportStack *ChooseSummaryStack(const ReportDesc *rep) { 269 if (rep->mops.Size()) 270 return rep->mops[0]->stack; 271 if (rep->stacks.Size()) 272 return rep->stacks[0]; 273 if (rep->mutexes.Size()) 274 return rep->mutexes[0]->stack; 275 if (rep->threads.Size()) 276 return rep->threads[0]->stack; 277 return 0; 278 } 279 280 static bool FrameIsInternal(const SymbolizedStack *frame) { 281 if (frame == 0) 282 return false; 283 const char *file = frame->info.file; 284 const char *module = frame->info.module; 285 if (file != 0 && 286 (internal_strstr(file, "tsan_interceptors_posix.cpp") || 287 internal_strstr(file, "sanitizer_common_interceptors.inc") || 288 internal_strstr(file, "tsan_interface_"))) 289 return true; 290 if (module != 0 && (internal_strstr(module, "libclang_rt.tsan_"))) 291 return true; 292 return false; 293 } 294 295 static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) { 296 while (FrameIsInternal(frames) && frames->next) 297 frames = frames->next; 298 return frames; 299 } 300 301 void PrintReport(const ReportDesc *rep) { 302 Decorator d; 303 Printf("==================\n"); 304 const char *rep_typ_str = ReportTypeString(rep->typ, rep->tag); 305 Printf("%s", d.Warning()); 306 Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str, 307 (int)internal_getpid()); 308 Printf("%s", d.Default()); 309 310 if (rep->typ == ReportTypeErrnoInSignal) 311 Printf(" Signal %u handler invoked at:\n", rep->signum); 312 313 if (rep->typ == ReportTypeDeadlock) { 314 char thrbuf[kThreadBufSize]; 315 Printf(" Cycle in lock order graph: "); 316 for (uptr i = 0; i < rep->mutexes.Size(); i++) 317 PrintMutexShortWithAddress(rep->mutexes[i], " => "); 318 PrintMutexShort(rep->mutexes[0], "\n\n"); 319 CHECK_GT(rep->mutexes.Size(), 0U); 320 CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1), 321 rep->stacks.Size()); 322 for (uptr i = 0; i < rep->mutexes.Size(); i++) { 323 Printf(" Mutex "); 324 PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()], 325 " acquired here while holding mutex "); 326 PrintMutexShort(rep->mutexes[i], " in "); 327 Printf("%s", d.ThreadDescription()); 328 Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i])); 329 Printf("%s", d.Default()); 330 if (flags()->second_deadlock_stack) { 331 PrintStack(rep->stacks[2*i]); 332 Printf(" Mutex "); 333 PrintMutexShort(rep->mutexes[i], 334 " previously acquired by the same thread here:\n"); 335 PrintStack(rep->stacks[2*i+1]); 336 } else { 337 PrintStack(rep->stacks[i]); 338 if (i == 0) 339 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 " 340 "to get more informative warning message\n\n"); 341 } 342 } 343 } else { 344 for (uptr i = 0; i < rep->stacks.Size(); i++) { 345 if (i) 346 Printf(" and:\n"); 347 PrintStack(rep->stacks[i]); 348 } 349 } 350 351 for (uptr i = 0; i < rep->mops.Size(); i++) 352 PrintMop(rep->mops[i], i == 0); 353 354 if (rep->sleep) 355 PrintSleep(rep->sleep); 356 357 for (uptr i = 0; i < rep->locs.Size(); i++) 358 PrintLocation(rep->locs[i]); 359 360 if (rep->typ != ReportTypeDeadlock) { 361 for (uptr i = 0; i < rep->mutexes.Size(); i++) 362 PrintMutex(rep->mutexes[i]); 363 } 364 365 for (uptr i = 0; i < rep->threads.Size(); i++) 366 PrintThread(rep->threads[i]); 367 368 if (rep->typ == ReportTypeThreadLeak && rep->count > 1) 369 Printf(" And %d more similar thread leaks.\n\n", rep->count - 1); 370 371 if (ReportStack *stack = ChooseSummaryStack(rep)) { 372 if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames)) 373 ReportErrorSummary(rep_typ_str, frame->info); 374 } 375 376 if (common_flags()->print_module_map == 2) 377 DumpProcessMap(); 378 379 Printf("==================\n"); 380 } 381 382 #else // #if !SANITIZER_GO 383 384 const Tid kMainGoroutineId = 1; 385 386 void PrintStack(const ReportStack *ent) { 387 if (ent == 0 || ent->frames == 0) { 388 Printf(" [failed to restore the stack]\n"); 389 return; 390 } 391 SymbolizedStack *frame = ent->frames; 392 for (int i = 0; frame; frame = frame->next, i++) { 393 const AddressInfo &info = frame->info; 394 Printf(" %s()\n %s:%d +0x%zx\n", info.function, 395 StripPathPrefix(info.file, common_flags()->strip_path_prefix), 396 info.line, info.module_offset); 397 } 398 } 399 400 static void PrintMop(const ReportMop *mop, bool first) { 401 Printf("\n"); 402 Printf("%s at %p by ", 403 (first ? (mop->write ? "Write" : "Read") 404 : (mop->write ? "Previous write" : "Previous read")), 405 reinterpret_cast<void *>(mop->addr)); 406 if (mop->tid == kMainGoroutineId) 407 Printf("main goroutine:\n"); 408 else 409 Printf("goroutine %d:\n", mop->tid); 410 PrintStack(mop->stack); 411 } 412 413 static void PrintLocation(const ReportLocation *loc) { 414 switch (loc->type) { 415 case ReportLocationHeap: { 416 Printf("\n"); 417 Printf("Heap block of size %zu at %p allocated by ", loc->heap_chunk_size, 418 reinterpret_cast<void *>(loc->heap_chunk_start)); 419 if (loc->tid == kMainGoroutineId) 420 Printf("main goroutine:\n"); 421 else 422 Printf("goroutine %d:\n", loc->tid); 423 PrintStack(loc->stack); 424 break; 425 } 426 case ReportLocationGlobal: { 427 Printf("\n"); 428 Printf("Global var %s of size %zu at %p declared at %s:%zu\n", 429 loc->global.name, loc->global.size, 430 reinterpret_cast<void *>(loc->global.start), loc->global.file, 431 loc->global.line); 432 break; 433 } 434 default: 435 break; 436 } 437 } 438 439 static void PrintThread(const ReportThread *rt) { 440 if (rt->id == kMainGoroutineId) 441 return; 442 Printf("\n"); 443 Printf("Goroutine %d (%s) created at:\n", 444 rt->id, rt->running ? "running" : "finished"); 445 PrintStack(rt->stack); 446 } 447 448 void PrintReport(const ReportDesc *rep) { 449 Printf("==================\n"); 450 if (rep->typ == ReportTypeRace) { 451 Printf("WARNING: DATA RACE"); 452 for (uptr i = 0; i < rep->mops.Size(); i++) 453 PrintMop(rep->mops[i], i == 0); 454 for (uptr i = 0; i < rep->locs.Size(); i++) 455 PrintLocation(rep->locs[i]); 456 for (uptr i = 0; i < rep->threads.Size(); i++) 457 PrintThread(rep->threads[i]); 458 } else if (rep->typ == ReportTypeDeadlock) { 459 Printf("WARNING: DEADLOCK\n"); 460 for (uptr i = 0; i < rep->mutexes.Size(); i++) { 461 Printf("Goroutine %d lock mutex %u while holding mutex %u:\n", 999, 462 rep->mutexes[i]->id, 463 rep->mutexes[(i + 1) % rep->mutexes.Size()]->id); 464 PrintStack(rep->stacks[2*i]); 465 Printf("\n"); 466 Printf("Mutex %u was previously locked here:\n", 467 rep->mutexes[(i + 1) % rep->mutexes.Size()]->id); 468 PrintStack(rep->stacks[2*i + 1]); 469 Printf("\n"); 470 } 471 } 472 Printf("==================\n"); 473 } 474 475 #endif 476 477 } // namespace __tsan 478