asan_linux.cc revision 996651d09e56caa91ffcc33bf1a13a283cfcd5e2
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 && FLAG_handle_segv; 73} 74 75void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size) { 76 return internal_mmap((void*)fixed_addr, size, 77 PROT_READ | PROT_WRITE, 78 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, 79 0, 0); 80} 81 82void *AsanMprotect(uptr fixed_addr, uptr size) { 83 return internal_mmap((void*)fixed_addr, size, 84 PROT_NONE, 85 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, 86 0, 0); 87} 88 89// Like getenv, but reads env directly from /proc and does not use libc. 90// This function should be called first inside __asan_init. 91const char* AsanGetEnv(const char* name) { 92 static char *environ; 93 static uptr len; 94 static bool inited; 95 if (!inited) { 96 inited = true; 97 uptr environ_size; 98 len = ReadFileToBuffer("/proc/self/environ", 99 &environ, &environ_size, 1 << 26); 100 } 101 if (!environ || len == 0) return 0; 102 uptr namelen = internal_strlen(name); 103 const char *p = environ; 104 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer 105 // proc file has the format NAME=value\0NAME=value\0NAME=value\0... 106 const char* endp = 107 (char*)internal_memchr(p, '\0', len - (p - environ)); 108 if (endp == 0) // this entry isn't NUL terminated 109 return 0; 110 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match. 111 return p + namelen + 1; // point after = 112 p = endp + 1; 113 } 114 return 0; // Not found. 115} 116 117AsanLock::AsanLock(LinkerInitialized) { 118 // We assume that pthread_mutex_t initialized to all zeroes is a valid 119 // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers 120 // a gcc warning: 121 // extended initializer lists only available with -std=c++0x or -std=gnu++0x 122} 123 124void AsanLock::Lock() { 125 CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_)); 126 pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_); 127 CHECK(!owner_); 128 owner_ = (uptr)pthread_self(); 129} 130 131void AsanLock::Unlock() { 132 CHECK(owner_ == (uptr)pthread_self()); 133 owner_ = 0; 134 pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_); 135} 136 137#ifdef __arm__ 138#define UNWIND_STOP _URC_END_OF_STACK 139#define UNWIND_CONTINUE _URC_NO_REASON 140#else 141#define UNWIND_STOP _URC_NORMAL_STOP 142#define UNWIND_CONTINUE _URC_NO_REASON 143#endif 144 145uptr Unwind_GetIP(struct _Unwind_Context *ctx) { 146#ifdef __arm__ 147 uptr val; 148 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE, 149 15 /* r15 = PC */, _UVRSD_UINT32, &val); 150 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed"); 151 // Clear the Thumb bit. 152 return val & ~(uptr)1; 153#else 154 return _Unwind_GetIP(ctx); 155#endif 156} 157 158_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, 159 void *param) { 160 AsanStackTrace *b = (AsanStackTrace*)param; 161 CHECK(b->size < b->max_size); 162 uptr pc = Unwind_GetIP(ctx); 163 b->trace[b->size++] = pc; 164 if (b->size == b->max_size) return UNWIND_STOP; 165 return UNWIND_CONTINUE; 166} 167 168void AsanStackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) { 169 size = 0; 170 trace[0] = pc; 171 if ((max_s) > 1) { 172 max_size = max_s; 173#ifdef __arm__ 174 _Unwind_Backtrace(Unwind_Trace, this); 175#else 176 FastUnwindStack(pc, bp); 177#endif 178 } 179} 180 181} // namespace __asan 182 183#endif // __linux__ 184