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