blob: 461510561014ee6b55826920903e26297601941d [file] [log] [blame]
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +00001//===-- 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 Hines2d1fdb22014-05-28 23:58:16 -070010// This file is shared between various sanitizers' runtime libraries and
11// implements OSX-specific functions.
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000012//===----------------------------------------------------------------------===//
13
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000014#include "sanitizer_platform.h"
15#if SANITIZER_MAC
16
Alexander Potapenko2bf0d032013-02-06 14:41:15 +000017// 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 Samsonovae4d9ca2012-06-04 14:27:50 +000023
Alexey Samsonove5931fd2012-06-07 07:13:46 +000024#include "sanitizer_common.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070025#include "sanitizer_flags.h"
Alexey Samsonov94b50362012-06-05 14:25:27 +000026#include "sanitizer_internal_defs.h"
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000027#include "sanitizer_libc.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070028#include "sanitizer_mac.h"
Alexey Samsonov7847d772013-09-10 14:36:16 +000029#include "sanitizer_placement_new.h"
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070030#include "sanitizer_platform_limits_posix.h"
Alexey Samsonov6895adc2012-06-07 06:15:12 +000031#include "sanitizer_procmaps.h"
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000032
Alexey Samsonov3dbeabb2012-06-14 14:07:21 +000033#include <crt_externs.h> // for _NSGetEnviron
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070034#include <errno.h>
Alexey Samsonove5931fd2012-06-07 07:13:46 +000035#include <fcntl.h>
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070036#include <libkern/OSAtomic.h>
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070037#include <mach-o/dyld.h>
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070038#include <mach/mach.h>
Alexey Samsonove5931fd2012-06-07 07:13:46 +000039#include <pthread.h>
Alexey Samsonov0969bcf2012-06-18 08:44:30 +000040#include <sched.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070041#include <signal.h>
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070042#include <stdlib.h>
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000043#include <sys/mman.h>
Alexey Samsonove5931fd2012-06-07 07:13:46 +000044#include <sys/resource.h>
Alexey Samsonovc5d46512012-06-05 07:05:10 +000045#include <sys/stat.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070046#include <sys/sysctl.h>
Alexey Samsonovc5d46512012-06-05 07:05:10 +000047#include <sys/types.h>
Alexey Samsonova56aefd2012-06-05 08:32:53 +000048#include <unistd.h>
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000049
50namespace __sanitizer {
51
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000052#include "sanitizer_syscall_generic.inc"
53
Alexey Samsonove5931fd2012-06-07 07:13:46 +000054// ---------------------- sanitizer_libc.h
Peter Collingbourne65246dc2013-05-08 15:07:12 +000055uptr 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 Samsonovae4d9ca2012-06-04 14:27:50 +000058}
59
Peter Collingbourne65246dc2013-05-08 15:07:12 +000060uptr internal_munmap(void *addr, uptr length) {
Alexey Samsonov1f11d312012-06-05 09:49:25 +000061 return munmap(addr, length);
62}
63
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070064int internal_mprotect(void *addr, uptr length, int prot) {
65 return mprotect(addr, length, prot);
66}
67
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000068uptr internal_close(fd_t fd) {
Alexey Samsonova56aefd2012-06-05 08:32:53 +000069 return close(fd);
70}
71
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000072uptr internal_open(const char *filename, int flags) {
Alexey Samsonovee7cc442013-02-01 15:58:46 +000073 return open(filename, flags);
74}
75
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000076uptr internal_open(const char *filename, int flags, u32 mode) {
Alexey Samsonovee7cc442013-02-01 15:58:46 +000077 return open(filename, flags, mode);
78}
79
Alexey Samsonova56aefd2012-06-05 08:32:53 +000080uptr internal_read(fd_t fd, void *buf, uptr count) {
81 return read(fd, buf, count);
82}
83
84uptr internal_write(fd_t fd, const void *buf, uptr count) {
85 return write(fd, buf, count);
86}
87
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000088uptr internal_stat(const char *path, void *buf) {
Alexey Samsonov7c50ee72013-02-04 10:31:39 +000089 return stat(path, (struct stat *)buf);
Alexey Samsonov4c9317a2013-02-04 10:16:50 +000090}
91
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000092uptr internal_lstat(const char *path, void *buf) {
Alexey Samsonov7c50ee72013-02-04 10:31:39 +000093 return lstat(path, (struct stat *)buf);
Alexey Samsonov4c9317a2013-02-04 10:16:50 +000094}
95
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000096uptr internal_fstat(fd_t fd, void *buf) {
Alexey Samsonov7c50ee72013-02-04 10:31:39 +000097 return fstat(fd, (struct stat *)buf);
Alexey Samsonov4c9317a2013-02-04 10:16:50 +000098}
99
Alexey Samsonov8e820fc2012-06-06 07:30:33 +0000100uptr internal_filesize(fd_t fd) {
Alexey Samsonov668accc2012-08-02 10:09:31 +0000101 struct stat st;
Alexey Samsonov4c9317a2013-02-04 10:16:50 +0000102 if (internal_fstat(fd, &st))
Alexey Samsonov8e820fc2012-06-06 07:30:33 +0000103 return -1;
104 return (uptr)st.st_size;
105}
106
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000107uptr internal_dup2(int oldfd, int newfd) {
Alexey Samsonov8e820fc2012-06-06 07:30:33 +0000108 return dup2(oldfd, newfd);
109}
110
Alexey Samsonovd1b8f582012-09-05 14:48:24 +0000111uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
112 return readlink(path, buf, bufsize);
113}
114
Stephen Hines86277eb2015-03-23 12:06:32 -0700115uptr internal_unlink(const char *path) {
116 return unlink(path);
117}
118
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000119uptr internal_sched_yield() {
Alexey Samsonov0969bcf2012-06-18 08:44:30 +0000120 return sched_yield();
121}
122
Alexey Samsonovf8822472013-02-20 13:54:32 +0000123void internal__exit(int exitcode) {
124 _exit(exitcode);
125}
126
Peter Collingbourne0b694fc2013-05-17 16:56:53 +0000127uptr internal_getpid() {
128 return getpid();
129}
130
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700131int internal_sigaction(int signum, const void *act, void *oldact) {
132 return sigaction(signum,
133 (struct sigaction *)act, (struct sigaction *)oldact);
134}
135
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700136void internal_sigfillset(__sanitizer_sigset_t *set) { sigfillset(set); }
137
138uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
139 __sanitizer_sigset_t *oldset) {
140 return sigprocmask(how, set, oldset);
141}
142
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700143int internal_fork() {
144 // TODO(glider): this may call user's pthread_atfork() handlers which is bad.
145 return fork();
146}
147
148uptr internal_rename(const char *oldpath, const char *newpath) {
149 return rename(oldpath, newpath);
150}
151
152uptr internal_ftruncate(fd_t fd, uptr size) {
153 return ftruncate(fd, size);
154}
155
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000156// ----------------- sanitizer_common.h
Alexey Samsonov93b4caf2012-11-09 14:45:30 +0000157bool 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 Vyukove0023f72012-10-02 12:58:14 +0000165uptr GetTid() {
166 return reinterpret_cast<uptr>(pthread_self());
167}
168
Alexey Samsonoved996f72012-06-07 07:32:00 +0000169void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000170 uptr *stack_bottom) {
171 CHECK(stack_top);
172 CHECK(stack_bottom);
173 uptr stacksize = pthread_get_stacksize_np(pthread_self());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700174 // 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 Hines6d186232014-11-26 17:56:19 -0800177 if ((GetMacosVersion() >= MACOS_VERSION_MAVERICKS) && at_initialization &&
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700178 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 Samsonove5931fd2012-06-07 07:13:46 +0000188 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
189 *stack_top = (uptr)stackaddr;
Alexey Samsonoved996f72012-06-07 07:32:00 +0000190 *stack_bottom = *stack_top - stacksize;
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000191}
192
Alexey Samsonov3dbeabb2012-06-14 14:07:21 +0000193const char *GetEnv(const char *name) {
194 char ***env_ptr = _NSGetEnviron();
Alexander Potapenkob527f7d2013-11-13 13:34:53 +0000195 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 Samsonov3dbeabb2012-06-14 14:07:21 +0000200 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 Samsonove5931fd2012-06-07 07:13:46 +0000216
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700217uptr 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 Samsonovd7e5bb42012-09-17 09:12:39 +0000232void ReExec() {
233 UNIMPLEMENTED();
234}
235
Peter Collingbourne4df343a2013-05-20 17:05:29 +0000236uptr GetPageSize() {
237 return sysconf(_SC_PAGESIZE);
238}
239
Alexey Samsonov93af5942013-03-14 13:30:56 +0000240BlockingMutex::BlockingMutex() {
241 internal_memset(this, 0, sizeof(*this));
242}
243
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +0000244void BlockingMutex::Lock() {
245 CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
Kostya Serebryanyf155fcc2013-02-26 12:59:06 +0000246 CHECK_EQ(OS_SPINLOCK_INIT, 0);
247 CHECK_NE(owner_, (uptr)pthread_self());
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +0000248 OSSpinLockLock((OSSpinLock*)&opaque_storage_);
249 CHECK(!owner_);
250 owner_ = (uptr)pthread_self();
251}
252
253void BlockingMutex::Unlock() {
254 CHECK(owner_ == (uptr)pthread_self());
255 owner_ = 0;
256 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
257}
258
Alexey Samsonovce700972013-03-11 15:45:20 +0000259void BlockingMutex::CheckLocked() {
260 CHECK_EQ((uptr)pthread_self(), owner_);
261}
262
Dmitry Vyukovd4bb4a62013-06-06 13:00:32 +0000263u64 NanoTime() {
264 return 0;
265}
266
Evgeniy Stepanovb114ed82013-03-13 08:19:53 +0000267uptr GetTlsSize() {
268 return 0;
269}
270
271void InitTlsSize() {
272}
273
Sergey Matveev24323de2013-05-07 14:41:43 +0000274void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
275 uptr *tls_addr, uptr *tls_size) {
Dmitry Vyukov4a95e332013-06-06 13:20:40 +0000276#ifndef SANITIZER_GO
Sergey Matveev24323de2013-05-07 14:41:43 +0000277 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 Vyukov4a95e332013-06-06 13:20:40 +0000283#else
284 *stk_addr = 0;
285 *stk_size = 0;
286 *tls_addr = 0;
287 *tls_size = 0;
288#endif
Sergey Matveev24323de2013-05-07 14:41:43 +0000289}
290
Alexey Samsonov7847d772013-09-10 14:36:16 +0000291uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
292 string_predicate_t filter) {
293 MemoryMappingLayout memory_mapping(false);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700294 return memory_mapping.DumpListOfModules(modules, max_modules, filter);
295}
296
297bool IsDeadlySignal(int signum) {
298 return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv;
299}
300
301MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED;
302
303MacosVersion 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 Hines6d186232014-11-26 17:56:19 -0800320 case '4': return MACOS_VERSION_YOSEMITE;
Stephen Hines86277eb2015-03-23 12:06:32 -0700321 default:
322 if (IsDigit(version[1]))
323 return MACOS_VERSION_UNKNOWN_NEWER;
324 else
325 return MACOS_VERSION_UNKNOWN;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700326 }
Alexey Samsonov7847d772013-09-10 14:36:16 +0000327 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700328 default: return MACOS_VERSION_UNKNOWN;
Alexey Samsonov7847d772013-09-10 14:36:16 +0000329 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700330}
331
332MacosVersion 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 Samsonov7847d772013-09-10 14:36:16 +0000342}
343
Stephen Hines86277eb2015-03-23 12:06:32 -0700344uptr GetRSS() {
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700345 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 Hines86277eb2015-03-23 12:06:32 -0700354}
355
356void *internal_start_thread(void (*func)(void *arg), void *arg) { return 0; }
357void internal_join_thread(void *th) { }
358
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700359void 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 Samsonovae4d9ca2012-06-04 14:27:50 +0000372} // namespace __sanitizer
373
Alexey Samsonov46f93952013-04-03 07:24:35 +0000374#endif // SANITIZER_MAC