blob: deac03470dc2e8d6cef31b0c3664af4e0147e814 [file] [log] [blame]
Alexey Samsonove5f58952012-06-04 13:50:10 +00001//===-- asan_interceptors.cc ----------------------------------------------===//
Kostya Serebryany1e172b42011-11-30 01:07:02 +00002//
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 Serebryanyd47189c2012-01-11 02:32:40 +000012// Intercept various libc functions.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000013//===----------------------------------------------------------------------===//
14#include "asan_interceptors.h"
15
16#include "asan_allocator.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000017#include "asan_internal.h"
18#include "asan_mapping.h"
Alexey Samsonov7e843492013-03-28 15:42:43 +000019#include "asan_poisoning.h"
Alexey Samsonov487fee72012-08-09 08:32:33 +000020#include "asan_report.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021#include "asan_stack.h"
22#include "asan_stats.h"
Alexey Samsonovc0d78c12012-06-04 13:27:49 +000023#include "sanitizer_common/sanitizer_libc.h"
24
Kostya Serebryany1e172b42011-11-30 01:07:02 +000025namespace __asan {
26
Kostya Serebryanya84805f2013-02-21 07:07:39 +000027// Return true if we can quickly decide that the region is unpoisoned.
28static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) {
29 if (size == 0) return true;
30 if (size <= 32)
31 return !AddressIsPoisoned(beg) &&
32 !AddressIsPoisoned(beg + size - 1) &&
33 !AddressIsPoisoned(beg + size / 2);
34 return false;
35}
36
Kostya Serebryany1e172b42011-11-30 01:07:02 +000037// We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
38// and ASAN_WRITE_RANGE as macro instead of function so
39// that no extra frames are created, and stack trace contains
40// relevant information only.
Kostya Serebryanyeb280932012-12-28 15:24:16 +000041// We check all shadow bytes.
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000042#define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \
43 uptr __offset = (uptr)(offset); \
44 uptr __size = (uptr)(size); \
Kostya Serebryany1b057b22013-02-26 07:25:18 +000045 uptr __bad = 0; \
Stephen Hines2d1fdb22014-05-28 23:58:16 -070046 if (__offset > __offset + __size) { \
47 GET_STACK_TRACE_FATAL_HERE; \
48 ReportStringFunctionSizeOverflow(__offset, __size, &stack); \
49 } \
Kostya Serebryanya84805f2013-02-21 07:07:39 +000050 if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \
Kostya Serebryany1b057b22013-02-26 07:25:18 +000051 (__bad = __asan_region_is_poisoned(__offset, __size))) { \
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000052 GET_CURRENT_PC_BP_SP; \
Kostya Serebryany1b057b22013-02-26 07:25:18 +000053 __asan_report_error(pc, bp, sp, __bad, isWrite, __size); \
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000054 } \
55 } while (0)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000056
Kostya Serebryanyeb280932012-12-28 15:24:16 +000057#define ASAN_READ_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, false)
Evgeniy Stepanov341b9e62013-06-28 11:02:43 +000058#define ASAN_WRITE_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, true)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000059
60// Behavior of functions like "memcpy" or "strcpy" is undefined
61// if memory intervals overlap. We report error in this case.
62// Macro is used to avoid creation of new frames.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000063static inline bool RangesOverlap(const char *offset1, uptr length1,
64 const char *offset2, uptr length2) {
Kostya Serebryany0985ca22011-12-28 19:08:49 +000065 return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
Kostya Serebryany1e172b42011-11-30 01:07:02 +000066}
Kostya Serebryanyc5e72a32011-12-28 19:24:31 +000067#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
Kostya Serebryany1e172b42011-11-30 01:07:02 +000068 const char *offset1 = (const char*)_offset1; \
69 const char *offset2 = (const char*)_offset2; \
Kostya Serebryany0985ca22011-12-28 19:08:49 +000070 if (RangesOverlap(offset1, length1, offset2, length2)) { \
Kostya Serebryanya30c8f92012-12-13 09:34:23 +000071 GET_STACK_TRACE_FATAL_HERE; \
Alexey Samsonov487fee72012-08-09 08:32:33 +000072 ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
73 offset2, length2, &stack); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +000074 } \
Kostya Serebryanye1301912011-12-05 18:56:29 +000075} while (0)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000076
Alexey Samsonovc9256972012-06-15 13:09:52 +000077static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
Alexey Samsonov81a7a4a2012-03-24 09:10:50 +000078#if ASAN_INTERCEPT_STRNLEN
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000079 if (REAL(strnlen) != 0) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +000080 return REAL(strnlen)(s, maxlen);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081 }
Alexey Samsonovf2598fc2012-02-02 10:39:40 +000082#endif
Alexey Samsonovc9256972012-06-15 13:09:52 +000083 return internal_strnlen(s, maxlen);
Kostya Serebryanya4ccf872012-01-09 22:20:49 +000084}
Kostya Serebryany1e172b42011-11-30 01:07:02 +000085
Kostya Serebryanyc20b3212013-01-18 06:43:13 +000086void SetThreadName(const char *name) {
Alexey Samsonov89c13842013-03-20 09:23:28 +000087 AsanThread *t = GetCurrentThread();
Kostya Serebryanyc20b3212013-01-18 06:43:13 +000088 if (t)
Alexey Samsonovdef1be92013-03-21 11:23:41 +000089 asanThreadRegistry().SetThreadName(t->tid(), name);
Kostya Serebryanyc20b3212013-01-18 06:43:13 +000090}
91
Dmitry Vyukov8cde99f2013-10-03 15:43:59 +000092int OnExit() {
Dmitry Vyukov14dd9802013-10-03 15:22:29 +000093 // FIXME: ask frontend whether we need to return failure.
94 return 0;
95}
96
Kostya Serebryany1e172b42011-11-30 01:07:02 +000097} // namespace __asan
98
99// ---------------------- Wrappers ---------------- {{{1
100using namespace __asan; // NOLINT
101
Evgeniy Stepanov12eb79d2013-07-09 09:53:37 +0000102DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr)
103DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
104
Evgeniy Stepanova537ea92013-11-11 11:28:30 +0000105#if !SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700106#define ASAN_INTERCEPT_FUNC(name) \
107 do { \
108 if ((!INTERCEPT_FUNCTION(name) || !REAL(name))) \
109 VReport(1, "AddressSanitizer: failed to intercept '" #name "'\n"); \
Evgeniy Stepanova537ea92013-11-11 11:28:30 +0000110 } while (0)
111#else
112// OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
113#define ASAN_INTERCEPT_FUNC(name)
114#endif // SANITIZER_MAC
115
116#define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name)
Evgeniy Stepanov82a90802013-01-18 13:12:56 +0000117#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
118 ASAN_WRITE_RANGE(ptr, size)
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +0000119#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) ASAN_READ_RANGE(ptr, size)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700120#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
121 do { \
122 if (asan_init_is_running) \
123 return REAL(func)(__VA_ARGS__); \
124 ctx = 0; \
125 (void) ctx; \
126 if (SANITIZER_MAC && UNLIKELY(!asan_inited)) \
127 return REAL(func)(__VA_ARGS__); \
128 ENSURE_ASAN_INITED(); \
Evgeniy Stepanov82a90802013-01-18 13:12:56 +0000129 } while (false)
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +0000130#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
131 do { \
132 } while (false)
133#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
134 do { \
135 } while (false)
136#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
137 do { \
138 } while (false)
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +0000139#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
Dmitry Vyukove767e352013-11-14 16:48:22 +0000140// Should be asanThreadRegistry().SetThreadNameByUserId(thread, name)
141// But asan does not remember UserId's for threads (pthread_t);
142// and remembers all ever existed threads, so the linear search by UserId
143// can be slow.
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +0000144#define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
Dmitry Vyukove767e352013-11-14 16:48:22 +0000145 do { \
146 } while (false)
Evgeniy Stepanove18e3f02013-08-12 13:19:53 +0000147#define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
Dmitry Vyukov14dd9802013-10-03 15:22:29 +0000148#define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700149#define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res) CovUpdateMapping()
150#define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() CovUpdateMapping()
Stephen Hines6d186232014-11-26 17:56:19 -0800151#define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited)
Evgeniy Stepanov4f32c0b2013-01-18 13:01:18 +0000152#include "sanitizer_common/sanitizer_common_interceptors.inc"
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000153
Evgeniy Stepanov881b6772013-04-12 14:57:03 +0000154#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(p, s)
155#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(p, s)
Evgeniy Stepanovae4e6fd2013-07-09 09:29:19 +0000156#define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
157 do { \
Evgeniy Stepanovdfab31b2013-10-29 18:29:39 +0000158 (void)(p); \
159 (void)(s); \
Evgeniy Stepanovae4e6fd2013-07-09 09:29:19 +0000160 } while (false)
161#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
162 do { \
Evgeniy Stepanovdfab31b2013-10-29 18:29:39 +0000163 (void)(p); \
164 (void)(s); \
Evgeniy Stepanovae4e6fd2013-07-09 09:29:19 +0000165 } while (false)
Evgeniy Stepanov881b6772013-04-12 14:57:03 +0000166#include "sanitizer_common/sanitizer_common_syscalls.inc"
167
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000168static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000169 AsanThread *t = (AsanThread*)arg;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000170 SetCurrentThread(t);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000171 return t->ThreadStart(GetTid());
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000172}
173
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000174#if ASAN_INTERCEPT_PTHREAD_CREATE
Evgeniy Stepanovb8ef9252012-02-22 12:31:25 +0000175INTERCEPTOR(int, pthread_create, void *thread,
176 void *attr, void *(*start_routine)(void*), void *arg) {
Sergey Matveevc6ac98d2013-07-08 12:57:24 +0000177 EnsureMainThreadIDIsCorrect();
Alexey Samsonovbdd09662013-04-23 13:57:35 +0000178 // Strict init-order checking in thread-hostile.
Alexey Samsonov9465cbd2013-07-01 16:16:41 +0000179 if (flags()->strict_init_order)
180 StopInitOrderChecking();
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000181 GET_STACK_TRACE_THREAD;
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000182 int detached = 0;
183 if (attr != 0)
Evgeniy Stepanovbb6bc9a2013-11-11 08:56:49 +0000184 REAL(pthread_attr_getdetachstate)(attr, &detached);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000185
Alexey Samsonov89c13842013-03-20 09:23:28 +0000186 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000187 AsanThread *t = AsanThread::Create(start_routine, arg);
188 CreateThreadContextArgs args = { t, &stack };
189 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000190 return REAL(pthread_create)(thread, attr, asan_thread_start, t);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000191}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000192#endif // ASAN_INTERCEPT_PTHREAD_CREATE
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000193
Alexey Samsonov34a32022012-03-26 09:07:29 +0000194#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700195
196#if SANITIZER_ANDROID
197INTERCEPTOR(void*, bsd_signal, int signum, void *handler) {
198 if (!AsanInterceptsSignal(signum) ||
199 common_flags()->allow_user_segv_handler) {
200 return REAL(bsd_signal)(signum, handler);
201 }
202 return 0;
203}
204#else
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000205INTERCEPTOR(void*, signal, int signum, void *handler) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700206 if (!AsanInterceptsSignal(signum) ||
207 common_flags()->allow_user_segv_handler) {
Alexander Potapenko034bda52012-04-16 08:33:01 +0000208 return REAL(signal)(signum, handler);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000209 }
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000210 return 0;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000211}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700212#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000213
Alexey Samsonovda13ba82012-02-16 17:00:45 +0000214INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
215 struct sigaction *oldact) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700216 if (!AsanInterceptsSignal(signum) ||
217 common_flags()->allow_user_segv_handler) {
Alexander Potapenko034bda52012-04-16 08:33:01 +0000218 return REAL(sigaction)(signum, act, oldact);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000219 }
Alexander Potapenko034bda52012-04-16 08:33:01 +0000220 return 0;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000221}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700222
223namespace __sanitizer {
224int real_sigaction(int signum, const void *act, void *oldact) {
Stephen Hines6d186232014-11-26 17:56:19 -0800225 return REAL(sigaction)(signum, (const struct sigaction *)act,
226 (struct sigaction *)oldact);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700227}
228} // namespace __sanitizer
229
Evgeniy Stepanove1ba0002013-03-19 15:26:41 +0000230#elif SANITIZER_POSIX
Alexey Samsonov34a32022012-03-26 09:07:29 +0000231// We need to have defined REAL(sigaction) on posix systems.
232DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
Alexey Samsonovfdde5a92013-06-10 14:17:08 +0000233 struct sigaction *oldact)
Alexey Samsonov34a32022012-03-26 09:07:29 +0000234#endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000235
Alexey Samsonov08700282012-11-23 09:46:34 +0000236#if ASAN_INTERCEPT_SWAPCONTEXT
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000237static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
238 // Align to page size.
239 uptr PageSize = GetPageSizeCached();
240 uptr bottom = stack & ~(PageSize - 1);
241 ssize += stack - bottom;
242 ssize = RoundUpTo(ssize, PageSize);
243 static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb
Kostya Serebryany541cfb12013-01-18 11:30:36 +0000244 if (ssize && ssize <= kMaxSaneContextStackSize) {
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000245 PoisonShadow(bottom, ssize, 0);
246 }
247}
248
Alexey Samsonov08700282012-11-23 09:46:34 +0000249INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
250 struct ucontext_t *ucp) {
251 static bool reported_warning = false;
252 if (!reported_warning) {
253 Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
254 "functions and may produce false positives in some cases!\n");
255 reported_warning = true;
256 }
257 // Clear shadow memory for new context (it may share stack
258 // with current context).
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000259 uptr stack, ssize;
260 ReadContextStack(ucp, &stack, &ssize);
261 ClearShadowMemoryForContextStack(stack, ssize);
Alexey Samsonov08700282012-11-23 09:46:34 +0000262 int res = REAL(swapcontext)(oucp, ucp);
263 // swapcontext technically does not return, but program may swap context to
264 // "oucp" later, that would look as if swapcontext() returned 0.
265 // We need to clear shadow for ucp once again, as it may be in arbitrary
266 // state.
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000267 ClearShadowMemoryForContextStack(stack, ssize);
Alexey Samsonov08700282012-11-23 09:46:34 +0000268 return res;
269}
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000270#endif // ASAN_INTERCEPT_SWAPCONTEXT
Alexey Samsonov08700282012-11-23 09:46:34 +0000271
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000272INTERCEPTOR(void, longjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000273 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000274 REAL(longjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000275}
276
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000277#if ASAN_INTERCEPT__LONGJMP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000278INTERCEPTOR(void, _longjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000279 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000280 REAL(_longjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000281}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000282#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000283
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000284#if ASAN_INTERCEPT_SIGLONGJMP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000285INTERCEPTOR(void, siglongjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000286 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000287 REAL(siglongjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000288}
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000289#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000290
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000291#if ASAN_INTERCEPT___CXA_THROW
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000292INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000293 CHECK(REAL(__cxa_throw));
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000294 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000295 REAL(__cxa_throw)(a, b, c);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000296}
297#endif
298
Stephen Hines6d186232014-11-26 17:56:19 -0800299#if SANITIZER_WINDOWS
300INTERCEPTOR_WINAPI(void, RaiseException, void *a, void *b, void *c, void *d) {
301 CHECK(REAL(RaiseException));
302 __asan_handle_no_return();
303 REAL(RaiseException)(a, b, c, d);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000304}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000305
Stephen Hines6d186232014-11-26 17:56:19 -0800306INTERCEPTOR(int, _except_handler3, void *a, void *b, void *c, void *d) {
307 CHECK(REAL(_except_handler3));
308 __asan_handle_no_return();
309 return REAL(_except_handler3)(a, b, c, d);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000310}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000311
Stephen Hines6d186232014-11-26 17:56:19 -0800312#if ASAN_DYNAMIC
313// This handler is named differently in -MT and -MD CRTs.
314#define _except_handler4 _except_handler4_common
315#endif
316INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) {
317 CHECK(REAL(_except_handler4));
318 __asan_handle_no_return();
319 return REAL(_except_handler4)(a, b, c, d);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000320}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700321#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000322
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000323static inline int CharCmp(unsigned char c1, unsigned char c2) {
324 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
325}
326
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000327INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700328 if (UNLIKELY(!asan_inited)) return internal_memcmp(a1, a2, size);
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000329 ENSURE_ASAN_INITED();
Kostya Serebryany1b057b22013-02-26 07:25:18 +0000330 if (flags()->replace_intrin) {
Alexander Potapenko8bd5e742013-02-28 14:09:30 +0000331 if (flags()->strict_memcmp) {
332 // Check the entire regions even if the first bytes of the buffers are
333 // different.
334 ASAN_READ_RANGE(a1, size);
335 ASAN_READ_RANGE(a2, size);
336 // Fallthrough to REAL(memcmp) below.
337 } else {
338 unsigned char c1 = 0, c2 = 0;
339 const unsigned char *s1 = (const unsigned char*)a1;
340 const unsigned char *s2 = (const unsigned char*)a2;
341 uptr i;
342 for (i = 0; i < size; i++) {
343 c1 = s1[i];
344 c2 = s2[i];
345 if (c1 != c2) break;
346 }
347 ASAN_READ_RANGE(s1, Min(i + 1, size));
348 ASAN_READ_RANGE(s2, Min(i + 1, size));
349 return CharCmp(c1, c2);
350 }
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000351 }
Kostya Serebryany1b057b22013-02-26 07:25:18 +0000352 return REAL(memcmp(a1, a2, size));
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000353}
354
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700355void *__asan_memcpy(void *to, const void *from, uptr size) {
356 if (UNLIKELY(!asan_inited)) return internal_memcpy(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000357 // memcpy is called during __asan_init() from the internals
358 // of printf(...).
359 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000360 return REAL(memcpy)(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000361 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000362 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000363 if (flags()->replace_intrin) {
Kostya Serebryanyc655cfa2012-01-17 18:43:52 +0000364 if (to != from) {
365 // We do not treat memcpy with to==from as a bug.
366 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
367 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
368 }
Alexander Potapenkoc75d8482012-12-10 16:02:13 +0000369 ASAN_READ_RANGE(from, size);
370 ASAN_WRITE_RANGE(to, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000371 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700372 return REAL(memcpy)(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000373}
374
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700375void *__asan_memset(void *block, int c, uptr size) {
376 if (UNLIKELY(!asan_inited)) return internal_memset(block, c, size);
Alexander Potapenkoebb97022012-03-29 12:20:47 +0000377 // memset is called inside Printf.
Kostya Serebryanye1301912011-12-05 18:56:29 +0000378 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000379 return REAL(memset)(block, c, size);
Kostya Serebryanye1301912011-12-05 18:56:29 +0000380 }
381 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000382 if (flags()->replace_intrin) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000383 ASAN_WRITE_RANGE(block, size);
384 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000385 return REAL(memset)(block, c, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000386}
387
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700388void *__asan_memmove(void *to, const void *from, uptr size) {
389 if (UNLIKELY(!asan_inited))
390 return internal_memmove(to, from, size);
391 ENSURE_ASAN_INITED();
392 if (flags()->replace_intrin) {
393 ASAN_READ_RANGE(from, size);
394 ASAN_WRITE_RANGE(to, size);
395 }
396 return internal_memmove(to, from, size);
397}
398
399INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
400 return __asan_memmove(to, from, size);
401}
402
403INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
404#if !SANITIZER_MAC
405 return __asan_memcpy(to, from, size);
406#else
407 // At least on 10.7 and 10.8 both memcpy() and memmove() are being replaced
408 // with WRAP(memcpy). As a result, false positives are reported for memmove()
409 // calls. If we just disable error reporting with
410 // ASAN_OPTIONS=replace_intrin=0, memmove() is still replaced with
411 // internal_memcpy(), which may lead to crashes, see
412 // http://llvm.org/bugs/show_bug.cgi?id=16362.
413 return __asan_memmove(to, from, size);
414#endif // !SANITIZER_MAC
415}
416
417INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
418 return __asan_memset(block, c, size);
419}
420
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000421INTERCEPTOR(char*, strchr, const char *str, int c) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700422 if (UNLIKELY(!asan_inited)) return internal_strchr(str, c);
Alexander Potapenko8d6e3f72012-09-10 08:35:12 +0000423 // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is
424 // used.
425 if (asan_init_is_running) {
426 return REAL(strchr)(str, c);
427 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000428 ENSURE_ASAN_INITED();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000429 char *result = REAL(strchr)(str, c);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000430 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000431 uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000432 ASAN_READ_RANGE(str, bytes_read);
433 }
434 return result;
435}
436
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000437#if ASAN_INTERCEPT_INDEX
438# if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
Evgeniy Stepanov4b0c5f22012-02-13 12:12:32 +0000439INTERCEPTOR(char*, index, const char *string, int c)
Alexander Potapenko72bbfd42013-02-21 15:15:43 +0000440 ALIAS(WRAPPER_NAME(strchr));
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000441# else
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000442# if SANITIZER_MAC
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000443DECLARE_REAL(char*, index, const char *string, int c)
444OVERRIDE_FUNCTION(index, strchr);
445# else
Alexey Samsonovfdde5a92013-06-10 14:17:08 +0000446DEFINE_REAL(char*, index, const char *string, int c)
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000447# endif
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000448# endif
449#endif // ASAN_INTERCEPT_INDEX
Kostya Serebryanyaf0f01d2011-12-28 02:24:50 +0000450
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000451// For both strcat() and strncat() we need to check the validity of |to|
452// argument irrespective of the |from| length.
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000453INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000454 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000455 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000456 uptr from_length = REAL(strlen)(from);
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000457 ASAN_READ_RANGE(from, from_length + 1);
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000458 uptr to_length = REAL(strlen)(to);
459 ASAN_READ_RANGE(to, to_length);
460 ASAN_WRITE_RANGE(to + to_length, from_length + 1);
461 // If the copying actually happens, the |from| string should not overlap
462 // with the resulting string starting at |to|, which has a length of
463 // to_length + from_length + 1.
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000464 if (from_length > 0) {
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000465 CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
466 from, from_length + 1);
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000467 }
468 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000469 return REAL(strcat)(to, from); // NOLINT
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000470}
471
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000472INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
473 ENSURE_ASAN_INITED();
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000474 if (flags()->replace_str) {
Alexey Samsonovc9256972012-06-15 13:09:52 +0000475 uptr from_length = MaybeRealStrnlen(from, size);
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000476 uptr copy_length = Min(size, from_length + 1);
477 ASAN_READ_RANGE(from, copy_length);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000478 uptr to_length = REAL(strlen)(to);
479 ASAN_READ_RANGE(to, to_length);
480 ASAN_WRITE_RANGE(to + to_length, from_length + 1);
481 if (from_length > 0) {
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000482 CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
483 from, copy_length);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000484 }
485 }
486 return REAL(strncat)(to, from, size);
487}
488
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000489INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000490#if SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700491 if (UNLIKELY(!asan_inited)) return REAL(strcpy)(to, from); // NOLINT
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000492#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000493 // strcpy is called from malloc_default_purgeable_zone()
494 // in __asan::ReplaceSystemAlloc() on Mac.
495 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000496 return REAL(strcpy)(to, from); // NOLINT
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000497 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000498 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000499 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000500 uptr from_size = REAL(strlen)(from) + 1;
Kostya Serebryanyc5e72a32011-12-28 19:24:31 +0000501 CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000502 ASAN_READ_RANGE(from, from_size);
503 ASAN_WRITE_RANGE(to, from_size);
504 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000505 return REAL(strcpy)(to, from); // NOLINT
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000506}
507
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000508#if ASAN_INTERCEPT_STRDUP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000509INTERCEPTOR(char*, strdup, const char *s) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700510 if (UNLIKELY(!asan_inited)) return internal_strdup(s);
Kostya Serebryanye1301912011-12-05 18:56:29 +0000511 ENSURE_ASAN_INITED();
Alexey Samsonovd530d892013-06-21 14:41:59 +0000512 uptr length = REAL(strlen)(s);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000513 if (flags()->replace_str) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000514 ASAN_READ_RANGE(s, length + 1);
515 }
Alexey Samsonovd530d892013-06-21 14:41:59 +0000516 GET_STACK_TRACE_MALLOC;
517 void *new_mem = asan_malloc(length + 1, &stack);
518 REAL(memcpy)(new_mem, s, length + 1);
519 return reinterpret_cast<char*>(new_mem);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000520}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000521#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000522
Stephen Hines6d186232014-11-26 17:56:19 -0800523INTERCEPTOR(SIZE_T, strlen, const char *s) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700524 if (UNLIKELY(!asan_inited)) return internal_strlen(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000525 // strlen is called from malloc_default_purgeable_zone()
526 // in __asan::ReplaceSystemAlloc() on Mac.
527 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000528 return REAL(strlen)(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000529 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000530 ENSURE_ASAN_INITED();
Stephen Hines6d186232014-11-26 17:56:19 -0800531 SIZE_T length = REAL(strlen)(s);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000532 if (flags()->replace_str) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000533 ASAN_READ_RANGE(s, length + 1);
534 }
535 return length;
536}
537
Stephen Hines6d186232014-11-26 17:56:19 -0800538INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
539 SIZE_T length = REAL(wcslen)(s);
Reid Klecknerb99228d2013-09-05 01:13:49 +0000540 if (!asan_init_is_running) {
541 ENSURE_ASAN_INITED();
542 ASAN_READ_RANGE(s, (length + 1) * sizeof(wchar_t));
543 }
544 return length;
545}
546
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000547INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
Kostya Serebryanye1301912011-12-05 18:56:29 +0000548 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000549 if (flags()->replace_str) {
Alexey Samsonovc9256972012-06-15 13:09:52 +0000550 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
Kostya Serebryanyc5e72a32011-12-28 19:24:31 +0000551 CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000552 ASAN_READ_RANGE(from, from_size);
553 ASAN_WRITE_RANGE(to, size);
554 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000555 return REAL(strncpy)(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000556}
557
Alexey Samsonov81a7a4a2012-03-24 09:10:50 +0000558#if ASAN_INTERCEPT_STRNLEN
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000559INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) {
Kostya Serebryanye1301912011-12-05 18:56:29 +0000560 ENSURE_ASAN_INITED();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000561 uptr length = REAL(strnlen)(s, maxlen);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000562 if (flags()->replace_str) {
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000563 ASAN_READ_RANGE(s, Min(length + 1, maxlen));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000564 }
565 return length;
566}
Alexey Samsonov81a7a4a2012-03-24 09:10:50 +0000567#endif // ASAN_INTERCEPT_STRNLEN
Kostya Serebryany547652c2012-01-09 19:35:11 +0000568
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000569static inline bool IsValidStrtolBase(int base) {
570 return (base == 0) || (2 <= base && base <= 36);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000571}
572
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000573static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000574 CHECK(endptr);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000575 if (nptr == *endptr) {
576 // No digits were found at strtol call, we need to find out the last
577 // symbol accessed by strtoll on our own.
578 // We get this symbol by skipping leading blanks and optional +/- sign.
579 while (IsSpace(*nptr)) nptr++;
580 if (*nptr == '+' || *nptr == '-') nptr++;
Stephen Hines6d186232014-11-26 17:56:19 -0800581 *endptr = const_cast<char *>(nptr);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000582 }
583 CHECK(*endptr >= nptr);
584}
585
Alexey Samsonov847f9322012-03-29 08:04:35 +0000586INTERCEPTOR(long, strtol, const char *nptr, // NOLINT
587 char **endptr, int base) {
588 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000589 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000590 return REAL(strtol)(nptr, endptr, base);
591 }
592 char *real_endptr;
593 long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000594 if (endptr != 0) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000595 *endptr = real_endptr;
596 }
597 if (IsValidStrtolBase(base)) {
598 FixRealStrtolEndptr(nptr, &real_endptr);
599 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
600 }
601 return result;
602}
603
604INTERCEPTOR(int, atoi, const char *nptr) {
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000605#if SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700606 if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000607#endif
Alexey Samsonov847f9322012-03-29 08:04:35 +0000608 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000609 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000610 return REAL(atoi)(nptr);
611 }
612 char *real_endptr;
613 // "man atoi" tells that behavior of atoi(nptr) is the same as
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000614 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
Alexey Samsonov847f9322012-03-29 08:04:35 +0000615 // parsed integer can't be stored in *long* type (even if it's
616 // different from int). So, we just imitate this behavior.
617 int result = REAL(strtol)(nptr, &real_endptr, 10);
618 FixRealStrtolEndptr(nptr, &real_endptr);
619 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
620 return result;
621}
622
623INTERCEPTOR(long, atol, const char *nptr) { // NOLINT
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000624#if SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700625 if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000626#endif
Alexey Samsonov847f9322012-03-29 08:04:35 +0000627 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000628 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000629 return REAL(atol)(nptr);
630 }
631 char *real_endptr;
632 long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT
633 FixRealStrtolEndptr(nptr, &real_endptr);
634 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
635 return result;
636}
637
638#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000639INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT
640 char **endptr, int base) {
641 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000642 if (!flags()->replace_str) {
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000643 return REAL(strtoll)(nptr, endptr, base);
644 }
645 char *real_endptr;
646 long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000647 if (endptr != 0) {
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000648 *endptr = real_endptr;
649 }
650 // If base has unsupported value, strtoll can exit with EINVAL
651 // without reading any characters. So do additional checks only
652 // if base is valid.
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000653 if (IsValidStrtolBase(base)) {
654 FixRealStrtolEndptr(nptr, &real_endptr);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000655 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
656 }
657 return result;
658}
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000659
Alexey Samsonov847f9322012-03-29 08:04:35 +0000660INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000661 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000662 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000663 return REAL(atoll)(nptr);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000664 }
665 char *real_endptr;
Alexey Samsonov847f9322012-03-29 08:04:35 +0000666 long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT
667 FixRealStrtolEndptr(nptr, &real_endptr);
668 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000669 return result;
670}
Alexey Samsonov847f9322012-03-29 08:04:35 +0000671#endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000672
Alexey Samsonov46efcb02013-05-24 11:46:56 +0000673static void AtCxaAtexit(void *unused) {
674 (void)unused;
675 StopInitOrderChecking();
676}
677
678#if ASAN_INTERCEPT___CXA_ATEXIT
679INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
680 void *dso_handle) {
Alexander Potapenkob527f7d2013-11-13 13:34:53 +0000681#if SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700682 if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
Alexander Potapenkob527f7d2013-11-13 13:34:53 +0000683#endif
Alexey Samsonov46efcb02013-05-24 11:46:56 +0000684 ENSURE_ASAN_INITED();
685 int res = REAL(__cxa_atexit)(func, arg, dso_handle);
686 REAL(__cxa_atexit)(AtCxaAtexit, 0, 0);
687 return res;
688}
689#endif // ASAN_INTERCEPT___CXA_ATEXIT
690
Stephen Hines6a211c52014-07-21 00:49:56 -0700691#if ASAN_INTERCEPT_FORK
692INTERCEPTOR(int, fork, void) {
693 ENSURE_ASAN_INITED();
694 if (common_flags()->coverage) CovBeforeFork();
695 int pid = REAL(fork)();
696 if (common_flags()->coverage) CovAfterFork(pid);
697 return pid;
698}
699#endif // ASAN_INTERCEPT_FORK
700
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000701#if SANITIZER_WINDOWS
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000702INTERCEPTOR_WINAPI(DWORD, CreateThread,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000703 void* security, uptr stack_size,
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000704 DWORD (__stdcall *start_routine)(void*), void* arg,
Alexey Samsonovb9a92842013-07-01 16:38:38 +0000705 DWORD thr_flags, void* tid) {
Alexey Samsonovbdd09662013-04-23 13:57:35 +0000706 // Strict init-order checking in thread-hostile.
Alexey Samsonov9465cbd2013-07-01 16:16:41 +0000707 if (flags()->strict_init_order)
708 StopInitOrderChecking();
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000709 GET_STACK_TRACE_THREAD;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000710 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonovdfe3f962013-03-22 07:48:23 +0000711 AsanThread *t = AsanThread::Create(start_routine, arg);
712 CreateThreadContextArgs args = { t, &stack };
Timur Iskhodzhanov8d2438a2013-05-29 17:26:25 +0000713 bool detached = false; // FIXME: how can we determine it on Windows?
Alexey Samsonovdfe3f962013-03-22 07:48:23 +0000714 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000715 return REAL(CreateThread)(security, stack_size,
Alexey Samsonovb9a92842013-07-01 16:38:38 +0000716 asan_thread_start, t, thr_flags, tid);
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000717}
718
719namespace __asan {
720void InitializeWindowsInterceptors() {
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000721 ASAN_INTERCEPT_FUNC(CreateThread);
Stephen Hines6d186232014-11-26 17:56:19 -0800722 ASAN_INTERCEPT_FUNC(RaiseException);
723 ASAN_INTERCEPT_FUNC(_except_handler3);
724 ASAN_INTERCEPT_FUNC(_except_handler4);
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000725}
726
727} // namespace __asan
728#endif
729
Kostya Serebryany547652c2012-01-09 19:35:11 +0000730// ---------------------- InitializeAsanInterceptors ---------------- {{{1
731namespace __asan {
732void InitializeAsanInterceptors() {
Kostya Serebryanyfdbdab52012-03-16 21:02:13 +0000733 static bool was_called_once;
734 CHECK(was_called_once == false);
735 was_called_once = true;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700736 InitializeCommonInterceptors();
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000737
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000738 // Intercept mem* functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000739 ASAN_INTERCEPT_FUNC(memcmp);
740 ASAN_INTERCEPT_FUNC(memmove);
741 ASAN_INTERCEPT_FUNC(memset);
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000742 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000743 ASAN_INTERCEPT_FUNC(memcpy);
Alexander Potapenko573fb4b2012-02-01 10:07:52 +0000744 }
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000745
746 // Intercept str* functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000747 ASAN_INTERCEPT_FUNC(strcat); // NOLINT
748 ASAN_INTERCEPT_FUNC(strchr);
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000749 ASAN_INTERCEPT_FUNC(strcpy); // NOLINT
750 ASAN_INTERCEPT_FUNC(strlen);
Reid Klecknerb99228d2013-09-05 01:13:49 +0000751 ASAN_INTERCEPT_FUNC(wcslen);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000752 ASAN_INTERCEPT_FUNC(strncat);
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000753 ASAN_INTERCEPT_FUNC(strncpy);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000754#if ASAN_INTERCEPT_STRDUP
755 ASAN_INTERCEPT_FUNC(strdup);
756#endif
757#if ASAN_INTERCEPT_STRNLEN
758 ASAN_INTERCEPT_FUNC(strnlen);
759#endif
Alexander Potapenko69563982013-02-05 15:57:12 +0000760#if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000761 ASAN_INTERCEPT_FUNC(index);
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000762#endif
Kostya Serebryany547652c2012-01-09 19:35:11 +0000763
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000764 ASAN_INTERCEPT_FUNC(atoi);
765 ASAN_INTERCEPT_FUNC(atol);
766 ASAN_INTERCEPT_FUNC(strtol);
Alexey Samsonov847f9322012-03-29 08:04:35 +0000767#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000768 ASAN_INTERCEPT_FUNC(atoll);
769 ASAN_INTERCEPT_FUNC(strtoll);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000770#endif
771
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000772 // Intecept signal- and jump-related functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000773 ASAN_INTERCEPT_FUNC(longjmp);
Alexey Samsonov34a32022012-03-26 09:07:29 +0000774#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000775 ASAN_INTERCEPT_FUNC(sigaction);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700776#if SANITIZER_ANDROID
777 ASAN_INTERCEPT_FUNC(bsd_signal);
778#else
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000779 ASAN_INTERCEPT_FUNC(signal);
Evgeniy Stepanov919c2472012-02-13 12:04:36 +0000780#endif
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700781#endif
Alexey Samsonov08700282012-11-23 09:46:34 +0000782#if ASAN_INTERCEPT_SWAPCONTEXT
783 ASAN_INTERCEPT_FUNC(swapcontext);
784#endif
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000785#if ASAN_INTERCEPT__LONGJMP
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000786 ASAN_INTERCEPT_FUNC(_longjmp);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000787#endif
788#if ASAN_INTERCEPT_SIGLONGJMP
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000789 ASAN_INTERCEPT_FUNC(siglongjmp);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000790#endif
791
792 // Intercept exception handling functions.
793#if ASAN_INTERCEPT___CXA_THROW
Stephen Hines6d186232014-11-26 17:56:19 -0800794 ASAN_INTERCEPT_FUNC(__cxa_throw);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000795#endif
796
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000797 // Intercept threading-related functions
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000798#if ASAN_INTERCEPT_PTHREAD_CREATE
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000799 ASAN_INTERCEPT_FUNC(pthread_create);
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000800#endif
801
Alexey Samsonov46efcb02013-05-24 11:46:56 +0000802 // Intercept atexit function.
803#if ASAN_INTERCEPT___CXA_ATEXIT
804 ASAN_INTERCEPT_FUNC(__cxa_atexit);
805#endif
806
Stephen Hines6a211c52014-07-21 00:49:56 -0700807#if ASAN_INTERCEPT_FORK
808 ASAN_INTERCEPT_FUNC(fork);
809#endif
810
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000811 // Some Windows-specific interceptors.
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000812#if SANITIZER_WINDOWS
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000813 InitializeWindowsInterceptors();
814#endif
815
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700816 VReport(1, "AddressSanitizer: libc interceptors initialized\n");
Kostya Serebryany547652c2012-01-09 19:35:11 +0000817}
818
819} // namespace __asan