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_platform_limits_posix.h"
31 #include "sanitizer_procmaps.h"
32 #include "sanitizer_ptrauth.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 ((GetMacosAlignedVersion() >= MacosVersion(10, 9)) && 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 static MacosVersion GetMacosAlignedVersionInternal() {
610   u16 kernel_major = GetDarwinKernelVersion().major;
611   const u16 version_offset = 4;
612   CHECK_GE(kernel_major, version_offset);
613   u16 macos_major = kernel_major - version_offset;
614   return MacosVersion(10, macos_major);
615 }
616 
617 static_assert(sizeof(MacosVersion) == sizeof(atomic_uint32_t::Type),
618               "MacosVersion cache size");
619 static atomic_uint32_t cached_macos_version;
620 
621 MacosVersion GetMacosAlignedVersion() {
622   atomic_uint32_t::Type result =
623       atomic_load(&cached_macos_version, memory_order_acquire);
624   if (!result) {
625     MacosVersion version = GetMacosAlignedVersionInternal();
626     result = *reinterpret_cast<atomic_uint32_t::Type *>(&version);
627     atomic_store(&cached_macos_version, result, memory_order_release);
628   }
629   return *reinterpret_cast<MacosVersion *>(&result);
630 }
631 
632 void ParseVersion(const char *vers, u16 *major, u16 *minor) {
633   // Format: <major>.<minor>.<patch>\0
634   CHECK_GE(internal_strlen(vers), 5);
635   const char *p = vers;
636   *major = internal_simple_strtoll(p, &p, /*base=*/10);
637   CHECK_EQ(*p, '.');
638   p += 1;
639   *minor = internal_simple_strtoll(p, &p, /*base=*/10);
640 }
641 
642 DarwinKernelVersion GetDarwinKernelVersion() {
643   char buf[100];
644   size_t len = sizeof(buf);
645   int res = internal_sysctlbyname("kern.osrelease", buf, &len, nullptr, 0);
646   CHECK_EQ(res, 0);
647 
648   u16 major, minor;
649   ParseVersion(buf, &major, &minor);
650 
651   return DarwinKernelVersion(major, minor);
652 }
653 
654 uptr GetRSS() {
655   struct task_basic_info info;
656   unsigned count = TASK_BASIC_INFO_COUNT;
657   kern_return_t result =
658       task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count);
659   if (UNLIKELY(result != KERN_SUCCESS)) {
660     Report("Cannot get task info. Error: %d\n", result);
661     Die();
662   }
663   return info.resident_size;
664 }
665 
666 void *internal_start_thread(void *(*func)(void *arg), void *arg) {
667   // Start the thread with signals blocked, otherwise it can steal user signals.
668   __sanitizer_sigset_t set, old;
669   internal_sigfillset(&set);
670   internal_sigprocmask(SIG_SETMASK, &set, &old);
671   pthread_t th;
672   pthread_create(&th, 0, func, arg);
673   internal_sigprocmask(SIG_SETMASK, &old, 0);
674   return th;
675 }
676 
677 void internal_join_thread(void *th) { pthread_join((pthread_t)th, 0); }
678 
679 #if !SANITIZER_GO
680 static BlockingMutex syslog_lock(LINKER_INITIALIZED);
681 #endif
682 
683 void WriteOneLineToSyslog(const char *s) {
684 #if !SANITIZER_GO
685   syslog_lock.CheckLocked();
686   asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", s);
687 #endif
688 }
689 
690 void LogMessageOnPrintf(const char *str) {
691   // Log all printf output to CrashLog.
692   if (common_flags()->abort_on_error)
693     CRAppendCrashLogMessage(str);
694 }
695 
696 void LogFullErrorReport(const char *buffer) {
697 #if !SANITIZER_GO
698   // Log with os_trace. This will make it into the crash log.
699 #if SANITIZER_OS_TRACE
700   if (GetMacosAlignedVersion() >= MacosVersion(10, 10)) {
701     // os_trace requires the message (format parameter) to be a string literal.
702     if (internal_strncmp(SanitizerToolName, "AddressSanitizer",
703                          sizeof("AddressSanitizer") - 1) == 0)
704       os_trace("Address Sanitizer reported a failure.");
705     else if (internal_strncmp(SanitizerToolName, "UndefinedBehaviorSanitizer",
706                               sizeof("UndefinedBehaviorSanitizer") - 1) == 0)
707       os_trace("Undefined Behavior Sanitizer reported a failure.");
708     else if (internal_strncmp(SanitizerToolName, "ThreadSanitizer",
709                               sizeof("ThreadSanitizer") - 1) == 0)
710       os_trace("Thread Sanitizer reported a failure.");
711     else
712       os_trace("Sanitizer tool reported a failure.");
713 
714     if (common_flags()->log_to_syslog)
715       os_trace("Consult syslog for more information.");
716   }
717 #endif
718 
719   // Log to syslog.
720   // The logging on OS X may call pthread_create so we need the threading
721   // environment to be fully initialized. Also, this should never be called when
722   // holding the thread registry lock since that may result in a deadlock. If
723   // the reporting thread holds the thread registry mutex, and asl_log waits
724   // for GCD to dispatch a new thread, the process will deadlock, because the
725   // pthread_create wrapper needs to acquire the lock as well.
726   BlockingMutexLock l(&syslog_lock);
727   if (common_flags()->log_to_syslog)
728     WriteToSyslog(buffer);
729 
730   // The report is added to CrashLog as part of logging all of Printf output.
731 #endif
732 }
733 
734 SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
735 #if defined(__x86_64__) || defined(__i386__)
736   ucontext_t *ucontext = static_cast<ucontext_t*>(context);
737   return ucontext->uc_mcontext->__es.__err & 2 /*T_PF_WRITE*/ ? WRITE : READ;
738 #else
739   return UNKNOWN;
740 #endif
741 }
742 
743 bool SignalContext::IsTrueFaultingAddress() const {
744   auto si = static_cast<const siginfo_t *>(siginfo);
745   // "Real" SIGSEGV codes (e.g., SEGV_MAPERR, SEGV_MAPERR) are non-zero.
746   return si->si_signo == SIGSEGV && si->si_code != 0;
747 }
748 
749 #if defined(__aarch64__) && defined(arm_thread_state64_get_sp)
750   #define AARCH64_GET_REG(r) \
751     (uptr)ptrauth_strip(     \
752         (void *)arm_thread_state64_get_##r(ucontext->uc_mcontext->__ss), 0)
753 #else
754   #define AARCH64_GET_REG(r) ucontext->uc_mcontext->__ss.__##r
755 #endif
756 
757 static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
758   ucontext_t *ucontext = (ucontext_t*)context;
759 # if defined(__aarch64__)
760   *pc = AARCH64_GET_REG(pc);
761 #   if defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
762   *bp = AARCH64_GET_REG(fp);
763 #   else
764   *bp = AARCH64_GET_REG(lr);
765 #   endif
766   *sp = AARCH64_GET_REG(sp);
767 # elif defined(__x86_64__)
768   *pc = ucontext->uc_mcontext->__ss.__rip;
769   *bp = ucontext->uc_mcontext->__ss.__rbp;
770   *sp = ucontext->uc_mcontext->__ss.__rsp;
771 # elif defined(__arm__)
772   *pc = ucontext->uc_mcontext->__ss.__pc;
773   *bp = ucontext->uc_mcontext->__ss.__r[7];
774   *sp = ucontext->uc_mcontext->__ss.__sp;
775 # elif defined(__i386__)
776   *pc = ucontext->uc_mcontext->__ss.__eip;
777   *bp = ucontext->uc_mcontext->__ss.__ebp;
778   *sp = ucontext->uc_mcontext->__ss.__esp;
779 # else
780 # error "Unknown architecture"
781 # endif
782 }
783 
784 void SignalContext::InitPcSpBp() {
785   addr = (uptr)ptrauth_strip((void *)addr, 0);
786   GetPcSpBp(context, &pc, &sp, &bp);
787 }
788 
789 void InitializePlatformEarly() {
790   // Only use xnu_fast_mmap when on x86_64 and the kernel supports it.
791   use_xnu_fast_mmap =
792 #if defined(__x86_64__)
793       GetDarwinKernelVersion() >= DarwinKernelVersion(17, 5);
794 #else
795       false;
796 #endif
797 }
798 
799 #if !SANITIZER_GO
800 static const char kDyldInsertLibraries[] = "DYLD_INSERT_LIBRARIES";
801 LowLevelAllocator allocator_for_env;
802 
803 // Change the value of the env var |name|, leaking the original value.
804 // If |name_value| is NULL, the variable is deleted from the environment,
805 // otherwise the corresponding "NAME=value" string is replaced with
806 // |name_value|.
807 void LeakyResetEnv(const char *name, const char *name_value) {
808   char **env = GetEnviron();
809   uptr name_len = internal_strlen(name);
810   while (*env != 0) {
811     uptr len = internal_strlen(*env);
812     if (len > name_len) {
813       const char *p = *env;
814       if (!internal_memcmp(p, name, name_len) && p[name_len] == '=') {
815         // Match.
816         if (name_value) {
817           // Replace the old value with the new one.
818           *env = const_cast<char*>(name_value);
819         } else {
820           // Shift the subsequent pointers back.
821           char **del = env;
822           do {
823             del[0] = del[1];
824           } while (*del++);
825         }
826       }
827     }
828     env++;
829   }
830 }
831 
832 SANITIZER_WEAK_CXX_DEFAULT_IMPL
833 bool ReexecDisabled() {
834   return false;
835 }
836 
837 extern "C" SANITIZER_WEAK_ATTRIBUTE double dyldVersionNumber;
838 static const double kMinDyldVersionWithAutoInterposition = 360.0;
839 
840 bool DyldNeedsEnvVariable() {
841   // Although sanitizer support was added to LLVM on OS X 10.7+, GCC users
842   // still may want use them on older systems. On older Darwin platforms, dyld
843   // doesn't export dyldVersionNumber symbol and we simply return true.
844   if (!&dyldVersionNumber) return true;
845   // If running on OS X 10.11+ or iOS 9.0+, dyld will interpose even if
846   // DYLD_INSERT_LIBRARIES is not set. However, checking OS version via
847   // GetMacosAlignedVersion() doesn't work for the simulator. Let's instead
848   // check `dyldVersionNumber`, which is exported by dyld, against a known
849   // version number from the first OS release where this appeared.
850   return dyldVersionNumber < kMinDyldVersionWithAutoInterposition;
851 }
852 
853 void MaybeReexec() {
854   // FIXME: This should really live in some "InitializePlatform" method.
855   MonotonicNanoTime();
856 
857   if (ReexecDisabled()) return;
858 
859   // Make sure the dynamic runtime library is preloaded so that the
860   // wrappers work. If it is not, set DYLD_INSERT_LIBRARIES and re-exec
861   // ourselves.
862   Dl_info info;
863   RAW_CHECK(dladdr((void*)((uptr)&__sanitizer_report_error_summary), &info));
864   char *dyld_insert_libraries =
865       const_cast<char*>(GetEnv(kDyldInsertLibraries));
866   uptr old_env_len = dyld_insert_libraries ?
867       internal_strlen(dyld_insert_libraries) : 0;
868   uptr fname_len = internal_strlen(info.dli_fname);
869   const char *dylib_name = StripModuleName(info.dli_fname);
870   uptr dylib_name_len = internal_strlen(dylib_name);
871 
872   bool lib_is_in_env = dyld_insert_libraries &&
873                        internal_strstr(dyld_insert_libraries, dylib_name);
874   if (DyldNeedsEnvVariable() && !lib_is_in_env) {
875     // DYLD_INSERT_LIBRARIES is not set or does not contain the runtime
876     // library.
877     InternalScopedString program_name(1024);
878     uint32_t buf_size = program_name.size();
879     _NSGetExecutablePath(program_name.data(), &buf_size);
880     char *new_env = const_cast<char*>(info.dli_fname);
881     if (dyld_insert_libraries) {
882       // Append the runtime dylib name to the existing value of
883       // DYLD_INSERT_LIBRARIES.
884       new_env = (char*)allocator_for_env.Allocate(old_env_len + fname_len + 2);
885       internal_strncpy(new_env, dyld_insert_libraries, old_env_len);
886       new_env[old_env_len] = ':';
887       // Copy fname_len and add a trailing zero.
888       internal_strncpy(new_env + old_env_len + 1, info.dli_fname,
889                        fname_len + 1);
890       // Ok to use setenv() since the wrappers don't depend on the value of
891       // asan_inited.
892       setenv(kDyldInsertLibraries, new_env, /*overwrite*/1);
893     } else {
894       // Set DYLD_INSERT_LIBRARIES equal to the runtime dylib name.
895       setenv(kDyldInsertLibraries, info.dli_fname, /*overwrite*/0);
896     }
897     VReport(1, "exec()-ing the program with\n");
898     VReport(1, "%s=%s\n", kDyldInsertLibraries, new_env);
899     VReport(1, "to enable wrappers.\n");
900     execv(program_name.data(), *_NSGetArgv());
901 
902     // We get here only if execv() failed.
903     Report("ERROR: The process is launched without DYLD_INSERT_LIBRARIES, "
904            "which is required for the sanitizer to work. We tried to set the "
905            "environment variable and re-execute itself, but execv() failed, "
906            "possibly because of sandbox restrictions. Make sure to launch the "
907            "executable with:\n%s=%s\n", kDyldInsertLibraries, new_env);
908     RAW_CHECK("execv failed" && 0);
909   }
910 
911   // Verify that interceptors really work.  We'll use dlsym to locate
912   // "pthread_create", if interceptors are working, it should really point to
913   // "wrap_pthread_create" within our own dylib.
914   Dl_info info_pthread_create;
915   void *dlopen_addr = dlsym(RTLD_DEFAULT, "pthread_create");
916   RAW_CHECK(dladdr(dlopen_addr, &info_pthread_create));
917   if (internal_strcmp(info.dli_fname, info_pthread_create.dli_fname) != 0) {
918     Report(
919         "ERROR: Interceptors are not working. This may be because %s is "
920         "loaded too late (e.g. via dlopen). Please launch the executable "
921         "with:\n%s=%s\n",
922         SanitizerToolName, kDyldInsertLibraries, info.dli_fname);
923     RAW_CHECK("interceptors not installed" && 0);
924   }
925 
926   if (!lib_is_in_env)
927     return;
928 
929   if (!common_flags()->strip_env)
930     return;
931 
932   // DYLD_INSERT_LIBRARIES is set and contains the runtime library. Let's remove
933   // the dylib from the environment variable, because interceptors are installed
934   // and we don't want our children to inherit the variable.
935 
936   uptr env_name_len = internal_strlen(kDyldInsertLibraries);
937   // Allocate memory to hold the previous env var name, its value, the '='
938   // sign and the '\0' char.
939   char *new_env = (char*)allocator_for_env.Allocate(
940       old_env_len + 2 + env_name_len);
941   RAW_CHECK(new_env);
942   internal_memset(new_env, '\0', old_env_len + 2 + env_name_len);
943   internal_strncpy(new_env, kDyldInsertLibraries, env_name_len);
944   new_env[env_name_len] = '=';
945   char *new_env_pos = new_env + env_name_len + 1;
946 
947   // Iterate over colon-separated pieces of |dyld_insert_libraries|.
948   char *piece_start = dyld_insert_libraries;
949   char *piece_end = NULL;
950   char *old_env_end = dyld_insert_libraries + old_env_len;
951   do {
952     if (piece_start[0] == ':') piece_start++;
953     piece_end = internal_strchr(piece_start, ':');
954     if (!piece_end) piece_end = dyld_insert_libraries + old_env_len;
955     if ((uptr)(piece_start - dyld_insert_libraries) > old_env_len) break;
956     uptr piece_len = piece_end - piece_start;
957 
958     char *filename_start =
959         (char *)internal_memrchr(piece_start, '/', piece_len);
960     uptr filename_len = piece_len;
961     if (filename_start) {
962       filename_start += 1;
963       filename_len = piece_len - (filename_start - piece_start);
964     } else {
965       filename_start = piece_start;
966     }
967 
968     // If the current piece isn't the runtime library name,
969     // append it to new_env.
970     if ((dylib_name_len != filename_len) ||
971         (internal_memcmp(filename_start, dylib_name, dylib_name_len) != 0)) {
972       if (new_env_pos != new_env + env_name_len + 1) {
973         new_env_pos[0] = ':';
974         new_env_pos++;
975       }
976       internal_strncpy(new_env_pos, piece_start, piece_len);
977       new_env_pos += piece_len;
978     }
979     // Move on to the next piece.
980     piece_start = piece_end;
981   } while (piece_start < old_env_end);
982 
983   // Can't use setenv() here, because it requires the allocator to be
984   // initialized.
985   // FIXME: instead of filtering DYLD_INSERT_LIBRARIES here, do it in
986   // a separate function called after InitializeAllocator().
987   if (new_env_pos == new_env + env_name_len + 1) new_env = NULL;
988   LeakyResetEnv(kDyldInsertLibraries, new_env);
989 }
990 #endif  // SANITIZER_GO
991 
992 char **GetArgv() {
993   return *_NSGetArgv();
994 }
995 
996 #if SANITIZER_IOS
997 // The task_vm_info struct is normally provided by the macOS SDK, but we need
998 // fields only available in 10.12+. Declare the struct manually to be able to
999 // build against older SDKs.
1000 struct __sanitizer_task_vm_info {
1001   mach_vm_size_t virtual_size;
1002   integer_t region_count;
1003   integer_t page_size;
1004   mach_vm_size_t resident_size;
1005   mach_vm_size_t resident_size_peak;
1006   mach_vm_size_t device;
1007   mach_vm_size_t device_peak;
1008   mach_vm_size_t internal;
1009   mach_vm_size_t internal_peak;
1010   mach_vm_size_t external;
1011   mach_vm_size_t external_peak;
1012   mach_vm_size_t reusable;
1013   mach_vm_size_t reusable_peak;
1014   mach_vm_size_t purgeable_volatile_pmap;
1015   mach_vm_size_t purgeable_volatile_resident;
1016   mach_vm_size_t purgeable_volatile_virtual;
1017   mach_vm_size_t compressed;
1018   mach_vm_size_t compressed_peak;
1019   mach_vm_size_t compressed_lifetime;
1020   mach_vm_size_t phys_footprint;
1021   mach_vm_address_t min_address;
1022   mach_vm_address_t max_address;
1023 };
1024 #define __SANITIZER_TASK_VM_INFO_COUNT ((mach_msg_type_number_t) \
1025     (sizeof(__sanitizer_task_vm_info) / sizeof(natural_t)))
1026 
1027 static uptr GetTaskInfoMaxAddress() {
1028   __sanitizer_task_vm_info vm_info = {} /* zero initialize */;
1029   mach_msg_type_number_t count = __SANITIZER_TASK_VM_INFO_COUNT;
1030   int err = task_info(mach_task_self(), TASK_VM_INFO, (int *)&vm_info, &count);
1031   return err ? 0 : vm_info.max_address;
1032 }
1033 
1034 uptr GetMaxUserVirtualAddress() {
1035   static uptr max_vm = GetTaskInfoMaxAddress();
1036   if (max_vm != 0)
1037     return max_vm - 1;
1038 
1039   // xnu cannot provide vm address limit
1040 # if SANITIZER_WORDSIZE == 32
1041   return 0xffe00000 - 1;
1042 # else
1043   return 0x200000000 - 1;
1044 # endif
1045 }
1046 
1047 #else // !SANITIZER_IOS
1048 
1049 uptr GetMaxUserVirtualAddress() {
1050 # if SANITIZER_WORDSIZE == 64
1051   return (1ULL << 47) - 1;  // 0x00007fffffffffffUL;
1052 # else // SANITIZER_WORDSIZE == 32
1053   static_assert(SANITIZER_WORDSIZE == 32, "Wrong wordsize");
1054   return (1ULL << 32) - 1;  // 0xffffffff;
1055 # endif
1056 }
1057 #endif
1058 
1059 uptr GetMaxVirtualAddress() {
1060   return GetMaxUserVirtualAddress();
1061 }
1062 
1063 uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
1064                               uptr *largest_gap_found,
1065                               uptr *max_occupied_addr) {
1066   typedef vm_region_submap_short_info_data_64_t RegionInfo;
1067   enum { kRegionInfoSize = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64 };
1068   // Start searching for available memory region past PAGEZERO, which is
1069   // 4KB on 32-bit and 4GB on 64-bit.
1070   mach_vm_address_t start_address =
1071     (SANITIZER_WORDSIZE == 32) ? 0x000000001000 : 0x000100000000;
1072 
1073   mach_vm_address_t address = start_address;
1074   mach_vm_address_t free_begin = start_address;
1075   kern_return_t kr = KERN_SUCCESS;
1076   if (largest_gap_found) *largest_gap_found = 0;
1077   if (max_occupied_addr) *max_occupied_addr = 0;
1078   while (kr == KERN_SUCCESS) {
1079     mach_vm_size_t vmsize = 0;
1080     natural_t depth = 0;
1081     RegionInfo vminfo;
1082     mach_msg_type_number_t count = kRegionInfoSize;
1083     kr = mach_vm_region_recurse(mach_task_self(), &address, &vmsize, &depth,
1084                                 (vm_region_info_t)&vminfo, &count);
1085     if (kr == KERN_INVALID_ADDRESS) {
1086       // No more regions beyond "address", consider the gap at the end of VM.
1087       address = GetMaxVirtualAddress() + 1;
1088       vmsize = 0;
1089     } else {
1090       if (max_occupied_addr) *max_occupied_addr = address + vmsize;
1091     }
1092     if (free_begin != address) {
1093       // We found a free region [free_begin..address-1].
1094       uptr gap_start = RoundUpTo((uptr)free_begin + left_padding, alignment);
1095       uptr gap_end = RoundDownTo((uptr)address, alignment);
1096       uptr gap_size = gap_end > gap_start ? gap_end - gap_start : 0;
1097       if (size < gap_size) {
1098         return gap_start;
1099       }
1100 
1101       if (largest_gap_found && *largest_gap_found < gap_size) {
1102         *largest_gap_found = gap_size;
1103       }
1104     }
1105     // Move to the next region.
1106     address += vmsize;
1107     free_begin = address;
1108   }
1109 
1110   // We looked at all free regions and could not find one large enough.
1111   return 0;
1112 }
1113 
1114 // FIXME implement on this platform.
1115 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size) { }
1116 
1117 void SignalContext::DumpAllRegisters(void *context) {
1118   Report("Register values:\n");
1119 
1120   ucontext_t *ucontext = (ucontext_t*)context;
1121 # define DUMPREG64(r) \
1122     Printf("%s = 0x%016llx  ", #r, ucontext->uc_mcontext->__ss.__ ## r);
1123 # define DUMPREGA64(r) \
1124     Printf("   %s = 0x%016llx  ", #r, AARCH64_GET_REG(r));
1125 # define DUMPREG32(r) \
1126     Printf("%s = 0x%08x  ", #r, ucontext->uc_mcontext->__ss.__ ## r);
1127 # define DUMPREG_(r)   Printf(" "); DUMPREG(r);
1128 # define DUMPREG__(r)  Printf("  "); DUMPREG(r);
1129 # define DUMPREG___(r) Printf("   "); DUMPREG(r);
1130 
1131 # if defined(__x86_64__)
1132 #  define DUMPREG(r) DUMPREG64(r)
1133   DUMPREG(rax); DUMPREG(rbx); DUMPREG(rcx); DUMPREG(rdx); Printf("\n");
1134   DUMPREG(rdi); DUMPREG(rsi); DUMPREG(rbp); DUMPREG(rsp); Printf("\n");
1135   DUMPREG_(r8); DUMPREG_(r9); DUMPREG(r10); DUMPREG(r11); Printf("\n");
1136   DUMPREG(r12); DUMPREG(r13); DUMPREG(r14); DUMPREG(r15); Printf("\n");
1137 # elif defined(__i386__)
1138 #  define DUMPREG(r) DUMPREG32(r)
1139   DUMPREG(eax); DUMPREG(ebx); DUMPREG(ecx); DUMPREG(edx); Printf("\n");
1140   DUMPREG(edi); DUMPREG(esi); DUMPREG(ebp); DUMPREG(esp); Printf("\n");
1141 # elif defined(__aarch64__)
1142 #  define DUMPREG(r) DUMPREG64(r)
1143   DUMPREG_(x[0]); DUMPREG_(x[1]); DUMPREG_(x[2]); DUMPREG_(x[3]); Printf("\n");
1144   DUMPREG_(x[4]); DUMPREG_(x[5]); DUMPREG_(x[6]); DUMPREG_(x[7]); Printf("\n");
1145   DUMPREG_(x[8]); DUMPREG_(x[9]); DUMPREG(x[10]); DUMPREG(x[11]); Printf("\n");
1146   DUMPREG(x[12]); DUMPREG(x[13]); DUMPREG(x[14]); DUMPREG(x[15]); Printf("\n");
1147   DUMPREG(x[16]); DUMPREG(x[17]); DUMPREG(x[18]); DUMPREG(x[19]); Printf("\n");
1148   DUMPREG(x[20]); DUMPREG(x[21]); DUMPREG(x[22]); DUMPREG(x[23]); Printf("\n");
1149   DUMPREG(x[24]); DUMPREG(x[25]); DUMPREG(x[26]); DUMPREG(x[27]); Printf("\n");
1150   DUMPREG(x[28]); DUMPREGA64(fp); DUMPREGA64(lr); DUMPREGA64(sp); Printf("\n");
1151 # elif defined(__arm__)
1152 #  define DUMPREG(r) DUMPREG32(r)
1153   DUMPREG_(r[0]); DUMPREG_(r[1]); DUMPREG_(r[2]); DUMPREG_(r[3]); Printf("\n");
1154   DUMPREG_(r[4]); DUMPREG_(r[5]); DUMPREG_(r[6]); DUMPREG_(r[7]); Printf("\n");
1155   DUMPREG_(r[8]); DUMPREG_(r[9]); DUMPREG(r[10]); DUMPREG(r[11]); Printf("\n");
1156   DUMPREG(r[12]); DUMPREG___(sp); DUMPREG___(lr); DUMPREG___(pc); Printf("\n");
1157 # else
1158 # error "Unknown architecture"
1159 # endif
1160 
1161 # undef DUMPREG64
1162 # undef DUMPREG32
1163 # undef DUMPREG_
1164 # undef DUMPREG__
1165 # undef DUMPREG___
1166 # undef DUMPREG
1167 }
1168 
1169 static inline bool CompareBaseAddress(const LoadedModule &a,
1170                                       const LoadedModule &b) {
1171   return a.base_address() < b.base_address();
1172 }
1173 
1174 void FormatUUID(char *out, uptr size, const u8 *uuid) {
1175   internal_snprintf(out, size,
1176                     "<%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-"
1177                     "%02X%02X%02X%02X%02X%02X>",
1178                     uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5],
1179                     uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11],
1180                     uuid[12], uuid[13], uuid[14], uuid[15]);
1181 }
1182 
1183 void PrintModuleMap() {
1184   Printf("Process module map:\n");
1185   MemoryMappingLayout memory_mapping(false);
1186   InternalMmapVector<LoadedModule> modules;
1187   modules.reserve(128);
1188   memory_mapping.DumpListOfModules(&modules);
1189   Sort(modules.data(), modules.size(), CompareBaseAddress);
1190   for (uptr i = 0; i < modules.size(); ++i) {
1191     char uuid_str[128];
1192     FormatUUID(uuid_str, sizeof(uuid_str), modules[i].uuid());
1193     Printf("0x%zx-0x%zx %s (%s) %s\n", modules[i].base_address(),
1194            modules[i].max_executable_address(), modules[i].full_name(),
1195            ModuleArchToString(modules[i].arch()), uuid_str);
1196   }
1197   Printf("End of module map.\n");
1198 }
1199 
1200 void CheckNoDeepBind(const char *filename, int flag) {
1201   // Do nothing.
1202 }
1203 
1204 bool GetRandom(void *buffer, uptr length, bool blocking) {
1205   if (!buffer || !length || length > 256)
1206     return false;
1207   // arc4random never fails.
1208   REAL(arc4random_buf)(buffer, length);
1209   return true;
1210 }
1211 
1212 u32 GetNumberOfCPUs() {
1213   return (u32)sysconf(_SC_NPROCESSORS_ONLN);
1214 }
1215 
1216 }  // namespace __sanitizer
1217 
1218 #endif  // SANITIZER_MAC
1219