blob: 4ae03ece25d1b66087a41606b774fe9b6fe16062 [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()
Evgeniy Stepanov4f32c0b2013-01-18 13:01:18 +0000151#include "sanitizer_common/sanitizer_common_interceptors.inc"
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000152
Evgeniy Stepanov881b6772013-04-12 14:57:03 +0000153#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(p, s)
154#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(p, s)
Evgeniy Stepanovae4e6fd2013-07-09 09:29:19 +0000155#define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
156 do { \
Evgeniy Stepanovdfab31b2013-10-29 18:29:39 +0000157 (void)(p); \
158 (void)(s); \
Evgeniy Stepanovae4e6fd2013-07-09 09:29:19 +0000159 } while (false)
160#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
161 do { \
Evgeniy Stepanovdfab31b2013-10-29 18:29:39 +0000162 (void)(p); \
163 (void)(s); \
Evgeniy Stepanovae4e6fd2013-07-09 09:29:19 +0000164 } while (false)
Evgeniy Stepanov881b6772013-04-12 14:57:03 +0000165#include "sanitizer_common/sanitizer_common_syscalls.inc"
166
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000167static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000168 AsanThread *t = (AsanThread*)arg;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000169 SetCurrentThread(t);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000170 return t->ThreadStart(GetTid());
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000171}
172
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000173#if ASAN_INTERCEPT_PTHREAD_CREATE
Evgeniy Stepanovb8ef9252012-02-22 12:31:25 +0000174INTERCEPTOR(int, pthread_create, void *thread,
175 void *attr, void *(*start_routine)(void*), void *arg) {
Sergey Matveevc6ac98d2013-07-08 12:57:24 +0000176 EnsureMainThreadIDIsCorrect();
Alexey Samsonovbdd09662013-04-23 13:57:35 +0000177 // Strict init-order checking in thread-hostile.
Alexey Samsonov9465cbd2013-07-01 16:16:41 +0000178 if (flags()->strict_init_order)
179 StopInitOrderChecking();
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000180 GET_STACK_TRACE_THREAD;
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000181 int detached = 0;
182 if (attr != 0)
Evgeniy Stepanovbb6bc9a2013-11-11 08:56:49 +0000183 REAL(pthread_attr_getdetachstate)(attr, &detached);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000184
Alexey Samsonov89c13842013-03-20 09:23:28 +0000185 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000186 AsanThread *t = AsanThread::Create(start_routine, arg);
187 CreateThreadContextArgs args = { t, &stack };
188 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000189 return REAL(pthread_create)(thread, attr, asan_thread_start, t);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000190}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000191#endif // ASAN_INTERCEPT_PTHREAD_CREATE
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000192
Alexey Samsonov34a32022012-03-26 09:07:29 +0000193#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700194
195#if SANITIZER_ANDROID
196INTERCEPTOR(void*, bsd_signal, int signum, void *handler) {
197 if (!AsanInterceptsSignal(signum) ||
198 common_flags()->allow_user_segv_handler) {
199 return REAL(bsd_signal)(signum, handler);
200 }
201 return 0;
202}
203#else
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000204INTERCEPTOR(void*, signal, int signum, void *handler) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700205 if (!AsanInterceptsSignal(signum) ||
206 common_flags()->allow_user_segv_handler) {
Alexander Potapenko034bda52012-04-16 08:33:01 +0000207 return REAL(signal)(signum, handler);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000208 }
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000209 return 0;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000210}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700211#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000212
Alexey Samsonovda13ba82012-02-16 17:00:45 +0000213INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
214 struct sigaction *oldact) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700215 if (!AsanInterceptsSignal(signum) ||
216 common_flags()->allow_user_segv_handler) {
Alexander Potapenko034bda52012-04-16 08:33:01 +0000217 return REAL(sigaction)(signum, act, oldact);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000218 }
Alexander Potapenko034bda52012-04-16 08:33:01 +0000219 return 0;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000220}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700221
222namespace __sanitizer {
223int real_sigaction(int signum, const void *act, void *oldact) {
224 return REAL(sigaction)(signum,
225 (struct sigaction *)act, (struct sigaction *)oldact);
226}
227} // namespace __sanitizer
228
Evgeniy Stepanove1ba0002013-03-19 15:26:41 +0000229#elif SANITIZER_POSIX
Alexey Samsonov34a32022012-03-26 09:07:29 +0000230// We need to have defined REAL(sigaction) on posix systems.
231DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
Alexey Samsonovfdde5a92013-06-10 14:17:08 +0000232 struct sigaction *oldact)
Alexey Samsonov34a32022012-03-26 09:07:29 +0000233#endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000234
Alexey Samsonov08700282012-11-23 09:46:34 +0000235#if ASAN_INTERCEPT_SWAPCONTEXT
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000236static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
237 // Align to page size.
238 uptr PageSize = GetPageSizeCached();
239 uptr bottom = stack & ~(PageSize - 1);
240 ssize += stack - bottom;
241 ssize = RoundUpTo(ssize, PageSize);
242 static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb
Kostya Serebryany541cfb12013-01-18 11:30:36 +0000243 if (ssize && ssize <= kMaxSaneContextStackSize) {
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000244 PoisonShadow(bottom, ssize, 0);
245 }
246}
247
Alexey Samsonov08700282012-11-23 09:46:34 +0000248INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
249 struct ucontext_t *ucp) {
250 static bool reported_warning = false;
251 if (!reported_warning) {
252 Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
253 "functions and may produce false positives in some cases!\n");
254 reported_warning = true;
255 }
256 // Clear shadow memory for new context (it may share stack
257 // with current context).
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000258 uptr stack, ssize;
259 ReadContextStack(ucp, &stack, &ssize);
260 ClearShadowMemoryForContextStack(stack, ssize);
Alexey Samsonov08700282012-11-23 09:46:34 +0000261 int res = REAL(swapcontext)(oucp, ucp);
262 // swapcontext technically does not return, but program may swap context to
263 // "oucp" later, that would look as if swapcontext() returned 0.
264 // We need to clear shadow for ucp once again, as it may be in arbitrary
265 // state.
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000266 ClearShadowMemoryForContextStack(stack, ssize);
Alexey Samsonov08700282012-11-23 09:46:34 +0000267 return res;
268}
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000269#endif // ASAN_INTERCEPT_SWAPCONTEXT
Alexey Samsonov08700282012-11-23 09:46:34 +0000270
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000271INTERCEPTOR(void, longjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000272 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000273 REAL(longjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000274}
275
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000276#if ASAN_INTERCEPT__LONGJMP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000277INTERCEPTOR(void, _longjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000278 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000279 REAL(_longjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000280}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000281#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000282
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000283#if ASAN_INTERCEPT_SIGLONGJMP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000284INTERCEPTOR(void, siglongjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000285 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000286 REAL(siglongjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000287}
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000288#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000289
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000290#if ASAN_INTERCEPT___CXA_THROW
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000291INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000292 CHECK(REAL(__cxa_throw));
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000293 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000294 REAL(__cxa_throw)(a, b, c);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000295}
296#endif
297
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700298#if ASAN_INTERCEPT_MLOCKX
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000299// intercept mlock and friends.
300// Since asan maps 16T of RAM, mlock is completely unfriendly to asan.
301// All functions return 0 (success).
302static void MlockIsUnsupported() {
Timur Iskhodzhanov8d2438a2013-05-29 17:26:25 +0000303 static bool printed = false;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000304 if (printed) return;
305 printed = true;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700306 VPrintf(1,
307 "INFO: AddressSanitizer ignores "
308 "mlock/mlockall/munlock/munlockall\n");
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000309}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000310
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000311INTERCEPTOR(int, mlock, const void *addr, uptr len) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000312 MlockIsUnsupported();
313 return 0;
314}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000315
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000316INTERCEPTOR(int, munlock, const void *addr, uptr len) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000317 MlockIsUnsupported();
318 return 0;
319}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000320
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000321INTERCEPTOR(int, mlockall, int flags) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000322 MlockIsUnsupported();
323 return 0;
324}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000325
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000326INTERCEPTOR(int, munlockall, void) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000327 MlockIsUnsupported();
328 return 0;
329}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700330#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000331
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000332static inline int CharCmp(unsigned char c1, unsigned char c2) {
333 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
334}
335
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000336INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700337 if (UNLIKELY(!asan_inited)) return internal_memcmp(a1, a2, size);
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000338 ENSURE_ASAN_INITED();
Kostya Serebryany1b057b22013-02-26 07:25:18 +0000339 if (flags()->replace_intrin) {
Alexander Potapenko8bd5e742013-02-28 14:09:30 +0000340 if (flags()->strict_memcmp) {
341 // Check the entire regions even if the first bytes of the buffers are
342 // different.
343 ASAN_READ_RANGE(a1, size);
344 ASAN_READ_RANGE(a2, size);
345 // Fallthrough to REAL(memcmp) below.
346 } else {
347 unsigned char c1 = 0, c2 = 0;
348 const unsigned char *s1 = (const unsigned char*)a1;
349 const unsigned char *s2 = (const unsigned char*)a2;
350 uptr i;
351 for (i = 0; i < size; i++) {
352 c1 = s1[i];
353 c2 = s2[i];
354 if (c1 != c2) break;
355 }
356 ASAN_READ_RANGE(s1, Min(i + 1, size));
357 ASAN_READ_RANGE(s2, Min(i + 1, size));
358 return CharCmp(c1, c2);
359 }
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000360 }
Kostya Serebryany1b057b22013-02-26 07:25:18 +0000361 return REAL(memcmp(a1, a2, size));
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000362}
363
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700364void *__asan_memcpy(void *to, const void *from, uptr size) {
365 if (UNLIKELY(!asan_inited)) return internal_memcpy(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000366 // memcpy is called during __asan_init() from the internals
367 // of printf(...).
368 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000369 return REAL(memcpy)(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000370 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000371 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000372 if (flags()->replace_intrin) {
Kostya Serebryanyc655cfa2012-01-17 18:43:52 +0000373 if (to != from) {
374 // We do not treat memcpy with to==from as a bug.
375 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
376 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
377 }
Alexander Potapenkoc75d8482012-12-10 16:02:13 +0000378 ASAN_READ_RANGE(from, size);
379 ASAN_WRITE_RANGE(to, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000380 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700381 return REAL(memcpy)(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000382}
383
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700384void *__asan_memset(void *block, int c, uptr size) {
385 if (UNLIKELY(!asan_inited)) return internal_memset(block, c, size);
Alexander Potapenkoebb97022012-03-29 12:20:47 +0000386 // memset is called inside Printf.
Kostya Serebryanye1301912011-12-05 18:56:29 +0000387 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000388 return REAL(memset)(block, c, size);
Kostya Serebryanye1301912011-12-05 18:56:29 +0000389 }
390 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000391 if (flags()->replace_intrin) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000392 ASAN_WRITE_RANGE(block, size);
393 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000394 return REAL(memset)(block, c, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000395}
396
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700397void *__asan_memmove(void *to, const void *from, uptr size) {
398 if (UNLIKELY(!asan_inited))
399 return internal_memmove(to, from, size);
400 ENSURE_ASAN_INITED();
401 if (flags()->replace_intrin) {
402 ASAN_READ_RANGE(from, size);
403 ASAN_WRITE_RANGE(to, size);
404 }
405 return internal_memmove(to, from, size);
406}
407
408INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
409 return __asan_memmove(to, from, size);
410}
411
412INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
413#if !SANITIZER_MAC
414 return __asan_memcpy(to, from, size);
415#else
416 // At least on 10.7 and 10.8 both memcpy() and memmove() are being replaced
417 // with WRAP(memcpy). As a result, false positives are reported for memmove()
418 // calls. If we just disable error reporting with
419 // ASAN_OPTIONS=replace_intrin=0, memmove() is still replaced with
420 // internal_memcpy(), which may lead to crashes, see
421 // http://llvm.org/bugs/show_bug.cgi?id=16362.
422 return __asan_memmove(to, from, size);
423#endif // !SANITIZER_MAC
424}
425
426INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
427 return __asan_memset(block, c, size);
428}
429
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000430INTERCEPTOR(char*, strchr, const char *str, int c) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700431 if (UNLIKELY(!asan_inited)) return internal_strchr(str, c);
Alexander Potapenko8d6e3f72012-09-10 08:35:12 +0000432 // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is
433 // used.
434 if (asan_init_is_running) {
435 return REAL(strchr)(str, c);
436 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000437 ENSURE_ASAN_INITED();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000438 char *result = REAL(strchr)(str, c);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000439 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000440 uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000441 ASAN_READ_RANGE(str, bytes_read);
442 }
443 return result;
444}
445
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000446#if ASAN_INTERCEPT_INDEX
447# if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
Evgeniy Stepanov4b0c5f22012-02-13 12:12:32 +0000448INTERCEPTOR(char*, index, const char *string, int c)
Alexander Potapenko72bbfd42013-02-21 15:15:43 +0000449 ALIAS(WRAPPER_NAME(strchr));
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000450# else
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000451# if SANITIZER_MAC
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000452DECLARE_REAL(char*, index, const char *string, int c)
453OVERRIDE_FUNCTION(index, strchr);
454# else
Alexey Samsonovfdde5a92013-06-10 14:17:08 +0000455DEFINE_REAL(char*, index, const char *string, int c)
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000456# endif
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000457# endif
458#endif // ASAN_INTERCEPT_INDEX
Kostya Serebryanyaf0f01d2011-12-28 02:24:50 +0000459
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000460// For both strcat() and strncat() we need to check the validity of |to|
461// argument irrespective of the |from| length.
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000462INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000463 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000464 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000465 uptr from_length = REAL(strlen)(from);
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000466 ASAN_READ_RANGE(from, from_length + 1);
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000467 uptr to_length = REAL(strlen)(to);
468 ASAN_READ_RANGE(to, to_length);
469 ASAN_WRITE_RANGE(to + to_length, from_length + 1);
470 // If the copying actually happens, the |from| string should not overlap
471 // with the resulting string starting at |to|, which has a length of
472 // to_length + from_length + 1.
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000473 if (from_length > 0) {
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000474 CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
475 from, from_length + 1);
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000476 }
477 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000478 return REAL(strcat)(to, from); // NOLINT
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000479}
480
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000481INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
482 ENSURE_ASAN_INITED();
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000483 if (flags()->replace_str) {
Alexey Samsonovc9256972012-06-15 13:09:52 +0000484 uptr from_length = MaybeRealStrnlen(from, size);
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000485 uptr copy_length = Min(size, from_length + 1);
486 ASAN_READ_RANGE(from, copy_length);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000487 uptr to_length = REAL(strlen)(to);
488 ASAN_READ_RANGE(to, to_length);
489 ASAN_WRITE_RANGE(to + to_length, from_length + 1);
490 if (from_length > 0) {
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000491 CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
492 from, copy_length);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000493 }
494 }
495 return REAL(strncat)(to, from, size);
496}
497
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000498INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000499#if SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700500 if (UNLIKELY(!asan_inited)) return REAL(strcpy)(to, from); // NOLINT
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000501#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000502 // strcpy is called from malloc_default_purgeable_zone()
503 // in __asan::ReplaceSystemAlloc() on Mac.
504 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000505 return REAL(strcpy)(to, from); // NOLINT
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000506 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000507 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000508 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000509 uptr from_size = REAL(strlen)(from) + 1;
Kostya Serebryanyc5e72a32011-12-28 19:24:31 +0000510 CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000511 ASAN_READ_RANGE(from, from_size);
512 ASAN_WRITE_RANGE(to, from_size);
513 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000514 return REAL(strcpy)(to, from); // NOLINT
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000515}
516
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000517#if ASAN_INTERCEPT_STRDUP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000518INTERCEPTOR(char*, strdup, const char *s) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700519 if (UNLIKELY(!asan_inited)) return internal_strdup(s);
Kostya Serebryanye1301912011-12-05 18:56:29 +0000520 ENSURE_ASAN_INITED();
Alexey Samsonovd530d892013-06-21 14:41:59 +0000521 uptr length = REAL(strlen)(s);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000522 if (flags()->replace_str) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000523 ASAN_READ_RANGE(s, length + 1);
524 }
Alexey Samsonovd530d892013-06-21 14:41:59 +0000525 GET_STACK_TRACE_MALLOC;
526 void *new_mem = asan_malloc(length + 1, &stack);
527 REAL(memcpy)(new_mem, s, length + 1);
528 return reinterpret_cast<char*>(new_mem);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000529}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000530#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000531
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000532INTERCEPTOR(uptr, strlen, const char *s) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700533 if (UNLIKELY(!asan_inited)) return internal_strlen(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000534 // strlen is called from malloc_default_purgeable_zone()
535 // in __asan::ReplaceSystemAlloc() on Mac.
536 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000537 return REAL(strlen)(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000538 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000539 ENSURE_ASAN_INITED();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000540 uptr length = REAL(strlen)(s);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000541 if (flags()->replace_str) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000542 ASAN_READ_RANGE(s, length + 1);
543 }
544 return length;
545}
546
Reid Klecknerb99228d2013-09-05 01:13:49 +0000547INTERCEPTOR(uptr, wcslen, const wchar_t *s) {
548 uptr length = REAL(wcslen)(s);
549 if (!asan_init_is_running) {
550 ENSURE_ASAN_INITED();
551 ASAN_READ_RANGE(s, (length + 1) * sizeof(wchar_t));
552 }
553 return length;
554}
555
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000556INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
Kostya Serebryanye1301912011-12-05 18:56:29 +0000557 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000558 if (flags()->replace_str) {
Alexey Samsonovc9256972012-06-15 13:09:52 +0000559 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
Kostya Serebryanyc5e72a32011-12-28 19:24:31 +0000560 CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000561 ASAN_READ_RANGE(from, from_size);
562 ASAN_WRITE_RANGE(to, size);
563 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000564 return REAL(strncpy)(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000565}
566
Alexey Samsonov81a7a4a2012-03-24 09:10:50 +0000567#if ASAN_INTERCEPT_STRNLEN
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000568INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) {
Kostya Serebryanye1301912011-12-05 18:56:29 +0000569 ENSURE_ASAN_INITED();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000570 uptr length = REAL(strnlen)(s, maxlen);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000571 if (flags()->replace_str) {
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000572 ASAN_READ_RANGE(s, Min(length + 1, maxlen));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000573 }
574 return length;
575}
Alexey Samsonov81a7a4a2012-03-24 09:10:50 +0000576#endif // ASAN_INTERCEPT_STRNLEN
Kostya Serebryany547652c2012-01-09 19:35:11 +0000577
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000578static inline bool IsValidStrtolBase(int base) {
579 return (base == 0) || (2 <= base && base <= 36);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000580}
581
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000582static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000583 CHECK(endptr);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000584 if (nptr == *endptr) {
585 // No digits were found at strtol call, we need to find out the last
586 // symbol accessed by strtoll on our own.
587 // We get this symbol by skipping leading blanks and optional +/- sign.
588 while (IsSpace(*nptr)) nptr++;
589 if (*nptr == '+' || *nptr == '-') nptr++;
590 *endptr = (char*)nptr;
591 }
592 CHECK(*endptr >= nptr);
593}
594
Alexey Samsonov847f9322012-03-29 08:04:35 +0000595INTERCEPTOR(long, strtol, const char *nptr, // NOLINT
596 char **endptr, int base) {
597 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000598 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000599 return REAL(strtol)(nptr, endptr, base);
600 }
601 char *real_endptr;
602 long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000603 if (endptr != 0) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000604 *endptr = real_endptr;
605 }
606 if (IsValidStrtolBase(base)) {
607 FixRealStrtolEndptr(nptr, &real_endptr);
608 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
609 }
610 return result;
611}
612
613INTERCEPTOR(int, atoi, const char *nptr) {
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000614#if SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700615 if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000616#endif
Alexey Samsonov847f9322012-03-29 08:04:35 +0000617 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000618 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000619 return REAL(atoi)(nptr);
620 }
621 char *real_endptr;
622 // "man atoi" tells that behavior of atoi(nptr) is the same as
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000623 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
Alexey Samsonov847f9322012-03-29 08:04:35 +0000624 // parsed integer can't be stored in *long* type (even if it's
625 // different from int). So, we just imitate this behavior.
626 int result = REAL(strtol)(nptr, &real_endptr, 10);
627 FixRealStrtolEndptr(nptr, &real_endptr);
628 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
629 return result;
630}
631
632INTERCEPTOR(long, atol, const char *nptr) { // NOLINT
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000633#if SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700634 if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000635#endif
Alexey Samsonov847f9322012-03-29 08:04:35 +0000636 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000637 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000638 return REAL(atol)(nptr);
639 }
640 char *real_endptr;
641 long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT
642 FixRealStrtolEndptr(nptr, &real_endptr);
643 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
644 return result;
645}
646
647#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000648INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT
649 char **endptr, int base) {
650 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000651 if (!flags()->replace_str) {
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000652 return REAL(strtoll)(nptr, endptr, base);
653 }
654 char *real_endptr;
655 long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000656 if (endptr != 0) {
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000657 *endptr = real_endptr;
658 }
659 // If base has unsupported value, strtoll can exit with EINVAL
660 // without reading any characters. So do additional checks only
661 // if base is valid.
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000662 if (IsValidStrtolBase(base)) {
663 FixRealStrtolEndptr(nptr, &real_endptr);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000664 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
665 }
666 return result;
667}
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000668
Alexey Samsonov847f9322012-03-29 08:04:35 +0000669INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000670 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000671 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000672 return REAL(atoll)(nptr);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000673 }
674 char *real_endptr;
Alexey Samsonov847f9322012-03-29 08:04:35 +0000675 long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT
676 FixRealStrtolEndptr(nptr, &real_endptr);
677 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000678 return result;
679}
Alexey Samsonov847f9322012-03-29 08:04:35 +0000680#endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000681
Alexey Samsonov46efcb02013-05-24 11:46:56 +0000682static void AtCxaAtexit(void *unused) {
683 (void)unused;
684 StopInitOrderChecking();
685}
686
687#if ASAN_INTERCEPT___CXA_ATEXIT
688INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
689 void *dso_handle) {
Alexander Potapenkob527f7d2013-11-13 13:34:53 +0000690#if SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700691 if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
Alexander Potapenkob527f7d2013-11-13 13:34:53 +0000692#endif
Alexey Samsonov46efcb02013-05-24 11:46:56 +0000693 ENSURE_ASAN_INITED();
694 int res = REAL(__cxa_atexit)(func, arg, dso_handle);
695 REAL(__cxa_atexit)(AtCxaAtexit, 0, 0);
696 return res;
697}
698#endif // ASAN_INTERCEPT___CXA_ATEXIT
699
Stephen Hines6a211c52014-07-21 00:49:56 -0700700#if ASAN_INTERCEPT_FORK
701INTERCEPTOR(int, fork, void) {
702 ENSURE_ASAN_INITED();
703 if (common_flags()->coverage) CovBeforeFork();
704 int pid = REAL(fork)();
705 if (common_flags()->coverage) CovAfterFork(pid);
706 return pid;
707}
708#endif // ASAN_INTERCEPT_FORK
709
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000710#if SANITIZER_WINDOWS
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000711INTERCEPTOR_WINAPI(DWORD, CreateThread,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000712 void* security, uptr stack_size,
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000713 DWORD (__stdcall *start_routine)(void*), void* arg,
Alexey Samsonovb9a92842013-07-01 16:38:38 +0000714 DWORD thr_flags, void* tid) {
Alexey Samsonovbdd09662013-04-23 13:57:35 +0000715 // Strict init-order checking in thread-hostile.
Alexey Samsonov9465cbd2013-07-01 16:16:41 +0000716 if (flags()->strict_init_order)
717 StopInitOrderChecking();
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000718 GET_STACK_TRACE_THREAD;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000719 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonovdfe3f962013-03-22 07:48:23 +0000720 AsanThread *t = AsanThread::Create(start_routine, arg);
721 CreateThreadContextArgs args = { t, &stack };
Timur Iskhodzhanov8d2438a2013-05-29 17:26:25 +0000722 bool detached = false; // FIXME: how can we determine it on Windows?
Alexey Samsonovdfe3f962013-03-22 07:48:23 +0000723 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000724 return REAL(CreateThread)(security, stack_size,
Alexey Samsonovb9a92842013-07-01 16:38:38 +0000725 asan_thread_start, t, thr_flags, tid);
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000726}
727
728namespace __asan {
729void InitializeWindowsInterceptors() {
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000730 ASAN_INTERCEPT_FUNC(CreateThread);
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000731}
732
733} // namespace __asan
734#endif
735
Kostya Serebryany547652c2012-01-09 19:35:11 +0000736// ---------------------- InitializeAsanInterceptors ---------------- {{{1
737namespace __asan {
738void InitializeAsanInterceptors() {
Kostya Serebryanyfdbdab52012-03-16 21:02:13 +0000739 static bool was_called_once;
740 CHECK(was_called_once == false);
741 was_called_once = true;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700742 InitializeCommonInterceptors();
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000743
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000744 // Intercept mem* functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000745 ASAN_INTERCEPT_FUNC(memcmp);
746 ASAN_INTERCEPT_FUNC(memmove);
747 ASAN_INTERCEPT_FUNC(memset);
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000748 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000749 ASAN_INTERCEPT_FUNC(memcpy);
Alexander Potapenko573fb4b2012-02-01 10:07:52 +0000750 }
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000751
752 // Intercept str* functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000753 ASAN_INTERCEPT_FUNC(strcat); // NOLINT
754 ASAN_INTERCEPT_FUNC(strchr);
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000755 ASAN_INTERCEPT_FUNC(strcpy); // NOLINT
756 ASAN_INTERCEPT_FUNC(strlen);
Reid Klecknerb99228d2013-09-05 01:13:49 +0000757 ASAN_INTERCEPT_FUNC(wcslen);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000758 ASAN_INTERCEPT_FUNC(strncat);
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000759 ASAN_INTERCEPT_FUNC(strncpy);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000760#if ASAN_INTERCEPT_STRDUP
761 ASAN_INTERCEPT_FUNC(strdup);
762#endif
763#if ASAN_INTERCEPT_STRNLEN
764 ASAN_INTERCEPT_FUNC(strnlen);
765#endif
Alexander Potapenko69563982013-02-05 15:57:12 +0000766#if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000767 ASAN_INTERCEPT_FUNC(index);
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000768#endif
Kostya Serebryany547652c2012-01-09 19:35:11 +0000769
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000770 ASAN_INTERCEPT_FUNC(atoi);
771 ASAN_INTERCEPT_FUNC(atol);
772 ASAN_INTERCEPT_FUNC(strtol);
Alexey Samsonov847f9322012-03-29 08:04:35 +0000773#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000774 ASAN_INTERCEPT_FUNC(atoll);
775 ASAN_INTERCEPT_FUNC(strtoll);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000776#endif
777
Alexander Potapenkof0c6de32012-08-15 09:46:45 +0000778#if ASAN_INTERCEPT_MLOCKX
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000779 // Intercept mlock/munlock.
780 ASAN_INTERCEPT_FUNC(mlock);
781 ASAN_INTERCEPT_FUNC(munlock);
782 ASAN_INTERCEPT_FUNC(mlockall);
783 ASAN_INTERCEPT_FUNC(munlockall);
Alexander Potapenkof0c6de32012-08-15 09:46:45 +0000784#endif
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000785
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000786 // Intecept signal- and jump-related functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000787 ASAN_INTERCEPT_FUNC(longjmp);
Alexey Samsonov34a32022012-03-26 09:07:29 +0000788#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000789 ASAN_INTERCEPT_FUNC(sigaction);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700790#if SANITIZER_ANDROID
791 ASAN_INTERCEPT_FUNC(bsd_signal);
792#else
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000793 ASAN_INTERCEPT_FUNC(signal);
Evgeniy Stepanov919c2472012-02-13 12:04:36 +0000794#endif
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700795#endif
Alexey Samsonov08700282012-11-23 09:46:34 +0000796#if ASAN_INTERCEPT_SWAPCONTEXT
797 ASAN_INTERCEPT_FUNC(swapcontext);
798#endif
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000799#if ASAN_INTERCEPT__LONGJMP
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000800 ASAN_INTERCEPT_FUNC(_longjmp);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000801#endif
802#if ASAN_INTERCEPT_SIGLONGJMP
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000803 ASAN_INTERCEPT_FUNC(siglongjmp);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000804#endif
805
806 // Intercept exception handling functions.
807#if ASAN_INTERCEPT___CXA_THROW
808 INTERCEPT_FUNCTION(__cxa_throw);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000809#endif
810
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000811 // Intercept threading-related functions
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000812#if ASAN_INTERCEPT_PTHREAD_CREATE
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000813 ASAN_INTERCEPT_FUNC(pthread_create);
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000814#endif
815
Alexey Samsonov46efcb02013-05-24 11:46:56 +0000816 // Intercept atexit function.
817#if ASAN_INTERCEPT___CXA_ATEXIT
818 ASAN_INTERCEPT_FUNC(__cxa_atexit);
819#endif
820
Stephen Hines6a211c52014-07-21 00:49:56 -0700821#if ASAN_INTERCEPT_FORK
822 ASAN_INTERCEPT_FUNC(fork);
823#endif
824
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000825 // Some Windows-specific interceptors.
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000826#if SANITIZER_WINDOWS
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000827 InitializeWindowsInterceptors();
828#endif
829
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700830 VReport(1, "AddressSanitizer: libc interceptors initialized\n");
Kostya Serebryany547652c2012-01-09 19:35:11 +0000831}
832
833} // namespace __asan