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