Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 1 | //===-- sanitizer_mac.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 | // |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 10 | // This file is shared between various sanitizers' runtime libraries and |
| 11 | // implements OSX-specific functions. |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | #include "sanitizer_platform.h" |
| 15 | #if SANITIZER_MAC |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 16 | #include "sanitizer_mac.h" |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 17 | |
Alexander Potapenko | 2bf0d03 | 2013-02-06 14:41:15 +0000 | [diff] [blame] | 18 | // Use 64-bit inodes in file operations. ASan does not support OS X 10.5, so |
| 19 | // the clients will most certainly use 64-bit ones as well. |
| 20 | #ifndef _DARWIN_USE_64_BIT_INODE |
| 21 | #define _DARWIN_USE_64_BIT_INODE 1 |
| 22 | #endif |
| 23 | #include <stdio.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 24 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 25 | #include "sanitizer_common.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 26 | #include "sanitizer_flags.h" |
Alexey Samsonov | 94b5036 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 27 | #include "sanitizer_internal_defs.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 28 | #include "sanitizer_libc.h" |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 29 | #include "sanitizer_placement_new.h" |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 30 | #include "sanitizer_platform_limits_posix.h" |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 31 | #include "sanitizer_procmaps.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 32 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 33 | #if !SANITIZER_IOS |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 34 | #include <crt_externs.h> // for _NSGetEnviron |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 35 | #else |
| 36 | extern char **environ; |
| 37 | #endif |
| 38 | |
| 39 | #if defined(__has_include) && __has_include(<os/trace.h>) |
| 40 | #define SANITIZER_OS_TRACE 1 |
| 41 | #include <os/trace.h> |
| 42 | #else |
| 43 | #define SANITIZER_OS_TRACE 0 |
| 44 | #endif |
| 45 | |
| 46 | #if !SANITIZER_IOS |
| 47 | #include <crt_externs.h> // for _NSGetArgv and _NSGetEnviron |
| 48 | #else |
| 49 | extern "C" { |
| 50 | extern char ***_NSGetArgv(void); |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | #include <asl.h> |
| 55 | #include <dlfcn.h> // for dladdr() |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 56 | #include <errno.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 57 | #include <fcntl.h> |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 58 | #include <libkern/OSAtomic.h> |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 59 | #include <mach-o/dyld.h> |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 60 | #include <mach/mach.h> |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 61 | #include <mach/vm_statistics.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 62 | #include <pthread.h> |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 63 | #include <sched.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 64 | #include <signal.h> |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 65 | #include <stdlib.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 66 | #include <sys/mman.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 67 | #include <sys/resource.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 68 | #include <sys/stat.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 69 | #include <sys/sysctl.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 70 | #include <sys/types.h> |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 71 | #include <unistd.h> |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 72 | #include <util.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 73 | |
| 74 | namespace __sanitizer { |
| 75 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 76 | #include "sanitizer_syscall_generic.inc" |
| 77 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 78 | // ---------------------- sanitizer_libc.h |
Peter Collingbourne | 65246dc | 2013-05-08 15:07:12 +0000 | [diff] [blame] | 79 | uptr internal_mmap(void *addr, size_t length, int prot, int flags, |
| 80 | int fd, u64 offset) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 81 | if (fd == -1) fd = VM_MAKE_TAG(VM_MEMORY_ANALYSIS_TOOL); |
Peter Collingbourne | 65246dc | 2013-05-08 15:07:12 +0000 | [diff] [blame] | 82 | return (uptr)mmap(addr, length, prot, flags, fd, offset); |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Peter Collingbourne | 65246dc | 2013-05-08 15:07:12 +0000 | [diff] [blame] | 85 | uptr internal_munmap(void *addr, uptr length) { |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 86 | return munmap(addr, length); |
| 87 | } |
| 88 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 89 | int internal_mprotect(void *addr, uptr length, int prot) { |
| 90 | return mprotect(addr, length, prot); |
| 91 | } |
| 92 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 93 | uptr internal_close(fd_t fd) { |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 94 | return close(fd); |
| 95 | } |
| 96 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 97 | uptr internal_open(const char *filename, int flags) { |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 98 | return open(filename, flags); |
| 99 | } |
| 100 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 101 | uptr internal_open(const char *filename, int flags, u32 mode) { |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 102 | return open(filename, flags, mode); |
| 103 | } |
| 104 | |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 105 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
| 106 | return read(fd, buf, count); |
| 107 | } |
| 108 | |
| 109 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
| 110 | return write(fd, buf, count); |
| 111 | } |
| 112 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 113 | uptr internal_stat(const char *path, void *buf) { |
Alexey Samsonov | 7c50ee7 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 114 | return stat(path, (struct stat *)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 117 | uptr internal_lstat(const char *path, void *buf) { |
Alexey Samsonov | 7c50ee7 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 118 | return lstat(path, (struct stat *)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 121 | uptr internal_fstat(fd_t fd, void *buf) { |
Alexey Samsonov | 7c50ee7 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 122 | return fstat(fd, (struct stat *)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 125 | uptr internal_filesize(fd_t fd) { |
Alexey Samsonov | 668accc | 2012-08-02 10:09:31 +0000 | [diff] [blame] | 126 | struct stat st; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 127 | if (internal_fstat(fd, &st)) |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 128 | return -1; |
| 129 | return (uptr)st.st_size; |
| 130 | } |
| 131 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 132 | uptr internal_dup2(int oldfd, int newfd) { |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 133 | return dup2(oldfd, newfd); |
| 134 | } |
| 135 | |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 136 | uptr internal_readlink(const char *path, char *buf, uptr bufsize) { |
| 137 | return readlink(path, buf, bufsize); |
| 138 | } |
| 139 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 140 | uptr internal_unlink(const char *path) { |
| 141 | return unlink(path); |
| 142 | } |
| 143 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 144 | uptr internal_sched_yield() { |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 145 | return sched_yield(); |
| 146 | } |
| 147 | |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 148 | void internal__exit(int exitcode) { |
| 149 | _exit(exitcode); |
| 150 | } |
| 151 | |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 152 | uptr internal_getpid() { |
| 153 | return getpid(); |
| 154 | } |
| 155 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 156 | int internal_sigaction(int signum, const void *act, void *oldact) { |
| 157 | return sigaction(signum, |
| 158 | (struct sigaction *)act, (struct sigaction *)oldact); |
| 159 | } |
| 160 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 161 | void internal_sigfillset(__sanitizer_sigset_t *set) { sigfillset(set); } |
| 162 | |
| 163 | uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set, |
| 164 | __sanitizer_sigset_t *oldset) { |
| 165 | return sigprocmask(how, set, oldset); |
| 166 | } |
| 167 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 168 | // Doesn't call pthread_atfork() handlers. |
| 169 | extern "C" pid_t __fork(void); |
| 170 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 171 | int internal_fork() { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 172 | return __fork(); |
| 173 | } |
| 174 | |
| 175 | int internal_forkpty(int *amaster) { |
| 176 | int master, slave; |
| 177 | if (openpty(&master, &slave, nullptr, nullptr, nullptr) == -1) return -1; |
| 178 | int pid = __fork(); |
| 179 | if (pid == -1) { |
| 180 | close(master); |
| 181 | close(slave); |
| 182 | return -1; |
| 183 | } |
| 184 | if (pid == 0) { |
| 185 | close(master); |
| 186 | CHECK_EQ(login_tty(slave), 0); |
| 187 | } else { |
| 188 | *amaster = master; |
| 189 | close(slave); |
| 190 | } |
| 191 | return pid; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | uptr internal_rename(const char *oldpath, const char *newpath) { |
| 195 | return rename(oldpath, newpath); |
| 196 | } |
| 197 | |
| 198 | uptr internal_ftruncate(fd_t fd, uptr size) { |
| 199 | return ftruncate(fd, size); |
| 200 | } |
| 201 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 202 | // ----------------- sanitizer_common.h |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 203 | bool FileExists(const char *filename) { |
| 204 | struct stat st; |
| 205 | if (stat(filename, &st)) |
| 206 | return false; |
| 207 | // Sanity check: filename is a regular file. |
| 208 | return S_ISREG(st.st_mode); |
| 209 | } |
| 210 | |
Dmitry Vyukov | e0023f7 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 211 | uptr GetTid() { |
| 212 | return reinterpret_cast<uptr>(pthread_self()); |
| 213 | } |
| 214 | |
Alexey Samsonov | ed996f7 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 215 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 216 | uptr *stack_bottom) { |
| 217 | CHECK(stack_top); |
| 218 | CHECK(stack_bottom); |
| 219 | uptr stacksize = pthread_get_stacksize_np(pthread_self()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 220 | // pthread_get_stacksize_np() returns an incorrect stack size for the main |
| 221 | // thread on Mavericks. See |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 222 | // https://github.com/google/sanitizers/issues/261 |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 223 | if ((GetMacosVersion() >= MACOS_VERSION_MAVERICKS) && at_initialization && |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 224 | stacksize == (1 << 19)) { |
| 225 | struct rlimit rl; |
| 226 | CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0); |
| 227 | // Most often rl.rlim_cur will be the desired 8M. |
| 228 | if (rl.rlim_cur < kMaxThreadStackSize) { |
| 229 | stacksize = rl.rlim_cur; |
| 230 | } else { |
| 231 | stacksize = kMaxThreadStackSize; |
| 232 | } |
| 233 | } |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 234 | void *stackaddr = pthread_get_stackaddr_np(pthread_self()); |
| 235 | *stack_top = (uptr)stackaddr; |
Alexey Samsonov | ed996f7 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 236 | *stack_bottom = *stack_top - stacksize; |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 239 | char **GetEnviron() { |
| 240 | #if !SANITIZER_IOS |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 241 | char ***env_ptr = _NSGetEnviron(); |
Alexander Potapenko | b527f7d | 2013-11-13 13:34:53 +0000 | [diff] [blame] | 242 | if (!env_ptr) { |
| 243 | Report("_NSGetEnviron() returned NULL. Please make sure __asan_init() is " |
| 244 | "called after libSystem_initializer().\n"); |
| 245 | CHECK(env_ptr); |
| 246 | } |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 247 | char **environ = *env_ptr; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 248 | #endif |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 249 | CHECK(environ); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 250 | return environ; |
| 251 | } |
| 252 | |
| 253 | const char *GetEnv(const char *name) { |
| 254 | char **env = GetEnviron(); |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 255 | uptr name_len = internal_strlen(name); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 256 | while (*env != 0) { |
| 257 | uptr len = internal_strlen(*env); |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 258 | if (len > name_len) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 259 | const char *p = *env; |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 260 | if (!internal_memcmp(p, name, name_len) && |
| 261 | p[name_len] == '=') { // Match. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 262 | return *env + name_len + 1; // String starting after =. |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 265 | env++; |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 266 | } |
| 267 | return 0; |
| 268 | } |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 269 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 270 | uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) { |
| 271 | CHECK_LE(kMaxPathLength, buf_len); |
| 272 | |
| 273 | // On OS X the executable path is saved to the stack by dyld. Reading it |
| 274 | // from there is much faster than calling dladdr, especially for large |
| 275 | // binaries with symbols. |
| 276 | InternalScopedString exe_path(kMaxPathLength); |
| 277 | uint32_t size = exe_path.size(); |
| 278 | if (_NSGetExecutablePath(exe_path.data(), &size) == 0 && |
| 279 | realpath(exe_path.data(), buf) != 0) { |
| 280 | return internal_strlen(buf); |
| 281 | } |
| 282 | return 0; |
| 283 | } |
| 284 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 285 | uptr ReadLongProcessName(/*out*/char *buf, uptr buf_len) { |
| 286 | return ReadBinaryName(buf, buf_len); |
| 287 | } |
| 288 | |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 289 | void ReExec() { |
| 290 | UNIMPLEMENTED(); |
| 291 | } |
| 292 | |
Peter Collingbourne | 4df343a | 2013-05-20 17:05:29 +0000 | [diff] [blame] | 293 | uptr GetPageSize() { |
| 294 | return sysconf(_SC_PAGESIZE); |
| 295 | } |
| 296 | |
Alexey Samsonov | 93af594 | 2013-03-14 13:30:56 +0000 | [diff] [blame] | 297 | BlockingMutex::BlockingMutex() { |
| 298 | internal_memset(this, 0, sizeof(*this)); |
| 299 | } |
| 300 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 301 | void BlockingMutex::Lock() { |
| 302 | CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_)); |
Kostya Serebryany | f155fcc | 2013-02-26 12:59:06 +0000 | [diff] [blame] | 303 | CHECK_EQ(OS_SPINLOCK_INIT, 0); |
| 304 | CHECK_NE(owner_, (uptr)pthread_self()); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 305 | OSSpinLockLock((OSSpinLock*)&opaque_storage_); |
| 306 | CHECK(!owner_); |
| 307 | owner_ = (uptr)pthread_self(); |
| 308 | } |
| 309 | |
| 310 | void BlockingMutex::Unlock() { |
| 311 | CHECK(owner_ == (uptr)pthread_self()); |
| 312 | owner_ = 0; |
| 313 | OSSpinLockUnlock((OSSpinLock*)&opaque_storage_); |
| 314 | } |
| 315 | |
Alexey Samsonov | ce70097 | 2013-03-11 15:45:20 +0000 | [diff] [blame] | 316 | void BlockingMutex::CheckLocked() { |
| 317 | CHECK_EQ((uptr)pthread_self(), owner_); |
| 318 | } |
| 319 | |
Dmitry Vyukov | d4bb4a6 | 2013-06-06 13:00:32 +0000 | [diff] [blame] | 320 | u64 NanoTime() { |
| 321 | return 0; |
| 322 | } |
| 323 | |
Evgeniy Stepanov | b114ed8 | 2013-03-13 08:19:53 +0000 | [diff] [blame] | 324 | uptr GetTlsSize() { |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | void InitTlsSize() { |
| 329 | } |
| 330 | |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 331 | void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size, |
| 332 | uptr *tls_addr, uptr *tls_size) { |
Dmitry Vyukov | 4a95e33 | 2013-06-06 13:20:40 +0000 | [diff] [blame] | 333 | #ifndef SANITIZER_GO |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 334 | uptr stack_top, stack_bottom; |
| 335 | GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom); |
| 336 | *stk_addr = stack_bottom; |
| 337 | *stk_size = stack_top - stack_bottom; |
| 338 | *tls_addr = 0; |
| 339 | *tls_size = 0; |
Dmitry Vyukov | 4a95e33 | 2013-06-06 13:20:40 +0000 | [diff] [blame] | 340 | #else |
| 341 | *stk_addr = 0; |
| 342 | *stk_size = 0; |
| 343 | *tls_addr = 0; |
| 344 | *tls_size = 0; |
| 345 | #endif |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 348 | uptr GetListOfModules(LoadedModule *modules, uptr max_modules, |
| 349 | string_predicate_t filter) { |
| 350 | MemoryMappingLayout memory_mapping(false); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 351 | return memory_mapping.DumpListOfModules(modules, max_modules, filter); |
| 352 | } |
| 353 | |
| 354 | bool IsDeadlySignal(int signum) { |
| 355 | return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv; |
| 356 | } |
| 357 | |
| 358 | MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED; |
| 359 | |
| 360 | MacosVersion GetMacosVersionInternal() { |
| 361 | int mib[2] = { CTL_KERN, KERN_OSRELEASE }; |
| 362 | char version[100]; |
| 363 | uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]); |
| 364 | for (uptr i = 0; i < maxlen; i++) version[i] = '\0'; |
| 365 | // Get the version length. |
| 366 | CHECK_NE(sysctl(mib, 2, 0, &len, 0, 0), -1); |
| 367 | CHECK_LT(len, maxlen); |
| 368 | CHECK_NE(sysctl(mib, 2, version, &len, 0, 0), -1); |
| 369 | switch (version[0]) { |
| 370 | case '9': return MACOS_VERSION_LEOPARD; |
| 371 | case '1': { |
| 372 | switch (version[1]) { |
| 373 | case '0': return MACOS_VERSION_SNOW_LEOPARD; |
| 374 | case '1': return MACOS_VERSION_LION; |
| 375 | case '2': return MACOS_VERSION_MOUNTAIN_LION; |
| 376 | case '3': return MACOS_VERSION_MAVERICKS; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 377 | case '4': return MACOS_VERSION_YOSEMITE; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 378 | default: |
| 379 | if (IsDigit(version[1])) |
| 380 | return MACOS_VERSION_UNKNOWN_NEWER; |
| 381 | else |
| 382 | return MACOS_VERSION_UNKNOWN; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 383 | } |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 384 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 385 | default: return MACOS_VERSION_UNKNOWN; |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 386 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | MacosVersion GetMacosVersion() { |
| 390 | atomic_uint32_t *cache = |
| 391 | reinterpret_cast<atomic_uint32_t*>(&cached_macos_version); |
| 392 | MacosVersion result = |
| 393 | static_cast<MacosVersion>(atomic_load(cache, memory_order_acquire)); |
| 394 | if (result == MACOS_VERSION_UNINITIALIZED) { |
| 395 | result = GetMacosVersionInternal(); |
| 396 | atomic_store(cache, result, memory_order_release); |
| 397 | } |
| 398 | return result; |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 401 | uptr GetRSS() { |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 402 | struct task_basic_info info; |
| 403 | unsigned count = TASK_BASIC_INFO_COUNT; |
| 404 | kern_return_t result = |
| 405 | task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count); |
| 406 | if (UNLIKELY(result != KERN_SUCCESS)) { |
| 407 | Report("Cannot get task info. Error: %d\n", result); |
| 408 | Die(); |
| 409 | } |
| 410 | return info.resident_size; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 413 | void *internal_start_thread(void(*func)(void *arg), void *arg) { |
| 414 | // Start the thread with signals blocked, otherwise it can steal user signals. |
| 415 | __sanitizer_sigset_t set, old; |
| 416 | internal_sigfillset(&set); |
| 417 | internal_sigprocmask(SIG_SETMASK, &set, &old); |
| 418 | pthread_t th; |
| 419 | pthread_create(&th, 0, (void*(*)(void *arg))func, arg); |
| 420 | internal_sigprocmask(SIG_SETMASK, &old, 0); |
| 421 | return th; |
| 422 | } |
| 423 | |
| 424 | void internal_join_thread(void *th) { pthread_join((pthread_t)th, 0); } |
| 425 | |
| 426 | static BlockingMutex syslog_lock(LINKER_INITIALIZED); |
| 427 | |
| 428 | void WriteOneLineToSyslog(const char *s) { |
| 429 | syslog_lock.CheckLocked(); |
| 430 | asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", s); |
| 431 | } |
| 432 | |
| 433 | void LogFullErrorReport(const char *buffer) { |
| 434 | // Log with os_trace. This will make it into the crash log. |
| 435 | #if SANITIZER_OS_TRACE |
| 436 | if (GetMacosVersion() >= MACOS_VERSION_YOSEMITE) { |
| 437 | // os_trace requires the message (format parameter) to be a string literal. |
| 438 | if (internal_strncmp(SanitizerToolName, "AddressSanitizer", |
| 439 | sizeof("AddressSanitizer") - 1) == 0) |
| 440 | os_trace("Address Sanitizer reported a failure."); |
| 441 | else if (internal_strncmp(SanitizerToolName, "UndefinedBehaviorSanitizer", |
| 442 | sizeof("UndefinedBehaviorSanitizer") - 1) == 0) |
| 443 | os_trace("Undefined Behavior Sanitizer reported a failure."); |
| 444 | else if (internal_strncmp(SanitizerToolName, "ThreadSanitizer", |
| 445 | sizeof("ThreadSanitizer") - 1) == 0) |
| 446 | os_trace("Thread Sanitizer reported a failure."); |
| 447 | else |
| 448 | os_trace("Sanitizer tool reported a failure."); |
| 449 | |
| 450 | if (common_flags()->log_to_syslog) |
| 451 | os_trace("Consult syslog for more information."); |
| 452 | } |
| 453 | #endif |
| 454 | |
| 455 | // Log to syslog. |
| 456 | // The logging on OS X may call pthread_create so we need the threading |
| 457 | // environment to be fully initialized. Also, this should never be called when |
| 458 | // holding the thread registry lock since that may result in a deadlock. If |
| 459 | // the reporting thread holds the thread registry mutex, and asl_log waits |
| 460 | // for GCD to dispatch a new thread, the process will deadlock, because the |
| 461 | // pthread_create wrapper needs to acquire the lock as well. |
| 462 | BlockingMutexLock l(&syslog_lock); |
| 463 | if (common_flags()->log_to_syslog) |
| 464 | WriteToSyslog(buffer); |
| 465 | |
| 466 | // Log to CrashLog. |
| 467 | if (common_flags()->abort_on_error) |
| 468 | CRSetCrashLogMessage(buffer); |
| 469 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 470 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 471 | void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { |
| 472 | ucontext_t *ucontext = (ucontext_t*)context; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 473 | # if defined(__aarch64__) |
| 474 | *pc = ucontext->uc_mcontext->__ss.__pc; |
| 475 | # if defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 |
| 476 | *bp = ucontext->uc_mcontext->__ss.__fp; |
| 477 | # else |
| 478 | *bp = ucontext->uc_mcontext->__ss.__lr; |
| 479 | # endif |
| 480 | *sp = ucontext->uc_mcontext->__ss.__sp; |
| 481 | # elif defined(__x86_64__) |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 482 | *pc = ucontext->uc_mcontext->__ss.__rip; |
| 483 | *bp = ucontext->uc_mcontext->__ss.__rbp; |
| 484 | *sp = ucontext->uc_mcontext->__ss.__rsp; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 485 | # elif defined(__arm__) |
| 486 | *pc = ucontext->uc_mcontext->__ss.__pc; |
| 487 | *bp = ucontext->uc_mcontext->__ss.__r[7]; |
| 488 | *sp = ucontext->uc_mcontext->__ss.__sp; |
| 489 | # elif defined(__i386__) |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 490 | *pc = ucontext->uc_mcontext->__ss.__eip; |
| 491 | *bp = ucontext->uc_mcontext->__ss.__ebp; |
| 492 | *sp = ucontext->uc_mcontext->__ss.__esp; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 493 | # else |
| 494 | # error "Unknown architecture" |
| 495 | # endif |
| 496 | } |
| 497 | |
| 498 | static const char kDyldInsertLibraries[] = "DYLD_INSERT_LIBRARIES"; |
| 499 | LowLevelAllocator allocator_for_env; |
| 500 | |
| 501 | // Change the value of the env var |name|, leaking the original value. |
| 502 | // If |name_value| is NULL, the variable is deleted from the environment, |
| 503 | // otherwise the corresponding "NAME=value" string is replaced with |
| 504 | // |name_value|. |
| 505 | void LeakyResetEnv(const char *name, const char *name_value) { |
| 506 | char **env = GetEnviron(); |
| 507 | uptr name_len = internal_strlen(name); |
| 508 | while (*env != 0) { |
| 509 | uptr len = internal_strlen(*env); |
| 510 | if (len > name_len) { |
| 511 | const char *p = *env; |
| 512 | if (!internal_memcmp(p, name, name_len) && p[name_len] == '=') { |
| 513 | // Match. |
| 514 | if (name_value) { |
| 515 | // Replace the old value with the new one. |
| 516 | *env = const_cast<char*>(name_value); |
| 517 | } else { |
| 518 | // Shift the subsequent pointers back. |
| 519 | char **del = env; |
| 520 | do { |
| 521 | del[0] = del[1]; |
| 522 | } while (*del++); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | env++; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | static bool reexec_disabled = false; |
| 531 | |
| 532 | void DisableReexec() { |
| 533 | reexec_disabled = true; |
| 534 | } |
| 535 | |
| 536 | extern "C" double dyldVersionNumber; |
| 537 | static const double kMinDyldVersionWithAutoInterposition = 360.0; |
| 538 | |
| 539 | bool DyldNeedsEnvVariable() { |
| 540 | // If running on OS X 10.11+ or iOS 9.0+, dyld will interpose even if |
| 541 | // DYLD_INSERT_LIBRARIES is not set. However, checking OS version via |
| 542 | // GetMacosVersion() doesn't work for the simulator. Let's instead check |
| 543 | // `dyldVersionNumber`, which is exported by dyld, against a known version |
| 544 | // number from the first OS release where this appeared. |
| 545 | return dyldVersionNumber < kMinDyldVersionWithAutoInterposition; |
| 546 | } |
| 547 | |
| 548 | void MaybeReexec() { |
| 549 | if (reexec_disabled) return; |
| 550 | |
| 551 | // Make sure the dynamic runtime library is preloaded so that the |
| 552 | // wrappers work. If it is not, set DYLD_INSERT_LIBRARIES and re-exec |
| 553 | // ourselves. |
| 554 | Dl_info info; |
| 555 | CHECK(dladdr((void*)((uptr)&__sanitizer_report_error_summary), &info)); |
| 556 | char *dyld_insert_libraries = |
| 557 | const_cast<char*>(GetEnv(kDyldInsertLibraries)); |
| 558 | uptr old_env_len = dyld_insert_libraries ? |
| 559 | internal_strlen(dyld_insert_libraries) : 0; |
| 560 | uptr fname_len = internal_strlen(info.dli_fname); |
| 561 | const char *dylib_name = StripModuleName(info.dli_fname); |
| 562 | uptr dylib_name_len = internal_strlen(dylib_name); |
| 563 | |
| 564 | bool lib_is_in_env = dyld_insert_libraries && |
| 565 | internal_strstr(dyld_insert_libraries, dylib_name); |
| 566 | if (DyldNeedsEnvVariable() && !lib_is_in_env) { |
| 567 | // DYLD_INSERT_LIBRARIES is not set or does not contain the runtime |
| 568 | // library. |
| 569 | InternalScopedString program_name(1024); |
| 570 | uint32_t buf_size = program_name.size(); |
| 571 | _NSGetExecutablePath(program_name.data(), &buf_size); |
| 572 | char *new_env = const_cast<char*>(info.dli_fname); |
| 573 | if (dyld_insert_libraries) { |
| 574 | // Append the runtime dylib name to the existing value of |
| 575 | // DYLD_INSERT_LIBRARIES. |
| 576 | new_env = (char*)allocator_for_env.Allocate(old_env_len + fname_len + 2); |
| 577 | internal_strncpy(new_env, dyld_insert_libraries, old_env_len); |
| 578 | new_env[old_env_len] = ':'; |
| 579 | // Copy fname_len and add a trailing zero. |
| 580 | internal_strncpy(new_env + old_env_len + 1, info.dli_fname, |
| 581 | fname_len + 1); |
| 582 | // Ok to use setenv() since the wrappers don't depend on the value of |
| 583 | // asan_inited. |
| 584 | setenv(kDyldInsertLibraries, new_env, /*overwrite*/1); |
| 585 | } else { |
| 586 | // Set DYLD_INSERT_LIBRARIES equal to the runtime dylib name. |
| 587 | setenv(kDyldInsertLibraries, info.dli_fname, /*overwrite*/0); |
| 588 | } |
| 589 | VReport(1, "exec()-ing the program with\n"); |
| 590 | VReport(1, "%s=%s\n", kDyldInsertLibraries, new_env); |
| 591 | VReport(1, "to enable wrappers.\n"); |
| 592 | execv(program_name.data(), *_NSGetArgv()); |
| 593 | |
| 594 | // We get here only if execv() failed. |
| 595 | Report("ERROR: The process is launched without DYLD_INSERT_LIBRARIES, " |
| 596 | "which is required for the sanitizer to work. We tried to set the " |
| 597 | "environment variable and re-execute itself, but execv() failed, " |
| 598 | "possibly because of sandbox restrictions. Make sure to launch the " |
| 599 | "executable with:\n%s=%s\n", kDyldInsertLibraries, new_env); |
| 600 | CHECK("execv failed" && 0); |
| 601 | } |
| 602 | |
| 603 | if (!lib_is_in_env) |
| 604 | return; |
| 605 | |
| 606 | // DYLD_INSERT_LIBRARIES is set and contains the runtime library. Let's remove |
| 607 | // the dylib from the environment variable, because interceptors are installed |
| 608 | // and we don't want our children to inherit the variable. |
| 609 | |
| 610 | uptr env_name_len = internal_strlen(kDyldInsertLibraries); |
| 611 | // Allocate memory to hold the previous env var name, its value, the '=' |
| 612 | // sign and the '\0' char. |
| 613 | char *new_env = (char*)allocator_for_env.Allocate( |
| 614 | old_env_len + 2 + env_name_len); |
| 615 | CHECK(new_env); |
| 616 | internal_memset(new_env, '\0', old_env_len + 2 + env_name_len); |
| 617 | internal_strncpy(new_env, kDyldInsertLibraries, env_name_len); |
| 618 | new_env[env_name_len] = '='; |
| 619 | char *new_env_pos = new_env + env_name_len + 1; |
| 620 | |
| 621 | // Iterate over colon-separated pieces of |dyld_insert_libraries|. |
| 622 | char *piece_start = dyld_insert_libraries; |
| 623 | char *piece_end = NULL; |
| 624 | char *old_env_end = dyld_insert_libraries + old_env_len; |
| 625 | do { |
| 626 | if (piece_start[0] == ':') piece_start++; |
| 627 | piece_end = internal_strchr(piece_start, ':'); |
| 628 | if (!piece_end) piece_end = dyld_insert_libraries + old_env_len; |
| 629 | if ((uptr)(piece_start - dyld_insert_libraries) > old_env_len) break; |
| 630 | uptr piece_len = piece_end - piece_start; |
| 631 | |
| 632 | char *filename_start = |
| 633 | (char *)internal_memrchr(piece_start, '/', piece_len); |
| 634 | uptr filename_len = piece_len; |
| 635 | if (filename_start) { |
| 636 | filename_start += 1; |
| 637 | filename_len = piece_len - (filename_start - piece_start); |
| 638 | } else { |
| 639 | filename_start = piece_start; |
| 640 | } |
| 641 | |
| 642 | // If the current piece isn't the runtime library name, |
| 643 | // append it to new_env. |
| 644 | if ((dylib_name_len != filename_len) || |
| 645 | (internal_memcmp(filename_start, dylib_name, dylib_name_len) != 0)) { |
| 646 | if (new_env_pos != new_env + env_name_len + 1) { |
| 647 | new_env_pos[0] = ':'; |
| 648 | new_env_pos++; |
| 649 | } |
| 650 | internal_strncpy(new_env_pos, piece_start, piece_len); |
| 651 | new_env_pos += piece_len; |
| 652 | } |
| 653 | // Move on to the next piece. |
| 654 | piece_start = piece_end; |
| 655 | } while (piece_start < old_env_end); |
| 656 | |
| 657 | // Can't use setenv() here, because it requires the allocator to be |
| 658 | // initialized. |
| 659 | // FIXME: instead of filtering DYLD_INSERT_LIBRARIES here, do it in |
| 660 | // a separate function called after InitializeAllocator(). |
| 661 | if (new_env_pos == new_env + env_name_len + 1) new_env = NULL; |
| 662 | LeakyResetEnv(kDyldInsertLibraries, new_env); |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 665 | } // namespace __sanitizer |
| 666 | |
Alexey Samsonov | 46f9395 | 2013-04-03 07:24:35 +0000 | [diff] [blame] | 667 | #endif // SANITIZER_MAC |