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" |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 16 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 17 | #if SANITIZER_FREEBSD || SANITIZER_LINUX |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 18 | |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 19 | #include "sanitizer_common.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 20 | #include "sanitizer_flags.h" |
Alexey Samsonov | 94b5036 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 21 | #include "sanitizer_internal_defs.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 22 | #include "sanitizer_libc.h" |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 23 | #include "sanitizer_linux.h" |
Alexander Potapenko | 93da8b6 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 24 | #include "sanitizer_mutex.h" |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 25 | #include "sanitizer_placement_new.h" |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 26 | #include "sanitizer_procmaps.h" |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 27 | #include "sanitizer_stacktrace.h" |
Alexander Potapenko | 5ce93fc | 2013-05-23 11:53:36 +0000 | [diff] [blame] | 28 | #include "sanitizer_symbolizer.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 29 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 30 | #if !SANITIZER_FREEBSD |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 31 | #include <asm/param.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 32 | #endif |
| 33 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 34 | // For mips64, syscall(__NR_stat) fills the buffer in the 'struct kernel_stat' |
| 35 | // format. Struct kernel_stat is defined as 'struct stat' in asm/stat.h. To |
| 36 | // access stat from asm/stat.h, without conflicting with definition in |
| 37 | // sys/stat.h, we use this trick. |
| 38 | #if defined(__mips64) |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 39 | #include <asm/unistd.h> |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | #define stat kernel_stat |
| 42 | #include <asm/stat.h> |
| 43 | #undef stat |
| 44 | #endif |
| 45 | |
Evgeniy Stepanov | b114ed8 | 2013-03-13 08:19:53 +0000 | [diff] [blame] | 46 | #include <dlfcn.h> |
Alexey Samsonov | 35a7faf | 2013-02-27 13:03:35 +0000 | [diff] [blame] | 47 | #include <errno.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 48 | #include <fcntl.h> |
Peter Collingbourne | 2e75ac9 | 2013-07-29 19:09:49 +0000 | [diff] [blame] | 49 | #include <link.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 50 | #include <pthread.h> |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 51 | #include <sched.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 52 | #include <sys/mman.h> |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 53 | #include <sys/ptrace.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 54 | #include <sys/resource.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 55 | #include <sys/stat.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 56 | #include <sys/syscall.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 57 | #include <sys/time.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 58 | #include <sys/types.h> |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 59 | #include <ucontext.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 60 | #include <unistd.h> |
Alexey Samsonov | 35a7faf | 2013-02-27 13:03:35 +0000 | [diff] [blame] | 61 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 62 | #if SANITIZER_FREEBSD |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 63 | #include <sys/sysctl.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 64 | #include <machine/atomic.h> |
| 65 | extern "C" { |
| 66 | // <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on |
| 67 | // FreeBSD 9.2 and 10.0. |
| 68 | #include <sys/umtx.h> |
| 69 | } |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 70 | extern char **environ; // provided by crt1 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 71 | #endif // SANITIZER_FREEBSD |
| 72 | |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 73 | #if !SANITIZER_ANDROID |
Alexey Samsonov | 35a7faf | 2013-02-27 13:03:35 +0000 | [diff] [blame] | 74 | #include <sys/signal.h> |
| 75 | #endif |
Dmitry Vyukov | fa5c41e | 2013-01-30 14:39:27 +0000 | [diff] [blame] | 76 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 77 | #if SANITIZER_LINUX |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 78 | // <linux/time.h> |
| 79 | struct kernel_timeval { |
| 80 | long tv_sec; |
| 81 | long tv_usec; |
| 82 | }; |
| 83 | |
Dmitry Vyukov | fa5c41e | 2013-01-30 14:39:27 +0000 | [diff] [blame] | 84 | // <linux/futex.h> is broken on some linux distributions. |
| 85 | const int FUTEX_WAIT = 0; |
| 86 | const int FUTEX_WAKE = 1; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 87 | #endif // SANITIZER_LINUX |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 88 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 89 | // Are we using 32-bit or 64-bit Linux syscalls? |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 90 | // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32 |
Kostya Serebryany | 08bfe49 | 2012-11-20 08:57:26 +0000 | [diff] [blame] | 91 | // but it still needs to use 64-bit syscalls. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 92 | #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__powerpc64__) || \ |
| 93 | SANITIZER_WORDSIZE == 64) |
Kostya Serebryany | 9d0dbba | 2012-11-19 07:53:36 +0000 | [diff] [blame] | 94 | # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1 |
| 95 | #else |
| 96 | # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0 |
| 97 | #endif |
| 98 | |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 99 | namespace __sanitizer { |
| 100 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 101 | #if SANITIZER_LINUX && defined(__x86_64__) |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 102 | #include "sanitizer_syscall_linux_x86_64.inc" |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 103 | #elif SANITIZER_LINUX && defined(__aarch64__) |
| 104 | #include "sanitizer_syscall_linux_aarch64.inc" |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 105 | #else |
| 106 | #include "sanitizer_syscall_generic.inc" |
| 107 | #endif |
| 108 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 109 | // --------------- sanitizer_libc.h |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 110 | uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd, |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 111 | OFF_T offset) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 112 | #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS |
| 113 | return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd, |
Kostya Serebryany | e041c60 | 2013-11-06 17:47:39 +0000 | [diff] [blame] | 114 | offset); |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 115 | #else |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 116 | // mmap2 specifies file offset in 4096-byte units. |
| 117 | CHECK(IsAligned(offset, 4096)); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 118 | return internal_syscall(SYSCALL(mmap2), addr, length, prot, flags, fd, |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 119 | offset / 4096); |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 120 | #endif |
| 121 | } |
| 122 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 123 | uptr internal_munmap(void *addr, uptr length) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 124 | return internal_syscall(SYSCALL(munmap), (uptr)addr, length); |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 127 | int internal_mprotect(void *addr, uptr length, int prot) { |
| 128 | return internal_syscall(SYSCALL(mprotect), (uptr)addr, length, prot); |
| 129 | } |
| 130 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 131 | uptr internal_close(fd_t fd) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 132 | return internal_syscall(SYSCALL(close), fd); |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 135 | uptr internal_open(const char *filename, int flags) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 136 | #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
| 137 | return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags); |
| 138 | #else |
| 139 | return internal_syscall(SYSCALL(open), (uptr)filename, flags); |
| 140 | #endif |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 143 | uptr internal_open(const char *filename, int flags, u32 mode) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 144 | #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
| 145 | return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags, |
| 146 | mode); |
| 147 | #else |
| 148 | return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode); |
| 149 | #endif |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 152 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
Evgeniy Stepanov | 3334e12 | 2012-10-02 13:41:40 +0000 | [diff] [blame] | 153 | sptr res; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 154 | HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf, |
| 155 | count)); |
Evgeniy Stepanov | 3334e12 | 2012-10-02 13:41:40 +0000 | [diff] [blame] | 156 | return res; |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
Evgeniy Stepanov | 3334e12 | 2012-10-02 13:41:40 +0000 | [diff] [blame] | 160 | sptr res; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 161 | HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf, |
| 162 | count)); |
Evgeniy Stepanov | 3334e12 | 2012-10-02 13:41:40 +0000 | [diff] [blame] | 163 | return res; |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 166 | uptr internal_ftruncate(fd_t fd, uptr size) { |
| 167 | sptr res; |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 168 | HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(ftruncate), fd, |
| 169 | (OFF_T)size)); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 170 | return res; |
| 171 | } |
| 172 | |
| 173 | #if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && !SANITIZER_FREEBSD |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 174 | static void stat64_to_stat(struct stat64 *in, struct stat *out) { |
| 175 | internal_memset(out, 0, sizeof(*out)); |
| 176 | out->st_dev = in->st_dev; |
| 177 | out->st_ino = in->st_ino; |
| 178 | out->st_mode = in->st_mode; |
| 179 | out->st_nlink = in->st_nlink; |
| 180 | out->st_uid = in->st_uid; |
| 181 | out->st_gid = in->st_gid; |
| 182 | out->st_rdev = in->st_rdev; |
| 183 | out->st_size = in->st_size; |
| 184 | out->st_blksize = in->st_blksize; |
| 185 | out->st_blocks = in->st_blocks; |
| 186 | out->st_atime = in->st_atime; |
| 187 | out->st_mtime = in->st_mtime; |
| 188 | out->st_ctime = in->st_ctime; |
| 189 | out->st_ino = in->st_ino; |
| 190 | } |
| 191 | #endif |
| 192 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 193 | #if defined(__mips64) |
| 194 | static void kernel_stat_to_stat(struct kernel_stat *in, struct stat *out) { |
| 195 | internal_memset(out, 0, sizeof(*out)); |
| 196 | out->st_dev = in->st_dev; |
| 197 | out->st_ino = in->st_ino; |
| 198 | out->st_mode = in->st_mode; |
| 199 | out->st_nlink = in->st_nlink; |
| 200 | out->st_uid = in->st_uid; |
| 201 | out->st_gid = in->st_gid; |
| 202 | out->st_rdev = in->st_rdev; |
| 203 | out->st_size = in->st_size; |
| 204 | out->st_blksize = in->st_blksize; |
| 205 | out->st_blocks = in->st_blocks; |
| 206 | out->st_atime = in->st_atime_nsec; |
| 207 | out->st_mtime = in->st_mtime_nsec; |
| 208 | out->st_ctime = in->st_ctime_nsec; |
| 209 | out->st_ino = in->st_ino; |
| 210 | } |
| 211 | #endif |
| 212 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 213 | uptr internal_stat(const char *path, void *buf) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 214 | #if SANITIZER_FREEBSD |
| 215 | return internal_syscall(SYSCALL(stat), path, buf); |
| 216 | #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
| 217 | return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, |
| 218 | (uptr)buf, 0); |
| 219 | #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 220 | # if defined(__mips64) |
| 221 | // For mips64, stat syscall fills buffer in the format of kernel_stat |
| 222 | struct kernel_stat kbuf; |
| 223 | int res = internal_syscall(SYSCALL(stat), path, &kbuf); |
| 224 | kernel_stat_to_stat(&kbuf, (struct stat *)buf); |
| 225 | return res; |
| 226 | # else |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 227 | return internal_syscall(SYSCALL(stat), (uptr)path, (uptr)buf); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 228 | # endif |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 229 | #else |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 230 | struct stat64 buf64; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 231 | int res = internal_syscall(SYSCALL(stat64), path, &buf64); |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 232 | stat64_to_stat(&buf64, (struct stat *)buf); |
| 233 | return res; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 234 | #endif |
| 235 | } |
| 236 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 237 | uptr internal_lstat(const char *path, void *buf) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 238 | #if SANITIZER_FREEBSD |
| 239 | return internal_syscall(SYSCALL(lstat), path, buf); |
| 240 | #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
| 241 | return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, |
| 242 | (uptr)buf, AT_SYMLINK_NOFOLLOW); |
| 243 | #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS |
| 244 | return internal_syscall(SYSCALL(lstat), (uptr)path, (uptr)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 245 | #else |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 246 | struct stat64 buf64; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 247 | int res = internal_syscall(SYSCALL(lstat64), path, &buf64); |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 248 | stat64_to_stat(&buf64, (struct stat *)buf); |
| 249 | return res; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 250 | #endif |
| 251 | } |
| 252 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 253 | uptr internal_fstat(fd_t fd, void *buf) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 254 | #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS |
| 255 | return internal_syscall(SYSCALL(fstat), fd, (uptr)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 256 | #else |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 257 | struct stat64 buf64; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 258 | int res = internal_syscall(SYSCALL(fstat64), fd, &buf64); |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 259 | stat64_to_stat(&buf64, (struct stat *)buf); |
| 260 | return res; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 261 | #endif |
| 262 | } |
| 263 | |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 264 | uptr internal_filesize(fd_t fd) { |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 265 | struct stat st; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 266 | if (internal_fstat(fd, &st)) |
| 267 | return -1; |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 268 | return (uptr)st.st_size; |
| 269 | } |
| 270 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 271 | uptr internal_dup2(int oldfd, int newfd) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 272 | #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
| 273 | return internal_syscall(SYSCALL(dup3), oldfd, newfd, 0); |
| 274 | #else |
| 275 | return internal_syscall(SYSCALL(dup2), oldfd, newfd); |
| 276 | #endif |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 279 | uptr internal_readlink(const char *path, char *buf, uptr bufsize) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 280 | #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
| 281 | return internal_syscall(SYSCALL(readlinkat), AT_FDCWD, |
| 282 | (uptr)path, (uptr)buf, bufsize); |
| 283 | #else |
| 284 | return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize); |
| 285 | #endif |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 288 | uptr internal_unlink(const char *path) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 289 | #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
| 290 | return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0); |
| 291 | #else |
| 292 | return internal_syscall(SYSCALL(unlink), (uptr)path); |
| 293 | #endif |
| 294 | } |
| 295 | |
| 296 | uptr internal_rename(const char *oldpath, const char *newpath) { |
| 297 | #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
| 298 | return internal_syscall(SYSCALL(renameat), AT_FDCWD, (uptr)oldpath, AT_FDCWD, |
| 299 | (uptr)newpath); |
| 300 | #else |
| 301 | return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath); |
| 302 | #endif |
Dmitry Vyukov | 6d6ab9e | 2013-03-20 10:28:36 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 305 | uptr internal_sched_yield() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 306 | return internal_syscall(SYSCALL(sched_yield)); |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 309 | void internal__exit(int exitcode) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 310 | #if SANITIZER_FREEBSD |
| 311 | internal_syscall(SYSCALL(exit), exitcode); |
| 312 | #else |
| 313 | internal_syscall(SYSCALL(exit_group), exitcode); |
| 314 | #endif |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 315 | Die(); // Unreachable. |
| 316 | } |
| 317 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 318 | uptr internal_execve(const char *filename, char *const argv[], |
| 319 | char *const envp[]) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 320 | return internal_syscall(SYSCALL(execve), (uptr)filename, (uptr)argv, |
| 321 | (uptr)envp); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 324 | // ----------------- sanitizer_common.h |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 325 | bool FileExists(const char *filename) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 326 | struct stat st; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 327 | #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 328 | if (internal_syscall(SYSCALL(newfstatat), AT_FDCWD, filename, &st, 0)) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 329 | #else |
Evgeniy Stepanov | 2be3a28 | 2013-05-07 12:47:04 +0000 | [diff] [blame] | 330 | if (internal_stat(filename, &st)) |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 331 | #endif |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 332 | return false; |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 333 | // Sanity check: filename is a regular file. |
| 334 | return S_ISREG(st.st_mode); |
| 335 | } |
| 336 | |
Dmitry Vyukov | e0023f7 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 337 | uptr GetTid() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 338 | #if SANITIZER_FREEBSD |
| 339 | return (uptr)pthread_self(); |
| 340 | #else |
| 341 | return internal_syscall(SYSCALL(gettid)); |
| 342 | #endif |
Dmitry Vyukov | e0023f7 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 345 | u64 NanoTime() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 346 | #if SANITIZER_FREEBSD |
| 347 | timeval tv; |
| 348 | #else |
Peter Collingbourne | 5e7ba22 | 2013-10-21 18:11:57 +0000 | [diff] [blame] | 349 | kernel_timeval tv; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 350 | #endif |
Peter Collingbourne | 5e7ba22 | 2013-10-21 18:11:57 +0000 | [diff] [blame] | 351 | internal_memset(&tv, 0, sizeof(tv)); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 352 | internal_syscall(SYSCALL(gettimeofday), (uptr)&tv, 0); |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 353 | return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000; |
| 354 | } |
| 355 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 356 | // Like getenv, but reads env directly from /proc (on Linux) or parses the |
| 357 | // 'environ' array (on FreeBSD) and does not use libc. This function should be |
| 358 | // called first inside __asan_init. |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 359 | const char *GetEnv(const char *name) { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 360 | #if SANITIZER_FREEBSD |
| 361 | if (::environ != 0) { |
| 362 | uptr NameLen = internal_strlen(name); |
| 363 | for (char **Env = ::environ; *Env != 0; Env++) { |
| 364 | if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=') |
| 365 | return (*Env) + NameLen + 1; |
| 366 | } |
| 367 | } |
| 368 | return 0; // Not found. |
| 369 | #elif SANITIZER_LINUX |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 370 | static char *environ; |
| 371 | static uptr len; |
| 372 | static bool inited; |
| 373 | if (!inited) { |
| 374 | inited = true; |
| 375 | uptr environ_size; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 376 | if (!ReadFileToBuffer("/proc/self/environ", &environ, &environ_size, &len)) |
| 377 | environ = nullptr; |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 378 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 379 | if (!environ || len == 0) return nullptr; |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 380 | uptr namelen = internal_strlen(name); |
| 381 | const char *p = environ; |
| 382 | while (*p != '\0') { // will happen at the \0\0 that terminates the buffer |
| 383 | // proc file has the format NAME=value\0NAME=value\0NAME=value\0... |
| 384 | const char* endp = |
| 385 | (char*)internal_memchr(p, '\0', len - (p - environ)); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 386 | if (!endp) // this entry isn't NUL terminated |
| 387 | return nullptr; |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 388 | else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match. |
| 389 | return p + namelen + 1; // point after = |
| 390 | p = endp + 1; |
| 391 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 392 | return nullptr; // Not found. |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 393 | #else |
| 394 | #error "Unsupported platform" |
| 395 | #endif |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Evgeniy Stepanov | eab0611 | 2013-02-14 14:40:03 +0000 | [diff] [blame] | 398 | extern "C" { |
Timur Iskhodzhanov | 3c80c6c | 2013-08-13 11:42:45 +0000 | [diff] [blame] | 399 | SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end; |
Evgeniy Stepanov | eab0611 | 2013-02-14 14:40:03 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Peter Collingbourne | 26337b6 | 2013-05-20 14:25:32 +0000 | [diff] [blame] | 402 | #if !SANITIZER_GO |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 403 | static void ReadNullSepFileToArray(const char *path, char ***arr, |
| 404 | int arr_size) { |
| 405 | char *buff; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 406 | uptr buff_size; |
| 407 | uptr buff_len; |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 408 | *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray"); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 409 | if (!ReadFileToBuffer(path, &buff, &buff_size, &buff_len, 1024 * 1024)) { |
| 410 | (*arr)[0] = nullptr; |
| 411 | return; |
| 412 | } |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 413 | (*arr)[0] = buff; |
| 414 | int count, i; |
| 415 | for (count = 1, i = 1; ; i++) { |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 416 | if (buff[i] == 0) { |
| 417 | if (buff[i+1] == 0) break; |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 418 | (*arr)[count] = &buff[i+1]; |
| 419 | CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible. |
| 420 | count++; |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 423 | (*arr)[count] = nullptr; |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 424 | } |
Peter Collingbourne | 26337b6 | 2013-05-20 14:25:32 +0000 | [diff] [blame] | 425 | #endif |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 426 | |
Peter Collingbourne | 26337b6 | 2013-05-20 14:25:32 +0000 | [diff] [blame] | 427 | static void GetArgsAndEnv(char*** argv, char*** envp) { |
| 428 | #if !SANITIZER_GO |
| 429 | if (&__libc_stack_end) { |
| 430 | #endif |
| 431 | uptr* stack_end = (uptr*)__libc_stack_end; |
| 432 | int argc = *stack_end; |
| 433 | *argv = (char**)(stack_end + 1); |
| 434 | *envp = (char**)(stack_end + argc + 2); |
| 435 | #if !SANITIZER_GO |
| 436 | } else { |
| 437 | static const int kMaxArgv = 2000, kMaxEnvp = 2000; |
| 438 | ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv); |
| 439 | ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp); |
| 440 | } |
| 441 | #endif |
Evgeniy Stepanov | eab0611 | 2013-02-14 14:40:03 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Evgeniy Stepanov | eab0611 | 2013-02-14 14:40:03 +0000 | [diff] [blame] | 444 | void ReExec() { |
Peter Collingbourne | 23709c9 | 2013-01-17 19:50:42 +0000 | [diff] [blame] | 445 | char **argv, **envp; |
Evgeniy Stepanov | eab0611 | 2013-02-14 14:40:03 +0000 | [diff] [blame] | 446 | GetArgsAndEnv(&argv, &envp); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 447 | uptr rv = internal_execve("/proc/self/exe", argv, envp); |
| 448 | int rverrno; |
| 449 | CHECK_EQ(internal_iserror(rv, &rverrno), true); |
| 450 | Printf("execve failed, errno %d\n", rverrno); |
Evgeniy Stepanov | f35eae8 | 2013-02-19 11:09:29 +0000 | [diff] [blame] | 451 | Die(); |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 454 | enum MutexState { |
| 455 | MtxUnlocked = 0, |
| 456 | MtxLocked = 1, |
| 457 | MtxSleeping = 2 |
| 458 | }; |
| 459 | |
Alexey Samsonov | 93af594 | 2013-03-14 13:30:56 +0000 | [diff] [blame] | 460 | BlockingMutex::BlockingMutex() { |
| 461 | internal_memset(this, 0, sizeof(*this)); |
| 462 | } |
| 463 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 464 | void BlockingMutex::Lock() { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 465 | CHECK_EQ(owner_, 0); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 466 | atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_); |
| 467 | if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked) |
| 468 | return; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 469 | while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) { |
| 470 | #if SANITIZER_FREEBSD |
| 471 | _umtx_op(m, UMTX_OP_WAIT_UINT, MtxSleeping, 0, 0); |
| 472 | #else |
| 473 | internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAIT, MtxSleeping, 0, 0, 0); |
| 474 | #endif |
| 475 | } |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void BlockingMutex::Unlock() { |
| 479 | atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_); |
| 480 | u32 v = atomic_exchange(m, MtxUnlocked, memory_order_relaxed); |
Dmitry Vyukov | 4852601 | 2013-01-14 08:01:58 +0000 | [diff] [blame] | 481 | CHECK_NE(v, MtxUnlocked); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 482 | if (v == MtxSleeping) { |
| 483 | #if SANITIZER_FREEBSD |
| 484 | _umtx_op(m, UMTX_OP_WAKE, 1, 0, 0); |
| 485 | #else |
| 486 | internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAKE, 1, 0, 0, 0); |
| 487 | #endif |
| 488 | } |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Alexey Samsonov | ce70097 | 2013-03-11 15:45:20 +0000 | [diff] [blame] | 491 | void BlockingMutex::CheckLocked() { |
| 492 | atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_); |
| 493 | CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed)); |
| 494 | } |
| 495 | |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 496 | // ----------------- sanitizer_linux.h |
| 497 | // The actual size of this structure is specified by d_reclen. |
| 498 | // Note that getdents64 uses a different structure format. We only provide the |
| 499 | // 32-bit syscall here. |
| 500 | struct linux_dirent { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 501 | #if SANITIZER_X32 || defined(__aarch64__) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 502 | u64 d_ino; |
| 503 | u64 d_off; |
| 504 | #else |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 505 | unsigned long d_ino; |
| 506 | unsigned long d_off; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 507 | #endif |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 508 | unsigned short d_reclen; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 509 | #ifdef __aarch64__ |
| 510 | unsigned char d_type; |
| 511 | #endif |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 512 | char d_name[256]; |
| 513 | }; |
| 514 | |
| 515 | // Syscall wrappers. |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 516 | uptr internal_ptrace(int request, int pid, void *addr, void *data) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 517 | return internal_syscall(SYSCALL(ptrace), request, pid, (uptr)addr, |
| 518 | (uptr)data); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 521 | uptr internal_waitpid(int pid, int *status, int options) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 522 | return internal_syscall(SYSCALL(wait4), pid, (uptr)status, options, |
Kostya Serebryany | e041c60 | 2013-11-06 17:47:39 +0000 | [diff] [blame] | 523 | 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() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 527 | return internal_syscall(SYSCALL(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() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 531 | return internal_syscall(SYSCALL(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) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 535 | #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
| 536 | return internal_syscall(SYSCALL(getdents64), fd, (uptr)dirp, count); |
| 537 | #else |
| 538 | return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count); |
| 539 | #endif |
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_lseek(fd_t fd, OFF_T offset, int whence) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 543 | return internal_syscall(SYSCALL(lseek), fd, offset, whence); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 546 | #if SANITIZER_LINUX |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 547 | uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 548 | return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 549 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 550 | #endif |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 551 | |
| 552 | uptr internal_sigaltstack(const struct sigaltstack *ss, |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 553 | struct sigaltstack *oss) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 554 | return internal_syscall(SYSCALL(sigaltstack), (uptr)ss, (uptr)oss); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 557 | int internal_fork() { |
| 558 | #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS |
| 559 | return internal_syscall(SYSCALL(clone), SIGCHLD, 0); |
| 560 | #else |
| 561 | return internal_syscall(SYSCALL(fork)); |
| 562 | #endif |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 565 | #if SANITIZER_LINUX |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 566 | #define SA_RESTORER 0x04000000 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 567 | // Doesn't set sa_restorer, use with caution (see below). |
| 568 | int internal_sigaction_norestorer(int signum, const void *act, void *oldact) { |
| 569 | __sanitizer_kernel_sigaction_t k_act, k_oldact; |
| 570 | internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t)); |
| 571 | internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t)); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 572 | const __sanitizer_sigaction *u_act = (const __sanitizer_sigaction *)act; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 573 | __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact; |
| 574 | if (u_act) { |
| 575 | k_act.handler = u_act->handler; |
| 576 | k_act.sigaction = u_act->sigaction; |
| 577 | internal_memcpy(&k_act.sa_mask, &u_act->sa_mask, |
| 578 | sizeof(__sanitizer_kernel_sigset_t)); |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 579 | // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL). |
| 580 | k_act.sa_flags = u_act->sa_flags | SA_RESTORER; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 581 | // FIXME: most often sa_restorer is unset, however the kernel requires it |
| 582 | // to point to a valid signal restorer that calls the rt_sigreturn syscall. |
| 583 | // If sa_restorer passed to the kernel is NULL, the program may crash upon |
| 584 | // signal delivery or fail to unwind the stack in the signal handler. |
| 585 | // libc implementation of sigaction() passes its own restorer to |
| 586 | // rt_sigaction, so we need to do the same (we'll need to reimplement the |
| 587 | // restorers; for x86_64 the restorer address can be obtained from |
| 588 | // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact). |
| 589 | k_act.sa_restorer = u_act->sa_restorer; |
| 590 | } |
| 591 | |
| 592 | uptr result = internal_syscall(SYSCALL(rt_sigaction), (uptr)signum, |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 593 | (uptr)(u_act ? &k_act : nullptr), |
| 594 | (uptr)(u_oldact ? &k_oldact : nullptr), |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 595 | (uptr)sizeof(__sanitizer_kernel_sigset_t)); |
| 596 | |
| 597 | if ((result == 0) && u_oldact) { |
| 598 | u_oldact->handler = k_oldact.handler; |
| 599 | u_oldact->sigaction = k_oldact.sigaction; |
| 600 | internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask, |
| 601 | sizeof(__sanitizer_kernel_sigset_t)); |
| 602 | u_oldact->sa_flags = k_oldact.sa_flags; |
| 603 | u_oldact->sa_restorer = k_oldact.sa_restorer; |
| 604 | } |
| 605 | return result; |
| 606 | } |
| 607 | #endif // SANITIZER_LINUX |
| 608 | |
| 609 | uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set, |
| 610 | __sanitizer_sigset_t *oldset) { |
| 611 | #if SANITIZER_FREEBSD |
| 612 | return internal_syscall(SYSCALL(sigprocmask), how, set, oldset); |
| 613 | #else |
| 614 | __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set; |
| 615 | __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset; |
| 616 | return internal_syscall(SYSCALL(rt_sigprocmask), (uptr)how, |
| 617 | (uptr)&k_set->sig[0], (uptr)&k_oldset->sig[0], |
| 618 | sizeof(__sanitizer_kernel_sigset_t)); |
| 619 | #endif |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 622 | void internal_sigfillset(__sanitizer_sigset_t *set) { |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 623 | internal_memset(set, 0xff, sizeof(*set)); |
| 624 | } |
| 625 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 626 | #if SANITIZER_LINUX |
| 627 | void internal_sigdelset(__sanitizer_sigset_t *set, int signum) { |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 628 | signum -= 1; |
| 629 | CHECK_GE(signum, 0); |
| 630 | CHECK_LT(signum, sizeof(*set) * 8); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 631 | __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set; |
| 632 | const uptr idx = signum / (sizeof(k_set->sig[0]) * 8); |
| 633 | const uptr bit = signum % (sizeof(k_set->sig[0]) * 8); |
| 634 | k_set->sig[idx] &= ~(1 << bit); |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 635 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 636 | #endif // SANITIZER_LINUX |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 637 | |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 638 | // ThreadLister implementation. |
| 639 | ThreadLister::ThreadLister(int pid) |
| 640 | : pid_(pid), |
| 641 | descriptor_(-1), |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 642 | buffer_(4096), |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 643 | error_(true), |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 644 | entry_((struct linux_dirent *)buffer_.data()), |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 645 | bytes_read_(0) { |
| 646 | char task_directory_path[80]; |
| 647 | internal_snprintf(task_directory_path, sizeof(task_directory_path), |
| 648 | "/proc/%d/task/", pid); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 649 | uptr openrv = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY); |
| 650 | if (internal_iserror(openrv)) { |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 651 | error_ = true; |
| 652 | Report("Can't open /proc/%d/task for reading.\n", pid); |
| 653 | } else { |
| 654 | error_ = false; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 655 | descriptor_ = openrv; |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | int ThreadLister::GetNextTID() { |
| 660 | int tid = -1; |
| 661 | do { |
| 662 | if (error_) |
| 663 | return -1; |
| 664 | if ((char *)entry_ >= &buffer_[bytes_read_] && !GetDirectoryEntries()) |
| 665 | return -1; |
| 666 | if (entry_->d_ino != 0 && entry_->d_name[0] >= '0' && |
| 667 | entry_->d_name[0] <= '9') { |
| 668 | // Found a valid tid. |
| 669 | tid = (int)internal_atoll(entry_->d_name); |
| 670 | } |
| 671 | entry_ = (struct linux_dirent *)(((char *)entry_) + entry_->d_reclen); |
| 672 | } while (tid < 0); |
| 673 | return tid; |
| 674 | } |
| 675 | |
| 676 | void ThreadLister::Reset() { |
| 677 | if (error_ || descriptor_ < 0) |
| 678 | return; |
| 679 | internal_lseek(descriptor_, 0, SEEK_SET); |
| 680 | } |
| 681 | |
| 682 | ThreadLister::~ThreadLister() { |
| 683 | if (descriptor_ >= 0) |
| 684 | internal_close(descriptor_); |
| 685 | } |
| 686 | |
| 687 | bool ThreadLister::error() { return error_; } |
| 688 | |
| 689 | bool ThreadLister::GetDirectoryEntries() { |
| 690 | CHECK_GE(descriptor_, 0); |
| 691 | CHECK_NE(error_, true); |
| 692 | bytes_read_ = internal_getdents(descriptor_, |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 693 | (struct linux_dirent *)buffer_.data(), |
| 694 | buffer_.size()); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 695 | if (internal_iserror(bytes_read_)) { |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 696 | Report("Can't read directory entries from /proc/%d/task.\n", pid_); |
| 697 | error_ = true; |
| 698 | return false; |
| 699 | } else if (bytes_read_ == 0) { |
| 700 | return false; |
| 701 | } |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 702 | entry_ = (struct linux_dirent *)buffer_.data(); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 703 | return true; |
| 704 | } |
| 705 | |
Peter Collingbourne | 4df343a | 2013-05-20 17:05:29 +0000 | [diff] [blame] | 706 | uptr GetPageSize() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 707 | #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__)) |
Peter Collingbourne | 4df343a | 2013-05-20 17:05:29 +0000 | [diff] [blame] | 708 | return EXEC_PAGESIZE; |
Kostya Serebryany | a8bc34e | 2013-05-21 06:15:50 +0000 | [diff] [blame] | 709 | #else |
| 710 | return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy. |
| 711 | #endif |
Peter Collingbourne | 4df343a | 2013-05-20 17:05:29 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 714 | uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 715 | #if SANITIZER_FREEBSD |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 716 | const int Mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; |
| 717 | const char *default_module_name = "kern.proc.pathname"; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 718 | size_t Size = buf_len; |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 719 | bool IsErr = (sysctl(Mib, ARRAY_SIZE(Mib), buf, &Size, NULL, 0) != 0); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 720 | int readlink_error = IsErr ? errno : 0; |
| 721 | uptr module_name_len = Size; |
| 722 | #else |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 723 | const char *default_module_name = "/proc/self/exe"; |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 724 | uptr module_name_len = internal_readlink( |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 725 | default_module_name, buf, buf_len); |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 726 | int readlink_error; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 727 | bool IsErr = internal_iserror(module_name_len, &readlink_error); |
| 728 | #endif |
| 729 | if (IsErr) { |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 730 | // We can't read binary name for some reason, assume it's unknown. |
| 731 | Report("WARNING: reading executable name failed with errno %d, " |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 732 | "some stack frames may not be symbolized\n", readlink_error); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 733 | module_name_len = internal_snprintf(buf, buf_len, "%s", |
| 734 | default_module_name); |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 735 | CHECK_LT(module_name_len, buf_len); |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 736 | } |
| 737 | return module_name_len; |
| 738 | } |
| 739 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 740 | uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) { |
| 741 | #if SANITIZER_LINUX |
| 742 | char *tmpbuf; |
| 743 | uptr tmpsize; |
| 744 | uptr tmplen; |
| 745 | if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf, &tmpsize, &tmplen, |
| 746 | 1024 * 1024)) { |
| 747 | internal_strncpy(buf, tmpbuf, buf_len); |
| 748 | UnmapOrDie(tmpbuf, tmpsize); |
| 749 | return internal_strlen(buf); |
| 750 | } |
| 751 | #endif |
| 752 | return ReadBinaryName(buf, buf_len); |
| 753 | } |
| 754 | |
Sergey Matveev | 3de0086 | 2013-05-14 13:24:46 +0000 | [diff] [blame] | 755 | // Match full names of the form /path/to/base_name{-,.}* |
| 756 | bool LibraryNameIs(const char *full_name, const char *base_name) { |
| 757 | const char *name = full_name; |
| 758 | // Strip path. |
| 759 | while (*name != '\0') name++; |
| 760 | while (name > full_name && *name != '/') name--; |
| 761 | if (*name == '/') name++; |
| 762 | uptr base_name_length = internal_strlen(base_name); |
| 763 | if (internal_strncmp(name, base_name, base_name_length)) return false; |
| 764 | return (name[base_name_length] == '-' || name[base_name_length] == '.'); |
| 765 | } |
| 766 | |
Evgeniy Stepanov | d054abe | 2013-07-30 08:39:16 +0000 | [diff] [blame] | 767 | #if !SANITIZER_ANDROID |
Peter Collingbourne | 2e75ac9 | 2013-07-29 19:09:49 +0000 | [diff] [blame] | 768 | // Call cb for each region mapped by map. |
| 769 | void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 770 | CHECK_NE(map, nullptr); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 771 | #if !SANITIZER_FREEBSD |
Peter Collingbourne | 2e75ac9 | 2013-07-29 19:09:49 +0000 | [diff] [blame] | 772 | typedef ElfW(Phdr) Elf_Phdr; |
| 773 | typedef ElfW(Ehdr) Elf_Ehdr; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 774 | #endif // !SANITIZER_FREEBSD |
Peter Collingbourne | 2e75ac9 | 2013-07-29 19:09:49 +0000 | [diff] [blame] | 775 | char *base = (char *)map->l_addr; |
| 776 | Elf_Ehdr *ehdr = (Elf_Ehdr *)base; |
| 777 | char *phdrs = base + ehdr->e_phoff; |
| 778 | char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize; |
| 779 | |
| 780 | // Find the segment with the minimum base so we can "relocate" the p_vaddr |
| 781 | // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC |
| 782 | // objects have a non-zero base. |
Peter Collingbourne | f50b0fc | 2013-07-29 20:13:41 +0000 | [diff] [blame] | 783 | uptr preferred_base = (uptr)-1; |
Peter Collingbourne | 2e75ac9 | 2013-07-29 19:09:49 +0000 | [diff] [blame] | 784 | for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) { |
| 785 | Elf_Phdr *phdr = (Elf_Phdr *)iter; |
| 786 | if (phdr->p_type == PT_LOAD && preferred_base > (uptr)phdr->p_vaddr) |
| 787 | preferred_base = (uptr)phdr->p_vaddr; |
| 788 | } |
| 789 | |
| 790 | // Compute the delta from the real base to get a relocation delta. |
| 791 | sptr delta = (uptr)base - preferred_base; |
| 792 | // Now we can figure out what the loader really mapped. |
| 793 | for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) { |
| 794 | Elf_Phdr *phdr = (Elf_Phdr *)iter; |
| 795 | if (phdr->p_type == PT_LOAD) { |
| 796 | uptr seg_start = phdr->p_vaddr + delta; |
| 797 | uptr seg_end = seg_start + phdr->p_memsz; |
| 798 | // None of these values are aligned. We consider the ragged edges of the |
| 799 | // load command as defined, since they are mapped from the file. |
| 800 | seg_start = RoundDownTo(seg_start, GetPageSizeCached()); |
| 801 | seg_end = RoundUpTo(seg_end, GetPageSizeCached()); |
| 802 | cb((void *)seg_start, seg_end - seg_start); |
| 803 | } |
| 804 | } |
| 805 | } |
Evgeniy Stepanov | d054abe | 2013-07-30 08:39:16 +0000 | [diff] [blame] | 806 | #endif |
Peter Collingbourne | 2e75ac9 | 2013-07-29 19:09:49 +0000 | [diff] [blame] | 807 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 808 | #if defined(__x86_64__) && SANITIZER_LINUX |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 809 | // We cannot use glibc's clone wrapper, because it messes with the child |
| 810 | // task's TLS. It writes the PID and TID of the child task to its thread |
| 811 | // descriptor, but in our case the child task shares the thread descriptor with |
| 812 | // the parent (because we don't know how to allocate a new thread |
| 813 | // descriptor to keep glibc happy). So the stock version of clone(), when |
| 814 | // used with CLONE_VM, would end up corrupting the parent's thread descriptor. |
| 815 | uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, |
| 816 | int *parent_tidptr, void *newtls, int *child_tidptr) { |
| 817 | long long res; |
| 818 | if (!fn || !child_stack) |
| 819 | return -EINVAL; |
| 820 | CHECK_EQ(0, (uptr)child_stack % 16); |
Kostya Serebryany | e041c60 | 2013-11-06 17:47:39 +0000 | [diff] [blame] | 821 | child_stack = (char *)child_stack - 2 * sizeof(unsigned long long); |
| 822 | ((unsigned long long *)child_stack)[0] = (uptr)fn; |
| 823 | ((unsigned long long *)child_stack)[1] = (uptr)arg; |
| 824 | register void *r8 __asm__("r8") = newtls; |
| 825 | register int *r10 __asm__("r10") = child_tidptr; |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 826 | __asm__ __volatile__( |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 827 | /* %rax = syscall(%rax = SYSCALL(clone), |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 828 | * %rdi = flags, |
| 829 | * %rsi = child_stack, |
| 830 | * %rdx = parent_tidptr, |
| 831 | * %r8 = new_tls, |
| 832 | * %r10 = child_tidptr) |
| 833 | */ |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 834 | "syscall\n" |
| 835 | |
| 836 | /* if (%rax != 0) |
| 837 | * return; |
| 838 | */ |
| 839 | "testq %%rax,%%rax\n" |
| 840 | "jnz 1f\n" |
| 841 | |
| 842 | /* In the child. Terminate unwind chain. */ |
Sergey Matveev | 497ae56 | 2013-10-08 16:38:39 +0000 | [diff] [blame] | 843 | // XXX: We should also terminate the CFI unwind chain |
| 844 | // here. Unfortunately clang 3.2 doesn't support the |
| 845 | // necessary CFI directives, so we skip that part. |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 846 | "xorq %%rbp,%%rbp\n" |
| 847 | |
| 848 | /* Call "fn(arg)". */ |
| 849 | "popq %%rax\n" |
| 850 | "popq %%rdi\n" |
| 851 | "call *%%rax\n" |
| 852 | |
| 853 | /* Call _exit(%rax). */ |
| 854 | "movq %%rax,%%rdi\n" |
| 855 | "movq %2,%%rax\n" |
| 856 | "syscall\n" |
| 857 | |
| 858 | /* Return to parent. */ |
| 859 | "1:\n" |
| 860 | : "=a" (res) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 861 | : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 862 | "S"(child_stack), |
| 863 | "D"(flags), |
| 864 | "d"(parent_tidptr), |
Kostya Serebryany | e041c60 | 2013-11-06 17:47:39 +0000 | [diff] [blame] | 865 | "r"(r8), |
| 866 | "r"(r10) |
| 867 | : "rsp", "memory", "r11", "rcx"); |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 868 | return res; |
| 869 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 870 | #elif defined(__mips__) |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 871 | uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, |
| 872 | int *parent_tidptr, void *newtls, int *child_tidptr) { |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 873 | long long res; |
| 874 | if (!fn || !child_stack) |
| 875 | return -EINVAL; |
| 876 | CHECK_EQ(0, (uptr)child_stack % 16); |
| 877 | child_stack = (char *)child_stack - 2 * sizeof(unsigned long long); |
| 878 | ((unsigned long long *)child_stack)[0] = (uptr)fn; |
| 879 | ((unsigned long long *)child_stack)[1] = (uptr)arg; |
| 880 | register void *a3 __asm__("$7") = newtls; |
| 881 | register int *a4 __asm__("$8") = child_tidptr; |
| 882 | // We don't have proper CFI directives here because it requires alot of code |
| 883 | // for very marginal benefits. |
| 884 | __asm__ __volatile__( |
| 885 | /* $v0 = syscall($v0 = __NR_clone, |
| 886 | * $a0 = flags, |
| 887 | * $a1 = child_stack, |
| 888 | * $a2 = parent_tidptr, |
| 889 | * $a3 = new_tls, |
| 890 | * $a4 = child_tidptr) |
| 891 | */ |
| 892 | ".cprestore 16;\n" |
| 893 | "move $4,%1;\n" |
| 894 | "move $5,%2;\n" |
| 895 | "move $6,%3;\n" |
| 896 | "move $7,%4;\n" |
| 897 | /* Store the fifth argument on stack |
| 898 | * if we are using 32-bit abi. |
| 899 | */ |
| 900 | #if SANITIZER_WORDSIZE == 32 |
| 901 | "lw %5,16($29);\n" |
| 902 | #else |
| 903 | "move $8,%5;\n" |
| 904 | #endif |
| 905 | "li $2,%6;\n" |
| 906 | "syscall;\n" |
| 907 | |
| 908 | /* if ($v0 != 0) |
| 909 | * return; |
| 910 | */ |
| 911 | "bnez $2,1f;\n" |
| 912 | |
| 913 | /* Call "fn(arg)". */ |
| 914 | "ld $25,0($29);\n" |
| 915 | "ld $4,8($29);\n" |
| 916 | "jal $25;\n" |
| 917 | |
| 918 | /* Call _exit($v0). */ |
| 919 | "move $4,$2;\n" |
| 920 | "li $2,%7;\n" |
| 921 | "syscall;\n" |
| 922 | |
| 923 | /* Return to parent. */ |
| 924 | "1:\n" |
| 925 | : "=r" (res) |
| 926 | : "r"(flags), |
| 927 | "r"(child_stack), |
| 928 | "r"(parent_tidptr), |
| 929 | "r"(a3), |
| 930 | "r"(a4), |
| 931 | "i"(__NR_clone), |
| 932 | "i"(__NR_exit) |
| 933 | : "memory", "$29" ); |
| 934 | return res; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 935 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 936 | #elif defined(__aarch64__) |
| 937 | uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, |
| 938 | int *parent_tidptr, void *newtls, int *child_tidptr) { |
| 939 | long long res; |
| 940 | if (!fn || !child_stack) |
| 941 | return -EINVAL; |
| 942 | CHECK_EQ(0, (uptr)child_stack % 16); |
| 943 | child_stack = (char *)child_stack - 2 * sizeof(unsigned long long); |
| 944 | ((unsigned long long *)child_stack)[0] = (uptr)fn; |
| 945 | ((unsigned long long *)child_stack)[1] = (uptr)arg; |
| 946 | |
| 947 | register int (*__fn)(void *) __asm__("x0") = fn; |
| 948 | register void *__stack __asm__("x1") = child_stack; |
| 949 | register int __flags __asm__("x2") = flags; |
| 950 | register void *__arg __asm__("x3") = arg; |
| 951 | register int *__ptid __asm__("x4") = parent_tidptr; |
| 952 | register void *__tls __asm__("x5") = newtls; |
| 953 | register int *__ctid __asm__("x6") = child_tidptr; |
| 954 | |
| 955 | __asm__ __volatile__( |
| 956 | "mov x0,x2\n" /* flags */ |
| 957 | "mov x2,x4\n" /* ptid */ |
| 958 | "mov x3,x5\n" /* tls */ |
| 959 | "mov x4,x6\n" /* ctid */ |
| 960 | "mov x8,%9\n" /* clone */ |
| 961 | |
| 962 | "svc 0x0\n" |
| 963 | |
| 964 | /* if (%r0 != 0) |
| 965 | * return %r0; |
| 966 | */ |
| 967 | "cmp x0, #0\n" |
| 968 | "bne 1f\n" |
| 969 | |
| 970 | /* In the child, now. Call "fn(arg)". */ |
| 971 | "ldp x1, x0, [sp], #16\n" |
| 972 | "blr x1\n" |
| 973 | |
| 974 | /* Call _exit(%r0). */ |
| 975 | "mov x8, %10\n" |
| 976 | "svc 0x0\n" |
| 977 | "1:\n" |
| 978 | |
| 979 | : "=r" (res) |
| 980 | : "i"(-EINVAL), |
| 981 | "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg), |
| 982 | "r"(__ptid), "r"(__tls), "r"(__ctid), |
| 983 | "i"(__NR_clone), "i"(__NR_exit) |
| 984 | : "x30", "memory"); |
| 985 | return res; |
| 986 | } |
| 987 | #elif defined(__powerpc64__) |
| 988 | uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, |
| 989 | int *parent_tidptr, void *newtls, int *child_tidptr) { |
| 990 | long long res; |
| 991 | /* Stack frame offsets. */ |
| 992 | #if _CALL_ELF != 2 |
| 993 | #define FRAME_MIN_SIZE 112 |
| 994 | #define FRAME_TOC_SAVE 40 |
| 995 | #else |
| 996 | #define FRAME_MIN_SIZE 32 |
| 997 | #define FRAME_TOC_SAVE 24 |
| 998 | #endif |
| 999 | if (!fn || !child_stack) |
| 1000 | return -EINVAL; |
| 1001 | CHECK_EQ(0, (uptr)child_stack % 16); |
| 1002 | child_stack = (char *)child_stack - 2 * sizeof(unsigned long long); |
| 1003 | ((unsigned long long *)child_stack)[0] = (uptr)fn; |
| 1004 | ((unsigned long long *)child_stack)[1] = (uptr)arg; |
| 1005 | |
| 1006 | register int (*__fn)(void *) __asm__("r3") = fn; |
| 1007 | register void *__cstack __asm__("r4") = child_stack; |
| 1008 | register int __flags __asm__("r5") = flags; |
| 1009 | register void * __arg __asm__("r6") = arg; |
| 1010 | register int * __ptidptr __asm__("r7") = parent_tidptr; |
| 1011 | register void * __newtls __asm__("r8") = newtls; |
| 1012 | register int * __ctidptr __asm__("r9") = child_tidptr; |
| 1013 | |
| 1014 | __asm__ __volatile__( |
| 1015 | /* fn, arg, child_stack are saved acrVoss the syscall */ |
| 1016 | "mr 28, %5\n\t" |
| 1017 | "mr 29, %6\n\t" |
| 1018 | "mr 27, %8\n\t" |
| 1019 | |
| 1020 | /* syscall |
| 1021 | r3 == flags |
| 1022 | r4 == child_stack |
| 1023 | r5 == parent_tidptr |
| 1024 | r6 == newtls |
| 1025 | r7 == child_tidptr */ |
| 1026 | "mr 3, %7\n\t" |
| 1027 | "mr 5, %9\n\t" |
| 1028 | "mr 6, %10\n\t" |
| 1029 | "mr 7, %11\n\t" |
| 1030 | "li 0, %3\n\t" |
| 1031 | "sc\n\t" |
| 1032 | |
| 1033 | /* Test if syscall was successful */ |
| 1034 | "cmpdi cr1, 3, 0\n\t" |
| 1035 | "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t" |
| 1036 | "bne- cr1, 1f\n\t" |
| 1037 | |
| 1038 | /* Do the function call */ |
| 1039 | "std 2, %13(1)\n\t" |
| 1040 | #if _CALL_ELF != 2 |
| 1041 | "ld 0, 0(28)\n\t" |
| 1042 | "ld 2, 8(28)\n\t" |
| 1043 | "mtctr 0\n\t" |
| 1044 | #else |
| 1045 | "mr 12, 28\n\t" |
| 1046 | "mtctr 12\n\t" |
| 1047 | #endif |
| 1048 | "mr 3, 27\n\t" |
| 1049 | "bctrl\n\t" |
| 1050 | "ld 2, %13(1)\n\t" |
| 1051 | |
| 1052 | /* Call _exit(r3) */ |
| 1053 | "li 0, %4\n\t" |
| 1054 | "sc\n\t" |
| 1055 | |
| 1056 | /* Return to parent */ |
| 1057 | "1:\n\t" |
| 1058 | "mr %0, 3\n\t" |
| 1059 | : "=r" (res) |
| 1060 | : "0" (-1), "i" (EINVAL), |
| 1061 | "i" (__NR_clone), "i" (__NR_exit), |
| 1062 | "r" (__fn), "r" (__cstack), "r" (__flags), |
| 1063 | "r" (__arg), "r" (__ptidptr), "r" (__newtls), |
| 1064 | "r" (__ctidptr), "i" (FRAME_MIN_SIZE), "i" (FRAME_TOC_SAVE) |
| 1065 | : "cr0", "cr1", "memory", "ctr", |
| 1066 | "r0", "r29", "r27", "r28"); |
| 1067 | return res; |
| 1068 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1069 | #endif // defined(__x86_64__) && SANITIZER_LINUX |
| 1070 | |
| 1071 | #if SANITIZER_ANDROID |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1072 | #if __ANDROID_API__ < 21 |
| 1073 | extern "C" __attribute__((weak)) int dl_iterate_phdr( |
| 1074 | int (*)(struct dl_phdr_info *, size_t, void *), void *); |
| 1075 | #endif |
| 1076 | |
| 1077 | static int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size, |
| 1078 | void *data) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1079 | // Any name starting with "lib" indicates a bug in L where library base names |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1080 | // are returned instead of paths. |
| 1081 | if (info->dlpi_name && info->dlpi_name[0] == 'l' && |
| 1082 | info->dlpi_name[1] == 'i' && info->dlpi_name[2] == 'b') { |
| 1083 | *(bool *)data = true; |
| 1084 | return 1; |
| 1085 | } |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | static atomic_uint32_t android_api_level; |
| 1090 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1091 | static AndroidApiLevel AndroidDetectApiLevel() { |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1092 | if (!&dl_iterate_phdr) |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1093 | return ANDROID_KITKAT; // K or lower |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1094 | bool base_name_seen = false; |
| 1095 | dl_iterate_phdr(dl_iterate_phdr_test_cb, &base_name_seen); |
| 1096 | if (base_name_seen) |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1097 | return ANDROID_LOLLIPOP_MR1; // L MR1 |
| 1098 | return ANDROID_POST_LOLLIPOP; // post-L |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1099 | // Plain L (API level 21) is completely broken wrt ASan and not very |
| 1100 | // interesting to detect. |
| 1101 | } |
| 1102 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1103 | AndroidApiLevel AndroidGetApiLevel() { |
| 1104 | AndroidApiLevel level = |
| 1105 | (AndroidApiLevel)atomic_load(&android_api_level, memory_order_relaxed); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1106 | if (level) return level; |
| 1107 | level = AndroidDetectApiLevel(); |
| 1108 | atomic_store(&android_api_level, level, memory_order_relaxed); |
| 1109 | return level; |
| 1110 | } |
| 1111 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1112 | #endif |
| 1113 | |
| 1114 | bool IsDeadlySignal(int signum) { |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1115 | if (common_flags()->handle_abort && signum == SIGABRT) |
| 1116 | return true; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1117 | if (common_flags()->handle_sigill && signum == SIGILL) |
| 1118 | return true; |
| 1119 | if (common_flags()->handle_sigfpe && signum == SIGFPE) |
| 1120 | return true; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1121 | return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1124 | #ifndef SANITIZER_GO |
| 1125 | void *internal_start_thread(void(*func)(void *arg), void *arg) { |
| 1126 | // Start the thread with signals blocked, otherwise it can steal user signals. |
| 1127 | __sanitizer_sigset_t set, old; |
| 1128 | internal_sigfillset(&set); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1129 | #if SANITIZER_LINUX && !SANITIZER_ANDROID |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1130 | // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked |
| 1131 | // on any thread, setuid call hangs (see test/tsan/setuid.c). |
| 1132 | internal_sigdelset(&set, 33); |
| 1133 | #endif |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1134 | internal_sigprocmask(SIG_SETMASK, &set, &old); |
| 1135 | void *th; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1136 | real_pthread_create(&th, nullptr, (void*(*)(void *arg))func, arg); |
| 1137 | internal_sigprocmask(SIG_SETMASK, &old, nullptr); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1138 | return th; |
| 1139 | } |
| 1140 | |
| 1141 | void internal_join_thread(void *th) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1142 | real_pthread_join(th, nullptr); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1143 | } |
| 1144 | #else |
| 1145 | void *internal_start_thread(void (*func)(void *), void *arg) { return 0; } |
| 1146 | |
| 1147 | void internal_join_thread(void *th) {} |
| 1148 | #endif |
| 1149 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 1150 | void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { |
| 1151 | #if defined(__arm__) |
| 1152 | ucontext_t *ucontext = (ucontext_t*)context; |
| 1153 | *pc = ucontext->uc_mcontext.arm_pc; |
| 1154 | *bp = ucontext->uc_mcontext.arm_fp; |
| 1155 | *sp = ucontext->uc_mcontext.arm_sp; |
| 1156 | #elif defined(__aarch64__) |
| 1157 | ucontext_t *ucontext = (ucontext_t*)context; |
| 1158 | *pc = ucontext->uc_mcontext.pc; |
| 1159 | *bp = ucontext->uc_mcontext.regs[29]; |
| 1160 | *sp = ucontext->uc_mcontext.sp; |
| 1161 | #elif defined(__hppa__) |
| 1162 | ucontext_t *ucontext = (ucontext_t*)context; |
| 1163 | *pc = ucontext->uc_mcontext.sc_iaoq[0]; |
| 1164 | /* GCC uses %r3 whenever a frame pointer is needed. */ |
| 1165 | *bp = ucontext->uc_mcontext.sc_gr[3]; |
| 1166 | *sp = ucontext->uc_mcontext.sc_gr[30]; |
| 1167 | #elif defined(__x86_64__) |
| 1168 | # if SANITIZER_FREEBSD |
| 1169 | ucontext_t *ucontext = (ucontext_t*)context; |
| 1170 | *pc = ucontext->uc_mcontext.mc_rip; |
| 1171 | *bp = ucontext->uc_mcontext.mc_rbp; |
| 1172 | *sp = ucontext->uc_mcontext.mc_rsp; |
| 1173 | # else |
| 1174 | ucontext_t *ucontext = (ucontext_t*)context; |
| 1175 | *pc = ucontext->uc_mcontext.gregs[REG_RIP]; |
| 1176 | *bp = ucontext->uc_mcontext.gregs[REG_RBP]; |
| 1177 | *sp = ucontext->uc_mcontext.gregs[REG_RSP]; |
| 1178 | # endif |
| 1179 | #elif defined(__i386__) |
| 1180 | # if SANITIZER_FREEBSD |
| 1181 | ucontext_t *ucontext = (ucontext_t*)context; |
| 1182 | *pc = ucontext->uc_mcontext.mc_eip; |
| 1183 | *bp = ucontext->uc_mcontext.mc_ebp; |
| 1184 | *sp = ucontext->uc_mcontext.mc_esp; |
| 1185 | # else |
| 1186 | ucontext_t *ucontext = (ucontext_t*)context; |
| 1187 | *pc = ucontext->uc_mcontext.gregs[REG_EIP]; |
| 1188 | *bp = ucontext->uc_mcontext.gregs[REG_EBP]; |
| 1189 | *sp = ucontext->uc_mcontext.gregs[REG_ESP]; |
| 1190 | # endif |
| 1191 | #elif defined(__powerpc__) || defined(__powerpc64__) |
| 1192 | ucontext_t *ucontext = (ucontext_t*)context; |
| 1193 | *pc = ucontext->uc_mcontext.regs->nip; |
| 1194 | *sp = ucontext->uc_mcontext.regs->gpr[PT_R1]; |
| 1195 | // The powerpc{,64}-linux ABIs do not specify r31 as the frame |
| 1196 | // pointer, but GCC always uses r31 when we need a frame pointer. |
| 1197 | *bp = ucontext->uc_mcontext.regs->gpr[PT_R31]; |
| 1198 | #elif defined(__sparc__) |
| 1199 | ucontext_t *ucontext = (ucontext_t*)context; |
| 1200 | uptr *stk_ptr; |
| 1201 | # if defined (__arch64__) |
| 1202 | *pc = ucontext->uc_mcontext.mc_gregs[MC_PC]; |
| 1203 | *sp = ucontext->uc_mcontext.mc_gregs[MC_O6]; |
| 1204 | stk_ptr = (uptr *) (*sp + 2047); |
| 1205 | *bp = stk_ptr[15]; |
| 1206 | # else |
| 1207 | *pc = ucontext->uc_mcontext.gregs[REG_PC]; |
| 1208 | *sp = ucontext->uc_mcontext.gregs[REG_O6]; |
| 1209 | stk_ptr = (uptr *) *sp; |
| 1210 | *bp = stk_ptr[15]; |
| 1211 | # endif |
| 1212 | #elif defined(__mips__) |
| 1213 | ucontext_t *ucontext = (ucontext_t*)context; |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1214 | *pc = ucontext->uc_mcontext.pc; |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 1215 | *bp = ucontext->uc_mcontext.gregs[30]; |
| 1216 | *sp = ucontext->uc_mcontext.gregs[29]; |
| 1217 | #else |
| 1218 | # error "Unsupported arch" |
| 1219 | #endif |
| 1220 | } |
| 1221 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1222 | void DisableReexec() { |
| 1223 | // No need to re-exec on Linux. |
| 1224 | } |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 1225 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1226 | void MaybeReexec() { |
| 1227 | // No need to re-exec on Linux. |
| 1228 | } |
| 1229 | |
| 1230 | } // namespace __sanitizer |
| 1231 | |
| 1232 | #endif // SANITIZER_FREEBSD || SANITIZER_LINUX |