| Kostya Serebryany | 1e172b4 | 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 | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | |
| 15 | #include "sanitizer_common/sanitizer_platform.h" |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 16 | #if SANITIZER_FREEBSD || SANITIZER_LINUX |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 17 | |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 18 | #include "asan_interceptors.h" |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 19 | #include "asan_internal.h" |
| Kostya Serebryany | c549dd7 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 20 | #include "asan_thread.h" |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 21 | #include "sanitizer_common/sanitizer_flags.h" |
| Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_libc.h" |
| Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_procmaps.h" |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 24 | |
| Kostya Serebryany | c549dd7 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 25 | #include <sys/time.h> |
| 26 | #include <sys/resource.h> |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 27 | #include <sys/mman.h> |
| 28 | #include <sys/syscall.h> |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <fcntl.h> |
| Kostya Serebryany | c549dd7 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 31 | #include <pthread.h> |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
| Evgeniy Stepanov | 9cfa194 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 34 | #include <unwind.h> |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 35 | |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 36 | #if SANITIZER_FREEBSD |
| 37 | #include <sys/link_elf.h> |
| Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 40 | #if SANITIZER_ANDROID || SANITIZER_FREEBSD |
| 41 | #include <ucontext.h> |
| Evgeniy Stepanov | aa33a50 | 2012-03-26 09:48:41 +0000 | [diff] [blame] | 42 | extern "C" void* _DYNAMIC; |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 43 | #else |
| 44 | #include <sys/ucontext.h> |
| 45 | #include <dlfcn.h> |
| 46 | #include <link.h> |
| 47 | #endif |
| 48 | |
| 49 | // x86_64 FreeBSD 9.2 and older define 64-bit register names in both 64-bit |
| 50 | // and 32-bit modes. |
| 51 | #if SANITIZER_FREEBSD |
| 52 | #include <sys/param.h> |
| 53 | # if __FreeBSD_version <= 902001 // v9.2 |
| 54 | # define mc_eip mc_rip |
| 55 | # define mc_ebp mc_rbp |
| 56 | # define mc_esp mc_rsp |
| 57 | # endif |
| 58 | #endif |
| 59 | |
| 60 | typedef enum { |
| 61 | ASAN_RT_VERSION_UNDEFINED = 0, |
| 62 | ASAN_RT_VERSION_DYNAMIC, |
| 63 | ASAN_RT_VERSION_STATIC, |
| 64 | } asan_rt_version_t; |
| 65 | |
| 66 | // FIXME: perhaps also store abi version here? |
| 67 | extern "C" { |
| 68 | SANITIZER_INTERFACE_ATTRIBUTE |
| 69 | asan_rt_version_t __asan_rt_version; |
| 70 | } |
| Evgeniy Stepanov | aa33a50 | 2012-03-26 09:48:41 +0000 | [diff] [blame] | 71 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 72 | namespace __asan { |
| 73 | |
| Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 74 | void MaybeReexec() { |
| 75 | // No need to re-exec on Linux. |
| 76 | } |
| 77 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 78 | void *AsanDoesNotSupportStaticLinkage() { |
| 79 | // This will fail to link with -static. |
| Kostya Serebryany | efb3fa3 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 80 | return &_DYNAMIC; // defined in link.h |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| Evgeniy Stepanov | 83cb787 | 2013-03-19 13:54:41 +0000 | [diff] [blame] | 83 | #if SANITIZER_ANDROID |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 84 | // FIXME: should we do anything for Android? |
| 85 | void AsanCheckDynamicRTPrereqs() {} |
| 86 | void AsanCheckIncompatibleRT() {} |
| 87 | #else |
| 88 | static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size, |
| 89 | void *data) { |
| 90 | // Continue until the first dynamic library is found |
| 91 | if (!info->dlpi_name || info->dlpi_name[0] == 0) |
| 92 | return 0; |
| 93 | |
| 94 | *(const char **)data = info->dlpi_name; |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | static bool IsDynamicRTName(const char *libname) { |
| 99 | return internal_strstr(libname, "libclang_rt.asan") || |
| 100 | internal_strstr(libname, "libasan.so"); |
| 101 | } |
| 102 | |
| 103 | static void ReportIncompatibleRT() { |
| 104 | Report("Your application is linked against incompatible ASan runtimes.\n"); |
| 105 | Die(); |
| 106 | } |
| 107 | |
| 108 | void AsanCheckDynamicRTPrereqs() { |
| 109 | // Ensure that dynamic RT is the first DSO in the list |
| 110 | const char *first_dso_name = 0; |
| 111 | dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name); |
| 112 | if (first_dso_name && !IsDynamicRTName(first_dso_name)) { |
| 113 | Report("ASan runtime does not come first in initial library list; " |
| 114 | "you should either link runtime to your application or " |
| 115 | "manually preload it with LD_PRELOAD.\n"); |
| 116 | Die(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void AsanCheckIncompatibleRT() { |
| 121 | if (ASAN_DYNAMIC) { |
| 122 | if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) { |
| 123 | __asan_rt_version = ASAN_RT_VERSION_DYNAMIC; |
| 124 | } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) { |
| 125 | ReportIncompatibleRT(); |
| 126 | } |
| 127 | } else { |
| 128 | if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) { |
| 129 | // Ensure that dynamic runtime is not present. We should detect it |
| 130 | // as early as possible, otherwise ASan interceptors could bind to |
| 131 | // the functions in dynamic ASan runtime instead of the functions in |
| 132 | // system libraries, causing crashes later in ASan initialization. |
| 133 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
| 134 | char filename[128]; |
| 135 | while (proc_maps.Next(0, 0, 0, filename, sizeof(filename), 0)) { |
| 136 | if (IsDynamicRTName(filename)) { |
| 137 | Report("Your application is linked against " |
| 138 | "incompatible ASan runtimes.\n"); |
| 139 | Die(); |
| 140 | } |
| 141 | } |
| 142 | __asan_rt_version = ASAN_RT_VERSION_STATIC; |
| 143 | } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) { |
| 144 | ReportIncompatibleRT(); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | #endif // SANITIZER_ANDROID |
| 149 | |
| 150 | void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { |
| 151 | #if defined(__arm__) |
| Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 152 | ucontext_t *ucontext = (ucontext_t*)context; |
| 153 | *pc = ucontext->uc_mcontext.arm_pc; |
| 154 | *bp = ucontext->uc_mcontext.arm_fp; |
| 155 | *sp = ucontext->uc_mcontext.arm_sp; |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 156 | #elif defined(__aarch64__) |
| 157 | ucontext_t *ucontext = (ucontext_t*)context; |
| 158 | *pc = ucontext->uc_mcontext.pc; |
| 159 | *bp = ucontext->uc_mcontext.regs[29]; |
| 160 | *sp = ucontext->uc_mcontext.sp; |
| 161 | #elif defined(__hppa__) |
| Kostya Serebryany | 8b0c8a8 | 2013-11-18 08:20:13 +0000 | [diff] [blame] | 162 | ucontext_t *ucontext = (ucontext_t*)context; |
| 163 | *pc = ucontext->uc_mcontext.sc_iaoq[0]; |
| 164 | /* GCC uses %r3 whenever a frame pointer is needed. */ |
| 165 | *bp = ucontext->uc_mcontext.sc_gr[3]; |
| 166 | *sp = ucontext->uc_mcontext.sc_gr[30]; |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 167 | #elif defined(__x86_64__) |
| 168 | # if SANITIZER_FREEBSD |
| 169 | ucontext_t *ucontext = (ucontext_t*)context; |
| 170 | *pc = ucontext->uc_mcontext.mc_rip; |
| 171 | *bp = ucontext->uc_mcontext.mc_rbp; |
| 172 | *sp = ucontext->uc_mcontext.mc_rsp; |
| 173 | # else |
| Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 174 | ucontext_t *ucontext = (ucontext_t*)context; |
| 175 | *pc = ucontext->uc_mcontext.gregs[REG_RIP]; |
| 176 | *bp = ucontext->uc_mcontext.gregs[REG_RBP]; |
| 177 | *sp = ucontext->uc_mcontext.gregs[REG_RSP]; |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 178 | # endif |
| 179 | #elif defined(__i386__) |
| 180 | # if SANITIZER_FREEBSD |
| 181 | ucontext_t *ucontext = (ucontext_t*)context; |
| 182 | *pc = ucontext->uc_mcontext.mc_eip; |
| 183 | *bp = ucontext->uc_mcontext.mc_ebp; |
| 184 | *sp = ucontext->uc_mcontext.mc_esp; |
| 185 | # else |
| Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 186 | ucontext_t *ucontext = (ucontext_t*)context; |
| 187 | *pc = ucontext->uc_mcontext.gregs[REG_EIP]; |
| 188 | *bp = ucontext->uc_mcontext.gregs[REG_EBP]; |
| 189 | *sp = ucontext->uc_mcontext.gregs[REG_ESP]; |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 190 | # endif |
| 191 | #elif defined(__sparc__) |
| Dmitry Vyukov | 16da794 | 2012-11-16 11:26:05 +0000 | [diff] [blame] | 192 | ucontext_t *ucontext = (ucontext_t*)context; |
| 193 | uptr *stk_ptr; |
| 194 | # if defined (__arch64__) |
| 195 | *pc = ucontext->uc_mcontext.mc_gregs[MC_PC]; |
| 196 | *sp = ucontext->uc_mcontext.mc_gregs[MC_O6]; |
| 197 | stk_ptr = (uptr *) (*sp + 2047); |
| 198 | *bp = stk_ptr[15]; |
| 199 | # else |
| 200 | *pc = ucontext->uc_mcontext.gregs[REG_PC]; |
| 201 | *sp = ucontext->uc_mcontext.gregs[REG_O6]; |
| 202 | stk_ptr = (uptr *) *sp; |
| 203 | *bp = stk_ptr[15]; |
| 204 | # endif |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 205 | #elif defined(__mips__) |
| Kostya Serebryany | 40527a5 | 2013-06-03 14:49:25 +0000 | [diff] [blame] | 206 | ucontext_t *ucontext = (ucontext_t*)context; |
| 207 | *pc = ucontext->uc_mcontext.gregs[31]; |
| 208 | *bp = ucontext->uc_mcontext.gregs[30]; |
| 209 | *sp = ucontext->uc_mcontext.gregs[29]; |
| Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 210 | #else |
| 211 | # error "Unsupported arch" |
| 212 | #endif |
| 213 | } |
| 214 | |
| Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 215 | bool AsanInterceptsSignal(int signum) { |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 216 | return signum == SIGSEGV && common_flags()->handle_segv; |
| Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| Alexander Potapenko | 75b19eb | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 219 | void AsanPlatformThreadInit() { |
| 220 | // Nothing here for now. |
| 221 | } |
| 222 | |
| Evgeniy Stepanov | 83cb787 | 2013-03-19 13:54:41 +0000 | [diff] [blame] | 223 | #if !SANITIZER_ANDROID |
| Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 224 | void ReadContextStack(void *context, uptr *stack, uptr *ssize) { |
| Alexey Samsonov | f3950c6 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 225 | ucontext_t *ucp = (ucontext_t*)context; |
| Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 226 | *stack = (uptr)ucp->uc_stack.ss_sp; |
| 227 | *ssize = ucp->uc_stack.ss_size; |
| Alexey Samsonov | f3950c6 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 228 | } |
| 229 | #else |
| Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 230 | void ReadContextStack(void *context, uptr *stack, uptr *ssize) { |
| Alexey Samsonov | ca2849c | 2013-01-18 09:20:06 +0000 | [diff] [blame] | 231 | UNIMPLEMENTED(); |
| Alexey Samsonov | f3950c6 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 232 | } |
| 233 | #endif |
| 234 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 235 | } // namespace __asan |
| Kostya Serebryany | d6567c5 | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 236 | |
| Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 237 | #endif // SANITIZER_FREEBSD || SANITIZER_LINUX |