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