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#if SANITIZER_FREEBSD || SANITIZER_LINUX
15
16#include "sanitizer_common.h"
17#include "sanitizer_flags.h"
18#include "sanitizer_internal_defs.h"
19#include "sanitizer_libc.h"
20#include "sanitizer_linux.h"
21#include "sanitizer_mutex.h"
22#include "sanitizer_placement_new.h"
23#include "sanitizer_procmaps.h"
24#include "sanitizer_stacktrace.h"
25#include "sanitizer_symbolizer.h"
26
27#if !SANITIZER_FREEBSD
28#include <asm/param.h>
29#endif
30
31#include <dlfcn.h>
32#include <errno.h>
33#include <fcntl.h>
34#if !SANITIZER_ANDROID
35#include <link.h>
36#endif
37#include <pthread.h>
38#include <sched.h>
39#include <sys/mman.h>
40#include <sys/ptrace.h>
41#include <sys/resource.h>
42#include <sys/stat.h>
43#include <sys/syscall.h>
44#include <sys/time.h>
45#include <sys/types.h>
46#include <unistd.h>
47
48#if SANITIZER_FREEBSD
49#include <sys/sysctl.h>
50#include <machine/atomic.h>
51extern "C" {
52// <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
53// FreeBSD 9.2 and 10.0.
54#include <sys/umtx.h>
55}
56extern char **environ;  // provided by crt1
57#endif  // SANITIZER_FREEBSD
58
59#if !SANITIZER_ANDROID
60#include <sys/signal.h>
61#endif
62
63#if SANITIZER_ANDROID
64#include <android/log.h>
65#include <sys/system_properties.h>
66#endif
67
68#if SANITIZER_LINUX
69// <linux/time.h>
70struct kernel_timeval {
71  long tv_sec;
72  long tv_usec;
73};
74
75// <linux/futex.h> is broken on some linux distributions.
76const int FUTEX_WAIT = 0;
77const int FUTEX_WAKE = 1;
78#endif  // SANITIZER_LINUX
79
80// Are we using 32-bit or 64-bit Linux syscalls?
81// x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
82// but it still needs to use 64-bit syscalls.
83#if SANITIZER_LINUX && (defined(__x86_64__) || SANITIZER_WORDSIZE == 64)
84# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
85#else
86# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
87#endif
88
89namespace __sanitizer {
90
91#if SANITIZER_LINUX && defined(__x86_64__)
92#include "sanitizer_syscall_linux_x86_64.inc"
93#else
94#include "sanitizer_syscall_generic.inc"
95#endif
96
97// --------------- sanitizer_libc.h
98uptr internal_mmap(void *addr, uptr length, int prot, int flags,
99                    int fd, u64 offset) {
100#if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
101  return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd,
102                          offset);
103#else
104  return internal_syscall(SYSCALL(mmap2), addr, length, prot, flags, fd,
105                          offset);
106#endif
107}
108
109uptr internal_munmap(void *addr, uptr length) {
110  return internal_syscall(SYSCALL(munmap), (uptr)addr, length);
111}
112
113uptr internal_close(fd_t fd) {
114  return internal_syscall(SYSCALL(close), fd);
115}
116
117uptr internal_open(const char *filename, int flags) {
118#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
119  return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags);
120#else
121  return internal_syscall(SYSCALL(open), (uptr)filename, flags);
122#endif
123}
124
125uptr internal_open(const char *filename, int flags, u32 mode) {
126#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
127  return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags,
128                          mode);
129#else
130  return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode);
131#endif
132}
133
134uptr OpenFile(const char *filename, bool write) {
135  return internal_open(filename,
136      write ? O_RDWR | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660);
137}
138
139uptr internal_read(fd_t fd, void *buf, uptr count) {
140  sptr res;
141  HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf,
142               count));
143  return res;
144}
145
146uptr internal_write(fd_t fd, const void *buf, uptr count) {
147  sptr res;
148  HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf,
149               count));
150  return res;
151}
152
153uptr internal_ftruncate(fd_t fd, uptr size) {
154  sptr res;
155  HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(ftruncate), fd, size));
156  return res;
157}
158
159#if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && !SANITIZER_FREEBSD
160static void stat64_to_stat(struct stat64 *in, struct stat *out) {
161  internal_memset(out, 0, sizeof(*out));
162  out->st_dev = in->st_dev;
163  out->st_ino = in->st_ino;
164  out->st_mode = in->st_mode;
165  out->st_nlink = in->st_nlink;
166  out->st_uid = in->st_uid;
167  out->st_gid = in->st_gid;
168  out->st_rdev = in->st_rdev;
169  out->st_size = in->st_size;
170  out->st_blksize = in->st_blksize;
171  out->st_blocks = in->st_blocks;
172  out->st_atime = in->st_atime;
173  out->st_mtime = in->st_mtime;
174  out->st_ctime = in->st_ctime;
175  out->st_ino = in->st_ino;
176}
177#endif
178
179uptr internal_stat(const char *path, void *buf) {
180#if SANITIZER_FREEBSD
181  return internal_syscall(SYSCALL(stat), path, buf);
182#elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
183  return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path,
184                          (uptr)buf, 0);
185#elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
186  return internal_syscall(SYSCALL(stat), (uptr)path, (uptr)buf);
187#else
188  struct stat64 buf64;
189  int res = internal_syscall(SYSCALL(stat64), path, &buf64);
190  stat64_to_stat(&buf64, (struct stat *)buf);
191  return res;
192#endif
193}
194
195uptr internal_lstat(const char *path, void *buf) {
196#if SANITIZER_FREEBSD
197  return internal_syscall(SYSCALL(lstat), path, buf);
198#elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
199  return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path,
200                         (uptr)buf, AT_SYMLINK_NOFOLLOW);
201#elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
202  return internal_syscall(SYSCALL(lstat), (uptr)path, (uptr)buf);
203#else
204  struct stat64 buf64;
205  int res = internal_syscall(SYSCALL(lstat64), path, &buf64);
206  stat64_to_stat(&buf64, (struct stat *)buf);
207  return res;
208#endif
209}
210
211uptr internal_fstat(fd_t fd, void *buf) {
212#if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
213  return internal_syscall(SYSCALL(fstat), fd, (uptr)buf);
214#else
215  struct stat64 buf64;
216  int res = internal_syscall(SYSCALL(fstat64), fd, &buf64);
217  stat64_to_stat(&buf64, (struct stat *)buf);
218  return res;
219#endif
220}
221
222uptr internal_filesize(fd_t fd) {
223  struct stat st;
224  if (internal_fstat(fd, &st))
225    return -1;
226  return (uptr)st.st_size;
227}
228
229uptr internal_dup2(int oldfd, int newfd) {
230#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
231  return internal_syscall(SYSCALL(dup3), oldfd, newfd, 0);
232#else
233  return internal_syscall(SYSCALL(dup2), oldfd, newfd);
234#endif
235}
236
237uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
238#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
239  return internal_syscall(SYSCALL(readlinkat), AT_FDCWD,
240                          (uptr)path, (uptr)buf, bufsize);
241#else
242  return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize);
243#endif
244}
245
246uptr internal_unlink(const char *path) {
247#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
248  return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0);
249#else
250  return internal_syscall(SYSCALL(unlink), (uptr)path);
251#endif
252}
253
254uptr internal_rename(const char *oldpath, const char *newpath) {
255#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
256  return internal_syscall(SYSCALL(renameat), AT_FDCWD, (uptr)oldpath, AT_FDCWD,
257                          (uptr)newpath);
258#else
259  return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath);
260#endif
261}
262
263uptr internal_sched_yield() {
264  return internal_syscall(SYSCALL(sched_yield));
265}
266
267void internal__exit(int exitcode) {
268#if SANITIZER_FREEBSD
269  internal_syscall(SYSCALL(exit), exitcode);
270#else
271  internal_syscall(SYSCALL(exit_group), exitcode);
272#endif
273  Die();  // Unreachable.
274}
275
276uptr internal_execve(const char *filename, char *const argv[],
277                     char *const envp[]) {
278  return internal_syscall(SYSCALL(execve), (uptr)filename, (uptr)argv,
279                          (uptr)envp);
280}
281
282// ----------------- sanitizer_common.h
283bool FileExists(const char *filename) {
284  struct stat st;
285#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
286  if (internal_syscall(SYSCALL(newfstatat), AT_FDCWD, filename, &st, 0))
287#else
288  if (internal_stat(filename, &st))
289#endif
290    return false;
291  // Sanity check: filename is a regular file.
292  return S_ISREG(st.st_mode);
293}
294
295uptr GetTid() {
296#if SANITIZER_FREEBSD
297  return (uptr)pthread_self();
298#else
299  return internal_syscall(SYSCALL(gettid));
300#endif
301}
302
303u64 NanoTime() {
304#if SANITIZER_FREEBSD
305  timeval tv;
306#else
307  kernel_timeval tv;
308#endif
309  internal_memset(&tv, 0, sizeof(tv));
310  internal_syscall(SYSCALL(gettimeofday), (uptr)&tv, 0);
311  return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
312}
313
314// Like getenv, but reads env directly from /proc (on Linux) or parses the
315// 'environ' array (on FreeBSD) and does not use libc. This function should be
316// called first inside __asan_init.
317const char *GetEnv(const char *name) {
318#if SANITIZER_FREEBSD
319  if (::environ != 0) {
320    uptr NameLen = internal_strlen(name);
321    for (char **Env = ::environ; *Env != 0; Env++) {
322      if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
323        return (*Env) + NameLen + 1;
324    }
325  }
326  return 0;  // Not found.
327#elif SANITIZER_LINUX
328  static char *environ;
329  static uptr len;
330  static bool inited;
331  if (!inited) {
332    inited = true;
333    uptr environ_size;
334    len = ReadFileToBuffer("/proc/self/environ",
335                           &environ, &environ_size, 1 << 26);
336  }
337  if (!environ || len == 0) return 0;
338  uptr namelen = internal_strlen(name);
339  const char *p = environ;
340  while (*p != '\0') {  // will happen at the \0\0 that terminates the buffer
341    // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
342    const char* endp =
343        (char*)internal_memchr(p, '\0', len - (p - environ));
344    if (endp == 0)  // this entry isn't NUL terminated
345      return 0;
346    else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=')  // Match.
347      return p + namelen + 1;  // point after =
348    p = endp + 1;
349  }
350  return 0;  // Not found.
351#else
352#error "Unsupported platform"
353#endif
354}
355
356extern "C" {
357  SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
358}
359
360#if !SANITIZER_GO
361static void ReadNullSepFileToArray(const char *path, char ***arr,
362                                   int arr_size) {
363  char *buff;
364  uptr buff_size = 0;
365  *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
366  ReadFileToBuffer(path, &buff, &buff_size, 1024 * 1024);
367  (*arr)[0] = buff;
368  int count, i;
369  for (count = 1, i = 1; ; i++) {
370    if (buff[i] == 0) {
371      if (buff[i+1] == 0) break;
372      (*arr)[count] = &buff[i+1];
373      CHECK_LE(count, arr_size - 1);  // FIXME: make this more flexible.
374      count++;
375    }
376  }
377  (*arr)[count] = 0;
378}
379#endif
380
381static void GetArgsAndEnv(char*** argv, char*** envp) {
382#if !SANITIZER_GO
383  if (&__libc_stack_end) {
384#endif
385    uptr* stack_end = (uptr*)__libc_stack_end;
386    int argc = *stack_end;
387    *argv = (char**)(stack_end + 1);
388    *envp = (char**)(stack_end + argc + 2);
389#if !SANITIZER_GO
390  } else {
391    static const int kMaxArgv = 2000, kMaxEnvp = 2000;
392    ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
393    ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
394  }
395#endif
396}
397
398void ReExec() {
399  char **argv, **envp;
400  GetArgsAndEnv(&argv, &envp);
401  uptr rv = internal_execve("/proc/self/exe", argv, envp);
402  int rverrno;
403  CHECK_EQ(internal_iserror(rv, &rverrno), true);
404  Printf("execve failed, errno %d\n", rverrno);
405  Die();
406}
407
408// Stub implementation of GetThreadStackAndTls for Go.
409#if SANITIZER_GO
410void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
411                          uptr *tls_addr, uptr *tls_size) {
412  *stk_addr = 0;
413  *stk_size = 0;
414  *tls_addr = 0;
415  *tls_size = 0;
416}
417#endif  // SANITIZER_GO
418
419enum MutexState {
420  MtxUnlocked = 0,
421  MtxLocked = 1,
422  MtxSleeping = 2
423};
424
425BlockingMutex::BlockingMutex(LinkerInitialized) {
426  CHECK_EQ(owner_, 0);
427}
428
429BlockingMutex::BlockingMutex() {
430  internal_memset(this, 0, sizeof(*this));
431}
432
433void BlockingMutex::Lock() {
434  atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
435  if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
436    return;
437  while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
438#if SANITIZER_FREEBSD
439    _umtx_op(m, UMTX_OP_WAIT_UINT, MtxSleeping, 0, 0);
440#else
441    internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAIT, MtxSleeping, 0, 0, 0);
442#endif
443  }
444}
445
446void BlockingMutex::Unlock() {
447  atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
448  u32 v = atomic_exchange(m, MtxUnlocked, memory_order_relaxed);
449  CHECK_NE(v, MtxUnlocked);
450  if (v == MtxSleeping) {
451#if SANITIZER_FREEBSD
452    _umtx_op(m, UMTX_OP_WAKE, 1, 0, 0);
453#else
454    internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAKE, 1, 0, 0, 0);
455#endif
456  }
457}
458
459void BlockingMutex::CheckLocked() {
460  atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
461  CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
462}
463
464// ----------------- sanitizer_linux.h
465// The actual size of this structure is specified by d_reclen.
466// Note that getdents64 uses a different structure format. We only provide the
467// 32-bit syscall here.
468struct linux_dirent {
469#if SANITIZER_X32
470  u64 d_ino;
471  u64 d_off;
472#else
473  unsigned long      d_ino;
474  unsigned long      d_off;
475#endif
476  unsigned short     d_reclen;
477  char               d_name[256];
478};
479
480// Syscall wrappers.
481uptr internal_ptrace(int request, int pid, void *addr, void *data) {
482  return internal_syscall(SYSCALL(ptrace), request, pid, (uptr)addr,
483                          (uptr)data);
484}
485
486uptr internal_waitpid(int pid, int *status, int options) {
487  return internal_syscall(SYSCALL(wait4), pid, (uptr)status, options,
488                          0 /* rusage */);
489}
490
491uptr internal_getpid() {
492  return internal_syscall(SYSCALL(getpid));
493}
494
495uptr internal_getppid() {
496  return internal_syscall(SYSCALL(getppid));
497}
498
499uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
500#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
501  return internal_syscall(SYSCALL(getdents64), fd, (uptr)dirp, count);
502#else
503  return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count);
504#endif
505}
506
507uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
508  return internal_syscall(SYSCALL(lseek), fd, offset, whence);
509}
510
511#if SANITIZER_LINUX
512uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
513  return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5);
514}
515#endif
516
517uptr internal_sigaltstack(const struct sigaltstack *ss,
518                         struct sigaltstack *oss) {
519  return internal_syscall(SYSCALL(sigaltstack), (uptr)ss, (uptr)oss);
520}
521
522int internal_fork() {
523#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
524  return internal_syscall(SYSCALL(clone), SIGCHLD, 0);
525#else
526  return internal_syscall(SYSCALL(fork));
527#endif
528}
529
530#if SANITIZER_LINUX
531// Doesn't set sa_restorer, use with caution (see below).
532int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
533  __sanitizer_kernel_sigaction_t k_act, k_oldact;
534  internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t));
535  internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t));
536  const __sanitizer_sigaction *u_act = (__sanitizer_sigaction *)act;
537  __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact;
538  if (u_act) {
539    k_act.handler = u_act->handler;
540    k_act.sigaction = u_act->sigaction;
541    internal_memcpy(&k_act.sa_mask, &u_act->sa_mask,
542                    sizeof(__sanitizer_kernel_sigset_t));
543    k_act.sa_flags = u_act->sa_flags;
544    // FIXME: most often sa_restorer is unset, however the kernel requires it
545    // to point to a valid signal restorer that calls the rt_sigreturn syscall.
546    // If sa_restorer passed to the kernel is NULL, the program may crash upon
547    // signal delivery or fail to unwind the stack in the signal handler.
548    // libc implementation of sigaction() passes its own restorer to
549    // rt_sigaction, so we need to do the same (we'll need to reimplement the
550    // restorers; for x86_64 the restorer address can be obtained from
551    // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
552    k_act.sa_restorer = u_act->sa_restorer;
553  }
554
555  uptr result = internal_syscall(SYSCALL(rt_sigaction), (uptr)signum,
556      (uptr)(u_act ? &k_act : NULL),
557      (uptr)(u_oldact ? &k_oldact : NULL),
558      (uptr)sizeof(__sanitizer_kernel_sigset_t));
559
560  if ((result == 0) && u_oldact) {
561    u_oldact->handler = k_oldact.handler;
562    u_oldact->sigaction = k_oldact.sigaction;
563    internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask,
564                    sizeof(__sanitizer_kernel_sigset_t));
565    u_oldact->sa_flags = k_oldact.sa_flags;
566    u_oldact->sa_restorer = k_oldact.sa_restorer;
567  }
568  return result;
569}
570#endif  // SANITIZER_LINUX
571
572uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
573    __sanitizer_sigset_t *oldset) {
574#if SANITIZER_FREEBSD
575  return internal_syscall(SYSCALL(sigprocmask), how, set, oldset);
576#else
577  __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
578  __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset;
579  return internal_syscall(SYSCALL(rt_sigprocmask), (uptr)how,
580                          (uptr)&k_set->sig[0], (uptr)&k_oldset->sig[0],
581                          sizeof(__sanitizer_kernel_sigset_t));
582#endif
583}
584
585void internal_sigfillset(__sanitizer_sigset_t *set) {
586  internal_memset(set, 0xff, sizeof(*set));
587}
588
589#if SANITIZER_LINUX
590void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
591  signum -= 1;
592  CHECK_GE(signum, 0);
593  CHECK_LT(signum, sizeof(*set) * 8);
594  __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
595  const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
596  const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
597  k_set->sig[idx] &= ~(1 << bit);
598}
599#endif  // SANITIZER_LINUX
600
601// ThreadLister implementation.
602ThreadLister::ThreadLister(int pid)
603  : pid_(pid),
604    descriptor_(-1),
605    buffer_(4096),
606    error_(true),
607    entry_((struct linux_dirent *)buffer_.data()),
608    bytes_read_(0) {
609  char task_directory_path[80];
610  internal_snprintf(task_directory_path, sizeof(task_directory_path),
611                    "/proc/%d/task/", pid);
612  uptr openrv = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY);
613  if (internal_iserror(openrv)) {
614    error_ = true;
615    Report("Can't open /proc/%d/task for reading.\n", pid);
616  } else {
617    error_ = false;
618    descriptor_ = openrv;
619  }
620}
621
622int ThreadLister::GetNextTID() {
623  int tid = -1;
624  do {
625    if (error_)
626      return -1;
627    if ((char *)entry_ >= &buffer_[bytes_read_] && !GetDirectoryEntries())
628      return -1;
629    if (entry_->d_ino != 0 && entry_->d_name[0] >= '0' &&
630        entry_->d_name[0] <= '9') {
631      // Found a valid tid.
632      tid = (int)internal_atoll(entry_->d_name);
633    }
634    entry_ = (struct linux_dirent *)(((char *)entry_) + entry_->d_reclen);
635  } while (tid < 0);
636  return tid;
637}
638
639void ThreadLister::Reset() {
640  if (error_ || descriptor_ < 0)
641    return;
642  internal_lseek(descriptor_, 0, SEEK_SET);
643}
644
645ThreadLister::~ThreadLister() {
646  if (descriptor_ >= 0)
647    internal_close(descriptor_);
648}
649
650bool ThreadLister::error() { return error_; }
651
652bool ThreadLister::GetDirectoryEntries() {
653  CHECK_GE(descriptor_, 0);
654  CHECK_NE(error_, true);
655  bytes_read_ = internal_getdents(descriptor_,
656                                  (struct linux_dirent *)buffer_.data(),
657                                  buffer_.size());
658  if (internal_iserror(bytes_read_)) {
659    Report("Can't read directory entries from /proc/%d/task.\n", pid_);
660    error_ = true;
661    return false;
662  } else if (bytes_read_ == 0) {
663    return false;
664  }
665  entry_ = (struct linux_dirent *)buffer_.data();
666  return true;
667}
668
669uptr GetPageSize() {
670#if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__))
671  return EXEC_PAGESIZE;
672#else
673  return sysconf(_SC_PAGESIZE);  // EXEC_PAGESIZE may not be trustworthy.
674#endif
675}
676
677static char proc_self_exe_cache_str[kMaxPathLength];
678static uptr proc_self_exe_cache_len = 0;
679
680uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
681  if (proc_self_exe_cache_len > 0) {
682    // If available, use the cached module name.
683    uptr module_name_len =
684        internal_snprintf(buf, buf_len, "%s", proc_self_exe_cache_str);
685    CHECK_LT(module_name_len, buf_len);
686    return module_name_len;
687  }
688#if SANITIZER_FREEBSD
689  const int Mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
690  size_t Size = buf_len;
691  bool IsErr = (sysctl(Mib, 4, buf, &Size, NULL, 0) != 0);
692  int readlink_error = IsErr ? errno : 0;
693  uptr module_name_len = Size;
694#else
695  uptr module_name_len = internal_readlink(
696      "/proc/self/exe", buf, buf_len);
697  int readlink_error;
698  bool IsErr = internal_iserror(module_name_len, &readlink_error);
699#endif
700  if (IsErr) {
701    // We can't read /proc/self/exe for some reason, assume the name of the
702    // binary is unknown.
703    Report("WARNING: readlink(\"/proc/self/exe\") failed with errno %d, "
704           "some stack frames may not be symbolized\n", readlink_error);
705    module_name_len = internal_snprintf(buf, buf_len, "/proc/self/exe");
706    CHECK_LT(module_name_len, buf_len);
707  }
708  return module_name_len;
709}
710
711void CacheBinaryName() {
712  if (!proc_self_exe_cache_len) {
713    proc_self_exe_cache_len =
714        ReadBinaryName(proc_self_exe_cache_str, kMaxPathLength);
715  }
716}
717
718// Match full names of the form /path/to/base_name{-,.}*
719bool LibraryNameIs(const char *full_name, const char *base_name) {
720  const char *name = full_name;
721  // Strip path.
722  while (*name != '\0') name++;
723  while (name > full_name && *name != '/') name--;
724  if (*name == '/') name++;
725  uptr base_name_length = internal_strlen(base_name);
726  if (internal_strncmp(name, base_name, base_name_length)) return false;
727  return (name[base_name_length] == '-' || name[base_name_length] == '.');
728}
729
730#if !SANITIZER_ANDROID
731// Call cb for each region mapped by map.
732void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) {
733#if !SANITIZER_FREEBSD
734  typedef ElfW(Phdr) Elf_Phdr;
735  typedef ElfW(Ehdr) Elf_Ehdr;
736#endif  // !SANITIZER_FREEBSD
737  char *base = (char *)map->l_addr;
738  Elf_Ehdr *ehdr = (Elf_Ehdr *)base;
739  char *phdrs = base + ehdr->e_phoff;
740  char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize;
741
742  // Find the segment with the minimum base so we can "relocate" the p_vaddr
743  // fields.  Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
744  // objects have a non-zero base.
745  uptr preferred_base = (uptr)-1;
746  for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
747    Elf_Phdr *phdr = (Elf_Phdr *)iter;
748    if (phdr->p_type == PT_LOAD && preferred_base > (uptr)phdr->p_vaddr)
749      preferred_base = (uptr)phdr->p_vaddr;
750  }
751
752  // Compute the delta from the real base to get a relocation delta.
753  sptr delta = (uptr)base - preferred_base;
754  // Now we can figure out what the loader really mapped.
755  for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
756    Elf_Phdr *phdr = (Elf_Phdr *)iter;
757    if (phdr->p_type == PT_LOAD) {
758      uptr seg_start = phdr->p_vaddr + delta;
759      uptr seg_end = seg_start + phdr->p_memsz;
760      // None of these values are aligned.  We consider the ragged edges of the
761      // load command as defined, since they are mapped from the file.
762      seg_start = RoundDownTo(seg_start, GetPageSizeCached());
763      seg_end = RoundUpTo(seg_end, GetPageSizeCached());
764      cb((void *)seg_start, seg_end - seg_start);
765    }
766  }
767}
768#endif
769
770#if defined(__x86_64__) && SANITIZER_LINUX
771// We cannot use glibc's clone wrapper, because it messes with the child
772// task's TLS. It writes the PID and TID of the child task to its thread
773// descriptor, but in our case the child task shares the thread descriptor with
774// the parent (because we don't know how to allocate a new thread
775// descriptor to keep glibc happy). So the stock version of clone(), when
776// used with CLONE_VM, would end up corrupting the parent's thread descriptor.
777uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
778                    int *parent_tidptr, void *newtls, int *child_tidptr) {
779  long long res;
780  if (!fn || !child_stack)
781    return -EINVAL;
782  CHECK_EQ(0, (uptr)child_stack % 16);
783  child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
784  ((unsigned long long *)child_stack)[0] = (uptr)fn;
785  ((unsigned long long *)child_stack)[1] = (uptr)arg;
786  register void *r8 __asm__("r8") = newtls;
787  register int *r10 __asm__("r10") = child_tidptr;
788  __asm__ __volatile__(
789                       /* %rax = syscall(%rax = SYSCALL(clone),
790                        *                %rdi = flags,
791                        *                %rsi = child_stack,
792                        *                %rdx = parent_tidptr,
793                        *                %r8  = new_tls,
794                        *                %r10 = child_tidptr)
795                        */
796                       "syscall\n"
797
798                       /* if (%rax != 0)
799                        *   return;
800                        */
801                       "testq  %%rax,%%rax\n"
802                       "jnz    1f\n"
803
804                       /* In the child. Terminate unwind chain. */
805                       // XXX: We should also terminate the CFI unwind chain
806                       // here. Unfortunately clang 3.2 doesn't support the
807                       // necessary CFI directives, so we skip that part.
808                       "xorq   %%rbp,%%rbp\n"
809
810                       /* Call "fn(arg)". */
811                       "popq   %%rax\n"
812                       "popq   %%rdi\n"
813                       "call   *%%rax\n"
814
815                       /* Call _exit(%rax). */
816                       "movq   %%rax,%%rdi\n"
817                       "movq   %2,%%rax\n"
818                       "syscall\n"
819
820                       /* Return to parent. */
821                     "1:\n"
822                       : "=a" (res)
823                       : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)),
824                         "S"(child_stack),
825                         "D"(flags),
826                         "d"(parent_tidptr),
827                         "r"(r8),
828                         "r"(r10)
829                       : "rsp", "memory", "r11", "rcx");
830  return res;
831}
832#endif  // defined(__x86_64__) && SANITIZER_LINUX
833
834#if SANITIZER_ANDROID
835static atomic_uint8_t android_log_initialized;
836
837void AndroidLogInit() {
838  atomic_store(&android_log_initialized, 1, memory_order_release);
839}
840// This thing is not, strictly speaking, async signal safe, but it does not seem
841// to cause any issues. Alternative is writing to log devices directly, but
842// their location and message format might change in the future, so we'd really
843// like to avoid that.
844void AndroidLogWrite(const char *buffer) {
845  if (!atomic_load(&android_log_initialized, memory_order_acquire))
846    return;
847
848  char *copy = internal_strdup(buffer);
849  char *p = copy;
850  char *q;
851  // __android_log_write has an implicit message length limit.
852  // Print one line at a time.
853  do {
854    q = internal_strchr(p, '\n');
855    if (q) *q = '\0';
856    __android_log_write(ANDROID_LOG_INFO, NULL, p);
857    if (q) p = q + 1;
858  } while (q);
859  InternalFree(copy);
860}
861
862void GetExtraActivationFlags(char *buf, uptr size) {
863  CHECK(size > PROP_VALUE_MAX);
864  __system_property_get("asan.options", buf);
865}
866#endif
867
868bool IsDeadlySignal(int signum) {
869  return (signum == SIGSEGV) && common_flags()->handle_segv;
870}
871
872}  // namespace __sanitizer
873
874#endif  // SANITIZER_FREEBSD || SANITIZER_LINUX
875