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