blob: b83096417840df5497ebefb0b036fe9e5fd04ca0 [file] [log] [blame]
Alexey Samsonov2c5fc3b2012-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//
Alexander Potapenko768e3152014-02-03 15:32:19 +000010// This file is shared between various sanitizers' runtime libraries and
11// implements OSX-specific functions.
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000012//===----------------------------------------------------------------------===//
13
Evgeniy Stepanov0af67232013-03-19 14:33:38 +000014#include "sanitizer_platform.h"
15#if SANITIZER_MAC
16
Alexander Potapenkod895ae92013-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 Samsonov2c5fc3b2012-06-04 14:27:50 +000023
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000024#include "sanitizer_common.h"
Alexander Potapenko789e3e12014-01-31 13:10:07 +000025#include "sanitizer_flags.h"
Alexey Samsonov5bbf8292012-06-05 14:25:27 +000026#include "sanitizer_internal_defs.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000027#include "sanitizer_libc.h"
Alexander Potapenko768e3152014-02-03 15:32:19 +000028#include "sanitizer_mac.h"
Alexey Samsonov7a36e612013-09-10 14:36:16 +000029#include "sanitizer_placement_new.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000030#include "sanitizer_procmaps.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000031
Alexey Samsonov0c53a382012-06-14 14:07:21 +000032#include <crt_externs.h> // for _NSGetEnviron
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000033#include <fcntl.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000034#include <pthread.h>
Alexey Samsonov58a3c582012-06-18 08:44:30 +000035#include <sched.h>
Alexander Potapenko789e3e12014-01-31 13:10:07 +000036#include <signal.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000037#include <sys/mman.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000038#include <sys/resource.h>
Alexey Samsonovdde1f112012-06-05 07:05:10 +000039#include <sys/stat.h>
Alexander Potapenko768e3152014-02-03 15:32:19 +000040#include <sys/sysctl.h>
Alexey Samsonovdde1f112012-06-05 07:05:10 +000041#include <sys/types.h>
Alexey Samsonov03c8b842012-06-05 08:32:53 +000042#include <unistd.h>
Dmitry Vyukovaf4b0b02013-01-14 08:01:58 +000043#include <libkern/OSAtomic.h>
Peter Collingbourne8e110ce2013-05-08 15:07:12 +000044#include <errno.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000045
46namespace __sanitizer {
47
Peter Collingbourne6f4be192013-05-08 14:43:49 +000048#include "sanitizer_syscall_generic.inc"
49
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000050// ---------------------- sanitizer_libc.h
Peter Collingbourne8e110ce2013-05-08 15:07:12 +000051uptr 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 Samsonov2c5fc3b2012-06-04 14:27:50 +000054}
55
Peter Collingbourne8e110ce2013-05-08 15:07:12 +000056uptr internal_munmap(void *addr, uptr length) {
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000057 return munmap(addr, length);
58}
59
Peter Collingbourne6f4be192013-05-08 14:43:49 +000060uptr internal_close(fd_t fd) {
Alexey Samsonov03c8b842012-06-05 08:32:53 +000061 return close(fd);
62}
63
Peter Collingbourne6f4be192013-05-08 14:43:49 +000064uptr internal_open(const char *filename, int flags) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000065 return open(filename, flags);
66}
67
Peter Collingbourne6f4be192013-05-08 14:43:49 +000068uptr internal_open(const char *filename, int flags, u32 mode) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000069 return open(filename, flags, mode);
70}
71
Peter Collingbourne6f4be192013-05-08 14:43:49 +000072uptr OpenFile(const char *filename, bool write) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000073 return internal_open(filename,
74 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
Alexey Samsonovdde1f112012-06-05 07:05:10 +000075}
76
Alexey Samsonov03c8b842012-06-05 08:32:53 +000077uptr internal_read(fd_t fd, void *buf, uptr count) {
78 return read(fd, buf, count);
79}
80
81uptr internal_write(fd_t fd, const void *buf, uptr count) {
82 return write(fd, buf, count);
83}
84
Peter Collingbourne6f4be192013-05-08 14:43:49 +000085uptr internal_stat(const char *path, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000086 return stat(path, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000087}
88
Peter Collingbourne6f4be192013-05-08 14:43:49 +000089uptr internal_lstat(const char *path, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000090 return lstat(path, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000091}
92
Peter Collingbourne6f4be192013-05-08 14:43:49 +000093uptr internal_fstat(fd_t fd, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000094 return fstat(fd, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000095}
96
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000097uptr internal_filesize(fd_t fd) {
Alexey Samsonovce8d4972012-08-02 10:09:31 +000098 struct stat st;
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000099 if (internal_fstat(fd, &st))
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000100 return -1;
101 return (uptr)st.st_size;
102}
103
Peter Collingbourne6f4be192013-05-08 14:43:49 +0000104uptr internal_dup2(int oldfd, int newfd) {
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000105 return dup2(oldfd, newfd);
106}
107
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000108uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
109 return readlink(path, buf, bufsize);
110}
111
Kuba Breckac52f3002014-12-29 02:18:59 +0000112uptr internal_unlink(const char *path) {
113 return unlink(path);
114}
115
Peter Collingbourne6f4be192013-05-08 14:43:49 +0000116uptr internal_sched_yield() {
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000117 return sched_yield();
118}
119
Alexey Samsonovaadd1f22013-02-20 13:54:32 +0000120void internal__exit(int exitcode) {
121 _exit(exitcode);
122}
123
Peter Collingbourneffaf2ea2013-05-17 16:56:53 +0000124uptr internal_getpid() {
125 return getpid();
126}
127
Alexander Potapenko789e3e12014-01-31 13:10:07 +0000128int internal_sigaction(int signum, const void *act, void *oldact) {
129 return sigaction(signum,
130 (struct sigaction *)act, (struct sigaction *)oldact);
131}
132
Alexander Potapenko4a6cac42014-05-13 16:17:54 +0000133int internal_fork() {
134 // TODO(glider): this may call user's pthread_atfork() handlers which is bad.
135 return fork();
136}
137
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000138uptr internal_rename(const char *oldpath, const char *newpath) {
139 return rename(oldpath, newpath);
140}
141
142uptr internal_ftruncate(fd_t fd, uptr size) {
143 return ftruncate(fd, size);
144}
145
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000146// ----------------- sanitizer_common.h
Alexey Samsonovae9b18b2012-11-09 14:45:30 +0000147bool FileExists(const char *filename) {
148 struct stat st;
149 if (stat(filename, &st))
150 return false;
151 // Sanity check: filename is a regular file.
152 return S_ISREG(st.st_mode);
153}
154
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000155uptr GetTid() {
156 return reinterpret_cast<uptr>(pthread_self());
157}
158
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000159void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000160 uptr *stack_bottom) {
161 CHECK(stack_top);
162 CHECK(stack_bottom);
163 uptr stacksize = pthread_get_stacksize_np(pthread_self());
Alexander Potapenkof6ff6b02014-02-03 16:42:29 +0000164 // pthread_get_stacksize_np() returns an incorrect stack size for the main
165 // thread on Mavericks. See
166 // https://code.google.com/p/address-sanitizer/issues/detail?id=261
Kuba Brecka0078cea72014-11-05 18:55:38 +0000167 if ((GetMacosVersion() >= MACOS_VERSION_MAVERICKS) && at_initialization &&
Alexander Potapenkof6ff6b02014-02-03 16:42:29 +0000168 stacksize == (1 << 19)) {
169 struct rlimit rl;
170 CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
171 // Most often rl.rlim_cur will be the desired 8M.
172 if (rl.rlim_cur < kMaxThreadStackSize) {
173 stacksize = rl.rlim_cur;
174 } else {
175 stacksize = kMaxThreadStackSize;
176 }
177 }
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000178 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
179 *stack_top = (uptr)stackaddr;
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000180 *stack_bottom = *stack_top - stacksize;
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000181}
182
Alexey Samsonov0c53a382012-06-14 14:07:21 +0000183const char *GetEnv(const char *name) {
184 char ***env_ptr = _NSGetEnviron();
Alexander Potapenkofa82ba92013-11-13 13:34:53 +0000185 if (!env_ptr) {
186 Report("_NSGetEnviron() returned NULL. Please make sure __asan_init() is "
187 "called after libSystem_initializer().\n");
188 CHECK(env_ptr);
189 }
Alexey Samsonov0c53a382012-06-14 14:07:21 +0000190 char **environ = *env_ptr;
191 CHECK(environ);
192 uptr name_len = internal_strlen(name);
193 while (*environ != 0) {
194 uptr len = internal_strlen(*environ);
195 if (len > name_len) {
196 const char *p = *environ;
197 if (!internal_memcmp(p, name, name_len) &&
198 p[name_len] == '=') { // Match.
199 return *environ + name_len + 1; // String starting after =.
200 }
201 }
202 environ++;
203 }
204 return 0;
205}
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000206
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000207void ReExec() {
208 UNIMPLEMENTED();
209}
210
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000211void PrepareForSandboxing(__sanitizer_sandbox_arguments *args) {
212 (void)args;
Alexander Potapenko1746f552012-12-10 13:10:40 +0000213 // Nothing here for now.
214}
215
Peter Collingbourneb69b8a42013-05-20 17:05:29 +0000216uptr GetPageSize() {
217 return sysconf(_SC_PAGESIZE);
218}
219
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000220BlockingMutex::BlockingMutex(LinkerInitialized) {
221 // We assume that OS_SPINLOCK_INIT is zero
222}
223
Alexey Samsonova097f7b2013-03-14 13:30:56 +0000224BlockingMutex::BlockingMutex() {
225 internal_memset(this, 0, sizeof(*this));
226}
227
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000228void BlockingMutex::Lock() {
229 CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
Kostya Serebryany459df6f2013-02-26 12:59:06 +0000230 CHECK_EQ(OS_SPINLOCK_INIT, 0);
231 CHECK_NE(owner_, (uptr)pthread_self());
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000232 OSSpinLockLock((OSSpinLock*)&opaque_storage_);
233 CHECK(!owner_);
234 owner_ = (uptr)pthread_self();
235}
236
237void BlockingMutex::Unlock() {
238 CHECK(owner_ == (uptr)pthread_self());
239 owner_ = 0;
240 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
241}
242
Alexey Samsonovdb7d9652013-03-11 15:45:20 +0000243void BlockingMutex::CheckLocked() {
244 CHECK_EQ((uptr)pthread_self(), owner_);
245}
246
Dmitry Vyukovc9e304a2013-06-06 13:00:32 +0000247u64 NanoTime() {
248 return 0;
249}
250
Evgeniy Stepanov5697b582013-03-13 08:19:53 +0000251uptr GetTlsSize() {
252 return 0;
253}
254
255void InitTlsSize() {
256}
257
Sergey Matveev954c6ef12013-05-07 14:41:43 +0000258void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
259 uptr *tls_addr, uptr *tls_size) {
Dmitry Vyukovce0247c2013-06-06 13:20:40 +0000260#ifndef SANITIZER_GO
Sergey Matveev954c6ef12013-05-07 14:41:43 +0000261 uptr stack_top, stack_bottom;
262 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
263 *stk_addr = stack_bottom;
264 *stk_size = stack_top - stack_bottom;
265 *tls_addr = 0;
266 *tls_size = 0;
Dmitry Vyukovce0247c2013-06-06 13:20:40 +0000267#else
268 *stk_addr = 0;
269 *stk_size = 0;
270 *tls_addr = 0;
271 *tls_size = 0;
272#endif
Sergey Matveev954c6ef12013-05-07 14:41:43 +0000273}
274
Alexey Samsonov7a36e612013-09-10 14:36:16 +0000275uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
276 string_predicate_t filter) {
277 MemoryMappingLayout memory_mapping(false);
Alexey Samsonov64ffa592013-12-25 08:39:38 +0000278 return memory_mapping.DumpListOfModules(modules, max_modules, filter);
Alexey Samsonov7a36e612013-09-10 14:36:16 +0000279}
280
Alexander Potapenko789e3e12014-01-31 13:10:07 +0000281bool IsDeadlySignal(int signum) {
282 return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv;
283}
284
Alexander Potapenko768e3152014-02-03 15:32:19 +0000285MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED;
286
287MacosVersion GetMacosVersionInternal() {
288 int mib[2] = { CTL_KERN, KERN_OSRELEASE };
289 char version[100];
290 uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]);
291 for (uptr i = 0; i < maxlen; i++) version[i] = '\0';
292 // Get the version length.
293 CHECK_NE(sysctl(mib, 2, 0, &len, 0, 0), -1);
294 CHECK_LT(len, maxlen);
295 CHECK_NE(sysctl(mib, 2, version, &len, 0, 0), -1);
296 switch (version[0]) {
297 case '9': return MACOS_VERSION_LEOPARD;
298 case '1': {
299 switch (version[1]) {
300 case '0': return MACOS_VERSION_SNOW_LEOPARD;
301 case '1': return MACOS_VERSION_LION;
302 case '2': return MACOS_VERSION_MOUNTAIN_LION;
303 case '3': return MACOS_VERSION_MAVERICKS;
Kuba Breckaf4bdbde2014-11-05 18:53:22 +0000304 case '4': return MACOS_VERSION_YOSEMITE;
Kuba Brecka731089b2014-12-16 04:46:15 +0000305 default:
306 if (IsDigit(version[1]))
307 return MACOS_VERSION_UNKNOWN_NEWER;
308 else
309 return MACOS_VERSION_UNKNOWN;
Alexander Potapenko768e3152014-02-03 15:32:19 +0000310 }
311 }
312 default: return MACOS_VERSION_UNKNOWN;
313 }
314}
315
316MacosVersion GetMacosVersion() {
317 atomic_uint32_t *cache =
318 reinterpret_cast<atomic_uint32_t*>(&cached_macos_version);
319 MacosVersion result =
320 static_cast<MacosVersion>(atomic_load(cache, memory_order_acquire));
321 if (result == MACOS_VERSION_UNINITIALIZED) {
322 result = GetMacosVersionInternal();
323 atomic_store(cache, result, memory_order_release);
324 }
325 return result;
326}
327
Kostya Serebryany6c54a6b2014-12-09 01:22:59 +0000328uptr GetRSS() {
329 return 0;
330}
331
Kostya Serebryany43eb7732014-12-16 19:13:01 +0000332void *internal_start_thread(void (*func)(void *arg), void *arg) { return 0; }
Kostya Serebryany5f5bc4a2014-12-16 21:06:07 +0000333void internal_join_thread(void *th) { }
Kostya Serebryany43eb7732014-12-16 19:13:01 +0000334
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +0000335} // namespace __sanitizer
336
Alexey Samsonova0e28a72013-04-03 07:24:35 +0000337#endif // SANITIZER_MAC