sanitizer_linux.cc revision e57f26b5c99cedd56de76f3a10cadcecece768bd
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_LINUX
17
18#include "sanitizer_common.h"
19#include "sanitizer_internal_defs.h"
20#include "sanitizer_libc.h"
21#include "sanitizer_linux.h"
22#include "sanitizer_mutex.h"
23#include "sanitizer_placement_new.h"
24#include "sanitizer_procmaps.h"
25#include "sanitizer_stacktrace.h"
26#include "sanitizer_symbolizer.h"
27
28#include <asm/param.h>
29#include <dlfcn.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <pthread.h>
33#include <sched.h>
34#include <sys/mman.h>
35#include <sys/ptrace.h>
36#include <sys/resource.h>
37#include <sys/stat.h>
38#include <sys/syscall.h>
39#include <sys/time.h>
40#include <sys/types.h>
41#include <unistd.h>
42#include <unwind.h>
43
44#if !SANITIZER_ANDROID
45#include <sys/signal.h>
46#endif
47
48// <linux/time.h>
49struct kernel_timeval {
50  long tv_sec;
51  long tv_usec;
52};
53
54// <linux/futex.h> is broken on some linux distributions.
55const int FUTEX_WAIT = 0;
56const int FUTEX_WAKE = 1;
57
58// Are we using 32-bit or 64-bit syscalls?
59// x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
60// but it still needs to use 64-bit syscalls.
61#if defined(__x86_64__) || SANITIZER_WORDSIZE == 64
62# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
63#else
64# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
65#endif
66
67namespace __sanitizer {
68
69#ifdef __x86_64__
70#include "sanitizer_syscall_linux_x86_64.inc"
71#else
72#include "sanitizer_syscall_generic.inc"
73#endif
74
75// --------------- sanitizer_libc.h
76uptr internal_mmap(void *addr, uptr length, int prot, int flags,
77                    int fd, u64 offset) {
78#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
79  return internal_syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
80#else
81  return internal_syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
82#endif
83}
84
85uptr internal_munmap(void *addr, uptr length) {
86  return internal_syscall(__NR_munmap, addr, length);
87}
88
89uptr internal_close(fd_t fd) {
90  return internal_syscall(__NR_close, fd);
91}
92
93uptr internal_open(const char *filename, int flags) {
94  return internal_syscall(__NR_open, filename, flags);
95}
96
97uptr internal_open(const char *filename, int flags, u32 mode) {
98  return internal_syscall(__NR_open, filename, flags, mode);
99}
100
101uptr OpenFile(const char *filename, bool write) {
102  return internal_open(filename,
103      write ? O_WRONLY | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660);
104}
105
106uptr internal_read(fd_t fd, void *buf, uptr count) {
107  sptr res;
108  HANDLE_EINTR(res, (sptr)internal_syscall(__NR_read, fd, buf, count));
109  return res;
110}
111
112uptr internal_write(fd_t fd, const void *buf, uptr count) {
113  sptr res;
114  HANDLE_EINTR(res, (sptr)internal_syscall(__NR_write, fd, buf, count));
115  return res;
116}
117
118#if !SANITIZER_LINUX_USES_64BIT_SYSCALLS
119static void stat64_to_stat(struct stat64 *in, struct stat *out) {
120  internal_memset(out, 0, sizeof(*out));
121  out->st_dev = in->st_dev;
122  out->st_ino = in->st_ino;
123  out->st_mode = in->st_mode;
124  out->st_nlink = in->st_nlink;
125  out->st_uid = in->st_uid;
126  out->st_gid = in->st_gid;
127  out->st_rdev = in->st_rdev;
128  out->st_size = in->st_size;
129  out->st_blksize = in->st_blksize;
130  out->st_blocks = in->st_blocks;
131  out->st_atime = in->st_atime;
132  out->st_mtime = in->st_mtime;
133  out->st_ctime = in->st_ctime;
134  out->st_ino = in->st_ino;
135}
136#endif
137
138uptr internal_stat(const char *path, void *buf) {
139#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
140  return internal_syscall(__NR_stat, path, buf);
141#else
142  struct stat64 buf64;
143  int res = internal_syscall(__NR_stat64, path, &buf64);
144  stat64_to_stat(&buf64, (struct stat *)buf);
145  return res;
146#endif
147}
148
149uptr internal_lstat(const char *path, void *buf) {
150#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
151  return internal_syscall(__NR_lstat, path, buf);
152#else
153  struct stat64 buf64;
154  int res = internal_syscall(__NR_lstat64, path, &buf64);
155  stat64_to_stat(&buf64, (struct stat *)buf);
156  return res;
157#endif
158}
159
160uptr internal_fstat(fd_t fd, void *buf) {
161#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
162  return internal_syscall(__NR_fstat, fd, buf);
163#else
164  struct stat64 buf64;
165  int res = internal_syscall(__NR_fstat64, fd, &buf64);
166  stat64_to_stat(&buf64, (struct stat *)buf);
167  return res;
168#endif
169}
170
171uptr internal_filesize(fd_t fd) {
172  struct stat st;
173  if (internal_fstat(fd, &st))
174    return -1;
175  return (uptr)st.st_size;
176}
177
178uptr internal_dup2(int oldfd, int newfd) {
179  return internal_syscall(__NR_dup2, oldfd, newfd);
180}
181
182uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
183  return internal_syscall(__NR_readlink, path, buf, bufsize);
184}
185
186uptr internal_unlink(const char *path) {
187  return internal_syscall(__NR_unlink, path);
188}
189
190uptr internal_sched_yield() {
191  return internal_syscall(__NR_sched_yield);
192}
193
194void internal__exit(int exitcode) {
195  internal_syscall(__NR_exit_group, exitcode);
196  Die();  // Unreachable.
197}
198
199uptr internal_execve(const char *filename, char *const argv[],
200                     char *const envp[]) {
201  return internal_syscall(__NR_execve, filename, argv, envp);
202}
203
204// ----------------- sanitizer_common.h
205bool FileExists(const char *filename) {
206  struct stat st;
207  if (internal_stat(filename, &st))
208    return false;
209  // Sanity check: filename is a regular file.
210  return S_ISREG(st.st_mode);
211}
212
213uptr GetTid() {
214  return internal_syscall(__NR_gettid);
215}
216
217u64 NanoTime() {
218  kernel_timeval tv = {};
219  internal_syscall(__NR_gettimeofday, &tv, 0);
220  return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
221}
222
223// Like getenv, but reads env directly from /proc and does not use libc.
224// This function should be called first inside __asan_init.
225const char *GetEnv(const char *name) {
226  static char *environ;
227  static uptr len;
228  static bool inited;
229  if (!inited) {
230    inited = true;
231    uptr environ_size;
232    len = ReadFileToBuffer("/proc/self/environ",
233                           &environ, &environ_size, 1 << 26);
234  }
235  if (!environ || len == 0) return 0;
236  uptr namelen = internal_strlen(name);
237  const char *p = environ;
238  while (*p != '\0') {  // will happen at the \0\0 that terminates the buffer
239    // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
240    const char* endp =
241        (char*)internal_memchr(p, '\0', len - (p - environ));
242    if (endp == 0)  // this entry isn't NUL terminated
243      return 0;
244    else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=')  // Match.
245      return p + namelen + 1;  // point after =
246    p = endp + 1;
247  }
248  return 0;  // Not found.
249}
250
251extern "C" {
252  extern void *__libc_stack_end SANITIZER_WEAK_ATTRIBUTE;
253}
254
255#if !SANITIZER_GO
256static void ReadNullSepFileToArray(const char *path, char ***arr,
257                                   int arr_size) {
258  char *buff;
259  uptr buff_size = 0;
260  *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
261  ReadFileToBuffer(path, &buff, &buff_size, 1024 * 1024);
262  (*arr)[0] = buff;
263  int count, i;
264  for (count = 1, i = 1; ; i++) {
265    if (buff[i] == 0) {
266      if (buff[i+1] == 0) break;
267      (*arr)[count] = &buff[i+1];
268      CHECK_LE(count, arr_size - 1);  // FIXME: make this more flexible.
269      count++;
270    }
271  }
272  (*arr)[count] = 0;
273}
274#endif
275
276static void GetArgsAndEnv(char*** argv, char*** envp) {
277#if !SANITIZER_GO
278  if (&__libc_stack_end) {
279#endif
280    uptr* stack_end = (uptr*)__libc_stack_end;
281    int argc = *stack_end;
282    *argv = (char**)(stack_end + 1);
283    *envp = (char**)(stack_end + argc + 2);
284#if !SANITIZER_GO
285  } else {
286    static const int kMaxArgv = 2000, kMaxEnvp = 2000;
287    ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
288    ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
289  }
290#endif
291}
292
293void ReExec() {
294  char **argv, **envp;
295  GetArgsAndEnv(&argv, &envp);
296  uptr rv = internal_execve("/proc/self/exe", argv, envp);
297  int rverrno;
298  CHECK_EQ(internal_iserror(rv, &rverrno), true);
299  Printf("execve failed, errno %d\n", rverrno);
300  Die();
301}
302
303void PrepareForSandboxing() {
304  // Some kinds of sandboxes may forbid filesystem access, so we won't be able
305  // to read the file mappings from /proc/self/maps. Luckily, neither the
306  // process will be able to load additional libraries, so it's fine to use the
307  // cached mappings.
308  MemoryMappingLayout::CacheMemoryMappings();
309  // Same for /proc/self/exe in the symbolizer.
310  SymbolizerPrepareForSandboxing();
311}
312
313// ----------------- sanitizer_procmaps.h
314// Linker initialized.
315ProcSelfMapsBuff MemoryMappingLayout::cached_proc_self_maps_;
316StaticSpinMutex MemoryMappingLayout::cache_lock_;  // Linker initialized.
317
318MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
319  proc_self_maps_.len =
320      ReadFileToBuffer("/proc/self/maps", &proc_self_maps_.data,
321                       &proc_self_maps_.mmaped_size, 1 << 26);
322  if (cache_enabled) {
323    if (proc_self_maps_.mmaped_size == 0) {
324      LoadFromCache();
325      CHECK_GT(proc_self_maps_.len, 0);
326    }
327  } else {
328    CHECK_GT(proc_self_maps_.mmaped_size, 0);
329  }
330  Reset();
331  // FIXME: in the future we may want to cache the mappings on demand only.
332  if (cache_enabled)
333    CacheMemoryMappings();
334}
335
336MemoryMappingLayout::~MemoryMappingLayout() {
337  // Only unmap the buffer if it is different from the cached one. Otherwise
338  // it will be unmapped when the cache is refreshed.
339  if (proc_self_maps_.data != cached_proc_self_maps_.data) {
340    UnmapOrDie(proc_self_maps_.data, proc_self_maps_.mmaped_size);
341  }
342}
343
344void MemoryMappingLayout::Reset() {
345  current_ = proc_self_maps_.data;
346}
347
348// static
349void MemoryMappingLayout::CacheMemoryMappings() {
350  SpinMutexLock l(&cache_lock_);
351  // Don't invalidate the cache if the mappings are unavailable.
352  ProcSelfMapsBuff old_proc_self_maps;
353  old_proc_self_maps = cached_proc_self_maps_;
354  cached_proc_self_maps_.len =
355      ReadFileToBuffer("/proc/self/maps", &cached_proc_self_maps_.data,
356                       &cached_proc_self_maps_.mmaped_size, 1 << 26);
357  if (cached_proc_self_maps_.mmaped_size == 0) {
358    cached_proc_self_maps_ = old_proc_self_maps;
359  } else {
360    if (old_proc_self_maps.mmaped_size) {
361      UnmapOrDie(old_proc_self_maps.data,
362                 old_proc_self_maps.mmaped_size);
363    }
364  }
365}
366
367void MemoryMappingLayout::LoadFromCache() {
368  SpinMutexLock l(&cache_lock_);
369  if (cached_proc_self_maps_.data) {
370    proc_self_maps_ = cached_proc_self_maps_;
371  }
372}
373
374// Parse a hex value in str and update str.
375static uptr ParseHex(char **str) {
376  uptr x = 0;
377  char *s;
378  for (s = *str; ; s++) {
379    char c = *s;
380    uptr v = 0;
381    if (c >= '0' && c <= '9')
382      v = c - '0';
383    else if (c >= 'a' && c <= 'f')
384      v = c - 'a' + 10;
385    else if (c >= 'A' && c <= 'F')
386      v = c - 'A' + 10;
387    else
388      break;
389    x = x * 16 + v;
390  }
391  *str = s;
392  return x;
393}
394
395static bool IsOneOf(char c, char c1, char c2) {
396  return c == c1 || c == c2;
397}
398
399static bool IsDecimal(char c) {
400  return c >= '0' && c <= '9';
401}
402
403bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
404                               char filename[], uptr filename_size,
405                               uptr *protection) {
406  char *last = proc_self_maps_.data + proc_self_maps_.len;
407  if (current_ >= last) return false;
408  uptr dummy;
409  if (!start) start = &dummy;
410  if (!end) end = &dummy;
411  if (!offset) offset = &dummy;
412  char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
413  if (next_line == 0)
414    next_line = last;
415  // Example: 08048000-08056000 r-xp 00000000 03:0c 64593   /foo/bar
416  *start = ParseHex(&current_);
417  CHECK_EQ(*current_++, '-');
418  *end = ParseHex(&current_);
419  CHECK_EQ(*current_++, ' ');
420  uptr local_protection = 0;
421  CHECK(IsOneOf(*current_, '-', 'r'));
422  if (*current_++ == 'r')
423    local_protection |= kProtectionRead;
424  CHECK(IsOneOf(*current_, '-', 'w'));
425  if (*current_++ == 'w')
426    local_protection |= kProtectionWrite;
427  CHECK(IsOneOf(*current_, '-', 'x'));
428  if (*current_++ == 'x')
429    local_protection |= kProtectionExecute;
430  CHECK(IsOneOf(*current_, 's', 'p'));
431  if (*current_++ == 's')
432    local_protection |= kProtectionShared;
433  if (protection) {
434    *protection = local_protection;
435  }
436  CHECK_EQ(*current_++, ' ');
437  *offset = ParseHex(&current_);
438  CHECK_EQ(*current_++, ' ');
439  ParseHex(&current_);
440  CHECK_EQ(*current_++, ':');
441  ParseHex(&current_);
442  CHECK_EQ(*current_++, ' ');
443  while (IsDecimal(*current_))
444    current_++;
445  // Qemu may lack the trailing space.
446  // http://code.google.com/p/address-sanitizer/issues/detail?id=160
447  // CHECK_EQ(*current_++, ' ');
448  // Skip spaces.
449  while (current_ < next_line && *current_ == ' ')
450    current_++;
451  // Fill in the filename.
452  uptr i = 0;
453  while (current_ < next_line) {
454    if (filename && i < filename_size - 1)
455      filename[i++] = *current_;
456    current_++;
457  }
458  if (filename && i < filename_size)
459    filename[i] = 0;
460  current_ = next_line + 1;
461  return true;
462}
463
464// Gets the object name and the offset by walking MemoryMappingLayout.
465bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
466                                                 char filename[],
467                                                 uptr filename_size,
468                                                 uptr *protection) {
469  return IterateForObjectNameAndOffset(addr, offset, filename, filename_size,
470                                       protection);
471}
472
473enum MutexState {
474  MtxUnlocked = 0,
475  MtxLocked = 1,
476  MtxSleeping = 2
477};
478
479BlockingMutex::BlockingMutex(LinkerInitialized) {
480  CHECK_EQ(owner_, 0);
481}
482
483BlockingMutex::BlockingMutex() {
484  internal_memset(this, 0, sizeof(*this));
485}
486
487void BlockingMutex::Lock() {
488  atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
489  if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
490    return;
491  while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked)
492    internal_syscall(__NR_futex, m, FUTEX_WAIT, MtxSleeping, 0, 0, 0);
493}
494
495void BlockingMutex::Unlock() {
496  atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
497  u32 v = atomic_exchange(m, MtxUnlocked, memory_order_relaxed);
498  CHECK_NE(v, MtxUnlocked);
499  if (v == MtxSleeping)
500    internal_syscall(__NR_futex, m, FUTEX_WAKE, 1, 0, 0, 0);
501}
502
503void BlockingMutex::CheckLocked() {
504  atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
505  CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
506}
507
508// ----------------- sanitizer_linux.h
509// The actual size of this structure is specified by d_reclen.
510// Note that getdents64 uses a different structure format. We only provide the
511// 32-bit syscall here.
512struct linux_dirent {
513  unsigned long      d_ino;
514  unsigned long      d_off;
515  unsigned short     d_reclen;
516  char               d_name[256];
517};
518
519// Syscall wrappers.
520uptr internal_ptrace(int request, int pid, void *addr, void *data) {
521  return internal_syscall(__NR_ptrace, request, pid, addr, data);
522}
523
524uptr internal_waitpid(int pid, int *status, int options) {
525  return internal_syscall(__NR_wait4, pid, status, options, 0 /* rusage */);
526}
527
528uptr internal_getpid() {
529  return internal_syscall(__NR_getpid);
530}
531
532uptr internal_getppid() {
533  return internal_syscall(__NR_getppid);
534}
535
536uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
537  return internal_syscall(__NR_getdents, fd, dirp, count);
538}
539
540uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
541  return internal_syscall(__NR_lseek, fd, offset, whence);
542}
543
544uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
545  return internal_syscall(__NR_prctl, option, arg2, arg3, arg4, arg5);
546}
547
548uptr internal_sigaltstack(const struct sigaltstack *ss,
549                         struct sigaltstack *oss) {
550  return internal_syscall(__NR_sigaltstack, ss, oss);
551}
552
553// ThreadLister implementation.
554ThreadLister::ThreadLister(int pid)
555  : pid_(pid),
556    descriptor_(-1),
557    buffer_(4096),
558    error_(true),
559    entry_((struct linux_dirent *)buffer_.data()),
560    bytes_read_(0) {
561  char task_directory_path[80];
562  internal_snprintf(task_directory_path, sizeof(task_directory_path),
563                    "/proc/%d/task/", pid);
564  uptr openrv = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY);
565  if (internal_iserror(openrv)) {
566    error_ = true;
567    Report("Can't open /proc/%d/task for reading.\n", pid);
568  } else {
569    error_ = false;
570    descriptor_ = openrv;
571  }
572}
573
574int ThreadLister::GetNextTID() {
575  int tid = -1;
576  do {
577    if (error_)
578      return -1;
579    if ((char *)entry_ >= &buffer_[bytes_read_] && !GetDirectoryEntries())
580      return -1;
581    if (entry_->d_ino != 0 && entry_->d_name[0] >= '0' &&
582        entry_->d_name[0] <= '9') {
583      // Found a valid tid.
584      tid = (int)internal_atoll(entry_->d_name);
585    }
586    entry_ = (struct linux_dirent *)(((char *)entry_) + entry_->d_reclen);
587  } while (tid < 0);
588  return tid;
589}
590
591void ThreadLister::Reset() {
592  if (error_ || descriptor_ < 0)
593    return;
594  internal_lseek(descriptor_, 0, SEEK_SET);
595}
596
597ThreadLister::~ThreadLister() {
598  if (descriptor_ >= 0)
599    internal_close(descriptor_);
600}
601
602bool ThreadLister::error() { return error_; }
603
604bool ThreadLister::GetDirectoryEntries() {
605  CHECK_GE(descriptor_, 0);
606  CHECK_NE(error_, true);
607  bytes_read_ = internal_getdents(descriptor_,
608                                  (struct linux_dirent *)buffer_.data(),
609                                  buffer_.size());
610  if (internal_iserror(bytes_read_)) {
611    Report("Can't read directory entries from /proc/%d/task.\n", pid_);
612    error_ = true;
613    return false;
614  } else if (bytes_read_ == 0) {
615    return false;
616  }
617  entry_ = (struct linux_dirent *)buffer_.data();
618  return true;
619}
620
621uptr GetPageSize() {
622#if defined(__x86_64__) || defined(__i386__)
623  return EXEC_PAGESIZE;
624#else
625  return sysconf(_SC_PAGESIZE);  // EXEC_PAGESIZE may not be trustworthy.
626#endif
627}
628
629// Match full names of the form /path/to/base_name{-,.}*
630bool LibraryNameIs(const char *full_name, const char *base_name) {
631  const char *name = full_name;
632  // Strip path.
633  while (*name != '\0') name++;
634  while (name > full_name && *name != '/') name--;
635  if (*name == '/') name++;
636  uptr base_name_length = internal_strlen(base_name);
637  if (internal_strncmp(name, base_name, base_name_length)) return false;
638  return (name[base_name_length] == '-' || name[base_name_length] == '.');
639}
640
641}  // namespace __sanitizer
642
643#endif  // SANITIZER_LINUX
644