tsan_report.cpp 15 KB

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