tsan_debugging.cpp 9.4 KB

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