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