Alexey Samsonov | ae4d9ca | 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 | //===----------------------------------------------------------------------===// |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | |
| 15 | #include "sanitizer_platform.h" |
| 16 | #if SANITIZER_LINUX |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 17 | |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 18 | #include "sanitizer_common.h" |
Alexey Samsonov | 94b5036 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 19 | #include "sanitizer_internal_defs.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 20 | #include "sanitizer_libc.h" |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 21 | #include "sanitizer_linux.h" |
Alexander Potapenko | 93da8b6 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 22 | #include "sanitizer_mutex.h" |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 23 | #include "sanitizer_placement_new.h" |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 24 | #include "sanitizer_procmaps.h" |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 25 | #include "sanitizer_stacktrace.h" |
Alexander Potapenko | 5ce93fc | 2013-05-23 11:53:36 +0000 | [diff] [blame^] | 26 | #include "sanitizer_symbolizer.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 27 | |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 28 | #include <asm/param.h> |
Evgeniy Stepanov | b114ed8 | 2013-03-13 08:19:53 +0000 | [diff] [blame] | 29 | #include <dlfcn.h> |
Alexey Samsonov | 35a7faf | 2013-02-27 13:03:35 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 32 | #include <pthread.h> |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 33 | #include <sched.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 34 | #include <sys/mman.h> |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 35 | #include <sys/ptrace.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 36 | #include <sys/resource.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 37 | #include <sys/stat.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 38 | #include <sys/syscall.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 39 | #include <sys/time.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | #include <unistd.h> |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 42 | #include <unwind.h> |
Alexey Samsonov | 35a7faf | 2013-02-27 13:03:35 +0000 | [diff] [blame] | 43 | |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 44 | #if !SANITIZER_ANDROID |
Alexey Samsonov | 35a7faf | 2013-02-27 13:03:35 +0000 | [diff] [blame] | 45 | #include <sys/signal.h> |
| 46 | #endif |
Dmitry Vyukov | fa5c41e | 2013-01-30 14:39:27 +0000 | [diff] [blame] | 47 | |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 48 | // <linux/time.h> |
| 49 | struct kernel_timeval { |
| 50 | long tv_sec; |
| 51 | long tv_usec; |
| 52 | }; |
| 53 | |
Dmitry Vyukov | fa5c41e | 2013-01-30 14:39:27 +0000 | [diff] [blame] | 54 | // <linux/futex.h> is broken on some linux distributions. |
| 55 | const int FUTEX_WAIT = 0; |
| 56 | const int FUTEX_WAKE = 1; |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 57 | |
Kostya Serebryany | 9d0dbba | 2012-11-19 07:53:36 +0000 | [diff] [blame] | 58 | // Are we using 32-bit or 64-bit syscalls? |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 59 | // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32 |
Kostya Serebryany | 08bfe49 | 2012-11-20 08:57:26 +0000 | [diff] [blame] | 60 | // but it still needs to use 64-bit syscalls. |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 61 | #if defined(__x86_64__) || SANITIZER_WORDSIZE == 64 |
Kostya Serebryany | 9d0dbba | 2012-11-19 07:53:36 +0000 | [diff] [blame] | 62 | # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1 |
| 63 | #else |
| 64 | # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0 |
| 65 | #endif |
| 66 | |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 67 | namespace __sanitizer { |
| 68 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 69 | #ifdef __x86_64__ |
| 70 | #include "sanitizer_syscall_linux_x86_64.inc" |
| 71 | #else |
| 72 | #include "sanitizer_syscall_generic.inc" |
| 73 | #endif |
| 74 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 75 | // --------------- sanitizer_libc.h |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 76 | uptr internal_mmap(void *addr, uptr length, int prot, int flags, |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 77 | int fd, u64 offset) { |
Kostya Serebryany | 9d0dbba | 2012-11-19 07:53:36 +0000 | [diff] [blame] | 78 | #if SANITIZER_LINUX_USES_64BIT_SYSCALLS |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 79 | return internal_syscall(__NR_mmap, addr, length, prot, flags, fd, offset); |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 80 | #else |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 81 | return internal_syscall(__NR_mmap2, addr, length, prot, flags, fd, offset); |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 82 | #endif |
| 83 | } |
| 84 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 85 | uptr internal_munmap(void *addr, uptr length) { |
| 86 | return internal_syscall(__NR_munmap, addr, length); |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 89 | uptr internal_close(fd_t fd) { |
| 90 | return internal_syscall(__NR_close, fd); |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 93 | uptr internal_open(const char *filename, int flags) { |
| 94 | return internal_syscall(__NR_open, filename, flags); |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 97 | uptr internal_open(const char *filename, int flags, u32 mode) { |
| 98 | return internal_syscall(__NR_open, filename, flags, mode); |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 101 | uptr OpenFile(const char *filename, bool write) { |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 102 | return internal_open(filename, |
Kostya Serebryany | 9b8a9c1 | 2012-06-06 14:11:31 +0000 | [diff] [blame] | 103 | write ? O_WRONLY | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660); |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 106 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
Evgeniy Stepanov | 3334e12 | 2012-10-02 13:41:40 +0000 | [diff] [blame] | 107 | sptr res; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 108 | HANDLE_EINTR(res, (sptr)internal_syscall(__NR_read, fd, buf, count)); |
Evgeniy Stepanov | 3334e12 | 2012-10-02 13:41:40 +0000 | [diff] [blame] | 109 | return res; |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
Evgeniy Stepanov | 3334e12 | 2012-10-02 13:41:40 +0000 | [diff] [blame] | 113 | sptr res; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 114 | HANDLE_EINTR(res, (sptr)internal_syscall(__NR_write, fd, buf, count)); |
Evgeniy Stepanov | 3334e12 | 2012-10-02 13:41:40 +0000 | [diff] [blame] | 115 | return res; |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 118 | #if !SANITIZER_LINUX_USES_64BIT_SYSCALLS |
| 119 | static void stat64_to_stat(struct stat64 *in, struct stat *out) { |
| 120 | internal_memset(out, 0, sizeof(*out)); |
| 121 | out->st_dev = in->st_dev; |
| 122 | out->st_ino = in->st_ino; |
| 123 | out->st_mode = in->st_mode; |
| 124 | out->st_nlink = in->st_nlink; |
| 125 | out->st_uid = in->st_uid; |
| 126 | out->st_gid = in->st_gid; |
| 127 | out->st_rdev = in->st_rdev; |
| 128 | out->st_size = in->st_size; |
| 129 | out->st_blksize = in->st_blksize; |
| 130 | out->st_blocks = in->st_blocks; |
| 131 | out->st_atime = in->st_atime; |
| 132 | out->st_mtime = in->st_mtime; |
| 133 | out->st_ctime = in->st_ctime; |
| 134 | out->st_ino = in->st_ino; |
| 135 | } |
| 136 | #endif |
| 137 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 138 | uptr internal_stat(const char *path, void *buf) { |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 139 | #if SANITIZER_LINUX_USES_64BIT_SYSCALLS |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 140 | return internal_syscall(__NR_stat, path, buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 141 | #else |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 142 | struct stat64 buf64; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 143 | int res = internal_syscall(__NR_stat64, path, &buf64); |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 144 | stat64_to_stat(&buf64, (struct stat *)buf); |
| 145 | return res; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 146 | #endif |
| 147 | } |
| 148 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 149 | uptr internal_lstat(const char *path, void *buf) { |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 150 | #if SANITIZER_LINUX_USES_64BIT_SYSCALLS |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 151 | return internal_syscall(__NR_lstat, path, buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 152 | #else |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 153 | struct stat64 buf64; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 154 | int res = internal_syscall(__NR_lstat64, path, &buf64); |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 155 | stat64_to_stat(&buf64, (struct stat *)buf); |
| 156 | return res; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 157 | #endif |
| 158 | } |
| 159 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 160 | uptr internal_fstat(fd_t fd, void *buf) { |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 161 | #if SANITIZER_LINUX_USES_64BIT_SYSCALLS |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 162 | return internal_syscall(__NR_fstat, fd, buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 163 | #else |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 164 | struct stat64 buf64; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 165 | int res = internal_syscall(__NR_fstat64, fd, &buf64); |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 166 | stat64_to_stat(&buf64, (struct stat *)buf); |
| 167 | return res; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 168 | #endif |
| 169 | } |
| 170 | |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 171 | uptr internal_filesize(fd_t fd) { |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 172 | struct stat st; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 173 | if (internal_fstat(fd, &st)) |
| 174 | return -1; |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 175 | return (uptr)st.st_size; |
| 176 | } |
| 177 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 178 | uptr internal_dup2(int oldfd, int newfd) { |
| 179 | return internal_syscall(__NR_dup2, oldfd, newfd); |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 182 | uptr internal_readlink(const char *path, char *buf, uptr bufsize) { |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 183 | return internal_syscall(__NR_readlink, path, buf, bufsize); |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 186 | uptr internal_unlink(const char *path) { |
| 187 | return internal_syscall(__NR_unlink, path); |
Dmitry Vyukov | 6d6ab9e | 2013-03-20 10:28:36 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 190 | uptr internal_sched_yield() { |
| 191 | return internal_syscall(__NR_sched_yield); |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 194 | void internal__exit(int exitcode) { |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 195 | internal_syscall(__NR_exit_group, exitcode); |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 196 | Die(); // Unreachable. |
| 197 | } |
| 198 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 199 | uptr internal_execve(const char *filename, char *const argv[], |
| 200 | char *const envp[]) { |
| 201 | return internal_syscall(__NR_execve, filename, argv, envp); |
| 202 | } |
| 203 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 204 | // ----------------- sanitizer_common.h |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 205 | bool FileExists(const char *filename) { |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 206 | struct stat st; |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 207 | if (internal_stat(filename, &st)) |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 208 | return false; |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 209 | // Sanity check: filename is a regular file. |
| 210 | return S_ISREG(st.st_mode); |
| 211 | } |
| 212 | |
Dmitry Vyukov | e0023f7 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 213 | uptr GetTid() { |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 214 | return internal_syscall(__NR_gettid); |
Dmitry Vyukov | e0023f7 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 217 | u64 NanoTime() { |
| 218 | kernel_timeval tv; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 219 | internal_syscall(__NR_gettimeofday, &tv, 0); |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 220 | return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000; |
| 221 | } |
| 222 | |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 223 | // Like getenv, but reads env directly from /proc and does not use libc. |
| 224 | // This function should be called first inside __asan_init. |
| 225 | const char *GetEnv(const char *name) { |
| 226 | static char *environ; |
| 227 | static uptr len; |
| 228 | static bool inited; |
| 229 | if (!inited) { |
| 230 | inited = true; |
| 231 | uptr environ_size; |
| 232 | len = ReadFileToBuffer("/proc/self/environ", |
| 233 | &environ, &environ_size, 1 << 26); |
| 234 | } |
| 235 | if (!environ || len == 0) return 0; |
| 236 | uptr namelen = internal_strlen(name); |
| 237 | const char *p = environ; |
| 238 | while (*p != '\0') { // will happen at the \0\0 that terminates the buffer |
| 239 | // proc file has the format NAME=value\0NAME=value\0NAME=value\0... |
| 240 | const char* endp = |
| 241 | (char*)internal_memchr(p, '\0', len - (p - environ)); |
| 242 | if (endp == 0) // this entry isn't NUL terminated |
| 243 | return 0; |
| 244 | else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match. |
| 245 | return p + namelen + 1; // point after = |
| 246 | p = endp + 1; |
| 247 | } |
| 248 | return 0; // Not found. |
| 249 | } |
| 250 | |
Evgeniy Stepanov | eab0611 | 2013-02-14 14:40:03 +0000 | [diff] [blame] | 251 | extern "C" { |
Peter Collingbourne | 26337b6 | 2013-05-20 14:25:32 +0000 | [diff] [blame] | 252 | extern void *__libc_stack_end SANITIZER_WEAK_ATTRIBUTE; |
Evgeniy Stepanov | eab0611 | 2013-02-14 14:40:03 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Peter Collingbourne | 26337b6 | 2013-05-20 14:25:32 +0000 | [diff] [blame] | 255 | #if !SANITIZER_GO |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 256 | static void ReadNullSepFileToArray(const char *path, char ***arr, |
| 257 | int arr_size) { |
| 258 | char *buff; |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 259 | uptr buff_size = 0; |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 260 | *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray"); |
| 261 | ReadFileToBuffer(path, &buff, &buff_size, 1024 * 1024); |
| 262 | (*arr)[0] = buff; |
| 263 | int count, i; |
| 264 | for (count = 1, i = 1; ; i++) { |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 265 | if (buff[i] == 0) { |
| 266 | if (buff[i+1] == 0) break; |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 267 | (*arr)[count] = &buff[i+1]; |
| 268 | CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible. |
| 269 | count++; |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 272 | (*arr)[count] = 0; |
| 273 | } |
Peter Collingbourne | 26337b6 | 2013-05-20 14:25:32 +0000 | [diff] [blame] | 274 | #endif |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 275 | |
Peter Collingbourne | 26337b6 | 2013-05-20 14:25:32 +0000 | [diff] [blame] | 276 | static void GetArgsAndEnv(char*** argv, char*** envp) { |
| 277 | #if !SANITIZER_GO |
| 278 | if (&__libc_stack_end) { |
| 279 | #endif |
| 280 | uptr* stack_end = (uptr*)__libc_stack_end; |
| 281 | int argc = *stack_end; |
| 282 | *argv = (char**)(stack_end + 1); |
| 283 | *envp = (char**)(stack_end + argc + 2); |
| 284 | #if !SANITIZER_GO |
| 285 | } else { |
| 286 | static const int kMaxArgv = 2000, kMaxEnvp = 2000; |
| 287 | ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv); |
| 288 | ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp); |
| 289 | } |
| 290 | #endif |
Evgeniy Stepanov | eab0611 | 2013-02-14 14:40:03 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Evgeniy Stepanov | eab0611 | 2013-02-14 14:40:03 +0000 | [diff] [blame] | 293 | void ReExec() { |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 294 | char **argv, **envp; |
Evgeniy Stepanov | eab0611 | 2013-02-14 14:40:03 +0000 | [diff] [blame] | 295 | GetArgsAndEnv(&argv, &envp); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 296 | uptr rv = internal_execve("/proc/self/exe", argv, envp); |
| 297 | int rverrno; |
| 298 | CHECK_EQ(internal_iserror(rv, &rverrno), true); |
| 299 | Printf("execve failed, errno %d\n", rverrno); |
Evgeniy Stepanov | f35eae8 | 2013-02-19 11:09:29 +0000 | [diff] [blame] | 300 | Die(); |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Alexander Potapenko | 2574257 | 2012-12-10 13:10:40 +0000 | [diff] [blame] | 303 | void PrepareForSandboxing() { |
| 304 | // Some kinds of sandboxes may forbid filesystem access, so we won't be able |
| 305 | // to read the file mappings from /proc/self/maps. Luckily, neither the |
| 306 | // process will be able to load additional libraries, so it's fine to use the |
| 307 | // cached mappings. |
| 308 | MemoryMappingLayout::CacheMemoryMappings(); |
Alexander Potapenko | 5ce93fc | 2013-05-23 11:53:36 +0000 | [diff] [blame^] | 309 | // Same for /proc/self/exe in the symbolizer. |
| 310 | SymbolizerPrepareForSandboxing(); |
Alexander Potapenko | 2574257 | 2012-12-10 13:10:40 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 313 | // ----------------- sanitizer_procmaps.h |
Dmitry Vyukov | 286dd3f | 2012-12-05 10:16:02 +0000 | [diff] [blame] | 314 | // Linker initialized. |
| 315 | ProcSelfMapsBuff MemoryMappingLayout::cached_proc_self_maps_; |
Alexander Potapenko | ad91267 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 316 | StaticSpinMutex MemoryMappingLayout::cache_lock_; // Linker initialized. |
Alexander Potapenko | 93da8b6 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 317 | |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 318 | MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) { |
Alexander Potapenko | ad91267 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 319 | proc_self_maps_.len = |
| 320 | ReadFileToBuffer("/proc/self/maps", &proc_self_maps_.data, |
| 321 | &proc_self_maps_.mmaped_size, 1 << 26); |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 322 | if (cache_enabled) { |
| 323 | if (proc_self_maps_.mmaped_size == 0) { |
| 324 | LoadFromCache(); |
| 325 | CHECK_GT(proc_self_maps_.len, 0); |
| 326 | } |
| 327 | } else { |
| 328 | CHECK_GT(proc_self_maps_.mmaped_size, 0); |
Alexander Potapenko | 93da8b6 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 329 | } |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 330 | Reset(); |
Alexander Potapenko | 93da8b6 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 331 | // FIXME: in the future we may want to cache the mappings on demand only. |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 332 | if (cache_enabled) |
| 333 | CacheMemoryMappings(); |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Alexey Samsonov | e1f5dac | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 336 | MemoryMappingLayout::~MemoryMappingLayout() { |
Alexander Potapenko | 7385f8b | 2012-12-04 23:30:00 +0000 | [diff] [blame] | 337 | // Only unmap the buffer if it is different from the cached one. Otherwise |
| 338 | // it will be unmapped when the cache is refreshed. |
| 339 | if (proc_self_maps_.data != cached_proc_self_maps_.data) { |
| 340 | UnmapOrDie(proc_self_maps_.data, proc_self_maps_.mmaped_size); |
| 341 | } |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Alexey Samsonov | e1f5dac | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 344 | void MemoryMappingLayout::Reset() { |
Alexander Potapenko | ad91267 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 345 | current_ = proc_self_maps_.data; |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Alexander Potapenko | 93da8b6 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 348 | // static |
| 349 | void MemoryMappingLayout::CacheMemoryMappings() { |
| 350 | SpinMutexLock l(&cache_lock_); |
| 351 | // Don't invalidate the cache if the mappings are unavailable. |
Alexander Potapenko | ad91267 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 352 | ProcSelfMapsBuff old_proc_self_maps; |
| 353 | old_proc_self_maps = cached_proc_self_maps_; |
| 354 | cached_proc_self_maps_.len = |
| 355 | ReadFileToBuffer("/proc/self/maps", &cached_proc_self_maps_.data, |
| 356 | &cached_proc_self_maps_.mmaped_size, 1 << 26); |
| 357 | if (cached_proc_self_maps_.mmaped_size == 0) { |
| 358 | cached_proc_self_maps_ = old_proc_self_maps; |
Alexander Potapenko | 93da8b6 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 359 | } else { |
Alexander Potapenko | ad91267 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 360 | if (old_proc_self_maps.mmaped_size) { |
| 361 | UnmapOrDie(old_proc_self_maps.data, |
| 362 | old_proc_self_maps.mmaped_size); |
Alexander Potapenko | 93da8b6 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void MemoryMappingLayout::LoadFromCache() { |
| 368 | SpinMutexLock l(&cache_lock_); |
Alexander Potapenko | ad91267 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 369 | if (cached_proc_self_maps_.data) { |
Dmitry Vyukov | 286dd3f | 2012-12-05 10:16:02 +0000 | [diff] [blame] | 370 | proc_self_maps_ = cached_proc_self_maps_; |
Alexander Potapenko | 93da8b6 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Kostya Serebryany | a4e4744 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 374 | // Parse a hex value in str and update str. |
| 375 | static uptr ParseHex(char **str) { |
| 376 | uptr x = 0; |
| 377 | char *s; |
| 378 | for (s = *str; ; s++) { |
| 379 | char c = *s; |
| 380 | uptr v = 0; |
| 381 | if (c >= '0' && c <= '9') |
| 382 | v = c - '0'; |
| 383 | else if (c >= 'a' && c <= 'f') |
| 384 | v = c - 'a' + 10; |
| 385 | else if (c >= 'A' && c <= 'F') |
| 386 | v = c - 'A' + 10; |
| 387 | else |
| 388 | break; |
| 389 | x = x * 16 + v; |
| 390 | } |
| 391 | *str = s; |
| 392 | return x; |
| 393 | } |
| 394 | |
Alexey Samsonov | ab11e0b | 2013-03-13 06:55:02 +0000 | [diff] [blame] | 395 | static bool IsOneOf(char c, char c1, char c2) { |
Kostya Serebryany | a4e4744 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 396 | return c == c1 || c == c2; |
| 397 | } |
| 398 | |
| 399 | static bool IsDecimal(char c) { |
| 400 | return c >= '0' && c <= '9'; |
| 401 | } |
| 402 | |
Alexey Samsonov | e1f5dac | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 403 | bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset, |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 404 | char filename[], uptr filename_size, |
| 405 | uptr *protection) { |
Alexander Potapenko | ad91267 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 406 | char *last = proc_self_maps_.data + proc_self_maps_.len; |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 407 | if (current_ >= last) return false; |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 408 | uptr dummy; |
| 409 | if (!start) start = &dummy; |
| 410 | if (!end) end = &dummy; |
| 411 | if (!offset) offset = &dummy; |
| 412 | char *next_line = (char*)internal_memchr(current_, '\n', last - current_); |
| 413 | if (next_line == 0) |
| 414 | next_line = last; |
Kostya Serebryany | a4e4744 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 415 | // Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar |
| 416 | *start = ParseHex(¤t_); |
Kostya Serebryany | bb8a951 | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 417 | CHECK_EQ(*current_++, '-'); |
Kostya Serebryany | a4e4744 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 418 | *end = ParseHex(¤t_); |
Kostya Serebryany | bb8a951 | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 419 | CHECK_EQ(*current_++, ' '); |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 420 | uptr local_protection = 0; |
Alexey Samsonov | ab11e0b | 2013-03-13 06:55:02 +0000 | [diff] [blame] | 421 | CHECK(IsOneOf(*current_, '-', 'r')); |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 422 | if (*current_++ == 'r') |
| 423 | local_protection |= kProtectionRead; |
Alexey Samsonov | ab11e0b | 2013-03-13 06:55:02 +0000 | [diff] [blame] | 424 | CHECK(IsOneOf(*current_, '-', 'w')); |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 425 | if (*current_++ == 'w') |
| 426 | local_protection |= kProtectionWrite; |
Alexey Samsonov | ab11e0b | 2013-03-13 06:55:02 +0000 | [diff] [blame] | 427 | CHECK(IsOneOf(*current_, '-', 'x')); |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 428 | if (*current_++ == 'x') |
| 429 | local_protection |= kProtectionExecute; |
Alexey Samsonov | ab11e0b | 2013-03-13 06:55:02 +0000 | [diff] [blame] | 430 | CHECK(IsOneOf(*current_, 's', 'p')); |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 431 | if (*current_++ == 's') |
| 432 | local_protection |= kProtectionShared; |
| 433 | if (protection) { |
| 434 | *protection = local_protection; |
| 435 | } |
Kostya Serebryany | bb8a951 | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 436 | CHECK_EQ(*current_++, ' '); |
Kostya Serebryany | a4e4744 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 437 | *offset = ParseHex(¤t_); |
Kostya Serebryany | bb8a951 | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 438 | CHECK_EQ(*current_++, ' '); |
Kostya Serebryany | a4e4744 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 439 | ParseHex(¤t_); |
Kostya Serebryany | bb8a951 | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 440 | CHECK_EQ(*current_++, ':'); |
Kostya Serebryany | a4e4744 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 441 | ParseHex(¤t_); |
Kostya Serebryany | bb8a951 | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 442 | CHECK_EQ(*current_++, ' '); |
Kostya Serebryany | a4e4744 | 2012-06-29 13:05:36 +0000 | [diff] [blame] | 443 | while (IsDecimal(*current_)) |
| 444 | current_++; |
Kostya Serebryany | bb8a951 | 2012-06-29 14:14:32 +0000 | [diff] [blame] | 445 | CHECK_EQ(*current_++, ' '); |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 446 | // Skip spaces. |
| 447 | while (current_ < next_line && *current_ == ' ') |
| 448 | current_++; |
| 449 | // Fill in the filename. |
| 450 | uptr i = 0; |
| 451 | while (current_ < next_line) { |
| 452 | if (filename && i < filename_size - 1) |
| 453 | filename[i++] = *current_; |
| 454 | current_++; |
| 455 | } |
| 456 | if (filename && i < filename_size) |
| 457 | filename[i] = 0; |
| 458 | current_ = next_line + 1; |
| 459 | return true; |
| 460 | } |
| 461 | |
Alexey Samsonov | e1f5dac | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 462 | // Gets the object name and the offset by walking MemoryMappingLayout. |
| 463 | bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset, |
| 464 | char filename[], |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 465 | uptr filename_size, |
| 466 | uptr *protection) { |
| 467 | return IterateForObjectNameAndOffset(addr, offset, filename, filename_size, |
| 468 | protection); |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 471 | enum MutexState { |
| 472 | MtxUnlocked = 0, |
| 473 | MtxLocked = 1, |
| 474 | MtxSleeping = 2 |
| 475 | }; |
| 476 | |
| 477 | BlockingMutex::BlockingMutex(LinkerInitialized) { |
Dmitry Vyukov | d164ed1 | 2013-01-14 08:21:34 +0000 | [diff] [blame] | 478 | CHECK_EQ(owner_, 0); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Alexey Samsonov | 93af594 | 2013-03-14 13:30:56 +0000 | [diff] [blame] | 481 | BlockingMutex::BlockingMutex() { |
| 482 | internal_memset(this, 0, sizeof(*this)); |
| 483 | } |
| 484 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 485 | void BlockingMutex::Lock() { |
| 486 | atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_); |
| 487 | if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked) |
| 488 | return; |
| 489 | while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 490 | internal_syscall(__NR_futex, m, FUTEX_WAIT, MtxSleeping, 0, 0, 0); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void BlockingMutex::Unlock() { |
| 494 | atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_); |
| 495 | u32 v = atomic_exchange(m, MtxUnlocked, memory_order_relaxed); |
Dmitry Vyukov | 4852601 | 2013-01-14 08:01:58 +0000 | [diff] [blame] | 496 | CHECK_NE(v, MtxUnlocked); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 497 | if (v == MtxSleeping) |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 498 | internal_syscall(__NR_futex, m, FUTEX_WAKE, 1, 0, 0, 0); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Alexey Samsonov | ce70097 | 2013-03-11 15:45:20 +0000 | [diff] [blame] | 501 | void BlockingMutex::CheckLocked() { |
| 502 | atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_); |
| 503 | CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed)); |
| 504 | } |
| 505 | |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 506 | // ----------------- sanitizer_linux.h |
| 507 | // The actual size of this structure is specified by d_reclen. |
| 508 | // Note that getdents64 uses a different structure format. We only provide the |
| 509 | // 32-bit syscall here. |
| 510 | struct linux_dirent { |
| 511 | unsigned long d_ino; |
| 512 | unsigned long d_off; |
| 513 | unsigned short d_reclen; |
| 514 | char d_name[256]; |
| 515 | }; |
| 516 | |
| 517 | // Syscall wrappers. |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 518 | uptr internal_ptrace(int request, int pid, void *addr, void *data) { |
| 519 | return internal_syscall(__NR_ptrace, request, pid, addr, data); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 522 | uptr internal_waitpid(int pid, int *status, int options) { |
| 523 | return internal_syscall(__NR_wait4, pid, status, options, 0 /* rusage */); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 526 | uptr internal_getpid() { |
| 527 | return internal_syscall(__NR_getpid); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 530 | uptr internal_getppid() { |
| 531 | return internal_syscall(__NR_getppid); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 534 | uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) { |
| 535 | return internal_syscall(__NR_getdents, fd, dirp, count); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 538 | uptr internal_lseek(fd_t fd, OFF_T offset, int whence) { |
| 539 | return internal_syscall(__NR_lseek, fd, offset, whence); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 542 | uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) { |
| 543 | return internal_syscall(__NR_prctl, option, arg2, arg3, arg4, arg5); |
| 544 | } |
| 545 | |
| 546 | uptr internal_sigaltstack(const struct sigaltstack *ss, |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 547 | struct sigaltstack *oss) { |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 548 | return internal_syscall(__NR_sigaltstack, ss, oss); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 551 | // ThreadLister implementation. |
| 552 | ThreadLister::ThreadLister(int pid) |
| 553 | : pid_(pid), |
| 554 | descriptor_(-1), |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 555 | buffer_(4096), |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 556 | error_(true), |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 557 | entry_((struct linux_dirent *)buffer_.data()), |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 558 | bytes_read_(0) { |
| 559 | char task_directory_path[80]; |
| 560 | internal_snprintf(task_directory_path, sizeof(task_directory_path), |
| 561 | "/proc/%d/task/", pid); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 562 | uptr openrv = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY); |
| 563 | if (internal_iserror(openrv)) { |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 564 | error_ = true; |
| 565 | Report("Can't open /proc/%d/task for reading.\n", pid); |
| 566 | } else { |
| 567 | error_ = false; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 568 | descriptor_ = openrv; |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | int ThreadLister::GetNextTID() { |
| 573 | int tid = -1; |
| 574 | do { |
| 575 | if (error_) |
| 576 | return -1; |
| 577 | if ((char *)entry_ >= &buffer_[bytes_read_] && !GetDirectoryEntries()) |
| 578 | return -1; |
| 579 | if (entry_->d_ino != 0 && entry_->d_name[0] >= '0' && |
| 580 | entry_->d_name[0] <= '9') { |
| 581 | // Found a valid tid. |
| 582 | tid = (int)internal_atoll(entry_->d_name); |
| 583 | } |
| 584 | entry_ = (struct linux_dirent *)(((char *)entry_) + entry_->d_reclen); |
| 585 | } while (tid < 0); |
| 586 | return tid; |
| 587 | } |
| 588 | |
| 589 | void ThreadLister::Reset() { |
| 590 | if (error_ || descriptor_ < 0) |
| 591 | return; |
| 592 | internal_lseek(descriptor_, 0, SEEK_SET); |
| 593 | } |
| 594 | |
| 595 | ThreadLister::~ThreadLister() { |
| 596 | if (descriptor_ >= 0) |
| 597 | internal_close(descriptor_); |
| 598 | } |
| 599 | |
| 600 | bool ThreadLister::error() { return error_; } |
| 601 | |
| 602 | bool ThreadLister::GetDirectoryEntries() { |
| 603 | CHECK_GE(descriptor_, 0); |
| 604 | CHECK_NE(error_, true); |
| 605 | bytes_read_ = internal_getdents(descriptor_, |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 606 | (struct linux_dirent *)buffer_.data(), |
| 607 | buffer_.size()); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 608 | if (internal_iserror(bytes_read_)) { |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 609 | Report("Can't read directory entries from /proc/%d/task.\n", pid_); |
| 610 | error_ = true; |
| 611 | return false; |
| 612 | } else if (bytes_read_ == 0) { |
| 613 | return false; |
| 614 | } |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 615 | entry_ = (struct linux_dirent *)buffer_.data(); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 616 | return true; |
| 617 | } |
| 618 | |
Peter Collingbourne | 4df343a | 2013-05-20 17:05:29 +0000 | [diff] [blame] | 619 | uptr GetPageSize() { |
Kostya Serebryany | a8bc34e | 2013-05-21 06:15:50 +0000 | [diff] [blame] | 620 | #if defined(__x86_64__) || defined(__i386__) |
Peter Collingbourne | 4df343a | 2013-05-20 17:05:29 +0000 | [diff] [blame] | 621 | return EXEC_PAGESIZE; |
Kostya Serebryany | a8bc34e | 2013-05-21 06:15:50 +0000 | [diff] [blame] | 622 | #else |
| 623 | return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy. |
| 624 | #endif |
Peter Collingbourne | 4df343a | 2013-05-20 17:05:29 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Sergey Matveev | 3de0086 | 2013-05-14 13:24:46 +0000 | [diff] [blame] | 627 | // Match full names of the form /path/to/base_name{-,.}* |
| 628 | bool LibraryNameIs(const char *full_name, const char *base_name) { |
| 629 | const char *name = full_name; |
| 630 | // Strip path. |
| 631 | while (*name != '\0') name++; |
| 632 | while (name > full_name && *name != '/') name--; |
| 633 | if (*name == '/') name++; |
| 634 | uptr base_name_length = internal_strlen(base_name); |
| 635 | if (internal_strncmp(name, base_name, base_name_length)) return false; |
| 636 | return (name[base_name_length] == '-' || name[base_name_length] == '.'); |
| 637 | } |
| 638 | |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 639 | } // namespace __sanitizer |
| 640 | |
Alexey Samsonov | 46f9395 | 2013-04-03 07:24:35 +0000 | [diff] [blame] | 641 | #endif // SANITIZER_LINUX |