1 //===-- asan_interceptors.cpp ---------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // Intercept various libc functions.
12 //===----------------------------------------------------------------------===//
14 #include "asan_interceptors.h"
15 #include "asan_allocator.h"
16 #include "asan_internal.h"
17 #include "asan_mapping.h"
18 #include "asan_poisoning.h"
19 #include "asan_report.h"
20 #include "asan_stack.h"
21 #include "asan_stats.h"
22 #include "asan_suppressions.h"
23 #include "lsan/lsan_common.h"
24 #include "sanitizer_common/sanitizer_libc.h"
26 // There is no general interception at all on Fuchsia.
27 // Only the functions in asan_interceptors_memintrinsics.cpp are
28 // really defined to replace libc functions.
29 #if !SANITIZER_FUCHSIA
32 # include "sanitizer_common/sanitizer_posix.h"
35 # if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION || \
36 ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION
40 # if defined(__i386) && SANITIZER_LINUX
41 # define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.1"
42 # elif defined(__mips__) && SANITIZER_LINUX
43 # define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.2"
48 #define ASAN_READ_STRING_OF_LEN(ctx, s, len, n) \
49 ASAN_READ_RANGE((ctx), (s), \
50 common_flags()->strict_string_checks ? (len) + 1 : (n))
52 # define ASAN_READ_STRING(ctx, s, n) \
53 ASAN_READ_STRING_OF_LEN((ctx), (s), internal_strlen(s), (n))
55 static inline uptr
MaybeRealStrnlen(const char *s
, uptr maxlen
) {
56 #if SANITIZER_INTERCEPT_STRNLEN
58 return REAL(strnlen
)(s
, maxlen
);
61 return internal_strnlen(s
, maxlen
);
64 void SetThreadName(const char *name
) {
65 AsanThread
*t
= GetCurrentThread();
67 asanThreadRegistry().SetThreadName(t
->tid(), name
);
71 if (CAN_SANITIZE_LEAKS
&& common_flags()->detect_leaks
&&
72 __lsan::HasReportedLeaks()) {
73 return common_flags()->exitcode
;
75 // FIXME: ask frontend whether we need to return failure.
81 // ---------------------- Wrappers ---------------- {{{1
82 using namespace __asan
;
84 DECLARE_REAL_AND_INTERCEPTOR(void *, malloc
, uptr
)
85 DECLARE_REAL_AND_INTERCEPTOR(void, free
, void *)
87 #define ASAN_INTERCEPTOR_ENTER(ctx, func) \
88 AsanInterceptorContext _ctx = {#func}; \
89 ctx = (void *)&_ctx; \
92 #define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name)
93 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
94 ASAN_INTERCEPT_FUNC_VER(name, ver)
95 #define COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(name, ver) \
96 ASAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver)
97 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
98 ASAN_WRITE_RANGE(ctx, ptr, size)
99 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
100 ASAN_READ_RANGE(ctx, ptr, size)
101 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
102 ASAN_INTERCEPTOR_ENTER(ctx, func); \
104 if (asan_init_is_running) \
105 return REAL(func)(__VA_ARGS__); \
106 if (SANITIZER_APPLE && UNLIKELY(!asan_inited)) \
107 return REAL(func)(__VA_ARGS__); \
108 ENSURE_ASAN_INITED(); \
110 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
113 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
116 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
119 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
122 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
123 // Should be asanThreadRegistry().SetThreadNameByUserId(thread, name)
124 // But asan does not remember UserId's for threads (pthread_t);
125 // and remembers all ever existed threads, so the linear search by UserId
127 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
130 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
131 // Strict init-order checking is dlopen-hostile:
132 // https://github.com/google/sanitizers/issues/178
133 # define COMMON_INTERCEPTOR_DLOPEN(filename, flag) \
135 if (flags()->strict_init_order) \
136 StopInitOrderChecking(); \
137 CheckNoDeepBind(filename, flag); \
138 REAL(dlopen)(filename, flag); \
140 # define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
141 # define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle)
142 # define COMMON_INTERCEPTOR_LIBRARY_UNLOADED()
143 # define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited)
144 # define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
145 if (AsanThread *t = GetCurrentThread()) { \
146 *begin = t->tls_begin(); \
147 *end = t->tls_end(); \
152 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
154 ASAN_INTERCEPTOR_ENTER(ctx, memmove); \
155 ASAN_MEMMOVE_IMPL(ctx, to, from, size); \
158 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
160 ASAN_INTERCEPTOR_ENTER(ctx, memcpy); \
161 ASAN_MEMCPY_IMPL(ctx, to, from, size); \
164 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
166 ASAN_INTERCEPTOR_ENTER(ctx, memset); \
167 ASAN_MEMSET_IMPL(ctx, block, c, size); \
170 #if CAN_SANITIZE_LEAKS
171 #define COMMON_INTERCEPTOR_STRERROR() \
172 __lsan::ScopedInterceptorDisabler disabler
175 #include "sanitizer_common/sanitizer_common_interceptors.inc"
176 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
178 // Syscall interceptors don't have contexts, we don't support suppressions
180 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(nullptr, p, s)
181 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(nullptr, p, s)
182 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
187 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
192 #include "sanitizer_common/sanitizer_common_syscalls.inc"
193 #include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
195 #if ASAN_INTERCEPT_PTHREAD_CREATE
196 static thread_return_t THREAD_CALLING_CONV
asan_thread_start(void *arg
) {
197 AsanThread
*t
= (AsanThread
*)arg
;
199 return t
->ThreadStart(GetTid());
202 INTERCEPTOR(int, pthread_create
, void *thread
,
203 void *attr
, void *(*start_routine
)(void*), void *arg
) {
204 EnsureMainThreadIDIsCorrect();
205 // Strict init-order checking is thread-hostile.
206 if (flags()->strict_init_order
)
207 StopInitOrderChecking();
208 GET_STACK_TRACE_THREAD
;
211 REAL(pthread_attr_getdetachstate
)(attr
, &detached
);
213 u32 current_tid
= GetCurrentTidOrInvalid();
215 AsanThread::Create(start_routine
, arg
, current_tid
, &stack
, detached
);
219 // Ignore all allocations made by pthread_create: thread stack/TLS may be
220 // stored by pthread for future reuse even after thread destruction, and
221 // the linked list it's stored in doesn't even hold valid pointers to the
222 // objects, the latter are calculated by obscure pointer arithmetic.
223 #if CAN_SANITIZE_LEAKS
224 __lsan::ScopedInterceptorDisabler disabler
;
226 result
= REAL(pthread_create
)(thread
, attr
, asan_thread_start
, t
);
229 // If the thread didn't start delete the AsanThread to avoid leaking it.
230 // Note AsanThreadContexts never get destroyed so the AsanThreadContext
231 // that was just created for the AsanThread is wasted.
237 INTERCEPTOR(int, pthread_join
, void *t
, void **arg
) {
238 return real_pthread_join(t
, arg
);
241 DEFINE_REAL_PTHREAD_FUNCTIONS
242 #endif // ASAN_INTERCEPT_PTHREAD_CREATE
244 #if ASAN_INTERCEPT_SWAPCONTEXT
245 static void ClearShadowMemoryForContextStack(uptr stack
, uptr ssize
) {
246 // Only clear if we know the stack. This should be true only for contexts
247 // created with makecontext().
250 // Align to page size.
251 uptr PageSize
= GetPageSizeCached();
252 uptr bottom
= RoundDownTo(stack
, PageSize
);
253 if (!AddrIsInMem(bottom
))
255 ssize
+= stack
- bottom
;
256 ssize
= RoundUpTo(ssize
, PageSize
);
257 PoisonShadow(bottom
, ssize
, 0);
260 INTERCEPTOR(void, makecontext
, struct ucontext_t
*ucp
, void (*func
)(), int argc
,
264 // We don't know a better way to forward ... into REAL function. We can
265 // increase args size if neccecary.
266 CHECK_LE(argc
, ARRAY_SIZE(args
));
267 internal_memset(args
, 0, sizeof(args
));
269 for (int i
= 0; i
< argc
; ++i
) args
[i
] = va_arg(ap
, uptr
);
272 # define ENUMERATE_ARRAY_4(start) \
273 args[start], args[start + 1], args[start + 2], args[start + 3]
274 # define ENUMERATE_ARRAY_16(start) \
275 ENUMERATE_ARRAY_4(start), ENUMERATE_ARRAY_4(start + 4), \
276 ENUMERATE_ARRAY_4(start + 8), ENUMERATE_ARRAY_4(start + 12)
277 # define ENUMERATE_ARRAY_64() \
278 ENUMERATE_ARRAY_16(0), ENUMERATE_ARRAY_16(16), ENUMERATE_ARRAY_16(32), \
279 ENUMERATE_ARRAY_16(48)
282 ((struct ucontext_t
*)ucp
, func
, argc
, ENUMERATE_ARRAY_64());
284 # undef ENUMERATE_ARRAY_4
285 # undef ENUMERATE_ARRAY_16
286 # undef ENUMERATE_ARRAY_64
288 // Sign the stack so we can identify it for unpoisoning.
289 SignContextStack(ucp
);
292 INTERCEPTOR(int, swapcontext
, struct ucontext_t
*oucp
,
293 struct ucontext_t
*ucp
) {
294 static bool reported_warning
= false;
295 if (!reported_warning
) {
296 Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
297 "functions and may produce false positives in some cases!\n");
298 reported_warning
= true;
300 // Clear shadow memory for new context (it may share stack
301 // with current context).
303 ReadContextStack(ucp
, &stack
, &ssize
);
304 ClearShadowMemoryForContextStack(stack
, ssize
);
306 # if __has_attribute(__indirect_return__) && \
307 (defined(__x86_64__) || defined(__i386__))
308 int (*real_swapcontext
)(struct ucontext_t
*, struct ucontext_t
*)
309 __attribute__((__indirect_return__
)) = REAL(swapcontext
);
310 int res
= real_swapcontext(oucp
, ucp
);
312 int res
= REAL(swapcontext
)(oucp
, ucp
);
314 // swapcontext technically does not return, but program may swap context to
315 // "oucp" later, that would look as if swapcontext() returned 0.
316 // We need to clear shadow for ucp once again, as it may be in arbitrary
318 ClearShadowMemoryForContextStack(stack
, ssize
);
321 #endif // ASAN_INTERCEPT_SWAPCONTEXT
324 #define longjmp __longjmp14
325 #define siglongjmp __siglongjmp14
328 INTERCEPTOR(void, longjmp
, void *env
, int val
) {
329 __asan_handle_no_return();
330 REAL(longjmp
)(env
, val
);
333 #if ASAN_INTERCEPT__LONGJMP
334 INTERCEPTOR(void, _longjmp
, void *env
, int val
) {
335 __asan_handle_no_return();
336 REAL(_longjmp
)(env
, val
);
340 #if ASAN_INTERCEPT___LONGJMP_CHK
341 INTERCEPTOR(void, __longjmp_chk
, void *env
, int val
) {
342 __asan_handle_no_return();
343 REAL(__longjmp_chk
)(env
, val
);
347 #if ASAN_INTERCEPT_SIGLONGJMP
348 INTERCEPTOR(void, siglongjmp
, void *env
, int val
) {
349 __asan_handle_no_return();
350 REAL(siglongjmp
)(env
, val
);
354 #if ASAN_INTERCEPT___CXA_THROW
355 INTERCEPTOR(void, __cxa_throw
, void *a
, void *b
, void *c
) {
356 CHECK(REAL(__cxa_throw
));
357 __asan_handle_no_return();
358 REAL(__cxa_throw
)(a
, b
, c
);
362 #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
363 INTERCEPTOR(void, __cxa_rethrow_primary_exception
, void *a
) {
364 CHECK(REAL(__cxa_rethrow_primary_exception
));
365 __asan_handle_no_return();
366 REAL(__cxa_rethrow_primary_exception
)(a
);
370 #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
371 INTERCEPTOR(_Unwind_Reason_Code
, _Unwind_RaiseException
,
372 _Unwind_Exception
*object
) {
373 CHECK(REAL(_Unwind_RaiseException
));
374 __asan_handle_no_return();
375 return REAL(_Unwind_RaiseException
)(object
);
379 #if ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION
380 INTERCEPTOR(_Unwind_Reason_Code
, _Unwind_SjLj_RaiseException
,
381 _Unwind_Exception
*object
) {
382 CHECK(REAL(_Unwind_SjLj_RaiseException
));
383 __asan_handle_no_return();
384 return REAL(_Unwind_SjLj_RaiseException
)(object
);
388 #if ASAN_INTERCEPT_INDEX
389 # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
390 INTERCEPTOR(char*, index
, const char *string
, int c
)
391 ALIAS(WRAPPER_NAME(strchr
));
394 DECLARE_REAL(char*, index
, const char *string
, int c
)
395 OVERRIDE_FUNCTION(index
, strchr
);
397 DEFINE_REAL(char*, index
, const char *string
, int c
)
400 #endif // ASAN_INTERCEPT_INDEX
402 // For both strcat() and strncat() we need to check the validity of |to|
403 // argument irrespective of the |from| length.
404 INTERCEPTOR(char *, strcat
, char *to
, const char *from
) {
406 ASAN_INTERCEPTOR_ENTER(ctx
, strcat
);
407 ENSURE_ASAN_INITED();
408 if (flags()->replace_str
) {
409 uptr from_length
= internal_strlen(from
);
410 ASAN_READ_RANGE(ctx
, from
, from_length
+ 1);
411 uptr to_length
= internal_strlen(to
);
412 ASAN_READ_STRING_OF_LEN(ctx
, to
, to_length
, to_length
);
413 ASAN_WRITE_RANGE(ctx
, to
+ to_length
, from_length
+ 1);
414 // If the copying actually happens, the |from| string should not overlap
415 // with the resulting string starting at |to|, which has a length of
416 // to_length + from_length + 1.
417 if (from_length
> 0) {
418 CHECK_RANGES_OVERLAP("strcat", to
, from_length
+ to_length
+ 1, from
,
422 return REAL(strcat
)(to
, from
);
425 INTERCEPTOR(char*, strncat
, char *to
, const char *from
, uptr size
) {
427 ASAN_INTERCEPTOR_ENTER(ctx
, strncat
);
428 ENSURE_ASAN_INITED();
429 if (flags()->replace_str
) {
430 uptr from_length
= MaybeRealStrnlen(from
, size
);
431 uptr copy_length
= Min(size
, from_length
+ 1);
432 ASAN_READ_RANGE(ctx
, from
, copy_length
);
433 uptr to_length
= internal_strlen(to
);
434 ASAN_READ_STRING_OF_LEN(ctx
, to
, to_length
, to_length
);
435 ASAN_WRITE_RANGE(ctx
, to
+ to_length
, from_length
+ 1);
436 if (from_length
> 0) {
437 CHECK_RANGES_OVERLAP("strncat", to
, to_length
+ copy_length
+ 1,
441 return REAL(strncat
)(to
, from
, size
);
444 INTERCEPTOR(char *, strcpy
, char *to
, const char *from
) {
446 ASAN_INTERCEPTOR_ENTER(ctx
, strcpy
);
448 if (UNLIKELY(!asan_inited
))
449 return REAL(strcpy
)(to
, from
);
451 // strcpy is called from malloc_default_purgeable_zone()
452 // in __asan::ReplaceSystemAlloc() on Mac.
453 if (asan_init_is_running
) {
454 return REAL(strcpy
)(to
, from
);
456 ENSURE_ASAN_INITED();
457 if (flags()->replace_str
) {
458 uptr from_size
= internal_strlen(from
) + 1;
459 CHECK_RANGES_OVERLAP("strcpy", to
, from_size
, from
, from_size
);
460 ASAN_READ_RANGE(ctx
, from
, from_size
);
461 ASAN_WRITE_RANGE(ctx
, to
, from_size
);
463 return REAL(strcpy
)(to
, from
);
466 INTERCEPTOR(char*, strdup
, const char *s
) {
468 ASAN_INTERCEPTOR_ENTER(ctx
, strdup
);
469 if (UNLIKELY(!asan_inited
)) return internal_strdup(s
);
470 ENSURE_ASAN_INITED();
471 uptr length
= internal_strlen(s
);
472 if (flags()->replace_str
) {
473 ASAN_READ_RANGE(ctx
, s
, length
+ 1);
475 GET_STACK_TRACE_MALLOC
;
476 void *new_mem
= asan_malloc(length
+ 1, &stack
);
478 REAL(memcpy
)(new_mem
, s
, length
+ 1);
480 return reinterpret_cast<char*>(new_mem
);
483 #if ASAN_INTERCEPT___STRDUP
484 INTERCEPTOR(char*, __strdup
, const char *s
) {
486 ASAN_INTERCEPTOR_ENTER(ctx
, strdup
);
487 if (UNLIKELY(!asan_inited
)) return internal_strdup(s
);
488 ENSURE_ASAN_INITED();
489 uptr length
= internal_strlen(s
);
490 if (flags()->replace_str
) {
491 ASAN_READ_RANGE(ctx
, s
, length
+ 1);
493 GET_STACK_TRACE_MALLOC
;
494 void *new_mem
= asan_malloc(length
+ 1, &stack
);
496 REAL(memcpy
)(new_mem
, s
, length
+ 1);
498 return reinterpret_cast<char*>(new_mem
);
500 #endif // ASAN_INTERCEPT___STRDUP
502 INTERCEPTOR(char*, strncpy
, char *to
, const char *from
, uptr size
) {
504 ASAN_INTERCEPTOR_ENTER(ctx
, strncpy
);
505 ENSURE_ASAN_INITED();
506 if (flags()->replace_str
) {
507 uptr from_size
= Min(size
, MaybeRealStrnlen(from
, size
) + 1);
508 CHECK_RANGES_OVERLAP("strncpy", to
, from_size
, from
, from_size
);
509 ASAN_READ_RANGE(ctx
, from
, from_size
);
510 ASAN_WRITE_RANGE(ctx
, to
, size
);
512 return REAL(strncpy
)(to
, from
, size
);
515 INTERCEPTOR(long, strtol
, const char *nptr
, char **endptr
, int base
) {
517 ASAN_INTERCEPTOR_ENTER(ctx
, strtol
);
518 ENSURE_ASAN_INITED();
519 if (!flags()->replace_str
) {
520 return REAL(strtol
)(nptr
, endptr
, base
);
523 long result
= REAL(strtol
)(nptr
, &real_endptr
, base
);
524 StrtolFixAndCheck(ctx
, nptr
, endptr
, real_endptr
, base
);
528 INTERCEPTOR(int, atoi
, const char *nptr
) {
530 ASAN_INTERCEPTOR_ENTER(ctx
, atoi
);
532 if (UNLIKELY(!asan_inited
)) return REAL(atoi
)(nptr
);
534 ENSURE_ASAN_INITED();
535 if (!flags()->replace_str
) {
536 return REAL(atoi
)(nptr
);
539 // "man atoi" tells that behavior of atoi(nptr) is the same as
540 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
541 // parsed integer can't be stored in *long* type (even if it's
542 // different from int). So, we just imitate this behavior.
543 int result
= REAL(strtol
)(nptr
, &real_endptr
, 10);
544 FixRealStrtolEndptr(nptr
, &real_endptr
);
545 ASAN_READ_STRING(ctx
, nptr
, (real_endptr
- nptr
) + 1);
549 INTERCEPTOR(long, atol
, const char *nptr
) {
551 ASAN_INTERCEPTOR_ENTER(ctx
, atol
);
553 if (UNLIKELY(!asan_inited
)) return REAL(atol
)(nptr
);
555 ENSURE_ASAN_INITED();
556 if (!flags()->replace_str
) {
557 return REAL(atol
)(nptr
);
560 long result
= REAL(strtol
)(nptr
, &real_endptr
, 10);
561 FixRealStrtolEndptr(nptr
, &real_endptr
);
562 ASAN_READ_STRING(ctx
, nptr
, (real_endptr
- nptr
) + 1);
566 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
567 INTERCEPTOR(long long, strtoll
, const char *nptr
, char **endptr
, int base
) {
569 ASAN_INTERCEPTOR_ENTER(ctx
, strtoll
);
570 ENSURE_ASAN_INITED();
571 if (!flags()->replace_str
) {
572 return REAL(strtoll
)(nptr
, endptr
, base
);
575 long long result
= REAL(strtoll
)(nptr
, &real_endptr
, base
);
576 StrtolFixAndCheck(ctx
, nptr
, endptr
, real_endptr
, base
);
580 INTERCEPTOR(long long, atoll
, const char *nptr
) {
582 ASAN_INTERCEPTOR_ENTER(ctx
, atoll
);
583 ENSURE_ASAN_INITED();
584 if (!flags()->replace_str
) {
585 return REAL(atoll
)(nptr
);
588 long long result
= REAL(strtoll
)(nptr
, &real_endptr
, 10);
589 FixRealStrtolEndptr(nptr
, &real_endptr
);
590 ASAN_READ_STRING(ctx
, nptr
, (real_endptr
- nptr
) + 1);
593 #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
595 #if ASAN_INTERCEPT___CXA_ATEXIT || ASAN_INTERCEPT_ATEXIT
596 static void AtCxaAtexit(void *unused
) {
598 StopInitOrderChecking();
602 #if ASAN_INTERCEPT___CXA_ATEXIT
603 INTERCEPTOR(int, __cxa_atexit
, void (*func
)(void *), void *arg
,
606 if (UNLIKELY(!asan_inited
)) return REAL(__cxa_atexit
)(func
, arg
, dso_handle
);
608 ENSURE_ASAN_INITED();
609 #if CAN_SANITIZE_LEAKS
610 __lsan::ScopedInterceptorDisabler disabler
;
612 int res
= REAL(__cxa_atexit
)(func
, arg
, dso_handle
);
613 REAL(__cxa_atexit
)(AtCxaAtexit
, nullptr, nullptr);
616 #endif // ASAN_INTERCEPT___CXA_ATEXIT
618 #if ASAN_INTERCEPT_ATEXIT
619 INTERCEPTOR(int, atexit
, void (*func
)()) {
620 ENSURE_ASAN_INITED();
621 #if CAN_SANITIZE_LEAKS
622 __lsan::ScopedInterceptorDisabler disabler
;
624 // Avoid calling real atexit as it is unreachable on at least on Linux.
625 int res
= REAL(__cxa_atexit
)((void (*)(void *a
))func
, nullptr, nullptr);
626 REAL(__cxa_atexit
)(AtCxaAtexit
, nullptr, nullptr);
631 #if ASAN_INTERCEPT_PTHREAD_ATFORK
633 extern int _pthread_atfork(void (*prepare
)(), void (*parent
)(),
637 INTERCEPTOR(int, pthread_atfork
, void (*prepare
)(), void (*parent
)(),
639 #if CAN_SANITIZE_LEAKS
640 __lsan::ScopedInterceptorDisabler disabler
;
642 // REAL(pthread_atfork) cannot be called due to symbol indirections at least
644 return _pthread_atfork(prepare
, parent
, child
);
648 #if ASAN_INTERCEPT_VFORK
649 DEFINE_REAL(int, vfork
)
650 DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(int, vfork
)
653 // ---------------------- InitializeAsanInterceptors ---------------- {{{1
655 void InitializeAsanInterceptors() {
656 static bool was_called_once
;
657 CHECK(!was_called_once
);
658 was_called_once
= true;
659 InitializeCommonInterceptors();
660 InitializeSignalInterceptors();
662 // Intercept str* functions.
663 ASAN_INTERCEPT_FUNC(strcat
);
664 ASAN_INTERCEPT_FUNC(strcpy
);
665 ASAN_INTERCEPT_FUNC(strncat
);
666 ASAN_INTERCEPT_FUNC(strncpy
);
667 ASAN_INTERCEPT_FUNC(strdup
);
668 #if ASAN_INTERCEPT___STRDUP
669 ASAN_INTERCEPT_FUNC(__strdup
);
671 #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
672 ASAN_INTERCEPT_FUNC(index
);
675 ASAN_INTERCEPT_FUNC(atoi
);
676 ASAN_INTERCEPT_FUNC(atol
);
677 ASAN_INTERCEPT_FUNC(strtol
);
678 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
679 ASAN_INTERCEPT_FUNC(atoll
);
680 ASAN_INTERCEPT_FUNC(strtoll
);
683 // Intecept jump-related functions.
684 ASAN_INTERCEPT_FUNC(longjmp
);
686 # if ASAN_INTERCEPT_SWAPCONTEXT
687 ASAN_INTERCEPT_FUNC(swapcontext
);
688 ASAN_INTERCEPT_FUNC(makecontext
);
690 # if ASAN_INTERCEPT__LONGJMP
691 ASAN_INTERCEPT_FUNC(_longjmp
);
693 #if ASAN_INTERCEPT___LONGJMP_CHK
694 ASAN_INTERCEPT_FUNC(__longjmp_chk
);
696 #if ASAN_INTERCEPT_SIGLONGJMP
697 ASAN_INTERCEPT_FUNC(siglongjmp
);
700 // Intercept exception handling functions.
701 #if ASAN_INTERCEPT___CXA_THROW
702 ASAN_INTERCEPT_FUNC(__cxa_throw
);
704 #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
705 ASAN_INTERCEPT_FUNC(__cxa_rethrow_primary_exception
);
707 // Indirectly intercept std::rethrow_exception.
708 #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
709 ASAN_INTERCEPT_FUNC(_Unwind_RaiseException
);
711 // Indirectly intercept std::rethrow_exception.
712 #if ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION
713 ASAN_INTERCEPT_FUNC(_Unwind_SjLj_RaiseException
);
716 // Intercept threading-related functions
717 #if ASAN_INTERCEPT_PTHREAD_CREATE
718 // TODO: this should probably have an unversioned fallback for newer arches?
719 #if defined(ASAN_PTHREAD_CREATE_VERSION)
720 ASAN_INTERCEPT_FUNC_VER(pthread_create
, ASAN_PTHREAD_CREATE_VERSION
);
722 ASAN_INTERCEPT_FUNC(pthread_create
);
724 ASAN_INTERCEPT_FUNC(pthread_join
);
727 // Intercept atexit function.
728 #if ASAN_INTERCEPT___CXA_ATEXIT
729 ASAN_INTERCEPT_FUNC(__cxa_atexit
);
732 #if ASAN_INTERCEPT_ATEXIT
733 ASAN_INTERCEPT_FUNC(atexit
);
736 #if ASAN_INTERCEPT_PTHREAD_ATFORK
737 ASAN_INTERCEPT_FUNC(pthread_atfork
);
740 #if ASAN_INTERCEPT_VFORK
741 ASAN_INTERCEPT_FUNC(vfork
);
744 InitializePlatformInterceptors();
746 VReport(1, "AddressSanitizer: libc interceptors initialized\n");
749 } // namespace __asan
751 #endif // !SANITIZER_FUCHSIA