1 //===-- asan_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 AddressSanitizer, an address sanity checker.
11 //
12 // This file contains various functions that are generally useful to call when
13 // using a debugger (LLDB, GDB).
14 //===----------------------------------------------------------------------===//
15 
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"
22 
23 namespace __asan {
24 
GetInfoForStackVar(uptr addr,AddressDescription * descr,AsanThread * t)25 void GetInfoForStackVar(uptr addr, AddressDescription *descr, AsanThread *t) {
26   descr->name[0] = 0;
27   descr->region_address = 0;
28   descr->region_size = 0;
29   descr->region_kind = "stack";
30 
31   AsanThread::StackFrameAccess access;
32   if (!t->GetStackFrameAccessByAddr(addr, &access))
33     return;
34   InternalMmapVector<StackVarDescr> vars(16);
35   if (!ParseFrameDescription(access.frame_descr, &vars)) {
36     return;
37   }
38 
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;
45       return;
46     }
47   }
48 }
49 
GetInfoForHeapAddress(uptr addr,AddressDescription * descr)50 void GetInfoForHeapAddress(uptr addr, AddressDescription *descr) {
51   AsanChunkView chunk = FindHeapChunkByAddress(addr);
52 
53   descr->name[0] = 0;
54   descr->region_address = 0;
55   descr->region_size = 0;
56 
57   if (!chunk.IsValid()) {
58     descr->region_kind = "heap-invalid";
59     return;
60   }
61 
62   descr->region_address = chunk.Beg();
63   descr->region_size = chunk.UsedSize();
64   descr->region_kind = "heap";
65 }
66 
AsanLocateAddress(uptr addr,AddressDescription * descr)67 void AsanLocateAddress(uptr addr, AddressDescription *descr) {
68   if (DescribeAddressIfShadow(addr, descr, /* print */ false)) {
69     return;
70   }
71   if (GetInfoForAddressIfGlobal(addr, descr)) {
72     return;
73   }
74   asanThreadRegistry().Lock();
75   AsanThread *thread = FindThreadByStackAddress(addr);
76   asanThreadRegistry().Unlock();
77   if (thread) {
78     GetInfoForStackVar(addr, descr, thread);
79     return;
80   }
81   GetInfoForHeapAddress(addr, descr);
82 }
83 
AsanGetStack(uptr addr,uptr * trace,u32 size,u32 * thread_id,bool alloc_stack)84 static uptr AsanGetStack(uptr addr, uptr *trace, u32 size, u32 *thread_id,
85                          bool alloc_stack) {
86   AsanChunkView chunk = FindHeapChunkByAddress(addr);
87   if (!chunk.IsValid()) return 0;
88 
89   StackTrace stack(nullptr, 0);
90   if (alloc_stack) {
91     if (chunk.AllocTid() == kInvalidTid) return 0;
92     stack = chunk.GetAllocStack();
93     if (thread_id) *thread_id = chunk.AllocTid();
94   } else {
95     if (chunk.FreeTid() == kInvalidTid) return 0;
96     stack = chunk.GetFreeStack();
97     if (thread_id) *thread_id = chunk.FreeTid();
98   }
99 
100   if (trace && size) {
101     size = Min(size, Min(stack.size, kStackTraceMax));
102     for (uptr i = 0; i < size; i++)
103       trace[i] = StackTrace::GetPreviousInstructionPc(stack.trace[i]);
104 
105     return size;
106   }
107 
108   return 0;
109 }
110 
111 } // namespace __asan
112 
113 using namespace __asan;
114 
115 SANITIZER_INTERFACE_ATTRIBUTE
__asan_locate_address(uptr addr,char * name,uptr name_size,uptr * region_address,uptr * region_size)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;
123 }
124 
125 SANITIZER_INTERFACE_ATTRIBUTE
__asan_get_alloc_stack(uptr addr,uptr * trace,uptr size,u32 * thread_id)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);
128 }
129 
130 SANITIZER_INTERFACE_ATTRIBUTE
__asan_get_free_stack(uptr addr,uptr * trace,uptr size,u32 * thread_id)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);
133 }
134 
135 SANITIZER_INTERFACE_ATTRIBUTE
__asan_get_shadow_mapping(uptr * shadow_scale,uptr * shadow_offset)136 void __asan_get_shadow_mapping(uptr *shadow_scale, uptr *shadow_offset) {
137   if (shadow_scale)
138     *shadow_scale = SHADOW_SCALE;
139   if (shadow_offset)
140     *shadow_offset = SHADOW_OFFSET;
141 }
142