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_APPLE
15# include "interception/interception.h"
16# include "sanitizer_mac.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// Start searching for available memory region past PAGEZERO, which is
26// 4KB on 32-bit and 4GB on 64-bit.
27# define GAP_SEARCH_START_ADDRESS \
28 ((SANITIZER_WORDSIZE == 32) ? 0x000000001000 : 0x000100000000)
29
30# include "sanitizer_allocator_internal.h"
31# include "sanitizer_common.h"
32# include "sanitizer_file.h"
33# include "sanitizer_flags.h"
34# include "sanitizer_interface_internal.h"
35# include "sanitizer_internal_defs.h"
36# include "sanitizer_libc.h"
37# include "sanitizer_platform_limits_posix.h"
38# include "sanitizer_procmaps.h"
39# include "sanitizer_ptrauth.h"
40
41# if !SANITIZER_IOS
42# include <crt_externs.h> // for _NSGetEnviron
43# else
44extern char **environ;
45# endif
46
47// Integrate with CrashReporter library if available
48# if defined(__has_include) && __has_include(<CrashReporterClient.h>)
49# define HAVE_CRASHREPORTERCLIENT_H 1
50# include <CrashReporterClient.h>
51# else
52# define HAVE_CRASHREPORTERCLIENT_H 0
53# endif
54
55# if !SANITIZER_IOS
56# include <crt_externs.h> // for _NSGetArgv and _NSGetEnviron
57# else
58extern "C" {
59extern char ***_NSGetArgv(void);
60}
61# endif
62
63# include <asl.h>
64# include <dlfcn.h> // for dladdr()
65# include <errno.h>
66# include <fcntl.h>
67# include <inttypes.h>
68# include <libkern/OSAtomic.h>
69# include <mach-o/dyld.h>
70# include <mach/mach.h>
71# include <mach/mach_error.h>
72# include <mach/mach_time.h>
73# include <mach/vm_statistics.h>
74# include <malloc/malloc.h>
75# include <os/log.h>
76# include <pthread.h>
77# include <pthread/introspection.h>
78# include <sched.h>
79# include <signal.h>
80# include <spawn.h>
81# include <stdlib.h>
82# include <sys/ioctl.h>
83# include <sys/mman.h>
84# include <sys/resource.h>
85# include <sys/stat.h>
86# include <sys/sysctl.h>
87# include <sys/types.h>
88# include <sys/wait.h>
89# include <unistd.h>
90# include <util.h>
91
92// From <crt_externs.h>, but we don't have that file on iOS.
93extern "C" {
94 extern char ***_NSGetArgv(void);
95 extern char ***_NSGetEnviron(void);
96}
97
98// From <mach/mach_vm.h>, but we don't have that file on iOS.
99extern "C" {
100extern kern_return_t mach_vm_region_recurse(vm_map_t target_task,
101 mach_vm_address_t* address,
102 mach_vm_size_t* size,
103 natural_t* nesting_depth,
104 vm_region_recurse_info_t info,
105 mach_msg_type_number_t* infoCnt);
106
107extern const void* _dyld_get_shared_cache_range(size_t* length);
108}
109
110# if !SANITIZER_GO
111// Weak symbol no-op when TSan is not linked
112SANITIZER_WEAK_ATTRIBUTE extern void __tsan_set_in_internal_write_call(
113 bool value) {}
114# endif
115
116namespace __sanitizer {
117
118#include "sanitizer_syscall_generic.inc"
119
120// Direct syscalls, don't call libmalloc hooks (but not available on 10.6).
121extern "C" void *__mmap(void *addr, size_t len, int prot, int flags, int fildes,
122 off_t off) SANITIZER_WEAK_ATTRIBUTE;
123extern "C" int __munmap(void *, size_t) SANITIZER_WEAK_ATTRIBUTE;
124
125// ---------------------- sanitizer_libc.h
126
127// From <mach/vm_statistics.h>, but not on older OSs.
128#ifndef VM_MEMORY_SANITIZER
129#define VM_MEMORY_SANITIZER 99
130#endif
131# ifndef VM_MEMORY_DEBUG
132# define VM_MEMORY_DEBUG 108
133# endif
134
135// XNU on Darwin provides a mmap flag that optimizes allocation/deallocation of
136// giant memory regions (i.e. shadow memory regions).
137#define kXnuFastMmapFd 0x4
138static size_t kXnuFastMmapThreshold = 2 << 30; // 2 GB
139static bool use_xnu_fast_mmap = false;
140bool debug_region_activated = false;
141
142uptr internal_mmap(void* addr_hint, size_t length, int prot, int flags, int fd,
143 u64 offset) {
144 bool use_debug_vm = false;
145 if (fd == -1) {
146 use_debug_vm = (debug_region_activated &&
147 (uptr)addr_hint >= DARWIN_DEBUG_MEMORY_START);
148 fd = VM_MAKE_TAG(use_debug_vm ? VM_MEMORY_DEBUG : VM_MEMORY_SANITIZER);
149 if (length >= kXnuFastMmapThreshold) {
150 if (use_xnu_fast_mmap) fd |= kXnuFastMmapFd;
151 }
152 }
153
154 auto mmap_fn = (&__mmap ? __mmap : mmap);
155 uptr addr = (uptr)mmap_fn(addr_hint, length, prot, flags, fd, offset);
156
157 if (addr == (uptr)MAP_FAILED)
158 return (uptr)MAP_FAILED;
159
160 // If mmap succeeded, check that the return value is within
161 // the expected range.
162 if (use_debug_vm) {
163 // Allocations from the debug region should be >= DARWIN_DEBUG_MEMORY_START
164 if (addr < DARWIN_DEBUG_MEMORY_START) {
165 Report(
166 "FATAL: Unsupported VM layout. mmap returned %p, Expected debug "
167 "memory >= %p\n",
168 addr, DARWIN_DEBUG_MEMORY_START);
169 Die();
170 }
171 } else {
172 // Allocations NOT from the debug region should be inside the range given by
173 // SANITIZER_MMAP_BEGIN and SANITIZER_MMAP_RANGE_SIZE
174 if (addr < SANITIZER_MMAP_BEGIN ||
175 addr + length > SANITIZER_MMAP_BEGIN + SANITIZER_MMAP_RANGE_SIZE) {
176 Report(
177 "FATAL: Unsupported VM layout. mmap returned %p, expected [%p, %p]\n",
178 addr, SANITIZER_MMAP_BEGIN,
179 SANITIZER_MMAP_BEGIN + SANITIZER_MMAP_RANGE_SIZE);
180 Die();
181 }
182 }
183
184 return addr;
185}
186
187uptr internal_munmap(void *addr, uptr length) {
188 if (&__munmap) return __munmap(addr, length);
189 return munmap(addr, length);
190}
191
192uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags,
193 void *new_address) {
194 CHECK(false && "internal_mremap is unimplemented on Mac");
195 return 0;
196}
197
198int internal_mprotect(void *addr, uptr length, int prot) {
199 return mprotect(addr, length, prot);
200}
201
202int internal_madvise(uptr addr, uptr length, int advice) {
203 return madvise((void *)addr, length, advice);
204}
205
206uptr internal_close(fd_t fd) {
207 return close(fd);
208}
209
210uptr internal_close_range(fd_t lowfd, fd_t highfd, int flags) {
211 return -1; // Not supported.
212}
213
214uptr internal_open(const char *filename, int flags) {
215 return open(filename, flags);
216}
217
218uptr internal_open(const char *filename, int flags, u32 mode) {
219 return open(filename, flags, mode);
220}
221
222uptr internal_read(fd_t fd, void *buf, uptr count) {
223 return read(fd, buf, count);
224}
225
226uptr internal_write(fd_t fd, const void *buf, uptr count) {
227# if SANITIZER_GO
228 return write(fd, buf, count);
229# else
230 // We need to disable interceptors when writing in TSan
231 __tsan_set_in_internal_write_call(true);
232 uptr res = write(fd, buf, count);
233 __tsan_set_in_internal_write_call(false);
234 return res;
235# endif
236}
237
238uptr internal_stat(const char *path, void *buf) {
239 return stat(path, (struct stat *)buf);
240}
241
242uptr internal_lstat(const char *path, void *buf) {
243 return lstat(path, (struct stat *)buf);
244}
245
246uptr internal_fstat(fd_t fd, void *buf) {
247 return fstat(fd, (struct stat *)buf);
248}
249
250uptr internal_filesize(fd_t fd) {
251 struct stat st;
252 if (internal_fstat(fd, &st))
253 return -1;
254 return (uptr)st.st_size;
255}
256
257uptr internal_dup(int oldfd) {
258 return dup(oldfd);
259}
260
261uptr internal_dup2(int oldfd, int newfd) {
262 return dup2(oldfd, newfd);
263}
264
265uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
266 return readlink(path, buf, bufsize);
267}
268
269uptr internal_unlink(const char *path) {
270 return unlink(path);
271}
272
273uptr internal_sched_yield() {
274 return sched_yield();
275}
276
277void internal__exit(int exitcode) {
278 _exit(exitcode);
279}
280
281void internal_usleep(u64 useconds) { usleep(useconds); }
282
283uptr internal_getpid() {
284 return getpid();
285}
286
287int internal_dlinfo(void *handle, int request, void *p) {
288 UNIMPLEMENTED();
289}
290
291int internal_sigaction(int signum, const void *act, void *oldact) {
292 return sigaction(signum,
293 (const struct sigaction *)act, (struct sigaction *)oldact);
294}
295
296void internal_sigfillset(__sanitizer_sigset_t *set) { sigfillset(set); }
297
298uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
299 __sanitizer_sigset_t *oldset) {
300 // Don't use sigprocmask here, because it affects all threads.
301 return pthread_sigmask(how, set, oldset);
302}
303
304// Doesn't call pthread_atfork() handlers (but not available on 10.6).
305extern "C" pid_t __fork(void) SANITIZER_WEAK_ATTRIBUTE;
306
307int internal_fork() {
308 if (&__fork)
309 return __fork();
310 return fork();
311}
312
313int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
314 uptr *oldlenp, const void *newp, uptr newlen) {
315 return sysctl(const_cast<int *>(name), namelen, oldp, (size_t *)oldlenp,
316 const_cast<void *>(newp), (size_t)newlen);
317}
318
319int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
320 const void *newp, uptr newlen) {
321 return sysctlbyname(sname, oldp, (size_t *)oldlenp, const_cast<void *>(newp),
322 (size_t)newlen);
323}
324
325bool internal_spawn(const char* argv[], const char* envp[], pid_t* pid,
326 fd_t fd_stdin, fd_t fd_stdout) {
327 // NOTE: Caller ensures that fd_stdin and fd_stdout are not 0, 1, or 2, since
328 // this can break communication.
329 //
330 // NOTE: Caller is responsible for closing fd_stdin after the process has
331 // died.
332
333 int res;
334 auto fd_closer = at_scope_exit([&] {
335 // NOTE: We intentionally do not close fd_stdin since this can
336 // cause us to receive a fatal SIGPIPE if the process dies.
337 internal_close(fd_stdout);
338 });
339
340 // File descriptor actions
341 posix_spawn_file_actions_t acts;
342 res = posix_spawn_file_actions_init(&acts);
343 if (res != 0)
344 return false;
345
346 auto acts_cleanup = at_scope_exit([&] {
347 posix_spawn_file_actions_destroy(&acts);
348 });
349
350 res = posix_spawn_file_actions_adddup2(&acts, fd_stdin, STDIN_FILENO) ||
351 posix_spawn_file_actions_adddup2(&acts, fd_stdout, STDOUT_FILENO) ||
352 posix_spawn_file_actions_addclose(&acts, fd_stdin) ||
353 posix_spawn_file_actions_addclose(&acts, fd_stdout);
354 if (res != 0)
355 return false;
356
357 // Spawn attributes
358 posix_spawnattr_t attrs;
359 res = posix_spawnattr_init(&attrs);
360 if (res != 0)
361 return false;
362
363 auto attrs_cleanup = at_scope_exit([&] {
364 posix_spawnattr_destroy(&attrs);
365 });
366
367 // In the spawned process, close all file descriptors that are not explicitly
368 // described by the file actions object. This is Darwin-specific extension.
369 res = posix_spawnattr_setflags(&attrs, POSIX_SPAWN_CLOEXEC_DEFAULT);
370 if (res != 0)
371 return false;
372
373 // posix_spawn
374 char **argv_casted = const_cast<char **>(argv);
375 char **envp_casted = const_cast<char **>(envp);
376 res = posix_spawn(pid, argv[0], &acts, &attrs, argv_casted, envp_casted);
377 if (res != 0)
378 return false;
379
380 return true;
381}
382
383uptr internal_rename(const char *oldpath, const char *newpath) {
384 return rename(oldpath, newpath);
385}
386
387uptr internal_ftruncate(fd_t fd, uptr size) {
388 return ftruncate(fd, size);
389}
390
391uptr internal_execve(const char *filename, char *const argv[],
392 char *const envp[]) {
393 return execve(filename, argv, envp);
394}
395
396uptr internal_waitpid(int pid, int *status, int options) {
397 return waitpid(pid, status, options);
398}
399
400kern_return_t internal_mach_vm_region_recurse(vm_map_t target_task,
401 mach_vm_address_t* address,
402 mach_vm_size_t* size,
403 natural_t* nesting_depth,
404 vm_region_recurse_info_t info,
405 mach_msg_type_number_t* infoCnt) {
406 kern_return_t kr = mach_vm_region_recurse(target_task, address, size,
407 nesting_depth, info, infoCnt);
408 if (kr == KERN_DENIED) {
409 Report(
410 "FATAL: mach_vm_region_recurse returned KERN_DENIED when checking "
411 "whether an address is mapped.\n");
412 Report("HINT: Is mach_vm_region_recurse allowed by sandbox?\n");
413 Die();
414 }
415 return kr;
416}
417
418// ----------------- sanitizer_common.h
419bool FileExists(const char *filename) {
420 if (ShouldMockFailureToOpen(filename))
421 return false;
422 struct stat st;
423 if (stat(filename, &st))
424 return false;
425 // Sanity check: filename is a regular file.
426 return S_ISREG(st.st_mode);
427}
428
429bool DirExists(const char *path) {
430 struct stat st;
431 if (stat(path, &st))
432 return false;
433 return S_ISDIR(st.st_mode);
434}
435
436ThreadID GetTid() {
437 ThreadID tid;
438 pthread_threadid_np(nullptr, &tid);
439 return tid;
440}
441
442void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
443 uptr *stack_bottom) {
444 CHECK(stack_top);
445 CHECK(stack_bottom);
446 uptr stacksize = pthread_get_stacksize_np(pthread_self());
447 // pthread_get_stacksize_np() returns an incorrect stack size for the main
448 // thread on Mavericks. See
449 // https://github.com/google/sanitizers/issues/261
450 if ((GetMacosAlignedVersion() >= MacosVersion(10, 9)) && at_initialization &&
451 stacksize == (1 << 19)) {
452 struct rlimit rl;
453 CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
454 // Most often rl.rlim_cur will be the desired 8M.
455 if (rl.rlim_cur < kMaxThreadStackSize) {
456 stacksize = rl.rlim_cur;
457 } else {
458 stacksize = kMaxThreadStackSize;
459 }
460 }
461 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
462 *stack_top = (uptr)stackaddr;
463 *stack_bottom = *stack_top - stacksize;
464}
465
466char **GetEnviron() {
467#if !SANITIZER_IOS
468 char ***env_ptr = _NSGetEnviron();
469 if (!env_ptr) {
470 Report("_NSGetEnviron() returned NULL. Please make sure __asan_init() is "
471 "called after libSystem_initializer().\n");
472 CHECK(env_ptr);
473 }
474 char **environ = *env_ptr;
475#endif
476 CHECK(environ);
477 return environ;
478}
479
480const char *GetEnv(const char *name) {
481 char **env = GetEnviron();
482 uptr name_len = internal_strlen(name);
483 while (*env != 0) {
484 uptr len = internal_strlen(*env);
485 if (len > name_len) {
486 const char *p = *env;
487 if (!internal_memcmp(p, name, name_len) &&
488 p[name_len] == '=') { // Match.
489 return *env + name_len + 1; // String starting after =.
490 }
491 }
492 env++;
493 }
494 return 0;
495}
496
497uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
498 CHECK_LE(kMaxPathLength, buf_len);
499
500 // On OS X the executable path is saved to the stack by dyld. Reading it
501 // from there is much faster than calling dladdr, especially for large
502 // binaries with symbols.
503 InternalMmapVector<char> exe_path(kMaxPathLength);
504 uint32_t size = exe_path.size();
505 if (_NSGetExecutablePath(exe_path.data(), &size) == 0 &&
506 realpath(exe_path.data(), buf) != 0) {
507 return internal_strlen(buf);
508 }
509 return 0;
510}
511
512uptr ReadLongProcessName(/*out*/char *buf, uptr buf_len) {
513 return ReadBinaryName(buf, buf_len);
514}
515
516void ReExec() {
517 UNIMPLEMENTED();
518}
519
520void CheckASLR() {
521 // Do nothing
522}
523
524void CheckMPROTECT() {
525 // Do nothing
526}
527
528uptr GetPageSize() {
529 return sysconf(_SC_PAGESIZE);
530}
531
532extern "C" unsigned malloc_num_zones;
533extern "C" malloc_zone_t **malloc_zones;
534malloc_zone_t sanitizer_zone;
535
536// We need to make sure that sanitizer_zone is registered as malloc_zones[0]. If
537// libmalloc tries to set up a different zone as malloc_zones[0], it will call
538// mprotect(malloc_zones, ..., PROT_READ). This interceptor will catch that and
539// make sure we are still the first (default) zone.
540void MprotectMallocZones(void *addr, int prot) {
541 if (addr == malloc_zones && prot == PROT_READ) {
542 if (malloc_num_zones > 1 && malloc_zones[0] != &sanitizer_zone) {
543 for (unsigned i = 1; i < malloc_num_zones; i++) {
544 if (malloc_zones[i] == &sanitizer_zone) {
545 // Swap malloc_zones[0] and malloc_zones[i].
546 malloc_zones[i] = malloc_zones[0];
547 malloc_zones[0] = &sanitizer_zone;
548 break;
549 }
550 }
551 }
552 }
553}
554
555void FutexWait(atomic_uint32_t *p, u32 cmp) {
556 // FIXME: implement actual blocking.
557 sched_yield();
558}
559
560void FutexWake(atomic_uint32_t *p, u32 count) {}
561
562u64 NanoTime() {
563 timeval tv;
564 internal_memset(&tv, 0, sizeof(tv));
565 gettimeofday(&tv, 0);
566 return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
567}
568
569// This needs to be called during initialization to avoid being racy.
570u64 MonotonicNanoTime() {
571 static mach_timebase_info_data_t timebase_info;
572 if (timebase_info.denom == 0) mach_timebase_info(&timebase_info);
573 return (mach_absolute_time() * timebase_info.numer) / timebase_info.denom;
574}
575
576uptr GetTlsSize() {
577 return 0;
578}
579
580uptr TlsBaseAddr() {
581 uptr segbase = 0;
582#if defined(__x86_64__)
583 asm("movq %%gs:0,%0" : "=r"(segbase));
584#elif defined(__i386__)
585 asm("movl %%gs:0,%0" : "=r"(segbase));
586#elif defined(__aarch64__)
587 asm("mrs %x0, tpidrro_el0" : "=r"(segbase));
588 segbase &= ~0x07ul; // macOS < 12 keeps the cpu id in the low 3 bits
589#endif
590 return segbase;
591}
592
593// The size of the tls on darwin does not appear to be well documented,
594// however the vm memory map suggests that it is 1024 uptrs in size,
595// with a size of 0x2000 bytes on x86_64 and 0x1000 bytes on i386.
596// On arm64 it is the size of the TSD array, from libpthread headers.
597uptr TlsSize() {
598# if defined(__x86_64__) || defined(__i386__)
599 return 1024 * sizeof(uptr);
600# elif defined(__aarch64__) && SANITIZER_IOSDEVICE
601 return 512 * sizeof(uptr);
602# elif defined(__aarch64__)
603 return 768 * sizeof(uptr);
604# else
605 return 0;
606# endif
607}
608
609void GetThreadStackAndTls(bool main, uptr *stk_begin, uptr *stk_end,
610 uptr *tls_begin, uptr *tls_end) {
611# if !SANITIZER_GO
612 GetThreadStackTopAndBottom(main, stk_end, stk_begin);
613 *tls_begin = TlsBaseAddr();
614 *tls_end = *tls_begin + TlsSize();
615# else
616 *stk_begin = 0;
617 *stk_end = 0;
618 *tls_begin = 0;
619 *tls_end = 0;
620# endif
621}
622
623void ListOfModules::init() {
624 clearOrInit();
625 MemoryMappingLayout memory_mapping(false);
626 memory_mapping.DumpListOfModules(&modules_);
627}
628
629void ListOfModules::fallbackInit() { clear(); }
630
631static HandleSignalMode GetHandleSignalModeImpl(int signum) {
632 switch (signum) {
633 case SIGABRT:
634 return common_flags()->handle_abort;
635 case SIGILL:
636 return common_flags()->handle_sigill;
637 case SIGTRAP:
638 return common_flags()->handle_sigtrap;
639 case SIGFPE:
640 return common_flags()->handle_sigfpe;
641 case SIGSEGV:
642 return common_flags()->handle_segv;
643 case SIGBUS:
644 return common_flags()->handle_sigbus;
645 }
646 return kHandleSignalNo;
647}
648
649HandleSignalMode GetHandleSignalMode(int signum) {
650 // Handling fatal signals on watchOS and tvOS devices is disallowed.
651 if ((SANITIZER_WATCHOS || SANITIZER_TVOS) && !(SANITIZER_IOSSIM))
652 return kHandleSignalNo;
653 HandleSignalMode result = GetHandleSignalModeImpl(signum);
654 if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler)
655 return kHandleSignalExclusive;
656 return result;
657}
658
659// Offset example:
660// XNU 17 -- macOS 10.13 -- iOS 11 -- tvOS 11 -- watchOS 4
661constexpr u16 GetOSMajorKernelOffset() {
662 if (SANITIZER_OSX)
663 return 4;
664 if (SANITIZER_IPHONEOS || SANITIZER_TVOS)
665 return 6;
666 if (SANITIZER_WATCHOS)
667 return 13;
668 if (SANITIZER_VISIONOS)
669 return 22;
670}
671
672using VersStr = char[64];
673
674static uptr ApproximateOSVersionViaKernelVersion(VersStr vers) {
675 u16 kernel_major = GetDarwinKernelVersion().major;
676 u16 offset = GetOSMajorKernelOffset();
677 CHECK_GE(kernel_major, offset);
678 u16 os_major = kernel_major - offset;
679
680 const char *format = "%d.0";
681 if (kernel_major >= 27) {
682 // with kernel major 27 <=> OSes 27.0, OS versions are aligned with kernel
683 os_major = kernel_major;
684 } else if (kernel_major >= 25) {
685 // with kernel_major 25 <=> OSes 26.0, OS versions are aligned
686 os_major = kernel_major + 1;
687 } else if (SANITIZER_OSX) {
688 if (os_major >= 16) { // macOS 11+
689 os_major -= 5;
690 } else { // macOS 10.15 and below
691 format = "10.%d";
692 }
693 }
694 return internal_snprintf(vers, sizeof(VersStr), format, os_major);
695}
696
697static void GetOSVersion(VersStr vers) {
698 uptr len = sizeof(VersStr);
699 if (SANITIZER_IOSSIM) {
700 const char *vers_env = GetEnv("SIMULATOR_RUNTIME_VERSION");
701 if (!vers_env) {
702 Report("ERROR: Running in simulator but SIMULATOR_RUNTIME_VERSION env "
703 "var is not set.\n");
704 Die();
705 }
706 len = internal_strlcpy(vers, vers_env, len);
707 } else {
708 int res =
709 internal_sysctlbyname("kern.osproductversion", vers, &len, nullptr, 0);
710
711 // XNU 17 (macOS 10.13) and below do not provide the sysctl
712 // `kern.osproductversion` entry (res != 0).
713 bool no_os_version = res != 0;
714
715 // For launchd, sanitizer initialization runs before sysctl is setup
716 // (res == 0 && len != strlen(vers), vers is not a valid version). However,
717 // the kernel version `kern.osrelease` is available.
718 bool launchd = (res == 0 && internal_strlen(vers) < 3);
719 if (launchd) CHECK_EQ(internal_getpid(), 1);
720
721 if (no_os_version || launchd) {
722 len = ApproximateOSVersionViaKernelVersion(vers);
723 }
724 }
725 CHECK_LT(len, sizeof(VersStr));
726}
727
728void ParseVersion(const char *vers, u16 *major, u16 *minor) {
729 // Format: <major>.<minor>[.<patch>]\0
730 CHECK_GE(internal_strlen(vers), 3);
731 const char *p = vers;
732 *major = internal_simple_strtoll(p, &p, /*base=*/10);
733 CHECK_EQ(*p, '.');
734 p += 1;
735 *minor = internal_simple_strtoll(p, &p, /*base=*/10);
736}
737
738// Aligned versions example:
739// macOS 10.15 -- iOS 13 -- tvOS 13 -- watchOS 6
740static void MapToMacos(u16 *major, u16 *minor) {
741 if (SANITIZER_OSX)
742 return;
743
744 // All supported platforms (including DriverKit) have
745 // aligned version numbers in macOS 27+
746 if (*major >= 27)
747 return;
748
749# if SANITIZER_DRIVERKIT
750 // Driverkit 25.0+ aligns with macOS 26+
751 if (*major >= 25) {
752 *major += 1;
753 return;
754 }
755# else
756 // macOS 26 and later have aligned version strings.
757 if (*major >= 26)
758 return;
759# endif
760
761 // Below are mappings for pre-macOS-25-aligned releases
762 if (SANITIZER_IPHONEOS || SANITIZER_TVOS)
763 *major += 2;
764 else if (SANITIZER_WATCHOS)
765 *major += 9;
766 else if (SANITIZER_VISIONOS)
767 *major += 18;
768 else
769 UNREACHABLE("unsupported platform");
770
771 if (*major >= 16) { // macOS 11+
772 *major -= 5;
773 } else { // macOS 10.15 and below
774 *minor = *major;
775 *major = 10;
776 }
777}
778
779static MacosVersion GetMacosAlignedVersionInternal() {
780 VersStr vers = {};
781 GetOSVersion(vers);
782
783 u16 major, minor;
784 ParseVersion(vers, &major, &minor);
785 MapToMacos(&major, &minor);
786
787 return MacosVersion(major, minor);
788}
789
790static_assert(sizeof(MacosVersion) == sizeof(atomic_uint32_t::Type),
791 "MacosVersion cache size");
792static atomic_uint32_t cached_macos_version;
793
794MacosVersion GetMacosAlignedVersion() {
795 atomic_uint32_t::Type result =
796 atomic_load(&cached_macos_version, memory_order_acquire);
797 if (!result) {
798 MacosVersion version = GetMacosAlignedVersionInternal();
799 result = *reinterpret_cast<atomic_uint32_t::Type *>(&version);
800 atomic_store(&cached_macos_version, result, memory_order_release);
801 }
802 return *reinterpret_cast<MacosVersion *>(&result);
803}
804
805DarwinKernelVersion GetDarwinKernelVersion() {
806 VersStr vers = {};
807 uptr len = sizeof(VersStr);
808 int res = internal_sysctlbyname("kern.osrelease", vers, &len, nullptr, 0);
809 CHECK_EQ(res, 0);
810 CHECK_LT(len, sizeof(VersStr));
811
812 u16 major, minor;
813 ParseVersion(vers, &major, &minor);
814
815 return DarwinKernelVersion(major, minor);
816}
817
818uptr GetRSS() {
819 struct task_basic_info info;
820 unsigned count = TASK_BASIC_INFO_COUNT;
821 kern_return_t result =
822 task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count);
823 if (UNLIKELY(result != KERN_SUCCESS)) {
824 Report("Cannot get task info. Error: %d\n", result);
825 Die();
826 }
827 return info.resident_size;
828}
829
830void *internal_start_thread(void *(*func)(void *arg), void *arg) {
831 // Start the thread with signals blocked, otherwise it can steal user signals.
832 __sanitizer_sigset_t set, old;
833 internal_sigfillset(&set);
834 internal_sigprocmask(SIG_SETMASK, &set, &old);
835 pthread_t th;
836 pthread_create(&th, 0, func, arg);
837 internal_sigprocmask(SIG_SETMASK, &old, 0);
838 return th;
839}
840
841void internal_join_thread(void *th) { pthread_join((pthread_t)th, 0); }
842
843#if !SANITIZER_GO
844static Mutex syslog_lock;
845# endif
846
847# if SANITIZER_DRIVERKIT
848# define SANITIZER_OS_LOG os_log
849# else
850# define SANITIZER_OS_LOG os_log_error
851# endif
852
853void WriteOneLineToSyslog(const char *s) {
854#if !SANITIZER_GO
855 syslog_lock.CheckLocked();
856 if (GetMacosAlignedVersion() >= MacosVersion(10, 12)) {
857 SANITIZER_OS_LOG(OS_LOG_DEFAULT, "%{public}s", s);
858 } else {
859#pragma clang diagnostic push
860// as_log is deprecated.
861#pragma clang diagnostic ignored "-Wdeprecated-declarations"
862 asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", s);
863#pragma clang diagnostic pop
864 }
865#endif
866}
867
868// buffer to store crash report application information
869static char crashreporter_info_buff[__sanitizer::kErrorMessageBufferSize] = {};
870static Mutex crashreporter_info_mutex;
871
872extern "C" {
873
874#if HAVE_CRASHREPORTERCLIENT_H
875// Available in CRASHREPORTER_ANNOTATIONS_VERSION 5+
876# ifdef CRASHREPORTER_ANNOTATIONS_INITIALIZER
877CRASHREPORTER_ANNOTATIONS_INITIALIZER()
878# else
879// Support for older CrashRerporter annotiations
880CRASH_REPORTER_CLIENT_HIDDEN
881struct crashreporter_annotations_t gCRAnnotations
882 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) = {
883 CRASHREPORTER_ANNOTATIONS_VERSION,
884 0,
885 0,
886 0,
887 0,
888 0,
889 0,
890# if CRASHREPORTER_ANNOTATIONS_VERSION > 4
891 0,
892# endif
893};
894# endif
895# else
896// Revert to previous crash reporter API if client header is not available
897static const char *__crashreporter_info__ __attribute__((__used__)) =
898 &crashreporter_info_buff[0];
899asm(".desc ___crashreporter_info__, 0x10");
900#endif // HAVE_CRASHREPORTERCLIENT_H
901
902} // extern "C"
903
904static void CRAppendCrashLogMessage(const char *msg) {
905 Lock l(&crashreporter_info_mutex);
906 internal_strlcat(crashreporter_info_buff, msg,
907 sizeof(crashreporter_info_buff));
908#if HAVE_CRASHREPORTERCLIENT_H
909 (void)CRSetCrashLogMessage(crashreporter_info_buff);
910#endif
911}
912
913void LogMessageOnPrintf(const char *str) {
914 // Log all printf output to CrashLog.
915 if (common_flags()->abort_on_error)
916 CRAppendCrashLogMessage(str);
917}
918
919void LogFullErrorReport(const char *buffer) {
920# if !SANITIZER_GO
921 // When logging with os_log_error this will make it into the crash log.
922 if (internal_strncmp(SanitizerToolName, "AddressSanitizer",
923 sizeof("AddressSanitizer") - 1) == 0)
924 SANITIZER_OS_LOG(OS_LOG_DEFAULT, "Address Sanitizer reported a failure.");
925 else if (internal_strncmp(SanitizerToolName, "UndefinedBehaviorSanitizer",
926 sizeof("UndefinedBehaviorSanitizer") - 1) == 0)
927 SANITIZER_OS_LOG(OS_LOG_DEFAULT,
928 "Undefined Behavior Sanitizer reported a failure.");
929 else if (internal_strncmp(SanitizerToolName, "ThreadSanitizer",
930 sizeof("ThreadSanitizer") - 1) == 0)
931 SANITIZER_OS_LOG(OS_LOG_DEFAULT, "Thread Sanitizer reported a failure.");
932 else
933 SANITIZER_OS_LOG(OS_LOG_DEFAULT, "Sanitizer tool reported a failure.");
934
935 if (common_flags()->log_to_syslog)
936 SANITIZER_OS_LOG(OS_LOG_DEFAULT, "Consult syslog for more information.");
937
938 // Log to syslog.
939 // The logging on OS X may call pthread_create so we need the threading
940 // environment to be fully initialized. Also, this should never be called when
941 // holding the thread registry lock since that may result in a deadlock. If
942 // the reporting thread holds the thread registry mutex, and asl_log waits
943 // for GCD to dispatch a new thread, the process will deadlock, because the
944 // pthread_create wrapper needs to acquire the lock as well.
945 Lock l(&syslog_lock);
946 if (common_flags()->log_to_syslog)
947 WriteToSyslog(buffer);
948
949 // The report is added to CrashLog as part of logging all of Printf output.
950# endif // !SANITIZER_GO
951}
952
953SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
954#if defined(__x86_64__) || defined(__i386__)
955 ucontext_t *ucontext = static_cast<ucontext_t*>(context);
956 return ucontext->uc_mcontext->__es.__err & 2 /*T_PF_WRITE*/ ? Write : Read;
957#elif defined(__arm64__)
958 ucontext_t *ucontext = static_cast<ucontext_t*>(context);
959 return ucontext->uc_mcontext->__es.__esr & 0x40 /*ISS_DA_WNR*/ ? Write : Read;
960#else
961 return Unknown;
962#endif
963}
964
965bool SignalContext::IsTrueFaultingAddress() const {
966 auto si = static_cast<const siginfo_t *>(siginfo);
967 // "Real" SIGSEGV codes (e.g., SEGV_MAPERR, SEGV_MAPERR) are non-zero.
968 return si->si_signo == SIGSEGV && si->si_code != 0;
969}
970
971#if defined(__aarch64__) && defined(arm_thread_state64_get_sp)
972 #define AARCH64_GET_REG(r) \
973 (uptr)ptrauth_strip( \
974 (void *)arm_thread_state64_get_##r(ucontext->uc_mcontext->__ss), 0)
975#else
976 #define AARCH64_GET_REG(r) (uptr)ucontext->uc_mcontext->__ss.__##r
977#endif
978
979static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
980 ucontext_t *ucontext = (ucontext_t*)context;
981# if defined(__aarch64__)
982 *pc = AARCH64_GET_REG(pc);
983 *bp = AARCH64_GET_REG(fp);
984 *sp = AARCH64_GET_REG(sp);
985# elif defined(__x86_64__)
986 *pc = ucontext->uc_mcontext->__ss.__rip;
987 *bp = ucontext->uc_mcontext->__ss.__rbp;
988 *sp = ucontext->uc_mcontext->__ss.__rsp;
989# elif defined(__arm__)
990 *pc = ucontext->uc_mcontext->__ss.__pc;
991 *bp = ucontext->uc_mcontext->__ss.__r[7];
992 *sp = ucontext->uc_mcontext->__ss.__sp;
993# elif defined(__i386__)
994 *pc = ucontext->uc_mcontext->__ss.__eip;
995 *bp = ucontext->uc_mcontext->__ss.__ebp;
996 *sp = ucontext->uc_mcontext->__ss.__esp;
997# else
998# error "Unknown architecture"
999# endif
1000}
1001
1002void SignalContext::InitPcSpBp() {
1003 addr = (uptr)ptrauth_strip((void *)addr, 0);
1004 GetPcSpBp(context, &pc, &sp, &bp);
1005}
1006
1007// ASan/TSan use mmap in a way that creates “deallocation gaps” which triggers
1008// EXC_GUARD exceptions on macOS 10.15+ (XNU 19.0+).
1009static void DisableMmapExcGuardExceptions() {
1010 using task_exc_guard_behavior_t = uint32_t;
1011 using task_set_exc_guard_behavior_t =
1012 kern_return_t(task_t task, task_exc_guard_behavior_t behavior);
1013 auto *set_behavior = (task_set_exc_guard_behavior_t *)dlsym(
1014 RTLD_DEFAULT, "task_set_exc_guard_behavior");
1015 if (set_behavior == nullptr) return;
1016 const task_exc_guard_behavior_t task_exc_guard_none = 0;
1017 kern_return_t res = set_behavior(mach_task_self(), task_exc_guard_none);
1018 if (res != KERN_SUCCESS) {
1019 Report(
1020 "WARN: task_set_exc_guard_behavior returned %d (%s), "
1021 "mmap may fail unexpectedly.\n",
1022 res, mach_error_string(res));
1023 if (res == KERN_DENIED)
1024 Report(
1025 "HINT: Check that task_set_exc_guard_behavior is allowed by "
1026 "sandbox.\n");
1027 }
1028}
1029
1030static void VerifyInterceptorsWorking();
1031static void StripEnv();
1032
1033void InitializePlatformEarly() {
1034 // Only use xnu_fast_mmap when on x86_64 and the kernel supports it.
1035 use_xnu_fast_mmap =
1036#if defined(__x86_64__)
1037 GetDarwinKernelVersion() >= DarwinKernelVersion(17, 5);
1038#else
1039 false;
1040#endif
1041 if (GetDarwinKernelVersion() >= DarwinKernelVersion(19, 0))
1042 DisableMmapExcGuardExceptions();
1043
1044# if !SANITIZER_GO
1045 MonotonicNanoTime(); // Call to initialize mach_timebase_info
1046 VerifyInterceptorsWorking();
1047 StripEnv();
1048# endif
1049}
1050
1051#if !SANITIZER_GO
1052static const char kDyldInsertLibraries[] = "DYLD_INSERT_LIBRARIES";
1053LowLevelAllocator allocator_for_env;
1054
1055static bool ShouldCheckInterceptors() {
1056 // Restrict "interceptors working?" check
1057 const char *sanitizer_names[] = {"AddressSanitizer", "ThreadSanitizer",
1058 "RealtimeSanitizer"};
1059 size_t count = sizeof(sanitizer_names) / sizeof(sanitizer_names[0]);
1060 for (size_t i = 0; i < count; i++) {
1061 if (internal_strcmp(sanitizer_names[i], SanitizerToolName) == 0)
1062 return true;
1063 }
1064 return false;
1065}
1066
1067static void VerifyInterceptorsWorking() {
1068 if (!common_flags()->verify_interceptors || !ShouldCheckInterceptors())
1069 return;
1070
1071 // Verify that interceptors really work. We'll use dlsym to locate
1072 // "puts", if interceptors are working, it should really point to
1073 // "wrap_puts" within our own dylib.
1074 Dl_info info_puts, info_runtime;
1075 RAW_CHECK(dladdr(dlsym(RTLD_DEFAULT, "puts"), &info_puts));
1076 RAW_CHECK(dladdr((void *)&VerifyInterceptorsWorking, &info_runtime));
1077 if (internal_strcmp(info_puts.dli_fname, info_runtime.dli_fname) != 0) {
1078 Report(
1079 "ERROR: Interceptors are not working. This may be because %s is "
1080 "loaded too late (e.g. via dlopen). Please launch the executable "
1081 "with:\n%s=%s\n",
1082 SanitizerToolName, kDyldInsertLibraries, info_runtime.dli_fname);
1083 RAW_CHECK("interceptors not installed" && 0);
1084 }
1085}
1086
1087// Change the value of the env var |name|, leaking the original value.
1088// If |name_value| is NULL, the variable is deleted from the environment,
1089// otherwise the corresponding "NAME=value" string is replaced with
1090// |name_value|.
1091static void LeakyResetEnv(const char *name, const char *name_value) {
1092 char **env = GetEnviron();
1093 uptr name_len = internal_strlen(name);
1094 while (*env != 0) {
1095 uptr len = internal_strlen(*env);
1096 if (len > name_len) {
1097 const char *p = *env;
1098 if (!internal_memcmp(p, name, name_len) && p[name_len] == '=') {
1099 // Match.
1100 if (name_value) {
1101 // Replace the old value with the new one.
1102 *env = const_cast<char*>(name_value);
1103 } else {
1104 // Shift the subsequent pointers back.
1105 char **del = env;
1106 do {
1107 del[0] = del[1];
1108 } while (*del++);
1109 }
1110 }
1111 }
1112 env++;
1113 }
1114}
1115
1116static void StripEnv() {
1117 if (!common_flags()->strip_env)
1118 return;
1119
1120 char *dyld_insert_libraries =
1121 const_cast<char *>(GetEnv(kDyldInsertLibraries));
1122 if (!dyld_insert_libraries)
1123 return;
1124
1125 Dl_info info;
1126 RAW_CHECK(dladdr((void *)&StripEnv, &info));
1127 const char *dylib_name = StripModuleName(info.dli_fname);
1128 bool lib_is_in_env = internal_strstr(dyld_insert_libraries, dylib_name);
1129 if (!lib_is_in_env)
1130 return;
1131
1132 // DYLD_INSERT_LIBRARIES is set and contains the runtime library. Let's remove
1133 // the dylib from the environment variable, because interceptors are installed
1134 // and we don't want our children to inherit the variable.
1135
1136 uptr old_env_len = internal_strlen(dyld_insert_libraries);
1137 uptr dylib_name_len = internal_strlen(dylib_name);
1138 uptr env_name_len = internal_strlen(kDyldInsertLibraries);
1139 // Allocate memory to hold the previous env var name, its value, the '='
1140 // sign and the '\0' char.
1141 char *new_env = (char*)allocator_for_env.Allocate(
1142 old_env_len + 2 + env_name_len);
1143 RAW_CHECK(new_env);
1144 internal_memset(new_env, '\0', old_env_len + 2 + env_name_len);
1145 internal_strncpy(new_env, kDyldInsertLibraries, env_name_len);
1146 new_env[env_name_len] = '=';
1147 char *new_env_pos = new_env + env_name_len + 1;
1148
1149 // Iterate over colon-separated pieces of |dyld_insert_libraries|.
1150 char *piece_start = dyld_insert_libraries;
1151 char *piece_end = NULL;
1152 char *old_env_end = dyld_insert_libraries + old_env_len;
1153 do {
1154 if (piece_start[0] == ':') piece_start++;
1155 piece_end = internal_strchr(piece_start, ':');
1156 if (!piece_end) piece_end = dyld_insert_libraries + old_env_len;
1157 if ((uptr)(piece_start - dyld_insert_libraries) > old_env_len) break;
1158 uptr piece_len = piece_end - piece_start;
1159
1160 char *filename_start =
1161 (char *)internal_memrchr(piece_start, '/', piece_len);
1162 uptr filename_len = piece_len;
1163 if (filename_start) {
1164 filename_start += 1;
1165 filename_len = piece_len - (filename_start - piece_start);
1166 } else {
1167 filename_start = piece_start;
1168 }
1169
1170 // If the current piece isn't the runtime library name,
1171 // append it to new_env.
1172 if ((dylib_name_len != filename_len) ||
1173 (internal_memcmp(filename_start, dylib_name, dylib_name_len) != 0)) {
1174 if (new_env_pos != new_env + env_name_len + 1) {
1175 new_env_pos[0] = ':';
1176 new_env_pos++;
1177 }
1178 internal_strncpy(new_env_pos, piece_start, piece_len);
1179 new_env_pos += piece_len;
1180 }
1181 // Move on to the next piece.
1182 piece_start = piece_end;
1183 } while (piece_start < old_env_end);
1184
1185 // Can't use setenv() here, because it requires the allocator to be
1186 // initialized.
1187 // FIXME: instead of filtering DYLD_INSERT_LIBRARIES here, do it in
1188 // a separate function called after InitializeAllocator().
1189 if (new_env_pos == new_env + env_name_len + 1) new_env = NULL;
1190 LeakyResetEnv(kDyldInsertLibraries, new_env);
1191}
1192#endif // SANITIZER_GO
1193
1194// Prints out a consolidated memory map: contiguous regions
1195// are merged together.
1196static void PrintVmmap() {
1197 const mach_vm_address_t max_vm_address = GetMaxVirtualAddress() + 1;
1198 mach_vm_address_t address = GAP_SEARCH_START_ADDRESS;
1199 kern_return_t kr = KERN_SUCCESS;
1200
1201 Report("Memory map:\n");
1202 mach_vm_address_t last = 0;
1203 mach_vm_address_t lastsz = 0;
1204
1205 while (1) {
1206 mach_vm_size_t vmsize = 0;
1207 natural_t depth = 0;
1208 vm_region_submap_short_info_data_64_t vminfo;
1209 mach_msg_type_number_t count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
1210 kr = internal_mach_vm_region_recurse(mach_task_self(), &address, &vmsize,
1211 &depth, (vm_region_info_t)&vminfo,
1212 &count);
1213
1214 if (kr == KERN_SUCCESS && address < max_vm_address) {
1215 if (last + lastsz == address) {
1216 // This region is contiguous with the last; merge together.
1217 lastsz += vmsize;
1218 } else {
1219 if (lastsz)
1220 Printf("|| `[%p, %p]` || size=0x%016" PRIx64 " ||\n", (void*)last,
1221 (void*)(last + lastsz), lastsz);
1222
1223 last = address;
1224 lastsz = vmsize;
1225 }
1226 address += vmsize;
1227 } else {
1228 // We've reached the end of the memory map. Print the last remaining
1229 // region, if there is one.
1230 if (lastsz)
1231 Printf("|| `[%p, %p]` || size=0x%016" PRIx64 " ||\n", (void*)last,
1232 (void*)(last + lastsz), lastsz);
1233
1234 break;
1235 }
1236 }
1237}
1238
1239static void ReportShadowAllocFail(uptr shadow_size_bytes, uptr alignment) {
1240 Report(
1241 "FATAL: Failed to allocate shadow memory. Tried to allocate %p bytes "
1242 "(alignment=%p).\n",
1243 (void*)shadow_size_bytes, (void*)alignment);
1244 PrintVmmap();
1245}
1246
1247char **GetArgv() {
1248 return *_NSGetArgv();
1249}
1250
1251// These platforms definitely support task_info.
1252# if SANITIZER_IOS || SANITIZER_DRIVERKIT || SANITIZER_OSX
1253
1254// The task_vm_info struct is normally provided by the macOS SDK, but we need
1255// fields only available in 10.12+. Declare the struct manually to be able to
1256// build against older SDKs.
1257struct __sanitizer_task_vm_info {
1258 mach_vm_size_t virtual_size;
1259 integer_t region_count;
1260 integer_t page_size;
1261 mach_vm_size_t resident_size;
1262 mach_vm_size_t resident_size_peak;
1263 mach_vm_size_t device;
1264 mach_vm_size_t device_peak;
1265 mach_vm_size_t internal;
1266 mach_vm_size_t internal_peak;
1267 mach_vm_size_t external;
1268 mach_vm_size_t external_peak;
1269 mach_vm_size_t reusable;
1270 mach_vm_size_t reusable_peak;
1271 mach_vm_size_t purgeable_volatile_pmap;
1272 mach_vm_size_t purgeable_volatile_resident;
1273 mach_vm_size_t purgeable_volatile_virtual;
1274 mach_vm_size_t compressed;
1275 mach_vm_size_t compressed_peak;
1276 mach_vm_size_t compressed_lifetime;
1277 mach_vm_size_t phys_footprint;
1278 mach_vm_address_t min_address;
1279 mach_vm_address_t max_address;
1280};
1281#define __SANITIZER_TASK_VM_INFO_COUNT ((mach_msg_type_number_t) \
1282 (sizeof(__sanitizer_task_vm_info) / sizeof(natural_t)))
1283
1284static uptr GetTaskInfoMaxAddress() {
1285 __sanitizer_task_vm_info vm_info = {} /* zero initialize */;
1286 mach_msg_type_number_t count = __SANITIZER_TASK_VM_INFO_COUNT;
1287 int err = task_info(mach_task_self(), TASK_VM_INFO, (int *)&vm_info, &count);
1288 return err ? 0 : vm_info.max_address;
1289}
1290
1291# endif
1292
1293# if SANITIZER_IOSDEVICE || SANITIZER_DRIVERKIT
1294
1295uptr GetMaxUserVirtualAddress() {
1296 static uptr max_vm = GetTaskInfoMaxAddress();
1297 if (max_vm != 0) {
1298 return max_vm - 1;
1299 }
1300
1301 // xnu cannot provide vm address limit
1302# if SANITIZER_WORDSIZE == 32
1303 constexpr uptr fallback_max_vm = 0xffe00000 - 1;
1304# else
1305 constexpr uptr fallback_max_vm = 0x200000000 - 1;
1306# endif
1307 static_assert(fallback_max_vm <= SANITIZER_MMAP_RANGE_SIZE,
1308 "Max virtual address must be less than mmap range size.");
1309 return fallback_max_vm;
1310}
1311
1312# else // !(SANITIZER_IOSDEVICE || SANITIZER_DRIVERKIT)
1313
1314uptr GetMaxUserVirtualAddress() {
1315# if SANITIZER_WORDSIZE == 64
1316 constexpr uptr max_vm = (1ULL << 47) - 1; // 0x00007fffffffffffUL;
1317# else // SANITIZER_WORDSIZE == 32
1318 static_assert(SANITIZER_WORDSIZE == 32, "Wrong wordsize");
1319 constexpr uptr max_vm = (1ULL << 32) - 1; // 0xffffffff;
1320# endif
1321 static_assert(max_vm <= SANITIZER_MMAP_RANGE_SIZE,
1322 "Max virtual address must be less than mmap range size.");
1323 return max_vm;
1324}
1325# endif
1326
1327bool ActivateDebugMemory() {
1328# if SANITIZER_EMBEDDED_VM_LAYOUT
1329 // On embedded devices, the debug region should be higher than the range
1330 // normally returned by mmap.
1331 static_assert(DARWIN_DEBUG_MEMORY_START >=
1332 SANITIZER_MMAP_BEGIN + SANITIZER_MMAP_RANGE_SIZE);
1333
1334 uptr cur_max = GetTaskInfoMaxAddress();
1335 // The "old layout" is used when the OS version is less than 27.0 or
1336 // when the max_offset is <= 448G (even in 27.0, not all processes
1337 // must use the new layout)
1338 if (cur_max <= DARWIN_DEBUG_MEMORY_VMOFFSET_FLOOR ||
1339 GetMacosAlignedVersion() < DARWIN_DEBUG_MEMORY_VERSION_FLOOR) {
1340 VReport(2, "Falling back to old layout cur_max=%p\n", (void*)cur_max);
1341 return false;
1342 }
1343
1344 // Now, we prepare to activate the "debug range". This expands the
1345 // max vm offset from ~512G to ~8TB, but doesn't change the space
1346 // available to applications. First, we check that the max vm offset
1347 // is not already expanded...
1348
1349 // Max address above the debug range is unexpected. It could suggest that
1350 // another tool has already activated the debug memory region, but that
1351 // would be unexpected as we are supposed to run early in process launch.
1352 // Since we do not guarantee compatbility with other tools, bail out here.
1353 if (cur_max > DARWIN_DEBUG_MEMORY_START) {
1354 Report(
1355 "FATAL: Unexpected VM layout. max addr = %p, expected <= %p. Is "
1356 "another tool using debug memory?",
1357 (void*)cur_max, DARWIN_DEBUG_MEMORY_START);
1358 Die();
1359 }
1360
1361 VReport(2, "Using debug memory range cur_max=%p\n", (void*)cur_max);
1362 uint32_t enabled = 1;
1363 int ret = sysctlbyname("vm.debug_range_enabled", NULL, NULL, &enabled,
1364 sizeof(enabled));
1365 uptr new_max = GetTaskInfoMaxAddress();
1366
1367 if (ret != 0 || new_max <= DARWIN_DEBUG_MEMORY_START) {
1368 // Not being able to activate debug memory is essentially guaranteed to be
1369 // fatal: for ASAN 512GB of VM requires 64G of contiguous shadow, which
1370 // there just isn't space for without debug range. So we do not attempt to
1371 // support that.
1372 Report(
1373 "FATAL: vm.debug_range_enabled sysctl failed to activate (ret=%d).\n",
1374 ret);
1375 Report(
1376 "HINT: Check that the device is in developer mode and that the "
1377 "sysctl is not denied by sandbox.\n");
1378 Die();
1379 }
1380
1381 // Success: The debug range was successfully enabled.
1382 VReport(2, "vm.debug_range_enabled updated successfully, new max=%p\n",
1383 (void*)new_max);
1384 debug_region_activated = true;
1385 return true;
1386
1387# endif // SANITIZER_EMBEDDED_VM_LAYOUT
1388 return false;
1389}
1390
1391bool GetDebugMemoryRange(mach_vm_range* debug_range) {
1392 size_t sz = sizeof(*debug_range);
1393 int ret =
1394 sysctlbyname("vm.vm_map_user_range_debug", debug_range, &sz, NULL, 0);
1395 return ret == 0 && sz == sizeof(*debug_range);
1396}
1397
1398uptr GetMaxVirtualAddress() {
1399 return GetMaxUserVirtualAddress();
1400}
1401
1402uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
1403 uptr min_shadow_base_alignment, uptr &high_mem_end,
1404 uptr granularity) {
1405 const uptr alignment =
1406 Max<uptr>(granularity << shadow_scale, 1ULL << min_shadow_base_alignment);
1407 const uptr left_padding =
1408 Max<uptr>(granularity, 1ULL << min_shadow_base_alignment);
1409
1410 uptr space_size = shadow_size_bytes;
1411
1412 uptr largest_gap_found = 0;
1413 uptr max_occupied_addr = 0;
1414
1415 // Note that the below call may expand the address space reported by
1416 // GetTaskInfoMaxAddress. The original max_vm is stored in kHighMemEnd (and
1417 // was used to compute shadow_size_bytes.) and also
1418 // GetMaxUserVirtualAddress/GetMaxVirtualAddress use a static variable and
1419 // thus always return the original max.
1420 bool use_debug_vm = ActivateDebugMemory();
1421
1422 VReport(2, "FindDynamicShadowStart, space_size = %p\n", (void *)space_size);
1423 uptr shadow_start = FindAvailableMemoryRange(
1424 space_size, alignment, left_padding, &largest_gap_found,
1425 &max_occupied_addr, use_debug_vm);
1426 // If the shadow doesn't fit, restrict the address space to make it fit.
1427 if (!use_debug_vm && shadow_start == 0) {
1428 VReport(
1429 2,
1430 "Shadow doesn't fit, largest_gap_found = %p, max_occupied_addr = %p\n",
1431 (void *)largest_gap_found, (void *)max_occupied_addr);
1432 uptr new_max_vm = RoundDownTo(largest_gap_found << shadow_scale, alignment);
1433 if (new_max_vm < max_occupied_addr) {
1434 Report("Unable to find a memory range for dynamic shadow.\n");
1435 Report(
1436 "\tspace_size = %p\n\tlargest_gap_found = %p\n\tmax_occupied_addr "
1437 "= %p\n\tnew_max_vm = %p\n",
1438 (void*)space_size, (void*)largest_gap_found, (void*)max_occupied_addr,
1439 (void*)new_max_vm);
1440 ReportShadowAllocFail(shadow_size_bytes, alignment);
1441 CHECK(0 && "cannot place shadow");
1442 }
1443 RestrictMemoryToMaxAddress(new_max_vm);
1444 high_mem_end = new_max_vm - 1;
1445 space_size = (high_mem_end >> shadow_scale);
1446 VReport(2, "FindDynamicShadowStart, space_size = %p\n", (void *)space_size);
1447 shadow_start = FindAvailableMemoryRange(space_size, alignment, left_padding,
1448 nullptr, nullptr,
1449 /* use_debug_vm */ false);
1450 if (shadow_start == 0) {
1451 Report("Unable to find a memory range after restricting VM.\n");
1452 ReportShadowAllocFail(shadow_size_bytes, alignment);
1453 CHECK(0 && "cannot place shadow after restricting vm");
1454 }
1455 }
1456
1457 CHECK_NE((uptr)0, shadow_start);
1458 CHECK(IsAligned(shadow_start, alignment));
1459 return shadow_start;
1460}
1461
1462// Returns a list of ranges which must be covered by shadow memory,
1463// and cannot overlap with any fixed mappings made by a sanitizer.
1464//
1465// NOTE: Ranges must only be page-aligned.
1466//
1467// This list may be empty on macOS < 27.0, which implies no additional
1468// restriction on the placement of shadow.
1469void GetAppRanges(InternalMmapVector<ReservedRange>& ranges) {
1470 if (GetMacosAlignedVersion() < DARWIN_DEBUG_MEMORY_VERSION_FLOOR)
1471 return;
1472
1473# if SANITIZER_EMBEDDED_VM_LAYOUT
1474 // NOTE: Even in iOS 27.0, some embedded devices and processes may not
1475 // use the new layout.
1476 if (GetTaskInfoMaxAddress() <= DARWIN_DEBUG_MEMORY_VMOFFSET_FLOOR)
1477 return;
1478# endif
1479
1480 ranges.reserve(2);
1481# if SANITIZER_WORDSIZE == 64
1482# if SANITIZER_IOSDEVICE
1483
1484 struct mach_vm_range debug_range;
1485 if (!GetDebugMemoryRange(&debug_range)) {
1486 Report("vm.vm_map_user_range_debug sysctl failed. Denied by sandbox?\n");
1487 Die();
1488 }
1489
1490 // Get app ranges
1491 size_t all_ranges_sz = 0;
1492
1493 // Call 1: Get length
1494 int ret = sysctlbyname("vm.all_map_ranges", NULL, &all_ranges_sz, NULL, 0);
1495 if (ret != 0) {
1496 Report("vm.all_map_ranges sysctl failed. Denied by sandbox?\n");
1497 Die();
1498 }
1499
1500 struct mach_vm_range* all_ranges =
1501 (struct mach_vm_range*)InternalAlloc(all_ranges_sz);
1502 // Call 2: Actually get the ranges
1503 ret = sysctlbyname("vm.all_map_ranges", all_ranges, &all_ranges_sz, NULL, 0);
1504 if (ret != 0) {
1505 Report("vm.all_map_ranges sysctl failed. Denied by sandbox?\n");
1506 Die();
1507 }
1508
1509 bool has_gpu_carveout =
1510 MemoryRangeIsKernelReserved(MACH_VM_MIN_GPU_CARVEOUT_ADDRESS_RAW,
1511 MACH_VM_MAX_GPU_CARVEOUT_ADDRESS_RAW);
1512 for (unsigned i = 0; i < (all_ranges_sz / sizeof(all_ranges[0])); i++) {
1513 struct mach_vm_range range = all_ranges[i];
1514 // Skip the debug range: we don't have to cover it with shadow.
1515 if (range.min_address == debug_range.min_address &&
1516 range.max_address == debug_range.max_address)
1517 continue;
1518 // Skip zero-size ranges
1519 if (range.min_address == range.max_address)
1520 continue;
1521
1522 // Split ranges that contain the carveout
1523 if (has_gpu_carveout) {
1524 // Pre-carveout range, if any
1525 if (range.min_address < MACH_VM_MIN_GPU_CARVEOUT_ADDRESS_RAW)
1526 ranges.push_back(
1527 {range.min_address,
1528 Min(range.max_address, MACH_VM_MIN_GPU_CARVEOUT_ADDRESS_RAW)});
1529
1530 // Post-carveout range, if any
1531 if (range.max_address > MACH_VM_MAX_GPU_CARVEOUT_ADDRESS_RAW)
1532 ranges.push_back(
1533 {Max(range.min_address, MACH_VM_MAX_GPU_CARVEOUT_ADDRESS_RAW),
1534 range.max_address});
1535 } else {
1536 ranges.push_back({range.min_address, range.max_address});
1537 }
1538 }
1539
1540 InternalFree(all_ranges);
1541
1542 // Compute the shared cache range
1543 size_t shared_cache_size;
1544 mach_vm_address_t shared_cache_base =
1545 (mach_vm_address_t)_dyld_get_shared_cache_range(&shared_cache_size);
1546
1547 CHECK(shared_cache_base != 0 && "_dyld_get_shared_cache_range returned NULL");
1548 VReport(2, "Shared cache: [%p, %p]\n", (void*)shared_cache_base,
1549 (void*)(shared_cache_base + shared_cache_size));
1550
1551 // Verify that the shared cache range is within one of the known ranges
1552 bool shared_cache_in_range = false;
1553 for (auto& [range_start, range_end] : ranges) {
1554 shared_cache_in_range |=
1555 (shared_cache_base >= range_start &&
1556 shared_cache_base + shared_cache_size <= range_end);
1557 // Note: GetMaxUserVirtualAddress returns the cached, pre-expansion
1558 // maximum (debug memory increases the max_address reported by task_info)
1559 if (range_end - 1 > GetMaxUserVirtualAddress()) {
1560 Report("FATAL: Unsupported VM layout. Range [%p, %p] is above max %p\n",
1561 range_start, range_end, GetMaxUserVirtualAddress());
1562 Die();
1563 }
1564 }
1565
1566 if (!shared_cache_in_range) {
1567 Report(
1568 "FATAL: Unsupported VM layout. Shared cache [%p, %p] is outside of "
1569 "expected range\n",
1570 shared_cache_base, shared_cache_base + shared_cache_size);
1571 Die();
1572 }
1573# else
1574 // The first 512GB are platform-reserved on macOS.
1575 ranges.push_back({0x1000UL, 0x8000000000UL});
1576# endif
1577# endif
1578
1579 VReport(2, "App ranges:\n");
1580 for (auto& [range_start, range_end] : ranges) {
1581 VReport(2, " [%p, %p]\n", range_start, range_end);
1582 }
1583}
1584
1585uptr MapDynamicShadowAndAliases(uptr shadow_size, uptr alias_size,
1586 uptr num_aliases, uptr ring_buffer_size) {
1587 CHECK(false && "HWASan aliasing is unimplemented on Mac");
1588 return 0;
1589}
1590
1591uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
1592 uptr* largest_gap_found, uptr* max_occupied_addr,
1593 bool use_debug_vm) {
1594 mach_vm_address_t max_vm_address = GetMaxVirtualAddress() + 1;
1595 mach_vm_address_t address = GAP_SEARCH_START_ADDRESS;
1596 mach_vm_address_t free_begin = GAP_SEARCH_START_ADDRESS;
1597
1598 // When using the debug VM range, we restrict our search to start at the
1599 // reported beginning of that range
1600 if (use_debug_vm) {
1601 struct mach_vm_range debug_range;
1602 if (!GetDebugMemoryRange(&debug_range)) {
1603 Report("vm_map_user_range_debug sysctl failed. Denied by sandbox?\n");
1604 Die();
1605 }
1606
1607 // When use_debug_vm is true, we want to search up to the end of the debug
1608 // range.
1609 max_vm_address = debug_range.max_address;
1610
1611 VReport(2, "FindAvailableMemoryRange: Debug range is [%p, %p], max_vm=%p\n",
1612 debug_range.min_address, debug_range.max_address,
1613 GetTaskInfoMaxAddress());
1614
1615 // Begin our search at the beginning of the debug range.
1616 address = debug_range.min_address;
1617 free_begin = debug_range.min_address;
1618 }
1619
1620 // Restrict the search to be after any reserved ranges
1621 InternalMmapVector<ReservedRange> app_ranges;
1622 GetAppRanges(app_ranges);
1623
1624 for (auto& [range_start, range_end] : app_ranges) {
1625 address = Max(address, (mach_vm_address_t)range_end);
1626 free_begin = Max(free_begin, (mach_vm_address_t)range_end);
1627 }
1628
1629 kern_return_t kr = KERN_SUCCESS;
1630 if (largest_gap_found) *largest_gap_found = 0;
1631 if (max_occupied_addr) *max_occupied_addr = 0;
1632 while (kr == KERN_SUCCESS) {
1633 mach_vm_size_t vmsize = 0;
1634 natural_t depth = 0;
1635 vm_region_submap_short_info_data_64_t vminfo;
1636 mach_msg_type_number_t count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
1637 kr = internal_mach_vm_region_recurse(mach_task_self(), &address, &vmsize,
1638 &depth, (vm_region_info_t)&vminfo,
1639 &count);
1640
1641 if (kr == KERN_SUCCESS) {
1642 // There are cases where going beyond the processes' max vm does
1643 // not return KERN_INVALID_ADDRESS so we check for going beyond that
1644 // max address as well.
1645 if (address > max_vm_address) {
1646 address = max_vm_address;
1647 kr = -1; // break after this iteration.
1648 } else if (max_occupied_addr) {
1649 *max_occupied_addr = address + vmsize;
1650 }
1651 } else if (kr == KERN_INVALID_ADDRESS) {
1652 // No more regions beyond "address", consider the gap at the end of VM.
1653 address = max_vm_address;
1654
1655 // We will break after this iteration anyway since kr != KERN_SUCCESS
1656 } else {
1657 Report(
1658 "WARNING: mach_vm_region_recurse returned unexpected code %d (%s)\n",
1659 kr, mach_error_string(kr));
1660 DCHECK(false && "mach_vm_region_recurse returned unexpected code");
1661 break; // address is not valid unless KERN_SUCCESS, therefore we must not
1662 // use it.
1663 }
1664
1665 if (free_begin != address) {
1666 // We found a free region [free_begin..address-1].
1667 uptr gap_start = RoundUpTo((uptr)free_begin + left_padding, alignment);
1668 uptr gap_end = RoundDownTo((uptr)Min(address, max_vm_address), alignment);
1669 uptr gap_size = gap_end > gap_start ? gap_end - gap_start : 0;
1670 if (size < gap_size) {
1671 return gap_start;
1672 }
1673
1674 if (largest_gap_found && *largest_gap_found < gap_size) {
1675 *largest_gap_found = gap_size;
1676 }
1677 }
1678 // Move to the next region.
1679 address += vmsize;
1680 free_begin = address;
1681 }
1682
1683 // We looked at all free regions and could not find one large enough.
1684 return 0;
1685}
1686
1687// This function (when used during initialization when there is
1688// only a single thread), can be used to verify that a range
1689// of memory hasn't already been mapped, and won't be mapped
1690// later in the shared cache.
1691//
1692// If the syscall mach_vm_region_recurse fails (due to sandbox),
1693// we assume that the memory is not mapped so that execution can continue.
1694//
1695// NOTE: range_end is inclusive
1696//
1697// WARNING: This function must NOT allocate memory, since it is
1698// used in InitializeShadowMemory between where we search for
1699// space for shadow and where we actually allocate it.
1700bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
1701 mach_vm_size_t vmsize = 0;
1702 natural_t depth = 0;
1703 vm_region_submap_short_info_data_64_t vminfo;
1704 mach_msg_type_number_t count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
1705 mach_vm_address_t address = range_start;
1706
1707 // First, check if the range is already mapped.
1708 kern_return_t kr = internal_mach_vm_region_recurse(
1709 mach_task_self(), &address, &vmsize, &depth, (vm_region_info_t)&vminfo,
1710 &count);
1711
1712 if (kr == KERN_SUCCESS && !IntervalsAreSeparate(address, address + vmsize - 1,
1713 range_start, range_end)) {
1714 // Overlaps with already-mapped memory
1715 return false;
1716 }
1717
1718 size_t cacheLength;
1719 uptr cacheStart = (uptr)_dyld_get_shared_cache_range(&cacheLength);
1720
1721 if (cacheStart &&
1722 !IntervalsAreSeparate(cacheStart, cacheStart + cacheLength - 1,
1723 range_start, range_end)) {
1724 // Overlaps with shared cache region
1725 return false;
1726 }
1727
1728 // We believe this address is available.
1729 return true;
1730}
1731
1732// Returns true if this range (range_end is exclusive) corresponds exactly
1733// to a kernel-reserved region such as the GPU carveout.
1734bool MemoryRangeIsKernelReserved(uptr range_start, uptr range_end) {
1735 mach_vm_size_t vmsize = 0;
1736 natural_t depth = 0;
1737 vm_region_submap_short_info_data_64_t vminfo;
1738 mach_msg_type_number_t count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
1739 mach_vm_address_t address = range_start;
1740
1741 // Kernel-reserved ranges (e.g. the GPU carve-out) have max_protection == 0,
1742 // i.e. they are permanently PROT_NONE
1743 kern_return_t kr = internal_mach_vm_region_recurse(
1744 mach_task_self(), &address, &vmsize, &depth, (vm_region_info_t)&vminfo,
1745 &count);
1746
1747 return (kr == KERN_SUCCESS && address == range_start &&
1748 address + vmsize == range_end && vminfo.max_protection == 0);
1749}
1750
1751// FIXME implement on this platform.
1752void GetMemoryProfile(fill_profile_f cb, uptr *stats) {}
1753
1754void SignalContext::DumpAllRegisters(void *context) {
1755 Report("Register values:\n");
1756
1757 ucontext_t *ucontext = (ucontext_t*)context;
1758# define DUMPREG64(r) \
1759 Printf("%s = 0x%016llx ", #r, ucontext->uc_mcontext->__ss.__ ## r);
1760# define DUMPREGA64(r) \
1761 Printf(" %s = 0x%016lx ", #r, AARCH64_GET_REG(r));
1762# define DUMPREG32(r) \
1763 Printf("%s = 0x%08x ", #r, ucontext->uc_mcontext->__ss.__ ## r);
1764# define DUMPREG_(r) Printf(" "); DUMPREG(r);
1765# define DUMPREG__(r) Printf(" "); DUMPREG(r);
1766# define DUMPREG___(r) Printf(" "); DUMPREG(r);
1767
1768# if defined(__x86_64__)
1769# define DUMPREG(r) DUMPREG64(r)
1770 DUMPREG(rax); DUMPREG(rbx); DUMPREG(rcx); DUMPREG(rdx); Printf("\n");
1771 DUMPREG(rdi); DUMPREG(rsi); DUMPREG(rbp); DUMPREG(rsp); Printf("\n");
1772 DUMPREG_(r8); DUMPREG_(r9); DUMPREG(r10); DUMPREG(r11); Printf("\n");
1773 DUMPREG(r12); DUMPREG(r13); DUMPREG(r14); DUMPREG(r15); Printf("\n");
1774# elif defined(__i386__)
1775# define DUMPREG(r) DUMPREG32(r)
1776 DUMPREG(eax); DUMPREG(ebx); DUMPREG(ecx); DUMPREG(edx); Printf("\n");
1777 DUMPREG(edi); DUMPREG(esi); DUMPREG(ebp); DUMPREG(esp); Printf("\n");
1778# elif defined(__aarch64__)
1779# define DUMPREG(r) DUMPREG64(r)
1780 DUMPREG_(x[0]); DUMPREG_(x[1]); DUMPREG_(x[2]); DUMPREG_(x[3]); Printf("\n");
1781 DUMPREG_(x[4]); DUMPREG_(x[5]); DUMPREG_(x[6]); DUMPREG_(x[7]); Printf("\n");
1782 DUMPREG_(x[8]); DUMPREG_(x[9]); DUMPREG(x[10]); DUMPREG(x[11]); Printf("\n");
1783 DUMPREG(x[12]); DUMPREG(x[13]); DUMPREG(x[14]); DUMPREG(x[15]); Printf("\n");
1784 DUMPREG(x[16]); DUMPREG(x[17]); DUMPREG(x[18]); DUMPREG(x[19]); Printf("\n");
1785 DUMPREG(x[20]); DUMPREG(x[21]); DUMPREG(x[22]); DUMPREG(x[23]); Printf("\n");
1786 DUMPREG(x[24]); DUMPREG(x[25]); DUMPREG(x[26]); DUMPREG(x[27]); Printf("\n");
1787 DUMPREG(x[28]); DUMPREGA64(fp); DUMPREGA64(lr); DUMPREGA64(sp); Printf("\n");
1788# elif defined(__arm__)
1789# define DUMPREG(r) DUMPREG32(r)
1790 DUMPREG_(r[0]); DUMPREG_(r[1]); DUMPREG_(r[2]); DUMPREG_(r[3]); Printf("\n");
1791 DUMPREG_(r[4]); DUMPREG_(r[5]); DUMPREG_(r[6]); DUMPREG_(r[7]); Printf("\n");
1792 DUMPREG_(r[8]); DUMPREG_(r[9]); DUMPREG(r[10]); DUMPREG(r[11]); Printf("\n");
1793 DUMPREG(r[12]); DUMPREG___(sp); DUMPREG___(lr); DUMPREG___(pc); Printf("\n");
1794# else
1795# error "Unknown architecture"
1796# endif
1797
1798# undef DUMPREG64
1799# undef DUMPREG32
1800# undef DUMPREG_
1801# undef DUMPREG__
1802# undef DUMPREG___
1803# undef DUMPREG
1804}
1805
1806static inline bool CompareBaseAddress(const LoadedModule &a,
1807 const LoadedModule &b) {
1808 return a.base_address() < b.base_address();
1809}
1810
1811void FormatUUID(char *out, uptr size, const u8 *uuid) {
1812 internal_snprintf(out, size,
1813 "<%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-"
1814 "%02X%02X%02X%02X%02X%02X>",
1815 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5],
1816 uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11],
1817 uuid[12], uuid[13], uuid[14], uuid[15]);
1818}
1819
1820void DumpProcessMap() {
1821 Printf("Process module map:\n");
1822 MemoryMappingLayout memory_mapping(false);
1823 InternalMmapVector<LoadedModule> modules;
1824 modules.reserve(128);
1825 memory_mapping.DumpListOfModules(&modules);
1826 Sort(modules.data(), modules.size(), CompareBaseAddress);
1827 for (uptr i = 0; i < modules.size(); ++i) {
1828 char uuid_str[128];
1829 FormatUUID(uuid_str, sizeof(uuid_str), modules[i].uuid());
1830 Printf("%p-%p %s (%s) %s\n", (void *)modules[i].base_address(),
1831 (void *)modules[i].max_address(), modules[i].full_name(),
1832 ModuleArchToString(modules[i].arch()), uuid_str);
1833 }
1834 Printf("End of module map.\n");
1835}
1836
1837void OnDlOpen(const char* filename, int flag) {
1838 // Do nothing.
1839}
1840
1841bool GetRandom(void *buffer, uptr length, bool blocking) {
1842 if (!buffer || !length || length > 256)
1843 return false;
1844 // arc4random never fails.
1845 REAL(arc4random_buf)(buffer, length);
1846 return true;
1847}
1848
1849u32 GetNumberOfCPUs() {
1850 return (u32)sysconf(_SC_NPROCESSORS_ONLN);
1851}
1852
1853void InitializePlatformCommonFlags(CommonFlags *cf) {}
1854
1855// Pthread introspection hook
1856//
1857// * GCD worker threads are created without a call to pthread_create(), but we
1858// still need to register these threads (with ThreadCreate/Start()).
1859// * We use the "pthread introspection hook" below to observe the creation of
1860// such threads.
1861// * GCD worker threads don't have parent threads and the CREATE event is
1862// delivered in the context of the thread itself. CREATE events for regular
1863// threads, are delivered on the parent. We use this to tell apart which
1864// threads are GCD workers with `thread == pthread_self()`.
1865//
1866static pthread_introspection_hook_t prev_pthread_introspection_hook;
1867static ThreadEventCallbacks thread_event_callbacks;
1868
1869static void sanitizer_pthread_introspection_hook(unsigned int event,
1870 pthread_t thread, void *addr,
1871 size_t size) {
1872 // create -> start -> terminate -> destroy
1873 // * create/destroy are usually (not guaranteed) delivered on the parent and
1874 // track resource allocation/reclamation
1875 // * start/terminate are guaranteed to be delivered in the context of the
1876 // thread and give hooks into "just after (before) thread starts (stops)
1877 // executing"
1878 DCHECK(event >= PTHREAD_INTROSPECTION_THREAD_CREATE &&
1879 event <= PTHREAD_INTROSPECTION_THREAD_DESTROY);
1880
1881 if (event == PTHREAD_INTROSPECTION_THREAD_CREATE) {
1882 bool gcd_worker = (thread == pthread_self());
1883 if (thread_event_callbacks.create)
1884 thread_event_callbacks.create((uptr)thread, gcd_worker);
1885 } else if (event == PTHREAD_INTROSPECTION_THREAD_START) {
1886 CHECK_EQ(thread, pthread_self());
1887 if (thread_event_callbacks.start)
1888 thread_event_callbacks.start((uptr)thread);
1889 }
1890
1891 if (prev_pthread_introspection_hook)
1892 prev_pthread_introspection_hook(event, thread, addr, size);
1893
1894 if (event == PTHREAD_INTROSPECTION_THREAD_TERMINATE) {
1895 CHECK_EQ(thread, pthread_self());
1896 if (thread_event_callbacks.terminate)
1897 thread_event_callbacks.terminate((uptr)thread);
1898 } else if (event == PTHREAD_INTROSPECTION_THREAD_DESTROY) {
1899 if (thread_event_callbacks.destroy)
1900 thread_event_callbacks.destroy((uptr)thread);
1901 }
1902}
1903
1904void InstallPthreadIntrospectionHook(const ThreadEventCallbacks &callbacks) {
1905 thread_event_callbacks = callbacks;
1906 prev_pthread_introspection_hook =
1907 pthread_introspection_hook_install(&sanitizer_pthread_introspection_hook);
1908}
1909
1910} // namespace __sanitizer
1911
1912#endif // SANITIZER_APPLE
1913