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