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_SIGLONGJMP
447 INTERCEPTOR(void, siglongjmp, void *env, int val) {
448 __asan_handle_no_return();
449 REAL(siglongjmp)(env, val);
453 #if ASAN_INTERCEPT___CXA_THROW
454 INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
455 CHECK(REAL(__cxa_throw));
456 __asan_handle_no_return();
457 REAL(__cxa_throw)(a, b, c);
461 void *__asan_memcpy(void *to, const void *from, uptr size) {
462 ASAN_MEMCPY_IMPL(nullptr, to, from, size);
465 void *__asan_memset(void *block, int c, uptr size) {
466 ASAN_MEMSET_IMPL(nullptr, block, c, size);
469 void *__asan_memmove(void *to, const void *from, uptr size) {
470 ASAN_MEMMOVE_IMPL(nullptr, to, from, size);
473 #if ASAN_INTERCEPT_INDEX
474 # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
475 INTERCEPTOR(char*, index, const char *string, int c)
476 ALIAS(WRAPPER_NAME(strchr));
479 DECLARE_REAL(char*, index, const char *string, int c)
480 OVERRIDE_FUNCTION(index, strchr);
482 DEFINE_REAL(char*, index, const char *string, int c)
485 #endif // ASAN_INTERCEPT_INDEX
487 // For both strcat() and strncat() we need to check the validity of |to|
488 // argument irrespective of the |from| length.
489 INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT
491 ASAN_INTERCEPTOR_ENTER(ctx, strcat); // NOLINT
492 ENSURE_ASAN_INITED();
493 if (flags()->replace_str) {
494 uptr from_length = REAL(strlen)(from);
495 ASAN_READ_RANGE(ctx, from, from_length + 1);
496 uptr to_length = REAL(strlen)(to);
497 ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
498 ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
499 // If the copying actually happens, the |from| string should not overlap
500 // with the resulting string starting at |to|, which has a length of
501 // to_length + from_length + 1.
502 if (from_length > 0) {
503 CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
504 from, from_length + 1);
507 return REAL(strcat)(to, from); // NOLINT
510 INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
512 ASAN_INTERCEPTOR_ENTER(ctx, strncat);
513 ENSURE_ASAN_INITED();
514 if (flags()->replace_str) {
515 uptr from_length = MaybeRealStrnlen(from, size);
516 uptr copy_length = Min(size, from_length + 1);
517 ASAN_READ_RANGE(ctx, from, copy_length);
518 uptr to_length = REAL(strlen)(to);
519 ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
520 ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
521 if (from_length > 0) {
522 CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
526 return REAL(strncat)(to, from, size);
529 INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT
531 ASAN_INTERCEPTOR_ENTER(ctx, strcpy); // NOLINT
533 if (UNLIKELY(!asan_inited)) return REAL(strcpy)(to, from); // NOLINT
535 // strcpy is called from malloc_default_purgeable_zone()
536 // in __asan::ReplaceSystemAlloc() on Mac.
537 if (asan_init_is_running) {
538 return REAL(strcpy)(to, from); // NOLINT
540 ENSURE_ASAN_INITED();
541 if (flags()->replace_str) {
542 uptr from_size = REAL(strlen)(from) + 1;
543 CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
544 ASAN_READ_RANGE(ctx, from, from_size);
545 ASAN_WRITE_RANGE(ctx, to, from_size);
547 return REAL(strcpy)(to, from); // NOLINT
550 INTERCEPTOR(char*, strdup, const char *s) {
552 ASAN_INTERCEPTOR_ENTER(ctx, strdup);
553 if (UNLIKELY(!asan_inited)) return internal_strdup(s);
554 ENSURE_ASAN_INITED();
555 uptr length = REAL(strlen)(s);
556 if (flags()->replace_str) {
557 ASAN_READ_RANGE(ctx, s, length + 1);
559 GET_STACK_TRACE_MALLOC;
560 void *new_mem = asan_malloc(length + 1, &stack);
561 REAL(memcpy)(new_mem, s, length + 1);
562 return reinterpret_cast<char*>(new_mem);
565 #if ASAN_INTERCEPT___STRDUP
566 INTERCEPTOR(char*, __strdup, const char *s) {
568 ASAN_INTERCEPTOR_ENTER(ctx, strdup);
569 if (UNLIKELY(!asan_inited)) return internal_strdup(s);
570 ENSURE_ASAN_INITED();
571 uptr length = REAL(strlen)(s);
572 if (flags()->replace_str) {
573 ASAN_READ_RANGE(ctx, s, length + 1);
575 GET_STACK_TRACE_MALLOC;
576 void *new_mem = asan_malloc(length + 1, &stack);
577 REAL(memcpy)(new_mem, s, length + 1);
578 return reinterpret_cast<char*>(new_mem);
580 #endif // ASAN_INTERCEPT___STRDUP
582 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
584 ASAN_INTERCEPTOR_ENTER(ctx, wcslen);
585 SIZE_T length = internal_wcslen(s);
586 if (!asan_init_is_running) {
587 ENSURE_ASAN_INITED();
588 ASAN_READ_RANGE(ctx, s, (length + 1) * sizeof(wchar_t));
593 INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
595 ASAN_INTERCEPTOR_ENTER(ctx, strncpy);
596 ENSURE_ASAN_INITED();
597 if (flags()->replace_str) {
598 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
599 CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
600 ASAN_READ_RANGE(ctx, from, from_size);
601 ASAN_WRITE_RANGE(ctx, to, size);
603 return REAL(strncpy)(to, from, size);
606 INTERCEPTOR(long, strtol, const char *nptr, // NOLINT
607 char **endptr, int base) {
609 ASAN_INTERCEPTOR_ENTER(ctx, strtol);
610 ENSURE_ASAN_INITED();
611 if (!flags()->replace_str) {
612 return REAL(strtol)(nptr, endptr, base);
615 long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT
616 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
620 INTERCEPTOR(int, atoi, const char *nptr) {
622 ASAN_INTERCEPTOR_ENTER(ctx, atoi);
624 if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
626 ENSURE_ASAN_INITED();
627 if (!flags()->replace_str) {
628 return REAL(atoi)(nptr);
631 // "man atoi" tells that behavior of atoi(nptr) is the same as
632 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
633 // parsed integer can't be stored in *long* type (even if it's
634 // different from int). So, we just imitate this behavior.
635 int result = REAL(strtol)(nptr, &real_endptr, 10);
636 FixRealStrtolEndptr(nptr, &real_endptr);
637 ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
641 INTERCEPTOR(long, atol, const char *nptr) { // NOLINT
643 ASAN_INTERCEPTOR_ENTER(ctx, atol);
645 if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
647 ENSURE_ASAN_INITED();
648 if (!flags()->replace_str) {
649 return REAL(atol)(nptr);
652 long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT
653 FixRealStrtolEndptr(nptr, &real_endptr);
654 ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
658 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
659 INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT
660 char **endptr, int base) {
662 ASAN_INTERCEPTOR_ENTER(ctx, strtoll);
663 ENSURE_ASAN_INITED();
664 if (!flags()->replace_str) {
665 return REAL(strtoll)(nptr, endptr, base);
668 long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT
669 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
673 INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT
675 ASAN_INTERCEPTOR_ENTER(ctx, atoll);
676 ENSURE_ASAN_INITED();
677 if (!flags()->replace_str) {
678 return REAL(atoll)(nptr);
681 long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT
682 FixRealStrtolEndptr(nptr, &real_endptr);
683 ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
686 #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
688 #if ASAN_INTERCEPT___CXA_ATEXIT
689 static void AtCxaAtexit(void *unused) {
691 StopInitOrderChecking();
694 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
697 if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
699 ENSURE_ASAN_INITED();
700 int res = REAL(__cxa_atexit)(func, arg, dso_handle);
701 REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
704 #endif // ASAN_INTERCEPT___CXA_ATEXIT
706 #if ASAN_INTERCEPT_FORK
707 INTERCEPTOR(int, fork, void) {
708 ENSURE_ASAN_INITED();
709 if (common_flags()->coverage) CovBeforeFork();
710 int pid = REAL(fork)();
711 if (common_flags()->coverage) CovAfterFork(pid);
714 #endif // ASAN_INTERCEPT_FORK
716 // ---------------------- InitializeAsanInterceptors ---------------- {{{1
718 void InitializeAsanInterceptors() {
719 static bool was_called_once;
720 CHECK(!was_called_once);
721 was_called_once = true;
722 InitializeCommonInterceptors();
724 // Intercept str* functions.
725 ASAN_INTERCEPT_FUNC(strcat); // NOLINT
726 ASAN_INTERCEPT_FUNC(strcpy); // NOLINT
727 ASAN_INTERCEPT_FUNC(wcslen);
728 ASAN_INTERCEPT_FUNC(strncat);
729 ASAN_INTERCEPT_FUNC(strncpy);
730 ASAN_INTERCEPT_FUNC(strdup);
731 #if ASAN_INTERCEPT___STRDUP
732 ASAN_INTERCEPT_FUNC(__strdup);
734 #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
735 ASAN_INTERCEPT_FUNC(index);
738 ASAN_INTERCEPT_FUNC(atoi);
739 ASAN_INTERCEPT_FUNC(atol);
740 ASAN_INTERCEPT_FUNC(strtol);
741 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
742 ASAN_INTERCEPT_FUNC(atoll);
743 ASAN_INTERCEPT_FUNC(strtoll);
746 // Intecept signal- and jump-related functions.
747 ASAN_INTERCEPT_FUNC(longjmp);
748 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
749 ASAN_INTERCEPT_FUNC(sigaction);
750 #if SANITIZER_ANDROID
751 ASAN_INTERCEPT_FUNC(bsd_signal);
753 ASAN_INTERCEPT_FUNC(signal);
755 #if ASAN_INTERCEPT_SWAPCONTEXT
756 ASAN_INTERCEPT_FUNC(swapcontext);
758 #if ASAN_INTERCEPT__LONGJMP
759 ASAN_INTERCEPT_FUNC(_longjmp);
761 #if ASAN_INTERCEPT_SIGLONGJMP
762 ASAN_INTERCEPT_FUNC(siglongjmp);
765 // Intercept exception handling functions.
766 #if ASAN_INTERCEPT___CXA_THROW
767 ASAN_INTERCEPT_FUNC(__cxa_throw);
770 // Intercept threading-related functions
771 #if ASAN_INTERCEPT_PTHREAD_CREATE
772 #if defined(ASAN_PTHREAD_CREATE_VERSION)
773 ASAN_INTERCEPT_FUNC_VER(pthread_create, ASAN_PTHREAD_CREATE_VERSION);
775 ASAN_INTERCEPT_FUNC(pthread_create);
777 ASAN_INTERCEPT_FUNC(pthread_join);
780 // Intercept atexit function.
781 #if ASAN_INTERCEPT___CXA_ATEXIT
782 ASAN_INTERCEPT_FUNC(__cxa_atexit);
785 #if ASAN_INTERCEPT_FORK
786 ASAN_INTERCEPT_FUNC(fork);
789 InitializePlatformInterceptors();
791 VReport(1, "AddressSanitizer: libc interceptors initialized\n");
794 } // namespace __asan