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.
10 //
11 // Intercept various libc functions.
12 //===----------------------------------------------------------------------===//
13 
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"
25 
26 // There is no general interception at all on Fuchsia and RTEMS.
27 // Only the functions in asan_interceptors_memintrinsics.cpp are
28 // really defined to replace libc functions.
29 #if !SANITIZER_FUCHSIA && !SANITIZER_RTEMS
30 
31 #if SANITIZER_POSIX
32 #include "sanitizer_common/sanitizer_posix.h"
33 #endif
34 
35 #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION || \
36     ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION
37 #include <unwind.h>
38 #endif
39 
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
45 
46 namespace __asan {
47 
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))
51 
52 #define ASAN_READ_STRING(ctx, s, n)                             \
53   ASAN_READ_STRING_OF_LEN((ctx), (s), REAL(strlen)(s), (n))
54 
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);
59   }
60 #endif
61   return internal_strnlen(s, maxlen);
62 }
63 
64 void SetThreadName(const char *name) {
65   AsanThread *t = GetCurrentThread();
66   if (t)
67     asanThreadRegistry().SetThreadName(t->tid(), name);
68 }
69 
70 int OnExit() {
71   if (CAN_SANITIZE_LEAKS && common_flags()->detect_leaks &&
72       __lsan::HasReportedLeaks()) {
73     return common_flags()->exitcode;
74   }
75   // FIXME: ask frontend whether we need to return failure.
76   return 0;
77 }
78 
79 } // namespace __asan
80 
81 // ---------------------- Wrappers ---------------- {{{1
82 using namespace __asan;
83 
84 DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr)
85 DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
86 
87 #define ASAN_INTERCEPTOR_ENTER(ctx, func)                                      \
88   AsanInterceptorContext _ctx = {#func};                                       \
89   ctx = (void *)&_ctx;                                                         \
90   (void) ctx;                                                                  \
91 
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_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
96   ASAN_WRITE_RANGE(ctx, ptr, size)
97 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
98   ASAN_READ_RANGE(ctx, ptr, size)
99 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...)                               \
100   ASAN_INTERCEPTOR_ENTER(ctx, func);                                           \
101   do {                                                                         \
102     if (asan_init_is_running)                                                  \
103       return REAL(func)(__VA_ARGS__);                                          \
104     if (SANITIZER_MAC && UNLIKELY(!asan_inited))                               \
105       return REAL(func)(__VA_ARGS__);                                          \
106     ENSURE_ASAN_INITED();                                                      \
107   } while (false)
108 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
109   do {                                            \
110   } while (false)
111 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
112   do {                                         \
113   } while (false)
114 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
115   do {                                         \
116   } while (false)
117 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
118   do {                                                      \
119   } while (false)
120 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
121 // Should be asanThreadRegistry().SetThreadNameByUserId(thread, name)
122 // But asan does not remember UserId's for threads (pthread_t);
123 // and remembers all ever existed threads, so the linear search by UserId
124 // can be slow.
125 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
126   do {                                                         \
127   } while (false)
128 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
129 // Strict init-order checking is dlopen-hostile:
130 // https://github.com/google/sanitizers/issues/178
131 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag)                           \
132   do {                                                                         \
133     if (flags()->strict_init_order)                                            \
134       StopInitOrderChecking();                                                 \
135     CheckNoDeepBind(filename, flag);                                           \
136   } while (false)
137 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
138 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle)
139 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED()
140 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited)
141 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end)                           \
142   if (AsanThread *t = GetCurrentThread()) {                                    \
143     *begin = t->tls_begin();                                                   \
144     *end = t->tls_end();                                                       \
145   } else {                                                                     \
146     *begin = *end = 0;                                                         \
147   }
148 
149 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
150   do {                                                       \
151     ASAN_INTERCEPTOR_ENTER(ctx, memmove);                    \
152     ASAN_MEMMOVE_IMPL(ctx, to, from, size);                  \
153   } while (false)
154 
155 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
156   do {                                                      \
157     ASAN_INTERCEPTOR_ENTER(ctx, memcpy);                    \
158     ASAN_MEMCPY_IMPL(ctx, to, from, size);                  \
159   } while (false)
160 
161 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
162   do {                                                      \
163     ASAN_INTERCEPTOR_ENTER(ctx, memset);                    \
164     ASAN_MEMSET_IMPL(ctx, block, c, size);                  \
165   } while (false)
166 
167 #if CAN_SANITIZE_LEAKS
168 #define COMMON_INTERCEPTOR_STRERROR()                       \
169   __lsan::ScopedInterceptorDisabler disabler
170 #endif
171 
172 #include "sanitizer_common/sanitizer_common_interceptors.inc"
173 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
174 
175 // Syscall interceptors don't have contexts, we don't support suppressions
176 // for them.
177 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(nullptr, p, s)
178 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(nullptr, p, s)
179 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
180   do {                                       \
181     (void)(p);                               \
182     (void)(s);                               \
183   } while (false)
184 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
185   do {                                        \
186     (void)(p);                                \
187     (void)(s);                                \
188   } while (false)
189 #include "sanitizer_common/sanitizer_common_syscalls.inc"
190 #include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
191 
192 #if ASAN_INTERCEPT_PTHREAD_CREATE
193 static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
194   AsanThread *t = (AsanThread *)arg;
195   SetCurrentThread(t);
196   return t->ThreadStart(GetTid());
197 }
198 
199 INTERCEPTOR(int, pthread_create, void *thread,
200     void *attr, void *(*start_routine)(void*), void *arg) {
201   EnsureMainThreadIDIsCorrect();
202   // Strict init-order checking is thread-hostile.
203   if (flags()->strict_init_order)
204     StopInitOrderChecking();
205   GET_STACK_TRACE_THREAD;
206   int detached = 0;
207   if (attr)
208     REAL(pthread_attr_getdetachstate)(attr, &detached);
209 
210   u32 current_tid = GetCurrentTidOrInvalid();
211   AsanThread *t =
212       AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
213 
214   int result;
215   {
216     // Ignore all allocations made by pthread_create: thread stack/TLS may be
217     // stored by pthread for future reuse even after thread destruction, and
218     // the linked list it's stored in doesn't even hold valid pointers to the
219     // objects, the latter are calculated by obscure pointer arithmetic.
220 #if CAN_SANITIZE_LEAKS
221     __lsan::ScopedInterceptorDisabler disabler;
222 #endif
223     result = REAL(pthread_create)(thread, attr, asan_thread_start, t);
224   }
225   if (result != 0) {
226     // If the thread didn't start delete the AsanThread to avoid leaking it.
227     // Note AsanThreadContexts never get destroyed so the AsanThreadContext
228     // that was just created for the AsanThread is wasted.
229     t->Destroy();
230   }
231   return result;
232 }
233 
234 INTERCEPTOR(int, pthread_join, void *t, void **arg) {
235   return real_pthread_join(t, arg);
236 }
237 
238 DEFINE_REAL_PTHREAD_FUNCTIONS
239 #endif  // ASAN_INTERCEPT_PTHREAD_CREATE
240 
241 #if ASAN_INTERCEPT_SWAPCONTEXT
242 static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
243   // Align to page size.
244   uptr PageSize = GetPageSizeCached();
245   uptr bottom = stack & ~(PageSize - 1);
246   ssize += stack - bottom;
247   ssize = RoundUpTo(ssize, PageSize);
248   static const uptr kMaxSaneContextStackSize = 1 << 22;  // 4 Mb
249   if (AddrIsInMem(bottom) && ssize && ssize <= kMaxSaneContextStackSize) {
250     PoisonShadow(bottom, ssize, 0);
251   }
252 }
253 
254 INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
255             struct ucontext_t *ucp) {
256   static bool reported_warning = false;
257   if (!reported_warning) {
258     Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
259            "functions and may produce false positives in some cases!\n");
260     reported_warning = true;
261   }
262   // Clear shadow memory for new context (it may share stack
263   // with current context).
264   uptr stack, ssize;
265   ReadContextStack(ucp, &stack, &ssize);
266   ClearShadowMemoryForContextStack(stack, ssize);
267 #if __has_attribute(__indirect_return__) && \
268     (defined(__x86_64__) || defined(__i386__))
269   int (*real_swapcontext)(struct ucontext_t *, struct ucontext_t *)
270     __attribute__((__indirect_return__))
271     = REAL(swapcontext);
272   int res = real_swapcontext(oucp, ucp);
273 #else
274   int res = REAL(swapcontext)(oucp, ucp);
275 #endif
276   // swapcontext technically does not return, but program may swap context to
277   // "oucp" later, that would look as if swapcontext() returned 0.
278   // We need to clear shadow for ucp once again, as it may be in arbitrary
279   // state.
280   ClearShadowMemoryForContextStack(stack, ssize);
281   return res;
282 }
283 #endif  // ASAN_INTERCEPT_SWAPCONTEXT
284 
285 #if SANITIZER_NETBSD
286 #define longjmp __longjmp14
287 #define siglongjmp __siglongjmp14
288 #endif
289 
290 INTERCEPTOR(void, longjmp, void *env, int val) {
291   __asan_handle_no_return();
292   REAL(longjmp)(env, val);
293 }
294 
295 #if ASAN_INTERCEPT__LONGJMP
296 INTERCEPTOR(void, _longjmp, void *env, int val) {
297   __asan_handle_no_return();
298   REAL(_longjmp)(env, val);
299 }
300 #endif
301 
302 #if ASAN_INTERCEPT___LONGJMP_CHK
303 INTERCEPTOR(void, __longjmp_chk, void *env, int val) {
304   __asan_handle_no_return();
305   REAL(__longjmp_chk)(env, val);
306 }
307 #endif
308 
309 #if ASAN_INTERCEPT_SIGLONGJMP
310 INTERCEPTOR(void, siglongjmp, void *env, int val) {
311   __asan_handle_no_return();
312   REAL(siglongjmp)(env, val);
313 }
314 #endif
315 
316 #if ASAN_INTERCEPT___CXA_THROW
317 INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
318   CHECK(REAL(__cxa_throw));
319   __asan_handle_no_return();
320   REAL(__cxa_throw)(a, b, c);
321 }
322 #endif
323 
324 #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
325 INTERCEPTOR(void, __cxa_rethrow_primary_exception, void *a) {
326   CHECK(REAL(__cxa_rethrow_primary_exception));
327   __asan_handle_no_return();
328   REAL(__cxa_rethrow_primary_exception)(a);
329 }
330 #endif
331 
332 #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
333 INTERCEPTOR(_Unwind_Reason_Code, _Unwind_RaiseException,
334             _Unwind_Exception *object) {
335   CHECK(REAL(_Unwind_RaiseException));
336   __asan_handle_no_return();
337   return REAL(_Unwind_RaiseException)(object);
338 }
339 #endif
340 
341 #if ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION
342 INTERCEPTOR(_Unwind_Reason_Code, _Unwind_SjLj_RaiseException,
343             _Unwind_Exception *object) {
344   CHECK(REAL(_Unwind_SjLj_RaiseException));
345   __asan_handle_no_return();
346   return REAL(_Unwind_SjLj_RaiseException)(object);
347 }
348 #endif
349 
350 #if ASAN_INTERCEPT_INDEX
351 # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
352 INTERCEPTOR(char*, index, const char *string, int c)
353   ALIAS(WRAPPER_NAME(strchr));
354 # else
355 #  if SANITIZER_MAC
356 DECLARE_REAL(char*, index, const char *string, int c)
357 OVERRIDE_FUNCTION(index, strchr);
358 #  else
359 DEFINE_REAL(char*, index, const char *string, int c)
360 #  endif
361 # endif
362 #endif  // ASAN_INTERCEPT_INDEX
363 
364 // For both strcat() and strncat() we need to check the validity of |to|
365 // argument irrespective of the |from| length.
366   INTERCEPTOR(char *, strcat, char *to, const char *from) {
367     void *ctx;
368     ASAN_INTERCEPTOR_ENTER(ctx, strcat);
369     ENSURE_ASAN_INITED();
370     if (flags()->replace_str) {
371       uptr from_length = REAL(strlen)(from);
372       ASAN_READ_RANGE(ctx, from, from_length + 1);
373       uptr to_length = REAL(strlen)(to);
374       ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
375       ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
376       // If the copying actually happens, the |from| string should not overlap
377       // with the resulting string starting at |to|, which has a length of
378       // to_length + from_length + 1.
379       if (from_length > 0) {
380         CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1, from,
381                              from_length + 1);
382       }
383     }
384     return REAL(strcat)(to, from);
385   }
386 
387 INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
388   void *ctx;
389   ASAN_INTERCEPTOR_ENTER(ctx, strncat);
390   ENSURE_ASAN_INITED();
391   if (flags()->replace_str) {
392     uptr from_length = MaybeRealStrnlen(from, size);
393     uptr copy_length = Min(size, from_length + 1);
394     ASAN_READ_RANGE(ctx, from, copy_length);
395     uptr to_length = REAL(strlen)(to);
396     ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
397     ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
398     if (from_length > 0) {
399       CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
400                            from, copy_length);
401     }
402   }
403   return REAL(strncat)(to, from, size);
404 }
405 
406 INTERCEPTOR(char *, strcpy, char *to, const char *from) {
407   void *ctx;
408   ASAN_INTERCEPTOR_ENTER(ctx, strcpy);
409 #if SANITIZER_MAC
410   if (UNLIKELY(!asan_inited))
411     return REAL(strcpy)(to, from);
412 #endif
413   // strcpy is called from malloc_default_purgeable_zone()
414   // in __asan::ReplaceSystemAlloc() on Mac.
415   if (asan_init_is_running) {
416     return REAL(strcpy)(to, from);
417   }
418   ENSURE_ASAN_INITED();
419   if (flags()->replace_str) {
420     uptr from_size = REAL(strlen)(from) + 1;
421     CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
422     ASAN_READ_RANGE(ctx, from, from_size);
423     ASAN_WRITE_RANGE(ctx, to, from_size);
424   }
425   return REAL(strcpy)(to, from);
426 }
427 
428 INTERCEPTOR(char*, strdup, const char *s) {
429   void *ctx;
430   ASAN_INTERCEPTOR_ENTER(ctx, strdup);
431   if (UNLIKELY(!asan_inited)) return internal_strdup(s);
432   ENSURE_ASAN_INITED();
433   uptr length = REAL(strlen)(s);
434   if (flags()->replace_str) {
435     ASAN_READ_RANGE(ctx, s, length + 1);
436   }
437   GET_STACK_TRACE_MALLOC;
438   void *new_mem = asan_malloc(length + 1, &stack);
439   REAL(memcpy)(new_mem, s, length + 1);
440   return reinterpret_cast<char*>(new_mem);
441 }
442 
443 #if ASAN_INTERCEPT___STRDUP
444 INTERCEPTOR(char*, __strdup, const char *s) {
445   void *ctx;
446   ASAN_INTERCEPTOR_ENTER(ctx, strdup);
447   if (UNLIKELY(!asan_inited)) return internal_strdup(s);
448   ENSURE_ASAN_INITED();
449   uptr length = REAL(strlen)(s);
450   if (flags()->replace_str) {
451     ASAN_READ_RANGE(ctx, s, length + 1);
452   }
453   GET_STACK_TRACE_MALLOC;
454   void *new_mem = asan_malloc(length + 1, &stack);
455   REAL(memcpy)(new_mem, s, length + 1);
456   return reinterpret_cast<char*>(new_mem);
457 }
458 #endif // ASAN_INTERCEPT___STRDUP
459 
460 INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
461   void *ctx;
462   ASAN_INTERCEPTOR_ENTER(ctx, strncpy);
463   ENSURE_ASAN_INITED();
464   if (flags()->replace_str) {
465     uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
466     CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
467     ASAN_READ_RANGE(ctx, from, from_size);
468     ASAN_WRITE_RANGE(ctx, to, size);
469   }
470   return REAL(strncpy)(to, from, size);
471 }
472 
473 INTERCEPTOR(long, strtol, const char *nptr, char **endptr, int base) {
474   void *ctx;
475   ASAN_INTERCEPTOR_ENTER(ctx, strtol);
476   ENSURE_ASAN_INITED();
477   if (!flags()->replace_str) {
478     return REAL(strtol)(nptr, endptr, base);
479   }
480   char *real_endptr;
481   long result = REAL(strtol)(nptr, &real_endptr, base);
482   StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
483   return result;
484 }
485 
486 INTERCEPTOR(int, atoi, const char *nptr) {
487   void *ctx;
488   ASAN_INTERCEPTOR_ENTER(ctx, atoi);
489 #if SANITIZER_MAC
490   if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
491 #endif
492   ENSURE_ASAN_INITED();
493   if (!flags()->replace_str) {
494     return REAL(atoi)(nptr);
495   }
496   char *real_endptr;
497   // "man atoi" tells that behavior of atoi(nptr) is the same as
498   // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
499   // parsed integer can't be stored in *long* type (even if it's
500   // different from int). So, we just imitate this behavior.
501   int result = REAL(strtol)(nptr, &real_endptr, 10);
502   FixRealStrtolEndptr(nptr, &real_endptr);
503   ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
504   return result;
505 }
506 
507 INTERCEPTOR(long, atol, const char *nptr) {
508   void *ctx;
509   ASAN_INTERCEPTOR_ENTER(ctx, atol);
510 #if SANITIZER_MAC
511   if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
512 #endif
513   ENSURE_ASAN_INITED();
514   if (!flags()->replace_str) {
515     return REAL(atol)(nptr);
516   }
517   char *real_endptr;
518   long result = REAL(strtol)(nptr, &real_endptr, 10);
519   FixRealStrtolEndptr(nptr, &real_endptr);
520   ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
521   return result;
522 }
523 
524 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
525 INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr, int base) {
526   void *ctx;
527   ASAN_INTERCEPTOR_ENTER(ctx, strtoll);
528   ENSURE_ASAN_INITED();
529   if (!flags()->replace_str) {
530     return REAL(strtoll)(nptr, endptr, base);
531   }
532   char *real_endptr;
533   long long result = REAL(strtoll)(nptr, &real_endptr, base);
534   StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
535   return result;
536 }
537 
538 INTERCEPTOR(long long, atoll, const char *nptr) {
539   void *ctx;
540   ASAN_INTERCEPTOR_ENTER(ctx, atoll);
541   ENSURE_ASAN_INITED();
542   if (!flags()->replace_str) {
543     return REAL(atoll)(nptr);
544   }
545   char *real_endptr;
546   long long result = REAL(strtoll)(nptr, &real_endptr, 10);
547   FixRealStrtolEndptr(nptr, &real_endptr);
548   ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
549   return result;
550 }
551 #endif  // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
552 
553 #if ASAN_INTERCEPT___CXA_ATEXIT || ASAN_INTERCEPT_ATEXIT
554 static void AtCxaAtexit(void *unused) {
555   (void)unused;
556   StopInitOrderChecking();
557 }
558 #endif
559 
560 #if ASAN_INTERCEPT___CXA_ATEXIT
561 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
562             void *dso_handle) {
563 #if SANITIZER_MAC
564   if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
565 #endif
566   ENSURE_ASAN_INITED();
567 #if CAN_SANITIZE_LEAKS
568   __lsan::ScopedInterceptorDisabler disabler;
569 #endif
570   int res = REAL(__cxa_atexit)(func, arg, dso_handle);
571   REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
572   return res;
573 }
574 #endif  // ASAN_INTERCEPT___CXA_ATEXIT
575 
576 #if ASAN_INTERCEPT_ATEXIT
577 INTERCEPTOR(int, atexit, void (*func)()) {
578   ENSURE_ASAN_INITED();
579 #if CAN_SANITIZE_LEAKS
580   __lsan::ScopedInterceptorDisabler disabler;
581 #endif
582   // Avoid calling real atexit as it is unrechable on at least on Linux.
583   int res = REAL(__cxa_atexit)((void (*)(void *a))func, nullptr, nullptr);
584   REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
585   return res;
586 }
587 #endif
588 
589 #if ASAN_INTERCEPT_PTHREAD_ATFORK
590 extern "C" {
591 extern int _pthread_atfork(void (*prepare)(), void (*parent)(),
592                            void (*child)());
593 };
594 
595 INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(),
596             void (*child)()) {
597 #if CAN_SANITIZE_LEAKS
598   __lsan::ScopedInterceptorDisabler disabler;
599 #endif
600   // REAL(pthread_atfork) cannot be called due to symbol indirections at least
601   // on NetBSD
602   return _pthread_atfork(prepare, parent, child);
603 }
604 #endif
605 
606 #if ASAN_INTERCEPT_VFORK
607 DEFINE_REAL(int, vfork)
608 DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(int, vfork)
609 #endif
610 
611 // ---------------------- InitializeAsanInterceptors ---------------- {{{1
612 namespace __asan {
613 void InitializeAsanInterceptors() {
614   static bool was_called_once;
615   CHECK(!was_called_once);
616   was_called_once = true;
617   InitializeCommonInterceptors();
618   InitializeSignalInterceptors();
619 
620   // Intercept str* functions.
621   ASAN_INTERCEPT_FUNC(strcat);
622   ASAN_INTERCEPT_FUNC(strcpy);
623   ASAN_INTERCEPT_FUNC(strncat);
624   ASAN_INTERCEPT_FUNC(strncpy);
625   ASAN_INTERCEPT_FUNC(strdup);
626 #if ASAN_INTERCEPT___STRDUP
627   ASAN_INTERCEPT_FUNC(__strdup);
628 #endif
629 #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
630   ASAN_INTERCEPT_FUNC(index);
631 #endif
632 
633   ASAN_INTERCEPT_FUNC(atoi);
634   ASAN_INTERCEPT_FUNC(atol);
635   ASAN_INTERCEPT_FUNC(strtol);
636 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
637   ASAN_INTERCEPT_FUNC(atoll);
638   ASAN_INTERCEPT_FUNC(strtoll);
639 #endif
640 
641   // Intecept jump-related functions.
642   ASAN_INTERCEPT_FUNC(longjmp);
643 
644 #if ASAN_INTERCEPT_SWAPCONTEXT
645   ASAN_INTERCEPT_FUNC(swapcontext);
646 #endif
647 #if ASAN_INTERCEPT__LONGJMP
648   ASAN_INTERCEPT_FUNC(_longjmp);
649 #endif
650 #if ASAN_INTERCEPT___LONGJMP_CHK
651   ASAN_INTERCEPT_FUNC(__longjmp_chk);
652 #endif
653 #if ASAN_INTERCEPT_SIGLONGJMP
654   ASAN_INTERCEPT_FUNC(siglongjmp);
655 #endif
656 
657   // Intercept exception handling functions.
658 #if ASAN_INTERCEPT___CXA_THROW
659   ASAN_INTERCEPT_FUNC(__cxa_throw);
660 #endif
661 #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
662   ASAN_INTERCEPT_FUNC(__cxa_rethrow_primary_exception);
663 #endif
664   // Indirectly intercept std::rethrow_exception.
665 #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
666   INTERCEPT_FUNCTION(_Unwind_RaiseException);
667 #endif
668   // Indirectly intercept std::rethrow_exception.
669 #if ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION
670   INTERCEPT_FUNCTION(_Unwind_SjLj_RaiseException);
671 #endif
672 
673   // Intercept threading-related functions
674 #if ASAN_INTERCEPT_PTHREAD_CREATE
675 #if defined(ASAN_PTHREAD_CREATE_VERSION)
676   ASAN_INTERCEPT_FUNC_VER(pthread_create, ASAN_PTHREAD_CREATE_VERSION);
677 #else
678   ASAN_INTERCEPT_FUNC(pthread_create);
679 #endif
680   ASAN_INTERCEPT_FUNC(pthread_join);
681 #endif
682 
683   // Intercept atexit function.
684 #if ASAN_INTERCEPT___CXA_ATEXIT
685   ASAN_INTERCEPT_FUNC(__cxa_atexit);
686 #endif
687 
688 #if ASAN_INTERCEPT_ATEXIT
689   ASAN_INTERCEPT_FUNC(atexit);
690 #endif
691 
692 #if ASAN_INTERCEPT_PTHREAD_ATFORK
693   ASAN_INTERCEPT_FUNC(pthread_atfork);
694 #endif
695 
696 #if ASAN_INTERCEPT_VFORK
697   ASAN_INTERCEPT_FUNC(vfork);
698 #endif
699 
700   InitializePlatformInterceptors();
701 
702   VReport(1, "AddressSanitizer: libc interceptors initialized\n");
703 }
704 
705 } // namespace __asan
706 
707 #endif  // !SANITIZER_FUCHSIA
708