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