[CMake] Respect CMAKE_CXX_FLAGS in custom clang_compile commands
[blocksruntime.git] / lib / asan / asan_interceptors.cc
blob41d6e588a6168f0e72c6c3cd4f0982e11cb52d4c
1 //===-- asan_interceptors.cc ----------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // Intercept various libc functions.
13 //===----------------------------------------------------------------------===//
14 #include "asan_interceptors.h"
16 #include "asan_allocator.h"
17 #include "asan_internal.h"
18 #include "asan_mapping.h"
19 #include "asan_poisoning.h"
20 #include "asan_report.h"
21 #include "asan_stack.h"
22 #include "asan_stats.h"
23 #include "sanitizer_common/sanitizer_libc.h"
25 namespace __asan {
27 // Return true if we can quickly decide that the region is unpoisoned.
28 static 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;
37 // 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.
41 // We check all shadow bytes.
42 #define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \
43 uptr __offset = (uptr)(offset); \
44 uptr __size = (uptr)(size); \
45 uptr __bad = 0; \
46 if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \
47 (__bad = __asan_region_is_poisoned(__offset, __size))) { \
48 GET_CURRENT_PC_BP_SP; \
49 __asan_report_error(pc, bp, sp, __bad, isWrite, __size); \
50 } \
51 } while (0)
53 #define ASAN_READ_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, false)
54 #define ASAN_WRITE_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, true)
56 // Behavior of functions like "memcpy" or "strcpy" is undefined
57 // if memory intervals overlap. We report error in this case.
58 // Macro is used to avoid creation of new frames.
59 static inline bool RangesOverlap(const char *offset1, uptr length1,
60 const char *offset2, uptr length2) {
61 return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
63 #define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
64 const char *offset1 = (const char*)_offset1; \
65 const char *offset2 = (const char*)_offset2; \
66 if (RangesOverlap(offset1, length1, offset2, length2)) { \
67 GET_STACK_TRACE_FATAL_HERE; \
68 ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
69 offset2, length2, &stack); \
70 } \
71 } while (0)
73 static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
74 #if ASAN_INTERCEPT_STRNLEN
75 if (REAL(strnlen) != 0) {
76 return REAL(strnlen)(s, maxlen);
78 #endif
79 return internal_strnlen(s, maxlen);
82 void SetThreadName(const char *name) {
83 AsanThread *t = GetCurrentThread();
84 if (t)
85 asanThreadRegistry().SetThreadName(t->tid(), name);
88 int OnExit() {
89 // FIXME: ask frontend whether we need to return failure.
90 return 0;
93 } // namespace __asan
95 // ---------------------- Wrappers ---------------- {{{1
96 using namespace __asan; // NOLINT
98 DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr)
99 DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
101 #if !SANITIZER_MAC
102 #define ASAN_INTERCEPT_FUNC(name) \
103 do { \
104 if ((!INTERCEPT_FUNCTION(name) || !REAL(name))) \
105 VReport(1, "AddressSanitizer: failed to intercept '" #name "'\n"); \
106 } while (0)
107 #else
108 // OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
109 #define ASAN_INTERCEPT_FUNC(name)
110 #endif // SANITIZER_MAC
112 #define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name)
113 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
114 ASAN_WRITE_RANGE(ptr, size)
115 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) ASAN_READ_RANGE(ptr, size)
116 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
117 do { \
118 if (asan_init_is_running) return REAL(func)(__VA_ARGS__); \
119 ctx = 0; \
120 (void) ctx; \
121 if (SANITIZER_MAC && !asan_inited) return REAL(func)(__VA_ARGS__); \
122 ENSURE_ASAN_INITED(); \
123 } while (false)
124 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
125 do { \
126 } while (false)
127 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
128 do { \
129 } while (false)
130 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
131 do { \
132 } while (false)
133 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
134 // Should be asanThreadRegistry().SetThreadNameByUserId(thread, name)
135 // But asan does not remember UserId's for threads (pthread_t);
136 // and remembers all ever existed threads, so the linear search by UserId
137 // can be slow.
138 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
139 do { \
140 } while (false)
141 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
142 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
143 #include "sanitizer_common/sanitizer_common_interceptors.inc"
145 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(p, s)
146 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(p, s)
147 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
148 do { \
149 (void)(p); \
150 (void)(s); \
151 } while (false)
152 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
153 do { \
154 (void)(p); \
155 (void)(s); \
156 } while (false)
157 #include "sanitizer_common/sanitizer_common_syscalls.inc"
159 static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
160 AsanThread *t = (AsanThread*)arg;
161 SetCurrentThread(t);
162 return t->ThreadStart(GetTid());
165 #if ASAN_INTERCEPT_PTHREAD_CREATE
166 INTERCEPTOR(int, pthread_create, void *thread,
167 void *attr, void *(*start_routine)(void*), void *arg) {
168 EnsureMainThreadIDIsCorrect();
169 // Strict init-order checking in thread-hostile.
170 if (flags()->strict_init_order)
171 StopInitOrderChecking();
172 GET_STACK_TRACE_THREAD;
173 int detached = 0;
174 if (attr != 0)
175 REAL(pthread_attr_getdetachstate)(attr, &detached);
177 u32 current_tid = GetCurrentTidOrInvalid();
178 AsanThread *t = AsanThread::Create(start_routine, arg);
179 CreateThreadContextArgs args = { t, &stack };
180 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
181 return REAL(pthread_create)(thread, attr, asan_thread_start, t);
183 #endif // ASAN_INTERCEPT_PTHREAD_CREATE
185 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
187 #if SANITIZER_ANDROID
188 INTERCEPTOR(void*, bsd_signal, int signum, void *handler) {
189 if (!AsanInterceptsSignal(signum) ||
190 common_flags()->allow_user_segv_handler) {
191 return REAL(bsd_signal)(signum, handler);
193 return 0;
195 #else
196 INTERCEPTOR(void*, signal, int signum, void *handler) {
197 if (!AsanInterceptsSignal(signum) ||
198 common_flags()->allow_user_segv_handler) {
199 return REAL(signal)(signum, handler);
201 return 0;
203 #endif
205 INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
206 struct sigaction *oldact) {
207 if (!AsanInterceptsSignal(signum) ||
208 common_flags()->allow_user_segv_handler) {
209 return REAL(sigaction)(signum, act, oldact);
211 return 0;
214 namespace __sanitizer {
215 int real_sigaction(int signum, const void *act, void *oldact) {
216 return REAL(sigaction)(signum,
217 (struct sigaction *)act, (struct sigaction *)oldact);
219 } // namespace __sanitizer
221 #elif SANITIZER_POSIX
222 // We need to have defined REAL(sigaction) on posix systems.
223 DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
224 struct sigaction *oldact)
225 #endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
227 #if ASAN_INTERCEPT_SWAPCONTEXT
228 static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
229 // Align to page size.
230 uptr PageSize = GetPageSizeCached();
231 uptr bottom = stack & ~(PageSize - 1);
232 ssize += stack - bottom;
233 ssize = RoundUpTo(ssize, PageSize);
234 static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb
235 if (ssize && ssize <= kMaxSaneContextStackSize) {
236 PoisonShadow(bottom, ssize, 0);
240 INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
241 struct ucontext_t *ucp) {
242 static bool reported_warning = false;
243 if (!reported_warning) {
244 Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
245 "functions and may produce false positives in some cases!\n");
246 reported_warning = true;
248 // Clear shadow memory for new context (it may share stack
249 // with current context).
250 uptr stack, ssize;
251 ReadContextStack(ucp, &stack, &ssize);
252 ClearShadowMemoryForContextStack(stack, ssize);
253 int res = REAL(swapcontext)(oucp, ucp);
254 // swapcontext technically does not return, but program may swap context to
255 // "oucp" later, that would look as if swapcontext() returned 0.
256 // We need to clear shadow for ucp once again, as it may be in arbitrary
257 // state.
258 ClearShadowMemoryForContextStack(stack, ssize);
259 return res;
261 #endif // ASAN_INTERCEPT_SWAPCONTEXT
263 INTERCEPTOR(void, longjmp, void *env, int val) {
264 __asan_handle_no_return();
265 REAL(longjmp)(env, val);
268 #if ASAN_INTERCEPT__LONGJMP
269 INTERCEPTOR(void, _longjmp, void *env, int val) {
270 __asan_handle_no_return();
271 REAL(_longjmp)(env, val);
273 #endif
275 #if ASAN_INTERCEPT_SIGLONGJMP
276 INTERCEPTOR(void, siglongjmp, void *env, int val) {
277 __asan_handle_no_return();
278 REAL(siglongjmp)(env, val);
280 #endif
282 #if ASAN_INTERCEPT___CXA_THROW
283 INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
284 CHECK(REAL(__cxa_throw));
285 __asan_handle_no_return();
286 REAL(__cxa_throw)(a, b, c);
288 #endif
290 // intercept mlock and friends.
291 // Since asan maps 16T of RAM, mlock is completely unfriendly to asan.
292 // All functions return 0 (success).
293 static void MlockIsUnsupported() {
294 static bool printed = false;
295 if (printed) return;
296 printed = true;
297 VPrintf(1,
298 "INFO: AddressSanitizer ignores "
299 "mlock/mlockall/munlock/munlockall\n");
302 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
303 MlockIsUnsupported();
304 return 0;
307 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
308 MlockIsUnsupported();
309 return 0;
312 INTERCEPTOR(int, mlockall, int flags) {
313 MlockIsUnsupported();
314 return 0;
317 INTERCEPTOR(int, munlockall, void) {
318 MlockIsUnsupported();
319 return 0;
322 static inline int CharCmp(unsigned char c1, unsigned char c2) {
323 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
326 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
327 if (!asan_inited) return internal_memcmp(a1, a2, size);
328 ENSURE_ASAN_INITED();
329 if (flags()->replace_intrin) {
330 if (flags()->strict_memcmp) {
331 // Check the entire regions even if the first bytes of the buffers are
332 // different.
333 ASAN_READ_RANGE(a1, size);
334 ASAN_READ_RANGE(a2, size);
335 // Fallthrough to REAL(memcmp) below.
336 } else {
337 unsigned char c1 = 0, c2 = 0;
338 const unsigned char *s1 = (const unsigned char*)a1;
339 const unsigned char *s2 = (const unsigned char*)a2;
340 uptr i;
341 for (i = 0; i < size; i++) {
342 c1 = s1[i];
343 c2 = s2[i];
344 if (c1 != c2) break;
346 ASAN_READ_RANGE(s1, Min(i + 1, size));
347 ASAN_READ_RANGE(s2, Min(i + 1, size));
348 return CharCmp(c1, c2);
351 return REAL(memcmp(a1, a2, size));
354 #define MEMMOVE_BODY { \
355 if (!asan_inited) return internal_memmove(to, from, size); \
356 if (asan_init_is_running) { \
357 return REAL(memmove)(to, from, size); \
359 ENSURE_ASAN_INITED(); \
360 if (flags()->replace_intrin) { \
361 ASAN_READ_RANGE(from, size); \
362 ASAN_WRITE_RANGE(to, size); \
364 return internal_memmove(to, from, size); \
367 INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) MEMMOVE_BODY
369 INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
370 #if !SANITIZER_MAC
371 if (!asan_inited) return internal_memcpy(to, from, size);
372 // memcpy is called during __asan_init() from the internals
373 // of printf(...).
374 if (asan_init_is_running) {
375 return REAL(memcpy)(to, from, size);
377 ENSURE_ASAN_INITED();
378 if (flags()->replace_intrin) {
379 if (to != from) {
380 // We do not treat memcpy with to==from as a bug.
381 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
382 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
384 ASAN_READ_RANGE(from, size);
385 ASAN_WRITE_RANGE(to, size);
387 // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8, so
388 // calling REAL(memcpy) here leads to infinite recursion.
389 // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
390 return internal_memcpy(to, from, size);
391 #else
392 // At least on 10.7 and 10.8 both memcpy() and memmove() are being replaced
393 // with WRAP(memcpy). As a result, false positives are reported for memmove()
394 // calls. If we just disable error reporting with
395 // ASAN_OPTIONS=replace_intrin=0, memmove() is still replaced with
396 // internal_memcpy(), which may lead to crashes, see
397 // http://llvm.org/bugs/show_bug.cgi?id=16362.
398 MEMMOVE_BODY
399 #endif // !SANITIZER_MAC
402 INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
403 if (!asan_inited) return internal_memset(block, c, size);
404 // memset is called inside Printf.
405 if (asan_init_is_running) {
406 return REAL(memset)(block, c, size);
408 ENSURE_ASAN_INITED();
409 if (flags()->replace_intrin) {
410 ASAN_WRITE_RANGE(block, size);
412 return REAL(memset)(block, c, size);
415 INTERCEPTOR(char*, strchr, const char *str, int c) {
416 if (!asan_inited) return internal_strchr(str, c);
417 // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is
418 // used.
419 if (asan_init_is_running) {
420 return REAL(strchr)(str, c);
422 ENSURE_ASAN_INITED();
423 char *result = REAL(strchr)(str, c);
424 if (flags()->replace_str) {
425 uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1;
426 ASAN_READ_RANGE(str, bytes_read);
428 return result;
431 #if ASAN_INTERCEPT_INDEX
432 # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
433 INTERCEPTOR(char*, index, const char *string, int c)
434 ALIAS(WRAPPER_NAME(strchr));
435 # else
436 # if SANITIZER_MAC
437 DECLARE_REAL(char*, index, const char *string, int c)
438 OVERRIDE_FUNCTION(index, strchr);
439 # else
440 DEFINE_REAL(char*, index, const char *string, int c)
441 # endif
442 # endif
443 #endif // ASAN_INTERCEPT_INDEX
445 // For both strcat() and strncat() we need to check the validity of |to|
446 // argument irrespective of the |from| length.
447 INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT
448 ENSURE_ASAN_INITED();
449 if (flags()->replace_str) {
450 uptr from_length = REAL(strlen)(from);
451 ASAN_READ_RANGE(from, from_length + 1);
452 uptr to_length = REAL(strlen)(to);
453 ASAN_READ_RANGE(to, to_length);
454 ASAN_WRITE_RANGE(to + to_length, from_length + 1);
455 // If the copying actually happens, the |from| string should not overlap
456 // with the resulting string starting at |to|, which has a length of
457 // to_length + from_length + 1.
458 if (from_length > 0) {
459 CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
460 from, from_length + 1);
463 return REAL(strcat)(to, from); // NOLINT
466 INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
467 ENSURE_ASAN_INITED();
468 if (flags()->replace_str) {
469 uptr from_length = MaybeRealStrnlen(from, size);
470 uptr copy_length = Min(size, from_length + 1);
471 ASAN_READ_RANGE(from, copy_length);
472 uptr to_length = REAL(strlen)(to);
473 ASAN_READ_RANGE(to, to_length);
474 ASAN_WRITE_RANGE(to + to_length, from_length + 1);
475 if (from_length > 0) {
476 CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
477 from, copy_length);
480 return REAL(strncat)(to, from, size);
483 INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT
484 #if SANITIZER_MAC
485 if (!asan_inited) return REAL(strcpy)(to, from); // NOLINT
486 #endif
487 // strcpy is called from malloc_default_purgeable_zone()
488 // in __asan::ReplaceSystemAlloc() on Mac.
489 if (asan_init_is_running) {
490 return REAL(strcpy)(to, from); // NOLINT
492 ENSURE_ASAN_INITED();
493 if (flags()->replace_str) {
494 uptr from_size = REAL(strlen)(from) + 1;
495 CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
496 ASAN_READ_RANGE(from, from_size);
497 ASAN_WRITE_RANGE(to, from_size);
499 return REAL(strcpy)(to, from); // NOLINT
502 #if ASAN_INTERCEPT_STRDUP
503 INTERCEPTOR(char*, strdup, const char *s) {
504 if (!asan_inited) return internal_strdup(s);
505 ENSURE_ASAN_INITED();
506 uptr length = REAL(strlen)(s);
507 if (flags()->replace_str) {
508 ASAN_READ_RANGE(s, length + 1);
510 GET_STACK_TRACE_MALLOC;
511 void *new_mem = asan_malloc(length + 1, &stack);
512 REAL(memcpy)(new_mem, s, length + 1);
513 return reinterpret_cast<char*>(new_mem);
515 #endif
517 INTERCEPTOR(uptr, strlen, const char *s) {
518 if (!asan_inited) return internal_strlen(s);
519 // strlen is called from malloc_default_purgeable_zone()
520 // in __asan::ReplaceSystemAlloc() on Mac.
521 if (asan_init_is_running) {
522 return REAL(strlen)(s);
524 ENSURE_ASAN_INITED();
525 uptr length = REAL(strlen)(s);
526 if (flags()->replace_str) {
527 ASAN_READ_RANGE(s, length + 1);
529 return length;
532 INTERCEPTOR(uptr, wcslen, const wchar_t *s) {
533 uptr length = REAL(wcslen)(s);
534 if (!asan_init_is_running) {
535 ENSURE_ASAN_INITED();
536 ASAN_READ_RANGE(s, (length + 1) * sizeof(wchar_t));
538 return length;
541 INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
542 ENSURE_ASAN_INITED();
543 if (flags()->replace_str) {
544 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
545 CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
546 ASAN_READ_RANGE(from, from_size);
547 ASAN_WRITE_RANGE(to, size);
549 return REAL(strncpy)(to, from, size);
552 #if ASAN_INTERCEPT_STRNLEN
553 INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) {
554 ENSURE_ASAN_INITED();
555 uptr length = REAL(strnlen)(s, maxlen);
556 if (flags()->replace_str) {
557 ASAN_READ_RANGE(s, Min(length + 1, maxlen));
559 return length;
561 #endif // ASAN_INTERCEPT_STRNLEN
563 static inline bool IsValidStrtolBase(int base) {
564 return (base == 0) || (2 <= base && base <= 36);
567 static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
568 CHECK(endptr);
569 if (nptr == *endptr) {
570 // No digits were found at strtol call, we need to find out the last
571 // symbol accessed by strtoll on our own.
572 // We get this symbol by skipping leading blanks and optional +/- sign.
573 while (IsSpace(*nptr)) nptr++;
574 if (*nptr == '+' || *nptr == '-') nptr++;
575 *endptr = (char*)nptr;
577 CHECK(*endptr >= nptr);
580 INTERCEPTOR(long, strtol, const char *nptr, // NOLINT
581 char **endptr, int base) {
582 ENSURE_ASAN_INITED();
583 if (!flags()->replace_str) {
584 return REAL(strtol)(nptr, endptr, base);
586 char *real_endptr;
587 long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT
588 if (endptr != 0) {
589 *endptr = real_endptr;
591 if (IsValidStrtolBase(base)) {
592 FixRealStrtolEndptr(nptr, &real_endptr);
593 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
595 return result;
598 INTERCEPTOR(int, atoi, const char *nptr) {
599 #if SANITIZER_MAC
600 if (!asan_inited) return REAL(atoi)(nptr);
601 #endif
602 ENSURE_ASAN_INITED();
603 if (!flags()->replace_str) {
604 return REAL(atoi)(nptr);
606 char *real_endptr;
607 // "man atoi" tells that behavior of atoi(nptr) is the same as
608 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
609 // parsed integer can't be stored in *long* type (even if it's
610 // different from int). So, we just imitate this behavior.
611 int result = REAL(strtol)(nptr, &real_endptr, 10);
612 FixRealStrtolEndptr(nptr, &real_endptr);
613 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
614 return result;
617 INTERCEPTOR(long, atol, const char *nptr) { // NOLINT
618 #if SANITIZER_MAC
619 if (!asan_inited) return REAL(atol)(nptr);
620 #endif
621 ENSURE_ASAN_INITED();
622 if (!flags()->replace_str) {
623 return REAL(atol)(nptr);
625 char *real_endptr;
626 long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT
627 FixRealStrtolEndptr(nptr, &real_endptr);
628 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
629 return result;
632 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
633 INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT
634 char **endptr, int base) {
635 ENSURE_ASAN_INITED();
636 if (!flags()->replace_str) {
637 return REAL(strtoll)(nptr, endptr, base);
639 char *real_endptr;
640 long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT
641 if (endptr != 0) {
642 *endptr = real_endptr;
644 // If base has unsupported value, strtoll can exit with EINVAL
645 // without reading any characters. So do additional checks only
646 // if base is valid.
647 if (IsValidStrtolBase(base)) {
648 FixRealStrtolEndptr(nptr, &real_endptr);
649 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
651 return result;
654 INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT
655 ENSURE_ASAN_INITED();
656 if (!flags()->replace_str) {
657 return REAL(atoll)(nptr);
659 char *real_endptr;
660 long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT
661 FixRealStrtolEndptr(nptr, &real_endptr);
662 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
663 return result;
665 #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
667 static void AtCxaAtexit(void *unused) {
668 (void)unused;
669 StopInitOrderChecking();
672 #if ASAN_INTERCEPT___CXA_ATEXIT
673 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
674 void *dso_handle) {
675 #if SANITIZER_MAC
676 if (!asan_inited) return REAL(__cxa_atexit)(func, arg, dso_handle);
677 #endif
678 ENSURE_ASAN_INITED();
679 int res = REAL(__cxa_atexit)(func, arg, dso_handle);
680 REAL(__cxa_atexit)(AtCxaAtexit, 0, 0);
681 return res;
683 #endif // ASAN_INTERCEPT___CXA_ATEXIT
685 #if SANITIZER_WINDOWS
686 INTERCEPTOR_WINAPI(DWORD, CreateThread,
687 void* security, uptr stack_size,
688 DWORD (__stdcall *start_routine)(void*), void* arg,
689 DWORD thr_flags, void* tid) {
690 // Strict init-order checking in thread-hostile.
691 if (flags()->strict_init_order)
692 StopInitOrderChecking();
693 GET_STACK_TRACE_THREAD;
694 u32 current_tid = GetCurrentTidOrInvalid();
695 AsanThread *t = AsanThread::Create(start_routine, arg);
696 CreateThreadContextArgs args = { t, &stack };
697 bool detached = false; // FIXME: how can we determine it on Windows?
698 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
699 return REAL(CreateThread)(security, stack_size,
700 asan_thread_start, t, thr_flags, tid);
703 namespace __asan {
704 void InitializeWindowsInterceptors() {
705 ASAN_INTERCEPT_FUNC(CreateThread);
708 } // namespace __asan
709 #endif
711 // ---------------------- InitializeAsanInterceptors ---------------- {{{1
712 namespace __asan {
713 void InitializeAsanInterceptors() {
714 static bool was_called_once;
715 CHECK(was_called_once == false);
716 was_called_once = true;
717 SANITIZER_COMMON_INTERCEPTORS_INIT;
719 // Intercept mem* functions.
720 ASAN_INTERCEPT_FUNC(memcmp);
721 ASAN_INTERCEPT_FUNC(memmove);
722 ASAN_INTERCEPT_FUNC(memset);
723 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
724 ASAN_INTERCEPT_FUNC(memcpy);
727 // Intercept str* functions.
728 ASAN_INTERCEPT_FUNC(strcat); // NOLINT
729 ASAN_INTERCEPT_FUNC(strchr);
730 ASAN_INTERCEPT_FUNC(strcpy); // NOLINT
731 ASAN_INTERCEPT_FUNC(strlen);
732 ASAN_INTERCEPT_FUNC(wcslen);
733 ASAN_INTERCEPT_FUNC(strncat);
734 ASAN_INTERCEPT_FUNC(strncpy);
735 #if ASAN_INTERCEPT_STRDUP
736 ASAN_INTERCEPT_FUNC(strdup);
737 #endif
738 #if ASAN_INTERCEPT_STRNLEN
739 ASAN_INTERCEPT_FUNC(strnlen);
740 #endif
741 #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
742 ASAN_INTERCEPT_FUNC(index);
743 #endif
745 ASAN_INTERCEPT_FUNC(atoi);
746 ASAN_INTERCEPT_FUNC(atol);
747 ASAN_INTERCEPT_FUNC(strtol);
748 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
749 ASAN_INTERCEPT_FUNC(atoll);
750 ASAN_INTERCEPT_FUNC(strtoll);
751 #endif
753 #if ASAN_INTERCEPT_MLOCKX
754 // Intercept mlock/munlock.
755 ASAN_INTERCEPT_FUNC(mlock);
756 ASAN_INTERCEPT_FUNC(munlock);
757 ASAN_INTERCEPT_FUNC(mlockall);
758 ASAN_INTERCEPT_FUNC(munlockall);
759 #endif
761 // Intecept signal- and jump-related functions.
762 ASAN_INTERCEPT_FUNC(longjmp);
763 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
764 ASAN_INTERCEPT_FUNC(sigaction);
765 #if SANITIZER_ANDROID
766 ASAN_INTERCEPT_FUNC(bsd_signal);
767 #else
768 ASAN_INTERCEPT_FUNC(signal);
769 #endif
770 #endif
771 #if ASAN_INTERCEPT_SWAPCONTEXT
772 ASAN_INTERCEPT_FUNC(swapcontext);
773 #endif
774 #if ASAN_INTERCEPT__LONGJMP
775 ASAN_INTERCEPT_FUNC(_longjmp);
776 #endif
777 #if ASAN_INTERCEPT_SIGLONGJMP
778 ASAN_INTERCEPT_FUNC(siglongjmp);
779 #endif
781 // Intercept exception handling functions.
782 #if ASAN_INTERCEPT___CXA_THROW
783 INTERCEPT_FUNCTION(__cxa_throw);
784 #endif
786 // Intercept threading-related functions
787 #if ASAN_INTERCEPT_PTHREAD_CREATE
788 ASAN_INTERCEPT_FUNC(pthread_create);
789 #endif
791 // Intercept atexit function.
792 #if ASAN_INTERCEPT___CXA_ATEXIT
793 ASAN_INTERCEPT_FUNC(__cxa_atexit);
794 #endif
796 // Some Windows-specific interceptors.
797 #if SANITIZER_WINDOWS
798 InitializeWindowsInterceptors();
799 #endif
801 VReport(1, "AddressSanitizer: libc interceptors initialized\n");
804 } // namespace __asan