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