tsan_report.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. namespace __tsan {
  20. class Decorator: public __sanitizer::SanitizerCommonDecorator {
  21. public:
  22. Decorator() : SanitizerCommonDecorator() { }
  23. const char *Access() { return Blue(); }
  24. const char *ThreadDescription() { return Cyan(); }
  25. const char *Location() { return Green(); }
  26. const char *Sleep() { return Yellow(); }
  27. const char *Mutex() { return Magenta(); }
  28. };
  29. ReportDesc::ReportDesc()
  30. : tag(kExternalTagNone)
  31. , stacks()
  32. , mops()
  33. , locs()
  34. , mutexes()
  35. , threads()
  36. , unique_tids()
  37. , sleep()
  38. , count() {
  39. }
  40. ReportMop::ReportMop()
  41. : mset() {
  42. }
  43. ReportDesc::~ReportDesc() {
  44. // FIXME(dvyukov): it must be leaking a lot of memory.
  45. }
  46. #if !SANITIZER_GO
  47. const int kThreadBufSize = 32;
  48. const char *thread_name(char *buf, Tid tid) {
  49. if (tid == kMainTid)
  50. return "main thread";
  51. internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
  52. return buf;
  53. }
  54. static const char *ReportTypeString(ReportType typ, uptr tag) {
  55. switch (typ) {
  56. case ReportTypeRace:
  57. return "data race";
  58. case ReportTypeVptrRace:
  59. return "data race on vptr (ctor/dtor vs virtual call)";
  60. case ReportTypeUseAfterFree:
  61. return "heap-use-after-free";
  62. case ReportTypeVptrUseAfterFree:
  63. return "heap-use-after-free (virtual call vs free)";
  64. case ReportTypeExternalRace: {
  65. const char *str = GetReportHeaderFromTag(tag);
  66. return str ? str : "race on external object";
  67. }
  68. case ReportTypeThreadLeak:
  69. return "thread leak";
  70. case ReportTypeMutexDestroyLocked:
  71. return "destroy of a locked mutex";
  72. case ReportTypeMutexDoubleLock:
  73. return "double lock of a mutex";
  74. case ReportTypeMutexInvalidAccess:
  75. return "use of an invalid mutex (e.g. uninitialized or destroyed)";
  76. case ReportTypeMutexBadUnlock:
  77. return "unlock of an unlocked mutex (or by a wrong thread)";
  78. case ReportTypeMutexBadReadLock:
  79. return "read lock of a write locked mutex";
  80. case ReportTypeMutexBadReadUnlock:
  81. return "read unlock of a write locked mutex";
  82. case ReportTypeSignalUnsafe:
  83. return "signal-unsafe call inside of a signal";
  84. case ReportTypeErrnoInSignal:
  85. return "signal handler spoils errno";
  86. case ReportTypeDeadlock:
  87. return "lock-order-inversion (potential deadlock)";
  88. case ReportTypeMutexHeldWrongContext:
  89. return "mutex held in the wrong context";
  90. // No default case so compiler warns us if we miss one
  91. }
  92. UNREACHABLE("missing case");
  93. }
  94. void PrintStack(const ReportStack *ent) {
  95. if (ent == 0 || ent->frames == 0) {
  96. Printf(" [failed to restore the stack]\n\n");
  97. return;
  98. }
  99. SymbolizedStack *frame = ent->frames;
  100. for (int i = 0; frame && frame->info.address; frame = frame->next, i++) {
  101. InternalScopedString res;
  102. StackTracePrinter::GetOrInit()->RenderFrame(
  103. &res, common_flags()->stack_trace_format, i, frame->info.address,
  104. &frame->info, common_flags()->symbolize_vs_style,
  105. common_flags()->strip_path_prefix);
  106. Printf("%s\n", res.data());
  107. }
  108. Printf("\n");
  109. }
  110. static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
  111. for (uptr i = 0; i < mset.Size(); i++) {
  112. if (i == 0)
  113. Printf(" (mutexes:");
  114. const ReportMopMutex m = mset[i];
  115. Printf(" %s M%u", m.write ? "write" : "read", m.id);
  116. Printf(i == mset.Size() - 1 ? ")" : ",");
  117. }
  118. }
  119. static const char *MopDesc(bool first, bool write, bool atomic) {
  120. return atomic ? (first ? (write ? "Atomic write" : "Atomic read")
  121. : (write ? "Previous atomic write" : "Previous atomic read"))
  122. : (first ? (write ? "Write" : "Read")
  123. : (write ? "Previous write" : "Previous read"));
  124. }
  125. static const char *ExternalMopDesc(bool first, bool write) {
  126. return first ? (write ? "Modifying" : "Read-only")
  127. : (write ? "Previous modifying" : "Previous read-only");
  128. }
  129. static void PrintMop(const ReportMop *mop, bool first) {
  130. Decorator d;
  131. char thrbuf[kThreadBufSize];
  132. Printf("%s", d.Access());
  133. if (mop->external_tag == kExternalTagNone) {
  134. Printf(" %s of size %d at %p by %s",
  135. MopDesc(first, mop->write, mop->atomic), mop->size,
  136. (void *)mop->addr, thread_name(thrbuf, mop->tid));
  137. } else {
  138. const char *object_type = GetObjectTypeFromTag(mop->external_tag);
  139. if (object_type == nullptr)
  140. object_type = "external object";
  141. Printf(" %s access of %s at %p by %s",
  142. ExternalMopDesc(first, mop->write), object_type,
  143. (void *)mop->addr, thread_name(thrbuf, mop->tid));
  144. }
  145. PrintMutexSet(mop->mset);
  146. Printf(":\n");
  147. Printf("%s", d.Default());
  148. PrintStack(mop->stack);
  149. }
  150. static void PrintLocation(const ReportLocation *loc) {
  151. Decorator d;
  152. char thrbuf[kThreadBufSize];
  153. bool print_stack = false;
  154. Printf("%s", d.Location());
  155. if (loc->type == ReportLocationGlobal) {
  156. const DataInfo &global = loc->global;
  157. if (global.size != 0)
  158. Printf(" Location is global '%s' of size %zu at %p (%s+0x%zx)\n\n",
  159. global.name, global.size, reinterpret_cast<void *>(global.start),
  160. StripModuleName(global.module), global.module_offset);
  161. else
  162. Printf(" Location is global '%s' at %p (%s+0x%zx)\n\n", global.name,
  163. reinterpret_cast<void *>(global.start),
  164. StripModuleName(global.module), global.module_offset);
  165. } else if (loc->type == ReportLocationHeap) {
  166. char thrbuf[kThreadBufSize];
  167. const char *object_type = GetObjectTypeFromTag(loc->external_tag);
  168. if (!object_type) {
  169. Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
  170. loc->heap_chunk_size,
  171. reinterpret_cast<void *>(loc->heap_chunk_start),
  172. thread_name(thrbuf, loc->tid));
  173. } else {
  174. Printf(" Location is %s of size %zu at %p allocated by %s:\n",
  175. object_type, loc->heap_chunk_size,
  176. reinterpret_cast<void *>(loc->heap_chunk_start),
  177. thread_name(thrbuf, loc->tid));
  178. }
  179. print_stack = true;
  180. } else if (loc->type == ReportLocationStack) {
  181. Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
  182. } else if (loc->type == ReportLocationTLS) {
  183. Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
  184. } else if (loc->type == ReportLocationFD) {
  185. Printf(" Location is file descriptor %d %s by %s at:\n", loc->fd,
  186. loc->fd_closed ? "destroyed" : "created",
  187. thread_name(thrbuf, loc->tid));
  188. print_stack = true;
  189. }
  190. Printf("%s", d.Default());
  191. if (print_stack)
  192. PrintStack(loc->stack);
  193. }
  194. static void PrintMutexShort(const ReportMutex *rm, const char *after) {
  195. Decorator d;
  196. Printf("%sM%d%s%s", d.Mutex(), rm->id, d.Default(), after);
  197. }
  198. static void PrintMutexShortWithAddress(const ReportMutex *rm,
  199. const char *after) {
  200. Decorator d;
  201. Printf("%sM%d (%p)%s%s", d.Mutex(), rm->id,
  202. reinterpret_cast<void *>(rm->addr), d.Default(), after);
  203. }
  204. static void PrintMutex(const ReportMutex *rm) {
  205. Decorator d;
  206. Printf("%s", d.Mutex());
  207. Printf(" Mutex M%u (%p) created at:\n", rm->id,
  208. reinterpret_cast<void *>(rm->addr));
  209. Printf("%s", d.Default());
  210. PrintStack(rm->stack);
  211. }
  212. static void PrintThread(const ReportThread *rt) {
  213. Decorator d;
  214. if (rt->id == kMainTid) // Little sense in describing the main thread.
  215. return;
  216. Printf("%s", d.ThreadDescription());
  217. Printf(" Thread T%d", rt->id);
  218. if (rt->name && rt->name[0] != '\0')
  219. Printf(" '%s'", rt->name);
  220. char thrbuf[kThreadBufSize];
  221. const char *thread_status = rt->running ? "running" : "finished";
  222. if (rt->thread_type == ThreadType::Worker) {
  223. Printf(" (tid=%llu, %s) is a GCD worker thread\n", rt->os_id,
  224. thread_status);
  225. Printf("\n");
  226. Printf("%s", d.Default());
  227. return;
  228. }
  229. Printf(" (tid=%llu, %s) created by %s", rt->os_id, thread_status,
  230. thread_name(thrbuf, rt->parent_tid));
  231. if (rt->stack)
  232. Printf(" at:");
  233. Printf("\n");
  234. Printf("%s", d.Default());
  235. PrintStack(rt->stack);
  236. }
  237. static void PrintSleep(const ReportStack *s) {
  238. Decorator d;
  239. Printf("%s", d.Sleep());
  240. Printf(" As if synchronized via sleep:\n");
  241. Printf("%s", d.Default());
  242. PrintStack(s);
  243. }
  244. static ReportStack *ChooseSummaryStack(const ReportDesc *rep) {
  245. if (rep->mops.Size())
  246. return rep->mops[0]->stack;
  247. if (rep->stacks.Size())
  248. return rep->stacks[0];
  249. if (rep->mutexes.Size())
  250. return rep->mutexes[0]->stack;
  251. if (rep->threads.Size())
  252. return rep->threads[0]->stack;
  253. return 0;
  254. }
  255. static const SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) {
  256. if (const SymbolizedStack *f = SkipInternalFrames(frames))
  257. return f;
  258. return frames; // Fallback to the top frame.
  259. }
  260. void PrintReport(const ReportDesc *rep) {
  261. Decorator d;
  262. Printf("==================\n");
  263. const char *rep_typ_str = ReportTypeString(rep->typ, rep->tag);
  264. Printf("%s", d.Warning());
  265. Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str,
  266. (int)internal_getpid());
  267. Printf("%s", d.Default());
  268. if (rep->typ == ReportTypeErrnoInSignal)
  269. Printf(" Signal %u handler invoked at:\n", rep->signum);
  270. if (rep->typ == ReportTypeDeadlock) {
  271. char thrbuf[kThreadBufSize];
  272. Printf(" Cycle in lock order graph: ");
  273. for (uptr i = 0; i < rep->mutexes.Size(); i++)
  274. PrintMutexShortWithAddress(rep->mutexes[i], " => ");
  275. PrintMutexShort(rep->mutexes[0], "\n\n");
  276. CHECK_GT(rep->mutexes.Size(), 0U);
  277. CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1),
  278. rep->stacks.Size());
  279. for (uptr i = 0; i < rep->mutexes.Size(); i++) {
  280. Printf(" Mutex ");
  281. PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()],
  282. " acquired here while holding mutex ");
  283. PrintMutexShort(rep->mutexes[i], " in ");
  284. Printf("%s", d.ThreadDescription());
  285. Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i]));
  286. Printf("%s", d.Default());
  287. if (flags()->second_deadlock_stack) {
  288. PrintStack(rep->stacks[2*i]);
  289. Printf(" Mutex ");
  290. PrintMutexShort(rep->mutexes[i],
  291. " previously acquired by the same thread here:\n");
  292. PrintStack(rep->stacks[2*i+1]);
  293. } else {
  294. PrintStack(rep->stacks[i]);
  295. if (i == 0)
  296. Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
  297. "to get more informative warning message\n\n");
  298. }
  299. }
  300. } else {
  301. for (uptr i = 0; i < rep->stacks.Size(); i++) {
  302. if (i)
  303. Printf(" and:\n");
  304. PrintStack(rep->stacks[i]);
  305. }
  306. }
  307. for (uptr i = 0; i < rep->mops.Size(); i++)
  308. PrintMop(rep->mops[i], i == 0);
  309. if (rep->sleep)
  310. PrintSleep(rep->sleep);
  311. for (uptr i = 0; i < rep->locs.Size(); i++)
  312. PrintLocation(rep->locs[i]);
  313. if (rep->typ != ReportTypeDeadlock) {
  314. for (uptr i = 0; i < rep->mutexes.Size(); i++)
  315. PrintMutex(rep->mutexes[i]);
  316. }
  317. for (uptr i = 0; i < rep->threads.Size(); i++)
  318. PrintThread(rep->threads[i]);
  319. if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
  320. Printf(" And %d more similar thread leaks.\n\n", rep->count - 1);
  321. if (ReportStack *stack = ChooseSummaryStack(rep)) {
  322. if (const SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames))
  323. ReportErrorSummary(rep_typ_str, frame->info);
  324. }
  325. if (common_flags()->print_module_map == 2)
  326. DumpProcessMap();
  327. Printf("==================\n");
  328. }
  329. #else // #if !SANITIZER_GO
  330. const Tid kMainGoroutineId = 1;
  331. void PrintStack(const ReportStack *ent) {
  332. if (ent == 0 || ent->frames == 0) {
  333. Printf(" [failed to restore the stack]\n");
  334. return;
  335. }
  336. SymbolizedStack *frame = ent->frames;
  337. for (int i = 0; frame; frame = frame->next, i++) {
  338. const AddressInfo &info = frame->info;
  339. Printf(" %s()\n %s:%d +0x%zx\n", info.function,
  340. StripPathPrefix(info.file, common_flags()->strip_path_prefix),
  341. info.line, info.module_offset);
  342. }
  343. }
  344. static void PrintMop(const ReportMop *mop, bool first) {
  345. Printf("\n");
  346. Printf("%s at %p by ",
  347. (first ? (mop->write ? "Write" : "Read")
  348. : (mop->write ? "Previous write" : "Previous read")),
  349. reinterpret_cast<void *>(mop->addr));
  350. if (mop->tid == kMainGoroutineId)
  351. Printf("main goroutine:\n");
  352. else
  353. Printf("goroutine %d:\n", mop->tid);
  354. PrintStack(mop->stack);
  355. }
  356. static void PrintLocation(const ReportLocation *loc) {
  357. switch (loc->type) {
  358. case ReportLocationHeap: {
  359. Printf("\n");
  360. Printf("Heap block of size %zu at %p allocated by ", loc->heap_chunk_size,
  361. reinterpret_cast<void *>(loc->heap_chunk_start));
  362. if (loc->tid == kMainGoroutineId)
  363. Printf("main goroutine:\n");
  364. else
  365. Printf("goroutine %d:\n", loc->tid);
  366. PrintStack(loc->stack);
  367. break;
  368. }
  369. case ReportLocationGlobal: {
  370. Printf("\n");
  371. Printf("Global var %s of size %zu at %p declared at %s:%zu\n",
  372. loc->global.name, loc->global.size,
  373. reinterpret_cast<void *>(loc->global.start), loc->global.file,
  374. loc->global.line);
  375. break;
  376. }
  377. default:
  378. break;
  379. }
  380. }
  381. static void PrintThread(const ReportThread *rt) {
  382. if (rt->id == kMainGoroutineId)
  383. return;
  384. Printf("\n");
  385. Printf("Goroutine %d (%s) created at:\n",
  386. rt->id, rt->running ? "running" : "finished");
  387. PrintStack(rt->stack);
  388. }
  389. void PrintReport(const ReportDesc *rep) {
  390. Printf("==================\n");
  391. if (rep->typ == ReportTypeRace) {
  392. Printf("WARNING: DATA RACE");
  393. for (uptr i = 0; i < rep->mops.Size(); i++)
  394. PrintMop(rep->mops[i], i == 0);
  395. for (uptr i = 0; i < rep->locs.Size(); i++)
  396. PrintLocation(rep->locs[i]);
  397. for (uptr i = 0; i < rep->threads.Size(); i++)
  398. PrintThread(rep->threads[i]);
  399. } else if (rep->typ == ReportTypeDeadlock) {
  400. Printf("WARNING: DEADLOCK\n");
  401. for (uptr i = 0; i < rep->mutexes.Size(); i++) {
  402. Printf("Goroutine %d lock mutex %u while holding mutex %u:\n", 999,
  403. rep->mutexes[i]->id,
  404. rep->mutexes[(i + 1) % rep->mutexes.Size()]->id);
  405. PrintStack(rep->stacks[2*i]);
  406. Printf("\n");
  407. Printf("Mutex %u was previously locked here:\n",
  408. rep->mutexes[(i + 1) % rep->mutexes.Size()]->id);
  409. PrintStack(rep->stacks[2*i + 1]);
  410. Printf("\n");
  411. }
  412. }
  413. Printf("==================\n");
  414. }
  415. #endif
  416. } // namespace __tsan