asan_interceptors.cc revision e18e3f07802c420eb4b2da407e148084b75cecc9
1//===-- asan_interceptors.cc ----------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Intercept various libc functions.
13//===----------------------------------------------------------------------===//
14#include "asan_interceptors.h"
15
16#include "asan_allocator.h"
17#include "asan_intercepted_functions.h"
18#include "asan_internal.h"
19#include "asan_mapping.h"
20#include "asan_poisoning.h"
21#include "asan_report.h"
22#include "asan_stack.h"
23#include "asan_stats.h"
24#include "interception/interception.h"
25#include "sanitizer_common/sanitizer_libc.h"
26
27namespace __asan {
28
29// Return true if we can quickly decide that the region is unpoisoned.
30static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) {
31  if (size == 0) return true;
32  if (size <= 32)
33    return !AddressIsPoisoned(beg) &&
34           !AddressIsPoisoned(beg + size - 1) &&
35           !AddressIsPoisoned(beg + size / 2);
36  return false;
37}
38
39// We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
40// and ASAN_WRITE_RANGE as macro instead of function so
41// that no extra frames are created, and stack trace contains
42// relevant information only.
43// We check all shadow bytes.
44#define ACCESS_MEMORY_RANGE(offset, size, isWrite) do {                 \
45    uptr __offset = (uptr)(offset);                                     \
46    uptr __size = (uptr)(size);                                         \
47    uptr __bad = 0;                                                     \
48    if (!QuickCheckForUnpoisonedRegion(__offset, __size) &&             \
49        (__bad = __asan_region_is_poisoned(__offset, __size))) {        \
50      GET_CURRENT_PC_BP_SP;                                             \
51      __asan_report_error(pc, bp, sp, __bad, isWrite, __size);          \
52    }                                                                   \
53  } while (0)
54
55#define ASAN_READ_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, false)
56#define ASAN_WRITE_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, true)
57
58// Behavior of functions like "memcpy" or "strcpy" is undefined
59// if memory intervals overlap. We report error in this case.
60// Macro is used to avoid creation of new frames.
61static inline bool RangesOverlap(const char *offset1, uptr length1,
62                                 const char *offset2, uptr length2) {
63  return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
64}
65#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
66  const char *offset1 = (const char*)_offset1; \
67  const char *offset2 = (const char*)_offset2; \
68  if (RangesOverlap(offset1, length1, offset2, length2)) { \
69    GET_STACK_TRACE_FATAL_HERE; \
70    ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
71                                            offset2, length2, &stack); \
72  } \
73} while (0)
74
75#define ENSURE_ASAN_INITED() do { \
76  CHECK(!asan_init_is_running); \
77  if (!asan_inited) { \
78    __asan_init(); \
79  } \
80} while (0)
81
82static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
83#if ASAN_INTERCEPT_STRNLEN
84  if (REAL(strnlen) != 0) {
85    return REAL(strnlen)(s, maxlen);
86  }
87#endif
88  return internal_strnlen(s, maxlen);
89}
90
91void SetThreadName(const char *name) {
92  AsanThread *t = GetCurrentThread();
93  if (t)
94    asanThreadRegistry().SetThreadName(t->tid(), name);
95}
96
97}  // namespace __asan
98
99// ---------------------- Wrappers ---------------- {{{1
100using namespace __asan;  // NOLINT
101
102DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr)
103DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
104
105#define COMMON_INTERCEPTOR_UNPOISON_PARAM(ctx, count) \
106  do {                                                \
107  } while (false)
108#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
109  ASAN_WRITE_RANGE(ptr, size)
110#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) ASAN_READ_RANGE(ptr, size)
111#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...)              \
112  do {                                                        \
113    if (asan_init_is_running) return REAL(func)(__VA_ARGS__); \
114    ctx = 0;                                                  \
115    (void) ctx;                                               \
116    ENSURE_ASAN_INITED();                                     \
117  } while (false)
118#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
119  do {                                         \
120  } while (false)
121#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
122  do {                                         \
123  } while (false)
124#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
125  do {                                                      \
126  } while (false)
127#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
128#define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
129#include "sanitizer_common/sanitizer_common_interceptors.inc"
130
131#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(p, s)
132#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(p, s)
133#define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
134  do {                                       \
135  } while (false)
136#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
137  do {                                        \
138  } while (false)
139#include "sanitizer_common/sanitizer_common_syscalls.inc"
140
141static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
142  AsanThread *t = (AsanThread*)arg;
143  SetCurrentThread(t);
144  return t->ThreadStart(GetTid());
145}
146
147#if ASAN_INTERCEPT_PTHREAD_CREATE
148extern "C" int pthread_attr_getdetachstate(void *attr, int *v);
149
150INTERCEPTOR(int, pthread_create, void *thread,
151    void *attr, void *(*start_routine)(void*), void *arg) {
152  EnsureMainThreadIDIsCorrect();
153  // Strict init-order checking in thread-hostile.
154  if (flags()->strict_init_order)
155    StopInitOrderChecking();
156  GET_STACK_TRACE_THREAD;
157  int detached = 0;
158  if (attr != 0)
159    pthread_attr_getdetachstate(attr, &detached);
160
161  u32 current_tid = GetCurrentTidOrInvalid();
162  AsanThread *t = AsanThread::Create(start_routine, arg);
163  CreateThreadContextArgs args = { t, &stack };
164  asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
165  return REAL(pthread_create)(thread, attr, asan_thread_start, t);
166}
167#endif  // ASAN_INTERCEPT_PTHREAD_CREATE
168
169#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
170INTERCEPTOR(void*, signal, int signum, void *handler) {
171  if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) {
172    return REAL(signal)(signum, handler);
173  }
174  return 0;
175}
176
177INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
178                            struct sigaction *oldact) {
179  if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) {
180    return REAL(sigaction)(signum, act, oldact);
181  }
182  return 0;
183}
184#elif SANITIZER_POSIX
185// We need to have defined REAL(sigaction) on posix systems.
186DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
187    struct sigaction *oldact)
188#endif  // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
189
190#if ASAN_INTERCEPT_SWAPCONTEXT
191static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
192  // Align to page size.
193  uptr PageSize = GetPageSizeCached();
194  uptr bottom = stack & ~(PageSize - 1);
195  ssize += stack - bottom;
196  ssize = RoundUpTo(ssize, PageSize);
197  static const uptr kMaxSaneContextStackSize = 1 << 22;  // 4 Mb
198  if (ssize && ssize <= kMaxSaneContextStackSize) {
199    PoisonShadow(bottom, ssize, 0);
200  }
201}
202
203INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
204            struct ucontext_t *ucp) {
205  static bool reported_warning = false;
206  if (!reported_warning) {
207    Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
208           "functions and may produce false positives in some cases!\n");
209    reported_warning = true;
210  }
211  // Clear shadow memory for new context (it may share stack
212  // with current context).
213  uptr stack, ssize;
214  ReadContextStack(ucp, &stack, &ssize);
215  ClearShadowMemoryForContextStack(stack, ssize);
216  int res = REAL(swapcontext)(oucp, ucp);
217  // swapcontext technically does not return, but program may swap context to
218  // "oucp" later, that would look as if swapcontext() returned 0.
219  // We need to clear shadow for ucp once again, as it may be in arbitrary
220  // state.
221  ClearShadowMemoryForContextStack(stack, ssize);
222  return res;
223}
224#endif  // ASAN_INTERCEPT_SWAPCONTEXT
225
226INTERCEPTOR(void, longjmp, void *env, int val) {
227  __asan_handle_no_return();
228  REAL(longjmp)(env, val);
229}
230
231#if ASAN_INTERCEPT__LONGJMP
232INTERCEPTOR(void, _longjmp, void *env, int val) {
233  __asan_handle_no_return();
234  REAL(_longjmp)(env, val);
235}
236#endif
237
238#if ASAN_INTERCEPT_SIGLONGJMP
239INTERCEPTOR(void, siglongjmp, void *env, int val) {
240  __asan_handle_no_return();
241  REAL(siglongjmp)(env, val);
242}
243#endif
244
245#if ASAN_INTERCEPT___CXA_THROW
246INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
247  CHECK(REAL(__cxa_throw));
248  __asan_handle_no_return();
249  REAL(__cxa_throw)(a, b, c);
250}
251#endif
252
253// intercept mlock and friends.
254// Since asan maps 16T of RAM, mlock is completely unfriendly to asan.
255// All functions return 0 (success).
256static void MlockIsUnsupported() {
257  static bool printed = false;
258  if (printed) return;
259  printed = true;
260  if (flags()->verbosity > 0) {
261    Printf("INFO: AddressSanitizer ignores "
262           "mlock/mlockall/munlock/munlockall\n");
263  }
264}
265
266INTERCEPTOR(int, mlock, const void *addr, uptr len) {
267  MlockIsUnsupported();
268  return 0;
269}
270
271INTERCEPTOR(int, munlock, const void *addr, uptr len) {
272  MlockIsUnsupported();
273  return 0;
274}
275
276INTERCEPTOR(int, mlockall, int flags) {
277  MlockIsUnsupported();
278  return 0;
279}
280
281INTERCEPTOR(int, munlockall, void) {
282  MlockIsUnsupported();
283  return 0;
284}
285
286static inline int CharCmp(unsigned char c1, unsigned char c2) {
287  return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
288}
289
290INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
291  if (!asan_inited) return internal_memcmp(a1, a2, size);
292  ENSURE_ASAN_INITED();
293  if (flags()->replace_intrin) {
294    if (flags()->strict_memcmp) {
295      // Check the entire regions even if the first bytes of the buffers are
296      // different.
297      ASAN_READ_RANGE(a1, size);
298      ASAN_READ_RANGE(a2, size);
299      // Fallthrough to REAL(memcmp) below.
300    } else {
301      unsigned char c1 = 0, c2 = 0;
302      const unsigned char *s1 = (const unsigned char*)a1;
303      const unsigned char *s2 = (const unsigned char*)a2;
304      uptr i;
305      for (i = 0; i < size; i++) {
306        c1 = s1[i];
307        c2 = s2[i];
308        if (c1 != c2) break;
309      }
310      ASAN_READ_RANGE(s1, Min(i + 1, size));
311      ASAN_READ_RANGE(s2, Min(i + 1, size));
312      return CharCmp(c1, c2);
313    }
314  }
315  return REAL(memcmp(a1, a2, size));
316}
317
318INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
319  if (!asan_inited) return internal_memcpy(to, from, size);
320  // memcpy is called during __asan_init() from the internals
321  // of printf(...).
322  if (asan_init_is_running) {
323    return REAL(memcpy)(to, from, size);
324  }
325  ENSURE_ASAN_INITED();
326  if (flags()->replace_intrin) {
327    if (to != from) {
328      // We do not treat memcpy with to==from as a bug.
329      // See http://llvm.org/bugs/show_bug.cgi?id=11763.
330      CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
331    }
332    ASAN_READ_RANGE(from, size);
333    ASAN_WRITE_RANGE(to, size);
334  }
335  // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8.
336  // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
337  return internal_memcpy(to, from, size);
338}
339
340INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
341  if (!asan_inited) return internal_memmove(to, from, size);
342  if (asan_init_is_running) {
343    return REAL(memmove)(to, from, size);
344  }
345  ENSURE_ASAN_INITED();
346  if (flags()->replace_intrin) {
347    ASAN_READ_RANGE(from, size);
348    ASAN_WRITE_RANGE(to, size);
349  }
350  // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8.
351  // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
352  return internal_memmove(to, from, size);
353}
354
355INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
356  if (!asan_inited) return internal_memset(block, c, size);
357  // memset is called inside Printf.
358  if (asan_init_is_running) {
359    return REAL(memset)(block, c, size);
360  }
361  ENSURE_ASAN_INITED();
362  if (flags()->replace_intrin) {
363    ASAN_WRITE_RANGE(block, size);
364  }
365  return REAL(memset)(block, c, size);
366}
367
368INTERCEPTOR(char*, strchr, const char *str, int c) {
369  if (!asan_inited) return internal_strchr(str, c);
370  // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is
371  // used.
372  if (asan_init_is_running) {
373    return REAL(strchr)(str, c);
374  }
375  ENSURE_ASAN_INITED();
376  char *result = REAL(strchr)(str, c);
377  if (flags()->replace_str) {
378    uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1;
379    ASAN_READ_RANGE(str, bytes_read);
380  }
381  return result;
382}
383
384#if ASAN_INTERCEPT_INDEX
385# if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
386INTERCEPTOR(char*, index, const char *string, int c)
387  ALIAS(WRAPPER_NAME(strchr));
388# else
389#  if SANITIZER_MAC
390DECLARE_REAL(char*, index, const char *string, int c)
391OVERRIDE_FUNCTION(index, strchr);
392#  else
393DEFINE_REAL(char*, index, const char *string, int c)
394#  endif
395# endif
396#endif  // ASAN_INTERCEPT_INDEX
397
398// For both strcat() and strncat() we need to check the validity of |to|
399// argument irrespective of the |from| length.
400INTERCEPTOR(char*, strcat, char *to, const char *from) {  // NOLINT
401  ENSURE_ASAN_INITED();
402  if (flags()->replace_str) {
403    uptr from_length = REAL(strlen)(from);
404    ASAN_READ_RANGE(from, from_length + 1);
405    uptr to_length = REAL(strlen)(to);
406    ASAN_READ_RANGE(to, to_length);
407    ASAN_WRITE_RANGE(to + to_length, from_length + 1);
408    // If the copying actually happens, the |from| string should not overlap
409    // with the resulting string starting at |to|, which has a length of
410    // to_length + from_length + 1.
411    if (from_length > 0) {
412      CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
413                           from, from_length + 1);
414    }
415  }
416  return REAL(strcat)(to, from);  // NOLINT
417}
418
419INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
420  ENSURE_ASAN_INITED();
421  if (flags()->replace_str) {
422    uptr from_length = MaybeRealStrnlen(from, size);
423    uptr copy_length = Min(size, from_length + 1);
424    ASAN_READ_RANGE(from, copy_length);
425    uptr to_length = REAL(strlen)(to);
426    ASAN_READ_RANGE(to, to_length);
427    ASAN_WRITE_RANGE(to + to_length, from_length + 1);
428    if (from_length > 0) {
429      CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
430                           from, copy_length);
431    }
432  }
433  return REAL(strncat)(to, from, size);
434}
435
436INTERCEPTOR(char*, strcpy, char *to, const char *from) {  // NOLINT
437#if SANITIZER_MAC
438  if (!asan_inited) return REAL(strcpy)(to, from);  // NOLINT
439#endif
440  // strcpy is called from malloc_default_purgeable_zone()
441  // in __asan::ReplaceSystemAlloc() on Mac.
442  if (asan_init_is_running) {
443    return REAL(strcpy)(to, from);  // NOLINT
444  }
445  ENSURE_ASAN_INITED();
446  if (flags()->replace_str) {
447    uptr from_size = REAL(strlen)(from) + 1;
448    CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
449    ASAN_READ_RANGE(from, from_size);
450    ASAN_WRITE_RANGE(to, from_size);
451  }
452  return REAL(strcpy)(to, from);  // NOLINT
453}
454
455#if ASAN_INTERCEPT_STRDUP
456INTERCEPTOR(char*, strdup, const char *s) {
457  if (!asan_inited) return internal_strdup(s);
458  ENSURE_ASAN_INITED();
459  uptr length = REAL(strlen)(s);
460  if (flags()->replace_str) {
461    ASAN_READ_RANGE(s, length + 1);
462  }
463  GET_STACK_TRACE_MALLOC;
464  void *new_mem = asan_malloc(length + 1, &stack);
465  REAL(memcpy)(new_mem, s, length + 1);
466  return reinterpret_cast<char*>(new_mem);
467}
468#endif
469
470INTERCEPTOR(uptr, strlen, const char *s) {
471  if (!asan_inited) return internal_strlen(s);
472  // strlen is called from malloc_default_purgeable_zone()
473  // in __asan::ReplaceSystemAlloc() on Mac.
474  if (asan_init_is_running) {
475    return REAL(strlen)(s);
476  }
477  ENSURE_ASAN_INITED();
478  uptr length = REAL(strlen)(s);
479  if (flags()->replace_str) {
480    ASAN_READ_RANGE(s, length + 1);
481  }
482  return length;
483}
484
485INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
486  ENSURE_ASAN_INITED();
487  if (flags()->replace_str) {
488    uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
489    CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
490    ASAN_READ_RANGE(from, from_size);
491    ASAN_WRITE_RANGE(to, size);
492  }
493  return REAL(strncpy)(to, from, size);
494}
495
496#if ASAN_INTERCEPT_STRNLEN
497INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) {
498  ENSURE_ASAN_INITED();
499  uptr length = REAL(strnlen)(s, maxlen);
500  if (flags()->replace_str) {
501    ASAN_READ_RANGE(s, Min(length + 1, maxlen));
502  }
503  return length;
504}
505#endif  // ASAN_INTERCEPT_STRNLEN
506
507static inline bool IsValidStrtolBase(int base) {
508  return (base == 0) || (2 <= base && base <= 36);
509}
510
511static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
512  CHECK(endptr);
513  if (nptr == *endptr) {
514    // No digits were found at strtol call, we need to find out the last
515    // symbol accessed by strtoll on our own.
516    // We get this symbol by skipping leading blanks and optional +/- sign.
517    while (IsSpace(*nptr)) nptr++;
518    if (*nptr == '+' || *nptr == '-') nptr++;
519    *endptr = (char*)nptr;
520  }
521  CHECK(*endptr >= nptr);
522}
523
524INTERCEPTOR(long, strtol, const char *nptr,  // NOLINT
525            char **endptr, int base) {
526  ENSURE_ASAN_INITED();
527  if (!flags()->replace_str) {
528    return REAL(strtol)(nptr, endptr, base);
529  }
530  char *real_endptr;
531  long result = REAL(strtol)(nptr, &real_endptr, base);  // NOLINT
532  if (endptr != 0) {
533    *endptr = real_endptr;
534  }
535  if (IsValidStrtolBase(base)) {
536    FixRealStrtolEndptr(nptr, &real_endptr);
537    ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
538  }
539  return result;
540}
541
542INTERCEPTOR(int, atoi, const char *nptr) {
543#if SANITIZER_MAC
544  if (!asan_inited) return REAL(atoi)(nptr);
545#endif
546  ENSURE_ASAN_INITED();
547  if (!flags()->replace_str) {
548    return REAL(atoi)(nptr);
549  }
550  char *real_endptr;
551  // "man atoi" tells that behavior of atoi(nptr) is the same as
552  // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
553  // parsed integer can't be stored in *long* type (even if it's
554  // different from int). So, we just imitate this behavior.
555  int result = REAL(strtol)(nptr, &real_endptr, 10);
556  FixRealStrtolEndptr(nptr, &real_endptr);
557  ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
558  return result;
559}
560
561INTERCEPTOR(long, atol, const char *nptr) {  // NOLINT
562#if SANITIZER_MAC
563  if (!asan_inited) return REAL(atol)(nptr);
564#endif
565  ENSURE_ASAN_INITED();
566  if (!flags()->replace_str) {
567    return REAL(atol)(nptr);
568  }
569  char *real_endptr;
570  long result = REAL(strtol)(nptr, &real_endptr, 10);  // NOLINT
571  FixRealStrtolEndptr(nptr, &real_endptr);
572  ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
573  return result;
574}
575
576#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
577INTERCEPTOR(long long, strtoll, const char *nptr,  // NOLINT
578            char **endptr, int base) {
579  ENSURE_ASAN_INITED();
580  if (!flags()->replace_str) {
581    return REAL(strtoll)(nptr, endptr, base);
582  }
583  char *real_endptr;
584  long long result = REAL(strtoll)(nptr, &real_endptr, base);  // NOLINT
585  if (endptr != 0) {
586    *endptr = real_endptr;
587  }
588  // If base has unsupported value, strtoll can exit with EINVAL
589  // without reading any characters. So do additional checks only
590  // if base is valid.
591  if (IsValidStrtolBase(base)) {
592    FixRealStrtolEndptr(nptr, &real_endptr);
593    ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
594  }
595  return result;
596}
597
598INTERCEPTOR(long long, atoll, const char *nptr) {  // NOLINT
599  ENSURE_ASAN_INITED();
600  if (!flags()->replace_str) {
601    return REAL(atoll)(nptr);
602  }
603  char *real_endptr;
604  long long result = REAL(strtoll)(nptr, &real_endptr, 10);  // NOLINT
605  FixRealStrtolEndptr(nptr, &real_endptr);
606  ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
607  return result;
608}
609#endif  // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
610
611static void AtCxaAtexit(void *unused) {
612  (void)unused;
613  StopInitOrderChecking();
614}
615
616#if ASAN_INTERCEPT___CXA_ATEXIT
617INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
618            void *dso_handle) {
619  ENSURE_ASAN_INITED();
620  int res = REAL(__cxa_atexit)(func, arg, dso_handle);
621  REAL(__cxa_atexit)(AtCxaAtexit, 0, 0);
622  return res;
623}
624#endif  // ASAN_INTERCEPT___CXA_ATEXIT
625
626#if !SANITIZER_MAC
627#define ASAN_INTERCEPT_FUNC(name) do { \
628      if (!INTERCEPT_FUNCTION(name) && flags()->verbosity > 0) \
629        Report("AddressSanitizer: failed to intercept '" #name "'\n"); \
630    } while (0)
631#else
632// OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
633#define ASAN_INTERCEPT_FUNC(name)
634#endif  // SANITIZER_MAC
635
636#if SANITIZER_WINDOWS
637INTERCEPTOR_WINAPI(DWORD, CreateThread,
638                   void* security, uptr stack_size,
639                   DWORD (__stdcall *start_routine)(void*), void* arg,
640                   DWORD thr_flags, void* tid) {
641  // Strict init-order checking in thread-hostile.
642  if (flags()->strict_init_order)
643    StopInitOrderChecking();
644  GET_STACK_TRACE_THREAD;
645  u32 current_tid = GetCurrentTidOrInvalid();
646  AsanThread *t = AsanThread::Create(start_routine, arg);
647  CreateThreadContextArgs args = { t, &stack };
648  bool detached = false;  // FIXME: how can we determine it on Windows?
649  asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args);
650  return REAL(CreateThread)(security, stack_size,
651                            asan_thread_start, t, thr_flags, tid);
652}
653
654namespace __asan {
655void InitializeWindowsInterceptors() {
656  ASAN_INTERCEPT_FUNC(CreateThread);
657}
658
659}  // namespace __asan
660#endif
661
662// ---------------------- InitializeAsanInterceptors ---------------- {{{1
663namespace __asan {
664void InitializeAsanInterceptors() {
665  static bool was_called_once;
666  CHECK(was_called_once == false);
667  was_called_once = true;
668  SANITIZER_COMMON_INTERCEPTORS_INIT;
669
670  // Intercept mem* functions.
671  ASAN_INTERCEPT_FUNC(memcmp);
672  ASAN_INTERCEPT_FUNC(memmove);
673  ASAN_INTERCEPT_FUNC(memset);
674  if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
675    ASAN_INTERCEPT_FUNC(memcpy);
676  }
677
678  // Intercept str* functions.
679  ASAN_INTERCEPT_FUNC(strcat);  // NOLINT
680  ASAN_INTERCEPT_FUNC(strchr);
681  ASAN_INTERCEPT_FUNC(strcpy);  // NOLINT
682  ASAN_INTERCEPT_FUNC(strlen);
683  ASAN_INTERCEPT_FUNC(strncat);
684  ASAN_INTERCEPT_FUNC(strncpy);
685#if ASAN_INTERCEPT_STRDUP
686  ASAN_INTERCEPT_FUNC(strdup);
687#endif
688#if ASAN_INTERCEPT_STRNLEN
689  ASAN_INTERCEPT_FUNC(strnlen);
690#endif
691#if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
692  ASAN_INTERCEPT_FUNC(index);
693#endif
694
695  ASAN_INTERCEPT_FUNC(atoi);
696  ASAN_INTERCEPT_FUNC(atol);
697  ASAN_INTERCEPT_FUNC(strtol);
698#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
699  ASAN_INTERCEPT_FUNC(atoll);
700  ASAN_INTERCEPT_FUNC(strtoll);
701#endif
702
703#if ASAN_INTERCEPT_MLOCKX
704  // Intercept mlock/munlock.
705  ASAN_INTERCEPT_FUNC(mlock);
706  ASAN_INTERCEPT_FUNC(munlock);
707  ASAN_INTERCEPT_FUNC(mlockall);
708  ASAN_INTERCEPT_FUNC(munlockall);
709#endif
710
711  // Intecept signal- and jump-related functions.
712  ASAN_INTERCEPT_FUNC(longjmp);
713#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
714  ASAN_INTERCEPT_FUNC(sigaction);
715  ASAN_INTERCEPT_FUNC(signal);
716#endif
717#if ASAN_INTERCEPT_SWAPCONTEXT
718  ASAN_INTERCEPT_FUNC(swapcontext);
719#endif
720#if ASAN_INTERCEPT__LONGJMP
721  ASAN_INTERCEPT_FUNC(_longjmp);
722#endif
723#if ASAN_INTERCEPT_SIGLONGJMP
724  ASAN_INTERCEPT_FUNC(siglongjmp);
725#endif
726
727  // Intercept exception handling functions.
728#if ASAN_INTERCEPT___CXA_THROW
729  INTERCEPT_FUNCTION(__cxa_throw);
730#endif
731
732  // Intercept threading-related functions
733#if ASAN_INTERCEPT_PTHREAD_CREATE
734  ASAN_INTERCEPT_FUNC(pthread_create);
735#endif
736
737  // Intercept atexit function.
738#if ASAN_INTERCEPT___CXA_ATEXIT
739  ASAN_INTERCEPT_FUNC(__cxa_atexit);
740#endif
741
742  // Some Windows-specific interceptors.
743#if SANITIZER_WINDOWS
744  InitializeWindowsInterceptors();
745#endif
746
747  if (flags()->verbosity > 0) {
748    Report("AddressSanitizer: libc interceptors initialized\n");
749  }
750}
751
752}  // namespace __asan
753