Alexey Samsonov | e5f5895 | 2012-06-04 13:50:10 +0000 | [diff] [blame] | 1 | //===-- asan_interceptors.cc ----------------------------------------------===// |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 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 | // |
Kostya Serebryany | d47189c | 2012-01-11 02:32:40 +0000 | [diff] [blame] | 12 | // Intercept various libc functions. |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "asan_interceptors.h" |
| 15 | |
| 16 | #include "asan_allocator.h" |
Alexander Potapenko | 05bf9a5 | 2012-08-15 15:24:48 +0000 | [diff] [blame] | 17 | #include "asan_intercepted_functions.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 18 | #include "asan_internal.h" |
| 19 | #include "asan_mapping.h" |
Alexey Samsonov | 7e84349 | 2013-03-28 15:42:43 +0000 | [diff] [blame] | 20 | #include "asan_poisoning.h" |
Alexey Samsonov | 487fee7 | 2012-08-09 08:32:33 +0000 | [diff] [blame] | 21 | #include "asan_report.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 22 | #include "asan_stack.h" |
| 23 | #include "asan_stats.h" |
Alexey Samsonov | 5b29018 | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 24 | #include "interception/interception.h" |
Alexey Samsonov | c0d78c1 | 2012-06-04 13:27:49 +0000 | [diff] [blame] | 25 | #include "sanitizer_common/sanitizer_libc.h" |
| 26 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 27 | namespace __asan { |
| 28 | |
Kostya Serebryany | a84805f | 2013-02-21 07:07:39 +0000 | [diff] [blame] | 29 | // Return true if we can quickly decide that the region is unpoisoned. |
| 30 | static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) { |
| 31 | if (size == 0) return true; |
| 32 | if (size <= 32) |
| 33 | return !AddressIsPoisoned(beg) && |
| 34 | !AddressIsPoisoned(beg + size - 1) && |
| 35 | !AddressIsPoisoned(beg + size / 2); |
| 36 | return false; |
| 37 | } |
| 38 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 39 | // We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE, |
| 40 | // and ASAN_WRITE_RANGE as macro instead of function so |
| 41 | // that no extra frames are created, and stack trace contains |
| 42 | // relevant information only. |
Kostya Serebryany | eb28093 | 2012-12-28 15:24:16 +0000 | [diff] [blame] | 43 | // We check all shadow bytes. |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 44 | #define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \ |
| 45 | uptr __offset = (uptr)(offset); \ |
| 46 | uptr __size = (uptr)(size); \ |
Kostya Serebryany | 1b057b2 | 2013-02-26 07:25:18 +0000 | [diff] [blame] | 47 | uptr __bad = 0; \ |
Kostya Serebryany | a84805f | 2013-02-21 07:07:39 +0000 | [diff] [blame] | 48 | if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \ |
Kostya Serebryany | 1b057b2 | 2013-02-26 07:25:18 +0000 | [diff] [blame] | 49 | (__bad = __asan_region_is_poisoned(__offset, __size))) { \ |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 50 | GET_CURRENT_PC_BP_SP; \ |
Kostya Serebryany | 1b057b2 | 2013-02-26 07:25:18 +0000 | [diff] [blame] | 51 | __asan_report_error(pc, bp, sp, __bad, isWrite, __size); \ |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 52 | } \ |
| 53 | } while (0) |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 54 | |
Kostya Serebryany | eb28093 | 2012-12-28 15:24:16 +0000 | [diff] [blame] | 55 | #define ASAN_READ_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, false) |
| 56 | #define ASAN_WRITE_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, true); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 57 | |
| 58 | // Behavior of functions like "memcpy" or "strcpy" is undefined |
| 59 | // if memory intervals overlap. We report error in this case. |
| 60 | // Macro is used to avoid creation of new frames. |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 61 | static inline bool RangesOverlap(const char *offset1, uptr length1, |
| 62 | const char *offset2, uptr length2) { |
Kostya Serebryany | 0985ca2 | 2011-12-28 19:08:49 +0000 | [diff] [blame] | 63 | return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1)); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 64 | } |
Kostya Serebryany | c5e72a3 | 2011-12-28 19:24:31 +0000 | [diff] [blame] | 65 | #define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \ |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 66 | const char *offset1 = (const char*)_offset1; \ |
| 67 | const char *offset2 = (const char*)_offset2; \ |
Kostya Serebryany | 0985ca2 | 2011-12-28 19:08:49 +0000 | [diff] [blame] | 68 | if (RangesOverlap(offset1, length1, offset2, length2)) { \ |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 69 | GET_STACK_TRACE_FATAL_HERE; \ |
Alexey Samsonov | 487fee7 | 2012-08-09 08:32:33 +0000 | [diff] [blame] | 70 | ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \ |
| 71 | offset2, length2, &stack); \ |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 72 | } \ |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 73 | } while (0) |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 74 | |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 75 | #define ENSURE_ASAN_INITED() do { \ |
| 76 | CHECK(!asan_init_is_running); \ |
| 77 | if (!asan_inited) { \ |
| 78 | __asan_init(); \ |
| 79 | } \ |
| 80 | } while (0) |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 81 | |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 82 | static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) { |
Alexey Samsonov | 81a7a4a | 2012-03-24 09:10:50 +0000 | [diff] [blame] | 83 | #if ASAN_INTERCEPT_STRNLEN |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 84 | if (REAL(strnlen) != 0) { |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 85 | return REAL(strnlen)(s, maxlen); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 86 | } |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 87 | #endif |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 88 | return internal_strnlen(s, maxlen); |
Kostya Serebryany | a4ccf87 | 2012-01-09 22:20:49 +0000 | [diff] [blame] | 89 | } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 90 | |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 91 | void SetThreadName(const char *name) { |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 92 | AsanThread *t = GetCurrentThread(); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 93 | if (t) |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 94 | asanThreadRegistry().SetThreadName(t->tid(), name); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Alexey Samsonov | bdd0966 | 2013-04-23 13:57:35 +0000 | [diff] [blame] | 97 | static void DisableStrictInitOrderChecker() { |
| 98 | if (flags()->strict_init_order) |
| 99 | flags()->check_initialization_order = false; |
| 100 | } |
| 101 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 102 | } // namespace __asan |
| 103 | |
| 104 | // ---------------------- Wrappers ---------------- {{{1 |
| 105 | using namespace __asan; // NOLINT |
| 106 | |
Evgeniy Stepanov | 82a9080 | 2013-01-18 13:12:56 +0000 | [diff] [blame] | 107 | #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \ |
| 108 | ASAN_WRITE_RANGE(ptr, size) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 109 | #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) ASAN_READ_RANGE(ptr, size) |
Evgeniy Stepanov | 9d1525e | 2013-05-29 09:09:58 +0000 | [diff] [blame] | 110 | #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ |
| 111 | do { \ |
| 112 | if (asan_init_is_running) return REAL(func)(__VA_ARGS__); \ |
| 113 | ctx = 0; \ |
| 114 | (void) ctx; \ |
| 115 | ENSURE_ASAN_INITED(); \ |
Evgeniy Stepanov | 82a9080 | 2013-01-18 13:12:56 +0000 | [diff] [blame] | 116 | } while (false) |
Evgeniy Stepanov | 9d1525e | 2013-05-29 09:09:58 +0000 | [diff] [blame] | 117 | #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \ |
| 118 | do { \ |
| 119 | } while (false) |
| 120 | #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \ |
| 121 | do { \ |
| 122 | } while (false) |
| 123 | #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \ |
| 124 | do { \ |
| 125 | } while (false) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 126 | #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name) |
Evgeniy Stepanov | 4f32c0b | 2013-01-18 13:01:18 +0000 | [diff] [blame] | 127 | #include "sanitizer_common/sanitizer_common_interceptors.inc" |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 128 | |
Evgeniy Stepanov | 881b677 | 2013-04-12 14:57:03 +0000 | [diff] [blame] | 129 | #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(p, s) |
| 130 | #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(p, s) |
| 131 | #define COMMON_SYSCALL_POST_READ_RANGE(p, s) |
| 132 | #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) |
| 133 | #include "sanitizer_common/sanitizer_common_syscalls.inc" |
| 134 | |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 135 | static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) { |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 136 | AsanThread *t = (AsanThread*)arg; |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 137 | SetCurrentThread(t); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 138 | return t->ThreadStart(GetTid()); |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 141 | #if ASAN_INTERCEPT_PTHREAD_CREATE |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 142 | extern "C" int pthread_attr_getdetachstate(void *attr, int *v); |
| 143 | |
Evgeniy Stepanov | b8ef925 | 2012-02-22 12:31:25 +0000 | [diff] [blame] | 144 | INTERCEPTOR(int, pthread_create, void *thread, |
| 145 | void *attr, void *(*start_routine)(void*), void *arg) { |
Alexey Samsonov | bdd0966 | 2013-04-23 13:57:35 +0000 | [diff] [blame] | 146 | // Strict init-order checking in thread-hostile. |
| 147 | DisableStrictInitOrderChecker(); |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 148 | GET_STACK_TRACE_THREAD; |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 149 | int detached = 0; |
| 150 | if (attr != 0) |
| 151 | pthread_attr_getdetachstate(attr, &detached); |
| 152 | |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 153 | u32 current_tid = GetCurrentTidOrInvalid(); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 154 | AsanThread *t = AsanThread::Create(start_routine, arg); |
| 155 | CreateThreadContextArgs args = { t, &stack }; |
| 156 | asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args); |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 157 | return REAL(pthread_create)(thread, attr, asan_thread_start, t); |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 158 | } |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 159 | #endif // ASAN_INTERCEPT_PTHREAD_CREATE |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 160 | |
Alexey Samsonov | 34a3202 | 2012-03-26 09:07:29 +0000 | [diff] [blame] | 161 | #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 162 | INTERCEPTOR(void*, signal, int signum, void *handler) { |
Alexey Samsonov | 332bf33 | 2013-04-25 10:52:15 +0000 | [diff] [blame] | 163 | if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) { |
Alexander Potapenko | 034bda5 | 2012-04-16 08:33:01 +0000 | [diff] [blame] | 164 | return REAL(signal)(signum, handler); |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 165 | } |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 166 | return 0; |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Alexey Samsonov | da13ba8 | 2012-02-16 17:00:45 +0000 | [diff] [blame] | 169 | INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act, |
| 170 | struct sigaction *oldact) { |
Alexey Samsonov | 332bf33 | 2013-04-25 10:52:15 +0000 | [diff] [blame] | 171 | if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) { |
Alexander Potapenko | 034bda5 | 2012-04-16 08:33:01 +0000 | [diff] [blame] | 172 | return REAL(sigaction)(signum, act, oldact); |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 173 | } |
Alexander Potapenko | 034bda5 | 2012-04-16 08:33:01 +0000 | [diff] [blame] | 174 | return 0; |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 175 | } |
Evgeniy Stepanov | e1ba000 | 2013-03-19 15:26:41 +0000 | [diff] [blame] | 176 | #elif SANITIZER_POSIX |
Alexey Samsonov | 34a3202 | 2012-03-26 09:07:29 +0000 | [diff] [blame] | 177 | // We need to have defined REAL(sigaction) on posix systems. |
| 178 | DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act, |
| 179 | struct sigaction *oldact); |
| 180 | #endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 181 | |
Alexey Samsonov | 0870028 | 2012-11-23 09:46:34 +0000 | [diff] [blame] | 182 | #if ASAN_INTERCEPT_SWAPCONTEXT |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 183 | static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) { |
| 184 | // Align to page size. |
| 185 | uptr PageSize = GetPageSizeCached(); |
| 186 | uptr bottom = stack & ~(PageSize - 1); |
| 187 | ssize += stack - bottom; |
| 188 | ssize = RoundUpTo(ssize, PageSize); |
| 189 | static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb |
Kostya Serebryany | 541cfb1 | 2013-01-18 11:30:36 +0000 | [diff] [blame] | 190 | if (ssize && ssize <= kMaxSaneContextStackSize) { |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 191 | PoisonShadow(bottom, ssize, 0); |
| 192 | } |
| 193 | } |
| 194 | |
Alexey Samsonov | 0870028 | 2012-11-23 09:46:34 +0000 | [diff] [blame] | 195 | INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp, |
| 196 | struct ucontext_t *ucp) { |
| 197 | static bool reported_warning = false; |
| 198 | if (!reported_warning) { |
| 199 | Report("WARNING: ASan doesn't fully support makecontext/swapcontext " |
| 200 | "functions and may produce false positives in some cases!\n"); |
| 201 | reported_warning = true; |
| 202 | } |
| 203 | // Clear shadow memory for new context (it may share stack |
| 204 | // with current context). |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 205 | uptr stack, ssize; |
| 206 | ReadContextStack(ucp, &stack, &ssize); |
| 207 | ClearShadowMemoryForContextStack(stack, ssize); |
Alexey Samsonov | 0870028 | 2012-11-23 09:46:34 +0000 | [diff] [blame] | 208 | int res = REAL(swapcontext)(oucp, ucp); |
| 209 | // swapcontext technically does not return, but program may swap context to |
| 210 | // "oucp" later, that would look as if swapcontext() returned 0. |
| 211 | // We need to clear shadow for ucp once again, as it may be in arbitrary |
| 212 | // state. |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 213 | ClearShadowMemoryForContextStack(stack, ssize); |
Alexey Samsonov | 0870028 | 2012-11-23 09:46:34 +0000 | [diff] [blame] | 214 | return res; |
| 215 | } |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 216 | #endif // ASAN_INTERCEPT_SWAPCONTEXT |
Alexey Samsonov | 0870028 | 2012-11-23 09:46:34 +0000 | [diff] [blame] | 217 | |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 218 | INTERCEPTOR(void, longjmp, void *env, int val) { |
Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 219 | __asan_handle_no_return(); |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 220 | REAL(longjmp)(env, val); |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 223 | #if ASAN_INTERCEPT__LONGJMP |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 224 | INTERCEPTOR(void, _longjmp, void *env, int val) { |
Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 225 | __asan_handle_no_return(); |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 226 | REAL(_longjmp)(env, val); |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 227 | } |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 228 | #endif |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 229 | |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 230 | #if ASAN_INTERCEPT_SIGLONGJMP |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 231 | INTERCEPTOR(void, siglongjmp, void *env, int val) { |
Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 232 | __asan_handle_no_return(); |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 233 | REAL(siglongjmp)(env, val); |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 234 | } |
Timur Iskhodzhanov | 07bb9f1 | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 235 | #endif |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 236 | |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 237 | #if ASAN_INTERCEPT___CXA_THROW |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 238 | INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) { |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 239 | CHECK(REAL(__cxa_throw)); |
Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 240 | __asan_handle_no_return(); |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 241 | REAL(__cxa_throw)(a, b, c); |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 242 | } |
| 243 | #endif |
| 244 | |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 245 | // intercept mlock and friends. |
| 246 | // Since asan maps 16T of RAM, mlock is completely unfriendly to asan. |
| 247 | // All functions return 0 (success). |
| 248 | static void MlockIsUnsupported() { |
Timur Iskhodzhanov | 8d2438a | 2013-05-29 17:26:25 +0000 | [diff] [blame] | 249 | static bool printed = false; |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 250 | if (printed) return; |
| 251 | printed = true; |
| 252 | Printf("INFO: AddressSanitizer ignores mlock/mlockall/munlock/munlockall\n"); |
| 253 | } |
Alexey Samsonov | 3389b8e | 2012-01-30 13:42:44 +0000 | [diff] [blame] | 254 | |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 255 | extern "C" { |
Alexander Potapenko | 00f1c09 | 2012-08-15 09:22:57 +0000 | [diff] [blame] | 256 | INTERCEPTOR(int, mlock, const void *addr, uptr len) { |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 257 | MlockIsUnsupported(); |
| 258 | return 0; |
| 259 | } |
Alexey Samsonov | 3389b8e | 2012-01-30 13:42:44 +0000 | [diff] [blame] | 260 | |
Alexander Potapenko | 00f1c09 | 2012-08-15 09:22:57 +0000 | [diff] [blame] | 261 | INTERCEPTOR(int, munlock, const void *addr, uptr len) { |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 262 | MlockIsUnsupported(); |
| 263 | return 0; |
| 264 | } |
Alexey Samsonov | 3389b8e | 2012-01-30 13:42:44 +0000 | [diff] [blame] | 265 | |
Alexander Potapenko | 00f1c09 | 2012-08-15 09:22:57 +0000 | [diff] [blame] | 266 | INTERCEPTOR(int, mlockall, int flags) { |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 267 | MlockIsUnsupported(); |
| 268 | return 0; |
| 269 | } |
Alexey Samsonov | 3389b8e | 2012-01-30 13:42:44 +0000 | [diff] [blame] | 270 | |
Alexander Potapenko | 00f1c09 | 2012-08-15 09:22:57 +0000 | [diff] [blame] | 271 | INTERCEPTOR(int, munlockall, void) { |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 272 | MlockIsUnsupported(); |
| 273 | return 0; |
| 274 | } |
| 275 | } // extern "C" |
| 276 | |
Kostya Serebryany | 52fb238 | 2011-12-28 18:56:42 +0000 | [diff] [blame] | 277 | static inline int CharCmp(unsigned char c1, unsigned char c2) { |
| 278 | return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1; |
| 279 | } |
| 280 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 281 | INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) { |
Alexander Potapenko | 50f2e30 | 2012-10-05 12:11:06 +0000 | [diff] [blame] | 282 | if (!asan_inited) return internal_memcmp(a1, a2, size); |
Kostya Serebryany | 52fb238 | 2011-12-28 18:56:42 +0000 | [diff] [blame] | 283 | ENSURE_ASAN_INITED(); |
Kostya Serebryany | 1b057b2 | 2013-02-26 07:25:18 +0000 | [diff] [blame] | 284 | if (flags()->replace_intrin) { |
Alexander Potapenko | 8bd5e74 | 2013-02-28 14:09:30 +0000 | [diff] [blame] | 285 | if (flags()->strict_memcmp) { |
| 286 | // Check the entire regions even if the first bytes of the buffers are |
| 287 | // different. |
| 288 | ASAN_READ_RANGE(a1, size); |
| 289 | ASAN_READ_RANGE(a2, size); |
| 290 | // Fallthrough to REAL(memcmp) below. |
| 291 | } else { |
| 292 | unsigned char c1 = 0, c2 = 0; |
| 293 | const unsigned char *s1 = (const unsigned char*)a1; |
| 294 | const unsigned char *s2 = (const unsigned char*)a2; |
| 295 | uptr i; |
| 296 | for (i = 0; i < size; i++) { |
| 297 | c1 = s1[i]; |
| 298 | c2 = s2[i]; |
| 299 | if (c1 != c2) break; |
| 300 | } |
| 301 | ASAN_READ_RANGE(s1, Min(i + 1, size)); |
| 302 | ASAN_READ_RANGE(s2, Min(i + 1, size)); |
| 303 | return CharCmp(c1, c2); |
| 304 | } |
Kostya Serebryany | 52fb238 | 2011-12-28 18:56:42 +0000 | [diff] [blame] | 305 | } |
Kostya Serebryany | 1b057b2 | 2013-02-26 07:25:18 +0000 | [diff] [blame] | 306 | return REAL(memcmp(a1, a2, size)); |
Kostya Serebryany | 52fb238 | 2011-12-28 18:56:42 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 309 | INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) { |
Alexander Potapenko | 50f2e30 | 2012-10-05 12:11:06 +0000 | [diff] [blame] | 310 | if (!asan_inited) return internal_memcpy(to, from, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 311 | // memcpy is called during __asan_init() from the internals |
| 312 | // of printf(...). |
| 313 | if (asan_init_is_running) { |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 314 | return REAL(memcpy)(to, from, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 315 | } |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 316 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 317 | if (flags()->replace_intrin) { |
Kostya Serebryany | c655cfa | 2012-01-17 18:43:52 +0000 | [diff] [blame] | 318 | if (to != from) { |
| 319 | // We do not treat memcpy with to==from as a bug. |
| 320 | // See http://llvm.org/bugs/show_bug.cgi?id=11763. |
| 321 | CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); |
| 322 | } |
Alexander Potapenko | c75d848 | 2012-12-10 16:02:13 +0000 | [diff] [blame] | 323 | ASAN_READ_RANGE(from, size); |
| 324 | ASAN_WRITE_RANGE(to, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 325 | } |
Alexander Potapenko | f1673e6 | 2012-10-15 15:34:41 +0000 | [diff] [blame] | 326 | // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8. |
| 327 | // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116. |
| 328 | return internal_memcpy(to, from, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 331 | INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) { |
Alexander Potapenko | 3e8458a | 2012-10-24 09:19:16 +0000 | [diff] [blame] | 332 | if (!asan_inited) return internal_memmove(to, from, size); |
Alexey Samsonov | a1a25c1 | 2012-06-08 14:04:04 +0000 | [diff] [blame] | 333 | if (asan_init_is_running) { |
| 334 | return REAL(memmove)(to, from, size); |
| 335 | } |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 336 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 337 | if (flags()->replace_intrin) { |
Alexander Potapenko | c75d848 | 2012-12-10 16:02:13 +0000 | [diff] [blame] | 338 | ASAN_READ_RANGE(from, size); |
| 339 | ASAN_WRITE_RANGE(to, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 340 | } |
Alexander Potapenko | f1673e6 | 2012-10-15 15:34:41 +0000 | [diff] [blame] | 341 | // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8. |
| 342 | // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116. |
| 343 | return internal_memmove(to, from, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 346 | INTERCEPTOR(void*, memset, void *block, int c, uptr size) { |
Alexander Potapenko | 50f2e30 | 2012-10-05 12:11:06 +0000 | [diff] [blame] | 347 | if (!asan_inited) return internal_memset(block, c, size); |
Alexander Potapenko | ebb9702 | 2012-03-29 12:20:47 +0000 | [diff] [blame] | 348 | // memset is called inside Printf. |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 349 | if (asan_init_is_running) { |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 350 | return REAL(memset)(block, c, size); |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 351 | } |
| 352 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 353 | if (flags()->replace_intrin) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 354 | ASAN_WRITE_RANGE(block, size); |
| 355 | } |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 356 | return REAL(memset)(block, c, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 359 | INTERCEPTOR(char*, strchr, const char *str, int c) { |
Alexander Potapenko | 50f2e30 | 2012-10-05 12:11:06 +0000 | [diff] [blame] | 360 | if (!asan_inited) return internal_strchr(str, c); |
Alexander Potapenko | 8d6e3f7 | 2012-09-10 08:35:12 +0000 | [diff] [blame] | 361 | // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is |
| 362 | // used. |
| 363 | if (asan_init_is_running) { |
| 364 | return REAL(strchr)(str, c); |
| 365 | } |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 366 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 367 | char *result = REAL(strchr)(str, c); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 368 | if (flags()->replace_str) { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 369 | uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 370 | ASAN_READ_RANGE(str, bytes_read); |
| 371 | } |
| 372 | return result; |
| 373 | } |
| 374 | |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 375 | #if ASAN_INTERCEPT_INDEX |
| 376 | # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX |
Evgeniy Stepanov | 4b0c5f2 | 2012-02-13 12:12:32 +0000 | [diff] [blame] | 377 | INTERCEPTOR(char*, index, const char *string, int c) |
Alexander Potapenko | 72bbfd4 | 2013-02-21 15:15:43 +0000 | [diff] [blame] | 378 | ALIAS(WRAPPER_NAME(strchr)); |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 379 | # else |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 380 | # if SANITIZER_MAC |
Alexander Potapenko | 50a002e | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 381 | DECLARE_REAL(char*, index, const char *string, int c) |
| 382 | OVERRIDE_FUNCTION(index, strchr); |
| 383 | # else |
| 384 | DEFINE_REAL(char*, index, const char *string, int c); |
| 385 | # endif |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 386 | # endif |
| 387 | #endif // ASAN_INTERCEPT_INDEX |
Kostya Serebryany | af0f01d | 2011-12-28 02:24:50 +0000 | [diff] [blame] | 388 | |
Alexander Potapenko | 37b3fcd | 2012-08-02 10:25:46 +0000 | [diff] [blame] | 389 | // For both strcat() and strncat() we need to check the validity of |to| |
| 390 | // argument irrespective of the |from| length. |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 391 | INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT |
Kostya Serebryany | 0985ca2 | 2011-12-28 19:08:49 +0000 | [diff] [blame] | 392 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 393 | if (flags()->replace_str) { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 394 | uptr from_length = REAL(strlen)(from); |
Kostya Serebryany | 0985ca2 | 2011-12-28 19:08:49 +0000 | [diff] [blame] | 395 | ASAN_READ_RANGE(from, from_length + 1); |
Alexander Potapenko | 37b3fcd | 2012-08-02 10:25:46 +0000 | [diff] [blame] | 396 | uptr to_length = REAL(strlen)(to); |
| 397 | ASAN_READ_RANGE(to, to_length); |
| 398 | ASAN_WRITE_RANGE(to + to_length, from_length + 1); |
| 399 | // If the copying actually happens, the |from| string should not overlap |
| 400 | // with the resulting string starting at |to|, which has a length of |
| 401 | // to_length + from_length + 1. |
Kostya Serebryany | 0985ca2 | 2011-12-28 19:08:49 +0000 | [diff] [blame] | 402 | if (from_length > 0) { |
Alexander Potapenko | 37b3fcd | 2012-08-02 10:25:46 +0000 | [diff] [blame] | 403 | CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1, |
| 404 | from, from_length + 1); |
Kostya Serebryany | 0985ca2 | 2011-12-28 19:08:49 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 407 | return REAL(strcat)(to, from); // NOLINT |
Kostya Serebryany | 0985ca2 | 2011-12-28 19:08:49 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Alexey Samsonov | c1bdd5a | 2012-06-08 13:27:46 +0000 | [diff] [blame] | 410 | INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) { |
| 411 | ENSURE_ASAN_INITED(); |
Alexander Potapenko | 37b3fcd | 2012-08-02 10:25:46 +0000 | [diff] [blame] | 412 | if (flags()->replace_str) { |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 413 | uptr from_length = MaybeRealStrnlen(from, size); |
Alexander Potapenko | 37b3fcd | 2012-08-02 10:25:46 +0000 | [diff] [blame] | 414 | uptr copy_length = Min(size, from_length + 1); |
| 415 | ASAN_READ_RANGE(from, copy_length); |
Alexey Samsonov | c1bdd5a | 2012-06-08 13:27:46 +0000 | [diff] [blame] | 416 | uptr to_length = REAL(strlen)(to); |
| 417 | ASAN_READ_RANGE(to, to_length); |
| 418 | ASAN_WRITE_RANGE(to + to_length, from_length + 1); |
| 419 | if (from_length > 0) { |
Alexander Potapenko | 37b3fcd | 2012-08-02 10:25:46 +0000 | [diff] [blame] | 420 | CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1, |
| 421 | from, copy_length); |
Alexey Samsonov | c1bdd5a | 2012-06-08 13:27:46 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | return REAL(strncat)(to, from, size); |
| 425 | } |
| 426 | |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 427 | INTERCEPTOR(int, strcmp, const char *s1, const char *s2) { |
Alexander Potapenko | 50f2e30 | 2012-10-05 12:11:06 +0000 | [diff] [blame] | 428 | if (!asan_inited) return internal_strcmp(s1, s2); |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 429 | if (asan_init_is_running) { |
| 430 | return REAL(strcmp)(s1, s2); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 431 | } |
Kostya Serebryany | 8648e77 | 2012-07-27 07:09:49 +0000 | [diff] [blame] | 432 | ENSURE_ASAN_INITED(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 433 | unsigned char c1, c2; |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 434 | uptr i; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 435 | for (i = 0; ; i++) { |
| 436 | c1 = (unsigned char)s1[i]; |
| 437 | c2 = (unsigned char)s2[i]; |
| 438 | if (c1 != c2 || c1 == '\0') break; |
| 439 | } |
| 440 | ASAN_READ_RANGE(s1, i + 1); |
| 441 | ASAN_READ_RANGE(s2, i + 1); |
| 442 | return CharCmp(c1, c2); |
| 443 | } |
| 444 | |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 445 | INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 446 | #if SANITIZER_MAC |
Alexander Potapenko | beda44f | 2012-08-17 10:08:51 +0000 | [diff] [blame] | 447 | if (!asan_inited) return REAL(strcpy)(to, from); // NOLINT |
Alexander Potapenko | 0ef5310 | 2012-08-17 09:00:08 +0000 | [diff] [blame] | 448 | #endif |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 449 | // strcpy is called from malloc_default_purgeable_zone() |
| 450 | // in __asan::ReplaceSystemAlloc() on Mac. |
| 451 | if (asan_init_is_running) { |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 452 | return REAL(strcpy)(to, from); // NOLINT |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 453 | } |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 454 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 455 | if (flags()->replace_str) { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 456 | uptr from_size = REAL(strlen)(from) + 1; |
Kostya Serebryany | c5e72a3 | 2011-12-28 19:24:31 +0000 | [diff] [blame] | 457 | CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 458 | ASAN_READ_RANGE(from, from_size); |
| 459 | ASAN_WRITE_RANGE(to, from_size); |
| 460 | } |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 461 | return REAL(strcpy)(to, from); // NOLINT |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 464 | #if ASAN_INTERCEPT_STRDUP |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 465 | INTERCEPTOR(char*, strdup, const char *s) { |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 466 | #if SANITIZER_MAC |
Alexander Potapenko | 71578fa | 2012-10-26 11:31:14 +0000 | [diff] [blame] | 467 | // FIXME: because internal_strdup() uses InternalAlloc(), which currently |
| 468 | // just calls malloc() on Mac, we can't use internal_strdup() with the |
| 469 | // dynamic runtime. We can remove the call to REAL(strdup) once InternalAlloc |
| 470 | // starts using mmap() instead. |
| 471 | // See also http://code.google.com/p/address-sanitizer/issues/detail?id=123. |
| 472 | if (!asan_inited) return REAL(strdup)(s); |
| 473 | #endif |
Alexander Potapenko | 50f2e30 | 2012-10-05 12:11:06 +0000 | [diff] [blame] | 474 | if (!asan_inited) return internal_strdup(s); |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 475 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 476 | if (flags()->replace_str) { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 477 | uptr length = REAL(strlen)(s); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 478 | ASAN_READ_RANGE(s, length + 1); |
| 479 | } |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 480 | return REAL(strdup)(s); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 481 | } |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 482 | #endif |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 483 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 484 | INTERCEPTOR(uptr, strlen, const char *s) { |
Alexander Potapenko | 50f2e30 | 2012-10-05 12:11:06 +0000 | [diff] [blame] | 485 | if (!asan_inited) return internal_strlen(s); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 486 | // strlen is called from malloc_default_purgeable_zone() |
| 487 | // in __asan::ReplaceSystemAlloc() on Mac. |
| 488 | if (asan_init_is_running) { |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 489 | return REAL(strlen)(s); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 490 | } |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 491 | ENSURE_ASAN_INITED(); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 492 | uptr length = REAL(strlen)(s); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 493 | if (flags()->replace_str) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 494 | ASAN_READ_RANGE(s, length + 1); |
| 495 | } |
| 496 | return length; |
| 497 | } |
| 498 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 499 | INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) { |
Alexander Potapenko | 50f2e30 | 2012-10-05 12:11:06 +0000 | [diff] [blame] | 500 | if (!asan_inited) return internal_strncmp(s1, s2, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 501 | // strncmp is called from malloc_default_purgeable_zone() |
| 502 | // in __asan::ReplaceSystemAlloc() on Mac. |
| 503 | if (asan_init_is_running) { |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 504 | return REAL(strncmp)(s1, s2, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 505 | } |
Kostya Serebryany | 8648e77 | 2012-07-27 07:09:49 +0000 | [diff] [blame] | 506 | ENSURE_ASAN_INITED(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 507 | unsigned char c1 = 0, c2 = 0; |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 508 | uptr i; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 509 | for (i = 0; i < size; i++) { |
| 510 | c1 = (unsigned char)s1[i]; |
| 511 | c2 = (unsigned char)s2[i]; |
| 512 | if (c1 != c2 || c1 == '\0') break; |
| 513 | } |
Kostya Serebryany | 2d8b3bd | 2011-12-02 18:42:04 +0000 | [diff] [blame] | 514 | ASAN_READ_RANGE(s1, Min(i + 1, size)); |
| 515 | ASAN_READ_RANGE(s2, Min(i + 1, size)); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 516 | return CharCmp(c1, c2); |
| 517 | } |
| 518 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 519 | INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) { |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 520 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 521 | if (flags()->replace_str) { |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 522 | uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1); |
Kostya Serebryany | c5e72a3 | 2011-12-28 19:24:31 +0000 | [diff] [blame] | 523 | CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 524 | ASAN_READ_RANGE(from, from_size); |
| 525 | ASAN_WRITE_RANGE(to, size); |
| 526 | } |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 527 | return REAL(strncpy)(to, from, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Alexey Samsonov | 81a7a4a | 2012-03-24 09:10:50 +0000 | [diff] [blame] | 530 | #if ASAN_INTERCEPT_STRNLEN |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 531 | INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) { |
Kostya Serebryany | e130191 | 2011-12-05 18:56:29 +0000 | [diff] [blame] | 532 | ENSURE_ASAN_INITED(); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 533 | uptr length = REAL(strnlen)(s, maxlen); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 534 | if (flags()->replace_str) { |
Kostya Serebryany | 2d8b3bd | 2011-12-02 18:42:04 +0000 | [diff] [blame] | 535 | ASAN_READ_RANGE(s, Min(length + 1, maxlen)); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 536 | } |
| 537 | return length; |
| 538 | } |
Alexey Samsonov | 81a7a4a | 2012-03-24 09:10:50 +0000 | [diff] [blame] | 539 | #endif // ASAN_INTERCEPT_STRNLEN |
Kostya Serebryany | 547652c | 2012-01-09 19:35:11 +0000 | [diff] [blame] | 540 | |
Alexey Samsonov | 8f6a77f | 2012-03-26 16:42:22 +0000 | [diff] [blame] | 541 | static inline bool IsValidStrtolBase(int base) { |
| 542 | return (base == 0) || (2 <= base && base <= 36); |
Alexey Samsonov | 84ba324 | 2012-03-24 08:39:14 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Alexey Samsonov | 8f6a77f | 2012-03-26 16:42:22 +0000 | [diff] [blame] | 545 | static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) { |
Kostya Serebryany | a27bdf7 | 2013-04-05 14:40:25 +0000 | [diff] [blame] | 546 | CHECK(endptr); |
Alexey Samsonov | 8f6a77f | 2012-03-26 16:42:22 +0000 | [diff] [blame] | 547 | if (nptr == *endptr) { |
| 548 | // No digits were found at strtol call, we need to find out the last |
| 549 | // symbol accessed by strtoll on our own. |
| 550 | // We get this symbol by skipping leading blanks and optional +/- sign. |
| 551 | while (IsSpace(*nptr)) nptr++; |
| 552 | if (*nptr == '+' || *nptr == '-') nptr++; |
| 553 | *endptr = (char*)nptr; |
| 554 | } |
| 555 | CHECK(*endptr >= nptr); |
| 556 | } |
| 557 | |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 558 | INTERCEPTOR(long, strtol, const char *nptr, // NOLINT |
| 559 | char **endptr, int base) { |
| 560 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 561 | if (!flags()->replace_str) { |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 562 | return REAL(strtol)(nptr, endptr, base); |
| 563 | } |
| 564 | char *real_endptr; |
| 565 | long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 566 | if (endptr != 0) { |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 567 | *endptr = real_endptr; |
| 568 | } |
| 569 | if (IsValidStrtolBase(base)) { |
| 570 | FixRealStrtolEndptr(nptr, &real_endptr); |
| 571 | ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); |
| 572 | } |
| 573 | return result; |
| 574 | } |
| 575 | |
| 576 | INTERCEPTOR(int, atoi, const char *nptr) { |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 577 | #if SANITIZER_MAC |
Alexander Potapenko | 0ef5310 | 2012-08-17 09:00:08 +0000 | [diff] [blame] | 578 | if (!asan_inited) return REAL(atoi)(nptr); |
| 579 | #endif |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 580 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 581 | if (!flags()->replace_str) { |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 582 | return REAL(atoi)(nptr); |
| 583 | } |
| 584 | char *real_endptr; |
| 585 | // "man atoi" tells that behavior of atoi(nptr) is the same as |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 586 | // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 587 | // parsed integer can't be stored in *long* type (even if it's |
| 588 | // different from int). So, we just imitate this behavior. |
| 589 | int result = REAL(strtol)(nptr, &real_endptr, 10); |
| 590 | FixRealStrtolEndptr(nptr, &real_endptr); |
| 591 | ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); |
| 592 | return result; |
| 593 | } |
| 594 | |
| 595 | INTERCEPTOR(long, atol, const char *nptr) { // NOLINT |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 596 | #if SANITIZER_MAC |
Alexander Potapenko | 0ef5310 | 2012-08-17 09:00:08 +0000 | [diff] [blame] | 597 | if (!asan_inited) return REAL(atol)(nptr); |
| 598 | #endif |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 599 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 600 | if (!flags()->replace_str) { |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 601 | return REAL(atol)(nptr); |
| 602 | } |
| 603 | char *real_endptr; |
| 604 | long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT |
| 605 | FixRealStrtolEndptr(nptr, &real_endptr); |
| 606 | ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); |
| 607 | return result; |
| 608 | } |
| 609 | |
| 610 | #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL |
Alexey Samsonov | 84ba324 | 2012-03-24 08:39:14 +0000 | [diff] [blame] | 611 | INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT |
| 612 | char **endptr, int base) { |
| 613 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 614 | if (!flags()->replace_str) { |
Alexey Samsonov | 84ba324 | 2012-03-24 08:39:14 +0000 | [diff] [blame] | 615 | return REAL(strtoll)(nptr, endptr, base); |
| 616 | } |
| 617 | char *real_endptr; |
| 618 | long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 619 | if (endptr != 0) { |
Alexey Samsonov | 84ba324 | 2012-03-24 08:39:14 +0000 | [diff] [blame] | 620 | *endptr = real_endptr; |
| 621 | } |
| 622 | // If base has unsupported value, strtoll can exit with EINVAL |
| 623 | // without reading any characters. So do additional checks only |
| 624 | // if base is valid. |
Alexey Samsonov | 8f6a77f | 2012-03-26 16:42:22 +0000 | [diff] [blame] | 625 | if (IsValidStrtolBase(base)) { |
| 626 | FixRealStrtolEndptr(nptr, &real_endptr); |
Alexey Samsonov | 84ba324 | 2012-03-24 08:39:14 +0000 | [diff] [blame] | 627 | ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); |
| 628 | } |
| 629 | return result; |
| 630 | } |
Alexey Samsonov | 84ba324 | 2012-03-24 08:39:14 +0000 | [diff] [blame] | 631 | |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 632 | INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT |
Alexey Samsonov | 8f6a77f | 2012-03-26 16:42:22 +0000 | [diff] [blame] | 633 | ENSURE_ASAN_INITED(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 634 | if (!flags()->replace_str) { |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 635 | return REAL(atoll)(nptr); |
Alexey Samsonov | 8f6a77f | 2012-03-26 16:42:22 +0000 | [diff] [blame] | 636 | } |
| 637 | char *real_endptr; |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 638 | long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT |
| 639 | FixRealStrtolEndptr(nptr, &real_endptr); |
| 640 | ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); |
Alexey Samsonov | 8f6a77f | 2012-03-26 16:42:22 +0000 | [diff] [blame] | 641 | return result; |
| 642 | } |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 643 | #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL |
Alexey Samsonov | 8f6a77f | 2012-03-26 16:42:22 +0000 | [diff] [blame] | 644 | |
Alexey Samsonov | 46efcb0 | 2013-05-24 11:46:56 +0000 | [diff] [blame] | 645 | static void AtCxaAtexit(void *unused) { |
| 646 | (void)unused; |
| 647 | StopInitOrderChecking(); |
| 648 | } |
| 649 | |
| 650 | #if ASAN_INTERCEPT___CXA_ATEXIT |
| 651 | INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg, |
| 652 | void *dso_handle) { |
| 653 | ENSURE_ASAN_INITED(); |
| 654 | int res = REAL(__cxa_atexit)(func, arg, dso_handle); |
| 655 | REAL(__cxa_atexit)(AtCxaAtexit, 0, 0); |
| 656 | return res; |
| 657 | } |
| 658 | #endif // ASAN_INTERCEPT___CXA_ATEXIT |
| 659 | |
Alexander Potapenko | fd55281 | 2013-06-07 15:10:02 +0000 | [diff] [blame] | 660 | #if !SANITIZER_MAC |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 661 | #define ASAN_INTERCEPT_FUNC(name) do { \ |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 662 | if (!INTERCEPT_FUNCTION(name) && flags()->verbosity > 0) \ |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 663 | Report("AddressSanitizer: failed to intercept '" #name "'\n"); \ |
| 664 | } while (0) |
Alexander Potapenko | fd55281 | 2013-06-07 15:10:02 +0000 | [diff] [blame] | 665 | #else |
| 666 | // OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION. |
| 667 | #define ASAN_INTERCEPT_FUNC(name) |
| 668 | #endif // SANITIZER_MAC |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 669 | |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 670 | #if SANITIZER_WINDOWS |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 671 | INTERCEPTOR_WINAPI(DWORD, CreateThread, |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 672 | void* security, uptr stack_size, |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 673 | DWORD (__stdcall *start_routine)(void*), void* arg, |
| 674 | DWORD flags, void* tid) { |
Alexey Samsonov | bdd0966 | 2013-04-23 13:57:35 +0000 | [diff] [blame] | 675 | // Strict init-order checking in thread-hostile. |
| 676 | DisableStrictInitOrderChecker(); |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 677 | GET_STACK_TRACE_THREAD; |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 678 | u32 current_tid = GetCurrentTidOrInvalid(); |
Alexey Samsonov | dfe3f96 | 2013-03-22 07:48:23 +0000 | [diff] [blame] | 679 | AsanThread *t = AsanThread::Create(start_routine, arg); |
| 680 | CreateThreadContextArgs args = { t, &stack }; |
Timur Iskhodzhanov | 8d2438a | 2013-05-29 17:26:25 +0000 | [diff] [blame] | 681 | bool detached = false; // FIXME: how can we determine it on Windows? |
Alexey Samsonov | dfe3f96 | 2013-03-22 07:48:23 +0000 | [diff] [blame] | 682 | asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args); |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 683 | return REAL(CreateThread)(security, stack_size, |
| 684 | asan_thread_start, t, flags, tid); |
| 685 | } |
| 686 | |
| 687 | namespace __asan { |
| 688 | void InitializeWindowsInterceptors() { |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 689 | ASAN_INTERCEPT_FUNC(CreateThread); |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | } // namespace __asan |
| 693 | #endif |
| 694 | |
Kostya Serebryany | 547652c | 2012-01-09 19:35:11 +0000 | [diff] [blame] | 695 | // ---------------------- InitializeAsanInterceptors ---------------- {{{1 |
| 696 | namespace __asan { |
| 697 | void InitializeAsanInterceptors() { |
Kostya Serebryany | fdbdab5 | 2012-03-16 21:02:13 +0000 | [diff] [blame] | 698 | static bool was_called_once; |
| 699 | CHECK(was_called_once == false); |
| 700 | was_called_once = true; |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 701 | SANITIZER_COMMON_INTERCEPTORS_INIT; |
| 702 | |
Timur Iskhodzhanov | 07bb9f1 | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 703 | // Intercept mem* functions. |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 704 | ASAN_INTERCEPT_FUNC(memcmp); |
| 705 | ASAN_INTERCEPT_FUNC(memmove); |
| 706 | ASAN_INTERCEPT_FUNC(memset); |
Alexey Samsonov | 38dd4ed | 2012-03-20 10:54:40 +0000 | [diff] [blame] | 707 | if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 708 | ASAN_INTERCEPT_FUNC(memcpy); |
Alexander Potapenko | 573fb4b | 2012-02-01 10:07:52 +0000 | [diff] [blame] | 709 | } |
Timur Iskhodzhanov | 07bb9f1 | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 710 | |
| 711 | // Intercept str* functions. |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 712 | ASAN_INTERCEPT_FUNC(strcat); // NOLINT |
| 713 | ASAN_INTERCEPT_FUNC(strchr); |
| 714 | ASAN_INTERCEPT_FUNC(strcmp); |
| 715 | ASAN_INTERCEPT_FUNC(strcpy); // NOLINT |
| 716 | ASAN_INTERCEPT_FUNC(strlen); |
Alexey Samsonov | c1bdd5a | 2012-06-08 13:27:46 +0000 | [diff] [blame] | 717 | ASAN_INTERCEPT_FUNC(strncat); |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 718 | ASAN_INTERCEPT_FUNC(strncmp); |
| 719 | ASAN_INTERCEPT_FUNC(strncpy); |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 720 | #if ASAN_INTERCEPT_STRDUP |
| 721 | ASAN_INTERCEPT_FUNC(strdup); |
| 722 | #endif |
| 723 | #if ASAN_INTERCEPT_STRNLEN |
| 724 | ASAN_INTERCEPT_FUNC(strnlen); |
| 725 | #endif |
Alexander Potapenko | 6956398 | 2013-02-05 15:57:12 +0000 | [diff] [blame] | 726 | #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 727 | ASAN_INTERCEPT_FUNC(index); |
Timur Iskhodzhanov | 07bb9f1 | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 728 | #endif |
Kostya Serebryany | 547652c | 2012-01-09 19:35:11 +0000 | [diff] [blame] | 729 | |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 730 | ASAN_INTERCEPT_FUNC(atoi); |
| 731 | ASAN_INTERCEPT_FUNC(atol); |
| 732 | ASAN_INTERCEPT_FUNC(strtol); |
Alexey Samsonov | 847f932 | 2012-03-29 08:04:35 +0000 | [diff] [blame] | 733 | #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 734 | ASAN_INTERCEPT_FUNC(atoll); |
| 735 | ASAN_INTERCEPT_FUNC(strtoll); |
Alexey Samsonov | 84ba324 | 2012-03-24 08:39:14 +0000 | [diff] [blame] | 736 | #endif |
| 737 | |
Alexander Potapenko | f0c6de3 | 2012-08-15 09:46:45 +0000 | [diff] [blame] | 738 | #if ASAN_INTERCEPT_MLOCKX |
Alexander Potapenko | 00f1c09 | 2012-08-15 09:22:57 +0000 | [diff] [blame] | 739 | // Intercept mlock/munlock. |
| 740 | ASAN_INTERCEPT_FUNC(mlock); |
| 741 | ASAN_INTERCEPT_FUNC(munlock); |
| 742 | ASAN_INTERCEPT_FUNC(mlockall); |
| 743 | ASAN_INTERCEPT_FUNC(munlockall); |
Alexander Potapenko | f0c6de3 | 2012-08-15 09:46:45 +0000 | [diff] [blame] | 744 | #endif |
Alexander Potapenko | 00f1c09 | 2012-08-15 09:22:57 +0000 | [diff] [blame] | 745 | |
Timur Iskhodzhanov | 07bb9f1 | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 746 | // Intecept signal- and jump-related functions. |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 747 | ASAN_INTERCEPT_FUNC(longjmp); |
Alexey Samsonov | 34a3202 | 2012-03-26 09:07:29 +0000 | [diff] [blame] | 748 | #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 749 | ASAN_INTERCEPT_FUNC(sigaction); |
| 750 | ASAN_INTERCEPT_FUNC(signal); |
Evgeniy Stepanov | 919c247 | 2012-02-13 12:04:36 +0000 | [diff] [blame] | 751 | #endif |
Alexey Samsonov | 0870028 | 2012-11-23 09:46:34 +0000 | [diff] [blame] | 752 | #if ASAN_INTERCEPT_SWAPCONTEXT |
| 753 | ASAN_INTERCEPT_FUNC(swapcontext); |
| 754 | #endif |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 755 | #if ASAN_INTERCEPT__LONGJMP |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 756 | ASAN_INTERCEPT_FUNC(_longjmp); |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 757 | #endif |
| 758 | #if ASAN_INTERCEPT_SIGLONGJMP |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 759 | ASAN_INTERCEPT_FUNC(siglongjmp); |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 760 | #endif |
| 761 | |
| 762 | // Intercept exception handling functions. |
| 763 | #if ASAN_INTERCEPT___CXA_THROW |
| 764 | INTERCEPT_FUNCTION(__cxa_throw); |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 765 | #endif |
| 766 | |
Timur Iskhodzhanov | 07bb9f1 | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 767 | // Intercept threading-related functions |
Alexey Samsonov | fd2ae4f | 2012-08-01 11:17:00 +0000 | [diff] [blame] | 768 | #if ASAN_INTERCEPT_PTHREAD_CREATE |
Dmitry Vyukov | 580469d | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 769 | ASAN_INTERCEPT_FUNC(pthread_create); |
Timur Iskhodzhanov | 07bb9f1 | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 770 | #endif |
| 771 | |
Alexey Samsonov | 46efcb0 | 2013-05-24 11:46:56 +0000 | [diff] [blame] | 772 | // Intercept atexit function. |
| 773 | #if ASAN_INTERCEPT___CXA_ATEXIT |
| 774 | ASAN_INTERCEPT_FUNC(__cxa_atexit); |
| 775 | #endif |
| 776 | |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 777 | // Some Windows-specific interceptors. |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 778 | #if SANITIZER_WINDOWS |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 779 | InitializeWindowsInterceptors(); |
| 780 | #endif |
| 781 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 782 | if (flags()->verbosity > 0) { |
Alexander Potapenko | 2962f26 | 2012-03-21 09:33:05 +0000 | [diff] [blame] | 783 | Report("AddressSanitizer: libc interceptors initialized\n"); |
Kostya Serebryany | 547652c | 2012-01-09 19:35:11 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
| 787 | } // namespace __asan |