tsan_report.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. //===-- tsan_report.cc ----------------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is a part of ThreadSanitizer (TSan), a race detector.
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "tsan_report.h"
  12. #include "tsan_platform.h"
  13. #include "tsan_rtl.h"
  14. #include "sanitizer_common/sanitizer_placement_new.h"
  15. #include "sanitizer_common/sanitizer_report_decorator.h"
  16. #include "sanitizer_common/sanitizer_stacktrace_printer.h"
  17. namespace __tsan {
  18. ReportStack::ReportStack() : next(nullptr), info(), suppressable(false) {}
  19. ReportStack *ReportStack::New(uptr addr) {
  20. void *mem = internal_alloc(MBlockReportStack, sizeof(ReportStack));
  21. ReportStack *res = new(mem) ReportStack();
  22. res->info.address = addr;
  23. return res;
  24. }
  25. ReportLocation::ReportLocation(ReportLocationType type)
  26. : type(type), global(), heap_chunk_start(0), heap_chunk_size(0), tid(0),
  27. fd(0), suppressable(false), stack(nullptr) {}
  28. ReportLocation *ReportLocation::New(ReportLocationType type) {
  29. void *mem = internal_alloc(MBlockReportStack, sizeof(ReportLocation));
  30. return new(mem) ReportLocation(type);
  31. }
  32. class Decorator: public __sanitizer::SanitizerCommonDecorator {
  33. public:
  34. Decorator() : SanitizerCommonDecorator() { }
  35. const char *Warning() { return Red(); }
  36. const char *EndWarning() { return Default(); }
  37. const char *Access() { return Blue(); }
  38. const char *EndAccess() { return Default(); }
  39. const char *ThreadDescription() { return Cyan(); }
  40. const char *EndThreadDescription() { return Default(); }
  41. const char *Location() { return Green(); }
  42. const char *EndLocation() { return Default(); }
  43. const char *Sleep() { return Yellow(); }
  44. const char *EndSleep() { return Default(); }
  45. const char *Mutex() { return Magenta(); }
  46. const char *EndMutex() { return Default(); }
  47. };
  48. ReportDesc::ReportDesc()
  49. : stacks(MBlockReportStack)
  50. , mops(MBlockReportMop)
  51. , locs(MBlockReportLoc)
  52. , mutexes(MBlockReportMutex)
  53. , threads(MBlockReportThread)
  54. , unique_tids(MBlockReportThread)
  55. , sleep()
  56. , count() {
  57. }
  58. ReportMop::ReportMop()
  59. : mset(MBlockReportMutex) {
  60. }
  61. ReportDesc::~ReportDesc() {
  62. // FIXME(dvyukov): it must be leaking a lot of memory.
  63. }
  64. #ifndef TSAN_GO
  65. const int kThreadBufSize = 32;
  66. const char *thread_name(char *buf, int tid) {
  67. if (tid == 0)
  68. return "main thread";
  69. internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
  70. return buf;
  71. }
  72. static const char *ReportTypeString(ReportType typ) {
  73. if (typ == ReportTypeRace)
  74. return "data race";
  75. if (typ == ReportTypeVptrRace)
  76. return "data race on vptr (ctor/dtor vs virtual call)";
  77. if (typ == ReportTypeUseAfterFree)
  78. return "heap-use-after-free";
  79. if (typ == ReportTypeVptrUseAfterFree)
  80. return "heap-use-after-free (virtual call vs free)";
  81. if (typ == ReportTypeThreadLeak)
  82. return "thread leak";
  83. if (typ == ReportTypeMutexDestroyLocked)
  84. return "destroy of a locked mutex";
  85. if (typ == ReportTypeMutexDoubleLock)
  86. return "double lock of a mutex";
  87. if (typ == ReportTypeMutexBadUnlock)
  88. return "unlock of an unlocked mutex (or by a wrong thread)";
  89. if (typ == ReportTypeMutexBadReadLock)
  90. return "read lock of a write locked mutex";
  91. if (typ == ReportTypeMutexBadReadUnlock)
  92. return "read unlock of a write locked mutex";
  93. if (typ == ReportTypeSignalUnsafe)
  94. return "signal-unsafe call inside of a signal";
  95. if (typ == ReportTypeErrnoInSignal)
  96. return "signal handler spoils errno";
  97. if (typ == ReportTypeDeadlock)
  98. return "lock-order-inversion (potential deadlock)";
  99. return "";
  100. }
  101. void PrintStack(const ReportStack *ent) {
  102. if (ent == 0) {
  103. Printf(" [failed to restore the stack]\n\n");
  104. return;
  105. }
  106. for (int i = 0; ent && ent->info.address; ent = ent->next, i++) {
  107. InternalScopedString res(2 * GetPageSizeCached());
  108. RenderFrame(&res, common_flags()->stack_trace_format, i, ent->info,
  109. common_flags()->strip_path_prefix, "__interceptor_");
  110. Printf("%s\n", res.data());
  111. }
  112. Printf("\n");
  113. }
  114. static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
  115. for (uptr i = 0; i < mset.Size(); i++) {
  116. if (i == 0)
  117. Printf(" (mutexes:");
  118. const ReportMopMutex m = mset[i];
  119. Printf(" %s M%llu", m.write ? "write" : "read", m.id);
  120. Printf(i == mset.Size() - 1 ? ")" : ",");
  121. }
  122. }
  123. static const char *MopDesc(bool first, bool write, bool atomic) {
  124. return atomic ? (first ? (write ? "Atomic write" : "Atomic read")
  125. : (write ? "Previous atomic write" : "Previous atomic read"))
  126. : (first ? (write ? "Write" : "Read")
  127. : (write ? "Previous write" : "Previous read"));
  128. }
  129. static void PrintMop(const ReportMop *mop, bool first) {
  130. Decorator d;
  131. char thrbuf[kThreadBufSize];
  132. Printf("%s", d.Access());
  133. Printf(" %s of size %d at %p by %s",
  134. MopDesc(first, mop->write, mop->atomic),
  135. mop->size, (void*)mop->addr,
  136. thread_name(thrbuf, mop->tid));
  137. PrintMutexSet(mop->mset);
  138. Printf(":\n");
  139. Printf("%s", d.EndAccess());
  140. PrintStack(mop->stack);
  141. }
  142. static void PrintLocation(const ReportLocation *loc) {
  143. Decorator d;
  144. char thrbuf[kThreadBufSize];
  145. bool print_stack = false;
  146. Printf("%s", d.Location());
  147. if (loc->type == ReportLocationGlobal) {
  148. const DataInfo &global = loc->global;
  149. Printf(" Location is global '%s' of size %zu at %p (%s+%p)\n\n",
  150. global.name, global.size, global.start,
  151. StripModuleName(global.module), global.module_offset);
  152. } else if (loc->type == ReportLocationHeap) {
  153. char thrbuf[kThreadBufSize];
  154. Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
  155. loc->heap_chunk_size, loc->heap_chunk_start,
  156. thread_name(thrbuf, loc->tid));
  157. print_stack = true;
  158. } else if (loc->type == ReportLocationStack) {
  159. Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
  160. } else if (loc->type == ReportLocationTLS) {
  161. Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
  162. } else if (loc->type == ReportLocationFD) {
  163. Printf(" Location is file descriptor %d created by %s at:\n",
  164. loc->fd, thread_name(thrbuf, loc->tid));
  165. print_stack = true;
  166. }
  167. Printf("%s", d.EndLocation());
  168. if (print_stack)
  169. PrintStack(loc->stack);
  170. }
  171. static void PrintMutexShort(const ReportMutex *rm, const char *after) {
  172. Decorator d;
  173. Printf("%sM%zd%s%s", d.Mutex(), rm->id, d.EndMutex(), after);
  174. }
  175. static void PrintMutexShortWithAddress(const ReportMutex *rm,
  176. const char *after) {
  177. Decorator d;
  178. Printf("%sM%zd (%p)%s%s", d.Mutex(), rm->id, rm->addr, d.EndMutex(), after);
  179. }
  180. static void PrintMutex(const ReportMutex *rm) {
  181. Decorator d;
  182. if (rm->destroyed) {
  183. Printf("%s", d.Mutex());
  184. Printf(" Mutex M%llu is already destroyed.\n\n", rm->id);
  185. Printf("%s", d.EndMutex());
  186. } else {
  187. Printf("%s", d.Mutex());
  188. Printf(" Mutex M%llu (%p) created at:\n", rm->id, rm->addr);
  189. Printf("%s", d.EndMutex());
  190. PrintStack(rm->stack);
  191. }
  192. }
  193. static void PrintThread(const ReportThread *rt) {
  194. Decorator d;
  195. if (rt->id == 0) // Little sense in describing the main thread.
  196. return;
  197. Printf("%s", d.ThreadDescription());
  198. Printf(" Thread T%d", rt->id);
  199. if (rt->name && rt->name[0] != '\0')
  200. Printf(" '%s'", rt->name);
  201. char thrbuf[kThreadBufSize];
  202. Printf(" (tid=%zu, %s) created by %s",
  203. rt->pid, rt->running ? "running" : "finished",
  204. thread_name(thrbuf, rt->parent_tid));
  205. if (rt->stack)
  206. Printf(" at:");
  207. Printf("\n");
  208. Printf("%s", d.EndThreadDescription());
  209. PrintStack(rt->stack);
  210. }
  211. static void PrintSleep(const ReportStack *s) {
  212. Decorator d;
  213. Printf("%s", d.Sleep());
  214. Printf(" As if synchronized via sleep:\n");
  215. Printf("%s", d.EndSleep());
  216. PrintStack(s);
  217. }
  218. static ReportStack *ChooseSummaryStack(const ReportDesc *rep) {
  219. if (rep->mops.Size())
  220. return rep->mops[0]->stack;
  221. if (rep->stacks.Size())
  222. return rep->stacks[0];
  223. if (rep->mutexes.Size())
  224. return rep->mutexes[0]->stack;
  225. if (rep->threads.Size())
  226. return rep->threads[0]->stack;
  227. return 0;
  228. }
  229. ReportStack *SkipTsanInternalFrames(ReportStack *ent) {
  230. while (FrameIsInternal(ent) && ent->next)
  231. ent = ent->next;
  232. return ent;
  233. }
  234. void PrintReport(const ReportDesc *rep) {
  235. Decorator d;
  236. Printf("==================\n");
  237. const char *rep_typ_str = ReportTypeString(rep->typ);
  238. Printf("%s", d.Warning());
  239. Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str,
  240. (int)internal_getpid());
  241. Printf("%s", d.EndWarning());
  242. if (rep->typ == ReportTypeDeadlock) {
  243. char thrbuf[kThreadBufSize];
  244. Printf(" Cycle in lock order graph: ");
  245. for (uptr i = 0; i < rep->mutexes.Size(); i++)
  246. PrintMutexShortWithAddress(rep->mutexes[i], " => ");
  247. PrintMutexShort(rep->mutexes[0], "\n\n");
  248. CHECK_GT(rep->mutexes.Size(), 0U);
  249. CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1),
  250. rep->stacks.Size());
  251. for (uptr i = 0; i < rep->mutexes.Size(); i++) {
  252. Printf(" Mutex ");
  253. PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()],
  254. " acquired here while holding mutex ");
  255. PrintMutexShort(rep->mutexes[i], " in ");
  256. Printf("%s", d.ThreadDescription());
  257. Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i]));
  258. Printf("%s", d.EndThreadDescription());
  259. if (flags()->second_deadlock_stack) {
  260. PrintStack(rep->stacks[2*i]);
  261. Printf(" Mutex ");
  262. PrintMutexShort(rep->mutexes[i],
  263. " previously acquired by the same thread here:\n");
  264. PrintStack(rep->stacks[2*i+1]);
  265. } else {
  266. PrintStack(rep->stacks[i]);
  267. if (i == 0)
  268. Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
  269. "to get more informative warning message\n\n");
  270. }
  271. }
  272. } else {
  273. for (uptr i = 0; i < rep->stacks.Size(); i++) {
  274. if (i)
  275. Printf(" and:\n");
  276. PrintStack(rep->stacks[i]);
  277. }
  278. }
  279. for (uptr i = 0; i < rep->mops.Size(); i++)
  280. PrintMop(rep->mops[i], i == 0);
  281. if (rep->sleep)
  282. PrintSleep(rep->sleep);
  283. for (uptr i = 0; i < rep->locs.Size(); i++)
  284. PrintLocation(rep->locs[i]);
  285. if (rep->typ != ReportTypeDeadlock) {
  286. for (uptr i = 0; i < rep->mutexes.Size(); i++)
  287. PrintMutex(rep->mutexes[i]);
  288. }
  289. for (uptr i = 0; i < rep->threads.Size(); i++)
  290. PrintThread(rep->threads[i]);
  291. if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
  292. Printf(" And %d more similar thread leaks.\n\n", rep->count - 1);
  293. if (ReportStack *ent = SkipTsanInternalFrames(ChooseSummaryStack(rep))) {
  294. const AddressInfo &info = ent->info;
  295. ReportErrorSummary(rep_typ_str, info.file, info.line, info.function);
  296. }
  297. Printf("==================\n");
  298. }
  299. #else // #ifndef TSAN_GO
  300. const int kMainThreadId = 1;
  301. void PrintStack(const ReportStack *ent) {
  302. if (ent == 0) {
  303. Printf(" [failed to restore the stack]\n");
  304. return;
  305. }
  306. for (int i = 0; ent; ent = ent->next, i++) {
  307. const AddressInfo &info = ent->info;
  308. Printf(" %s()\n %s:%d +0x%zx\n", info.function, info.file, info.line,
  309. (void *)info.module_offset);
  310. }
  311. }
  312. static void PrintMop(const ReportMop *mop, bool first) {
  313. Printf("\n");
  314. Printf("%s by ",
  315. (first ? (mop->write ? "Write" : "Read")
  316. : (mop->write ? "Previous write" : "Previous read")));
  317. if (mop->tid == kMainThreadId)
  318. Printf("main goroutine:\n");
  319. else
  320. Printf("goroutine %d:\n", mop->tid);
  321. PrintStack(mop->stack);
  322. }
  323. static void PrintThread(const ReportThread *rt) {
  324. if (rt->id == kMainThreadId)
  325. return;
  326. Printf("\n");
  327. Printf("Goroutine %d (%s) created at:\n",
  328. rt->id, rt->running ? "running" : "finished");
  329. PrintStack(rt->stack);
  330. }
  331. void PrintReport(const ReportDesc *rep) {
  332. Printf("==================\n");
  333. if (rep->typ == ReportTypeRace) {
  334. Printf("WARNING: DATA RACE");
  335. for (uptr i = 0; i < rep->mops.Size(); i++)
  336. PrintMop(rep->mops[i], i == 0);
  337. for (uptr i = 0; i < rep->threads.Size(); i++)
  338. PrintThread(rep->threads[i]);
  339. } else if (rep->typ == ReportTypeDeadlock) {
  340. Printf("WARNING: DEADLOCK\n");
  341. for (uptr i = 0; i < rep->mutexes.Size(); i++) {
  342. Printf("Goroutine %d lock mutex %d while holding mutex %d:\n",
  343. 999, rep->mutexes[i]->id,
  344. rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
  345. PrintStack(rep->stacks[2*i]);
  346. Printf("\n");
  347. Printf("Mutex %d was previously locked here:\n",
  348. rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
  349. PrintStack(rep->stacks[2*i + 1]);
  350. Printf("\n");
  351. }
  352. }
  353. Printf("==================\n");
  354. }
  355. #endif
  356. } // namespace __tsan