Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Evgeniy Stepanov | 0af6723 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | |
| 15 | #include "sanitizer_common/sanitizer_platform.h" |
| 16 | #if SANITIZER_LINUX |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 17 | |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 18 | #include "asan_interceptors.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 19 | #include "asan_internal.h" |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 20 | #include "asan_thread.h" |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_libc.h" |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_procmaps.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 23 | |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 24 | #include <sys/time.h> |
| 25 | #include <sys/resource.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 26 | #include <sys/mman.h> |
| 27 | #include <sys/syscall.h> |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <fcntl.h> |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 30 | #include <pthread.h> |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 33 | #include <unwind.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 34 | |
Evgeniy Stepanov | d3b5660 | 2013-03-19 13:54:41 +0000 | [diff] [blame] | 35 | #if !SANITIZER_ANDROID |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 36 | // FIXME: where to get ucontext on Android? |
| 37 | #include <sys/ucontext.h> |
| 38 | #endif |
| 39 | |
Evgeniy Stepanov | 4cc2631 | 2012-03-26 09:48:41 +0000 | [diff] [blame] | 40 | extern "C" void* _DYNAMIC; |
| 41 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 42 | namespace __asan { |
| 43 | |
Alexander Potapenko | fefc1e9 | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 44 | void MaybeReexec() { |
| 45 | // No need to re-exec on Linux. |
| 46 | } |
| 47 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 48 | void *AsanDoesNotSupportStaticLinkage() { |
| 49 | // This will fail to link with -static. |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 50 | return &_DYNAMIC; // defined in link.h |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 53 | void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { |
Evgeniy Stepanov | d3b5660 | 2013-03-19 13:54:41 +0000 | [diff] [blame] | 54 | #if SANITIZER_ANDROID |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 55 | *pc = *sp = *bp = 0; |
| 56 | #elif defined(__arm__) |
| 57 | ucontext_t *ucontext = (ucontext_t*)context; |
| 58 | *pc = ucontext->uc_mcontext.arm_pc; |
| 59 | *bp = ucontext->uc_mcontext.arm_fp; |
| 60 | *sp = ucontext->uc_mcontext.arm_sp; |
Kostya Serebryany | a92b07d | 2013-11-18 08:20:13 +0000 | [diff] [blame^] | 61 | # elif defined(__hppa__) |
| 62 | ucontext_t *ucontext = (ucontext_t*)context; |
| 63 | *pc = ucontext->uc_mcontext.sc_iaoq[0]; |
| 64 | /* GCC uses %r3 whenever a frame pointer is needed. */ |
| 65 | *bp = ucontext->uc_mcontext.sc_gr[3]; |
| 66 | *sp = ucontext->uc_mcontext.sc_gr[30]; |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 67 | # elif defined(__x86_64__) |
| 68 | ucontext_t *ucontext = (ucontext_t*)context; |
| 69 | *pc = ucontext->uc_mcontext.gregs[REG_RIP]; |
| 70 | *bp = ucontext->uc_mcontext.gregs[REG_RBP]; |
| 71 | *sp = ucontext->uc_mcontext.gregs[REG_RSP]; |
| 72 | # elif defined(__i386__) |
| 73 | ucontext_t *ucontext = (ucontext_t*)context; |
| 74 | *pc = ucontext->uc_mcontext.gregs[REG_EIP]; |
| 75 | *bp = ucontext->uc_mcontext.gregs[REG_EBP]; |
| 76 | *sp = ucontext->uc_mcontext.gregs[REG_ESP]; |
Kostya Serebryany | 46de580 | 2012-11-20 07:00:42 +0000 | [diff] [blame] | 77 | # elif defined(__powerpc__) || defined(__powerpc64__) |
| 78 | ucontext_t *ucontext = (ucontext_t*)context; |
| 79 | *pc = ucontext->uc_mcontext.regs->nip; |
| 80 | *sp = ucontext->uc_mcontext.regs->gpr[PT_R1]; |
| 81 | // The powerpc{,64}-linux ABIs do not specify r31 as the frame |
| 82 | // pointer, but GCC always uses r31 when we need a frame pointer. |
| 83 | *bp = ucontext->uc_mcontext.regs->gpr[PT_R31]; |
Dmitry Vyukov | 4ee90c23 | 2012-11-16 11:26:05 +0000 | [diff] [blame] | 84 | # elif defined(__sparc__) |
| 85 | ucontext_t *ucontext = (ucontext_t*)context; |
| 86 | uptr *stk_ptr; |
| 87 | # if defined (__arch64__) |
| 88 | *pc = ucontext->uc_mcontext.mc_gregs[MC_PC]; |
| 89 | *sp = ucontext->uc_mcontext.mc_gregs[MC_O6]; |
| 90 | stk_ptr = (uptr *) (*sp + 2047); |
| 91 | *bp = stk_ptr[15]; |
| 92 | # else |
| 93 | *pc = ucontext->uc_mcontext.gregs[REG_PC]; |
| 94 | *sp = ucontext->uc_mcontext.gregs[REG_O6]; |
| 95 | stk_ptr = (uptr *) *sp; |
| 96 | *bp = stk_ptr[15]; |
| 97 | # endif |
Kostya Serebryany | c1aa0e8 | 2013-06-03 14:49:25 +0000 | [diff] [blame] | 98 | # elif defined(__mips__) |
| 99 | ucontext_t *ucontext = (ucontext_t*)context; |
| 100 | *pc = ucontext->uc_mcontext.gregs[31]; |
| 101 | *bp = ucontext->uc_mcontext.gregs[30]; |
| 102 | *sp = ucontext->uc_mcontext.gregs[29]; |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 103 | #else |
| 104 | # error "Unsupported arch" |
| 105 | #endif |
| 106 | } |
| 107 | |
Kostya Serebryany | 9fd01e5 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 108 | bool AsanInterceptsSignal(int signum) { |
Alexey Samsonov | 34efb8e | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 109 | return signum == SIGSEGV && flags()->handle_segv; |
Kostya Serebryany | 9fd01e5 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Alexander Potapenko | 51e6488 | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 112 | void AsanPlatformThreadInit() { |
| 113 | // Nothing here for now. |
| 114 | } |
| 115 | |
Evgeniy Stepanov | d3b5660 | 2013-03-19 13:54:41 +0000 | [diff] [blame] | 116 | #if !SANITIZER_ANDROID |
Alexey Samsonov | 4f1885a | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 117 | void ReadContextStack(void *context, uptr *stack, uptr *ssize) { |
Alexey Samsonov | aac36b3 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 118 | ucontext_t *ucp = (ucontext_t*)context; |
Alexey Samsonov | 4f1885a | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 119 | *stack = (uptr)ucp->uc_stack.ss_sp; |
| 120 | *ssize = ucp->uc_stack.ss_size; |
Alexey Samsonov | aac36b3 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 121 | } |
| 122 | #else |
Alexey Samsonov | 4f1885a | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 123 | void ReadContextStack(void *context, uptr *stack, uptr *ssize) { |
Alexey Samsonov | 9585613 | 2013-01-18 09:20:06 +0000 | [diff] [blame] | 124 | UNIMPLEMENTED(); |
Alexey Samsonov | aac36b3 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 125 | } |
| 126 | #endif |
| 127 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 128 | } // namespace __asan |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 129 | |
Alexey Samsonov | 21cb743 | 2013-04-03 07:29:53 +0000 | [diff] [blame] | 130 | #endif // SANITIZER_LINUX |