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 |
| 16 | |
Alexander Potapenko | 2bf0d03 | 2013-02-06 14:41:15 +0000 | [diff] [blame] | 17 | // Use 64-bit inodes in file operations. ASan does not support OS X 10.5, so |
| 18 | // the clients will most certainly use 64-bit ones as well. |
| 19 | #ifndef _DARWIN_USE_64_BIT_INODE |
| 20 | #define _DARWIN_USE_64_BIT_INODE 1 |
| 21 | #endif |
| 22 | #include <stdio.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 23 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 24 | #include "sanitizer_common.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 25 | #include "sanitizer_flags.h" |
Alexey Samsonov | 94b5036 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 26 | #include "sanitizer_internal_defs.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 27 | #include "sanitizer_libc.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 28 | #include "sanitizer_mac.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 | |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 33 | #include <crt_externs.h> // for _NSGetEnviron |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 34 | #include <errno.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 35 | #include <fcntl.h> |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 36 | #include <libkern/OSAtomic.h> |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 37 | #include <mach-o/dyld.h> |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 38 | #include <mach/mach.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 39 | #include <pthread.h> |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 40 | #include <sched.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 41 | #include <signal.h> |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 42 | #include <stdlib.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 43 | #include <sys/mman.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 44 | #include <sys/resource.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 45 | #include <sys/stat.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 46 | #include <sys/sysctl.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 47 | #include <sys/types.h> |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 48 | #include <unistd.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 49 | |
| 50 | namespace __sanitizer { |
| 51 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 52 | #include "sanitizer_syscall_generic.inc" |
| 53 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 54 | // ---------------------- sanitizer_libc.h |
Peter Collingbourne | 65246dc | 2013-05-08 15:07:12 +0000 | [diff] [blame] | 55 | uptr internal_mmap(void *addr, size_t length, int prot, int flags, |
| 56 | int fd, u64 offset) { |
| 57 | return (uptr)mmap(addr, length, prot, flags, fd, offset); |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Peter Collingbourne | 65246dc | 2013-05-08 15:07:12 +0000 | [diff] [blame] | 60 | uptr internal_munmap(void *addr, uptr length) { |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 61 | return munmap(addr, length); |
| 62 | } |
| 63 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 64 | int internal_mprotect(void *addr, uptr length, int prot) { |
| 65 | return mprotect(addr, length, prot); |
| 66 | } |
| 67 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 68 | uptr internal_close(fd_t fd) { |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 69 | return close(fd); |
| 70 | } |
| 71 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 72 | uptr internal_open(const char *filename, int flags) { |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 73 | return open(filename, flags); |
| 74 | } |
| 75 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 76 | uptr internal_open(const char *filename, int flags, u32 mode) { |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 77 | return open(filename, flags, mode); |
| 78 | } |
| 79 | |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 80 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
| 81 | return read(fd, buf, count); |
| 82 | } |
| 83 | |
| 84 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
| 85 | return write(fd, buf, count); |
| 86 | } |
| 87 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 88 | uptr internal_stat(const char *path, void *buf) { |
Alexey Samsonov | 7c50ee7 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 89 | return stat(path, (struct stat *)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 92 | uptr internal_lstat(const char *path, void *buf) { |
Alexey Samsonov | 7c50ee7 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 93 | return lstat(path, (struct stat *)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 96 | uptr internal_fstat(fd_t fd, void *buf) { |
Alexey Samsonov | 7c50ee7 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 97 | return fstat(fd, (struct stat *)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 100 | uptr internal_filesize(fd_t fd) { |
Alexey Samsonov | 668accc | 2012-08-02 10:09:31 +0000 | [diff] [blame] | 101 | struct stat st; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 102 | if (internal_fstat(fd, &st)) |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 103 | return -1; |
| 104 | return (uptr)st.st_size; |
| 105 | } |
| 106 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 107 | uptr internal_dup2(int oldfd, int newfd) { |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 108 | return dup2(oldfd, newfd); |
| 109 | } |
| 110 | |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 111 | uptr internal_readlink(const char *path, char *buf, uptr bufsize) { |
| 112 | return readlink(path, buf, bufsize); |
| 113 | } |
| 114 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 115 | uptr internal_unlink(const char *path) { |
| 116 | return unlink(path); |
| 117 | } |
| 118 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 119 | uptr internal_sched_yield() { |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 120 | return sched_yield(); |
| 121 | } |
| 122 | |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 123 | void internal__exit(int exitcode) { |
| 124 | _exit(exitcode); |
| 125 | } |
| 126 | |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 127 | uptr internal_getpid() { |
| 128 | return getpid(); |
| 129 | } |
| 130 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 131 | int internal_sigaction(int signum, const void *act, void *oldact) { |
| 132 | return sigaction(signum, |
| 133 | (struct sigaction *)act, (struct sigaction *)oldact); |
| 134 | } |
| 135 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 136 | void internal_sigfillset(__sanitizer_sigset_t *set) { sigfillset(set); } |
| 137 | |
| 138 | uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set, |
| 139 | __sanitizer_sigset_t *oldset) { |
| 140 | return sigprocmask(how, set, oldset); |
| 141 | } |
| 142 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 143 | int internal_fork() { |
| 144 | // TODO(glider): this may call user's pthread_atfork() handlers which is bad. |
| 145 | return fork(); |
| 146 | } |
| 147 | |
| 148 | uptr internal_rename(const char *oldpath, const char *newpath) { |
| 149 | return rename(oldpath, newpath); |
| 150 | } |
| 151 | |
| 152 | uptr internal_ftruncate(fd_t fd, uptr size) { |
| 153 | return ftruncate(fd, size); |
| 154 | } |
| 155 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 156 | // ----------------- sanitizer_common.h |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 157 | bool FileExists(const char *filename) { |
| 158 | struct stat st; |
| 159 | if (stat(filename, &st)) |
| 160 | return false; |
| 161 | // Sanity check: filename is a regular file. |
| 162 | return S_ISREG(st.st_mode); |
| 163 | } |
| 164 | |
Dmitry Vyukov | e0023f7 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 165 | uptr GetTid() { |
| 166 | return reinterpret_cast<uptr>(pthread_self()); |
| 167 | } |
| 168 | |
Alexey Samsonov | ed996f7 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 169 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 170 | uptr *stack_bottom) { |
| 171 | CHECK(stack_top); |
| 172 | CHECK(stack_bottom); |
| 173 | uptr stacksize = pthread_get_stacksize_np(pthread_self()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 174 | // pthread_get_stacksize_np() returns an incorrect stack size for the main |
| 175 | // thread on Mavericks. See |
| 176 | // https://code.google.com/p/address-sanitizer/issues/detail?id=261 |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 177 | if ((GetMacosVersion() >= MACOS_VERSION_MAVERICKS) && at_initialization && |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 178 | stacksize == (1 << 19)) { |
| 179 | struct rlimit rl; |
| 180 | CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0); |
| 181 | // Most often rl.rlim_cur will be the desired 8M. |
| 182 | if (rl.rlim_cur < kMaxThreadStackSize) { |
| 183 | stacksize = rl.rlim_cur; |
| 184 | } else { |
| 185 | stacksize = kMaxThreadStackSize; |
| 186 | } |
| 187 | } |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 188 | void *stackaddr = pthread_get_stackaddr_np(pthread_self()); |
| 189 | *stack_top = (uptr)stackaddr; |
Alexey Samsonov | ed996f7 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 190 | *stack_bottom = *stack_top - stacksize; |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 193 | const char *GetEnv(const char *name) { |
| 194 | char ***env_ptr = _NSGetEnviron(); |
Alexander Potapenko | b527f7d | 2013-11-13 13:34:53 +0000 | [diff] [blame] | 195 | if (!env_ptr) { |
| 196 | Report("_NSGetEnviron() returned NULL. Please make sure __asan_init() is " |
| 197 | "called after libSystem_initializer().\n"); |
| 198 | CHECK(env_ptr); |
| 199 | } |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 200 | char **environ = *env_ptr; |
| 201 | CHECK(environ); |
| 202 | uptr name_len = internal_strlen(name); |
| 203 | while (*environ != 0) { |
| 204 | uptr len = internal_strlen(*environ); |
| 205 | if (len > name_len) { |
| 206 | const char *p = *environ; |
| 207 | if (!internal_memcmp(p, name, name_len) && |
| 208 | p[name_len] == '=') { // Match. |
| 209 | return *environ + name_len + 1; // String starting after =. |
| 210 | } |
| 211 | } |
| 212 | environ++; |
| 213 | } |
| 214 | return 0; |
| 215 | } |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 216 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 217 | uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) { |
| 218 | CHECK_LE(kMaxPathLength, buf_len); |
| 219 | |
| 220 | // On OS X the executable path is saved to the stack by dyld. Reading it |
| 221 | // from there is much faster than calling dladdr, especially for large |
| 222 | // binaries with symbols. |
| 223 | InternalScopedString exe_path(kMaxPathLength); |
| 224 | uint32_t size = exe_path.size(); |
| 225 | if (_NSGetExecutablePath(exe_path.data(), &size) == 0 && |
| 226 | realpath(exe_path.data(), buf) != 0) { |
| 227 | return internal_strlen(buf); |
| 228 | } |
| 229 | return 0; |
| 230 | } |
| 231 | |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 232 | void ReExec() { |
| 233 | UNIMPLEMENTED(); |
| 234 | } |
| 235 | |
Peter Collingbourne | 4df343a | 2013-05-20 17:05:29 +0000 | [diff] [blame] | 236 | uptr GetPageSize() { |
| 237 | return sysconf(_SC_PAGESIZE); |
| 238 | } |
| 239 | |
Alexey Samsonov | 93af594 | 2013-03-14 13:30:56 +0000 | [diff] [blame] | 240 | BlockingMutex::BlockingMutex() { |
| 241 | internal_memset(this, 0, sizeof(*this)); |
| 242 | } |
| 243 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 244 | void BlockingMutex::Lock() { |
| 245 | CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_)); |
Kostya Serebryany | f155fcc | 2013-02-26 12:59:06 +0000 | [diff] [blame] | 246 | CHECK_EQ(OS_SPINLOCK_INIT, 0); |
| 247 | CHECK_NE(owner_, (uptr)pthread_self()); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 248 | OSSpinLockLock((OSSpinLock*)&opaque_storage_); |
| 249 | CHECK(!owner_); |
| 250 | owner_ = (uptr)pthread_self(); |
| 251 | } |
| 252 | |
| 253 | void BlockingMutex::Unlock() { |
| 254 | CHECK(owner_ == (uptr)pthread_self()); |
| 255 | owner_ = 0; |
| 256 | OSSpinLockUnlock((OSSpinLock*)&opaque_storage_); |
| 257 | } |
| 258 | |
Alexey Samsonov | ce70097 | 2013-03-11 15:45:20 +0000 | [diff] [blame] | 259 | void BlockingMutex::CheckLocked() { |
| 260 | CHECK_EQ((uptr)pthread_self(), owner_); |
| 261 | } |
| 262 | |
Dmitry Vyukov | d4bb4a6 | 2013-06-06 13:00:32 +0000 | [diff] [blame] | 263 | u64 NanoTime() { |
| 264 | return 0; |
| 265 | } |
| 266 | |
Evgeniy Stepanov | b114ed8 | 2013-03-13 08:19:53 +0000 | [diff] [blame] | 267 | uptr GetTlsSize() { |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | void InitTlsSize() { |
| 272 | } |
| 273 | |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 274 | void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size, |
| 275 | uptr *tls_addr, uptr *tls_size) { |
Dmitry Vyukov | 4a95e33 | 2013-06-06 13:20:40 +0000 | [diff] [blame] | 276 | #ifndef SANITIZER_GO |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 277 | uptr stack_top, stack_bottom; |
| 278 | GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom); |
| 279 | *stk_addr = stack_bottom; |
| 280 | *stk_size = stack_top - stack_bottom; |
| 281 | *tls_addr = 0; |
| 282 | *tls_size = 0; |
Dmitry Vyukov | 4a95e33 | 2013-06-06 13:20:40 +0000 | [diff] [blame] | 283 | #else |
| 284 | *stk_addr = 0; |
| 285 | *stk_size = 0; |
| 286 | *tls_addr = 0; |
| 287 | *tls_size = 0; |
| 288 | #endif |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 291 | uptr GetListOfModules(LoadedModule *modules, uptr max_modules, |
| 292 | string_predicate_t filter) { |
| 293 | MemoryMappingLayout memory_mapping(false); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 294 | return memory_mapping.DumpListOfModules(modules, max_modules, filter); |
| 295 | } |
| 296 | |
| 297 | bool IsDeadlySignal(int signum) { |
| 298 | return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv; |
| 299 | } |
| 300 | |
| 301 | MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED; |
| 302 | |
| 303 | MacosVersion GetMacosVersionInternal() { |
| 304 | int mib[2] = { CTL_KERN, KERN_OSRELEASE }; |
| 305 | char version[100]; |
| 306 | uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]); |
| 307 | for (uptr i = 0; i < maxlen; i++) version[i] = '\0'; |
| 308 | // Get the version length. |
| 309 | CHECK_NE(sysctl(mib, 2, 0, &len, 0, 0), -1); |
| 310 | CHECK_LT(len, maxlen); |
| 311 | CHECK_NE(sysctl(mib, 2, version, &len, 0, 0), -1); |
| 312 | switch (version[0]) { |
| 313 | case '9': return MACOS_VERSION_LEOPARD; |
| 314 | case '1': { |
| 315 | switch (version[1]) { |
| 316 | case '0': return MACOS_VERSION_SNOW_LEOPARD; |
| 317 | case '1': return MACOS_VERSION_LION; |
| 318 | case '2': return MACOS_VERSION_MOUNTAIN_LION; |
| 319 | case '3': return MACOS_VERSION_MAVERICKS; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 320 | case '4': return MACOS_VERSION_YOSEMITE; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 321 | default: |
| 322 | if (IsDigit(version[1])) |
| 323 | return MACOS_VERSION_UNKNOWN_NEWER; |
| 324 | else |
| 325 | return MACOS_VERSION_UNKNOWN; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 326 | } |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 327 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 328 | default: return MACOS_VERSION_UNKNOWN; |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 329 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | MacosVersion GetMacosVersion() { |
| 333 | atomic_uint32_t *cache = |
| 334 | reinterpret_cast<atomic_uint32_t*>(&cached_macos_version); |
| 335 | MacosVersion result = |
| 336 | static_cast<MacosVersion>(atomic_load(cache, memory_order_acquire)); |
| 337 | if (result == MACOS_VERSION_UNINITIALIZED) { |
| 338 | result = GetMacosVersionInternal(); |
| 339 | atomic_store(cache, result, memory_order_release); |
| 340 | } |
| 341 | return result; |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 344 | uptr GetRSS() { |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 345 | struct task_basic_info info; |
| 346 | unsigned count = TASK_BASIC_INFO_COUNT; |
| 347 | kern_return_t result = |
| 348 | task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count); |
| 349 | if (UNLIKELY(result != KERN_SUCCESS)) { |
| 350 | Report("Cannot get task info. Error: %d\n", result); |
| 351 | Die(); |
| 352 | } |
| 353 | return info.resident_size; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void *internal_start_thread(void (*func)(void *arg), void *arg) { return 0; } |
| 357 | void internal_join_thread(void *th) { } |
| 358 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 359 | void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { |
| 360 | ucontext_t *ucontext = (ucontext_t*)context; |
| 361 | # if SANITIZER_WORDSIZE == 64 |
| 362 | *pc = ucontext->uc_mcontext->__ss.__rip; |
| 363 | *bp = ucontext->uc_mcontext->__ss.__rbp; |
| 364 | *sp = ucontext->uc_mcontext->__ss.__rsp; |
| 365 | # else |
| 366 | *pc = ucontext->uc_mcontext->__ss.__eip; |
| 367 | *bp = ucontext->uc_mcontext->__ss.__ebp; |
| 368 | *sp = ucontext->uc_mcontext->__ss.__esp; |
| 369 | # endif // SANITIZER_WORDSIZE |
| 370 | } |
| 371 | |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 372 | } // namespace __sanitizer |
| 373 | |
Alexey Samsonov | 46f9395 | 2013-04-03 07:24:35 +0000 | [diff] [blame] | 374 | #endif // SANITIZER_MAC |