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