1 //===-- asan_interceptors.cc ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // Intercept various libc functions.
13 //===----------------------------------------------------------------------===//
15 #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 "asan_suppressions.h"
24 #include "lsan/lsan_common.h"
25 #include "sanitizer_common/sanitizer_libc.h"
28 #include "sanitizer_common/sanitizer_posix.h"
31 #if defined(__i386) && SANITIZER_LINUX
32 #define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.1"
33 #elif defined(__mips__) && SANITIZER_LINUX
34 #define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.2"
39 // Return true if we can quickly decide that the region is unpoisoned.
40 // We assume that a redzone is at least 16 bytes.
41 static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) {
42 if (size == 0) return true;
44 return !AddressIsPoisoned(beg) &&
45 !AddressIsPoisoned(beg + size - 1) &&
46 !AddressIsPoisoned(beg + size / 2);
48 return !AddressIsPoisoned(beg) &&
49 !AddressIsPoisoned(beg + size / 4) &&
50 !AddressIsPoisoned(beg + size - 1) &&
51 !AddressIsPoisoned(beg + 3 * size / 4) &&
52 !AddressIsPoisoned(beg + size / 2);
56 struct AsanInterceptorContext {
57 const char *interceptor_name;
60 // We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
61 // and ASAN_WRITE_RANGE as macro instead of function so
62 // that no extra frames are created, and stack trace contains
63 // relevant information only.
64 // We check all shadow bytes.
65 #define ACCESS_MEMORY_RANGE(ctx, offset, size, isWrite) do { \
66 uptr __offset = (uptr)(offset); \
67 uptr __size = (uptr)(size); \
69 if (__offset > __offset + __size) { \
70 GET_STACK_TRACE_FATAL_HERE; \
71 ReportStringFunctionSizeOverflow(__offset, __size, &stack); \
73 if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \
74 (__bad = __asan_region_is_poisoned(__offset, __size))) { \
75 AsanInterceptorContext *_ctx = (AsanInterceptorContext *)ctx; \
76 bool suppressed = false; \
78 suppressed = IsInterceptorSuppressed(_ctx->interceptor_name); \
79 if (!suppressed && HaveStackTraceBasedSuppressions()) { \
80 GET_STACK_TRACE_FATAL_HERE; \
81 suppressed = IsStackTraceSuppressed(&stack); \
85 GET_CURRENT_PC_BP_SP; \
86 ReportGenericError(pc, bp, sp, __bad, isWrite, __size, 0, false);\
91 // memcpy is called during __asan_init() from the internals of printf(...).
92 // We do not treat memcpy with to==from as a bug.
93 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
94 #define ASAN_MEMCPY_IMPL(ctx, to, from, size) \
96 if (UNLIKELY(!asan_inited)) return internal_memcpy(to, from, size); \
97 if (asan_init_is_running) { \
98 return REAL(memcpy)(to, from, size); \
100 ENSURE_ASAN_INITED(); \
101 if (flags()->replace_intrin) { \
103 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); \
105 ASAN_READ_RANGE(ctx, from, size); \
106 ASAN_WRITE_RANGE(ctx, to, size); \
108 return REAL(memcpy)(to, from, size); \
111 // memset is called inside Printf.
112 #define ASAN_MEMSET_IMPL(ctx, block, c, size) \
114 if (UNLIKELY(!asan_inited)) return internal_memset(block, c, size); \
115 if (asan_init_is_running) { \
116 return REAL(memset)(block, c, size); \
118 ENSURE_ASAN_INITED(); \
119 if (flags()->replace_intrin) { \
120 ASAN_WRITE_RANGE(ctx, block, size); \
122 return REAL(memset)(block, c, size); \
125 #define ASAN_MEMMOVE_IMPL(ctx, to, from, size) \
127 if (UNLIKELY(!asan_inited)) return internal_memmove(to, from, size); \
128 ENSURE_ASAN_INITED(); \
129 if (flags()->replace_intrin) { \
130 ASAN_READ_RANGE(ctx, from, size); \
131 ASAN_WRITE_RANGE(ctx, to, size); \
133 return internal_memmove(to, from, size); \
136 #define ASAN_READ_RANGE(ctx, offset, size) \
137 ACCESS_MEMORY_RANGE(ctx, offset, size, false)
138 #define ASAN_WRITE_RANGE(ctx, offset, size) \
139 ACCESS_MEMORY_RANGE(ctx, offset, size, true)
141 #define ASAN_READ_STRING_OF_LEN(ctx, s, len, n) \
142 ASAN_READ_RANGE((ctx), (s), \
143 common_flags()->strict_string_checks ? (len) + 1 : (n))
145 #define ASAN_READ_STRING(ctx, s, n) \
146 ASAN_READ_STRING_OF_LEN((ctx), (s), REAL(strlen)(s), (n))
148 // Behavior of functions like "memcpy" or "strcpy" is undefined
149 // if memory intervals overlap. We report error in this case.
150 // Macro is used to avoid creation of new frames.
151 static inline bool RangesOverlap(const char *offset1, uptr length1,
152 const char *offset2, uptr length2) {
153 return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
155 #define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
156 const char *offset1 = (const char*)_offset1; \
157 const char *offset2 = (const char*)_offset2; \
158 if (RangesOverlap(offset1, length1, offset2, length2)) { \
159 GET_STACK_TRACE_FATAL_HERE; \
160 ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
161 offset2, length2, &stack); \
165 static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
166 #if SANITIZER_INTERCEPT_STRNLEN
168 return REAL(strnlen)(s, maxlen);
171 return internal_strnlen(s, maxlen);
174 void SetThreadName(const char *name) {
175 AsanThread *t = GetCurrentThread();
177 asanThreadRegistry().SetThreadName(t->tid(), name);
181 // FIXME: ask frontend whether we need to return failure.
185 } // namespace __asan
187 // ---------------------- Wrappers ---------------- {{{1
188 using namespace __asan; // NOLINT
190 DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr)
191 DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
193 #define ASAN_INTERCEPTOR_ENTER(ctx, func) \
194 AsanInterceptorContext _ctx = {#func}; \
195 ctx = (void *)&_ctx; \
198 #define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name)
199 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
200 ASAN_INTERCEPT_FUNC_VER(name, ver)
201 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
202 ASAN_WRITE_RANGE(ctx, ptr, size)
203 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
204 ASAN_READ_RANGE(ctx, ptr, size)
205 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
206 ASAN_INTERCEPTOR_ENTER(ctx, func); \
208 if (asan_init_is_running) \
209 return REAL(func)(__VA_ARGS__); \
210 if (SANITIZER_MAC && UNLIKELY(!asan_inited)) \
211 return REAL(func)(__VA_ARGS__); \
212 ENSURE_ASAN_INITED(); \
214 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
217 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
220 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
223 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
226 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
227 // Should be asanThreadRegistry().SetThreadNameByUserId(thread, name)
228 // But asan does not remember UserId's for threads (pthread_t);
229 // and remembers all ever existed threads, so the linear search by UserId
231 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
234 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
235 // Strict init-order checking is dlopen-hostile:
236 // https://github.com/google/sanitizers/issues/178
237 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
239 if (flags()->strict_init_order) \
240 StopInitOrderChecking(); \
241 CheckNoDeepBind(filename, flag); \
243 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
244 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \
245 CoverageUpdateMapping()
246 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() CoverageUpdateMapping()
247 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited)
248 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
249 if (AsanThread *t = GetCurrentThread()) { \
250 *begin = t->tls_begin(); \
251 *end = t->tls_end(); \
256 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
258 ASAN_INTERCEPTOR_ENTER(ctx, memmove); \
259 ASAN_MEMMOVE_IMPL(ctx, to, from, size); \
262 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
264 ASAN_INTERCEPTOR_ENTER(ctx, memcpy); \
265 ASAN_MEMCPY_IMPL(ctx, to, from, size); \
268 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
270 ASAN_INTERCEPTOR_ENTER(ctx, memset); \
271 ASAN_MEMSET_IMPL(ctx, block, c, size); \
274 #include "sanitizer_common/sanitizer_common_interceptors.inc"
276 // Syscall interceptors don't have contexts, we don't support suppressions
278 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(nullptr, p, s)
279 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(nullptr, p, s)
280 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
285 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
290 #include "sanitizer_common/sanitizer_common_syscalls.inc"
292 struct ThreadStartParam {
294 atomic_uintptr_t is_registered;
297 #if ASAN_INTERCEPT_PTHREAD_CREATE
298 static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
299 ThreadStartParam *param = reinterpret_cast<ThreadStartParam *>(arg);
300 AsanThread *t = nullptr;
301 while ((t = reinterpret_cast<AsanThread *>(
302 atomic_load(¶m->t, memory_order_acquire))) == nullptr)
303 internal_sched_yield();
305 return t->ThreadStart(GetTid(), ¶m->is_registered);
308 INTERCEPTOR(int, pthread_create, void *thread,
309 void *attr, void *(*start_routine)(void*), void *arg) {
310 EnsureMainThreadIDIsCorrect();
311 // Strict init-order checking is thread-hostile.
312 if (flags()->strict_init_order)
313 StopInitOrderChecking();
314 GET_STACK_TRACE_THREAD;
317 REAL(pthread_attr_getdetachstate)(attr, &detached);
318 ThreadStartParam param;
319 atomic_store(¶m.t, 0, memory_order_relaxed);
320 atomic_store(¶m.is_registered, 0, memory_order_relaxed);
323 // Ignore all allocations made by pthread_create: thread stack/TLS may be
324 // stored by pthread for future reuse even after thread destruction, and
325 // the linked list it's stored in doesn't even hold valid pointers to the
326 // objects, the latter are calculated by obscure pointer arithmetic.
327 #if CAN_SANITIZE_LEAKS
328 __lsan::ScopedInterceptorDisabler disabler;
330 result = REAL(pthread_create)(thread, attr, asan_thread_start, ¶m);
333 u32 current_tid = GetCurrentTidOrInvalid();
335 AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
336 atomic_store(¶m.t, reinterpret_cast<uptr>(t), memory_order_release);
337 // Wait until the AsanThread object is initialized and the ThreadRegistry
338 // entry is in "started" state. One reason for this is that after this
339 // interceptor exits, the child thread's stack may be the only thing holding
340 // the |arg| pointer. This may cause LSan to report a leak if leak checking
341 // happens at a point when the interceptor has already exited, but the stack
342 // range for the child thread is not yet known.
343 while (atomic_load(¶m.is_registered, memory_order_acquire) == 0)
344 internal_sched_yield();
349 INTERCEPTOR(int, pthread_join, void *t, void **arg) {
350 return real_pthread_join(t, arg);
353 DEFINE_REAL_PTHREAD_FUNCTIONS
354 #endif // ASAN_INTERCEPT_PTHREAD_CREATE
356 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
358 #if SANITIZER_ANDROID
359 INTERCEPTOR(void*, bsd_signal, int signum, void *handler) {
360 if (!IsHandledDeadlySignal(signum) ||
361 common_flags()->allow_user_segv_handler) {
362 return REAL(bsd_signal)(signum, handler);
368 INTERCEPTOR(void*, signal, int signum, void *handler) {
369 if (!IsHandledDeadlySignal(signum) ||
370 common_flags()->allow_user_segv_handler) {
371 return REAL(signal)(signum, handler);
376 INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
377 struct sigaction *oldact) {
378 if (!IsHandledDeadlySignal(signum) ||
379 common_flags()->allow_user_segv_handler) {
380 return REAL(sigaction)(signum, act, oldact);
385 namespace __sanitizer {
386 int real_sigaction(int signum, const void *act, void *oldact) {
387 return REAL(sigaction)(signum, (const struct sigaction *)act,
388 (struct sigaction *)oldact);
390 } // namespace __sanitizer
392 #elif SANITIZER_POSIX
393 // We need to have defined REAL(sigaction) on posix systems.
394 DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
395 struct sigaction *oldact)
396 #endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
398 #if ASAN_INTERCEPT_SWAPCONTEXT
399 static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
400 // Align to page size.
401 uptr PageSize = GetPageSizeCached();
402 uptr bottom = stack & ~(PageSize - 1);
403 ssize += stack - bottom;
404 ssize = RoundUpTo(ssize, PageSize);
405 static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb
406 if (AddrIsInMem(bottom) && ssize && ssize <= kMaxSaneContextStackSize) {
407 PoisonShadow(bottom, ssize, 0);
411 INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
412 struct ucontext_t *ucp) {
413 static bool reported_warning = false;
414 if (!reported_warning) {
415 Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
416 "functions and may produce false positives in some cases!\n");
417 reported_warning = true;
419 // Clear shadow memory for new context (it may share stack
420 // with current context).
422 ReadContextStack(ucp, &stack, &ssize);
423 ClearShadowMemoryForContextStack(stack, ssize);
424 int res = REAL(swapcontext)(oucp, ucp);
425 // swapcontext technically does not return, but program may swap context to
426 // "oucp" later, that would look as if swapcontext() returned 0.
427 // We need to clear shadow for ucp once again, as it may be in arbitrary
429 ClearShadowMemoryForContextStack(stack, ssize);
432 #endif // ASAN_INTERCEPT_SWAPCONTEXT
434 INTERCEPTOR(void, longjmp, void *env, int val) {
435 __asan_handle_no_return();
436 REAL(longjmp)(env, val);
439 #if ASAN_INTERCEPT__LONGJMP
440 INTERCEPTOR(void, _longjmp, void *env, int val) {
441 __asan_handle_no_return();
442 REAL(_longjmp)(env, val);
446 #if ASAN_INTERCEPT___LONGJMP_CHK
447 INTERCEPTOR(void, __longjmp_chk, void *env, int val) {
448 __asan_handle_no_return();
449 REAL(__longjmp_chk)(env, val);
453 #if ASAN_INTERCEPT_SIGLONGJMP
454 INTERCEPTOR(void, siglongjmp, void *env, int val) {
455 __asan_handle_no_return();
456 REAL(siglongjmp)(env, val);
460 #if ASAN_INTERCEPT___CXA_THROW
461 INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
462 CHECK(REAL(__cxa_throw));
463 __asan_handle_no_return();
464 REAL(__cxa_throw)(a, b, c);
468 void *__asan_memcpy(void *to, const void *from, uptr size) {
469 ASAN_MEMCPY_IMPL(nullptr, to, from, size);
472 void *__asan_memset(void *block, int c, uptr size) {
473 ASAN_MEMSET_IMPL(nullptr, block, c, size);
476 void *__asan_memmove(void *to, const void *from, uptr size) {
477 ASAN_MEMMOVE_IMPL(nullptr, to, from, size);
480 #if ASAN_INTERCEPT_INDEX
481 # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
482 INTERCEPTOR(char*, index, const char *string, int c)
483 ALIAS(WRAPPER_NAME(strchr));
486 DECLARE_REAL(char*, index, const char *string, int c)
487 OVERRIDE_FUNCTION(index, strchr);
489 DEFINE_REAL(char*, index, const char *string, int c)
492 #endif // ASAN_INTERCEPT_INDEX
494 // For both strcat() and strncat() we need to check the validity of |to|
495 // argument irrespective of the |from| length.
496 INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT
498 ASAN_INTERCEPTOR_ENTER(ctx, strcat); // NOLINT
499 ENSURE_ASAN_INITED();
500 if (flags()->replace_str) {
501 uptr from_length = REAL(strlen)(from);
502 ASAN_READ_RANGE(ctx, from, from_length + 1);
503 uptr to_length = REAL(strlen)(to);
504 ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
505 ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
506 // If the copying actually happens, the |from| string should not overlap
507 // with the resulting string starting at |to|, which has a length of
508 // to_length + from_length + 1.
509 if (from_length > 0) {
510 CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
511 from, from_length + 1);
514 return REAL(strcat)(to, from); // NOLINT
517 INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
519 ASAN_INTERCEPTOR_ENTER(ctx, strncat);
520 ENSURE_ASAN_INITED();
521 if (flags()->replace_str) {
522 uptr from_length = MaybeRealStrnlen(from, size);
523 uptr copy_length = Min(size, from_length + 1);
524 ASAN_READ_RANGE(ctx, from, copy_length);
525 uptr to_length = REAL(strlen)(to);
526 ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
527 ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
528 if (from_length > 0) {
529 CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
533 return REAL(strncat)(to, from, size);
536 INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT
538 ASAN_INTERCEPTOR_ENTER(ctx, strcpy); // NOLINT
540 if (UNLIKELY(!asan_inited)) return REAL(strcpy)(to, from); // NOLINT
542 // strcpy is called from malloc_default_purgeable_zone()
543 // in __asan::ReplaceSystemAlloc() on Mac.
544 if (asan_init_is_running) {
545 return REAL(strcpy)(to, from); // NOLINT
547 ENSURE_ASAN_INITED();
548 if (flags()->replace_str) {
549 uptr from_size = REAL(strlen)(from) + 1;
550 CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
551 ASAN_READ_RANGE(ctx, from, from_size);
552 ASAN_WRITE_RANGE(ctx, to, from_size);
554 return REAL(strcpy)(to, from); // NOLINT
557 INTERCEPTOR(char*, strdup, const char *s) {
559 ASAN_INTERCEPTOR_ENTER(ctx, strdup);
560 if (UNLIKELY(!asan_inited)) return internal_strdup(s);
561 ENSURE_ASAN_INITED();
562 uptr length = REAL(strlen)(s);
563 if (flags()->replace_str) {
564 ASAN_READ_RANGE(ctx, s, length + 1);
566 GET_STACK_TRACE_MALLOC;
567 void *new_mem = asan_malloc(length + 1, &stack);
568 REAL(memcpy)(new_mem, s, length + 1);
569 return reinterpret_cast<char*>(new_mem);
572 #if ASAN_INTERCEPT___STRDUP
573 INTERCEPTOR(char*, __strdup, const char *s) {
575 ASAN_INTERCEPTOR_ENTER(ctx, strdup);
576 if (UNLIKELY(!asan_inited)) return internal_strdup(s);
577 ENSURE_ASAN_INITED();
578 uptr length = REAL(strlen)(s);
579 if (flags()->replace_str) {
580 ASAN_READ_RANGE(ctx, s, length + 1);
582 GET_STACK_TRACE_MALLOC;
583 void *new_mem = asan_malloc(length + 1, &stack);
584 REAL(memcpy)(new_mem, s, length + 1);
585 return reinterpret_cast<char*>(new_mem);
587 #endif // ASAN_INTERCEPT___STRDUP
589 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
591 ASAN_INTERCEPTOR_ENTER(ctx, wcslen);
592 SIZE_T length = internal_wcslen(s);
593 if (!asan_init_is_running) {
594 ENSURE_ASAN_INITED();
595 ASAN_READ_RANGE(ctx, s, (length + 1) * sizeof(wchar_t));
600 INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
602 ASAN_INTERCEPTOR_ENTER(ctx, strncpy);
603 ENSURE_ASAN_INITED();
604 if (flags()->replace_str) {
605 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
606 CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
607 ASAN_READ_RANGE(ctx, from, from_size);
608 ASAN_WRITE_RANGE(ctx, to, size);
610 return REAL(strncpy)(to, from, size);
613 INTERCEPTOR(long, strtol, const char *nptr, // NOLINT
614 char **endptr, int base) {
616 ASAN_INTERCEPTOR_ENTER(ctx, strtol);
617 ENSURE_ASAN_INITED();
618 if (!flags()->replace_str) {
619 return REAL(strtol)(nptr, endptr, base);
622 long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT
623 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
627 INTERCEPTOR(int, atoi, const char *nptr) {
629 ASAN_INTERCEPTOR_ENTER(ctx, atoi);
631 if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
633 ENSURE_ASAN_INITED();
634 if (!flags()->replace_str) {
635 return REAL(atoi)(nptr);
638 // "man atoi" tells that behavior of atoi(nptr) is the same as
639 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
640 // parsed integer can't be stored in *long* type (even if it's
641 // different from int). So, we just imitate this behavior.
642 int result = REAL(strtol)(nptr, &real_endptr, 10);
643 FixRealStrtolEndptr(nptr, &real_endptr);
644 ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
648 INTERCEPTOR(long, atol, const char *nptr) { // NOLINT
650 ASAN_INTERCEPTOR_ENTER(ctx, atol);
652 if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
654 ENSURE_ASAN_INITED();
655 if (!flags()->replace_str) {
656 return REAL(atol)(nptr);
659 long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT
660 FixRealStrtolEndptr(nptr, &real_endptr);
661 ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
665 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
666 INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT
667 char **endptr, int base) {
669 ASAN_INTERCEPTOR_ENTER(ctx, strtoll);
670 ENSURE_ASAN_INITED();
671 if (!flags()->replace_str) {
672 return REAL(strtoll)(nptr, endptr, base);
675 long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT
676 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
680 INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT
682 ASAN_INTERCEPTOR_ENTER(ctx, atoll);
683 ENSURE_ASAN_INITED();
684 if (!flags()->replace_str) {
685 return REAL(atoll)(nptr);
688 long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT
689 FixRealStrtolEndptr(nptr, &real_endptr);
690 ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
693 #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
695 #if ASAN_INTERCEPT___CXA_ATEXIT
696 static void AtCxaAtexit(void *unused) {
698 StopInitOrderChecking();
701 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
704 if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
706 ENSURE_ASAN_INITED();
707 int res = REAL(__cxa_atexit)(func, arg, dso_handle);
708 REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
711 #endif // ASAN_INTERCEPT___CXA_ATEXIT
713 #if ASAN_INTERCEPT_FORK
714 INTERCEPTOR(int, fork, void) {
715 ENSURE_ASAN_INITED();
716 if (common_flags()->coverage) CovBeforeFork();
717 int pid = REAL(fork)();
718 if (common_flags()->coverage) CovAfterFork(pid);
721 #endif // ASAN_INTERCEPT_FORK
723 // ---------------------- InitializeAsanInterceptors ---------------- {{{1
725 void InitializeAsanInterceptors() {
726 static bool was_called_once;
727 CHECK(!was_called_once);
728 was_called_once = true;
729 InitializeCommonInterceptors();
731 // Intercept str* functions.
732 ASAN_INTERCEPT_FUNC(strcat); // NOLINT
733 ASAN_INTERCEPT_FUNC(strcpy); // NOLINT
734 ASAN_INTERCEPT_FUNC(wcslen);
735 ASAN_INTERCEPT_FUNC(strncat);
736 ASAN_INTERCEPT_FUNC(strncpy);
737 ASAN_INTERCEPT_FUNC(strdup);
738 #if ASAN_INTERCEPT___STRDUP
739 ASAN_INTERCEPT_FUNC(__strdup);
741 #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
742 ASAN_INTERCEPT_FUNC(index);
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);
753 // Intecept signal- and jump-related functions.
754 ASAN_INTERCEPT_FUNC(longjmp);
755 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
756 ASAN_INTERCEPT_FUNC(sigaction);
757 #if SANITIZER_ANDROID
758 ASAN_INTERCEPT_FUNC(bsd_signal);
760 ASAN_INTERCEPT_FUNC(signal);
762 #if ASAN_INTERCEPT_SWAPCONTEXT
763 ASAN_INTERCEPT_FUNC(swapcontext);
765 #if ASAN_INTERCEPT__LONGJMP
766 ASAN_INTERCEPT_FUNC(_longjmp);
768 #if ASAN_INTERCEPT___LONGJMP_CHK
769 ASAN_INTERCEPT_FUNC(__longjmp_chk);
771 #if ASAN_INTERCEPT_SIGLONGJMP
772 ASAN_INTERCEPT_FUNC(siglongjmp);
775 // Intercept exception handling functions.
776 #if ASAN_INTERCEPT___CXA_THROW
777 ASAN_INTERCEPT_FUNC(__cxa_throw);
780 // Intercept threading-related functions
781 #if ASAN_INTERCEPT_PTHREAD_CREATE
782 #if defined(ASAN_PTHREAD_CREATE_VERSION)
783 ASAN_INTERCEPT_FUNC_VER(pthread_create, ASAN_PTHREAD_CREATE_VERSION);
785 ASAN_INTERCEPT_FUNC(pthread_create);
787 ASAN_INTERCEPT_FUNC(pthread_join);
790 // Intercept atexit function.
791 #if ASAN_INTERCEPT___CXA_ATEXIT
792 ASAN_INTERCEPT_FUNC(__cxa_atexit);
795 #if ASAN_INTERCEPT_FORK
796 ASAN_INTERCEPT_FUNC(fork);
799 InitializePlatformInterceptors();
801 VReport(1, "AddressSanitizer: libc interceptors initialized\n");
804 } // namespace __asan