1//===-- sanitizer_linux.cc ------------------------------------------------===//
2//
3// This file is distributed under the University of Illinois Open Source
4// License. See LICENSE.TXT for details.
5//
6//===----------------------------------------------------------------------===//
7//
8// This file is shared between AddressSanitizer and ThreadSanitizer
9// run-time libraries and implements linux-specific functions from
10// sanitizer_libc.h.
11//===----------------------------------------------------------------------===//
12
13#include "sanitizer_platform.h"
14
15#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
16    SANITIZER_OPENBSD || SANITIZER_SOLARIS
17
18#include "sanitizer_common.h"
19#include "sanitizer_flags.h"
20#include "sanitizer_getauxval.h"
21#include "sanitizer_internal_defs.h"
22#include "sanitizer_libc.h"
23#include "sanitizer_linux.h"
24#include "sanitizer_mutex.h"
25#include "sanitizer_placement_new.h"
26#include "sanitizer_procmaps.h"
27
28#if SANITIZER_LINUX
29#include <asm/param.h>
30#endif
31
32// For mips64, syscall(__NR_stat) fills the buffer in the 'struct kernel_stat'
33// format. Struct kernel_stat is defined as 'struct stat' in asm/stat.h. To
34// access stat from asm/stat.h, without conflicting with definition in
35// sys/stat.h, we use this trick.
36#if SANITIZER_LINUX
37#if defined(__mips64)
38#include <asm/unistd.h>
39#include <sys/types.h>
40#define stat kernel_stat
41#include <asm/stat.h>
42#undef stat
43#endif
44#endif
45
46#if SANITIZER_NETBSD
47#include <lwp.h>
48#endif
49
50#include <dlfcn.h>
51#include <errno.h>
52#include <fcntl.h>
53#include <link.h>
54#include <pthread.h>
55#include <sched.h>
56#include <signal.h>
57#include <sys/mman.h>
58#include <sys/param.h>
59#if !SANITIZER_SOLARIS
60#include <sys/ptrace.h>
61#endif
62#include <sys/resource.h>
63#include <sys/stat.h>
64#include <sys/syscall.h>
65#include <sys/time.h>
66#include <sys/types.h>
67#if !SANITIZER_OPENBSD
68#include <ucontext.h>
69#endif
70#if SANITIZER_OPENBSD
71#include <sys/futex.h>
72#include <sys/sysctl.h>
73#endif
74#include <unistd.h>
75
76#if SANITIZER_LINUX
77#include <sys/utsname.h>
78#endif
79
80#if SANITIZER_LINUX && !SANITIZER_ANDROID
81#include <sys/personality.h>
82#endif
83
84#if SANITIZER_FREEBSD
85#include <sys/exec.h>
86#include <sys/sysctl.h>
87#include <machine/atomic.h>
88extern "C" {
89// <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
90// FreeBSD 9.2 and 10.0.
91#include <sys/umtx.h>
92}
93#include <sys/thr.h>
94#endif  // SANITIZER_FREEBSD
95#if SANITIZER_NETBSD
96#include <limits.h>	// For NAME_MAX
97#include <sys/sysctl.h>
98#include <sys/exec.h>
99extern struct ps_strings *__ps_strings;
100extern char **environ;  // provided by crt1
101#endif  // SANITIZER_NETBSD
102
103#if SANITIZER_NETBSD
104#include <limits.h>  // For NAME_MAX
105#include <sys/sysctl.h>
106#include <sys/exec.h>
107extern struct ps_strings *__ps_strings;
108#endif  // SANITIZER_NETBSD
109
110#if SANITIZER_SOLARIS
111#include <stdlib.h>
112#include <thread.h>
113#define environ _environ
114#endif
115
116extern char **environ;
117
118#if SANITIZER_LINUX
119// <linux/time.h>
120struct kernel_timeval {
121  long tv_sec;
122  long tv_usec;
123};
124
125// <linux/futex.h> is broken on some linux distributions.
126const int FUTEX_WAIT = 0;
127const int FUTEX_WAKE = 1;
128const int FUTEX_PRIVATE_FLAG = 128;
129const int FUTEX_WAIT_PRIVATE = FUTEX_WAIT | FUTEX_PRIVATE_FLAG;
130const int FUTEX_WAKE_PRIVATE = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
131#endif  // SANITIZER_LINUX
132
133// Are we using 32-bit or 64-bit Linux syscalls?
134// x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
135// but it still needs to use 64-bit syscalls.
136#if SANITIZER_LINUX && (defined(__x86_64__) || defined(__powerpc64__) ||       \
137                        SANITIZER_WORDSIZE == 64)
138# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
139#else
140# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
141#endif
142
143#if defined(__x86_64__) || SANITIZER_MIPS64
144extern "C" {
145extern void internal_sigreturn();
146}
147#endif
148
149// Note : FreeBSD had implemented both
150// Linux and OpenBSD apis, available from
151// future 12.x version most likely
152#if SANITIZER_LINUX && defined(__NR_getrandom)
153# if !defined(GRND_NONBLOCK)
154#  define GRND_NONBLOCK 1
155# endif
156# define SANITIZER_USE_GETRANDOM 1
157#else
158# define SANITIZER_USE_GETRANDOM 0
159#endif  // SANITIZER_LINUX && defined(__NR_getrandom)
160
161#if SANITIZER_OPENBSD
162# define SANITIZER_USE_GETENTROPY 1
163#else
164# if SANITIZER_FREEBSD && __FreeBSD_version >= 1200000
165#   define SANITIZER_USE_GETENTROPY 1
166# else
167#   define SANITIZER_USE_GETENTROPY 0
168# endif
169#endif // SANITIZER_USE_GETENTROPY
170
171namespace __sanitizer {
172
173#if SANITIZER_LINUX && defined(__x86_64__)
174#include "sanitizer_syscall_linux_x86_64.inc"
175#elif SANITIZER_LINUX && defined(__aarch64__)
176#include "sanitizer_syscall_linux_aarch64.inc"
177#elif SANITIZER_LINUX && defined(__arm__)
178#include "sanitizer_syscall_linux_arm.inc"
179#else
180#include "sanitizer_syscall_generic.inc"
181#endif
182
183// --------------- sanitizer_libc.h
184#if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
185#if !SANITIZER_S390 && !SANITIZER_OPENBSD
186uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
187                   OFF_T offset) {
188#if SANITIZER_NETBSD
189
190#if !defined(_LP64) && BYTE_ORDER == _BIG_ENDIAN
191#define __SYSCALL_TO_UINTPTR_T(V)       ((uintptr_t)((V)>>32))
192#else
193#define __SYSCALL_TO_UINTPTR_T(V)       ((uintptr_t)(V))
194#endif
195
196  return __SYSCALL_TO_UINTPTR_T(
197	    internal_syscall64(SYSCALL(mmap), addr, length, prot, flags, fd,
198			      (long)0, offset));
199  //return internal_syscall_ptr(SYSCALL(mmap), addr, length, prot, flags, fd,
200  //                            (long)0, offset);
201#elif SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
202  return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd,
203                          offset, 0);
204#else
205  // mmap2 specifies file offset in 4096-byte units.
206  CHECK(IsAligned(offset, 4096));
207  return internal_syscall(SYSCALL(mmap2), addr, length, prot, flags, fd,
208                          offset / 4096);
209#endif
210}
211#endif // !SANITIZER_S390 && !SANITIZER_OPENBSD
212
213#if !SANITIZER_OPENBSD
214uptr internal_munmap(void *addr, uptr length) {
215  return internal_syscall_ptr(SYSCALL(munmap), (uptr)addr, length);
216}
217
218int internal_mprotect(void *addr, uptr length, int prot) {
219  return internal_syscall_ptr(SYSCALL(mprotect), (uptr)addr, length, prot);
220}
221#endif
222
223uptr internal_close(fd_t fd) {
224  return internal_syscall(SYSCALL(close), fd);
225}
226
227uptr internal_open(const char *filename, int flags) {
228#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
229  return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags);
230#else
231  return internal_syscall_ptr(SYSCALL(open), (uptr)filename, flags);
232#endif
233}
234
235uptr internal_open(const char *filename, int flags, u32 mode) {
236#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
237  return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags,
238                          mode);
239#else
240  return internal_syscall_ptr(SYSCALL(open), (uptr)filename, flags, mode);
241#endif
242}
243
244uptr internal_read(fd_t fd, void *buf, uptr count) {
245  sptr res;
246  HANDLE_EINTR(res,
247               (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf, count));
248  return res;
249}
250
251uptr internal_write(fd_t fd, const void *buf, uptr count) {
252  sptr res;
253  HANDLE_EINTR(res,
254               (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf, count));
255  return res;
256}
257
258uptr internal_ftruncate(fd_t fd, uptr size) {
259  sptr res;
260#if SANITIZER_NETBSD
261  //HANDLE_EINTR(res, internal_syscall64(SYSCALL(ftruncate), fd, 0, (s64)size));
262  HANDLE_EINTR(res, internal_syscall(SYSCALL(ftruncate), fd, 0, (s64)size));
263#else
264  HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(ftruncate), fd,
265               (OFF_T)size));
266#endif
267  return res;
268}
269
270#if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && SANITIZER_LINUX
271static void stat64_to_stat(struct stat64 *in, struct stat *out) {
272  internal_memset(out, 0, sizeof(*out));
273  out->st_dev = in->st_dev;
274  out->st_ino = in->st_ino;
275  out->st_mode = in->st_mode;
276  out->st_nlink = in->st_nlink;
277  out->st_uid = in->st_uid;
278  out->st_gid = in->st_gid;
279  out->st_rdev = in->st_rdev;
280  out->st_size = in->st_size;
281  out->st_blksize = in->st_blksize;
282  out->st_blocks = in->st_blocks;
283  out->st_atime = in->st_atime;
284  out->st_mtime = in->st_mtime;
285  out->st_ctime = in->st_ctime;
286}
287#endif
288
289#if defined(__mips64) && SANITIZER_LINUX
290// Undefine compatibility macros from <sys/stat.h>
291// so that they would not clash with the kernel_stat
292// st_[a|m|c]time fields
293#undef st_atime
294#undef st_mtime
295#undef st_ctime
296#if defined(SANITIZER_ANDROID)
297// Bionic sys/stat.h defines additional macros
298// for compatibility with the old NDKs and
299// they clash with the kernel_stat structure
300// st_[a|m|c]time_nsec fields.
301#undef st_atime_nsec
302#undef st_mtime_nsec
303#undef st_ctime_nsec
304#endif
305
306static void kernel_stat_to_stat(struct kernel_stat *in, struct stat *out) {
307  internal_memset(out, 0, sizeof(*out));
308  out->st_dev = in->st_dev;
309  out->st_ino = in->st_ino;
310  out->st_mode = in->st_mode;
311  out->st_nlink = in->st_nlink;
312  out->st_uid = in->st_uid;
313  out->st_gid = in->st_gid;
314  out->st_rdev = in->st_rdev;
315  out->st_size = in->st_size;
316  out->st_blksize = in->st_blksize;
317  out->st_blocks = in->st_blocks;
318#if defined(__USE_MISC)     || \
319    defined(__USE_XOPEN2K8) || \
320    defined(SANITIZER_ANDROID)
321  out->st_atim.tv_sec = in->st_atime;
322  out->st_atim.tv_nsec = in->st_atime_nsec;
323  out->st_mtim.tv_sec = in->st_mtime;
324  out->st_mtim.tv_nsec = in->st_mtime_nsec;
325  out->st_ctim.tv_sec = in->st_ctime;
326  out->st_ctim.tv_nsec = in->st_ctime_nsec;
327#else
328  out->st_atime = in->st_atime;
329  out->st_atimensec = in->st_atime_nsec;
330  out->st_mtime = in->st_mtime;
331  out->st_mtimensec = in->st_mtime_nsec;
332  out->st_ctime = in->st_ctime;
333  out->st_atimensec = in->st_ctime_nsec;
334#endif
335}
336#endif
337
338uptr internal_stat(const char *path, void *buf) {
339#if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD
340  return internal_syscall(SYSCALL(fstatat), AT_FDCWD, (uptr)path, (uptr)buf, 0);
341#elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
342  return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, (uptr)buf,
343                          0);
344#elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
345# if defined(__mips64)
346  // For mips64, stat syscall fills buffer in the format of kernel_stat
347  struct kernel_stat kbuf;
348  int res = internal_syscall(SYSCALL(stat), path, &kbuf);
349  kernel_stat_to_stat(&kbuf, (struct stat *)buf);
350  return res;
351# else
352  return internal_syscall(SYSCALL(stat), (uptr)path, (uptr)buf);
353# endif
354#else
355  struct stat64 buf64;
356  int res = internal_syscall(SYSCALL(stat64), path, &buf64);
357  stat64_to_stat(&buf64, (struct stat *)buf);
358  return res;
359#endif
360}
361
362uptr internal_lstat(const char *path, void *buf) {
363#if SANITIZER_NETBSD
364  return internal_syscall(SYSCALL(lstat), path, buf);
365#elif SANITIZER_FREEBSD || SANITIZER_OPENBSD
366  return internal_syscall(SYSCALL(fstatat), AT_FDCWD, (uptr)path, (uptr)buf,
367                          AT_SYMLINK_NOFOLLOW);
368#elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
369  return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, (uptr)buf,
370                          AT_SYMLINK_NOFOLLOW);
371#elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
372# if SANITIZER_MIPS64
373  // For mips64, lstat syscall fills buffer in the format of kernel_stat
374  struct kernel_stat kbuf;
375  int res = internal_syscall(SYSCALL(lstat), path, &kbuf);
376  kernel_stat_to_stat(&kbuf, (struct stat *)buf);
377  return res;
378# else
379  return internal_syscall(SYSCALL(lstat), (uptr)path, (uptr)buf);
380# endif
381#else
382  struct stat64 buf64;
383  int res = internal_syscall(SYSCALL(lstat64), path, &buf64);
384  stat64_to_stat(&buf64, (struct stat *)buf);
385  return res;
386#endif
387}
388
389uptr internal_fstat(fd_t fd, void *buf) {
390#if SANITIZER_NETBSD
391  return internal_syscall_ptr(SYSCALL(fstat), fd, (uptr)buf);
392#elif SANITIZER_FREEBSD || SANITIZER_OPENBSD || \
393    SANITIZER_LINUX_USES_64BIT_SYSCALLS
394#if SANITIZER_MIPS64 && !SANITIZER_OPENBSD
395  // For mips64, fstat syscall fills buffer in the format of kernel_stat
396  struct kernel_stat kbuf;
397  int res = internal_syscall(SYSCALL(fstat), fd, &kbuf);
398  kernel_stat_to_stat(&kbuf, (struct stat *)buf);
399  return res;
400# else
401  return internal_syscall(SYSCALL(fstat), fd, (uptr)buf);
402# endif
403#else
404  struct stat64 buf64;
405  int res = internal_syscall(SYSCALL(fstat64), fd, &buf64);
406  stat64_to_stat(&buf64, (struct stat *)buf);
407  return res;
408#endif
409}
410
411uptr internal_filesize(fd_t fd) {
412  struct stat st;
413  if (internal_fstat(fd, &st))
414    return -1;
415  return (uptr)st.st_size;
416}
417
418uptr internal_dup2(int oldfd, int newfd) {
419#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
420  return internal_syscall(SYSCALL(dup3), oldfd, newfd, 0);
421#else
422  return internal_syscall(SYSCALL(dup2), oldfd, newfd);
423#endif
424}
425
426uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
427#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
428  return internal_syscall(SYSCALL(readlinkat), AT_FDCWD, (uptr)path, (uptr)buf,
429                          bufsize);
430#elif SANITIZER_OPENBSD
431  return internal_syscall(SYSCALL(readlinkat), AT_FDCWD, (uptr)path, (uptr)buf,
432                          bufsize);
433#else
434  return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize);
435#endif
436}
437
438uptr internal_unlink(const char *path) {
439#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS || SANITIZER_OPENBSD
440  return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0);
441#else
442  return internal_syscall_ptr(SYSCALL(unlink), (uptr)path);
443#endif
444}
445
446uptr internal_rename(const char *oldpath, const char *newpath) {
447#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS || SANITIZER_OPENBSD
448  return internal_syscall(SYSCALL(renameat), AT_FDCWD, (uptr)oldpath, AT_FDCWD,
449                          (uptr)newpath);
450#else
451  return internal_syscall_ptr(SYSCALL(rename), (uptr)oldpath, (uptr)newpath);
452#endif
453}
454
455uptr internal_sched_yield() {
456  return internal_syscall(SYSCALL(sched_yield));
457}
458
459void internal__exit(int exitcode) {
460#if SANITIZER_FREEBSD || SANITIZER_OPENBSD
461  internal_syscall(SYSCALL(exit), exitcode);
462#else
463  internal_syscall(SYSCALL(exit_group), exitcode);
464#endif
465  Die();  // Unreachable.
466}
467
468unsigned int internal_sleep(unsigned int seconds) {
469  struct timespec ts;
470  ts.tv_sec = 1;
471  ts.tv_nsec = 0;
472  int res = internal_syscall_ptr(SYSCALL(nanosleep), &ts, &ts);
473  if (res) return ts.tv_sec;
474  return 0;
475}
476
477uptr internal_execve(const char *filename, char *const argv[],
478                     char *const envp[]) {
479  return internal_syscall_ptr(SYSCALL(execve), (uptr)filename, (uptr)argv,
480                          (uptr)envp);
481}
482#endif  // !SANITIZER_SOLARIS && !SANITIZER_NETBSD
483
484// ----------------- sanitizer_common.h
485bool FileExists(const char *filename) {
486  struct stat st;
487#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
488  if (internal_syscall(SYSCALL(newfstatat), AT_FDCWD, filename, &st, 0))
489#else
490  if (internal_stat(filename, &st))
491#endif
492    return false;
493  // Sanity check: filename is a regular file.
494  return S_ISREG(st.st_mode);
495}
496
497#if !SANITIZER_NETBSD
498tid_t GetTid() {
499#if SANITIZER_FREEBSD
500  long Tid;
501  thr_self(&Tid);
502  return Tid;
503#elif SANITIZER_OPENBSD
504  return internal_syscall(SYSCALL(getthrid));
505#elif SANITIZER_SOLARIS
506  return thr_self();
507#else
508  return internal_syscall(SYSCALL(gettid));
509#endif
510}
511
512int TgKill(pid_t pid, tid_t tid, int sig) {
513#if SANITIZER_LINUX
514  return internal_syscall(SYSCALL(tgkill), pid, tid, sig);
515#elif SANITIZER_FREEBSD
516  return internal_syscall(SYSCALL(thr_kill2), pid, tid, sig);
517#elif SANITIZER_OPENBSD
518  (void)pid;
519  return internal_syscall(SYSCALL(thrkill), tid, sig, nullptr);
520#elif SANITIZER_SOLARIS
521  (void)pid;
522  return thr_kill(tid, sig);
523#endif
524}
525#endif
526
527#if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
528u64 NanoTime() {
529#if SANITIZER_FREEBSD || SANITIZER_OPENBSD
530  timeval tv;
531#else
532  kernel_timeval tv;
533#endif
534  internal_memset(&tv, 0, sizeof(tv));
535  internal_syscall(SYSCALL(gettimeofday), &tv, 0);
536  return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
537}
538
539uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
540  return internal_syscall(SYSCALL(clock_gettime), clk_id, tp);
541}
542#endif  // !SANITIZER_SOLARIS && !SANITIZER_NETBSD
543
544// Like getenv, but reads env directly from /proc (on Linux) or parses the
545// 'environ' array (on some others) and does not use libc. This function
546// should be called first inside __asan_init.
547const char *GetEnv(const char *name) {
548#if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD || \
549    SANITIZER_SOLARIS
550  if (::environ != 0) {
551    uptr NameLen = internal_strlen(name);
552    for (char **Env = ::environ; *Env != 0; Env++) {
553      if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
554        return (*Env) + NameLen + 1;
555    }
556  }
557  return 0;  // Not found.
558#elif SANITIZER_LINUX
559  static char *environ;
560  static uptr len;
561  static bool inited;
562  if (!inited) {
563    inited = true;
564    uptr environ_size;
565    if (!ReadFileToBuffer("/proc/self/environ", &environ, &environ_size, &len))
566      environ = nullptr;
567  }
568  if (!environ || len == 0) return nullptr;
569  uptr namelen = internal_strlen(name);
570  const char *p = environ;
571  while (*p != '\0') {  // will happen at the \0\0 that terminates the buffer
572    // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
573    const char* endp =
574        (char*)internal_memchr(p, '\0', len - (p - environ));
575    if (!endp)  // this entry isn't NUL terminated
576      return nullptr;
577    else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=')  // Match.
578      return p + namelen + 1;  // point after =
579    p = endp + 1;
580  }
581  return nullptr;  // Not found.
582#else
583#error "Unsupported platform"
584#endif
585}
586
587#if !SANITIZER_FREEBSD && !SANITIZER_NETBSD && !SANITIZER_OPENBSD
588extern "C" {
589SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
590}
591#endif
592
593#if !SANITIZER_GO && !SANITIZER_FREEBSD && !SANITIZER_NETBSD &&                \
594    !SANITIZER_OPENBSD
595static void ReadNullSepFileToArray(const char *path, char ***arr,
596                                   int arr_size) {
597  char *buff;
598  uptr buff_size;
599  uptr buff_len;
600  *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
601  if (!ReadFileToBuffer(path, &buff, &buff_size, &buff_len, 1024 * 1024)) {
602    (*arr)[0] = nullptr;
603    return;
604  }
605  (*arr)[0] = buff;
606  int count, i;
607  for (count = 1, i = 1; ; i++) {
608    if (buff[i] == 0) {
609      if (buff[i+1] == 0) break;
610      (*arr)[count] = &buff[i+1];
611      CHECK_LE(count, arr_size - 1);  // FIXME: make this more flexible.
612      count++;
613    }
614  }
615  (*arr)[count] = nullptr;
616}
617#endif
618
619#if !SANITIZER_OPENBSD
620static void GetArgsAndEnv(char ***argv, char ***envp) {
621#if SANITIZER_FREEBSD
622  // On FreeBSD, retrieving the argument and environment arrays is done via the
623  // kern.ps_strings sysctl, which returns a pointer to a structure containing
624  // this information. See also <sys/exec.h>.
625  ps_strings *pss;
626  uptr sz = sizeof(pss);
627  if (internal_sysctlbyname("kern.ps_strings", &pss, &sz, NULL, 0) == -1) {
628    Printf("sysctl kern.ps_strings failed\n");
629    Die();
630  }
631  *argv = pss->ps_argvstr;
632  *envp = pss->ps_envstr;
633#elif SANITIZER_NETBSD
634  *argv = __ps_strings->ps_argvstr;
635  *envp = __ps_strings->ps_envstr;
636#else // SANITIZER_FREEBSD
637#if !SANITIZER_GO
638  if (&__libc_stack_end) {
639#endif // !SANITIZER_GO
640    uptr* stack_end = (uptr*)__libc_stack_end;
641    int argc = *stack_end;
642    *argv = (char**)(stack_end + 1);
643    *envp = (char**)(stack_end + argc + 2);
644#if !SANITIZER_GO
645  } else {
646    static const int kMaxArgv = 2000, kMaxEnvp = 2000;
647    ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
648    ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
649  }
650#endif // !SANITIZER_GO
651#endif // SANITIZER_FREEBSD
652}
653
654char **GetArgv() {
655  char **argv, **envp;
656  GetArgsAndEnv(&argv, &envp);
657  return argv;
658}
659
660void ReExec() {
661  char **argv, **envp;
662  const char *pathname = "/proc/self/exe";
663
664#if SANITIZER_NETBSD
665  static const int name[] = {
666    CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
667  };
668  char path[400];
669  uptr len;
670
671  len = sizeof(path);
672  if (internal_sysctl(name, ARRAY_SIZE(name), path, &len, NULL, 0) != -1)
673    pathname = path;
674#elif SANITIZER_SOLARIS
675  pathname = getexecname();
676  CHECK_NE(pathname, NULL);
677#endif
678
679  GetArgsAndEnv(&argv, &envp);
680  uptr rv = internal_execve(pathname, argv, envp);
681  int rverrno;
682  CHECK_EQ(internal_iserror(rv, &rverrno), true);
683  Printf("execve failed, errno %d\n", rverrno);
684  Die();
685}
686#endif
687
688#if !SANITIZER_SOLARIS
689enum MutexState {
690  MtxUnlocked = 0,
691  MtxLocked = 1,
692  MtxSleeping = 2
693};
694
695BlockingMutex::BlockingMutex() {
696  internal_memset(this, 0, sizeof(*this));
697}
698
699void BlockingMutex::Lock() {
700  CHECK_EQ(owner_, 0);
701  atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
702  if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
703    return;
704  while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
705#if SANITIZER_FREEBSD
706    _umtx_op(m, UMTX_OP_WAIT_UINT, MtxSleeping, 0, 0);
707#elif SANITIZER_NETBSD
708    sched_yield(); /* No userspace futex-like synchronization */
709#else
710    internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAIT_PRIVATE, MtxSleeping,
711                     0, 0, 0);
712#endif
713  }
714}
715
716void BlockingMutex::Unlock() {
717  atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
718  u32 v = atomic_exchange(m, MtxUnlocked, memory_order_release);
719  CHECK_NE(v, MtxUnlocked);
720  if (v == MtxSleeping) {
721#if SANITIZER_FREEBSD
722    _umtx_op(m, UMTX_OP_WAKE, 1, 0, 0);
723#elif SANITIZER_NETBSD
724                   /* No userspace futex-like synchronization */
725#else
726    internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAKE_PRIVATE, 1, 0, 0, 0);
727#endif
728  }
729}
730
731void BlockingMutex::CheckLocked() {
732  atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
733  CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
734}
735#endif // !SANITIZER_SOLARIS
736
737// ----------------- sanitizer_linux.h
738// The actual size of this structure is specified by d_reclen.
739// Note that getdents64 uses a different structure format. We only provide the
740// 32-bit syscall here.
741#if SANITIZER_NETBSD
742// Not used
743#elif SANITIZER_OPENBSD
744// struct dirent is different for Linux and us. At this moment, we use only
745// d_fileno (Linux call this d_ino), d_reclen, and d_name.
746struct linux_dirent {
747  u64 d_ino;  // d_fileno
748  u16 d_reclen;
749  u16 d_namlen;  // not used
750  u8 d_type;     // not used
751  char d_name[NAME_MAX + 1];
752};
753#else
754struct linux_dirent {
755#if SANITIZER_X32 || defined(__aarch64__)
756  u64 d_ino;
757  u64 d_off;
758#else
759  unsigned long      d_ino;
760  unsigned long      d_off;
761#endif
762  unsigned short     d_reclen;
763#ifdef __aarch64__
764  unsigned char      d_type;
765#endif
766  char               d_name[256];
767};
768#endif
769
770#if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
771// Syscall wrappers.
772uptr internal_ptrace(int request, int pid, void *addr, void *data) {
773  return internal_syscall(SYSCALL(ptrace), request, pid, (uptr)addr,
774                          (uptr)data);
775}
776
777uptr internal_waitpid(int pid, int *status, int options) {
778  return internal_syscall(SYSCALL(wait4), pid, (uptr)status, options,
779                          0 /* rusage */);
780}
781
782uptr internal_getpid() {
783  return internal_syscall(SYSCALL(getpid));
784}
785
786uptr internal_getppid() {
787  return internal_syscall(SYSCALL(getppid));
788}
789
790int internal_dlinfo(void *handle, int request, void *p) {
791#if SANITIZER_FREEBSD || (SANITIZER_LINUX && !SANITIZER_ANDROID) || \
792    SANITIZER_SOLARIS
793  return dlinfo(handle, request, p);
794#else
795  UNIMPLEMENTED();
796#endif
797}
798
799uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
800#if SANITIZER_FREEBSD
801  return internal_syscall(SYSCALL(getdirentries), fd, (uptr)dirp, count, NULL);
802#elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
803  return internal_syscall(SYSCALL(getdents64), fd, (uptr)dirp, count);
804#else
805  return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count);
806#endif
807}
808
809uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
810  return internal_syscall(SYSCALL(lseek), fd, offset, whence);
811}
812
813#if SANITIZER_LINUX
814uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
815  return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5);
816}
817#endif
818
819uptr internal_sigaltstack(const void *ss, void *oss) {
820  return internal_syscall_ptr(SYSCALL(sigaltstack), (uptr)ss, (uptr)oss);
821}
822
823int internal_fork() {
824#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
825  return internal_syscall(SYSCALL(clone), SIGCHLD, 0);
826#else
827  return internal_syscall(SYSCALL(fork));
828#endif
829}
830
831#if SANITIZER_NETBSD
832#include <signal.h>
833extern "C" int __sigaction_siginfo(int, const struct sigaction *, struct sigaction *);
834int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
835
836    return __sigaction_siginfo(signum,
837	reinterpret_cast<const struct sigaction *>(act),
838	reinterpret_cast<struct sigaction *>(oldact));
839}
840#endif
841
842#if SANITIZER_FREEBSD || SANITIZER_OPENBSD
843int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
844                    uptr *oldlenp, const void *newp, uptr newlen) {
845#if SANITIZER_OPENBSD
846  return sysctl(name, namelen, oldp, (size_t *)oldlenp, (void *)newp,
847                (size_t)newlen);
848#else
849  return sysctl(name, namelen, oldp, (size_t *)oldlenp, newp, (size_t)newlen);
850#endif
851}
852
853#if SANITIZER_FREEBSD
854int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
855                          const void *newp, uptr newlen) {
856  return sysctlbyname(sname, oldp, (size_t *)oldlenp, newp, (size_t)newlen);
857}
858#endif
859#endif
860
861#if SANITIZER_LINUX
862#define SA_RESTORER 0x04000000
863// Doesn't set sa_restorer if the caller did not set it, so use with caution
864//(see below).
865int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
866  __sanitizer_kernel_sigaction_t k_act, k_oldact;
867  internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t));
868  internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t));
869  const __sanitizer_sigaction *u_act = (const __sanitizer_sigaction *)act;
870  __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact;
871  if (u_act) {
872    k_act.handler = u_act->handler;
873    k_act.sigaction = u_act->sigaction;
874    internal_memcpy(&k_act.sa_mask, &u_act->sa_mask,
875                    sizeof(__sanitizer_kernel_sigset_t));
876    // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL).
877    k_act.sa_flags = u_act->sa_flags | SA_RESTORER;
878    // FIXME: most often sa_restorer is unset, however the kernel requires it
879    // to point to a valid signal restorer that calls the rt_sigreturn syscall.
880    // If sa_restorer passed to the kernel is NULL, the program may crash upon
881    // signal delivery or fail to unwind the stack in the signal handler.
882    // libc implementation of sigaction() passes its own restorer to
883    // rt_sigaction, so we need to do the same (we'll need to reimplement the
884    // restorers; for x86_64 the restorer address can be obtained from
885    // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
886#if !SANITIZER_ANDROID || !SANITIZER_MIPS32
887    k_act.sa_restorer = u_act->sa_restorer;
888#endif
889  }
890
891  uptr result = internal_syscall(SYSCALL(rt_sigaction), (uptr)signum,
892      (uptr)(u_act ? &k_act : nullptr),
893      (uptr)(u_oldact ? &k_oldact : nullptr),
894      (uptr)sizeof(__sanitizer_kernel_sigset_t));
895
896  if ((result == 0) && u_oldact) {
897    u_oldact->handler = k_oldact.handler;
898    u_oldact->sigaction = k_oldact.sigaction;
899    internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask,
900                    sizeof(__sanitizer_kernel_sigset_t));
901    u_oldact->sa_flags = k_oldact.sa_flags;
902#if !SANITIZER_ANDROID || !SANITIZER_MIPS32
903    u_oldact->sa_restorer = k_oldact.sa_restorer;
904#endif
905  }
906  return result;
907}
908
909// Invokes sigaction via a raw syscall with a restorer, but does not support
910// all platforms yet.
911// We disable for Go simply because we have not yet added to buildgo.sh.
912#if (defined(__x86_64__) || SANITIZER_MIPS64) && !SANITIZER_GO
913int internal_sigaction_syscall(int signum, const void *act, void *oldact) {
914  if (act == nullptr)
915    return internal_sigaction_norestorer(signum, act, oldact);
916  __sanitizer_sigaction u_adjust;
917  internal_memcpy(&u_adjust, act, sizeof(u_adjust));
918#if !SANITIZER_ANDROID || !SANITIZER_MIPS32
919  if (u_adjust.sa_restorer == nullptr) {
920    u_adjust.sa_restorer = internal_sigreturn;
921  }
922#endif
923  return internal_sigaction_norestorer(signum, (const void *)&u_adjust, oldact);
924}
925#endif  // defined(__x86_64__) && !SANITIZER_GO
926#endif  // SANITIZER_LINUX
927
928uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
929                          __sanitizer_sigset_t *oldset) {
930#if SANITIZER_FREEBSD || SANITIZER_OPENBSD
931  return internal_syscall(SYSCALL(sigprocmask), how, set, oldset);
932#else
933  __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
934  __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset;
935  return internal_syscall(SYSCALL(rt_sigprocmask), (uptr)how,
936                          (uptr)&k_set->sig[0], (uptr)&k_oldset->sig[0],
937                          sizeof(__sanitizer_kernel_sigset_t));
938#endif
939}
940
941void internal_sigfillset(__sanitizer_sigset_t *set) {
942  internal_memset(set, 0xff, sizeof(*set));
943}
944
945void internal_sigemptyset(__sanitizer_sigset_t *set) {
946  internal_memset(set, 0, sizeof(*set));
947}
948
949#if SANITIZER_LINUX
950void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
951  signum -= 1;
952  CHECK_GE(signum, 0);
953  CHECK_LT(signum, sizeof(*set) * 8);
954  __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
955  const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
956  const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
957  k_set->sig[idx] &= ~(1 << bit);
958}
959
960bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
961  signum -= 1;
962  CHECK_GE(signum, 0);
963  CHECK_LT(signum, sizeof(*set) * 8);
964  __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
965  const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
966  const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
967  return k_set->sig[idx] & (1 << bit);
968}
969#elif SANITIZER_FREEBSD
970void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
971  sigset_t *rset = reinterpret_cast<sigset_t *>(set);
972  sigdelset(rset, signum);
973}
974
975bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
976  sigset_t *rset = reinterpret_cast<sigset_t *>(set);
977  return sigismember(rset, signum);
978}
979#endif
980#endif // !SANITIZER_SOLARIS
981
982#if !SANITIZER_NETBSD
983// ThreadLister implementation.
984ThreadLister::ThreadLister(pid_t pid) : pid_(pid), buffer_(4096) {
985  char task_directory_path[80];
986  internal_snprintf(task_directory_path, sizeof(task_directory_path),
987                    "/proc/%d/task/", pid);
988  descriptor_ = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY);
989  if (internal_iserror(descriptor_)) {
990    Report("Can't open /proc/%d/task for reading.\n", pid);
991  }
992}
993
994ThreadLister::Result ThreadLister::ListThreads(
995    InternalMmapVector<tid_t> *threads) {
996  if (internal_iserror(descriptor_))
997    return Error;
998  internal_lseek(descriptor_, 0, SEEK_SET);
999  threads->clear();
1000
1001  Result result = Ok;
1002  for (bool first_read = true;; first_read = false) {
1003    // Resize to max capacity if it was downsized by IsAlive.
1004    buffer_.resize(buffer_.capacity());
1005    CHECK_GE(buffer_.size(), 4096);
1006    uptr read = internal_getdents(
1007        descriptor_, (struct linux_dirent *)buffer_.data(), buffer_.size());
1008    if (!read)
1009      return result;
1010    if (internal_iserror(read)) {
1011      Report("Can't read directory entries from /proc/%d/task.\n", pid_);
1012      return Error;
1013    }
1014
1015    for (uptr begin = (uptr)buffer_.data(), end = begin + read; begin < end;) {
1016      struct linux_dirent *entry = (struct linux_dirent *)begin;
1017      begin += entry->d_reclen;
1018      if (entry->d_ino == 1) {
1019        // Inode 1 is for bad blocks and also can be a reason for early return.
1020        // Should be emitted if kernel tried to output terminating thread.
1021        // See proc_task_readdir implementation in Linux.
1022        result = Incomplete;
1023      }
1024      if (entry->d_ino && *entry->d_name >= '0' && *entry->d_name <= '9')
1025        threads->push_back(internal_atoll(entry->d_name));
1026    }
1027
1028    // Now we are going to detect short-read or early EOF. In such cases Linux
1029    // can return inconsistent list with missing alive threads.
1030    // Code will just remember that the list can be incomplete but it will
1031    // continue reads to return as much as possible.
1032    if (!first_read) {
1033      // The first one was a short-read by definition.
1034      result = Incomplete;
1035    } else if (read > buffer_.size() - 1024) {
1036      // Read was close to the buffer size. So double the size and assume the
1037      // worst.
1038      buffer_.resize(buffer_.size() * 2);
1039      result = Incomplete;
1040    } else if (!threads->empty() && !IsAlive(threads->back())) {
1041      // Maybe Linux early returned from read on terminated thread (!pid_alive)
1042      // and failed to restore read position.
1043      // See next_tid and proc_task_instantiate in Linux.
1044      result = Incomplete;
1045    }
1046  }
1047}
1048
1049bool ThreadLister::IsAlive(int tid) {
1050  // /proc/%d/task/%d/status uses same call to detect alive threads as
1051  // proc_task_readdir. See task_state implementation in Linux.
1052  char path[80];
1053  internal_snprintf(path, sizeof(path), "/proc/%d/task/%d/status", pid_, tid);
1054  if (!ReadFileToVector(path, &buffer_) || buffer_.empty())
1055    return false;
1056  buffer_.push_back(0);
1057  static const char kPrefix[] = "\nPPid:";
1058  const char *field = internal_strstr(buffer_.data(), kPrefix);
1059  if (!field)
1060    return false;
1061  field += internal_strlen(kPrefix);
1062  return (int)internal_atoll(field) != 0;
1063}
1064
1065ThreadLister::~ThreadLister() {
1066  if (!internal_iserror(descriptor_))
1067    internal_close(descriptor_);
1068}
1069#endif
1070
1071#if SANITIZER_WORDSIZE == 32
1072// Take care of unusable kernel area in top gigabyte.
1073static uptr GetKernelAreaSize() {
1074#if SANITIZER_LINUX && !SANITIZER_X32
1075  const uptr gbyte = 1UL << 30;
1076
1077  // Firstly check if there are writable segments
1078  // mapped to top gigabyte (e.g. stack).
1079  MemoryMappingLayout proc_maps(/*cache_enabled*/true);
1080  MemoryMappedSegment segment;
1081  while (proc_maps.Next(&segment)) {
1082    if ((segment.end >= 3 * gbyte) && segment.IsWritable()) return 0;
1083  }
1084
1085#if !SANITIZER_ANDROID
1086  // Even if nothing is mapped, top Gb may still be accessible
1087  // if we are running on 64-bit kernel.
1088  // Uname may report misleading results if personality type
1089  // is modified (e.g. under schroot) so check this as well.
1090  struct utsname uname_info;
1091  int pers = personality(0xffffffffUL);
1092  if (!(pers & PER_MASK)
1093      && uname(&uname_info) == 0
1094      && internal_strstr(uname_info.machine, "64"))
1095    return 0;
1096#endif  // SANITIZER_ANDROID
1097
1098  // Top gigabyte is reserved for kernel.
1099  return gbyte;
1100#else
1101  return 0;
1102#endif  // SANITIZER_LINUX && !SANITIZER_X32
1103}
1104#endif  // SANITIZER_WORDSIZE == 32
1105
1106uptr GetMaxVirtualAddress() {
1107#if (SANITIZER_NETBSD || SANITIZER_OPENBSD) && defined(__x86_64__)
1108  return 0x7f7ffffff000ULL;  // (0x00007f8000000000 - PAGE_SIZE)
1109#elif SANITIZER_WORDSIZE == 64
1110# if defined(__powerpc64__) || defined(__aarch64__)
1111  // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
1112  // We somehow need to figure out which one we are using now and choose
1113  // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
1114  // Note that with 'ulimit -s unlimited' the stack is moved away from the top
1115  // of the address space, so simply checking the stack address is not enough.
1116  // This should (does) work for both PowerPC64 Endian modes.
1117  // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
1118  return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
1119# elif defined(__mips64)
1120  return (1ULL << 40) - 1;  // 0x000000ffffffffffUL;
1121# elif defined(__s390x__)
1122  return (1ULL << 53) - 1;  // 0x001fffffffffffffUL;
1123# elif defined(__sparc__)
1124  return ~(uptr)0;
1125# else
1126  return (1ULL << 47) - 1;  // 0x00007fffffffffffUL;
1127# endif
1128#else  // SANITIZER_WORDSIZE == 32
1129# if defined(__s390__)
1130  return (1ULL << 31) - 1;  // 0x7fffffff;
1131# else
1132  return (1ULL << 32) - 1;  // 0xffffffff;
1133# endif
1134#endif  // SANITIZER_WORDSIZE
1135}
1136
1137uptr GetMaxUserVirtualAddress() {
1138  uptr addr = GetMaxVirtualAddress();
1139#if SANITIZER_WORDSIZE == 32 && !defined(__s390__)
1140  if (!common_flags()->full_address_space)
1141    addr -= GetKernelAreaSize();
1142  CHECK_LT(reinterpret_cast<uptr>(&addr), addr);
1143#endif
1144  return addr;
1145}
1146
1147uptr GetPageSize() {
1148// Android post-M sysconf(_SC_PAGESIZE) crashes if called from .preinit_array.
1149#if SANITIZER_ANDROID
1150  return 4096;
1151#elif SANITIZER_FREEBSD || SANITIZER_NETBSD
1152// Use sysctl as sysconf can trigger interceptors internally.
1153  int pz = 0;
1154  uptr pzl = sizeof(pz);
1155  int mib[2] = {CTL_HW, HW_PAGESIZE};
1156  int rv = internal_sysctl(mib, 2, &pz, &pzl, nullptr, 0);
1157  CHECK_EQ(rv, 0);
1158  return (uptr)pz;
1159#elif SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__))
1160  return EXEC_PAGESIZE;
1161#elif SANITIZER_USE_GETAUXVAL
1162  return getauxval(AT_PAGESZ);
1163#else
1164  return sysconf(_SC_PAGESIZE);  // EXEC_PAGESIZE may not be trustworthy.
1165#endif
1166}
1167
1168#if !SANITIZER_OPENBSD
1169uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
1170#if SANITIZER_SOLARIS
1171  const char *default_module_name = getexecname();
1172  CHECK_NE(default_module_name, NULL);
1173  return internal_snprintf(buf, buf_len, "%s", default_module_name);
1174#else
1175#if SANITIZER_FREEBSD || SANITIZER_NETBSD
1176#if SANITIZER_FREEBSD
1177  const int Mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
1178#else
1179  const int Mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
1180#endif
1181  const char *default_module_name = "kern.proc.pathname";
1182  uptr Size = buf_len;
1183  bool IsErr =
1184      (internal_sysctl(Mib, ARRAY_SIZE(Mib), buf, &Size, NULL, 0) != 0);
1185  int readlink_error = IsErr ? errno : 0;
1186  uptr module_name_len = Size;
1187#else
1188  const char *default_module_name = "/proc/self/exe";
1189  uptr module_name_len = internal_readlink(
1190      default_module_name, buf, buf_len);
1191  int readlink_error;
1192  bool IsErr = internal_iserror(module_name_len, &readlink_error);
1193#endif  // SANITIZER_SOLARIS
1194  if (IsErr) {
1195    // We can't read binary name for some reason, assume it's unknown.
1196    Report("WARNING: reading executable name failed with errno %d, "
1197           "some stack frames may not be symbolized\n", readlink_error);
1198    module_name_len = internal_snprintf(buf, buf_len, "%s",
1199                                        default_module_name);
1200    CHECK_LT(module_name_len, buf_len);
1201  }
1202  return module_name_len;
1203#endif
1204}
1205#endif // !SANITIZER_OPENBSD
1206
1207uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
1208#if SANITIZER_LINUX
1209  char *tmpbuf;
1210  uptr tmpsize;
1211  uptr tmplen;
1212  if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf, &tmpsize, &tmplen,
1213                       1024 * 1024)) {
1214    internal_strncpy(buf, tmpbuf, buf_len);
1215    UnmapOrDie(tmpbuf, tmpsize);
1216    return internal_strlen(buf);
1217  }
1218#endif
1219  return ReadBinaryName(buf, buf_len);
1220}
1221
1222// Match full names of the form /path/to/base_name{-,.}*
1223bool LibraryNameIs(const char *full_name, const char *base_name) {
1224  const char *name = full_name;
1225  // Strip path.
1226  while (*name != '\0') name++;
1227  while (name > full_name && *name != '/') name--;
1228  if (*name == '/') name++;
1229  uptr base_name_length = internal_strlen(base_name);
1230  if (internal_strncmp(name, base_name, base_name_length)) return false;
1231  return (name[base_name_length] == '-' || name[base_name_length] == '.');
1232}
1233
1234#if !SANITIZER_ANDROID
1235// Call cb for each region mapped by map.
1236void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) {
1237  CHECK_NE(map, nullptr);
1238#if !SANITIZER_FREEBSD && !SANITIZER_OPENBSD
1239  typedef ElfW(Phdr) Elf_Phdr;
1240  typedef ElfW(Ehdr) Elf_Ehdr;
1241#endif // !SANITIZER_FREEBSD && !SANITIZER_OPENBSD
1242  char *base = (char *)map->l_addr;
1243  Elf_Ehdr *ehdr = (Elf_Ehdr *)base;
1244  char *phdrs = base + ehdr->e_phoff;
1245  char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize;
1246
1247  // Find the segment with the minimum base so we can "relocate" the p_vaddr
1248  // fields.  Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
1249  // objects have a non-zero base.
1250  uptr preferred_base = (uptr)-1;
1251  for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
1252    Elf_Phdr *phdr = (Elf_Phdr *)iter;
1253    if (phdr->p_type == PT_LOAD && preferred_base > (uptr)phdr->p_vaddr)
1254      preferred_base = (uptr)phdr->p_vaddr;
1255  }
1256
1257  // Compute the delta from the real base to get a relocation delta.
1258  sptr delta = (uptr)base - preferred_base;
1259  // Now we can figure out what the loader really mapped.
1260  for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
1261    Elf_Phdr *phdr = (Elf_Phdr *)iter;
1262    if (phdr->p_type == PT_LOAD) {
1263      uptr seg_start = phdr->p_vaddr + delta;
1264      uptr seg_end = seg_start + phdr->p_memsz;
1265      // None of these values are aligned.  We consider the ragged edges of the
1266      // load command as defined, since they are mapped from the file.
1267      seg_start = RoundDownTo(seg_start, GetPageSizeCached());
1268      seg_end = RoundUpTo(seg_end, GetPageSizeCached());
1269      cb((void *)seg_start, seg_end - seg_start);
1270    }
1271  }
1272}
1273#endif
1274
1275#if defined(__x86_64__) && SANITIZER_LINUX
1276// We cannot use glibc's clone wrapper, because it messes with the child
1277// task's TLS. It writes the PID and TID of the child task to its thread
1278// descriptor, but in our case the child task shares the thread descriptor with
1279// the parent (because we don't know how to allocate a new thread
1280// descriptor to keep glibc happy). So the stock version of clone(), when
1281// used with CLONE_VM, would end up corrupting the parent's thread descriptor.
1282uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1283                    int *parent_tidptr, void *newtls, int *child_tidptr) {
1284  long long res;
1285  if (!fn || !child_stack)
1286    return -EINVAL;
1287  CHECK_EQ(0, (uptr)child_stack % 16);
1288  child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1289  ((unsigned long long *)child_stack)[0] = (uptr)fn;
1290  ((unsigned long long *)child_stack)[1] = (uptr)arg;
1291  register void *r8 __asm__("r8") = newtls;
1292  register int *r10 __asm__("r10") = child_tidptr;
1293  __asm__ __volatile__(
1294                       /* %rax = syscall(%rax = SYSCALL(clone),
1295                        *                %rdi = flags,
1296                        *                %rsi = child_stack,
1297                        *                %rdx = parent_tidptr,
1298                        *                %r8  = new_tls,
1299                        *                %r10 = child_tidptr)
1300                        */
1301                       "syscall\n"
1302
1303                       /* if (%rax != 0)
1304                        *   return;
1305                        */
1306                       "testq  %%rax,%%rax\n"
1307                       "jnz    1f\n"
1308
1309                       /* In the child. Terminate unwind chain. */
1310                       // XXX: We should also terminate the CFI unwind chain
1311                       // here. Unfortunately clang 3.2 doesn't support the
1312                       // necessary CFI directives, so we skip that part.
1313                       "xorq   %%rbp,%%rbp\n"
1314
1315                       /* Call "fn(arg)". */
1316                       "popq   %%rax\n"
1317                       "popq   %%rdi\n"
1318                       "call   *%%rax\n"
1319
1320                       /* Call _exit(%rax). */
1321                       "movq   %%rax,%%rdi\n"
1322                       "movq   %2,%%rax\n"
1323                       "syscall\n"
1324
1325                       /* Return to parent. */
1326                     "1:\n"
1327                       : "=a" (res)
1328                       : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)),
1329                         "S"(child_stack),
1330                         "D"(flags),
1331                         "d"(parent_tidptr),
1332                         "r"(r8),
1333                         "r"(r10)
1334                       : "memory", "r11", "rcx");
1335  return res;
1336}
1337#elif defined(__mips__) && SANITIZER_LINUX
1338uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1339                    int *parent_tidptr, void *newtls, int *child_tidptr) {
1340  long long res;
1341  if (!fn || !child_stack)
1342    return -EINVAL;
1343  CHECK_EQ(0, (uptr)child_stack % 16);
1344  child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1345  ((unsigned long long *)child_stack)[0] = (uptr)fn;
1346  ((unsigned long long *)child_stack)[1] = (uptr)arg;
1347  register void *a3 __asm__("$7") = newtls;
1348  register int *a4 __asm__("$8") = child_tidptr;
1349  // We don't have proper CFI directives here because it requires alot of code
1350  // for very marginal benefits.
1351  __asm__ __volatile__(
1352                       /* $v0 = syscall($v0 = __NR_clone,
1353                        * $a0 = flags,
1354                        * $a1 = child_stack,
1355                        * $a2 = parent_tidptr,
1356                        * $a3 = new_tls,
1357                        * $a4 = child_tidptr)
1358                        */
1359                       ".cprestore 16;\n"
1360                       "move $4,%1;\n"
1361                       "move $5,%2;\n"
1362                       "move $6,%3;\n"
1363                       "move $7,%4;\n"
1364                       /* Store the fifth argument on stack
1365                        * if we are using 32-bit abi.
1366                        */
1367#if SANITIZER_WORDSIZE == 32
1368                       "lw %5,16($29);\n"
1369#else
1370                       "move $8,%5;\n"
1371#endif
1372                       "li $2,%6;\n"
1373                       "syscall;\n"
1374
1375                       /* if ($v0 != 0)
1376                        * return;
1377                        */
1378                       "bnez $2,1f;\n"
1379
1380                       /* Call "fn(arg)". */
1381#if SANITIZER_WORDSIZE == 32
1382#ifdef __BIG_ENDIAN__
1383                       "lw $25,4($29);\n"
1384                       "lw $4,12($29);\n"
1385#else
1386                       "lw $25,0($29);\n"
1387                       "lw $4,8($29);\n"
1388#endif
1389#else
1390                       "ld $25,0($29);\n"
1391                       "ld $4,8($29);\n"
1392#endif
1393                       "jal $25;\n"
1394
1395                       /* Call _exit($v0). */
1396                       "move $4,$2;\n"
1397                       "li $2,%7;\n"
1398                       "syscall;\n"
1399
1400                       /* Return to parent. */
1401                     "1:\n"
1402                       : "=r" (res)
1403                       : "r"(flags),
1404                         "r"(child_stack),
1405                         "r"(parent_tidptr),
1406                         "r"(a3),
1407                         "r"(a4),
1408                         "i"(__NR_clone),
1409                         "i"(__NR_exit)
1410                       : "memory", "$29" );
1411  return res;
1412}
1413#elif defined(__aarch64__) && SANITIZER_LINUX
1414uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1415                    int *parent_tidptr, void *newtls, int *child_tidptr) {
1416  long long res;
1417  if (!fn || !child_stack)
1418    return -EINVAL;
1419  CHECK_EQ(0, (uptr)child_stack % 16);
1420  child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1421  ((unsigned long long *)child_stack)[0] = (uptr)fn;
1422  ((unsigned long long *)child_stack)[1] = (uptr)arg;
1423
1424  register int (*__fn)(void *)  __asm__("x0") = fn;
1425  register void *__stack __asm__("x1") = child_stack;
1426  register int   __flags __asm__("x2") = flags;
1427  register void *__arg   __asm__("x3") = arg;
1428  register int  *__ptid  __asm__("x4") = parent_tidptr;
1429  register void *__tls   __asm__("x5") = newtls;
1430  register int  *__ctid  __asm__("x6") = child_tidptr;
1431
1432  __asm__ __volatile__(
1433                       "mov x0,x2\n" /* flags  */
1434                       "mov x2,x4\n" /* ptid  */
1435                       "mov x3,x5\n" /* tls  */
1436                       "mov x4,x6\n" /* ctid  */
1437                       "mov x8,%9\n" /* clone  */
1438
1439                       "svc 0x0\n"
1440
1441                       /* if (%r0 != 0)
1442                        *   return %r0;
1443                        */
1444                       "cmp x0, #0\n"
1445                       "bne 1f\n"
1446
1447                       /* In the child, now. Call "fn(arg)". */
1448                       "ldp x1, x0, [sp], #16\n"
1449                       "blr x1\n"
1450
1451                       /* Call _exit(%r0).  */
1452                       "mov x8, %10\n"
1453                       "svc 0x0\n"
1454                     "1:\n"
1455
1456                       : "=r" (res)
1457                       : "i"(-EINVAL),
1458                         "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg),
1459                         "r"(__ptid), "r"(__tls), "r"(__ctid),
1460                         "i"(__NR_clone), "i"(__NR_exit)
1461                       : "x30", "memory");
1462  return res;
1463}
1464#elif defined(__powerpc64__) && SANITIZER_LINUX
1465uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1466                   int *parent_tidptr, void *newtls, int *child_tidptr) {
1467  long long res;
1468// Stack frame structure.
1469#if SANITIZER_PPC64V1
1470//   Back chain == 0        (SP + 112)
1471// Frame (112 bytes):
1472//   Parameter save area    (SP + 48), 8 doublewords
1473//   TOC save area          (SP + 40)
1474//   Link editor doubleword (SP + 32)
1475//   Compiler doubleword    (SP + 24)
1476//   LR save area           (SP + 16)
1477//   CR save area           (SP + 8)
1478//   Back chain             (SP + 0)
1479# define FRAME_SIZE 112
1480# define FRAME_TOC_SAVE_OFFSET 40
1481#elif SANITIZER_PPC64V2
1482//   Back chain == 0        (SP + 32)
1483// Frame (32 bytes):
1484//   TOC save area          (SP + 24)
1485//   LR save area           (SP + 16)
1486//   CR save area           (SP + 8)
1487//   Back chain             (SP + 0)
1488# define FRAME_SIZE 32
1489# define FRAME_TOC_SAVE_OFFSET 24
1490#else
1491# error "Unsupported PPC64 ABI"
1492#endif
1493  if (!fn || !child_stack)
1494    return -EINVAL;
1495  CHECK_EQ(0, (uptr)child_stack % 16);
1496
1497  register int (*__fn)(void *) __asm__("r3") = fn;
1498  register void *__cstack      __asm__("r4") = child_stack;
1499  register int __flags         __asm__("r5") = flags;
1500  register void *__arg         __asm__("r6") = arg;
1501  register int *__ptidptr      __asm__("r7") = parent_tidptr;
1502  register void *__newtls      __asm__("r8") = newtls;
1503  register int *__ctidptr      __asm__("r9") = child_tidptr;
1504
1505 __asm__ __volatile__(
1506           /* fn and arg are saved across the syscall */
1507           "mr 28, %5\n\t"
1508           "mr 27, %8\n\t"
1509
1510           /* syscall
1511             r0 == __NR_clone
1512             r3 == flags
1513             r4 == child_stack
1514             r5 == parent_tidptr
1515             r6 == newtls
1516             r7 == child_tidptr */
1517           "mr 3, %7\n\t"
1518           "mr 5, %9\n\t"
1519           "mr 6, %10\n\t"
1520           "mr 7, %11\n\t"
1521           "li 0, %3\n\t"
1522           "sc\n\t"
1523
1524           /* Test if syscall was successful */
1525           "cmpdi  cr1, 3, 0\n\t"
1526           "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1527           "bne-   cr1, 1f\n\t"
1528
1529           /* Set up stack frame */
1530           "li    29, 0\n\t"
1531           "stdu  29, -8(1)\n\t"
1532           "stdu  1, -%12(1)\n\t"
1533           /* Do the function call */
1534           "std   2, %13(1)\n\t"
1535#if SANITIZER_PPC64V1
1536           "ld    0, 0(28)\n\t"
1537           "ld    2, 8(28)\n\t"
1538           "mtctr 0\n\t"
1539#elif SANITIZER_PPC64V2
1540           "mr    12, 28\n\t"
1541           "mtctr 12\n\t"
1542#else
1543# error "Unsupported PPC64 ABI"
1544#endif
1545           "mr    3, 27\n\t"
1546           "bctrl\n\t"
1547           "ld    2, %13(1)\n\t"
1548
1549           /* Call _exit(r3) */
1550           "li 0, %4\n\t"
1551           "sc\n\t"
1552
1553           /* Return to parent */
1554           "1:\n\t"
1555           "mr %0, 3\n\t"
1556             : "=r" (res)
1557             : "0" (-1),
1558               "i" (EINVAL),
1559               "i" (__NR_clone),
1560               "i" (__NR_exit),
1561               "r" (__fn),
1562               "r" (__cstack),
1563               "r" (__flags),
1564               "r" (__arg),
1565               "r" (__ptidptr),
1566               "r" (__newtls),
1567               "r" (__ctidptr),
1568               "i" (FRAME_SIZE),
1569               "i" (FRAME_TOC_SAVE_OFFSET)
1570             : "cr0", "cr1", "memory", "ctr", "r0", "r27", "r28", "r29");
1571  return res;
1572}
1573#elif defined(__i386__) && SANITIZER_LINUX
1574uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1575                    int *parent_tidptr, void *newtls, int *child_tidptr) {
1576  int res;
1577  if (!fn || !child_stack)
1578    return -EINVAL;
1579  CHECK_EQ(0, (uptr)child_stack % 16);
1580  child_stack = (char *)child_stack - 7 * sizeof(unsigned int);
1581  ((unsigned int *)child_stack)[0] = (uptr)flags;
1582  ((unsigned int *)child_stack)[1] = (uptr)0;
1583  ((unsigned int *)child_stack)[2] = (uptr)fn;
1584  ((unsigned int *)child_stack)[3] = (uptr)arg;
1585  __asm__ __volatile__(
1586                       /* %eax = syscall(%eax = SYSCALL(clone),
1587                        *                %ebx = flags,
1588                        *                %ecx = child_stack,
1589                        *                %edx = parent_tidptr,
1590                        *                %esi  = new_tls,
1591                        *                %edi = child_tidptr)
1592                        */
1593
1594                        /* Obtain flags */
1595                        "movl    (%%ecx), %%ebx\n"
1596                        /* Do the system call */
1597                        "pushl   %%ebx\n"
1598                        "pushl   %%esi\n"
1599                        "pushl   %%edi\n"
1600                        /* Remember the flag value.  */
1601                        "movl    %%ebx, (%%ecx)\n"
1602                        "int     $0x80\n"
1603                        "popl    %%edi\n"
1604                        "popl    %%esi\n"
1605                        "popl    %%ebx\n"
1606
1607                        /* if (%eax != 0)
1608                         *   return;
1609                         */
1610
1611                        "test    %%eax,%%eax\n"
1612                        "jnz    1f\n"
1613
1614                        /* terminate the stack frame */
1615                        "xorl   %%ebp,%%ebp\n"
1616                        /* Call FN. */
1617                        "call    *%%ebx\n"
1618#ifdef PIC
1619                        "call    here\n"
1620                        "here:\n"
1621                        "popl    %%ebx\n"
1622                        "addl    $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n"
1623#endif
1624                        /* Call exit */
1625                        "movl    %%eax, %%ebx\n"
1626                        "movl    %2, %%eax\n"
1627                        "int     $0x80\n"
1628                        "1:\n"
1629                       : "=a" (res)
1630                       : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)),
1631                         "c"(child_stack),
1632                         "d"(parent_tidptr),
1633                         "S"(newtls),
1634                         "D"(child_tidptr)
1635                       : "memory");
1636  return res;
1637}
1638#elif defined(__arm__) && SANITIZER_LINUX
1639uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1640                    int *parent_tidptr, void *newtls, int *child_tidptr) {
1641  unsigned int res;
1642  if (!fn || !child_stack)
1643    return -EINVAL;
1644  child_stack = (char *)child_stack - 2 * sizeof(unsigned int);
1645  ((unsigned int *)child_stack)[0] = (uptr)fn;
1646  ((unsigned int *)child_stack)[1] = (uptr)arg;
1647  register int r0 __asm__("r0") = flags;
1648  register void *r1 __asm__("r1") = child_stack;
1649  register int *r2 __asm__("r2") = parent_tidptr;
1650  register void *r3 __asm__("r3") = newtls;
1651  register int *r4 __asm__("r4") = child_tidptr;
1652  register int r7 __asm__("r7") = __NR_clone;
1653
1654#if __ARM_ARCH > 4 || defined (__ARM_ARCH_4T__)
1655# define ARCH_HAS_BX
1656#endif
1657#if __ARM_ARCH > 4
1658# define ARCH_HAS_BLX
1659#endif
1660
1661#ifdef ARCH_HAS_BX
1662# ifdef ARCH_HAS_BLX
1663#  define BLX(R) "blx "  #R "\n"
1664# else
1665#  define BLX(R) "mov lr, pc; bx " #R "\n"
1666# endif
1667#else
1668# define BLX(R)  "mov lr, pc; mov pc," #R "\n"
1669#endif
1670
1671  __asm__ __volatile__(
1672                       /* %r0 = syscall(%r7 = SYSCALL(clone),
1673                        *               %r0 = flags,
1674                        *               %r1 = child_stack,
1675                        *               %r2 = parent_tidptr,
1676                        *               %r3  = new_tls,
1677                        *               %r4 = child_tidptr)
1678                        */
1679
1680                       /* Do the system call */
1681                       "swi 0x0\n"
1682
1683                       /* if (%r0 != 0)
1684                        *   return %r0;
1685                        */
1686                       "cmp r0, #0\n"
1687                       "bne 1f\n"
1688
1689                       /* In the child, now. Call "fn(arg)". */
1690                       "ldr r0, [sp, #4]\n"
1691                       "ldr ip, [sp], #8\n"
1692                       BLX(ip)
1693                       /* Call _exit(%r0). */
1694                       "mov r7, %7\n"
1695                       "swi 0x0\n"
1696                       "1:\n"
1697                       "mov %0, r0\n"
1698                       : "=r"(res)
1699                       : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r7),
1700                         "i"(__NR_exit)
1701                       : "memory");
1702  return res;
1703}
1704#endif  // defined(__x86_64__) && SANITIZER_LINUX
1705
1706#if SANITIZER_ANDROID
1707#if __ANDROID_API__ < 21
1708extern "C" __attribute__((weak)) int dl_iterate_phdr(
1709    int (*)(struct dl_phdr_info *, size_t, void *), void *);
1710#endif
1711
1712static int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size,
1713                                   void *data) {
1714  // Any name starting with "lib" indicates a bug in L where library base names
1715  // are returned instead of paths.
1716  if (info->dlpi_name && info->dlpi_name[0] == 'l' &&
1717      info->dlpi_name[1] == 'i' && info->dlpi_name[2] == 'b') {
1718    *(bool *)data = true;
1719    return 1;
1720  }
1721  return 0;
1722}
1723
1724static atomic_uint32_t android_api_level;
1725
1726static AndroidApiLevel AndroidDetectApiLevelStatic() {
1727#if __ANDROID_API__ <= 19
1728  return ANDROID_KITKAT;
1729#elif __ANDROID_API__ <= 22
1730  return ANDROID_LOLLIPOP_MR1;
1731#else
1732  return ANDROID_POST_LOLLIPOP;
1733#endif
1734}
1735
1736static AndroidApiLevel AndroidDetectApiLevel() {
1737  if (!&dl_iterate_phdr)
1738    return ANDROID_KITKAT; // K or lower
1739  bool base_name_seen = false;
1740  dl_iterate_phdr(dl_iterate_phdr_test_cb, &base_name_seen);
1741  if (base_name_seen)
1742    return ANDROID_LOLLIPOP_MR1; // L MR1
1743  return ANDROID_POST_LOLLIPOP;   // post-L
1744  // Plain L (API level 21) is completely broken wrt ASan and not very
1745  // interesting to detect.
1746}
1747
1748extern "C" __attribute__((weak)) void* _DYNAMIC;
1749
1750AndroidApiLevel AndroidGetApiLevel() {
1751  AndroidApiLevel level =
1752      (AndroidApiLevel)atomic_load(&android_api_level, memory_order_relaxed);
1753  if (level) return level;
1754  level = &_DYNAMIC == nullptr ? AndroidDetectApiLevelStatic()
1755                               : AndroidDetectApiLevel();
1756  atomic_store(&android_api_level, level, memory_order_relaxed);
1757  return level;
1758}
1759
1760#endif
1761
1762static HandleSignalMode GetHandleSignalModeImpl(int signum) {
1763  switch (signum) {
1764    case SIGABRT:
1765      return common_flags()->handle_abort;
1766    case SIGILL:
1767      return common_flags()->handle_sigill;
1768    case SIGTRAP:
1769      return common_flags()->handle_sigtrap;
1770    case SIGFPE:
1771      return common_flags()->handle_sigfpe;
1772    case SIGSEGV:
1773      return common_flags()->handle_segv;
1774    case SIGBUS:
1775      return common_flags()->handle_sigbus;
1776  }
1777  return kHandleSignalNo;
1778}
1779
1780HandleSignalMode GetHandleSignalMode(int signum) {
1781  HandleSignalMode result = GetHandleSignalModeImpl(signum);
1782  if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler)
1783    return kHandleSignalExclusive;
1784  return result;
1785}
1786
1787#if !SANITIZER_GO
1788void *internal_start_thread(void(*func)(void *arg), void *arg) {
1789  // Start the thread with signals blocked, otherwise it can steal user signals.
1790  __sanitizer_sigset_t set, old;
1791  internal_sigfillset(&set);
1792#if SANITIZER_LINUX && !SANITIZER_ANDROID
1793  // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
1794  // on any thread, setuid call hangs (see test/tsan/setuid.c).
1795  internal_sigdelset(&set, 33);
1796#endif
1797  internal_sigprocmask(SIG_SETMASK, &set, &old);
1798  void *th;
1799  real_pthread_create(&th, nullptr, (void*(*)(void *arg))func, arg);
1800  internal_sigprocmask(SIG_SETMASK, &old, nullptr);
1801  return th;
1802}
1803
1804void internal_join_thread(void *th) {
1805  real_pthread_join(th, nullptr);
1806}
1807#else
1808void *internal_start_thread(void (*func)(void *), void *arg) { return 0; }
1809
1810void internal_join_thread(void *th) {}
1811#endif
1812
1813#if defined(__aarch64__)
1814#if SANITIZER_LINUX
1815// Android headers in the older NDK releases miss this definition.
1816struct __sanitizer_esr_context {
1817  struct _aarch64_ctx head;
1818  uint64_t esr;
1819};
1820
1821static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) {
1822  static const u32 kEsrMagic = 0x45535201;
1823  u8 *aux = ucontext->uc_mcontext.__reserved;
1824  while (true) {
1825    _aarch64_ctx *ctx = (_aarch64_ctx *)aux;
1826    if (ctx->size == 0) break;
1827    if (ctx->magic == kEsrMagic) {
1828      *esr = ((__sanitizer_esr_context *)ctx)->esr;
1829      return true;
1830    }
1831    aux += ctx->size;
1832  }
1833  return false;
1834}
1835#else
1836static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) {
1837  return false;
1838}
1839#endif
1840#endif
1841
1842#if SANITIZER_OPENBSD
1843using Context = sigcontext;
1844#else
1845using Context = ucontext_t;
1846#endif
1847
1848SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
1849  Context *ucontext = (Context *)context;
1850#if defined(__x86_64__) || defined(__i386__)
1851  static const uptr PF_WRITE = 1U << 1;
1852#if SANITIZER_FREEBSD
1853  uptr err = ucontext->uc_mcontext.mc_err;
1854#elif SANITIZER_NETBSD
1855  uptr err = ucontext->uc_mcontext.__gregs[_REG_ERR];
1856#elif SANITIZER_OPENBSD
1857  uptr err = ucontext->sc_err;
1858#elif SANITIZER_SOLARIS && defined(__i386__)
1859  const int Err = 13;
1860  uptr err = ucontext->uc_mcontext.gregs[Err];
1861#else
1862  uptr err = ucontext->uc_mcontext.gregs[REG_ERR];
1863#endif // SANITIZER_FREEBSD
1864  return err & PF_WRITE ? WRITE : READ;
1865#elif defined(__mips__)
1866  uint32_t *exception_source;
1867  uint32_t faulty_instruction;
1868  uint32_t op_code;
1869
1870#if SANITIZER_NETBSD
1871  ucontext_t *nucontext = (ucontext_t *)ucontext;
1872  exception_source = (uint32_t *)_UC_MACHINE_PC(nucontext);
1873#else
1874  exception_source = (uint32_t *)ucontext->uc_mcontext.pc;
1875#endif
1876  faulty_instruction = (uint32_t)(*exception_source);
1877
1878  op_code = (faulty_instruction >> 26) & 0x3f;
1879
1880  // FIXME: Add support for FPU, microMIPS, DSP, MSA memory instructions.
1881  switch (op_code) {
1882    case 0x28:  // sb
1883    case 0x29:  // sh
1884    case 0x2b:  // sw
1885    case 0x3f:  // sd
1886#if __mips_isa_rev < 6
1887    case 0x2c:  // sdl
1888    case 0x2d:  // sdr
1889    case 0x2a:  // swl
1890    case 0x2e:  // swr
1891#endif
1892      return SignalContext::WRITE;
1893
1894    case 0x20:  // lb
1895    case 0x24:  // lbu
1896    case 0x21:  // lh
1897    case 0x25:  // lhu
1898    case 0x23:  // lw
1899    case 0x27:  // lwu
1900    case 0x37:  // ld
1901#if __mips_isa_rev < 6
1902    case 0x1a:  // ldl
1903    case 0x1b:  // ldr
1904    case 0x22:  // lwl
1905    case 0x26:  // lwr
1906#endif
1907      return SignalContext::READ;
1908#if __mips_isa_rev == 6
1909    case 0x3b:  // pcrel
1910      op_code = (faulty_instruction >> 19) & 0x3;
1911      switch (op_code) {
1912        case 0x1:  // lwpc
1913        case 0x2:  // lwupc
1914          return SignalContext::READ;
1915      }
1916#endif
1917  }
1918  return SignalContext::UNKNOWN;
1919#elif defined(__arm__) && !SANITIZER_NETBSD
1920  static const uptr FSR_WRITE = 1U << 11;
1921  uptr fsr = ucontext->uc_mcontext.error_code;
1922  return fsr & FSR_WRITE ? WRITE : READ;
1923#elif defined(__aarch64__)
1924  static const u64 ESR_ELx_WNR = 1U << 6;
1925  u64 esr;
1926  if (!Aarch64GetESR(ucontext, &esr)) return UNKNOWN;
1927  return esr & ESR_ELx_WNR ? WRITE : READ;
1928#elif defined(__sparc__)
1929  // Decode the instruction to determine the access type.
1930  // From OpenSolaris $SRC/uts/sun4/os/trap.c (get_accesstype).
1931# if SANITIZER_SOLARIS
1932  uptr pc = ucontext->uc_mcontext.gregs[REG_PC];
1933# elif SANITIZER_NETBSD
1934  uptr pc = ucontext->uc_mcontext.__gregs[_REG_PC];
1935# else
1936  // Historical BSDism here.
1937  struct sigcontext *scontext = (struct sigcontext *)ucontext;
1938#  if defined(__arch64__)
1939  uptr pc = scontext->sigc_regs.tpc;
1940#  else
1941  uptr pc = scontext->si_regs.pc;
1942#  endif
1943# endif
1944  u32 instr = *(u32 *)pc;
1945  return (instr >> 21) & 1 ? WRITE: READ;
1946#else
1947  (void)ucontext;
1948  return UNKNOWN;  // FIXME: Implement.
1949#endif
1950}
1951
1952void SignalContext::DumpAllRegisters(void *context) {
1953  // FIXME: Implement this.
1954}
1955
1956static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
1957#if SANITIZER_NETBSD
1958  // This covers all NetBSD architectures
1959  ucontext_t *ucontext = (ucontext_t *)context;
1960  *pc = _UC_MACHINE_PC(ucontext);
1961  *bp = _UC_MACHINE_FP(ucontext);
1962  *sp = _UC_MACHINE_SP(ucontext);
1963#elif defined(__arm__)
1964  ucontext_t *ucontext = (ucontext_t*)context;
1965  *pc = ucontext->uc_mcontext.arm_pc;
1966  *bp = ucontext->uc_mcontext.arm_fp;
1967  *sp = ucontext->uc_mcontext.arm_sp;
1968#elif defined(__aarch64__)
1969  ucontext_t *ucontext = (ucontext_t*)context;
1970  *pc = ucontext->uc_mcontext.pc;
1971  *bp = ucontext->uc_mcontext.regs[29];
1972  *sp = ucontext->uc_mcontext.sp;
1973#elif defined(__hppa__)
1974  ucontext_t *ucontext = (ucontext_t*)context;
1975  *pc = ucontext->uc_mcontext.sc_iaoq[0];
1976  /* GCC uses %r3 whenever a frame pointer is needed.  */
1977  *bp = ucontext->uc_mcontext.sc_gr[3];
1978  *sp = ucontext->uc_mcontext.sc_gr[30];
1979#elif defined(__x86_64__)
1980# if SANITIZER_FREEBSD
1981  ucontext_t *ucontext = (ucontext_t*)context;
1982  *pc = ucontext->uc_mcontext.mc_rip;
1983  *bp = ucontext->uc_mcontext.mc_rbp;
1984  *sp = ucontext->uc_mcontext.mc_rsp;
1985#elif SANITIZER_OPENBSD
1986  sigcontext *ucontext = (sigcontext *)context;
1987  *pc = ucontext->sc_rip;
1988  *bp = ucontext->sc_rbp;
1989  *sp = ucontext->sc_rsp;
1990# else
1991  ucontext_t *ucontext = (ucontext_t*)context;
1992  *pc = ucontext->uc_mcontext.gregs[REG_RIP];
1993  *bp = ucontext->uc_mcontext.gregs[REG_RBP];
1994  *sp = ucontext->uc_mcontext.gregs[REG_RSP];
1995# endif
1996#elif defined(__i386__)
1997# if SANITIZER_FREEBSD
1998  ucontext_t *ucontext = (ucontext_t*)context;
1999  *pc = ucontext->uc_mcontext.mc_eip;
2000  *bp = ucontext->uc_mcontext.mc_ebp;
2001  *sp = ucontext->uc_mcontext.mc_esp;
2002#elif SANITIZER_OPENBSD
2003  sigcontext *ucontext = (sigcontext *)context;
2004  *pc = ucontext->sc_eip;
2005  *bp = ucontext->sc_ebp;
2006  *sp = ucontext->sc_esp;
2007# else
2008  ucontext_t *ucontext = (ucontext_t*)context;
2009# if SANITIZER_SOLARIS
2010  /* Use the numeric values: the symbolic ones are undefined by llvm
2011     include/llvm/Support/Solaris.h.  */
2012# ifndef REG_EIP
2013#  define REG_EIP 14 // REG_PC
2014# endif
2015# ifndef REG_EBP
2016#  define REG_EBP  6 // REG_FP
2017# endif
2018# ifndef REG_ESP
2019#  define REG_ESP 17 // REG_SP
2020# endif
2021# endif
2022  *pc = ucontext->uc_mcontext.gregs[REG_EIP];
2023  *bp = ucontext->uc_mcontext.gregs[REG_EBP];
2024  *sp = ucontext->uc_mcontext.gregs[REG_ESP];
2025# endif
2026#elif defined(__powerpc__) || defined(__powerpc64__)
2027  ucontext_t *ucontext = (ucontext_t*)context;
2028  *pc = ucontext->uc_mcontext.regs->nip;
2029  *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
2030  // The powerpc{,64}-linux ABIs do not specify r31 as the frame
2031  // pointer, but GCC always uses r31 when we need a frame pointer.
2032  *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
2033#elif defined(__sparc__)
2034# if defined(__arch64__) || defined(__sparcv9)
2035#  define STACK_BIAS 2047
2036# else
2037#  define STACK_BIAS 0
2038# endif
2039# if SANITIZER_SOLARIS
2040  ucontext_t *ucontext = (ucontext_t*)context;
2041  *pc = ucontext->uc_mcontext.gregs[REG_PC];
2042  *sp = ucontext->uc_mcontext.gregs[REG_O6] + STACK_BIAS;
2043# elif SANITIZER_NETBSD
2044  *pc = ucontext->uc_mcontext.__gregs[_REG_PC];
2045  *sp = ucontext->uc_mcontext.__gregs[_REG_O6] + STACK_BIAS;
2046# else
2047  // Historical BSDism here.
2048  struct sigcontext *scontext = (struct sigcontext *)context;
2049#  if defined(__arch64__)
2050  *pc = scontext->sigc_regs.tpc;
2051  *sp = scontext->sigc_regs.u_regs[14] + STACK_BIAS;
2052#  else
2053  *pc = scontext->si_regs.pc;
2054  *sp = scontext->si_regs.u_regs[14];
2055#  endif
2056# endif
2057  *bp = (uptr) ((uhwptr *) *sp)[14] + STACK_BIAS;
2058#elif defined(__mips__)
2059  ucontext_t *ucontext = (ucontext_t*)context;
2060  *pc = ucontext->uc_mcontext.pc;
2061  *bp = ucontext->uc_mcontext.gregs[30];
2062  *sp = ucontext->uc_mcontext.gregs[29];
2063#elif defined(__s390__)
2064  ucontext_t *ucontext = (ucontext_t*)context;
2065# if defined(__s390x__)
2066  *pc = ucontext->uc_mcontext.psw.addr;
2067# else
2068  *pc = ucontext->uc_mcontext.psw.addr & 0x7fffffff;
2069# endif
2070  *bp = ucontext->uc_mcontext.gregs[11];
2071  *sp = ucontext->uc_mcontext.gregs[15];
2072#else
2073# error "Unsupported arch"
2074#endif
2075}
2076
2077void SignalContext::InitPcSpBp() { GetPcSpBp(context, &pc, &sp, &bp); }
2078
2079void MaybeReexec() {
2080  // No need to re-exec on Linux.
2081}
2082
2083void CheckASLR() {
2084#if SANITIZER_NETBSD
2085  int mib[3];
2086  int paxflags;
2087  uptr len = sizeof(paxflags);
2088
2089  mib[0] = CTL_PROC;
2090  mib[1] = internal_getpid();
2091  mib[2] = PROC_PID_PAXFLAGS;
2092
2093  if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
2094    Printf("sysctl failed\n");
2095    Die();
2096  }
2097
2098  if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_ASLR)) {
2099    Printf("This sanitizer is not compatible with enabled ASLR\n");
2100    Die();
2101  }
2102#elif SANITIZER_PPC64V2
2103  // Disable ASLR for Linux PPC64LE.
2104  int old_personality = personality(0xffffffff);
2105  if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) {
2106    VReport(1, "WARNING: Program is being run with address space layout "
2107               "randomization (ASLR) enabled which prevents the thread and "
2108               "memory sanitizers from working on powerpc64le.\n"
2109               "ASLR will be disabled and the program re-executed.\n");
2110    CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1);
2111    ReExec();
2112  }
2113#else
2114  // Do nothing
2115#endif
2116}
2117
2118void PrintModuleMap() { }
2119
2120void CheckNoDeepBind(const char *filename, int flag) {
2121#ifdef RTLD_DEEPBIND
2122  if (flag & RTLD_DEEPBIND) {
2123    Report(
2124        "You are trying to dlopen a %s shared library with RTLD_DEEPBIND flag"
2125        " which is incompatibe with sanitizer runtime "
2126        "(see https://github.com/google/sanitizers/issues/611 for details"
2127        "). If you want to run %s library under sanitizers please remove "
2128        "RTLD_DEEPBIND from dlopen flags.\n",
2129        filename, filename);
2130    Die();
2131  }
2132#endif
2133}
2134
2135uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
2136                              uptr *largest_gap_found,
2137                              uptr *max_occupied_addr) {
2138  UNREACHABLE("FindAvailableMemoryRange is not available");
2139  return 0;
2140}
2141
2142bool GetRandom(void *buffer, uptr length, bool blocking) {
2143  if (!buffer || !length || length > 256)
2144    return false;
2145#if SANITIZER_USE_GETENTROPY
2146  uptr rnd = getentropy(buffer, length);
2147  int rverrno = 0;
2148  if (internal_iserror(rnd, &rverrno) && rverrno == EFAULT)
2149    return false;
2150  else if (rnd == 0)
2151    return true;
2152#endif // SANITIZER_USE_GETENTROPY
2153
2154#if SANITIZER_USE_GETRANDOM
2155  static atomic_uint8_t skip_getrandom_syscall;
2156  if (!atomic_load_relaxed(&skip_getrandom_syscall)) {
2157    // Up to 256 bytes, getrandom will not be interrupted.
2158    uptr res = internal_syscall(SYSCALL(getrandom), buffer, length,
2159                                blocking ? 0 : GRND_NONBLOCK);
2160    int rverrno = 0;
2161    if (internal_iserror(res, &rverrno) && rverrno == ENOSYS)
2162      atomic_store_relaxed(&skip_getrandom_syscall, 1);
2163    else if (res == length)
2164      return true;
2165  }
2166#endif // SANITIZER_USE_GETRANDOM
2167  // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
2168  // blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
2169  uptr fd = internal_open("/dev/urandom", O_RDONLY);
2170  if (internal_iserror(fd))
2171    return false;
2172  uptr res = internal_read(fd, buffer, length);
2173  if (internal_iserror(res))
2174    return false;
2175  internal_close(fd);
2176  return true;
2177}
2178
2179} // namespace __sanitizer
2180
2181#endif
2182