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