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 || \
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"
29 # if SANITIZER_LINUX && !SANITIZER_GO
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. sparc64 is similar, using
37 // syscall(__NR_stat64) and struct kernel_stat64.
38 # if SANITIZER_LINUX && (SANITIZER_MIPS64 || SANITIZER_SPARC64)
39 # include <asm/unistd.h>
40 # include <sys/types.h>
41 # define stat kernel_stat
42 # if SANITIZER_SPARC64
43 # define stat64 kernel_stat64
49 # define st_atime st_atim
50 # define st_mtime st_mtim
51 # define st_ctime st_ctim
53 # include <asm/stat.h>
65 # include <sys/mman.h>
66 # if !SANITIZER_SOLARIS
67 # include <sys/ptrace.h>
69 # include <sys/resource.h>
70 # include <sys/stat.h>
71 # include <sys/syscall.h>
72 # include <sys/time.h>
73 # include <sys/types.h>
74 # include <ucontext.h>
78 # include <sys/utsname.h>
81 # if SANITIZER_LINUX && !SANITIZER_ANDROID
82 # include <sys/personality.h>
85 # if SANITIZER_LINUX && defined(__loongarch__)
86 # include <sys/sysmacros.h>
89 # if SANITIZER_FREEBSD
90 # include <machine/atomic.h>
91 # include <sys/exec.h>
92 # include <sys/procctl.h>
93 # include <sys/sysctl.h>
95 // <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
96 // FreeBSD 9.2 and 10.0.
97 # include <sys/umtx.h>
100 # endif // SANITIZER_FREEBSD
102 # if SANITIZER_NETBSD
103 # include <limits.h> // For NAME_MAX
104 # include <sys/exec.h>
105 # include <sys/sysctl.h>
106 extern struct ps_strings
*__ps_strings
;
107 # endif // SANITIZER_NETBSD
109 # if SANITIZER_SOLARIS
112 # define environ _environ
115 extern char **environ
;
119 struct kernel_timeval
{
124 // <linux/futex.h> is broken on some linux distributions.
125 const int FUTEX_WAIT
= 0;
126 const int FUTEX_WAKE
= 1;
127 const int FUTEX_PRIVATE_FLAG
= 128;
128 const int FUTEX_WAIT_PRIVATE
= FUTEX_WAIT
| FUTEX_PRIVATE_FLAG
;
129 const int FUTEX_WAKE_PRIVATE
= FUTEX_WAKE
| FUTEX_PRIVATE_FLAG
;
130 # endif // SANITIZER_LINUX
132 // Are we using 32-bit or 64-bit Linux syscalls?
133 // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
134 // but it still needs to use 64-bit syscalls.
135 # if SANITIZER_LINUX && (defined(__x86_64__) || defined(__powerpc64__) || \
136 SANITIZER_WORDSIZE == 64 || \
137 (defined(__mips__) && _MIPS_SIM == _ABIN32))
138 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
140 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
143 // Note : FreeBSD implemented both Linux and OpenBSD apis.
144 # if SANITIZER_LINUX && defined(__NR_getrandom)
145 # if !defined(GRND_NONBLOCK)
146 # define GRND_NONBLOCK 1
148 # define SANITIZER_USE_GETRANDOM 1
150 # define SANITIZER_USE_GETRANDOM 0
151 # endif // SANITIZER_LINUX && defined(__NR_getrandom)
153 # if SANITIZER_FREEBSD
154 # define SANITIZER_USE_GETENTROPY 1
157 namespace __sanitizer
{
159 void SetSigProcMask(__sanitizer_sigset_t
*set
, __sanitizer_sigset_t
*oldset
) {
160 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK
, set
, oldset
));
163 // Block asynchronous signals
164 void BlockSignals(__sanitizer_sigset_t
*oldset
) {
165 __sanitizer_sigset_t set
;
166 internal_sigfillset(&set
);
167 # if SANITIZER_LINUX && !SANITIZER_ANDROID
168 // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
169 // on any thread, setuid call hangs.
170 // See test/sanitizer_common/TestCases/Linux/setuid.c.
171 internal_sigdelset(&set
, 33);
174 // Seccomp-BPF-sandboxed processes rely on SIGSYS to handle trapped syscalls.
175 // If this signal is blocked, such calls cannot be handled and the process may
177 internal_sigdelset(&set
, 31);
179 // Don't block synchronous signals
180 internal_sigdelset(&set
, SIGSEGV
);
181 internal_sigdelset(&set
, SIGBUS
);
182 internal_sigdelset(&set
, SIGILL
);
183 internal_sigdelset(&set
, SIGTRAP
);
184 internal_sigdelset(&set
, SIGABRT
);
185 internal_sigdelset(&set
, SIGFPE
);
186 internal_sigdelset(&set
, SIGPIPE
);
189 SetSigProcMask(&set
, oldset
);
192 ScopedBlockSignals::ScopedBlockSignals(__sanitizer_sigset_t
*copy
) {
193 BlockSignals(&saved_
);
195 internal_memcpy(copy
, &saved_
, sizeof(saved_
));
198 ScopedBlockSignals::~ScopedBlockSignals() { SetSigProcMask(&saved_
, nullptr); }
200 # if SANITIZER_LINUX && defined(__x86_64__)
201 # include "sanitizer_syscall_linux_x86_64.inc"
202 # elif SANITIZER_LINUX && SANITIZER_RISCV64
203 # include "sanitizer_syscall_linux_riscv64.inc"
204 # elif SANITIZER_LINUX && defined(__aarch64__)
205 # include "sanitizer_syscall_linux_aarch64.inc"
206 # elif SANITIZER_LINUX && defined(__arm__)
207 # include "sanitizer_syscall_linux_arm.inc"
208 # elif SANITIZER_LINUX && defined(__hexagon__)
209 # include "sanitizer_syscall_linux_hexagon.inc"
210 # elif SANITIZER_LINUX && SANITIZER_LOONGARCH64
211 # include "sanitizer_syscall_linux_loongarch64.inc"
213 # include "sanitizer_syscall_generic.inc"
216 // --------------- sanitizer_libc.h
217 # if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
219 uptr
internal_mmap(void *addr
, uptr length
, int prot
, int flags
, int fd
,
221 # if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
222 return internal_syscall(SYSCALL(mmap
), (uptr
)addr
, length
, prot
, flags
, fd
,
225 // mmap2 specifies file offset in 4096-byte units.
226 CHECK(IsAligned(offset
, 4096));
227 return internal_syscall(SYSCALL(mmap2
), addr
, length
, prot
, flags
, fd
,
228 (OFF_T
)(offset
/ 4096));
231 # endif // !SANITIZER_S390
233 uptr
internal_munmap(void *addr
, uptr length
) {
234 return internal_syscall(SYSCALL(munmap
), (uptr
)addr
, length
);
238 uptr
internal_mremap(void *old_address
, uptr old_size
, uptr new_size
, int flags
,
240 return internal_syscall(SYSCALL(mremap
), (uptr
)old_address
, old_size
,
241 new_size
, flags
, (uptr
)new_address
);
245 int internal_mprotect(void *addr
, uptr length
, int prot
) {
246 return internal_syscall(SYSCALL(mprotect
), (uptr
)addr
, length
, prot
);
249 int internal_madvise(uptr addr
, uptr length
, int advice
) {
250 return internal_syscall(SYSCALL(madvise
), addr
, length
, advice
);
253 uptr
internal_close(fd_t fd
) { return internal_syscall(SYSCALL(close
), fd
); }
255 uptr
internal_open(const char *filename
, int flags
) {
257 return internal_syscall(SYSCALL(openat
), AT_FDCWD
, (uptr
)filename
, flags
);
259 return internal_syscall(SYSCALL(open
), (uptr
)filename
, flags
);
263 uptr
internal_open(const char *filename
, int flags
, u32 mode
) {
265 return internal_syscall(SYSCALL(openat
), AT_FDCWD
, (uptr
)filename
, flags
,
268 return internal_syscall(SYSCALL(open
), (uptr
)filename
, flags
, mode
);
272 uptr
internal_read(fd_t fd
, void *buf
, uptr count
) {
275 (sptr
)internal_syscall(SYSCALL(read
), fd
, (uptr
)buf
, count
));
279 uptr
internal_write(fd_t fd
, const void *buf
, uptr count
) {
282 (sptr
)internal_syscall(SYSCALL(write
), fd
, (uptr
)buf
, count
));
286 uptr
internal_ftruncate(fd_t fd
, uptr size
) {
289 (sptr
)internal_syscall(SYSCALL(ftruncate
), fd
, (OFF_T
)size
));
293 # if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && SANITIZER_LINUX
294 static void stat64_to_stat(struct stat64
*in
, struct stat
*out
) {
295 internal_memset(out
, 0, sizeof(*out
));
296 out
->st_dev
= in
->st_dev
;
297 out
->st_ino
= in
->st_ino
;
298 out
->st_mode
= in
->st_mode
;
299 out
->st_nlink
= in
->st_nlink
;
300 out
->st_uid
= in
->st_uid
;
301 out
->st_gid
= in
->st_gid
;
302 out
->st_rdev
= in
->st_rdev
;
303 out
->st_size
= in
->st_size
;
304 out
->st_blksize
= in
->st_blksize
;
305 out
->st_blocks
= in
->st_blocks
;
306 out
->st_atime
= in
->st_atime
;
307 out
->st_mtime
= in
->st_mtime
;
308 out
->st_ctime
= in
->st_ctime
;
312 # if SANITIZER_LINUX && defined(__loongarch__)
313 static void statx_to_stat(struct statx
*in
, struct stat
*out
) {
314 internal_memset(out
, 0, sizeof(*out
));
315 out
->st_dev
= makedev(in
->stx_dev_major
, in
->stx_dev_minor
);
316 out
->st_ino
= in
->stx_ino
;
317 out
->st_mode
= in
->stx_mode
;
318 out
->st_nlink
= in
->stx_nlink
;
319 out
->st_uid
= in
->stx_uid
;
320 out
->st_gid
= in
->stx_gid
;
321 out
->st_rdev
= makedev(in
->stx_rdev_major
, in
->stx_rdev_minor
);
322 out
->st_size
= in
->stx_size
;
323 out
->st_blksize
= in
->stx_blksize
;
324 out
->st_blocks
= in
->stx_blocks
;
325 out
->st_atime
= in
->stx_atime
.tv_sec
;
326 out
->st_atim
.tv_nsec
= in
->stx_atime
.tv_nsec
;
327 out
->st_mtime
= in
->stx_mtime
.tv_sec
;
328 out
->st_mtim
.tv_nsec
= in
->stx_mtime
.tv_nsec
;
329 out
->st_ctime
= in
->stx_ctime
.tv_sec
;
330 out
->st_ctim
.tv_nsec
= in
->stx_ctime
.tv_nsec
;
334 # if SANITIZER_MIPS64 || SANITIZER_SPARC64
335 # if SANITIZER_MIPS64
336 typedef struct kernel_stat kstat_t
;
338 typedef struct kernel_stat64 kstat_t
;
340 // Undefine compatibility macros from <sys/stat.h>
341 // so that they would not clash with the kernel_stat
342 // st_[a|m|c]time fields
348 # if defined(SANITIZER_ANDROID)
349 // Bionic sys/stat.h defines additional macros
350 // for compatibility with the old NDKs and
351 // they clash with the kernel_stat structure
352 // st_[a|m|c]time_nsec fields.
353 # undef st_atime_nsec
354 # undef st_mtime_nsec
355 # undef st_ctime_nsec
357 static void kernel_stat_to_stat(kstat_t
*in
, struct stat
*out
) {
358 internal_memset(out
, 0, sizeof(*out
));
359 out
->st_dev
= in
->st_dev
;
360 out
->st_ino
= in
->st_ino
;
361 out
->st_mode
= in
->st_mode
;
362 out
->st_nlink
= in
->st_nlink
;
363 out
->st_uid
= in
->st_uid
;
364 out
->st_gid
= in
->st_gid
;
365 out
->st_rdev
= in
->st_rdev
;
366 out
->st_size
= in
->st_size
;
367 out
->st_blksize
= in
->st_blksize
;
368 out
->st_blocks
= in
->st_blocks
;
369 # if defined(__USE_MISC) || defined(__USE_XOPEN2K8) || \
370 defined(SANITIZER_ANDROID)
371 out
->st_atim
.tv_sec
= in
->st_atime
;
372 out
->st_atim
.tv_nsec
= in
->st_atime_nsec
;
373 out
->st_mtim
.tv_sec
= in
->st_mtime
;
374 out
->st_mtim
.tv_nsec
= in
->st_mtime_nsec
;
375 out
->st_ctim
.tv_sec
= in
->st_ctime
;
376 out
->st_ctim
.tv_nsec
= in
->st_ctime_nsec
;
378 out
->st_atime
= in
->st_atime
;
379 out
->st_atimensec
= in
->st_atime_nsec
;
380 out
->st_mtime
= in
->st_mtime
;
381 out
->st_mtimensec
= in
->st_mtime_nsec
;
382 out
->st_ctime
= in
->st_ctime
;
383 out
->st_atimensec
= in
->st_ctime_nsec
;
388 uptr
internal_stat(const char *path
, void *buf
) {
389 # if SANITIZER_FREEBSD
390 return internal_syscall(SYSCALL(fstatat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
, 0);
391 # elif SANITIZER_LINUX
392 # if defined(__loongarch__)
394 int res
= internal_syscall(SYSCALL(statx
), AT_FDCWD
, (uptr
)path
,
395 AT_NO_AUTOMOUNT
, STATX_BASIC_STATS
, (uptr
)&bufx
);
396 statx_to_stat(&bufx
, (struct stat
*)buf
);
398 # elif (SANITIZER_WORDSIZE == 64 || SANITIZER_X32 || \
399 (defined(__mips__) && _MIPS_SIM == _ABIN32)) && \
401 return internal_syscall(SYSCALL(newfstatat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
,
403 # elif SANITIZER_SPARC64
405 int res
= internal_syscall(SYSCALL(fstatat64
), AT_FDCWD
, (uptr
)path
,
407 kernel_stat_to_stat(&buf64
, (struct stat
*)buf
);
411 int res
= internal_syscall(SYSCALL(fstatat64
), AT_FDCWD
, (uptr
)path
,
413 stat64_to_stat(&buf64
, (struct stat
*)buf
);
418 int res
= internal_syscall(SYSCALL(stat64
), path
, &buf64
);
419 stat64_to_stat(&buf64
, (struct stat
*)buf
);
424 uptr
internal_lstat(const char *path
, void *buf
) {
425 # if SANITIZER_FREEBSD
426 return internal_syscall(SYSCALL(fstatat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
,
427 AT_SYMLINK_NOFOLLOW
);
428 # elif SANITIZER_LINUX
429 # if defined(__loongarch__)
431 int res
= internal_syscall(SYSCALL(statx
), AT_FDCWD
, (uptr
)path
,
432 AT_SYMLINK_NOFOLLOW
| AT_NO_AUTOMOUNT
,
433 STATX_BASIC_STATS
, (uptr
)&bufx
);
434 statx_to_stat(&bufx
, (struct stat
*)buf
);
436 # elif (defined(_LP64) || SANITIZER_X32 || \
437 (defined(__mips__) && _MIPS_SIM == _ABIN32)) && \
439 return internal_syscall(SYSCALL(newfstatat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
,
440 AT_SYMLINK_NOFOLLOW
);
441 # elif SANITIZER_SPARC64
443 int res
= internal_syscall(SYSCALL(fstatat64
), AT_FDCWD
, (uptr
)path
,
444 (uptr
)&buf64
, AT_SYMLINK_NOFOLLOW
);
445 kernel_stat_to_stat(&buf64
, (struct stat
*)buf
);
449 int res
= internal_syscall(SYSCALL(fstatat64
), AT_FDCWD
, (uptr
)path
,
450 (uptr
)&buf64
, AT_SYMLINK_NOFOLLOW
);
451 stat64_to_stat(&buf64
, (struct stat
*)buf
);
456 int res
= internal_syscall(SYSCALL(lstat64
), path
, &buf64
);
457 stat64_to_stat(&buf64
, (struct stat
*)buf
);
462 uptr
internal_fstat(fd_t fd
, void *buf
) {
463 # if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
464 # if SANITIZER_MIPS64
465 // For mips64, fstat syscall fills buffer in the format of kernel_stat
467 int res
= internal_syscall(SYSCALL(fstat
), fd
, &kbuf
);
468 kernel_stat_to_stat(&kbuf
, (struct stat
*)buf
);
470 # elif SANITIZER_LINUX && SANITIZER_SPARC64
471 // For sparc64, fstat64 syscall fills buffer in the format of kernel_stat64
473 int res
= internal_syscall(SYSCALL(fstat64
), fd
, &kbuf
);
474 kernel_stat_to_stat(&kbuf
, (struct stat
*)buf
);
476 # elif SANITIZER_LINUX && defined(__loongarch__)
478 int res
= internal_syscall(SYSCALL(statx
), fd
, "", AT_EMPTY_PATH
,
479 STATX_BASIC_STATS
, (uptr
)&bufx
);
480 statx_to_stat(&bufx
, (struct stat
*)buf
);
483 return internal_syscall(SYSCALL(fstat
), fd
, (uptr
)buf
);
487 int res
= internal_syscall(SYSCALL(fstat64
), fd
, &buf64
);
488 stat64_to_stat(&buf64
, (struct stat
*)buf
);
493 uptr
internal_filesize(fd_t fd
) {
495 if (internal_fstat(fd
, &st
))
497 return (uptr
)st
.st_size
;
500 uptr
internal_dup(int oldfd
) { return internal_syscall(SYSCALL(dup
), oldfd
); }
502 uptr
internal_dup2(int oldfd
, int newfd
) {
504 return internal_syscall(SYSCALL(dup3
), oldfd
, newfd
, 0);
506 return internal_syscall(SYSCALL(dup2
), oldfd
, newfd
);
510 uptr
internal_readlink(const char *path
, char *buf
, uptr bufsize
) {
512 return internal_syscall(SYSCALL(readlinkat
), AT_FDCWD
, (uptr
)path
, (uptr
)buf
,
515 return internal_syscall(SYSCALL(readlink
), (uptr
)path
, (uptr
)buf
, bufsize
);
519 uptr
internal_unlink(const char *path
) {
521 return internal_syscall(SYSCALL(unlinkat
), AT_FDCWD
, (uptr
)path
, 0);
523 return internal_syscall(SYSCALL(unlink
), (uptr
)path
);
527 uptr
internal_rename(const char *oldpath
, const char *newpath
) {
528 # if (defined(__riscv) || defined(__loongarch__)) && defined(__linux__)
529 return internal_syscall(SYSCALL(renameat2
), AT_FDCWD
, (uptr
)oldpath
, AT_FDCWD
,
531 # elif SANITIZER_LINUX
532 return internal_syscall(SYSCALL(renameat
), AT_FDCWD
, (uptr
)oldpath
, AT_FDCWD
,
535 return internal_syscall(SYSCALL(rename
), (uptr
)oldpath
, (uptr
)newpath
);
539 uptr
internal_sched_yield() { return internal_syscall(SYSCALL(sched_yield
)); }
541 void internal_usleep(u64 useconds
) {
543 ts
.tv_sec
= useconds
/ 1000000;
544 ts
.tv_nsec
= (useconds
% 1000000) * 1000;
545 internal_syscall(SYSCALL(nanosleep
), &ts
, &ts
);
548 uptr
internal_execve(const char *filename
, char *const argv
[],
549 char *const envp
[]) {
550 return internal_syscall(SYSCALL(execve
), (uptr
)filename
, (uptr
)argv
,
553 # endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD
555 # if !SANITIZER_NETBSD
556 void internal__exit(int exitcode
) {
557 # if SANITIZER_FREEBSD || SANITIZER_SOLARIS
558 internal_syscall(SYSCALL(exit
), exitcode
);
560 internal_syscall(SYSCALL(exit_group
), exitcode
);
562 Die(); // Unreachable.
564 # endif // !SANITIZER_NETBSD
566 // ----------------- sanitizer_common.h
567 bool FileExists(const char *filename
) {
568 if (ShouldMockFailureToOpen(filename
))
571 if (internal_stat(filename
, &st
))
573 // Sanity check: filename is a regular file.
574 return S_ISREG(st
.st_mode
);
577 bool DirExists(const char *path
) {
579 if (internal_stat(path
, &st
))
581 return S_ISDIR(st
.st_mode
);
584 # if !SANITIZER_NETBSD
586 # if SANITIZER_FREEBSD
590 # elif SANITIZER_SOLARIS
593 return internal_syscall(SYSCALL(gettid
));
597 int TgKill(pid_t pid
, tid_t tid
, int sig
) {
599 return internal_syscall(SYSCALL(tgkill
), pid
, tid
, sig
);
600 # elif SANITIZER_FREEBSD
601 return internal_syscall(SYSCALL(thr_kill2
), pid
, tid
, sig
);
602 # elif SANITIZER_SOLARIS
604 errno
= thr_kill(tid
, sig
);
605 // TgKill is expected to return -1 on error, not an errno.
606 return errno
!= 0 ? -1 : 0;
614 internal_memset(&tv
, 0, sizeof(tv
));
615 internal_syscall(SYSCALL(gettimeofday
), &tv
, 0);
616 return (u64
)tv
.tv_sec
* 1000 * 1000 * 1000 + tv
.tv_usec
* 1000;
618 // Used by real_clock_gettime.
619 uptr
internal_clock_gettime(__sanitizer_clockid_t clk_id
, void *tp
) {
620 return internal_syscall(SYSCALL(clock_gettime
), clk_id
, tp
);
622 # elif !SANITIZER_SOLARIS && !SANITIZER_NETBSD
625 clock_gettime(CLOCK_REALTIME
, &ts
);
626 return (u64
)ts
.tv_sec
* 1000 * 1000 * 1000 + ts
.tv_nsec
;
630 // Like getenv, but reads env directly from /proc (on Linux) or parses the
631 // 'environ' array (on some others) and does not use libc. This function
632 // should be called first inside __asan_init.
633 const char *GetEnv(const char *name
) {
634 # if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_SOLARIS
635 if (::environ
!= 0) {
636 uptr NameLen
= internal_strlen(name
);
637 for (char **Env
= ::environ
; *Env
!= 0; Env
++) {
638 if (internal_strncmp(*Env
, name
, NameLen
) == 0 && (*Env
)[NameLen
] == '=')
639 return (*Env
) + NameLen
+ 1;
642 return 0; // Not found.
643 # elif SANITIZER_LINUX
644 static char *environ
;
650 if (!ReadFileToBuffer("/proc/self/environ", &environ
, &environ_size
, &len
))
653 if (!environ
|| len
== 0)
655 uptr namelen
= internal_strlen(name
);
656 const char *p
= environ
;
657 while (*p
!= '\0') { // will happen at the \0\0 that terminates the buffer
658 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
659 const char *endp
= (char *)internal_memchr(p
, '\0', len
- (p
- environ
));
660 if (!endp
) // this entry isn't NUL terminated
662 else if (!internal_memcmp(p
, name
, namelen
) && p
[namelen
] == '=') // Match.
663 return p
+ namelen
+ 1; // point after =
666 return nullptr; // Not found.
668 # error "Unsupported platform"
672 # if !SANITIZER_FREEBSD && !SANITIZER_NETBSD && !SANITIZER_GO
674 SANITIZER_WEAK_ATTRIBUTE
extern void *__libc_stack_end
;
678 # if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
679 static void ReadNullSepFileToArray(const char *path
, char ***arr
,
684 *arr
= (char **)MmapOrDie(arr_size
* sizeof(char *), "NullSepFileArray");
685 if (!ReadFileToBuffer(path
, &buff
, &buff_size
, &buff_len
, 1024 * 1024)) {
691 for (count
= 1, i
= 1;; i
++) {
693 if (buff
[i
+ 1] == 0)
695 (*arr
)[count
] = &buff
[i
+ 1];
696 CHECK_LE(count
, arr_size
- 1); // FIXME: make this more flexible.
700 (*arr
)[count
] = nullptr;
704 static void GetArgsAndEnv(char ***argv
, char ***envp
) {
705 # if SANITIZER_FREEBSD
706 // On FreeBSD, retrieving the argument and environment arrays is done via the
707 // kern.ps_strings sysctl, which returns a pointer to a structure containing
708 // this information. See also <sys/exec.h>.
710 uptr sz
= sizeof(pss
);
711 if (internal_sysctlbyname("kern.ps_strings", &pss
, &sz
, NULL
, 0) == -1) {
712 Printf("sysctl kern.ps_strings failed\n");
715 *argv
= pss
->ps_argvstr
;
716 *envp
= pss
->ps_envstr
;
717 # elif SANITIZER_NETBSD
718 *argv
= __ps_strings
->ps_argvstr
;
719 *envp
= __ps_strings
->ps_envstr
;
720 # else // SANITIZER_FREEBSD
722 if (&__libc_stack_end
) {
723 uptr
*stack_end
= (uptr
*)__libc_stack_end
;
724 // Normally argc can be obtained from *stack_end, however, on ARM glibc's
725 // _start clobbers it:
726 // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/arm/start.S;hb=refs/heads/release/2.31/master#l75
727 // Do not special-case ARM and infer argc from argv everywhere.
729 while (stack_end
[argc
+ 1]) argc
++;
730 *argv
= (char **)(stack_end
+ 1);
731 *envp
= (char **)(stack_end
+ argc
+ 2);
733 # endif // !SANITIZER_GO
734 static const int kMaxArgv
= 2000, kMaxEnvp
= 2000;
735 ReadNullSepFileToArray("/proc/self/cmdline", argv
, kMaxArgv
);
736 ReadNullSepFileToArray("/proc/self/environ", envp
, kMaxEnvp
);
739 # endif // !SANITIZER_GO
740 # endif // SANITIZER_FREEBSD
745 GetArgsAndEnv(&argv
, &envp
);
749 char **GetEnviron() {
751 GetArgsAndEnv(&argv
, &envp
);
755 # if !SANITIZER_SOLARIS
756 void FutexWait(atomic_uint32_t
*p
, u32 cmp
) {
757 # if SANITIZER_FREEBSD
758 _umtx_op(p
, UMTX_OP_WAIT_UINT
, cmp
, 0, 0);
759 # elif SANITIZER_NETBSD
760 sched_yield(); /* No userspace futex-like synchronization */
762 internal_syscall(SYSCALL(futex
), (uptr
)p
, FUTEX_WAIT_PRIVATE
, cmp
, 0, 0, 0);
766 void FutexWake(atomic_uint32_t
*p
, u32 count
) {
767 # if SANITIZER_FREEBSD
768 _umtx_op(p
, UMTX_OP_WAKE
, count
, 0, 0);
769 # elif SANITIZER_NETBSD
770 /* No userspace futex-like synchronization */
772 internal_syscall(SYSCALL(futex
), (uptr
)p
, FUTEX_WAKE_PRIVATE
, count
, 0, 0, 0);
776 # endif // !SANITIZER_SOLARIS
778 // ----------------- sanitizer_linux.h
779 // The actual size of this structure is specified by d_reclen.
780 // Note that getdents64 uses a different structure format. We only provide the
781 // 32-bit syscall here.
782 # if SANITIZER_NETBSD
785 struct linux_dirent
{
786 # if SANITIZER_X32 || SANITIZER_LINUX
793 unsigned short d_reclen
;
795 unsigned char d_type
;
801 # if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
803 uptr
internal_ptrace(int request
, int pid
, void *addr
, void *data
) {
804 return internal_syscall(SYSCALL(ptrace
), request
, pid
, (uptr
)addr
,
808 uptr
internal_waitpid(int pid
, int *status
, int options
) {
809 return internal_syscall(SYSCALL(wait4
), pid
, (uptr
)status
, options
,
813 uptr
internal_getpid() { return internal_syscall(SYSCALL(getpid
)); }
815 uptr
internal_getppid() { return internal_syscall(SYSCALL(getppid
)); }
817 int internal_dlinfo(void *handle
, int request
, void *p
) {
818 # if SANITIZER_FREEBSD
819 return dlinfo(handle
, request
, p
);
825 uptr
internal_getdents(fd_t fd
, struct linux_dirent
*dirp
, unsigned int count
) {
826 # if SANITIZER_FREEBSD
827 return internal_syscall(SYSCALL(getdirentries
), fd
, (uptr
)dirp
, count
, NULL
);
828 # elif SANITIZER_LINUX
829 return internal_syscall(SYSCALL(getdents64
), fd
, (uptr
)dirp
, count
);
831 return internal_syscall(SYSCALL(getdents
), fd
, (uptr
)dirp
, count
);
835 uptr
internal_lseek(fd_t fd
, OFF_T offset
, int whence
) {
836 return internal_syscall(SYSCALL(lseek
), fd
, offset
, whence
);
840 uptr
internal_prctl(int option
, uptr arg2
, uptr arg3
, uptr arg4
, uptr arg5
) {
841 return internal_syscall(SYSCALL(prctl
), option
, arg2
, arg3
, arg4
, arg5
);
843 # if defined(__x86_64__)
844 # include <asm/unistd_64.h>
845 // Currently internal_arch_prctl() is only needed on x86_64.
846 uptr
internal_arch_prctl(int option
, uptr arg2
) {
847 return internal_syscall(__NR_arch_prctl
, option
, arg2
);
852 uptr
internal_sigaltstack(const void *ss
, void *oss
) {
853 return internal_syscall(SYSCALL(sigaltstack
), (uptr
)ss
, (uptr
)oss
);
856 extern "C" pid_t
__fork(void);
858 int internal_fork() {
861 return internal_syscall(SYSCALL(clone
), 0, SIGCHLD
);
862 # elif SANITIZER_SPARC
863 // The clone syscall interface on SPARC differs massively from the rest,
864 // so fall back to __fork.
867 return internal_syscall(SYSCALL(clone
), SIGCHLD
, 0);
870 return internal_syscall(SYSCALL(fork
));
874 # if SANITIZER_FREEBSD
875 int internal_sysctl(const int *name
, unsigned int namelen
, void *oldp
,
876 uptr
*oldlenp
, const void *newp
, uptr newlen
) {
877 return internal_syscall(SYSCALL(__sysctl
), name
, namelen
, oldp
,
878 (size_t *)oldlenp
, newp
, (size_t)newlen
);
881 int internal_sysctlbyname(const char *sname
, void *oldp
, uptr
*oldlenp
,
882 const void *newp
, uptr newlen
) {
883 // Note: this function can be called during startup, so we need to avoid
884 // calling any interceptable functions. On FreeBSD >= 1300045 sysctlbyname()
885 // is a real syscall, but for older versions it calls sysctlnametomib()
886 // followed by sysctl(). To avoid calling the intercepted version and
887 // asserting if this happens during startup, call the real sysctlnametomib()
888 // followed by internal_sysctl() if the syscall is not available.
889 # ifdef SYS___sysctlbyname
890 return internal_syscall(SYSCALL(__sysctlbyname
), sname
,
891 internal_strlen(sname
), oldp
, (size_t *)oldlenp
, newp
,
894 static decltype(sysctlnametomib
) *real_sysctlnametomib
= nullptr;
895 if (!real_sysctlnametomib
)
896 real_sysctlnametomib
=
897 (decltype(sysctlnametomib
) *)dlsym(RTLD_NEXT
, "sysctlnametomib");
898 CHECK(real_sysctlnametomib
);
900 int oid
[CTL_MAXNAME
];
901 size_t len
= CTL_MAXNAME
;
902 if (real_sysctlnametomib(sname
, oid
, &len
) == -1)
904 return internal_sysctl(oid
, len
, oldp
, oldlenp
, newp
, newlen
);
910 # define SA_RESTORER 0x04000000
911 // Doesn't set sa_restorer if the caller did not set it, so use with caution
913 int internal_sigaction_norestorer(int signum
, const void *act
, void *oldact
) {
914 __sanitizer_kernel_sigaction_t k_act
, k_oldact
;
915 internal_memset(&k_act
, 0, sizeof(__sanitizer_kernel_sigaction_t
));
916 internal_memset(&k_oldact
, 0, sizeof(__sanitizer_kernel_sigaction_t
));
917 const __sanitizer_sigaction
*u_act
= (const __sanitizer_sigaction
*)act
;
918 __sanitizer_sigaction
*u_oldact
= (__sanitizer_sigaction
*)oldact
;
920 k_act
.handler
= u_act
->handler
;
921 k_act
.sigaction
= u_act
->sigaction
;
922 internal_memcpy(&k_act
.sa_mask
, &u_act
->sa_mask
,
923 sizeof(__sanitizer_kernel_sigset_t
));
924 // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL).
925 k_act
.sa_flags
= u_act
->sa_flags
| SA_RESTORER
;
926 // FIXME: most often sa_restorer is unset, however the kernel requires it
927 // to point to a valid signal restorer that calls the rt_sigreturn syscall.
928 // If sa_restorer passed to the kernel is NULL, the program may crash upon
929 // signal delivery or fail to unwind the stack in the signal handler.
930 // libc implementation of sigaction() passes its own restorer to
931 // rt_sigaction, so we need to do the same (we'll need to reimplement the
932 // restorers; for x86_64 the restorer address can be obtained from
933 // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
934 # if !SANITIZER_ANDROID || !SANITIZER_MIPS32
935 k_act
.sa_restorer
= u_act
->sa_restorer
;
939 uptr result
= internal_syscall(SYSCALL(rt_sigaction
), (uptr
)signum
,
940 (uptr
)(u_act
? &k_act
: nullptr),
941 (uptr
)(u_oldact
? &k_oldact
: nullptr),
942 (uptr
)sizeof(__sanitizer_kernel_sigset_t
));
944 if ((result
== 0) && u_oldact
) {
945 u_oldact
->handler
= k_oldact
.handler
;
946 u_oldact
->sigaction
= k_oldact
.sigaction
;
947 internal_memcpy(&u_oldact
->sa_mask
, &k_oldact
.sa_mask
,
948 sizeof(__sanitizer_kernel_sigset_t
));
949 u_oldact
->sa_flags
= k_oldact
.sa_flags
;
950 # if !SANITIZER_ANDROID || !SANITIZER_MIPS32
951 u_oldact
->sa_restorer
= k_oldact
.sa_restorer
;
956 # endif // SANITIZER_LINUX
958 uptr
internal_sigprocmask(int how
, __sanitizer_sigset_t
*set
,
959 __sanitizer_sigset_t
*oldset
) {
960 # if SANITIZER_FREEBSD
961 return internal_syscall(SYSCALL(sigprocmask
), how
, set
, oldset
);
963 __sanitizer_kernel_sigset_t
*k_set
= (__sanitizer_kernel_sigset_t
*)set
;
964 __sanitizer_kernel_sigset_t
*k_oldset
= (__sanitizer_kernel_sigset_t
*)oldset
;
965 return internal_syscall(SYSCALL(rt_sigprocmask
), (uptr
)how
, (uptr
)k_set
,
966 (uptr
)k_oldset
, sizeof(__sanitizer_kernel_sigset_t
));
970 void internal_sigfillset(__sanitizer_sigset_t
*set
) {
971 internal_memset(set
, 0xff, sizeof(*set
));
974 void internal_sigemptyset(__sanitizer_sigset_t
*set
) {
975 internal_memset(set
, 0, sizeof(*set
));
979 void internal_sigdelset(__sanitizer_sigset_t
*set
, int signum
) {
982 CHECK_LT(signum
, sizeof(*set
) * 8);
983 __sanitizer_kernel_sigset_t
*k_set
= (__sanitizer_kernel_sigset_t
*)set
;
984 const uptr idx
= signum
/ (sizeof(k_set
->sig
[0]) * 8);
985 const uptr bit
= signum
% (sizeof(k_set
->sig
[0]) * 8);
986 k_set
->sig
[idx
] &= ~((uptr
)1 << bit
);
989 bool internal_sigismember(__sanitizer_sigset_t
*set
, int signum
) {
992 CHECK_LT(signum
, sizeof(*set
) * 8);
993 __sanitizer_kernel_sigset_t
*k_set
= (__sanitizer_kernel_sigset_t
*)set
;
994 const uptr idx
= signum
/ (sizeof(k_set
->sig
[0]) * 8);
995 const uptr bit
= signum
% (sizeof(k_set
->sig
[0]) * 8);
996 return k_set
->sig
[idx
] & ((uptr
)1 << bit
);
998 # elif SANITIZER_FREEBSD
999 uptr
internal_procctl(int type
, int id
, int cmd
, void *data
) {
1000 return internal_syscall(SYSCALL(procctl
), type
, id
, cmd
, data
);
1003 void internal_sigdelset(__sanitizer_sigset_t
*set
, int signum
) {
1004 sigset_t
*rset
= reinterpret_cast<sigset_t
*>(set
);
1005 sigdelset(rset
, signum
);
1008 bool internal_sigismember(__sanitizer_sigset_t
*set
, int signum
) {
1009 sigset_t
*rset
= reinterpret_cast<sigset_t
*>(set
);
1010 return sigismember(rset
, signum
);
1013 # endif // !SANITIZER_SOLARIS
1015 # if !SANITIZER_NETBSD
1016 // ThreadLister implementation.
1017 ThreadLister::ThreadLister(pid_t pid
) : pid_(pid
), buffer_(4096) {
1018 char task_directory_path
[80];
1019 internal_snprintf(task_directory_path
, sizeof(task_directory_path
),
1020 "/proc/%d/task/", pid
);
1021 descriptor_
= internal_open(task_directory_path
, O_RDONLY
| O_DIRECTORY
);
1022 if (internal_iserror(descriptor_
)) {
1023 Report("Can't open /proc/%d/task for reading.\n", pid
);
1027 ThreadLister::Result
ThreadLister::ListThreads(
1028 InternalMmapVector
<tid_t
> *threads
) {
1029 if (internal_iserror(descriptor_
))
1031 internal_lseek(descriptor_
, 0, SEEK_SET
);
1035 for (bool first_read
= true;; first_read
= false) {
1036 // Resize to max capacity if it was downsized by IsAlive.
1037 buffer_
.resize(buffer_
.capacity());
1038 CHECK_GE(buffer_
.size(), 4096);
1039 uptr read
= internal_getdents(
1040 descriptor_
, (struct linux_dirent
*)buffer_
.data(), buffer_
.size());
1043 if (internal_iserror(read
)) {
1044 Report("Can't read directory entries from /proc/%d/task.\n", pid_
);
1048 for (uptr begin
= (uptr
)buffer_
.data(), end
= begin
+ read
; begin
< end
;) {
1049 struct linux_dirent
*entry
= (struct linux_dirent
*)begin
;
1050 begin
+= entry
->d_reclen
;
1051 if (entry
->d_ino
== 1) {
1052 // Inode 1 is for bad blocks and also can be a reason for early return.
1053 // Should be emitted if kernel tried to output terminating thread.
1054 // See proc_task_readdir implementation in Linux.
1055 result
= Incomplete
;
1057 if (entry
->d_ino
&& *entry
->d_name
>= '0' && *entry
->d_name
<= '9')
1058 threads
->push_back(internal_atoll(entry
->d_name
));
1061 // Now we are going to detect short-read or early EOF. In such cases Linux
1062 // can return inconsistent list with missing alive threads.
1063 // Code will just remember that the list can be incomplete but it will
1064 // continue reads to return as much as possible.
1066 // The first one was a short-read by definition.
1067 result
= Incomplete
;
1068 } else if (read
> buffer_
.size() - 1024) {
1069 // Read was close to the buffer size. So double the size and assume the
1071 buffer_
.resize(buffer_
.size() * 2);
1072 result
= Incomplete
;
1073 } else if (!threads
->empty() && !IsAlive(threads
->back())) {
1074 // Maybe Linux early returned from read on terminated thread (!pid_alive)
1075 // and failed to restore read position.
1076 // See next_tid and proc_task_instantiate in Linux.
1077 result
= Incomplete
;
1082 bool ThreadLister::IsAlive(int tid
) {
1083 // /proc/%d/task/%d/status uses same call to detect alive threads as
1084 // proc_task_readdir. See task_state implementation in Linux.
1086 internal_snprintf(path
, sizeof(path
), "/proc/%d/task/%d/status", pid_
, tid
);
1087 if (!ReadFileToVector(path
, &buffer_
) || buffer_
.empty())
1089 buffer_
.push_back(0);
1090 static const char kPrefix
[] = "\nPPid:";
1091 const char *field
= internal_strstr(buffer_
.data(), kPrefix
);
1094 field
+= internal_strlen(kPrefix
);
1095 return (int)internal_atoll(field
) != 0;
1098 ThreadLister::~ThreadLister() {
1099 if (!internal_iserror(descriptor_
))
1100 internal_close(descriptor_
);
1104 # if SANITIZER_WORDSIZE == 32
1105 // Take care of unusable kernel area in top gigabyte.
1106 static uptr
GetKernelAreaSize() {
1107 # if SANITIZER_LINUX && !SANITIZER_X32
1108 const uptr gbyte
= 1UL << 30;
1110 // Firstly check if there are writable segments
1111 // mapped to top gigabyte (e.g. stack).
1112 MemoryMappingLayout
proc_maps(/*cache_enabled*/ true);
1113 if (proc_maps
.Error())
1115 MemoryMappedSegment segment
;
1116 while (proc_maps
.Next(&segment
)) {
1117 if ((segment
.end
>= 3 * gbyte
) && segment
.IsWritable())
1121 # if !SANITIZER_ANDROID
1122 // Even if nothing is mapped, top Gb may still be accessible
1123 // if we are running on 64-bit kernel.
1124 // Uname may report misleading results if personality type
1125 // is modified (e.g. under schroot) so check this as well.
1126 struct utsname uname_info
;
1127 int pers
= personality(0xffffffffUL
);
1128 if (!(pers
& PER_MASK
) && internal_uname(&uname_info
) == 0 &&
1129 internal_strstr(uname_info
.machine
, "64"))
1131 # endif // SANITIZER_ANDROID
1133 // Top gigabyte is reserved for kernel.
1137 # endif // SANITIZER_LINUX && !SANITIZER_X32
1139 # endif // SANITIZER_WORDSIZE == 32
1141 uptr
GetMaxVirtualAddress() {
1142 # if SANITIZER_NETBSD && defined(__x86_64__)
1143 return 0x7f7ffffff000ULL
; // (0x00007f8000000000 - PAGE_SIZE)
1144 # elif SANITIZER_WORDSIZE == 64
1145 # if defined(__powerpc64__) || defined(__aarch64__) || \
1146 defined(__loongarch__) || SANITIZER_RISCV64
1147 // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
1148 // We somehow need to figure out which one we are using now and choose
1149 // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
1150 // Note that with 'ulimit -s unlimited' the stack is moved away from the top
1151 // of the address space, so simply checking the stack address is not enough.
1152 // This should (does) work for both PowerPC64 Endian modes.
1153 // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
1154 // loongarch64 also has multiple address space layouts: default is 47-bit.
1155 // RISC-V 64 also has multiple address space layouts: 39, 48 and 57-bit.
1156 return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
1157 # elif SANITIZER_MIPS64
1158 return (1ULL << 40) - 1; // 0x000000ffffffffffUL;
1159 # elif defined(__s390x__)
1160 return (1ULL << 53) - 1; // 0x001fffffffffffffUL;
1161 # elif defined(__sparc__)
1164 return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
1166 # else // SANITIZER_WORDSIZE == 32
1167 # if defined(__s390__)
1168 return (1ULL << 31) - 1; // 0x7fffffff;
1170 return (1ULL << 32) - 1; // 0xffffffff;
1172 # endif // SANITIZER_WORDSIZE
1175 uptr
GetMaxUserVirtualAddress() {
1176 uptr addr
= GetMaxVirtualAddress();
1177 # if SANITIZER_WORDSIZE == 32 && !defined(__s390__)
1178 if (!common_flags()->full_address_space
)
1179 addr
-= GetKernelAreaSize();
1180 CHECK_LT(reinterpret_cast<uptr
>(&addr
), addr
);
1185 # if !SANITIZER_ANDROID || defined(__aarch64__)
1186 uptr
GetPageSize() {
1187 # if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__)) && \
1188 defined(EXEC_PAGESIZE)
1189 return EXEC_PAGESIZE
;
1190 # elif SANITIZER_FREEBSD || SANITIZER_NETBSD
1191 // Use sysctl as sysconf can trigger interceptors internally.
1193 uptr pzl
= sizeof(pz
);
1194 int mib
[2] = {CTL_HW
, HW_PAGESIZE
};
1195 int rv
= internal_sysctl(mib
, 2, &pz
, &pzl
, nullptr, 0);
1198 # elif SANITIZER_USE_GETAUXVAL
1199 return getauxval(AT_PAGESZ
);
1201 return sysconf(_SC_PAGESIZE
); // EXEC_PAGESIZE may not be trustworthy.
1206 uptr
ReadBinaryName(/*out*/ char *buf
, uptr buf_len
) {
1207 # if SANITIZER_SOLARIS
1208 const char *default_module_name
= getexecname();
1209 CHECK_NE(default_module_name
, NULL
);
1210 return internal_snprintf(buf
, buf_len
, "%s", default_module_name
);
1212 # if SANITIZER_FREEBSD || SANITIZER_NETBSD
1213 # if SANITIZER_FREEBSD
1214 const int Mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
1216 const int Mib
[4] = {CTL_KERN
, KERN_PROC_ARGS
, -1, KERN_PROC_PATHNAME
};
1218 const char *default_module_name
= "kern.proc.pathname";
1219 uptr Size
= buf_len
;
1221 (internal_sysctl(Mib
, ARRAY_SIZE(Mib
), buf
, &Size
, NULL
, 0) != 0);
1222 int readlink_error
= IsErr
? errno
: 0;
1223 uptr module_name_len
= Size
;
1225 const char *default_module_name
= "/proc/self/exe";
1226 uptr module_name_len
= internal_readlink(default_module_name
, buf
, buf_len
);
1228 bool IsErr
= internal_iserror(module_name_len
, &readlink_error
);
1231 // We can't read binary name for some reason, assume it's unknown.
1233 "WARNING: reading executable name failed with errno %d, "
1234 "some stack frames may not be symbolized\n",
1237 internal_snprintf(buf
, buf_len
, "%s", default_module_name
);
1238 CHECK_LT(module_name_len
, buf_len
);
1240 return module_name_len
;
1244 uptr
ReadLongProcessName(/*out*/ char *buf
, uptr buf_len
) {
1245 # if SANITIZER_LINUX
1249 if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf
, &tmpsize
, &tmplen
,
1251 internal_strncpy(buf
, tmpbuf
, buf_len
);
1252 UnmapOrDie(tmpbuf
, tmpsize
);
1253 return internal_strlen(buf
);
1256 return ReadBinaryName(buf
, buf_len
);
1259 // Match full names of the form /path/to/base_name{-,.}*
1260 bool LibraryNameIs(const char *full_name
, const char *base_name
) {
1261 const char *name
= full_name
;
1263 while (*name
!= '\0') name
++;
1264 while (name
> full_name
&& *name
!= '/') name
--;
1267 uptr base_name_length
= internal_strlen(base_name
);
1268 if (internal_strncmp(name
, base_name
, base_name_length
))
1270 return (name
[base_name_length
] == '-' || name
[base_name_length
] == '.');
1273 # if !SANITIZER_ANDROID
1274 // Call cb for each region mapped by map.
1275 void ForEachMappedRegion(link_map
*map
, void (*cb
)(const void *, uptr
)) {
1276 CHECK_NE(map
, nullptr);
1277 # if !SANITIZER_FREEBSD
1278 typedef ElfW(Phdr
) Elf_Phdr
;
1279 typedef ElfW(Ehdr
) Elf_Ehdr
;
1280 # endif // !SANITIZER_FREEBSD
1281 char *base
= (char *)map
->l_addr
;
1282 Elf_Ehdr
*ehdr
= (Elf_Ehdr
*)base
;
1283 char *phdrs
= base
+ ehdr
->e_phoff
;
1284 char *phdrs_end
= phdrs
+ ehdr
->e_phnum
* ehdr
->e_phentsize
;
1286 // Find the segment with the minimum base so we can "relocate" the p_vaddr
1287 // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
1288 // objects have a non-zero base.
1289 uptr preferred_base
= (uptr
)-1;
1290 for (char *iter
= phdrs
; iter
!= phdrs_end
; iter
+= ehdr
->e_phentsize
) {
1291 Elf_Phdr
*phdr
= (Elf_Phdr
*)iter
;
1292 if (phdr
->p_type
== PT_LOAD
&& preferred_base
> (uptr
)phdr
->p_vaddr
)
1293 preferred_base
= (uptr
)phdr
->p_vaddr
;
1296 // Compute the delta from the real base to get a relocation delta.
1297 sptr delta
= (uptr
)base
- preferred_base
;
1298 // Now we can figure out what the loader really mapped.
1299 for (char *iter
= phdrs
; iter
!= phdrs_end
; iter
+= ehdr
->e_phentsize
) {
1300 Elf_Phdr
*phdr
= (Elf_Phdr
*)iter
;
1301 if (phdr
->p_type
== PT_LOAD
) {
1302 uptr seg_start
= phdr
->p_vaddr
+ delta
;
1303 uptr seg_end
= seg_start
+ phdr
->p_memsz
;
1304 // None of these values are aligned. We consider the ragged edges of the
1305 // load command as defined, since they are mapped from the file.
1306 seg_start
= RoundDownTo(seg_start
, GetPageSizeCached());
1307 seg_end
= RoundUpTo(seg_end
, GetPageSizeCached());
1308 cb((void *)seg_start
, seg_end
- seg_start
);
1314 # if SANITIZER_LINUX
1315 # if defined(__x86_64__)
1316 // We cannot use glibc's clone wrapper, because it messes with the child
1317 // task's TLS. It writes the PID and TID of the child task to its thread
1318 // descriptor, but in our case the child task shares the thread descriptor with
1319 // the parent (because we don't know how to allocate a new thread
1320 // descriptor to keep glibc happy). So the stock version of clone(), when
1321 // used with CLONE_VM, would end up corrupting the parent's thread descriptor.
1322 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1323 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1325 if (!fn
|| !child_stack
)
1327 CHECK_EQ(0, (uptr
)child_stack
% 16);
1328 child_stack
= (char *)child_stack
- 2 * sizeof(unsigned long long);
1329 ((unsigned long long *)child_stack
)[0] = (uptr
)fn
;
1330 ((unsigned long long *)child_stack
)[1] = (uptr
)arg
;
1331 register void *r8
__asm__("r8") = newtls
;
1332 register int *r10
__asm__("r10") = child_tidptr
;
1333 __asm__
__volatile__(
1334 /* %rax = syscall(%rax = SYSCALL(clone),
1336 * %rsi = child_stack,
1337 * %rdx = parent_tidptr,
1339 * %r10 = child_tidptr)
1346 "testq %%rax,%%rax\n"
1349 /* In the child. Terminate unwind chain. */
1350 // XXX: We should also terminate the CFI unwind chain
1351 // here. Unfortunately clang 3.2 doesn't support the
1352 // necessary CFI directives, so we skip that part.
1353 "xorq %%rbp,%%rbp\n"
1355 /* Call "fn(arg)". */
1360 /* Call _exit(%rax). */
1361 "movq %%rax,%%rdi\n"
1365 /* Return to parent. */
1368 : "a"(SYSCALL(clone
)), "i"(SYSCALL(exit
)), "S"(child_stack
), "D"(flags
),
1369 "d"(parent_tidptr
), "r"(r8
), "r"(r10
)
1370 : "memory", "r11", "rcx");
1373 # elif defined(__mips__)
1374 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1375 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1377 if (!fn
|| !child_stack
)
1379 CHECK_EQ(0, (uptr
)child_stack
% 16);
1380 child_stack
= (char *)child_stack
- 2 * sizeof(unsigned long long);
1381 ((unsigned long long *)child_stack
)[0] = (uptr
)fn
;
1382 ((unsigned long long *)child_stack
)[1] = (uptr
)arg
;
1383 register void *a3
__asm__("$7") = newtls
;
1384 register int *a4
__asm__("$8") = child_tidptr
;
1385 // We don't have proper CFI directives here because it requires alot of code
1386 // for very marginal benefits.
1387 __asm__
__volatile__(
1388 /* $v0 = syscall($v0 = __NR_clone,
1390 * $a1 = child_stack,
1391 * $a2 = parent_tidptr,
1393 * $a4 = child_tidptr)
1400 /* Store the fifth argument on stack
1401 * if we are using 32-bit abi.
1403 # if SANITIZER_WORDSIZE == 32
1416 /* Call "fn(arg)". */
1417 # if SANITIZER_WORDSIZE == 32
1418 # ifdef __BIG_ENDIAN__
1431 /* Call _exit($v0). */
1436 /* Return to parent. */
1439 : "r"(flags
), "r"(child_stack
), "r"(parent_tidptr
), "r"(a3
), "r"(a4
),
1440 "i"(__NR_clone
), "i"(__NR_exit
)
1444 # elif SANITIZER_RISCV64
1445 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1446 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1447 if (!fn
|| !child_stack
)
1450 CHECK_EQ(0, (uptr
)child_stack
% 16);
1452 register int res
__asm__("a0");
1453 register int __flags
__asm__("a0") = flags
;
1454 register void *__stack
__asm__("a1") = child_stack
;
1455 register int *__ptid
__asm__("a2") = parent_tidptr
;
1456 register void *__tls
__asm__("a3") = newtls
;
1457 register int *__ctid
__asm__("a4") = child_tidptr
;
1458 register int (*__fn
)(void *) __asm__("a5") = fn
;
1459 register void *__arg
__asm__("a6") = arg
;
1460 register int nr_clone
__asm__("a7") = __NR_clone
;
1462 __asm__
__volatile__(
1470 // In the child, now. Call "fn(arg)".
1475 "addi a7, zero, %9\n"
1480 : "0"(__flags
), "r"(__stack
), "r"(__ptid
), "r"(__tls
), "r"(__ctid
),
1481 "r"(__fn
), "r"(__arg
), "r"(nr_clone
), "i"(__NR_exit
)
1485 # elif defined(__aarch64__)
1486 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1487 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1488 register long long res
__asm__("x0");
1489 if (!fn
|| !child_stack
)
1491 CHECK_EQ(0, (uptr
)child_stack
% 16);
1492 child_stack
= (char *)child_stack
- 2 * sizeof(unsigned long long);
1493 ((unsigned long long *)child_stack
)[0] = (uptr
)fn
;
1494 ((unsigned long long *)child_stack
)[1] = (uptr
)arg
;
1496 register int (*__fn
)(void *) __asm__("x0") = fn
;
1497 register void *__stack
__asm__("x1") = child_stack
;
1498 register int __flags
__asm__("x2") = flags
;
1499 register void *__arg
__asm__("x3") = arg
;
1500 register int *__ptid
__asm__("x4") = parent_tidptr
;
1501 register void *__tls
__asm__("x5") = newtls
;
1502 register int *__ctid
__asm__("x6") = child_tidptr
;
1504 __asm__
__volatile__(
1505 "mov x0,x2\n" /* flags */
1506 "mov x2,x4\n" /* ptid */
1507 "mov x3,x5\n" /* tls */
1508 "mov x4,x6\n" /* ctid */
1509 "mov x8,%9\n" /* clone */
1519 /* In the child, now. Call "fn(arg)". */
1520 "ldp x1, x0, [sp], #16\n"
1523 /* Call _exit(%r0). */
1529 : "i"(-EINVAL
), "r"(__fn
), "r"(__stack
), "r"(__flags
), "r"(__arg
),
1530 "r"(__ptid
), "r"(__tls
), "r"(__ctid
), "i"(__NR_clone
), "i"(__NR_exit
)
1534 # elif SANITIZER_LOONGARCH64
1535 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1536 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1537 if (!fn
|| !child_stack
)
1540 CHECK_EQ(0, (uptr
)child_stack
% 16);
1542 register int res
__asm__("$a0");
1543 register int __flags
__asm__("$a0") = flags
;
1544 register void *__stack
__asm__("$a1") = child_stack
;
1545 register int *__ptid
__asm__("$a2") = parent_tidptr
;
1546 register int *__ctid
__asm__("$a3") = child_tidptr
;
1547 register void *__tls
__asm__("$a4") = newtls
;
1548 register int (*__fn
)(void *) __asm__("$a5") = fn
;
1549 register void *__arg
__asm__("$a6") = arg
;
1550 register int nr_clone
__asm__("$a7") = __NR_clone
;
1552 __asm__
__volatile__(
1559 // In the child, now. Call "fn(arg)".
1561 "jirl $ra, $a5, 0\n"
1564 "addi.d $a7, $zero, %9\n"
1570 : "0"(__flags
), "r"(__stack
), "r"(__ptid
), "r"(__ctid
), "r"(__tls
),
1571 "r"(__fn
), "r"(__arg
), "r"(nr_clone
), "i"(__NR_exit
)
1572 : "memory", "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7",
1576 # elif defined(__powerpc64__)
1577 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1578 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1580 // Stack frame structure.
1581 # if SANITIZER_PPC64V1
1582 // Back chain == 0 (SP + 112)
1583 // Frame (112 bytes):
1584 // Parameter save area (SP + 48), 8 doublewords
1585 // TOC save area (SP + 40)
1586 // Link editor doubleword (SP + 32)
1587 // Compiler doubleword (SP + 24)
1588 // LR save area (SP + 16)
1589 // CR save area (SP + 8)
1590 // Back chain (SP + 0)
1591 # define FRAME_SIZE 112
1592 # define FRAME_TOC_SAVE_OFFSET 40
1593 # elif SANITIZER_PPC64V2
1594 // Back chain == 0 (SP + 32)
1595 // Frame (32 bytes):
1596 // TOC save area (SP + 24)
1597 // LR save area (SP + 16)
1598 // CR save area (SP + 8)
1599 // Back chain (SP + 0)
1600 # define FRAME_SIZE 32
1601 # define FRAME_TOC_SAVE_OFFSET 24
1603 # error "Unsupported PPC64 ABI"
1605 if (!fn
|| !child_stack
)
1607 CHECK_EQ(0, (uptr
)child_stack
% 16);
1609 register int (*__fn
)(void *) __asm__("r3") = fn
;
1610 register void *__cstack
__asm__("r4") = child_stack
;
1611 register int __flags
__asm__("r5") = flags
;
1612 register void *__arg
__asm__("r6") = arg
;
1613 register int *__ptidptr
__asm__("r7") = parent_tidptr
;
1614 register void *__newtls
__asm__("r8") = newtls
;
1615 register int *__ctidptr
__asm__("r9") = child_tidptr
;
1617 __asm__
__volatile__(
1618 /* fn and arg are saved across the syscall */
1628 r7 == child_tidptr */
1636 /* Test if syscall was successful */
1637 "cmpdi cr1, 3, 0\n\t"
1638 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1641 /* Set up stack frame */
1643 "stdu 29, -8(1)\n\t"
1644 "stdu 1, -%12(1)\n\t"
1645 /* Do the function call */
1647 # if SANITIZER_PPC64V1
1651 # elif SANITIZER_PPC64V2
1655 # error "Unsupported PPC64 ABI"
1661 /* Call _exit(r3) */
1665 /* Return to parent */
1669 : "0"(-1), "i"(EINVAL
), "i"(__NR_clone
), "i"(__NR_exit
), "r"(__fn
),
1670 "r"(__cstack
), "r"(__flags
), "r"(__arg
), "r"(__ptidptr
), "r"(__newtls
),
1671 "r"(__ctidptr
), "i"(FRAME_SIZE
), "i"(FRAME_TOC_SAVE_OFFSET
)
1672 : "cr0", "cr1", "memory", "ctr", "r0", "r27", "r28", "r29");
1675 # elif defined(__i386__)
1676 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1677 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1679 if (!fn
|| !child_stack
)
1681 CHECK_EQ(0, (uptr
)child_stack
% 16);
1682 child_stack
= (char *)child_stack
- 7 * sizeof(unsigned int);
1683 ((unsigned int *)child_stack
)[0] = (uptr
)flags
;
1684 ((unsigned int *)child_stack
)[1] = (uptr
)0;
1685 ((unsigned int *)child_stack
)[2] = (uptr
)fn
;
1686 ((unsigned int *)child_stack
)[3] = (uptr
)arg
;
1687 __asm__
__volatile__(
1688 /* %eax = syscall(%eax = SYSCALL(clone),
1690 * %ecx = child_stack,
1691 * %edx = parent_tidptr,
1693 * %edi = child_tidptr)
1697 "movl (%%ecx), %%ebx\n"
1698 /* Do the system call */
1702 /* Remember the flag value. */
1703 "movl %%ebx, (%%ecx)\n"
1713 "test %%eax,%%eax\n"
1716 /* terminate the stack frame */
1717 "xorl %%ebp,%%ebp\n"
1724 "addl $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n"
1727 "movl %%eax, %%ebx\n"
1732 : "a"(SYSCALL(clone
)), "i"(SYSCALL(exit
)), "c"(child_stack
),
1733 "d"(parent_tidptr
), "S"(newtls
), "D"(child_tidptr
)
1737 # elif defined(__arm__)
1738 uptr
internal_clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
,
1739 int *parent_tidptr
, void *newtls
, int *child_tidptr
) {
1741 if (!fn
|| !child_stack
)
1743 child_stack
= (char *)child_stack
- 2 * sizeof(unsigned int);
1744 ((unsigned int *)child_stack
)[0] = (uptr
)fn
;
1745 ((unsigned int *)child_stack
)[1] = (uptr
)arg
;
1746 register int r0
__asm__("r0") = flags
;
1747 register void *r1
__asm__("r1") = child_stack
;
1748 register int *r2
__asm__("r2") = parent_tidptr
;
1749 register void *r3
__asm__("r3") = newtls
;
1750 register int *r4
__asm__("r4") = child_tidptr
;
1751 register int r7
__asm__("r7") = __NR_clone
;
1753 # if __ARM_ARCH > 4 || defined(__ARM_ARCH_4T__)
1754 # define ARCH_HAS_BX
1757 # define ARCH_HAS_BLX
1761 # ifdef ARCH_HAS_BLX
1762 # define BLX(R) "blx " #R "\n"
1764 # define BLX(R) "mov lr, pc; bx " #R "\n"
1767 # define BLX(R) "mov lr, pc; mov pc," #R "\n"
1770 __asm__
__volatile__(
1771 /* %r0 = syscall(%r7 = SYSCALL(clone),
1773 * %r1 = child_stack,
1774 * %r2 = parent_tidptr,
1776 * %r4 = child_tidptr)
1779 /* Do the system call */
1788 /* In the child, now. Call "fn(arg)". */
1789 "ldr r0, [sp, #4]\n"
1790 "ldr ip, [sp], #8\n" BLX(ip
)
1791 /* Call _exit(%r0). */
1797 : "r"(r0
), "r"(r1
), "r"(r2
), "r"(r3
), "r"(r4
), "r"(r7
), "i"(__NR_exit
)
1802 # endif // SANITIZER_LINUX
1804 # if SANITIZER_LINUX
1805 int internal_uname(struct utsname
*buf
) {
1806 return internal_syscall(SYSCALL(uname
), buf
);
1810 # if SANITIZER_ANDROID
1811 # if __ANDROID_API__ < 21
1812 extern "C" __attribute__((weak
)) int dl_iterate_phdr(
1813 int (*)(struct dl_phdr_info
*, size_t, void *), void *);
1816 static int dl_iterate_phdr_test_cb(struct dl_phdr_info
*info
, size_t size
,
1818 // Any name starting with "lib" indicates a bug in L where library base names
1819 // are returned instead of paths.
1820 if (info
->dlpi_name
&& info
->dlpi_name
[0] == 'l' &&
1821 info
->dlpi_name
[1] == 'i' && info
->dlpi_name
[2] == 'b') {
1822 *(bool *)data
= true;
1828 static atomic_uint32_t android_api_level
;
1830 static AndroidApiLevel
AndroidDetectApiLevelStatic() {
1831 # if __ANDROID_API__ <= 19
1832 return ANDROID_KITKAT
;
1833 # elif __ANDROID_API__ <= 22
1834 return ANDROID_LOLLIPOP_MR1
;
1836 return ANDROID_POST_LOLLIPOP
;
1840 static AndroidApiLevel
AndroidDetectApiLevel() {
1841 if (!&dl_iterate_phdr
)
1842 return ANDROID_KITKAT
; // K or lower
1843 bool base_name_seen
= false;
1844 dl_iterate_phdr(dl_iterate_phdr_test_cb
, &base_name_seen
);
1846 return ANDROID_LOLLIPOP_MR1
; // L MR1
1847 return ANDROID_POST_LOLLIPOP
; // post-L
1848 // Plain L (API level 21) is completely broken wrt ASan and not very
1849 // interesting to detect.
1852 extern "C" __attribute__((weak
)) void *_DYNAMIC
;
1854 AndroidApiLevel
AndroidGetApiLevel() {
1855 AndroidApiLevel level
=
1856 (AndroidApiLevel
)atomic_load(&android_api_level
, memory_order_relaxed
);
1859 level
= &_DYNAMIC
== nullptr ? AndroidDetectApiLevelStatic()
1860 : AndroidDetectApiLevel();
1861 atomic_store(&android_api_level
, level
, memory_order_relaxed
);
1867 static HandleSignalMode
GetHandleSignalModeImpl(int signum
) {
1870 return common_flags()->handle_abort
;
1872 return common_flags()->handle_sigill
;
1874 return common_flags()->handle_sigtrap
;
1876 return common_flags()->handle_sigfpe
;
1878 return common_flags()->handle_segv
;
1880 return common_flags()->handle_sigbus
;
1882 return kHandleSignalNo
;
1885 HandleSignalMode
GetHandleSignalMode(int signum
) {
1886 HandleSignalMode result
= GetHandleSignalModeImpl(signum
);
1887 if (result
== kHandleSignalYes
&& !common_flags()->allow_user_segv_handler
)
1888 return kHandleSignalExclusive
;
1893 void *internal_start_thread(void *(*func
)(void *arg
), void *arg
) {
1894 if (&internal_pthread_create
== 0)
1896 // Start the thread with signals blocked, otherwise it can steal user signals.
1897 ScopedBlockSignals
block(nullptr);
1899 internal_pthread_create(&th
, nullptr, func
, arg
);
1903 void internal_join_thread(void *th
) {
1904 if (&internal_pthread_join
)
1905 internal_pthread_join(th
, nullptr);
1908 void *internal_start_thread(void *(*func
)(void *), void *arg
) { return 0; }
1910 void internal_join_thread(void *th
) {}
1913 # if SANITIZER_LINUX && defined(__aarch64__)
1914 // Android headers in the older NDK releases miss this definition.
1915 struct __sanitizer_esr_context
{
1916 struct _aarch64_ctx head
;
1920 static bool Aarch64GetESR(ucontext_t
*ucontext
, u64
*esr
) {
1921 static const u32 kEsrMagic
= 0x45535201;
1922 u8
*aux
= reinterpret_cast<u8
*>(ucontext
->uc_mcontext
.__reserved
);
1924 _aarch64_ctx
*ctx
= (_aarch64_ctx
*)aux
;
1927 if (ctx
->magic
== kEsrMagic
) {
1928 *esr
= ((__sanitizer_esr_context
*)ctx
)->esr
;
1935 # elif SANITIZER_FREEBSD && defined(__aarch64__)
1936 // FreeBSD doesn't provide ESR in the ucontext.
1937 static bool Aarch64GetESR(ucontext_t
*ucontext
, u64
*esr
) { return false; }
1940 using Context
= ucontext_t
;
1942 SignalContext::WriteFlag
SignalContext::GetWriteFlag() const {
1943 Context
*ucontext
= (Context
*)context
;
1944 # if defined(__x86_64__) || defined(__i386__)
1945 static const uptr PF_WRITE
= 1U << 1;
1946 # if SANITIZER_FREEBSD
1947 uptr err
= ucontext
->uc_mcontext
.mc_err
;
1948 # elif SANITIZER_NETBSD
1949 uptr err
= ucontext
->uc_mcontext
.__gregs
[_REG_ERR
];
1950 # elif SANITIZER_SOLARIS && defined(__i386__)
1952 uptr err
= ucontext
->uc_mcontext
.gregs
[Err
];
1954 uptr err
= ucontext
->uc_mcontext
.gregs
[REG_ERR
];
1955 # endif // SANITIZER_FREEBSD
1956 return err
& PF_WRITE
? Write
: Read
;
1957 # elif defined(__mips__)
1958 uint32_t *exception_source
;
1959 uint32_t faulty_instruction
;
1962 exception_source
= (uint32_t *)ucontext
->uc_mcontext
.pc
;
1963 faulty_instruction
= (uint32_t)(*exception_source
);
1965 op_code
= (faulty_instruction
>> 26) & 0x3f;
1967 // FIXME: Add support for FPU, microMIPS, DSP, MSA memory instructions.
1973 # if __mips_isa_rev < 6
1979 return SignalContext::Write
;
1988 # if __mips_isa_rev < 6
1994 return SignalContext::Read
;
1995 # if __mips_isa_rev == 6
1997 op_code
= (faulty_instruction
>> 19) & 0x3;
2001 return SignalContext::Read
;
2005 return SignalContext::Unknown
;
2006 # elif defined(__arm__)
2007 static const uptr FSR_WRITE
= 1U << 11;
2008 uptr fsr
= ucontext
->uc_mcontext
.error_code
;
2009 return fsr
& FSR_WRITE
? Write
: Read
;
2010 # elif defined(__aarch64__)
2011 static const u64 ESR_ELx_WNR
= 1U << 6;
2013 if (!Aarch64GetESR(ucontext
, &esr
))
2015 return esr
& ESR_ELx_WNR
? Write
: Read
;
2016 # elif defined(__loongarch__)
2017 // In the musl environment, the Linux kernel uapi sigcontext.h is not
2018 // included in signal.h. To avoid missing the SC_ADDRERR_{RD,WR} macros,
2019 // copy them here. The LoongArch Linux kernel uapi is already stable,
2020 // so there's no need to worry about the value changing.
2021 # ifndef SC_ADDRERR_RD
2022 // Address error was due to memory load
2023 # define SC_ADDRERR_RD (1 << 30)
2025 # ifndef SC_ADDRERR_WR
2026 // Address error was due to memory store
2027 # define SC_ADDRERR_WR (1 << 31)
2029 u32 flags
= ucontext
->uc_mcontext
.__flags
;
2030 if (flags
& SC_ADDRERR_RD
)
2031 return SignalContext::Read
;
2032 if (flags
& SC_ADDRERR_WR
)
2033 return SignalContext::Write
;
2034 return SignalContext::Unknown
;
2035 # elif defined(__sparc__)
2036 // Decode the instruction to determine the access type.
2037 // From OpenSolaris $SRC/uts/sun4/os/trap.c (get_accesstype).
2038 # if SANITIZER_SOLARIS
2039 uptr pc
= ucontext
->uc_mcontext
.gregs
[REG_PC
];
2041 // Historical BSDism here.
2042 struct sigcontext
*scontext
= (struct sigcontext
*)context
;
2043 # if defined(__arch64__)
2044 uptr pc
= scontext
->sigc_regs
.tpc
;
2046 uptr pc
= scontext
->si_regs
.pc
;
2049 u32 instr
= *(u32
*)pc
;
2050 return (instr
>> 21) & 1 ? Write
: Read
;
2051 # elif defined(__riscv)
2052 # if SANITIZER_FREEBSD
2053 unsigned long pc
= ucontext
->uc_mcontext
.mc_gpregs
.gp_sepc
;
2055 unsigned long pc
= ucontext
->uc_mcontext
.__gregs
[REG_PC
];
2057 unsigned faulty_instruction
= *(uint16_t *)pc
;
2059 # if defined(__riscv_compressed)
2060 if ((faulty_instruction
& 0x3) != 0x3) { // it's a compressed instruction
2061 // set op_bits to the instruction bits [1, 0, 15, 14, 13]
2063 ((faulty_instruction
& 0x3) << 3) | (faulty_instruction
>> 13);
2064 unsigned rd
= faulty_instruction
& 0xF80; // bits 7-11, inclusive
2066 case 0b10'010: // c.lwsp (rd != x0)
2067 # if __riscv_xlen == 64
2068 case 0b10'011: // c.ldsp (rd != x0)
2070 return rd
? SignalContext::Read
: SignalContext::Unknown
;
2071 case 0b00'010: // c.lw
2072 # if __riscv_flen >= 32 && __riscv_xlen == 32
2073 case 0b10'011: // c.flwsp
2075 # if __riscv_flen >= 32 || __riscv_xlen == 64
2076 case 0b00'011: // c.flw / c.ld
2078 # if __riscv_flen == 64
2079 case 0b00'001: // c.fld
2080 case 0b10'001: // c.fldsp
2082 return SignalContext::Read
;
2083 case 0b00'110: // c.sw
2084 case 0b10'110: // c.swsp
2085 # if __riscv_flen >= 32 || __riscv_xlen == 64
2086 case 0b00'111: // c.fsw / c.sd
2087 case 0b10'111: // c.fswsp / c.sdsp
2089 # if __riscv_flen == 64
2090 case 0b00'101: // c.fsd
2091 case 0b10'101: // c.fsdsp
2093 return SignalContext::Write
;
2095 return SignalContext::Unknown
;
2100 unsigned opcode
= faulty_instruction
& 0x7f; // lower 7 bits
2101 unsigned funct3
= (faulty_instruction
>> 12) & 0x7; // bits 12-14, inclusive
2103 case 0b0000011: // loads
2108 # if __riscv_xlen == 64
2113 return SignalContext::Read
;
2115 return SignalContext::Unknown
;
2117 case 0b0100011: // stores
2122 # if __riscv_xlen == 64
2125 return SignalContext::Write
;
2127 return SignalContext::Unknown
;
2129 # if __riscv_flen >= 32
2130 case 0b0000111: // floating-point loads
2133 # if __riscv_flen == 64
2136 return SignalContext::Read
;
2138 return SignalContext::Unknown
;
2140 case 0b0100111: // floating-point stores
2143 # if __riscv_flen == 64
2146 return SignalContext::Write
;
2148 return SignalContext::Unknown
;
2152 return SignalContext::Unknown
;
2156 return Unknown
; // FIXME: Implement.
2160 bool SignalContext::IsTrueFaultingAddress() const {
2161 auto si
= static_cast<const siginfo_t
*>(siginfo
);
2162 // SIGSEGV signals without a true fault address have si_code set to 128.
2163 return si
->si_signo
== SIGSEGV
&& si
->si_code
!= 128;
2167 static const char *RegNumToRegName(int reg
) {
2169 # if SANITIZER_LINUX
2170 # if defined(__x86_64__)
2203 # elif defined(__i386__)
2228 # if SANITIZER_LINUX
2230 static void DumpSingleReg(ucontext_t
*ctx
, int RegNum
) {
2231 const char *RegName
= RegNumToRegName(RegNum
);
2232 # if defined(__x86_64__)
2233 Printf("%s%s = 0x%016llx ", internal_strlen(RegName
) == 2 ? " " : "",
2234 RegName
, ctx
->uc_mcontext
.gregs
[RegNum
]);
2235 # elif defined(__i386__)
2236 Printf("%s = 0x%08x ", RegName
, ctx
->uc_mcontext
.gregs
[RegNum
]);
2243 void SignalContext::DumpAllRegisters(void *context
) {
2244 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2245 # if SANITIZER_LINUX
2246 # if defined(__x86_64__)
2247 Report("Register values:\n");
2248 DumpSingleReg(ucontext
, REG_RAX
);
2249 DumpSingleReg(ucontext
, REG_RBX
);
2250 DumpSingleReg(ucontext
, REG_RCX
);
2251 DumpSingleReg(ucontext
, REG_RDX
);
2253 DumpSingleReg(ucontext
, REG_RDI
);
2254 DumpSingleReg(ucontext
, REG_RSI
);
2255 DumpSingleReg(ucontext
, REG_RBP
);
2256 DumpSingleReg(ucontext
, REG_RSP
);
2258 DumpSingleReg(ucontext
, REG_R8
);
2259 DumpSingleReg(ucontext
, REG_R9
);
2260 DumpSingleReg(ucontext
, REG_R10
);
2261 DumpSingleReg(ucontext
, REG_R11
);
2263 DumpSingleReg(ucontext
, REG_R12
);
2264 DumpSingleReg(ucontext
, REG_R13
);
2265 DumpSingleReg(ucontext
, REG_R14
);
2266 DumpSingleReg(ucontext
, REG_R15
);
2268 # elif defined(__i386__)
2269 // Duplication of this report print is caused by partial support
2270 // of register values dumping. In case of unsupported yet architecture let's
2271 // avoid printing 'Register values:' without actual values in the following
2273 Report("Register values:\n");
2274 DumpSingleReg(ucontext
, REG_EAX
);
2275 DumpSingleReg(ucontext
, REG_EBX
);
2276 DumpSingleReg(ucontext
, REG_ECX
);
2277 DumpSingleReg(ucontext
, REG_EDX
);
2279 DumpSingleReg(ucontext
, REG_EDI
);
2280 DumpSingleReg(ucontext
, REG_ESI
);
2281 DumpSingleReg(ucontext
, REG_EBP
);
2282 DumpSingleReg(ucontext
, REG_ESP
);
2287 # elif SANITIZER_FREEBSD
2288 # if defined(__x86_64__)
2289 Report("Register values:\n");
2290 Printf("rax = 0x%016lx ", ucontext
->uc_mcontext
.mc_rax
);
2291 Printf("rbx = 0x%016lx ", ucontext
->uc_mcontext
.mc_rbx
);
2292 Printf("rcx = 0x%016lx ", ucontext
->uc_mcontext
.mc_rcx
);
2293 Printf("rdx = 0x%016lx ", ucontext
->uc_mcontext
.mc_rdx
);
2295 Printf("rdi = 0x%016lx ", ucontext
->uc_mcontext
.mc_rdi
);
2296 Printf("rsi = 0x%016lx ", ucontext
->uc_mcontext
.mc_rsi
);
2297 Printf("rbp = 0x%016lx ", ucontext
->uc_mcontext
.mc_rbp
);
2298 Printf("rsp = 0x%016lx ", ucontext
->uc_mcontext
.mc_rsp
);
2300 Printf(" r8 = 0x%016lx ", ucontext
->uc_mcontext
.mc_r8
);
2301 Printf(" r9 = 0x%016lx ", ucontext
->uc_mcontext
.mc_r9
);
2302 Printf("r10 = 0x%016lx ", ucontext
->uc_mcontext
.mc_r10
);
2303 Printf("r11 = 0x%016lx ", ucontext
->uc_mcontext
.mc_r11
);
2305 Printf("r12 = 0x%016lx ", ucontext
->uc_mcontext
.mc_r12
);
2306 Printf("r13 = 0x%016lx ", ucontext
->uc_mcontext
.mc_r13
);
2307 Printf("r14 = 0x%016lx ", ucontext
->uc_mcontext
.mc_r14
);
2308 Printf("r15 = 0x%016lx ", ucontext
->uc_mcontext
.mc_r15
);
2310 # elif defined(__i386__)
2311 Report("Register values:\n");
2312 Printf("eax = 0x%08x ", ucontext
->uc_mcontext
.mc_eax
);
2313 Printf("ebx = 0x%08x ", ucontext
->uc_mcontext
.mc_ebx
);
2314 Printf("ecx = 0x%08x ", ucontext
->uc_mcontext
.mc_ecx
);
2315 Printf("edx = 0x%08x ", ucontext
->uc_mcontext
.mc_edx
);
2317 Printf("edi = 0x%08x ", ucontext
->uc_mcontext
.mc_edi
);
2318 Printf("esi = 0x%08x ", ucontext
->uc_mcontext
.mc_esi
);
2319 Printf("ebp = 0x%08x ", ucontext
->uc_mcontext
.mc_ebp
);
2320 Printf("esp = 0x%08x ", ucontext
->uc_mcontext
.mc_esp
);
2326 // FIXME: Implement this for other OSes and architectures.
2329 static void GetPcSpBp(void *context
, uptr
*pc
, uptr
*sp
, uptr
*bp
) {
2330 # if SANITIZER_NETBSD
2331 // This covers all NetBSD architectures
2332 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2333 *pc
= _UC_MACHINE_PC(ucontext
);
2334 *bp
= _UC_MACHINE_FP(ucontext
);
2335 *sp
= _UC_MACHINE_SP(ucontext
);
2336 # elif defined(__arm__)
2337 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2338 *pc
= ucontext
->uc_mcontext
.arm_pc
;
2339 *bp
= ucontext
->uc_mcontext
.arm_fp
;
2340 *sp
= ucontext
->uc_mcontext
.arm_sp
;
2341 # elif defined(__aarch64__)
2342 # if SANITIZER_FREEBSD
2343 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2344 *pc
= ucontext
->uc_mcontext
.mc_gpregs
.gp_elr
;
2345 *bp
= ucontext
->uc_mcontext
.mc_gpregs
.gp_x
[29];
2346 *sp
= ucontext
->uc_mcontext
.mc_gpregs
.gp_sp
;
2348 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2349 *pc
= ucontext
->uc_mcontext
.pc
;
2350 *bp
= ucontext
->uc_mcontext
.regs
[29];
2351 *sp
= ucontext
->uc_mcontext
.sp
;
2353 # elif defined(__hppa__)
2354 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2355 *pc
= ucontext
->uc_mcontext
.sc_iaoq
[0];
2356 /* GCC uses %r3 whenever a frame pointer is needed. */
2357 *bp
= ucontext
->uc_mcontext
.sc_gr
[3];
2358 *sp
= ucontext
->uc_mcontext
.sc_gr
[30];
2359 # elif defined(__x86_64__)
2360 # if SANITIZER_FREEBSD
2361 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2362 *pc
= ucontext
->uc_mcontext
.mc_rip
;
2363 *bp
= ucontext
->uc_mcontext
.mc_rbp
;
2364 *sp
= ucontext
->uc_mcontext
.mc_rsp
;
2366 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2367 *pc
= ucontext
->uc_mcontext
.gregs
[REG_RIP
];
2368 *bp
= ucontext
->uc_mcontext
.gregs
[REG_RBP
];
2369 *sp
= ucontext
->uc_mcontext
.gregs
[REG_RSP
];
2371 # elif defined(__i386__)
2372 # if SANITIZER_FREEBSD
2373 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2374 *pc
= ucontext
->uc_mcontext
.mc_eip
;
2375 *bp
= ucontext
->uc_mcontext
.mc_ebp
;
2376 *sp
= ucontext
->uc_mcontext
.mc_esp
;
2378 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2379 # if SANITIZER_SOLARIS
2380 /* Use the numeric values: the symbolic ones are undefined by llvm
2381 include/llvm/Support/Solaris.h. */
2383 # define REG_EIP 14 // REG_PC
2386 # define REG_EBP 6 // REG_FP
2389 # define REG_UESP 17 // REG_SP
2392 *pc
= ucontext
->uc_mcontext
.gregs
[REG_EIP
];
2393 *bp
= ucontext
->uc_mcontext
.gregs
[REG_EBP
];
2394 *sp
= ucontext
->uc_mcontext
.gregs
[REG_UESP
];
2396 # elif defined(__powerpc__) || defined(__powerpc64__)
2397 # if SANITIZER_FREEBSD
2398 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2399 *pc
= ucontext
->uc_mcontext
.mc_srr0
;
2400 *sp
= ucontext
->uc_mcontext
.mc_frame
[1];
2401 *bp
= ucontext
->uc_mcontext
.mc_frame
[31];
2403 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2404 *pc
= ucontext
->uc_mcontext
.regs
->nip
;
2405 *sp
= ucontext
->uc_mcontext
.regs
->gpr
[PT_R1
];
2406 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
2407 // pointer, but GCC always uses r31 when we need a frame pointer.
2408 *bp
= ucontext
->uc_mcontext
.regs
->gpr
[PT_R31
];
2410 # elif defined(__sparc__)
2411 # if defined(__arch64__) || defined(__sparcv9)
2412 # define STACK_BIAS 2047
2414 # define STACK_BIAS 0
2416 # if SANITIZER_SOLARIS
2417 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2418 *pc
= ucontext
->uc_mcontext
.gregs
[REG_PC
];
2419 *sp
= ucontext
->uc_mcontext
.gregs
[REG_O6
] + STACK_BIAS
;
2421 // Historical BSDism here.
2422 struct sigcontext
*scontext
= (struct sigcontext
*)context
;
2423 # if defined(__arch64__)
2424 *pc
= scontext
->sigc_regs
.tpc
;
2425 *sp
= scontext
->sigc_regs
.u_regs
[14] + STACK_BIAS
;
2427 *pc
= scontext
->si_regs
.pc
;
2428 *sp
= scontext
->si_regs
.u_regs
[14];
2431 *bp
= (uptr
)((uhwptr
*)*sp
)[14] + STACK_BIAS
;
2432 # elif defined(__mips__)
2433 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2434 *pc
= ucontext
->uc_mcontext
.pc
;
2435 *bp
= ucontext
->uc_mcontext
.gregs
[30];
2436 *sp
= ucontext
->uc_mcontext
.gregs
[29];
2437 # elif defined(__s390__)
2438 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2439 # if defined(__s390x__)
2440 *pc
= ucontext
->uc_mcontext
.psw
.addr
;
2442 *pc
= ucontext
->uc_mcontext
.psw
.addr
& 0x7fffffff;
2444 *bp
= ucontext
->uc_mcontext
.gregs
[11];
2445 *sp
= ucontext
->uc_mcontext
.gregs
[15];
2446 # elif defined(__riscv)
2447 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2448 # if SANITIZER_FREEBSD
2449 *pc
= ucontext
->uc_mcontext
.mc_gpregs
.gp_sepc
;
2450 *bp
= ucontext
->uc_mcontext
.mc_gpregs
.gp_s
[0];
2451 *sp
= ucontext
->uc_mcontext
.mc_gpregs
.gp_sp
;
2453 *pc
= ucontext
->uc_mcontext
.__gregs
[REG_PC
];
2454 *bp
= ucontext
->uc_mcontext
.__gregs
[REG_S0
];
2455 *sp
= ucontext
->uc_mcontext
.__gregs
[REG_SP
];
2457 # elif defined(__hexagon__)
2458 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2459 *pc
= ucontext
->uc_mcontext
.pc
;
2460 *bp
= ucontext
->uc_mcontext
.r30
;
2461 *sp
= ucontext
->uc_mcontext
.r29
;
2462 # elif defined(__loongarch__)
2463 ucontext_t
*ucontext
= (ucontext_t
*)context
;
2464 *pc
= ucontext
->uc_mcontext
.__pc
;
2465 *bp
= ucontext
->uc_mcontext
.__gregs
[22];
2466 *sp
= ucontext
->uc_mcontext
.__gregs
[3];
2468 # error "Unsupported arch"
2472 void SignalContext::InitPcSpBp() { GetPcSpBp(context
, &pc
, &sp
, &bp
); }
2474 void InitializePlatformEarly() {
2479 # if SANITIZER_NETBSD
2482 uptr len
= sizeof(paxflags
);
2485 mib
[1] = internal_getpid();
2486 mib
[2] = PROC_PID_PAXFLAGS
;
2488 if (UNLIKELY(internal_sysctl(mib
, 3, &paxflags
, &len
, NULL
, 0) == -1)) {
2489 Printf("sysctl failed\n");
2493 if (UNLIKELY(paxflags
& CTL_PROC_PAXFLAGS_ASLR
)) {
2495 "This sanitizer is not compatible with enabled ASLR.\n"
2496 "To disable ASLR, please run \"paxctl +a %s\" and try again.\n",
2500 # elif SANITIZER_FREEBSD
2502 int r
= internal_procctl(P_PID
, 0, PROC_ASLR_STATUS
, &aslr_status
);
2503 if (UNLIKELY(r
== -1)) {
2504 // We're making things less 'dramatic' here since
2505 // the cmd is not necessarily guaranteed to be here
2506 // just yet regarding FreeBSD release
2509 if ((aslr_status
& PROC_ASLR_ACTIVE
) != 0) {
2511 "This sanitizer is not compatible with enabled ASLR "
2512 "and binaries compiled with PIE\n"
2513 "ASLR will be disabled and the program re-executed.\n");
2514 int aslr_ctl
= PROC_ASLR_FORCE_DISABLE
;
2515 CHECK_NE(internal_procctl(P_PID
, 0, PROC_ASLR_CTL
, &aslr_ctl
), -1);
2518 # elif SANITIZER_PPC64V2
2519 // Disable ASLR for Linux PPC64LE.
2520 int old_personality
= personality(0xffffffff);
2521 if (old_personality
!= -1 && (old_personality
& ADDR_NO_RANDOMIZE
) == 0) {
2523 "WARNING: Program is being run with address space layout "
2524 "randomization (ASLR) enabled which prevents the thread and "
2525 "memory sanitizers from working on powerpc64le.\n"
2526 "ASLR will be disabled and the program re-executed.\n");
2527 CHECK_NE(personality(old_personality
| ADDR_NO_RANDOMIZE
), -1);
2535 void CheckMPROTECT() {
2536 # if SANITIZER_NETBSD
2539 uptr len
= sizeof(paxflags
);
2542 mib
[1] = internal_getpid();
2543 mib
[2] = PROC_PID_PAXFLAGS
;
2545 if (UNLIKELY(internal_sysctl(mib
, 3, &paxflags
, &len
, NULL
, 0) == -1)) {
2546 Printf("sysctl failed\n");
2550 if (UNLIKELY(paxflags
& CTL_PROC_PAXFLAGS_MPROTECT
)) {
2551 Printf("This sanitizer is not compatible with enabled MPROTECT\n");
2559 void CheckNoDeepBind(const char *filename
, int flag
) {
2560 # ifdef RTLD_DEEPBIND
2561 if (flag
& RTLD_DEEPBIND
) {
2563 "You are trying to dlopen a %s shared library with RTLD_DEEPBIND flag"
2564 " which is incompatible with sanitizer runtime "
2565 "(see https://github.com/google/sanitizers/issues/611 for details"
2566 "). If you want to run %s library under sanitizers please remove "
2567 "RTLD_DEEPBIND from dlopen flags.\n",
2568 filename
, filename
);
2574 uptr
FindAvailableMemoryRange(uptr size
, uptr alignment
, uptr left_padding
,
2575 uptr
*largest_gap_found
,
2576 uptr
*max_occupied_addr
) {
2577 UNREACHABLE("FindAvailableMemoryRange is not available");
2581 bool GetRandom(void *buffer
, uptr length
, bool blocking
) {
2582 if (!buffer
|| !length
|| length
> 256)
2584 # if SANITIZER_USE_GETENTROPY
2585 uptr rnd
= getentropy(buffer
, length
);
2587 if (internal_iserror(rnd
, &rverrno
) && rverrno
== EFAULT
)
2591 # endif // SANITIZER_USE_GETENTROPY
2593 # if SANITIZER_USE_GETRANDOM
2594 static atomic_uint8_t skip_getrandom_syscall
;
2595 if (!atomic_load_relaxed(&skip_getrandom_syscall
)) {
2596 // Up to 256 bytes, getrandom will not be interrupted.
2597 uptr res
= internal_syscall(SYSCALL(getrandom
), buffer
, length
,
2598 blocking
? 0 : GRND_NONBLOCK
);
2600 if (internal_iserror(res
, &rverrno
) && rverrno
== ENOSYS
)
2601 atomic_store_relaxed(&skip_getrandom_syscall
, 1);
2602 else if (res
== length
)
2605 # endif // SANITIZER_USE_GETRANDOM
2606 // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
2607 // blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
2608 uptr fd
= internal_open("/dev/urandom", O_RDONLY
);
2609 if (internal_iserror(fd
))
2611 uptr res
= internal_read(fd
, buffer
, length
);
2612 if (internal_iserror(res
))
2618 } // namespace __sanitizer