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 | //===----------------------------------------------------------------------===// |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 14 | #ifdef __linux__ |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 15 | |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 16 | #include "asan_interceptors.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 17 | #include "asan_internal.h" |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 +0000 | [diff] [blame] | 18 | #include "asan_lock.h" |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 19 | #include "asan_thread.h" |
Kostya Serebryany | ee92877 | 2012-08-28 13:25:55 +0000 | [diff] [blame] | 20 | #include "asan_thread_registry.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 | be01398 | 2012-08-27 11:15:55 +0000 | [diff] [blame] | 35 | #if !ASAN_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 | be01398 | 2012-08-27 11:15:55 +0000 | [diff] [blame] | 54 | #if ASAN_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; |
| 61 | # elif defined(__x86_64__) |
| 62 | ucontext_t *ucontext = (ucontext_t*)context; |
| 63 | *pc = ucontext->uc_mcontext.gregs[REG_RIP]; |
| 64 | *bp = ucontext->uc_mcontext.gregs[REG_RBP]; |
| 65 | *sp = ucontext->uc_mcontext.gregs[REG_RSP]; |
| 66 | # elif defined(__i386__) |
| 67 | ucontext_t *ucontext = (ucontext_t*)context; |
| 68 | *pc = ucontext->uc_mcontext.gregs[REG_EIP]; |
| 69 | *bp = ucontext->uc_mcontext.gregs[REG_EBP]; |
| 70 | *sp = ucontext->uc_mcontext.gregs[REG_ESP]; |
Kostya Serebryany | 46de580 | 2012-11-20 07:00:42 +0000 | [diff] [blame] | 71 | # elif defined(__powerpc__) || defined(__powerpc64__) |
| 72 | ucontext_t *ucontext = (ucontext_t*)context; |
| 73 | *pc = ucontext->uc_mcontext.regs->nip; |
| 74 | *sp = ucontext->uc_mcontext.regs->gpr[PT_R1]; |
| 75 | // The powerpc{,64}-linux ABIs do not specify r31 as the frame |
| 76 | // pointer, but GCC always uses r31 when we need a frame pointer. |
| 77 | *bp = ucontext->uc_mcontext.regs->gpr[PT_R31]; |
Dmitry Vyukov | 4ee90c23 | 2012-11-16 11:26:05 +0000 | [diff] [blame] | 78 | # elif defined(__sparc__) |
| 79 | ucontext_t *ucontext = (ucontext_t*)context; |
| 80 | uptr *stk_ptr; |
| 81 | # if defined (__arch64__) |
| 82 | *pc = ucontext->uc_mcontext.mc_gregs[MC_PC]; |
| 83 | *sp = ucontext->uc_mcontext.mc_gregs[MC_O6]; |
| 84 | stk_ptr = (uptr *) (*sp + 2047); |
| 85 | *bp = stk_ptr[15]; |
| 86 | # else |
| 87 | *pc = ucontext->uc_mcontext.gregs[REG_PC]; |
| 88 | *sp = ucontext->uc_mcontext.gregs[REG_O6]; |
| 89 | stk_ptr = (uptr *) *sp; |
| 90 | *bp = stk_ptr[15]; |
| 91 | # endif |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 92 | #else |
| 93 | # error "Unsupported arch" |
| 94 | #endif |
| 95 | } |
| 96 | |
Kostya Serebryany | 9fd01e5 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 97 | bool AsanInterceptsSignal(int signum) { |
Alexey Samsonov | 34efb8e | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 98 | return signum == SIGSEGV && flags()->handle_segv; |
Kostya Serebryany | 9fd01e5 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Alexander Potapenko | 51e6488 | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 101 | void AsanPlatformThreadInit() { |
| 102 | // Nothing here for now. |
| 103 | } |
| 104 | |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 +0000 | [diff] [blame] | 105 | AsanLock::AsanLock(LinkerInitialized) { |
| 106 | // We assume that pthread_mutex_t initialized to all zeroes is a valid |
| 107 | // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers |
| 108 | // a gcc warning: |
| 109 | // extended initializer lists only available with -std=c++0x or -std=gnu++0x |
| 110 | } |
| 111 | |
| 112 | void AsanLock::Lock() { |
| 113 | CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_)); |
| 114 | pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_); |
| 115 | CHECK(!owner_); |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 116 | owner_ = (uptr)pthread_self(); |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void AsanLock::Unlock() { |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 120 | CHECK(owner_ == (uptr)pthread_self()); |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 +0000 | [diff] [blame] | 121 | owner_ = 0; |
| 122 | pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_); |
| 123 | } |
| 124 | |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 125 | #ifdef __arm__ |
| 126 | #define UNWIND_STOP _URC_END_OF_STACK |
| 127 | #define UNWIND_CONTINUE _URC_NO_REASON |
| 128 | #else |
| 129 | #define UNWIND_STOP _URC_NORMAL_STOP |
| 130 | #define UNWIND_CONTINUE _URC_NO_REASON |
| 131 | #endif |
| 132 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 133 | uptr Unwind_GetIP(struct _Unwind_Context *ctx) { |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 134 | #ifdef __arm__ |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 135 | uptr val; |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 136 | _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE, |
| 137 | 15 /* r15 = PC */, _UVRSD_UINT32, &val); |
| 138 | CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed"); |
| 139 | // Clear the Thumb bit. |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 140 | return val & ~(uptr)1; |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 141 | #else |
| 142 | return _Unwind_GetIP(ctx); |
| 143 | #endif |
| 144 | } |
| 145 | |
| 146 | _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, |
| 147 | void *param) { |
Kostya Serebryany | 6b0d775 | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 148 | StackTrace *b = (StackTrace*)param; |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 149 | CHECK(b->size < b->max_size); |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 150 | uptr pc = Unwind_GetIP(ctx); |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 151 | b->trace[b->size++] = pc; |
| 152 | if (b->size == b->max_size) return UNWIND_STOP; |
| 153 | return UNWIND_CONTINUE; |
| 154 | } |
| 155 | |
Kostya Serebryany | ee92877 | 2012-08-28 13:25:55 +0000 | [diff] [blame] | 156 | void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp) { |
| 157 | stack->size = 0; |
| 158 | stack->trace[0] = pc; |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 159 | if ((max_s) > 1) { |
Kostya Serebryany | ee92877 | 2012-08-28 13:25:55 +0000 | [diff] [blame] | 160 | stack->max_size = max_s; |
Kostya Serebryany | 214b715 | 2012-12-03 18:39:21 +0000 | [diff] [blame^] | 161 | #if defined(__arm__) || \ |
| 162 | defined(__powerpc__) || defined(__powerpc64__) || \ |
| 163 | defined(__sparc__) |
Kostya Serebryany | ee92877 | 2012-08-28 13:25:55 +0000 | [diff] [blame] | 164 | _Unwind_Backtrace(Unwind_Trace, stack); |
Kostya Serebryany | 6db8ad4 | 2012-11-20 15:15:44 +0000 | [diff] [blame] | 165 | // Pop off the two ASAN functions from the backtrace. |
| 166 | stack->PopStackFrames(2); |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 167 | #else |
Kostya Serebryany | ee92877 | 2012-08-28 13:25:55 +0000 | [diff] [blame] | 168 | if (!asan_inited) return; |
| 169 | if (AsanThread *t = asanThreadRegistry().GetCurrent()) |
| 170 | stack->FastUnwindStack(pc, bp, t->stack_top(), t->stack_bottom()); |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 171 | #endif |
| 172 | } |
| 173 | } |
| 174 | |
Alexey Samsonov | aac36b3 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 175 | #if !ASAN_ANDROID |
| 176 | void ClearShadowMemoryForContext(void *context) { |
| 177 | ucontext_t *ucp = (ucontext_t*)context; |
| 178 | uptr sp = (uptr)ucp->uc_stack.ss_sp; |
| 179 | uptr size = ucp->uc_stack.ss_size; |
| 180 | // Align to page size. |
Kostya Serebryany | f22c697 | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 181 | uptr PageSize = GetPageSizeCached(); |
| 182 | uptr bottom = sp & ~(PageSize - 1); |
Alexey Samsonov | aac36b3 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 183 | size += sp - bottom; |
Kostya Serebryany | f22c697 | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 184 | size = RoundUpTo(size, PageSize); |
Alexey Samsonov | aac36b3 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 185 | PoisonShadow(bottom, size, 0); |
| 186 | } |
| 187 | #else |
| 188 | void ClearShadowMemoryForContext(void *context) { |
| 189 | UNIMPLEMENTED(); |
| 190 | } |
| 191 | #endif |
| 192 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 193 | } // namespace __asan |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 194 | |
| 195 | #endif // __linux__ |