]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/tsan/rtl/tsan_debugging.cc
Merge ^/head r319548 through r319778.
[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 == ReportTypeExternalRace) return "external-race";
28   if (typ == ReportTypeThreadLeak) return "thread-leak";
29   if (typ == ReportTypeMutexDestroyLocked) return "locked-mutex-destroy";
30   if (typ == ReportTypeMutexDoubleLock) return "mutex-double-lock";
31   if (typ == ReportTypeMutexInvalidAccess) return "mutex-invalid-access";
32   if (typ == ReportTypeMutexBadUnlock) return "mutex-bad-unlock";
33   if (typ == ReportTypeMutexBadReadLock) return "mutex-bad-read-lock";
34   if (typ == ReportTypeMutexBadReadUnlock) return "mutex-bad-read-unlock";
35   if (typ == ReportTypeSignalUnsafe) return "signal-unsafe-call";
36   if (typ == ReportTypeErrnoInSignal) return "errno-in-signal-handler";
37   if (typ == ReportTypeDeadlock) return "lock-order-inversion";
38   return "";
39 }
40
41 static const char *ReportLocationTypeDescription(ReportLocationType typ) {
42   if (typ == ReportLocationGlobal) return "global";
43   if (typ == ReportLocationHeap) return "heap";
44   if (typ == ReportLocationStack) return "stack";
45   if (typ == ReportLocationTLS) return "tls";
46   if (typ == ReportLocationFD) return "fd";
47   return "";
48 }
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
60 // Meant to be called by the debugger.
61 SANITIZER_INTERFACE_ATTRIBUTE
62 void *__tsan_get_current_report() {
63   return const_cast<ReportDesc*>(cur_thread()->current_report);
64 }
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
85 SANITIZER_INTERFACE_ATTRIBUTE
86 int __tsan_get_report_stack(void *report, uptr idx, void **trace,
87                             uptr trace_size) {
88   const ReportDesc *rep = (ReportDesc *)report;
89   CHECK_LT(idx, rep->stacks.Size());
90   ReportStack *stack = rep->stacks[idx];
91   if (stack) CopyTrace(stack->frames, trace, trace_size);
92   return stack ? 1 : 0;
93 }
94
95 SANITIZER_INTERFACE_ATTRIBUTE
96 int __tsan_get_report_mop(void *report, uptr idx, int *tid, void **addr,
97                           int *size, int *write, int *atomic, void **trace,
98                           uptr trace_size) {
99   const ReportDesc *rep = (ReportDesc *)report;
100   CHECK_LT(idx, rep->mops.Size());
101   ReportMop *mop = rep->mops[idx];
102   *tid = mop->tid;
103   *addr = (void *)mop->addr;
104   *size = mop->size;
105   *write = mop->write ? 1 : 0;
106   *atomic = mop->atomic ? 1 : 0;
107   if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size);
108   return 1;
109 }
110
111 SANITIZER_INTERFACE_ATTRIBUTE
112 int __tsan_get_report_loc(void *report, uptr idx, const char **type,
113                           void **addr, uptr *start, uptr *size, int *tid,
114                           int *fd, int *suppressable, void **trace,
115                           uptr trace_size) {
116   const ReportDesc *rep = (ReportDesc *)report;
117   CHECK_LT(idx, rep->locs.Size());
118   ReportLocation *loc = rep->locs[idx];
119   *type = ReportLocationTypeDescription(loc->type);
120   *addr = (void *)loc->global.start;
121   *start = loc->heap_chunk_start;
122   *size = loc->heap_chunk_size;
123   *tid = loc->tid;
124   *fd = loc->fd;
125   *suppressable = loc->suppressable;
126   if (loc->stack) CopyTrace(loc->stack->frames, trace, trace_size);
127   return 1;
128 }
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
140 SANITIZER_INTERFACE_ATTRIBUTE
141 int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr,
142                             int *destroyed, void **trace, uptr trace_size) {
143   const ReportDesc *rep = (ReportDesc *)report;
144   CHECK_LT(idx, rep->mutexes.Size());
145   ReportMutex *mutex = rep->mutexes[idx];
146   *mutex_id = mutex->id;
147   *addr = (void *)mutex->addr;
148   *destroyed = mutex->destroyed;
149   if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size);
150   return 1;
151 }
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
169 SANITIZER_INTERFACE_ATTRIBUTE
170 int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid) {
171   const ReportDesc *rep = (ReportDesc *)report;
172   CHECK_LT(idx, rep->unique_tids.Size());
173   *tid = rep->unique_tids[idx];
174   return 1;
175 }
176
177 SANITIZER_INTERFACE_ATTRIBUTE
178 const char *__tsan_locate_address(uptr addr, char *name, uptr name_size,
179                                   uptr *region_address_ptr,
180                                   uptr *region_size_ptr) {
181   uptr region_address = 0;
182   uptr region_size = 0;
183   const char *region_kind = nullptr;
184   if (name && name_size > 0) name[0] = 0;
185
186   if (IsMetaMem(addr)) {
187     region_kind = "meta shadow";
188   } else if (IsShadowMem(addr)) {
189     region_kind = "shadow";
190   } else {
191     bool is_stack = false;
192     MBlock *b = 0;
193     Allocator *a = allocator();
194     if (a->PointerIsMine((void *)addr)) {
195       void *block_begin = a->GetBlockBegin((void *)addr);
196       if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
197     }
198
199     if (b != 0) {
200       region_address = (uptr)allocator()->GetBlockBegin((void *)addr);
201       region_size = b->siz;
202       region_kind = "heap";
203     } else {
204       // TODO(kuba.brecka): We should not lock. This is supposed to be called
205       // from within the debugger when other threads are stopped.
206       ctx->thread_registry->Lock();
207       ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack);
208       ctx->thread_registry->Unlock();
209       if (tctx) {
210         region_kind = is_stack ? "stack" : "tls";
211       } else {
212         region_kind = "global";
213         DataInfo info;
214         if (Symbolizer::GetOrInit()->SymbolizeData(addr, &info)) {
215           internal_strncpy(name, info.name, name_size);
216           region_address = info.start;
217           region_size = info.size;
218         }
219       }
220     }
221   }
222
223   CHECK(region_kind);
224   if (region_address_ptr) *region_address_ptr = region_address;
225   if (region_size_ptr) *region_size_ptr = region_size;
226   return region_kind;
227 }
228
229 SANITIZER_INTERFACE_ATTRIBUTE
230 int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
231                            tid_t *os_id) {
232   MBlock *b = 0;
233   Allocator *a = allocator();
234   if (a->PointerIsMine((void *)addr)) {
235     void *block_begin = a->GetBlockBegin((void *)addr);
236     if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
237   }
238   if (b == 0) return 0;
239
240   *thread_id = b->tid;
241   // No locking.  This is supposed to be called from within the debugger when
242   // other threads are stopped.
243   ThreadContextBase *tctx = ctx->thread_registry->GetThreadLocked(b->tid);
244   *os_id = tctx->os_id;
245
246   StackTrace stack = StackDepotGet(b->stk);
247   size = Min(size, (uptr)stack.size);
248   for (uptr i = 0; i < size; i++) trace[i] = stack.trace[stack.size - i - 1];
249   return size;
250 }