asan_stack.cc revision 238901
1//===-- asan_stack.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// Code for ASan stack trace. 13//===----------------------------------------------------------------------===// 14#include "asan_interceptors.h" 15#include "asan_lock.h" 16#include "asan_stack.h" 17#include "asan_thread.h" 18#include "asan_thread_registry.h" 19#include "sanitizer_common/sanitizer_procmaps.h" 20#include "sanitizer_common/sanitizer_symbolizer.h" 21 22#ifdef ASAN_USE_EXTERNAL_SYMBOLIZER 23extern bool 24ASAN_USE_EXTERNAL_SYMBOLIZER(const void *pc, char *out, int out_size); 25#endif 26 27namespace __asan { 28 29// ----------------------- AsanStackTrace ----------------------------- {{{1 30// PCs in stack traces are actually the return addresses, that is, 31// addresses of the next instructions after the call. That's why we 32// decrement them. 33static uptr patch_pc(uptr pc) { 34#ifdef __arm__ 35 // Cancel Thumb bit. 36 pc = pc & (~1); 37#endif 38 return pc - 1; 39} 40 41#if defined(ASAN_USE_EXTERNAL_SYMBOLIZER) 42void AsanStackTrace::PrintStack(uptr *addr, uptr size) { 43 for (uptr i = 0; i < size && addr[i]; i++) { 44 uptr pc = addr[i]; 45 if (i < size - 1 && addr[i + 1]) 46 pc = patch_pc(pc); 47 char buff[4096]; 48 ASAN_USE_EXTERNAL_SYMBOLIZER((void*)pc, buff, sizeof(buff)); 49 AsanPrintf(" #%zu 0x%zx %s\n", i, pc, buff); 50 } 51} 52 53#else // ASAN_USE_EXTERNAL_SYMBOLIZER 54void AsanStackTrace::PrintStack(uptr *addr, uptr size) { 55 ProcessMaps proc_maps; 56 uptr frame_num = 0; 57 for (uptr i = 0; i < size && addr[i]; i++) { 58 uptr pc = addr[i]; 59 if (i < size - 1 && addr[i + 1]) 60 pc = patch_pc(pc); 61 AddressInfo addr_frames[64]; 62 uptr addr_frames_num = 0; 63 if (flags()->symbolize) { 64 addr_frames_num = SymbolizeCode(pc, addr_frames, 65 ASAN_ARRAY_SIZE(addr_frames)); 66 } 67 if (addr_frames_num > 0) { 68 for (uptr j = 0; j < addr_frames_num; j++) { 69 AddressInfo &info = addr_frames[j]; 70 AsanPrintf(" #%zu 0x%zx", frame_num, pc); 71 if (info.function) { 72 AsanPrintf(" in %s", info.function); 73 } 74 if (info.file) { 75 AsanPrintf(" %s:%d:%d", info.file, info.line, info.column); 76 } else if (info.module) { 77 AsanPrintf(" (%s+0x%zx)", info.module, info.module_offset); 78 } 79 AsanPrintf("\n"); 80 info.Clear(); 81 frame_num++; 82 } 83 } else { 84 uptr offset; 85 char filename[4096]; 86 if (proc_maps.GetObjectNameAndOffset(pc, &offset, 87 filename, sizeof(filename))) { 88 AsanPrintf(" #%zu 0x%zx (%s+0x%zx)\n", frame_num, pc, filename, 89 offset); 90 } else { 91 AsanPrintf(" #%zu 0x%zx\n", frame_num, pc); 92 } 93 frame_num++; 94 } 95 } 96} 97#endif // ASAN_USE_EXTERNAL_SYMBOLIZER 98 99uptr AsanStackTrace::GetCurrentPc() { 100 return GET_CALLER_PC(); 101} 102 103void AsanStackTrace::FastUnwindStack(uptr pc, uptr bp) { 104 CHECK(size == 0 && trace[0] == pc); 105 size = 1; 106 if (!asan_inited) return; 107 AsanThread *t = asanThreadRegistry().GetCurrent(); 108 if (!t) return; 109 uptr *frame = (uptr*)bp; 110 uptr *prev_frame = frame; 111 uptr *top = (uptr*)t->stack_top(); 112 uptr *bottom = (uptr*)t->stack_bottom(); 113 while (frame >= prev_frame && 114 frame < top - 2 && 115 frame > bottom && 116 size < max_size) { 117 uptr pc1 = frame[1]; 118 if (pc1 != pc) { 119 trace[size++] = pc1; 120 } 121 prev_frame = frame; 122 frame = (uptr*)frame[0]; 123 } 124} 125 126// On 32-bits we don't compress stack traces. 127// On 64-bits we compress stack traces: if a given pc differes slightly from 128// the previous one, we record a 31-bit offset instead of the full pc. 129uptr AsanStackTrace::CompressStack(AsanStackTrace *stack, 130 u32 *compressed, uptr size) { 131#if __WORDSIZE == 32 132 // Don't compress, just copy. 133 uptr res = 0; 134 for (uptr i = 0; i < stack->size && i < size; i++) { 135 compressed[i] = stack->trace[i]; 136 res++; 137 } 138 if (stack->size < size) 139 compressed[stack->size] = 0; 140#else // 64 bits, compress. 141 uptr prev_pc = 0; 142 const uptr kMaxOffset = (1ULL << 30) - 1; 143 uptr c_index = 0; 144 uptr res = 0; 145 for (uptr i = 0, n = stack->size; i < n; i++) { 146 uptr pc = stack->trace[i]; 147 if (!pc) break; 148 if ((s64)pc < 0) break; 149 // Printf("C pc[%zu] %zx\n", i, pc); 150 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) { 151 uptr offset = (s64)(pc - prev_pc); 152 offset |= (1U << 31); 153 if (c_index >= size) break; 154 // Printf("C co[%zu] offset %zx\n", i, offset); 155 compressed[c_index++] = offset; 156 } else { 157 uptr hi = pc >> 32; 158 uptr lo = (pc << 32) >> 32; 159 CHECK((hi & (1 << 31)) == 0); 160 if (c_index + 1 >= size) break; 161 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo); 162 compressed[c_index++] = hi; 163 compressed[c_index++] = lo; 164 } 165 res++; 166 prev_pc = pc; 167 } 168 if (c_index < size) 169 compressed[c_index] = 0; 170 if (c_index + 1 < size) 171 compressed[c_index + 1] = 0; 172#endif // __WORDSIZE 173 174 // debug-only code 175#if 0 176 AsanStackTrace check_stack; 177 UncompressStack(&check_stack, compressed, size); 178 if (res < check_stack.size) { 179 Printf("res %zu check_stack.size %zu; c_size %zu\n", res, 180 check_stack.size, size); 181 } 182 // |res| may be greater than check_stack.size, because 183 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames. 184 CHECK(res >= check_stack.size); 185 CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace, 186 check_stack.size * sizeof(uptr))); 187#endif 188 189 return res; 190} 191 192void AsanStackTrace::UncompressStack(AsanStackTrace *stack, 193 u32 *compressed, uptr size) { 194#if __WORDSIZE == 32 195 // Don't uncompress, just copy. 196 stack->size = 0; 197 for (uptr i = 0; i < size && i < kStackTraceMax; i++) { 198 if (!compressed[i]) break; 199 stack->size++; 200 stack->trace[i] = compressed[i]; 201 } 202#else // 64 bits, uncompress 203 uptr prev_pc = 0; 204 stack->size = 0; 205 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) { 206 u32 x = compressed[i]; 207 uptr pc = 0; 208 if (x & (1U << 31)) { 209 // Printf("U co[%zu] offset: %x\n", i, x); 210 // this is an offset 211 s32 offset = x; 212 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend. 213 pc = prev_pc + offset; 214 CHECK(pc); 215 } else { 216 // CHECK(i + 1 < size); 217 if (i + 1 >= size) break; 218 uptr hi = x; 219 uptr lo = compressed[i+1]; 220 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo); 221 i++; 222 pc = (hi << 32) | lo; 223 if (!pc) break; 224 } 225 // Printf("U pc[%zu] %zx\n", stack->size, pc); 226 stack->trace[stack->size++] = pc; 227 prev_pc = pc; 228 } 229#endif // __WORDSIZE 230} 231 232} // namespace __asan 233