Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 1 | //===-- sanitizer_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 shared between AddressSanitizer and ThreadSanitizer |
| 11 | // run-time libraries and implements linux-specific functions from |
| 12 | // sanitizer_libc.h. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #ifdef __linux__ |
| 15 | |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 16 | #include "sanitizer_common.h" |
Alexey Samsonov | 5bbf829 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 17 | #include "sanitizer_internal_defs.h" |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 18 | #include "sanitizer_libc.h" |
Alexey Samsonov | 961276a | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 19 | #include "sanitizer_placement_new.h" |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 20 | #include "sanitizer_procmaps.h" |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 21 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 22 | #include <fcntl.h> |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 23 | #include <pthread.h> |
Alexey Samsonov | 58a3c58 | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 24 | #include <sched.h> |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 25 | #include <sys/mman.h> |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 26 | #include <sys/resource.h> |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 27 | #include <sys/stat.h> |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 28 | #include <sys/syscall.h> |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 29 | #include <sys/time.h> |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | namespace __sanitizer { |
| 34 | |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 35 | // --------------- sanitizer_libc.h |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 36 | void *internal_mmap(void *addr, uptr length, int prot, int flags, |
| 37 | int fd, u64 offset) { |
| 38 | #if __WORDSIZE == 64 |
| 39 | return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); |
| 40 | #else |
| 41 | return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset); |
| 42 | #endif |
| 43 | } |
| 44 | |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 45 | int internal_munmap(void *addr, uptr length) { |
| 46 | return syscall(__NR_munmap, addr, length); |
| 47 | } |
| 48 | |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 49 | int internal_close(fd_t fd) { |
| 50 | return syscall(__NR_close, fd); |
| 51 | } |
| 52 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 53 | fd_t internal_open(const char *filename, bool write) { |
| 54 | return syscall(__NR_open, filename, |
Kostya Serebryany | 64166ca | 2012-06-06 14:11:31 +0000 | [diff] [blame] | 55 | write ? O_WRONLY | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660); |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 58 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
| 59 | return (uptr)syscall(__NR_read, fd, buf, count); |
| 60 | } |
| 61 | |
| 62 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
| 63 | return (uptr)syscall(__NR_write, fd, buf, count); |
| 64 | } |
| 65 | |
Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 66 | uptr internal_filesize(fd_t fd) { |
Alexey Samsonov | 961276a | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 67 | #if __WORDSIZE == 64 |
| 68 | struct stat st; |
Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 69 | if (syscall(__NR_fstat, fd, &st)) |
| 70 | return -1; |
Alexey Samsonov | 961276a | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 71 | #else |
| 72 | struct stat64 st; |
| 73 | if (syscall(__NR_fstat64, fd, &st)) |
| 74 | return -1; |
| 75 | #endif |
Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 76 | return (uptr)st.st_size; |
| 77 | } |
| 78 | |
| 79 | int internal_dup2(int oldfd, int newfd) { |
| 80 | return syscall(__NR_dup2, oldfd, newfd); |
| 81 | } |
| 82 | |
Alexey Samsonov | 58a3c58 | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 83 | int internal_sched_yield() { |
| 84 | return syscall(__NR_sched_yield); |
| 85 | } |
| 86 | |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 87 | // ----------------- sanitizer_common.h |
Alexey Samsonov | cf4d3a0 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 88 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 89 | uptr *stack_bottom) { |
| 90 | static const uptr kMaxThreadStackSize = 256 * (1 << 20); // 256M |
| 91 | CHECK(stack_top); |
| 92 | CHECK(stack_bottom); |
Alexey Samsonov | cf4d3a0 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 93 | if (at_initialization) { |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 94 | // This is the main thread. Libpthread may not be initialized yet. |
| 95 | struct rlimit rl; |
Kostya Serebryany | 98390d0 | 2012-06-20 15:19:17 +0000 | [diff] [blame] | 96 | CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0); |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 97 | |
| 98 | // Find the mapping that contains a stack variable. |
| 99 | ProcessMaps proc_maps; |
| 100 | uptr start, end, offset; |
| 101 | uptr prev_end = 0; |
| 102 | while (proc_maps.Next(&start, &end, &offset, 0, 0)) { |
| 103 | if ((uptr)&rl < end) |
| 104 | break; |
| 105 | prev_end = end; |
| 106 | } |
| 107 | CHECK((uptr)&rl >= start && (uptr)&rl < end); |
| 108 | |
| 109 | // Get stacksize from rlimit, but clip it so that it does not overlap |
| 110 | // with other mappings. |
| 111 | uptr stacksize = rl.rlim_cur; |
| 112 | if (stacksize > end - prev_end) |
| 113 | stacksize = end - prev_end; |
| 114 | // When running with unlimited stack size, we still want to set some limit. |
| 115 | // The unlimited stack size is caused by 'ulimit -s unlimited'. |
| 116 | // Also, for some reason, GNU make spawns subprocesses with unlimited stack. |
| 117 | if (stacksize > kMaxThreadStackSize) |
| 118 | stacksize = kMaxThreadStackSize; |
| 119 | *stack_top = end; |
| 120 | *stack_bottom = end - stacksize; |
| 121 | return; |
| 122 | } |
| 123 | pthread_attr_t attr; |
Kostya Serebryany | 98390d0 | 2012-06-20 15:19:17 +0000 | [diff] [blame] | 124 | CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0); |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 125 | uptr stacksize = 0; |
| 126 | void *stackaddr = 0; |
| 127 | pthread_attr_getstack(&attr, &stackaddr, (size_t*)&stacksize); |
| 128 | pthread_attr_destroy(&attr); |
| 129 | |
| 130 | *stack_top = (uptr)stackaddr + stacksize; |
| 131 | *stack_bottom = (uptr)stackaddr; |
| 132 | CHECK(stacksize < kMaxThreadStackSize); // Sanity check. |
| 133 | } |
| 134 | |
Alexey Samsonov | 0c53a38 | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 135 | // Like getenv, but reads env directly from /proc and does not use libc. |
| 136 | // This function should be called first inside __asan_init. |
| 137 | const char *GetEnv(const char *name) { |
| 138 | static char *environ; |
| 139 | static uptr len; |
| 140 | static bool inited; |
| 141 | if (!inited) { |
| 142 | inited = true; |
| 143 | uptr environ_size; |
| 144 | len = ReadFileToBuffer("/proc/self/environ", |
| 145 | &environ, &environ_size, 1 << 26); |
| 146 | } |
| 147 | if (!environ || len == 0) return 0; |
| 148 | uptr namelen = internal_strlen(name); |
| 149 | const char *p = environ; |
| 150 | while (*p != '\0') { // will happen at the \0\0 that terminates the buffer |
| 151 | // proc file has the format NAME=value\0NAME=value\0NAME=value\0... |
| 152 | const char* endp = |
| 153 | (char*)internal_memchr(p, '\0', len - (p - environ)); |
| 154 | if (endp == 0) // this entry isn't NUL terminated |
| 155 | return 0; |
| 156 | else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match. |
| 157 | return p + namelen + 1; // point after = |
| 158 | p = endp + 1; |
| 159 | } |
| 160 | return 0; // Not found. |
| 161 | } |
| 162 | |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 163 | // ----------------- sanitizer_procmaps.h |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 164 | ProcessMaps::ProcessMaps() { |
| 165 | proc_self_maps_buff_len_ = |
| 166 | ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_, |
| 167 | &proc_self_maps_buff_mmaped_size_, 1 << 26); |
Kostya Serebryany | 98390d0 | 2012-06-20 15:19:17 +0000 | [diff] [blame] | 168 | CHECK_GT(proc_self_maps_buff_len_, 0); |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 169 | // internal_write(2, proc_self_maps_buff_, proc_self_maps_buff_len_); |
| 170 | Reset(); |
| 171 | } |
| 172 | |
| 173 | ProcessMaps::~ProcessMaps() { |
| 174 | UnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_); |
| 175 | } |
| 176 | |
| 177 | void ProcessMaps::Reset() { |
| 178 | current_ = proc_self_maps_buff_; |
| 179 | } |
| 180 | |
Kostya Serebryany | d32d537 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 181 | // Parse a hex value in str and update str. |
| 182 | static uptr ParseHex(char **str) { |
| 183 | uptr x = 0; |
| 184 | char *s; |
| 185 | for (s = *str; ; s++) { |
| 186 | char c = *s; |
| 187 | uptr v = 0; |
| 188 | if (c >= '0' && c <= '9') |
| 189 | v = c - '0'; |
| 190 | else if (c >= 'a' && c <= 'f') |
| 191 | v = c - 'a' + 10; |
| 192 | else if (c >= 'A' && c <= 'F') |
| 193 | v = c - 'A' + 10; |
| 194 | else |
| 195 | break; |
| 196 | x = x * 16 + v; |
| 197 | } |
| 198 | *str = s; |
| 199 | return x; |
| 200 | } |
| 201 | |
| 202 | static bool IsOnOf(char c, char c1, char c2) { |
| 203 | return c == c1 || c == c2; |
| 204 | } |
| 205 | |
| 206 | static bool IsDecimal(char c) { |
| 207 | return c >= '0' && c <= '9'; |
| 208 | } |
| 209 | |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 210 | bool ProcessMaps::Next(uptr *start, uptr *end, uptr *offset, |
| 211 | char filename[], uptr filename_size) { |
| 212 | char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_; |
| 213 | if (current_ >= last) return false; |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 214 | uptr dummy; |
| 215 | if (!start) start = &dummy; |
| 216 | if (!end) end = &dummy; |
| 217 | if (!offset) offset = &dummy; |
| 218 | char *next_line = (char*)internal_memchr(current_, '\n', last - current_); |
| 219 | if (next_line == 0) |
| 220 | next_line = last; |
Kostya Serebryany | d32d537 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 221 | // Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar |
| 222 | *start = ParseHex(¤t_); |
Kostya Serebryany | 5766a9e | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 223 | CHECK_EQ(*current_++, '-'); |
Kostya Serebryany | d32d537 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 224 | *end = ParseHex(¤t_); |
Kostya Serebryany | 5766a9e | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 225 | CHECK_EQ(*current_++, ' '); |
Kostya Serebryany | d32d537 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 226 | CHECK(IsOnOf(*current_++, '-', 'r')); |
| 227 | CHECK(IsOnOf(*current_++, '-', 'w')); |
| 228 | CHECK(IsOnOf(*current_++, '-', 'x')); |
| 229 | CHECK(IsOnOf(*current_++, 's', 'p')); |
Kostya Serebryany | 5766a9e | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 230 | CHECK_EQ(*current_++, ' '); |
Kostya Serebryany | d32d537 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 231 | *offset = ParseHex(¤t_); |
Kostya Serebryany | 5766a9e | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 232 | CHECK_EQ(*current_++, ' '); |
Kostya Serebryany | d32d537 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 233 | ParseHex(¤t_); |
Kostya Serebryany | 5766a9e | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 234 | CHECK_EQ(*current_++, ':'); |
Kostya Serebryany | d32d537 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 235 | ParseHex(¤t_); |
Kostya Serebryany | 5766a9e | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 236 | CHECK_EQ(*current_++, ' '); |
Kostya Serebryany | d32d537 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 237 | while (IsDecimal(*current_)) |
| 238 | current_++; |
Kostya Serebryany | 5766a9e | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 239 | CHECK_EQ(*current_++, ' '); |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 240 | // Skip spaces. |
| 241 | while (current_ < next_line && *current_ == ' ') |
| 242 | current_++; |
| 243 | // Fill in the filename. |
| 244 | uptr i = 0; |
| 245 | while (current_ < next_line) { |
| 246 | if (filename && i < filename_size - 1) |
| 247 | filename[i++] = *current_; |
| 248 | current_++; |
| 249 | } |
| 250 | if (filename && i < filename_size) |
| 251 | filename[i] = 0; |
| 252 | current_ = next_line + 1; |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | // Gets the object name and the offset by walking ProcessMaps. |
| 257 | bool ProcessMaps::GetObjectNameAndOffset(uptr addr, uptr *offset, |
| 258 | char filename[], |
| 259 | uptr filename_size) { |
| 260 | return IterateForObjectNameAndOffset(addr, offset, filename, filename_size); |
| 261 | } |
| 262 | |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 263 | } // namespace __sanitizer |
| 264 | |
| 265 | #endif // __linux__ |