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_MAC 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 created by %s at:\n", 204 loc->fd, thread_name(thrbuf, loc->tid)); 205 print_stack = true; 206 } 207 Printf("%s", d.Default()); 208 if (print_stack) 209 PrintStack(loc->stack); 210 } 211 212 static void PrintMutexShort(const ReportMutex *rm, const char *after) { 213 Decorator d; 214 Printf("%sM%d%s%s", d.Mutex(), rm->id, d.Default(), after); 215 } 216 217 static void PrintMutexShortWithAddress(const ReportMutex *rm, 218 const char *after) { 219 Decorator d; 220 Printf("%sM%d (%p)%s%s", d.Mutex(), rm->id, 221 reinterpret_cast<void *>(rm->addr), d.Default(), after); 222 } 223 224 static void PrintMutex(const ReportMutex *rm) { 225 Decorator d; 226 Printf("%s", d.Mutex()); 227 Printf(" Mutex M%u (%p) created at:\n", rm->id, 228 reinterpret_cast<void *>(rm->addr)); 229 Printf("%s", d.Default()); 230 PrintStack(rm->stack); 231 } 232 233 static void PrintThread(const ReportThread *rt) { 234 Decorator d; 235 if (rt->id == kMainTid) // Little sense in describing the main thread. 236 return; 237 Printf("%s", d.ThreadDescription()); 238 Printf(" Thread T%d", rt->id); 239 if (rt->name && rt->name[0] != '\0') 240 Printf(" '%s'", rt->name); 241 char thrbuf[kThreadBufSize]; 242 const char *thread_status = rt->running ? "running" : "finished"; 243 if (rt->thread_type == ThreadType::Worker) { 244 Printf(" (tid=%llu, %s) is a GCD worker thread\n", rt->os_id, 245 thread_status); 246 Printf("\n"); 247 Printf("%s", d.Default()); 248 return; 249 } 250 Printf(" (tid=%llu, %s) created by %s", rt->os_id, thread_status, 251 thread_name(thrbuf, rt->parent_tid)); 252 if (rt->stack) 253 Printf(" at:"); 254 Printf("\n"); 255 Printf("%s", d.Default()); 256 PrintStack(rt->stack); 257 } 258 259 static void PrintSleep(const ReportStack *s) { 260 Decorator d; 261 Printf("%s", d.Sleep()); 262 Printf(" As if synchronized via sleep:\n"); 263 Printf("%s", d.Default()); 264 PrintStack(s); 265 } 266 267 static ReportStack *ChooseSummaryStack(const ReportDesc *rep) { 268 if (rep->mops.Size()) 269 return rep->mops[0]->stack; 270 if (rep->stacks.Size()) 271 return rep->stacks[0]; 272 if (rep->mutexes.Size()) 273 return rep->mutexes[0]->stack; 274 if (rep->threads.Size()) 275 return rep->threads[0]->stack; 276 return 0; 277 } 278 279 static bool FrameIsInternal(const SymbolizedStack *frame) { 280 if (frame == 0) 281 return false; 282 const char *file = frame->info.file; 283 const char *module = frame->info.module; 284 if (file != 0 && 285 (internal_strstr(file, "tsan_interceptors_posix.cpp") || 286 internal_strstr(file, "sanitizer_common_interceptors.inc") || 287 internal_strstr(file, "tsan_interface_"))) 288 return true; 289 if (module != 0 && (internal_strstr(module, "libclang_rt.tsan_"))) 290 return true; 291 return false; 292 } 293 294 static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) { 295 while (FrameIsInternal(frames) && frames->next) 296 frames = frames->next; 297 return frames; 298 } 299 300 void PrintReport(const ReportDesc *rep) { 301 Decorator d; 302 Printf("==================\n"); 303 const char *rep_typ_str = ReportTypeString(rep->typ, rep->tag); 304 Printf("%s", d.Warning()); 305 Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str, 306 (int)internal_getpid()); 307 Printf("%s", d.Default()); 308 309 if (rep->typ == ReportTypeDeadlock) { 310 char thrbuf[kThreadBufSize]; 311 Printf(" Cycle in lock order graph: "); 312 for (uptr i = 0; i < rep->mutexes.Size(); i++) 313 PrintMutexShortWithAddress(rep->mutexes[i], " => "); 314 PrintMutexShort(rep->mutexes[0], "\n\n"); 315 CHECK_GT(rep->mutexes.Size(), 0U); 316 CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1), 317 rep->stacks.Size()); 318 for (uptr i = 0; i < rep->mutexes.Size(); i++) { 319 Printf(" Mutex "); 320 PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()], 321 " acquired here while holding mutex "); 322 PrintMutexShort(rep->mutexes[i], " in "); 323 Printf("%s", d.ThreadDescription()); 324 Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i])); 325 Printf("%s", d.Default()); 326 if (flags()->second_deadlock_stack) { 327 PrintStack(rep->stacks[2*i]); 328 Printf(" Mutex "); 329 PrintMutexShort(rep->mutexes[i], 330 " previously acquired by the same thread here:\n"); 331 PrintStack(rep->stacks[2*i+1]); 332 } else { 333 PrintStack(rep->stacks[i]); 334 if (i == 0) 335 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 " 336 "to get more informative warning message\n\n"); 337 } 338 } 339 } else { 340 for (uptr i = 0; i < rep->stacks.Size(); i++) { 341 if (i) 342 Printf(" and:\n"); 343 PrintStack(rep->stacks[i]); 344 } 345 } 346 347 for (uptr i = 0; i < rep->mops.Size(); i++) 348 PrintMop(rep->mops[i], i == 0); 349 350 if (rep->sleep) 351 PrintSleep(rep->sleep); 352 353 for (uptr i = 0; i < rep->locs.Size(); i++) 354 PrintLocation(rep->locs[i]); 355 356 if (rep->typ != ReportTypeDeadlock) { 357 for (uptr i = 0; i < rep->mutexes.Size(); i++) 358 PrintMutex(rep->mutexes[i]); 359 } 360 361 for (uptr i = 0; i < rep->threads.Size(); i++) 362 PrintThread(rep->threads[i]); 363 364 if (rep->typ == ReportTypeThreadLeak && rep->count > 1) 365 Printf(" And %d more similar thread leaks.\n\n", rep->count - 1); 366 367 if (ReportStack *stack = ChooseSummaryStack(rep)) { 368 if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames)) 369 ReportErrorSummary(rep_typ_str, frame->info); 370 } 371 372 if (common_flags()->print_module_map == 2) 373 DumpProcessMap(); 374 375 Printf("==================\n"); 376 } 377 378 #else // #if !SANITIZER_GO 379 380 const Tid kMainGoroutineId = 1; 381 382 void PrintStack(const ReportStack *ent) { 383 if (ent == 0 || ent->frames == 0) { 384 Printf(" [failed to restore the stack]\n"); 385 return; 386 } 387 SymbolizedStack *frame = ent->frames; 388 for (int i = 0; frame; frame = frame->next, i++) { 389 const AddressInfo &info = frame->info; 390 Printf(" %s()\n %s:%d +0x%zx\n", info.function, 391 StripPathPrefix(info.file, common_flags()->strip_path_prefix), 392 info.line, info.module_offset); 393 } 394 } 395 396 static void PrintMop(const ReportMop *mop, bool first) { 397 Printf("\n"); 398 Printf("%s at %p by ", 399 (first ? (mop->write ? "Write" : "Read") 400 : (mop->write ? "Previous write" : "Previous read")), 401 reinterpret_cast<void *>(mop->addr)); 402 if (mop->tid == kMainGoroutineId) 403 Printf("main goroutine:\n"); 404 else 405 Printf("goroutine %d:\n", mop->tid); 406 PrintStack(mop->stack); 407 } 408 409 static void PrintLocation(const ReportLocation *loc) { 410 switch (loc->type) { 411 case ReportLocationHeap: { 412 Printf("\n"); 413 Printf("Heap block of size %zu at %p allocated by ", loc->heap_chunk_size, 414 reinterpret_cast<void *>(loc->heap_chunk_start)); 415 if (loc->tid == kMainGoroutineId) 416 Printf("main goroutine:\n"); 417 else 418 Printf("goroutine %d:\n", loc->tid); 419 PrintStack(loc->stack); 420 break; 421 } 422 case ReportLocationGlobal: { 423 Printf("\n"); 424 Printf("Global var %s of size %zu at %p declared at %s:%zu\n", 425 loc->global.name, loc->global.size, 426 reinterpret_cast<void *>(loc->global.start), loc->global.file, 427 loc->global.line); 428 break; 429 } 430 default: 431 break; 432 } 433 } 434 435 static void PrintThread(const ReportThread *rt) { 436 if (rt->id == kMainGoroutineId) 437 return; 438 Printf("\n"); 439 Printf("Goroutine %d (%s) created at:\n", 440 rt->id, rt->running ? "running" : "finished"); 441 PrintStack(rt->stack); 442 } 443 444 void PrintReport(const ReportDesc *rep) { 445 Printf("==================\n"); 446 if (rep->typ == ReportTypeRace) { 447 Printf("WARNING: DATA RACE"); 448 for (uptr i = 0; i < rep->mops.Size(); i++) 449 PrintMop(rep->mops[i], i == 0); 450 for (uptr i = 0; i < rep->locs.Size(); i++) 451 PrintLocation(rep->locs[i]); 452 for (uptr i = 0; i < rep->threads.Size(); i++) 453 PrintThread(rep->threads[i]); 454 } else if (rep->typ == ReportTypeDeadlock) { 455 Printf("WARNING: DEADLOCK\n"); 456 for (uptr i = 0; i < rep->mutexes.Size(); i++) { 457 Printf("Goroutine %d lock mutex %u while holding mutex %u:\n", 999, 458 rep->mutexes[i]->id, 459 rep->mutexes[(i + 1) % rep->mutexes.Size()]->id); 460 PrintStack(rep->stacks[2*i]); 461 Printf("\n"); 462 Printf("Mutex %u was previously locked here:\n", 463 rep->mutexes[(i + 1) % rep->mutexes.Size()]->id); 464 PrintStack(rep->stacks[2*i + 1]); 465 Printf("\n"); 466 } 467 } 468 Printf("==================\n"); 469 } 470 471 #endif 472 473 } // namespace __tsan 474