d: Fix testcase failure of gdc.dg/Wbuiltin_declaration_mismatch2.d.
[official-gcc.git] / libsanitizer / asan / asan_interceptors.cpp
blob0f1600552481b41b463a94d3d870395f45d277e2
1 //===-- asan_interceptors.cpp ---------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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
31 # if SANITIZER_POSIX
32 # include "sanitizer_common/sanitizer_posix.h"
33 # endif
35 # if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION || \
36 ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION
37 # include <unwind.h>
38 # endif
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"
44 # endif
46 namespace __asan {
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
57 if (REAL(strnlen)) {
58 return REAL(strnlen)(s, maxlen);
60 #endif
61 return internal_strnlen(s, maxlen);
64 void SetThreadName(const char *name) {
65 AsanThread *t = GetCurrentThread();
66 if (t)
67 asanThreadRegistry().SetThreadName(t->tid(), name);
70 int OnExit() {
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.
76 return 0;
79 } // namespace __asan
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; \
90 (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); \
103 do { \
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(); \
109 } while (false)
110 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
111 do { \
112 } while (false)
113 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
114 do { \
115 } while (false)
116 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
117 do { \
118 } while (false)
119 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
120 do { \
121 } while (false)
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
126 // can be slow.
127 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
128 do { \
129 } while (false)
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) \
134 ({ \
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(); \
148 } else { \
149 *begin = *end = 0; \
152 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
153 do { \
154 ASAN_INTERCEPTOR_ENTER(ctx, memmove); \
155 ASAN_MEMMOVE_IMPL(ctx, to, from, size); \
156 } while (false)
158 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
159 do { \
160 ASAN_INTERCEPTOR_ENTER(ctx, memcpy); \
161 ASAN_MEMCPY_IMPL(ctx, to, from, size); \
162 } while (false)
164 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
165 do { \
166 ASAN_INTERCEPTOR_ENTER(ctx, memset); \
167 ASAN_MEMSET_IMPL(ctx, block, c, size); \
168 } while (false)
170 #if CAN_SANITIZE_LEAKS
171 #define COMMON_INTERCEPTOR_STRERROR() \
172 __lsan::ScopedInterceptorDisabler disabler
173 #endif
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
179 // for them.
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) \
183 do { \
184 (void)(p); \
185 (void)(s); \
186 } while (false)
187 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
188 do { \
189 (void)(p); \
190 (void)(s); \
191 } while (false)
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;
198 SetCurrentThread(t);
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;
209 int detached = 0;
210 if (attr)
211 REAL(pthread_attr_getdetachstate)(attr, &detached);
213 u32 current_tid = GetCurrentTidOrInvalid();
214 AsanThread *t =
215 AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
217 int result;
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;
225 #endif
226 result = REAL(pthread_create)(thread, attr, asan_thread_start, t);
228 if (result != 0) {
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.
232 t->Destroy();
234 return result;
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().
248 if (!ssize)
249 return;
250 // Align to page size.
251 uptr PageSize = GetPageSizeCached();
252 uptr bottom = RoundDownTo(stack, PageSize);
253 if (!AddrIsInMem(bottom))
254 return;
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,
261 ...) {
262 va_list ap;
263 uptr args[64];
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));
268 va_start(ap, argc);
269 for (int i = 0; i < argc; ++i) args[i] = va_arg(ap, uptr);
270 va_end(ap);
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)
281 REAL(makecontext)
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).
302 uptr stack, ssize;
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);
311 # else
312 int res = REAL(swapcontext)(oucp, ucp);
313 # endif
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
317 // state.
318 ClearShadowMemoryForContextStack(stack, ssize);
319 return res;
321 #endif // ASAN_INTERCEPT_SWAPCONTEXT
323 #if SANITIZER_NETBSD
324 #define longjmp __longjmp14
325 #define siglongjmp __siglongjmp14
326 #endif
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);
338 #endif
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);
345 #endif
347 #if ASAN_INTERCEPT_SIGLONGJMP
348 INTERCEPTOR(void, siglongjmp, void *env, int val) {
349 __asan_handle_no_return();
350 REAL(siglongjmp)(env, val);
352 #endif
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);
360 #endif
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);
368 #endif
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);
377 #endif
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);
386 #endif
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));
392 # else
393 # if SANITIZER_APPLE
394 DECLARE_REAL(char*, index, const char *string, int c)
395 OVERRIDE_FUNCTION(index, strchr);
396 # else
397 DEFINE_REAL(char*, index, const char *string, int c)
398 # endif
399 # endif
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) {
405 void *ctx;
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,
419 from_length + 1);
422 return REAL(strcat)(to, from);
425 INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
426 void *ctx;
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,
438 from, copy_length);
441 return REAL(strncat)(to, from, size);
444 INTERCEPTOR(char *, strcpy, char *to, const char *from) {
445 void *ctx;
446 ASAN_INTERCEPTOR_ENTER(ctx, strcpy);
447 #if SANITIZER_APPLE
448 if (UNLIKELY(!asan_inited))
449 return REAL(strcpy)(to, from);
450 #endif
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) {
467 void *ctx;
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);
477 if (new_mem) {
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) {
485 void *ctx;
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);
495 if (new_mem) {
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) {
503 void *ctx;
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) {
516 void *ctx;
517 ASAN_INTERCEPTOR_ENTER(ctx, strtol);
518 ENSURE_ASAN_INITED();
519 if (!flags()->replace_str) {
520 return REAL(strtol)(nptr, endptr, base);
522 char *real_endptr;
523 long result = REAL(strtol)(nptr, &real_endptr, base);
524 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
525 return result;
528 INTERCEPTOR(int, atoi, const char *nptr) {
529 void *ctx;
530 ASAN_INTERCEPTOR_ENTER(ctx, atoi);
531 #if SANITIZER_APPLE
532 if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
533 #endif
534 ENSURE_ASAN_INITED();
535 if (!flags()->replace_str) {
536 return REAL(atoi)(nptr);
538 char *real_endptr;
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);
546 return result;
549 INTERCEPTOR(long, atol, const char *nptr) {
550 void *ctx;
551 ASAN_INTERCEPTOR_ENTER(ctx, atol);
552 #if SANITIZER_APPLE
553 if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
554 #endif
555 ENSURE_ASAN_INITED();
556 if (!flags()->replace_str) {
557 return REAL(atol)(nptr);
559 char *real_endptr;
560 long result = REAL(strtol)(nptr, &real_endptr, 10);
561 FixRealStrtolEndptr(nptr, &real_endptr);
562 ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
563 return result;
566 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
567 INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr, int base) {
568 void *ctx;
569 ASAN_INTERCEPTOR_ENTER(ctx, strtoll);
570 ENSURE_ASAN_INITED();
571 if (!flags()->replace_str) {
572 return REAL(strtoll)(nptr, endptr, base);
574 char *real_endptr;
575 long long result = REAL(strtoll)(nptr, &real_endptr, base);
576 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
577 return result;
580 INTERCEPTOR(long long, atoll, const char *nptr) {
581 void *ctx;
582 ASAN_INTERCEPTOR_ENTER(ctx, atoll);
583 ENSURE_ASAN_INITED();
584 if (!flags()->replace_str) {
585 return REAL(atoll)(nptr);
587 char *real_endptr;
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);
591 return result;
593 #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
595 #if ASAN_INTERCEPT___CXA_ATEXIT || ASAN_INTERCEPT_ATEXIT
596 static void AtCxaAtexit(void *unused) {
597 (void)unused;
598 StopInitOrderChecking();
600 #endif
602 #if ASAN_INTERCEPT___CXA_ATEXIT
603 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
604 void *dso_handle) {
605 #if SANITIZER_APPLE
606 if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
607 #endif
608 ENSURE_ASAN_INITED();
609 #if CAN_SANITIZE_LEAKS
610 __lsan::ScopedInterceptorDisabler disabler;
611 #endif
612 int res = REAL(__cxa_atexit)(func, arg, dso_handle);
613 REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
614 return res;
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;
623 #endif
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);
627 return res;
629 #endif
631 #if ASAN_INTERCEPT_PTHREAD_ATFORK
632 extern "C" {
633 extern int _pthread_atfork(void (*prepare)(), void (*parent)(),
634 void (*child)());
637 INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(),
638 void (*child)()) {
639 #if CAN_SANITIZE_LEAKS
640 __lsan::ScopedInterceptorDisabler disabler;
641 #endif
642 // REAL(pthread_atfork) cannot be called due to symbol indirections at least
643 // on NetBSD
644 return _pthread_atfork(prepare, parent, child);
646 #endif
648 #if ASAN_INTERCEPT_VFORK
649 DEFINE_REAL(int, vfork)
650 DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(int, vfork)
651 #endif
653 // ---------------------- InitializeAsanInterceptors ---------------- {{{1
654 namespace __asan {
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);
670 #endif
671 #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
672 ASAN_INTERCEPT_FUNC(index);
673 #endif
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);
681 #endif
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);
689 # endif
690 # if ASAN_INTERCEPT__LONGJMP
691 ASAN_INTERCEPT_FUNC(_longjmp);
692 #endif
693 #if ASAN_INTERCEPT___LONGJMP_CHK
694 ASAN_INTERCEPT_FUNC(__longjmp_chk);
695 #endif
696 #if ASAN_INTERCEPT_SIGLONGJMP
697 ASAN_INTERCEPT_FUNC(siglongjmp);
698 #endif
700 // Intercept exception handling functions.
701 #if ASAN_INTERCEPT___CXA_THROW
702 ASAN_INTERCEPT_FUNC(__cxa_throw);
703 #endif
704 #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
705 ASAN_INTERCEPT_FUNC(__cxa_rethrow_primary_exception);
706 #endif
707 // Indirectly intercept std::rethrow_exception.
708 #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
709 ASAN_INTERCEPT_FUNC(_Unwind_RaiseException);
710 #endif
711 // Indirectly intercept std::rethrow_exception.
712 #if ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION
713 ASAN_INTERCEPT_FUNC(_Unwind_SjLj_RaiseException);
714 #endif
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);
721 #else
722 ASAN_INTERCEPT_FUNC(pthread_create);
723 #endif
724 ASAN_INTERCEPT_FUNC(pthread_join);
725 #endif
727 // Intercept atexit function.
728 #if ASAN_INTERCEPT___CXA_ATEXIT
729 ASAN_INTERCEPT_FUNC(__cxa_atexit);
730 #endif
732 #if ASAN_INTERCEPT_ATEXIT
733 ASAN_INTERCEPT_FUNC(atexit);
734 #endif
736 #if ASAN_INTERCEPT_PTHREAD_ATFORK
737 ASAN_INTERCEPT_FUNC(pthread_atfork);
738 #endif
740 #if ASAN_INTERCEPT_VFORK
741 ASAN_INTERCEPT_FUNC(vfork);
742 #endif
744 InitializePlatformInterceptors();
746 VReport(1, "AddressSanitizer: libc interceptors initialized\n");
749 } // namespace __asan
751 #endif // !SANITIZER_FUCHSIA