1 //===-- sanitizer_linux.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries and implements linux-specific functions from
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
17 SANITIZER_OPENBSD || SANITIZER_SOLARIS
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"
30 #include <asm/param.h>
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.
38 #include <asm/unistd.h>
39 #include <sys/types.h>
40 #define stat kernel_stat
53 #include <sys/param.h>
54 #if !SANITIZER_SOLARIS
55 #include <sys/ptrace.h>
57 #include <sys/resource.h>
59 #include <sys/syscall.h>
61 #include <sys/types.h>
62 #if !SANITIZER_OPENBSD
66 #include <sys/futex.h>
67 #include <sys/sysctl.h>
72 #include <sys/utsname.h>
75 #if SANITIZER_LINUX && !SANITIZER_ANDROID
76 #include <sys/personality.h>
81 #include <sys/sysctl.h>
82 #include <machine/atomic.h>
84 // <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
85 // FreeBSD 9.2 and 10.0.
89 #endif // SANITIZER_FREEBSD
92 #include <limits.h> // For NAME_MAX
93 #include <sys/sysctl.h>
95 extern struct ps_strings
*__ps_strings
;
96 #endif // SANITIZER_NETBSD
101 #define environ _environ
104 extern char **environ
;
108 struct kernel_timeval
{
113 // <linux/futex.h> is broken on some linux distributions.
114 const int FUTEX_WAIT
= 0;
115 const int FUTEX_WAKE
= 1;
116 const int FUTEX_PRIVATE_FLAG
= 128;
117 const int FUTEX_WAIT_PRIVATE
= FUTEX_WAIT
| FUTEX_PRIVATE_FLAG
;
118 const int FUTEX_WAKE_PRIVATE
= FUTEX_WAKE
| FUTEX_PRIVATE_FLAG
;
119 #endif // SANITIZER_LINUX
121 // Are we using 32-bit or 64-bit Linux syscalls?
122 // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
123 // but it still needs to use 64-bit syscalls.
124 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__powerpc64__) || \
125 SANITIZER_WORDSIZE == 64)
126 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
128 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
131 // Note : FreeBSD had implemented both
132 // Linux and OpenBSD apis, available from
133 // future 12.x version most likely
134 #if SANITIZER_LINUX && defined(__NR_getrandom)
135 # if !defined(GRND_NONBLOCK)
136 # define GRND_NONBLOCK 1
138 # define SANITIZER_USE_GETRANDOM 1
140 # define SANITIZER_USE_GETRANDOM 0
141 #endif // SANITIZER_LINUX && defined(__NR_getrandom)
143 #if SANITIZER_OPENBSD
144 # define SANITIZER_USE_GETENTROPY 1
146 # if SANITIZER_FREEBSD && __FreeBSD_version >= 1200000
147 # define SANITIZER_USE_GETENTROPY 1
149 # define SANITIZER_USE_GETENTROPY 0
151 #endif // SANITIZER_USE_GETENTROPY
153 namespace __sanitizer
{
155 #if SANITIZER_LINUX && defined(__x86_64__)
156 #include "sanitizer_syscall_linux_x86_64.inc"
157 #elif SANITIZER_LINUX && defined(__aarch64__)
158 #include "sanitizer_syscall_linux_aarch64.inc"
159 #elif SANITIZER_LINUX && defined(__arm__)
160 #include "sanitizer_syscall_linux_arm.inc"
162 #include "sanitizer_syscall_generic.inc"
165 // --------------- sanitizer_libc.h
166 #if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
167 #if !SANITIZER_S390 && !SANITIZER_OPENBSD
168 uptr
internal_mmap(void *addr
, uptr length
, int prot
, int flags
, int fd
,
170 #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
171 return internal_syscall(SYSCALL(mmap
), (uptr
)addr
, length
, prot
, flags
, fd
,
174 // mmap2 specifies file offset in 4096-byte units.
175 CHECK(IsAligned(offset
, 4096));
176 return internal_syscall(SYSCALL(mmap2
), addr
, length
, prot
, flags
, fd
,
180 #endif // !SANITIZER_S390 && !SANITIZER_OPENBSD
182 #if !SANITIZER_OPENBSD
183 uptr
internal_munmap(void *addr
, uptr length
) {
184 return internal_syscall(SYSCALL(munmap
), (uptr
)addr
, length
);
187 int internal_mprotect(void *addr
, uptr length
, int prot
) {
188 return internal_syscall(SYSCALL(mprotect
), (uptr
)addr
, length
, prot
);
192 uptr
internal_close(fd_t fd
) {
193 return internal_syscall(SYSCALL(close
), fd
);
196 uptr
internal_open(const char *filename
, int flags
) {
197 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
198 return internal_syscall(SYSCALL(openat
), AT_FDCWD
, (uptr
)filename
, flags
);
200 return internal_syscall(SYSCALL(open
), (uptr
)filename
, flags
);
204 uptr
internal_open(const char *filename
, int flags
, u32 mode
) {
205 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
206 return internal_syscall(SYSCALL(openat
), AT_FDCWD
, (uptr
)filename
, flags
,
209 return internal_syscall(SYSCALL(open
), (uptr
)filename
, flags
, mode
);
213 uptr
internal_read(fd_t fd
, void *buf
, uptr count
) {
216 (sptr
)internal_syscall(SYSCALL(read
), fd
, (uptr
)buf
, count
));
220 uptr
internal_write(fd_t fd
, const void *buf
, uptr count
) {
223 (sptr
)internal_syscall(SYSCALL(write
), fd
, (uptr
)buf
, count
));
227 uptr
internal_ftruncate(fd_t fd
, uptr size
) {
229 HANDLE_EINTR(res
, (sptr
)internal_syscall(SYSCALL(ftruncate
), fd
,
234 #if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && SANITIZER_LINUX
235 static void stat64_to_stat(struct stat64
*in
, struct stat
*out
) {
236 internal_memset(out
, 0, sizeof(*out
));
237 out
->st_dev
= in
->st_dev
;
238 out
->st_ino
= in
->st_ino
;
239 out
->st_mode
= in
->st_mode
;
240 out
->st_nlink
= in
->st_nlink
;
241 out
->st_uid
= in
->st_uid
;
242 out
->st_gid
= in
->st_gid
;
243 out
->st_rdev
= in
->st_rdev
;
244 out
->st_size
= in
->st_size
;
245 out
->st_blksize
= in
->st_blksize
;
246 out
->st_blocks
= in
->st_blocks
;
247 out
->st_atime
= in
->st_atime
;
248 out
->st_mtime
= in
->st_mtime
;
249 out
->st_ctime
= in
->st_ctime
;
253 #if defined(__mips64)
254 // Undefine compatibility macros from <sys/stat.h>
255 // so that they would not clash with the kernel_stat
256 // st_[a|m|c]time fields
260 #if defined(SANITIZER_ANDROID)
261 // Bionic sys/stat.h defines additional macros
262 // for compatibility with the old NDKs and
263 // they clash with the kernel_stat structure
264 // st_[a|m|c]time_nsec fields.
269 static void kernel_stat_to_stat(struct kernel_stat
*in
, struct stat
*out
) {
270 internal_memset(out
, 0, sizeof(*out
));
271 out
->st_dev
= in
->st_dev
;
272 out
->st_ino
= in
->st_ino
;
273 out
->st_mode
= in
->st_mode
;
274 out
->st_nlink
= in
->st_nlink
;
275 out
->st_uid
= in
->st_uid
;
276 out
->st_gid
= in
->st_gid
;
277 out
->st_rdev
= in
->st_rdev
;
278 out
->st_size
= in
->st_size
;
279 out
->st_blksize
= in
->st_blksize
;
280 out
->st_blocks
= in
->st_blocks
;
281 #if defined(__USE_MISC) || \
282 defined(__USE_XOPEN2K8) || \
283 defined(SANITIZER_ANDROID)
284 out
->st_atim
.tv_sec
= in
->st_atime
;
285 out
->st_atim
.tv_nsec
= in
->st_atime_nsec
;
286 out
->st_mtim
.tv_sec
= in
->st_mtime
;
287 out
->st_mtim
.tv_nsec
= in
->st_mtime_nsec
;
288 out
->st_ctim
.tv_sec
= in
->st_ctime
;
289 out
->st_ctim
.tv_nsec
= in
->st_ctime_nsec
;
291 out
->st_atime
= in
->st_atime
;
292 out
->st_atimensec
= in
->st_atime_nsec
;
293 out
->st_mtime
= in
->st_mtime
;
294 out
->st_mtimensec
= in
->st_mtime_nsec
;
295 out
->st_ctime
= in
->st_ctime
;
296 out
->st_atimensec
= in
->st_ctime_nsec
;
301 uptr
internal_stat(const char *path
, void *buf
) {
302 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
303 return internal_syscall(SYSCALL(fstatat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
, 0);
304 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
305 return internal_syscall(SYSCALL(newfstatat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
,
307 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
308 # if defined(__mips64)
309 // For mips64, stat syscall fills buffer in the format of kernel_stat
310 struct kernel_stat kbuf
;
311 int res
= internal_syscall(SYSCALL(stat
), path
, &kbuf
);
312 kernel_stat_to_stat(&kbuf
, (struct stat
*)buf
);
315 return internal_syscall(SYSCALL(stat
), (uptr
)path
, (uptr
)buf
);
319 int res
= internal_syscall(SYSCALL(stat64
), path
, &buf64
);
320 stat64_to_stat(&buf64
, (struct stat
*)buf
);
325 uptr
internal_lstat(const char *path
, void *buf
) {
326 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
327 return internal_syscall(SYSCALL(fstatat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
,
328 AT_SYMLINK_NOFOLLOW
);
329 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
330 return internal_syscall(SYSCALL(newfstatat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
,
331 AT_SYMLINK_NOFOLLOW
);
332 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
333 # if SANITIZER_MIPS64
334 // For mips64, lstat syscall fills buffer in the format of kernel_stat
335 struct kernel_stat kbuf
;
336 int res
= internal_syscall(SYSCALL(lstat
), path
, &kbuf
);
337 kernel_stat_to_stat(&kbuf
, (struct stat
*)buf
);
340 return internal_syscall(SYSCALL(lstat
), (uptr
)path
, (uptr
)buf
);
344 int res
= internal_syscall(SYSCALL(lstat64
), path
, &buf64
);
345 stat64_to_stat(&buf64
, (struct stat
*)buf
);
350 uptr
internal_fstat(fd_t fd
, void *buf
) {
351 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD || \
352 SANITIZER_LINUX_USES_64BIT_SYSCALLS
353 #if SANITIZER_MIPS64 && !SANITIZER_OPENBSD
354 // For mips64, fstat syscall fills buffer in the format of kernel_stat
355 struct kernel_stat kbuf
;
356 int res
= internal_syscall(SYSCALL(fstat
), fd
, &kbuf
);
357 kernel_stat_to_stat(&kbuf
, (struct stat
*)buf
);
360 return internal_syscall(SYSCALL(fstat
), fd
, (uptr
)buf
);
364 int res
= internal_syscall(SYSCALL(fstat64
), fd
, &buf64
);
365 stat64_to_stat(&buf64
, (struct stat
*)buf
);
370 uptr
internal_filesize(fd_t fd
) {
372 if (internal_fstat(fd
, &st
))
374 return (uptr
)st
.st_size
;
377 uptr
internal_dup(int oldfd
) {
378 return internal_syscall(SYSCALL(dup
), oldfd
);
381 uptr
internal_dup2(int oldfd
, int newfd
) {
382 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
383 return internal_syscall(SYSCALL(dup3
), oldfd
, newfd
, 0);
385 return internal_syscall(SYSCALL(dup2
), oldfd
, newfd
);
389 uptr
internal_readlink(const char *path
, char *buf
, uptr bufsize
) {
390 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
391 return internal_syscall(SYSCALL(readlinkat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
,
393 #elif SANITIZER_OPENBSD
394 return internal_syscall(SYSCALL(readlinkat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
,
397 return internal_syscall(SYSCALL(readlink
), (uptr
)path
, (uptr
)buf
, bufsize
);
401 uptr
internal_unlink(const char *path
) {
402 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS || SANITIZER_OPENBSD
403 return internal_syscall(SYSCALL(unlinkat
), AT_FDCWD
, (uptr
)path
, 0);
405 return internal_syscall(SYSCALL(unlink
), (uptr
)path
);
409 uptr
internal_rename(const char *oldpath
, const char *newpath
) {
410 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS || SANITIZER_OPENBSD
411 return internal_syscall(SYSCALL(renameat
), AT_FDCWD
, (uptr
)oldpath
, AT_FDCWD
,
414 return internal_syscall(SYSCALL(rename
), (uptr
)oldpath
, (uptr
)newpath
);
418 uptr
internal_sched_yield() {
419 return internal_syscall(SYSCALL(sched_yield
));
422 void internal__exit(int exitcode
) {
423 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
424 internal_syscall(SYSCALL(exit
), exitcode
);
426 internal_syscall(SYSCALL(exit_group
), exitcode
);
428 Die(); // Unreachable.
431 unsigned int internal_sleep(unsigned int seconds
) {
435 int res
= internal_syscall(SYSCALL(nanosleep
), &ts
, &ts
);
436 if (res
) return ts
.tv_sec
;
440 uptr
internal_execve(const char *filename
, char *const argv
[],
441 char *const envp
[]) {
442 return internal_syscall(SYSCALL(execve
), (uptr
)filename
, (uptr
)argv
,
445 #endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD
447 // ----------------- sanitizer_common.h
448 bool FileExists(const char *filename
) {
449 if (ShouldMockFailureToOpen(filename
))
452 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
453 if (internal_syscall(SYSCALL(newfstatat
), AT_FDCWD
, filename
, &st
, 0))
455 if (internal_stat(filename
, &st
))
458 // Sanity check: filename is a regular file.
459 return S_ISREG(st
.st_mode
);
462 #if !SANITIZER_NETBSD
464 #if SANITIZER_FREEBSD
468 #elif SANITIZER_OPENBSD
469 return internal_syscall(SYSCALL(getthrid
));
470 #elif SANITIZER_SOLARIS
473 return internal_syscall(SYSCALL(gettid
));
477 int TgKill(pid_t pid
, tid_t tid
, int sig
) {
479 return internal_syscall(SYSCALL(tgkill
), pid
, tid
, sig
);
480 #elif SANITIZER_FREEBSD
481 return internal_syscall(SYSCALL(thr_kill2
), pid
, tid
, sig
);
482 #elif SANITIZER_OPENBSD
484 return internal_syscall(SYSCALL(thrkill
), tid
, sig
, nullptr);
485 #elif SANITIZER_SOLARIS
487 return thr_kill(tid
, sig
);
492 #if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
494 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
499 internal_memset(&tv
, 0, sizeof(tv
));
500 internal_syscall(SYSCALL(gettimeofday
), &tv
, 0);
501 return (u64
)tv
.tv_sec
* 1000*1000*1000 + tv
.tv_usec
* 1000;
504 uptr
internal_clock_gettime(__sanitizer_clockid_t clk_id
, void *tp
) {
505 return internal_syscall(SYSCALL(clock_gettime
), clk_id
, tp
);
507 #endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD
509 // Like getenv, but reads env directly from /proc (on Linux) or parses the
510 // 'environ' array (on some others) and does not use libc. This function
511 // should be called first inside __asan_init.
512 const char *GetEnv(const char *name
) {
513 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD || \
515 if (::environ
!= 0) {
516 uptr NameLen
= internal_strlen(name
);
517 for (char **Env
= ::environ
; *Env
!= 0; Env
++) {
518 if (internal_strncmp(*Env
, name
, NameLen
) == 0 && (*Env
)[NameLen
] == '=')
519 return (*Env
) + NameLen
+ 1;
522 return 0; // Not found.
523 #elif SANITIZER_LINUX
524 static char *environ
;
530 if (!ReadFileToBuffer("/proc/self/environ", &environ
, &environ_size
, &len
))
533 if (!environ
|| len
== 0) return nullptr;
534 uptr namelen
= internal_strlen(name
);
535 const char *p
= environ
;
536 while (*p
!= '\0') { // will happen at the \0\0 that terminates the buffer
537 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
539 (char*)internal_memchr(p
, '\0', len
- (p
- environ
));
540 if (!endp
) // this entry isn't NUL terminated
542 else if (!internal_memcmp(p
, name
, namelen
) && p
[namelen
] == '=') // Match.
543 return p
+ namelen
+ 1; // point after =
546 return nullptr; // Not found.
548 #error "Unsupported platform"
552 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD && !SANITIZER_OPENBSD
554 SANITIZER_WEAK_ATTRIBUTE
extern void *__libc_stack_end
;
558 #if !SANITIZER_GO && !SANITIZER_FREEBSD && !SANITIZER_NETBSD && \
560 static void ReadNullSepFileToArray(const char *path
, char ***arr
,
565 *arr
= (char **)MmapOrDie(arr_size
* sizeof(char *), "NullSepFileArray");
566 if (!ReadFileToBuffer(path
, &buff
, &buff_size
, &buff_len
, 1024 * 1024)) {
572 for (count
= 1, i
= 1; ; i
++) {
574 if (buff
[i
+1] == 0) break;
575 (*arr
)[count
] = &buff
[i
+1];
576 CHECK_LE(count
, arr_size
- 1); // FIXME: make this more flexible.
580 (*arr
)[count
] = nullptr;
584 #if !SANITIZER_OPENBSD
585 static void GetArgsAndEnv(char ***argv
, char ***envp
) {
586 #if SANITIZER_FREEBSD
587 // On FreeBSD, retrieving the argument and environment arrays is done via the
588 // kern.ps_strings sysctl, which returns a pointer to a structure containing
589 // this information. See also <sys/exec.h>.
591 uptr sz
= sizeof(pss
);
592 if (internal_sysctlbyname("kern.ps_strings", &pss
, &sz
, NULL
, 0) == -1) {
593 Printf("sysctl kern.ps_strings failed\n");
596 *argv
= pss
->ps_argvstr
;
597 *envp
= pss
->ps_envstr
;
598 #elif SANITIZER_NETBSD
599 *argv
= __ps_strings
->ps_argvstr
;
600 *envp
= __ps_strings
->ps_envstr
;
601 #else // SANITIZER_FREEBSD
603 if (&__libc_stack_end
) {
604 #endif // !SANITIZER_GO
605 uptr
* stack_end
= (uptr
*)__libc_stack_end
;
606 int argc
= *stack_end
;
607 *argv
= (char**)(stack_end
+ 1);
608 *envp
= (char**)(stack_end
+ argc
+ 2);
611 static const int kMaxArgv
= 2000, kMaxEnvp
= 2000;
612 ReadNullSepFileToArray("/proc/self/cmdline", argv
, kMaxArgv
);
613 ReadNullSepFileToArray("/proc/self/environ", envp
, kMaxEnvp
);
615 #endif // !SANITIZER_GO
616 #endif // SANITIZER_FREEBSD
621 GetArgsAndEnv(&argv
, &envp
);
625 char **GetEnviron() {
627 GetArgsAndEnv(&argv
, &envp
);
631 #endif // !SANITIZER_OPENBSD
633 #if !SANITIZER_SOLARIS
640 BlockingMutex::BlockingMutex() {
641 internal_memset(this, 0, sizeof(*this));
644 void BlockingMutex::Lock() {
646 atomic_uint32_t
*m
= reinterpret_cast<atomic_uint32_t
*>(&opaque_storage_
);
647 if (atomic_exchange(m
, MtxLocked
, memory_order_acquire
) == MtxUnlocked
)
649 while (atomic_exchange(m
, MtxSleeping
, memory_order_acquire
) != MtxUnlocked
) {
650 #if SANITIZER_FREEBSD
651 _umtx_op(m
, UMTX_OP_WAIT_UINT
, MtxSleeping
, 0, 0);
652 #elif SANITIZER_NETBSD
653 sched_yield(); /* No userspace futex-like synchronization */
655 internal_syscall(SYSCALL(futex
), (uptr
)m
, FUTEX_WAIT_PRIVATE
, MtxSleeping
,
661 void BlockingMutex::Unlock() {
662 atomic_uint32_t
*m
= reinterpret_cast<atomic_uint32_t
*>(&opaque_storage_
);
663 u32 v
= atomic_exchange(m
, MtxUnlocked
, memory_order_release
);
664 CHECK_NE(v
, MtxUnlocked
);
665 if (v
== MtxSleeping
) {
666 #if SANITIZER_FREEBSD
667 _umtx_op(m
, UMTX_OP_WAKE
, 1, 0, 0);
668 #elif SANITIZER_NETBSD
669 /* No userspace futex-like synchronization */
671 internal_syscall(SYSCALL(futex
), (uptr
)m
, FUTEX_WAKE_PRIVATE
, 1, 0, 0, 0);
676 void BlockingMutex::CheckLocked() {
677 atomic_uint32_t
*m
= reinterpret_cast<atomic_uint32_t
*>(&opaque_storage_
);
678 CHECK_NE(MtxUnlocked
, atomic_load(m
, memory_order_relaxed
));
680 #endif // !SANITIZER_SOLARIS
682 // ----------------- sanitizer_linux.h
683 // The actual size of this structure is specified by d_reclen.
684 // Note that getdents64 uses a different structure format. We only provide the
685 // 32-bit syscall here.
688 #elif SANITIZER_OPENBSD
689 // struct dirent is different for Linux and us. At this moment, we use only
690 // d_fileno (Linux call this d_ino), d_reclen, and d_name.
691 struct linux_dirent
{
692 u64 d_ino
; // d_fileno
694 u16 d_namlen
; // not used
695 u8 d_type
; // not used
696 char d_name
[NAME_MAX
+ 1];
699 struct linux_dirent
{
700 #if SANITIZER_X32 || defined(__aarch64__)
707 unsigned short d_reclen
;
709 unsigned char d_type
;
715 #if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
717 uptr
internal_ptrace(int request
, int pid
, void *addr
, void *data
) {
718 return internal_syscall(SYSCALL(ptrace
), request
, pid
, (uptr
)addr
,
722 uptr
internal_waitpid(int pid
, int *status
, int options
) {
723 return internal_syscall(SYSCALL(wait4
), pid
, (uptr
)status
, options
,
727 uptr
internal_getpid() {
728 return internal_syscall(SYSCALL(getpid
));
731 uptr
internal_getppid() {
732 return internal_syscall(SYSCALL(getppid
));
735 uptr
internal_getdents(fd_t fd
, struct linux_dirent
*dirp
, unsigned int count
) {
736 #if SANITIZER_FREEBSD
737 return internal_syscall(SYSCALL(getdirentries
), fd
, (uptr
)dirp
, count
, NULL
);
738 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
739 return internal_syscall(SYSCALL(getdents64
), fd
, (uptr
)dirp
, count
);
741 return internal_syscall(SYSCALL(getdents
), fd
, (uptr
)dirp
, count
);
745 uptr
internal_lseek(fd_t fd
, OFF_T offset
, int whence
) {
746 return internal_syscall(SYSCALL(lseek
), fd
, offset
, whence
);
750 uptr
internal_prctl(int option
, uptr arg2
, uptr arg3
, uptr arg4
, uptr arg5
) {
751 return internal_syscall(SYSCALL(prctl
), option
, arg2
, arg3
, arg4
, arg5
);
755 uptr
internal_sigaltstack(const void *ss
, void *oss
) {
756 return internal_syscall(SYSCALL(sigaltstack
), (uptr
)ss
, (uptr
)oss
);
759 int internal_fork() {
760 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
761 return internal_syscall(SYSCALL(clone
), SIGCHLD
, 0);
763 return internal_syscall(SYSCALL(fork
));
767 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
768 int internal_sysctl(const int *name
, unsigned int namelen
, void *oldp
,
769 uptr
*oldlenp
, const void *newp
, uptr newlen
) {
770 #if SANITIZER_OPENBSD
771 return sysctl(name
, namelen
, oldp
, (size_t *)oldlenp
, (void *)newp
,
774 return internal_syscall(SYSCALL(__sysctl
), name
, namelen
, oldp
,
775 (size_t *)oldlenp
, newp
, (size_t)newlen
);
779 #if SANITIZER_FREEBSD
780 int internal_sysctlbyname(const char *sname
, void *oldp
, uptr
*oldlenp
,
781 const void *newp
, uptr newlen
) {
782 static decltype(sysctlbyname
) *real
= nullptr;
784 real
= (decltype(sysctlbyname
) *)dlsym(RTLD_NEXT
, "sysctlbyname");
786 return real(sname
, oldp
, (size_t *)oldlenp
, newp
, (size_t)newlen
);
792 #define SA_RESTORER 0x04000000
793 // Doesn't set sa_restorer if the caller did not set it, so use with caution
795 int internal_sigaction_norestorer(int signum
, const void *act
, void *oldact
) {
796 __sanitizer_kernel_sigaction_t k_act
, k_oldact
;
797 internal_memset(&k_act
, 0, sizeof(__sanitizer_kernel_sigaction_t
));
798 internal_memset(&k_oldact
, 0, sizeof(__sanitizer_kernel_sigaction_t
));
799 const __sanitizer_sigaction
*u_act
= (const __sanitizer_sigaction
*)act
;
800 __sanitizer_sigaction
*u_oldact
= (__sanitizer_sigaction
*)oldact
;
802 k_act
.handler
= u_act
->handler
;
803 k_act
.sigaction
= u_act
->sigaction
;
804 internal_memcpy(&k_act
.sa_mask
, &u_act
->sa_mask
,
805 sizeof(__sanitizer_kernel_sigset_t
));
806 // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL).
807 k_act
.sa_flags
= u_act
->sa_flags
| SA_RESTORER
;
808 // FIXME: most often sa_restorer is unset, however the kernel requires it
809 // to point to a valid signal restorer that calls the rt_sigreturn syscall.
810 // If sa_restorer passed to the kernel is NULL, the program may crash upon
811 // signal delivery or fail to unwind the stack in the signal handler.
812 // libc implementation of sigaction() passes its own restorer to
813 // rt_sigaction, so we need to do the same (we'll need to reimplement the
814 // restorers; for x86_64 the restorer address can be obtained from
815 // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
816 #if !SANITIZER_ANDROID || !SANITIZER_MIPS32
817 k_act
.sa_restorer
= u_act
->sa_restorer
;
821 uptr result
= internal_syscall(SYSCALL(rt_sigaction
), (uptr
)signum
,
822 (uptr
)(u_act
? &k_act
: nullptr),
823 (uptr
)(u_oldact
? &k_oldact
: nullptr),
824 (uptr
)sizeof(__sanitizer_kernel_sigset_t
));
826 if ((result
== 0) && u_oldact
) {
827 u_oldact
->handler
= k_oldact
.handler
;
828 u_oldact
->sigaction
= k_oldact
.sigaction
;
829 internal_memcpy(&u_oldact
->sa_mask
, &k_oldact
.sa_mask
,
830 sizeof(__sanitizer_kernel_sigset_t
));
831 u_oldact
->sa_flags
= k_oldact
.sa_flags
;
832 #if !SANITIZER_ANDROID || !SANITIZER_MIPS32
833 u_oldact
->sa_restorer
= k_oldact
.sa_restorer
;
838 #endif // SANITIZER_LINUX
840 uptr
internal_sigprocmask(int how
, __sanitizer_sigset_t
*set
,
841 __sanitizer_sigset_t
*oldset
) {
842 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
843 return internal_syscall(SYSCALL(sigprocmask
), how
, set
, oldset
);
845 __sanitizer_kernel_sigset_t
*k_set
= (__sanitizer_kernel_sigset_t
*)set
;
846 __sanitizer_kernel_sigset_t
*k_oldset
= (__sanitizer_kernel_sigset_t
*)oldset
;
847 return internal_syscall(SYSCALL(rt_sigprocmask
), (uptr
)how
,
848 (uptr
)&k_set
->sig
[0], (uptr
)&k_oldset
->sig
[0],
849 sizeof(__sanitizer_kernel_sigset_t
));
853 void internal_sigfillset(__sanitizer_sigset_t
*set
) {
854 internal_memset(set
, 0xff, sizeof(*set
));
857 void internal_sigemptyset(__sanitizer_sigset_t
*set
) {
858 internal_memset(set
, 0, sizeof(*set
));
862 void internal_sigdelset(__sanitizer_sigset_t
*set
, int signum
) {
865 CHECK_LT(signum
, sizeof(*set
) * 8);
866 __sanitizer_kernel_sigset_t
*k_set
= (__sanitizer_kernel_sigset_t
*)set
;
867 const uptr idx
= signum
/ (sizeof(k_set
->sig
[0]) * 8);
868 const uptr bit
= signum
% (sizeof(k_set
->sig
[0]) * 8);
869 k_set
->sig
[idx
] &= ~(1 << bit
);
872 bool internal_sigismember(__sanitizer_sigset_t
*set
, int signum
) {
875 CHECK_LT(signum
, sizeof(*set
) * 8);
876 __sanitizer_kernel_sigset_t
*k_set
= (__sanitizer_kernel_sigset_t
*)set
;
877 const uptr idx
= signum
/ (sizeof(k_set
->sig
[0]) * 8);
878 const uptr bit
= signum
% (sizeof(k_set
->sig
[0]) * 8);
879 return k_set
->sig
[idx
] & (1 << bit
);
881 #elif SANITIZER_FREEBSD
882 void internal_sigdelset(__sanitizer_sigset_t
*set
, int signum
) {
883 sigset_t
*rset
= reinterpret_cast<sigset_t
*>(set
);
884 sigdelset(rset
, signum
);
887 bool internal_sigismember(__sanitizer_sigset_t
*set
, int signum
) {
888 sigset_t
*rset
= reinterpret_cast<sigset_t
*>(set
);
889 return sigismember(rset
, signum
);
892 #endif // !SANITIZER_SOLARIS
894 #if !SANITIZER_NETBSD
895 // ThreadLister implementation.
896 ThreadLister::ThreadLister(pid_t pid
) : pid_(pid
), buffer_(4096) {
897 char task_directory_path
[80];
898 internal_snprintf(task_directory_path
, sizeof(task_directory_path
),
899 "/proc/%d/task/", pid
);
900 descriptor_
= internal_open(task_directory_path
, O_RDONLY
| O_DIRECTORY
);
901 if (internal_iserror(descriptor_
)) {
902 Report("Can't open /proc/%d/task for reading.\n", pid
);
906 ThreadLister::Result
ThreadLister::ListThreads(
907 InternalMmapVector
<tid_t
> *threads
) {
908 if (internal_iserror(descriptor_
))
910 internal_lseek(descriptor_
, 0, SEEK_SET
);
914 for (bool first_read
= true;; first_read
= false) {
915 // Resize to max capacity if it was downsized by IsAlive.
916 buffer_
.resize(buffer_
.capacity());
917 CHECK_GE(buffer_
.size(), 4096);
918 uptr read
= internal_getdents(
919 descriptor_
, (struct linux_dirent
*)buffer_
.data(), buffer_
.size());
922 if (internal_iserror(read
)) {
923 Report("Can't read directory entries from /proc/%d/task.\n", pid_
);
927 for (uptr begin
= (uptr
)buffer_
.data(), end
= begin
+ read
; begin
< end
;) {
928 struct linux_dirent
*entry
= (struct linux_dirent
*)begin
;
929 begin
+= entry
->d_reclen
;
930 if (entry
->d_ino
== 1) {
931 // Inode 1 is for bad blocks and also can be a reason for early return.
932 // Should be emitted if kernel tried to output terminating thread.
933 // See proc_task_readdir implementation in Linux.
936 if (entry
->d_ino
&& *entry
->d_name
>= '0' && *entry
->d_name
<= '9')
937 threads
->push_back(internal_atoll(entry
->d_name
));
940 // Now we are going to detect short-read or early EOF. In such cases Linux
941 // can return inconsistent list with missing alive threads.
942 // Code will just remember that the list can be incomplete but it will
943 // continue reads to return as much as possible.
945 // The first one was a short-read by definition.
947 } else if (read
> buffer_
.size() - 1024) {
948 // Read was close to the buffer size. So double the size and assume the
950 buffer_
.resize(buffer_
.size() * 2);
952 } else if (!threads
->empty() && !IsAlive(threads
->back())) {
953 // Maybe Linux early returned from read on terminated thread (!pid_alive)
954 // and failed to restore read position.
955 // See next_tid and proc_task_instantiate in Linux.
961 bool ThreadLister::IsAlive(int tid
) {
962 // /proc/%d/task/%d/status uses same call to detect alive threads as
963 // proc_task_readdir. See task_state implementation in Linux.
965 internal_snprintf(path
, sizeof(path
), "/proc/%d/task/%d/status", pid_
, tid
);
966 if (!ReadFileToVector(path
, &buffer_
) || buffer_
.empty())
968 buffer_
.push_back(0);
969 static const char kPrefix
[] = "\nPPid:";
970 const char *field
= internal_strstr(buffer_
.data(), kPrefix
);
973 field
+= internal_strlen(kPrefix
);
974 return (int)internal_atoll(field
) != 0;
977 ThreadLister::~ThreadLister() {
978 if (!internal_iserror(descriptor_
))
979 internal_close(descriptor_
);
983 #if SANITIZER_WORDSIZE == 32
984 // Take care of unusable kernel area in top gigabyte.
985 static uptr
GetKernelAreaSize() {
986 #if SANITIZER_LINUX && !SANITIZER_X32
987 const uptr gbyte
= 1UL << 30;
989 // Firstly check if there are writable segments
990 // mapped to top gigabyte (e.g. stack).
991 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
992 if (proc_maps
.Error())
994 MemoryMappedSegment segment
;
995 while (proc_maps
.Next(&segment
)) {
996 if ((segment
.end
>= 3 * gbyte
) && segment
.IsWritable()) return 0;
999 #if !SANITIZER_ANDROID
1000 // Even if nothing is mapped, top Gb may still be accessible
1001 // if we are running on 64-bit kernel.
1002 // Uname may report misleading results if personality type
1003 // is modified (e.g. under schroot) so check this as well.
1004 struct utsname uname_info
;
1005 int pers
= personality(0xffffffffUL
);
1006 if (!(pers
& PER_MASK
)
1007 && uname(&uname_info
) == 0
1008 && internal_strstr(uname_info
.machine
, "64"))
1010 #endif // SANITIZER_ANDROID
1012 // Top gigabyte is reserved for kernel.
1016 #endif // SANITIZER_LINUX && !SANITIZER_X32
1018 #endif // SANITIZER_WORDSIZE == 32
1020 uptr
GetMaxVirtualAddress() {
1021 #if (SANITIZER_NETBSD || SANITIZER_OPENBSD) && defined(__x86_64__)
1022 return 0x7f7ffffff000ULL
; // (0x00007f8000000000 - PAGE_SIZE)
1023 #elif SANITIZER_WORDSIZE == 64
1024 # if defined(__powerpc64__) || defined(__aarch64__)
1025 // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
1026 // We somehow need to figure out which one we are using now and choose
1027 // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
1028 // Note that with 'ulimit -s unlimited' the stack is moved away from the top
1029 // of the address space, so simply checking the stack address is not enough.
1030 // This should (does) work for both PowerPC64 Endian modes.
1031 // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
1032 return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
1033 # elif defined(__mips64)
1034 return (1ULL << 40) - 1; // 0x000000ffffffffffUL;
1035 # elif defined(__s390x__)
1036 return (1ULL << 53) - 1; // 0x001fffffffffffffUL;
1037 #elif defined(__sparc__)
1040 return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
1042 #else // SANITIZER_WORDSIZE == 32
1043 # if defined(__s390__)
1044 return (1ULL << 31) - 1; // 0x7fffffff;
1046 return (1ULL << 32) - 1; // 0xffffffff;
1048 #endif // SANITIZER_WORDSIZE
1051 uptr
GetMaxUserVirtualAddress() {
1052 uptr addr
= GetMaxVirtualAddress();
1053 #if SANITIZER_WORDSIZE == 32 && !defined(__s390__)
1054 if (!common_flags()->full_address_space
)
1055 addr
-= GetKernelAreaSize();
1056 CHECK_LT(reinterpret_cast<uptr
>(&addr
), addr
);
1061 #if !SANITIZER_ANDROID
1062 uptr
GetPageSize() {
1063 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__))
1064 return EXEC_PAGESIZE
;
1065 #elif SANITIZER_USE_GETAUXVAL
1066 return getauxval(AT_PAGESZ
);
1067 #elif SANITIZER_FREEBSD || SANITIZER_NETBSD
1068 // Use sysctl as sysconf can trigger interceptors internally.
1070 uptr pzl
= sizeof(pz
);
1071 int mib
[2] = {CTL_HW
, HW_PAGESIZE
};
1072 int rv
= internal_sysctl(mib
, 2, &pz
, &pzl
, nullptr, 0);
1076 return sysconf(_SC_PAGESIZE
); // EXEC_PAGESIZE may not be trustworthy.
1079 #endif // !SANITIZER_ANDROID
1081 #if !SANITIZER_OPENBSD
1082 uptr
ReadBinaryName(/*out*/char *buf
, uptr buf_len
) {
1083 #if SANITIZER_SOLARIS
1084 const char *default_module_name
= getexecname();
1085 CHECK_NE(default_module_name
, NULL
);
1086 return internal_snprintf(buf
, buf_len
, "%s", default_module_name
);
1088 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
1089 #if SANITIZER_FREEBSD
1090 const int Mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
1092 const int Mib
[4] = {CTL_KERN
, KERN_PROC_ARGS
, -1, KERN_PROC_PATHNAME
};
1094 const char *default_module_name
= "kern.proc.pathname";
1095 uptr Size
= buf_len
;
1097 (internal_sysctl(Mib
, ARRAY_SIZE(Mib
), buf
, &Size
, NULL
, 0) != 0);
1098 int readlink_error
= IsErr
? errno
: 0;
1099 uptr module_name_len
= Size
;
1101 const char *default_module_name
= "/proc/self/exe";
1102 uptr module_name_len
= internal_readlink(
1103 default_module_name
, buf
, buf_len
);
1105 bool IsErr
= internal_iserror(module_name_len
, &readlink_error
);
1106 #endif // SANITIZER_SOLARIS
1108 // We can't read binary name for some reason, assume it's unknown.
1109 Report("WARNING: reading executable name failed with errno %d, "
1110 "some stack frames may not be symbolized\n", readlink_error
);
1111 module_name_len
= internal_snprintf(buf
, buf_len
, "%s",
1112 default_module_name
);
1113 CHECK_LT(module_name_len
, buf_len
);
1115 return module_name_len
;
1118 #endif // !SANITIZER_OPENBSD
1120 uptr
ReadLongProcessName(/*out*/ char *buf
, uptr buf_len
) {
1125 if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf
, &tmpsize
, &tmplen
,
1127 internal_strncpy(buf
, tmpbuf
, buf_len
);
1128 UnmapOrDie(tmpbuf
, tmpsize
);
1129 return internal_strlen(buf
);
1132 return ReadBinaryName(buf
, buf_len
);
1135 // Match full names of the form /path/to/base_name{-,.}*
1136 bool LibraryNameIs(const char *full_name
, const char *base_name
) {
1137 const char *name
= full_name
;
1139 while (*name
!= '\0') name
++;
1140 while (name
> full_name
&& *name
!= '/') name
--;
1141 if (*name
== '/') name
++;
1142 uptr base_name_length
= internal_strlen(base_name
);
1143 if (internal_strncmp(name
, base_name
, base_name_length
)) return false;
1144 return (name
[base_name_length
] == '-' || name
[base_name_length
] == '.');
1147 #if !SANITIZER_ANDROID
1148 // Call cb for each region mapped by map.
1149 void ForEachMappedRegion(link_map
*map
, void (*cb
)(const void *, uptr
)) {
1150 CHECK_NE(map
, nullptr);
1151 #if !SANITIZER_FREEBSD && !SANITIZER_OPENBSD
1152 typedef ElfW(Phdr
) Elf_Phdr
;
1153 typedef ElfW(Ehdr
) Elf_Ehdr
;
1154 #endif // !SANITIZER_FREEBSD && !SANITIZER_OPENBSD
1155 char *base
= (char *)map
->l_addr
;
1156 Elf_Ehdr
*ehdr
= (Elf_Ehdr
*)base
;
1157 char *phdrs
= base
+ ehdr
->e_phoff
;
1158 char *phdrs_end
= phdrs
+ ehdr
->e_phnum
* ehdr
->e_phentsize
;
1160 // Find the segment with the minimum base so we can "relocate" the p_vaddr
1161 // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
1162 // objects have a non-zero base.
1163 uptr preferred_base
= (uptr
)-1;
1164 for (char *iter
= phdrs
; iter
!= phdrs_end
; iter
+= ehdr
->e_phentsize
) {
1165 Elf_Phdr
*phdr
= (Elf_Phdr
*)iter
;
1166 if (phdr
->p_type
== PT_LOAD
&& preferred_base
> (uptr
)phdr
->p_vaddr
)
1167 preferred_base
= (uptr
)phdr
->p_vaddr
;
1170 // Compute the delta from the real base to get a relocation delta.
1171 sptr delta
= (uptr
)base
- preferred_base
;
1172 // Now we can figure out what the loader really mapped.
1173 for (char *iter
= phdrs
; iter
!= phdrs_end
; iter
+= ehdr
->e_phentsize
) {
1174 Elf_Phdr
*phdr
= (Elf_Phdr
*)iter
;
1175 if (phdr
->p_type
== PT_LOAD
) {
1176 uptr seg_start
= phdr
->p_vaddr
+ delta
;
1177 uptr seg_end
= seg_start
+ phdr
->p_memsz
;
1178 // None of these values are aligned. We consider the ragged edges of the
1179 // load command as defined, since they are mapped from the file.
1180 seg_start
= RoundDownTo(seg_start
, GetPageSizeCached());
1181 seg_end
= RoundUpTo(seg_end
, GetPageSizeCached());
1182 cb((void *)seg_start
, seg_end
- seg_start
);
1188 #if defined(__x86_64__) && SANITIZER_LINUX
1189 // We cannot use glibc's clone wrapper, because it messes with the child
1190 // task's TLS. It writes the PID and TID of the child task to its thread
1191 // descriptor, but in our case the child task shares the thread descriptor with
1192 // the parent (because we don't know how to allocate a new thread
1193 // descriptor to keep glibc happy). So the stock version of clone(), when
1194 // used with CLONE_VM, would end up corrupting the parent's thread descriptor.
1195 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1196 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1198 if (!fn
|| !child_stack
)
1200 CHECK_EQ(0, (uptr
)child_stack
% 16);
1201 child_stack
= (char *)child_stack
- 2 * sizeof(unsigned long long);
1202 ((unsigned long long *)child_stack
)[0] = (uptr
)fn
;
1203 ((unsigned long long *)child_stack
)[1] = (uptr
)arg
;
1204 register void *r8
__asm__("r8") = newtls
;
1205 register int *r10
__asm__("r10") = child_tidptr
;
1206 __asm__
__volatile__(
1207 /* %rax = syscall(%rax = SYSCALL(clone),
1209 * %rsi = child_stack,
1210 * %rdx = parent_tidptr,
1212 * %r10 = child_tidptr)
1219 "testq %%rax,%%rax\n"
1222 /* In the child. Terminate unwind chain. */
1223 // XXX: We should also terminate the CFI unwind chain
1224 // here. Unfortunately clang 3.2 doesn't support the
1225 // necessary CFI directives, so we skip that part.
1226 "xorq %%rbp,%%rbp\n"
1228 /* Call "fn(arg)". */
1233 /* Call _exit(%rax). */
1234 "movq %%rax,%%rdi\n"
1238 /* Return to parent. */
1241 : "a"(SYSCALL(clone
)), "i"(SYSCALL(exit
)),
1247 : "memory", "r11", "rcx");
1250 #elif defined(__mips__)
1251 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1252 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1254 if (!fn
|| !child_stack
)
1256 CHECK_EQ(0, (uptr
)child_stack
% 16);
1257 child_stack
= (char *)child_stack
- 2 * sizeof(unsigned long long);
1258 ((unsigned long long *)child_stack
)[0] = (uptr
)fn
;
1259 ((unsigned long long *)child_stack
)[1] = (uptr
)arg
;
1260 register void *a3
__asm__("$7") = newtls
;
1261 register int *a4
__asm__("$8") = child_tidptr
;
1262 // We don't have proper CFI directives here because it requires alot of code
1263 // for very marginal benefits.
1264 __asm__
__volatile__(
1265 /* $v0 = syscall($v0 = __NR_clone,
1267 * $a1 = child_stack,
1268 * $a2 = parent_tidptr,
1270 * $a4 = child_tidptr)
1277 /* Store the fifth argument on stack
1278 * if we are using 32-bit abi.
1280 #if SANITIZER_WORDSIZE == 32
1293 /* Call "fn(arg)". */
1294 #if SANITIZER_WORDSIZE == 32
1295 #ifdef __BIG_ENDIAN__
1308 /* Call _exit($v0). */
1313 /* Return to parent. */
1323 : "memory", "$29" );
1326 #elif defined(__aarch64__)
1327 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1328 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1330 if (!fn
|| !child_stack
)
1332 CHECK_EQ(0, (uptr
)child_stack
% 16);
1333 child_stack
= (char *)child_stack
- 2 * sizeof(unsigned long long);
1334 ((unsigned long long *)child_stack
)[0] = (uptr
)fn
;
1335 ((unsigned long long *)child_stack
)[1] = (uptr
)arg
;
1337 register int (*__fn
)(void *) __asm__("x0") = fn
;
1338 register void *__stack
__asm__("x1") = child_stack
;
1339 register int __flags
__asm__("x2") = flags
;
1340 register void *__arg
__asm__("x3") = arg
;
1341 register int *__ptid
__asm__("x4") = parent_tidptr
;
1342 register void *__tls
__asm__("x5") = newtls
;
1343 register int *__ctid
__asm__("x6") = child_tidptr
;
1345 __asm__
__volatile__(
1346 "mov x0,x2\n" /* flags */
1347 "mov x2,x4\n" /* ptid */
1348 "mov x3,x5\n" /* tls */
1349 "mov x4,x6\n" /* ctid */
1350 "mov x8,%9\n" /* clone */
1360 /* In the child, now. Call "fn(arg)". */
1361 "ldp x1, x0, [sp], #16\n"
1364 /* Call _exit(%r0). */
1371 "r"(__fn
), "r"(__stack
), "r"(__flags
), "r"(__arg
),
1372 "r"(__ptid
), "r"(__tls
), "r"(__ctid
),
1373 "i"(__NR_clone
), "i"(__NR_exit
)
1377 #elif defined(__powerpc64__)
1378 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1379 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1381 // Stack frame structure.
1382 #if SANITIZER_PPC64V1
1383 // Back chain == 0 (SP + 112)
1384 // Frame (112 bytes):
1385 // Parameter save area (SP + 48), 8 doublewords
1386 // TOC save area (SP + 40)
1387 // Link editor doubleword (SP + 32)
1388 // Compiler doubleword (SP + 24)
1389 // LR save area (SP + 16)
1390 // CR save area (SP + 8)
1391 // Back chain (SP + 0)
1392 # define FRAME_SIZE 112
1393 # define FRAME_TOC_SAVE_OFFSET 40
1394 #elif SANITIZER_PPC64V2
1395 // Back chain == 0 (SP + 32)
1396 // Frame (32 bytes):
1397 // TOC save area (SP + 24)
1398 // LR save area (SP + 16)
1399 // CR save area (SP + 8)
1400 // Back chain (SP + 0)
1401 # define FRAME_SIZE 32
1402 # define FRAME_TOC_SAVE_OFFSET 24
1404 # error "Unsupported PPC64 ABI"
1406 if (!fn
|| !child_stack
)
1408 CHECK_EQ(0, (uptr
)child_stack
% 16);
1410 register int (*__fn
)(void *) __asm__("r3") = fn
;
1411 register void *__cstack
__asm__("r4") = child_stack
;
1412 register int __flags
__asm__("r5") = flags
;
1413 register void *__arg
__asm__("r6") = arg
;
1414 register int *__ptidptr
__asm__("r7") = parent_tidptr
;
1415 register void *__newtls
__asm__("r8") = newtls
;
1416 register int *__ctidptr
__asm__("r9") = child_tidptr
;
1418 __asm__
__volatile__(
1419 /* fn and arg are saved across the syscall */
1429 r7 == child_tidptr */
1437 /* Test if syscall was successful */
1438 "cmpdi cr1, 3, 0\n\t"
1439 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1442 /* Set up stack frame */
1444 "stdu 29, -8(1)\n\t"
1445 "stdu 1, -%12(1)\n\t"
1446 /* Do the function call */
1448 #if SANITIZER_PPC64V1
1452 #elif SANITIZER_PPC64V2
1456 # error "Unsupported PPC64 ABI"
1462 /* Call _exit(r3) */
1466 /* Return to parent */
1482 "i" (FRAME_TOC_SAVE_OFFSET
)
1483 : "cr0", "cr1", "memory", "ctr", "r0", "r27", "r28", "r29");
1486 #elif defined(__i386__) && SANITIZER_LINUX
1487 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1488 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1490 if (!fn
|| !child_stack
)
1492 CHECK_EQ(0, (uptr
)child_stack
% 16);
1493 child_stack
= (char *)child_stack
- 7 * sizeof(unsigned int);
1494 ((unsigned int *)child_stack
)[0] = (uptr
)flags
;
1495 ((unsigned int *)child_stack
)[1] = (uptr
)0;
1496 ((unsigned int *)child_stack
)[2] = (uptr
)fn
;
1497 ((unsigned int *)child_stack
)[3] = (uptr
)arg
;
1498 __asm__
__volatile__(
1499 /* %eax = syscall(%eax = SYSCALL(clone),
1501 * %ecx = child_stack,
1502 * %edx = parent_tidptr,
1504 * %edi = child_tidptr)
1508 "movl (%%ecx), %%ebx\n"
1509 /* Do the system call */
1513 /* Remember the flag value. */
1514 "movl %%ebx, (%%ecx)\n"
1524 "test %%eax,%%eax\n"
1527 /* terminate the stack frame */
1528 "xorl %%ebp,%%ebp\n"
1535 "addl $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n"
1538 "movl %%eax, %%ebx\n"
1543 : "a"(SYSCALL(clone
)), "i"(SYSCALL(exit
)),
1551 #elif defined(__arm__) && SANITIZER_LINUX
1552 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1553 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1555 if (!fn
|| !child_stack
)
1557 child_stack
= (char *)child_stack
- 2 * sizeof(unsigned int);
1558 ((unsigned int *)child_stack
)[0] = (uptr
)fn
;
1559 ((unsigned int *)child_stack
)[1] = (uptr
)arg
;
1560 register int r0
__asm__("r0") = flags
;
1561 register void *r1
__asm__("r1") = child_stack
;
1562 register int *r2
__asm__("r2") = parent_tidptr
;
1563 register void *r3
__asm__("r3") = newtls
;
1564 register int *r4
__asm__("r4") = child_tidptr
;
1565 register int r7
__asm__("r7") = __NR_clone
;
1567 #if __ARM_ARCH > 4 || defined (__ARM_ARCH_4T__)
1568 # define ARCH_HAS_BX
1571 # define ARCH_HAS_BLX
1575 # ifdef ARCH_HAS_BLX
1576 # define BLX(R) "blx " #R "\n"
1578 # define BLX(R) "mov lr, pc; bx " #R "\n"
1581 # define BLX(R) "mov lr, pc; mov pc," #R "\n"
1584 __asm__
__volatile__(
1585 /* %r0 = syscall(%r7 = SYSCALL(clone),
1587 * %r1 = child_stack,
1588 * %r2 = parent_tidptr,
1590 * %r4 = child_tidptr)
1593 /* Do the system call */
1602 /* In the child, now. Call "fn(arg)". */
1603 "ldr r0, [sp, #4]\n"
1604 "ldr ip, [sp], #8\n"
1606 /* Call _exit(%r0). */
1612 : "r"(r0
), "r"(r1
), "r"(r2
), "r"(r3
), "r"(r4
), "r"(r7
),
1617 #endif // defined(__x86_64__) && SANITIZER_LINUX
1619 #if SANITIZER_ANDROID
1620 #if __ANDROID_API__ < 21
1621 extern "C" __attribute__((weak
)) int dl_iterate_phdr(
1622 int (*)(struct dl_phdr_info
*, size_t, void *), void *);
1625 static int dl_iterate_phdr_test_cb(struct dl_phdr_info
*info
, size_t size
,
1627 // Any name starting with "lib" indicates a bug in L where library base names
1628 // are returned instead of paths.
1629 if (info
->dlpi_name
&& info
->dlpi_name
[0] == 'l' &&
1630 info
->dlpi_name
[1] == 'i' && info
->dlpi_name
[2] == 'b') {
1631 *(bool *)data
= true;
1637 static atomic_uint32_t android_api_level
;
1639 static AndroidApiLevel
AndroidDetectApiLevelStatic() {
1640 #if __ANDROID_API__ <= 19
1641 return ANDROID_KITKAT
;
1642 #elif __ANDROID_API__ <= 22
1643 return ANDROID_LOLLIPOP_MR1
;
1645 return ANDROID_POST_LOLLIPOP
;
1649 static AndroidApiLevel
AndroidDetectApiLevel() {
1650 if (!&dl_iterate_phdr
)
1651 return ANDROID_KITKAT
; // K or lower
1652 bool base_name_seen
= false;
1653 dl_iterate_phdr(dl_iterate_phdr_test_cb
, &base_name_seen
);
1655 return ANDROID_LOLLIPOP_MR1
; // L MR1
1656 return ANDROID_POST_LOLLIPOP
; // post-L
1657 // Plain L (API level 21) is completely broken wrt ASan and not very
1658 // interesting to detect.
1661 extern "C" __attribute__((weak
)) void* _DYNAMIC
;
1663 AndroidApiLevel
AndroidGetApiLevel() {
1664 AndroidApiLevel level
=
1665 (AndroidApiLevel
)atomic_load(&android_api_level
, memory_order_relaxed
);
1666 if (level
) return level
;
1667 level
= &_DYNAMIC
== nullptr ? AndroidDetectApiLevelStatic()
1668 : AndroidDetectApiLevel();
1669 atomic_store(&android_api_level
, level
, memory_order_relaxed
);
1675 static HandleSignalMode
GetHandleSignalModeImpl(int signum
) {
1678 return common_flags()->handle_abort
;
1680 return common_flags()->handle_sigill
;
1682 return common_flags()->handle_sigtrap
;
1684 return common_flags()->handle_sigfpe
;
1686 return common_flags()->handle_segv
;
1688 return common_flags()->handle_sigbus
;
1690 return kHandleSignalNo
;
1693 HandleSignalMode
GetHandleSignalMode(int signum
) {
1694 HandleSignalMode result
= GetHandleSignalModeImpl(signum
);
1695 if (result
== kHandleSignalYes
&& !common_flags()->allow_user_segv_handler
)
1696 return kHandleSignalExclusive
;
1701 void *internal_start_thread(void(*func
)(void *arg
), void *arg
) {
1702 // Start the thread with signals blocked, otherwise it can steal user signals.
1703 __sanitizer_sigset_t set
, old
;
1704 internal_sigfillset(&set
);
1705 #if SANITIZER_LINUX && !SANITIZER_ANDROID
1706 // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
1707 // on any thread, setuid call hangs (see test/tsan/setuid.c).
1708 internal_sigdelset(&set
, 33);
1710 internal_sigprocmask(SIG_SETMASK
, &set
, &old
);
1712 real_pthread_create(&th
, nullptr, (void*(*)(void *arg
))func
, arg
);
1713 internal_sigprocmask(SIG_SETMASK
, &old
, nullptr);
1717 void internal_join_thread(void *th
) {
1718 real_pthread_join(th
, nullptr);
1721 void *internal_start_thread(void (*func
)(void *), void *arg
) { return 0; }
1723 void internal_join_thread(void *th
) {}
1726 #if defined(__aarch64__)
1727 // Android headers in the older NDK releases miss this definition.
1728 struct __sanitizer_esr_context
{
1729 struct _aarch64_ctx head
;
1733 static bool Aarch64GetESR(ucontext_t
*ucontext
, u64
*esr
) {
1734 static const u32 kEsrMagic
= 0x45535201;
1735 u8
*aux
= ucontext
->uc_mcontext
.__reserved
;
1737 _aarch64_ctx
*ctx
= (_aarch64_ctx
*)aux
;
1738 if (ctx
->size
== 0) break;
1739 if (ctx
->magic
== kEsrMagic
) {
1740 *esr
= ((__sanitizer_esr_context
*)ctx
)->esr
;
1749 #if SANITIZER_OPENBSD
1750 using Context
= sigcontext
;
1752 using Context
= ucontext_t
;
1755 SignalContext::WriteFlag
SignalContext::GetWriteFlag() const {
1756 Context
*ucontext
= (Context
*)context
;
1757 #if defined(__x86_64__) || defined(__i386__)
1758 static const uptr PF_WRITE
= 1U << 1;
1759 #if SANITIZER_FREEBSD
1760 uptr err
= ucontext
->uc_mcontext
.mc_err
;
1761 #elif SANITIZER_NETBSD
1762 uptr err
= ucontext
->uc_mcontext
.__gregs
[_REG_ERR
];
1763 #elif SANITIZER_OPENBSD
1764 uptr err
= ucontext
->sc_err
;
1765 #elif SANITIZER_SOLARIS && defined(__i386__)
1767 uptr err
= ucontext
->uc_mcontext
.gregs
[Err
];
1769 uptr err
= ucontext
->uc_mcontext
.gregs
[REG_ERR
];
1770 #endif // SANITIZER_FREEBSD
1771 return err
& PF_WRITE
? WRITE
: READ
;
1772 #elif defined(__mips__)
1773 uint32_t *exception_source
;
1774 uint32_t faulty_instruction
;
1777 exception_source
= (uint32_t *)ucontext
->uc_mcontext
.pc
;
1778 faulty_instruction
= (uint32_t)(*exception_source
);
1780 op_code
= (faulty_instruction
>> 26) & 0x3f;
1782 // FIXME: Add support for FPU, microMIPS, DSP, MSA memory instructions.
1788 #if __mips_isa_rev < 6
1794 return SignalContext::WRITE
;
1803 #if __mips_isa_rev < 6
1809 return SignalContext::READ
;
1810 #if __mips_isa_rev == 6
1812 op_code
= (faulty_instruction
>> 19) & 0x3;
1816 return SignalContext::READ
;
1820 return SignalContext::UNKNOWN
;
1821 #elif defined(__arm__)
1822 static const uptr FSR_WRITE
= 1U << 11;
1823 uptr fsr
= ucontext
->uc_mcontext
.error_code
;
1824 return fsr
& FSR_WRITE
? WRITE
: READ
;
1825 #elif defined(__aarch64__)
1826 static const u64 ESR_ELx_WNR
= 1U << 6;
1828 if (!Aarch64GetESR(ucontext
, &esr
)) return UNKNOWN
;
1829 return esr
& ESR_ELx_WNR
? WRITE
: READ
;
1830 #elif defined(__sparc__)
1831 // Decode the instruction to determine the access type.
1832 // From OpenSolaris $SRC/uts/sun4/os/trap.c (get_accesstype).
1833 #if SANITIZER_SOLARIS
1834 uptr pc
= ucontext
->uc_mcontext
.gregs
[REG_PC
];
1836 // Historical BSDism here.
1837 struct sigcontext
*scontext
= (struct sigcontext
*)context
;
1838 #if defined(__arch64__)
1839 uptr pc
= scontext
->sigc_regs
.tpc
;
1841 uptr pc
= scontext
->si_regs
.pc
;
1844 u32 instr
= *(u32
*)pc
;
1845 return (instr
>> 21) & 1 ? WRITE
: READ
;
1848 return UNKNOWN
; // FIXME: Implement.
1852 void SignalContext::DumpAllRegisters(void *context
) {
1853 // FIXME: Implement this.
1856 static void GetPcSpBp(void *context
, uptr
*pc
, uptr
*sp
, uptr
*bp
) {
1857 #if SANITIZER_NETBSD
1858 // This covers all NetBSD architectures
1859 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1860 *pc
= _UC_MACHINE_PC(ucontext
);
1861 *bp
= _UC_MACHINE_FP(ucontext
);
1862 *sp
= _UC_MACHINE_SP(ucontext
);
1863 #elif defined(__arm__)
1864 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1865 *pc
= ucontext
->uc_mcontext
.arm_pc
;
1866 *bp
= ucontext
->uc_mcontext
.arm_fp
;
1867 *sp
= ucontext
->uc_mcontext
.arm_sp
;
1868 #elif defined(__aarch64__)
1869 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1870 *pc
= ucontext
->uc_mcontext
.pc
;
1871 *bp
= ucontext
->uc_mcontext
.regs
[29];
1872 *sp
= ucontext
->uc_mcontext
.sp
;
1873 #elif defined(__hppa__)
1874 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1875 *pc
= ucontext
->uc_mcontext
.sc_iaoq
[0];
1876 /* GCC uses %r3 whenever a frame pointer is needed. */
1877 *bp
= ucontext
->uc_mcontext
.sc_gr
[3];
1878 *sp
= ucontext
->uc_mcontext
.sc_gr
[30];
1879 #elif defined(__x86_64__)
1880 # if SANITIZER_FREEBSD
1881 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1882 *pc
= ucontext
->uc_mcontext
.mc_rip
;
1883 *bp
= ucontext
->uc_mcontext
.mc_rbp
;
1884 *sp
= ucontext
->uc_mcontext
.mc_rsp
;
1885 #elif SANITIZER_OPENBSD
1886 sigcontext
*ucontext
= (sigcontext
*)context
;
1887 *pc
= ucontext
->sc_rip
;
1888 *bp
= ucontext
->sc_rbp
;
1889 *sp
= ucontext
->sc_rsp
;
1891 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1892 *pc
= ucontext
->uc_mcontext
.gregs
[REG_RIP
];
1893 *bp
= ucontext
->uc_mcontext
.gregs
[REG_RBP
];
1894 *sp
= ucontext
->uc_mcontext
.gregs
[REG_RSP
];
1896 #elif defined(__i386__)
1897 # if SANITIZER_FREEBSD
1898 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1899 *pc
= ucontext
->uc_mcontext
.mc_eip
;
1900 *bp
= ucontext
->uc_mcontext
.mc_ebp
;
1901 *sp
= ucontext
->uc_mcontext
.mc_esp
;
1902 #elif SANITIZER_OPENBSD
1903 sigcontext
*ucontext
= (sigcontext
*)context
;
1904 *pc
= ucontext
->sc_eip
;
1905 *bp
= ucontext
->sc_ebp
;
1906 *sp
= ucontext
->sc_esp
;
1908 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1909 # if SANITIZER_SOLARIS
1910 /* Use the numeric values: the symbolic ones are undefined by llvm
1911 include/llvm/Support/Solaris.h. */
1913 # define REG_EIP 14 // REG_PC
1916 # define REG_EBP 6 // REG_FP
1919 # define REG_ESP 17 // REG_SP
1922 *pc
= ucontext
->uc_mcontext
.gregs
[REG_EIP
];
1923 *bp
= ucontext
->uc_mcontext
.gregs
[REG_EBP
];
1924 *sp
= ucontext
->uc_mcontext
.gregs
[REG_ESP
];
1926 #elif defined(__powerpc__) || defined(__powerpc64__)
1927 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1928 *pc
= ucontext
->uc_mcontext
.regs
->nip
;
1929 *sp
= ucontext
->uc_mcontext
.regs
->gpr
[PT_R1
];
1930 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
1931 // pointer, but GCC always uses r31 when we need a frame pointer.
1932 *bp
= ucontext
->uc_mcontext
.regs
->gpr
[PT_R31
];
1933 #elif defined(__sparc__)
1934 #if defined(__arch64__) || defined(__sparcv9)
1935 #define STACK_BIAS 2047
1937 #define STACK_BIAS 0
1939 # if SANITIZER_SOLARIS
1940 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1941 *pc
= ucontext
->uc_mcontext
.gregs
[REG_PC
];
1942 *sp
= ucontext
->uc_mcontext
.gregs
[REG_O6
] + STACK_BIAS
;
1944 // Historical BSDism here.
1945 struct sigcontext
*scontext
= (struct sigcontext
*)context
;
1946 #if defined(__arch64__)
1947 *pc
= scontext
->sigc_regs
.tpc
;
1948 *sp
= scontext
->sigc_regs
.u_regs
[14] + STACK_BIAS
;
1950 *pc
= scontext
->si_regs
.pc
;
1951 *sp
= scontext
->si_regs
.u_regs
[14];
1954 *bp
= (uptr
)((uhwptr
*)*sp
)[14] + STACK_BIAS
;
1955 #elif defined(__mips__)
1956 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1957 *pc
= ucontext
->uc_mcontext
.pc
;
1958 *bp
= ucontext
->uc_mcontext
.gregs
[30];
1959 *sp
= ucontext
->uc_mcontext
.gregs
[29];
1960 #elif defined(__s390__)
1961 ucontext_t
*ucontext
= (ucontext_t
*)context
;
1962 # if defined(__s390x__)
1963 *pc
= ucontext
->uc_mcontext
.psw
.addr
;
1965 *pc
= ucontext
->uc_mcontext
.psw
.addr
& 0x7fffffff;
1967 *bp
= ucontext
->uc_mcontext
.gregs
[11];
1968 *sp
= ucontext
->uc_mcontext
.gregs
[15];
1970 # error "Unsupported arch"
1974 void SignalContext::InitPcSpBp() { GetPcSpBp(context
, &pc
, &sp
, &bp
); }
1976 void InitializePlatformEarly() {
1980 void MaybeReexec() {
1981 // No need to re-exec on Linux.
1985 #if SANITIZER_NETBSD
1988 uptr len
= sizeof(paxflags
);
1991 mib
[1] = internal_getpid();
1992 mib
[2] = PROC_PID_PAXFLAGS
;
1994 if (UNLIKELY(internal_sysctl(mib
, 3, &paxflags
, &len
, NULL
, 0) == -1)) {
1995 Printf("sysctl failed\n");
1999 if (UNLIKELY(paxflags
& CTL_PROC_PAXFLAGS_ASLR
)) {
2000 Printf("This sanitizer is not compatible with enabled ASLR\n");
2003 #elif SANITIZER_PPC64V2
2004 // Disable ASLR for Linux PPC64LE.
2005 int old_personality
= personality(0xffffffff);
2006 if (old_personality
!= -1 && (old_personality
& ADDR_NO_RANDOMIZE
) == 0) {
2007 VReport(1, "WARNING: Program is being run with address space layout "
2008 "randomization (ASLR) enabled which prevents the thread and "
2009 "memory sanitizers from working on powerpc64le.\n"
2010 "ASLR will be disabled and the program re-executed.\n");
2011 CHECK_NE(personality(old_personality
| ADDR_NO_RANDOMIZE
), -1);
2019 void CheckMPROTECT() {
2020 #if SANITIZER_NETBSD
2023 uptr len
= sizeof(paxflags
);
2026 mib
[1] = internal_getpid();
2027 mib
[2] = PROC_PID_PAXFLAGS
;
2029 if (UNLIKELY(internal_sysctl(mib
, 3, &paxflags
, &len
, NULL
, 0) == -1)) {
2030 Printf("sysctl failed\n");
2034 if (UNLIKELY(paxflags
& CTL_PROC_PAXFLAGS_MPROTECT
)) {
2035 Printf("This sanitizer is not compatible with enabled MPROTECT\n");
2043 void PrintModuleMap() { }
2045 void CheckNoDeepBind(const char *filename
, int flag
) {
2046 #ifdef RTLD_DEEPBIND
2047 if (flag
& RTLD_DEEPBIND
) {
2049 "You are trying to dlopen a %s shared library with RTLD_DEEPBIND flag"
2050 " which is incompatibe with sanitizer runtime "
2051 "(see https://github.com/google/sanitizers/issues/611 for details"
2052 "). If you want to run %s library under sanitizers please remove "
2053 "RTLD_DEEPBIND from dlopen flags.\n",
2054 filename
, filename
);
2060 uptr
FindAvailableMemoryRange(uptr size
, uptr alignment
, uptr left_padding
,
2061 uptr
*largest_gap_found
,
2062 uptr
*max_occupied_addr
) {
2063 UNREACHABLE("FindAvailableMemoryRange is not available");
2067 bool GetRandom(void *buffer
, uptr length
, bool blocking
) {
2068 if (!buffer
|| !length
|| length
> 256)
2070 #if SANITIZER_USE_GETENTROPY
2071 uptr rnd
= getentropy(buffer
, length
);
2073 if (internal_iserror(rnd
, &rverrno
) && rverrno
== EFAULT
)
2077 #endif // SANITIZER_USE_GETENTROPY
2079 #if SANITIZER_USE_GETRANDOM
2080 static atomic_uint8_t skip_getrandom_syscall
;
2081 if (!atomic_load_relaxed(&skip_getrandom_syscall
)) {
2082 // Up to 256 bytes, getrandom will not be interrupted.
2083 uptr res
= internal_syscall(SYSCALL(getrandom
), buffer
, length
,
2084 blocking
? 0 : GRND_NONBLOCK
);
2086 if (internal_iserror(res
, &rverrno
) && rverrno
== ENOSYS
)
2087 atomic_store_relaxed(&skip_getrandom_syscall
, 1);
2088 else if (res
== length
)
2091 #endif // SANITIZER_USE_GETRANDOM
2092 // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
2093 // blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
2094 uptr fd
= internal_open("/dev/urandom", O_RDONLY
);
2095 if (internal_iserror(fd
))
2097 uptr res
= internal_read(fd
, buffer
, length
);
2098 if (internal_iserror(res
))
2104 } // namespace __sanitizer