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