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