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