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