1 //===-- sanitizer_mac.cpp -------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is shared between various sanitizers' runtime libraries and
10 // implements OSX-specific functions.
11 //===----------------------------------------------------------------------===//
12 
13 #include "sanitizer_platform.h"
14 #if SANITIZER_MAC
15 #include "sanitizer_mac.h"
16 #include "interception/interception.h"
17 
18 // 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>
24 
25 #include "sanitizer_common.h"
26 #include "sanitizer_file.h"
27 #include "sanitizer_flags.h"
28 #include "sanitizer_internal_defs.h"
29 #include "sanitizer_libc.h"
30 #include "sanitizer_placement_new.h"
31 #include "sanitizer_platform_limits_posix.h"
32 #include "sanitizer_procmaps.h"
33 
34 #if !SANITIZER_IOS
35 #include <crt_externs.h>  // for _NSGetEnviron
36 #else
37 extern char **environ;
38 #endif
39 
40 #if defined(__has_include) && __has_include(<os/trace.h>) && defined(__BLOCKS__)
41 #define SANITIZER_OS_TRACE 1
42 #include <os/trace.h>
43 #else
44 #define SANITIZER_OS_TRACE 0
45 #endif
46 
47 #if !SANITIZER_IOS
48 #include <crt_externs.h>  // for _NSGetArgv and _NSGetEnviron
49 #else
50 extern "C" {
51   extern char ***_NSGetArgv(void);
52 }
53 #endif
54 
55 #include <asl.h>
56 #include <dlfcn.h>  // for dladdr()
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <libkern/OSAtomic.h>
60 #include <mach-o/dyld.h>
61 #include <mach/mach.h>
62 #include <mach/mach_time.h>
63 #include <mach/vm_statistics.h>
64 #include <malloc/malloc.h>
65 #include <pthread.h>
66 #include <sched.h>
67 #include <signal.h>
68 #include <spawn.h>
69 #include <stdlib.h>
70 #include <sys/ioctl.h>
71 #include <sys/mman.h>
72 #include <sys/resource.h>
73 #include <sys/stat.h>
74 #include <sys/sysctl.h>
75 #include <sys/types.h>
76 #include <sys/wait.h>
77 #include <unistd.h>
78 #include <util.h>
79 
80 // From <crt_externs.h>, but we don't have that file on iOS.
81 extern "C" {
82   extern char ***_NSGetArgv(void);
83   extern char ***_NSGetEnviron(void);
84 }
85 
86 // From <mach/mach_vm.h>, but we don't have that file on iOS.
87 extern "C" {
88   extern kern_return_t mach_vm_region_recurse(
89     vm_map_t target_task,
90     mach_vm_address_t *address,
91     mach_vm_size_t *size,
92     natural_t *nesting_depth,
93     vm_region_recurse_info_t info,
94     mach_msg_type_number_t *infoCnt);
95 }
96 
97 namespace __sanitizer {
98 
99 #include "sanitizer_syscall_generic.inc"
100 
101 // Direct syscalls, don't call libmalloc hooks (but not available on 10.6).
102 extern "C" void *__mmap(void *addr, size_t len, int prot, int flags, int fildes,
103                         off_t off) SANITIZER_WEAK_ATTRIBUTE;
104 extern "C" int __munmap(void *, size_t) SANITIZER_WEAK_ATTRIBUTE;
105 
106 // ---------------------- sanitizer_libc.h
107 
108 // From <mach/vm_statistics.h>, but not on older OSs.
109 #ifndef VM_MEMORY_SANITIZER
110 #define VM_MEMORY_SANITIZER 99
111 #endif
112 
113 // XNU on Darwin provides a mmap flag that optimizes allocation/deallocation of
114 // giant memory regions (i.e. shadow memory regions).
115 #define kXnuFastMmapFd 0x4
116 static size_t kXnuFastMmapThreshold = 2 << 30; // 2 GB
117 static bool use_xnu_fast_mmap = false;
118 
internal_mmap(void * addr,size_t length,int prot,int flags,int fd,u64 offset)119 uptr internal_mmap(void *addr, size_t length, int prot, int flags,
120                    int fd, u64 offset) {
121   if (fd == -1) {
122     fd = VM_MAKE_TAG(VM_MEMORY_SANITIZER);
123     if (length >= kXnuFastMmapThreshold) {
124       if (use_xnu_fast_mmap) fd |= kXnuFastMmapFd;
125     }
126   }
127   if (&__mmap) return (uptr)__mmap(addr, length, prot, flags, fd, offset);
128   return (uptr)mmap(addr, length, prot, flags, fd, offset);
129 }
130 
internal_munmap(void * addr,uptr length)131 uptr internal_munmap(void *addr, uptr length) {
132   if (&__munmap) return __munmap(addr, length);
133   return munmap(addr, length);
134 }
135 
internal_mprotect(void * addr,uptr length,int prot)136 int internal_mprotect(void *addr, uptr length, int prot) {
137   return mprotect(addr, length, prot);
138 }
139 
internal_close(fd_t fd)140 uptr internal_close(fd_t fd) {
141   return close(fd);
142 }
143 
internal_open(const char * filename,int flags)144 uptr internal_open(const char *filename, int flags) {
145   return open(filename, flags);
146 }
147 
internal_open(const char * filename,int flags,u32 mode)148 uptr internal_open(const char *filename, int flags, u32 mode) {
149   return open(filename, flags, mode);
150 }
151 
internal_read(fd_t fd,void * buf,uptr count)152 uptr internal_read(fd_t fd, void *buf, uptr count) {
153   return read(fd, buf, count);
154 }
155 
internal_write(fd_t fd,const void * buf,uptr count)156 uptr internal_write(fd_t fd, const void *buf, uptr count) {
157   return write(fd, buf, count);
158 }
159 
internal_stat(const char * path,void * buf)160 uptr internal_stat(const char *path, void *buf) {
161   return stat(path, (struct stat *)buf);
162 }
163 
internal_lstat(const char * path,void * buf)164 uptr internal_lstat(const char *path, void *buf) {
165   return lstat(path, (struct stat *)buf);
166 }
167 
internal_fstat(fd_t fd,void * buf)168 uptr internal_fstat(fd_t fd, void *buf) {
169   return fstat(fd, (struct stat *)buf);
170 }
171 
internal_filesize(fd_t fd)172 uptr internal_filesize(fd_t fd) {
173   struct stat st;
174   if (internal_fstat(fd, &st))
175     return -1;
176   return (uptr)st.st_size;
177 }
178 
internal_dup(int oldfd)179 uptr internal_dup(int oldfd) {
180   return dup(oldfd);
181 }
182 
internal_dup2(int oldfd,int newfd)183 uptr internal_dup2(int oldfd, int newfd) {
184   return dup2(oldfd, newfd);
185 }
186 
internal_readlink(const char * path,char * buf,uptr bufsize)187 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
188   return readlink(path, buf, bufsize);
189 }
190 
internal_unlink(const char * path)191 uptr internal_unlink(const char *path) {
192   return unlink(path);
193 }
194 
internal_sched_yield()195 uptr internal_sched_yield() {
196   return sched_yield();
197 }
198 
internal__exit(int exitcode)199 void internal__exit(int exitcode) {
200   _exit(exitcode);
201 }
202 
internal_sleep(unsigned int seconds)203 unsigned int internal_sleep(unsigned int seconds) {
204   return sleep(seconds);
205 }
206 
internal_getpid()207 uptr internal_getpid() {
208   return getpid();
209 }
210 
internal_sigaction(int signum,const void * act,void * oldact)211 int internal_sigaction(int signum, const void *act, void *oldact) {
212   return sigaction(signum,
213                    (const struct sigaction *)act, (struct sigaction *)oldact);
214 }
215 
internal_sigfillset(__sanitizer_sigset_t * set)216 void internal_sigfillset(__sanitizer_sigset_t *set) { sigfillset(set); }
217 
internal_sigprocmask(int how,__sanitizer_sigset_t * set,__sanitizer_sigset_t * oldset)218 uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
219                           __sanitizer_sigset_t *oldset) {
220   // Don't use sigprocmask here, because it affects all threads.
221   return pthread_sigmask(how, set, oldset);
222 }
223 
224 // Doesn't call pthread_atfork() handlers (but not available on 10.6).
225 extern "C" pid_t __fork(void) SANITIZER_WEAK_ATTRIBUTE;
226 
internal_fork()227 int internal_fork() {
228   if (&__fork)
229     return __fork();
230   return fork();
231 }
232 
internal_sysctl(const int * name,unsigned int namelen,void * oldp,uptr * oldlenp,const void * newp,uptr newlen)233 int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
234                     uptr *oldlenp, const void *newp, uptr newlen) {
235   return sysctl(const_cast<int *>(name), namelen, oldp, (size_t *)oldlenp,
236                 const_cast<void *>(newp), (size_t)newlen);
237 }
238 
internal_sysctlbyname(const char * sname,void * oldp,uptr * oldlenp,const void * newp,uptr newlen)239 int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
240                           const void *newp, uptr newlen) {
241   return sysctlbyname(sname, oldp, (size_t *)oldlenp, const_cast<void *>(newp),
242                       (size_t)newlen);
243 }
244 
internal_spawn_impl(const char * argv[],pid_t * pid)245 static fd_t internal_spawn_impl(const char *argv[], pid_t *pid) {
246   fd_t master_fd = kInvalidFd;
247   fd_t slave_fd = kInvalidFd;
248 
249   auto fd_closer = at_scope_exit([&] {
250     internal_close(master_fd);
251     internal_close(slave_fd);
252   });
253 
254   // We need a new pseudoterminal to avoid buffering problems. The 'atos' tool
255   // in particular detects when it's talking to a pipe and forgets to flush the
256   // output stream after sending a response.
257   master_fd = posix_openpt(O_RDWR);
258   if (master_fd == kInvalidFd) return kInvalidFd;
259 
260   int res = grantpt(master_fd) || unlockpt(master_fd);
261   if (res != 0) return kInvalidFd;
262 
263   // Use TIOCPTYGNAME instead of ptsname() to avoid threading problems.
264   char slave_pty_name[128];
265   res = ioctl(master_fd, TIOCPTYGNAME, slave_pty_name);
266   if (res == -1) return kInvalidFd;
267 
268   slave_fd = internal_open(slave_pty_name, O_RDWR);
269   if (slave_fd == kInvalidFd) return kInvalidFd;
270 
271   // File descriptor actions
272   posix_spawn_file_actions_t acts;
273   res = posix_spawn_file_actions_init(&acts);
274   if (res != 0) return kInvalidFd;
275 
276   auto acts_cleanup = at_scope_exit([&] {
277     posix_spawn_file_actions_destroy(&acts);
278   });
279 
280   res = posix_spawn_file_actions_adddup2(&acts, slave_fd, STDIN_FILENO) ||
281         posix_spawn_file_actions_adddup2(&acts, slave_fd, STDOUT_FILENO) ||
282         posix_spawn_file_actions_addclose(&acts, slave_fd);
283   if (res != 0) return kInvalidFd;
284 
285   // Spawn attributes
286   posix_spawnattr_t attrs;
287   res = posix_spawnattr_init(&attrs);
288   if (res != 0) return kInvalidFd;
289 
290   auto attrs_cleanup  = at_scope_exit([&] {
291     posix_spawnattr_destroy(&attrs);
292   });
293 
294   // In the spawned process, close all file descriptors that are not explicitly
295   // described by the file actions object. This is Darwin-specific extension.
296   res = posix_spawnattr_setflags(&attrs, POSIX_SPAWN_CLOEXEC_DEFAULT);
297   if (res != 0) return kInvalidFd;
298 
299   // posix_spawn
300   char **argv_casted = const_cast<char **>(argv);
301   char **env = GetEnviron();
302   res = posix_spawn(pid, argv[0], &acts, &attrs, argv_casted, env);
303   if (res != 0) return kInvalidFd;
304 
305   // Disable echo in the new terminal, disable CR.
306   struct termios termflags;
307   tcgetattr(master_fd, &termflags);
308   termflags.c_oflag &= ~ONLCR;
309   termflags.c_lflag &= ~ECHO;
310   tcsetattr(master_fd, TCSANOW, &termflags);
311 
312   // On success, do not close master_fd on scope exit.
313   fd_t fd = master_fd;
314   master_fd = kInvalidFd;
315 
316   return fd;
317 }
318 
internal_spawn(const char * argv[],pid_t * pid)319 fd_t internal_spawn(const char *argv[], pid_t *pid) {
320   // The client program may close its stdin and/or stdout and/or stderr thus
321   // allowing open/posix_openpt to reuse file descriptors 0, 1 or 2. In this
322   // case the communication is broken if either the parent or the child tries to
323   // close or duplicate these descriptors. We temporarily reserve these
324   // descriptors here to prevent this.
325   fd_t low_fds[3];
326   size_t count = 0;
327 
328   for (; count < 3; count++) {
329     low_fds[count] = posix_openpt(O_RDWR);
330     if (low_fds[count] >= STDERR_FILENO)
331       break;
332   }
333 
334   fd_t fd = internal_spawn_impl(argv, pid);
335 
336   for (; count > 0; count--) {
337     internal_close(low_fds[count]);
338   }
339 
340   return fd;
341 }
342 
internal_rename(const char * oldpath,const char * newpath)343 uptr internal_rename(const char *oldpath, const char *newpath) {
344   return rename(oldpath, newpath);
345 }
346 
internal_ftruncate(fd_t fd,uptr size)347 uptr internal_ftruncate(fd_t fd, uptr size) {
348   return ftruncate(fd, size);
349 }
350 
internal_execve(const char * filename,char * const argv[],char * const envp[])351 uptr internal_execve(const char *filename, char *const argv[],
352                      char *const envp[]) {
353   return execve(filename, argv, envp);
354 }
355 
internal_waitpid(int pid,int * status,int options)356 uptr internal_waitpid(int pid, int *status, int options) {
357   return waitpid(pid, status, options);
358 }
359 
360 // ----------------- sanitizer_common.h
FileExists(const char * filename)361 bool FileExists(const char *filename) {
362   if (ShouldMockFailureToOpen(filename))
363     return false;
364   struct stat st;
365   if (stat(filename, &st))
366     return false;
367   // Sanity check: filename is a regular file.
368   return S_ISREG(st.st_mode);
369 }
370 
GetTid()371 tid_t GetTid() {
372   tid_t tid;
373   pthread_threadid_np(nullptr, &tid);
374   return tid;
375 }
376 
GetThreadStackTopAndBottom(bool at_initialization,uptr * stack_top,uptr * stack_bottom)377 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
378                                 uptr *stack_bottom) {
379   CHECK(stack_top);
380   CHECK(stack_bottom);
381   uptr stacksize = pthread_get_stacksize_np(pthread_self());
382   // pthread_get_stacksize_np() returns an incorrect stack size for the main
383   // thread on Mavericks. See
384   // https://github.com/google/sanitizers/issues/261
385   if ((GetMacosVersion() >= MACOS_VERSION_MAVERICKS) && at_initialization &&
386       stacksize == (1 << 19))  {
387     struct rlimit rl;
388     CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
389     // Most often rl.rlim_cur will be the desired 8M.
390     if (rl.rlim_cur < kMaxThreadStackSize) {
391       stacksize = rl.rlim_cur;
392     } else {
393       stacksize = kMaxThreadStackSize;
394     }
395   }
396   void *stackaddr = pthread_get_stackaddr_np(pthread_self());
397   *stack_top = (uptr)stackaddr;
398   *stack_bottom = *stack_top - stacksize;
399 }
400 
GetEnviron()401 char **GetEnviron() {
402 #if !SANITIZER_IOS
403   char ***env_ptr = _NSGetEnviron();
404   if (!env_ptr) {
405     Report("_NSGetEnviron() returned NULL. Please make sure __asan_init() is "
406            "called after libSystem_initializer().\n");
407     CHECK(env_ptr);
408   }
409   char **environ = *env_ptr;
410 #endif
411   CHECK(environ);
412   return environ;
413 }
414 
GetEnv(const char * name)415 const char *GetEnv(const char *name) {
416   char **env = GetEnviron();
417   uptr name_len = internal_strlen(name);
418   while (*env != 0) {
419     uptr len = internal_strlen(*env);
420     if (len > name_len) {
421       const char *p = *env;
422       if (!internal_memcmp(p, name, name_len) &&
423           p[name_len] == '=') {  // Match.
424         return *env + name_len + 1;  // String starting after =.
425       }
426     }
427     env++;
428   }
429   return 0;
430 }
431 
ReadBinaryName(char * buf,uptr buf_len)432 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
433   CHECK_LE(kMaxPathLength, buf_len);
434 
435   // On OS X the executable path is saved to the stack by dyld. Reading it
436   // from there is much faster than calling dladdr, especially for large
437   // binaries with symbols.
438   InternalScopedString exe_path(kMaxPathLength);
439   uint32_t size = exe_path.size();
440   if (_NSGetExecutablePath(exe_path.data(), &size) == 0 &&
441       realpath(exe_path.data(), buf) != 0) {
442     return internal_strlen(buf);
443   }
444   return 0;
445 }
446 
ReadLongProcessName(char * buf,uptr buf_len)447 uptr ReadLongProcessName(/*out*/char *buf, uptr buf_len) {
448   return ReadBinaryName(buf, buf_len);
449 }
450 
ReExec()451 void ReExec() {
452   UNIMPLEMENTED();
453 }
454 
CheckASLR()455 void CheckASLR() {
456   // Do nothing
457 }
458 
CheckMPROTECT()459 void CheckMPROTECT() {
460   // Do nothing
461 }
462 
GetPageSize()463 uptr GetPageSize() {
464   return sysconf(_SC_PAGESIZE);
465 }
466 
467 extern "C" unsigned malloc_num_zones;
468 extern "C" malloc_zone_t **malloc_zones;
469 malloc_zone_t sanitizer_zone;
470 
471 // We need to make sure that sanitizer_zone is registered as malloc_zones[0]. If
472 // libmalloc tries to set up a different zone as malloc_zones[0], it will call
473 // mprotect(malloc_zones, ..., PROT_READ).  This interceptor will catch that and
474 // make sure we are still the first (default) zone.
MprotectMallocZones(void * addr,int prot)475 void MprotectMallocZones(void *addr, int prot) {
476   if (addr == malloc_zones && prot == PROT_READ) {
477     if (malloc_num_zones > 1 && malloc_zones[0] != &sanitizer_zone) {
478       for (unsigned i = 1; i < malloc_num_zones; i++) {
479         if (malloc_zones[i] == &sanitizer_zone) {
480           // Swap malloc_zones[0] and malloc_zones[i].
481           malloc_zones[i] = malloc_zones[0];
482           malloc_zones[0] = &sanitizer_zone;
483           break;
484         }
485       }
486     }
487   }
488 }
489 
BlockingMutex()490 BlockingMutex::BlockingMutex() {
491   internal_memset(this, 0, sizeof(*this));
492 }
493 
Lock()494 void BlockingMutex::Lock() {
495   CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
496   CHECK_EQ(OS_SPINLOCK_INIT, 0);
497   CHECK_EQ(owner_, 0);
498   OSSpinLockLock((OSSpinLock*)&opaque_storage_);
499 }
500 
Unlock()501 void BlockingMutex::Unlock() {
502   OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
503 }
504 
CheckLocked()505 void BlockingMutex::CheckLocked() {
506   CHECK_NE(*(OSSpinLock*)&opaque_storage_, 0);
507 }
508 
NanoTime()509 u64 NanoTime() {
510   timeval tv;
511   internal_memset(&tv, 0, sizeof(tv));
512   gettimeofday(&tv, 0);
513   return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
514 }
515 
516 // This needs to be called during initialization to avoid being racy.
MonotonicNanoTime()517 u64 MonotonicNanoTime() {
518   static mach_timebase_info_data_t timebase_info;
519   if (timebase_info.denom == 0) mach_timebase_info(&timebase_info);
520   return (mach_absolute_time() * timebase_info.numer) / timebase_info.denom;
521 }
522 
GetTlsSize()523 uptr GetTlsSize() {
524   return 0;
525 }
526 
InitTlsSize()527 void InitTlsSize() {
528 }
529 
TlsBaseAddr()530 uptr TlsBaseAddr() {
531   uptr segbase = 0;
532 #if defined(__x86_64__)
533   asm("movq %%gs:0,%0" : "=r"(segbase));
534 #elif defined(__i386__)
535   asm("movl %%gs:0,%0" : "=r"(segbase));
536 #endif
537   return segbase;
538 }
539 
540 // The size of the tls on darwin does not appear to be well documented,
541 // however the vm memory map suggests that it is 1024 uptrs in size,
542 // with a size of 0x2000 bytes on x86_64 and 0x1000 bytes on i386.
TlsSize()543 uptr TlsSize() {
544 #if defined(__x86_64__) || defined(__i386__)
545   return 1024 * sizeof(uptr);
546 #else
547   return 0;
548 #endif
549 }
550 
GetThreadStackAndTls(bool main,uptr * stk_addr,uptr * stk_size,uptr * tls_addr,uptr * tls_size)551 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
552                           uptr *tls_addr, uptr *tls_size) {
553 #if !SANITIZER_GO
554   uptr stack_top, stack_bottom;
555   GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
556   *stk_addr = stack_bottom;
557   *stk_size = stack_top - stack_bottom;
558   *tls_addr = TlsBaseAddr();
559   *tls_size = TlsSize();
560 #else
561   *stk_addr = 0;
562   *stk_size = 0;
563   *tls_addr = 0;
564   *tls_size = 0;
565 #endif
566 }
567 
init()568 void ListOfModules::init() {
569   clearOrInit();
570   MemoryMappingLayout memory_mapping(false);
571   memory_mapping.DumpListOfModules(&modules_);
572 }
573 
fallbackInit()574 void ListOfModules::fallbackInit() { clear(); }
575 
GetHandleSignalModeImpl(int signum)576 static HandleSignalMode GetHandleSignalModeImpl(int signum) {
577   switch (signum) {
578     case SIGABRT:
579       return common_flags()->handle_abort;
580     case SIGILL:
581       return common_flags()->handle_sigill;
582     case SIGTRAP:
583       return common_flags()->handle_sigtrap;
584     case SIGFPE:
585       return common_flags()->handle_sigfpe;
586     case SIGSEGV:
587       return common_flags()->handle_segv;
588     case SIGBUS:
589       return common_flags()->handle_sigbus;
590   }
591   return kHandleSignalNo;
592 }
593 
GetHandleSignalMode(int signum)594 HandleSignalMode GetHandleSignalMode(int signum) {
595   // Handling fatal signals on watchOS and tvOS devices is disallowed.
596   if ((SANITIZER_WATCHOS || SANITIZER_TVOS) && !(SANITIZER_IOSSIM))
597     return kHandleSignalNo;
598   HandleSignalMode result = GetHandleSignalModeImpl(signum);
599   if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler)
600     return kHandleSignalExclusive;
601   return result;
602 }
603 
604 MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED;
605 
GetMacosVersionInternal()606 MacosVersion GetMacosVersionInternal() {
607   int mib[2] = { CTL_KERN, KERN_OSRELEASE };
608   char version[100];
609   uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]);
610   for (uptr i = 0; i < maxlen; i++) version[i] = '\0';
611   // Get the version length.
612   CHECK_NE(internal_sysctl(mib, 2, 0, &len, 0, 0), -1);
613   CHECK_LT(len, maxlen);
614   CHECK_NE(internal_sysctl(mib, 2, version, &len, 0, 0), -1);
615 
616   // Expect <major>.<minor>(.<patch>)
617   CHECK_GE(len, 3);
618   const char *p = version;
619   int major = internal_simple_strtoll(p, &p, /*base=*/10);
620   if (*p != '.') return MACOS_VERSION_UNKNOWN;
621   p += 1;
622   int minor = internal_simple_strtoll(p, &p, /*base=*/10);
623   if (*p != '.') return MACOS_VERSION_UNKNOWN;
624 
625   switch (major) {
626     case 9: return MACOS_VERSION_LEOPARD;
627     case 10: return MACOS_VERSION_SNOW_LEOPARD;
628     case 11: return MACOS_VERSION_LION;
629     case 12: return MACOS_VERSION_MOUNTAIN_LION;
630     case 13: return MACOS_VERSION_MAVERICKS;
631     case 14: return MACOS_VERSION_YOSEMITE;
632     case 15: return MACOS_VERSION_EL_CAPITAN;
633     case 16: return MACOS_VERSION_SIERRA;
634     case 17:
635       // Not a typo, 17.5 Darwin Kernel Version maps to High Sierra 10.13.4.
636       if (minor >= 5)
637         return MACOS_VERSION_HIGH_SIERRA_DOT_RELEASE_4;
638       return MACOS_VERSION_HIGH_SIERRA;
639     case 18: return MACOS_VERSION_MOJAVE;
640     case 19: return MACOS_VERSION_CATALINA;
641     default:
642       if (major < 9) return MACOS_VERSION_UNKNOWN;
643       return MACOS_VERSION_UNKNOWN_NEWER;
644   }
645 }
646 
GetMacosVersion()647 MacosVersion GetMacosVersion() {
648   atomic_uint32_t *cache =
649       reinterpret_cast<atomic_uint32_t*>(&cached_macos_version);
650   MacosVersion result =
651       static_cast<MacosVersion>(atomic_load(cache, memory_order_acquire));
652   if (result == MACOS_VERSION_UNINITIALIZED) {
653     result = GetMacosVersionInternal();
654     atomic_store(cache, result, memory_order_release);
655   }
656   return result;
657 }
658 
PlatformHasDifferentMemcpyAndMemmove()659 bool PlatformHasDifferentMemcpyAndMemmove() {
660   // On OS X 10.7 memcpy() and memmove() are both resolved
661   // into memmove$VARIANT$sse42.
662   // See also https://github.com/google/sanitizers/issues/34.
663   // TODO(glider): need to check dynamically that memcpy() and memmove() are
664   // actually the same function.
665   return GetMacosVersion() == MACOS_VERSION_SNOW_LEOPARD;
666 }
667 
GetRSS()668 uptr GetRSS() {
669   struct task_basic_info info;
670   unsigned count = TASK_BASIC_INFO_COUNT;
671   kern_return_t result =
672       task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count);
673   if (UNLIKELY(result != KERN_SUCCESS)) {
674     Report("Cannot get task info. Error: %d\n", result);
675     Die();
676   }
677   return info.resident_size;
678 }
679 
internal_start_thread(void (* func)(void * arg),void * arg)680 void *internal_start_thread(void(*func)(void *arg), void *arg) {
681   // Start the thread with signals blocked, otherwise it can steal user signals.
682   __sanitizer_sigset_t set, old;
683   internal_sigfillset(&set);
684   internal_sigprocmask(SIG_SETMASK, &set, &old);
685   pthread_t th;
686   pthread_create(&th, 0, (void*(*)(void *arg))func, arg);
687   internal_sigprocmask(SIG_SETMASK, &old, 0);
688   return th;
689 }
690 
internal_join_thread(void * th)691 void internal_join_thread(void *th) { pthread_join((pthread_t)th, 0); }
692 
693 #if !SANITIZER_GO
694 static BlockingMutex syslog_lock(LINKER_INITIALIZED);
695 #endif
696 
WriteOneLineToSyslog(const char * s)697 void WriteOneLineToSyslog(const char *s) {
698 #if !SANITIZER_GO
699   syslog_lock.CheckLocked();
700   asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", s);
701 #endif
702 }
703 
LogMessageOnPrintf(const char * str)704 void LogMessageOnPrintf(const char *str) {
705   // Log all printf output to CrashLog.
706   if (common_flags()->abort_on_error)
707     CRAppendCrashLogMessage(str);
708 }
709 
LogFullErrorReport(const char * buffer)710 void LogFullErrorReport(const char *buffer) {
711 #if !SANITIZER_GO
712   // Log with os_trace. This will make it into the crash log.
713 #if SANITIZER_OS_TRACE
714   if (GetMacosVersion() >= MACOS_VERSION_YOSEMITE) {
715     // os_trace requires the message (format parameter) to be a string literal.
716     if (internal_strncmp(SanitizerToolName, "AddressSanitizer",
717                          sizeof("AddressSanitizer") - 1) == 0)
718       os_trace("Address Sanitizer reported a failure.");
719     else if (internal_strncmp(SanitizerToolName, "UndefinedBehaviorSanitizer",
720                               sizeof("UndefinedBehaviorSanitizer") - 1) == 0)
721       os_trace("Undefined Behavior Sanitizer reported a failure.");
722     else if (internal_strncmp(SanitizerToolName, "ThreadSanitizer",
723                               sizeof("ThreadSanitizer") - 1) == 0)
724       os_trace("Thread Sanitizer reported a failure.");
725     else
726       os_trace("Sanitizer tool reported a failure.");
727 
728     if (common_flags()->log_to_syslog)
729       os_trace("Consult syslog for more information.");
730   }
731 #endif
732 
733   // Log to syslog.
734   // The logging on OS X may call pthread_create so we need the threading
735   // environment to be fully initialized. Also, this should never be called when
736   // holding the thread registry lock since that may result in a deadlock. If
737   // the reporting thread holds the thread registry mutex, and asl_log waits
738   // for GCD to dispatch a new thread, the process will deadlock, because the
739   // pthread_create wrapper needs to acquire the lock as well.
740   BlockingMutexLock l(&syslog_lock);
741   if (common_flags()->log_to_syslog)
742     WriteToSyslog(buffer);
743 
744   // The report is added to CrashLog as part of logging all of Printf output.
745 #endif
746 }
747 
GetWriteFlag() const748 SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
749 #if defined(__x86_64__) || defined(__i386__)
750   ucontext_t *ucontext = static_cast<ucontext_t*>(context);
751   return ucontext->uc_mcontext->__es.__err & 2 /*T_PF_WRITE*/ ? WRITE : READ;
752 #else
753   return UNKNOWN;
754 #endif
755 }
756 
IsTrueFaultingAddress() const757 bool SignalContext::IsTrueFaultingAddress() const {
758   auto si = static_cast<const siginfo_t *>(siginfo);
759   // "Real" SIGSEGV codes (e.g., SEGV_MAPERR, SEGV_MAPERR) are non-zero.
760   return si->si_signo == SIGSEGV && si->si_code != 0;
761 }
762 
GetPcSpBp(void * context,uptr * pc,uptr * sp,uptr * bp)763 static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
764   ucontext_t *ucontext = (ucontext_t*)context;
765 # if defined(__aarch64__)
766   *pc = ucontext->uc_mcontext->__ss.__pc;
767 #   if defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
768   *bp = ucontext->uc_mcontext->__ss.__fp;
769 #   else
770   *bp = ucontext->uc_mcontext->__ss.__lr;
771 #   endif
772   *sp = ucontext->uc_mcontext->__ss.__sp;
773 # elif defined(__x86_64__)
774   *pc = ucontext->uc_mcontext->__ss.__rip;
775   *bp = ucontext->uc_mcontext->__ss.__rbp;
776   *sp = ucontext->uc_mcontext->__ss.__rsp;
777 # elif defined(__arm__)
778   *pc = ucontext->uc_mcontext->__ss.__pc;
779   *bp = ucontext->uc_mcontext->__ss.__r[7];
780   *sp = ucontext->uc_mcontext->__ss.__sp;
781 # elif defined(__i386__)
782   *pc = ucontext->uc_mcontext->__ss.__eip;
783   *bp = ucontext->uc_mcontext->__ss.__ebp;
784   *sp = ucontext->uc_mcontext->__ss.__esp;
785 # else
786 # error "Unknown architecture"
787 # endif
788 }
789 
InitPcSpBp()790 void SignalContext::InitPcSpBp() { GetPcSpBp(context, &pc, &sp, &bp); }
791 
InitializePlatformEarly()792 void InitializePlatformEarly() {
793   // Only use xnu_fast_mmap when on x86_64 and the OS supports it.
794   use_xnu_fast_mmap =
795 #if defined(__x86_64__)
796       GetMacosVersion() >= MACOS_VERSION_HIGH_SIERRA_DOT_RELEASE_4;
797 #else
798       false;
799 #endif
800 }
801 
802 #if !SANITIZER_GO
803 static const char kDyldInsertLibraries[] = "DYLD_INSERT_LIBRARIES";
804 LowLevelAllocator allocator_for_env;
805 
806 // Change the value of the env var |name|, leaking the original value.
807 // If |name_value| is NULL, the variable is deleted from the environment,
808 // otherwise the corresponding "NAME=value" string is replaced with
809 // |name_value|.
LeakyResetEnv(const char * name,const char * name_value)810 void LeakyResetEnv(const char *name, const char *name_value) {
811   char **env = GetEnviron();
812   uptr name_len = internal_strlen(name);
813   while (*env != 0) {
814     uptr len = internal_strlen(*env);
815     if (len > name_len) {
816       const char *p = *env;
817       if (!internal_memcmp(p, name, name_len) && p[name_len] == '=') {
818         // Match.
819         if (name_value) {
820           // Replace the old value with the new one.
821           *env = const_cast<char*>(name_value);
822         } else {
823           // Shift the subsequent pointers back.
824           char **del = env;
825           do {
826             del[0] = del[1];
827           } while (*del++);
828         }
829       }
830     }
831     env++;
832   }
833 }
834 
835 SANITIZER_WEAK_CXX_DEFAULT_IMPL
ReexecDisabled()836 bool ReexecDisabled() {
837   return false;
838 }
839 
840 extern "C" SANITIZER_WEAK_ATTRIBUTE double dyldVersionNumber;
841 static const double kMinDyldVersionWithAutoInterposition = 360.0;
842 
DyldNeedsEnvVariable()843 bool DyldNeedsEnvVariable() {
844   // Although sanitizer support was added to LLVM on OS X 10.7+, GCC users
845   // still may want use them on older systems. On older Darwin platforms, dyld
846   // doesn't export dyldVersionNumber symbol and we simply return true.
847   if (!&dyldVersionNumber) return true;
848   // If running on OS X 10.11+ or iOS 9.0+, dyld will interpose even if
849   // DYLD_INSERT_LIBRARIES is not set. However, checking OS version via
850   // GetMacosVersion() doesn't work for the simulator. Let's instead check
851   // `dyldVersionNumber`, which is exported by dyld, against a known version
852   // number from the first OS release where this appeared.
853   return dyldVersionNumber < kMinDyldVersionWithAutoInterposition;
854 }
855 
MaybeReexec()856 void MaybeReexec() {
857   // FIXME: This should really live in some "InitializePlatform" method.
858   MonotonicNanoTime();
859 
860   if (ReexecDisabled()) return;
861 
862   // Make sure the dynamic runtime library is preloaded so that the
863   // wrappers work. If it is not, set DYLD_INSERT_LIBRARIES and re-exec
864   // ourselves.
865   Dl_info info;
866   RAW_CHECK(dladdr((void*)((uptr)&__sanitizer_report_error_summary), &info));
867   char *dyld_insert_libraries =
868       const_cast<char*>(GetEnv(kDyldInsertLibraries));
869   uptr old_env_len = dyld_insert_libraries ?
870       internal_strlen(dyld_insert_libraries) : 0;
871   uptr fname_len = internal_strlen(info.dli_fname);
872   const char *dylib_name = StripModuleName(info.dli_fname);
873   uptr dylib_name_len = internal_strlen(dylib_name);
874 
875   bool lib_is_in_env = dyld_insert_libraries &&
876                        internal_strstr(dyld_insert_libraries, dylib_name);
877   if (DyldNeedsEnvVariable() && !lib_is_in_env) {
878     // DYLD_INSERT_LIBRARIES is not set or does not contain the runtime
879     // library.
880     InternalScopedString program_name(1024);
881     uint32_t buf_size = program_name.size();
882     _NSGetExecutablePath(program_name.data(), &buf_size);
883     char *new_env = const_cast<char*>(info.dli_fname);
884     if (dyld_insert_libraries) {
885       // Append the runtime dylib name to the existing value of
886       // DYLD_INSERT_LIBRARIES.
887       new_env = (char*)allocator_for_env.Allocate(old_env_len + fname_len + 2);
888       internal_strncpy(new_env, dyld_insert_libraries, old_env_len);
889       new_env[old_env_len] = ':';
890       // Copy fname_len and add a trailing zero.
891       internal_strncpy(new_env + old_env_len + 1, info.dli_fname,
892                        fname_len + 1);
893       // Ok to use setenv() since the wrappers don't depend on the value of
894       // asan_inited.
895       setenv(kDyldInsertLibraries, new_env, /*overwrite*/1);
896     } else {
897       // Set DYLD_INSERT_LIBRARIES equal to the runtime dylib name.
898       setenv(kDyldInsertLibraries, info.dli_fname, /*overwrite*/0);
899     }
900     VReport(1, "exec()-ing the program with\n");
901     VReport(1, "%s=%s\n", kDyldInsertLibraries, new_env);
902     VReport(1, "to enable wrappers.\n");
903     execv(program_name.data(), *_NSGetArgv());
904 
905     // We get here only if execv() failed.
906     Report("ERROR: The process is launched without DYLD_INSERT_LIBRARIES, "
907            "which is required for the sanitizer to work. We tried to set the "
908            "environment variable and re-execute itself, but execv() failed, "
909            "possibly because of sandbox restrictions. Make sure to launch the "
910            "executable with:\n%s=%s\n", kDyldInsertLibraries, new_env);
911     RAW_CHECK("execv failed" && 0);
912   }
913 
914   // Verify that interceptors really work.  We'll use dlsym to locate
915   // "pthread_create", if interceptors are working, it should really point to
916   // "wrap_pthread_create" within our own dylib.
917   Dl_info info_pthread_create;
918   void *dlopen_addr = dlsym(RTLD_DEFAULT, "pthread_create");
919   RAW_CHECK(dladdr(dlopen_addr, &info_pthread_create));
920   if (internal_strcmp(info.dli_fname, info_pthread_create.dli_fname) != 0) {
921     Report(
922         "ERROR: Interceptors are not working. This may be because %s is "
923         "loaded too late (e.g. via dlopen). Please launch the executable "
924         "with:\n%s=%s\n",
925         SanitizerToolName, kDyldInsertLibraries, info.dli_fname);
926     RAW_CHECK("interceptors not installed" && 0);
927   }
928 
929   if (!lib_is_in_env)
930     return;
931 
932   if (!common_flags()->strip_env)
933     return;
934 
935   // DYLD_INSERT_LIBRARIES is set and contains the runtime library. Let's remove
936   // the dylib from the environment variable, because interceptors are installed
937   // and we don't want our children to inherit the variable.
938 
939   uptr env_name_len = internal_strlen(kDyldInsertLibraries);
940   // Allocate memory to hold the previous env var name, its value, the '='
941   // sign and the '\0' char.
942   char *new_env = (char*)allocator_for_env.Allocate(
943       old_env_len + 2 + env_name_len);
944   RAW_CHECK(new_env);
945   internal_memset(new_env, '\0', old_env_len + 2 + env_name_len);
946   internal_strncpy(new_env, kDyldInsertLibraries, env_name_len);
947   new_env[env_name_len] = '=';
948   char *new_env_pos = new_env + env_name_len + 1;
949 
950   // Iterate over colon-separated pieces of |dyld_insert_libraries|.
951   char *piece_start = dyld_insert_libraries;
952   char *piece_end = NULL;
953   char *old_env_end = dyld_insert_libraries + old_env_len;
954   do {
955     if (piece_start[0] == ':') piece_start++;
956     piece_end = internal_strchr(piece_start, ':');
957     if (!piece_end) piece_end = dyld_insert_libraries + old_env_len;
958     if ((uptr)(piece_start - dyld_insert_libraries) > old_env_len) break;
959     uptr piece_len = piece_end - piece_start;
960 
961     char *filename_start =
962         (char *)internal_memrchr(piece_start, '/', piece_len);
963     uptr filename_len = piece_len;
964     if (filename_start) {
965       filename_start += 1;
966       filename_len = piece_len - (filename_start - piece_start);
967     } else {
968       filename_start = piece_start;
969     }
970 
971     // If the current piece isn't the runtime library name,
972     // append it to new_env.
973     if ((dylib_name_len != filename_len) ||
974         (internal_memcmp(filename_start, dylib_name, dylib_name_len) != 0)) {
975       if (new_env_pos != new_env + env_name_len + 1) {
976         new_env_pos[0] = ':';
977         new_env_pos++;
978       }
979       internal_strncpy(new_env_pos, piece_start, piece_len);
980       new_env_pos += piece_len;
981     }
982     // Move on to the next piece.
983     piece_start = piece_end;
984   } while (piece_start < old_env_end);
985 
986   // Can't use setenv() here, because it requires the allocator to be
987   // initialized.
988   // FIXME: instead of filtering DYLD_INSERT_LIBRARIES here, do it in
989   // a separate function called after InitializeAllocator().
990   if (new_env_pos == new_env + env_name_len + 1) new_env = NULL;
991   LeakyResetEnv(kDyldInsertLibraries, new_env);
992 }
993 #endif  // SANITIZER_GO
994 
GetArgv()995 char **GetArgv() {
996   return *_NSGetArgv();
997 }
998 
999 #if SANITIZER_IOS
1000 // The task_vm_info struct is normally provided by the macOS SDK, but we need
1001 // fields only available in 10.12+. Declare the struct manually to be able to
1002 // build against older SDKs.
1003 struct __sanitizer_task_vm_info {
1004   mach_vm_size_t virtual_size;
1005   integer_t region_count;
1006   integer_t page_size;
1007   mach_vm_size_t resident_size;
1008   mach_vm_size_t resident_size_peak;
1009   mach_vm_size_t device;
1010   mach_vm_size_t device_peak;
1011   mach_vm_size_t internal;
1012   mach_vm_size_t internal_peak;
1013   mach_vm_size_t external;
1014   mach_vm_size_t external_peak;
1015   mach_vm_size_t reusable;
1016   mach_vm_size_t reusable_peak;
1017   mach_vm_size_t purgeable_volatile_pmap;
1018   mach_vm_size_t purgeable_volatile_resident;
1019   mach_vm_size_t purgeable_volatile_virtual;
1020   mach_vm_size_t compressed;
1021   mach_vm_size_t compressed_peak;
1022   mach_vm_size_t compressed_lifetime;
1023   mach_vm_size_t phys_footprint;
1024   mach_vm_address_t min_address;
1025   mach_vm_address_t max_address;
1026 };
1027 #define __SANITIZER_TASK_VM_INFO_COUNT ((mach_msg_type_number_t) \
1028     (sizeof(__sanitizer_task_vm_info) / sizeof(natural_t)))
1029 
GetTaskInfoMaxAddress()1030 static uptr GetTaskInfoMaxAddress() {
1031   __sanitizer_task_vm_info vm_info = {} /* zero initialize */;
1032   mach_msg_type_number_t count = __SANITIZER_TASK_VM_INFO_COUNT;
1033   int err = task_info(mach_task_self(), TASK_VM_INFO, (int *)&vm_info, &count);
1034   return err ? 0 : vm_info.max_address;
1035 }
1036 
GetMaxUserVirtualAddress()1037 uptr GetMaxUserVirtualAddress() {
1038   static uptr max_vm = GetTaskInfoMaxAddress();
1039   if (max_vm != 0)
1040     return max_vm - 1;
1041 
1042   // xnu cannot provide vm address limit
1043 # if SANITIZER_WORDSIZE == 32
1044   return 0xffe00000 - 1;
1045 # else
1046   return 0x200000000 - 1;
1047 # endif
1048 }
1049 
1050 #else // !SANITIZER_IOS
1051 
GetMaxUserVirtualAddress()1052 uptr GetMaxUserVirtualAddress() {
1053 # if SANITIZER_WORDSIZE == 64
1054   return (1ULL << 47) - 1;  // 0x00007fffffffffffUL;
1055 # else // SANITIZER_WORDSIZE == 32
1056   static_assert(SANITIZER_WORDSIZE == 32, "Wrong wordsize");
1057   return (1ULL << 32) - 1;  // 0xffffffff;
1058 # endif
1059 }
1060 #endif
1061 
GetMaxVirtualAddress()1062 uptr GetMaxVirtualAddress() {
1063   return GetMaxUserVirtualAddress();
1064 }
1065 
FindAvailableMemoryRange(uptr size,uptr alignment,uptr left_padding,uptr * largest_gap_found,uptr * max_occupied_addr)1066 uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
1067                               uptr *largest_gap_found,
1068                               uptr *max_occupied_addr) {
1069   typedef vm_region_submap_short_info_data_64_t RegionInfo;
1070   enum { kRegionInfoSize = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64 };
1071   // Start searching for available memory region past PAGEZERO, which is
1072   // 4KB on 32-bit and 4GB on 64-bit.
1073   mach_vm_address_t start_address =
1074     (SANITIZER_WORDSIZE == 32) ? 0x000000001000 : 0x000100000000;
1075 
1076   mach_vm_address_t address = start_address;
1077   mach_vm_address_t free_begin = start_address;
1078   kern_return_t kr = KERN_SUCCESS;
1079   if (largest_gap_found) *largest_gap_found = 0;
1080   if (max_occupied_addr) *max_occupied_addr = 0;
1081   while (kr == KERN_SUCCESS) {
1082     mach_vm_size_t vmsize = 0;
1083     natural_t depth = 0;
1084     RegionInfo vminfo;
1085     mach_msg_type_number_t count = kRegionInfoSize;
1086     kr = mach_vm_region_recurse(mach_task_self(), &address, &vmsize, &depth,
1087                                 (vm_region_info_t)&vminfo, &count);
1088     if (kr == KERN_INVALID_ADDRESS) {
1089       // No more regions beyond "address", consider the gap at the end of VM.
1090       address = GetMaxVirtualAddress() + 1;
1091       vmsize = 0;
1092     } else {
1093       if (max_occupied_addr) *max_occupied_addr = address + vmsize;
1094     }
1095     if (free_begin != address) {
1096       // We found a free region [free_begin..address-1].
1097       uptr gap_start = RoundUpTo((uptr)free_begin + left_padding, alignment);
1098       uptr gap_end = RoundDownTo((uptr)address, alignment);
1099       uptr gap_size = gap_end > gap_start ? gap_end - gap_start : 0;
1100       if (size < gap_size) {
1101         return gap_start;
1102       }
1103 
1104       if (largest_gap_found && *largest_gap_found < gap_size) {
1105         *largest_gap_found = gap_size;
1106       }
1107     }
1108     // Move to the next region.
1109     address += vmsize;
1110     free_begin = address;
1111   }
1112 
1113   // We looked at all free regions and could not find one large enough.
1114   return 0;
1115 }
1116 
1117 // FIXME implement on this platform.
GetMemoryProfile(fill_profile_f cb,uptr * stats,uptr stats_size)1118 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size) { }
1119 
DumpAllRegisters(void * context)1120 void SignalContext::DumpAllRegisters(void *context) {
1121   Report("Register values:\n");
1122 
1123   ucontext_t *ucontext = (ucontext_t*)context;
1124 # define DUMPREG64(r) \
1125     Printf("%s = 0x%016llx  ", #r, ucontext->uc_mcontext->__ss.__ ## r);
1126 # define DUMPREG32(r) \
1127     Printf("%s = 0x%08x  ", #r, ucontext->uc_mcontext->__ss.__ ## r);
1128 # define DUMPREG_(r)   Printf(" "); DUMPREG(r);
1129 # define DUMPREG__(r)  Printf("  "); DUMPREG(r);
1130 # define DUMPREG___(r) Printf("   "); DUMPREG(r);
1131 
1132 # if defined(__x86_64__)
1133 #  define DUMPREG(r) DUMPREG64(r)
1134   DUMPREG(rax); DUMPREG(rbx); DUMPREG(rcx); DUMPREG(rdx); Printf("\n");
1135   DUMPREG(rdi); DUMPREG(rsi); DUMPREG(rbp); DUMPREG(rsp); Printf("\n");
1136   DUMPREG_(r8); DUMPREG_(r9); DUMPREG(r10); DUMPREG(r11); Printf("\n");
1137   DUMPREG(r12); DUMPREG(r13); DUMPREG(r14); DUMPREG(r15); Printf("\n");
1138 # elif defined(__i386__)
1139 #  define DUMPREG(r) DUMPREG32(r)
1140   DUMPREG(eax); DUMPREG(ebx); DUMPREG(ecx); DUMPREG(edx); Printf("\n");
1141   DUMPREG(edi); DUMPREG(esi); DUMPREG(ebp); DUMPREG(esp); Printf("\n");
1142 # elif defined(__aarch64__)
1143 #  define DUMPREG(r) DUMPREG64(r)
1144   DUMPREG_(x[0]); DUMPREG_(x[1]); DUMPREG_(x[2]); DUMPREG_(x[3]); Printf("\n");
1145   DUMPREG_(x[4]); DUMPREG_(x[5]); DUMPREG_(x[6]); DUMPREG_(x[7]); Printf("\n");
1146   DUMPREG_(x[8]); DUMPREG_(x[9]); DUMPREG(x[10]); DUMPREG(x[11]); Printf("\n");
1147   DUMPREG(x[12]); DUMPREG(x[13]); DUMPREG(x[14]); DUMPREG(x[15]); Printf("\n");
1148   DUMPREG(x[16]); DUMPREG(x[17]); DUMPREG(x[18]); DUMPREG(x[19]); Printf("\n");
1149   DUMPREG(x[20]); DUMPREG(x[21]); DUMPREG(x[22]); DUMPREG(x[23]); Printf("\n");
1150   DUMPREG(x[24]); DUMPREG(x[25]); DUMPREG(x[26]); DUMPREG(x[27]); Printf("\n");
1151   DUMPREG(x[28]); DUMPREG___(fp); DUMPREG___(lr); DUMPREG___(sp); Printf("\n");
1152 # elif defined(__arm__)
1153 #  define DUMPREG(r) DUMPREG32(r)
1154   DUMPREG_(r[0]); DUMPREG_(r[1]); DUMPREG_(r[2]); DUMPREG_(r[3]); Printf("\n");
1155   DUMPREG_(r[4]); DUMPREG_(r[5]); DUMPREG_(r[6]); DUMPREG_(r[7]); Printf("\n");
1156   DUMPREG_(r[8]); DUMPREG_(r[9]); DUMPREG(r[10]); DUMPREG(r[11]); Printf("\n");
1157   DUMPREG(r[12]); DUMPREG___(sp); DUMPREG___(lr); DUMPREG___(pc); Printf("\n");
1158 # else
1159 # error "Unknown architecture"
1160 # endif
1161 
1162 # undef DUMPREG64
1163 # undef DUMPREG32
1164 # undef DUMPREG_
1165 # undef DUMPREG__
1166 # undef DUMPREG___
1167 # undef DUMPREG
1168 }
1169 
CompareBaseAddress(const LoadedModule & a,const LoadedModule & b)1170 static inline bool CompareBaseAddress(const LoadedModule &a,
1171                                       const LoadedModule &b) {
1172   return a.base_address() < b.base_address();
1173 }
1174 
FormatUUID(char * out,uptr size,const u8 * uuid)1175 void FormatUUID(char *out, uptr size, const u8 *uuid) {
1176   internal_snprintf(out, size,
1177                     "<%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-"
1178                     "%02X%02X%02X%02X%02X%02X>",
1179                     uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5],
1180                     uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11],
1181                     uuid[12], uuid[13], uuid[14], uuid[15]);
1182 }
1183 
PrintModuleMap()1184 void PrintModuleMap() {
1185   Printf("Process module map:\n");
1186   MemoryMappingLayout memory_mapping(false);
1187   InternalMmapVector<LoadedModule> modules;
1188   modules.reserve(128);
1189   memory_mapping.DumpListOfModules(&modules);
1190   Sort(modules.data(), modules.size(), CompareBaseAddress);
1191   for (uptr i = 0; i < modules.size(); ++i) {
1192     char uuid_str[128];
1193     FormatUUID(uuid_str, sizeof(uuid_str), modules[i].uuid());
1194     Printf("0x%zx-0x%zx %s (%s) %s\n", modules[i].base_address(),
1195            modules[i].max_executable_address(), modules[i].full_name(),
1196            ModuleArchToString(modules[i].arch()), uuid_str);
1197   }
1198   Printf("End of module map.\n");
1199 }
1200 
CheckNoDeepBind(const char * filename,int flag)1201 void CheckNoDeepBind(const char *filename, int flag) {
1202   // Do nothing.
1203 }
1204 
GetRandom(void * buffer,uptr length,bool blocking)1205 bool GetRandom(void *buffer, uptr length, bool blocking) {
1206   if (!buffer || !length || length > 256)
1207     return false;
1208   // arc4random never fails.
1209   REAL(arc4random_buf)(buffer, length);
1210   return true;
1211 }
1212 
GetNumberOfCPUs()1213 u32 GetNumberOfCPUs() {
1214   return (u32)sysconf(_SC_NPROCESSORS_ONLN);
1215 }
1216 
1217 }  // namespace __sanitizer
1218 
1219 #endif  // SANITIZER_MAC
1220