tsan_report.cpp 15 KB

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