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 static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) {
41 if (size == 0) return true;
43 return !AddressIsPoisoned(beg) &&
44 !AddressIsPoisoned(beg + size - 1) &&
45 !AddressIsPoisoned(beg + size / 2);
49 struct AsanInterceptorContext {
50 const char *interceptor_name;
53 // We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
54 // and ASAN_WRITE_RANGE as macro instead of function so
55 // that no extra frames are created, and stack trace contains
56 // relevant information only.
57 // We check all shadow bytes.
58 #define ACCESS_MEMORY_RANGE(ctx, offset, size, isWrite) do { \
59 uptr __offset = (uptr)(offset); \
60 uptr __size = (uptr)(size); \
62 if (__offset > __offset + __size) { \
63 GET_STACK_TRACE_FATAL_HERE; \
64 ReportStringFunctionSizeOverflow(__offset, __size, &stack); \
66 if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \
67 (__bad = __asan_region_is_poisoned(__offset, __size))) { \
68 AsanInterceptorContext *_ctx = (AsanInterceptorContext *)ctx; \
69 bool suppressed = false; \
71 suppressed = IsInterceptorSuppressed(_ctx->interceptor_name); \
72 if (!suppressed && HaveStackTraceBasedSuppressions()) { \
73 GET_STACK_TRACE_FATAL_HERE; \
74 suppressed = IsStackTraceSuppressed(&stack); \
78 GET_CURRENT_PC_BP_SP; \
79 ReportGenericError(pc, bp, sp, __bad, isWrite, __size, 0, false);\
84 // memcpy is called during __asan_init() from the internals of printf(...).
85 // We do not treat memcpy with to==from as a bug.
86 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
87 #define ASAN_MEMCPY_IMPL(ctx, to, from, size) \
89 if (UNLIKELY(!asan_inited)) return internal_memcpy(to, from, size); \
90 if (asan_init_is_running) { \
91 return REAL(memcpy)(to, from, size); \
93 ENSURE_ASAN_INITED(); \
94 if (flags()->replace_intrin) { \
96 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); \
98 ASAN_READ_RANGE(ctx, from, size); \
99 ASAN_WRITE_RANGE(ctx, to, size); \
101 return REAL(memcpy)(to, from, size); \
104 // memset is called inside Printf.
105 #define ASAN_MEMSET_IMPL(ctx, block, c, size) \
107 if (UNLIKELY(!asan_inited)) return internal_memset(block, c, size); \
108 if (asan_init_is_running) { \
109 return REAL(memset)(block, c, size); \
111 ENSURE_ASAN_INITED(); \
112 if (flags()->replace_intrin) { \
113 ASAN_WRITE_RANGE(ctx, block, size); \
115 return REAL(memset)(block, c, size); \
118 #define ASAN_MEMMOVE_IMPL(ctx, to, from, size) \
120 if (UNLIKELY(!asan_inited)) return internal_memmove(to, from, size); \
121 ENSURE_ASAN_INITED(); \
122 if (flags()->replace_intrin) { \
123 ASAN_READ_RANGE(ctx, from, size); \
124 ASAN_WRITE_RANGE(ctx, to, size); \
126 return internal_memmove(to, from, size); \
129 #define ASAN_READ_RANGE(ctx, offset, size) \
130 ACCESS_MEMORY_RANGE(ctx, offset, size, false)
131 #define ASAN_WRITE_RANGE(ctx, offset, size) \
132 ACCESS_MEMORY_RANGE(ctx, offset, size, true)
134 #define ASAN_READ_STRING_OF_LEN(ctx, s, len, n) \
135 ASAN_READ_RANGE((ctx), (s), \
136 common_flags()->strict_string_checks ? (len) + 1 : (n))
138 #define ASAN_READ_STRING(ctx, s, n) \
139 ASAN_READ_STRING_OF_LEN((ctx), (s), REAL(strlen)(s), (n))
141 // Behavior of functions like "memcpy" or "strcpy" is undefined
142 // if memory intervals overlap. We report error in this case.
143 // Macro is used to avoid creation of new frames.
144 static inline bool RangesOverlap(const char *offset1, uptr length1,
145 const char *offset2, uptr length2) {
146 return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
148 #define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
149 const char *offset1 = (const char*)_offset1; \
150 const char *offset2 = (const char*)_offset2; \
151 if (RangesOverlap(offset1, length1, offset2, length2)) { \
152 GET_STACK_TRACE_FATAL_HERE; \
153 ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
154 offset2, length2, &stack); \
158 static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
159 #if SANITIZER_INTERCEPT_STRNLEN
161 return REAL(strnlen)(s, maxlen);
164 return internal_strnlen(s, maxlen);
167 void SetThreadName(const char *name) {
168 AsanThread *t = GetCurrentThread();
170 asanThreadRegistry().SetThreadName(t->tid(), name);
174 // FIXME: ask frontend whether we need to return failure.
178 } // namespace __asan
180 // ---------------------- Wrappers ---------------- {{{1
181 using namespace __asan; // NOLINT
183 DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr)
184 DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
186 #define ASAN_INTERCEPTOR_ENTER(ctx, func) \
187 AsanInterceptorContext _ctx = {#func}; \
188 ctx = (void *)&_ctx; \
191 #define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name)
192 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
193 ASAN_INTERCEPT_FUNC_VER(name, ver)
194 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
195 ASAN_WRITE_RANGE(ctx, ptr, size)
196 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
197 ASAN_READ_RANGE(ctx, ptr, size)
198 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
199 ASAN_INTERCEPTOR_ENTER(ctx, func); \
201 if (asan_init_is_running) \
202 return REAL(func)(__VA_ARGS__); \
203 if (SANITIZER_MAC && UNLIKELY(!asan_inited)) \
204 return REAL(func)(__VA_ARGS__); \
205 ENSURE_ASAN_INITED(); \
207 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
210 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
213 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
216 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
219 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
220 // Should be asanThreadRegistry().SetThreadNameByUserId(thread, name)
221 // But asan does not remember UserId's for threads (pthread_t);
222 // and remembers all ever existed threads, so the linear search by UserId
224 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
227 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
228 // Strict init-order checking is dlopen-hostile:
229 // https://github.com/google/sanitizers/issues/178
230 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
231 if (flags()->strict_init_order) { \
232 StopInitOrderChecking(); \
234 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
235 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \
236 CoverageUpdateMapping()
237 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() CoverageUpdateMapping()
238 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited)
239 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
240 if (AsanThread *t = GetCurrentThread()) { \
241 *begin = t->tls_begin(); \
242 *end = t->tls_end(); \
247 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
249 ASAN_INTERCEPTOR_ENTER(ctx, memmove); \
250 ASAN_MEMMOVE_IMPL(ctx, to, from, size); \
253 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
255 ASAN_INTERCEPTOR_ENTER(ctx, memcpy); \
256 ASAN_MEMCPY_IMPL(ctx, to, from, size); \
259 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
261 ASAN_INTERCEPTOR_ENTER(ctx, memset); \
262 ASAN_MEMSET_IMPL(ctx, block, c, size); \
265 #include "sanitizer_common/sanitizer_common_interceptors.inc"
267 // Syscall interceptors don't have contexts, we don't support suppressions
269 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(nullptr, p, s)
270 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(nullptr, p, s)
271 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
276 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
281 #include "sanitizer_common/sanitizer_common_syscalls.inc"
283 struct ThreadStartParam {
285 atomic_uintptr_t is_registered;
288 #if ASAN_INTERCEPT_PTHREAD_CREATE
289 static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
290 ThreadStartParam *param = reinterpret_cast<ThreadStartParam *>(arg);
291 AsanThread *t = nullptr;
292 while ((t = reinterpret_cast<AsanThread *>(
293 atomic_load(¶m->t, memory_order_acquire))) == nullptr)
294 internal_sched_yield();
296 return t->ThreadStart(GetTid(), ¶m->is_registered);
299 INTERCEPTOR(int, pthread_create, void *thread,
300 void *attr, void *(*start_routine)(void*), void *arg) {
301 EnsureMainThreadIDIsCorrect();
302 // Strict init-order checking is thread-hostile.
303 if (flags()->strict_init_order)
304 StopInitOrderChecking();
305 GET_STACK_TRACE_THREAD;
308 REAL(pthread_attr_getdetachstate)(attr, &detached);
309 ThreadStartParam param;
310 atomic_store(¶m.t, 0, memory_order_relaxed);
311 atomic_store(¶m.is_registered, 0, memory_order_relaxed);
314 // Ignore all allocations made by pthread_create: thread stack/TLS may be
315 // stored by pthread for future reuse even after thread destruction, and
316 // the linked list it's stored in doesn't even hold valid pointers to the
317 // objects, the latter are calculated by obscure pointer arithmetic.
318 #if CAN_SANITIZE_LEAKS
319 __lsan::ScopedInterceptorDisabler disabler;
321 result = REAL(pthread_create)(thread, attr, asan_thread_start, ¶m);
324 u32 current_tid = GetCurrentTidOrInvalid();
326 AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
327 atomic_store(¶m.t, reinterpret_cast<uptr>(t), memory_order_release);
328 // Wait until the AsanThread object is initialized and the ThreadRegistry
329 // entry is in "started" state. One reason for this is that after this
330 // interceptor exits, the child thread's stack may be the only thing holding
331 // the |arg| pointer. This may cause LSan to report a leak if leak checking
332 // happens at a point when the interceptor has already exited, but the stack
333 // range for the child thread is not yet known.
334 while (atomic_load(¶m.is_registered, memory_order_acquire) == 0)
335 internal_sched_yield();
340 INTERCEPTOR(int, pthread_join, void *t, void **arg) {
341 return real_pthread_join(t, arg);
344 DEFINE_REAL_PTHREAD_FUNCTIONS
345 #endif // ASAN_INTERCEPT_PTHREAD_CREATE
347 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
349 #if SANITIZER_ANDROID
350 INTERCEPTOR(void*, bsd_signal, int signum, void *handler) {
351 if (!IsHandledDeadlySignal(signum) ||
352 common_flags()->allow_user_segv_handler) {
353 return REAL(bsd_signal)(signum, handler);
359 INTERCEPTOR(void*, signal, int signum, void *handler) {
360 if (!IsHandledDeadlySignal(signum) ||
361 common_flags()->allow_user_segv_handler) {
362 return REAL(signal)(signum, handler);
367 INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
368 struct sigaction *oldact) {
369 if (!IsHandledDeadlySignal(signum) ||
370 common_flags()->allow_user_segv_handler) {
371 return REAL(sigaction)(signum, act, oldact);
376 namespace __sanitizer {
377 int real_sigaction(int signum, const void *act, void *oldact) {
378 return REAL(sigaction)(signum, (const struct sigaction *)act,
379 (struct sigaction *)oldact);
381 } // namespace __sanitizer
383 #elif SANITIZER_POSIX
384 // We need to have defined REAL(sigaction) on posix systems.
385 DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
386 struct sigaction *oldact)
387 #endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
389 #if ASAN_INTERCEPT_SWAPCONTEXT
390 static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
391 // Align to page size.
392 uptr PageSize = GetPageSizeCached();
393 uptr bottom = stack & ~(PageSize - 1);
394 ssize += stack - bottom;
395 ssize = RoundUpTo(ssize, PageSize);
396 static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb
397 if (AddrIsInMem(bottom) && ssize && ssize <= kMaxSaneContextStackSize) {
398 PoisonShadow(bottom, ssize, 0);
402 INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
403 struct ucontext_t *ucp) {
404 static bool reported_warning = false;
405 if (!reported_warning) {
406 Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
407 "functions and may produce false positives in some cases!\n");
408 reported_warning = true;
410 // Clear shadow memory for new context (it may share stack
411 // with current context).
413 ReadContextStack(ucp, &stack, &ssize);
414 ClearShadowMemoryForContextStack(stack, ssize);
415 int res = REAL(swapcontext)(oucp, ucp);
416 // swapcontext technically does not return, but program may swap context to
417 // "oucp" later, that would look as if swapcontext() returned 0.
418 // We need to clear shadow for ucp once again, as it may be in arbitrary
420 ClearShadowMemoryForContextStack(stack, ssize);
423 #endif // ASAN_INTERCEPT_SWAPCONTEXT
425 INTERCEPTOR(void, longjmp, void *env, int val) {
426 __asan_handle_no_return();
427 REAL(longjmp)(env, val);
430 #if ASAN_INTERCEPT__LONGJMP
431 INTERCEPTOR(void, _longjmp, void *env, int val) {
432 __asan_handle_no_return();
433 REAL(_longjmp)(env, val);
437 #if ASAN_INTERCEPT_SIGLONGJMP
438 INTERCEPTOR(void, siglongjmp, void *env, int val) {
439 __asan_handle_no_return();
440 REAL(siglongjmp)(env, val);
444 #if ASAN_INTERCEPT___CXA_THROW
445 INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
446 CHECK(REAL(__cxa_throw));
447 __asan_handle_no_return();
448 REAL(__cxa_throw)(a, b, c);
452 void *__asan_memcpy(void *to, const void *from, uptr size) {
453 ASAN_MEMCPY_IMPL(nullptr, to, from, size);
456 void *__asan_memset(void *block, int c, uptr size) {
457 ASAN_MEMSET_IMPL(nullptr, block, c, size);
460 void *__asan_memmove(void *to, const void *from, uptr size) {
461 ASAN_MEMMOVE_IMPL(nullptr, to, from, size);
464 #if ASAN_INTERCEPT_INDEX
465 # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
466 INTERCEPTOR(char*, index, const char *string, int c)
467 ALIAS(WRAPPER_NAME(strchr));
470 DECLARE_REAL(char*, index, const char *string, int c)
471 OVERRIDE_FUNCTION(index, strchr);
473 DEFINE_REAL(char*, index, const char *string, int c)
476 #endif // ASAN_INTERCEPT_INDEX
478 // For both strcat() and strncat() we need to check the validity of |to|
479 // argument irrespective of the |from| length.
480 INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT
482 ASAN_INTERCEPTOR_ENTER(ctx, strcat); // NOLINT
483 ENSURE_ASAN_INITED();
484 if (flags()->replace_str) {
485 uptr from_length = REAL(strlen)(from);
486 ASAN_READ_RANGE(ctx, from, from_length + 1);
487 uptr to_length = REAL(strlen)(to);
488 ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
489 ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
490 // If the copying actually happens, the |from| string should not overlap
491 // with the resulting string starting at |to|, which has a length of
492 // to_length + from_length + 1.
493 if (from_length > 0) {
494 CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
495 from, from_length + 1);
498 return REAL(strcat)(to, from); // NOLINT
501 INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
503 ASAN_INTERCEPTOR_ENTER(ctx, strncat);
504 ENSURE_ASAN_INITED();
505 if (flags()->replace_str) {
506 uptr from_length = MaybeRealStrnlen(from, size);
507 uptr copy_length = Min(size, from_length + 1);
508 ASAN_READ_RANGE(ctx, from, copy_length);
509 uptr to_length = REAL(strlen)(to);
510 ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
511 ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
512 if (from_length > 0) {
513 CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
517 return REAL(strncat)(to, from, size);
520 INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT
522 ASAN_INTERCEPTOR_ENTER(ctx, strcpy); // NOLINT
524 if (UNLIKELY(!asan_inited)) return REAL(strcpy)(to, from); // NOLINT
526 // strcpy is called from malloc_default_purgeable_zone()
527 // in __asan::ReplaceSystemAlloc() on Mac.
528 if (asan_init_is_running) {
529 return REAL(strcpy)(to, from); // NOLINT
531 ENSURE_ASAN_INITED();
532 if (flags()->replace_str) {
533 uptr from_size = REAL(strlen)(from) + 1;
534 CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
535 ASAN_READ_RANGE(ctx, from, from_size);
536 ASAN_WRITE_RANGE(ctx, to, from_size);
538 return REAL(strcpy)(to, from); // NOLINT
541 INTERCEPTOR(char*, strdup, const char *s) {
543 ASAN_INTERCEPTOR_ENTER(ctx, strdup);
544 if (UNLIKELY(!asan_inited)) return internal_strdup(s);
545 ENSURE_ASAN_INITED();
546 uptr length = REAL(strlen)(s);
547 if (flags()->replace_str) {
548 ASAN_READ_RANGE(ctx, s, length + 1);
550 GET_STACK_TRACE_MALLOC;
551 void *new_mem = asan_malloc(length + 1, &stack);
552 REAL(memcpy)(new_mem, s, length + 1);
553 return reinterpret_cast<char*>(new_mem);
556 #if ASAN_INTERCEPT___STRDUP
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);
571 #endif // ASAN_INTERCEPT___STRDUP
573 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
575 ASAN_INTERCEPTOR_ENTER(ctx, wcslen);
576 SIZE_T length = internal_wcslen(s);
577 if (!asan_init_is_running) {
578 ENSURE_ASAN_INITED();
579 ASAN_READ_RANGE(ctx, s, (length + 1) * sizeof(wchar_t));
584 INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
586 ASAN_INTERCEPTOR_ENTER(ctx, strncpy);
587 ENSURE_ASAN_INITED();
588 if (flags()->replace_str) {
589 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
590 CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
591 ASAN_READ_RANGE(ctx, from, from_size);
592 ASAN_WRITE_RANGE(ctx, to, size);
594 return REAL(strncpy)(to, from, size);
597 INTERCEPTOR(long, strtol, const char *nptr, // NOLINT
598 char **endptr, int base) {
600 ASAN_INTERCEPTOR_ENTER(ctx, strtol);
601 ENSURE_ASAN_INITED();
602 if (!flags()->replace_str) {
603 return REAL(strtol)(nptr, endptr, base);
606 long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT
607 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
611 INTERCEPTOR(int, atoi, const char *nptr) {
613 ASAN_INTERCEPTOR_ENTER(ctx, atoi);
615 if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
617 ENSURE_ASAN_INITED();
618 if (!flags()->replace_str) {
619 return REAL(atoi)(nptr);
622 // "man atoi" tells that behavior of atoi(nptr) is the same as
623 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
624 // parsed integer can't be stored in *long* type (even if it's
625 // different from int). So, we just imitate this behavior.
626 int result = REAL(strtol)(nptr, &real_endptr, 10);
627 FixRealStrtolEndptr(nptr, &real_endptr);
628 ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
632 INTERCEPTOR(long, atol, const char *nptr) { // NOLINT
634 ASAN_INTERCEPTOR_ENTER(ctx, atol);
636 if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
638 ENSURE_ASAN_INITED();
639 if (!flags()->replace_str) {
640 return REAL(atol)(nptr);
643 long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT
644 FixRealStrtolEndptr(nptr, &real_endptr);
645 ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
649 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
650 INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT
651 char **endptr, int base) {
653 ASAN_INTERCEPTOR_ENTER(ctx, strtoll);
654 ENSURE_ASAN_INITED();
655 if (!flags()->replace_str) {
656 return REAL(strtoll)(nptr, endptr, base);
659 long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT
660 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
664 INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT
666 ASAN_INTERCEPTOR_ENTER(ctx, atoll);
667 ENSURE_ASAN_INITED();
668 if (!flags()->replace_str) {
669 return REAL(atoll)(nptr);
672 long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT
673 FixRealStrtolEndptr(nptr, &real_endptr);
674 ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
677 #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
679 #if ASAN_INTERCEPT___CXA_ATEXIT
680 static void AtCxaAtexit(void *unused) {
682 StopInitOrderChecking();
685 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
688 if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
690 ENSURE_ASAN_INITED();
691 int res = REAL(__cxa_atexit)(func, arg, dso_handle);
692 REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
695 #endif // ASAN_INTERCEPT___CXA_ATEXIT
697 #if ASAN_INTERCEPT_FORK
698 INTERCEPTOR(int, fork, void) {
699 ENSURE_ASAN_INITED();
700 if (common_flags()->coverage) CovBeforeFork();
701 int pid = REAL(fork)();
702 if (common_flags()->coverage) CovAfterFork(pid);
705 #endif // ASAN_INTERCEPT_FORK
707 // ---------------------- InitializeAsanInterceptors ---------------- {{{1
709 void InitializeAsanInterceptors() {
710 static bool was_called_once;
711 CHECK(!was_called_once);
712 was_called_once = true;
713 InitializeCommonInterceptors();
715 // Intercept str* functions.
716 ASAN_INTERCEPT_FUNC(strcat); // NOLINT
717 ASAN_INTERCEPT_FUNC(strcpy); // NOLINT
718 ASAN_INTERCEPT_FUNC(wcslen);
719 ASAN_INTERCEPT_FUNC(strncat);
720 ASAN_INTERCEPT_FUNC(strncpy);
721 ASAN_INTERCEPT_FUNC(strdup);
722 #if ASAN_INTERCEPT___STRDUP
723 ASAN_INTERCEPT_FUNC(__strdup);
725 #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
726 ASAN_INTERCEPT_FUNC(index);
729 ASAN_INTERCEPT_FUNC(atoi);
730 ASAN_INTERCEPT_FUNC(atol);
731 ASAN_INTERCEPT_FUNC(strtol);
732 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
733 ASAN_INTERCEPT_FUNC(atoll);
734 ASAN_INTERCEPT_FUNC(strtoll);
737 // Intecept signal- and jump-related functions.
738 ASAN_INTERCEPT_FUNC(longjmp);
739 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
740 ASAN_INTERCEPT_FUNC(sigaction);
741 #if SANITIZER_ANDROID
742 ASAN_INTERCEPT_FUNC(bsd_signal);
744 ASAN_INTERCEPT_FUNC(signal);
746 #if ASAN_INTERCEPT_SWAPCONTEXT
747 ASAN_INTERCEPT_FUNC(swapcontext);
749 #if ASAN_INTERCEPT__LONGJMP
750 ASAN_INTERCEPT_FUNC(_longjmp);
752 #if ASAN_INTERCEPT_SIGLONGJMP
753 ASAN_INTERCEPT_FUNC(siglongjmp);
756 // Intercept exception handling functions.
757 #if ASAN_INTERCEPT___CXA_THROW
758 ASAN_INTERCEPT_FUNC(__cxa_throw);
761 // Intercept threading-related functions
762 #if ASAN_INTERCEPT_PTHREAD_CREATE
763 #if defined(ASAN_PTHREAD_CREATE_VERSION)
764 ASAN_INTERCEPT_FUNC_VER(pthread_create, ASAN_PTHREAD_CREATE_VERSION);
766 ASAN_INTERCEPT_FUNC(pthread_create);
768 ASAN_INTERCEPT_FUNC(pthread_join);
771 // Intercept atexit function.
772 #if ASAN_INTERCEPT___CXA_ATEXIT
773 ASAN_INTERCEPT_FUNC(__cxa_atexit);
776 #if ASAN_INTERCEPT_FORK
777 ASAN_INTERCEPT_FUNC(fork);
780 InitializePlatformInterceptors();
782 VReport(1, "AddressSanitizer: libc interceptors initialized\n");
785 } // namespace __asan