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