tsan_debugging.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //===-- tsan_debugging.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. // TSan debugging API implementation.
  12. //===----------------------------------------------------------------------===//
  13. #include "tsan_interface.h"
  14. #include "tsan_report.h"
  15. #include "tsan_rtl.h"
  16. #include "sanitizer_common/sanitizer_stackdepot.h"
  17. using namespace __tsan;
  18. static const char *ReportTypeDescription(ReportType typ) {
  19. switch (typ) {
  20. case ReportTypeRace: return "data-race";
  21. case ReportTypeVptrRace: return "data-race-vptr";
  22. case ReportTypeUseAfterFree: return "heap-use-after-free";
  23. case ReportTypeVptrUseAfterFree: return "heap-use-after-free-vptr";
  24. case ReportTypeExternalRace: return "external-race";
  25. case ReportTypeThreadLeak: return "thread-leak";
  26. case ReportTypeMutexDestroyLocked: return "locked-mutex-destroy";
  27. case ReportTypeMutexDoubleLock: return "mutex-double-lock";
  28. case ReportTypeMutexInvalidAccess: return "mutex-invalid-access";
  29. case ReportTypeMutexBadUnlock: return "mutex-bad-unlock";
  30. case ReportTypeMutexBadReadLock: return "mutex-bad-read-lock";
  31. case ReportTypeMutexBadReadUnlock: return "mutex-bad-read-unlock";
  32. case ReportTypeSignalUnsafe: return "signal-unsafe-call";
  33. case ReportTypeErrnoInSignal: return "errno-in-signal-handler";
  34. case ReportTypeDeadlock: return "lock-order-inversion";
  35. // No default case so compiler warns us if we miss one
  36. }
  37. UNREACHABLE("missing case");
  38. }
  39. static const char *ReportLocationTypeDescription(ReportLocationType typ) {
  40. switch (typ) {
  41. case ReportLocationGlobal: return "global";
  42. case ReportLocationHeap: return "heap";
  43. case ReportLocationStack: return "stack";
  44. case ReportLocationTLS: return "tls";
  45. case ReportLocationFD: return "fd";
  46. // No default case so compiler warns us if we miss one
  47. }
  48. UNREACHABLE("missing case");
  49. }
  50. static void CopyTrace(SymbolizedStack *first_frame, void **trace,
  51. uptr trace_size) {
  52. uptr i = 0;
  53. for (SymbolizedStack *frame = first_frame; frame != nullptr;
  54. frame = frame->next) {
  55. trace[i++] = (void *)frame->info.address;
  56. if (i >= trace_size) break;
  57. }
  58. }
  59. // Meant to be called by the debugger.
  60. SANITIZER_INTERFACE_ATTRIBUTE
  61. void *__tsan_get_current_report() {
  62. return const_cast<ReportDesc*>(cur_thread()->current_report);
  63. }
  64. SANITIZER_INTERFACE_ATTRIBUTE
  65. int __tsan_get_report_data(void *report, const char **description, int *count,
  66. int *stack_count, int *mop_count, int *loc_count,
  67. int *mutex_count, int *thread_count,
  68. int *unique_tid_count, void **sleep_trace,
  69. uptr trace_size) {
  70. const ReportDesc *rep = (ReportDesc *)report;
  71. *description = ReportTypeDescription(rep->typ);
  72. *count = rep->count;
  73. *stack_count = rep->stacks.Size();
  74. *mop_count = rep->mops.Size();
  75. *loc_count = rep->locs.Size();
  76. *mutex_count = rep->mutexes.Size();
  77. *thread_count = rep->threads.Size();
  78. *unique_tid_count = rep->unique_tids.Size();
  79. if (rep->sleep) CopyTrace(rep->sleep->frames, sleep_trace, trace_size);
  80. return 1;
  81. }
  82. SANITIZER_INTERFACE_ATTRIBUTE
  83. int __tsan_get_report_tag(void *report, uptr *tag) {
  84. const ReportDesc *rep = (ReportDesc *)report;
  85. *tag = rep->tag;
  86. return 1;
  87. }
  88. SANITIZER_INTERFACE_ATTRIBUTE
  89. int __tsan_get_report_stack(void *report, uptr idx, void **trace,
  90. uptr trace_size) {
  91. const ReportDesc *rep = (ReportDesc *)report;
  92. CHECK_LT(idx, rep->stacks.Size());
  93. ReportStack *stack = rep->stacks[idx];
  94. if (stack) CopyTrace(stack->frames, trace, trace_size);
  95. return stack ? 1 : 0;
  96. }
  97. SANITIZER_INTERFACE_ATTRIBUTE
  98. int __tsan_get_report_mop(void *report, uptr idx, int *tid, void **addr,
  99. int *size, int *write, int *atomic, void **trace,
  100. uptr trace_size) {
  101. const ReportDesc *rep = (ReportDesc *)report;
  102. CHECK_LT(idx, rep->mops.Size());
  103. ReportMop *mop = rep->mops[idx];
  104. *tid = mop->tid;
  105. *addr = (void *)mop->addr;
  106. *size = mop->size;
  107. *write = mop->write ? 1 : 0;
  108. *atomic = mop->atomic ? 1 : 0;
  109. if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size);
  110. return 1;
  111. }
  112. SANITIZER_INTERFACE_ATTRIBUTE
  113. int __tsan_get_report_loc(void *report, uptr idx, const char **type,
  114. void **addr, uptr *start, uptr *size, int *tid,
  115. int *fd, int *suppressable, void **trace,
  116. uptr trace_size) {
  117. const ReportDesc *rep = (ReportDesc *)report;
  118. CHECK_LT(idx, rep->locs.Size());
  119. ReportLocation *loc = rep->locs[idx];
  120. *type = ReportLocationTypeDescription(loc->type);
  121. *addr = (void *)loc->global.start;
  122. *start = loc->heap_chunk_start;
  123. *size = loc->heap_chunk_size;
  124. *tid = loc->tid;
  125. *fd = loc->fd;
  126. *suppressable = loc->suppressable;
  127. if (loc->stack) CopyTrace(loc->stack->frames, trace, trace_size);
  128. return 1;
  129. }
  130. SANITIZER_INTERFACE_ATTRIBUTE
  131. int __tsan_get_report_loc_object_type(void *report, uptr idx,
  132. const char **object_type) {
  133. const ReportDesc *rep = (ReportDesc *)report;
  134. CHECK_LT(idx, rep->locs.Size());
  135. ReportLocation *loc = rep->locs[idx];
  136. *object_type = GetObjectTypeFromTag(loc->external_tag);
  137. return 1;
  138. }
  139. SANITIZER_INTERFACE_ATTRIBUTE
  140. int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr,
  141. int *destroyed, void **trace, uptr trace_size) {
  142. const ReportDesc *rep = (ReportDesc *)report;
  143. CHECK_LT(idx, rep->mutexes.Size());
  144. ReportMutex *mutex = rep->mutexes[idx];
  145. *mutex_id = mutex->id;
  146. *addr = (void *)mutex->addr;
  147. *destroyed = false;
  148. if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size);
  149. return 1;
  150. }
  151. SANITIZER_INTERFACE_ATTRIBUTE
  152. int __tsan_get_report_thread(void *report, uptr idx, int *tid, tid_t *os_id,
  153. int *running, const char **name, int *parent_tid,
  154. void **trace, uptr trace_size) {
  155. const ReportDesc *rep = (ReportDesc *)report;
  156. CHECK_LT(idx, rep->threads.Size());
  157. ReportThread *thread = rep->threads[idx];
  158. *tid = thread->id;
  159. *os_id = thread->os_id;
  160. *running = thread->running;
  161. *name = thread->name;
  162. *parent_tid = thread->parent_tid;
  163. if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size);
  164. return 1;
  165. }
  166. SANITIZER_INTERFACE_ATTRIBUTE
  167. int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid) {
  168. const ReportDesc *rep = (ReportDesc *)report;
  169. CHECK_LT(idx, rep->unique_tids.Size());
  170. *tid = rep->unique_tids[idx];
  171. return 1;
  172. }
  173. SANITIZER_INTERFACE_ATTRIBUTE
  174. const char *__tsan_locate_address(uptr addr, char *name, uptr name_size,
  175. uptr *region_address_ptr,
  176. uptr *region_size_ptr) {
  177. uptr region_address = 0;
  178. uptr region_size = 0;
  179. const char *region_kind = nullptr;
  180. if (name && name_size > 0) name[0] = 0;
  181. if (IsMetaMem(reinterpret_cast<u32 *>(addr))) {
  182. region_kind = "meta shadow";
  183. } else if (IsShadowMem(reinterpret_cast<RawShadow *>(addr))) {
  184. region_kind = "shadow";
  185. } else {
  186. bool is_stack = false;
  187. MBlock *b = 0;
  188. Allocator *a = allocator();
  189. if (a->PointerIsMine((void *)addr)) {
  190. void *block_begin = a->GetBlockBegin((void *)addr);
  191. if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
  192. }
  193. if (b != 0) {
  194. region_address = (uptr)allocator()->GetBlockBegin((void *)addr);
  195. region_size = b->siz;
  196. region_kind = "heap";
  197. } else {
  198. // TODO(kuba.brecka): We should not lock. This is supposed to be called
  199. // from within the debugger when other threads are stopped.
  200. ctx->thread_registry.Lock();
  201. ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack);
  202. ctx->thread_registry.Unlock();
  203. if (tctx) {
  204. region_kind = is_stack ? "stack" : "tls";
  205. } else {
  206. region_kind = "global";
  207. DataInfo info;
  208. if (Symbolizer::GetOrInit()->SymbolizeData(addr, &info)) {
  209. internal_strncpy(name, info.name, name_size);
  210. region_address = info.start;
  211. region_size = info.size;
  212. }
  213. }
  214. }
  215. }
  216. CHECK(region_kind);
  217. if (region_address_ptr) *region_address_ptr = region_address;
  218. if (region_size_ptr) *region_size_ptr = region_size;
  219. return region_kind;
  220. }
  221. SANITIZER_INTERFACE_ATTRIBUTE
  222. int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
  223. tid_t *os_id) {
  224. MBlock *b = 0;
  225. Allocator *a = allocator();
  226. if (a->PointerIsMine((void *)addr)) {
  227. void *block_begin = a->GetBlockBegin((void *)addr);
  228. if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
  229. }
  230. if (b == 0) return 0;
  231. *thread_id = b->tid;
  232. // No locking. This is supposed to be called from within the debugger when
  233. // other threads are stopped.
  234. ThreadContextBase *tctx = ctx->thread_registry.GetThreadLocked(b->tid);
  235. *os_id = tctx->os_id;
  236. StackTrace stack = StackDepotGet(b->stk);
  237. size = Min(size, (uptr)stack.size);
  238. for (uptr i = 0; i < size; i++) trace[i] = stack.trace[stack.size - i - 1];
  239. return size;
  240. }