asan_linux.cc revision 75b19ebf25af204cf209d108997272822241d6da
1//===-- asan_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 a part of AddressSanitizer, an address sanity checker.
11//
12// Linux-specific details.
13//===----------------------------------------------------------------------===//
14#ifdef __linux__
15
16#include "asan_interceptors.h"
17#include "asan_internal.h"
18#include "asan_lock.h"
19#include "asan_thread.h"
20#include "sanitizer_common/sanitizer_libc.h"
21#include "sanitizer_common/sanitizer_procmaps.h"
22
23#include <sys/time.h>
24#include <sys/resource.h>
25#include <sys/mman.h>
26#include <sys/syscall.h>
27#include <sys/types.h>
28#include <fcntl.h>
29#include <pthread.h>
30#include <stdio.h>
31#include <unistd.h>
32#include <unwind.h>
33
34#ifndef ANDROID
35// FIXME: where to get ucontext on Android?
36#include <sys/ucontext.h>
37#endif
38
39extern "C" void* _DYNAMIC;
40
41namespace __asan {
42
43void *AsanDoesNotSupportStaticLinkage() {
44  // This will fail to link with -static.
45  return &_DYNAMIC;  // defined in link.h
46}
47
48void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
49#ifdef ANDROID
50  *pc = *sp = *bp = 0;
51#elif defined(__arm__)
52  ucontext_t *ucontext = (ucontext_t*)context;
53  *pc = ucontext->uc_mcontext.arm_pc;
54  *bp = ucontext->uc_mcontext.arm_fp;
55  *sp = ucontext->uc_mcontext.arm_sp;
56# elif defined(__x86_64__)
57  ucontext_t *ucontext = (ucontext_t*)context;
58  *pc = ucontext->uc_mcontext.gregs[REG_RIP];
59  *bp = ucontext->uc_mcontext.gregs[REG_RBP];
60  *sp = ucontext->uc_mcontext.gregs[REG_RSP];
61# elif defined(__i386__)
62  ucontext_t *ucontext = (ucontext_t*)context;
63  *pc = ucontext->uc_mcontext.gregs[REG_EIP];
64  *bp = ucontext->uc_mcontext.gregs[REG_EBP];
65  *sp = ucontext->uc_mcontext.gregs[REG_ESP];
66#else
67# error "Unsupported arch"
68#endif
69}
70
71bool AsanInterceptsSignal(int signum) {
72  return signum == SIGSEGV && flags()->handle_segv;
73}
74
75void AsanPlatformThreadInit() {
76  // Nothing here for now.
77}
78
79AsanLock::AsanLock(LinkerInitialized) {
80  // We assume that pthread_mutex_t initialized to all zeroes is a valid
81  // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
82  // a gcc warning:
83  // extended initializer lists only available with -std=c++0x or -std=gnu++0x
84}
85
86void AsanLock::Lock() {
87  CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_));
88  pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_);
89  CHECK(!owner_);
90  owner_ = (uptr)pthread_self();
91}
92
93void AsanLock::Unlock() {
94  CHECK(owner_ == (uptr)pthread_self());
95  owner_ = 0;
96  pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_);
97}
98
99#ifdef __arm__
100#define UNWIND_STOP _URC_END_OF_STACK
101#define UNWIND_CONTINUE _URC_NO_REASON
102#else
103#define UNWIND_STOP _URC_NORMAL_STOP
104#define UNWIND_CONTINUE _URC_NO_REASON
105#endif
106
107uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
108#ifdef __arm__
109  uptr val;
110  _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
111      15 /* r15 = PC */, _UVRSD_UINT32, &val);
112  CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
113  // Clear the Thumb bit.
114  return val & ~(uptr)1;
115#else
116  return _Unwind_GetIP(ctx);
117#endif
118}
119
120_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx,
121    void *param) {
122  AsanStackTrace *b = (AsanStackTrace*)param;
123  CHECK(b->size < b->max_size);
124  uptr pc = Unwind_GetIP(ctx);
125  b->trace[b->size++] = pc;
126  if (b->size == b->max_size) return UNWIND_STOP;
127  return UNWIND_CONTINUE;
128}
129
130void AsanStackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) {
131  size = 0;
132  trace[0] = pc;
133  if ((max_s) > 1) {
134    max_size = max_s;
135#ifdef __arm__
136    _Unwind_Backtrace(Unwind_Trace, this);
137#else
138     FastUnwindStack(pc, bp);
139#endif
140  }
141}
142
143}  // namespace __asan
144
145#endif  // __linux__
146