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 | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 18 | #include "asan_procmaps.h" |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 19 | #include "asan_thread.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 20 | |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 21 | #include <sys/time.h> |
| 22 | #include <sys/resource.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 23 | #include <sys/mman.h> |
| 24 | #include <sys/syscall.h> |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <fcntl.h> |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 27 | #include <link.h> |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 28 | #include <pthread.h> |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 30 | #include <unistd.h> |
| 31 | |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 32 | #ifndef ANDROID |
| 33 | // FIXME: where to get ucontext on Android? |
| 34 | #include <sys/ucontext.h> |
| 35 | #endif |
| 36 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 37 | namespace __asan { |
| 38 | |
| 39 | void *AsanDoesNotSupportStaticLinkage() { |
| 40 | // This will fail to link with -static. |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 41 | return &_DYNAMIC; // defined in link.h |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 44 | void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) { |
| 45 | #ifdef ANDROID |
| 46 | *pc = *sp = *bp = 0; |
| 47 | #elif defined(__arm__) |
| 48 | ucontext_t *ucontext = (ucontext_t*)context; |
| 49 | *pc = ucontext->uc_mcontext.arm_pc; |
| 50 | *bp = ucontext->uc_mcontext.arm_fp; |
| 51 | *sp = ucontext->uc_mcontext.arm_sp; |
| 52 | # elif defined(__x86_64__) |
| 53 | ucontext_t *ucontext = (ucontext_t*)context; |
| 54 | *pc = ucontext->uc_mcontext.gregs[REG_RIP]; |
| 55 | *bp = ucontext->uc_mcontext.gregs[REG_RBP]; |
| 56 | *sp = ucontext->uc_mcontext.gregs[REG_RSP]; |
| 57 | # elif defined(__i386__) |
| 58 | ucontext_t *ucontext = (ucontext_t*)context; |
| 59 | *pc = ucontext->uc_mcontext.gregs[REG_EIP]; |
| 60 | *bp = ucontext->uc_mcontext.gregs[REG_EBP]; |
| 61 | *sp = ucontext->uc_mcontext.gregs[REG_ESP]; |
| 62 | #else |
| 63 | # error "Unsupported arch" |
| 64 | #endif |
| 65 | } |
| 66 | |
Kostya Serebryany | 9fd01e5 | 2012-01-09 18:53:15 +0000 | [diff] [blame^] | 67 | bool AsanInterceptsSignal(int signum) { |
| 68 | return signum == SIGSEGV && FLAG_handle_segv; |
| 69 | } |
| 70 | |
Kostya Serebryany | a772096 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 71 | 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] | 72 | int fd, uint64_t offset) { |
| 73 | # if __WORDSIZE == 64 |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 74 | return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 75 | # else |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 76 | return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 77 | # endif |
| 78 | } |
| 79 | |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 80 | void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) { |
| 81 | size = RoundUpTo(size, kPageSize); |
| 82 | void *res = asan_mmap(0, size, |
| 83 | PROT_READ | PROT_WRITE, |
| 84 | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 85 | if (res == (void*)-1) { |
| 86 | OutOfMemoryMessageAndDie(mem_type, size); |
| 87 | } |
| 88 | return res; |
| 89 | } |
| 90 | |
Kostya Serebryany | a772096 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 91 | void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) { |
| 92 | return asan_mmap((void*)fixed_addr, size, |
| 93 | PROT_READ | PROT_WRITE, |
| 94 | MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, |
| 95 | 0, 0); |
| 96 | } |
| 97 | |
| 98 | void *AsanMmapFixedReserve(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, |
| 102 | 0, 0); |
| 103 | } |
| 104 | |
| 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"); |
| 117 | ASAN_DIE; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | ssize_t AsanWrite(int fd, const void *buf, size_t count) { |
| 122 | return (ssize_t)syscall(__NR_write, fd, buf, count); |
| 123 | } |
| 124 | |
| 125 | int AsanOpenReadonly(const char* filename) { |
| 126 | return open(filename, O_RDONLY); |
| 127 | } |
| 128 | |
| 129 | ssize_t AsanRead(int fd, void *buf, size_t count) { |
| 130 | return (ssize_t)syscall(__NR_read, fd, buf, count); |
| 131 | } |
| 132 | |
| 133 | int AsanClose(int fd) { |
| 134 | return close(fd); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 137 | AsanProcMaps::AsanProcMaps() { |
| 138 | proc_self_maps_buff_len_ = |
| 139 | ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_, |
| 140 | &proc_self_maps_buff_mmaped_size_, 1 << 20); |
| 141 | CHECK(proc_self_maps_buff_len_ > 0); |
| 142 | // AsanWrite(2, proc_self_maps_buff_, proc_self_maps_buff_len_); |
| 143 | Reset(); |
| 144 | } |
| 145 | |
| 146 | AsanProcMaps::~AsanProcMaps() { |
| 147 | AsanUnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_); |
| 148 | } |
| 149 | |
| 150 | void AsanProcMaps::Reset() { |
| 151 | current_ = proc_self_maps_buff_; |
| 152 | } |
| 153 | |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 154 | bool AsanProcMaps::Next(uintptr_t *start, uintptr_t *end, |
| 155 | uintptr_t *offset, char filename[], |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 156 | size_t filename_size) { |
| 157 | char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_; |
| 158 | if (current_ >= last) return false; |
| 159 | int consumed = 0; |
| 160 | char flags[10]; |
| 161 | int major, minor; |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 162 | uintptr_t inode; |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 163 | char *next_line = (char*)internal_memchr(current_, '\n', last - current_); |
| 164 | if (next_line == NULL) |
| 165 | next_line = last; |
| 166 | if (SScanf(current_, |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 167 | "%lx-%lx %4s %lx %x:%x %ld %n", |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 168 | start, end, flags, offset, &major, &minor, |
| 169 | &inode, &consumed) != 7) |
| 170 | return false; |
| 171 | current_ += consumed; |
| 172 | // Skip spaces. |
| 173 | while (current_ < next_line && *current_ == ' ') |
| 174 | current_++; |
| 175 | // Fill in the filename. |
| 176 | size_t i = 0; |
| 177 | while (current_ < next_line) { |
| 178 | if (filename && i < filename_size - 1) |
| 179 | filename[i++] = *current_; |
| 180 | current_++; |
| 181 | } |
| 182 | if (filename && i < filename_size) |
| 183 | filename[i] = 0; |
| 184 | current_ = next_line + 1; |
| 185 | return true; |
| 186 | } |
| 187 | |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 188 | struct DlIterateData { |
| 189 | int count; |
| 190 | uintptr_t addr; |
| 191 | uintptr_t offset; |
| 192 | char *filename; |
| 193 | size_t filename_size; |
| 194 | }; |
| 195 | |
| 196 | static int dl_iterate_phdr_callback(struct dl_phdr_info *info, |
| 197 | size_t size, void *raw_data) { |
| 198 | DlIterateData *data = (DlIterateData*)raw_data; |
| 199 | int count = data->count++; |
| 200 | if (info->dlpi_addr > data->addr) |
| 201 | return 0; |
| 202 | if (count == 0) { |
| 203 | // The first item (the main executable) does not have a so name, |
| 204 | // but we can just read it from /proc/self/exe. |
| 205 | ssize_t path_len = readlink("/proc/self/exe", |
| 206 | data->filename, data->filename_size - 1); |
| 207 | data->filename[path_len] = 0; |
| 208 | } else { |
| 209 | CHECK(info->dlpi_name); |
| 210 | real_strncpy(data->filename, info->dlpi_name, data->filename_size); |
| 211 | } |
| 212 | data->offset = data->addr - info->dlpi_addr; |
| 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | // Gets the object name and the offset using dl_iterate_phdr. |
| 217 | bool AsanProcMaps::GetObjectNameAndOffset(uintptr_t addr, uintptr_t *offset, |
| 218 | char filename[], |
| 219 | size_t filename_size) { |
| 220 | DlIterateData data; |
| 221 | data.count = 0; |
| 222 | data.addr = addr; |
| 223 | data.filename = filename; |
| 224 | data.filename_size = filename_size; |
| 225 | if (dl_iterate_phdr(dl_iterate_phdr_callback, &data)) { |
| 226 | *offset = data.offset; |
| 227 | return true; |
| 228 | } |
| 229 | return false; |
| 230 | } |
| 231 | |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 232 | void AsanThread::SetThreadStackTopAndBottom() { |
| 233 | if (tid() == 0) { |
| 234 | // This is the main thread. Libpthread may not be initialized yet. |
| 235 | struct rlimit rl; |
| 236 | CHECK(getrlimit(RLIMIT_STACK, &rl) == 0); |
| 237 | |
| 238 | // Find the mapping that contains a stack variable. |
| 239 | AsanProcMaps proc_maps; |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 240 | uintptr_t start, end, offset; |
| 241 | uintptr_t prev_end = 0; |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 242 | while (proc_maps.Next(&start, &end, &offset, NULL, 0)) { |
| 243 | if ((uintptr_t)&rl < end) |
| 244 | break; |
| 245 | prev_end = end; |
| 246 | } |
| 247 | CHECK((uintptr_t)&rl >= start && (uintptr_t)&rl < end); |
| 248 | |
| 249 | // Get stacksize from rlimit, but clip it so that it does not overlap |
| 250 | // with other mappings. |
| 251 | size_t stacksize = rl.rlim_cur; |
| 252 | if (stacksize > end - prev_end) |
| 253 | stacksize = end - prev_end; |
| 254 | if (stacksize > kMaxThreadStackSize) |
| 255 | stacksize = kMaxThreadStackSize; |
| 256 | stack_top_ = end; |
| 257 | stack_bottom_ = end - stacksize; |
| 258 | CHECK(AddrIsInStack((uintptr_t)&rl)); |
| 259 | return; |
| 260 | } |
| 261 | pthread_attr_t attr; |
| 262 | CHECK(pthread_getattr_np(pthread_self(), &attr) == 0); |
| 263 | size_t stacksize = 0; |
| 264 | void *stackaddr = NULL; |
| 265 | pthread_attr_getstack(&attr, &stackaddr, &stacksize); |
| 266 | pthread_attr_destroy(&attr); |
| 267 | |
| 268 | stack_top_ = (uintptr_t)stackaddr + stacksize; |
| 269 | stack_bottom_ = (uintptr_t)stackaddr; |
| 270 | // When running with unlimited stack size, we still want to set some limit. |
| 271 | // The unlimited stack size is caused by 'ulimit -s unlimited'. |
| 272 | // Also, for some reason, GNU make spawns subrocesses with unlimited stack. |
| 273 | if (stacksize > kMaxThreadStackSize) { |
| 274 | stack_bottom_ = stack_top_ - kMaxThreadStackSize; |
| 275 | } |
| 276 | CHECK(AddrIsInStack((uintptr_t)&attr)); |
| 277 | } |
| 278 | |
Kostya Serebryany | 2b08718 | 2012-01-06 02:12:25 +0000 | [diff] [blame] | 279 | void AsanDisableCoreDumper() { |
| 280 | struct rlimit nocore; |
| 281 | nocore.rlim_cur = 0; |
| 282 | nocore.rlim_max = 0; |
| 283 | setrlimit(RLIMIT_CORE, &nocore); |
| 284 | } |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 285 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 286 | } // namespace __asan |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 287 | |
| 288 | #endif // __linux__ |