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 || SANITIZER_HAIKU
18
19# include "sanitizer_common.h"
20# include "sanitizer_dl.h"
21# include "sanitizer_flags.h"
22# include "sanitizer_getauxval.h"
23# include "sanitizer_internal_defs.h"
24# include "sanitizer_libc.h"
25# include "sanitizer_linux.h"
26# include "sanitizer_mutex.h"
27# include "sanitizer_placement_new.h"
28# include "sanitizer_procmaps.h"
29
30# if SANITIZER_LINUX && !SANITIZER_GO
31# include <asm/param.h>
32# endif
33
34// For mips64, syscall(__NR_stat) fills the buffer in the 'struct kernel_stat'
35// format. Struct kernel_stat is defined as 'struct stat' in asm/stat.h. To
36// access stat from asm/stat.h, without conflicting with definition in
37// sys/stat.h, we use this trick. sparc64 is similar, using
38// syscall(__NR_stat64) and struct kernel_stat64.
39# if SANITIZER_LINUX && (SANITIZER_MIPS64 || SANITIZER_SPARC64)
40# include <asm/unistd.h>
41# include <sys/types.h>
42# define stat kernel_stat
43# if SANITIZER_SPARC64
44# define stat64 kernel_stat64
45# endif
46# if SANITIZER_GO
47# undef st_atime
48# undef st_mtime
49# undef st_ctime
50# define st_atime st_atim
51# define st_mtime st_mtim
52# define st_ctime st_ctim
53# endif
54# include <asm/stat.h>
55# undef stat
56# undef stat64
57# endif
58
59# include <dlfcn.h>
60# include <errno.h>
61# include <fcntl.h>
62# include <link.h>
63# include <pthread.h>
64# include <sched.h>
65# include <signal.h>
66# include <sys/mman.h>
67# if !SANITIZER_SOLARIS && !SANITIZER_HAIKU
68# include <sys/ptrace.h>
69# endif
70# include <sys/resource.h>
71# include <sys/stat.h>
72# if !SANITIZER_HAIKU
73# include <sys/syscall.h>
74# include <ucontext.h>
75# endif
76# include <sys/time.h>
77# include <sys/types.h>
78# include <unistd.h>
79
80# if SANITIZER_LINUX
81# include <sys/utsname.h>
82# endif
83
84# if SANITIZER_LINUX && !SANITIZER_ANDROID
85# include <sys/personality.h>
86# endif
87
88# if SANITIZER_ANDROID && __ANDROID_API__ < 35
89// The weak `strerrorname_np` (introduced in API level 35) definition,
90// allows for checking the API level at runtime.
91extern "C" SANITIZER_WEAK_ATTRIBUTE const char *strerrorname_np(int);
92# endif
93
94# if SANITIZER_LINUX && \
95 (defined(__loongarch__) || defined(__hexagon__) || defined(__alpha__))
96# include <sys/sysmacros.h>
97# endif
98
99// Hexagon uses statx() instead of stat64(). glibc provides struct statx
100// through <sys/stat.h>, but musl does not — pull it from <linux/stat.h>.
101// On this musl/hexagon combination the two headers coexist without conflict.
102# if SANITIZER_LINUX && defined(__hexagon__)
103# include <linux/stat.h>
104# endif
105
106# if SANITIZER_LINUX && defined(__powerpc64__)
107# include <asm/ptrace.h>
108# endif
109
110# if SANITIZER_FREEBSD
111# include <machine/atomic.h>
112# include <sys/exec.h>
113# include <sys/procctl.h>
114# include <sys/sysctl.h>
115extern "C" {
116// <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
117// FreeBSD 9.2 and 10.0.
118# include <sys/umtx.h>
119}
120# include <sys/thr.h>
121# endif // SANITIZER_FREEBSD
122
123# if SANITIZER_NETBSD
124# include <limits.h> // For NAME_MAX
125# include <sys/exec.h>
126# include <sys/sysctl.h>
127extern struct ps_strings *__ps_strings;
128# endif // SANITIZER_NETBSD
129
130# if SANITIZER_SOLARIS
131# include <stddef.h>
132# include <stdlib.h>
133# include <sys/frame.h>
134# include <thread.h>
135# define environ _environ
136# endif
137
138# if SANITIZER_HAIKU
139# include <OS.h>
140# include <elf.h>
141# include <image.h>
142extern "C" char **__libc_argv;
143# endif
144
145extern char **environ;
146
147# if SANITIZER_LINUX
148// <linux/time.h>
149struct kernel_timeval {
150 long tv_sec;
151 long tv_usec;
152};
153
154// <linux/futex.h> is broken on some linux distributions.
155const int FUTEX_WAIT = 0;
156const int FUTEX_WAKE = 1;
157const int FUTEX_PRIVATE_FLAG = 128;
158const int FUTEX_WAIT_PRIVATE = FUTEX_WAIT | FUTEX_PRIVATE_FLAG;
159const int FUTEX_WAKE_PRIVATE = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
160# endif // SANITIZER_LINUX
161
162// Are we using 32-bit or 64-bit Linux syscalls?
163// x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
164// but it still needs to use 64-bit syscalls.
165# if SANITIZER_LINUX && \
166 (defined(__x86_64__) || defined(__powerpc64__) || \
167 SANITIZER_WORDSIZE == 64 || \
168 (defined(__mips__) && defined(_ABIN32) && _MIPS_SIM == _ABIN32))
169# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
170# else
171# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
172# endif
173
174// Note : FreeBSD implemented both Linux and OpenBSD apis.
175# if SANITIZER_LINUX && defined(__NR_getrandom)
176# if !defined(GRND_NONBLOCK)
177# define GRND_NONBLOCK 1
178# endif
179# define SANITIZER_USE_GETRANDOM 1
180# else
181# define SANITIZER_USE_GETRANDOM 0
182# endif // SANITIZER_LINUX && defined(__NR_getrandom)
183
184# if SANITIZER_FREEBSD
185# define SANITIZER_USE_GETENTROPY 1
186extern "C" void *__sys_mmap(void *addr, size_t len, int prot, int flags, int fd,
187 off_t offset);
188# endif
189
190namespace __sanitizer {
191
192void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset) {
193 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, set, oldset));
194}
195
196# if SANITIZER_LINUX
197// Deletes the specified signal from newset, if it is not present in oldset
198// Equivalently: newset[signum] = newset[signum] & oldset[signum]
199static void KeepUnblocked(__sanitizer_sigset_t &newset,
200 __sanitizer_sigset_t &oldset, int signum) {
201 // FIXME: https://github.com/google/sanitizers/issues/1816
202 if (SANITIZER_ANDROID || !internal_sigismember(set: &oldset, signum))
203 internal_sigdelset(set: &newset, signum);
204}
205# endif
206
207// Block asynchronous signals
208void BlockSignals(__sanitizer_sigset_t *oldset) {
209 __sanitizer_sigset_t newset;
210 internal_sigfillset(set: &newset);
211
212# if SANITIZER_LINUX
213 __sanitizer_sigset_t currentset;
214
215# if !SANITIZER_ANDROID
216 // FIXME: https://github.com/google/sanitizers/issues/1816
217 SetSigProcMask(NULL, oldset: &currentset);
218
219 // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
220 // on any thread, setuid call hangs.
221 // See test/sanitizer_common/TestCases/Linux/setuid.c.
222 KeepUnblocked(newset, oldset&: currentset, signum: 33);
223# endif // !SANITIZER_ANDROID
224
225 // Seccomp-BPF-sandboxed processes rely on SIGSYS to handle trapped syscalls.
226 // If this signal is blocked, such calls cannot be handled and the process may
227 // hang.
228 KeepUnblocked(newset, oldset&: currentset, signum: 31);
229
230# if !SANITIZER_ANDROID
231 // Don't block synchronous signals
232 // but also don't unblock signals that the user had deliberately blocked.
233 // FIXME: https://github.com/google/sanitizers/issues/1816
234 KeepUnblocked(newset, oldset&: currentset, SIGSEGV);
235 KeepUnblocked(newset, oldset&: currentset, SIGBUS);
236 KeepUnblocked(newset, oldset&: currentset, SIGILL);
237 KeepUnblocked(newset, oldset&: currentset, SIGTRAP);
238 KeepUnblocked(newset, oldset&: currentset, SIGABRT);
239 KeepUnblocked(newset, oldset&: currentset, SIGFPE);
240 KeepUnblocked(newset, oldset&: currentset, SIGPIPE);
241# endif //! SANITIZER_ANDROID
242
243# endif // SANITIZER_LINUX
244
245 SetSigProcMask(set: &newset, oldset);
246}
247
248ScopedBlockSignals::ScopedBlockSignals(__sanitizer_sigset_t *copy) {
249 BlockSignals(oldset: &saved_);
250 if (copy)
251 internal_memcpy(dest: copy, src: &saved_, n: sizeof(saved_));
252}
253
254ScopedBlockSignals::~ScopedBlockSignals() { SetSigProcMask(set: &saved_, oldset: nullptr); }
255
256# if SANITIZER_LINUX && defined(__x86_64__)
257# include "sanitizer_syscall_linux_x86_64.inc"
258# elif SANITIZER_LINUX && SANITIZER_RISCV64
259# include "sanitizer_syscall_linux_riscv64.inc"
260# elif SANITIZER_LINUX && defined(__aarch64__)
261# include "sanitizer_syscall_linux_aarch64.inc"
262# elif SANITIZER_LINUX && defined(__arm__)
263# include "sanitizer_syscall_linux_arm.inc"
264# elif SANITIZER_LINUX && defined(__hexagon__)
265# include "sanitizer_syscall_linux_hexagon.inc"
266# elif SANITIZER_LINUX && SANITIZER_LOONGARCH64
267# include "sanitizer_syscall_linux_loongarch64.inc"
268# elif SANITIZER_LINUX && SANITIZER_ALPHA
269# include "sanitizer_syscall_linux_alpha.inc"
270# else
271# include "sanitizer_syscall_generic.inc"
272# endif
273
274// --------------- sanitizer_libc.h
275# if !SANITIZER_SOLARIS && !SANITIZER_NETBSD && !SANITIZER_HAIKU
276# if !SANITIZER_S390
277uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
278 u64 offset) {
279# if SANITIZER_FREEBSD
280 return (uptr)__sys_mmap(addr, length, prot, flags, fd, offset);
281# elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
282 return internal_syscall(SYSCALL(mmap), arg1: (uptr)addr, arg2: length, arg3: prot, arg4: flags, arg5: fd,
283 arg6: offset);
284# else
285 // mmap2 specifies file offset in 4096-byte units.
286 CHECK(IsAligned(offset, 4096));
287 return internal_syscall(SYSCALL(mmap2), addr, length, prot, flags, fd,
288 (OFF_T)(offset / 4096));
289# endif
290}
291# endif // !SANITIZER_S390
292
293uptr internal_munmap(void *addr, uptr length) {
294 return internal_syscall(SYSCALL(munmap), arg1: (uptr)addr, arg2: length);
295}
296
297# if SANITIZER_LINUX
298uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags,
299 void *new_address) {
300 return internal_syscall(SYSCALL(mremap), arg1: (uptr)old_address, arg2: old_size,
301 arg3: new_size, arg4: flags, arg5: (uptr)new_address);
302}
303# endif
304
305int internal_mprotect(void *addr, uptr length, int prot) {
306 return internal_syscall(SYSCALL(mprotect), arg1: (uptr)addr, arg2: length, arg3: prot);
307}
308
309int internal_madvise(uptr addr, uptr length, int advice) {
310 return internal_syscall(SYSCALL(madvise), arg1: addr, arg2: length, arg3: advice);
311}
312
313uptr internal_close_range(fd_t lowfd, fd_t highfd, int flags) {
314# if SANITIZER_FREEBSD || (SANITIZER_LINUX && defined(__NR_close_range))
315 return internal_syscall(SYSCALL(close_range), arg1: lowfd, arg2: highfd, arg3: flags);
316# endif
317 return -1; // Not supported.
318}
319
320uptr internal_close(fd_t fd) { return internal_syscall(SYSCALL(close), arg1: fd); }
321
322uptr internal_open(const char *filename, int flags) {
323# if SANITIZER_LINUX
324 return internal_syscall(SYSCALL(openat), AT_FDCWD, arg2: (uptr)filename, arg3: flags);
325# else
326 return internal_syscall(SYSCALL(open), (uptr)filename, flags);
327# endif
328}
329
330uptr internal_open(const char *filename, int flags, u32 mode) {
331# if SANITIZER_LINUX
332 return internal_syscall(SYSCALL(openat), AT_FDCWD, arg2: (uptr)filename, arg3: flags,
333 arg4: mode);
334# else
335 return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode);
336# endif
337}
338
339uptr internal_read(fd_t fd, void *buf, uptr count) {
340 sptr res;
341 HANDLE_EINTR(res,
342 (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf, count));
343 return res;
344}
345
346uptr internal_write(fd_t fd, const void *buf, uptr count) {
347 sptr res;
348 HANDLE_EINTR(res,
349 (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf, count));
350 return res;
351}
352
353uptr internal_ftruncate(fd_t fd, uptr size) {
354 sptr res;
355 HANDLE_EINTR(res,
356 (sptr)internal_syscall(SYSCALL(ftruncate), fd, (OFF_T)size));
357 return res;
358}
359
360# if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && SANITIZER_LINUX && \
361 !defined(__hexagon__)
362static void stat64_to_stat(struct stat64 *in, struct stat *out) {
363 internal_memset(out, 0, sizeof(*out));
364 out->st_dev = in->st_dev;
365 out->st_ino = in->st_ino;
366 out->st_mode = in->st_mode;
367 out->st_nlink = in->st_nlink;
368 out->st_uid = in->st_uid;
369 out->st_gid = in->st_gid;
370 out->st_rdev = in->st_rdev;
371 out->st_size = in->st_size;
372 out->st_blksize = in->st_blksize;
373 out->st_blocks = in->st_blocks;
374 out->st_atime = in->st_atime;
375 out->st_mtime = in->st_mtime;
376 out->st_ctime = in->st_ctime;
377}
378# endif
379
380# if SANITIZER_LINUX && \
381 (defined(__loongarch__) || defined(__hexagon__) || defined(__alpha__))
382static void statx_to_stat(struct statx *in, struct stat *out) {
383 internal_memset(out, 0, sizeof(*out));
384 out->st_dev = makedev(in->stx_dev_major, in->stx_dev_minor);
385 out->st_ino = in->stx_ino;
386 out->st_mode = in->stx_mode;
387 out->st_nlink = in->stx_nlink;
388 out->st_uid = in->stx_uid;
389 out->st_gid = in->stx_gid;
390 out->st_rdev = makedev(in->stx_rdev_major, in->stx_rdev_minor);
391 out->st_size = in->stx_size;
392 out->st_blksize = in->stx_blksize;
393 out->st_blocks = in->stx_blocks;
394 out->st_atime = in->stx_atime.tv_sec;
395 out->st_atim.tv_nsec = in->stx_atime.tv_nsec;
396 out->st_mtime = in->stx_mtime.tv_sec;
397 out->st_mtim.tv_nsec = in->stx_mtime.tv_nsec;
398 out->st_ctime = in->stx_ctime.tv_sec;
399 out->st_ctim.tv_nsec = in->stx_ctime.tv_nsec;
400}
401# endif
402
403# if SANITIZER_MIPS64 || SANITIZER_SPARC64
404# if SANITIZER_MIPS64
405typedef struct kernel_stat kstat_t;
406# else
407typedef struct kernel_stat64 kstat_t;
408# endif
409// Undefine compatibility macros from <sys/stat.h>
410// so that they would not clash with the kernel_stat
411// st_[a|m|c]time fields
412# if !SANITIZER_GO
413# undef st_atime
414# undef st_mtime
415# undef st_ctime
416# endif
417# if defined(SANITIZER_ANDROID)
418// Bionic sys/stat.h defines additional macros
419// for compatibility with the old NDKs and
420// they clash with the kernel_stat structure
421// st_[a|m|c]time_nsec fields.
422# undef st_atime_nsec
423# undef st_mtime_nsec
424# undef st_ctime_nsec
425# endif
426static void kernel_stat_to_stat(kstat_t *in, struct stat *out) {
427 internal_memset(out, 0, sizeof(*out));
428 out->st_dev = in->st_dev;
429 out->st_ino = in->st_ino;
430 out->st_mode = in->st_mode;
431 out->st_nlink = in->st_nlink;
432 out->st_uid = in->st_uid;
433 out->st_gid = in->st_gid;
434 out->st_rdev = in->st_rdev;
435 out->st_size = in->st_size;
436 out->st_blksize = in->st_blksize;
437 out->st_blocks = in->st_blocks;
438# if defined(__USE_MISC) || defined(__USE_XOPEN2K8) || \
439 defined(SANITIZER_ANDROID)
440 out->st_atim.tv_sec = in->st_atime;
441 out->st_atim.tv_nsec = in->st_atime_nsec;
442 out->st_mtim.tv_sec = in->st_mtime;
443 out->st_mtim.tv_nsec = in->st_mtime_nsec;
444 out->st_ctim.tv_sec = in->st_ctime;
445 out->st_ctim.tv_nsec = in->st_ctime_nsec;
446# else
447 out->st_atime = in->st_atime;
448 out->st_atimensec = in->st_atime_nsec;
449 out->st_mtime = in->st_mtime;
450 out->st_mtimensec = in->st_mtime_nsec;
451 out->st_ctime = in->st_ctime;
452 out->st_atimensec = in->st_ctime_nsec;
453# endif
454}
455# endif
456
457uptr internal_stat(const char *path, void *buf) {
458# if SANITIZER_FREEBSD
459 return internal_syscall(SYSCALL(fstatat), AT_FDCWD, (uptr)path, (uptr)buf, 0);
460# elif SANITIZER_LINUX
461# if defined(__loongarch__) || defined(__hexagon__) || defined(__alpha__)
462 struct statx bufx;
463 int res = internal_syscall(SYSCALL(statx), AT_FDCWD, (uptr)path,
464 AT_NO_AUTOMOUNT, STATX_BASIC_STATS, (uptr)&bufx);
465 statx_to_stat(&bufx, (struct stat *)buf);
466 return res;
467# elif ( \
468 SANITIZER_WORDSIZE == 64 || SANITIZER_X32 || \
469 (defined(__mips__) && defined(_ABIN32) && _MIPS_SIM == _ABIN32)) && \
470 !SANITIZER_SPARC
471 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, arg2: (uptr)path, arg3: (uptr)buf,
472 arg4: 0);
473# elif SANITIZER_SPARC64
474 kstat_t buf64;
475 int res = internal_syscall(SYSCALL(fstatat64), AT_FDCWD, (uptr)path,
476 (uptr)&buf64, 0);
477 kernel_stat_to_stat(&buf64, (struct stat *)buf);
478 return res;
479# else
480 struct stat64 buf64;
481 int res = internal_syscall(SYSCALL(fstatat64), AT_FDCWD, (uptr)path,
482 (uptr)&buf64, 0);
483 stat64_to_stat(&buf64, (struct stat *)buf);
484 return res;
485# endif
486# else
487 struct stat64 buf64;
488 int res = internal_syscall(SYSCALL(stat64), path, &buf64);
489 stat64_to_stat(&buf64, (struct stat *)buf);
490 return res;
491# endif
492}
493
494uptr internal_lstat(const char *path, void *buf) {
495# if SANITIZER_FREEBSD
496 return internal_syscall(SYSCALL(fstatat), AT_FDCWD, (uptr)path, (uptr)buf,
497 AT_SYMLINK_NOFOLLOW);
498# elif SANITIZER_LINUX
499# if defined(__loongarch__) || defined(__hexagon__) || defined(__alpha__)
500 struct statx bufx;
501 int res = internal_syscall(SYSCALL(statx), AT_FDCWD, (uptr)path,
502 AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT,
503 STATX_BASIC_STATS, (uptr)&bufx);
504 statx_to_stat(&bufx, (struct stat *)buf);
505 return res;
506# elif ( \
507 defined(_LP64) || SANITIZER_X32 || \
508 (defined(__mips__) && defined(_ABIN32) && _MIPS_SIM == _ABIN32)) && \
509 !SANITIZER_SPARC
510 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, arg2: (uptr)path, arg3: (uptr)buf,
511 AT_SYMLINK_NOFOLLOW);
512# elif SANITIZER_SPARC64
513 kstat_t buf64;
514 int res = internal_syscall(SYSCALL(fstatat64), AT_FDCWD, (uptr)path,
515 (uptr)&buf64, AT_SYMLINK_NOFOLLOW);
516 kernel_stat_to_stat(&buf64, (struct stat *)buf);
517 return res;
518# else
519 struct stat64 buf64;
520 int res = internal_syscall(SYSCALL(fstatat64), AT_FDCWD, (uptr)path,
521 (uptr)&buf64, AT_SYMLINK_NOFOLLOW);
522 stat64_to_stat(&buf64, (struct stat *)buf);
523 return res;
524# endif
525# else
526 struct stat64 buf64;
527 int res = internal_syscall(SYSCALL(lstat64), path, &buf64);
528 stat64_to_stat(&buf64, (struct stat *)buf);
529 return res;
530# endif
531}
532
533uptr internal_fstat(fd_t fd, void *buf) {
534# if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
535# if SANITIZER_MIPS64
536 // For mips64, fstat syscall fills buffer in the format of kernel_stat
537 kstat_t kbuf;
538 int res = internal_syscall(SYSCALL(fstat), fd, &kbuf);
539 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
540 return res;
541# elif SANITIZER_LINUX && SANITIZER_SPARC64
542 // For sparc64, fstat64 syscall fills buffer in the format of kernel_stat64
543 kstat_t kbuf;
544 int res = internal_syscall(SYSCALL(fstat64), fd, &kbuf);
545 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
546 return res;
547# elif SANITIZER_LINUX && (defined(__loongarch__) || defined(__alpha__))
548 struct statx bufx;
549 int res = internal_syscall(SYSCALL(statx), fd, "", AT_EMPTY_PATH,
550 STATX_BASIC_STATS, (uptr)&bufx);
551 statx_to_stat(&bufx, (struct stat *)buf);
552 return res;
553# else
554 return internal_syscall(SYSCALL(fstat), arg1: fd, arg2: (uptr)buf);
555# endif
556# elif SANITIZER_LINUX && defined(__hexagon__)
557 // Hexagon musl lacks struct stat64; use statx() instead.
558 struct statx bufx;
559 int res = internal_syscall(SYSCALL(statx), fd, "", AT_EMPTY_PATH,
560 STATX_BASIC_STATS, (uptr)&bufx);
561 statx_to_stat(&bufx, (struct stat*)buf);
562 return res;
563# else
564 struct stat64 buf64;
565 int res = internal_syscall(SYSCALL(fstat64), fd, &buf64);
566 stat64_to_stat(&buf64, (struct stat *)buf);
567 return res;
568# endif
569}
570
571uptr internal_filesize(fd_t fd) {
572 struct stat st;
573 if (internal_fstat(fd, buf: &st))
574 return -1;
575 return (uptr)st.st_size;
576}
577
578uptr internal_dup(int oldfd) { return internal_syscall(SYSCALL(dup), arg1: oldfd); }
579
580uptr internal_dup2(int oldfd, int newfd) {
581# if SANITIZER_LINUX
582 return internal_syscall(SYSCALL(dup3), arg1: oldfd, arg2: newfd, arg3: 0);
583# else
584 return internal_syscall(SYSCALL(dup2), oldfd, newfd);
585# endif
586}
587
588uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
589# if SANITIZER_LINUX
590 return internal_syscall(SYSCALL(readlinkat), AT_FDCWD, arg2: (uptr)path, arg3: (uptr)buf,
591 arg4: bufsize);
592# else
593 return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize);
594# endif
595}
596
597uptr internal_unlink(const char *path) {
598# if SANITIZER_LINUX
599 return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, arg2: (uptr)path, arg3: 0);
600# else
601 return internal_syscall(SYSCALL(unlink), (uptr)path);
602# endif
603}
604
605uptr internal_rename(const char *oldpath, const char *newpath) {
606# if (defined(__riscv) || defined(__loongarch__)) && defined(__linux__)
607 return internal_syscall(SYSCALL(renameat2), AT_FDCWD, (uptr)oldpath, AT_FDCWD,
608 (uptr)newpath, 0);
609# elif SANITIZER_LINUX
610 return internal_syscall(SYSCALL(renameat), AT_FDCWD, arg2: (uptr)oldpath, AT_FDCWD,
611 arg4: (uptr)newpath);
612# else
613 return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath);
614# endif
615}
616
617uptr internal_sched_yield() { return internal_syscall(SYSCALL(sched_yield)); }
618
619void internal_usleep(u64 useconds) {
620# if SANITIZER_LINUX && defined(__NR_clock_nanosleep_time64)
621 struct {
622 s64 tv_sec;
623 s64 tv_nsec;
624 } ts64;
625 ts64.tv_sec = useconds / 1000000;
626 ts64.tv_nsec = (useconds % 1000000) * 1000;
627 [[maybe_unused]] uptr Res = internal_syscall(
628 SYSCALL(clock_nanosleep_time64), CLOCK_MONOTONIC, 0, &ts64, &ts64);
629 [[maybe_unused]] int Err;
630 DCHECK(!internal_iserror(Res, &Err) || Err != ENOSYS);
631# else
632 struct timespec ts;
633 ts.tv_sec = useconds / 1000000;
634 ts.tv_nsec = (useconds % 1000000) * 1000;
635 internal_syscall(SYSCALL(nanosleep), arg1: &ts, arg2: &ts);
636# endif
637}
638
639uptr internal_execve(const char *filename, char *const argv[],
640 char *const envp[]) {
641 return internal_syscall(SYSCALL(execve), arg1: (uptr)filename, arg2: (uptr)argv,
642 arg3: (uptr)envp);
643}
644# endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD && !SANITIZER_HAIKU
645
646# if !SANITIZER_NETBSD && !SANITIZER_HAIKU
647void internal__exit(int exitcode) {
648# if SANITIZER_FREEBSD || SANITIZER_SOLARIS
649 internal_syscall(SYSCALL(exit), exitcode);
650# else
651 internal_syscall(SYSCALL(exit_group), arg1: exitcode);
652# endif
653 Die(); // Unreachable.
654}
655# endif // !SANITIZER_NETBSD && !SANITIZER_HAIKU
656
657// ----------------- sanitizer_common.h
658bool FileExists(const char *filename) {
659 if (ShouldMockFailureToOpen(path: filename))
660 return false;
661 struct stat st;
662 if (internal_stat(path: filename, buf: &st))
663 return false;
664 // Sanity check: filename is a regular file.
665 return S_ISREG(st.st_mode);
666}
667
668bool DirExists(const char *path) {
669 struct stat st;
670 if (internal_stat(path, buf: &st))
671 return false;
672 return S_ISDIR(st.st_mode);
673}
674
675# if !SANITIZER_NETBSD
676ThreadID GetTid() {
677# if SANITIZER_FREEBSD
678 long Tid;
679 thr_self(&Tid);
680 return Tid;
681# elif SANITIZER_SOLARIS
682 return thr_self();
683# elif SANITIZER_HAIKU
684 return find_thread(NULL);
685# else
686 return internal_syscall(SYSCALL(gettid));
687# endif
688}
689
690int TgKill(pid_t pid, ThreadID tid, int sig) {
691# if SANITIZER_LINUX
692 return internal_syscall(SYSCALL(tgkill), arg1: pid, arg2: tid, arg3: sig);
693# elif SANITIZER_FREEBSD
694 return internal_syscall(SYSCALL(thr_kill2), pid, tid, sig);
695# elif SANITIZER_SOLARIS
696 (void)pid;
697 errno = thr_kill(tid, sig);
698 // TgKill is expected to return -1 on error, not an errno.
699 return errno != 0 ? -1 : 0;
700# elif SANITIZER_HAIKU
701 return kill_thread(tid);
702# endif
703}
704# endif
705
706# if SANITIZER_GLIBC
707u64 NanoTime() {
708 kernel_timeval tv;
709 internal_memset(s: &tv, c: 0, n: sizeof(tv));
710 internal_syscall(SYSCALL(gettimeofday), arg1: &tv, arg2: 0);
711 return (u64)tv.tv_sec * 1000 * 1000 * 1000 + tv.tv_usec * 1000;
712}
713// Used by real_clock_gettime.
714uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
715 return internal_syscall(SYSCALL(clock_gettime), arg1: clk_id, arg2: tp);
716}
717# elif !SANITIZER_SOLARIS && !SANITIZER_NETBSD
718u64 NanoTime() {
719 struct timespec ts;
720 clock_gettime(CLOCK_REALTIME, &ts);
721 return (u64)ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
722}
723# endif
724
725// Like getenv, but reads env directly from /proc (on Linux) or parses the
726// 'environ' array (on some others) and does not use libc. This function
727// should be called first inside __asan_init.
728const char *GetEnv(const char *name) {
729# if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_SOLARIS || \
730 SANITIZER_HAIKU
731 if (::environ != 0) {
732 uptr NameLen = internal_strlen(name);
733 for (char **Env = ::environ; *Env != 0; Env++) {
734 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
735 return (*Env) + NameLen + 1;
736 }
737 }
738 return 0; // Not found.
739# elif SANITIZER_LINUX
740 static char *environ;
741 static uptr len;
742 static bool inited;
743 if (!inited) {
744 inited = true;
745 uptr environ_size;
746 if (!ReadFileToBuffer(file_name: "/proc/self/environ", buff: &environ, buff_size: &environ_size, read_len: &len))
747 environ = nullptr;
748 }
749 if (!environ || len == 0)
750 return nullptr;
751 uptr namelen = internal_strlen(s: name);
752 const char *p = environ;
753 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
754 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
755 const char *endp = (char *)internal_memchr(s: p, c: '\0', n: len - (p - environ));
756 if (!endp) // this entry isn't NUL terminated
757 return nullptr;
758 else if (!internal_memcmp(s1: p, s2: name, n: namelen) && p[namelen] == '=') // Match.
759 return p + namelen + 1; // point after =
760 p = endp + 1;
761 }
762 return nullptr; // Not found.
763# else
764# error "Unsupported platform"
765# endif
766}
767
768# if !SANITIZER_HAIKU && !SANITIZER_FREEBSD && !SANITIZER_NETBSD && \
769 !SANITIZER_GO
770extern "C" {
771SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
772}
773# endif
774
775# if !SANITIZER_HAIKU && !SANITIZER_FREEBSD && !SANITIZER_NETBSD
776static void ReadNullSepFileToArray(const char *path, char ***arr,
777 int arr_size) {
778 char *buff;
779 uptr buff_size;
780 uptr buff_len;
781 *arr = (char **)MmapOrDie(size: arr_size * sizeof(char *), mem_type: "NullSepFileArray");
782 if (!ReadFileToBuffer(file_name: path, buff: &buff, buff_size: &buff_size, read_len: &buff_len, max_len: 1024 * 1024)) {
783 (*arr)[0] = nullptr;
784 return;
785 }
786 (*arr)[0] = buff;
787 int count, i;
788 for (count = 1, i = 1;; i++) {
789 if (buff[i] == 0) {
790 if (buff[i + 1] == 0)
791 break;
792 (*arr)[count] = &buff[i + 1];
793 CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible.
794 count++;
795 }
796 }
797 (*arr)[count] = nullptr;
798}
799# endif
800
801static void GetArgsAndEnv(char ***argv, char ***envp) {
802# if SANITIZER_HAIKU
803 *argv = __libc_argv;
804 *envp = environ;
805# elif SANITIZER_FREEBSD
806 // On FreeBSD, retrieving the argument and environment arrays is done via the
807 // kern.ps_strings sysctl, which returns a pointer to a structure containing
808 // this information. See also <sys/exec.h>.
809 ps_strings *pss;
810 uptr sz = sizeof(pss);
811 if (internal_sysctlbyname("kern.ps_strings", &pss, &sz, NULL, 0) == -1) {
812 Printf("sysctl kern.ps_strings failed\n");
813 Die();
814 }
815 *argv = pss->ps_argvstr;
816 *envp = pss->ps_envstr;
817# elif SANITIZER_NETBSD
818 *argv = __ps_strings->ps_argvstr;
819 *envp = __ps_strings->ps_envstr;
820# else // SANITIZER_FREEBSD
821# if !SANITIZER_GO
822 if (&__libc_stack_end) {
823 uptr *stack_end = (uptr *)__libc_stack_end;
824 // Linux/sparc64 needs an adjustment, cf. glibc
825 // sysdeps/sparc/sparc{32,64}/dl-machine.h (DL_STACK_END).
826# if SANITIZER_LINUX && defined(__sparc__)
827 stack_end = &stack_end[16];
828# endif
829 // Normally argc can be obtained from *stack_end, however, on ARM glibc's
830 // _start clobbers it:
831 // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/arm/start.S;hb=refs/heads/release/2.31/master#l75
832 // Do not special-case ARM and infer argc from argv everywhere.
833 int argc = 0;
834 while (stack_end[argc + 1]) argc++;
835 *argv = (char **)(stack_end + 1);
836 *envp = (char **)(stack_end + argc + 2);
837 } else {
838# endif // !SANITIZER_GO
839 static const int kMaxArgv = 2000, kMaxEnvp = 2000;
840 ReadNullSepFileToArray(path: "/proc/self/cmdline", arr: argv, arr_size: kMaxArgv);
841 ReadNullSepFileToArray(path: "/proc/self/environ", arr: envp, arr_size: kMaxEnvp);
842# if !SANITIZER_GO
843 }
844# endif // !SANITIZER_GO
845# endif // SANITIZER_HAIKU
846}
847
848char **GetArgv() {
849 char **argv, **envp;
850 GetArgsAndEnv(argv: &argv, envp: &envp);
851 return argv;
852}
853
854char **GetEnviron() {
855 char **argv, **envp;
856 GetArgsAndEnv(argv: &argv, envp: &envp);
857 return envp;
858}
859
860# if !SANITIZER_SOLARIS
861void FutexWait(atomic_uint32_t *p, u32 cmp) {
862# if SANITIZER_FREEBSD
863 _umtx_op(p, UMTX_OP_WAIT_UINT, cmp, 0, 0);
864# elif SANITIZER_NETBSD || SANITIZER_HAIKU
865 sched_yield(); /* No userspace futex-like synchronization */
866# else
867 internal_syscall(SYSCALL(futex), arg1: (uptr)p, arg2: FUTEX_WAIT_PRIVATE, arg3: cmp, arg4: 0, arg5: 0, arg6: 0);
868# endif
869}
870
871void FutexWake(atomic_uint32_t *p, u32 count) {
872# if SANITIZER_FREEBSD
873 _umtx_op(p, UMTX_OP_WAKE, count, 0, 0);
874# elif SANITIZER_NETBSD || SANITIZER_HAIKU
875 /* No userspace futex-like synchronization */
876# else
877 internal_syscall(SYSCALL(futex), arg1: (uptr)p, arg2: FUTEX_WAKE_PRIVATE, arg3: count, arg4: 0, arg5: 0, arg6: 0);
878# endif
879}
880
881# endif // !SANITIZER_SOLARIS
882
883// ----------------- sanitizer_linux.h
884// The actual size of this structure is specified by d_reclen.
885// Note that getdents64 uses a different structure format. We only provide the
886// 32-bit syscall here.
887# if SANITIZER_NETBSD
888// Not used
889# else
890struct linux_dirent {
891# if SANITIZER_X32 || SANITIZER_LINUX
892 u64 d_ino;
893 u64 d_off;
894# else
895 unsigned long d_ino;
896 unsigned long d_off;
897# endif
898 unsigned short d_reclen;
899# if SANITIZER_LINUX
900 unsigned char d_type;
901# endif
902 char d_name[256];
903};
904# endif
905
906# if !SANITIZER_SOLARIS && !SANITIZER_NETBSD && !SANITIZER_HAIKU
907// Syscall wrappers.
908uptr internal_ptrace(int request, int pid, void *addr, void *data) {
909 return internal_syscall(SYSCALL(ptrace), arg1: request, arg2: pid, arg3: (uptr)addr,
910 arg4: (uptr)data);
911}
912
913uptr internal_waitpid(int pid, int *status, int options) {
914 return internal_syscall(SYSCALL(wait4), arg1: pid, arg2: (uptr)status, arg3: options,
915 arg4: 0 /* rusage */);
916}
917
918uptr internal_getpid() { return internal_syscall(SYSCALL(getpid)); }
919
920uptr internal_getppid() { return internal_syscall(SYSCALL(getppid)); }
921
922int internal_dlinfo(void *handle, int request, void *p) {
923# if SANITIZER_FREEBSD
924 return dlinfo(handle, request, p);
925# else
926 UNIMPLEMENTED();
927# endif
928}
929
930uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
931# if SANITIZER_FREEBSD
932 return internal_syscall(SYSCALL(getdirentries), fd, (uptr)dirp, count, NULL);
933# elif SANITIZER_LINUX
934 return internal_syscall(SYSCALL(getdents64), arg1: fd, arg2: (uptr)dirp, arg3: count);
935# else
936 return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count);
937# endif
938}
939
940uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
941 return internal_syscall(SYSCALL(lseek), arg1: fd, arg2: offset, arg3: whence);
942}
943
944# if SANITIZER_LINUX
945uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
946 return internal_syscall(SYSCALL(prctl), arg1: option, arg2, arg3, arg4, arg5);
947}
948# if defined(__x86_64__)
949# include <asm/unistd_64.h>
950// Currently internal_arch_prctl() is only needed on x86_64.
951uptr internal_arch_prctl(int option, uptr arg2) {
952 return internal_syscall(__NR_arch_prctl, arg1: option, arg2);
953}
954# endif
955# endif
956
957uptr internal_sigaltstack(const void *ss, void *oss) {
958 return internal_syscall(SYSCALL(sigaltstack), arg1: (uptr)ss, arg2: (uptr)oss);
959}
960
961extern "C" pid_t __fork(void);
962
963int internal_fork() {
964# if SANITIZER_LINUX
965# if SANITIZER_S390
966 return internal_syscall(SYSCALL(clone), 0, SIGCHLD);
967# elif SANITIZER_SPARC
968 // The clone syscall interface on SPARC differs massively from the rest,
969 // so fall back to __fork.
970 return __fork();
971# else
972 return internal_syscall(SYSCALL(clone), SIGCHLD, arg2: 0);
973# endif
974# else
975 return internal_syscall(SYSCALL(fork));
976# endif
977}
978
979# if SANITIZER_FREEBSD
980int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
981 uptr *oldlenp, const void *newp, uptr newlen) {
982 return internal_syscall(SYSCALL(__sysctl), name, namelen, oldp,
983 (size_t *)oldlenp, newp, (size_t)newlen);
984}
985
986int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
987 const void *newp, uptr newlen) {
988 // Note: this function can be called during startup, so we need to avoid
989 // calling any interceptable functions. On FreeBSD >= 1300045 sysctlbyname()
990 // is a real syscall, but for older versions it calls sysctlnametomib()
991 // followed by sysctl(). To avoid calling the intercepted version and
992 // asserting if this happens during startup, call the real sysctlnametomib()
993 // followed by internal_sysctl() if the syscall is not available.
994# ifdef SYS___sysctlbyname
995 return internal_syscall(SYSCALL(__sysctlbyname), sname,
996 internal_strlen(sname), oldp, (size_t *)oldlenp, newp,
997 (size_t)newlen);
998# else
999 static decltype(sysctlnametomib) *real_sysctlnametomib = nullptr;
1000 if (!real_sysctlnametomib)
1001 real_sysctlnametomib =
1002 (decltype(sysctlnametomib) *)dlsym(RTLD_NEXT, "sysctlnametomib");
1003 CHECK(real_sysctlnametomib);
1004
1005 int oid[CTL_MAXNAME];
1006 size_t len = CTL_MAXNAME;
1007 if (real_sysctlnametomib(sname, oid, &len) == -1)
1008 return (-1);
1009 return internal_sysctl(oid, len, oldp, oldlenp, newp, newlen);
1010# endif
1011}
1012# endif
1013
1014# if SANITIZER_LINUX
1015# define SA_RESTORER 0x04000000
1016// Doesn't set sa_restorer if the caller did not set it, so use with caution
1017//(see below).
1018int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
1019 __sanitizer_kernel_sigaction_t k_act, k_oldact;
1020 internal_memset(s: &k_act, c: 0, n: sizeof(__sanitizer_kernel_sigaction_t));
1021 internal_memset(s: &k_oldact, c: 0, n: sizeof(__sanitizer_kernel_sigaction_t));
1022 const __sanitizer_sigaction *u_act = (const __sanitizer_sigaction *)act;
1023 __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact;
1024 if (u_act) {
1025 k_act.handler = u_act->handler;
1026 k_act.sigaction = u_act->sigaction;
1027 internal_memcpy(dest: &k_act.sa_mask, src: &u_act->sa_mask,
1028 n: sizeof(__sanitizer_kernel_sigset_t));
1029# if defined(__hppa__)
1030 // PA-RISC has no SA_RESTORER; the kernel supplies the signal trampoline.
1031 k_act.sa_flags = u_act->sa_flags;
1032# else
1033 // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL).
1034 k_act.sa_flags = u_act->sa_flags | SA_RESTORER;
1035# endif
1036 // FIXME: most often sa_restorer is unset, however the kernel requires it
1037 // to point to a valid signal restorer that calls the rt_sigreturn syscall.
1038 // If sa_restorer passed to the kernel is NULL, the program may crash upon
1039 // signal delivery or fail to unwind the stack in the signal handler.
1040 // libc implementation of sigaction() passes its own restorer to
1041 // rt_sigaction, so we need to do the same (we'll need to reimplement the
1042 // restorers; for x86_64 the restorer address can be obtained from
1043 // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
1044# if (!SANITIZER_ANDROID || !SANITIZER_MIPS32) && !defined(__alpha__) && \
1045 !defined(__hppa__)
1046 k_act.sa_restorer = u_act->sa_restorer;
1047# endif
1048 }
1049
1050 uptr result = internal_syscall(SYSCALL(rt_sigaction), arg1: (uptr)signum,
1051 arg2: (uptr)(u_act ? &k_act : nullptr),
1052 arg3: (uptr)(u_oldact ? &k_oldact : nullptr),
1053 arg4: (uptr)sizeof(__sanitizer_kernel_sigset_t));
1054
1055 if ((result == 0) && u_oldact) {
1056 u_oldact->handler = k_oldact.handler;
1057 u_oldact->sigaction = k_oldact.sigaction;
1058 internal_memcpy(dest: &u_oldact->sa_mask, src: &k_oldact.sa_mask,
1059 n: sizeof(__sanitizer_kernel_sigset_t));
1060 u_oldact->sa_flags = k_oldact.sa_flags;
1061# if (!SANITIZER_ANDROID || !SANITIZER_MIPS32) && !defined(__alpha__) && \
1062 !defined(__hppa__)
1063 u_oldact->sa_restorer = k_oldact.sa_restorer;
1064# endif
1065 }
1066 return result;
1067}
1068# endif // SANITIZER_LINUX
1069
1070uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
1071 __sanitizer_sigset_t *oldset) {
1072# if SANITIZER_FREEBSD
1073 return internal_syscall(SYSCALL(sigprocmask), how, set, oldset);
1074# else
1075 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
1076 __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset;
1077 return internal_syscall(SYSCALL(rt_sigprocmask), arg1: (uptr)how, arg2: (uptr)k_set,
1078 arg3: (uptr)k_oldset, arg4: sizeof(__sanitizer_kernel_sigset_t));
1079# endif
1080}
1081
1082void internal_sigfillset(__sanitizer_sigset_t *set) {
1083 internal_memset(s: set, c: 0xff, n: sizeof(*set));
1084}
1085
1086void internal_sigemptyset(__sanitizer_sigset_t *set) {
1087 internal_memset(s: set, c: 0, n: sizeof(*set));
1088}
1089
1090# if SANITIZER_LINUX
1091void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
1092 signum -= 1;
1093 CHECK_GE(signum, 0);
1094 CHECK_LT(signum, sizeof(*set) * 8);
1095 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
1096 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
1097 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
1098 k_set->sig[idx] &= ~((uptr)1 << bit);
1099}
1100
1101bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
1102 signum -= 1;
1103 CHECK_GE(signum, 0);
1104 CHECK_LT(signum, sizeof(*set) * 8);
1105 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
1106 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
1107 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
1108 return k_set->sig[idx] & ((uptr)1 << bit);
1109}
1110# elif SANITIZER_FREEBSD
1111uptr internal_procctl(int type, int id, int cmd, void *data) {
1112 return internal_syscall(SYSCALL(procctl), type, id, cmd, data);
1113}
1114
1115void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
1116 sigset_t *rset = reinterpret_cast<sigset_t *>(set);
1117 sigdelset(rset, signum);
1118}
1119
1120bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
1121 sigset_t *rset = reinterpret_cast<sigset_t *>(set);
1122 return sigismember(rset, signum);
1123}
1124# endif
1125# endif // !SANITIZER_SOLARIS
1126
1127# if !SANITIZER_NETBSD && !SANITIZER_HAIKU
1128// ThreadLister implementation.
1129ThreadLister::ThreadLister(pid_t pid) : buffer_(4096) {
1130 task_path_.AppendF(format: "/proc/%d/task", pid);
1131}
1132
1133ThreadLister::Result ThreadLister::ListThreads(
1134 InternalMmapVector<ThreadID> *threads) {
1135 int descriptor = internal_open(filename: task_path_.data(), O_RDONLY | O_DIRECTORY);
1136 if (internal_iserror(retval: descriptor)) {
1137 Report(format: "Can't open %s for reading.\n", task_path_.data());
1138 return Error;
1139 }
1140 auto cleanup = at_scope_exit(fn: [&] { internal_close(fd: descriptor); });
1141 threads->clear();
1142
1143 Result result = Ok;
1144 for (bool first_read = true;; first_read = false) {
1145 CHECK_GE(buffer_.size(), 4096);
1146 uptr read = internal_getdents(
1147 fd: descriptor, dirp: (struct linux_dirent *)buffer_.data(), count: buffer_.size());
1148 if (!read)
1149 return result;
1150 if (internal_iserror(retval: read)) {
1151 Report(format: "Can't read directory entries from %s.\n", task_path_.data());
1152 return Error;
1153 }
1154
1155 for (uptr begin = (uptr)buffer_.data(), end = begin + read; begin < end;) {
1156 struct linux_dirent *entry = (struct linux_dirent *)begin;
1157 begin += entry->d_reclen;
1158 if (entry->d_ino == 1) {
1159 // Inode 1 is for bad blocks and also can be a reason for early return.
1160 // Should be emitted if kernel tried to output terminating thread.
1161 // See proc_task_readdir implementation in Linux.
1162 result = Incomplete;
1163 }
1164 if (entry->d_ino && *entry->d_name >= '0' && *entry->d_name <= '9')
1165 threads->push_back(element: internal_atoll(nptr: entry->d_name));
1166 }
1167
1168 // Now we are going to detect short-read or early EOF. In such cases Linux
1169 // can return inconsistent list with missing alive threads.
1170 // Code will just remember that the list can be incomplete but it will
1171 // continue reads to return as much as possible.
1172 if (!first_read) {
1173 // The first one was a short-read by definition.
1174 result = Incomplete;
1175 } else if (read > buffer_.size() - 1024) {
1176 // Read was close to the buffer size. So double the size and assume the
1177 // worst.
1178 buffer_.resize(new_size: buffer_.size() * 2);
1179 result = Incomplete;
1180 } else if (!threads->empty() && !IsAlive(tid: threads->back())) {
1181 // Maybe Linux early returned from read on terminated thread (!pid_alive)
1182 // and failed to restore read position.
1183 // See next_tid and proc_task_instantiate in Linux.
1184 result = Incomplete;
1185 }
1186 }
1187}
1188
1189const char *ThreadLister::LoadStatus(ThreadID tid) {
1190 status_path_.clear();
1191 status_path_.AppendF(format: "%s/%llu/status", task_path_.data(), tid);
1192 auto cleanup = at_scope_exit(fn: [&] {
1193 // Resize back to capacity if it is downsized by `ReadFileToVector`.
1194 buffer_.resize(new_size: buffer_.capacity());
1195 });
1196 if (!ReadFileToVector(file_name: status_path_.data(), buff: &buffer_) || buffer_.empty())
1197 return nullptr;
1198 buffer_.push_back(element: '\0');
1199 return buffer_.data();
1200}
1201
1202bool ThreadLister::IsAlive(ThreadID tid) {
1203 // /proc/%d/task/%d/status uses same call to detect alive threads as
1204 // proc_task_readdir. See task_state implementation in Linux.
1205 static const char kPrefix[] = "\nPPid:";
1206 const char *status = LoadStatus(tid);
1207 if (!status)
1208 return false;
1209 const char *field = internal_strstr(haystack: status, needle: kPrefix);
1210 if (!field)
1211 return false;
1212 field += internal_strlen(s: kPrefix);
1213 return (int)internal_atoll(nptr: field) != 0;
1214}
1215
1216# endif
1217
1218# if SANITIZER_WORDSIZE == 32
1219// Take care of unusable kernel area in top gigabyte.
1220static uptr GetKernelAreaSize() {
1221# if SANITIZER_LINUX && !SANITIZER_X32
1222 const uptr gbyte = 1UL << 30;
1223
1224 // Firstly check if there are writable segments
1225 // mapped to top gigabyte (e.g. stack).
1226 MemoryMappingLayout proc_maps(/*cache_enabled*/ true);
1227 if (proc_maps.Error())
1228 return 0;
1229 MemoryMappedSegment segment;
1230 while (proc_maps.Next(&segment)) {
1231 if ((segment.end >= 3 * gbyte) && segment.IsWritable())
1232 return 0;
1233 }
1234
1235# if !SANITIZER_ANDROID
1236 // Even if nothing is mapped, top Gb may still be accessible
1237 // if we are running on 64-bit kernel.
1238 // Uname may report misleading results if personality type
1239 // is modified (e.g. under schroot) so check this as well.
1240 struct utsname uname_info;
1241 int pers = personality(0xffffffffUL);
1242 if (!(pers & PER_MASK) && internal_uname(&uname_info) == 0 &&
1243 internal_strstr(uname_info.machine, "64"))
1244 return 0;
1245# endif // SANITIZER_ANDROID
1246
1247 // Top gigabyte is reserved for kernel.
1248 return gbyte;
1249# else
1250 return 0;
1251# endif // SANITIZER_LINUX && !SANITIZER_X32
1252}
1253# endif // SANITIZER_WORDSIZE == 32
1254
1255uptr GetMaxVirtualAddress() {
1256# if SANITIZER_NETBSD && defined(__x86_64__)
1257 return 0x7f7ffffff000ULL; // (0x00007f8000000000 - PAGE_SIZE)
1258# elif SANITIZER_WORDSIZE == 64
1259# if defined(__powerpc64__) || defined(__aarch64__) || \
1260 defined(__loongarch__) || SANITIZER_RISCV64
1261 // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
1262 // We somehow need to figure out which one we are using now and choose
1263 // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
1264 // Note that with 'ulimit -s unlimited' the stack is moved away from the top
1265 // of the address space, so simply checking the stack address is not enough.
1266 // This should (does) work for both PowerPC64 Endian modes.
1267 // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
1268 // loongarch64 also has multiple address space layouts: default is 47-bit.
1269 // RISC-V 64 also has multiple address space layouts: 39, 48 and 57-bit.
1270 return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
1271# elif SANITIZER_ALPHA
1272 // Linux/Alpha uses a 42-bit user VAS (TASK_SIZE = 0x40000000000). With
1273 // fixed shadow offset 0x10000000000 (1 TiB) the layout is:
1274 // LowMem: [0x000000000000, 0x00ffffffffff] (1 TiB)
1275 // LowShadow: [0x010000000000, 0x011fffffffff] (128 GiB)
1276 // ShadowGap: [0x012000000000, 0x012fffffffff]
1277 // HighShadow:[0x013000000000, 0x017fffffffff] (256 GiB)
1278 // HighMem: [0x018000000000, 0x03ffffffffff] (2.5 TiB, stack near top)
1279 // Capping at TASK_SIZE - 1 avoids treating kernel addresses as HighMem.
1280 return (1ULL << 42) - 1; // TASK_SIZE - 1
1281# elif SANITIZER_MIPS64
1282 return (1ULL << 40) - 1; // 0x000000ffffffffffUL;
1283# elif defined(__s390x__)
1284 return (1ULL << 53) - 1; // 0x001fffffffffffffUL;
1285# elif defined(__sparc__)
1286 return ~(uptr)0;
1287# else
1288 return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
1289# endif
1290# else // SANITIZER_WORDSIZE == 32
1291# if defined(__s390__)
1292 return (1ULL << 31) - 1; // 0x7fffffff;
1293# else
1294 return (1ULL << 32) - 1; // 0xffffffff;
1295# endif
1296# endif // SANITIZER_WORDSIZE
1297}
1298
1299uptr GetMaxUserVirtualAddress() {
1300 uptr addr = GetMaxVirtualAddress();
1301# if SANITIZER_WORDSIZE == 32 && !defined(__s390__)
1302 if (!common_flags()->full_address_space)
1303 addr -= GetKernelAreaSize();
1304 CHECK_LT(reinterpret_cast<uptr>(&addr), addr);
1305# endif
1306 return addr;
1307}
1308
1309# if !SANITIZER_ANDROID || defined(__aarch64__)
1310uptr GetPageSize() {
1311# if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__)) && \
1312 defined(EXEC_PAGESIZE)
1313 return EXEC_PAGESIZE;
1314# elif SANITIZER_FREEBSD || SANITIZER_NETBSD
1315 // Use sysctl as sysconf can trigger interceptors internally.
1316 int pz = 0;
1317 uptr pzl = sizeof(pz);
1318 int mib[2] = {CTL_HW, HW_PAGESIZE};
1319 int rv = internal_sysctl(mib, 2, &pz, &pzl, nullptr, 0);
1320 CHECK_EQ(rv, 0);
1321 return (uptr)pz;
1322# elif SANITIZER_USE_GETAUXVAL
1323# if SANITIZER_ANDROID && __ANDROID_API__ < 35
1324 // The 16 KB page size was introduced in Android 15 (API level 35), while
1325 // earlier versions of Android always used a 4 KB page size.
1326 // We are checking the weak definition of `strerrorname_np` (introduced in API
1327 // level 35) because some earlier API levels crashed when
1328 // `getauxval(AT_PAGESZ)` was called from the `.preinit_array`.
1329 if (!strerrorname_np)
1330 return 4096;
1331# endif
1332
1333 return getauxval(AT_PAGESZ);
1334# else
1335 return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
1336# endif
1337}
1338# endif
1339
1340uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) {
1341# if SANITIZER_HAIKU
1342 int32 cookie = 0;
1343 image_info info;
1344 const char *argv0 = "<UNKNOWN>";
1345 while (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) {
1346 if (info.type != B_APP_IMAGE)
1347 continue;
1348 argv0 = info.name;
1349 break;
1350 }
1351 internal_strncpy(buf, argv0, buf_len);
1352 return internal_strlen(buf);
1353# elif SANITIZER_SOLARIS
1354 const char *default_module_name = getexecname();
1355 CHECK_NE(default_module_name, NULL);
1356 return internal_snprintf(buf, buf_len, "%s", default_module_name);
1357# else
1358# if SANITIZER_FREEBSD || SANITIZER_NETBSD
1359# if SANITIZER_FREEBSD
1360 const int Mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
1361# else
1362 const int Mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
1363# endif
1364 const char *default_module_name = "kern.proc.pathname";
1365 uptr Size = buf_len;
1366 bool IsErr =
1367 (internal_sysctl(Mib, ARRAY_SIZE(Mib), buf, &Size, NULL, 0) != 0);
1368 int readlink_error = IsErr ? errno : 0;
1369 uptr module_name_len = Size;
1370# else
1371 const char *default_module_name = "/proc/self/exe";
1372 uptr module_name_len = internal_readlink(path: default_module_name, buf, bufsize: buf_len);
1373 int readlink_error;
1374 bool IsErr = internal_iserror(retval: module_name_len, rverrno: &readlink_error);
1375# endif
1376 if (IsErr) {
1377 // We can't read binary name for some reason, assume it's unknown.
1378 Report(
1379 format: "WARNING: reading executable name failed with errno %d, "
1380 "some stack frames may not be symbolized\n",
1381 readlink_error);
1382 module_name_len =
1383 internal_snprintf(buffer: buf, length: buf_len, format: "%s", default_module_name);
1384 CHECK_LT(module_name_len, buf_len);
1385 }
1386 return module_name_len;
1387# endif
1388}
1389
1390uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
1391# if SANITIZER_LINUX
1392 char *tmpbuf;
1393 uptr tmpsize;
1394 uptr tmplen;
1395 if (ReadFileToBuffer(file_name: "/proc/self/cmdline", buff: &tmpbuf, buff_size: &tmpsize, read_len: &tmplen,
1396 max_len: 1024 * 1024)) {
1397 internal_strncpy(dst: buf, src: tmpbuf, n: buf_len);
1398 UnmapOrDie(addr: tmpbuf, size: tmpsize);
1399 return internal_strlen(s: buf);
1400 }
1401# endif
1402 return ReadBinaryName(buf, buf_len);
1403}
1404
1405// Match full names of the form /path/to/base_name{-,.}*
1406bool LibraryNameIs(const char *full_name, const char *base_name) {
1407 const char *name = full_name;
1408 // Strip path.
1409 while (*name != '\0') name++;
1410 while (name > full_name && *name != '/') name--;
1411 if (*name == '/')
1412 name++;
1413 uptr base_name_length = internal_strlen(s: base_name);
1414 if (internal_strncmp(s1: name, s2: base_name, n: base_name_length))
1415 return false;
1416 return (name[base_name_length] == '-' || name[base_name_length] == '.');
1417}
1418
1419# if !SANITIZER_ANDROID && !SANITIZER_HAIKU
1420// Call cb for each region mapped by map.
1421void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) {
1422 CHECK_NE(map, nullptr);
1423# if !SANITIZER_FREEBSD && !SANITIZER_HAIKU
1424 typedef ElfW(Phdr) Elf_Phdr;
1425 typedef ElfW(Ehdr) Elf_Ehdr;
1426# endif // !SANITIZER_FREEBSD
1427 char* base = DladdrElfHeaderBase(ld: (void*)map->l_ld, addr: (char*)map->l_addr);
1428 Elf_Ehdr *ehdr = (Elf_Ehdr *)base;
1429 char *phdrs = base + ehdr->e_phoff;
1430 char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize;
1431
1432 // Find the segment with the minimum base so we can "relocate" the p_vaddr
1433 // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
1434 // objects have a non-zero base.
1435 uptr preferred_base = (uptr)-1;
1436 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
1437 Elf_Phdr *phdr = (Elf_Phdr *)iter;
1438 if (phdr->p_type == PT_LOAD && preferred_base > (uptr)phdr->p_vaddr)
1439 preferred_base = (uptr)phdr->p_vaddr;
1440 }
1441
1442 // Compute the delta from the real base to get a relocation delta.
1443 sptr delta = (uptr)base - preferred_base;
1444 // Now we can figure out what the loader really mapped.
1445 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
1446 Elf_Phdr *phdr = (Elf_Phdr *)iter;
1447 if (phdr->p_type == PT_LOAD) {
1448 uptr seg_start = phdr->p_vaddr + delta;
1449 uptr seg_end = seg_start + phdr->p_memsz;
1450 // None of these values are aligned. We consider the ragged edges of the
1451 // load command as defined, since they are mapped from the file.
1452 seg_start = RoundDownTo(x: seg_start, boundary: GetPageSizeCached());
1453 seg_end = RoundUpTo(size: seg_end, boundary: GetPageSizeCached());
1454 cb((void *)seg_start, seg_end - seg_start);
1455 }
1456 }
1457}
1458# endif
1459
1460# if SANITIZER_LINUX
1461# if defined(__x86_64__)
1462// We cannot use glibc's clone wrapper, because it messes with the child
1463// task's TLS. It writes the PID and TID of the child task to its thread
1464// descriptor, but in our case the child task shares the thread descriptor with
1465// the parent (because we don't know how to allocate a new thread
1466// descriptor to keep glibc happy). So the stock version of clone(), when
1467// used with CLONE_VM, would end up corrupting the parent's thread descriptor.
1468uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1469 int *parent_tidptr, void *newtls, int *child_tidptr) {
1470 long long res;
1471 if (!fn || !child_stack)
1472 return -EINVAL;
1473 CHECK_EQ(0, (uptr)child_stack % 16);
1474 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1475 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1476 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1477 register void *r8 __asm__("r8") = newtls;
1478 register int *r10 __asm__("r10") = child_tidptr;
1479 __asm__ __volatile__(
1480 /* %rax = syscall(%rax = SYSCALL(clone),
1481 * %rdi = flags,
1482 * %rsi = child_stack,
1483 * %rdx = parent_tidptr,
1484 * %r8 = new_tls,
1485 * %r10 = child_tidptr)
1486 */
1487 "syscall\n"
1488
1489 /* if (%rax != 0)
1490 * return;
1491 */
1492 "testq %%rax,%%rax\n"
1493 "jnz 1f\n"
1494
1495 /* In the child. Terminate unwind chain. */
1496 // XXX: We should also terminate the CFI unwind chain
1497 // here. Unfortunately clang 3.2 doesn't support the
1498 // necessary CFI directives, so we skip that part.
1499 "xorq %%rbp,%%rbp\n"
1500
1501 /* Call "fn(arg)". */
1502 "popq %%rax\n"
1503 "popq %%rdi\n"
1504 "call *%%rax\n"
1505
1506 /* Call _exit(%rax). */
1507 "movq %%rax,%%rdi\n"
1508 "movq %2,%%rax\n"
1509 "syscall\n"
1510
1511 /* Return to parent. */
1512 "1:\n"
1513 : "=a"(res)
1514 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), "S"(child_stack), "D"(flags),
1515 "d"(parent_tidptr), "r"(r8), "r"(r10)
1516 : "memory", "r11", "rcx");
1517 return res;
1518}
1519# elif defined(__mips__)
1520uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1521 int *parent_tidptr, void *newtls, int *child_tidptr) {
1522 long long res;
1523 if (!fn || !child_stack)
1524 return -EINVAL;
1525 CHECK_EQ(0, (uptr)child_stack % 16);
1526 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1527 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1528 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1529 register void *a3 __asm__("$7") = newtls;
1530 register int *a4 __asm__("$8") = child_tidptr;
1531 // We don't have proper CFI directives here because it requires alot of code
1532 // for very marginal benefits.
1533 __asm__ __volatile__(
1534 /* $v0 = syscall($v0 = __NR_clone,
1535 * $a0 = flags,
1536 * $a1 = child_stack,
1537 * $a2 = parent_tidptr,
1538 * $a3 = new_tls,
1539 * $a4 = child_tidptr)
1540 */
1541 ".cprestore 16;\n"
1542 "move $4,%1;\n"
1543 "move $5,%2;\n"
1544 "move $6,%3;\n"
1545 "move $7,%4;\n"
1546 /* Store the fifth argument on stack
1547 * if we are using 32-bit abi.
1548 */
1549# if SANITIZER_WORDSIZE == 32
1550 "lw %5,16($29);\n"
1551# else
1552 "move $8,%5;\n"
1553# endif
1554 "li $2,%6;\n"
1555 "syscall;\n"
1556
1557 /* if ($v0 != 0)
1558 * return;
1559 */
1560 "bnez $2,1f;\n"
1561
1562 /* Call "fn(arg)". */
1563# if SANITIZER_WORDSIZE == 32
1564# ifdef __BIG_ENDIAN__
1565 "lw $25,4($29);\n"
1566 "lw $4,12($29);\n"
1567# else
1568 "lw $25,0($29);\n"
1569 "lw $4,8($29);\n"
1570# endif
1571# else
1572 "ld $25,0($29);\n"
1573 "ld $4,8($29);\n"
1574# endif
1575 "jal $25;\n"
1576
1577 /* Call _exit($v0). */
1578 "move $4,$2;\n"
1579 "li $2,%7;\n"
1580 "syscall;\n"
1581
1582 /* Return to parent. */
1583 "1:\n"
1584 : "=r"(res)
1585 : "r"(flags), "r"(child_stack), "r"(parent_tidptr), "r"(a3), "r"(a4),
1586 "i"(__NR_clone), "i"(__NR_exit)
1587 : "memory", "$29");
1588 return res;
1589}
1590# elif SANITIZER_RISCV64
1591uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1592 int *parent_tidptr, void *newtls, int *child_tidptr) {
1593 if (!fn || !child_stack)
1594 return -EINVAL;
1595
1596 CHECK_EQ(0, (uptr)child_stack % 16);
1597
1598 register int res __asm__("a0");
1599 register int __flags __asm__("a0") = flags;
1600 register void *__stack __asm__("a1") = child_stack;
1601 register int *__ptid __asm__("a2") = parent_tidptr;
1602 register void *__tls __asm__("a3") = newtls;
1603 register int *__ctid __asm__("a4") = child_tidptr;
1604 register int (*__fn)(void *) __asm__("a5") = fn;
1605 register void *__arg __asm__("a6") = arg;
1606 register int nr_clone __asm__("a7") = __NR_clone;
1607
1608 __asm__ __volatile__(
1609 "ecall\n"
1610
1611 /* if (a0 != 0)
1612 * return a0;
1613 */
1614 "bnez a0, 1f\n"
1615
1616 // In the child, now. Call "fn(arg)".
1617 "mv a0, a6\n"
1618 "jalr a5\n"
1619
1620 // Call _exit(a0).
1621 "addi a7, zero, %9\n"
1622 "ecall\n"
1623 "1:\n"
1624
1625 : "=r"(res)
1626 : "0"(__flags), "r"(__stack), "r"(__ptid), "r"(__tls), "r"(__ctid),
1627 "r"(__fn), "r"(__arg), "r"(nr_clone), "i"(__NR_exit)
1628 : "memory");
1629 return res;
1630}
1631# elif defined(__aarch64__)
1632uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1633 int *parent_tidptr, void *newtls, int *child_tidptr) {
1634 register long long res __asm__("x0");
1635 if (!fn || !child_stack)
1636 return -EINVAL;
1637 CHECK_EQ(0, (uptr)child_stack % 16);
1638 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1639 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1640 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1641
1642 register int (*__fn)(void *) __asm__("x0") = fn;
1643 register void *__stack __asm__("x1") = child_stack;
1644 register int __flags __asm__("x2") = flags;
1645 register void *__arg __asm__("x3") = arg;
1646 register int *__ptid __asm__("x4") = parent_tidptr;
1647 register void *__tls __asm__("x5") = newtls;
1648 register int *__ctid __asm__("x6") = child_tidptr;
1649
1650 __asm__ __volatile__(
1651 "mov x0,x2\n" /* flags */
1652 "mov x2,x4\n" /* ptid */
1653 "mov x3,x5\n" /* tls */
1654 "mov x4,x6\n" /* ctid */
1655 "mov x8,%9\n" /* clone */
1656
1657 "svc 0x0\n"
1658
1659 /* if (%r0 != 0)
1660 * return %r0;
1661 */
1662 "cmp x0, #0\n"
1663 "bne 1f\n"
1664
1665 /* In the child, now. Call "fn(arg)". */
1666 "ldp x1, x0, [sp], #16\n"
1667 "blr x1\n"
1668
1669 /* Call _exit(%r0). */
1670 "mov x8, %10\n"
1671 "svc 0x0\n"
1672 "1:\n"
1673
1674 : "=r"(res)
1675 : "i"(-EINVAL), "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg),
1676 "r"(__ptid), "r"(__tls), "r"(__ctid), "i"(__NR_clone), "i"(__NR_exit)
1677 : "x30", "memory");
1678 return res;
1679}
1680# elif SANITIZER_LOONGARCH64
1681uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1682 int *parent_tidptr, void *newtls, int *child_tidptr) {
1683 if (!fn || !child_stack)
1684 return -EINVAL;
1685
1686 CHECK_EQ(0, (uptr)child_stack % 16);
1687
1688 register int res __asm__("$a0");
1689 register int __flags __asm__("$a0") = flags;
1690 register void *__stack __asm__("$a1") = child_stack;
1691 register int *__ptid __asm__("$a2") = parent_tidptr;
1692 register int *__ctid __asm__("$a3") = child_tidptr;
1693 register void *__tls __asm__("$a4") = newtls;
1694 register int (*__fn)(void *) __asm__("$a5") = fn;
1695 register void *__arg __asm__("$a6") = arg;
1696 register int nr_clone __asm__("$a7") = __NR_clone;
1697
1698 __asm__ __volatile__(
1699 "syscall 0\n"
1700
1701 // if ($a0 != 0)
1702 // return $a0;
1703 "bnez $a0, 1f\n"
1704
1705 // In the child, now. Call "fn(arg)".
1706 "move $a0, $a6\n"
1707 "jirl $ra, $a5, 0\n"
1708
1709 // Call _exit($a0).
1710 "addi.d $a7, $zero, %9\n"
1711 "syscall 0\n"
1712
1713 "1:\n"
1714
1715 : "=r"(res)
1716 : "0"(__flags), "r"(__stack), "r"(__ptid), "r"(__ctid), "r"(__tls),
1717 "r"(__fn), "r"(__arg), "r"(nr_clone), "i"(__NR_exit)
1718 : "memory", "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7",
1719 "$t8");
1720 return res;
1721}
1722# elif defined(__powerpc64__)
1723uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1724 int *parent_tidptr, void *newtls, int *child_tidptr) {
1725 long long res;
1726// Stack frame structure.
1727# if SANITIZER_PPC64V1
1728 // Back chain == 0 (SP + 112)
1729 // Frame (112 bytes):
1730 // Parameter save area (SP + 48), 8 doublewords
1731 // TOC save area (SP + 40)
1732 // Link editor doubleword (SP + 32)
1733 // Compiler doubleword (SP + 24)
1734 // LR save area (SP + 16)
1735 // CR save area (SP + 8)
1736 // Back chain (SP + 0)
1737# define FRAME_SIZE 112
1738# define FRAME_TOC_SAVE_OFFSET 40
1739# elif SANITIZER_PPC64V2
1740 // Back chain == 0 (SP + 32)
1741 // Frame (32 bytes):
1742 // TOC save area (SP + 24)
1743 // LR save area (SP + 16)
1744 // CR save area (SP + 8)
1745 // Back chain (SP + 0)
1746# define FRAME_SIZE 32
1747# define FRAME_TOC_SAVE_OFFSET 24
1748# else
1749# error "Unsupported PPC64 ABI"
1750# endif
1751 if (!fn || !child_stack)
1752 return -EINVAL;
1753 CHECK_EQ(0, (uptr)child_stack % 16);
1754
1755 register int (*__fn)(void *) __asm__("r3") = fn;
1756 register void *__cstack __asm__("r4") = child_stack;
1757 register int __flags __asm__("r5") = flags;
1758 register void *__arg __asm__("r6") = arg;
1759 register int *__ptidptr __asm__("r7") = parent_tidptr;
1760 register void *__newtls __asm__("r8") = newtls;
1761 register int *__ctidptr __asm__("r9") = child_tidptr;
1762
1763 __asm__ __volatile__(
1764 /* fn and arg are saved across the syscall */
1765 "mr 28, %5\n\t"
1766 "mr 27, %8\n\t"
1767
1768 /* syscall
1769 r0 == __NR_clone
1770 r3 == flags
1771 r4 == child_stack
1772 r5 == parent_tidptr
1773 r6 == newtls
1774 r7 == child_tidptr */
1775 "mr 3, %7\n\t"
1776 "mr 5, %9\n\t"
1777 "mr 6, %10\n\t"
1778 "mr 7, %11\n\t"
1779 "li 0, %3\n\t"
1780 "sc\n\t"
1781
1782 /* Test if syscall was successful */
1783 "cmpdi cr1, 3, 0\n\t"
1784 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1785 "bne- cr1, 1f\n\t"
1786
1787 /* Set up stack frame */
1788 "li 29, 0\n\t"
1789 "stdu 29, -8(1)\n\t"
1790 "stdu 1, -%12(1)\n\t"
1791 /* Do the function call */
1792 "std 2, %13(1)\n\t"
1793# if SANITIZER_PPC64V1
1794 "ld 0, 0(28)\n\t"
1795 "ld 2, 8(28)\n\t"
1796 "mtctr 0\n\t"
1797# elif SANITIZER_PPC64V2
1798 "mr 12, 28\n\t"
1799 "mtctr 12\n\t"
1800# else
1801# error "Unsupported PPC64 ABI"
1802# endif
1803 "mr 3, 27\n\t"
1804 "bctrl\n\t"
1805 "ld 2, %13(1)\n\t"
1806
1807 /* Call _exit(r3) */
1808 "li 0, %4\n\t"
1809 "sc\n\t"
1810
1811 /* Return to parent */
1812 "1:\n\t"
1813 "mr %0, 3\n\t"
1814 : "=r"(res)
1815 : "0"(-1), "i"(EINVAL), "i"(__NR_clone), "i"(__NR_exit), "r"(__fn),
1816 "r"(__cstack), "r"(__flags), "r"(__arg), "r"(__ptidptr), "r"(__newtls),
1817 "r"(__ctidptr), "i"(FRAME_SIZE), "i"(FRAME_TOC_SAVE_OFFSET)
1818 : "cr0", "cr1", "memory", "ctr", "r0", "r27", "r28", "r29");
1819 return res;
1820}
1821# elif defined(__i386__)
1822uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1823 int *parent_tidptr, void *newtls, int *child_tidptr) {
1824 int res;
1825 if (!fn || !child_stack)
1826 return -EINVAL;
1827 CHECK_EQ(0, (uptr)child_stack % 16);
1828 child_stack = (char *)child_stack - 7 * sizeof(unsigned int);
1829 ((unsigned int *)child_stack)[0] = (uptr)flags;
1830 ((unsigned int *)child_stack)[1] = (uptr)0;
1831 ((unsigned int *)child_stack)[2] = (uptr)fn;
1832 ((unsigned int *)child_stack)[3] = (uptr)arg;
1833 __asm__ __volatile__(
1834 /* %eax = syscall(%eax = SYSCALL(clone),
1835 * %ebx = flags,
1836 * %ecx = child_stack,
1837 * %edx = parent_tidptr,
1838 * %esi = new_tls,
1839 * %edi = child_tidptr)
1840 */
1841
1842 /* Obtain flags */
1843 "movl (%%ecx), %%ebx\n"
1844 /* Do the system call */
1845 "pushl %%ebx\n"
1846 "pushl %%esi\n"
1847 "pushl %%edi\n"
1848 /* Remember the flag value. */
1849 "movl %%ebx, (%%ecx)\n"
1850 "int $0x80\n"
1851 "popl %%edi\n"
1852 "popl %%esi\n"
1853 "popl %%ebx\n"
1854
1855 /* if (%eax != 0)
1856 * return;
1857 */
1858
1859 "test %%eax,%%eax\n"
1860 "jnz 1f\n"
1861
1862 /* terminate the stack frame */
1863 "xorl %%ebp,%%ebp\n"
1864 /* Call FN. */
1865 "call *%%ebx\n"
1866# ifdef PIC
1867 "call here\n"
1868 "here:\n"
1869 "popl %%ebx\n"
1870 "addl $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n"
1871# endif
1872 /* Call exit */
1873 "movl %%eax, %%ebx\n"
1874 "movl %2, %%eax\n"
1875 "int $0x80\n"
1876 "1:\n"
1877 : "=a"(res)
1878 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), "c"(child_stack),
1879 "d"(parent_tidptr), "S"(newtls), "D"(child_tidptr)
1880 : "memory");
1881 return res;
1882}
1883# elif defined(__arm__)
1884uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1885 int *parent_tidptr, void *newtls, int *child_tidptr) {
1886 unsigned int res;
1887 if (!fn || !child_stack)
1888 return -EINVAL;
1889 child_stack = (char *)child_stack - 2 * sizeof(unsigned int);
1890 ((unsigned int *)child_stack)[0] = (uptr)fn;
1891 ((unsigned int *)child_stack)[1] = (uptr)arg;
1892 register int r0 __asm__("r0") = flags;
1893 register void *r1 __asm__("r1") = child_stack;
1894 register int *r2 __asm__("r2") = parent_tidptr;
1895 register void *r3 __asm__("r3") = newtls;
1896 register int *r4 __asm__("r4") = child_tidptr;
1897 register int r7 __asm__("r7") = __NR_clone;
1898
1899# if __ARM_ARCH > 4 || defined(__ARM_ARCH_4T__)
1900# define ARCH_HAS_BX
1901# endif
1902# if __ARM_ARCH > 4
1903# define ARCH_HAS_BLX
1904# endif
1905
1906# ifdef ARCH_HAS_BX
1907# ifdef ARCH_HAS_BLX
1908# define BLX(R) "blx " #R "\n"
1909# else
1910# define BLX(R) "mov lr, pc; bx " #R "\n"
1911# endif
1912# else
1913# define BLX(R) "mov lr, pc; mov pc," #R "\n"
1914# endif
1915
1916 __asm__ __volatile__(
1917 /* %r0 = syscall(%r7 = SYSCALL(clone),
1918 * %r0 = flags,
1919 * %r1 = child_stack,
1920 * %r2 = parent_tidptr,
1921 * %r3 = new_tls,
1922 * %r4 = child_tidptr)
1923 */
1924
1925 /* Do the system call */
1926 "swi 0x0\n"
1927
1928 /* if (%r0 != 0)
1929 * return %r0;
1930 */
1931 "cmp r0, #0\n"
1932 "bne 1f\n"
1933
1934 /* In the child, now. Call "fn(arg)". */
1935 "ldr r0, [sp, #4]\n"
1936 "ldr ip, [sp], #8\n" BLX(ip)
1937 /* Call _exit(%r0). */
1938 "mov r7, %7\n"
1939 "swi 0x0\n"
1940 "1:\n"
1941 "mov %0, r0\n"
1942 : "=r"(res)
1943 : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r7), "i"(__NR_exit)
1944 : "memory");
1945 return res;
1946}
1947# elif defined(__hexagon__)
1948uptr internal_clone(int (*fn)(void*), void* child_stack, int flags, void* arg,
1949 int* parent_tidptr, void* newtls, int* child_tidptr) {
1950 if (!fn || !child_stack)
1951 return -EINVAL;
1952 child_stack = (char*)child_stack - 2 * sizeof(unsigned int);
1953 ((unsigned int*)child_stack)[0] = (uptr)fn;
1954 ((unsigned int*)child_stack)[1] = (uptr)arg;
1955
1956 // Hexagon clone syscall uses the generic argument order (no
1957 // CONFIG_CLONE_BACKWARDS): flags, stack, ptid, ctid, tls.
1958 register int r0 __asm__("r0") = flags;
1959 register void* r1 __asm__("r1") = child_stack;
1960 register int* r2 __asm__("r2") = parent_tidptr;
1961 register int* r3 __asm__("r3") = child_tidptr;
1962 register void* r4 __asm__("r4") = newtls;
1963 register int r6 __asm__("r6") = __NR_clone;
1964
1965 __asm__ __volatile__(
1966 "trap0(#1)\n" /* syscall */
1967 "{ p0 = cmp.eq(r0, #0)\n" /* child? */
1968 " if (!p0.new) jump:nt 1f }\n"
1969 "r1 = memw(r29 + #0)\n" /* r1 = fn */
1970 "r0 = memw(r29 + #4)\n" /* r0 = arg */
1971 "callr r1\n" /* fn(arg) */
1972 "r6 = #%7\n" /* __NR_exit */
1973 "trap0(#1)\n"
1974 "1:\n"
1975 : "=r"(r0)
1976 : "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r6), "i"(__NR_exit)
1977 : "memory", "p0", "r1", "lr");
1978 return (uptr)r0;
1979}
1980# endif
1981# endif // SANITIZER_LINUX
1982
1983# if SANITIZER_LINUX
1984int internal_uname(struct utsname *buf) {
1985 return internal_syscall(SYSCALL(uname), arg1: buf);
1986}
1987# endif
1988
1989static HandleSignalMode GetHandleSignalModeImpl(int signum) {
1990 switch (signum) {
1991 case SIGABRT:
1992 return common_flags()->handle_abort;
1993 case SIGILL:
1994 return common_flags()->handle_sigill;
1995 case SIGTRAP:
1996 return common_flags()->handle_sigtrap;
1997 case SIGFPE:
1998 return common_flags()->handle_sigfpe;
1999 case SIGSEGV:
2000 return common_flags()->handle_segv;
2001 case SIGBUS:
2002 return common_flags()->handle_sigbus;
2003 }
2004 return kHandleSignalNo;
2005}
2006
2007HandleSignalMode GetHandleSignalMode(int signum) {
2008 HandleSignalMode result = GetHandleSignalModeImpl(signum);
2009 if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler)
2010 return kHandleSignalExclusive;
2011 return result;
2012}
2013
2014# if !SANITIZER_GO
2015void *internal_start_thread(void *(*func)(void *arg), void *arg) {
2016 if (&internal_pthread_create == 0)
2017 return nullptr;
2018 // Start the thread with signals blocked, otherwise it can steal user signals.
2019 ScopedBlockSignals block(nullptr);
2020 void *th;
2021 internal_pthread_create(th: &th, attr: nullptr, callback: func, param: arg);
2022 return th;
2023}
2024
2025void internal_join_thread(void *th) {
2026 if (&internal_pthread_join)
2027 internal_pthread_join(th, ret: nullptr);
2028}
2029# else
2030void *internal_start_thread(void *(*func)(void *), void *arg) { return 0; }
2031
2032void internal_join_thread(void *th) {}
2033# endif
2034
2035# if SANITIZER_LINUX && defined(__aarch64__)
2036// Android headers in the older NDK releases miss this definition.
2037struct __sanitizer_esr_context {
2038 struct _aarch64_ctx head;
2039 uint64_t esr;
2040};
2041
2042static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) {
2043 static const u32 kEsrMagic = 0x45535201;
2044 u8 *aux = reinterpret_cast<u8 *>(ucontext->uc_mcontext.__reserved);
2045 while (true) {
2046 _aarch64_ctx *ctx = (_aarch64_ctx *)aux;
2047 if (ctx->size == 0)
2048 break;
2049 if (ctx->magic == kEsrMagic) {
2050 *esr = ((__sanitizer_esr_context *)ctx)->esr;
2051 return true;
2052 }
2053 aux += ctx->size;
2054 }
2055 return false;
2056}
2057# elif SANITIZER_FREEBSD && defined(__aarch64__)
2058// FreeBSD doesn't provide ESR in the ucontext.
2059static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) { return false; }
2060# endif
2061
2062using Context = ucontext_t;
2063
2064SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
2065 Context *ucontext = (Context *)context;
2066# if defined(__x86_64__) || defined(__i386__)
2067# if !SANITIZER_HAIKU
2068 static const uptr PF_WRITE = 1U << 1;
2069# endif
2070# if SANITIZER_FREEBSD
2071 uptr err = ucontext->uc_mcontext.mc_err;
2072# elif SANITIZER_NETBSD
2073 uptr err = ucontext->uc_mcontext.__gregs[_REG_ERR];
2074# elif SANITIZER_HAIKU
2075 uptr err = 0; // FIXME: ucontext->uc_mcontext.r13;
2076 // The err register was added on the main branch and not
2077 // available with the current release. To be reverted later.
2078 // https://github.com/haiku/haiku/commit/11adda21aa4e6b24f71a496868a44d7607bc3764
2079# elif SANITIZER_SOLARIS && defined(__i386__)
2080 const int Err = 13;
2081 uptr err = ucontext->uc_mcontext.gregs[Err];
2082# else
2083 uptr err = ucontext->uc_mcontext.gregs[REG_ERR];
2084# endif // SANITIZER_FREEBSD
2085 return err & PF_WRITE ? Write : Read;
2086# elif defined(__mips__)
2087 uint32_t *exception_source;
2088 uint32_t faulty_instruction;
2089 uint32_t op_code;
2090
2091 exception_source = (uint32_t *)ucontext->uc_mcontext.pc;
2092 faulty_instruction = (uint32_t)(*exception_source);
2093
2094 op_code = (faulty_instruction >> 26) & 0x3f;
2095
2096 // FIXME: Add support for FPU, microMIPS, DSP, MSA memory instructions.
2097 switch (op_code) {
2098 case 0x28: // sb
2099 case 0x29: // sh
2100 case 0x2b: // sw
2101 case 0x3f: // sd
2102# if __mips_isa_rev < 6
2103 case 0x2c: // sdl
2104 case 0x2d: // sdr
2105 case 0x2a: // swl
2106 case 0x2e: // swr
2107# endif
2108 return SignalContext::Write;
2109
2110 case 0x20: // lb
2111 case 0x24: // lbu
2112 case 0x21: // lh
2113 case 0x25: // lhu
2114 case 0x23: // lw
2115 case 0x27: // lwu
2116 case 0x37: // ld
2117# if __mips_isa_rev < 6
2118 case 0x1a: // ldl
2119 case 0x1b: // ldr
2120 case 0x22: // lwl
2121 case 0x26: // lwr
2122# endif
2123 return SignalContext::Read;
2124# if __mips_isa_rev == 6
2125 case 0x3b: // pcrel
2126 op_code = (faulty_instruction >> 19) & 0x3;
2127 switch (op_code) {
2128 case 0x1: // lwpc
2129 case 0x2: // lwupc
2130 return SignalContext::Read;
2131 }
2132# endif
2133 }
2134 return SignalContext::Unknown;
2135# elif defined(__arm__)
2136 static const uptr FSR_WRITE = 1U << 11;
2137 uptr fsr = ucontext->uc_mcontext.error_code;
2138 return fsr & FSR_WRITE ? Write : Read;
2139# elif defined(__aarch64__)
2140 static const u64 ESR_ELx_WNR = 1U << 6;
2141 u64 esr;
2142 if (!Aarch64GetESR(ucontext, &esr))
2143 return Unknown;
2144 return esr & ESR_ELx_WNR ? Write : Read;
2145# elif defined(__loongarch__)
2146 // In the musl environment, the Linux kernel uapi sigcontext.h is not
2147 // included in signal.h. To avoid missing the SC_ADDRERR_{RD,WR} macros,
2148 // copy them here. The LoongArch Linux kernel uapi is already stable,
2149 // so there's no need to worry about the value changing.
2150# ifndef SC_ADDRERR_RD
2151 // Address error was due to memory load
2152# define SC_ADDRERR_RD (1 << 30)
2153# endif
2154# ifndef SC_ADDRERR_WR
2155 // Address error was due to memory store
2156# define SC_ADDRERR_WR (1 << 31)
2157# endif
2158 u32 flags = ucontext->uc_mcontext.__flags;
2159 if (flags & SC_ADDRERR_RD)
2160 return SignalContext::Read;
2161 if (flags & SC_ADDRERR_WR)
2162 return SignalContext::Write;
2163 return SignalContext::Unknown;
2164# elif defined(__sparc__)
2165 // Decode the instruction to determine the access type.
2166 // From OpenSolaris $SRC/uts/sun4/os/trap.c (get_accesstype).
2167# if SANITIZER_SOLARIS
2168 uptr pc = ucontext->uc_mcontext.gregs[REG_PC];
2169# else
2170 // Historical BSDism here.
2171 struct sigcontext *scontext = (struct sigcontext *)context;
2172# if defined(__arch64__)
2173 uptr pc = scontext->sigc_regs.tpc;
2174# else
2175 uptr pc = scontext->si_regs.pc;
2176# endif
2177# endif
2178 u32 instr = *(u32 *)pc;
2179 return (instr >> 21) & 1 ? Write : Read;
2180# elif defined(__riscv)
2181# if SANITIZER_FREEBSD
2182 unsigned long pc = ucontext->uc_mcontext.mc_gpregs.gp_sepc;
2183# else
2184 unsigned long pc = ucontext->uc_mcontext.__gregs[REG_PC];
2185# endif
2186 unsigned faulty_instruction = *(uint16_t *)pc;
2187
2188# if defined(__riscv_compressed)
2189 if ((faulty_instruction & 0x3) != 0x3) { // it's a compressed instruction
2190 // set op_bits to the instruction bits [1, 0, 15, 14, 13]
2191 unsigned op_bits =
2192 ((faulty_instruction & 0x3) << 3) | (faulty_instruction >> 13);
2193 unsigned rd = faulty_instruction & 0xF80; // bits 7-11, inclusive
2194 switch (op_bits) {
2195 case 0b10'010: // c.lwsp (rd != x0)
2196# if __riscv_xlen == 64
2197 case 0b10'011: // c.ldsp (rd != x0)
2198# endif
2199 return rd ? SignalContext::Read : SignalContext::Unknown;
2200 case 0b00'010: // c.lw
2201# if __riscv_flen >= 32 && __riscv_xlen == 32
2202 case 0b10'011: // c.flwsp
2203# endif
2204# if __riscv_flen >= 32 || __riscv_xlen == 64
2205 case 0b00'011: // c.flw / c.ld
2206# endif
2207# if __riscv_flen == 64
2208 case 0b00'001: // c.fld
2209 case 0b10'001: // c.fldsp
2210# endif
2211 return SignalContext::Read;
2212 case 0b00'110: // c.sw
2213 case 0b10'110: // c.swsp
2214# if __riscv_flen >= 32 || __riscv_xlen == 64
2215 case 0b00'111: // c.fsw / c.sd
2216 case 0b10'111: // c.fswsp / c.sdsp
2217# endif
2218# if __riscv_flen == 64
2219 case 0b00'101: // c.fsd
2220 case 0b10'101: // c.fsdsp
2221# endif
2222 return SignalContext::Write;
2223 default:
2224 return SignalContext::Unknown;
2225 }
2226 }
2227# endif
2228
2229 unsigned opcode = faulty_instruction & 0x7f; // lower 7 bits
2230 unsigned funct3 = (faulty_instruction >> 12) & 0x7; // bits 12-14, inclusive
2231 switch (opcode) {
2232 case 0b0000011: // loads
2233 switch (funct3) {
2234 case 0b000: // lb
2235 case 0b001: // lh
2236 case 0b010: // lw
2237# if __riscv_xlen == 64
2238 case 0b011: // ld
2239# endif
2240 case 0b100: // lbu
2241 case 0b101: // lhu
2242 return SignalContext::Read;
2243 default:
2244 return SignalContext::Unknown;
2245 }
2246 case 0b0100011: // stores
2247 switch (funct3) {
2248 case 0b000: // sb
2249 case 0b001: // sh
2250 case 0b010: // sw
2251# if __riscv_xlen == 64
2252 case 0b011: // sd
2253# endif
2254 return SignalContext::Write;
2255 default:
2256 return SignalContext::Unknown;
2257 }
2258# if __riscv_flen >= 32
2259 case 0b0000111: // floating-point loads
2260 switch (funct3) {
2261 case 0b010: // flw
2262# if __riscv_flen == 64
2263 case 0b011: // fld
2264# endif
2265 return SignalContext::Read;
2266 default:
2267 return SignalContext::Unknown;
2268 }
2269 case 0b0100111: // floating-point stores
2270 switch (funct3) {
2271 case 0b010: // fsw
2272# if __riscv_flen == 64
2273 case 0b011: // fsd
2274# endif
2275 return SignalContext::Write;
2276 default:
2277 return SignalContext::Unknown;
2278 }
2279# endif
2280 default:
2281 return SignalContext::Unknown;
2282 }
2283# else
2284 (void)ucontext;
2285 return Unknown; // FIXME: Implement.
2286# endif
2287}
2288
2289bool SignalContext::IsTrueFaultingAddress() const {
2290 auto si = static_cast<const siginfo_t *>(siginfo);
2291 // SIGSEGV signals without a true fault address have si_code set to 128.
2292 return si->si_signo == SIGSEGV && si->si_code != 128;
2293}
2294
2295UNUSED
2296static const char *RegNumToRegName(int reg) {
2297 switch (reg) {
2298# if SANITIZER_LINUX && SANITIZER_GLIBC || SANITIZER_NETBSD
2299# if defined(__x86_64__)
2300# if SANITIZER_NETBSD
2301# define REG_RAX _REG_RAX
2302# define REG_RBX _REG_RBX
2303# define REG_RCX _REG_RCX
2304# define REG_RDX _REG_RDX
2305# define REG_RDI _REG_RDI
2306# define REG_RSI _REG_RSI
2307# define REG_RBP _REG_RBP
2308# define REG_RSP _REG_RSP
2309# define REG_R8 _REG_R8
2310# define REG_R9 _REG_R9
2311# define REG_R10 _REG_R10
2312# define REG_R11 _REG_R11
2313# define REG_R12 _REG_R12
2314# define REG_R13 _REG_R13
2315# define REG_R14 _REG_R14
2316# define REG_R15 _REG_R15
2317# endif
2318 case REG_RAX:
2319 return "rax";
2320 case REG_RBX:
2321 return "rbx";
2322 case REG_RCX:
2323 return "rcx";
2324 case REG_RDX:
2325 return "rdx";
2326 case REG_RDI:
2327 return "rdi";
2328 case REG_RSI:
2329 return "rsi";
2330 case REG_RBP:
2331 return "rbp";
2332 case REG_RSP:
2333 return "rsp";
2334 case REG_R8:
2335 return "r8";
2336 case REG_R9:
2337 return "r9";
2338 case REG_R10:
2339 return "r10";
2340 case REG_R11:
2341 return "r11";
2342 case REG_R12:
2343 return "r12";
2344 case REG_R13:
2345 return "r13";
2346 case REG_R14:
2347 return "r14";
2348 case REG_R15:
2349 return "r15";
2350# elif defined(__i386__)
2351# if SANITIZER_NETBSD
2352# define REG_EAX _REG_EAX
2353# define REG_EBX _REG_EBX
2354# define REG_ECX _REG_ECX
2355# define REG_EDX _REG_EDX
2356# define REG_EDI _REG_EDI
2357# define REG_ESI _REG_ESI
2358# define REG_EBP _REG_EBP
2359# define REG_ESP _REG_ESP
2360# endif
2361 case REG_EAX:
2362 return "eax";
2363 case REG_EBX:
2364 return "ebx";
2365 case REG_ECX:
2366 return "ecx";
2367 case REG_EDX:
2368 return "edx";
2369 case REG_EDI:
2370 return "edi";
2371 case REG_ESI:
2372 return "esi";
2373 case REG_EBP:
2374 return "ebp";
2375 case REG_ESP:
2376 return "esp";
2377# elif defined(__arm__)
2378# ifdef MAKE_CASE
2379# undef MAKE_CASE
2380# endif
2381# define REG_STR(reg) #reg
2382# define MAKE_CASE(N) \
2383 case REG_R##N: \
2384 return REG_STR(r##N)
2385 MAKE_CASE(0);
2386 MAKE_CASE(1);
2387 MAKE_CASE(2);
2388 MAKE_CASE(3);
2389 MAKE_CASE(4);
2390 MAKE_CASE(5);
2391 MAKE_CASE(6);
2392 MAKE_CASE(7);
2393 MAKE_CASE(8);
2394 MAKE_CASE(9);
2395 MAKE_CASE(10);
2396 MAKE_CASE(11);
2397 MAKE_CASE(12);
2398 case REG_R13:
2399 return "sp";
2400 case REG_R14:
2401 return "lr";
2402 case REG_R15:
2403 return "pc";
2404# elif defined(__aarch64__)
2405# define REG_STR(reg) #reg
2406# define MAKE_CASE(N) \
2407 case N: \
2408 return REG_STR(x##N)
2409 MAKE_CASE(0);
2410 MAKE_CASE(1);
2411 MAKE_CASE(2);
2412 MAKE_CASE(3);
2413 MAKE_CASE(4);
2414 MAKE_CASE(5);
2415 MAKE_CASE(6);
2416 MAKE_CASE(7);
2417 MAKE_CASE(8);
2418 MAKE_CASE(9);
2419 MAKE_CASE(10);
2420 MAKE_CASE(11);
2421 MAKE_CASE(12);
2422 MAKE_CASE(13);
2423 MAKE_CASE(14);
2424 MAKE_CASE(15);
2425 MAKE_CASE(16);
2426 MAKE_CASE(17);
2427 MAKE_CASE(18);
2428 MAKE_CASE(19);
2429 MAKE_CASE(20);
2430 MAKE_CASE(21);
2431 MAKE_CASE(22);
2432 MAKE_CASE(23);
2433 MAKE_CASE(24);
2434 MAKE_CASE(25);
2435 MAKE_CASE(26);
2436 MAKE_CASE(27);
2437 MAKE_CASE(28);
2438 case 29:
2439 return "fp";
2440 case 30:
2441 return "lr";
2442 case 31:
2443 return "sp";
2444# endif
2445# endif // SANITIZER_LINUX && SANITIZER_GLIBC
2446 default:
2447 return NULL;
2448 }
2449 return NULL;
2450}
2451
2452# if ((SANITIZER_LINUX && SANITIZER_GLIBC) || SANITIZER_NETBSD) && \
2453 (defined(__arm__) || defined(__aarch64__))
2454static uptr GetArmRegister(ucontext_t *ctx, int RegNum) {
2455 switch (RegNum) {
2456# if defined(__arm__) && !SANITIZER_NETBSD
2457# ifdef MAKE_CASE
2458# undef MAKE_CASE
2459# endif
2460# define MAKE_CASE(N) \
2461 case REG_R##N: \
2462 return ctx->uc_mcontext.arm_r##N
2463 MAKE_CASE(0);
2464 MAKE_CASE(1);
2465 MAKE_CASE(2);
2466 MAKE_CASE(3);
2467 MAKE_CASE(4);
2468 MAKE_CASE(5);
2469 MAKE_CASE(6);
2470 MAKE_CASE(7);
2471 MAKE_CASE(8);
2472 MAKE_CASE(9);
2473 MAKE_CASE(10);
2474 case REG_R11:
2475 return ctx->uc_mcontext.arm_fp;
2476 case REG_R12:
2477 return ctx->uc_mcontext.arm_ip;
2478 case REG_R13:
2479 return ctx->uc_mcontext.arm_sp;
2480 case REG_R14:
2481 return ctx->uc_mcontext.arm_lr;
2482 case REG_R15:
2483 return ctx->uc_mcontext.arm_pc;
2484# elif defined(__aarch64__)
2485# if SANITIZER_LINUX
2486 case 0 ... 30:
2487 return ctx->uc_mcontext.regs[RegNum];
2488 case 31:
2489 return ctx->uc_mcontext.sp;
2490# elif SANITIZER_NETBSD
2491 case 0 ... 31:
2492 return ctx->uc_mcontext.__gregs[RegNum];
2493# endif
2494# endif
2495 default:
2496 return 0;
2497 }
2498 return 0;
2499}
2500# endif // SANITIZER_LINUX && SANITIZER_GLIBC && (defined(__arm__) ||
2501 // defined(__aarch64__))
2502
2503UNUSED
2504static void DumpSingleReg(ucontext_t *ctx, int RegNum) {
2505 const char *RegName = RegNumToRegName(reg: RegNum);
2506# if SANITIZER_LINUX && SANITIZER_GLIBC || SANITIZER_NETBSD
2507# if defined(__x86_64__)
2508 Printf(format: "%s%s = 0x%016llx ", internal_strlen(s: RegName) == 2 ? " " : "",
2509 RegName,
2510# if SANITIZER_LINUX
2511 ctx->uc_mcontext.gregs[RegNum]
2512# elif SANITIZER_NETBSD
2513 (unsigned long long)ctx->uc_mcontext.__gregs[RegNum]
2514# endif
2515 );
2516# elif defined(__i386__)
2517 Printf("%s = 0x%08x ", RegName,
2518# if SANITIZER_LINUX
2519 ctx->uc_mcontext.gregs[RegNum]
2520# elif SANITIZER_NETBSD
2521 ctx->uc_mcontext.__gregs[RegNum]
2522# endif
2523 );
2524# elif defined(__arm__)
2525 Printf("%s%s = 0x%08zx ", internal_strlen(RegName) == 2 ? " " : "", RegName,
2526 GetArmRegister(ctx, RegNum));
2527# elif defined(__aarch64__)
2528 Printf("%s%s = 0x%016zx ", internal_strlen(RegName) == 2 ? " " : "", RegName,
2529 GetArmRegister(ctx, RegNum));
2530# else
2531 (void)RegName;
2532# endif
2533# else
2534 (void)RegName;
2535# endif
2536}
2537
2538void SignalContext::DumpAllRegisters(void *context) {
2539 ucontext_t *ucontext = (ucontext_t *)context;
2540# if SANITIZER_LINUX && SANITIZER_GLIBC || SANITIZER_NETBSD
2541# if defined(__x86_64__)
2542 Report(format: "Register values:\n");
2543 DumpSingleReg(ctx: ucontext, REG_RAX);
2544 DumpSingleReg(ctx: ucontext, REG_RBX);
2545 DumpSingleReg(ctx: ucontext, REG_RCX);
2546 DumpSingleReg(ctx: ucontext, REG_RDX);
2547 Printf(format: "\n");
2548 DumpSingleReg(ctx: ucontext, REG_RDI);
2549 DumpSingleReg(ctx: ucontext, REG_RSI);
2550 DumpSingleReg(ctx: ucontext, REG_RBP);
2551 DumpSingleReg(ctx: ucontext, REG_RSP);
2552 Printf(format: "\n");
2553 DumpSingleReg(ctx: ucontext, REG_R8);
2554 DumpSingleReg(ctx: ucontext, REG_R9);
2555 DumpSingleReg(ctx: ucontext, REG_R10);
2556 DumpSingleReg(ctx: ucontext, REG_R11);
2557 Printf(format: "\n");
2558 DumpSingleReg(ctx: ucontext, REG_R12);
2559 DumpSingleReg(ctx: ucontext, REG_R13);
2560 DumpSingleReg(ctx: ucontext, REG_R14);
2561 DumpSingleReg(ctx: ucontext, REG_R15);
2562 Printf(format: "\n");
2563# elif defined(__i386__)
2564 // Duplication of this report print is caused by partial support
2565 // of register values dumping. In case of unsupported yet architecture let's
2566 // avoid printing 'Register values:' without actual values in the following
2567 // output.
2568 Report("Register values:\n");
2569 DumpSingleReg(ucontext, REG_EAX);
2570 DumpSingleReg(ucontext, REG_EBX);
2571 DumpSingleReg(ucontext, REG_ECX);
2572 DumpSingleReg(ucontext, REG_EDX);
2573 Printf("\n");
2574 DumpSingleReg(ucontext, REG_EDI);
2575 DumpSingleReg(ucontext, REG_ESI);
2576 DumpSingleReg(ucontext, REG_EBP);
2577 DumpSingleReg(ucontext, REG_ESP);
2578 Printf("\n");
2579# elif defined(__arm__) && !SANITIZER_NETBSD
2580 Report("Register values:\n");
2581 DumpSingleReg(ucontext, REG_R0);
2582 DumpSingleReg(ucontext, REG_R1);
2583 DumpSingleReg(ucontext, REG_R2);
2584 DumpSingleReg(ucontext, REG_R3);
2585 Printf("\n");
2586 DumpSingleReg(ucontext, REG_R4);
2587 DumpSingleReg(ucontext, REG_R5);
2588 DumpSingleReg(ucontext, REG_R6);
2589 DumpSingleReg(ucontext, REG_R7);
2590 Printf("\n");
2591 DumpSingleReg(ucontext, REG_R8);
2592 DumpSingleReg(ucontext, REG_R9);
2593 DumpSingleReg(ucontext, REG_R10);
2594 DumpSingleReg(ucontext, REG_R11);
2595 Printf("\n");
2596 DumpSingleReg(ucontext, REG_R12);
2597 DumpSingleReg(ucontext, REG_R13);
2598 DumpSingleReg(ucontext, REG_R14);
2599 DumpSingleReg(ucontext, REG_R15);
2600 Printf("\n");
2601# elif defined(__aarch64__)
2602 Report("Register values:\n");
2603 for (int i = 0; i <= 31; ++i) {
2604 DumpSingleReg(ucontext, i);
2605 if (i % 4 == 3)
2606 Printf("\n");
2607 }
2608# else
2609 (void)ucontext;
2610# endif
2611# elif SANITIZER_FREEBSD
2612# if defined(__x86_64__)
2613 Report("Register values:\n");
2614 Printf("rax = 0x%016lx ", ucontext->uc_mcontext.mc_rax);
2615 Printf("rbx = 0x%016lx ", ucontext->uc_mcontext.mc_rbx);
2616 Printf("rcx = 0x%016lx ", ucontext->uc_mcontext.mc_rcx);
2617 Printf("rdx = 0x%016lx ", ucontext->uc_mcontext.mc_rdx);
2618 Printf("\n");
2619 Printf("rdi = 0x%016lx ", ucontext->uc_mcontext.mc_rdi);
2620 Printf("rsi = 0x%016lx ", ucontext->uc_mcontext.mc_rsi);
2621 Printf("rbp = 0x%016lx ", ucontext->uc_mcontext.mc_rbp);
2622 Printf("rsp = 0x%016lx ", ucontext->uc_mcontext.mc_rsp);
2623 Printf("\n");
2624 Printf(" r8 = 0x%016lx ", ucontext->uc_mcontext.mc_r8);
2625 Printf(" r9 = 0x%016lx ", ucontext->uc_mcontext.mc_r9);
2626 Printf("r10 = 0x%016lx ", ucontext->uc_mcontext.mc_r10);
2627 Printf("r11 = 0x%016lx ", ucontext->uc_mcontext.mc_r11);
2628 Printf("\n");
2629 Printf("r12 = 0x%016lx ", ucontext->uc_mcontext.mc_r12);
2630 Printf("r13 = 0x%016lx ", ucontext->uc_mcontext.mc_r13);
2631 Printf("r14 = 0x%016lx ", ucontext->uc_mcontext.mc_r14);
2632 Printf("r15 = 0x%016lx ", ucontext->uc_mcontext.mc_r15);
2633 Printf("\n");
2634# elif defined(__i386__)
2635 Report("Register values:\n");
2636 Printf("eax = 0x%08x ", ucontext->uc_mcontext.mc_eax);
2637 Printf("ebx = 0x%08x ", ucontext->uc_mcontext.mc_ebx);
2638 Printf("ecx = 0x%08x ", ucontext->uc_mcontext.mc_ecx);
2639 Printf("edx = 0x%08x ", ucontext->uc_mcontext.mc_edx);
2640 Printf("\n");
2641 Printf("edi = 0x%08x ", ucontext->uc_mcontext.mc_edi);
2642 Printf("esi = 0x%08x ", ucontext->uc_mcontext.mc_esi);
2643 Printf("ebp = 0x%08x ", ucontext->uc_mcontext.mc_ebp);
2644 Printf("esp = 0x%08x ", ucontext->uc_mcontext.mc_esp);
2645 Printf("\n");
2646# else
2647 (void)ucontext;
2648# endif
2649# else
2650 (void)ucontext;
2651# endif
2652 // FIXME: Implement this for other OSes and architectures.
2653}
2654
2655static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
2656# if SANITIZER_NETBSD
2657 // This covers all NetBSD architectures
2658 ucontext_t *ucontext = (ucontext_t *)context;
2659 *pc = _UC_MACHINE_PC(ucontext);
2660 *bp = _UC_MACHINE_FP(ucontext);
2661 *sp = _UC_MACHINE_SP(ucontext);
2662# elif defined(__arm__)
2663 ucontext_t *ucontext = (ucontext_t *)context;
2664 *pc = ucontext->uc_mcontext.arm_pc;
2665 *bp = ucontext->uc_mcontext.arm_fp;
2666 *sp = ucontext->uc_mcontext.arm_sp;
2667# elif defined(__aarch64__)
2668# if SANITIZER_FREEBSD
2669 ucontext_t *ucontext = (ucontext_t *)context;
2670 *pc = ucontext->uc_mcontext.mc_gpregs.gp_elr;
2671 *bp = ucontext->uc_mcontext.mc_gpregs.gp_x[29];
2672 *sp = ucontext->uc_mcontext.mc_gpregs.gp_sp;
2673# else
2674 ucontext_t *ucontext = (ucontext_t *)context;
2675 *pc = ucontext->uc_mcontext.pc;
2676 *bp = ucontext->uc_mcontext.regs[29];
2677 *sp = ucontext->uc_mcontext.sp;
2678# endif
2679# elif defined(__hppa__)
2680 ucontext_t *ucontext = (ucontext_t *)context;
2681 *pc = ucontext->uc_mcontext.sc_iaoq[0];
2682 /* GCC uses %r3 whenever a frame pointer is needed. */
2683 *bp = ucontext->uc_mcontext.sc_gr[3];
2684 *sp = ucontext->uc_mcontext.sc_gr[30];
2685# elif defined(__x86_64__)
2686# if SANITIZER_FREEBSD
2687 ucontext_t *ucontext = (ucontext_t *)context;
2688 *pc = ucontext->uc_mcontext.mc_rip;
2689 *bp = ucontext->uc_mcontext.mc_rbp;
2690 *sp = ucontext->uc_mcontext.mc_rsp;
2691# elif SANITIZER_HAIKU
2692 ucontext_t *ucontext = (ucontext_t *)context;
2693 *pc = ucontext->uc_mcontext.rip;
2694 *bp = ucontext->uc_mcontext.rbp;
2695 *sp = ucontext->uc_mcontext.rsp;
2696# else
2697 ucontext_t *ucontext = (ucontext_t *)context;
2698 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
2699 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
2700 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
2701# endif
2702# elif defined(__i386__)
2703# if SANITIZER_FREEBSD
2704 ucontext_t *ucontext = (ucontext_t *)context;
2705 *pc = ucontext->uc_mcontext.mc_eip;
2706 *bp = ucontext->uc_mcontext.mc_ebp;
2707 *sp = ucontext->uc_mcontext.mc_esp;
2708# elif SANITIZER_HAIKU
2709 ucontext_t *ucontext = (ucontext_t *)context;
2710 *pc = ucontext->uc_mcontext.eip;
2711 *bp = ucontext->uc_mcontext.ebp;
2712 *sp = ucontext->uc_mcontext.esp;
2713# else
2714 ucontext_t *ucontext = (ucontext_t *)context;
2715# if SANITIZER_SOLARIS
2716 /* Use the numeric values: the symbolic ones are undefined by llvm
2717 include/llvm/Support/Solaris.h. */
2718# ifndef REG_EIP
2719# define REG_EIP 14 // REG_PC
2720# endif
2721# ifndef REG_EBP
2722# define REG_EBP 6 // REG_FP
2723# endif
2724# ifndef REG_UESP
2725# define REG_UESP 17 // REG_SP
2726# endif
2727# endif
2728 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
2729 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
2730 *sp = ucontext->uc_mcontext.gregs[REG_UESP];
2731# endif
2732# elif defined(__powerpc__) || defined(__powerpc64__)
2733# if SANITIZER_FREEBSD
2734 ucontext_t *ucontext = (ucontext_t *)context;
2735 *pc = ucontext->uc_mcontext.mc_srr0;
2736 *sp = ucontext->uc_mcontext.mc_frame[1];
2737 *bp = ucontext->uc_mcontext.mc_frame[31];
2738# else
2739 ucontext_t *ucontext = (ucontext_t *)context;
2740 *pc = ucontext->uc_mcontext.regs->nip;
2741 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
2742 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
2743 // pointer, but GCC always uses r31 when we need a frame pointer.
2744 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
2745# endif
2746# elif defined(__sparc__)
2747# if defined(__arch64__) || defined(__sparcv9)
2748# define STACK_BIAS 2047
2749# else
2750# define STACK_BIAS 0
2751# endif
2752# if SANITIZER_SOLARIS
2753 ucontext_t *ucontext = (ucontext_t *)context;
2754 *pc = ucontext->uc_mcontext.gregs[REG_PC];
2755 *sp = ucontext->uc_mcontext.gregs[REG_SP] + STACK_BIAS;
2756 // Avoid SEGV when dereferencing sp on stack overflow with non-faulting load.
2757 // This requires a SPARC V9 CPU. Cannot use #ASI_PNF here: only supported
2758 // since clang-19.
2759# if defined(__sparcv9)
2760 asm("ldxa [%[fp]] 0x82, %[bp]"
2761# else
2762 asm("lduwa [%[fp]] 0x82, %[bp]"
2763# endif
2764 : [bp] "=r"(*bp)
2765 : [fp] "r"(&((struct frame *)*sp)->fr_savfp));
2766 if (*bp)
2767 *bp += STACK_BIAS;
2768# else
2769 // Historical BSDism here.
2770 struct sigcontext *scontext = (struct sigcontext *)context;
2771# if defined(__arch64__)
2772 *pc = scontext->sigc_regs.tpc;
2773 *sp = scontext->sigc_regs.u_regs[14] + STACK_BIAS;
2774# else
2775 *pc = scontext->si_regs.pc;
2776 *sp = scontext->si_regs.u_regs[14];
2777# endif
2778 *bp = (uptr)((uhwptr *)*sp)[14] + STACK_BIAS;
2779# endif
2780# elif defined(__mips__)
2781 ucontext_t *ucontext = (ucontext_t *)context;
2782 *pc = ucontext->uc_mcontext.pc;
2783 *bp = ucontext->uc_mcontext.gregs[30];
2784 *sp = ucontext->uc_mcontext.gregs[29];
2785# elif defined(__s390__)
2786 ucontext_t *ucontext = (ucontext_t *)context;
2787# if defined(__s390x__)
2788 *pc = ucontext->uc_mcontext.psw.addr;
2789# else
2790 *pc = ucontext->uc_mcontext.psw.addr & 0x7fffffff;
2791# endif
2792 *bp = ucontext->uc_mcontext.gregs[11];
2793 *sp = ucontext->uc_mcontext.gregs[15];
2794# elif defined(__riscv)
2795 ucontext_t *ucontext = (ucontext_t *)context;
2796# if SANITIZER_FREEBSD
2797 *pc = ucontext->uc_mcontext.mc_gpregs.gp_sepc;
2798 *bp = ucontext->uc_mcontext.mc_gpregs.gp_s[0];
2799 *sp = ucontext->uc_mcontext.mc_gpregs.gp_sp;
2800# else
2801 *pc = ucontext->uc_mcontext.__gregs[REG_PC];
2802 *bp = ucontext->uc_mcontext.__gregs[REG_S0];
2803 *sp = ucontext->uc_mcontext.__gregs[REG_SP];
2804# endif
2805# elif defined(__hexagon__)
2806 ucontext_t *ucontext = (ucontext_t *)context;
2807 *pc = ucontext->uc_mcontext.pc;
2808 *bp = ucontext->uc_mcontext.r30;
2809 *sp = ucontext->uc_mcontext.r29;
2810# elif defined(__loongarch__)
2811 ucontext_t *ucontext = (ucontext_t *)context;
2812 *pc = ucontext->uc_mcontext.__pc;
2813 *bp = ucontext->uc_mcontext.__gregs[22];
2814 *sp = ucontext->uc_mcontext.__gregs[3];
2815# elif defined(__alpha__)
2816 ucontext_t* ucontext = (ucontext_t*)context;
2817 *pc = ucontext->uc_mcontext.sc_pc;
2818 *bp = ucontext->uc_mcontext.sc_regs[15]; // $fp / $s6
2819 *sp = ucontext->uc_mcontext.sc_regs[30]; // $sp
2820# else
2821# error "Unsupported arch"
2822# endif
2823}
2824
2825void SignalContext::InitPcSpBp() { GetPcSpBp(context, pc: &pc, sp: &sp, bp: &bp); }
2826
2827void InitializePlatformEarly() { InitTlsSize(); }
2828
2829void CheckASLR() {
2830# if SANITIZER_NETBSD
2831 int mib[3];
2832 int paxflags;
2833 uptr len = sizeof(paxflags);
2834
2835 mib[0] = CTL_PROC;
2836 mib[1] = internal_getpid();
2837 mib[2] = PROC_PID_PAXFLAGS;
2838
2839 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
2840 Printf("sysctl failed\n");
2841 Die();
2842 }
2843
2844 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_ASLR)) {
2845 Printf(
2846 "This sanitizer is not compatible with enabled ASLR.\n"
2847 "To disable ASLR, please run \"paxctl +a %s\" and try again.\n",
2848 GetArgv()[0]);
2849 Die();
2850 }
2851# elif SANITIZER_FREEBSD
2852 int aslr_status;
2853 int r = internal_procctl(P_PID, 0, PROC_ASLR_STATUS, &aslr_status);
2854 if (UNLIKELY(r == -1)) {
2855 // We're making things less 'dramatic' here since
2856 // the cmd is not necessarily guaranteed to be here
2857 // just yet regarding FreeBSD release
2858 return;
2859 }
2860 if ((aslr_status & PROC_ASLR_ACTIVE) != 0) {
2861 VReport(1,
2862 "This sanitizer is not compatible with enabled ASLR "
2863 "and binaries compiled with PIE\n"
2864 "ASLR will be disabled and the program re-executed.\n");
2865 int aslr_ctl = PROC_ASLR_FORCE_DISABLE;
2866 CHECK_NE(internal_procctl(P_PID, 0, PROC_ASLR_CTL, &aslr_ctl), -1);
2867 ReExec();
2868 }
2869# elif SANITIZER_PPC64V2
2870 // Disable ASLR for Linux PPC64LE.
2871 int old_personality = personality(0xffffffff);
2872 if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) {
2873 VReport(1,
2874 "WARNING: Program is being run with address space layout "
2875 "randomization (ASLR) enabled which prevents the thread and "
2876 "memory sanitizers from working on powerpc64le.\n"
2877 "ASLR will be disabled and the program re-executed.\n");
2878 CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1);
2879 ReExec();
2880 }
2881# else
2882 // Do nothing
2883# endif
2884}
2885
2886void CheckMPROTECT() {
2887# if SANITIZER_NETBSD
2888 int mib[3];
2889 int paxflags;
2890 uptr len = sizeof(paxflags);
2891
2892 mib[0] = CTL_PROC;
2893 mib[1] = internal_getpid();
2894 mib[2] = PROC_PID_PAXFLAGS;
2895
2896 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
2897 Printf("sysctl failed\n");
2898 Die();
2899 }
2900
2901 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_MPROTECT)) {
2902 Printf("This sanitizer is not compatible with enabled MPROTECT\n");
2903 Die();
2904 }
2905# else
2906 // Do nothing
2907# endif
2908}
2909
2910void OnDlOpen(const char* filename, int flag) {
2911# ifdef RTLD_DEEPBIND
2912 if (flag & RTLD_DEEPBIND) {
2913 Report(
2914 format: "You are trying to dlopen a %s shared library with RTLD_DEEPBIND flag"
2915 " which is incompatible with sanitizer runtime "
2916 "(see https://github.com/google/sanitizers/issues/611 for details"
2917 "). If you want to run %s library under sanitizers please remove "
2918 "RTLD_DEEPBIND from dlopen flags.\n",
2919 filename, filename);
2920 Die();
2921 }
2922# endif
2923}
2924
2925uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
2926 uptr *largest_gap_found,
2927 uptr *max_occupied_addr) {
2928 UNREACHABLE("FindAvailableMemoryRange is not available");
2929 return 0;
2930}
2931
2932bool GetRandom(void *buffer, uptr length, bool blocking) {
2933 if (!buffer || !length || length > 256)
2934 return false;
2935# if SANITIZER_USE_GETENTROPY
2936 uptr rnd = getentropy(buffer, length);
2937 int rverrno = 0;
2938 if (internal_iserror(rnd, &rverrno) && rverrno == EFAULT)
2939 return false;
2940 else if (rnd == 0)
2941 return true;
2942# endif // SANITIZER_USE_GETENTROPY
2943
2944# if SANITIZER_USE_GETRANDOM
2945 static atomic_uint8_t skip_getrandom_syscall;
2946 if (!atomic_load_relaxed(a: &skip_getrandom_syscall)) {
2947 // Up to 256 bytes, getrandom will not be interrupted.
2948 uptr res = internal_syscall(SYSCALL(getrandom), arg1: buffer, arg2: length,
2949 arg3: blocking ? 0 : GRND_NONBLOCK);
2950 int rverrno = 0;
2951 if (internal_iserror(retval: res, rverrno: &rverrno) && rverrno == ENOSYS)
2952 atomic_store_relaxed(a: &skip_getrandom_syscall, v: 1);
2953 else if (res == length)
2954 return true;
2955 }
2956# endif // SANITIZER_USE_GETRANDOM
2957 // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
2958 // blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
2959 uptr fd = internal_open(filename: "/dev/urandom", O_RDONLY);
2960 if (internal_iserror(retval: fd))
2961 return false;
2962 uptr res = internal_read(fd, buf: buffer, count: length);
2963 if (internal_iserror(retval: res))
2964 return false;
2965 internal_close(fd);
2966 return true;
2967}
2968
2969} // namespace __sanitizer
2970
2971#endif
2972