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