1 //===-- sanitizer_linux.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 AddressSanitizer and ThreadSanitizer 10 // run-time libraries and implements linux-specific functions from 11 // sanitizer_libc.h. 12 //===----------------------------------------------------------------------===// 13 14 #include "sanitizer_platform.h" 15 16 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \ 17 SANITIZER_SOLARIS 18 19 #include "sanitizer_common.h" 20 #include "sanitizer_flags.h" 21 #include "sanitizer_getauxval.h" 22 #include "sanitizer_internal_defs.h" 23 #include "sanitizer_libc.h" 24 #include "sanitizer_linux.h" 25 #include "sanitizer_mutex.h" 26 #include "sanitizer_placement_new.h" 27 #include "sanitizer_procmaps.h" 28 29 #if SANITIZER_LINUX && !SANITIZER_GO 30 #include <asm/param.h> 31 #endif 32 33 // For mips64, syscall(__NR_stat) fills the buffer in the 'struct kernel_stat' 34 // format. Struct kernel_stat is defined as 'struct stat' in asm/stat.h. To 35 // access stat from asm/stat.h, without conflicting with definition in 36 // sys/stat.h, we use this trick. 37 #if defined(__mips64) 38 #include <asm/unistd.h> 39 #include <sys/types.h> 40 #define stat kernel_stat 41 #if SANITIZER_GO 42 #undef st_atime 43 #undef st_mtime 44 #undef st_ctime 45 #define st_atime st_atim 46 #define st_mtime st_mtim 47 #define st_ctime st_ctim 48 #endif 49 #include <asm/stat.h> 50 #undef stat 51 #endif 52 53 #include <dlfcn.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <link.h> 57 #include <pthread.h> 58 #include <sched.h> 59 #include <signal.h> 60 #include <sys/mman.h> 61 #include <sys/param.h> 62 #if !SANITIZER_SOLARIS 63 #include <sys/ptrace.h> 64 #endif 65 #include <sys/resource.h> 66 #include <sys/stat.h> 67 #include <sys/syscall.h> 68 #include <sys/time.h> 69 #include <sys/types.h> 70 #include <ucontext.h> 71 #include <unistd.h> 72 73 #if SANITIZER_LINUX 74 #include <sys/utsname.h> 75 #endif 76 77 #if SANITIZER_LINUX && !SANITIZER_ANDROID 78 #include <sys/personality.h> 79 #endif 80 81 #if SANITIZER_FREEBSD 82 #include <sys/exec.h> 83 #include <sys/procctl.h> 84 #include <sys/sysctl.h> 85 #include <machine/atomic.h> 86 extern "C" { 87 // <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on 88 // FreeBSD 9.2 and 10.0. 89 #include <sys/umtx.h> 90 } 91 #include <sys/thr.h> 92 #endif // SANITIZER_FREEBSD 93 94 #if SANITIZER_NETBSD 95 #include <limits.h> // For NAME_MAX 96 #include <sys/sysctl.h> 97 #include <sys/exec.h> 98 extern struct ps_strings *__ps_strings; 99 #endif // SANITIZER_NETBSD 100 101 #if SANITIZER_SOLARIS 102 #include <stdlib.h> 103 #include <thread.h> 104 #define environ _environ 105 #endif 106 107 extern char **environ; 108 109 #if SANITIZER_LINUX 110 // <linux/time.h> 111 struct kernel_timeval { 112 long tv_sec; 113 long tv_usec; 114 }; 115 116 // <linux/futex.h> is broken on some linux distributions. 117 const int FUTEX_WAIT = 0; 118 const int FUTEX_WAKE = 1; 119 const int FUTEX_PRIVATE_FLAG = 128; 120 const int FUTEX_WAIT_PRIVATE = FUTEX_WAIT | FUTEX_PRIVATE_FLAG; 121 const int FUTEX_WAKE_PRIVATE = FUTEX_WAKE | FUTEX_PRIVATE_FLAG; 122 #endif // SANITIZER_LINUX 123 124 // Are we using 32-bit or 64-bit Linux syscalls? 125 // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32 126 // but it still needs to use 64-bit syscalls. 127 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__powerpc64__) || \ 128 SANITIZER_WORDSIZE == 64) 129 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1 130 #else 131 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0 132 #endif 133 134 // Note : FreeBSD had implemented both 135 // Linux apis, available from 136 // future 12.x version most likely 137 #if SANITIZER_LINUX && defined(__NR_getrandom) 138 # if !defined(GRND_NONBLOCK) 139 # define GRND_NONBLOCK 1 140 # endif 141 # define SANITIZER_USE_GETRANDOM 1 142 #else 143 # define SANITIZER_USE_GETRANDOM 0 144 #endif // SANITIZER_LINUX && defined(__NR_getrandom) 145 146 #if SANITIZER_FREEBSD && __FreeBSD_version >= 1200000 147 # define SANITIZER_USE_GETENTROPY 1 148 #else 149 # define SANITIZER_USE_GETENTROPY 0 150 #endif 151 152 namespace __sanitizer { 153 154 #if SANITIZER_LINUX && defined(__x86_64__) 155 #include "sanitizer_syscall_linux_x86_64.inc" 156 #elif SANITIZER_LINUX && SANITIZER_RISCV64 157 #include "sanitizer_syscall_linux_riscv64.inc" 158 #elif SANITIZER_LINUX && defined(__aarch64__) 159 #include "sanitizer_syscall_linux_aarch64.inc" 160 #elif SANITIZER_LINUX && defined(__arm__) 161 #include "sanitizer_syscall_linux_arm.inc" 162 #else 163 #include "sanitizer_syscall_generic.inc" 164 #endif 165 166 // --------------- sanitizer_libc.h 167 #if !SANITIZER_SOLARIS && !SANITIZER_NETBSD 168 #if !SANITIZER_S390 169 uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd, 170 u64 offset) { 171 #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS 172 return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd, 173 offset); 174 #else 175 // mmap2 specifies file offset in 4096-byte units. 176 CHECK(IsAligned(offset, 4096)); 177 return internal_syscall(SYSCALL(mmap2), addr, length, prot, flags, fd, 178 offset / 4096); 179 #endif 180 } 181 #endif // !SANITIZER_S390 182 183 uptr internal_munmap(void *addr, uptr length) { 184 return internal_syscall(SYSCALL(munmap), (uptr)addr, length); 185 } 186 187 #if SANITIZER_LINUX 188 uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags, 189 void *new_address) { 190 return internal_syscall(SYSCALL(mremap), (uptr)old_address, old_size, 191 new_size, flags, (uptr)new_address); 192 } 193 #endif 194 195 int internal_mprotect(void *addr, uptr length, int prot) { 196 return internal_syscall(SYSCALL(mprotect), (uptr)addr, length, prot); 197 } 198 199 int internal_madvise(uptr addr, uptr length, int advice) { 200 return internal_syscall(SYSCALL(madvise), addr, length, advice); 201 } 202 203 uptr internal_close(fd_t fd) { 204 return internal_syscall(SYSCALL(close), fd); 205 } 206 207 uptr internal_open(const char *filename, int flags) { 208 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 209 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags); 210 #else 211 return internal_syscall(SYSCALL(open), (uptr)filename, flags); 212 #endif 213 } 214 215 uptr internal_open(const char *filename, int flags, u32 mode) { 216 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 217 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags, 218 mode); 219 #else 220 return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode); 221 #endif 222 } 223 224 uptr internal_read(fd_t fd, void *buf, uptr count) { 225 sptr res; 226 HANDLE_EINTR(res, 227 (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf, count)); 228 return res; 229 } 230 231 uptr internal_write(fd_t fd, const void *buf, uptr count) { 232 sptr res; 233 HANDLE_EINTR(res, 234 (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf, count)); 235 return res; 236 } 237 238 uptr internal_ftruncate(fd_t fd, uptr size) { 239 sptr res; 240 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(ftruncate), fd, 241 (OFF_T)size)); 242 return res; 243 } 244 245 #if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && SANITIZER_LINUX 246 static void stat64_to_stat(struct stat64 *in, struct stat *out) { 247 internal_memset(out, 0, sizeof(*out)); 248 out->st_dev = in->st_dev; 249 out->st_ino = in->st_ino; 250 out->st_mode = in->st_mode; 251 out->st_nlink = in->st_nlink; 252 out->st_uid = in->st_uid; 253 out->st_gid = in->st_gid; 254 out->st_rdev = in->st_rdev; 255 out->st_size = in->st_size; 256 out->st_blksize = in->st_blksize; 257 out->st_blocks = in->st_blocks; 258 out->st_atime = in->st_atime; 259 out->st_mtime = in->st_mtime; 260 out->st_ctime = in->st_ctime; 261 } 262 #endif 263 264 #if defined(__mips64) 265 // Undefine compatibility macros from <sys/stat.h> 266 // so that they would not clash with the kernel_stat 267 // st_[a|m|c]time fields 268 #if !SANITIZER_GO 269 #undef st_atime 270 #undef st_mtime 271 #undef st_ctime 272 #endif 273 #if defined(SANITIZER_ANDROID) 274 // Bionic sys/stat.h defines additional macros 275 // for compatibility with the old NDKs and 276 // they clash with the kernel_stat structure 277 // st_[a|m|c]time_nsec fields. 278 #undef st_atime_nsec 279 #undef st_mtime_nsec 280 #undef st_ctime_nsec 281 #endif 282 static void kernel_stat_to_stat(struct kernel_stat *in, struct stat *out) { 283 internal_memset(out, 0, sizeof(*out)); 284 out->st_dev = in->st_dev; 285 out->st_ino = in->st_ino; 286 out->st_mode = in->st_mode; 287 out->st_nlink = in->st_nlink; 288 out->st_uid = in->st_uid; 289 out->st_gid = in->st_gid; 290 out->st_rdev = in->st_rdev; 291 out->st_size = in->st_size; 292 out->st_blksize = in->st_blksize; 293 out->st_blocks = in->st_blocks; 294 #if defined(__USE_MISC) || \ 295 defined(__USE_XOPEN2K8) || \ 296 defined(SANITIZER_ANDROID) 297 out->st_atim.tv_sec = in->st_atime; 298 out->st_atim.tv_nsec = in->st_atime_nsec; 299 out->st_mtim.tv_sec = in->st_mtime; 300 out->st_mtim.tv_nsec = in->st_mtime_nsec; 301 out->st_ctim.tv_sec = in->st_ctime; 302 out->st_ctim.tv_nsec = in->st_ctime_nsec; 303 #else 304 out->st_atime = in->st_atime; 305 out->st_atimensec = in->st_atime_nsec; 306 out->st_mtime = in->st_mtime; 307 out->st_mtimensec = in->st_mtime_nsec; 308 out->st_ctime = in->st_ctime; 309 out->st_atimensec = in->st_ctime_nsec; 310 #endif 311 } 312 #endif 313 314 uptr internal_stat(const char *path, void *buf) { 315 #if SANITIZER_FREEBSD 316 return internal_syscall(SYSCALL(fstatat), AT_FDCWD, (uptr)path, (uptr)buf, 0); 317 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 318 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, (uptr)buf, 319 0); 320 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS 321 # if defined(__mips64) 322 // For mips64, stat syscall fills buffer in the format of kernel_stat 323 struct kernel_stat kbuf; 324 int res = internal_syscall(SYSCALL(stat), path, &kbuf); 325 kernel_stat_to_stat(&kbuf, (struct stat *)buf); 326 return res; 327 # else 328 return internal_syscall(SYSCALL(stat), (uptr)path, (uptr)buf); 329 # endif 330 #else 331 struct stat64 buf64; 332 int res = internal_syscall(SYSCALL(stat64), path, &buf64); 333 stat64_to_stat(&buf64, (struct stat *)buf); 334 return res; 335 #endif 336 } 337 338 uptr internal_lstat(const char *path, void *buf) { 339 #if SANITIZER_FREEBSD 340 return internal_syscall(SYSCALL(fstatat), AT_FDCWD, (uptr)path, (uptr)buf, 341 AT_SYMLINK_NOFOLLOW); 342 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 343 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, (uptr)buf, 344 AT_SYMLINK_NOFOLLOW); 345 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS 346 # if SANITIZER_MIPS64 347 // For mips64, lstat syscall fills buffer in the format of kernel_stat 348 struct kernel_stat kbuf; 349 int res = internal_syscall(SYSCALL(lstat), path, &kbuf); 350 kernel_stat_to_stat(&kbuf, (struct stat *)buf); 351 return res; 352 # else 353 return internal_syscall(SYSCALL(lstat), (uptr)path, (uptr)buf); 354 # endif 355 #else 356 struct stat64 buf64; 357 int res = internal_syscall(SYSCALL(lstat64), path, &buf64); 358 stat64_to_stat(&buf64, (struct stat *)buf); 359 return res; 360 #endif 361 } 362 363 uptr internal_fstat(fd_t fd, void *buf) { 364 #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS 365 #if SANITIZER_MIPS64 366 // For mips64, fstat syscall fills buffer in the format of kernel_stat 367 struct kernel_stat kbuf; 368 int res = internal_syscall(SYSCALL(fstat), fd, &kbuf); 369 kernel_stat_to_stat(&kbuf, (struct stat *)buf); 370 return res; 371 # else 372 return internal_syscall(SYSCALL(fstat), fd, (uptr)buf); 373 # endif 374 #else 375 struct stat64 buf64; 376 int res = internal_syscall(SYSCALL(fstat64), fd, &buf64); 377 stat64_to_stat(&buf64, (struct stat *)buf); 378 return res; 379 #endif 380 } 381 382 uptr internal_filesize(fd_t fd) { 383 struct stat st; 384 if (internal_fstat(fd, &st)) 385 return -1; 386 return (uptr)st.st_size; 387 } 388 389 uptr internal_dup(int oldfd) { 390 return internal_syscall(SYSCALL(dup), oldfd); 391 } 392 393 uptr internal_dup2(int oldfd, int newfd) { 394 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 395 return internal_syscall(SYSCALL(dup3), oldfd, newfd, 0); 396 #else 397 return internal_syscall(SYSCALL(dup2), oldfd, newfd); 398 #endif 399 } 400 401 uptr internal_readlink(const char *path, char *buf, uptr bufsize) { 402 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 403 return internal_syscall(SYSCALL(readlinkat), AT_FDCWD, (uptr)path, (uptr)buf, 404 bufsize); 405 #else 406 return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize); 407 #endif 408 } 409 410 uptr internal_unlink(const char *path) { 411 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 412 return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0); 413 #else 414 return internal_syscall(SYSCALL(unlink), (uptr)path); 415 #endif 416 } 417 418 uptr internal_rename(const char *oldpath, const char *newpath) { 419 #if defined(__riscv) 420 return internal_syscall(SYSCALL(renameat2), AT_FDCWD, (uptr)oldpath, AT_FDCWD, 421 (uptr)newpath, 0); 422 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 423 return internal_syscall(SYSCALL(renameat), AT_FDCWD, (uptr)oldpath, AT_FDCWD, 424 (uptr)newpath); 425 #else 426 return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath); 427 #endif 428 } 429 430 uptr internal_sched_yield() { 431 return internal_syscall(SYSCALL(sched_yield)); 432 } 433 434 void internal_usleep(u64 useconds) { 435 struct timespec ts; 436 ts.tv_sec = useconds / 1000000; 437 ts.tv_nsec = (useconds % 1000000) * 1000; 438 internal_syscall(SYSCALL(nanosleep), &ts, &ts); 439 } 440 441 uptr internal_execve(const char *filename, char *const argv[], 442 char *const envp[]) { 443 return internal_syscall(SYSCALL(execve), (uptr)filename, (uptr)argv, 444 (uptr)envp); 445 } 446 #endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD 447 448 #if !SANITIZER_NETBSD 449 void internal__exit(int exitcode) { 450 #if SANITIZER_FREEBSD || SANITIZER_SOLARIS 451 internal_syscall(SYSCALL(exit), exitcode); 452 #else 453 internal_syscall(SYSCALL(exit_group), exitcode); 454 #endif 455 Die(); // Unreachable. 456 } 457 #endif // !SANITIZER_NETBSD 458 459 // ----------------- sanitizer_common.h 460 bool FileExists(const char *filename) { 461 if (ShouldMockFailureToOpen(filename)) 462 return false; 463 struct stat st; 464 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 465 if (internal_syscall(SYSCALL(newfstatat), AT_FDCWD, filename, &st, 0)) 466 #else 467 if (internal_stat(filename, &st)) 468 #endif 469 return false; 470 // Sanity check: filename is a regular file. 471 return S_ISREG(st.st_mode); 472 } 473 474 #if !SANITIZER_NETBSD 475 tid_t GetTid() { 476 #if SANITIZER_FREEBSD 477 long Tid; 478 thr_self(&Tid); 479 return Tid; 480 #elif SANITIZER_SOLARIS 481 return thr_self(); 482 #else 483 return internal_syscall(SYSCALL(gettid)); 484 #endif 485 } 486 487 int TgKill(pid_t pid, tid_t tid, int sig) { 488 #if SANITIZER_LINUX 489 return internal_syscall(SYSCALL(tgkill), pid, tid, sig); 490 #elif SANITIZER_FREEBSD 491 return internal_syscall(SYSCALL(thr_kill2), pid, tid, sig); 492 #elif SANITIZER_SOLARIS 493 (void)pid; 494 return thr_kill(tid, sig); 495 #endif 496 } 497 #endif 498 499 #if SANITIZER_GLIBC 500 u64 NanoTime() { 501 kernel_timeval tv; 502 internal_memset(&tv, 0, sizeof(tv)); 503 internal_syscall(SYSCALL(gettimeofday), &tv, 0); 504 return (u64)tv.tv_sec * 1000 * 1000 * 1000 + tv.tv_usec * 1000; 505 } 506 // Used by real_clock_gettime. 507 uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) { 508 return internal_syscall(SYSCALL(clock_gettime), clk_id, tp); 509 } 510 #elif !SANITIZER_SOLARIS && !SANITIZER_NETBSD 511 u64 NanoTime() { 512 struct timespec ts; 513 clock_gettime(CLOCK_REALTIME, &ts); 514 return (u64)ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec; 515 } 516 #endif 517 518 // Like getenv, but reads env directly from /proc (on Linux) or parses the 519 // 'environ' array (on some others) and does not use libc. This function 520 // should be called first inside __asan_init. 521 const char *GetEnv(const char *name) { 522 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_SOLARIS 523 if (::environ != 0) { 524 uptr NameLen = internal_strlen(name); 525 for (char **Env = ::environ; *Env != 0; Env++) { 526 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=') 527 return (*Env) + NameLen + 1; 528 } 529 } 530 return 0; // Not found. 531 #elif SANITIZER_LINUX 532 static char *environ; 533 static uptr len; 534 static bool inited; 535 if (!inited) { 536 inited = true; 537 uptr environ_size; 538 if (!ReadFileToBuffer("/proc/self/environ", &environ, &environ_size, &len)) 539 environ = nullptr; 540 } 541 if (!environ || len == 0) return nullptr; 542 uptr namelen = internal_strlen(name); 543 const char *p = environ; 544 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer 545 // proc file has the format NAME=value\0NAME=value\0NAME=value\0... 546 const char* endp = 547 (char*)internal_memchr(p, '\0', len - (p - environ)); 548 if (!endp) // this entry isn't NUL terminated 549 return nullptr; 550 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match. 551 return p + namelen + 1; // point after = 552 p = endp + 1; 553 } 554 return nullptr; // Not found. 555 #else 556 #error "Unsupported platform" 557 #endif 558 } 559 560 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD && !SANITIZER_GO 561 extern "C" { 562 SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end; 563 } 564 #endif 565 566 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 567 static void ReadNullSepFileToArray(const char *path, char ***arr, 568 int arr_size) { 569 char *buff; 570 uptr buff_size; 571 uptr buff_len; 572 *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray"); 573 if (!ReadFileToBuffer(path, &buff, &buff_size, &buff_len, 1024 * 1024)) { 574 (*arr)[0] = nullptr; 575 return; 576 } 577 (*arr)[0] = buff; 578 int count, i; 579 for (count = 1, i = 1; ; i++) { 580 if (buff[i] == 0) { 581 if (buff[i+1] == 0) break; 582 (*arr)[count] = &buff[i+1]; 583 CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible. 584 count++; 585 } 586 } 587 (*arr)[count] = nullptr; 588 } 589 #endif 590 591 static void GetArgsAndEnv(char ***argv, char ***envp) { 592 #if SANITIZER_FREEBSD 593 // On FreeBSD, retrieving the argument and environment arrays is done via the 594 // kern.ps_strings sysctl, which returns a pointer to a structure containing 595 // this information. See also <sys/exec.h>. 596 ps_strings *pss; 597 uptr sz = sizeof(pss); 598 if (internal_sysctlbyname("kern.ps_strings", &pss, &sz, NULL, 0) == -1) { 599 Printf("sysctl kern.ps_strings failed\n"); 600 Die(); 601 } 602 *argv = pss->ps_argvstr; 603 *envp = pss->ps_envstr; 604 #elif SANITIZER_NETBSD 605 *argv = __ps_strings->ps_argvstr; 606 *envp = __ps_strings->ps_envstr; 607 #else // SANITIZER_FREEBSD 608 #if !SANITIZER_GO 609 if (&__libc_stack_end) { 610 uptr* stack_end = (uptr*)__libc_stack_end; 611 // Normally argc can be obtained from *stack_end, however, on ARM glibc's 612 // _start clobbers it: 613 // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/arm/start.S;hb=refs/heads/release/2.31/master#l75 614 // Do not special-case ARM and infer argc from argv everywhere. 615 int argc = 0; 616 while (stack_end[argc + 1]) argc++; 617 *argv = (char**)(stack_end + 1); 618 *envp = (char**)(stack_end + argc + 2); 619 } else { 620 #endif // !SANITIZER_GO 621 static const int kMaxArgv = 2000, kMaxEnvp = 2000; 622 ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv); 623 ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp); 624 #if !SANITIZER_GO 625 } 626 #endif // !SANITIZER_GO 627 #endif // SANITIZER_FREEBSD 628 } 629 630 char **GetArgv() { 631 char **argv, **envp; 632 GetArgsAndEnv(&argv, &envp); 633 return argv; 634 } 635 636 char **GetEnviron() { 637 char **argv, **envp; 638 GetArgsAndEnv(&argv, &envp); 639 return envp; 640 } 641 642 #if !SANITIZER_SOLARIS 643 void FutexWait(atomic_uint32_t *p, u32 cmp) { 644 # if SANITIZER_FREEBSD 645 _umtx_op(p, UMTX_OP_WAIT_UINT, cmp, 0, 0); 646 # elif SANITIZER_NETBSD 647 sched_yield(); /* No userspace futex-like synchronization */ 648 # else 649 internal_syscall(SYSCALL(futex), (uptr)p, FUTEX_WAIT_PRIVATE, cmp, 0, 0, 0); 650 # endif 651 } 652 653 void FutexWake(atomic_uint32_t *p, u32 count) { 654 # if SANITIZER_FREEBSD 655 _umtx_op(p, UMTX_OP_WAKE, count, 0, 0); 656 # elif SANITIZER_NETBSD 657 /* No userspace futex-like synchronization */ 658 # else 659 internal_syscall(SYSCALL(futex), (uptr)p, FUTEX_WAKE_PRIVATE, count, 0, 0, 0); 660 # endif 661 } 662 663 enum { MtxUnlocked = 0, MtxLocked = 1, MtxSleeping = 2 }; 664 665 BlockingMutex::BlockingMutex() { 666 internal_memset(this, 0, sizeof(*this)); 667 } 668 669 void BlockingMutex::Lock() { 670 CHECK_EQ(owner_, 0); 671 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_); 672 if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked) 673 return; 674 while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) { 675 #if SANITIZER_FREEBSD 676 _umtx_op(m, UMTX_OP_WAIT_UINT, MtxSleeping, 0, 0); 677 #elif SANITIZER_NETBSD 678 sched_yield(); /* No userspace futex-like synchronization */ 679 #else 680 internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAIT_PRIVATE, MtxSleeping, 681 0, 0, 0); 682 #endif 683 } 684 } 685 686 void BlockingMutex::Unlock() { 687 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_); 688 u32 v = atomic_exchange(m, MtxUnlocked, memory_order_release); 689 CHECK_NE(v, MtxUnlocked); 690 if (v == MtxSleeping) { 691 #if SANITIZER_FREEBSD 692 _umtx_op(m, UMTX_OP_WAKE, 1, 0, 0); 693 #elif SANITIZER_NETBSD 694 /* No userspace futex-like synchronization */ 695 #else 696 internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAKE_PRIVATE, 1, 0, 0, 0); 697 #endif 698 } 699 } 700 701 void BlockingMutex::CheckLocked() const { 702 auto m = reinterpret_cast<atomic_uint32_t const *>(&opaque_storage_); 703 CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed)); 704 } 705 # endif // !SANITIZER_SOLARIS 706 707 // ----------------- sanitizer_linux.h 708 // The actual size of this structure is specified by d_reclen. 709 // Note that getdents64 uses a different structure format. We only provide the 710 // 32-bit syscall here. 711 #if SANITIZER_NETBSD 712 // Not used 713 #else 714 struct linux_dirent { 715 #if SANITIZER_X32 || defined(__aarch64__) || SANITIZER_RISCV64 716 u64 d_ino; 717 u64 d_off; 718 #else 719 unsigned long d_ino; 720 unsigned long d_off; 721 #endif 722 unsigned short d_reclen; 723 #if defined(__aarch64__) || SANITIZER_RISCV64 724 unsigned char d_type; 725 #endif 726 char d_name[256]; 727 }; 728 #endif 729 730 #if !SANITIZER_SOLARIS && !SANITIZER_NETBSD 731 // Syscall wrappers. 732 uptr internal_ptrace(int request, int pid, void *addr, void *data) { 733 return internal_syscall(SYSCALL(ptrace), request, pid, (uptr)addr, 734 (uptr)data); 735 } 736 737 uptr internal_waitpid(int pid, int *status, int options) { 738 return internal_syscall(SYSCALL(wait4), pid, (uptr)status, options, 739 0 /* rusage */); 740 } 741 742 uptr internal_getpid() { 743 return internal_syscall(SYSCALL(getpid)); 744 } 745 746 uptr internal_getppid() { 747 return internal_syscall(SYSCALL(getppid)); 748 } 749 750 int internal_dlinfo(void *handle, int request, void *p) { 751 #if SANITIZER_FREEBSD 752 return dlinfo(handle, request, p); 753 #else 754 UNIMPLEMENTED(); 755 #endif 756 } 757 758 uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) { 759 #if SANITIZER_FREEBSD 760 return internal_syscall(SYSCALL(getdirentries), fd, (uptr)dirp, count, NULL); 761 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 762 return internal_syscall(SYSCALL(getdents64), fd, (uptr)dirp, count); 763 #else 764 return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count); 765 #endif 766 } 767 768 uptr internal_lseek(fd_t fd, OFF_T offset, int whence) { 769 return internal_syscall(SYSCALL(lseek), fd, offset, whence); 770 } 771 772 #if SANITIZER_LINUX 773 uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) { 774 return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5); 775 } 776 #endif 777 778 uptr internal_sigaltstack(const void *ss, void *oss) { 779 return internal_syscall(SYSCALL(sigaltstack), (uptr)ss, (uptr)oss); 780 } 781 782 int internal_fork() { 783 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 784 return internal_syscall(SYSCALL(clone), SIGCHLD, 0); 785 #else 786 return internal_syscall(SYSCALL(fork)); 787 #endif 788 } 789 790 #if SANITIZER_FREEBSD 791 int internal_sysctl(const int *name, unsigned int namelen, void *oldp, 792 uptr *oldlenp, const void *newp, uptr newlen) { 793 return internal_syscall(SYSCALL(__sysctl), name, namelen, oldp, 794 (size_t *)oldlenp, newp, (size_t)newlen); 795 } 796 797 int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp, 798 const void *newp, uptr newlen) { 799 // Note: this function can be called during startup, so we need to avoid 800 // calling any interceptable functions. On FreeBSD >= 1300045 sysctlbyname() 801 // is a real syscall, but for older versions it calls sysctlnametomib() 802 // followed by sysctl(). To avoid calling the intercepted version and 803 // asserting if this happens during startup, call the real sysctlnametomib() 804 // followed by internal_sysctl() if the syscall is not available. 805 #ifdef SYS___sysctlbyname 806 return internal_syscall(SYSCALL(__sysctlbyname), sname, 807 internal_strlen(sname), oldp, (size_t *)oldlenp, newp, 808 (size_t)newlen); 809 #else 810 static decltype(sysctlnametomib) *real_sysctlnametomib = nullptr; 811 if (!real_sysctlnametomib) 812 real_sysctlnametomib = 813 (decltype(sysctlnametomib) *)dlsym(RTLD_NEXT, "sysctlnametomib"); 814 CHECK(real_sysctlnametomib); 815 816 int oid[CTL_MAXNAME]; 817 size_t len = CTL_MAXNAME; 818 if (real_sysctlnametomib(sname, oid, &len) == -1) 819 return (-1); 820 return internal_sysctl(oid, len, oldp, oldlenp, newp, newlen); 821 #endif 822 } 823 #endif 824 825 #if SANITIZER_LINUX 826 #define SA_RESTORER 0x04000000 827 // Doesn't set sa_restorer if the caller did not set it, so use with caution 828 //(see below). 829 int internal_sigaction_norestorer(int signum, const void *act, void *oldact) { 830 __sanitizer_kernel_sigaction_t k_act, k_oldact; 831 internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t)); 832 internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t)); 833 const __sanitizer_sigaction *u_act = (const __sanitizer_sigaction *)act; 834 __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact; 835 if (u_act) { 836 k_act.handler = u_act->handler; 837 k_act.sigaction = u_act->sigaction; 838 internal_memcpy(&k_act.sa_mask, &u_act->sa_mask, 839 sizeof(__sanitizer_kernel_sigset_t)); 840 // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL). 841 k_act.sa_flags = u_act->sa_flags | SA_RESTORER; 842 // FIXME: most often sa_restorer is unset, however the kernel requires it 843 // to point to a valid signal restorer that calls the rt_sigreturn syscall. 844 // If sa_restorer passed to the kernel is NULL, the program may crash upon 845 // signal delivery or fail to unwind the stack in the signal handler. 846 // libc implementation of sigaction() passes its own restorer to 847 // rt_sigaction, so we need to do the same (we'll need to reimplement the 848 // restorers; for x86_64 the restorer address can be obtained from 849 // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact). 850 #if !SANITIZER_ANDROID || !SANITIZER_MIPS32 851 k_act.sa_restorer = u_act->sa_restorer; 852 #endif 853 } 854 855 uptr result = internal_syscall(SYSCALL(rt_sigaction), (uptr)signum, 856 (uptr)(u_act ? &k_act : nullptr), 857 (uptr)(u_oldact ? &k_oldact : nullptr), 858 (uptr)sizeof(__sanitizer_kernel_sigset_t)); 859 860 if ((result == 0) && u_oldact) { 861 u_oldact->handler = k_oldact.handler; 862 u_oldact->sigaction = k_oldact.sigaction; 863 internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask, 864 sizeof(__sanitizer_kernel_sigset_t)); 865 u_oldact->sa_flags = k_oldact.sa_flags; 866 #if !SANITIZER_ANDROID || !SANITIZER_MIPS32 867 u_oldact->sa_restorer = k_oldact.sa_restorer; 868 #endif 869 } 870 return result; 871 } 872 #endif // SANITIZER_LINUX 873 874 uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set, 875 __sanitizer_sigset_t *oldset) { 876 #if SANITIZER_FREEBSD 877 return internal_syscall(SYSCALL(sigprocmask), how, set, oldset); 878 #else 879 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set; 880 __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset; 881 return internal_syscall(SYSCALL(rt_sigprocmask), (uptr)how, (uptr)k_set, 882 (uptr)k_oldset, sizeof(__sanitizer_kernel_sigset_t)); 883 #endif 884 } 885 886 void internal_sigfillset(__sanitizer_sigset_t *set) { 887 internal_memset(set, 0xff, sizeof(*set)); 888 } 889 890 void internal_sigemptyset(__sanitizer_sigset_t *set) { 891 internal_memset(set, 0, sizeof(*set)); 892 } 893 894 #if SANITIZER_LINUX 895 void internal_sigdelset(__sanitizer_sigset_t *set, int signum) { 896 signum -= 1; 897 CHECK_GE(signum, 0); 898 CHECK_LT(signum, sizeof(*set) * 8); 899 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set; 900 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8); 901 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8); 902 k_set->sig[idx] &= ~((uptr)1 << bit); 903 } 904 905 bool internal_sigismember(__sanitizer_sigset_t *set, int signum) { 906 signum -= 1; 907 CHECK_GE(signum, 0); 908 CHECK_LT(signum, sizeof(*set) * 8); 909 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set; 910 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8); 911 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8); 912 return k_set->sig[idx] & ((uptr)1 << bit); 913 } 914 #elif SANITIZER_FREEBSD 915 void internal_sigdelset(__sanitizer_sigset_t *set, int signum) { 916 sigset_t *rset = reinterpret_cast<sigset_t *>(set); 917 sigdelset(rset, signum); 918 } 919 920 bool internal_sigismember(__sanitizer_sigset_t *set, int signum) { 921 sigset_t *rset = reinterpret_cast<sigset_t *>(set); 922 return sigismember(rset, signum); 923 } 924 #endif 925 #endif // !SANITIZER_SOLARIS 926 927 #if !SANITIZER_NETBSD 928 // ThreadLister implementation. 929 ThreadLister::ThreadLister(pid_t pid) : pid_(pid), buffer_(4096) { 930 char task_directory_path[80]; 931 internal_snprintf(task_directory_path, sizeof(task_directory_path), 932 "/proc/%d/task/", pid); 933 descriptor_ = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY); 934 if (internal_iserror(descriptor_)) { 935 Report("Can't open /proc/%d/task for reading.\n", pid); 936 } 937 } 938 939 ThreadLister::Result ThreadLister::ListThreads( 940 InternalMmapVector<tid_t> *threads) { 941 if (internal_iserror(descriptor_)) 942 return Error; 943 internal_lseek(descriptor_, 0, SEEK_SET); 944 threads->clear(); 945 946 Result result = Ok; 947 for (bool first_read = true;; first_read = false) { 948 // Resize to max capacity if it was downsized by IsAlive. 949 buffer_.resize(buffer_.capacity()); 950 CHECK_GE(buffer_.size(), 4096); 951 uptr read = internal_getdents( 952 descriptor_, (struct linux_dirent *)buffer_.data(), buffer_.size()); 953 if (!read) 954 return result; 955 if (internal_iserror(read)) { 956 Report("Can't read directory entries from /proc/%d/task.\n", pid_); 957 return Error; 958 } 959 960 for (uptr begin = (uptr)buffer_.data(), end = begin + read; begin < end;) { 961 struct linux_dirent *entry = (struct linux_dirent *)begin; 962 begin += entry->d_reclen; 963 if (entry->d_ino == 1) { 964 // Inode 1 is for bad blocks and also can be a reason for early return. 965 // Should be emitted if kernel tried to output terminating thread. 966 // See proc_task_readdir implementation in Linux. 967 result = Incomplete; 968 } 969 if (entry->d_ino && *entry->d_name >= '0' && *entry->d_name <= '9') 970 threads->push_back(internal_atoll(entry->d_name)); 971 } 972 973 // Now we are going to detect short-read or early EOF. In such cases Linux 974 // can return inconsistent list with missing alive threads. 975 // Code will just remember that the list can be incomplete but it will 976 // continue reads to return as much as possible. 977 if (!first_read) { 978 // The first one was a short-read by definition. 979 result = Incomplete; 980 } else if (read > buffer_.size() - 1024) { 981 // Read was close to the buffer size. So double the size and assume the 982 // worst. 983 buffer_.resize(buffer_.size() * 2); 984 result = Incomplete; 985 } else if (!threads->empty() && !IsAlive(threads->back())) { 986 // Maybe Linux early returned from read on terminated thread (!pid_alive) 987 // and failed to restore read position. 988 // See next_tid and proc_task_instantiate in Linux. 989 result = Incomplete; 990 } 991 } 992 } 993 994 bool ThreadLister::IsAlive(int tid) { 995 // /proc/%d/task/%d/status uses same call to detect alive threads as 996 // proc_task_readdir. See task_state implementation in Linux. 997 char path[80]; 998 internal_snprintf(path, sizeof(path), "/proc/%d/task/%d/status", pid_, tid); 999 if (!ReadFileToVector(path, &buffer_) || buffer_.empty()) 1000 return false; 1001 buffer_.push_back(0); 1002 static const char kPrefix[] = "\nPPid:"; 1003 const char *field = internal_strstr(buffer_.data(), kPrefix); 1004 if (!field) 1005 return false; 1006 field += internal_strlen(kPrefix); 1007 return (int)internal_atoll(field) != 0; 1008 } 1009 1010 ThreadLister::~ThreadLister() { 1011 if (!internal_iserror(descriptor_)) 1012 internal_close(descriptor_); 1013 } 1014 #endif 1015 1016 #if SANITIZER_WORDSIZE == 32 1017 // Take care of unusable kernel area in top gigabyte. 1018 static uptr GetKernelAreaSize() { 1019 #if SANITIZER_LINUX && !SANITIZER_X32 1020 const uptr gbyte = 1UL << 30; 1021 1022 // Firstly check if there are writable segments 1023 // mapped to top gigabyte (e.g. stack). 1024 MemoryMappingLayout proc_maps(/*cache_enabled*/true); 1025 if (proc_maps.Error()) 1026 return 0; 1027 MemoryMappedSegment segment; 1028 while (proc_maps.Next(&segment)) { 1029 if ((segment.end >= 3 * gbyte) && segment.IsWritable()) return 0; 1030 } 1031 1032 #if !SANITIZER_ANDROID 1033 // Even if nothing is mapped, top Gb may still be accessible 1034 // if we are running on 64-bit kernel. 1035 // Uname may report misleading results if personality type 1036 // is modified (e.g. under schroot) so check this as well. 1037 struct utsname uname_info; 1038 int pers = personality(0xffffffffUL); 1039 if (!(pers & PER_MASK) && internal_uname(&uname_info) == 0 && 1040 internal_strstr(uname_info.machine, "64")) 1041 return 0; 1042 #endif // SANITIZER_ANDROID 1043 1044 // Top gigabyte is reserved for kernel. 1045 return gbyte; 1046 #else 1047 return 0; 1048 #endif // SANITIZER_LINUX && !SANITIZER_X32 1049 } 1050 #endif // SANITIZER_WORDSIZE == 32 1051 1052 uptr GetMaxVirtualAddress() { 1053 #if SANITIZER_NETBSD && defined(__x86_64__) 1054 return 0x7f7ffffff000ULL; // (0x00007f8000000000 - PAGE_SIZE) 1055 #elif SANITIZER_WORDSIZE == 64 1056 # if defined(__powerpc64__) || defined(__aarch64__) 1057 // On PowerPC64 we have two different address space layouts: 44- and 46-bit. 1058 // We somehow need to figure out which one we are using now and choose 1059 // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL. 1060 // Note that with 'ulimit -s unlimited' the stack is moved away from the top 1061 // of the address space, so simply checking the stack address is not enough. 1062 // This should (does) work for both PowerPC64 Endian modes. 1063 // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit. 1064 return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1; 1065 #elif SANITIZER_RISCV64 1066 return (1ULL << 38) - 1; 1067 # elif defined(__mips64) 1068 return (1ULL << 40) - 1; // 0x000000ffffffffffUL; 1069 # elif defined(__s390x__) 1070 return (1ULL << 53) - 1; // 0x001fffffffffffffUL; 1071 #elif defined(__sparc__) 1072 return ~(uptr)0; 1073 # else 1074 return (1ULL << 47) - 1; // 0x00007fffffffffffUL; 1075 # endif 1076 #else // SANITIZER_WORDSIZE == 32 1077 # if defined(__s390__) 1078 return (1ULL << 31) - 1; // 0x7fffffff; 1079 # else 1080 return (1ULL << 32) - 1; // 0xffffffff; 1081 # endif 1082 #endif // SANITIZER_WORDSIZE 1083 } 1084 1085 uptr GetMaxUserVirtualAddress() { 1086 uptr addr = GetMaxVirtualAddress(); 1087 #if SANITIZER_WORDSIZE == 32 && !defined(__s390__) 1088 if (!common_flags()->full_address_space) 1089 addr -= GetKernelAreaSize(); 1090 CHECK_LT(reinterpret_cast<uptr>(&addr), addr); 1091 #endif 1092 return addr; 1093 } 1094 1095 #if !SANITIZER_ANDROID 1096 uptr GetPageSize() { 1097 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__)) && \ 1098 defined(EXEC_PAGESIZE) 1099 return EXEC_PAGESIZE; 1100 #elif SANITIZER_FREEBSD || SANITIZER_NETBSD 1101 // Use sysctl as sysconf can trigger interceptors internally. 1102 int pz = 0; 1103 uptr pzl = sizeof(pz); 1104 int mib[2] = {CTL_HW, HW_PAGESIZE}; 1105 int rv = internal_sysctl(mib, 2, &pz, &pzl, nullptr, 0); 1106 CHECK_EQ(rv, 0); 1107 return (uptr)pz; 1108 #elif SANITIZER_USE_GETAUXVAL 1109 return getauxval(AT_PAGESZ); 1110 #else 1111 return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy. 1112 #endif 1113 } 1114 #endif // !SANITIZER_ANDROID 1115 1116 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) { 1117 #if SANITIZER_SOLARIS 1118 const char *default_module_name = getexecname(); 1119 CHECK_NE(default_module_name, NULL); 1120 return internal_snprintf(buf, buf_len, "%s", default_module_name); 1121 #else 1122 #if SANITIZER_FREEBSD || SANITIZER_NETBSD 1123 #if SANITIZER_FREEBSD 1124 const int Mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; 1125 #else 1126 const int Mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME}; 1127 #endif 1128 const char *default_module_name = "kern.proc.pathname"; 1129 uptr Size = buf_len; 1130 bool IsErr = 1131 (internal_sysctl(Mib, ARRAY_SIZE(Mib), buf, &Size, NULL, 0) != 0); 1132 int readlink_error = IsErr ? errno : 0; 1133 uptr module_name_len = Size; 1134 #else 1135 const char *default_module_name = "/proc/self/exe"; 1136 uptr module_name_len = internal_readlink( 1137 default_module_name, buf, buf_len); 1138 int readlink_error; 1139 bool IsErr = internal_iserror(module_name_len, &readlink_error); 1140 #endif // SANITIZER_SOLARIS 1141 if (IsErr) { 1142 // We can't read binary name for some reason, assume it's unknown. 1143 Report("WARNING: reading executable name failed with errno %d, " 1144 "some stack frames may not be symbolized\n", readlink_error); 1145 module_name_len = internal_snprintf(buf, buf_len, "%s", 1146 default_module_name); 1147 CHECK_LT(module_name_len, buf_len); 1148 } 1149 return module_name_len; 1150 #endif 1151 } 1152 1153 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) { 1154 #if SANITIZER_LINUX 1155 char *tmpbuf; 1156 uptr tmpsize; 1157 uptr tmplen; 1158 if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf, &tmpsize, &tmplen, 1159 1024 * 1024)) { 1160 internal_strncpy(buf, tmpbuf, buf_len); 1161 UnmapOrDie(tmpbuf, tmpsize); 1162 return internal_strlen(buf); 1163 } 1164 #endif 1165 return ReadBinaryName(buf, buf_len); 1166 } 1167 1168 // Match full names of the form /path/to/base_name{-,.}* 1169 bool LibraryNameIs(const char *full_name, const char *base_name) { 1170 const char *name = full_name; 1171 // Strip path. 1172 while (*name != '\0') name++; 1173 while (name > full_name && *name != '/') name--; 1174 if (*name == '/') name++; 1175 uptr base_name_length = internal_strlen(base_name); 1176 if (internal_strncmp(name, base_name, base_name_length)) return false; 1177 return (name[base_name_length] == '-' || name[base_name_length] == '.'); 1178 } 1179 1180 #if !SANITIZER_ANDROID 1181 // Call cb for each region mapped by map. 1182 void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) { 1183 CHECK_NE(map, nullptr); 1184 #if !SANITIZER_FREEBSD 1185 typedef ElfW(Phdr) Elf_Phdr; 1186 typedef ElfW(Ehdr) Elf_Ehdr; 1187 #endif // !SANITIZER_FREEBSD 1188 char *base = (char *)map->l_addr; 1189 Elf_Ehdr *ehdr = (Elf_Ehdr *)base; 1190 char *phdrs = base + ehdr->e_phoff; 1191 char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize; 1192 1193 // Find the segment with the minimum base so we can "relocate" the p_vaddr 1194 // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC 1195 // objects have a non-zero base. 1196 uptr preferred_base = (uptr)-1; 1197 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) { 1198 Elf_Phdr *phdr = (Elf_Phdr *)iter; 1199 if (phdr->p_type == PT_LOAD && preferred_base > (uptr)phdr->p_vaddr) 1200 preferred_base = (uptr)phdr->p_vaddr; 1201 } 1202 1203 // Compute the delta from the real base to get a relocation delta. 1204 sptr delta = (uptr)base - preferred_base; 1205 // Now we can figure out what the loader really mapped. 1206 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) { 1207 Elf_Phdr *phdr = (Elf_Phdr *)iter; 1208 if (phdr->p_type == PT_LOAD) { 1209 uptr seg_start = phdr->p_vaddr + delta; 1210 uptr seg_end = seg_start + phdr->p_memsz; 1211 // None of these values are aligned. We consider the ragged edges of the 1212 // load command as defined, since they are mapped from the file. 1213 seg_start = RoundDownTo(seg_start, GetPageSizeCached()); 1214 seg_end = RoundUpTo(seg_end, GetPageSizeCached()); 1215 cb((void *)seg_start, seg_end - seg_start); 1216 } 1217 } 1218 } 1219 #endif 1220 1221 #if defined(__x86_64__) && SANITIZER_LINUX 1222 // We cannot use glibc's clone wrapper, because it messes with the child 1223 // task's TLS. It writes the PID and TID of the child task to its thread 1224 // descriptor, but in our case the child task shares the thread descriptor with 1225 // the parent (because we don't know how to allocate a new thread 1226 // descriptor to keep glibc happy). So the stock version of clone(), when 1227 // used with CLONE_VM, would end up corrupting the parent's thread descriptor. 1228 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1229 int *parent_tidptr, void *newtls, int *child_tidptr) { 1230 long long res; 1231 if (!fn || !child_stack) 1232 return -EINVAL; 1233 CHECK_EQ(0, (uptr)child_stack % 16); 1234 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long); 1235 ((unsigned long long *)child_stack)[0] = (uptr)fn; 1236 ((unsigned long long *)child_stack)[1] = (uptr)arg; 1237 register void *r8 __asm__("r8") = newtls; 1238 register int *r10 __asm__("r10") = child_tidptr; 1239 __asm__ __volatile__( 1240 /* %rax = syscall(%rax = SYSCALL(clone), 1241 * %rdi = flags, 1242 * %rsi = child_stack, 1243 * %rdx = parent_tidptr, 1244 * %r8 = new_tls, 1245 * %r10 = child_tidptr) 1246 */ 1247 "syscall\n" 1248 1249 /* if (%rax != 0) 1250 * return; 1251 */ 1252 "testq %%rax,%%rax\n" 1253 "jnz 1f\n" 1254 1255 /* In the child. Terminate unwind chain. */ 1256 // XXX: We should also terminate the CFI unwind chain 1257 // here. Unfortunately clang 3.2 doesn't support the 1258 // necessary CFI directives, so we skip that part. 1259 "xorq %%rbp,%%rbp\n" 1260 1261 /* Call "fn(arg)". */ 1262 "popq %%rax\n" 1263 "popq %%rdi\n" 1264 "call *%%rax\n" 1265 1266 /* Call _exit(%rax). */ 1267 "movq %%rax,%%rdi\n" 1268 "movq %2,%%rax\n" 1269 "syscall\n" 1270 1271 /* Return to parent. */ 1272 "1:\n" 1273 : "=a" (res) 1274 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), 1275 "S"(child_stack), 1276 "D"(flags), 1277 "d"(parent_tidptr), 1278 "r"(r8), 1279 "r"(r10) 1280 : "memory", "r11", "rcx"); 1281 return res; 1282 } 1283 #elif defined(__mips__) 1284 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1285 int *parent_tidptr, void *newtls, int *child_tidptr) { 1286 long long res; 1287 if (!fn || !child_stack) 1288 return -EINVAL; 1289 CHECK_EQ(0, (uptr)child_stack % 16); 1290 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long); 1291 ((unsigned long long *)child_stack)[0] = (uptr)fn; 1292 ((unsigned long long *)child_stack)[1] = (uptr)arg; 1293 register void *a3 __asm__("$7") = newtls; 1294 register int *a4 __asm__("$8") = child_tidptr; 1295 // We don't have proper CFI directives here because it requires alot of code 1296 // for very marginal benefits. 1297 __asm__ __volatile__( 1298 /* $v0 = syscall($v0 = __NR_clone, 1299 * $a0 = flags, 1300 * $a1 = child_stack, 1301 * $a2 = parent_tidptr, 1302 * $a3 = new_tls, 1303 * $a4 = child_tidptr) 1304 */ 1305 ".cprestore 16;\n" 1306 "move $4,%1;\n" 1307 "move $5,%2;\n" 1308 "move $6,%3;\n" 1309 "move $7,%4;\n" 1310 /* Store the fifth argument on stack 1311 * if we are using 32-bit abi. 1312 */ 1313 #if SANITIZER_WORDSIZE == 32 1314 "lw %5,16($29);\n" 1315 #else 1316 "move $8,%5;\n" 1317 #endif 1318 "li $2,%6;\n" 1319 "syscall;\n" 1320 1321 /* if ($v0 != 0) 1322 * return; 1323 */ 1324 "bnez $2,1f;\n" 1325 1326 /* Call "fn(arg)". */ 1327 #if SANITIZER_WORDSIZE == 32 1328 #ifdef __BIG_ENDIAN__ 1329 "lw $25,4($29);\n" 1330 "lw $4,12($29);\n" 1331 #else 1332 "lw $25,0($29);\n" 1333 "lw $4,8($29);\n" 1334 #endif 1335 #else 1336 "ld $25,0($29);\n" 1337 "ld $4,8($29);\n" 1338 #endif 1339 "jal $25;\n" 1340 1341 /* Call _exit($v0). */ 1342 "move $4,$2;\n" 1343 "li $2,%7;\n" 1344 "syscall;\n" 1345 1346 /* Return to parent. */ 1347 "1:\n" 1348 : "=r" (res) 1349 : "r"(flags), 1350 "r"(child_stack), 1351 "r"(parent_tidptr), 1352 "r"(a3), 1353 "r"(a4), 1354 "i"(__NR_clone), 1355 "i"(__NR_exit) 1356 : "memory", "$29" ); 1357 return res; 1358 } 1359 #elif SANITIZER_RISCV64 1360 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1361 int *parent_tidptr, void *newtls, int *child_tidptr) { 1362 if (!fn || !child_stack) 1363 return -EINVAL; 1364 1365 CHECK_EQ(0, (uptr)child_stack % 16); 1366 1367 register int res __asm__("a0"); 1368 register int __flags __asm__("a0") = flags; 1369 register void *__stack __asm__("a1") = child_stack; 1370 register int *__ptid __asm__("a2") = parent_tidptr; 1371 register void *__tls __asm__("a3") = newtls; 1372 register int *__ctid __asm__("a4") = child_tidptr; 1373 register int (*__fn)(void *) __asm__("a5") = fn; 1374 register void *__arg __asm__("a6") = arg; 1375 register int nr_clone __asm__("a7") = __NR_clone; 1376 1377 __asm__ __volatile__( 1378 "ecall\n" 1379 1380 /* if (a0 != 0) 1381 * return a0; 1382 */ 1383 "bnez a0, 1f\n" 1384 1385 // In the child, now. Call "fn(arg)". 1386 "mv a0, a6\n" 1387 "jalr a5\n" 1388 1389 // Call _exit(a0). 1390 "addi a7, zero, %9\n" 1391 "ecall\n" 1392 "1:\n" 1393 1394 : "=r"(res) 1395 : "0"(__flags), "r"(__stack), "r"(__ptid), "r"(__tls), "r"(__ctid), 1396 "r"(__fn), "r"(__arg), "r"(nr_clone), "i"(__NR_exit) 1397 : "memory"); 1398 return res; 1399 } 1400 #elif defined(__aarch64__) 1401 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1402 int *parent_tidptr, void *newtls, int *child_tidptr) { 1403 long long res; 1404 if (!fn || !child_stack) 1405 return -EINVAL; 1406 CHECK_EQ(0, (uptr)child_stack % 16); 1407 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long); 1408 ((unsigned long long *)child_stack)[0] = (uptr)fn; 1409 ((unsigned long long *)child_stack)[1] = (uptr)arg; 1410 1411 register int (*__fn)(void *) __asm__("x0") = fn; 1412 register void *__stack __asm__("x1") = child_stack; 1413 register int __flags __asm__("x2") = flags; 1414 register void *__arg __asm__("x3") = arg; 1415 register int *__ptid __asm__("x4") = parent_tidptr; 1416 register void *__tls __asm__("x5") = newtls; 1417 register int *__ctid __asm__("x6") = child_tidptr; 1418 1419 __asm__ __volatile__( 1420 "mov x0,x2\n" /* flags */ 1421 "mov x2,x4\n" /* ptid */ 1422 "mov x3,x5\n" /* tls */ 1423 "mov x4,x6\n" /* ctid */ 1424 "mov x8,%9\n" /* clone */ 1425 1426 "svc 0x0\n" 1427 1428 /* if (%r0 != 0) 1429 * return %r0; 1430 */ 1431 "cmp x0, #0\n" 1432 "bne 1f\n" 1433 1434 /* In the child, now. Call "fn(arg)". */ 1435 "ldp x1, x0, [sp], #16\n" 1436 "blr x1\n" 1437 1438 /* Call _exit(%r0). */ 1439 "mov x8, %10\n" 1440 "svc 0x0\n" 1441 "1:\n" 1442 1443 : "=r" (res) 1444 : "i"(-EINVAL), 1445 "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg), 1446 "r"(__ptid), "r"(__tls), "r"(__ctid), 1447 "i"(__NR_clone), "i"(__NR_exit) 1448 : "x30", "memory"); 1449 return res; 1450 } 1451 #elif defined(__powerpc64__) 1452 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1453 int *parent_tidptr, void *newtls, int *child_tidptr) { 1454 long long res; 1455 // Stack frame structure. 1456 #if SANITIZER_PPC64V1 1457 // Back chain == 0 (SP + 112) 1458 // Frame (112 bytes): 1459 // Parameter save area (SP + 48), 8 doublewords 1460 // TOC save area (SP + 40) 1461 // Link editor doubleword (SP + 32) 1462 // Compiler doubleword (SP + 24) 1463 // LR save area (SP + 16) 1464 // CR save area (SP + 8) 1465 // Back chain (SP + 0) 1466 # define FRAME_SIZE 112 1467 # define FRAME_TOC_SAVE_OFFSET 40 1468 #elif SANITIZER_PPC64V2 1469 // Back chain == 0 (SP + 32) 1470 // Frame (32 bytes): 1471 // TOC save area (SP + 24) 1472 // LR save area (SP + 16) 1473 // CR save area (SP + 8) 1474 // Back chain (SP + 0) 1475 # define FRAME_SIZE 32 1476 # define FRAME_TOC_SAVE_OFFSET 24 1477 #else 1478 # error "Unsupported PPC64 ABI" 1479 #endif 1480 if (!fn || !child_stack) 1481 return -EINVAL; 1482 CHECK_EQ(0, (uptr)child_stack % 16); 1483 1484 register int (*__fn)(void *) __asm__("r3") = fn; 1485 register void *__cstack __asm__("r4") = child_stack; 1486 register int __flags __asm__("r5") = flags; 1487 register void *__arg __asm__("r6") = arg; 1488 register int *__ptidptr __asm__("r7") = parent_tidptr; 1489 register void *__newtls __asm__("r8") = newtls; 1490 register int *__ctidptr __asm__("r9") = child_tidptr; 1491 1492 __asm__ __volatile__( 1493 /* fn and arg are saved across the syscall */ 1494 "mr 28, %5\n\t" 1495 "mr 27, %8\n\t" 1496 1497 /* syscall 1498 r0 == __NR_clone 1499 r3 == flags 1500 r4 == child_stack 1501 r5 == parent_tidptr 1502 r6 == newtls 1503 r7 == child_tidptr */ 1504 "mr 3, %7\n\t" 1505 "mr 5, %9\n\t" 1506 "mr 6, %10\n\t" 1507 "mr 7, %11\n\t" 1508 "li 0, %3\n\t" 1509 "sc\n\t" 1510 1511 /* Test if syscall was successful */ 1512 "cmpdi cr1, 3, 0\n\t" 1513 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t" 1514 "bne- cr1, 1f\n\t" 1515 1516 /* Set up stack frame */ 1517 "li 29, 0\n\t" 1518 "stdu 29, -8(1)\n\t" 1519 "stdu 1, -%12(1)\n\t" 1520 /* Do the function call */ 1521 "std 2, %13(1)\n\t" 1522 #if SANITIZER_PPC64V1 1523 "ld 0, 0(28)\n\t" 1524 "ld 2, 8(28)\n\t" 1525 "mtctr 0\n\t" 1526 #elif SANITIZER_PPC64V2 1527 "mr 12, 28\n\t" 1528 "mtctr 12\n\t" 1529 #else 1530 # error "Unsupported PPC64 ABI" 1531 #endif 1532 "mr 3, 27\n\t" 1533 "bctrl\n\t" 1534 "ld 2, %13(1)\n\t" 1535 1536 /* Call _exit(r3) */ 1537 "li 0, %4\n\t" 1538 "sc\n\t" 1539 1540 /* Return to parent */ 1541 "1:\n\t" 1542 "mr %0, 3\n\t" 1543 : "=r" (res) 1544 : "0" (-1), 1545 "i" (EINVAL), 1546 "i" (__NR_clone), 1547 "i" (__NR_exit), 1548 "r" (__fn), 1549 "r" (__cstack), 1550 "r" (__flags), 1551 "r" (__arg), 1552 "r" (__ptidptr), 1553 "r" (__newtls), 1554 "r" (__ctidptr), 1555 "i" (FRAME_SIZE), 1556 "i" (FRAME_TOC_SAVE_OFFSET) 1557 : "cr0", "cr1", "memory", "ctr", "r0", "r27", "r28", "r29"); 1558 return res; 1559 } 1560 #elif defined(__i386__) && SANITIZER_LINUX 1561 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1562 int *parent_tidptr, void *newtls, int *child_tidptr) { 1563 int res; 1564 if (!fn || !child_stack) 1565 return -EINVAL; 1566 CHECK_EQ(0, (uptr)child_stack % 16); 1567 child_stack = (char *)child_stack - 7 * sizeof(unsigned int); 1568 ((unsigned int *)child_stack)[0] = (uptr)flags; 1569 ((unsigned int *)child_stack)[1] = (uptr)0; 1570 ((unsigned int *)child_stack)[2] = (uptr)fn; 1571 ((unsigned int *)child_stack)[3] = (uptr)arg; 1572 __asm__ __volatile__( 1573 /* %eax = syscall(%eax = SYSCALL(clone), 1574 * %ebx = flags, 1575 * %ecx = child_stack, 1576 * %edx = parent_tidptr, 1577 * %esi = new_tls, 1578 * %edi = child_tidptr) 1579 */ 1580 1581 /* Obtain flags */ 1582 "movl (%%ecx), %%ebx\n" 1583 /* Do the system call */ 1584 "pushl %%ebx\n" 1585 "pushl %%esi\n" 1586 "pushl %%edi\n" 1587 /* Remember the flag value. */ 1588 "movl %%ebx, (%%ecx)\n" 1589 "int $0x80\n" 1590 "popl %%edi\n" 1591 "popl %%esi\n" 1592 "popl %%ebx\n" 1593 1594 /* if (%eax != 0) 1595 * return; 1596 */ 1597 1598 "test %%eax,%%eax\n" 1599 "jnz 1f\n" 1600 1601 /* terminate the stack frame */ 1602 "xorl %%ebp,%%ebp\n" 1603 /* Call FN. */ 1604 "call *%%ebx\n" 1605 #ifdef PIC 1606 "call here\n" 1607 "here:\n" 1608 "popl %%ebx\n" 1609 "addl $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n" 1610 #endif 1611 /* Call exit */ 1612 "movl %%eax, %%ebx\n" 1613 "movl %2, %%eax\n" 1614 "int $0x80\n" 1615 "1:\n" 1616 : "=a" (res) 1617 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), 1618 "c"(child_stack), 1619 "d"(parent_tidptr), 1620 "S"(newtls), 1621 "D"(child_tidptr) 1622 : "memory"); 1623 return res; 1624 } 1625 #elif defined(__arm__) && SANITIZER_LINUX 1626 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1627 int *parent_tidptr, void *newtls, int *child_tidptr) { 1628 unsigned int res; 1629 if (!fn || !child_stack) 1630 return -EINVAL; 1631 child_stack = (char *)child_stack - 2 * sizeof(unsigned int); 1632 ((unsigned int *)child_stack)[0] = (uptr)fn; 1633 ((unsigned int *)child_stack)[1] = (uptr)arg; 1634 register int r0 __asm__("r0") = flags; 1635 register void *r1 __asm__("r1") = child_stack; 1636 register int *r2 __asm__("r2") = parent_tidptr; 1637 register void *r3 __asm__("r3") = newtls; 1638 register int *r4 __asm__("r4") = child_tidptr; 1639 register int r7 __asm__("r7") = __NR_clone; 1640 1641 #if __ARM_ARCH > 4 || defined (__ARM_ARCH_4T__) 1642 # define ARCH_HAS_BX 1643 #endif 1644 #if __ARM_ARCH > 4 1645 # define ARCH_HAS_BLX 1646 #endif 1647 1648 #ifdef ARCH_HAS_BX 1649 # ifdef ARCH_HAS_BLX 1650 # define BLX(R) "blx " #R "\n" 1651 # else 1652 # define BLX(R) "mov lr, pc; bx " #R "\n" 1653 # endif 1654 #else 1655 # define BLX(R) "mov lr, pc; mov pc," #R "\n" 1656 #endif 1657 1658 __asm__ __volatile__( 1659 /* %r0 = syscall(%r7 = SYSCALL(clone), 1660 * %r0 = flags, 1661 * %r1 = child_stack, 1662 * %r2 = parent_tidptr, 1663 * %r3 = new_tls, 1664 * %r4 = child_tidptr) 1665 */ 1666 1667 /* Do the system call */ 1668 "swi 0x0\n" 1669 1670 /* if (%r0 != 0) 1671 * return %r0; 1672 */ 1673 "cmp r0, #0\n" 1674 "bne 1f\n" 1675 1676 /* In the child, now. Call "fn(arg)". */ 1677 "ldr r0, [sp, #4]\n" 1678 "ldr ip, [sp], #8\n" 1679 BLX(ip) 1680 /* Call _exit(%r0). */ 1681 "mov r7, %7\n" 1682 "swi 0x0\n" 1683 "1:\n" 1684 "mov %0, r0\n" 1685 : "=r"(res) 1686 : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r7), 1687 "i"(__NR_exit) 1688 : "memory"); 1689 return res; 1690 } 1691 #endif // defined(__x86_64__) && SANITIZER_LINUX 1692 1693 #if SANITIZER_LINUX 1694 int internal_uname(struct utsname *buf) { 1695 return internal_syscall(SYSCALL(uname), buf); 1696 } 1697 #endif 1698 1699 #if SANITIZER_ANDROID 1700 #if __ANDROID_API__ < 21 1701 extern "C" __attribute__((weak)) int dl_iterate_phdr( 1702 int (*)(struct dl_phdr_info *, size_t, void *), void *); 1703 #endif 1704 1705 static int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size, 1706 void *data) { 1707 // Any name starting with "lib" indicates a bug in L where library base names 1708 // are returned instead of paths. 1709 if (info->dlpi_name && info->dlpi_name[0] == 'l' && 1710 info->dlpi_name[1] == 'i' && info->dlpi_name[2] == 'b') { 1711 *(bool *)data = true; 1712 return 1; 1713 } 1714 return 0; 1715 } 1716 1717 static atomic_uint32_t android_api_level; 1718 1719 static AndroidApiLevel AndroidDetectApiLevelStatic() { 1720 #if __ANDROID_API__ <= 19 1721 return ANDROID_KITKAT; 1722 #elif __ANDROID_API__ <= 22 1723 return ANDROID_LOLLIPOP_MR1; 1724 #else 1725 return ANDROID_POST_LOLLIPOP; 1726 #endif 1727 } 1728 1729 static AndroidApiLevel AndroidDetectApiLevel() { 1730 if (!&dl_iterate_phdr) 1731 return ANDROID_KITKAT; // K or lower 1732 bool base_name_seen = false; 1733 dl_iterate_phdr(dl_iterate_phdr_test_cb, &base_name_seen); 1734 if (base_name_seen) 1735 return ANDROID_LOLLIPOP_MR1; // L MR1 1736 return ANDROID_POST_LOLLIPOP; // post-L 1737 // Plain L (API level 21) is completely broken wrt ASan and not very 1738 // interesting to detect. 1739 } 1740 1741 extern "C" __attribute__((weak)) void* _DYNAMIC; 1742 1743 AndroidApiLevel AndroidGetApiLevel() { 1744 AndroidApiLevel level = 1745 (AndroidApiLevel)atomic_load(&android_api_level, memory_order_relaxed); 1746 if (level) return level; 1747 level = &_DYNAMIC == nullptr ? AndroidDetectApiLevelStatic() 1748 : AndroidDetectApiLevel(); 1749 atomic_store(&android_api_level, level, memory_order_relaxed); 1750 return level; 1751 } 1752 1753 #endif 1754 1755 static HandleSignalMode GetHandleSignalModeImpl(int signum) { 1756 switch (signum) { 1757 case SIGABRT: 1758 return common_flags()->handle_abort; 1759 case SIGILL: 1760 return common_flags()->handle_sigill; 1761 case SIGTRAP: 1762 return common_flags()->handle_sigtrap; 1763 case SIGFPE: 1764 return common_flags()->handle_sigfpe; 1765 case SIGSEGV: 1766 return common_flags()->handle_segv; 1767 case SIGBUS: 1768 return common_flags()->handle_sigbus; 1769 } 1770 return kHandleSignalNo; 1771 } 1772 1773 HandleSignalMode GetHandleSignalMode(int signum) { 1774 HandleSignalMode result = GetHandleSignalModeImpl(signum); 1775 if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler) 1776 return kHandleSignalExclusive; 1777 return result; 1778 } 1779 1780 #if !SANITIZER_GO 1781 void *internal_start_thread(void *(*func)(void *arg), void *arg) { 1782 // Start the thread with signals blocked, otherwise it can steal user signals. 1783 __sanitizer_sigset_t set, old; 1784 internal_sigfillset(&set); 1785 #if SANITIZER_LINUX && !SANITIZER_ANDROID 1786 // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked 1787 // on any thread, setuid call hangs (see test/tsan/setuid.c). 1788 internal_sigdelset(&set, 33); 1789 #endif 1790 internal_sigprocmask(SIG_SETMASK, &set, &old); 1791 void *th; 1792 real_pthread_create(&th, nullptr, func, arg); 1793 internal_sigprocmask(SIG_SETMASK, &old, nullptr); 1794 return th; 1795 } 1796 1797 void internal_join_thread(void *th) { 1798 real_pthread_join(th, nullptr); 1799 } 1800 #else 1801 void *internal_start_thread(void *(*func)(void *), void *arg) { return 0; } 1802 1803 void internal_join_thread(void *th) {} 1804 #endif 1805 1806 #if defined(__aarch64__) 1807 // Android headers in the older NDK releases miss this definition. 1808 struct __sanitizer_esr_context { 1809 struct _aarch64_ctx head; 1810 uint64_t esr; 1811 }; 1812 1813 static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) { 1814 static const u32 kEsrMagic = 0x45535201; 1815 u8 *aux = ucontext->uc_mcontext.__reserved; 1816 while (true) { 1817 _aarch64_ctx *ctx = (_aarch64_ctx *)aux; 1818 if (ctx->size == 0) break; 1819 if (ctx->magic == kEsrMagic) { 1820 *esr = ((__sanitizer_esr_context *)ctx)->esr; 1821 return true; 1822 } 1823 aux += ctx->size; 1824 } 1825 return false; 1826 } 1827 #endif 1828 1829 using Context = ucontext_t; 1830 1831 SignalContext::WriteFlag SignalContext::GetWriteFlag() const { 1832 Context *ucontext = (Context *)context; 1833 #if defined(__x86_64__) || defined(__i386__) 1834 static const uptr PF_WRITE = 1U << 1; 1835 #if SANITIZER_FREEBSD 1836 uptr err = ucontext->uc_mcontext.mc_err; 1837 #elif SANITIZER_NETBSD 1838 uptr err = ucontext->uc_mcontext.__gregs[_REG_ERR]; 1839 #elif SANITIZER_SOLARIS && defined(__i386__) 1840 const int Err = 13; 1841 uptr err = ucontext->uc_mcontext.gregs[Err]; 1842 #else 1843 uptr err = ucontext->uc_mcontext.gregs[REG_ERR]; 1844 #endif // SANITIZER_FREEBSD 1845 return err & PF_WRITE ? WRITE : READ; 1846 #elif defined(__mips__) 1847 uint32_t *exception_source; 1848 uint32_t faulty_instruction; 1849 uint32_t op_code; 1850 1851 exception_source = (uint32_t *)ucontext->uc_mcontext.pc; 1852 faulty_instruction = (uint32_t)(*exception_source); 1853 1854 op_code = (faulty_instruction >> 26) & 0x3f; 1855 1856 // FIXME: Add support for FPU, microMIPS, DSP, MSA memory instructions. 1857 switch (op_code) { 1858 case 0x28: // sb 1859 case 0x29: // sh 1860 case 0x2b: // sw 1861 case 0x3f: // sd 1862 #if __mips_isa_rev < 6 1863 case 0x2c: // sdl 1864 case 0x2d: // sdr 1865 case 0x2a: // swl 1866 case 0x2e: // swr 1867 #endif 1868 return SignalContext::WRITE; 1869 1870 case 0x20: // lb 1871 case 0x24: // lbu 1872 case 0x21: // lh 1873 case 0x25: // lhu 1874 case 0x23: // lw 1875 case 0x27: // lwu 1876 case 0x37: // ld 1877 #if __mips_isa_rev < 6 1878 case 0x1a: // ldl 1879 case 0x1b: // ldr 1880 case 0x22: // lwl 1881 case 0x26: // lwr 1882 #endif 1883 return SignalContext::READ; 1884 #if __mips_isa_rev == 6 1885 case 0x3b: // pcrel 1886 op_code = (faulty_instruction >> 19) & 0x3; 1887 switch (op_code) { 1888 case 0x1: // lwpc 1889 case 0x2: // lwupc 1890 return SignalContext::READ; 1891 } 1892 #endif 1893 } 1894 return SignalContext::UNKNOWN; 1895 #elif defined(__arm__) 1896 static const uptr FSR_WRITE = 1U << 11; 1897 uptr fsr = ucontext->uc_mcontext.error_code; 1898 return fsr & FSR_WRITE ? WRITE : READ; 1899 #elif defined(__aarch64__) 1900 static const u64 ESR_ELx_WNR = 1U << 6; 1901 u64 esr; 1902 if (!Aarch64GetESR(ucontext, &esr)) return UNKNOWN; 1903 return esr & ESR_ELx_WNR ? WRITE : READ; 1904 #elif defined(__sparc__) 1905 // Decode the instruction to determine the access type. 1906 // From OpenSolaris $SRC/uts/sun4/os/trap.c (get_accesstype). 1907 #if SANITIZER_SOLARIS 1908 uptr pc = ucontext->uc_mcontext.gregs[REG_PC]; 1909 #else 1910 // Historical BSDism here. 1911 struct sigcontext *scontext = (struct sigcontext *)context; 1912 #if defined(__arch64__) 1913 uptr pc = scontext->sigc_regs.tpc; 1914 #else 1915 uptr pc = scontext->si_regs.pc; 1916 #endif 1917 #endif 1918 u32 instr = *(u32 *)pc; 1919 return (instr >> 21) & 1 ? WRITE: READ; 1920 #elif defined(__riscv) 1921 unsigned long pc = ucontext->uc_mcontext.__gregs[REG_PC]; 1922 unsigned faulty_instruction = *(uint16_t *)pc; 1923 1924 #if defined(__riscv_compressed) 1925 if ((faulty_instruction & 0x3) != 0x3) { // it's a compressed instruction 1926 // set op_bits to the instruction bits [1, 0, 15, 14, 13] 1927 unsigned op_bits = 1928 ((faulty_instruction & 0x3) << 3) | (faulty_instruction >> 13); 1929 unsigned rd = faulty_instruction & 0xF80; // bits 7-11, inclusive 1930 switch (op_bits) { 1931 case 0b10'010: // c.lwsp (rd != x0) 1932 #if __riscv_xlen == 64 1933 case 0b10'011: // c.ldsp (rd != x0) 1934 #endif 1935 return rd ? SignalContext::READ : SignalContext::UNKNOWN; 1936 case 0b00'010: // c.lw 1937 #if __riscv_flen >= 32 && __riscv_xlen == 32 1938 case 0b10'011: // c.flwsp 1939 #endif 1940 #if __riscv_flen >= 32 || __riscv_xlen == 64 1941 case 0b00'011: // c.flw / c.ld 1942 #endif 1943 #if __riscv_flen == 64 1944 case 0b00'001: // c.fld 1945 case 0b10'001: // c.fldsp 1946 #endif 1947 return SignalContext::READ; 1948 case 0b00'110: // c.sw 1949 case 0b10'110: // c.swsp 1950 #if __riscv_flen >= 32 || __riscv_xlen == 64 1951 case 0b00'111: // c.fsw / c.sd 1952 case 0b10'111: // c.fswsp / c.sdsp 1953 #endif 1954 #if __riscv_flen == 64 1955 case 0b00'101: // c.fsd 1956 case 0b10'101: // c.fsdsp 1957 #endif 1958 return SignalContext::WRITE; 1959 default: 1960 return SignalContext::UNKNOWN; 1961 } 1962 } 1963 #endif 1964 1965 unsigned opcode = faulty_instruction & 0x7f; // lower 7 bits 1966 unsigned funct3 = (faulty_instruction >> 12) & 0x7; // bits 12-14, inclusive 1967 switch (opcode) { 1968 case 0b0000011: // loads 1969 switch (funct3) { 1970 case 0b000: // lb 1971 case 0b001: // lh 1972 case 0b010: // lw 1973 #if __riscv_xlen == 64 1974 case 0b011: // ld 1975 #endif 1976 case 0b100: // lbu 1977 case 0b101: // lhu 1978 return SignalContext::READ; 1979 default: 1980 return SignalContext::UNKNOWN; 1981 } 1982 case 0b0100011: // stores 1983 switch (funct3) { 1984 case 0b000: // sb 1985 case 0b001: // sh 1986 case 0b010: // sw 1987 #if __riscv_xlen == 64 1988 case 0b011: // sd 1989 #endif 1990 return SignalContext::WRITE; 1991 default: 1992 return SignalContext::UNKNOWN; 1993 } 1994 #if __riscv_flen >= 32 1995 case 0b0000111: // floating-point loads 1996 switch (funct3) { 1997 case 0b010: // flw 1998 #if __riscv_flen == 64 1999 case 0b011: // fld 2000 #endif 2001 return SignalContext::READ; 2002 default: 2003 return SignalContext::UNKNOWN; 2004 } 2005 case 0b0100111: // floating-point stores 2006 switch (funct3) { 2007 case 0b010: // fsw 2008 #if __riscv_flen == 64 2009 case 0b011: // fsd 2010 #endif 2011 return SignalContext::WRITE; 2012 default: 2013 return SignalContext::UNKNOWN; 2014 } 2015 #endif 2016 default: 2017 return SignalContext::UNKNOWN; 2018 } 2019 #else 2020 (void)ucontext; 2021 return UNKNOWN; // FIXME: Implement. 2022 #endif 2023 } 2024 2025 bool SignalContext::IsTrueFaultingAddress() const { 2026 auto si = static_cast<const siginfo_t *>(siginfo); 2027 // SIGSEGV signals without a true fault address have si_code set to 128. 2028 return si->si_signo == SIGSEGV && si->si_code != 128; 2029 } 2030 2031 void SignalContext::DumpAllRegisters(void *context) { 2032 // FIXME: Implement this. 2033 } 2034 2035 static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { 2036 #if SANITIZER_NETBSD 2037 // This covers all NetBSD architectures 2038 ucontext_t *ucontext = (ucontext_t *)context; 2039 *pc = _UC_MACHINE_PC(ucontext); 2040 *bp = _UC_MACHINE_FP(ucontext); 2041 *sp = _UC_MACHINE_SP(ucontext); 2042 #elif defined(__arm__) 2043 ucontext_t *ucontext = (ucontext_t*)context; 2044 *pc = ucontext->uc_mcontext.arm_pc; 2045 *bp = ucontext->uc_mcontext.arm_fp; 2046 *sp = ucontext->uc_mcontext.arm_sp; 2047 #elif defined(__aarch64__) 2048 ucontext_t *ucontext = (ucontext_t*)context; 2049 *pc = ucontext->uc_mcontext.pc; 2050 *bp = ucontext->uc_mcontext.regs[29]; 2051 *sp = ucontext->uc_mcontext.sp; 2052 #elif defined(__hppa__) 2053 ucontext_t *ucontext = (ucontext_t*)context; 2054 *pc = ucontext->uc_mcontext.sc_iaoq[0]; 2055 /* GCC uses %r3 whenever a frame pointer is needed. */ 2056 *bp = ucontext->uc_mcontext.sc_gr[3]; 2057 *sp = ucontext->uc_mcontext.sc_gr[30]; 2058 #elif defined(__x86_64__) 2059 # if SANITIZER_FREEBSD 2060 ucontext_t *ucontext = (ucontext_t*)context; 2061 *pc = ucontext->uc_mcontext.mc_rip; 2062 *bp = ucontext->uc_mcontext.mc_rbp; 2063 *sp = ucontext->uc_mcontext.mc_rsp; 2064 # else 2065 ucontext_t *ucontext = (ucontext_t*)context; 2066 *pc = ucontext->uc_mcontext.gregs[REG_RIP]; 2067 *bp = ucontext->uc_mcontext.gregs[REG_RBP]; 2068 *sp = ucontext->uc_mcontext.gregs[REG_RSP]; 2069 # endif 2070 #elif defined(__i386__) 2071 # if SANITIZER_FREEBSD 2072 ucontext_t *ucontext = (ucontext_t*)context; 2073 *pc = ucontext->uc_mcontext.mc_eip; 2074 *bp = ucontext->uc_mcontext.mc_ebp; 2075 *sp = ucontext->uc_mcontext.mc_esp; 2076 # else 2077 ucontext_t *ucontext = (ucontext_t*)context; 2078 # if SANITIZER_SOLARIS 2079 /* Use the numeric values: the symbolic ones are undefined by llvm 2080 include/llvm/Support/Solaris.h. */ 2081 # ifndef REG_EIP 2082 # define REG_EIP 14 // REG_PC 2083 # endif 2084 # ifndef REG_EBP 2085 # define REG_EBP 6 // REG_FP 2086 # endif 2087 # ifndef REG_UESP 2088 # define REG_UESP 17 // REG_SP 2089 # endif 2090 # endif 2091 *pc = ucontext->uc_mcontext.gregs[REG_EIP]; 2092 *bp = ucontext->uc_mcontext.gregs[REG_EBP]; 2093 *sp = ucontext->uc_mcontext.gregs[REG_UESP]; 2094 # endif 2095 #elif defined(__powerpc__) || defined(__powerpc64__) 2096 ucontext_t *ucontext = (ucontext_t*)context; 2097 *pc = ucontext->uc_mcontext.regs->nip; 2098 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1]; 2099 // The powerpc{,64}-linux ABIs do not specify r31 as the frame 2100 // pointer, but GCC always uses r31 when we need a frame pointer. 2101 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31]; 2102 #elif defined(__sparc__) 2103 #if defined(__arch64__) || defined(__sparcv9) 2104 #define STACK_BIAS 2047 2105 #else 2106 #define STACK_BIAS 0 2107 # endif 2108 # if SANITIZER_SOLARIS 2109 ucontext_t *ucontext = (ucontext_t *)context; 2110 *pc = ucontext->uc_mcontext.gregs[REG_PC]; 2111 *sp = ucontext->uc_mcontext.gregs[REG_O6] + STACK_BIAS; 2112 #else 2113 // Historical BSDism here. 2114 struct sigcontext *scontext = (struct sigcontext *)context; 2115 #if defined(__arch64__) 2116 *pc = scontext->sigc_regs.tpc; 2117 *sp = scontext->sigc_regs.u_regs[14] + STACK_BIAS; 2118 #else 2119 *pc = scontext->si_regs.pc; 2120 *sp = scontext->si_regs.u_regs[14]; 2121 #endif 2122 # endif 2123 *bp = (uptr)((uhwptr *)*sp)[14] + STACK_BIAS; 2124 #elif defined(__mips__) 2125 ucontext_t *ucontext = (ucontext_t*)context; 2126 *pc = ucontext->uc_mcontext.pc; 2127 *bp = ucontext->uc_mcontext.gregs[30]; 2128 *sp = ucontext->uc_mcontext.gregs[29]; 2129 #elif defined(__s390__) 2130 ucontext_t *ucontext = (ucontext_t*)context; 2131 # if defined(__s390x__) 2132 *pc = ucontext->uc_mcontext.psw.addr; 2133 # else 2134 *pc = ucontext->uc_mcontext.psw.addr & 0x7fffffff; 2135 # endif 2136 *bp = ucontext->uc_mcontext.gregs[11]; 2137 *sp = ucontext->uc_mcontext.gregs[15]; 2138 #elif defined(__riscv) 2139 ucontext_t *ucontext = (ucontext_t*)context; 2140 *pc = ucontext->uc_mcontext.__gregs[REG_PC]; 2141 *bp = ucontext->uc_mcontext.__gregs[REG_S0]; 2142 *sp = ucontext->uc_mcontext.__gregs[REG_SP]; 2143 #else 2144 # error "Unsupported arch" 2145 #endif 2146 } 2147 2148 void SignalContext::InitPcSpBp() { GetPcSpBp(context, &pc, &sp, &bp); } 2149 2150 void InitializePlatformEarly() { 2151 // Do nothing. 2152 } 2153 2154 void MaybeReexec() { 2155 // No need to re-exec on Linux. 2156 } 2157 2158 void CheckASLR() { 2159 #if SANITIZER_NETBSD 2160 int mib[3]; 2161 int paxflags; 2162 uptr len = sizeof(paxflags); 2163 2164 mib[0] = CTL_PROC; 2165 mib[1] = internal_getpid(); 2166 mib[2] = PROC_PID_PAXFLAGS; 2167 2168 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) { 2169 Printf("sysctl failed\n"); 2170 Die(); 2171 } 2172 2173 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_ASLR)) { 2174 Printf("This sanitizer is not compatible with enabled ASLR.\n" 2175 "To disable ASLR, please run \"paxctl +a %s\" and try again.\n", 2176 GetArgv()[0]); 2177 Die(); 2178 } 2179 #elif SANITIZER_PPC64V2 2180 // Disable ASLR for Linux PPC64LE. 2181 int old_personality = personality(0xffffffff); 2182 if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) { 2183 VReport(1, "WARNING: Program is being run with address space layout " 2184 "randomization (ASLR) enabled which prevents the thread and " 2185 "memory sanitizers from working on powerpc64le.\n" 2186 "ASLR will be disabled and the program re-executed.\n"); 2187 CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1); 2188 ReExec(); 2189 } 2190 #elif SANITIZER_FREEBSD 2191 int aslr_status; 2192 if (UNLIKELY(procctl(P_PID, 0, PROC_ASLR_STATUS, &aslr_status) == -1)) { 2193 // We're making things less 'dramatic' here since 2194 // the cmd is not necessarily guaranteed to be here 2195 // just yet regarding FreeBSD release 2196 return; 2197 } 2198 if ((aslr_status & PROC_ASLR_ACTIVE) != 0) { 2199 Printf("This sanitizer is not compatible with enabled ASLR " 2200 "and binaries compiled with PIE\n"); 2201 Die(); 2202 } 2203 #else 2204 // Do nothing 2205 #endif 2206 } 2207 2208 void CheckMPROTECT() { 2209 #if SANITIZER_NETBSD 2210 int mib[3]; 2211 int paxflags; 2212 uptr len = sizeof(paxflags); 2213 2214 mib[0] = CTL_PROC; 2215 mib[1] = internal_getpid(); 2216 mib[2] = PROC_PID_PAXFLAGS; 2217 2218 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) { 2219 Printf("sysctl failed\n"); 2220 Die(); 2221 } 2222 2223 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_MPROTECT)) { 2224 Printf("This sanitizer is not compatible with enabled MPROTECT\n"); 2225 Die(); 2226 } 2227 #else 2228 // Do nothing 2229 #endif 2230 } 2231 2232 void CheckNoDeepBind(const char *filename, int flag) { 2233 #ifdef RTLD_DEEPBIND 2234 if (flag & RTLD_DEEPBIND) { 2235 Report( 2236 "You are trying to dlopen a %s shared library with RTLD_DEEPBIND flag" 2237 " which is incompatible with sanitizer runtime " 2238 "(see https://github.com/google/sanitizers/issues/611 for details" 2239 "). If you want to run %s library under sanitizers please remove " 2240 "RTLD_DEEPBIND from dlopen flags.\n", 2241 filename, filename); 2242 Die(); 2243 } 2244 #endif 2245 } 2246 2247 uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding, 2248 uptr *largest_gap_found, 2249 uptr *max_occupied_addr) { 2250 UNREACHABLE("FindAvailableMemoryRange is not available"); 2251 return 0; 2252 } 2253 2254 bool GetRandom(void *buffer, uptr length, bool blocking) { 2255 if (!buffer || !length || length > 256) 2256 return false; 2257 #if SANITIZER_USE_GETENTROPY 2258 uptr rnd = getentropy(buffer, length); 2259 int rverrno = 0; 2260 if (internal_iserror(rnd, &rverrno) && rverrno == EFAULT) 2261 return false; 2262 else if (rnd == 0) 2263 return true; 2264 #endif // SANITIZER_USE_GETENTROPY 2265 2266 #if SANITIZER_USE_GETRANDOM 2267 static atomic_uint8_t skip_getrandom_syscall; 2268 if (!atomic_load_relaxed(&skip_getrandom_syscall)) { 2269 // Up to 256 bytes, getrandom will not be interrupted. 2270 uptr res = internal_syscall(SYSCALL(getrandom), buffer, length, 2271 blocking ? 0 : GRND_NONBLOCK); 2272 int rverrno = 0; 2273 if (internal_iserror(res, &rverrno) && rverrno == ENOSYS) 2274 atomic_store_relaxed(&skip_getrandom_syscall, 1); 2275 else if (res == length) 2276 return true; 2277 } 2278 #endif // SANITIZER_USE_GETRANDOM 2279 // Up to 256 bytes, a read off /dev/urandom will not be interrupted. 2280 // blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom. 2281 uptr fd = internal_open("/dev/urandom", O_RDONLY); 2282 if (internal_iserror(fd)) 2283 return false; 2284 uptr res = internal_read(fd, buffer, length); 2285 if (internal_iserror(res)) 2286 return false; 2287 internal_close(fd); 2288 return true; 2289 } 2290 2291 } // namespace __sanitizer 2292 2293 #endif 2294