| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- asan_stack.cc -------------------------------------------*- C++ -*-===// |
| 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" |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 16 | #include "asan_procmaps.h" |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 17 | #include "asan_stack.h" |
| 18 | #include "asan_thread.h" |
| 19 | #include "asan_thread_registry.h" |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
| Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 23 | #if ASAN_USE_SYSINFO == 1 |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 24 | #include "sysinfo/sysinfo.h" |
| 25 | #endif |
| 26 | |
| 27 | #ifdef ASAN_USE_EXTERNAL_SYMBOLIZER |
| 28 | extern bool |
| 29 | ASAN_USE_EXTERNAL_SYMBOLIZER(const void *pc, char *out, int out_size); |
| 30 | #endif |
| 31 | |
| 32 | namespace __asan { |
| 33 | |
| 34 | // ----------------------- ProcSelfMaps ----------------------------- {{{1 |
| Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 35 | #if ASAN_USE_SYSINFO == 1 |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 36 | class ProcSelfMaps { |
| 37 | public: |
| 38 | void Init() { |
| 39 | ScopedLock lock(&mu_); |
| 40 | if (map_size_ != 0) return; // already inited |
| 41 | if (FLAG_v >= 2) { |
| 42 | Printf("ProcSelfMaps::Init()\n"); |
| 43 | } |
| 44 | ProcMapsIterator it(0, &proc_self_maps_); // 0 means "current pid" |
| 45 | |
| 46 | uint64 start, end, offset; |
| 47 | int64 inode; |
| 48 | char *flags, *filename; |
| 49 | CHECK(map_size_ == 0); |
| 50 | while (it.Next(&start, &end, &flags, &offset, &inode, &filename)) { |
| 51 | CHECK(map_size_ < kMaxProcSelfMapsSize); |
| 52 | Mapping &mapping = memory_map[map_size_]; |
| 53 | mapping.beg = start; |
| 54 | mapping.end = end; |
| 55 | mapping.offset = offset; |
| 56 | real_strncpy(mapping.name, |
| 57 | filename, ASAN_ARRAY_SIZE(mapping.name)); |
| 58 | mapping.name[ASAN_ARRAY_SIZE(mapping.name) - 1] = 0; |
| 59 | if (FLAG_v >= 2) { |
| 60 | Printf("[%ld] [%p,%p] off %p %s\n", map_size_, |
| 61 | mapping.beg, mapping.end, mapping.offset, mapping.name); |
| 62 | } |
| 63 | map_size_++; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void Print() { |
| 68 | Printf("%s\n", proc_self_maps_); |
| 69 | } |
| 70 | |
| 71 | void PrintPc(uintptr_t pc, int idx) { |
| 72 | for (size_t i = 0; i < map_size_; i++) { |
| 73 | Mapping &m = memory_map[i]; |
| 74 | if (pc >= m.beg && pc < m.end) { |
| 75 | uintptr_t offset = pc - m.beg; |
| 76 | if (i == 0) offset = pc; |
| 77 | Printf(" #%d 0x%lx (%s+0x%lx)\n", idx, pc, m.name, offset); |
| 78 | return; |
| 79 | } |
| 80 | } |
| 81 | Printf(" #%d 0x%lx\n", idx, pc); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | void copy_until_new_line(const char *str, char *dest, size_t max_size) { |
| 86 | size_t i = 0; |
| 87 | for (; str[i] && str[i] != '\n' && i < max_size - 1; i++) { |
| 88 | dest[i] = str[i]; |
| 89 | } |
| 90 | dest[i] = 0; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | struct Mapping { |
| 95 | uintptr_t beg, end, offset; |
| 96 | char name[1000]; |
| 97 | }; |
| 98 | static const size_t kMaxNumMapEntries = 4096; |
| 99 | static const size_t kMaxProcSelfMapsSize = 1 << 20; |
| 100 | ProcMapsIterator::Buffer proc_self_maps_; |
| 101 | size_t map_size_; |
| 102 | Mapping memory_map[kMaxNumMapEntries]; |
| 103 | |
| 104 | static AsanLock mu_; |
| 105 | }; |
| 106 | |
| 107 | static ProcSelfMaps proc_self_maps; |
| 108 | AsanLock ProcSelfMaps::mu_(LINKER_INITIALIZED); |
| 109 | |
| 110 | |
| 111 | void AsanStackTrace::PrintStack(uintptr_t *addr, size_t size) { |
| 112 | proc_self_maps.Init(); |
| 113 | for (size_t i = 0; i < size && addr[i]; i++) { |
| 114 | uintptr_t pc = addr[i]; |
| 115 | // int line; |
| 116 | proc_self_maps.PrintPc(pc, i); |
| 117 | // Printf(" #%ld 0x%lx %s\n", i, pc, rtn.c_str()); |
| 118 | } |
| 119 | } |
| 120 | #elif defined(ASAN_USE_EXTERNAL_SYMBOLIZER) |
| 121 | void AsanStackTrace::PrintStack(uintptr_t *addr, size_t size) { |
| 122 | for (size_t i = 0; i < size && addr[i]; i++) { |
| 123 | uintptr_t pc = addr[i]; |
| 124 | char buff[4096]; |
| 125 | ASAN_USE_EXTERNAL_SYMBOLIZER((void*)pc, buff, sizeof(buff)); |
| 126 | Printf(" #%ld 0x%lx %s\n", i, pc, buff); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | #else // ASAN_USE_SYSINFO |
| 131 | void AsanStackTrace::PrintStack(uintptr_t *addr, size_t size) { |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 132 | AsanProcMaps proc_maps; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 133 | for (size_t i = 0; i < size && addr[i]; i++) { |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 134 | proc_maps.Reset(); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 135 | uintptr_t pc = addr[i]; |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 136 | uint64_t start, end, offset; |
| 137 | char filename[4096]; |
| 138 | bool found = 0; |
| 139 | int map_idx = 0; |
| 140 | while (proc_maps.Next(&start, &end, &offset, |
| 141 | filename, sizeof(filename))) { |
| 142 | if (pc >= start && pc <= end) { |
| 143 | found = true; |
| 144 | uintptr_t relative_pc = (map_idx == 0) ? pc : (pc - start); |
| 145 | Printf(" #%ld 0x%lx (%s+0x%lx)\n", i, pc, filename, relative_pc); |
| 146 | break; |
| 147 | } |
| 148 | map_idx++; |
| 149 | } |
| 150 | if (!found) { |
| 151 | Printf(" #%ld 0x%lx\n", i, pc); |
| 152 | } |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | #endif // ASAN_USE_SYSINFO |
| 156 | |
| 157 | #ifdef __arm__ |
| 158 | #define UNWIND_STOP _URC_END_OF_STACK |
| 159 | #define UNWIND_CONTINUE _URC_OK |
| 160 | #else |
| 161 | #define UNWIND_STOP _URC_NORMAL_STOP |
| 162 | #define UNWIND_CONTINUE _URC_NO_REASON |
| 163 | #endif |
| 164 | |
| 165 | // ----------------------- AsanStackTrace ----------------------------- {{{1 |
| 166 | uintptr_t AsanStackTrace::GetCurrentPc() { |
| 167 | return GET_CALLER_PC(); |
| 168 | } |
| 169 | |
| 170 | void AsanStackTrace::FastUnwindStack(uintptr_t pc, uintptr_t bp) { |
| 171 | CHECK(size == 0 && trace[0] == pc); |
| 172 | size = 1; |
| 173 | if (!asan_inited) return; |
| 174 | AsanThread *t = asanThreadRegistry().GetCurrent(); |
| 175 | if (!t) return; |
| 176 | uintptr_t *frame = (uintptr_t*)bp; |
| 177 | uintptr_t *prev_frame = frame; |
| 178 | uintptr_t *top = (uintptr_t*)t->stack_top(); |
| 179 | uintptr_t *bottom = (uintptr_t*)t->stack_bottom(); |
| 180 | while (frame >= prev_frame && |
| 181 | frame < top && |
| 182 | frame > bottom && |
| 183 | size < max_size) { |
| 184 | uintptr_t pc1 = frame[1]; |
| 185 | if (pc1 != pc) { |
| 186 | trace[size++] = pc1; |
| 187 | } |
| 188 | prev_frame = frame; |
| 189 | frame = (uintptr_t*)frame[0]; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // On 32-bits we don't compress stack traces. |
| 194 | // On 64-bits we compress stack traces: if a given pc differes slightly from |
| 195 | // the previous one, we record a 31-bit offset instead of the full pc. |
| 196 | size_t AsanStackTrace::CompressStack(AsanStackTrace *stack, |
| 197 | uint32_t *compressed, size_t size) { |
| 198 | #if __WORDSIZE == 32 |
| 199 | // Don't compress, just copy. |
| 200 | size_t res = 0; |
| 201 | for (size_t i = 0; i < stack->size && i < size; i++) { |
| 202 | compressed[i] = stack->trace[i]; |
| 203 | res++; |
| 204 | } |
| Kostya Serebryany | 0ffe35c | 2011-12-28 19:55:30 +0000 | [diff] [blame] | 205 | if (stack->size < size) |
| 206 | compressed[stack->size] = 0; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 207 | #else // 64 bits, compress. |
| 208 | uintptr_t prev_pc = 0; |
| 209 | const uintptr_t kMaxOffset = (1ULL << 30) - 1; |
| 210 | uintptr_t c_index = 0; |
| 211 | size_t res = 0; |
| 212 | for (size_t i = 0, n = stack->size; i < n; i++) { |
| 213 | uintptr_t pc = stack->trace[i]; |
| 214 | if (!pc) break; |
| 215 | if ((int64_t)pc < 0) break; |
| 216 | // Printf("C pc[%ld] %lx\n", i, pc); |
| 217 | if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) { |
| 218 | uintptr_t offset = (int64_t)(pc - prev_pc); |
| 219 | offset |= (1U << 31); |
| 220 | if (c_index >= size) break; |
| 221 | // Printf("C co[%ld] offset %lx\n", i, offset); |
| 222 | compressed[c_index++] = offset; |
| 223 | } else { |
| 224 | uintptr_t hi = pc >> 32; |
| 225 | uintptr_t lo = (pc << 32) >> 32; |
| 226 | CHECK((hi & (1 << 31)) == 0); |
| 227 | if (c_index + 1 >= size) break; |
| 228 | // Printf("C co[%ld] hi/lo: %lx %lx\n", c_index, hi, lo); |
| 229 | compressed[c_index++] = hi; |
| 230 | compressed[c_index++] = lo; |
| 231 | } |
| 232 | res++; |
| 233 | prev_pc = pc; |
| 234 | } |
| Kostya Serebryany | 0ffe35c | 2011-12-28 19:55:30 +0000 | [diff] [blame] | 235 | if (c_index < size) |
| 236 | compressed[c_index] = 0; |
| 237 | if (c_index + 1 < size) |
| 238 | compressed[c_index + 1] = 0; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 239 | #endif // __WORDSIZE |
| 240 | |
| 241 | // debug-only code |
| 242 | #if 0 |
| 243 | AsanStackTrace check_stack; |
| 244 | UncompressStack(&check_stack, compressed, size); |
| 245 | if (res < check_stack.size) { |
| 246 | Printf("res %ld check_stack.size %ld; c_size %ld\n", res, |
| 247 | check_stack.size, size); |
| 248 | } |
| 249 | // |res| may be greater than check_stack.size, because |
| 250 | // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames. |
| 251 | CHECK(res >= check_stack.size); |
| Kostya Serebryany | 52fb238 | 2011-12-28 18:56:42 +0000 | [diff] [blame] | 252 | CHECK(0 == real_memcmp(check_stack.trace, stack->trace, |
| 253 | check_stack.size * sizeof(uintptr_t))); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 254 | #endif |
| 255 | |
| 256 | return res; |
| 257 | } |
| 258 | |
| 259 | void AsanStackTrace::UncompressStack(AsanStackTrace *stack, |
| 260 | uint32_t *compressed, size_t size) { |
| 261 | #if __WORDSIZE == 32 |
| 262 | // Don't uncompress, just copy. |
| 263 | stack->size = 0; |
| 264 | for (size_t i = 0; i < size && i < kStackTraceMax; i++) { |
| 265 | if (!compressed[i]) break; |
| 266 | stack->size++; |
| 267 | stack->trace[i] = compressed[i]; |
| 268 | } |
| 269 | #else // 64 bits, uncompress |
| 270 | uintptr_t prev_pc = 0; |
| 271 | stack->size = 0; |
| 272 | for (size_t i = 0; i < size && stack->size < kStackTraceMax; i++) { |
| 273 | uint32_t x = compressed[i]; |
| 274 | uintptr_t pc = 0; |
| 275 | if (x & (1U << 31)) { |
| 276 | // Printf("U co[%ld] offset: %x\n", i, x); |
| 277 | // this is an offset |
| 278 | int32_t offset = x; |
| 279 | offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend. |
| 280 | pc = prev_pc + offset; |
| 281 | CHECK(pc); |
| 282 | } else { |
| 283 | // CHECK(i + 1 < size); |
| 284 | if (i + 1 >= size) break; |
| 285 | uintptr_t hi = x; |
| 286 | uintptr_t lo = compressed[i+1]; |
| 287 | // Printf("U co[%ld] hi/lo: %lx %lx\n", i, hi, lo); |
| 288 | i++; |
| 289 | pc = (hi << 32) | lo; |
| 290 | if (!pc) break; |
| 291 | } |
| 292 | // Printf("U pc[%ld] %lx\n", stack->size, pc); |
| 293 | stack->trace[stack->size++] = pc; |
| 294 | prev_pc = pc; |
| 295 | } |
| 296 | #endif // __WORDSIZE |
| 297 | } |
| 298 | |
| 299 | } // namespace __asan |