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" |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_freebsd.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_libc.h" |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_procmaps.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 25 | |
Kostya Serebryany | c549dd7 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 26 | #include <sys/time.h> |
| 27 | #include <sys/resource.h> |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 28 | #include <sys/mman.h> |
| 29 | #include <sys/syscall.h> |
Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 30 | #include <sys/types.h> |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 31 | #include <dlfcn.h> |
Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 32 | #include <fcntl.h> |
Kostya Serebryany | c549dd7 | 2012-01-05 01:07:27 +0000 | [diff] [blame] | 33 | #include <pthread.h> |
Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 35 | #include <unistd.h> |
Evgeniy Stepanov | 9cfa194 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 36 | #include <unwind.h> |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 37 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 38 | #if SANITIZER_FREEBSD |
| 39 | #include <sys/link_elf.h> |
Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 42 | #if SANITIZER_ANDROID || SANITIZER_FREEBSD |
| 43 | #include <ucontext.h> |
Evgeniy Stepanov | aa33a50 | 2012-03-26 09:48:41 +0000 | [diff] [blame] | 44 | extern "C" void* _DYNAMIC; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 45 | #else |
| 46 | #include <sys/ucontext.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 47 | #include <link.h> |
| 48 | #endif |
| 49 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 50 | // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in |
| 51 | // 32-bit mode. |
| 52 | #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \ |
| 53 | __FreeBSD_version <= 902001 // v9.2 |
| 54 | #define ucontext_t xucontext_t |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 55 | #endif |
| 56 | |
| 57 | typedef enum { |
| 58 | ASAN_RT_VERSION_UNDEFINED = 0, |
| 59 | ASAN_RT_VERSION_DYNAMIC, |
| 60 | ASAN_RT_VERSION_STATIC, |
| 61 | } asan_rt_version_t; |
| 62 | |
| 63 | // FIXME: perhaps also store abi version here? |
| 64 | extern "C" { |
| 65 | SANITIZER_INTERFACE_ATTRIBUTE |
| 66 | asan_rt_version_t __asan_rt_version; |
| 67 | } |
Evgeniy Stepanov | aa33a50 | 2012-03-26 09:48:41 +0000 | [diff] [blame] | 68 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 69 | namespace __asan { |
| 70 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 71 | void InitializePlatformInterceptors() {} |
| 72 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 73 | void *AsanDoesNotSupportStaticLinkage() { |
| 74 | // This will fail to link with -static. |
Kostya Serebryany | efb3fa3 | 2012-01-05 23:50:34 +0000 | [diff] [blame] | 75 | return &_DYNAMIC; // defined in link.h |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Evgeniy Stepanov | 83cb787 | 2013-03-19 13:54:41 +0000 | [diff] [blame] | 78 | #if SANITIZER_ANDROID |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 79 | // FIXME: should we do anything for Android? |
| 80 | void AsanCheckDynamicRTPrereqs() {} |
| 81 | void AsanCheckIncompatibleRT() {} |
| 82 | #else |
| 83 | static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size, |
| 84 | void *data) { |
| 85 | // Continue until the first dynamic library is found |
| 86 | if (!info->dlpi_name || info->dlpi_name[0] == 0) |
| 87 | return 0; |
| 88 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 89 | // Ignore vDSO |
| 90 | if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0) |
| 91 | return 0; |
| 92 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 93 | *(const char **)data = info->dlpi_name; |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | static bool IsDynamicRTName(const char *libname) { |
| 98 | return internal_strstr(libname, "libclang_rt.asan") || |
| 99 | internal_strstr(libname, "libasan.so"); |
| 100 | } |
| 101 | |
| 102 | static void ReportIncompatibleRT() { |
| 103 | Report("Your application is linked against incompatible ASan runtimes.\n"); |
| 104 | Die(); |
| 105 | } |
| 106 | |
| 107 | void AsanCheckDynamicRTPrereqs() { |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 108 | if (!ASAN_DYNAMIC) |
| 109 | return; |
| 110 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 111 | // Ensure that dynamic RT is the first DSO in the list |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 112 | const char *first_dso_name = nullptr; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 113 | dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name); |
| 114 | if (first_dso_name && !IsDynamicRTName(first_dso_name)) { |
| 115 | Report("ASan runtime does not come first in initial library list; " |
| 116 | "you should either link runtime to your application or " |
| 117 | "manually preload it with LD_PRELOAD.\n"); |
| 118 | Die(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void AsanCheckIncompatibleRT() { |
| 123 | if (ASAN_DYNAMIC) { |
| 124 | if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) { |
| 125 | __asan_rt_version = ASAN_RT_VERSION_DYNAMIC; |
| 126 | } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) { |
| 127 | ReportIncompatibleRT(); |
| 128 | } |
| 129 | } else { |
| 130 | if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) { |
| 131 | // Ensure that dynamic runtime is not present. We should detect it |
| 132 | // as early as possible, otherwise ASan interceptors could bind to |
| 133 | // the functions in dynamic ASan runtime instead of the functions in |
| 134 | // system libraries, causing crashes later in ASan initialization. |
| 135 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
| 136 | char filename[128]; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 137 | while (proc_maps.Next(nullptr, nullptr, nullptr, filename, |
| 138 | sizeof(filename), nullptr)) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 139 | if (IsDynamicRTName(filename)) { |
| 140 | Report("Your application is linked against " |
| 141 | "incompatible ASan runtimes.\n"); |
| 142 | Die(); |
| 143 | } |
| 144 | } |
| 145 | __asan_rt_version = ASAN_RT_VERSION_STATIC; |
| 146 | } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) { |
| 147 | ReportIncompatibleRT(); |
| 148 | } |
| 149 | } |
| 150 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 151 | #endif // SANITIZER_ANDROID |
Alexander Potapenko | 75b19eb | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 152 | |
Evgeniy Stepanov | 83cb787 | 2013-03-19 13:54:41 +0000 | [diff] [blame] | 153 | #if !SANITIZER_ANDROID |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 154 | void ReadContextStack(void *context, uptr *stack, uptr *ssize) { |
Alexey Samsonov | f3950c6 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 155 | ucontext_t *ucp = (ucontext_t*)context; |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 156 | *stack = (uptr)ucp->uc_stack.ss_sp; |
| 157 | *ssize = ucp->uc_stack.ss_size; |
Alexey Samsonov | f3950c6 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 158 | } |
| 159 | #else |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 160 | void ReadContextStack(void *context, uptr *stack, uptr *ssize) { |
Alexey Samsonov | ca2849c | 2013-01-18 09:20:06 +0000 | [diff] [blame] | 161 | UNIMPLEMENTED(); |
Alexey Samsonov | f3950c6 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 162 | } |
| 163 | #endif |
| 164 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 165 | void *AsanDlSymNext(const char *sym) { |
| 166 | return dlsym(RTLD_NEXT, sym); |
| 167 | } |
| 168 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 169 | } // namespace __asan |
Kostya Serebryany | d6567c5 | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 170 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 171 | #endif // SANITIZER_FREEBSD || SANITIZER_LINUX |