tsan_report.cpp 15 KB

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