blob: 3cc3a6b5b9efebfc48007e442739ffe9bd2e140a [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//
10// This file is shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries and implements mac-specific functions from
12// sanitizer_libc.h.
13//===----------------------------------------------------------------------===//
14
Evgeniy Stepanov0af67232013-03-19 14:33:38 +000015#include "sanitizer_platform.h"
16#if SANITIZER_MAC
17
Alexander Potapenkod895ae92013-02-06 14:41:15 +000018// 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 Samsonov2c5fc3b2012-06-04 14:27:50 +000024
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000025#include "sanitizer_common.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"
Alexey Samsonov7a36e612013-09-10 14:36:16 +000028#include "sanitizer_placement_new.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000029#include "sanitizer_procmaps.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000030
Alexey Samsonov0c53a382012-06-14 14:07:21 +000031#include <crt_externs.h> // for _NSGetEnviron
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000032#include <fcntl.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000033#include <pthread.h>
Alexey Samsonov58a3c582012-06-18 08:44:30 +000034#include <sched.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000035#include <sys/mman.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000036#include <sys/resource.h>
Alexey Samsonovdde1f112012-06-05 07:05:10 +000037#include <sys/stat.h>
38#include <sys/types.h>
Alexey Samsonov03c8b842012-06-05 08:32:53 +000039#include <unistd.h>
Dmitry Vyukovaf4b0b02013-01-14 08:01:58 +000040#include <libkern/OSAtomic.h>
Peter Collingbourne8e110ce2013-05-08 15:07:12 +000041#include <errno.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000042
43namespace __sanitizer {
44
Peter Collingbourne6f4be192013-05-08 14:43:49 +000045#include "sanitizer_syscall_generic.inc"
46
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000047// ---------------------- sanitizer_libc.h
Peter Collingbourne8e110ce2013-05-08 15:07:12 +000048uptr internal_mmap(void *addr, size_t length, int prot, int flags,
49 int fd, u64 offset) {
50 return (uptr)mmap(addr, length, prot, flags, fd, offset);
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000051}
52
Peter Collingbourne8e110ce2013-05-08 15:07:12 +000053uptr internal_munmap(void *addr, uptr length) {
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000054 return munmap(addr, length);
55}
56
Peter Collingbourne6f4be192013-05-08 14:43:49 +000057uptr internal_close(fd_t fd) {
Alexey Samsonov03c8b842012-06-05 08:32:53 +000058 return close(fd);
59}
60
Peter Collingbourne6f4be192013-05-08 14:43:49 +000061uptr internal_open(const char *filename, int flags) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000062 return open(filename, flags);
63}
64
Peter Collingbourne6f4be192013-05-08 14:43:49 +000065uptr internal_open(const char *filename, int flags, u32 mode) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000066 return open(filename, flags, mode);
67}
68
Peter Collingbourne6f4be192013-05-08 14:43:49 +000069uptr OpenFile(const char *filename, bool write) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000070 return internal_open(filename,
71 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
Alexey Samsonovdde1f112012-06-05 07:05:10 +000072}
73
Alexey Samsonov03c8b842012-06-05 08:32:53 +000074uptr internal_read(fd_t fd, void *buf, uptr count) {
75 return read(fd, buf, count);
76}
77
78uptr internal_write(fd_t fd, const void *buf, uptr count) {
79 return write(fd, buf, count);
80}
81
Peter Collingbourne6f4be192013-05-08 14:43:49 +000082uptr internal_stat(const char *path, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000083 return stat(path, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000084}
85
Peter Collingbourne6f4be192013-05-08 14:43:49 +000086uptr internal_lstat(const char *path, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000087 return lstat(path, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000088}
89
Peter Collingbourne6f4be192013-05-08 14:43:49 +000090uptr internal_fstat(fd_t fd, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000091 return fstat(fd, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000092}
93
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000094uptr internal_filesize(fd_t fd) {
Alexey Samsonovce8d4972012-08-02 10:09:31 +000095 struct stat st;
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000096 if (internal_fstat(fd, &st))
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000097 return -1;
98 return (uptr)st.st_size;
99}
100
Peter Collingbourne6f4be192013-05-08 14:43:49 +0000101uptr internal_dup2(int oldfd, int newfd) {
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000102 return dup2(oldfd, newfd);
103}
104
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000105uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
106 return readlink(path, buf, bufsize);
107}
108
Peter Collingbourne6f4be192013-05-08 14:43:49 +0000109uptr internal_sched_yield() {
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000110 return sched_yield();
111}
112
Alexey Samsonovaadd1f22013-02-20 13:54:32 +0000113void internal__exit(int exitcode) {
114 _exit(exitcode);
115}
116
Peter Collingbourneffaf2ea2013-05-17 16:56:53 +0000117uptr internal_getpid() {
118 return getpid();
119}
120
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000121// ----------------- sanitizer_common.h
Alexey Samsonovae9b18b2012-11-09 14:45:30 +0000122bool FileExists(const char *filename) {
123 struct stat st;
124 if (stat(filename, &st))
125 return false;
126 // Sanity check: filename is a regular file.
127 return S_ISREG(st.st_mode);
128}
129
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000130uptr GetTid() {
131 return reinterpret_cast<uptr>(pthread_self());
132}
133
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000134void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000135 uptr *stack_bottom) {
136 CHECK(stack_top);
137 CHECK(stack_bottom);
138 uptr stacksize = pthread_get_stacksize_np(pthread_self());
139 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
140 *stack_top = (uptr)stackaddr;
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000141 *stack_bottom = *stack_top - stacksize;
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000142}
143
Alexey Samsonov0c53a382012-06-14 14:07:21 +0000144const char *GetEnv(const char *name) {
145 char ***env_ptr = _NSGetEnviron();
Alexander Potapenkofa82ba92013-11-13 13:34:53 +0000146 if (!env_ptr) {
147 Report("_NSGetEnviron() returned NULL. Please make sure __asan_init() is "
148 "called after libSystem_initializer().\n");
149 CHECK(env_ptr);
150 }
Alexey Samsonov0c53a382012-06-14 14:07:21 +0000151 char **environ = *env_ptr;
152 CHECK(environ);
153 uptr name_len = internal_strlen(name);
154 while (*environ != 0) {
155 uptr len = internal_strlen(*environ);
156 if (len > name_len) {
157 const char *p = *environ;
158 if (!internal_memcmp(p, name, name_len) &&
159 p[name_len] == '=') { // Match.
160 return *environ + name_len + 1; // String starting after =.
161 }
162 }
163 environ++;
164 }
165 return 0;
166}
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000167
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000168void ReExec() {
169 UNIMPLEMENTED();
170}
171
Alexander Potapenko1746f552012-12-10 13:10:40 +0000172void PrepareForSandboxing() {
173 // Nothing here for now.
174}
175
Peter Collingbourneb69b8a42013-05-20 17:05:29 +0000176uptr GetPageSize() {
177 return sysconf(_SC_PAGESIZE);
178}
179
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000180BlockingMutex::BlockingMutex(LinkerInitialized) {
181 // We assume that OS_SPINLOCK_INIT is zero
182}
183
Alexey Samsonova097f7b2013-03-14 13:30:56 +0000184BlockingMutex::BlockingMutex() {
185 internal_memset(this, 0, sizeof(*this));
186}
187
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000188void BlockingMutex::Lock() {
189 CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
Kostya Serebryany459df6f2013-02-26 12:59:06 +0000190 CHECK_EQ(OS_SPINLOCK_INIT, 0);
191 CHECK_NE(owner_, (uptr)pthread_self());
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000192 OSSpinLockLock((OSSpinLock*)&opaque_storage_);
193 CHECK(!owner_);
194 owner_ = (uptr)pthread_self();
195}
196
197void BlockingMutex::Unlock() {
198 CHECK(owner_ == (uptr)pthread_self());
199 owner_ = 0;
200 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
201}
202
Alexey Samsonovdb7d9652013-03-11 15:45:20 +0000203void BlockingMutex::CheckLocked() {
204 CHECK_EQ((uptr)pthread_self(), owner_);
205}
206
Dmitry Vyukovc9e304a2013-06-06 13:00:32 +0000207u64 NanoTime() {
208 return 0;
209}
210
Evgeniy Stepanov5697b582013-03-13 08:19:53 +0000211uptr GetTlsSize() {
212 return 0;
213}
214
215void InitTlsSize() {
216}
217
Sergey Matveev954c6ef12013-05-07 14:41:43 +0000218void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
219 uptr *tls_addr, uptr *tls_size) {
Dmitry Vyukovce0247c2013-06-06 13:20:40 +0000220#ifndef SANITIZER_GO
Sergey Matveev954c6ef12013-05-07 14:41:43 +0000221 uptr stack_top, stack_bottom;
222 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
223 *stk_addr = stack_bottom;
224 *stk_size = stack_top - stack_bottom;
225 *tls_addr = 0;
226 *tls_size = 0;
Dmitry Vyukovce0247c2013-06-06 13:20:40 +0000227#else
228 *stk_addr = 0;
229 *stk_size = 0;
230 *tls_addr = 0;
231 *tls_size = 0;
232#endif
Sergey Matveev954c6ef12013-05-07 14:41:43 +0000233}
234
Alexey Samsonov7a36e612013-09-10 14:36:16 +0000235uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
236 string_predicate_t filter) {
237 MemoryMappingLayout memory_mapping(false);
Alexey Samsonov64ffa592013-12-25 08:39:38 +0000238 return memory_mapping.DumpListOfModules(modules, max_modules, filter);
Alexey Samsonov7a36e612013-09-10 14:36:16 +0000239}
240
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +0000241} // namespace __sanitizer
242
Alexey Samsonova0e28a72013-04-03 07:24:35 +0000243#endif // SANITIZER_MAC