From 25d6c1b3c32e7b3c6882178848eb10d882413380 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Fri, 6 Jan 2012 19:11:09 +0000 Subject: [PATCH] [asan] move more code into OS-specific files llvm-svn: 147671 --- compiler-rt/lib/asan/asan_internal.h | 1 + compiler-rt/lib/asan/asan_linux.cc | 28 ++++++++++++++++ compiler-rt/lib/asan/asan_mac.cc | 15 +++++++++ compiler-rt/lib/asan/asan_rtl.cc | 50 +++------------------------- 4 files changed, 48 insertions(+), 46 deletions(-) diff --git a/compiler-rt/lib/asan/asan_internal.h b/compiler-rt/lib/asan/asan_internal.h index 1f078fa28ae0..169ed5311009 100644 --- a/compiler-rt/lib/asan/asan_internal.h +++ b/compiler-rt/lib/asan/asan_internal.h @@ -100,6 +100,7 @@ void *AsanMmapSomewhereOrDie(size_t size, const char *where); void AsanUnmapOrDie(void *ptr, size_t size); void AsanDisableCoreDumper(); +void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp); ssize_t AsanRead(int fd, void *buf, size_t count); ssize_t AsanWrite(int fd, const void *buf, size_t count); diff --git a/compiler-rt/lib/asan/asan_linux.cc b/compiler-rt/lib/asan/asan_linux.cc index bd92617cba24..2167efa4a872 100644 --- a/compiler-rt/lib/asan/asan_linux.cc +++ b/compiler-rt/lib/asan/asan_linux.cc @@ -29,6 +29,11 @@ #include #include +#ifndef ANDROID +// FIXME: where to get ucontext on Android? +#include +#endif + namespace __asan { void *AsanDoesNotSupportStaticLinkage() { @@ -36,6 +41,29 @@ void *AsanDoesNotSupportStaticLinkage() { return &_DYNAMIC; // defined in link.h } +void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) { +#ifdef ANDROID + *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]; +#else +# error "Unsupported arch" +#endif +} + static void *asan_mmap(void *addr, size_t length, int prot, int flags, int fd, uint64_t offset) { # if __WORDSIZE == 64 diff --git a/compiler-rt/lib/asan/asan_mac.cc b/compiler-rt/lib/asan/asan_mac.cc index 6d73309c6f63..af2d0e1b9d5f 100644 --- a/compiler-rt/lib/asan/asan_mac.cc +++ b/compiler-rt/lib/asan/asan_mac.cc @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -38,6 +39,20 @@ extern dispatch_barrier_async_f_f real_dispatch_barrier_async_f; extern dispatch_group_async_f_f real_dispatch_group_async_f; extern pthread_workqueue_additem_np_f real_pthread_workqueue_additem_np; +void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) { + ucontext_t *ucontext = (ucontext_t*)context; +# if __WORDSIZE == 64 + *pc = ucontext->uc_mcontext->__ss.__rip; + *bp = ucontext->uc_mcontext->__ss.__rbp; + *sp = ucontext->uc_mcontext->__ss.__rsp; +# else + *pc = ucontext->uc_mcontext->__ss.__eip; + *bp = ucontext->uc_mcontext->__ss.__ebp; + *sp = ucontext->uc_mcontext->__ss.__esp; +# endif // __WORDSIZE +} + + // No-op. Mac does not support static linkage anyway. void *AsanDoesNotSupportStaticLinkage() { return NULL; diff --git a/compiler-rt/lib/asan/asan_rtl.cc b/compiler-rt/lib/asan/asan_rtl.cc index 67b62dab9c2e..93701db4eb3d 100644 --- a/compiler-rt/lib/asan/asan_rtl.cc +++ b/compiler-rt/lib/asan/asan_rtl.cc @@ -36,9 +36,6 @@ #include #include #include -#ifndef ANDROID -#include -#endif // must not include on Linux namespace __asan { @@ -271,45 +268,6 @@ static void DescribeAddress(uintptr_t addr, uintptr_t access_size) { } // -------------------------- Run-time entry ------------------- {{{1 -void GetPcSpBpAx(void *context, - uintptr_t *pc, uintptr_t *sp, uintptr_t *bp, uintptr_t *ax) { -#ifndef ANDROID - ucontext_t *ucontext = (ucontext_t*)context; -#endif -#ifdef __APPLE__ -# if __WORDSIZE == 64 - *pc = ucontext->uc_mcontext->__ss.__rip; - *bp = ucontext->uc_mcontext->__ss.__rbp; - *sp = ucontext->uc_mcontext->__ss.__rsp; - *ax = ucontext->uc_mcontext->__ss.__rax; -# else - *pc = ucontext->uc_mcontext->__ss.__eip; - *bp = ucontext->uc_mcontext->__ss.__ebp; - *sp = ucontext->uc_mcontext->__ss.__esp; - *ax = ucontext->uc_mcontext->__ss.__eax; -# endif // __WORDSIZE -#else // assume linux -# if defined (ANDROID) - *pc = *sp = *bp = *ax = 0; -# elif defined(__arm__) - *pc = ucontext->uc_mcontext.arm_pc; - *bp = ucontext->uc_mcontext.arm_fp; - *sp = ucontext->uc_mcontext.arm_sp; - *ax = ucontext->uc_mcontext.arm_r0; -# elif __WORDSIZE == 64 - *pc = ucontext->uc_mcontext.gregs[REG_RIP]; - *bp = ucontext->uc_mcontext.gregs[REG_RBP]; - *sp = ucontext->uc_mcontext.gregs[REG_RSP]; - *ax = ucontext->uc_mcontext.gregs[REG_RAX]; -# else - *pc = ucontext->uc_mcontext.gregs[REG_EIP]; - *bp = ucontext->uc_mcontext.gregs[REG_EBP]; - *sp = ucontext->uc_mcontext.gregs[REG_ESP]; - *ax = ucontext->uc_mcontext.gregs[REG_EAX]; -# endif // __WORDSIZE -#endif -} - static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) { uintptr_t addr = (uintptr_t)siginfo->si_addr; if (AddrIsInShadow(addr) && FLAG_lazy_shadow) { @@ -322,11 +280,11 @@ static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) { } // Write the first message using the bullet-proof write. if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) ASAN_DIE; - uintptr_t pc, sp, bp, ax; - GetPcSpBpAx(context, &pc, &sp, &bp, &ax); + uintptr_t pc, sp, bp; + GetPcSpBp(context, &pc, &sp, &bp); Report("ERROR: AddressSanitizer crashed on unknown address %p" - " (pc %p sp %p bp %p ax %p T%d)\n", - addr, pc, sp, bp, ax, + " (pc %p sp %p bp %p T%d)\n", + addr, pc, sp, bp, asanThreadRegistry().GetCurrentTidOrMinusOne()); Printf("AddressSanitizer can not provide additional info. ABORTING\n"); GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, false, pc, bp);