blob: bfd82b4a1d366f6588eeaa0015c4206f46dca89b [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"
Alexander Potapenko05bf9a52012-08-15 15:24:48 +000017#include "asan_intercepted_functions.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000018#include "asan_internal.h"
19#include "asan_mapping.h"
Alexey Samsonov7e843492013-03-28 15:42:43 +000020#include "asan_poisoning.h"
Alexey Samsonov487fee72012-08-09 08:32:33 +000021#include "asan_report.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000022#include "asan_stack.h"
23#include "asan_stats.h"
Alexey Samsonov5b290182012-02-08 19:52:01 +000024#include "interception/interception.h"
Alexey Samsonovc0d78c12012-06-04 13:27:49 +000025#include "sanitizer_common/sanitizer_libc.h"
26
Kostya Serebryany1e172b42011-11-30 01:07:02 +000027namespace __asan {
28
Kostya Serebryanya84805f2013-02-21 07:07:39 +000029// Return true if we can quickly decide that the region is unpoisoned.
30static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) {
31 if (size == 0) return true;
32 if (size <= 32)
33 return !AddressIsPoisoned(beg) &&
34 !AddressIsPoisoned(beg + size - 1) &&
35 !AddressIsPoisoned(beg + size / 2);
36 return false;
37}
38
Kostya Serebryany1e172b42011-11-30 01:07:02 +000039// We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
40// and ASAN_WRITE_RANGE as macro instead of function so
41// that no extra frames are created, and stack trace contains
42// relevant information only.
Kostya Serebryanyeb280932012-12-28 15:24:16 +000043// We check all shadow bytes.
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000044#define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \
45 uptr __offset = (uptr)(offset); \
46 uptr __size = (uptr)(size); \
Kostya Serebryany1b057b22013-02-26 07:25:18 +000047 uptr __bad = 0; \
Kostya Serebryanya84805f2013-02-21 07:07:39 +000048 if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \
Kostya Serebryany1b057b22013-02-26 07:25:18 +000049 (__bad = __asan_region_is_poisoned(__offset, __size))) { \
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000050 GET_CURRENT_PC_BP_SP; \
Kostya Serebryany1b057b22013-02-26 07:25:18 +000051 __asan_report_error(pc, bp, sp, __bad, isWrite, __size); \
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000052 } \
53 } while (0)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000054
Kostya Serebryanyeb280932012-12-28 15:24:16 +000055#define ASAN_READ_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, false)
56#define ASAN_WRITE_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, true);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000057
58// Behavior of functions like "memcpy" or "strcpy" is undefined
59// if memory intervals overlap. We report error in this case.
60// Macro is used to avoid creation of new frames.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000061static inline bool RangesOverlap(const char *offset1, uptr length1,
62 const char *offset2, uptr length2) {
Kostya Serebryany0985ca22011-12-28 19:08:49 +000063 return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
Kostya Serebryany1e172b42011-11-30 01:07:02 +000064}
Kostya Serebryanyc5e72a32011-12-28 19:24:31 +000065#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
Kostya Serebryany1e172b42011-11-30 01:07:02 +000066 const char *offset1 = (const char*)_offset1; \
67 const char *offset2 = (const char*)_offset2; \
Kostya Serebryany0985ca22011-12-28 19:08:49 +000068 if (RangesOverlap(offset1, length1, offset2, length2)) { \
Kostya Serebryanya30c8f92012-12-13 09:34:23 +000069 GET_STACK_TRACE_FATAL_HERE; \
Alexey Samsonov487fee72012-08-09 08:32:33 +000070 ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
71 offset2, length2, &stack); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +000072 } \
Kostya Serebryanye1301912011-12-05 18:56:29 +000073} while (0)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000074
Kostya Serebryanye1301912011-12-05 18:56:29 +000075#define ENSURE_ASAN_INITED() do { \
76 CHECK(!asan_init_is_running); \
77 if (!asan_inited) { \
78 __asan_init(); \
79 } \
80} while (0)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081
Alexey Samsonovc9256972012-06-15 13:09:52 +000082static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
Alexey Samsonov81a7a4a2012-03-24 09:10:50 +000083#if ASAN_INTERCEPT_STRNLEN
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000084 if (REAL(strnlen) != 0) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +000085 return REAL(strnlen)(s, maxlen);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000086 }
Alexey Samsonovf2598fc2012-02-02 10:39:40 +000087#endif
Alexey Samsonovc9256972012-06-15 13:09:52 +000088 return internal_strnlen(s, maxlen);
Kostya Serebryanya4ccf872012-01-09 22:20:49 +000089}
Kostya Serebryany1e172b42011-11-30 01:07:02 +000090
Kostya Serebryanyc20b3212013-01-18 06:43:13 +000091void SetThreadName(const char *name) {
Alexey Samsonov89c13842013-03-20 09:23:28 +000092 AsanThread *t = GetCurrentThread();
Kostya Serebryanyc20b3212013-01-18 06:43:13 +000093 if (t)
Alexey Samsonovdef1be92013-03-21 11:23:41 +000094 asanThreadRegistry().SetThreadName(t->tid(), name);
Kostya Serebryanyc20b3212013-01-18 06:43:13 +000095}
96
Alexey Samsonovbdd09662013-04-23 13:57:35 +000097static void DisableStrictInitOrderChecker() {
98 if (flags()->strict_init_order)
99 flags()->check_initialization_order = false;
100}
101
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000102} // namespace __asan
103
104// ---------------------- Wrappers ---------------- {{{1
105using namespace __asan; // NOLINT
106
Evgeniy Stepanov82a90802013-01-18 13:12:56 +0000107#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
108 ASAN_WRITE_RANGE(ptr, size)
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +0000109#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) ASAN_READ_RANGE(ptr, size)
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +0000110#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
111 do { \
112 if (asan_init_is_running) return REAL(func)(__VA_ARGS__); \
113 ctx = 0; \
114 (void) ctx; \
115 ENSURE_ASAN_INITED(); \
Evgeniy Stepanov82a90802013-01-18 13:12:56 +0000116 } while (false)
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +0000117#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
118 do { \
119 } while (false)
120#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
121 do { \
122 } while (false)
123#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
124 do { \
125 } while (false)
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +0000126#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
Evgeniy Stepanov4f32c0b2013-01-18 13:01:18 +0000127#include "sanitizer_common/sanitizer_common_interceptors.inc"
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000128
Evgeniy Stepanov881b6772013-04-12 14:57:03 +0000129#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(p, s)
130#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(p, s)
131#define COMMON_SYSCALL_POST_READ_RANGE(p, s)
132#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s)
133#include "sanitizer_common/sanitizer_common_syscalls.inc"
134
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000135static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000136 AsanThread *t = (AsanThread*)arg;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000137 SetCurrentThread(t);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000138 return t->ThreadStart(GetTid());
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000139}
140
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000141#if ASAN_INTERCEPT_PTHREAD_CREATE
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000142extern "C" int pthread_attr_getdetachstate(void *attr, int *v);
143
Evgeniy Stepanovb8ef9252012-02-22 12:31:25 +0000144INTERCEPTOR(int, pthread_create, void *thread,
145 void *attr, void *(*start_routine)(void*), void *arg) {
Alexey Samsonovbdd09662013-04-23 13:57:35 +0000146 // Strict init-order checking in thread-hostile.
147 DisableStrictInitOrderChecker();
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000148 GET_STACK_TRACE_THREAD;
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000149 int detached = 0;
150 if (attr != 0)
151 pthread_attr_getdetachstate(attr, &detached);
152
Alexey Samsonov89c13842013-03-20 09:23:28 +0000153 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000154 AsanThread *t = AsanThread::Create(start_routine, arg);
155 CreateThreadContextArgs args = { t, &stack };
156 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000157 return REAL(pthread_create)(thread, attr, asan_thread_start, t);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000158}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000159#endif // ASAN_INTERCEPT_PTHREAD_CREATE
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000160
Alexey Samsonov34a32022012-03-26 09:07:29 +0000161#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000162INTERCEPTOR(void*, signal, int signum, void *handler) {
Alexey Samsonov332bf332013-04-25 10:52:15 +0000163 if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) {
Alexander Potapenko034bda52012-04-16 08:33:01 +0000164 return REAL(signal)(signum, handler);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000165 }
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000166 return 0;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000167}
168
Alexey Samsonovda13ba82012-02-16 17:00:45 +0000169INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
170 struct sigaction *oldact) {
Alexey Samsonov332bf332013-04-25 10:52:15 +0000171 if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) {
Alexander Potapenko034bda52012-04-16 08:33:01 +0000172 return REAL(sigaction)(signum, act, oldact);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000173 }
Alexander Potapenko034bda52012-04-16 08:33:01 +0000174 return 0;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000175}
Evgeniy Stepanove1ba0002013-03-19 15:26:41 +0000176#elif SANITIZER_POSIX
Alexey Samsonov34a32022012-03-26 09:07:29 +0000177// We need to have defined REAL(sigaction) on posix systems.
178DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
179 struct sigaction *oldact);
180#endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000181
Alexey Samsonov08700282012-11-23 09:46:34 +0000182#if ASAN_INTERCEPT_SWAPCONTEXT
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000183static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
184 // Align to page size.
185 uptr PageSize = GetPageSizeCached();
186 uptr bottom = stack & ~(PageSize - 1);
187 ssize += stack - bottom;
188 ssize = RoundUpTo(ssize, PageSize);
189 static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb
Kostya Serebryany541cfb12013-01-18 11:30:36 +0000190 if (ssize && ssize <= kMaxSaneContextStackSize) {
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000191 PoisonShadow(bottom, ssize, 0);
192 }
193}
194
Alexey Samsonov08700282012-11-23 09:46:34 +0000195INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
196 struct ucontext_t *ucp) {
197 static bool reported_warning = false;
198 if (!reported_warning) {
199 Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
200 "functions and may produce false positives in some cases!\n");
201 reported_warning = true;
202 }
203 // Clear shadow memory for new context (it may share stack
204 // with current context).
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000205 uptr stack, ssize;
206 ReadContextStack(ucp, &stack, &ssize);
207 ClearShadowMemoryForContextStack(stack, ssize);
Alexey Samsonov08700282012-11-23 09:46:34 +0000208 int res = REAL(swapcontext)(oucp, ucp);
209 // swapcontext technically does not return, but program may swap context to
210 // "oucp" later, that would look as if swapcontext() returned 0.
211 // We need to clear shadow for ucp once again, as it may be in arbitrary
212 // state.
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000213 ClearShadowMemoryForContextStack(stack, ssize);
Alexey Samsonov08700282012-11-23 09:46:34 +0000214 return res;
215}
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000216#endif // ASAN_INTERCEPT_SWAPCONTEXT
Alexey Samsonov08700282012-11-23 09:46:34 +0000217
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000218INTERCEPTOR(void, longjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000219 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000220 REAL(longjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000221}
222
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000223#if ASAN_INTERCEPT__LONGJMP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000224INTERCEPTOR(void, _longjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000225 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000226 REAL(_longjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000227}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000228#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000229
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000230#if ASAN_INTERCEPT_SIGLONGJMP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000231INTERCEPTOR(void, siglongjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000232 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000233 REAL(siglongjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000234}
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000235#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000236
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000237#if ASAN_INTERCEPT___CXA_THROW
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000238INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000239 CHECK(REAL(__cxa_throw));
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000240 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000241 REAL(__cxa_throw)(a, b, c);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000242}
243#endif
244
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000245// intercept mlock and friends.
246// Since asan maps 16T of RAM, mlock is completely unfriendly to asan.
247// All functions return 0 (success).
248static void MlockIsUnsupported() {
Timur Iskhodzhanov8d2438a2013-05-29 17:26:25 +0000249 static bool printed = false;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000250 if (printed) return;
251 printed = true;
252 Printf("INFO: AddressSanitizer ignores mlock/mlockall/munlock/munlockall\n");
253}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000254
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000255extern "C" {
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000256INTERCEPTOR(int, mlock, const void *addr, uptr len) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000257 MlockIsUnsupported();
258 return 0;
259}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000260
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000261INTERCEPTOR(int, munlock, const void *addr, uptr len) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000262 MlockIsUnsupported();
263 return 0;
264}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000265
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000266INTERCEPTOR(int, mlockall, int flags) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000267 MlockIsUnsupported();
268 return 0;
269}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000270
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000271INTERCEPTOR(int, munlockall, void) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000272 MlockIsUnsupported();
273 return 0;
274}
275} // extern "C"
276
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000277static inline int CharCmp(unsigned char c1, unsigned char c2) {
278 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
279}
280
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000281INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000282 if (!asan_inited) return internal_memcmp(a1, a2, size);
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000283 ENSURE_ASAN_INITED();
Kostya Serebryany1b057b22013-02-26 07:25:18 +0000284 if (flags()->replace_intrin) {
Alexander Potapenko8bd5e742013-02-28 14:09:30 +0000285 if (flags()->strict_memcmp) {
286 // Check the entire regions even if the first bytes of the buffers are
287 // different.
288 ASAN_READ_RANGE(a1, size);
289 ASAN_READ_RANGE(a2, size);
290 // Fallthrough to REAL(memcmp) below.
291 } else {
292 unsigned char c1 = 0, c2 = 0;
293 const unsigned char *s1 = (const unsigned char*)a1;
294 const unsigned char *s2 = (const unsigned char*)a2;
295 uptr i;
296 for (i = 0; i < size; i++) {
297 c1 = s1[i];
298 c2 = s2[i];
299 if (c1 != c2) break;
300 }
301 ASAN_READ_RANGE(s1, Min(i + 1, size));
302 ASAN_READ_RANGE(s2, Min(i + 1, size));
303 return CharCmp(c1, c2);
304 }
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000305 }
Kostya Serebryany1b057b22013-02-26 07:25:18 +0000306 return REAL(memcmp(a1, a2, size));
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000307}
308
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000309INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000310 if (!asan_inited) return internal_memcpy(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000311 // memcpy is called during __asan_init() from the internals
312 // of printf(...).
313 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000314 return REAL(memcpy)(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000315 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000316 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000317 if (flags()->replace_intrin) {
Kostya Serebryanyc655cfa2012-01-17 18:43:52 +0000318 if (to != from) {
319 // We do not treat memcpy with to==from as a bug.
320 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
321 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
322 }
Alexander Potapenkoc75d8482012-12-10 16:02:13 +0000323 ASAN_READ_RANGE(from, size);
324 ASAN_WRITE_RANGE(to, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000325 }
Alexander Potapenkof1673e62012-10-15 15:34:41 +0000326 // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8.
327 // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
328 return internal_memcpy(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000329}
330
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000331INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
Alexander Potapenko3e8458a2012-10-24 09:19:16 +0000332 if (!asan_inited) return internal_memmove(to, from, size);
Alexey Samsonova1a25c12012-06-08 14:04:04 +0000333 if (asan_init_is_running) {
334 return REAL(memmove)(to, from, size);
335 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000336 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000337 if (flags()->replace_intrin) {
Alexander Potapenkoc75d8482012-12-10 16:02:13 +0000338 ASAN_READ_RANGE(from, size);
339 ASAN_WRITE_RANGE(to, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000340 }
Alexander Potapenkof1673e62012-10-15 15:34:41 +0000341 // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8.
342 // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
343 return internal_memmove(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000344}
345
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000346INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000347 if (!asan_inited) return internal_memset(block, c, size);
Alexander Potapenkoebb97022012-03-29 12:20:47 +0000348 // memset is called inside Printf.
Kostya Serebryanye1301912011-12-05 18:56:29 +0000349 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000350 return REAL(memset)(block, c, size);
Kostya Serebryanye1301912011-12-05 18:56:29 +0000351 }
352 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000353 if (flags()->replace_intrin) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000354 ASAN_WRITE_RANGE(block, size);
355 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000356 return REAL(memset)(block, c, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000357}
358
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000359INTERCEPTOR(char*, strchr, const char *str, int c) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000360 if (!asan_inited) return internal_strchr(str, c);
Alexander Potapenko8d6e3f72012-09-10 08:35:12 +0000361 // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is
362 // used.
363 if (asan_init_is_running) {
364 return REAL(strchr)(str, c);
365 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000366 ENSURE_ASAN_INITED();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000367 char *result = REAL(strchr)(str, c);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000368 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000369 uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000370 ASAN_READ_RANGE(str, bytes_read);
371 }
372 return result;
373}
374
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000375#if ASAN_INTERCEPT_INDEX
376# if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
Evgeniy Stepanov4b0c5f22012-02-13 12:12:32 +0000377INTERCEPTOR(char*, index, const char *string, int c)
Alexander Potapenko72bbfd42013-02-21 15:15:43 +0000378 ALIAS(WRAPPER_NAME(strchr));
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000379# else
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000380# if SANITIZER_MAC
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000381DECLARE_REAL(char*, index, const char *string, int c)
382OVERRIDE_FUNCTION(index, strchr);
383# else
384DEFINE_REAL(char*, index, const char *string, int c);
385# endif
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000386# endif
387#endif // ASAN_INTERCEPT_INDEX
Kostya Serebryanyaf0f01d2011-12-28 02:24:50 +0000388
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000389// For both strcat() and strncat() we need to check the validity of |to|
390// argument irrespective of the |from| length.
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000391INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000392 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000393 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000394 uptr from_length = REAL(strlen)(from);
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000395 ASAN_READ_RANGE(from, from_length + 1);
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000396 uptr to_length = REAL(strlen)(to);
397 ASAN_READ_RANGE(to, to_length);
398 ASAN_WRITE_RANGE(to + to_length, from_length + 1);
399 // If the copying actually happens, the |from| string should not overlap
400 // with the resulting string starting at |to|, which has a length of
401 // to_length + from_length + 1.
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000402 if (from_length > 0) {
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000403 CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
404 from, from_length + 1);
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000405 }
406 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000407 return REAL(strcat)(to, from); // NOLINT
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000408}
409
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000410INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
411 ENSURE_ASAN_INITED();
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000412 if (flags()->replace_str) {
Alexey Samsonovc9256972012-06-15 13:09:52 +0000413 uptr from_length = MaybeRealStrnlen(from, size);
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000414 uptr copy_length = Min(size, from_length + 1);
415 ASAN_READ_RANGE(from, copy_length);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000416 uptr to_length = REAL(strlen)(to);
417 ASAN_READ_RANGE(to, to_length);
418 ASAN_WRITE_RANGE(to + to_length, from_length + 1);
419 if (from_length > 0) {
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000420 CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
421 from, copy_length);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000422 }
423 }
424 return REAL(strncat)(to, from, size);
425}
426
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000427INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000428 if (!asan_inited) return internal_strcmp(s1, s2);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000429 if (asan_init_is_running) {
430 return REAL(strcmp)(s1, s2);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000431 }
Kostya Serebryany8648e772012-07-27 07:09:49 +0000432 ENSURE_ASAN_INITED();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000433 unsigned char c1, c2;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000434 uptr i;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000435 for (i = 0; ; i++) {
436 c1 = (unsigned char)s1[i];
437 c2 = (unsigned char)s2[i];
438 if (c1 != c2 || c1 == '\0') break;
439 }
440 ASAN_READ_RANGE(s1, i + 1);
441 ASAN_READ_RANGE(s2, i + 1);
442 return CharCmp(c1, c2);
443}
444
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000445INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000446#if SANITIZER_MAC
Alexander Potapenkobeda44f2012-08-17 10:08:51 +0000447 if (!asan_inited) return REAL(strcpy)(to, from); // NOLINT
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000448#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000449 // strcpy is called from malloc_default_purgeable_zone()
450 // in __asan::ReplaceSystemAlloc() on Mac.
451 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000452 return REAL(strcpy)(to, from); // NOLINT
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000453 }
Kostya Serebryanye1301912011-12-05 18:56:29 +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_size = REAL(strlen)(from) + 1;
Kostya Serebryanyc5e72a32011-12-28 19:24:31 +0000457 CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000458 ASAN_READ_RANGE(from, from_size);
459 ASAN_WRITE_RANGE(to, from_size);
460 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000461 return REAL(strcpy)(to, from); // NOLINT
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000462}
463
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000464#if ASAN_INTERCEPT_STRDUP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000465INTERCEPTOR(char*, strdup, const char *s) {
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000466#if SANITIZER_MAC
Alexander Potapenko71578fa2012-10-26 11:31:14 +0000467 // FIXME: because internal_strdup() uses InternalAlloc(), which currently
468 // just calls malloc() on Mac, we can't use internal_strdup() with the
469 // dynamic runtime. We can remove the call to REAL(strdup) once InternalAlloc
470 // starts using mmap() instead.
471 // See also http://code.google.com/p/address-sanitizer/issues/detail?id=123.
472 if (!asan_inited) return REAL(strdup)(s);
473#endif
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000474 if (!asan_inited) return internal_strdup(s);
Kostya Serebryanye1301912011-12-05 18:56:29 +0000475 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000476 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000477 uptr length = REAL(strlen)(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000478 ASAN_READ_RANGE(s, length + 1);
479 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000480 return REAL(strdup)(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000481}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000482#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000483
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000484INTERCEPTOR(uptr, strlen, const char *s) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000485 if (!asan_inited) return internal_strlen(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000486 // strlen is called from malloc_default_purgeable_zone()
487 // in __asan::ReplaceSystemAlloc() on Mac.
488 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000489 return REAL(strlen)(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000490 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000491 ENSURE_ASAN_INITED();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000492 uptr length = REAL(strlen)(s);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000493 if (flags()->replace_str) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000494 ASAN_READ_RANGE(s, length + 1);
495 }
496 return length;
497}
498
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000499INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000500 if (!asan_inited) return internal_strncmp(s1, s2, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000501 // strncmp is called from malloc_default_purgeable_zone()
502 // in __asan::ReplaceSystemAlloc() on Mac.
503 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000504 return REAL(strncmp)(s1, s2, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000505 }
Kostya Serebryany8648e772012-07-27 07:09:49 +0000506 ENSURE_ASAN_INITED();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000507 unsigned char c1 = 0, c2 = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000508 uptr i;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000509 for (i = 0; i < size; i++) {
510 c1 = (unsigned char)s1[i];
511 c2 = (unsigned char)s2[i];
512 if (c1 != c2 || c1 == '\0') break;
513 }
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000514 ASAN_READ_RANGE(s1, Min(i + 1, size));
515 ASAN_READ_RANGE(s2, Min(i + 1, size));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000516 return CharCmp(c1, c2);
517}
518
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000519INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
Kostya Serebryanye1301912011-12-05 18:56:29 +0000520 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000521 if (flags()->replace_str) {
Alexey Samsonovc9256972012-06-15 13:09:52 +0000522 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
Kostya Serebryanyc5e72a32011-12-28 19:24:31 +0000523 CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000524 ASAN_READ_RANGE(from, from_size);
525 ASAN_WRITE_RANGE(to, size);
526 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000527 return REAL(strncpy)(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000528}
529
Alexey Samsonov81a7a4a2012-03-24 09:10:50 +0000530#if ASAN_INTERCEPT_STRNLEN
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000531INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) {
Kostya Serebryanye1301912011-12-05 18:56:29 +0000532 ENSURE_ASAN_INITED();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000533 uptr length = REAL(strnlen)(s, maxlen);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000534 if (flags()->replace_str) {
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000535 ASAN_READ_RANGE(s, Min(length + 1, maxlen));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000536 }
537 return length;
538}
Alexey Samsonov81a7a4a2012-03-24 09:10:50 +0000539#endif // ASAN_INTERCEPT_STRNLEN
Kostya Serebryany547652c2012-01-09 19:35:11 +0000540
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000541static inline bool IsValidStrtolBase(int base) {
542 return (base == 0) || (2 <= base && base <= 36);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000543}
544
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000545static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000546 CHECK(endptr);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000547 if (nptr == *endptr) {
548 // No digits were found at strtol call, we need to find out the last
549 // symbol accessed by strtoll on our own.
550 // We get this symbol by skipping leading blanks and optional +/- sign.
551 while (IsSpace(*nptr)) nptr++;
552 if (*nptr == '+' || *nptr == '-') nptr++;
553 *endptr = (char*)nptr;
554 }
555 CHECK(*endptr >= nptr);
556}
557
Alexey Samsonov847f9322012-03-29 08:04:35 +0000558INTERCEPTOR(long, strtol, const char *nptr, // NOLINT
559 char **endptr, int base) {
560 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000561 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000562 return REAL(strtol)(nptr, endptr, base);
563 }
564 char *real_endptr;
565 long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000566 if (endptr != 0) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000567 *endptr = real_endptr;
568 }
569 if (IsValidStrtolBase(base)) {
570 FixRealStrtolEndptr(nptr, &real_endptr);
571 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
572 }
573 return result;
574}
575
576INTERCEPTOR(int, atoi, const char *nptr) {
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000577#if SANITIZER_MAC
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000578 if (!asan_inited) return REAL(atoi)(nptr);
579#endif
Alexey Samsonov847f9322012-03-29 08:04:35 +0000580 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000581 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000582 return REAL(atoi)(nptr);
583 }
584 char *real_endptr;
585 // "man atoi" tells that behavior of atoi(nptr) is the same as
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000586 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
Alexey Samsonov847f9322012-03-29 08:04:35 +0000587 // parsed integer can't be stored in *long* type (even if it's
588 // different from int). So, we just imitate this behavior.
589 int result = REAL(strtol)(nptr, &real_endptr, 10);
590 FixRealStrtolEndptr(nptr, &real_endptr);
591 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
592 return result;
593}
594
595INTERCEPTOR(long, atol, const char *nptr) { // NOLINT
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000596#if SANITIZER_MAC
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000597 if (!asan_inited) return REAL(atol)(nptr);
598#endif
Alexey Samsonov847f9322012-03-29 08:04:35 +0000599 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000600 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000601 return REAL(atol)(nptr);
602 }
603 char *real_endptr;
604 long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT
605 FixRealStrtolEndptr(nptr, &real_endptr);
606 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
607 return result;
608}
609
610#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000611INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT
612 char **endptr, int base) {
613 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000614 if (!flags()->replace_str) {
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000615 return REAL(strtoll)(nptr, endptr, base);
616 }
617 char *real_endptr;
618 long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000619 if (endptr != 0) {
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000620 *endptr = real_endptr;
621 }
622 // If base has unsupported value, strtoll can exit with EINVAL
623 // without reading any characters. So do additional checks only
624 // if base is valid.
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000625 if (IsValidStrtolBase(base)) {
626 FixRealStrtolEndptr(nptr, &real_endptr);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000627 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
628 }
629 return result;
630}
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000631
Alexey Samsonov847f9322012-03-29 08:04:35 +0000632INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000633 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000634 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000635 return REAL(atoll)(nptr);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000636 }
637 char *real_endptr;
Alexey Samsonov847f9322012-03-29 08:04:35 +0000638 long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT
639 FixRealStrtolEndptr(nptr, &real_endptr);
640 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000641 return result;
642}
Alexey Samsonov847f9322012-03-29 08:04:35 +0000643#endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000644
Alexey Samsonov46efcb02013-05-24 11:46:56 +0000645static void AtCxaAtexit(void *unused) {
646 (void)unused;
647 StopInitOrderChecking();
648}
649
650#if ASAN_INTERCEPT___CXA_ATEXIT
651INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
652 void *dso_handle) {
653 ENSURE_ASAN_INITED();
654 int res = REAL(__cxa_atexit)(func, arg, dso_handle);
655 REAL(__cxa_atexit)(AtCxaAtexit, 0, 0);
656 return res;
657}
658#endif // ASAN_INTERCEPT___CXA_ATEXIT
659
Alexander Potapenkofd552812013-06-07 15:10:02 +0000660#if !SANITIZER_MAC
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000661#define ASAN_INTERCEPT_FUNC(name) do { \
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000662 if (!INTERCEPT_FUNCTION(name) && flags()->verbosity > 0) \
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000663 Report("AddressSanitizer: failed to intercept '" #name "'\n"); \
664 } while (0)
Alexander Potapenkofd552812013-06-07 15:10:02 +0000665#else
666// OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
667#define ASAN_INTERCEPT_FUNC(name)
668#endif // SANITIZER_MAC
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000669
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000670#if SANITIZER_WINDOWS
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000671INTERCEPTOR_WINAPI(DWORD, CreateThread,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000672 void* security, uptr stack_size,
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000673 DWORD (__stdcall *start_routine)(void*), void* arg,
674 DWORD flags, void* tid) {
Alexey Samsonovbdd09662013-04-23 13:57:35 +0000675 // Strict init-order checking in thread-hostile.
676 DisableStrictInitOrderChecker();
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000677 GET_STACK_TRACE_THREAD;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000678 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonovdfe3f962013-03-22 07:48:23 +0000679 AsanThread *t = AsanThread::Create(start_routine, arg);
680 CreateThreadContextArgs args = { t, &stack };
Timur Iskhodzhanov8d2438a2013-05-29 17:26:25 +0000681 bool detached = false; // FIXME: how can we determine it on Windows?
Alexey Samsonovdfe3f962013-03-22 07:48:23 +0000682 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000683 return REAL(CreateThread)(security, stack_size,
684 asan_thread_start, t, flags, tid);
685}
686
687namespace __asan {
688void InitializeWindowsInterceptors() {
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000689 ASAN_INTERCEPT_FUNC(CreateThread);
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000690}
691
692} // namespace __asan
693#endif
694
Kostya Serebryany547652c2012-01-09 19:35:11 +0000695// ---------------------- InitializeAsanInterceptors ---------------- {{{1
696namespace __asan {
697void InitializeAsanInterceptors() {
Kostya Serebryanyfdbdab52012-03-16 21:02:13 +0000698 static bool was_called_once;
699 CHECK(was_called_once == false);
700 was_called_once = true;
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000701 SANITIZER_COMMON_INTERCEPTORS_INIT;
702
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000703 // Intercept mem* functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000704 ASAN_INTERCEPT_FUNC(memcmp);
705 ASAN_INTERCEPT_FUNC(memmove);
706 ASAN_INTERCEPT_FUNC(memset);
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000707 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000708 ASAN_INTERCEPT_FUNC(memcpy);
Alexander Potapenko573fb4b2012-02-01 10:07:52 +0000709 }
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000710
711 // Intercept str* functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000712 ASAN_INTERCEPT_FUNC(strcat); // NOLINT
713 ASAN_INTERCEPT_FUNC(strchr);
714 ASAN_INTERCEPT_FUNC(strcmp);
715 ASAN_INTERCEPT_FUNC(strcpy); // NOLINT
716 ASAN_INTERCEPT_FUNC(strlen);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000717 ASAN_INTERCEPT_FUNC(strncat);
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000718 ASAN_INTERCEPT_FUNC(strncmp);
719 ASAN_INTERCEPT_FUNC(strncpy);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000720#if ASAN_INTERCEPT_STRDUP
721 ASAN_INTERCEPT_FUNC(strdup);
722#endif
723#if ASAN_INTERCEPT_STRNLEN
724 ASAN_INTERCEPT_FUNC(strnlen);
725#endif
Alexander Potapenko69563982013-02-05 15:57:12 +0000726#if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000727 ASAN_INTERCEPT_FUNC(index);
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000728#endif
Kostya Serebryany547652c2012-01-09 19:35:11 +0000729
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000730 ASAN_INTERCEPT_FUNC(atoi);
731 ASAN_INTERCEPT_FUNC(atol);
732 ASAN_INTERCEPT_FUNC(strtol);
Alexey Samsonov847f9322012-03-29 08:04:35 +0000733#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000734 ASAN_INTERCEPT_FUNC(atoll);
735 ASAN_INTERCEPT_FUNC(strtoll);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000736#endif
737
Alexander Potapenkof0c6de32012-08-15 09:46:45 +0000738#if ASAN_INTERCEPT_MLOCKX
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000739 // Intercept mlock/munlock.
740 ASAN_INTERCEPT_FUNC(mlock);
741 ASAN_INTERCEPT_FUNC(munlock);
742 ASAN_INTERCEPT_FUNC(mlockall);
743 ASAN_INTERCEPT_FUNC(munlockall);
Alexander Potapenkof0c6de32012-08-15 09:46:45 +0000744#endif
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000745
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000746 // Intecept signal- and jump-related functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000747 ASAN_INTERCEPT_FUNC(longjmp);
Alexey Samsonov34a32022012-03-26 09:07:29 +0000748#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000749 ASAN_INTERCEPT_FUNC(sigaction);
750 ASAN_INTERCEPT_FUNC(signal);
Evgeniy Stepanov919c2472012-02-13 12:04:36 +0000751#endif
Alexey Samsonov08700282012-11-23 09:46:34 +0000752#if ASAN_INTERCEPT_SWAPCONTEXT
753 ASAN_INTERCEPT_FUNC(swapcontext);
754#endif
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000755#if ASAN_INTERCEPT__LONGJMP
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000756 ASAN_INTERCEPT_FUNC(_longjmp);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000757#endif
758#if ASAN_INTERCEPT_SIGLONGJMP
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000759 ASAN_INTERCEPT_FUNC(siglongjmp);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000760#endif
761
762 // Intercept exception handling functions.
763#if ASAN_INTERCEPT___CXA_THROW
764 INTERCEPT_FUNCTION(__cxa_throw);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000765#endif
766
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000767 // Intercept threading-related functions
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000768#if ASAN_INTERCEPT_PTHREAD_CREATE
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000769 ASAN_INTERCEPT_FUNC(pthread_create);
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000770#endif
771
Alexey Samsonov46efcb02013-05-24 11:46:56 +0000772 // Intercept atexit function.
773#if ASAN_INTERCEPT___CXA_ATEXIT
774 ASAN_INTERCEPT_FUNC(__cxa_atexit);
775#endif
776
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000777 // Some Windows-specific interceptors.
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000778#if SANITIZER_WINDOWS
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000779 InitializeWindowsInterceptors();
780#endif
781
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000782 if (flags()->verbosity > 0) {
Alexander Potapenko2962f262012-03-21 09:33:05 +0000783 Report("AddressSanitizer: libc interceptors initialized\n");
Kostya Serebryany547652c2012-01-09 19:35:11 +0000784 }
785}
786
787} // namespace __asan