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