| Alexey Samsonov | 2c5fc3b | 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 | // |
| Alexander Potapenko | 768e315 | 2014-02-03 15:32:19 +0000 | [diff] [blame] | 10 | // This file is shared between various sanitizers' runtime libraries and |
| 11 | // implements OSX-specific functions. |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Evgeniy Stepanov | 0af6723 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | #include "sanitizer_platform.h" |
| 15 | #if SANITIZER_MAC |
| 16 | |
| Alexander Potapenko | d895ae9 | 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 | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 23 | |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 24 | #include "sanitizer_common.h" |
| Alexander Potapenko | 789e3e1 | 2014-01-31 13:10:07 +0000 | [diff] [blame] | 25 | #include "sanitizer_flags.h" |
| Alexey Samsonov | 5bbf829 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 26 | #include "sanitizer_internal_defs.h" |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 27 | #include "sanitizer_libc.h" |
| Alexander Potapenko | 768e315 | 2014-02-03 15:32:19 +0000 | [diff] [blame] | 28 | #include "sanitizer_mac.h" |
| Alexey Samsonov | 7a36e61 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 29 | #include "sanitizer_placement_new.h" |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 30 | #include "sanitizer_procmaps.h" |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 31 | |
| Alexey Samsonov | 0c53a38 | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 32 | #include <crt_externs.h> // for _NSGetEnviron |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 33 | #include <fcntl.h> |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 34 | #include <pthread.h> |
| Alexey Samsonov | 58a3c58 | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 35 | #include <sched.h> |
| Alexander Potapenko | 789e3e1 | 2014-01-31 13:10:07 +0000 | [diff] [blame] | 36 | #include <signal.h> |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 37 | #include <sys/mman.h> |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 38 | #include <sys/resource.h> |
| Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 39 | #include <sys/stat.h> |
| Alexander Potapenko | 768e315 | 2014-02-03 15:32:19 +0000 | [diff] [blame] | 40 | #include <sys/sysctl.h> |
| Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 41 | #include <sys/types.h> |
| Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 42 | #include <unistd.h> |
| Dmitry Vyukov | af4b0b0 | 2013-01-14 08:01:58 +0000 | [diff] [blame] | 43 | #include <libkern/OSAtomic.h> |
| Peter Collingbourne | 8e110ce | 2013-05-08 15:07:12 +0000 | [diff] [blame] | 44 | #include <errno.h> |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 45 | |
| 46 | namespace __sanitizer { |
| 47 | |
| Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 48 | #include "sanitizer_syscall_generic.inc" |
| 49 | |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 50 | // ---------------------- sanitizer_libc.h |
| Peter Collingbourne | 8e110ce | 2013-05-08 15:07:12 +0000 | [diff] [blame] | 51 | uptr internal_mmap(void *addr, size_t length, int prot, int flags, |
| 52 | int fd, u64 offset) { |
| 53 | return (uptr)mmap(addr, length, prot, flags, fd, offset); |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| Peter Collingbourne | 8e110ce | 2013-05-08 15:07:12 +0000 | [diff] [blame] | 56 | uptr internal_munmap(void *addr, uptr length) { |
| Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 57 | return munmap(addr, length); |
| 58 | } |
| 59 | |
| Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 60 | uptr internal_close(fd_t fd) { |
| Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 61 | return close(fd); |
| 62 | } |
| 63 | |
| Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 64 | uptr internal_open(const char *filename, int flags) { |
| Alexey Samsonov | 39313b7 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 65 | return open(filename, flags); |
| 66 | } |
| 67 | |
| Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 68 | uptr internal_open(const char *filename, int flags, u32 mode) { |
| Alexey Samsonov | 39313b7 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 69 | return open(filename, flags, mode); |
| 70 | } |
| 71 | |
| Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 72 | uptr OpenFile(const char *filename, bool write) { |
| Alexey Samsonov | 39313b7 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 73 | return internal_open(filename, |
| 74 | write ? O_WRONLY | O_CREAT : O_RDONLY, 0660); |
| Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 77 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
| 78 | return read(fd, buf, count); |
| 79 | } |
| 80 | |
| 81 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
| 82 | return write(fd, buf, count); |
| 83 | } |
| 84 | |
| Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 85 | uptr internal_stat(const char *path, void *buf) { |
| Alexey Samsonov | 576e270 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 86 | return stat(path, (struct stat *)buf); |
| Alexey Samsonov | 2c5cbd2 | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 89 | uptr internal_lstat(const char *path, void *buf) { |
| Alexey Samsonov | 576e270 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 90 | return lstat(path, (struct stat *)buf); |
| Alexey Samsonov | 2c5cbd2 | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 93 | uptr internal_fstat(fd_t fd, void *buf) { |
| Alexey Samsonov | 576e270 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 94 | return fstat(fd, (struct stat *)buf); |
| Alexey Samsonov | 2c5cbd2 | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 97 | uptr internal_filesize(fd_t fd) { |
| Alexey Samsonov | ce8d497 | 2012-08-02 10:09:31 +0000 | [diff] [blame] | 98 | struct stat st; |
| Alexey Samsonov | 2c5cbd2 | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 99 | if (internal_fstat(fd, &st)) |
| Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 100 | return -1; |
| 101 | return (uptr)st.st_size; |
| 102 | } |
| 103 | |
| Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 104 | uptr internal_dup2(int oldfd, int newfd) { |
| Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 105 | return dup2(oldfd, newfd); |
| 106 | } |
| 107 | |
| Alexey Samsonov | f6d2125 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 108 | uptr internal_readlink(const char *path, char *buf, uptr bufsize) { |
| 109 | return readlink(path, buf, bufsize); |
| 110 | } |
| 111 | |
| Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 112 | uptr internal_sched_yield() { |
| Alexey Samsonov | 58a3c58 | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 113 | return sched_yield(); |
| 114 | } |
| 115 | |
| Alexey Samsonov | aadd1f2 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 116 | void internal__exit(int exitcode) { |
| 117 | _exit(exitcode); |
| 118 | } |
| 119 | |
| Peter Collingbourne | ffaf2ea | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 120 | uptr internal_getpid() { |
| 121 | return getpid(); |
| 122 | } |
| 123 | |
| Alexander Potapenko | 789e3e1 | 2014-01-31 13:10:07 +0000 | [diff] [blame] | 124 | int internal_sigaction(int signum, const void *act, void *oldact) { |
| 125 | return sigaction(signum, |
| 126 | (struct sigaction *)act, (struct sigaction *)oldact); |
| 127 | } |
| 128 | |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 129 | // ----------------- sanitizer_common.h |
| Alexey Samsonov | ae9b18b | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 130 | bool FileExists(const char *filename) { |
| 131 | struct stat st; |
| 132 | if (stat(filename, &st)) |
| 133 | return false; |
| 134 | // Sanity check: filename is a regular file. |
| 135 | return S_ISREG(st.st_mode); |
| 136 | } |
| 137 | |
| Dmitry Vyukov | 56faa55 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 138 | uptr GetTid() { |
| 139 | return reinterpret_cast<uptr>(pthread_self()); |
| 140 | } |
| 141 | |
| Alexey Samsonov | cf4d3a0 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 142 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 143 | uptr *stack_bottom) { |
| 144 | CHECK(stack_top); |
| 145 | CHECK(stack_bottom); |
| 146 | uptr stacksize = pthread_get_stacksize_np(pthread_self()); |
| 147 | void *stackaddr = pthread_get_stackaddr_np(pthread_self()); |
| 148 | *stack_top = (uptr)stackaddr; |
| Alexey Samsonov | cf4d3a0 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 149 | *stack_bottom = *stack_top - stacksize; |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| Alexey Samsonov | 0c53a38 | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 152 | const char *GetEnv(const char *name) { |
| 153 | char ***env_ptr = _NSGetEnviron(); |
| Alexander Potapenko | fa82ba9 | 2013-11-13 13:34:53 +0000 | [diff] [blame] | 154 | if (!env_ptr) { |
| 155 | Report("_NSGetEnviron() returned NULL. Please make sure __asan_init() is " |
| 156 | "called after libSystem_initializer().\n"); |
| 157 | CHECK(env_ptr); |
| 158 | } |
| Alexey Samsonov | 0c53a38 | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 159 | char **environ = *env_ptr; |
| 160 | CHECK(environ); |
| 161 | uptr name_len = internal_strlen(name); |
| 162 | while (*environ != 0) { |
| 163 | uptr len = internal_strlen(*environ); |
| 164 | if (len > name_len) { |
| 165 | const char *p = *environ; |
| 166 | if (!internal_memcmp(p, name, name_len) && |
| 167 | p[name_len] == '=') { // Match. |
| 168 | return *environ + name_len + 1; // String starting after =. |
| 169 | } |
| 170 | } |
| 171 | environ++; |
| 172 | } |
| 173 | return 0; |
| 174 | } |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 175 | |
| Alexey Samsonov | 97ca306 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 176 | void ReExec() { |
| 177 | UNIMPLEMENTED(); |
| 178 | } |
| 179 | |
| Alexander Potapenko | 1746f55 | 2012-12-10 13:10:40 +0000 | [diff] [blame] | 180 | void PrepareForSandboxing() { |
| 181 | // Nothing here for now. |
| 182 | } |
| 183 | |
| Peter Collingbourne | b69b8a4 | 2013-05-20 17:05:29 +0000 | [diff] [blame] | 184 | uptr GetPageSize() { |
| 185 | return sysconf(_SC_PAGESIZE); |
| 186 | } |
| 187 | |
| Dmitry Vyukov | f22982b | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 188 | BlockingMutex::BlockingMutex(LinkerInitialized) { |
| 189 | // We assume that OS_SPINLOCK_INIT is zero |
| 190 | } |
| 191 | |
| Alexey Samsonov | a097f7b | 2013-03-14 13:30:56 +0000 | [diff] [blame] | 192 | BlockingMutex::BlockingMutex() { |
| 193 | internal_memset(this, 0, sizeof(*this)); |
| 194 | } |
| 195 | |
| Dmitry Vyukov | f22982b | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 196 | void BlockingMutex::Lock() { |
| 197 | CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_)); |
| Kostya Serebryany | 459df6f | 2013-02-26 12:59:06 +0000 | [diff] [blame] | 198 | CHECK_EQ(OS_SPINLOCK_INIT, 0); |
| 199 | CHECK_NE(owner_, (uptr)pthread_self()); |
| Dmitry Vyukov | f22982b | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 200 | OSSpinLockLock((OSSpinLock*)&opaque_storage_); |
| 201 | CHECK(!owner_); |
| 202 | owner_ = (uptr)pthread_self(); |
| 203 | } |
| 204 | |
| 205 | void BlockingMutex::Unlock() { |
| 206 | CHECK(owner_ == (uptr)pthread_self()); |
| 207 | owner_ = 0; |
| 208 | OSSpinLockUnlock((OSSpinLock*)&opaque_storage_); |
| 209 | } |
| 210 | |
| Alexey Samsonov | db7d965 | 2013-03-11 15:45:20 +0000 | [diff] [blame] | 211 | void BlockingMutex::CheckLocked() { |
| 212 | CHECK_EQ((uptr)pthread_self(), owner_); |
| 213 | } |
| 214 | |
| Dmitry Vyukov | c9e304a | 2013-06-06 13:00:32 +0000 | [diff] [blame] | 215 | u64 NanoTime() { |
| 216 | return 0; |
| 217 | } |
| 218 | |
| Evgeniy Stepanov | 5697b58 | 2013-03-13 08:19:53 +0000 | [diff] [blame] | 219 | uptr GetTlsSize() { |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | void InitTlsSize() { |
| 224 | } |
| 225 | |
| Sergey Matveev | 954c6ef1 | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 226 | void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size, |
| 227 | uptr *tls_addr, uptr *tls_size) { |
| Dmitry Vyukov | ce0247c | 2013-06-06 13:20:40 +0000 | [diff] [blame] | 228 | #ifndef SANITIZER_GO |
| Sergey Matveev | 954c6ef1 | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 229 | uptr stack_top, stack_bottom; |
| 230 | GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom); |
| 231 | *stk_addr = stack_bottom; |
| 232 | *stk_size = stack_top - stack_bottom; |
| 233 | *tls_addr = 0; |
| 234 | *tls_size = 0; |
| Dmitry Vyukov | ce0247c | 2013-06-06 13:20:40 +0000 | [diff] [blame] | 235 | #else |
| 236 | *stk_addr = 0; |
| 237 | *stk_size = 0; |
| 238 | *tls_addr = 0; |
| 239 | *tls_size = 0; |
| 240 | #endif |
| Sergey Matveev | 954c6ef1 | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| Alexey Samsonov | 7a36e61 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 243 | uptr GetListOfModules(LoadedModule *modules, uptr max_modules, |
| 244 | string_predicate_t filter) { |
| 245 | MemoryMappingLayout memory_mapping(false); |
| Alexey Samsonov | 64ffa59 | 2013-12-25 08:39:38 +0000 | [diff] [blame] | 246 | return memory_mapping.DumpListOfModules(modules, max_modules, filter); |
| Alexey Samsonov | 7a36e61 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| Alexander Potapenko | 789e3e1 | 2014-01-31 13:10:07 +0000 | [diff] [blame] | 249 | bool IsDeadlySignal(int signum) { |
| 250 | return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv; |
| 251 | } |
| 252 | |
| Alexander Potapenko | 768e315 | 2014-02-03 15:32:19 +0000 | [diff] [blame] | 253 | MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED; |
| 254 | |
| 255 | MacosVersion GetMacosVersionInternal() { |
| 256 | int mib[2] = { CTL_KERN, KERN_OSRELEASE }; |
| 257 | char version[100]; |
| 258 | uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]); |
| 259 | for (uptr i = 0; i < maxlen; i++) version[i] = '\0'; |
| 260 | // Get the version length. |
| 261 | CHECK_NE(sysctl(mib, 2, 0, &len, 0, 0), -1); |
| 262 | CHECK_LT(len, maxlen); |
| 263 | CHECK_NE(sysctl(mib, 2, version, &len, 0, 0), -1); |
| 264 | switch (version[0]) { |
| 265 | case '9': return MACOS_VERSION_LEOPARD; |
| 266 | case '1': { |
| 267 | switch (version[1]) { |
| 268 | case '0': return MACOS_VERSION_SNOW_LEOPARD; |
| 269 | case '1': return MACOS_VERSION_LION; |
| 270 | case '2': return MACOS_VERSION_MOUNTAIN_LION; |
| 271 | case '3': return MACOS_VERSION_MAVERICKS; |
| 272 | default: return MACOS_VERSION_UNKNOWN; |
| 273 | } |
| 274 | } |
| 275 | default: return MACOS_VERSION_UNKNOWN; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | MacosVersion GetMacosVersion() { |
| 280 | atomic_uint32_t *cache = |
| 281 | reinterpret_cast<atomic_uint32_t*>(&cached_macos_version); |
| 282 | MacosVersion result = |
| 283 | static_cast<MacosVersion>(atomic_load(cache, memory_order_acquire)); |
| 284 | if (result == MACOS_VERSION_UNINITIALIZED) { |
| 285 | result = GetMacosVersionInternal(); |
| 286 | atomic_store(cache, result, memory_order_release); |
| 287 | } |
| 288 | return result; |
| 289 | } |
| 290 | |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 291 | } // namespace __sanitizer |
| 292 | |
| Alexey Samsonov | a0e28a7 | 2013-04-03 07:24:35 +0000 | [diff] [blame] | 293 | #endif // SANITIZER_MAC |