1 //===-- asan_debugging.cc -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // This file contains various functions that are generally useful to call when
13 // using a debugger (LLDB, GDB).
14 //===----------------------------------------------------------------------===//
16 #include "asan_allocator.h"
17 #include "asan_flags.h"
18 #include "asan_internal.h"
19 #include "asan_mapping.h"
20 #include "asan_report.h"
21 #include "asan_thread.h"
25 void GetInfoForStackVar(uptr addr, AddressDescription *descr, AsanThread *t) {
27 descr->region_address = 0;
28 descr->region_size = 0;
29 descr->region_kind = "stack";
31 AsanThread::StackFrameAccess access;
32 if (!t->GetStackFrameAccessByAddr(addr, &access))
34 InternalMmapVector<StackVarDescr> vars(16);
35 if (!ParseFrameDescription(access.frame_descr, &vars)) {
39 for (uptr i = 0; i < vars.size(); i++) {
40 if (access.offset <= vars[i].beg + vars[i].size) {
41 internal_strncat(descr->name, vars[i].name_pos,
42 Min(descr->name_size, vars[i].name_len));
43 descr->region_address = addr - (access.offset - vars[i].beg);
44 descr->region_size = vars[i].size;
50 void GetInfoForHeapAddress(uptr addr, AddressDescription *descr) {
51 AsanChunkView chunk = FindHeapChunkByAddress(addr);
54 descr->region_address = 0;
55 descr->region_size = 0;
57 if (!chunk.IsValid()) {
58 descr->region_kind = "heap-invalid";
62 descr->region_address = chunk.Beg();
63 descr->region_size = chunk.UsedSize();
64 descr->region_kind = "heap";
67 void AsanLocateAddress(uptr addr, AddressDescription *descr) {
68 if (DescribeAddressIfShadow(addr, descr, /* print */ false)) {
71 if (GetInfoForAddressIfGlobal(addr, descr)) {
74 asanThreadRegistry().Lock();
75 AsanThread *thread = FindThreadByStackAddress(addr);
76 asanThreadRegistry().Unlock();
78 GetInfoForStackVar(addr, descr, thread);
81 GetInfoForHeapAddress(addr, descr);
84 static uptr AsanGetStack(uptr addr, uptr *trace, u32 size, u32 *thread_id,
86 AsanChunkView chunk = FindHeapChunkByAddress(addr);
87 if (!chunk.IsValid()) return 0;
89 StackTrace stack(nullptr, 0);
91 if (chunk.AllocTid() == kInvalidTid) return 0;
92 stack = chunk.GetAllocStack();
93 if (thread_id) *thread_id = chunk.AllocTid();
95 if (chunk.FreeTid() == kInvalidTid) return 0;
96 stack = chunk.GetFreeStack();
97 if (thread_id) *thread_id = chunk.FreeTid();
101 size = Min(size, Min(stack.size, kStackTraceMax));
102 for (uptr i = 0; i < size; i++)
103 trace[i] = StackTrace::GetPreviousInstructionPc(stack.trace[i]);
111 } // namespace __asan
113 using namespace __asan;
115 SANITIZER_INTERFACE_ATTRIBUTE
116 const char *__asan_locate_address(uptr addr, char *name, uptr name_size,
117 uptr *region_address, uptr *region_size) {
118 AddressDescription descr = { name, name_size, 0, 0, nullptr };
119 AsanLocateAddress(addr, &descr);
120 if (region_address) *region_address = descr.region_address;
121 if (region_size) *region_size = descr.region_size;
122 return descr.region_kind;
125 SANITIZER_INTERFACE_ATTRIBUTE
126 uptr __asan_get_alloc_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) {
127 return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ true);
130 SANITIZER_INTERFACE_ATTRIBUTE
131 uptr __asan_get_free_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) {
132 return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ false);
135 SANITIZER_INTERFACE_ATTRIBUTE
136 void __asan_get_shadow_mapping(uptr *shadow_scale, uptr *shadow_offset) {
138 *shadow_scale = SHADOW_SCALE;
140 *shadow_offset = SHADOW_OFFSET;