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