]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | // | |
10 | // This file is shared between various sanitizers' runtime libraries and | |
11 | // implements OSX-specific functions. | |
12 | //===----------------------------------------------------------------------===// | |
13 | ||
14 | #include "sanitizer_platform.h" | |
15 | #if SANITIZER_MAC | |
16 | ||
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> | |
23 | ||
24 | #include "sanitizer_common.h" | |
25 | #include "sanitizer_flags.h" | |
26 | #include "sanitizer_internal_defs.h" | |
27 | #include "sanitizer_libc.h" | |
28 | #include "sanitizer_mac.h" | |
29 | #include "sanitizer_placement_new.h" | |
92a42be0 | 30 | #include "sanitizer_platform_limits_posix.h" |
1a4d82fc JJ |
31 | #include "sanitizer_procmaps.h" |
32 | ||
92a42be0 | 33 | #if !SANITIZER_IOS |
1a4d82fc | 34 | #include <crt_externs.h> // for _NSGetEnviron |
92a42be0 SL |
35 | #else |
36 | extern char **environ; | |
37 | #endif | |
38 | ||
39 | #include <errno.h> | |
1a4d82fc | 40 | #include <fcntl.h> |
92a42be0 SL |
41 | #include <libkern/OSAtomic.h> |
42 | #include <mach-o/dyld.h> | |
43 | #include <mach/mach.h> | |
44 | #include <mach/vm_statistics.h> | |
1a4d82fc JJ |
45 | #include <pthread.h> |
46 | #include <sched.h> | |
47 | #include <signal.h> | |
92a42be0 | 48 | #include <stdlib.h> |
1a4d82fc JJ |
49 | #include <sys/mman.h> |
50 | #include <sys/resource.h> | |
51 | #include <sys/stat.h> | |
52 | #include <sys/sysctl.h> | |
53 | #include <sys/types.h> | |
54 | #include <unistd.h> | |
1a4d82fc JJ |
55 | |
56 | namespace __sanitizer { | |
57 | ||
58 | #include "sanitizer_syscall_generic.inc" | |
59 | ||
60 | // ---------------------- sanitizer_libc.h | |
61 | uptr internal_mmap(void *addr, size_t length, int prot, int flags, | |
62 | int fd, u64 offset) { | |
92a42be0 | 63 | if (fd == -1) fd = VM_MAKE_TAG(VM_MEMORY_ANALYSIS_TOOL); |
1a4d82fc JJ |
64 | return (uptr)mmap(addr, length, prot, flags, fd, offset); |
65 | } | |
66 | ||
67 | uptr internal_munmap(void *addr, uptr length) { | |
68 | return munmap(addr, length); | |
69 | } | |
70 | ||
92a42be0 SL |
71 | int internal_mprotect(void *addr, uptr length, int prot) { |
72 | return mprotect(addr, length, prot); | |
73 | } | |
74 | ||
1a4d82fc JJ |
75 | uptr internal_close(fd_t fd) { |
76 | return close(fd); | |
77 | } | |
78 | ||
79 | uptr internal_open(const char *filename, int flags) { | |
80 | return open(filename, flags); | |
81 | } | |
82 | ||
83 | uptr internal_open(const char *filename, int flags, u32 mode) { | |
84 | return open(filename, flags, mode); | |
85 | } | |
86 | ||
1a4d82fc JJ |
87 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
88 | return read(fd, buf, count); | |
89 | } | |
90 | ||
91 | uptr internal_write(fd_t fd, const void *buf, uptr count) { | |
92 | return write(fd, buf, count); | |
93 | } | |
94 | ||
95 | uptr internal_stat(const char *path, void *buf) { | |
96 | return stat(path, (struct stat *)buf); | |
97 | } | |
98 | ||
99 | uptr internal_lstat(const char *path, void *buf) { | |
100 | return lstat(path, (struct stat *)buf); | |
101 | } | |
102 | ||
103 | uptr internal_fstat(fd_t fd, void *buf) { | |
104 | return fstat(fd, (struct stat *)buf); | |
105 | } | |
106 | ||
107 | uptr internal_filesize(fd_t fd) { | |
108 | struct stat st; | |
109 | if (internal_fstat(fd, &st)) | |
110 | return -1; | |
111 | return (uptr)st.st_size; | |
112 | } | |
113 | ||
114 | uptr internal_dup2(int oldfd, int newfd) { | |
115 | return dup2(oldfd, newfd); | |
116 | } | |
117 | ||
118 | uptr internal_readlink(const char *path, char *buf, uptr bufsize) { | |
119 | return readlink(path, buf, bufsize); | |
120 | } | |
121 | ||
92a42be0 SL |
122 | uptr internal_unlink(const char *path) { |
123 | return unlink(path); | |
124 | } | |
125 | ||
1a4d82fc JJ |
126 | uptr internal_sched_yield() { |
127 | return sched_yield(); | |
128 | } | |
129 | ||
130 | void internal__exit(int exitcode) { | |
131 | _exit(exitcode); | |
132 | } | |
133 | ||
134 | uptr internal_getpid() { | |
135 | return getpid(); | |
136 | } | |
137 | ||
138 | int internal_sigaction(int signum, const void *act, void *oldact) { | |
139 | return sigaction(signum, | |
140 | (struct sigaction *)act, (struct sigaction *)oldact); | |
141 | } | |
142 | ||
92a42be0 SL |
143 | void internal_sigfillset(__sanitizer_sigset_t *set) { sigfillset(set); } |
144 | ||
145 | uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set, | |
146 | __sanitizer_sigset_t *oldset) { | |
147 | return sigprocmask(how, set, oldset); | |
148 | } | |
149 | ||
150 | int internal_fork() { | |
151 | // TODO(glider): this may call user's pthread_atfork() handlers which is bad. | |
152 | return fork(); | |
153 | } | |
154 | ||
155 | uptr internal_rename(const char *oldpath, const char *newpath) { | |
156 | return rename(oldpath, newpath); | |
157 | } | |
158 | ||
159 | uptr internal_ftruncate(fd_t fd, uptr size) { | |
160 | return ftruncate(fd, size); | |
161 | } | |
162 | ||
1a4d82fc JJ |
163 | // ----------------- sanitizer_common.h |
164 | bool FileExists(const char *filename) { | |
165 | struct stat st; | |
166 | if (stat(filename, &st)) | |
167 | return false; | |
168 | // Sanity check: filename is a regular file. | |
169 | return S_ISREG(st.st_mode); | |
170 | } | |
171 | ||
172 | uptr GetTid() { | |
173 | return reinterpret_cast<uptr>(pthread_self()); | |
174 | } | |
175 | ||
176 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, | |
177 | uptr *stack_bottom) { | |
178 | CHECK(stack_top); | |
179 | CHECK(stack_bottom); | |
180 | uptr stacksize = pthread_get_stacksize_np(pthread_self()); | |
181 | // pthread_get_stacksize_np() returns an incorrect stack size for the main | |
182 | // thread on Mavericks. See | |
183 | // https://code.google.com/p/address-sanitizer/issues/detail?id=261 | |
92a42be0 | 184 | if ((GetMacosVersion() >= MACOS_VERSION_MAVERICKS) && at_initialization && |
1a4d82fc JJ |
185 | stacksize == (1 << 19)) { |
186 | struct rlimit rl; | |
187 | CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0); | |
188 | // Most often rl.rlim_cur will be the desired 8M. | |
189 | if (rl.rlim_cur < kMaxThreadStackSize) { | |
190 | stacksize = rl.rlim_cur; | |
191 | } else { | |
192 | stacksize = kMaxThreadStackSize; | |
193 | } | |
194 | } | |
195 | void *stackaddr = pthread_get_stackaddr_np(pthread_self()); | |
196 | *stack_top = (uptr)stackaddr; | |
197 | *stack_bottom = *stack_top - stacksize; | |
198 | } | |
199 | ||
92a42be0 SL |
200 | char **GetEnviron() { |
201 | #if !SANITIZER_IOS | |
1a4d82fc JJ |
202 | char ***env_ptr = _NSGetEnviron(); |
203 | if (!env_ptr) { | |
204 | Report("_NSGetEnviron() returned NULL. Please make sure __asan_init() is " | |
205 | "called after libSystem_initializer().\n"); | |
206 | CHECK(env_ptr); | |
207 | } | |
208 | char **environ = *env_ptr; | |
92a42be0 | 209 | #endif |
1a4d82fc | 210 | CHECK(environ); |
92a42be0 SL |
211 | return environ; |
212 | } | |
213 | ||
214 | const char *GetEnv(const char *name) { | |
215 | char **env = GetEnviron(); | |
1a4d82fc | 216 | uptr name_len = internal_strlen(name); |
92a42be0 SL |
217 | while (*env != 0) { |
218 | uptr len = internal_strlen(*env); | |
1a4d82fc | 219 | if (len > name_len) { |
92a42be0 | 220 | const char *p = *env; |
1a4d82fc JJ |
221 | if (!internal_memcmp(p, name, name_len) && |
222 | p[name_len] == '=') { // Match. | |
92a42be0 | 223 | return *env + name_len + 1; // String starting after =. |
1a4d82fc JJ |
224 | } |
225 | } | |
92a42be0 | 226 | env++; |
1a4d82fc JJ |
227 | } |
228 | return 0; | |
229 | } | |
230 | ||
92a42be0 SL |
231 | uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) { |
232 | CHECK_LE(kMaxPathLength, buf_len); | |
233 | ||
234 | // On OS X the executable path is saved to the stack by dyld. Reading it | |
235 | // from there is much faster than calling dladdr, especially for large | |
236 | // binaries with symbols. | |
237 | InternalScopedString exe_path(kMaxPathLength); | |
238 | uint32_t size = exe_path.size(); | |
239 | if (_NSGetExecutablePath(exe_path.data(), &size) == 0 && | |
240 | realpath(exe_path.data(), buf) != 0) { | |
241 | return internal_strlen(buf); | |
242 | } | |
243 | return 0; | |
1a4d82fc JJ |
244 | } |
245 | ||
92a42be0 SL |
246 | uptr ReadLongProcessName(/*out*/char *buf, uptr buf_len) { |
247 | return ReadBinaryName(buf, buf_len); | |
1a4d82fc JJ |
248 | } |
249 | ||
92a42be0 SL |
250 | void ReExec() { |
251 | UNIMPLEMENTED(); | |
1a4d82fc JJ |
252 | } |
253 | ||
92a42be0 SL |
254 | uptr GetPageSize() { |
255 | return sysconf(_SC_PAGESIZE); | |
1a4d82fc JJ |
256 | } |
257 | ||
258 | BlockingMutex::BlockingMutex() { | |
259 | internal_memset(this, 0, sizeof(*this)); | |
260 | } | |
261 | ||
262 | void BlockingMutex::Lock() { | |
263 | CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_)); | |
264 | CHECK_EQ(OS_SPINLOCK_INIT, 0); | |
265 | CHECK_NE(owner_, (uptr)pthread_self()); | |
266 | OSSpinLockLock((OSSpinLock*)&opaque_storage_); | |
267 | CHECK(!owner_); | |
268 | owner_ = (uptr)pthread_self(); | |
269 | } | |
270 | ||
271 | void BlockingMutex::Unlock() { | |
272 | CHECK(owner_ == (uptr)pthread_self()); | |
273 | owner_ = 0; | |
274 | OSSpinLockUnlock((OSSpinLock*)&opaque_storage_); | |
275 | } | |
276 | ||
277 | void BlockingMutex::CheckLocked() { | |
278 | CHECK_EQ((uptr)pthread_self(), owner_); | |
279 | } | |
280 | ||
281 | u64 NanoTime() { | |
282 | return 0; | |
283 | } | |
284 | ||
285 | uptr GetTlsSize() { | |
286 | return 0; | |
287 | } | |
288 | ||
289 | void InitTlsSize() { | |
290 | } | |
291 | ||
292 | void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size, | |
293 | uptr *tls_addr, uptr *tls_size) { | |
294 | #ifndef SANITIZER_GO | |
295 | uptr stack_top, stack_bottom; | |
296 | GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom); | |
297 | *stk_addr = stack_bottom; | |
298 | *stk_size = stack_top - stack_bottom; | |
299 | *tls_addr = 0; | |
300 | *tls_size = 0; | |
301 | #else | |
302 | *stk_addr = 0; | |
303 | *stk_size = 0; | |
304 | *tls_addr = 0; | |
305 | *tls_size = 0; | |
306 | #endif | |
307 | } | |
308 | ||
309 | uptr GetListOfModules(LoadedModule *modules, uptr max_modules, | |
310 | string_predicate_t filter) { | |
311 | MemoryMappingLayout memory_mapping(false); | |
312 | return memory_mapping.DumpListOfModules(modules, max_modules, filter); | |
313 | } | |
314 | ||
315 | bool IsDeadlySignal(int signum) { | |
316 | return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv; | |
317 | } | |
318 | ||
319 | MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED; | |
320 | ||
321 | MacosVersion GetMacosVersionInternal() { | |
322 | int mib[2] = { CTL_KERN, KERN_OSRELEASE }; | |
323 | char version[100]; | |
324 | uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]); | |
325 | for (uptr i = 0; i < maxlen; i++) version[i] = '\0'; | |
326 | // Get the version length. | |
327 | CHECK_NE(sysctl(mib, 2, 0, &len, 0, 0), -1); | |
328 | CHECK_LT(len, maxlen); | |
329 | CHECK_NE(sysctl(mib, 2, version, &len, 0, 0), -1); | |
330 | switch (version[0]) { | |
331 | case '9': return MACOS_VERSION_LEOPARD; | |
332 | case '1': { | |
333 | switch (version[1]) { | |
334 | case '0': return MACOS_VERSION_SNOW_LEOPARD; | |
335 | case '1': return MACOS_VERSION_LION; | |
336 | case '2': return MACOS_VERSION_MOUNTAIN_LION; | |
337 | case '3': return MACOS_VERSION_MAVERICKS; | |
92a42be0 SL |
338 | case '4': return MACOS_VERSION_YOSEMITE; |
339 | default: | |
340 | if (IsDigit(version[1])) | |
341 | return MACOS_VERSION_UNKNOWN_NEWER; | |
342 | else | |
343 | return MACOS_VERSION_UNKNOWN; | |
1a4d82fc JJ |
344 | } |
345 | } | |
346 | default: return MACOS_VERSION_UNKNOWN; | |
347 | } | |
348 | } | |
349 | ||
350 | MacosVersion GetMacosVersion() { | |
351 | atomic_uint32_t *cache = | |
352 | reinterpret_cast<atomic_uint32_t*>(&cached_macos_version); | |
353 | MacosVersion result = | |
354 | static_cast<MacosVersion>(atomic_load(cache, memory_order_acquire)); | |
355 | if (result == MACOS_VERSION_UNINITIALIZED) { | |
356 | result = GetMacosVersionInternal(); | |
357 | atomic_store(cache, result, memory_order_release); | |
358 | } | |
359 | return result; | |
360 | } | |
361 | ||
92a42be0 SL |
362 | uptr GetRSS() { |
363 | struct task_basic_info info; | |
364 | unsigned count = TASK_BASIC_INFO_COUNT; | |
365 | kern_return_t result = | |
366 | task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count); | |
367 | if (UNLIKELY(result != KERN_SUCCESS)) { | |
368 | Report("Cannot get task info. Error: %d\n", result); | |
369 | Die(); | |
370 | } | |
371 | return info.resident_size; | |
372 | } | |
373 | ||
374 | void *internal_start_thread(void (*func)(void *arg), void *arg) { return 0; } | |
375 | void internal_join_thread(void *th) { } | |
376 | ||
377 | void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { | |
378 | ucontext_t *ucontext = (ucontext_t*)context; | |
379 | # if defined(__aarch64__) | |
380 | *pc = ucontext->uc_mcontext->__ss.__pc; | |
381 | # if defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 | |
382 | *bp = ucontext->uc_mcontext->__ss.__fp; | |
383 | # else | |
384 | *bp = ucontext->uc_mcontext->__ss.__lr; | |
385 | # endif | |
386 | *sp = ucontext->uc_mcontext->__ss.__sp; | |
387 | # elif defined(__x86_64__) | |
388 | *pc = ucontext->uc_mcontext->__ss.__rip; | |
389 | *bp = ucontext->uc_mcontext->__ss.__rbp; | |
390 | *sp = ucontext->uc_mcontext->__ss.__rsp; | |
391 | # elif defined(__arm__) | |
392 | *pc = ucontext->uc_mcontext->__ss.__pc; | |
393 | *bp = ucontext->uc_mcontext->__ss.__r[7]; | |
394 | *sp = ucontext->uc_mcontext->__ss.__sp; | |
395 | # elif defined(__i386__) | |
396 | *pc = ucontext->uc_mcontext->__ss.__eip; | |
397 | *bp = ucontext->uc_mcontext->__ss.__ebp; | |
398 | *sp = ucontext->uc_mcontext->__ss.__esp; | |
399 | # else | |
400 | # error "Unknown architecture" | |
401 | # endif | |
402 | } | |
403 | ||
1a4d82fc JJ |
404 | } // namespace __sanitizer |
405 | ||
406 | #endif // SANITIZER_MAC |