Vendor import of llvm-project branch release/19.x llvmorg-19.1.1-0-gd401987fe349...
[freebsd/src.git] / compiler-rt / lib / sanitizer_common / sanitizer_linux.cpp
blobb9b1f496df7c984d186ba9df757f4a407545db87
1 //===-- sanitizer_linux.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries and implements linux-specific functions from
11 // sanitizer_libc.h.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
17 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"
29 # if SANITIZER_LINUX && !SANITIZER_GO
30 # include <asm/param.h>
31 # endif
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
44 # endif
45 # if SANITIZER_GO
46 # undef st_atime
47 # undef st_mtime
48 # undef st_ctime
49 # define st_atime st_atim
50 # define st_mtime st_mtim
51 # define st_ctime st_ctim
52 # endif
53 # include <asm/stat.h>
54 # undef stat
55 # undef stat64
56 # endif
58 # include <dlfcn.h>
59 # include <errno.h>
60 # include <fcntl.h>
61 # include <link.h>
62 # include <pthread.h>
63 # include <sched.h>
64 # include <signal.h>
65 # include <sys/mman.h>
66 # if !SANITIZER_SOLARIS
67 # include <sys/ptrace.h>
68 # endif
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>
75 # include <unistd.h>
77 # if SANITIZER_LINUX
78 # include <sys/utsname.h>
79 # endif
81 # if SANITIZER_LINUX && !SANITIZER_ANDROID
82 # include <sys/personality.h>
83 # endif
85 # if SANITIZER_LINUX && defined(__loongarch__)
86 # include <sys/sysmacros.h>
87 # endif
89 # if SANITIZER_FREEBSD
90 # include <machine/atomic.h>
91 # include <sys/exec.h>
92 # include <sys/procctl.h>
93 # include <sys/sysctl.h>
94 extern "C" {
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>
99 # include <sys/thr.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
110 # include <stdlib.h>
111 # include <thread.h>
112 # define environ _environ
113 # endif
115 extern char **environ;
117 # if SANITIZER_LINUX
118 // <linux/time.h>
119 struct kernel_timeval {
120 long tv_sec;
121 long tv_usec;
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
139 # else
140 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
141 # endif
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
147 # endif
148 # define SANITIZER_USE_GETRANDOM 1
149 # else
150 # define SANITIZER_USE_GETRANDOM 0
151 # endif // SANITIZER_LINUX && defined(__NR_getrandom)
153 # if SANITIZER_FREEBSD
154 # define SANITIZER_USE_GETENTROPY 1
155 # endif
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);
172 # endif
173 # if SANITIZER_LINUX
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
176 // hang.
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);
187 # endif
189 SetSigProcMask(&set, oldset);
192 ScopedBlockSignals::ScopedBlockSignals(__sanitizer_sigset_t *copy) {
193 BlockSignals(&saved_);
194 if (copy)
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"
212 # else
213 # include "sanitizer_syscall_generic.inc"
214 # endif
216 // --------------- sanitizer_libc.h
217 # if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
218 # if !SANITIZER_S390
219 uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
220 u64 offset) {
221 # if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
222 return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd,
223 offset);
224 # else
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));
229 # endif
231 # endif // !SANITIZER_S390
233 uptr internal_munmap(void *addr, uptr length) {
234 return internal_syscall(SYSCALL(munmap), (uptr)addr, length);
237 # if SANITIZER_LINUX
238 uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags,
239 void *new_address) {
240 return internal_syscall(SYSCALL(mremap), (uptr)old_address, old_size,
241 new_size, flags, (uptr)new_address);
243 # endif
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) {
256 # if SANITIZER_LINUX
257 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags);
258 # else
259 return internal_syscall(SYSCALL(open), (uptr)filename, flags);
260 # endif
263 uptr internal_open(const char *filename, int flags, u32 mode) {
264 # if SANITIZER_LINUX
265 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags,
266 mode);
267 # else
268 return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode);
269 # endif
272 uptr internal_read(fd_t fd, void *buf, uptr count) {
273 sptr res;
274 HANDLE_EINTR(res,
275 (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf, count));
276 return res;
279 uptr internal_write(fd_t fd, const void *buf, uptr count) {
280 sptr res;
281 HANDLE_EINTR(res,
282 (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf, count));
283 return res;
286 uptr internal_ftruncate(fd_t fd, uptr size) {
287 sptr res;
288 HANDLE_EINTR(res,
289 (sptr)internal_syscall(SYSCALL(ftruncate), fd, (OFF_T)size));
290 return res;
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;
310 # endif
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;
332 # endif
334 # if SANITIZER_MIPS64 || SANITIZER_SPARC64
335 # if SANITIZER_MIPS64
336 typedef struct kernel_stat kstat_t;
337 # else
338 typedef struct kernel_stat64 kstat_t;
339 # endif
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
343 # if !SANITIZER_GO
344 # undef st_atime
345 # undef st_mtime
346 # undef st_ctime
347 # endif
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
356 # endif
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;
377 # else
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;
384 # endif
386 # endif
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__)
393 struct statx bufx;
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);
397 return res;
398 # elif (SANITIZER_WORDSIZE == 64 || SANITIZER_X32 || \
399 (defined(__mips__) && _MIPS_SIM == _ABIN32)) && \
400 !SANITIZER_SPARC
401 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, (uptr)buf,
403 # elif SANITIZER_SPARC64
404 kstat_t buf64;
405 int res = internal_syscall(SYSCALL(fstatat64), AT_FDCWD, (uptr)path,
406 (uptr)&buf64, 0);
407 kernel_stat_to_stat(&buf64, (struct stat *)buf);
408 return res;
409 # else
410 struct stat64 buf64;
411 int res = internal_syscall(SYSCALL(fstatat64), AT_FDCWD, (uptr)path,
412 (uptr)&buf64, 0);
413 stat64_to_stat(&buf64, (struct stat *)buf);
414 return res;
415 # endif
416 # else
417 struct stat64 buf64;
418 int res = internal_syscall(SYSCALL(stat64), path, &buf64);
419 stat64_to_stat(&buf64, (struct stat *)buf);
420 return res;
421 # endif
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__)
430 struct statx bufx;
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);
435 return res;
436 # elif (defined(_LP64) || SANITIZER_X32 || \
437 (defined(__mips__) && _MIPS_SIM == _ABIN32)) && \
438 !SANITIZER_SPARC
439 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, (uptr)buf,
440 AT_SYMLINK_NOFOLLOW);
441 # elif SANITIZER_SPARC64
442 kstat_t buf64;
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);
446 return res;
447 # else
448 struct stat64 buf64;
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);
452 return res;
453 # endif
454 # else
455 struct stat64 buf64;
456 int res = internal_syscall(SYSCALL(lstat64), path, &buf64);
457 stat64_to_stat(&buf64, (struct stat *)buf);
458 return res;
459 # endif
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
466 kstat_t kbuf;
467 int res = internal_syscall(SYSCALL(fstat), fd, &kbuf);
468 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
469 return res;
470 # elif SANITIZER_LINUX && SANITIZER_SPARC64
471 // For sparc64, fstat64 syscall fills buffer in the format of kernel_stat64
472 kstat_t kbuf;
473 int res = internal_syscall(SYSCALL(fstat64), fd, &kbuf);
474 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
475 return res;
476 # elif SANITIZER_LINUX && defined(__loongarch__)
477 struct statx bufx;
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);
481 return res;
482 # else
483 return internal_syscall(SYSCALL(fstat), fd, (uptr)buf);
484 # endif
485 # else
486 struct stat64 buf64;
487 int res = internal_syscall(SYSCALL(fstat64), fd, &buf64);
488 stat64_to_stat(&buf64, (struct stat *)buf);
489 return res;
490 # endif
493 uptr internal_filesize(fd_t fd) {
494 struct stat st;
495 if (internal_fstat(fd, &st))
496 return -1;
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) {
503 # if SANITIZER_LINUX
504 return internal_syscall(SYSCALL(dup3), oldfd, newfd, 0);
505 # else
506 return internal_syscall(SYSCALL(dup2), oldfd, newfd);
507 # endif
510 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
511 # if SANITIZER_LINUX
512 return internal_syscall(SYSCALL(readlinkat), AT_FDCWD, (uptr)path, (uptr)buf,
513 bufsize);
514 # else
515 return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize);
516 # endif
519 uptr internal_unlink(const char *path) {
520 # if SANITIZER_LINUX
521 return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0);
522 # else
523 return internal_syscall(SYSCALL(unlink), (uptr)path);
524 # endif
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,
530 (uptr)newpath, 0);
531 # elif SANITIZER_LINUX
532 return internal_syscall(SYSCALL(renameat), AT_FDCWD, (uptr)oldpath, AT_FDCWD,
533 (uptr)newpath);
534 # else
535 return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath);
536 # endif
539 uptr internal_sched_yield() { return internal_syscall(SYSCALL(sched_yield)); }
541 void internal_usleep(u64 useconds) {
542 struct timespec ts;
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,
551 (uptr)envp);
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);
559 # else
560 internal_syscall(SYSCALL(exit_group), exitcode);
561 # endif
562 Die(); // Unreachable.
564 # endif // !SANITIZER_NETBSD
566 // ----------------- sanitizer_common.h
567 bool FileExists(const char *filename) {
568 if (ShouldMockFailureToOpen(filename))
569 return false;
570 struct stat st;
571 if (internal_stat(filename, &st))
572 return false;
573 // Sanity check: filename is a regular file.
574 return S_ISREG(st.st_mode);
577 bool DirExists(const char *path) {
578 struct stat st;
579 if (internal_stat(path, &st))
580 return false;
581 return S_ISDIR(st.st_mode);
584 # if !SANITIZER_NETBSD
585 tid_t GetTid() {
586 # if SANITIZER_FREEBSD
587 long Tid;
588 thr_self(&Tid);
589 return Tid;
590 # elif SANITIZER_SOLARIS
591 return thr_self();
592 # else
593 return internal_syscall(SYSCALL(gettid));
594 # endif
597 int TgKill(pid_t pid, tid_t tid, int sig) {
598 # if SANITIZER_LINUX
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
603 (void)pid;
604 errno = thr_kill(tid, sig);
605 // TgKill is expected to return -1 on error, not an errno.
606 return errno != 0 ? -1 : 0;
607 # endif
609 # endif
611 # if SANITIZER_GLIBC
612 u64 NanoTime() {
613 kernel_timeval tv;
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
623 u64 NanoTime() {
624 struct timespec ts;
625 clock_gettime(CLOCK_REALTIME, &ts);
626 return (u64)ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
628 # endif
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;
645 static uptr len;
646 static bool inited;
647 if (!inited) {
648 inited = true;
649 uptr environ_size;
650 if (!ReadFileToBuffer("/proc/self/environ", &environ, &environ_size, &len))
651 environ = nullptr;
653 if (!environ || len == 0)
654 return nullptr;
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
661 return nullptr;
662 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
663 return p + namelen + 1; // point after =
664 p = endp + 1;
666 return nullptr; // Not found.
667 # else
668 # error "Unsupported platform"
669 # endif
672 # if !SANITIZER_FREEBSD && !SANITIZER_NETBSD && !SANITIZER_GO
673 extern "C" {
674 SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
676 # endif
678 # if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
679 static void ReadNullSepFileToArray(const char *path, char ***arr,
680 int arr_size) {
681 char *buff;
682 uptr buff_size;
683 uptr buff_len;
684 *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
685 if (!ReadFileToBuffer(path, &buff, &buff_size, &buff_len, 1024 * 1024)) {
686 (*arr)[0] = nullptr;
687 return;
689 (*arr)[0] = buff;
690 int count, i;
691 for (count = 1, i = 1;; i++) {
692 if (buff[i] == 0) {
693 if (buff[i + 1] == 0)
694 break;
695 (*arr)[count] = &buff[i + 1];
696 CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible.
697 count++;
700 (*arr)[count] = nullptr;
702 # endif
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>.
709 ps_strings *pss;
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");
713 Die();
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
721 # if !SANITIZER_GO
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.
728 int argc = 0;
729 while (stack_end[argc + 1]) argc++;
730 *argv = (char **)(stack_end + 1);
731 *envp = (char **)(stack_end + argc + 2);
732 } else {
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);
737 # if !SANITIZER_GO
739 # endif // !SANITIZER_GO
740 # endif // SANITIZER_FREEBSD
743 char **GetArgv() {
744 char **argv, **envp;
745 GetArgsAndEnv(&argv, &envp);
746 return argv;
749 char **GetEnviron() {
750 char **argv, **envp;
751 GetArgsAndEnv(&argv, &envp);
752 return 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 */
761 # else
762 internal_syscall(SYSCALL(futex), (uptr)p, FUTEX_WAIT_PRIVATE, cmp, 0, 0, 0);
763 # endif
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 */
771 # else
772 internal_syscall(SYSCALL(futex), (uptr)p, FUTEX_WAKE_PRIVATE, count, 0, 0, 0);
773 # endif
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
783 // Not used
784 # else
785 struct linux_dirent {
786 # if SANITIZER_X32 || SANITIZER_LINUX
787 u64 d_ino;
788 u64 d_off;
789 # else
790 unsigned long d_ino;
791 unsigned long d_off;
792 # endif
793 unsigned short d_reclen;
794 # if SANITIZER_LINUX
795 unsigned char d_type;
796 # endif
797 char d_name[256];
799 # endif
801 # if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
802 // Syscall wrappers.
803 uptr internal_ptrace(int request, int pid, void *addr, void *data) {
804 return internal_syscall(SYSCALL(ptrace), request, pid, (uptr)addr,
805 (uptr)data);
808 uptr internal_waitpid(int pid, int *status, int options) {
809 return internal_syscall(SYSCALL(wait4), pid, (uptr)status, options,
810 0 /* rusage */);
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);
820 # else
821 UNIMPLEMENTED();
822 # endif
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);
830 # else
831 return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count);
832 # endif
835 uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
836 return internal_syscall(SYSCALL(lseek), fd, offset, whence);
839 # if SANITIZER_LINUX
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);
849 # endif
850 # endif
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() {
859 # if SANITIZER_LINUX
860 # if SANITIZER_S390
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.
865 return __fork();
866 # else
867 return internal_syscall(SYSCALL(clone), SIGCHLD, 0);
868 # endif
869 # else
870 return internal_syscall(SYSCALL(fork));
871 # endif
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,
892 (size_t)newlen);
893 # else
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)
903 return (-1);
904 return internal_sysctl(oid, len, oldp, oldlenp, newp, newlen);
905 # endif
907 # endif
909 # if SANITIZER_LINUX
910 # define SA_RESTORER 0x04000000
911 // Doesn't set sa_restorer if the caller did not set it, so use with caution
912 //(see below).
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;
919 if (u_act) {
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;
936 # endif
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;
952 # endif
954 return result;
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);
962 # else
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));
967 # endif
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));
978 # if SANITIZER_LINUX
979 void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
980 signum -= 1;
981 CHECK_GE(signum, 0);
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) {
990 signum -= 1;
991 CHECK_GE(signum, 0);
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);
1012 # endif
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_))
1030 return Error;
1031 internal_lseek(descriptor_, 0, SEEK_SET);
1032 threads->clear();
1034 Result result = Ok;
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());
1041 if (!read)
1042 return result;
1043 if (internal_iserror(read)) {
1044 Report("Can't read directory entries from /proc/%d/task.\n", pid_);
1045 return Error;
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.
1065 if (!first_read) {
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
1070 // worst.
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.
1085 char path[80];
1086 internal_snprintf(path, sizeof(path), "/proc/%d/task/%d/status", pid_, tid);
1087 if (!ReadFileToVector(path, &buffer_) || buffer_.empty())
1088 return false;
1089 buffer_.push_back(0);
1090 static const char kPrefix[] = "\nPPid:";
1091 const char *field = internal_strstr(buffer_.data(), kPrefix);
1092 if (!field)
1093 return false;
1094 field += internal_strlen(kPrefix);
1095 return (int)internal_atoll(field) != 0;
1098 ThreadLister::~ThreadLister() {
1099 if (!internal_iserror(descriptor_))
1100 internal_close(descriptor_);
1102 # endif
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())
1114 return 0;
1115 MemoryMappedSegment segment;
1116 while (proc_maps.Next(&segment)) {
1117 if ((segment.end >= 3 * gbyte) && segment.IsWritable())
1118 return 0;
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"))
1130 return 0;
1131 # endif // SANITIZER_ANDROID
1133 // Top gigabyte is reserved for kernel.
1134 return gbyte;
1135 # else
1136 return 0;
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__)
1162 return ~(uptr)0;
1163 # else
1164 return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
1165 # endif
1166 # else // SANITIZER_WORDSIZE == 32
1167 # if defined(__s390__)
1168 return (1ULL << 31) - 1; // 0x7fffffff;
1169 # else
1170 return (1ULL << 32) - 1; // 0xffffffff;
1171 # endif
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);
1181 # endif
1182 return 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.
1192 int pz = 0;
1193 uptr pzl = sizeof(pz);
1194 int mib[2] = {CTL_HW, HW_PAGESIZE};
1195 int rv = internal_sysctl(mib, 2, &pz, &pzl, nullptr, 0);
1196 CHECK_EQ(rv, 0);
1197 return (uptr)pz;
1198 # elif SANITIZER_USE_GETAUXVAL
1199 return getauxval(AT_PAGESZ);
1200 # else
1201 return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
1202 # endif
1204 # endif
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);
1211 # else
1212 # if SANITIZER_FREEBSD || SANITIZER_NETBSD
1213 # if SANITIZER_FREEBSD
1214 const int Mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
1215 # else
1216 const int Mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
1217 # endif
1218 const char *default_module_name = "kern.proc.pathname";
1219 uptr Size = buf_len;
1220 bool IsErr =
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;
1224 # else
1225 const char *default_module_name = "/proc/self/exe";
1226 uptr module_name_len = internal_readlink(default_module_name, buf, buf_len);
1227 int readlink_error;
1228 bool IsErr = internal_iserror(module_name_len, &readlink_error);
1229 # endif
1230 if (IsErr) {
1231 // We can't read binary name for some reason, assume it's unknown.
1232 Report(
1233 "WARNING: reading executable name failed with errno %d, "
1234 "some stack frames may not be symbolized\n",
1235 readlink_error);
1236 module_name_len =
1237 internal_snprintf(buf, buf_len, "%s", default_module_name);
1238 CHECK_LT(module_name_len, buf_len);
1240 return module_name_len;
1241 # endif
1244 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
1245 # if SANITIZER_LINUX
1246 char *tmpbuf;
1247 uptr tmpsize;
1248 uptr tmplen;
1249 if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf, &tmpsize, &tmplen,
1250 1024 * 1024)) {
1251 internal_strncpy(buf, tmpbuf, buf_len);
1252 UnmapOrDie(tmpbuf, tmpsize);
1253 return internal_strlen(buf);
1255 # endif
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;
1262 // Strip path.
1263 while (*name != '\0') name++;
1264 while (name > full_name && *name != '/') name--;
1265 if (*name == '/')
1266 name++;
1267 uptr base_name_length = internal_strlen(base_name);
1268 if (internal_strncmp(name, base_name, base_name_length))
1269 return false;
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);
1312 # endif
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) {
1324 long long res;
1325 if (!fn || !child_stack)
1326 return -EINVAL;
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),
1335 * %rdi = flags,
1336 * %rsi = child_stack,
1337 * %rdx = parent_tidptr,
1338 * %r8 = new_tls,
1339 * %r10 = child_tidptr)
1341 "syscall\n"
1343 /* if (%rax != 0)
1344 * return;
1346 "testq %%rax,%%rax\n"
1347 "jnz 1f\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)". */
1356 "popq %%rax\n"
1357 "popq %%rdi\n"
1358 "call *%%rax\n"
1360 /* Call _exit(%rax). */
1361 "movq %%rax,%%rdi\n"
1362 "movq %2,%%rax\n"
1363 "syscall\n"
1365 /* Return to parent. */
1366 "1:\n"
1367 : "=a"(res)
1368 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), "S"(child_stack), "D"(flags),
1369 "d"(parent_tidptr), "r"(r8), "r"(r10)
1370 : "memory", "r11", "rcx");
1371 return res;
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) {
1376 long long res;
1377 if (!fn || !child_stack)
1378 return -EINVAL;
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,
1389 * $a0 = flags,
1390 * $a1 = child_stack,
1391 * $a2 = parent_tidptr,
1392 * $a3 = new_tls,
1393 * $a4 = child_tidptr)
1395 ".cprestore 16;\n"
1396 "move $4,%1;\n"
1397 "move $5,%2;\n"
1398 "move $6,%3;\n"
1399 "move $7,%4;\n"
1400 /* Store the fifth argument on stack
1401 * if we are using 32-bit abi.
1403 # if SANITIZER_WORDSIZE == 32
1404 "lw %5,16($29);\n"
1405 # else
1406 "move $8,%5;\n"
1407 # endif
1408 "li $2,%6;\n"
1409 "syscall;\n"
1411 /* if ($v0 != 0)
1412 * return;
1414 "bnez $2,1f;\n"
1416 /* Call "fn(arg)". */
1417 # if SANITIZER_WORDSIZE == 32
1418 # ifdef __BIG_ENDIAN__
1419 "lw $25,4($29);\n"
1420 "lw $4,12($29);\n"
1421 # else
1422 "lw $25,0($29);\n"
1423 "lw $4,8($29);\n"
1424 # endif
1425 # else
1426 "ld $25,0($29);\n"
1427 "ld $4,8($29);\n"
1428 # endif
1429 "jal $25;\n"
1431 /* Call _exit($v0). */
1432 "move $4,$2;\n"
1433 "li $2,%7;\n"
1434 "syscall;\n"
1436 /* Return to parent. */
1437 "1:\n"
1438 : "=r"(res)
1439 : "r"(flags), "r"(child_stack), "r"(parent_tidptr), "r"(a3), "r"(a4),
1440 "i"(__NR_clone), "i"(__NR_exit)
1441 : "memory", "$29");
1442 return res;
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)
1448 return -EINVAL;
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__(
1463 "ecall\n"
1465 /* if (a0 != 0)
1466 * return a0;
1468 "bnez a0, 1f\n"
1470 // In the child, now. Call "fn(arg)".
1471 "mv a0, a6\n"
1472 "jalr a5\n"
1474 // Call _exit(a0).
1475 "addi a7, zero, %9\n"
1476 "ecall\n"
1477 "1:\n"
1479 : "=r"(res)
1480 : "0"(__flags), "r"(__stack), "r"(__ptid), "r"(__tls), "r"(__ctid),
1481 "r"(__fn), "r"(__arg), "r"(nr_clone), "i"(__NR_exit)
1482 : "memory");
1483 return res;
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)
1490 return -EINVAL;
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 */
1511 "svc 0x0\n"
1513 /* if (%r0 != 0)
1514 * return %r0;
1516 "cmp x0, #0\n"
1517 "bne 1f\n"
1519 /* In the child, now. Call "fn(arg)". */
1520 "ldp x1, x0, [sp], #16\n"
1521 "blr x1\n"
1523 /* Call _exit(%r0). */
1524 "mov x8, %10\n"
1525 "svc 0x0\n"
1526 "1:\n"
1528 : "=r"(res)
1529 : "i"(-EINVAL), "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg),
1530 "r"(__ptid), "r"(__tls), "r"(__ctid), "i"(__NR_clone), "i"(__NR_exit)
1531 : "x30", "memory");
1532 return res;
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)
1538 return -EINVAL;
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__(
1553 "syscall 0\n"
1555 // if ($a0 != 0)
1556 // return $a0;
1557 "bnez $a0, 1f\n"
1559 // In the child, now. Call "fn(arg)".
1560 "move $a0, $a6\n"
1561 "jirl $ra, $a5, 0\n"
1563 // Call _exit($a0).
1564 "addi.d $a7, $zero, %9\n"
1565 "syscall 0\n"
1567 "1:\n"
1569 : "=r"(res)
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",
1573 "$t8");
1574 return res;
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) {
1579 long long res;
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
1602 # else
1603 # error "Unsupported PPC64 ABI"
1604 # endif
1605 if (!fn || !child_stack)
1606 return -EINVAL;
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 */
1619 "mr 28, %5\n\t"
1620 "mr 27, %8\n\t"
1622 /* syscall
1623 r0 == __NR_clone
1624 r3 == flags
1625 r4 == child_stack
1626 r5 == parent_tidptr
1627 r6 == newtls
1628 r7 == child_tidptr */
1629 "mr 3, %7\n\t"
1630 "mr 5, %9\n\t"
1631 "mr 6, %10\n\t"
1632 "mr 7, %11\n\t"
1633 "li 0, %3\n\t"
1634 "sc\n\t"
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"
1639 "bne- cr1, 1f\n\t"
1641 /* Set up stack frame */
1642 "li 29, 0\n\t"
1643 "stdu 29, -8(1)\n\t"
1644 "stdu 1, -%12(1)\n\t"
1645 /* Do the function call */
1646 "std 2, %13(1)\n\t"
1647 # if SANITIZER_PPC64V1
1648 "ld 0, 0(28)\n\t"
1649 "ld 2, 8(28)\n\t"
1650 "mtctr 0\n\t"
1651 # elif SANITIZER_PPC64V2
1652 "mr 12, 28\n\t"
1653 "mtctr 12\n\t"
1654 # else
1655 # error "Unsupported PPC64 ABI"
1656 # endif
1657 "mr 3, 27\n\t"
1658 "bctrl\n\t"
1659 "ld 2, %13(1)\n\t"
1661 /* Call _exit(r3) */
1662 "li 0, %4\n\t"
1663 "sc\n\t"
1665 /* Return to parent */
1666 "1:\n\t"
1667 "mr %0, 3\n\t"
1668 : "=r"(res)
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");
1673 return res;
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) {
1678 int res;
1679 if (!fn || !child_stack)
1680 return -EINVAL;
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),
1689 * %ebx = flags,
1690 * %ecx = child_stack,
1691 * %edx = parent_tidptr,
1692 * %esi = new_tls,
1693 * %edi = child_tidptr)
1696 /* Obtain flags */
1697 "movl (%%ecx), %%ebx\n"
1698 /* Do the system call */
1699 "pushl %%ebx\n"
1700 "pushl %%esi\n"
1701 "pushl %%edi\n"
1702 /* Remember the flag value. */
1703 "movl %%ebx, (%%ecx)\n"
1704 "int $0x80\n"
1705 "popl %%edi\n"
1706 "popl %%esi\n"
1707 "popl %%ebx\n"
1709 /* if (%eax != 0)
1710 * return;
1713 "test %%eax,%%eax\n"
1714 "jnz 1f\n"
1716 /* terminate the stack frame */
1717 "xorl %%ebp,%%ebp\n"
1718 /* Call FN. */
1719 "call *%%ebx\n"
1720 # ifdef PIC
1721 "call here\n"
1722 "here:\n"
1723 "popl %%ebx\n"
1724 "addl $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n"
1725 # endif
1726 /* Call exit */
1727 "movl %%eax, %%ebx\n"
1728 "movl %2, %%eax\n"
1729 "int $0x80\n"
1730 "1:\n"
1731 : "=a"(res)
1732 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), "c"(child_stack),
1733 "d"(parent_tidptr), "S"(newtls), "D"(child_tidptr)
1734 : "memory");
1735 return res;
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) {
1740 unsigned int res;
1741 if (!fn || !child_stack)
1742 return -EINVAL;
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
1755 # endif
1756 # if __ARM_ARCH > 4
1757 # define ARCH_HAS_BLX
1758 # endif
1760 # ifdef ARCH_HAS_BX
1761 # ifdef ARCH_HAS_BLX
1762 # define BLX(R) "blx " #R "\n"
1763 # else
1764 # define BLX(R) "mov lr, pc; bx " #R "\n"
1765 # endif
1766 # else
1767 # define BLX(R) "mov lr, pc; mov pc," #R "\n"
1768 # endif
1770 __asm__ __volatile__(
1771 /* %r0 = syscall(%r7 = SYSCALL(clone),
1772 * %r0 = flags,
1773 * %r1 = child_stack,
1774 * %r2 = parent_tidptr,
1775 * %r3 = new_tls,
1776 * %r4 = child_tidptr)
1779 /* Do the system call */
1780 "swi 0x0\n"
1782 /* if (%r0 != 0)
1783 * return %r0;
1785 "cmp r0, #0\n"
1786 "bne 1f\n"
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). */
1792 "mov r7, %7\n"
1793 "swi 0x0\n"
1794 "1:\n"
1795 "mov %0, r0\n"
1796 : "=r"(res)
1797 : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r7), "i"(__NR_exit)
1798 : "memory");
1799 return res;
1801 # endif
1802 # endif // SANITIZER_LINUX
1804 # if SANITIZER_LINUX
1805 int internal_uname(struct utsname *buf) {
1806 return internal_syscall(SYSCALL(uname), buf);
1808 # endif
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 *);
1814 # endif
1816 static int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size,
1817 void *data) {
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;
1823 return 1;
1825 return 0;
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;
1835 # else
1836 return ANDROID_POST_LOLLIPOP;
1837 # endif
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);
1845 if (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);
1857 if (level)
1858 return level;
1859 level = &_DYNAMIC == nullptr ? AndroidDetectApiLevelStatic()
1860 : AndroidDetectApiLevel();
1861 atomic_store(&android_api_level, level, memory_order_relaxed);
1862 return level;
1865 # endif
1867 static HandleSignalMode GetHandleSignalModeImpl(int signum) {
1868 switch (signum) {
1869 case SIGABRT:
1870 return common_flags()->handle_abort;
1871 case SIGILL:
1872 return common_flags()->handle_sigill;
1873 case SIGTRAP:
1874 return common_flags()->handle_sigtrap;
1875 case SIGFPE:
1876 return common_flags()->handle_sigfpe;
1877 case SIGSEGV:
1878 return common_flags()->handle_segv;
1879 case SIGBUS:
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;
1889 return result;
1892 # if !SANITIZER_GO
1893 void *internal_start_thread(void *(*func)(void *arg), void *arg) {
1894 if (&internal_pthread_create == 0)
1895 return nullptr;
1896 // Start the thread with signals blocked, otherwise it can steal user signals.
1897 ScopedBlockSignals block(nullptr);
1898 void *th;
1899 internal_pthread_create(&th, nullptr, func, arg);
1900 return th;
1903 void internal_join_thread(void *th) {
1904 if (&internal_pthread_join)
1905 internal_pthread_join(th, nullptr);
1907 # else
1908 void *internal_start_thread(void *(*func)(void *), void *arg) { return 0; }
1910 void internal_join_thread(void *th) {}
1911 # endif
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;
1917 uint64_t esr;
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);
1923 while (true) {
1924 _aarch64_ctx *ctx = (_aarch64_ctx *)aux;
1925 if (ctx->size == 0)
1926 break;
1927 if (ctx->magic == kEsrMagic) {
1928 *esr = ((__sanitizer_esr_context *)ctx)->esr;
1929 return true;
1931 aux += ctx->size;
1933 return false;
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; }
1938 # endif
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__)
1951 const int Err = 13;
1952 uptr err = ucontext->uc_mcontext.gregs[Err];
1953 # else
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;
1960 uint32_t op_code;
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.
1968 switch (op_code) {
1969 case 0x28: // sb
1970 case 0x29: // sh
1971 case 0x2b: // sw
1972 case 0x3f: // sd
1973 # if __mips_isa_rev < 6
1974 case 0x2c: // sdl
1975 case 0x2d: // sdr
1976 case 0x2a: // swl
1977 case 0x2e: // swr
1978 # endif
1979 return SignalContext::Write;
1981 case 0x20: // lb
1982 case 0x24: // lbu
1983 case 0x21: // lh
1984 case 0x25: // lhu
1985 case 0x23: // lw
1986 case 0x27: // lwu
1987 case 0x37: // ld
1988 # if __mips_isa_rev < 6
1989 case 0x1a: // ldl
1990 case 0x1b: // ldr
1991 case 0x22: // lwl
1992 case 0x26: // lwr
1993 # endif
1994 return SignalContext::Read;
1995 # if __mips_isa_rev == 6
1996 case 0x3b: // pcrel
1997 op_code = (faulty_instruction >> 19) & 0x3;
1998 switch (op_code) {
1999 case 0x1: // lwpc
2000 case 0x2: // lwupc
2001 return SignalContext::Read;
2003 # endif
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;
2012 u64 esr;
2013 if (!Aarch64GetESR(ucontext, &esr))
2014 return Unknown;
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)
2024 # endif
2025 # ifndef SC_ADDRERR_WR
2026 // Address error was due to memory store
2027 # define SC_ADDRERR_WR (1 << 31)
2028 # endif
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];
2040 # else
2041 // Historical BSDism here.
2042 struct sigcontext *scontext = (struct sigcontext *)context;
2043 # if defined(__arch64__)
2044 uptr pc = scontext->sigc_regs.tpc;
2045 # else
2046 uptr pc = scontext->si_regs.pc;
2047 # endif
2048 # endif
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;
2054 # else
2055 unsigned long pc = ucontext->uc_mcontext.__gregs[REG_PC];
2056 # endif
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]
2062 unsigned op_bits =
2063 ((faulty_instruction & 0x3) << 3) | (faulty_instruction >> 13);
2064 unsigned rd = faulty_instruction & 0xF80; // bits 7-11, inclusive
2065 switch (op_bits) {
2066 case 0b10'010: // c.lwsp (rd != x0)
2067 # if __riscv_xlen == 64
2068 case 0b10'011: // c.ldsp (rd != x0)
2069 # endif
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
2074 # endif
2075 # if __riscv_flen >= 32 || __riscv_xlen == 64
2076 case 0b00'011: // c.flw / c.ld
2077 # endif
2078 # if __riscv_flen == 64
2079 case 0b00'001: // c.fld
2080 case 0b10'001: // c.fldsp
2081 # endif
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
2088 # endif
2089 # if __riscv_flen == 64
2090 case 0b00'101: // c.fsd
2091 case 0b10'101: // c.fsdsp
2092 # endif
2093 return SignalContext::Write;
2094 default:
2095 return SignalContext::Unknown;
2098 # endif
2100 unsigned opcode = faulty_instruction & 0x7f; // lower 7 bits
2101 unsigned funct3 = (faulty_instruction >> 12) & 0x7; // bits 12-14, inclusive
2102 switch (opcode) {
2103 case 0b0000011: // loads
2104 switch (funct3) {
2105 case 0b000: // lb
2106 case 0b001: // lh
2107 case 0b010: // lw
2108 # if __riscv_xlen == 64
2109 case 0b011: // ld
2110 # endif
2111 case 0b100: // lbu
2112 case 0b101: // lhu
2113 return SignalContext::Read;
2114 default:
2115 return SignalContext::Unknown;
2117 case 0b0100011: // stores
2118 switch (funct3) {
2119 case 0b000: // sb
2120 case 0b001: // sh
2121 case 0b010: // sw
2122 # if __riscv_xlen == 64
2123 case 0b011: // sd
2124 # endif
2125 return SignalContext::Write;
2126 default:
2127 return SignalContext::Unknown;
2129 # if __riscv_flen >= 32
2130 case 0b0000111: // floating-point loads
2131 switch (funct3) {
2132 case 0b010: // flw
2133 # if __riscv_flen == 64
2134 case 0b011: // fld
2135 # endif
2136 return SignalContext::Read;
2137 default:
2138 return SignalContext::Unknown;
2140 case 0b0100111: // floating-point stores
2141 switch (funct3) {
2142 case 0b010: // fsw
2143 # if __riscv_flen == 64
2144 case 0b011: // fsd
2145 # endif
2146 return SignalContext::Write;
2147 default:
2148 return SignalContext::Unknown;
2150 # endif
2151 default:
2152 return SignalContext::Unknown;
2154 # else
2155 (void)ucontext;
2156 return Unknown; // FIXME: Implement.
2157 # endif
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;
2166 UNUSED
2167 static const char *RegNumToRegName(int reg) {
2168 switch (reg) {
2169 # if SANITIZER_LINUX
2170 # if defined(__x86_64__)
2171 case REG_RAX:
2172 return "rax";
2173 case REG_RBX:
2174 return "rbx";
2175 case REG_RCX:
2176 return "rcx";
2177 case REG_RDX:
2178 return "rdx";
2179 case REG_RDI:
2180 return "rdi";
2181 case REG_RSI:
2182 return "rsi";
2183 case REG_RBP:
2184 return "rbp";
2185 case REG_RSP:
2186 return "rsp";
2187 case REG_R8:
2188 return "r8";
2189 case REG_R9:
2190 return "r9";
2191 case REG_R10:
2192 return "r10";
2193 case REG_R11:
2194 return "r11";
2195 case REG_R12:
2196 return "r12";
2197 case REG_R13:
2198 return "r13";
2199 case REG_R14:
2200 return "r14";
2201 case REG_R15:
2202 return "r15";
2203 # elif defined(__i386__)
2204 case REG_EAX:
2205 return "eax";
2206 case REG_EBX:
2207 return "ebx";
2208 case REG_ECX:
2209 return "ecx";
2210 case REG_EDX:
2211 return "edx";
2212 case REG_EDI:
2213 return "edi";
2214 case REG_ESI:
2215 return "esi";
2216 case REG_EBP:
2217 return "ebp";
2218 case REG_ESP:
2219 return "esp";
2220 # endif
2221 # endif
2222 default:
2223 return NULL;
2225 return NULL;
2228 # if SANITIZER_LINUX
2229 UNUSED
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]);
2237 # else
2238 (void)RegName;
2239 # endif
2241 # endif
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);
2252 Printf("\n");
2253 DumpSingleReg(ucontext, REG_RDI);
2254 DumpSingleReg(ucontext, REG_RSI);
2255 DumpSingleReg(ucontext, REG_RBP);
2256 DumpSingleReg(ucontext, REG_RSP);
2257 Printf("\n");
2258 DumpSingleReg(ucontext, REG_R8);
2259 DumpSingleReg(ucontext, REG_R9);
2260 DumpSingleReg(ucontext, REG_R10);
2261 DumpSingleReg(ucontext, REG_R11);
2262 Printf("\n");
2263 DumpSingleReg(ucontext, REG_R12);
2264 DumpSingleReg(ucontext, REG_R13);
2265 DumpSingleReg(ucontext, REG_R14);
2266 DumpSingleReg(ucontext, REG_R15);
2267 Printf("\n");
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
2272 // output.
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);
2278 Printf("\n");
2279 DumpSingleReg(ucontext, REG_EDI);
2280 DumpSingleReg(ucontext, REG_ESI);
2281 DumpSingleReg(ucontext, REG_EBP);
2282 DumpSingleReg(ucontext, REG_ESP);
2283 Printf("\n");
2284 # else
2285 (void)ucontext;
2286 # endif
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);
2294 Printf("\n");
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);
2299 Printf("\n");
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);
2304 Printf("\n");
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);
2309 Printf("\n");
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);
2316 Printf("\n");
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);
2321 Printf("\n");
2322 # else
2323 (void)ucontext;
2324 # endif
2325 # endif
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;
2347 # else
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;
2352 # endif
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;
2365 # else
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];
2370 # endif
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;
2377 # else
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. */
2382 # ifndef REG_EIP
2383 # define REG_EIP 14 // REG_PC
2384 # endif
2385 # ifndef REG_EBP
2386 # define REG_EBP 6 // REG_FP
2387 # endif
2388 # ifndef REG_UESP
2389 # define REG_UESP 17 // REG_SP
2390 # endif
2391 # endif
2392 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
2393 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
2394 *sp = ucontext->uc_mcontext.gregs[REG_UESP];
2395 # endif
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];
2402 # else
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];
2409 # endif
2410 # elif defined(__sparc__)
2411 # if defined(__arch64__) || defined(__sparcv9)
2412 # define STACK_BIAS 2047
2413 # else
2414 # define STACK_BIAS 0
2415 # endif
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;
2420 # else
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;
2426 # else
2427 *pc = scontext->si_regs.pc;
2428 *sp = scontext->si_regs.u_regs[14];
2429 # endif
2430 # endif
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;
2441 # else
2442 *pc = ucontext->uc_mcontext.psw.addr & 0x7fffffff;
2443 # endif
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;
2452 # else
2453 *pc = ucontext->uc_mcontext.__gregs[REG_PC];
2454 *bp = ucontext->uc_mcontext.__gregs[REG_S0];
2455 *sp = ucontext->uc_mcontext.__gregs[REG_SP];
2456 # endif
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];
2467 # else
2468 # error "Unsupported arch"
2469 # endif
2472 void SignalContext::InitPcSpBp() { GetPcSpBp(context, &pc, &sp, &bp); }
2474 void InitializePlatformEarly() {
2475 // Do nothing.
2478 void CheckASLR() {
2479 # if SANITIZER_NETBSD
2480 int mib[3];
2481 int paxflags;
2482 uptr len = sizeof(paxflags);
2484 mib[0] = CTL_PROC;
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");
2490 Die();
2493 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_ASLR)) {
2494 Printf(
2495 "This sanitizer is not compatible with enabled ASLR.\n"
2496 "To disable ASLR, please run \"paxctl +a %s\" and try again.\n",
2497 GetArgv()[0]);
2498 Die();
2500 # elif SANITIZER_FREEBSD
2501 int aslr_status;
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
2507 return;
2509 if ((aslr_status & PROC_ASLR_ACTIVE) != 0) {
2510 VReport(1,
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);
2516 ReExec();
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) {
2522 VReport(1,
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);
2528 ReExec();
2530 # else
2531 // Do nothing
2532 # endif
2535 void CheckMPROTECT() {
2536 # if SANITIZER_NETBSD
2537 int mib[3];
2538 int paxflags;
2539 uptr len = sizeof(paxflags);
2541 mib[0] = CTL_PROC;
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");
2547 Die();
2550 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_MPROTECT)) {
2551 Printf("This sanitizer is not compatible with enabled MPROTECT\n");
2552 Die();
2554 # else
2555 // Do nothing
2556 # endif
2559 void CheckNoDeepBind(const char *filename, int flag) {
2560 # ifdef RTLD_DEEPBIND
2561 if (flag & RTLD_DEEPBIND) {
2562 Report(
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);
2569 Die();
2571 # endif
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");
2578 return 0;
2581 bool GetRandom(void *buffer, uptr length, bool blocking) {
2582 if (!buffer || !length || length > 256)
2583 return false;
2584 # if SANITIZER_USE_GETENTROPY
2585 uptr rnd = getentropy(buffer, length);
2586 int rverrno = 0;
2587 if (internal_iserror(rnd, &rverrno) && rverrno == EFAULT)
2588 return false;
2589 else if (rnd == 0)
2590 return true;
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);
2599 int rverrno = 0;
2600 if (internal_iserror(res, &rverrno) && rverrno == ENOSYS)
2601 atomic_store_relaxed(&skip_getrandom_syscall, 1);
2602 else if (res == length)
2603 return true;
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))
2610 return false;
2611 uptr res = internal_read(fd, buffer, length);
2612 if (internal_iserror(res))
2613 return false;
2614 internal_close(fd);
2615 return true;
2618 } // namespace __sanitizer
2620 #endif