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