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