Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- asan_linux.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 | // Linux-specific details. |
| 13 | //===----------------------------------------------------------------------===// |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 14 | #ifdef __linux__ |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 15 | |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 16 | #include "asan_interceptors.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 17 | #include "asan_internal.h" |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 +0000 | [diff] [blame] | 18 | #include "asan_lock.h" |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 19 | #include "asan_procmaps.h" |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 20 | #include "asan_thread.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 21 | |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 22 | #include <sys/time.h> |
| 23 | #include <sys/resource.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 24 | #include <sys/mman.h> |
| 25 | #include <sys/syscall.h> |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <fcntl.h> |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 28 | #include <link.h> |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 29 | #include <pthread.h> |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 32 | #include <unwind.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 33 | |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 34 | #ifndef ANDROID |
| 35 | // FIXME: where to get ucontext on Android? |
| 36 | #include <sys/ucontext.h> |
| 37 | #endif |
| 38 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 39 | namespace __asan { |
| 40 | |
| 41 | void *AsanDoesNotSupportStaticLinkage() { |
| 42 | // This will fail to link with -static. |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 43 | return &_DYNAMIC; // defined in link.h |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Alexander Potapenko | 720aaef | 2012-02-13 17:09:40 +0000 | [diff] [blame^] | 46 | bool AsanShadowRangeIsAvailable() { |
| 47 | // FIXME: shall we need anything here on Linux? |
| 48 | return true; |
| 49 | } |
| 50 | |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 51 | void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) { |
| 52 | #ifdef ANDROID |
| 53 | *pc = *sp = *bp = 0; |
| 54 | #elif defined(__arm__) |
| 55 | ucontext_t *ucontext = (ucontext_t*)context; |
| 56 | *pc = ucontext->uc_mcontext.arm_pc; |
| 57 | *bp = ucontext->uc_mcontext.arm_fp; |
| 58 | *sp = ucontext->uc_mcontext.arm_sp; |
| 59 | # elif defined(__x86_64__) |
| 60 | ucontext_t *ucontext = (ucontext_t*)context; |
| 61 | *pc = ucontext->uc_mcontext.gregs[REG_RIP]; |
| 62 | *bp = ucontext->uc_mcontext.gregs[REG_RBP]; |
| 63 | *sp = ucontext->uc_mcontext.gregs[REG_RSP]; |
| 64 | # elif defined(__i386__) |
| 65 | ucontext_t *ucontext = (ucontext_t*)context; |
| 66 | *pc = ucontext->uc_mcontext.gregs[REG_EIP]; |
| 67 | *bp = ucontext->uc_mcontext.gregs[REG_EBP]; |
| 68 | *sp = ucontext->uc_mcontext.gregs[REG_ESP]; |
| 69 | #else |
| 70 | # error "Unsupported arch" |
| 71 | #endif |
| 72 | } |
| 73 | |
Kostya Serebryany | 9fd01e5 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 74 | bool AsanInterceptsSignal(int signum) { |
| 75 | return signum == SIGSEGV && FLAG_handle_segv; |
| 76 | } |
| 77 | |
Kostya Serebryany | a772096 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 78 | static void *asan_mmap(void *addr, size_t length, int prot, int flags, |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 79 | int fd, uint64_t offset) { |
| 80 | # if __WORDSIZE == 64 |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 81 | return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 82 | # else |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 83 | return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 84 | # endif |
| 85 | } |
| 86 | |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 87 | void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) { |
| 88 | size = RoundUpTo(size, kPageSize); |
| 89 | void *res = asan_mmap(0, size, |
| 90 | PROT_READ | PROT_WRITE, |
| 91 | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 92 | if (res == (void*)-1) { |
| 93 | OutOfMemoryMessageAndDie(mem_type, size); |
| 94 | } |
| 95 | return res; |
| 96 | } |
| 97 | |
Kostya Serebryany | a772096 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 98 | void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) { |
| 99 | return asan_mmap((void*)fixed_addr, size, |
| 100 | PROT_READ | PROT_WRITE, |
| 101 | MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, |
| 102 | 0, 0); |
| 103 | } |
| 104 | |
Kostya Serebryany | a772096 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 105 | void *AsanMprotect(uintptr_t fixed_addr, size_t size) { |
| 106 | return asan_mmap((void*)fixed_addr, size, |
| 107 | PROT_NONE, |
| 108 | MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, |
| 109 | 0, 0); |
| 110 | } |
| 111 | |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 112 | void AsanUnmapOrDie(void *addr, size_t size) { |
| 113 | if (!addr || !size) return; |
| 114 | int res = syscall(__NR_munmap, addr, size); |
| 115 | if (res != 0) { |
| 116 | Report("Failed to unmap\n"); |
Kostya Serebryany | edb4a8a | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 117 | AsanDie(); |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Kostya Serebryany | edb4a8a | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 121 | size_t AsanWrite(int fd, const void *buf, size_t count) { |
| 122 | return (size_t)syscall(__NR_write, fd, buf, count); |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | int AsanOpenReadonly(const char* filename) { |
Kostya Serebryany | 546ba36 | 2012-02-06 19:23:38 +0000 | [diff] [blame] | 126 | return syscall(__NR_open, filename, O_RDONLY); |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Alexander Potapenko | 553c208 | 2012-01-13 12:59:48 +0000 | [diff] [blame] | 129 | // Like getenv, but reads env directly from /proc and does not use libc. |
| 130 | // This function should be called first inside __asan_init. |
| 131 | const char* AsanGetEnv(const char* name) { |
| 132 | static char *environ; |
| 133 | static size_t len; |
| 134 | static bool inited; |
| 135 | if (!inited) { |
| 136 | inited = true; |
| 137 | size_t environ_size; |
| 138 | len = ReadFileToBuffer("/proc/self/environ", |
Kostya Serebryany | 614b53d | 2012-02-07 00:47:35 +0000 | [diff] [blame] | 139 | &environ, &environ_size, 1 << 26); |
Alexander Potapenko | 553c208 | 2012-01-13 12:59:48 +0000 | [diff] [blame] | 140 | } |
| 141 | if (!environ || len == 0) return NULL; |
| 142 | size_t namelen = internal_strlen(name); |
| 143 | const char *p = environ; |
| 144 | while (*p != '\0') { // will happen at the \0\0 that terminates the buffer |
| 145 | // proc file has the format NAME=value\0NAME=value\0NAME=value\0... |
| 146 | const char* endp = |
| 147 | (char*)internal_memchr(p, '\0', len - (p - environ)); |
| 148 | if (endp == NULL) // this entry isn't NUL terminated |
| 149 | return NULL; |
| 150 | else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match. |
| 151 | return p + namelen + 1; // point after = |
| 152 | p = endp + 1; |
| 153 | } |
| 154 | return NULL; // Not found. |
| 155 | } |
| 156 | |
Kostya Serebryany | edb4a8a | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 157 | size_t AsanRead(int fd, void *buf, size_t count) { |
| 158 | return (size_t)syscall(__NR_read, fd, buf, count); |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | int AsanClose(int fd) { |
Kostya Serebryany | 546ba36 | 2012-02-06 19:23:38 +0000 | [diff] [blame] | 162 | return syscall(__NR_close, fd); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 165 | AsanProcMaps::AsanProcMaps() { |
| 166 | proc_self_maps_buff_len_ = |
| 167 | ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_, |
Kostya Serebryany | 614b53d | 2012-02-07 00:47:35 +0000 | [diff] [blame] | 168 | &proc_self_maps_buff_mmaped_size_, 1 << 26); |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 169 | CHECK(proc_self_maps_buff_len_ > 0); |
| 170 | // AsanWrite(2, proc_self_maps_buff_, proc_self_maps_buff_len_); |
| 171 | Reset(); |
| 172 | } |
| 173 | |
| 174 | AsanProcMaps::~AsanProcMaps() { |
| 175 | AsanUnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_); |
| 176 | } |
| 177 | |
| 178 | void AsanProcMaps::Reset() { |
| 179 | current_ = proc_self_maps_buff_; |
| 180 | } |
| 181 | |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 182 | bool AsanProcMaps::Next(uintptr_t *start, uintptr_t *end, |
| 183 | uintptr_t *offset, char filename[], |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 184 | size_t filename_size) { |
| 185 | char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_; |
| 186 | if (current_ >= last) return false; |
| 187 | int consumed = 0; |
| 188 | char flags[10]; |
| 189 | int major, minor; |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 190 | uintptr_t inode; |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 191 | char *next_line = (char*)internal_memchr(current_, '\n', last - current_); |
| 192 | if (next_line == NULL) |
| 193 | next_line = last; |
| 194 | if (SScanf(current_, |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 195 | "%lx-%lx %4s %lx %x:%x %ld %n", |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 196 | start, end, flags, offset, &major, &minor, |
| 197 | &inode, &consumed) != 7) |
| 198 | return false; |
| 199 | current_ += consumed; |
| 200 | // Skip spaces. |
| 201 | while (current_ < next_line && *current_ == ' ') |
| 202 | current_++; |
| 203 | // Fill in the filename. |
| 204 | size_t i = 0; |
| 205 | while (current_ < next_line) { |
| 206 | if (filename && i < filename_size - 1) |
| 207 | filename[i++] = *current_; |
| 208 | current_++; |
| 209 | } |
| 210 | if (filename && i < filename_size) |
| 211 | filename[i] = 0; |
| 212 | current_ = next_line + 1; |
| 213 | return true; |
| 214 | } |
| 215 | |
Evgeniy Stepanov | 1b65b17 | 2012-01-16 12:45:07 +0000 | [diff] [blame] | 216 | #ifdef __arm__ |
| 217 | |
| 218 | // Gets the object name and the offset by walking AsanProcMaps. |
| 219 | bool AsanProcMaps::GetObjectNameAndOffset(uintptr_t addr, uintptr_t *offset, |
| 220 | char filename[], |
| 221 | size_t filename_size) { |
Alexander Potapenko | 4257386 | 2012-01-18 11:16:05 +0000 | [diff] [blame] | 222 | return IterateForObjectNameAndOffset(addr, offset, filename, filename_size); |
Evgeniy Stepanov | 1b65b17 | 2012-01-16 12:45:07 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Alexey Samsonov | 209c514 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 225 | #else // __arm__ |
Evgeniy Stepanov | 1b65b17 | 2012-01-16 12:45:07 +0000 | [diff] [blame] | 226 | |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 227 | struct DlIterateData { |
| 228 | int count; |
| 229 | uintptr_t addr; |
| 230 | uintptr_t offset; |
| 231 | char *filename; |
| 232 | size_t filename_size; |
| 233 | }; |
| 234 | |
| 235 | static int dl_iterate_phdr_callback(struct dl_phdr_info *info, |
| 236 | size_t size, void *raw_data) { |
| 237 | DlIterateData *data = (DlIterateData*)raw_data; |
| 238 | int count = data->count++; |
| 239 | if (info->dlpi_addr > data->addr) |
| 240 | return 0; |
| 241 | if (count == 0) { |
| 242 | // The first item (the main executable) does not have a so name, |
| 243 | // but we can just read it from /proc/self/exe. |
Kostya Serebryany | edb4a8a | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 244 | size_t path_len = readlink("/proc/self/exe", |
| 245 | data->filename, data->filename_size - 1); |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 246 | data->filename[path_len] = 0; |
| 247 | } else { |
| 248 | CHECK(info->dlpi_name); |
Alexey Samsonov | e725478 | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 249 | REAL(strncpy)(data->filename, info->dlpi_name, data->filename_size); |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 250 | } |
| 251 | data->offset = data->addr - info->dlpi_addr; |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | // Gets the object name and the offset using dl_iterate_phdr. |
| 256 | bool AsanProcMaps::GetObjectNameAndOffset(uintptr_t addr, uintptr_t *offset, |
| 257 | char filename[], |
| 258 | size_t filename_size) { |
| 259 | DlIterateData data; |
| 260 | data.count = 0; |
| 261 | data.addr = addr; |
| 262 | data.filename = filename; |
| 263 | data.filename_size = filename_size; |
| 264 | if (dl_iterate_phdr(dl_iterate_phdr_callback, &data)) { |
| 265 | *offset = data.offset; |
| 266 | return true; |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
Alexey Samsonov | 209c514 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 271 | #endif // __arm__ |
Evgeniy Stepanov | 1b65b17 | 2012-01-16 12:45:07 +0000 | [diff] [blame] | 272 | |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 273 | void AsanThread::SetThreadStackTopAndBottom() { |
| 274 | if (tid() == 0) { |
| 275 | // This is the main thread. Libpthread may not be initialized yet. |
| 276 | struct rlimit rl; |
| 277 | CHECK(getrlimit(RLIMIT_STACK, &rl) == 0); |
| 278 | |
| 279 | // Find the mapping that contains a stack variable. |
| 280 | AsanProcMaps proc_maps; |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 281 | uintptr_t start, end, offset; |
| 282 | uintptr_t prev_end = 0; |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 283 | while (proc_maps.Next(&start, &end, &offset, NULL, 0)) { |
| 284 | if ((uintptr_t)&rl < end) |
| 285 | break; |
| 286 | prev_end = end; |
| 287 | } |
| 288 | CHECK((uintptr_t)&rl >= start && (uintptr_t)&rl < end); |
| 289 | |
| 290 | // Get stacksize from rlimit, but clip it so that it does not overlap |
| 291 | // with other mappings. |
| 292 | size_t stacksize = rl.rlim_cur; |
| 293 | if (stacksize > end - prev_end) |
| 294 | stacksize = end - prev_end; |
| 295 | if (stacksize > kMaxThreadStackSize) |
| 296 | stacksize = kMaxThreadStackSize; |
| 297 | stack_top_ = end; |
| 298 | stack_bottom_ = end - stacksize; |
| 299 | CHECK(AddrIsInStack((uintptr_t)&rl)); |
| 300 | return; |
| 301 | } |
| 302 | pthread_attr_t attr; |
| 303 | CHECK(pthread_getattr_np(pthread_self(), &attr) == 0); |
| 304 | size_t stacksize = 0; |
| 305 | void *stackaddr = NULL; |
| 306 | pthread_attr_getstack(&attr, &stackaddr, &stacksize); |
| 307 | pthread_attr_destroy(&attr); |
| 308 | |
| 309 | stack_top_ = (uintptr_t)stackaddr + stacksize; |
| 310 | stack_bottom_ = (uintptr_t)stackaddr; |
| 311 | // When running with unlimited stack size, we still want to set some limit. |
| 312 | // The unlimited stack size is caused by 'ulimit -s unlimited'. |
| 313 | // Also, for some reason, GNU make spawns subrocesses with unlimited stack. |
| 314 | if (stacksize > kMaxThreadStackSize) { |
| 315 | stack_bottom_ = stack_top_ - kMaxThreadStackSize; |
| 316 | } |
| 317 | CHECK(AddrIsInStack((uintptr_t)&attr)); |
| 318 | } |
| 319 | |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 +0000 | [diff] [blame] | 320 | AsanLock::AsanLock(LinkerInitialized) { |
| 321 | // We assume that pthread_mutex_t initialized to all zeroes is a valid |
| 322 | // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers |
| 323 | // a gcc warning: |
| 324 | // extended initializer lists only available with -std=c++0x or -std=gnu++0x |
| 325 | } |
| 326 | |
| 327 | void AsanLock::Lock() { |
| 328 | CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_)); |
| 329 | pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_); |
| 330 | CHECK(!owner_); |
| 331 | owner_ = (uintptr_t)pthread_self(); |
| 332 | } |
| 333 | |
| 334 | void AsanLock::Unlock() { |
| 335 | CHECK(owner_ == (uintptr_t)pthread_self()); |
| 336 | owner_ = 0; |
| 337 | pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_); |
| 338 | } |
| 339 | |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 340 | #ifdef __arm__ |
| 341 | #define UNWIND_STOP _URC_END_OF_STACK |
| 342 | #define UNWIND_CONTINUE _URC_NO_REASON |
| 343 | #else |
| 344 | #define UNWIND_STOP _URC_NORMAL_STOP |
| 345 | #define UNWIND_CONTINUE _URC_NO_REASON |
| 346 | #endif |
| 347 | |
| 348 | uintptr_t Unwind_GetIP(struct _Unwind_Context *ctx) { |
| 349 | #ifdef __arm__ |
| 350 | uintptr_t val; |
| 351 | _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE, |
| 352 | 15 /* r15 = PC */, _UVRSD_UINT32, &val); |
| 353 | CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed"); |
| 354 | // Clear the Thumb bit. |
| 355 | return val & ~(uintptr_t)1; |
| 356 | #else |
| 357 | return _Unwind_GetIP(ctx); |
| 358 | #endif |
| 359 | } |
| 360 | |
| 361 | _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, |
| 362 | void *param) { |
| 363 | AsanStackTrace *b = (AsanStackTrace*)param; |
| 364 | CHECK(b->size < b->max_size); |
| 365 | uintptr_t pc = Unwind_GetIP(ctx); |
| 366 | b->trace[b->size++] = pc; |
| 367 | if (b->size == b->max_size) return UNWIND_STOP; |
| 368 | return UNWIND_CONTINUE; |
| 369 | } |
| 370 | |
| 371 | void AsanStackTrace::GetStackTrace(size_t max_s, uintptr_t pc, uintptr_t bp) { |
| 372 | size = 0; |
| 373 | trace[0] = pc; |
| 374 | if ((max_s) > 1) { |
| 375 | max_size = max_s; |
| 376 | #ifdef __arm__ |
| 377 | _Unwind_Backtrace(Unwind_Trace, this); |
| 378 | #else |
| 379 | FastUnwindStack(pc, bp); |
| 380 | #endif |
| 381 | } |
| 382 | } |
| 383 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 384 | } // namespace __asan |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 385 | |
| 386 | #endif // __linux__ |