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"
|
2014-03-06 17:05:52 +08:00
|
|
|
#if SANITIZER_FREEBSD || 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"
|
2014-03-04 21:12:25 +08:00
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
2014-06-15 21:56:28 +08:00
|
|
|
#include "sanitizer_common/sanitizer_freebsd.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>
|
2014-06-06 18:57:21 +08:00
|
|
|
#include <dlfcn.h>
|
2011-12-29 06:58:01 +08:00
|
|
|
#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
|
|
|
|
2014-04-09 21:37:19 +08:00
|
|
|
#if SANITIZER_FREEBSD
|
|
|
|
#include <sys/link_elf.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SANITIZER_ANDROID || SANITIZER_FREEBSD
|
2014-02-10 21:34:43 +08:00
|
|
|
#include <ucontext.h>
|
2014-04-01 21:16:30 +08:00
|
|
|
extern "C" void* _DYNAMIC;
|
2014-02-10 21:34:43 +08:00
|
|
|
#else
|
2012-01-07 03:11:09 +08:00
|
|
|
#include <sys/ucontext.h>
|
2014-04-01 21:16:30 +08:00
|
|
|
#include <link.h>
|
2012-01-07 03:11:09 +08:00
|
|
|
#endif
|
|
|
|
|
2014-06-15 21:56:28 +08:00
|
|
|
// x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
|
|
|
|
// 32-bit mode.
|
|
|
|
#if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
|
|
|
|
__FreeBSD_version <= 902001 // v9.2
|
|
|
|
#define ucontext_t xucontext_t
|
2014-03-12 20:44:36 +08:00
|
|
|
#endif
|
|
|
|
|
2014-04-01 21:16:30 +08:00
|
|
|
typedef enum {
|
|
|
|
ASAN_RT_VERSION_UNDEFINED = 0,
|
|
|
|
ASAN_RT_VERSION_DYNAMIC,
|
|
|
|
ASAN_RT_VERSION_STATIC,
|
|
|
|
} asan_rt_version_t;
|
|
|
|
|
|
|
|
// FIXME: perhaps also store abi version here?
|
|
|
|
extern "C" {
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
asan_rt_version_t __asan_rt_version;
|
|
|
|
}
|
2012-03-26 17:48:41 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-04-02 17:36:36 +08:00
|
|
|
#if SANITIZER_ANDROID
|
|
|
|
// FIXME: should we do anything for Android?
|
|
|
|
void AsanCheckDynamicRTPrereqs() {}
|
|
|
|
void AsanCheckIncompatibleRT() {}
|
|
|
|
#else
|
2014-04-01 21:16:30 +08:00
|
|
|
static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
|
|
|
|
void *data) {
|
|
|
|
// Continue until the first dynamic library is found
|
|
|
|
if (!info->dlpi_name || info->dlpi_name[0] == 0)
|
|
|
|
return 0;
|
|
|
|
|
2014-06-02 18:39:40 +08:00
|
|
|
// Ignore vDSO
|
|
|
|
if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
|
|
|
|
return 0;
|
|
|
|
|
2014-04-01 21:16:30 +08:00
|
|
|
*(const char **)data = info->dlpi_name;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsDynamicRTName(const char *libname) {
|
|
|
|
return internal_strstr(libname, "libclang_rt.asan") ||
|
|
|
|
internal_strstr(libname, "libasan.so");
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:09:22 +08:00
|
|
|
static void ReportIncompatibleRT() {
|
|
|
|
Report("Your application is linked against incompatible ASan runtimes.\n");
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:16:30 +08:00
|
|
|
void AsanCheckDynamicRTPrereqs() {
|
|
|
|
// Ensure that dynamic RT is the first DSO in the list
|
|
|
|
const char *first_dso_name = 0;
|
|
|
|
dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
|
|
|
|
if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
|
|
|
|
Report("ASan runtime does not come first in initial library list; "
|
|
|
|
"you should either link runtime to your application or "
|
|
|
|
"manually preload it with LD_PRELOAD.\n");
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsanCheckIncompatibleRT() {
|
|
|
|
if (ASAN_DYNAMIC) {
|
|
|
|
if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
|
|
|
|
__asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
|
|
|
|
} else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
|
2014-04-02 21:09:22 +08:00
|
|
|
ReportIncompatibleRT();
|
2014-04-01 21:16:30 +08:00
|
|
|
}
|
|
|
|
} else {
|
2014-04-02 21:09:22 +08:00
|
|
|
if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
|
|
|
|
// Ensure that dynamic runtime is not present. We should detect it
|
|
|
|
// as early as possible, otherwise ASan interceptors could bind to
|
|
|
|
// the functions in dynamic ASan runtime instead of the functions in
|
|
|
|
// system libraries, causing crashes later in ASan initialization.
|
|
|
|
MemoryMappingLayout proc_maps(/*cache_enabled*/true);
|
|
|
|
char filename[128];
|
|
|
|
while (proc_maps.Next(0, 0, 0, filename, sizeof(filename), 0)) {
|
|
|
|
if (IsDynamicRTName(filename)) {
|
|
|
|
Report("Your application is linked against "
|
|
|
|
"incompatible ASan runtimes.\n");
|
|
|
|
Die();
|
|
|
|
}
|
2014-04-01 21:16:30 +08:00
|
|
|
}
|
2014-04-02 21:09:22 +08:00
|
|
|
__asan_rt_version = ASAN_RT_VERSION_STATIC;
|
|
|
|
} else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
|
|
|
|
ReportIncompatibleRT();
|
2014-04-01 21:16:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 17:36:36 +08:00
|
|
|
#endif // SANITIZER_ANDROID
|
2014-04-01 21:16:30 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
|
2014-02-10 21:34:43 +08:00
|
|
|
#if defined(__arm__)
|
2012-01-07 03:11:09 +08:00
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
*pc = ucontext->uc_mcontext.arm_pc;
|
|
|
|
*bp = ucontext->uc_mcontext.arm_fp;
|
|
|
|
*sp = ucontext->uc_mcontext.arm_sp;
|
2014-03-06 17:05:52 +08:00
|
|
|
#elif defined(__aarch64__)
|
2014-02-13 15:50:20 +08:00
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
*pc = ucontext->uc_mcontext.pc;
|
|
|
|
*bp = ucontext->uc_mcontext.regs[29];
|
|
|
|
*sp = ucontext->uc_mcontext.sp;
|
2014-03-06 17:05:52 +08:00
|
|
|
#elif defined(__hppa__)
|
2013-11-18 16:20:13 +08:00
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
*pc = ucontext->uc_mcontext.sc_iaoq[0];
|
|
|
|
/* GCC uses %r3 whenever a frame pointer is needed. */
|
|
|
|
*bp = ucontext->uc_mcontext.sc_gr[3];
|
|
|
|
*sp = ucontext->uc_mcontext.sc_gr[30];
|
2014-03-06 17:05:52 +08:00
|
|
|
#elif defined(__x86_64__)
|
|
|
|
# if SANITIZER_FREEBSD
|
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
*pc = ucontext->uc_mcontext.mc_rip;
|
|
|
|
*bp = ucontext->uc_mcontext.mc_rbp;
|
|
|
|
*sp = ucontext->uc_mcontext.mc_rsp;
|
|
|
|
# else
|
2012-01-07 03:11:09 +08:00
|
|
|
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];
|
2014-03-06 17:05:52 +08:00
|
|
|
# endif
|
|
|
|
#elif defined(__i386__)
|
|
|
|
# if SANITIZER_FREEBSD
|
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
|
|
|
*pc = ucontext->uc_mcontext.mc_eip;
|
|
|
|
*bp = ucontext->uc_mcontext.mc_ebp;
|
|
|
|
*sp = ucontext->uc_mcontext.mc_esp;
|
|
|
|
# else
|
2012-01-07 03:11:09 +08:00
|
|
|
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];
|
2014-03-06 17:05:52 +08:00
|
|
|
# endif
|
2014-05-30 16:52:03 +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];
|
2014-03-06 17:05:52 +08:00
|
|
|
#elif defined(__sparc__)
|
2012-11-16 19:26:05 +08:00
|
|
|
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
|
2014-03-06 17:05:52 +08:00
|
|
|
#elif defined(__mips__)
|
2013-06-03 22:49:25 +08:00
|
|
|
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) {
|
2014-01-28 17:28:57 +08:00
|
|
|
return signum == SIGSEGV && common_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
|
|
|
|
|
2014-06-06 18:57:21 +08:00
|
|
|
void *AsanDlSymNext(const char *sym) {
|
|
|
|
return dlsym(RTLD_NEXT, sym);
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
2011-12-02 05:40:52 +08:00
|
|
|
|
2014-03-06 17:05:52 +08:00
|
|
|
#endif // SANITIZER_FREEBSD || SANITIZER_LINUX
|