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" |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_libc.h" |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_procmaps.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 22 | |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 23 | #include <sys/time.h> |
| 24 | #include <sys/resource.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 25 | #include <sys/mman.h> |
| 26 | #include <sys/syscall.h> |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <fcntl.h> |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 29 | #include <pthread.h> |
Kostya Serebryany | cd271f5 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 32 | #include <unwind.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 33 | |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 34 | #ifndef ANDROID |
| 35 | // FIXME: where to get ucontext on Android? |
| 36 | #include <sys/ucontext.h> |
| 37 | #endif |
| 38 | |
Evgeniy Stepanov | 4cc2631 | 2012-03-26 09:48:41 +0000 | [diff] [blame] | 39 | extern "C" void* _DYNAMIC; |
| 40 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 41 | namespace __asan { |
| 42 | |
| 43 | void *AsanDoesNotSupportStaticLinkage() { |
| 44 | // This will fail to link with -static. |
Kostya Serebryany | 3b7fb10 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 45 | return &_DYNAMIC; // defined in link.h |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 48 | void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 49 | #ifdef ANDROID |
| 50 | *pc = *sp = *bp = 0; |
| 51 | #elif defined(__arm__) |
| 52 | ucontext_t *ucontext = (ucontext_t*)context; |
| 53 | *pc = ucontext->uc_mcontext.arm_pc; |
| 54 | *bp = ucontext->uc_mcontext.arm_fp; |
| 55 | *sp = ucontext->uc_mcontext.arm_sp; |
| 56 | # elif defined(__x86_64__) |
| 57 | ucontext_t *ucontext = (ucontext_t*)context; |
| 58 | *pc = ucontext->uc_mcontext.gregs[REG_RIP]; |
| 59 | *bp = ucontext->uc_mcontext.gregs[REG_RBP]; |
| 60 | *sp = ucontext->uc_mcontext.gregs[REG_RSP]; |
| 61 | # elif defined(__i386__) |
| 62 | ucontext_t *ucontext = (ucontext_t*)context; |
| 63 | *pc = ucontext->uc_mcontext.gregs[REG_EIP]; |
| 64 | *bp = ucontext->uc_mcontext.gregs[REG_EBP]; |
| 65 | *sp = ucontext->uc_mcontext.gregs[REG_ESP]; |
| 66 | #else |
| 67 | # error "Unsupported arch" |
| 68 | #endif |
| 69 | } |
| 70 | |
Kostya Serebryany | 9fd01e5 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 71 | bool AsanInterceptsSignal(int signum) { |
| 72 | return signum == SIGSEGV && FLAG_handle_segv; |
| 73 | } |
| 74 | |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 +0000 | [diff] [blame] | 75 | AsanLock::AsanLock(LinkerInitialized) { |
| 76 | // We assume that pthread_mutex_t initialized to all zeroes is a valid |
| 77 | // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers |
| 78 | // a gcc warning: |
| 79 | // extended initializer lists only available with -std=c++0x or -std=gnu++0x |
| 80 | } |
| 81 | |
| 82 | void AsanLock::Lock() { |
| 83 | CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_)); |
| 84 | pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_); |
| 85 | CHECK(!owner_); |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 86 | owner_ = (uptr)pthread_self(); |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void AsanLock::Unlock() { |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 90 | CHECK(owner_ == (uptr)pthread_self()); |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 +0000 | [diff] [blame] | 91 | owner_ = 0; |
| 92 | pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_); |
| 93 | } |
| 94 | |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 95 | #ifdef __arm__ |
| 96 | #define UNWIND_STOP _URC_END_OF_STACK |
| 97 | #define UNWIND_CONTINUE _URC_NO_REASON |
| 98 | #else |
| 99 | #define UNWIND_STOP _URC_NORMAL_STOP |
| 100 | #define UNWIND_CONTINUE _URC_NO_REASON |
| 101 | #endif |
| 102 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 103 | uptr Unwind_GetIP(struct _Unwind_Context *ctx) { |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 104 | #ifdef __arm__ |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 105 | uptr val; |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 106 | _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE, |
| 107 | 15 /* r15 = PC */, _UVRSD_UINT32, &val); |
| 108 | CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed"); |
| 109 | // Clear the Thumb bit. |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 110 | return val & ~(uptr)1; |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 111 | #else |
| 112 | return _Unwind_GetIP(ctx); |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, |
| 117 | void *param) { |
| 118 | AsanStackTrace *b = (AsanStackTrace*)param; |
| 119 | CHECK(b->size < b->max_size); |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 120 | uptr pc = Unwind_GetIP(ctx); |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 121 | b->trace[b->size++] = pc; |
| 122 | if (b->size == b->max_size) return UNWIND_STOP; |
| 123 | return UNWIND_CONTINUE; |
| 124 | } |
| 125 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 126 | void AsanStackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) { |
Evgeniy Stepanov | 84c44a8 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 127 | size = 0; |
| 128 | trace[0] = pc; |
| 129 | if ((max_s) > 1) { |
| 130 | max_size = max_s; |
| 131 | #ifdef __arm__ |
| 132 | _Unwind_Backtrace(Unwind_Trace, this); |
| 133 | #else |
| 134 | FastUnwindStack(pc, bp); |
| 135 | #endif |
| 136 | } |
| 137 | } |
| 138 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 139 | } // namespace __asan |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 140 | |
| 141 | #endif // __linux__ |