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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-03-19 22:33:38 +08:00
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
|
|
|
#if SANITIZER_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-05 09:07:27 +08:00
|
|
|
#include "asan_thread.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
|
|
|
|
2013-03-19 21:54:41 +08:00
|
|
|
#if !SANITIZER_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) {
|
2013-03-19 21:54:41 +08:00
|
|
|
#if SANITIZER_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
|
2013-06-03 22:49:25 +08:00
|
|
|
# elif defined(__mips__)
|
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
*pc = ucontext->uc_mcontext.gregs[31];
|
|
|
|
*bp = ucontext->uc_mcontext.gregs[30];
|
|
|
|
*sp = ucontext->uc_mcontext.gregs[29];
|
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.
|
|
|
|
}
|
|
|
|
|
2013-03-19 21:54:41 +08:00
|
|
|
#if !SANITIZER_ANDROID
|
2013-01-17 23:45:28 +08:00
|
|
|
void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
|
2012-11-23 18:14:44 +08:00
|
|
|
ucontext_t *ucp = (ucontext_t*)context;
|
2013-01-17 23:45:28 +08:00
|
|
|
*stack = (uptr)ucp->uc_stack.ss_sp;
|
|
|
|
*ssize = ucp->uc_stack.ss_size;
|
2012-11-23 18:14:44 +08:00
|
|
|
}
|
|
|
|
#else
|
2013-01-17 23:45:28 +08:00
|
|
|
void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
|
2013-01-18 17:20:06 +08:00
|
|
|
UNIMPLEMENTED();
|
2012-11-23 18:14:44 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
2011-12-02 05:40:52 +08:00
|
|
|
|
2013-04-03 15:29:53 +08:00
|
|
|
#endif // SANITIZER_LINUX
|