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) && defined(__linux__) 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 SANITIZER_LINUX 1222 #if defined(__x86_64__) 1223 // We cannot use glibc's clone wrapper, because it messes with the child 1224 // task's TLS. It writes the PID and TID of the child task to its thread 1225 // descriptor, but in our case the child task shares the thread descriptor with 1226 // the parent (because we don't know how to allocate a new thread 1227 // descriptor to keep glibc happy). So the stock version of clone(), when 1228 // used with CLONE_VM, would end up corrupting the parent's thread descriptor. 1229 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1230 int *parent_tidptr, void *newtls, int *child_tidptr) { 1231 long long res; 1232 if (!fn || !child_stack) 1233 return -EINVAL; 1234 CHECK_EQ(0, (uptr)child_stack % 16); 1235 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long); 1236 ((unsigned long long *)child_stack)[0] = (uptr)fn; 1237 ((unsigned long long *)child_stack)[1] = (uptr)arg; 1238 register void *r8 __asm__("r8") = newtls; 1239 register int *r10 __asm__("r10") = child_tidptr; 1240 __asm__ __volatile__( 1241 /* %rax = syscall(%rax = SYSCALL(clone), 1242 * %rdi = flags, 1243 * %rsi = child_stack, 1244 * %rdx = parent_tidptr, 1245 * %r8 = new_tls, 1246 * %r10 = child_tidptr) 1247 */ 1248 "syscall\n" 1249 1250 /* if (%rax != 0) 1251 * return; 1252 */ 1253 "testq %%rax,%%rax\n" 1254 "jnz 1f\n" 1255 1256 /* In the child. Terminate unwind chain. */ 1257 // XXX: We should also terminate the CFI unwind chain 1258 // here. Unfortunately clang 3.2 doesn't support the 1259 // necessary CFI directives, so we skip that part. 1260 "xorq %%rbp,%%rbp\n" 1261 1262 /* Call "fn(arg)". */ 1263 "popq %%rax\n" 1264 "popq %%rdi\n" 1265 "call *%%rax\n" 1266 1267 /* Call _exit(%rax). */ 1268 "movq %%rax,%%rdi\n" 1269 "movq %2,%%rax\n" 1270 "syscall\n" 1271 1272 /* Return to parent. */ 1273 "1:\n" 1274 : "=a" (res) 1275 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), 1276 "S"(child_stack), 1277 "D"(flags), 1278 "d"(parent_tidptr), 1279 "r"(r8), 1280 "r"(r10) 1281 : "memory", "r11", "rcx"); 1282 return res; 1283 } 1284 #elif defined(__mips__) 1285 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1286 int *parent_tidptr, void *newtls, int *child_tidptr) { 1287 long long res; 1288 if (!fn || !child_stack) 1289 return -EINVAL; 1290 CHECK_EQ(0, (uptr)child_stack % 16); 1291 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long); 1292 ((unsigned long long *)child_stack)[0] = (uptr)fn; 1293 ((unsigned long long *)child_stack)[1] = (uptr)arg; 1294 register void *a3 __asm__("$7") = newtls; 1295 register int *a4 __asm__("$8") = child_tidptr; 1296 // We don't have proper CFI directives here because it requires alot of code 1297 // for very marginal benefits. 1298 __asm__ __volatile__( 1299 /* $v0 = syscall($v0 = __NR_clone, 1300 * $a0 = flags, 1301 * $a1 = child_stack, 1302 * $a2 = parent_tidptr, 1303 * $a3 = new_tls, 1304 * $a4 = child_tidptr) 1305 */ 1306 ".cprestore 16;\n" 1307 "move $4,%1;\n" 1308 "move $5,%2;\n" 1309 "move $6,%3;\n" 1310 "move $7,%4;\n" 1311 /* Store the fifth argument on stack 1312 * if we are using 32-bit abi. 1313 */ 1314 #if SANITIZER_WORDSIZE == 32 1315 "lw %5,16($29);\n" 1316 #else 1317 "move $8,%5;\n" 1318 #endif 1319 "li $2,%6;\n" 1320 "syscall;\n" 1321 1322 /* if ($v0 != 0) 1323 * return; 1324 */ 1325 "bnez $2,1f;\n" 1326 1327 /* Call "fn(arg)". */ 1328 #if SANITIZER_WORDSIZE == 32 1329 #ifdef __BIG_ENDIAN__ 1330 "lw $25,4($29);\n" 1331 "lw $4,12($29);\n" 1332 #else 1333 "lw $25,0($29);\n" 1334 "lw $4,8($29);\n" 1335 #endif 1336 #else 1337 "ld $25,0($29);\n" 1338 "ld $4,8($29);\n" 1339 #endif 1340 "jal $25;\n" 1341 1342 /* Call _exit($v0). */ 1343 "move $4,$2;\n" 1344 "li $2,%7;\n" 1345 "syscall;\n" 1346 1347 /* Return to parent. */ 1348 "1:\n" 1349 : "=r" (res) 1350 : "r"(flags), 1351 "r"(child_stack), 1352 "r"(parent_tidptr), 1353 "r"(a3), 1354 "r"(a4), 1355 "i"(__NR_clone), 1356 "i"(__NR_exit) 1357 : "memory", "$29" ); 1358 return res; 1359 } 1360 #elif SANITIZER_RISCV64 1361 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1362 int *parent_tidptr, void *newtls, int *child_tidptr) { 1363 if (!fn || !child_stack) 1364 return -EINVAL; 1365 1366 CHECK_EQ(0, (uptr)child_stack % 16); 1367 1368 register int res __asm__("a0"); 1369 register int __flags __asm__("a0") = flags; 1370 register void *__stack __asm__("a1") = child_stack; 1371 register int *__ptid __asm__("a2") = parent_tidptr; 1372 register void *__tls __asm__("a3") = newtls; 1373 register int *__ctid __asm__("a4") = child_tidptr; 1374 register int (*__fn)(void *) __asm__("a5") = fn; 1375 register void *__arg __asm__("a6") = arg; 1376 register int nr_clone __asm__("a7") = __NR_clone; 1377 1378 __asm__ __volatile__( 1379 "ecall\n" 1380 1381 /* if (a0 != 0) 1382 * return a0; 1383 */ 1384 "bnez a0, 1f\n" 1385 1386 // In the child, now. Call "fn(arg)". 1387 "mv a0, a6\n" 1388 "jalr a5\n" 1389 1390 // Call _exit(a0). 1391 "addi a7, zero, %9\n" 1392 "ecall\n" 1393 "1:\n" 1394 1395 : "=r"(res) 1396 : "0"(__flags), "r"(__stack), "r"(__ptid), "r"(__tls), "r"(__ctid), 1397 "r"(__fn), "r"(__arg), "r"(nr_clone), "i"(__NR_exit) 1398 : "memory"); 1399 return res; 1400 } 1401 #elif defined(__aarch64__) 1402 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1403 int *parent_tidptr, void *newtls, int *child_tidptr) { 1404 long long res; 1405 if (!fn || !child_stack) 1406 return -EINVAL; 1407 CHECK_EQ(0, (uptr)child_stack % 16); 1408 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long); 1409 ((unsigned long long *)child_stack)[0] = (uptr)fn; 1410 ((unsigned long long *)child_stack)[1] = (uptr)arg; 1411 1412 register int (*__fn)(void *) __asm__("x0") = fn; 1413 register void *__stack __asm__("x1") = child_stack; 1414 register int __flags __asm__("x2") = flags; 1415 register void *__arg __asm__("x3") = arg; 1416 register int *__ptid __asm__("x4") = parent_tidptr; 1417 register void *__tls __asm__("x5") = newtls; 1418 register int *__ctid __asm__("x6") = child_tidptr; 1419 1420 __asm__ __volatile__( 1421 "mov x0,x2\n" /* flags */ 1422 "mov x2,x4\n" /* ptid */ 1423 "mov x3,x5\n" /* tls */ 1424 "mov x4,x6\n" /* ctid */ 1425 "mov x8,%9\n" /* clone */ 1426 1427 "svc 0x0\n" 1428 1429 /* if (%r0 != 0) 1430 * return %r0; 1431 */ 1432 "cmp x0, #0\n" 1433 "bne 1f\n" 1434 1435 /* In the child, now. Call "fn(arg)". */ 1436 "ldp x1, x0, [sp], #16\n" 1437 "blr x1\n" 1438 1439 /* Call _exit(%r0). */ 1440 "mov x8, %10\n" 1441 "svc 0x0\n" 1442 "1:\n" 1443 1444 : "=r" (res) 1445 : "i"(-EINVAL), 1446 "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg), 1447 "r"(__ptid), "r"(__tls), "r"(__ctid), 1448 "i"(__NR_clone), "i"(__NR_exit) 1449 : "x30", "memory"); 1450 return res; 1451 } 1452 #elif defined(__powerpc64__) 1453 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1454 int *parent_tidptr, void *newtls, int *child_tidptr) { 1455 long long res; 1456 // Stack frame structure. 1457 #if SANITIZER_PPC64V1 1458 // Back chain == 0 (SP + 112) 1459 // Frame (112 bytes): 1460 // Parameter save area (SP + 48), 8 doublewords 1461 // TOC save area (SP + 40) 1462 // Link editor doubleword (SP + 32) 1463 // Compiler doubleword (SP + 24) 1464 // LR save area (SP + 16) 1465 // CR save area (SP + 8) 1466 // Back chain (SP + 0) 1467 # define FRAME_SIZE 112 1468 # define FRAME_TOC_SAVE_OFFSET 40 1469 #elif SANITIZER_PPC64V2 1470 // Back chain == 0 (SP + 32) 1471 // Frame (32 bytes): 1472 // TOC save area (SP + 24) 1473 // LR save area (SP + 16) 1474 // CR save area (SP + 8) 1475 // Back chain (SP + 0) 1476 # define FRAME_SIZE 32 1477 # define FRAME_TOC_SAVE_OFFSET 24 1478 #else 1479 # error "Unsupported PPC64 ABI" 1480 #endif 1481 if (!fn || !child_stack) 1482 return -EINVAL; 1483 CHECK_EQ(0, (uptr)child_stack % 16); 1484 1485 register int (*__fn)(void *) __asm__("r3") = fn; 1486 register void *__cstack __asm__("r4") = child_stack; 1487 register int __flags __asm__("r5") = flags; 1488 register void *__arg __asm__("r6") = arg; 1489 register int *__ptidptr __asm__("r7") = parent_tidptr; 1490 register void *__newtls __asm__("r8") = newtls; 1491 register int *__ctidptr __asm__("r9") = child_tidptr; 1492 1493 __asm__ __volatile__( 1494 /* fn and arg are saved across the syscall */ 1495 "mr 28, %5\n\t" 1496 "mr 27, %8\n\t" 1497 1498 /* syscall 1499 r0 == __NR_clone 1500 r3 == flags 1501 r4 == child_stack 1502 r5 == parent_tidptr 1503 r6 == newtls 1504 r7 == child_tidptr */ 1505 "mr 3, %7\n\t" 1506 "mr 5, %9\n\t" 1507 "mr 6, %10\n\t" 1508 "mr 7, %11\n\t" 1509 "li 0, %3\n\t" 1510 "sc\n\t" 1511 1512 /* Test if syscall was successful */ 1513 "cmpdi cr1, 3, 0\n\t" 1514 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t" 1515 "bne- cr1, 1f\n\t" 1516 1517 /* Set up stack frame */ 1518 "li 29, 0\n\t" 1519 "stdu 29, -8(1)\n\t" 1520 "stdu 1, -%12(1)\n\t" 1521 /* Do the function call */ 1522 "std 2, %13(1)\n\t" 1523 #if SANITIZER_PPC64V1 1524 "ld 0, 0(28)\n\t" 1525 "ld 2, 8(28)\n\t" 1526 "mtctr 0\n\t" 1527 #elif SANITIZER_PPC64V2 1528 "mr 12, 28\n\t" 1529 "mtctr 12\n\t" 1530 #else 1531 # error "Unsupported PPC64 ABI" 1532 #endif 1533 "mr 3, 27\n\t" 1534 "bctrl\n\t" 1535 "ld 2, %13(1)\n\t" 1536 1537 /* Call _exit(r3) */ 1538 "li 0, %4\n\t" 1539 "sc\n\t" 1540 1541 /* Return to parent */ 1542 "1:\n\t" 1543 "mr %0, 3\n\t" 1544 : "=r" (res) 1545 : "0" (-1), 1546 "i" (EINVAL), 1547 "i" (__NR_clone), 1548 "i" (__NR_exit), 1549 "r" (__fn), 1550 "r" (__cstack), 1551 "r" (__flags), 1552 "r" (__arg), 1553 "r" (__ptidptr), 1554 "r" (__newtls), 1555 "r" (__ctidptr), 1556 "i" (FRAME_SIZE), 1557 "i" (FRAME_TOC_SAVE_OFFSET) 1558 : "cr0", "cr1", "memory", "ctr", "r0", "r27", "r28", "r29"); 1559 return res; 1560 } 1561 #elif defined(__i386__) 1562 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1563 int *parent_tidptr, void *newtls, int *child_tidptr) { 1564 int res; 1565 if (!fn || !child_stack) 1566 return -EINVAL; 1567 CHECK_EQ(0, (uptr)child_stack % 16); 1568 child_stack = (char *)child_stack - 7 * sizeof(unsigned int); 1569 ((unsigned int *)child_stack)[0] = (uptr)flags; 1570 ((unsigned int *)child_stack)[1] = (uptr)0; 1571 ((unsigned int *)child_stack)[2] = (uptr)fn; 1572 ((unsigned int *)child_stack)[3] = (uptr)arg; 1573 __asm__ __volatile__( 1574 /* %eax = syscall(%eax = SYSCALL(clone), 1575 * %ebx = flags, 1576 * %ecx = child_stack, 1577 * %edx = parent_tidptr, 1578 * %esi = new_tls, 1579 * %edi = child_tidptr) 1580 */ 1581 1582 /* Obtain flags */ 1583 "movl (%%ecx), %%ebx\n" 1584 /* Do the system call */ 1585 "pushl %%ebx\n" 1586 "pushl %%esi\n" 1587 "pushl %%edi\n" 1588 /* Remember the flag value. */ 1589 "movl %%ebx, (%%ecx)\n" 1590 "int $0x80\n" 1591 "popl %%edi\n" 1592 "popl %%esi\n" 1593 "popl %%ebx\n" 1594 1595 /* if (%eax != 0) 1596 * return; 1597 */ 1598 1599 "test %%eax,%%eax\n" 1600 "jnz 1f\n" 1601 1602 /* terminate the stack frame */ 1603 "xorl %%ebp,%%ebp\n" 1604 /* Call FN. */ 1605 "call *%%ebx\n" 1606 #ifdef PIC 1607 "call here\n" 1608 "here:\n" 1609 "popl %%ebx\n" 1610 "addl $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n" 1611 #endif 1612 /* Call exit */ 1613 "movl %%eax, %%ebx\n" 1614 "movl %2, %%eax\n" 1615 "int $0x80\n" 1616 "1:\n" 1617 : "=a" (res) 1618 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), 1619 "c"(child_stack), 1620 "d"(parent_tidptr), 1621 "S"(newtls), 1622 "D"(child_tidptr) 1623 : "memory"); 1624 return res; 1625 } 1626 #elif defined(__arm__) 1627 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, 1628 int *parent_tidptr, void *newtls, int *child_tidptr) { 1629 unsigned int res; 1630 if (!fn || !child_stack) 1631 return -EINVAL; 1632 child_stack = (char *)child_stack - 2 * sizeof(unsigned int); 1633 ((unsigned int *)child_stack)[0] = (uptr)fn; 1634 ((unsigned int *)child_stack)[1] = (uptr)arg; 1635 register int r0 __asm__("r0") = flags; 1636 register void *r1 __asm__("r1") = child_stack; 1637 register int *r2 __asm__("r2") = parent_tidptr; 1638 register void *r3 __asm__("r3") = newtls; 1639 register int *r4 __asm__("r4") = child_tidptr; 1640 register int r7 __asm__("r7") = __NR_clone; 1641 1642 #if __ARM_ARCH > 4 || defined (__ARM_ARCH_4T__) 1643 # define ARCH_HAS_BX 1644 #endif 1645 #if __ARM_ARCH > 4 1646 # define ARCH_HAS_BLX 1647 #endif 1648 1649 #ifdef ARCH_HAS_BX 1650 # ifdef ARCH_HAS_BLX 1651 # define BLX(R) "blx " #R "\n" 1652 # else 1653 # define BLX(R) "mov lr, pc; bx " #R "\n" 1654 # endif 1655 #else 1656 # define BLX(R) "mov lr, pc; mov pc," #R "\n" 1657 #endif 1658 1659 __asm__ __volatile__( 1660 /* %r0 = syscall(%r7 = SYSCALL(clone), 1661 * %r0 = flags, 1662 * %r1 = child_stack, 1663 * %r2 = parent_tidptr, 1664 * %r3 = new_tls, 1665 * %r4 = child_tidptr) 1666 */ 1667 1668 /* Do the system call */ 1669 "swi 0x0\n" 1670 1671 /* if (%r0 != 0) 1672 * return %r0; 1673 */ 1674 "cmp r0, #0\n" 1675 "bne 1f\n" 1676 1677 /* In the child, now. Call "fn(arg)". */ 1678 "ldr r0, [sp, #4]\n" 1679 "ldr ip, [sp], #8\n" 1680 BLX(ip) 1681 /* Call _exit(%r0). */ 1682 "mov r7, %7\n" 1683 "swi 0x0\n" 1684 "1:\n" 1685 "mov %0, r0\n" 1686 : "=r"(res) 1687 : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r7), 1688 "i"(__NR_exit) 1689 : "memory"); 1690 return res; 1691 } 1692 #endif 1693 #endif // SANITIZER_LINUX 1694 1695 #if SANITIZER_LINUX 1696 int internal_uname(struct utsname *buf) { 1697 return internal_syscall(SYSCALL(uname), buf); 1698 } 1699 #endif 1700 1701 #if SANITIZER_ANDROID 1702 #if __ANDROID_API__ < 21 1703 extern "C" __attribute__((weak)) int dl_iterate_phdr( 1704 int (*)(struct dl_phdr_info *, size_t, void *), void *); 1705 #endif 1706 1707 static int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size, 1708 void *data) { 1709 // Any name starting with "lib" indicates a bug in L where library base names 1710 // are returned instead of paths. 1711 if (info->dlpi_name && info->dlpi_name[0] == 'l' && 1712 info->dlpi_name[1] == 'i' && info->dlpi_name[2] == 'b') { 1713 *(bool *)data = true; 1714 return 1; 1715 } 1716 return 0; 1717 } 1718 1719 static atomic_uint32_t android_api_level; 1720 1721 static AndroidApiLevel AndroidDetectApiLevelStatic() { 1722 #if __ANDROID_API__ <= 19 1723 return ANDROID_KITKAT; 1724 #elif __ANDROID_API__ <= 22 1725 return ANDROID_LOLLIPOP_MR1; 1726 #else 1727 return ANDROID_POST_LOLLIPOP; 1728 #endif 1729 } 1730 1731 static AndroidApiLevel AndroidDetectApiLevel() { 1732 if (!&dl_iterate_phdr) 1733 return ANDROID_KITKAT; // K or lower 1734 bool base_name_seen = false; 1735 dl_iterate_phdr(dl_iterate_phdr_test_cb, &base_name_seen); 1736 if (base_name_seen) 1737 return ANDROID_LOLLIPOP_MR1; // L MR1 1738 return ANDROID_POST_LOLLIPOP; // post-L 1739 // Plain L (API level 21) is completely broken wrt ASan and not very 1740 // interesting to detect. 1741 } 1742 1743 extern "C" __attribute__((weak)) void* _DYNAMIC; 1744 1745 AndroidApiLevel AndroidGetApiLevel() { 1746 AndroidApiLevel level = 1747 (AndroidApiLevel)atomic_load(&android_api_level, memory_order_relaxed); 1748 if (level) return level; 1749 level = &_DYNAMIC == nullptr ? AndroidDetectApiLevelStatic() 1750 : AndroidDetectApiLevel(); 1751 atomic_store(&android_api_level, level, memory_order_relaxed); 1752 return level; 1753 } 1754 1755 #endif 1756 1757 static HandleSignalMode GetHandleSignalModeImpl(int signum) { 1758 switch (signum) { 1759 case SIGABRT: 1760 return common_flags()->handle_abort; 1761 case SIGILL: 1762 return common_flags()->handle_sigill; 1763 case SIGTRAP: 1764 return common_flags()->handle_sigtrap; 1765 case SIGFPE: 1766 return common_flags()->handle_sigfpe; 1767 case SIGSEGV: 1768 return common_flags()->handle_segv; 1769 case SIGBUS: 1770 return common_flags()->handle_sigbus; 1771 } 1772 return kHandleSignalNo; 1773 } 1774 1775 HandleSignalMode GetHandleSignalMode(int signum) { 1776 HandleSignalMode result = GetHandleSignalModeImpl(signum); 1777 if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler) 1778 return kHandleSignalExclusive; 1779 return result; 1780 } 1781 1782 #if !SANITIZER_GO 1783 void *internal_start_thread(void *(*func)(void *arg), void *arg) { 1784 // Start the thread with signals blocked, otherwise it can steal user signals. 1785 __sanitizer_sigset_t set, old; 1786 internal_sigfillset(&set); 1787 #if SANITIZER_LINUX && !SANITIZER_ANDROID 1788 // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked 1789 // on any thread, setuid call hangs (see test/tsan/setuid.c). 1790 internal_sigdelset(&set, 33); 1791 #endif 1792 internal_sigprocmask(SIG_SETMASK, &set, &old); 1793 void *th; 1794 real_pthread_create(&th, nullptr, func, arg); 1795 internal_sigprocmask(SIG_SETMASK, &old, nullptr); 1796 return th; 1797 } 1798 1799 void internal_join_thread(void *th) { 1800 real_pthread_join(th, nullptr); 1801 } 1802 #else 1803 void *internal_start_thread(void *(*func)(void *), void *arg) { return 0; } 1804 1805 void internal_join_thread(void *th) {} 1806 #endif 1807 1808 #if defined(__aarch64__) 1809 // Android headers in the older NDK releases miss this definition. 1810 struct __sanitizer_esr_context { 1811 struct _aarch64_ctx head; 1812 uint64_t esr; 1813 }; 1814 1815 static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) { 1816 static const u32 kEsrMagic = 0x45535201; 1817 u8 *aux = ucontext->uc_mcontext.__reserved; 1818 while (true) { 1819 _aarch64_ctx *ctx = (_aarch64_ctx *)aux; 1820 if (ctx->size == 0) break; 1821 if (ctx->magic == kEsrMagic) { 1822 *esr = ((__sanitizer_esr_context *)ctx)->esr; 1823 return true; 1824 } 1825 aux += ctx->size; 1826 } 1827 return false; 1828 } 1829 #endif 1830 1831 using Context = ucontext_t; 1832 1833 SignalContext::WriteFlag SignalContext::GetWriteFlag() const { 1834 Context *ucontext = (Context *)context; 1835 #if defined(__x86_64__) || defined(__i386__) 1836 static const uptr PF_WRITE = 1U << 1; 1837 #if SANITIZER_FREEBSD 1838 uptr err = ucontext->uc_mcontext.mc_err; 1839 #elif SANITIZER_NETBSD 1840 uptr err = ucontext->uc_mcontext.__gregs[_REG_ERR]; 1841 #elif SANITIZER_SOLARIS && defined(__i386__) 1842 const int Err = 13; 1843 uptr err = ucontext->uc_mcontext.gregs[Err]; 1844 #else 1845 uptr err = ucontext->uc_mcontext.gregs[REG_ERR]; 1846 #endif // SANITIZER_FREEBSD 1847 return err & PF_WRITE ? WRITE : READ; 1848 #elif defined(__mips__) 1849 uint32_t *exception_source; 1850 uint32_t faulty_instruction; 1851 uint32_t op_code; 1852 1853 exception_source = (uint32_t *)ucontext->uc_mcontext.pc; 1854 faulty_instruction = (uint32_t)(*exception_source); 1855 1856 op_code = (faulty_instruction >> 26) & 0x3f; 1857 1858 // FIXME: Add support for FPU, microMIPS, DSP, MSA memory instructions. 1859 switch (op_code) { 1860 case 0x28: // sb 1861 case 0x29: // sh 1862 case 0x2b: // sw 1863 case 0x3f: // sd 1864 #if __mips_isa_rev < 6 1865 case 0x2c: // sdl 1866 case 0x2d: // sdr 1867 case 0x2a: // swl 1868 case 0x2e: // swr 1869 #endif 1870 return SignalContext::WRITE; 1871 1872 case 0x20: // lb 1873 case 0x24: // lbu 1874 case 0x21: // lh 1875 case 0x25: // lhu 1876 case 0x23: // lw 1877 case 0x27: // lwu 1878 case 0x37: // ld 1879 #if __mips_isa_rev < 6 1880 case 0x1a: // ldl 1881 case 0x1b: // ldr 1882 case 0x22: // lwl 1883 case 0x26: // lwr 1884 #endif 1885 return SignalContext::READ; 1886 #if __mips_isa_rev == 6 1887 case 0x3b: // pcrel 1888 op_code = (faulty_instruction >> 19) & 0x3; 1889 switch (op_code) { 1890 case 0x1: // lwpc 1891 case 0x2: // lwupc 1892 return SignalContext::READ; 1893 } 1894 #endif 1895 } 1896 return SignalContext::UNKNOWN; 1897 #elif defined(__arm__) 1898 static const uptr FSR_WRITE = 1U << 11; 1899 uptr fsr = ucontext->uc_mcontext.error_code; 1900 return fsr & FSR_WRITE ? WRITE : READ; 1901 #elif defined(__aarch64__) 1902 static const u64 ESR_ELx_WNR = 1U << 6; 1903 u64 esr; 1904 if (!Aarch64GetESR(ucontext, &esr)) return UNKNOWN; 1905 return esr & ESR_ELx_WNR ? WRITE : READ; 1906 #elif defined(__sparc__) 1907 // Decode the instruction to determine the access type. 1908 // From OpenSolaris $SRC/uts/sun4/os/trap.c (get_accesstype). 1909 #if SANITIZER_SOLARIS 1910 uptr pc = ucontext->uc_mcontext.gregs[REG_PC]; 1911 #else 1912 // Historical BSDism here. 1913 struct sigcontext *scontext = (struct sigcontext *)context; 1914 #if defined(__arch64__) 1915 uptr pc = scontext->sigc_regs.tpc; 1916 #else 1917 uptr pc = scontext->si_regs.pc; 1918 #endif 1919 #endif 1920 u32 instr = *(u32 *)pc; 1921 return (instr >> 21) & 1 ? WRITE: READ; 1922 #elif defined(__riscv) 1923 #if SANITIZER_FREEBSD 1924 unsigned long pc = ucontext->uc_mcontext.mc_gpregs.gp_sepc; 1925 #else 1926 unsigned long pc = ucontext->uc_mcontext.__gregs[REG_PC]; 1927 #endif 1928 unsigned faulty_instruction = *(uint16_t *)pc; 1929 1930 #if defined(__riscv_compressed) 1931 if ((faulty_instruction & 0x3) != 0x3) { // it's a compressed instruction 1932 // set op_bits to the instruction bits [1, 0, 15, 14, 13] 1933 unsigned op_bits = 1934 ((faulty_instruction & 0x3) << 3) | (faulty_instruction >> 13); 1935 unsigned rd = faulty_instruction & 0xF80; // bits 7-11, inclusive 1936 switch (op_bits) { 1937 case 0b10'010: // c.lwsp (rd != x0) 1938 #if __riscv_xlen == 64 1939 case 0b10'011: // c.ldsp (rd != x0) 1940 #endif 1941 return rd ? SignalContext::READ : SignalContext::UNKNOWN; 1942 case 0b00'010: // c.lw 1943 #if __riscv_flen >= 32 && __riscv_xlen == 32 1944 case 0b10'011: // c.flwsp 1945 #endif 1946 #if __riscv_flen >= 32 || __riscv_xlen == 64 1947 case 0b00'011: // c.flw / c.ld 1948 #endif 1949 #if __riscv_flen == 64 1950 case 0b00'001: // c.fld 1951 case 0b10'001: // c.fldsp 1952 #endif 1953 return SignalContext::READ; 1954 case 0b00'110: // c.sw 1955 case 0b10'110: // c.swsp 1956 #if __riscv_flen >= 32 || __riscv_xlen == 64 1957 case 0b00'111: // c.fsw / c.sd 1958 case 0b10'111: // c.fswsp / c.sdsp 1959 #endif 1960 #if __riscv_flen == 64 1961 case 0b00'101: // c.fsd 1962 case 0b10'101: // c.fsdsp 1963 #endif 1964 return SignalContext::WRITE; 1965 default: 1966 return SignalContext::UNKNOWN; 1967 } 1968 } 1969 #endif 1970 1971 unsigned opcode = faulty_instruction & 0x7f; // lower 7 bits 1972 unsigned funct3 = (faulty_instruction >> 12) & 0x7; // bits 12-14, inclusive 1973 switch (opcode) { 1974 case 0b0000011: // loads 1975 switch (funct3) { 1976 case 0b000: // lb 1977 case 0b001: // lh 1978 case 0b010: // lw 1979 #if __riscv_xlen == 64 1980 case 0b011: // ld 1981 #endif 1982 case 0b100: // lbu 1983 case 0b101: // lhu 1984 return SignalContext::READ; 1985 default: 1986 return SignalContext::UNKNOWN; 1987 } 1988 case 0b0100011: // stores 1989 switch (funct3) { 1990 case 0b000: // sb 1991 case 0b001: // sh 1992 case 0b010: // sw 1993 #if __riscv_xlen == 64 1994 case 0b011: // sd 1995 #endif 1996 return SignalContext::WRITE; 1997 default: 1998 return SignalContext::UNKNOWN; 1999 } 2000 #if __riscv_flen >= 32 2001 case 0b0000111: // floating-point loads 2002 switch (funct3) { 2003 case 0b010: // flw 2004 #if __riscv_flen == 64 2005 case 0b011: // fld 2006 #endif 2007 return SignalContext::READ; 2008 default: 2009 return SignalContext::UNKNOWN; 2010 } 2011 case 0b0100111: // floating-point stores 2012 switch (funct3) { 2013 case 0b010: // fsw 2014 #if __riscv_flen == 64 2015 case 0b011: // fsd 2016 #endif 2017 return SignalContext::WRITE; 2018 default: 2019 return SignalContext::UNKNOWN; 2020 } 2021 #endif 2022 default: 2023 return SignalContext::UNKNOWN; 2024 } 2025 #else 2026 (void)ucontext; 2027 return UNKNOWN; // FIXME: Implement. 2028 #endif 2029 } 2030 2031 bool SignalContext::IsTrueFaultingAddress() const { 2032 auto si = static_cast<const siginfo_t *>(siginfo); 2033 // SIGSEGV signals without a true fault address have si_code set to 128. 2034 return si->si_signo == SIGSEGV && si->si_code != 128; 2035 } 2036 2037 void SignalContext::DumpAllRegisters(void *context) { 2038 // FIXME: Implement this. 2039 } 2040 2041 static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { 2042 #if SANITIZER_NETBSD 2043 // This covers all NetBSD architectures 2044 ucontext_t *ucontext = (ucontext_t *)context; 2045 *pc = _UC_MACHINE_PC(ucontext); 2046 *bp = _UC_MACHINE_FP(ucontext); 2047 *sp = _UC_MACHINE_SP(ucontext); 2048 #elif defined(__arm__) 2049 ucontext_t *ucontext = (ucontext_t*)context; 2050 *pc = ucontext->uc_mcontext.arm_pc; 2051 *bp = ucontext->uc_mcontext.arm_fp; 2052 *sp = ucontext->uc_mcontext.arm_sp; 2053 #elif defined(__aarch64__) 2054 ucontext_t *ucontext = (ucontext_t*)context; 2055 *pc = ucontext->uc_mcontext.pc; 2056 *bp = ucontext->uc_mcontext.regs[29]; 2057 *sp = ucontext->uc_mcontext.sp; 2058 #elif defined(__hppa__) 2059 ucontext_t *ucontext = (ucontext_t*)context; 2060 *pc = ucontext->uc_mcontext.sc_iaoq[0]; 2061 /* GCC uses %r3 whenever a frame pointer is needed. */ 2062 *bp = ucontext->uc_mcontext.sc_gr[3]; 2063 *sp = ucontext->uc_mcontext.sc_gr[30]; 2064 #elif defined(__x86_64__) 2065 # if SANITIZER_FREEBSD 2066 ucontext_t *ucontext = (ucontext_t*)context; 2067 *pc = ucontext->uc_mcontext.mc_rip; 2068 *bp = ucontext->uc_mcontext.mc_rbp; 2069 *sp = ucontext->uc_mcontext.mc_rsp; 2070 # else 2071 ucontext_t *ucontext = (ucontext_t*)context; 2072 *pc = ucontext->uc_mcontext.gregs[REG_RIP]; 2073 *bp = ucontext->uc_mcontext.gregs[REG_RBP]; 2074 *sp = ucontext->uc_mcontext.gregs[REG_RSP]; 2075 # endif 2076 #elif defined(__i386__) 2077 # if SANITIZER_FREEBSD 2078 ucontext_t *ucontext = (ucontext_t*)context; 2079 *pc = ucontext->uc_mcontext.mc_eip; 2080 *bp = ucontext->uc_mcontext.mc_ebp; 2081 *sp = ucontext->uc_mcontext.mc_esp; 2082 # else 2083 ucontext_t *ucontext = (ucontext_t*)context; 2084 # if SANITIZER_SOLARIS 2085 /* Use the numeric values: the symbolic ones are undefined by llvm 2086 include/llvm/Support/Solaris.h. */ 2087 # ifndef REG_EIP 2088 # define REG_EIP 14 // REG_PC 2089 # endif 2090 # ifndef REG_EBP 2091 # define REG_EBP 6 // REG_FP 2092 # endif 2093 # ifndef REG_UESP 2094 # define REG_UESP 17 // REG_SP 2095 # endif 2096 # endif 2097 *pc = ucontext->uc_mcontext.gregs[REG_EIP]; 2098 *bp = ucontext->uc_mcontext.gregs[REG_EBP]; 2099 *sp = ucontext->uc_mcontext.gregs[REG_UESP]; 2100 # endif 2101 #elif defined(__powerpc__) || defined(__powerpc64__) 2102 ucontext_t *ucontext = (ucontext_t*)context; 2103 *pc = ucontext->uc_mcontext.regs->nip; 2104 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1]; 2105 // The powerpc{,64}-linux ABIs do not specify r31 as the frame 2106 // pointer, but GCC always uses r31 when we need a frame pointer. 2107 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31]; 2108 #elif defined(__sparc__) 2109 #if defined(__arch64__) || defined(__sparcv9) 2110 #define STACK_BIAS 2047 2111 #else 2112 #define STACK_BIAS 0 2113 # endif 2114 # if SANITIZER_SOLARIS 2115 ucontext_t *ucontext = (ucontext_t *)context; 2116 *pc = ucontext->uc_mcontext.gregs[REG_PC]; 2117 *sp = ucontext->uc_mcontext.gregs[REG_O6] + STACK_BIAS; 2118 #else 2119 // Historical BSDism here. 2120 struct sigcontext *scontext = (struct sigcontext *)context; 2121 #if defined(__arch64__) 2122 *pc = scontext->sigc_regs.tpc; 2123 *sp = scontext->sigc_regs.u_regs[14] + STACK_BIAS; 2124 #else 2125 *pc = scontext->si_regs.pc; 2126 *sp = scontext->si_regs.u_regs[14]; 2127 #endif 2128 # endif 2129 *bp = (uptr)((uhwptr *)*sp)[14] + STACK_BIAS; 2130 #elif defined(__mips__) 2131 ucontext_t *ucontext = (ucontext_t*)context; 2132 *pc = ucontext->uc_mcontext.pc; 2133 *bp = ucontext->uc_mcontext.gregs[30]; 2134 *sp = ucontext->uc_mcontext.gregs[29]; 2135 #elif defined(__s390__) 2136 ucontext_t *ucontext = (ucontext_t*)context; 2137 # if defined(__s390x__) 2138 *pc = ucontext->uc_mcontext.psw.addr; 2139 # else 2140 *pc = ucontext->uc_mcontext.psw.addr & 0x7fffffff; 2141 # endif 2142 *bp = ucontext->uc_mcontext.gregs[11]; 2143 *sp = ucontext->uc_mcontext.gregs[15]; 2144 #elif defined(__riscv) 2145 ucontext_t *ucontext = (ucontext_t*)context; 2146 # if SANITIZER_FREEBSD 2147 *pc = ucontext->uc_mcontext.mc_gpregs.gp_sepc; 2148 *bp = ucontext->uc_mcontext.mc_gpregs.gp_s[0]; 2149 *sp = ucontext->uc_mcontext.mc_gpregs.gp_sp; 2150 # else 2151 *pc = ucontext->uc_mcontext.__gregs[REG_PC]; 2152 *bp = ucontext->uc_mcontext.__gregs[REG_S0]; 2153 *sp = ucontext->uc_mcontext.__gregs[REG_SP]; 2154 # endif 2155 #else 2156 # error "Unsupported arch" 2157 #endif 2158 } 2159 2160 void SignalContext::InitPcSpBp() { GetPcSpBp(context, &pc, &sp, &bp); } 2161 2162 void InitializePlatformEarly() { 2163 // Do nothing. 2164 } 2165 2166 void MaybeReexec() { 2167 // No need to re-exec on Linux. 2168 } 2169 2170 void CheckASLR() { 2171 #if SANITIZER_NETBSD 2172 int mib[3]; 2173 int paxflags; 2174 uptr len = sizeof(paxflags); 2175 2176 mib[0] = CTL_PROC; 2177 mib[1] = internal_getpid(); 2178 mib[2] = PROC_PID_PAXFLAGS; 2179 2180 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) { 2181 Printf("sysctl failed\n"); 2182 Die(); 2183 } 2184 2185 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_ASLR)) { 2186 Printf("This sanitizer is not compatible with enabled ASLR.\n" 2187 "To disable ASLR, please run \"paxctl +a %s\" and try again.\n", 2188 GetArgv()[0]); 2189 Die(); 2190 } 2191 #elif SANITIZER_PPC64V2 2192 // Disable ASLR for Linux PPC64LE. 2193 int old_personality = personality(0xffffffff); 2194 if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) { 2195 VReport(1, "WARNING: Program is being run with address space layout " 2196 "randomization (ASLR) enabled which prevents the thread and " 2197 "memory sanitizers from working on powerpc64le.\n" 2198 "ASLR will be disabled and the program re-executed.\n"); 2199 CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1); 2200 ReExec(); 2201 } 2202 #elif SANITIZER_FREEBSD 2203 int aslr_status; 2204 if (UNLIKELY(procctl(P_PID, 0, PROC_ASLR_STATUS, &aslr_status) == -1)) { 2205 // We're making things less 'dramatic' here since 2206 // the cmd is not necessarily guaranteed to be here 2207 // just yet regarding FreeBSD release 2208 return; 2209 } 2210 if ((aslr_status & PROC_ASLR_ACTIVE) != 0) { 2211 Printf("This sanitizer is not compatible with enabled ASLR " 2212 "and binaries compiled with PIE\n" 2213 "ASLR will be disabled and the program re-executed.\n"); 2214 int aslr_ctl = PROC_ASLR_FORCE_DISABLE; 2215 CHECK_NE(procctl(P_PID, 0, PROC_ASLR_CTL, &aslr_ctl), -1); 2216 ReExec(); 2217 } 2218 #else 2219 // Do nothing 2220 #endif 2221 } 2222 2223 void CheckMPROTECT() { 2224 #if SANITIZER_NETBSD 2225 int mib[3]; 2226 int paxflags; 2227 uptr len = sizeof(paxflags); 2228 2229 mib[0] = CTL_PROC; 2230 mib[1] = internal_getpid(); 2231 mib[2] = PROC_PID_PAXFLAGS; 2232 2233 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) { 2234 Printf("sysctl failed\n"); 2235 Die(); 2236 } 2237 2238 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_MPROTECT)) { 2239 Printf("This sanitizer is not compatible with enabled MPROTECT\n"); 2240 Die(); 2241 } 2242 #else 2243 // Do nothing 2244 #endif 2245 } 2246 2247 void CheckNoDeepBind(const char *filename, int flag) { 2248 #ifdef RTLD_DEEPBIND 2249 if (flag & RTLD_DEEPBIND) { 2250 Report( 2251 "You are trying to dlopen a %s shared library with RTLD_DEEPBIND flag" 2252 " which is incompatible with sanitizer runtime " 2253 "(see https://github.com/google/sanitizers/issues/611 for details" 2254 "). If you want to run %s library under sanitizers please remove " 2255 "RTLD_DEEPBIND from dlopen flags.\n", 2256 filename, filename); 2257 Die(); 2258 } 2259 #endif 2260 } 2261 2262 uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding, 2263 uptr *largest_gap_found, 2264 uptr *max_occupied_addr) { 2265 UNREACHABLE("FindAvailableMemoryRange is not available"); 2266 return 0; 2267 } 2268 2269 bool GetRandom(void *buffer, uptr length, bool blocking) { 2270 if (!buffer || !length || length > 256) 2271 return false; 2272 #if SANITIZER_USE_GETENTROPY 2273 uptr rnd = getentropy(buffer, length); 2274 int rverrno = 0; 2275 if (internal_iserror(rnd, &rverrno) && rverrno == EFAULT) 2276 return false; 2277 else if (rnd == 0) 2278 return true; 2279 #endif // SANITIZER_USE_GETENTROPY 2280 2281 #if SANITIZER_USE_GETRANDOM 2282 static atomic_uint8_t skip_getrandom_syscall; 2283 if (!atomic_load_relaxed(&skip_getrandom_syscall)) { 2284 // Up to 256 bytes, getrandom will not be interrupted. 2285 uptr res = internal_syscall(SYSCALL(getrandom), buffer, length, 2286 blocking ? 0 : GRND_NONBLOCK); 2287 int rverrno = 0; 2288 if (internal_iserror(res, &rverrno) && rverrno == ENOSYS) 2289 atomic_store_relaxed(&skip_getrandom_syscall, 1); 2290 else if (res == length) 2291 return true; 2292 } 2293 #endif // SANITIZER_USE_GETRANDOM 2294 // Up to 256 bytes, a read off /dev/urandom will not be interrupted. 2295 // blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom. 2296 uptr fd = internal_open("/dev/urandom", O_RDONLY); 2297 if (internal_iserror(fd)) 2298 return false; 2299 uptr res = internal_read(fd, buffer, length); 2300 if (internal_iserror(res)) 2301 return false; 2302 internal_close(fd); 2303 return true; 2304 } 2305 2306 } // namespace __sanitizer 2307 2308 #endif 2309