2011-11-30 09:07:02 +08:00
|
|
|
//===-- asan_linux.cc -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Linux-specific details.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-12-02 05:40:52 +08:00
|
|
|
#ifdef __linux__
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-01-05 08:44:33 +08:00
|
|
|
#include "asan_interceptors.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_internal.h"
|
2012-01-11 05:24:40 +08:00
|
|
|
#include "asan_lock.h"
|
2012-01-05 09:07:27 +08:00
|
|
|
#include "asan_thread.h"
|
2012-08-28 21:25:55 +08:00
|
|
|
#include "asan_thread_registry.h"
|
2012-06-04 22:27:50 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2012-06-07 14:15:12 +08:00
|
|
|
#include "sanitizer_common/sanitizer_procmaps.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-01-05 09:07:27 +08:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2011-11-30 09:07:02 +08:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/syscall.h>
|
2011-12-29 06:58:01 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
2012-01-05 09:07:27 +08:00
|
|
|
#include <pthread.h>
|
2012-01-05 08:44:33 +08:00
|
|
|
#include <stdio.h>
|
2011-11-30 09:07:02 +08:00
|
|
|
#include <unistd.h>
|
2012-01-19 19:34:18 +08:00
|
|
|
#include <unwind.h>
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-08-27 19:15:55 +08:00
|
|
|
#if !ASAN_ANDROID
|
2012-01-07 03:11:09 +08:00
|
|
|
// FIXME: where to get ucontext on Android?
|
|
|
|
#include <sys/ucontext.h>
|
|
|
|
#endif
|
|
|
|
|
2012-03-26 17:48:41 +08:00
|
|
|
extern "C" void* _DYNAMIC;
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
namespace __asan {
|
|
|
|
|
2012-08-24 17:22:05 +08:00
|
|
|
void MaybeReexec() {
|
|
|
|
// No need to re-exec on Linux.
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
void *AsanDoesNotSupportStaticLinkage() {
|
|
|
|
// This will fail to link with -static.
|
2012-01-06 07:50:34 +08:00
|
|
|
return &_DYNAMIC; // defined in link.h
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
|
2012-08-27 19:15:55 +08:00
|
|
|
#if ASAN_ANDROID
|
2012-01-07 03:11:09 +08:00
|
|
|
*pc = *sp = *bp = 0;
|
|
|
|
#elif defined(__arm__)
|
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
*pc = ucontext->uc_mcontext.arm_pc;
|
|
|
|
*bp = ucontext->uc_mcontext.arm_fp;
|
|
|
|
*sp = ucontext->uc_mcontext.arm_sp;
|
|
|
|
# elif defined(__x86_64__)
|
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
*pc = ucontext->uc_mcontext.gregs[REG_RIP];
|
|
|
|
*bp = ucontext->uc_mcontext.gregs[REG_RBP];
|
|
|
|
*sp = ucontext->uc_mcontext.gregs[REG_RSP];
|
|
|
|
# elif defined(__i386__)
|
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
*pc = ucontext->uc_mcontext.gregs[REG_EIP];
|
|
|
|
*bp = ucontext->uc_mcontext.gregs[REG_EBP];
|
|
|
|
*sp = ucontext->uc_mcontext.gregs[REG_ESP];
|
2012-11-20 15:00:42 +08:00
|
|
|
# elif defined(__powerpc__) || defined(__powerpc64__)
|
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
*pc = ucontext->uc_mcontext.regs->nip;
|
|
|
|
*sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
|
|
|
|
// The powerpc{,64}-linux ABIs do not specify r31 as the frame
|
|
|
|
// pointer, but GCC always uses r31 when we need a frame pointer.
|
|
|
|
*bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
|
2012-11-16 19:26:05 +08:00
|
|
|
# elif defined(__sparc__)
|
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
uptr *stk_ptr;
|
|
|
|
# if defined (__arch64__)
|
|
|
|
*pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
|
|
|
|
*sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
|
|
|
|
stk_ptr = (uptr *) (*sp + 2047);
|
|
|
|
*bp = stk_ptr[15];
|
|
|
|
# else
|
|
|
|
*pc = ucontext->uc_mcontext.gregs[REG_PC];
|
|
|
|
*sp = ucontext->uc_mcontext.gregs[REG_O6];
|
|
|
|
stk_ptr = (uptr *) *sp;
|
|
|
|
*bp = stk_ptr[15];
|
|
|
|
# endif
|
2012-01-07 03:11:09 +08:00
|
|
|
#else
|
|
|
|
# error "Unsupported arch"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-10 02:53:15 +08:00
|
|
|
bool AsanInterceptsSignal(int signum) {
|
2012-07-09 22:36:04 +08:00
|
|
|
return signum == SIGSEGV && flags()->handle_segv;
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
|
|
|
|
2012-07-23 22:07:58 +08:00
|
|
|
void AsanPlatformThreadInit() {
|
|
|
|
// Nothing here for now.
|
|
|
|
}
|
|
|
|
|
2012-01-11 05:24:40 +08:00
|
|
|
AsanLock::AsanLock(LinkerInitialized) {
|
|
|
|
// We assume that pthread_mutex_t initialized to all zeroes is a valid
|
|
|
|
// unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
|
|
|
|
// a gcc warning:
|
|
|
|
// extended initializer lists only available with -std=c++0x or -std=gnu++0x
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsanLock::Lock() {
|
|
|
|
CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_));
|
|
|
|
pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_);
|
|
|
|
CHECK(!owner_);
|
2012-05-31 22:35:53 +08:00
|
|
|
owner_ = (uptr)pthread_self();
|
2012-01-11 05:24:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsanLock::Unlock() {
|
2012-05-31 22:35:53 +08:00
|
|
|
CHECK(owner_ == (uptr)pthread_self());
|
2012-01-11 05:24:40 +08:00
|
|
|
owner_ = 0;
|
|
|
|
pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_);
|
|
|
|
}
|
|
|
|
|
2012-01-19 19:34:18 +08:00
|
|
|
#ifdef __arm__
|
|
|
|
#define UNWIND_STOP _URC_END_OF_STACK
|
|
|
|
#define UNWIND_CONTINUE _URC_NO_REASON
|
|
|
|
#else
|
|
|
|
#define UNWIND_STOP _URC_NORMAL_STOP
|
|
|
|
#define UNWIND_CONTINUE _URC_NO_REASON
|
|
|
|
#endif
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
|
2012-01-19 19:34:18 +08:00
|
|
|
#ifdef __arm__
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr val;
|
2012-01-19 19:34:18 +08:00
|
|
|
_Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
|
|
|
|
15 /* r15 = PC */, _UVRSD_UINT32, &val);
|
|
|
|
CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
|
|
|
|
// Clear the Thumb bit.
|
2012-05-31 22:35:53 +08:00
|
|
|
return val & ~(uptr)1;
|
2012-01-19 19:34:18 +08:00
|
|
|
#else
|
|
|
|
return _Unwind_GetIP(ctx);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx,
|
|
|
|
void *param) {
|
2012-08-28 19:54:30 +08:00
|
|
|
StackTrace *b = (StackTrace*)param;
|
2012-01-19 19:34:18 +08:00
|
|
|
CHECK(b->size < b->max_size);
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr pc = Unwind_GetIP(ctx);
|
2012-01-19 19:34:18 +08:00
|
|
|
b->trace[b->size++] = pc;
|
|
|
|
if (b->size == b->max_size) return UNWIND_STOP;
|
|
|
|
return UNWIND_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2012-08-28 21:25:55 +08:00
|
|
|
void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp) {
|
|
|
|
stack->size = 0;
|
|
|
|
stack->trace[0] = pc;
|
2012-01-19 19:34:18 +08:00
|
|
|
if ((max_s) > 1) {
|
2012-08-28 21:25:55 +08:00
|
|
|
stack->max_size = max_s;
|
2012-11-20 15:00:42 +08:00
|
|
|
#if defined(__arm__) || defined(__powerpc__) || defined(__powerpc64__)
|
2012-08-28 21:25:55 +08:00
|
|
|
_Unwind_Backtrace(Unwind_Trace, stack);
|
2012-01-19 19:34:18 +08:00
|
|
|
#else
|
2012-08-28 21:25:55 +08:00
|
|
|
if (!asan_inited) return;
|
|
|
|
if (AsanThread *t = asanThreadRegistry().GetCurrent())
|
|
|
|
stack->FastUnwindStack(pc, bp, t->stack_top(), t->stack_bottom());
|
2012-01-19 19:34:18 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
2011-12-02 05:40:52 +08:00
|
|
|
|
|
|
|
#endif // __linux__
|