blob: 0c467b36ca91c1cc654ae7466ac7d8b485bdeb7e [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 Stepanov897a4ae2013-04-09 14:34:59 +0000110#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
111 do { \
112 if (asan_init_is_running) \
113 return REAL(func)(__VA_ARGS__); \
114 ctx = 0; \
115 (void)ctx; \
116 ENSURE_ASAN_INITED(); \
Evgeniy Stepanov82a90802013-01-18 13:12:56 +0000117 } while (false)
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +0000118#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) do { } while (false)
119#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) do { } while (false)
120#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
Evgeniy Stepanov4f32c0b2013-01-18 13:01:18 +0000121#include "sanitizer_common/sanitizer_common_interceptors.inc"
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000122
Evgeniy Stepanov881b6772013-04-12 14:57:03 +0000123#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(p, s)
124#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(p, s)
125#define COMMON_SYSCALL_POST_READ_RANGE(p, s)
126#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s)
127#include "sanitizer_common/sanitizer_common_syscalls.inc"
128
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000129static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000130 AsanThread *t = (AsanThread*)arg;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000131 SetCurrentThread(t);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000132 return t->ThreadStart(GetTid());
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000133}
134
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000135#if ASAN_INTERCEPT_PTHREAD_CREATE
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000136extern "C" int pthread_attr_getdetachstate(void *attr, int *v);
137
Evgeniy Stepanovb8ef9252012-02-22 12:31:25 +0000138INTERCEPTOR(int, pthread_create, void *thread,
139 void *attr, void *(*start_routine)(void*), void *arg) {
Alexey Samsonovbdd09662013-04-23 13:57:35 +0000140 // Strict init-order checking in thread-hostile.
141 DisableStrictInitOrderChecker();
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000142 GET_STACK_TRACE_THREAD;
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000143 int detached = 0;
144 if (attr != 0)
145 pthread_attr_getdetachstate(attr, &detached);
146
Alexey Samsonov89c13842013-03-20 09:23:28 +0000147 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000148 AsanThread *t = AsanThread::Create(start_routine, arg);
149 CreateThreadContextArgs args = { t, &stack };
150 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000151 return REAL(pthread_create)(thread, attr, asan_thread_start, t);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000152}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000153#endif // ASAN_INTERCEPT_PTHREAD_CREATE
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000154
Alexey Samsonov34a32022012-03-26 09:07:29 +0000155#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000156INTERCEPTOR(void*, signal, int signum, void *handler) {
Alexey Samsonov332bf332013-04-25 10:52:15 +0000157 if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) {
Alexander Potapenko034bda52012-04-16 08:33:01 +0000158 return REAL(signal)(signum, handler);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000159 }
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000160 return 0;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000161}
162
Alexey Samsonovda13ba82012-02-16 17:00:45 +0000163INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
164 struct sigaction *oldact) {
Alexey Samsonov332bf332013-04-25 10:52:15 +0000165 if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) {
Alexander Potapenko034bda52012-04-16 08:33:01 +0000166 return REAL(sigaction)(signum, act, oldact);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000167 }
Alexander Potapenko034bda52012-04-16 08:33:01 +0000168 return 0;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000169}
Evgeniy Stepanove1ba0002013-03-19 15:26:41 +0000170#elif SANITIZER_POSIX
Alexey Samsonov34a32022012-03-26 09:07:29 +0000171// We need to have defined REAL(sigaction) on posix systems.
172DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
173 struct sigaction *oldact);
174#endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000175
Alexey Samsonov08700282012-11-23 09:46:34 +0000176#if ASAN_INTERCEPT_SWAPCONTEXT
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000177static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
178 // Align to page size.
179 uptr PageSize = GetPageSizeCached();
180 uptr bottom = stack & ~(PageSize - 1);
181 ssize += stack - bottom;
182 ssize = RoundUpTo(ssize, PageSize);
183 static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb
Kostya Serebryany541cfb12013-01-18 11:30:36 +0000184 if (ssize && ssize <= kMaxSaneContextStackSize) {
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000185 PoisonShadow(bottom, ssize, 0);
186 }
187}
188
Alexey Samsonov08700282012-11-23 09:46:34 +0000189INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
190 struct ucontext_t *ucp) {
191 static bool reported_warning = false;
192 if (!reported_warning) {
193 Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
194 "functions and may produce false positives in some cases!\n");
195 reported_warning = true;
196 }
197 // Clear shadow memory for new context (it may share stack
198 // with current context).
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000199 uptr stack, ssize;
200 ReadContextStack(ucp, &stack, &ssize);
201 ClearShadowMemoryForContextStack(stack, ssize);
Alexey Samsonov08700282012-11-23 09:46:34 +0000202 int res = REAL(swapcontext)(oucp, ucp);
203 // swapcontext technically does not return, but program may swap context to
204 // "oucp" later, that would look as if swapcontext() returned 0.
205 // We need to clear shadow for ucp once again, as it may be in arbitrary
206 // state.
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000207 ClearShadowMemoryForContextStack(stack, ssize);
Alexey Samsonov08700282012-11-23 09:46:34 +0000208 return res;
209}
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000210#endif // ASAN_INTERCEPT_SWAPCONTEXT
Alexey Samsonov08700282012-11-23 09:46:34 +0000211
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000212INTERCEPTOR(void, longjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000213 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000214 REAL(longjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000215}
216
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000217#if ASAN_INTERCEPT__LONGJMP
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}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000222#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000223
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000224#if ASAN_INTERCEPT_SIGLONGJMP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000225INTERCEPTOR(void, siglongjmp, void *env, int val) {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000226 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000227 REAL(siglongjmp)(env, val);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000228}
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000229#endif
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000230
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000231#if ASAN_INTERCEPT___CXA_THROW
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000232INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000233 CHECK(REAL(__cxa_throw));
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000234 __asan_handle_no_return();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000235 REAL(__cxa_throw)(a, b, c);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000236}
237#endif
238
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000239// intercept mlock and friends.
240// Since asan maps 16T of RAM, mlock is completely unfriendly to asan.
241// All functions return 0 (success).
242static void MlockIsUnsupported() {
243 static bool printed = 0;
244 if (printed) return;
245 printed = true;
246 Printf("INFO: AddressSanitizer ignores mlock/mlockall/munlock/munlockall\n");
247}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000248
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000249extern "C" {
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000250INTERCEPTOR(int, mlock, const void *addr, uptr len) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000251 MlockIsUnsupported();
252 return 0;
253}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000254
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000255INTERCEPTOR(int, munlock, const void *addr, uptr len) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000256 MlockIsUnsupported();
257 return 0;
258}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000259
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000260INTERCEPTOR(int, mlockall, int flags) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000261 MlockIsUnsupported();
262 return 0;
263}
Alexey Samsonov3389b8e2012-01-30 13:42:44 +0000264
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000265INTERCEPTOR(int, munlockall, void) {
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000266 MlockIsUnsupported();
267 return 0;
268}
269} // extern "C"
270
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000271static inline int CharCmp(unsigned char c1, unsigned char c2) {
272 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
273}
274
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000275INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000276 if (!asan_inited) return internal_memcmp(a1, a2, size);
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000277 ENSURE_ASAN_INITED();
Kostya Serebryany1b057b22013-02-26 07:25:18 +0000278 if (flags()->replace_intrin) {
Alexander Potapenko8bd5e742013-02-28 14:09:30 +0000279 if (flags()->strict_memcmp) {
280 // Check the entire regions even if the first bytes of the buffers are
281 // different.
282 ASAN_READ_RANGE(a1, size);
283 ASAN_READ_RANGE(a2, size);
284 // Fallthrough to REAL(memcmp) below.
285 } else {
286 unsigned char c1 = 0, c2 = 0;
287 const unsigned char *s1 = (const unsigned char*)a1;
288 const unsigned char *s2 = (const unsigned char*)a2;
289 uptr i;
290 for (i = 0; i < size; i++) {
291 c1 = s1[i];
292 c2 = s2[i];
293 if (c1 != c2) break;
294 }
295 ASAN_READ_RANGE(s1, Min(i + 1, size));
296 ASAN_READ_RANGE(s2, Min(i + 1, size));
297 return CharCmp(c1, c2);
298 }
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000299 }
Kostya Serebryany1b057b22013-02-26 07:25:18 +0000300 return REAL(memcmp(a1, a2, size));
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000301}
302
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000303INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000304 if (!asan_inited) return internal_memcpy(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000305 // memcpy is called during __asan_init() from the internals
306 // of printf(...).
307 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000308 return REAL(memcpy)(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000309 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000310 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000311 if (flags()->replace_intrin) {
Kostya Serebryanyc655cfa2012-01-17 18:43:52 +0000312 if (to != from) {
313 // We do not treat memcpy with to==from as a bug.
314 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
315 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
316 }
Alexander Potapenkoc75d8482012-12-10 16:02:13 +0000317 ASAN_READ_RANGE(from, size);
318 ASAN_WRITE_RANGE(to, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000319 }
Alexander Potapenkof1673e62012-10-15 15:34:41 +0000320 // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8.
321 // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
322 return internal_memcpy(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000323}
324
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000325INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
Alexander Potapenko3e8458a2012-10-24 09:19:16 +0000326 if (!asan_inited) return internal_memmove(to, from, size);
Alexey Samsonova1a25c12012-06-08 14:04:04 +0000327 if (asan_init_is_running) {
328 return REAL(memmove)(to, from, size);
329 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000330 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000331 if (flags()->replace_intrin) {
Alexander Potapenkoc75d8482012-12-10 16:02:13 +0000332 ASAN_READ_RANGE(from, size);
333 ASAN_WRITE_RANGE(to, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000334 }
Alexander Potapenkof1673e62012-10-15 15:34:41 +0000335 // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8.
336 // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
337 return internal_memmove(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000338}
339
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000340INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000341 if (!asan_inited) return internal_memset(block, c, size);
Alexander Potapenkoebb97022012-03-29 12:20:47 +0000342 // memset is called inside Printf.
Kostya Serebryanye1301912011-12-05 18:56:29 +0000343 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000344 return REAL(memset)(block, c, size);
Kostya Serebryanye1301912011-12-05 18:56:29 +0000345 }
346 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000347 if (flags()->replace_intrin) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000348 ASAN_WRITE_RANGE(block, size);
349 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000350 return REAL(memset)(block, c, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000351}
352
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000353INTERCEPTOR(char*, strchr, const char *str, int c) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000354 if (!asan_inited) return internal_strchr(str, c);
Alexander Potapenko8d6e3f72012-09-10 08:35:12 +0000355 // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is
356 // used.
357 if (asan_init_is_running) {
358 return REAL(strchr)(str, c);
359 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000360 ENSURE_ASAN_INITED();
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000361 char *result = REAL(strchr)(str, c);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000362 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000363 uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000364 ASAN_READ_RANGE(str, bytes_read);
365 }
366 return result;
367}
368
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000369#if ASAN_INTERCEPT_INDEX
370# if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
Evgeniy Stepanov4b0c5f22012-02-13 12:12:32 +0000371INTERCEPTOR(char*, index, const char *string, int c)
Alexander Potapenko72bbfd42013-02-21 15:15:43 +0000372 ALIAS(WRAPPER_NAME(strchr));
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000373# else
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000374# if SANITIZER_MAC
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000375DECLARE_REAL(char*, index, const char *string, int c)
376OVERRIDE_FUNCTION(index, strchr);
377# else
378DEFINE_REAL(char*, index, const char *string, int c);
379# endif
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000380# endif
381#endif // ASAN_INTERCEPT_INDEX
Kostya Serebryanyaf0f01d2011-12-28 02:24:50 +0000382
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000383// For both strcat() and strncat() we need to check the validity of |to|
384// argument irrespective of the |from| length.
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000385INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000386 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000387 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000388 uptr from_length = REAL(strlen)(from);
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000389 ASAN_READ_RANGE(from, from_length + 1);
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000390 uptr to_length = REAL(strlen)(to);
391 ASAN_READ_RANGE(to, to_length);
392 ASAN_WRITE_RANGE(to + to_length, from_length + 1);
393 // If the copying actually happens, the |from| string should not overlap
394 // with the resulting string starting at |to|, which has a length of
395 // to_length + from_length + 1.
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000396 if (from_length > 0) {
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000397 CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
398 from, from_length + 1);
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000399 }
400 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000401 return REAL(strcat)(to, from); // NOLINT
Kostya Serebryany0985ca22011-12-28 19:08:49 +0000402}
403
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000404INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
405 ENSURE_ASAN_INITED();
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000406 if (flags()->replace_str) {
Alexey Samsonovc9256972012-06-15 13:09:52 +0000407 uptr from_length = MaybeRealStrnlen(from, size);
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000408 uptr copy_length = Min(size, from_length + 1);
409 ASAN_READ_RANGE(from, copy_length);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000410 uptr to_length = REAL(strlen)(to);
411 ASAN_READ_RANGE(to, to_length);
412 ASAN_WRITE_RANGE(to + to_length, from_length + 1);
413 if (from_length > 0) {
Alexander Potapenko37b3fcd2012-08-02 10:25:46 +0000414 CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
415 from, copy_length);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000416 }
417 }
418 return REAL(strncat)(to, from, size);
419}
420
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000421INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000422 if (!asan_inited) return internal_strcmp(s1, s2);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000423 if (asan_init_is_running) {
424 return REAL(strcmp)(s1, s2);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000425 }
Kostya Serebryany8648e772012-07-27 07:09:49 +0000426 ENSURE_ASAN_INITED();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000427 unsigned char c1, c2;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000428 uptr i;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000429 for (i = 0; ; i++) {
430 c1 = (unsigned char)s1[i];
431 c2 = (unsigned char)s2[i];
432 if (c1 != c2 || c1 == '\0') break;
433 }
434 ASAN_READ_RANGE(s1, i + 1);
435 ASAN_READ_RANGE(s2, i + 1);
436 return CharCmp(c1, c2);
437}
438
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000439INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000440#if SANITIZER_MAC
Alexander Potapenkobeda44f2012-08-17 10:08:51 +0000441 if (!asan_inited) return REAL(strcpy)(to, from); // NOLINT
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000442#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000443 // strcpy is called from malloc_default_purgeable_zone()
444 // in __asan::ReplaceSystemAlloc() on Mac.
445 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000446 return REAL(strcpy)(to, from); // NOLINT
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000447 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000448 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000449 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000450 uptr from_size = REAL(strlen)(from) + 1;
Kostya Serebryanyc5e72a32011-12-28 19:24:31 +0000451 CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000452 ASAN_READ_RANGE(from, from_size);
453 ASAN_WRITE_RANGE(to, from_size);
454 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000455 return REAL(strcpy)(to, from); // NOLINT
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000456}
457
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000458#if ASAN_INTERCEPT_STRDUP
Alexey Samsonovf2598fc2012-02-02 10:39:40 +0000459INTERCEPTOR(char*, strdup, const char *s) {
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000460#if SANITIZER_MAC
Alexander Potapenko71578fa2012-10-26 11:31:14 +0000461 // FIXME: because internal_strdup() uses InternalAlloc(), which currently
462 // just calls malloc() on Mac, we can't use internal_strdup() with the
463 // dynamic runtime. We can remove the call to REAL(strdup) once InternalAlloc
464 // starts using mmap() instead.
465 // See also http://code.google.com/p/address-sanitizer/issues/detail?id=123.
466 if (!asan_inited) return REAL(strdup)(s);
467#endif
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000468 if (!asan_inited) return internal_strdup(s);
Kostya Serebryanye1301912011-12-05 18:56:29 +0000469 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000470 if (flags()->replace_str) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000471 uptr length = REAL(strlen)(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000472 ASAN_READ_RANGE(s, length + 1);
473 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000474 return REAL(strdup)(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000475}
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000476#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000477
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000478INTERCEPTOR(uptr, strlen, const char *s) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000479 if (!asan_inited) return internal_strlen(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000480 // strlen is called from malloc_default_purgeable_zone()
481 // in __asan::ReplaceSystemAlloc() on Mac.
482 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000483 return REAL(strlen)(s);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000484 }
Kostya Serebryanye1301912011-12-05 18:56:29 +0000485 ENSURE_ASAN_INITED();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000486 uptr length = REAL(strlen)(s);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000487 if (flags()->replace_str) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000488 ASAN_READ_RANGE(s, length + 1);
489 }
490 return length;
491}
492
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000493INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
Alexander Potapenko50f2e302012-10-05 12:11:06 +0000494 if (!asan_inited) return internal_strncmp(s1, s2, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000495 // strncmp is called from malloc_default_purgeable_zone()
496 // in __asan::ReplaceSystemAlloc() on Mac.
497 if (asan_init_is_running) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000498 return REAL(strncmp)(s1, s2, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000499 }
Kostya Serebryany8648e772012-07-27 07:09:49 +0000500 ENSURE_ASAN_INITED();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000501 unsigned char c1 = 0, c2 = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000502 uptr i;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000503 for (i = 0; i < size; i++) {
504 c1 = (unsigned char)s1[i];
505 c2 = (unsigned char)s2[i];
506 if (c1 != c2 || c1 == '\0') break;
507 }
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000508 ASAN_READ_RANGE(s1, Min(i + 1, size));
509 ASAN_READ_RANGE(s2, Min(i + 1, size));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000510 return CharCmp(c1, c2);
511}
512
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000513INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
Kostya Serebryanye1301912011-12-05 18:56:29 +0000514 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000515 if (flags()->replace_str) {
Alexey Samsonovc9256972012-06-15 13:09:52 +0000516 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
Kostya Serebryanyc5e72a32011-12-28 19:24:31 +0000517 CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000518 ASAN_READ_RANGE(from, from_size);
519 ASAN_WRITE_RANGE(to, size);
520 }
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000521 return REAL(strncpy)(to, from, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000522}
523
Alexey Samsonov81a7a4a2012-03-24 09:10:50 +0000524#if ASAN_INTERCEPT_STRNLEN
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000525INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) {
Kostya Serebryanye1301912011-12-05 18:56:29 +0000526 ENSURE_ASAN_INITED();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000527 uptr length = REAL(strnlen)(s, maxlen);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000528 if (flags()->replace_str) {
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000529 ASAN_READ_RANGE(s, Min(length + 1, maxlen));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000530 }
531 return length;
532}
Alexey Samsonov81a7a4a2012-03-24 09:10:50 +0000533#endif // ASAN_INTERCEPT_STRNLEN
Kostya Serebryany547652c2012-01-09 19:35:11 +0000534
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000535static inline bool IsValidStrtolBase(int base) {
536 return (base == 0) || (2 <= base && base <= 36);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000537}
538
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000539static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000540 CHECK(endptr);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000541 if (nptr == *endptr) {
542 // No digits were found at strtol call, we need to find out the last
543 // symbol accessed by strtoll on our own.
544 // We get this symbol by skipping leading blanks and optional +/- sign.
545 while (IsSpace(*nptr)) nptr++;
546 if (*nptr == '+' || *nptr == '-') nptr++;
547 *endptr = (char*)nptr;
548 }
549 CHECK(*endptr >= nptr);
550}
551
Alexey Samsonov847f9322012-03-29 08:04:35 +0000552INTERCEPTOR(long, strtol, const char *nptr, // NOLINT
553 char **endptr, int base) {
554 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000555 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000556 return REAL(strtol)(nptr, endptr, base);
557 }
558 char *real_endptr;
559 long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000560 if (endptr != 0) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000561 *endptr = real_endptr;
562 }
563 if (IsValidStrtolBase(base)) {
564 FixRealStrtolEndptr(nptr, &real_endptr);
565 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
566 }
567 return result;
568}
569
570INTERCEPTOR(int, atoi, const char *nptr) {
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000571#if SANITIZER_MAC
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000572 if (!asan_inited) return REAL(atoi)(nptr);
573#endif
Alexey Samsonov847f9322012-03-29 08:04:35 +0000574 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000575 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000576 return REAL(atoi)(nptr);
577 }
578 char *real_endptr;
579 // "man atoi" tells that behavior of atoi(nptr) is the same as
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000580 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
Alexey Samsonov847f9322012-03-29 08:04:35 +0000581 // parsed integer can't be stored in *long* type (even if it's
582 // different from int). So, we just imitate this behavior.
583 int result = REAL(strtol)(nptr, &real_endptr, 10);
584 FixRealStrtolEndptr(nptr, &real_endptr);
585 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
586 return result;
587}
588
589INTERCEPTOR(long, atol, const char *nptr) { // NOLINT
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000590#if SANITIZER_MAC
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000591 if (!asan_inited) return REAL(atol)(nptr);
592#endif
Alexey Samsonov847f9322012-03-29 08:04:35 +0000593 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000594 if (!flags()->replace_str) {
Alexey Samsonov847f9322012-03-29 08:04:35 +0000595 return REAL(atol)(nptr);
596 }
597 char *real_endptr;
598 long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT
599 FixRealStrtolEndptr(nptr, &real_endptr);
600 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
601 return result;
602}
603
604#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000605INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT
606 char **endptr, int base) {
607 ENSURE_ASAN_INITED();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000608 if (!flags()->replace_str) {
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000609 return REAL(strtoll)(nptr, endptr, base);
610 }
611 char *real_endptr;
612 long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000613 if (endptr != 0) {
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000614 *endptr = real_endptr;
615 }
616 // If base has unsupported value, strtoll can exit with EINVAL
617 // without reading any characters. So do additional checks only
618 // if base is valid.
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000619 if (IsValidStrtolBase(base)) {
620 FixRealStrtolEndptr(nptr, &real_endptr);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000621 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
622 }
623 return result;
624}
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000625
Alexey Samsonov847f9322012-03-29 08:04:35 +0000626INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +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(atoll)(nptr);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000630 }
631 char *real_endptr;
Alexey Samsonov847f9322012-03-29 08:04:35 +0000632 long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT
633 FixRealStrtolEndptr(nptr, &real_endptr);
634 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000635 return result;
636}
Alexey Samsonov847f9322012-03-29 08:04:35 +0000637#endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Alexey Samsonov8f6a77f2012-03-26 16:42:22 +0000638
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000639#define ASAN_INTERCEPT_FUNC(name) do { \
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000640 if (!INTERCEPT_FUNCTION(name) && flags()->verbosity > 0) \
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000641 Report("AddressSanitizer: failed to intercept '" #name "'\n"); \
642 } while (0)
643
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000644#if SANITIZER_WINDOWS
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000645INTERCEPTOR_WINAPI(DWORD, CreateThread,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000646 void* security, uptr stack_size,
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000647 DWORD (__stdcall *start_routine)(void*), void* arg,
648 DWORD flags, void* tid) {
Alexey Samsonovbdd09662013-04-23 13:57:35 +0000649 // Strict init-order checking in thread-hostile.
650 DisableStrictInitOrderChecker();
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000651 GET_STACK_TRACE_THREAD;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000652 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonovdfe3f962013-03-22 07:48:23 +0000653 AsanThread *t = AsanThread::Create(start_routine, arg);
654 CreateThreadContextArgs args = { t, &stack };
655 int detached = 0; // FIXME: how can we determine it on Windows?
656 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000657 return REAL(CreateThread)(security, stack_size,
658 asan_thread_start, t, flags, tid);
659}
660
661namespace __asan {
662void InitializeWindowsInterceptors() {
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000663 ASAN_INTERCEPT_FUNC(CreateThread);
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000664}
665
666} // namespace __asan
667#endif
668
Kostya Serebryany547652c2012-01-09 19:35:11 +0000669// ---------------------- InitializeAsanInterceptors ---------------- {{{1
670namespace __asan {
671void InitializeAsanInterceptors() {
Kostya Serebryanyfdbdab52012-03-16 21:02:13 +0000672 static bool was_called_once;
673 CHECK(was_called_once == false);
674 was_called_once = true;
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000675#if SANITIZER_MAC
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000676 return;
Alexander Potapenko69563982013-02-05 15:57:12 +0000677#else
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000678 SANITIZER_COMMON_INTERCEPTORS_INIT;
679
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000680 // Intercept mem* functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000681 ASAN_INTERCEPT_FUNC(memcmp);
682 ASAN_INTERCEPT_FUNC(memmove);
683 ASAN_INTERCEPT_FUNC(memset);
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000684 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000685 ASAN_INTERCEPT_FUNC(memcpy);
Alexander Potapenko573fb4b2012-02-01 10:07:52 +0000686 }
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000687
688 // Intercept str* functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000689 ASAN_INTERCEPT_FUNC(strcat); // NOLINT
690 ASAN_INTERCEPT_FUNC(strchr);
691 ASAN_INTERCEPT_FUNC(strcmp);
692 ASAN_INTERCEPT_FUNC(strcpy); // NOLINT
693 ASAN_INTERCEPT_FUNC(strlen);
Alexey Samsonovc1bdd5a2012-06-08 13:27:46 +0000694 ASAN_INTERCEPT_FUNC(strncat);
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000695 ASAN_INTERCEPT_FUNC(strncmp);
696 ASAN_INTERCEPT_FUNC(strncpy);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000697#if ASAN_INTERCEPT_STRDUP
698 ASAN_INTERCEPT_FUNC(strdup);
699#endif
700#if ASAN_INTERCEPT_STRNLEN
701 ASAN_INTERCEPT_FUNC(strnlen);
702#endif
Alexander Potapenko69563982013-02-05 15:57:12 +0000703#if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000704 ASAN_INTERCEPT_FUNC(index);
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000705#endif
Kostya Serebryany547652c2012-01-09 19:35:11 +0000706
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000707 ASAN_INTERCEPT_FUNC(atoi);
708 ASAN_INTERCEPT_FUNC(atol);
709 ASAN_INTERCEPT_FUNC(strtol);
Alexey Samsonov847f9322012-03-29 08:04:35 +0000710#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000711 ASAN_INTERCEPT_FUNC(atoll);
712 ASAN_INTERCEPT_FUNC(strtoll);
Alexey Samsonov84ba3242012-03-24 08:39:14 +0000713#endif
714
Alexander Potapenkof0c6de32012-08-15 09:46:45 +0000715#if ASAN_INTERCEPT_MLOCKX
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000716 // Intercept mlock/munlock.
717 ASAN_INTERCEPT_FUNC(mlock);
718 ASAN_INTERCEPT_FUNC(munlock);
719 ASAN_INTERCEPT_FUNC(mlockall);
720 ASAN_INTERCEPT_FUNC(munlockall);
Alexander Potapenkof0c6de32012-08-15 09:46:45 +0000721#endif
Alexander Potapenko00f1c092012-08-15 09:22:57 +0000722
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000723 // Intecept signal- and jump-related functions.
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000724 ASAN_INTERCEPT_FUNC(longjmp);
Alexey Samsonov34a32022012-03-26 09:07:29 +0000725#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000726 ASAN_INTERCEPT_FUNC(sigaction);
727 ASAN_INTERCEPT_FUNC(signal);
Evgeniy Stepanov919c2472012-02-13 12:04:36 +0000728#endif
Alexey Samsonov08700282012-11-23 09:46:34 +0000729#if ASAN_INTERCEPT_SWAPCONTEXT
730 ASAN_INTERCEPT_FUNC(swapcontext);
731#endif
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000732#if ASAN_INTERCEPT__LONGJMP
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000733 ASAN_INTERCEPT_FUNC(_longjmp);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000734#endif
735#if ASAN_INTERCEPT_SIGLONGJMP
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000736 ASAN_INTERCEPT_FUNC(siglongjmp);
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000737#endif
738
739 // Intercept exception handling functions.
740#if ASAN_INTERCEPT___CXA_THROW
741 INTERCEPT_FUNCTION(__cxa_throw);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000742#endif
743
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000744 // Intercept threading-related functions
Alexey Samsonovfd2ae4f2012-08-01 11:17:00 +0000745#if ASAN_INTERCEPT_PTHREAD_CREATE
Dmitry Vyukov580469d2012-05-24 13:54:31 +0000746 ASAN_INTERCEPT_FUNC(pthread_create);
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000747#endif
748
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000749 // Some Windows-specific interceptors.
Evgeniy Stepanov24e13722013-03-19 14:33:38 +0000750#if SANITIZER_WINDOWS
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000751 InitializeWindowsInterceptors();
752#endif
753
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000754 if (flags()->verbosity > 0) {
Alexander Potapenko2962f262012-03-21 09:33:05 +0000755 Report("AddressSanitizer: libc interceptors initialized\n");
Kostya Serebryany547652c2012-01-09 19:35:11 +0000756 }
Alexey Samsonov649a2702013-04-03 07:29:53 +0000757#endif // SANITIZER_MAC
Kostya Serebryany547652c2012-01-09 19:35:11 +0000758}
759
760} // namespace __asan