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