1//===-- sanitizer_linux.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is shared between AddressSanitizer and ThreadSanitizer
10// run-time libraries and implements linux-specific functions from
11// sanitizer_libc.h.
12//===----------------------------------------------------------------------===//
13
14#include "sanitizer_platform.h"
15
16#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
17 SANITIZER_SOLARIS
18
19# include "sanitizer_common.h"
20# include "sanitizer_flags.h"
21# include "sanitizer_getauxval.h"
22# include "sanitizer_internal_defs.h"
23# include "sanitizer_libc.h"
24# include "sanitizer_linux.h"
25# include "sanitizer_mutex.h"
26# include "sanitizer_placement_new.h"
27# include "sanitizer_procmaps.h"
28
29# if SANITIZER_LINUX && !SANITIZER_GO
30# include <asm/param.h>
31# endif
32
33// For mips64, syscall(__NR_stat) fills the buffer in the 'struct kernel_stat'
34// format. Struct kernel_stat is defined as 'struct stat' in asm/stat.h. To
35// access stat from asm/stat.h, without conflicting with definition in
36// sys/stat.h, we use this trick. 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
57
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>
76
77# if SANITIZER_LINUX
78# include <sys/utsname.h>
79# endif
80
81# if SANITIZER_LINUX && !SANITIZER_ANDROID
82# include <sys/personality.h>
83# endif
84
85# if SANITIZER_LINUX && defined(__loongarch__)
86# include <sys/sysmacros.h>
87# endif
88
89# if SANITIZER_FREEBSD
90# include <machine/atomic.h>
91# include <sys/exec.h>
92# include <sys/procctl.h>
93# include <sys/sysctl.h>
94extern "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>
98}
99# include <sys/thr.h>
100# endif // SANITIZER_FREEBSD
101
102# if SANITIZER_NETBSD
103# include <limits.h> // For NAME_MAX
104# include <sys/exec.h>
105# include <sys/sysctl.h>
106extern struct ps_strings *__ps_strings;
107# endif // SANITIZER_NETBSD
108
109# if SANITIZER_SOLARIS
110# include <stdlib.h>
111# include <thread.h>
112# define environ _environ
113# endif
114
115extern char **environ;
116
117# if SANITIZER_LINUX
118// <linux/time.h>
119struct kernel_timeval {
120 long tv_sec;
121 long tv_usec;
122};
123
124// <linux/futex.h> is broken on some linux distributions.
125const int FUTEX_WAIT = 0;
126const int FUTEX_WAKE = 1;
127const int FUTEX_PRIVATE_FLAG = 128;
128const int FUTEX_WAIT_PRIVATE = FUTEX_WAIT | FUTEX_PRIVATE_FLAG;
129const int FUTEX_WAKE_PRIVATE = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
130# endif // SANITIZER_LINUX
131
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
142
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)
152
153# if SANITIZER_FREEBSD
154# define SANITIZER_USE_GETENTROPY 1
155# endif
156
157namespace __sanitizer {
158
159void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset) {
160 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, set, oldset));
161}
162
163// Block asynchronous signals
164void BlockSignals(__sanitizer_sigset_t *oldset) {
165 __sanitizer_sigset_t set;
166 internal_sigfillset(set: &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: &set, signum: 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: &set, signum: 31);
178
179 // Don't block synchronous signals
180 internal_sigdelset(set: &set, SIGSEGV);
181 internal_sigdelset(set: &set, SIGBUS);
182 internal_sigdelset(set: &set, SIGILL);
183 internal_sigdelset(set: &set, SIGTRAP);
184 internal_sigdelset(set: &set, SIGABRT);
185 internal_sigdelset(set: &set, SIGFPE);
186 internal_sigdelset(set: &set, SIGPIPE);
187# endif
188
189 SetSigProcMask(set: &set, oldset);
190}
191
192ScopedBlockSignals::ScopedBlockSignals(__sanitizer_sigset_t *copy) {
193 BlockSignals(oldset: &saved_);
194 if (copy)
195 internal_memcpy(dest: copy, src: &saved_, n: sizeof(saved_));
196}
197
198ScopedBlockSignals::~ScopedBlockSignals() { SetSigProcMask(set: &saved_, oldset: nullptr); }
199
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
215
216// --------------- sanitizer_libc.h
217# if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
218# if !SANITIZER_S390
219uptr 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), arg1: (uptr)addr, arg2: length, arg3: prot, arg4: flags, arg5: fd,
223 arg6: 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
230}
231# endif // !SANITIZER_S390
232
233uptr internal_munmap(void *addr, uptr length) {
234 return internal_syscall(SYSCALL(munmap), arg1: (uptr)addr, arg2: length);
235}
236
237# if SANITIZER_LINUX
238uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags,
239 void *new_address) {
240 return internal_syscall(SYSCALL(mremap), arg1: (uptr)old_address, arg2: old_size,
241 arg3: new_size, arg4: flags, arg5: (uptr)new_address);
242}
243# endif
244
245int internal_mprotect(void *addr, uptr length, int prot) {
246 return internal_syscall(SYSCALL(mprotect), arg1: (uptr)addr, arg2: length, arg3: prot);
247}
248
249int internal_madvise(uptr addr, uptr length, int advice) {
250 return internal_syscall(SYSCALL(madvise), arg1: addr, arg2: length, arg3: advice);
251}
252
253uptr internal_close(fd_t fd) { return internal_syscall(SYSCALL(close), arg1: fd); }
254
255uptr internal_open(const char *filename, int flags) {
256# if SANITIZER_LINUX
257 return internal_syscall(SYSCALL(openat), AT_FDCWD, arg2: (uptr)filename, arg3: flags);
258# else
259 return internal_syscall(SYSCALL(open), (uptr)filename, flags);
260# endif
261}
262
263uptr internal_open(const char *filename, int flags, u32 mode) {
264# if SANITIZER_LINUX
265 return internal_syscall(SYSCALL(openat), AT_FDCWD, arg2: (uptr)filename, arg3: flags,
266 arg4: mode);
267# else
268 return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode);
269# endif
270}
271
272uptr 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;
277}
278
279uptr 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;
284}
285
286uptr 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;
291}
292
293# if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && SANITIZER_LINUX
294static 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;
309}
310# endif
311
312# if SANITIZER_LINUX && defined(__loongarch__)
313static 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;
331}
332# endif
333
334# if SANITIZER_MIPS64 || SANITIZER_SPARC64
335# if SANITIZER_MIPS64
336typedef struct kernel_stat kstat_t;
337# else
338typedef 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
357static 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
385}
386# endif
387
388uptr 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, arg2: (uptr)path, arg3: (uptr)buf,
402 arg4: 0);
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
422}
423
424uptr 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, arg2: (uptr)path, arg3: (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
460}
461
462uptr 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), arg1: fd, arg2: (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
491}
492
493uptr internal_filesize(fd_t fd) {
494 struct stat st;
495 if (internal_fstat(fd, buf: &st))
496 return -1;
497 return (uptr)st.st_size;
498}
499
500uptr internal_dup(int oldfd) { return internal_syscall(SYSCALL(dup), arg1: oldfd); }
501
502uptr internal_dup2(int oldfd, int newfd) {
503# if SANITIZER_LINUX
504 return internal_syscall(SYSCALL(dup3), arg1: oldfd, arg2: newfd, arg3: 0);
505# else
506 return internal_syscall(SYSCALL(dup2), oldfd, newfd);
507# endif
508}
509
510uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
511# if SANITIZER_LINUX
512 return internal_syscall(SYSCALL(readlinkat), AT_FDCWD, arg2: (uptr)path, arg3: (uptr)buf,
513 arg4: bufsize);
514# else
515 return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize);
516# endif
517}
518
519uptr internal_unlink(const char *path) {
520# if SANITIZER_LINUX
521 return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, arg2: (uptr)path, arg3: 0);
522# else
523 return internal_syscall(SYSCALL(unlink), (uptr)path);
524# endif
525}
526
527uptr 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, arg2: (uptr)oldpath, AT_FDCWD,
533 arg4: (uptr)newpath);
534# else
535 return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath);
536# endif
537}
538
539uptr internal_sched_yield() { return internal_syscall(SYSCALL(sched_yield)); }
540
541void 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), arg1: &ts, arg2: &ts);
546}
547
548uptr internal_execve(const char *filename, char *const argv[],
549 char *const envp[]) {
550 return internal_syscall(SYSCALL(execve), arg1: (uptr)filename, arg2: (uptr)argv,
551 arg3: (uptr)envp);
552}
553# endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD
554
555# if !SANITIZER_NETBSD
556void internal__exit(int exitcode) {
557# if SANITIZER_FREEBSD || SANITIZER_SOLARIS
558 internal_syscall(SYSCALL(exit), exitcode);
559# else
560 internal_syscall(SYSCALL(exit_group), arg1: exitcode);
561# endif
562 Die(); // Unreachable.
563}
564# endif // !SANITIZER_NETBSD
565
566// ----------------- sanitizer_common.h
567bool FileExists(const char *filename) {
568 if (ShouldMockFailureToOpen(path: filename))
569 return false;
570 struct stat st;
571 if (internal_stat(path: filename, buf: &st))
572 return false;
573 // Sanity check: filename is a regular file.
574 return S_ISREG(st.st_mode);
575}
576
577bool DirExists(const char *path) {
578 struct stat st;
579 if (internal_stat(path, buf: &st))
580 return false;
581 return S_ISDIR(st.st_mode);
582}
583
584# if !SANITIZER_NETBSD
585tid_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
595}
596
597int TgKill(pid_t pid, tid_t tid, int sig) {
598# if SANITIZER_LINUX
599 return internal_syscall(SYSCALL(tgkill), arg1: pid, arg2: tid, arg3: 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
608}
609# endif
610
611# if SANITIZER_GLIBC
612u64 NanoTime() {
613 kernel_timeval tv;
614 internal_memset(s: &tv, c: 0, n: sizeof(tv));
615 internal_syscall(SYSCALL(gettimeofday), arg1: &tv, arg2: 0);
616 return (u64)tv.tv_sec * 1000 * 1000 * 1000 + tv.tv_usec * 1000;
617}
618// Used by real_clock_gettime.
619uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
620 return internal_syscall(SYSCALL(clock_gettime), arg1: clk_id, arg2: tp);
621}
622# elif !SANITIZER_SOLARIS && !SANITIZER_NETBSD
623u64 NanoTime() {
624 struct timespec ts;
625 clock_gettime(CLOCK_REALTIME, &ts);
626 return (u64)ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
627}
628# endif
629
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.
633const 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;
640 }
641 }
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(file_name: "/proc/self/environ", buff: &environ, buff_size: &environ_size, read_len: &len))
651 environ = nullptr;
652 }
653 if (!environ || len == 0)
654 return nullptr;
655 uptr namelen = internal_strlen(s: 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(s: p, c: '\0', n: len - (p - environ));
660 if (!endp) // this entry isn't NUL terminated
661 return nullptr;
662 else if (!internal_memcmp(s1: p, s2: name, n: namelen) && p[namelen] == '=') // Match.
663 return p + namelen + 1; // point after =
664 p = endp + 1;
665 }
666 return nullptr; // Not found.
667# else
668# error "Unsupported platform"
669# endif
670}
671
672# if !SANITIZER_FREEBSD && !SANITIZER_NETBSD && !SANITIZER_GO
673extern "C" {
674SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
675}
676# endif
677
678# if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
679static 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(size: arr_size * sizeof(char *), mem_type: "NullSepFileArray");
685 if (!ReadFileToBuffer(file_name: path, buff: &buff, buff_size: &buff_size, read_len: &buff_len, max_len: 1024 * 1024)) {
686 (*arr)[0] = nullptr;
687 return;
688 }
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++;
698 }
699 }
700 (*arr)[count] = nullptr;
701}
702# endif
703
704static 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();
714 }
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(path: "/proc/self/cmdline", arr: argv, arr_size: kMaxArgv);
736 ReadNullSepFileToArray(path: "/proc/self/environ", arr: envp, arr_size: kMaxEnvp);
737# if !SANITIZER_GO
738 }
739# endif // !SANITIZER_GO
740# endif // SANITIZER_FREEBSD
741}
742
743char **GetArgv() {
744 char **argv, **envp;
745 GetArgsAndEnv(argv: &argv, envp: &envp);
746 return argv;
747}
748
749char **GetEnviron() {
750 char **argv, **envp;
751 GetArgsAndEnv(argv: &argv, envp: &envp);
752 return envp;
753}
754
755# if !SANITIZER_SOLARIS
756void 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), arg1: (uptr)p, arg2: FUTEX_WAIT_PRIVATE, arg3: cmp, arg4: 0, arg5: 0, arg6: 0);
763# endif
764}
765
766void 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), arg1: (uptr)p, arg2: FUTEX_WAKE_PRIVATE, arg3: count, arg4: 0, arg5: 0, arg6: 0);
773# endif
774}
775
776# endif // !SANITIZER_SOLARIS
777
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
785struct 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];
798};
799# endif
800
801# if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
802// Syscall wrappers.
803uptr internal_ptrace(int request, int pid, void *addr, void *data) {
804 return internal_syscall(SYSCALL(ptrace), arg1: request, arg2: pid, arg3: (uptr)addr,
805 arg4: (uptr)data);
806}
807
808uptr internal_waitpid(int pid, int *status, int options) {
809 return internal_syscall(SYSCALL(wait4), arg1: pid, arg2: (uptr)status, arg3: options,
810 arg4: 0 /* rusage */);
811}
812
813uptr internal_getpid() { return internal_syscall(SYSCALL(getpid)); }
814
815uptr internal_getppid() { return internal_syscall(SYSCALL(getppid)); }
816
817int internal_dlinfo(void *handle, int request, void *p) {
818# if SANITIZER_FREEBSD
819 return dlinfo(handle, request, p);
820# else
821 UNIMPLEMENTED();
822# endif
823}
824
825uptr 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), arg1: fd, arg2: (uptr)dirp, arg3: count);
830# else
831 return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count);
832# endif
833}
834
835uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
836 return internal_syscall(SYSCALL(lseek), arg1: fd, arg2: offset, arg3: whence);
837}
838
839# if SANITIZER_LINUX
840uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
841 return internal_syscall(SYSCALL(prctl), arg1: option, arg2, arg3, arg4, arg5);
842}
843# if defined(__x86_64__)
844# include <asm/unistd_64.h>
845// Currently internal_arch_prctl() is only needed on x86_64.
846uptr internal_arch_prctl(int option, uptr arg2) {
847 return internal_syscall(__NR_arch_prctl, arg1: option, arg2);
848}
849# endif
850# endif
851
852uptr internal_sigaltstack(const void *ss, void *oss) {
853 return internal_syscall(SYSCALL(sigaltstack), arg1: (uptr)ss, arg2: (uptr)oss);
854}
855
856extern "C" pid_t __fork(void);
857
858int 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, arg2: 0);
868# endif
869# else
870 return internal_syscall(SYSCALL(fork));
871# endif
872}
873
874# if SANITIZER_FREEBSD
875int 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);
879}
880
881int 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);
899
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
906}
907# endif
908
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).
913int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
914 __sanitizer_kernel_sigaction_t k_act, k_oldact;
915 internal_memset(s: &k_act, c: 0, n: sizeof(__sanitizer_kernel_sigaction_t));
916 internal_memset(s: &k_oldact, c: 0, n: 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(dest: &k_act.sa_mask, src: &u_act->sa_mask,
923 n: 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
937 }
938
939 uptr result = internal_syscall(SYSCALL(rt_sigaction), arg1: (uptr)signum,
940 arg2: (uptr)(u_act ? &k_act : nullptr),
941 arg3: (uptr)(u_oldact ? &k_oldact : nullptr),
942 arg4: (uptr)sizeof(__sanitizer_kernel_sigset_t));
943
944 if ((result == 0) && u_oldact) {
945 u_oldact->handler = k_oldact.handler;
946 u_oldact->sigaction = k_oldact.sigaction;
947 internal_memcpy(dest: &u_oldact->sa_mask, src: &k_oldact.sa_mask,
948 n: 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
953 }
954 return result;
955}
956# endif // SANITIZER_LINUX
957
958uptr 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), arg1: (uptr)how, arg2: (uptr)k_set,
966 arg3: (uptr)k_oldset, arg4: sizeof(__sanitizer_kernel_sigset_t));
967# endif
968}
969
970void internal_sigfillset(__sanitizer_sigset_t *set) {
971 internal_memset(s: set, c: 0xff, n: sizeof(*set));
972}
973
974void internal_sigemptyset(__sanitizer_sigset_t *set) {
975 internal_memset(s: set, c: 0, n: sizeof(*set));
976}
977
978# if SANITIZER_LINUX
979void 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);
987}
988
989bool 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);
997}
998# elif SANITIZER_FREEBSD
999uptr internal_procctl(int type, int id, int cmd, void *data) {
1000 return internal_syscall(SYSCALL(procctl), type, id, cmd, data);
1001}
1002
1003void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
1004 sigset_t *rset = reinterpret_cast<sigset_t *>(set);
1005 sigdelset(rset, signum);
1006}
1007
1008bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
1009 sigset_t *rset = reinterpret_cast<sigset_t *>(set);
1010 return sigismember(rset, signum);
1011}
1012# endif
1013# endif // !SANITIZER_SOLARIS
1014
1015# if !SANITIZER_NETBSD
1016// ThreadLister implementation.
1017ThreadLister::ThreadLister(pid_t pid) : pid_(pid), buffer_(4096) {
1018 char task_directory_path[80];
1019 internal_snprintf(buffer: task_directory_path, length: sizeof(task_directory_path),
1020 format: "/proc/%d/task/", pid);
1021 descriptor_ = internal_open(filename: task_directory_path, O_RDONLY | O_DIRECTORY);
1022 if (internal_iserror(retval: descriptor_)) {
1023 Report(format: "Can't open /proc/%d/task for reading.\n", pid);
1024 }
1025}
1026
1027ThreadLister::Result ThreadLister::ListThreads(
1028 InternalMmapVector<tid_t> *threads) {
1029 if (internal_iserror(retval: descriptor_))
1030 return Error;
1031 internal_lseek(fd: descriptor_, offset: 0, SEEK_SET);
1032 threads->clear();
1033
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(new_size: buffer_.capacity());
1038 CHECK_GE(buffer_.size(), 4096);
1039 uptr read = internal_getdents(
1040 fd: descriptor_, dirp: (struct linux_dirent *)buffer_.data(), count: buffer_.size());
1041 if (!read)
1042 return result;
1043 if (internal_iserror(retval: read)) {
1044 Report(format: "Can't read directory entries from /proc/%d/task.\n", pid_);
1045 return Error;
1046 }
1047
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;
1056 }
1057 if (entry->d_ino && *entry->d_name >= '0' && *entry->d_name <= '9')
1058 threads->push_back(element: internal_atoll(nptr: entry->d_name));
1059 }
1060
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(new_size: buffer_.size() * 2);
1072 result = Incomplete;
1073 } else if (!threads->empty() && !IsAlive(tid: 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;
1078 }
1079 }
1080}
1081
1082bool 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(buffer: path, length: sizeof(path), format: "/proc/%d/task/%d/status", pid_, tid);
1087 if (!ReadFileToVector(file_name: path, buff: &buffer_) || buffer_.empty())
1088 return false;
1089 buffer_.push_back(element: 0);
1090 static const char kPrefix[] = "\nPPid:";
1091 const char *field = internal_strstr(haystack: buffer_.data(), needle: kPrefix);
1092 if (!field)
1093 return false;
1094 field += internal_strlen(s: kPrefix);
1095 return (int)internal_atoll(nptr: field) != 0;
1096}
1097
1098ThreadLister::~ThreadLister() {
1099 if (!internal_iserror(retval: descriptor_))
1100 internal_close(fd: descriptor_);
1101}
1102# endif
1103
1104# if SANITIZER_WORDSIZE == 32
1105// Take care of unusable kernel area in top gigabyte.
1106static uptr GetKernelAreaSize() {
1107# if SANITIZER_LINUX && !SANITIZER_X32
1108 const uptr gbyte = 1UL << 30;
1109
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;
1119 }
1120
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
1132
1133 // Top gigabyte is reserved for kernel.
1134 return gbyte;
1135# else
1136 return 0;
1137# endif // SANITIZER_LINUX && !SANITIZER_X32
1138}
1139# endif // SANITIZER_WORDSIZE == 32
1140
1141uptr 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
1173}
1174
1175uptr 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;
1183}
1184
1185# if !SANITIZER_ANDROID || defined(__aarch64__)
1186uptr 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
1203}
1204# endif
1205
1206uptr 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(path: default_module_name, buf, bufsize: buf_len);
1227 int readlink_error;
1228 bool IsErr = internal_iserror(retval: module_name_len, rverrno: &readlink_error);
1229# endif
1230 if (IsErr) {
1231 // We can't read binary name for some reason, assume it's unknown.
1232 Report(
1233 format: "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(buffer: buf, length: buf_len, format: "%s", default_module_name);
1238 CHECK_LT(module_name_len, buf_len);
1239 }
1240 return module_name_len;
1241# endif
1242}
1243
1244uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
1245# if SANITIZER_LINUX
1246 char *tmpbuf;
1247 uptr tmpsize;
1248 uptr tmplen;
1249 if (ReadFileToBuffer(file_name: "/proc/self/cmdline", buff: &tmpbuf, buff_size: &tmpsize, read_len: &tmplen,
1250 max_len: 1024 * 1024)) {
1251 internal_strncpy(dst: buf, src: tmpbuf, n: buf_len);
1252 UnmapOrDie(addr: tmpbuf, size: tmpsize);
1253 return internal_strlen(s: buf);
1254 }
1255# endif
1256 return ReadBinaryName(buf, buf_len);
1257}
1258
1259// Match full names of the form /path/to/base_name{-,.}*
1260bool 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(s: base_name);
1268 if (internal_strncmp(s1: name, s2: base_name, n: base_name_length))
1269 return false;
1270 return (name[base_name_length] == '-' || name[base_name_length] == '.');
1271}
1272
1273# if !SANITIZER_ANDROID
1274// Call cb for each region mapped by map.
1275void 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;
1285
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;
1294 }
1295
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(x: seg_start, boundary: GetPageSizeCached());
1307 seg_end = RoundUpTo(size: seg_end, boundary: GetPageSizeCached());
1308 cb((void *)seg_start, seg_end - seg_start);
1309 }
1310 }
1311}
1312# endif
1313
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.
1322uptr 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)
1340 */
1341 "syscall\n"
1342
1343 /* if (%rax != 0)
1344 * return;
1345 */
1346 "testq %%rax,%%rax\n"
1347 "jnz 1f\n"
1348
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"
1354
1355 /* Call "fn(arg)". */
1356 "popq %%rax\n"
1357 "popq %%rdi\n"
1358 "call *%%rax\n"
1359
1360 /* Call _exit(%rax). */
1361 "movq %%rax,%%rdi\n"
1362 "movq %2,%%rax\n"
1363 "syscall\n"
1364
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;
1372}
1373# elif defined(__mips__)
1374uptr 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)
1394 */
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.
1402 */
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"
1410
1411 /* if ($v0 != 0)
1412 * return;
1413 */
1414 "bnez $2,1f;\n"
1415
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"
1430
1431 /* Call _exit($v0). */
1432 "move $4,$2;\n"
1433 "li $2,%7;\n"
1434 "syscall;\n"
1435
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;
1443}
1444# elif SANITIZER_RISCV64
1445uptr 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;
1449
1450 CHECK_EQ(0, (uptr)child_stack % 16);
1451
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;
1461
1462 __asm__ __volatile__(
1463 "ecall\n"
1464
1465 /* if (a0 != 0)
1466 * return a0;
1467 */
1468 "bnez a0, 1f\n"
1469
1470 // In the child, now. Call "fn(arg)".
1471 "mv a0, a6\n"
1472 "jalr a5\n"
1473
1474 // Call _exit(a0).
1475 "addi a7, zero, %9\n"
1476 "ecall\n"
1477 "1:\n"
1478
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;
1484}
1485# elif defined(__aarch64__)
1486uptr 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;
1495
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;
1503
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 */
1510
1511 "svc 0x0\n"
1512
1513 /* if (%r0 != 0)
1514 * return %r0;
1515 */
1516 "cmp x0, #0\n"
1517 "bne 1f\n"
1518
1519 /* In the child, now. Call "fn(arg)". */
1520 "ldp x1, x0, [sp], #16\n"
1521 "blr x1\n"
1522
1523 /* Call _exit(%r0). */
1524 "mov x8, %10\n"
1525 "svc 0x0\n"
1526 "1:\n"
1527
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;
1533}
1534# elif SANITIZER_LOONGARCH64
1535uptr 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;
1539
1540 CHECK_EQ(0, (uptr)child_stack % 16);
1541
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;
1551
1552 __asm__ __volatile__(
1553 "syscall 0\n"
1554
1555 // if ($a0 != 0)
1556 // return $a0;
1557 "bnez $a0, 1f\n"
1558
1559 // In the child, now. Call "fn(arg)".
1560 "move $a0, $a6\n"
1561 "jirl $ra, $a5, 0\n"
1562
1563 // Call _exit($a0).
1564 "addi.d $a7, $zero, %9\n"
1565 "syscall 0\n"
1566
1567 "1:\n"
1568
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;
1575}
1576# elif defined(__powerpc64__)
1577uptr 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);
1608
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;
1616
1617 __asm__ __volatile__(
1618 /* fn and arg are saved across the syscall */
1619 "mr 28, %5\n\t"
1620 "mr 27, %8\n\t"
1621
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"
1635
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"
1640
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"
1660
1661 /* Call _exit(r3) */
1662 "li 0, %4\n\t"
1663 "sc\n\t"
1664
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;
1674}
1675# elif defined(__i386__)
1676uptr 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)
1694 */
1695
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"
1708
1709 /* if (%eax != 0)
1710 * return;
1711 */
1712
1713 "test %%eax,%%eax\n"
1714 "jnz 1f\n"
1715
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;
1736}
1737# elif defined(__arm__)
1738uptr 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;
1752
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
1759
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
1769
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)
1777 */
1778
1779 /* Do the system call */
1780 "swi 0x0\n"
1781
1782 /* if (%r0 != 0)
1783 * return %r0;
1784 */
1785 "cmp r0, #0\n"
1786 "bne 1f\n"
1787
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;
1800}
1801# endif
1802# endif // SANITIZER_LINUX
1803
1804# if SANITIZER_LINUX
1805int internal_uname(struct utsname *buf) {
1806 return internal_syscall(SYSCALL(uname), arg1: buf);
1807}
1808# endif
1809
1810# if SANITIZER_ANDROID
1811# if __ANDROID_API__ < 21
1812extern "C" __attribute__((weak)) int dl_iterate_phdr(
1813 int (*)(struct dl_phdr_info *, size_t, void *), void *);
1814# endif
1815
1816static 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;
1824 }
1825 return 0;
1826}
1827
1828static atomic_uint32_t android_api_level;
1829
1830static 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
1838}
1839
1840static 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.
1850}
1851
1852extern "C" __attribute__((weak)) void *_DYNAMIC;
1853
1854AndroidApiLevel 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;
1863}
1864
1865# endif
1866
1867static 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;
1881 }
1882 return kHandleSignalNo;
1883}
1884
1885HandleSignalMode GetHandleSignalMode(int signum) {
1886 HandleSignalMode result = GetHandleSignalModeImpl(signum);
1887 if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler)
1888 return kHandleSignalExclusive;
1889 return result;
1890}
1891
1892# if !SANITIZER_GO
1893void *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: &th, attr: nullptr, callback: func, param: arg);
1900 return th;
1901}
1902
1903void internal_join_thread(void *th) {
1904 if (&internal_pthread_join)
1905 internal_pthread_join(th, ret: nullptr);
1906}
1907# else
1908void *internal_start_thread(void *(*func)(void *), void *arg) { return 0; }
1909
1910void internal_join_thread(void *th) {}
1911# endif
1912
1913# if SANITIZER_LINUX && defined(__aarch64__)
1914// Android headers in the older NDK releases miss this definition.
1915struct __sanitizer_esr_context {
1916 struct _aarch64_ctx head;
1917 uint64_t esr;
1918};
1919
1920static 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;
1930 }
1931 aux += ctx->size;
1932 }
1933 return false;
1934}
1935# elif SANITIZER_FREEBSD && defined(__aarch64__)
1936// FreeBSD doesn't provide ESR in the ucontext.
1937static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) { return false; }
1938# endif
1939
1940using Context = ucontext_t;
1941
1942SignalContext::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;
1961
1962 exception_source = (uint32_t *)ucontext->uc_mcontext.pc;
1963 faulty_instruction = (uint32_t)(*exception_source);
1964
1965 op_code = (faulty_instruction >> 26) & 0x3f;
1966
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;
1980
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;
2002 }
2003# endif
2004 }
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;
2058
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;
2096 }
2097 }
2098# endif
2099
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;
2116 }
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;
2128 }
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;
2139 }
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;
2149 }
2150# endif
2151 default:
2152 return SignalContext::Unknown;
2153 }
2154# else
2155 (void)ucontext;
2156 return Unknown; // FIXME: Implement.
2157# endif
2158}
2159
2160bool 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;
2164}
2165
2166UNUSED
2167static 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;
2224 }
2225 return NULL;
2226}
2227
2228# if SANITIZER_LINUX
2229UNUSED
2230static void DumpSingleReg(ucontext_t *ctx, int RegNum) {
2231 const char *RegName = RegNumToRegName(reg: RegNum);
2232# if defined(__x86_64__)
2233 Printf(format: "%s%s = 0x%016llx ", internal_strlen(s: 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
2240}
2241# endif
2242
2243void SignalContext::DumpAllRegisters(void *context) {
2244 ucontext_t *ucontext = (ucontext_t *)context;
2245# if SANITIZER_LINUX
2246# if defined(__x86_64__)
2247 Report(format: "Register values:\n");
2248 DumpSingleReg(ctx: ucontext, REG_RAX);
2249 DumpSingleReg(ctx: ucontext, REG_RBX);
2250 DumpSingleReg(ctx: ucontext, REG_RCX);
2251 DumpSingleReg(ctx: ucontext, REG_RDX);
2252 Printf(format: "\n");
2253 DumpSingleReg(ctx: ucontext, REG_RDI);
2254 DumpSingleReg(ctx: ucontext, REG_RSI);
2255 DumpSingleReg(ctx: ucontext, REG_RBP);
2256 DumpSingleReg(ctx: ucontext, REG_RSP);
2257 Printf(format: "\n");
2258 DumpSingleReg(ctx: ucontext, REG_R8);
2259 DumpSingleReg(ctx: ucontext, REG_R9);
2260 DumpSingleReg(ctx: ucontext, REG_R10);
2261 DumpSingleReg(ctx: ucontext, REG_R11);
2262 Printf(format: "\n");
2263 DumpSingleReg(ctx: ucontext, REG_R12);
2264 DumpSingleReg(ctx: ucontext, REG_R13);
2265 DumpSingleReg(ctx: ucontext, REG_R14);
2266 DumpSingleReg(ctx: ucontext, REG_R15);
2267 Printf(format: "\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.
2327}
2328
2329static 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
2470}
2471
2472void SignalContext::InitPcSpBp() { GetPcSpBp(context, pc: &pc, sp: &sp, bp: &bp); }
2473
2474void InitializePlatformEarly() {
2475 // Do nothing.
2476}
2477
2478void CheckASLR() {
2479# if SANITIZER_NETBSD
2480 int mib[3];
2481 int paxflags;
2482 uptr len = sizeof(paxflags);
2483
2484 mib[0] = CTL_PROC;
2485 mib[1] = internal_getpid();
2486 mib[2] = PROC_PID_PAXFLAGS;
2487
2488 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
2489 Printf("sysctl failed\n");
2490 Die();
2491 }
2492
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();
2499 }
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;
2508 }
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();
2517 }
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();
2529 }
2530# else
2531 // Do nothing
2532# endif
2533}
2534
2535void CheckMPROTECT() {
2536# if SANITIZER_NETBSD
2537 int mib[3];
2538 int paxflags;
2539 uptr len = sizeof(paxflags);
2540
2541 mib[0] = CTL_PROC;
2542 mib[1] = internal_getpid();
2543 mib[2] = PROC_PID_PAXFLAGS;
2544
2545 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
2546 Printf("sysctl failed\n");
2547 Die();
2548 }
2549
2550 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_MPROTECT)) {
2551 Printf("This sanitizer is not compatible with enabled MPROTECT\n");
2552 Die();
2553 }
2554# else
2555 // Do nothing
2556# endif
2557}
2558
2559void CheckNoDeepBind(const char *filename, int flag) {
2560# ifdef RTLD_DEEPBIND
2561 if (flag & RTLD_DEEPBIND) {
2562 Report(
2563 format: "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();
2570 }
2571# endif
2572}
2573
2574uptr 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;
2579}
2580
2581bool 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
2592
2593# if SANITIZER_USE_GETRANDOM
2594 static atomic_uint8_t skip_getrandom_syscall;
2595 if (!atomic_load_relaxed(a: &skip_getrandom_syscall)) {
2596 // Up to 256 bytes, getrandom will not be interrupted.
2597 uptr res = internal_syscall(SYSCALL(getrandom), arg1: buffer, arg2: length,
2598 arg3: blocking ? 0 : GRND_NONBLOCK);
2599 int rverrno = 0;
2600 if (internal_iserror(retval: res, rverrno: &rverrno) && rverrno == ENOSYS)
2601 atomic_store_relaxed(a: &skip_getrandom_syscall, v: 1);
2602 else if (res == length)
2603 return true;
2604 }
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(filename: "/dev/urandom", O_RDONLY);
2609 if (internal_iserror(retval: fd))
2610 return false;
2611 uptr res = internal_read(fd, buf: buffer, count: length);
2612 if (internal_iserror(retval: res))
2613 return false;
2614 internal_close(fd);
2615 return true;
2616}
2617
2618} // namespace __sanitizer
2619
2620#endif
2621