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