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