[ARM] Use bitmap to control cmse_nonsecure_call register clearing
[official-gcc.git] / libsanitizer / tsan / tsan_debugging.cc
blob9a9c67fc42ed01c124c1a37dbad9875afec4ca44
1 //===-- tsan_debugging.cc -------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 // TSan debugging API implementation.
11 //===----------------------------------------------------------------------===//
12 #include "tsan_interface.h"
13 #include "tsan_report.h"
14 #include "tsan_rtl.h"
16 #include "sanitizer_common/sanitizer_stackdepot.h"
18 using namespace __tsan;
20 static const char *ReportTypeDescription(ReportType typ) {
21 if (typ == ReportTypeRace) return "data-race";
22 if (typ == ReportTypeVptrRace) return "data-race-vptr";
23 if (typ == ReportTypeUseAfterFree) return "heap-use-after-free";
24 if (typ == ReportTypeVptrUseAfterFree) return "heap-use-after-free-vptr";
25 if (typ == ReportTypeExternalRace) return "external-race";
26 if (typ == ReportTypeThreadLeak) return "thread-leak";
27 if (typ == ReportTypeMutexDestroyLocked) return "locked-mutex-destroy";
28 if (typ == ReportTypeMutexDoubleLock) return "mutex-double-lock";
29 if (typ == ReportTypeMutexInvalidAccess) return "mutex-invalid-access";
30 if (typ == ReportTypeMutexBadUnlock) return "mutex-bad-unlock";
31 if (typ == ReportTypeMutexBadReadLock) return "mutex-bad-read-lock";
32 if (typ == ReportTypeMutexBadReadUnlock) return "mutex-bad-read-unlock";
33 if (typ == ReportTypeSignalUnsafe) return "signal-unsafe-call";
34 if (typ == ReportTypeErrnoInSignal) return "errno-in-signal-handler";
35 if (typ == ReportTypeDeadlock) return "lock-order-inversion";
36 return "";
39 static const char *ReportLocationTypeDescription(ReportLocationType typ) {
40 if (typ == ReportLocationGlobal) return "global";
41 if (typ == ReportLocationHeap) return "heap";
42 if (typ == ReportLocationStack) return "stack";
43 if (typ == ReportLocationTLS) return "tls";
44 if (typ == ReportLocationFD) return "fd";
45 return "";
48 static void CopyTrace(SymbolizedStack *first_frame, void **trace,
49 uptr trace_size) {
50 uptr i = 0;
51 for (SymbolizedStack *frame = first_frame; frame != nullptr;
52 frame = frame->next) {
53 trace[i++] = (void *)frame->info.address;
54 if (i >= trace_size) break;
58 // Meant to be called by the debugger.
59 SANITIZER_INTERFACE_ATTRIBUTE
60 void *__tsan_get_current_report() {
61 return const_cast<ReportDesc*>(cur_thread()->current_report);
64 SANITIZER_INTERFACE_ATTRIBUTE
65 int __tsan_get_report_data(void *report, const char **description, int *count,
66 int *stack_count, int *mop_count, int *loc_count,
67 int *mutex_count, int *thread_count,
68 int *unique_tid_count, void **sleep_trace,
69 uptr trace_size) {
70 const ReportDesc *rep = (ReportDesc *)report;
71 *description = ReportTypeDescription(rep->typ);
72 *count = rep->count;
73 *stack_count = rep->stacks.Size();
74 *mop_count = rep->mops.Size();
75 *loc_count = rep->locs.Size();
76 *mutex_count = rep->mutexes.Size();
77 *thread_count = rep->threads.Size();
78 *unique_tid_count = rep->unique_tids.Size();
79 if (rep->sleep) CopyTrace(rep->sleep->frames, sleep_trace, trace_size);
80 return 1;
83 SANITIZER_INTERFACE_ATTRIBUTE
84 int __tsan_get_report_stack(void *report, uptr idx, void **trace,
85 uptr trace_size) {
86 const ReportDesc *rep = (ReportDesc *)report;
87 CHECK_LT(idx, rep->stacks.Size());
88 ReportStack *stack = rep->stacks[idx];
89 if (stack) CopyTrace(stack->frames, trace, trace_size);
90 return stack ? 1 : 0;
93 SANITIZER_INTERFACE_ATTRIBUTE
94 int __tsan_get_report_mop(void *report, uptr idx, int *tid, void **addr,
95 int *size, int *write, int *atomic, void **trace,
96 uptr trace_size) {
97 const ReportDesc *rep = (ReportDesc *)report;
98 CHECK_LT(idx, rep->mops.Size());
99 ReportMop *mop = rep->mops[idx];
100 *tid = mop->tid;
101 *addr = (void *)mop->addr;
102 *size = mop->size;
103 *write = mop->write ? 1 : 0;
104 *atomic = mop->atomic ? 1 : 0;
105 if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size);
106 return 1;
109 SANITIZER_INTERFACE_ATTRIBUTE
110 int __tsan_get_report_loc(void *report, uptr idx, const char **type,
111 void **addr, uptr *start, uptr *size, int *tid,
112 int *fd, int *suppressable, void **trace,
113 uptr trace_size) {
114 const ReportDesc *rep = (ReportDesc *)report;
115 CHECK_LT(idx, rep->locs.Size());
116 ReportLocation *loc = rep->locs[idx];
117 *type = ReportLocationTypeDescription(loc->type);
118 *addr = (void *)loc->global.start;
119 *start = loc->heap_chunk_start;
120 *size = loc->heap_chunk_size;
121 *tid = loc->tid;
122 *fd = loc->fd;
123 *suppressable = loc->suppressable;
124 if (loc->stack) CopyTrace(loc->stack->frames, trace, trace_size);
125 return 1;
128 SANITIZER_INTERFACE_ATTRIBUTE
129 int __tsan_get_report_loc_object_type(void *report, uptr idx,
130 const char **object_type) {
131 const ReportDesc *rep = (ReportDesc *)report;
132 CHECK_LT(idx, rep->locs.Size());
133 ReportLocation *loc = rep->locs[idx];
134 *object_type = GetObjectTypeFromTag(loc->external_tag);
135 return 1;
138 SANITIZER_INTERFACE_ATTRIBUTE
139 int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr,
140 int *destroyed, void **trace, uptr trace_size) {
141 const ReportDesc *rep = (ReportDesc *)report;
142 CHECK_LT(idx, rep->mutexes.Size());
143 ReportMutex *mutex = rep->mutexes[idx];
144 *mutex_id = mutex->id;
145 *addr = (void *)mutex->addr;
146 *destroyed = mutex->destroyed;
147 if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size);
148 return 1;
151 SANITIZER_INTERFACE_ATTRIBUTE
152 int __tsan_get_report_thread(void *report, uptr idx, int *tid, tid_t *os_id,
153 int *running, const char **name, int *parent_tid,
154 void **trace, uptr trace_size) {
155 const ReportDesc *rep = (ReportDesc *)report;
156 CHECK_LT(idx, rep->threads.Size());
157 ReportThread *thread = rep->threads[idx];
158 *tid = thread->id;
159 *os_id = thread->os_id;
160 *running = thread->running;
161 *name = thread->name;
162 *parent_tid = thread->parent_tid;
163 if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size);
164 return 1;
167 SANITIZER_INTERFACE_ATTRIBUTE
168 int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid) {
169 const ReportDesc *rep = (ReportDesc *)report;
170 CHECK_LT(idx, rep->unique_tids.Size());
171 *tid = rep->unique_tids[idx];
172 return 1;
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;
184 if (IsMetaMem(addr)) {
185 region_kind = "meta shadow";
186 } else if (IsShadowMem(addr)) {
187 region_kind = "shadow";
188 } else {
189 bool is_stack = false;
190 MBlock *b = 0;
191 Allocator *a = allocator();
192 if (a->PointerIsMine((void *)addr)) {
193 void *block_begin = a->GetBlockBegin((void *)addr);
194 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
197 if (b != 0) {
198 region_address = (uptr)allocator()->GetBlockBegin((void *)addr);
199 region_size = b->siz;
200 region_kind = "heap";
201 } else {
202 // TODO(kuba.brecka): We should not lock. This is supposed to be called
203 // from within the debugger when other threads are stopped.
204 ctx->thread_registry->Lock();
205 ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack);
206 ctx->thread_registry->Unlock();
207 if (tctx) {
208 region_kind = is_stack ? "stack" : "tls";
209 } else {
210 region_kind = "global";
211 DataInfo info;
212 if (Symbolizer::GetOrInit()->SymbolizeData(addr, &info)) {
213 internal_strncpy(name, info.name, name_size);
214 region_address = info.start;
215 region_size = info.size;
221 CHECK(region_kind);
222 if (region_address_ptr) *region_address_ptr = region_address;
223 if (region_size_ptr) *region_size_ptr = region_size;
224 return region_kind;
227 SANITIZER_INTERFACE_ATTRIBUTE
228 int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
229 tid_t *os_id) {
230 MBlock *b = 0;
231 Allocator *a = allocator();
232 if (a->PointerIsMine((void *)addr)) {
233 void *block_begin = a->GetBlockBegin((void *)addr);
234 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
236 if (b == 0) return 0;
238 *thread_id = b->tid;
239 // No locking. This is supposed to be called from within the debugger when
240 // other threads are stopped.
241 ThreadContextBase *tctx = ctx->thread_registry->GetThreadLocked(b->tid);
242 *os_id = tctx->os_id;
244 StackTrace stack = StackDepotGet(b->stk);
245 size = Min(size, (uptr)stack.size);
246 for (uptr i = 0; i < size; i++) trace[i] = stack.trace[stack.size - i - 1];
247 return size;