asan_interceptors.cc revision e5f5895bda30f374b0b51412fd4d837fa59aed66
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_interface.h"
18#include "asan_internal.h"
19#include "asan_mapping.h"
20#include "asan_stack.h"
21#include "asan_stats.h"
22#include "asan_thread_registry.h"
23#include "interception/interception.h"
24#include "sanitizer_common/sanitizer_libc.h"
25
26using namespace __sanitizer;  // NOLINT
27
28// Use macro to describe if specific function should be
29// intercepted on a given platform.
30#if !defined(_WIN32)
31# define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 1
32#else
33# define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 0
34#endif
35
36#if !defined(__APPLE__)
37# define ASAN_INTERCEPT_STRNLEN 1
38#else
39# define ASAN_INTERCEPT_STRNLEN 0
40#endif
41
42#if defined(ANDROID) || defined(_WIN32)
43# define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 0
44#else
45# define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 1
46#endif
47
48// Use extern declarations of intercepted functions on Mac and Windows
49// to avoid including system headers.
50#if defined(__APPLE__) || (defined(_WIN32) && !defined(_DLL))
51extern "C" {
52// signal.h
53# if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
54struct sigaction;
55int sigaction(int sig, const struct sigaction *act,
56              struct sigaction *oldact);
57void *signal(int signum, void *handler);
58# endif
59
60// setjmp.h
61void longjmp(void* env, int value);
62# if !defined(_WIN32)
63void _longjmp(void *env, int value);
64# endif
65
66// string.h / strings.h
67int memcmp(const void *a1, const void *a2, uptr size);
68void* memmove(void *to, const void *from, uptr size);
69void* memcpy(void *to, const void *from, uptr size);
70void* memset(void *block, int c, uptr size);
71char* strchr(const char *str, int c);
72# if defined(__APPLE__)
73char* index(const char *string, int c);
74# endif
75char* strcat(char *to, const char* from);  // NOLINT
76char* strcpy(char *to, const char* from);  // NOLINT
77char* strncpy(char *to, const char* from, uptr size);
78int strcmp(const char *s1, const char* s2);
79int strncmp(const char *s1, const char* s2, uptr size);
80# if !defined(_WIN32)
81int strcasecmp(const char *s1, const char *s2);
82int strncasecmp(const char *s1, const char *s2, uptr n);
83char* strdup(const char *s);
84# endif
85uptr strlen(const char *s);
86# if ASAN_INTERCEPT_STRNLEN
87uptr strnlen(const char *s, uptr maxlen);
88# endif
89
90// stdlib.h
91int atoi(const char *nptr);
92long atol(const char *nptr);  // NOLINT
93long strtol(const char *nptr, char **endptr, int base);  // NOLINT
94# if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
95long long atoll(const char *nptr);  // NOLINT
96long long strtoll(const char *nptr, char **endptr, int base);  // NOLINT
97# endif
98
99// Windows threads.
100# if defined(_WIN32)
101__declspec(dllimport)
102void* __stdcall CreateThread(void *sec, uptr st, void* start,
103                             void *arg, DWORD fl, DWORD *id);
104# endif
105
106// Posix threads.
107# if !defined(_WIN32)
108int pthread_create(void *thread, void *attr, void *(*start_routine)(void*),
109                   void *arg);
110# endif
111}  // extern "C"
112#endif
113
114namespace __asan {
115
116// Instruments read/write access to a single byte in memory.
117// On error calls __asan_report_error, which aborts the program.
118#define ACCESS_ADDRESS(address, isWrite)   do {         \
119  if (AddressIsPoisoned(address)) {                     \
120    GET_CURRENT_PC_BP_SP;                               \
121    __asan_report_error(pc, bp, sp, address, isWrite, /* access_size */ 1); \
122  } \
123} while (0)
124
125// We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
126// and ASAN_WRITE_RANGE as macro instead of function so
127// that no extra frames are created, and stack trace contains
128// relevant information only.
129
130// Instruments read/write access to a memory range.
131// More complex implementation is possible, for now just
132// checking the first and the last byte of a range.
133#define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \
134  if (size > 0) { \
135    uptr ptr = (uptr)(offset); \
136    ACCESS_ADDRESS(ptr, isWrite); \
137    ACCESS_ADDRESS(ptr + (size) - 1, isWrite); \
138  } \
139} while (0)
140
141#define ASAN_READ_RANGE(offset, size) do { \
142  ACCESS_MEMORY_RANGE(offset, size, false); \
143} while (0)
144
145#define ASAN_WRITE_RANGE(offset, size) do { \
146  ACCESS_MEMORY_RANGE(offset, size, true); \
147} while (0)
148
149// Behavior of functions like "memcpy" or "strcpy" is undefined
150// if memory intervals overlap. We report error in this case.
151// Macro is used to avoid creation of new frames.
152static inline bool RangesOverlap(const char *offset1, uptr length1,
153                                 const char *offset2, uptr length2) {
154  return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
155}
156#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
157  const char *offset1 = (const char*)_offset1; \
158  const char *offset2 = (const char*)_offset2; \
159  if (RangesOverlap(offset1, length1, offset2, length2)) { \
160    Report("ERROR: AddressSanitizer %s-param-overlap: " \
161           "memory ranges [%p,%p) and [%p, %p) overlap\n", \
162           name, offset1, offset1 + length1, offset2, offset2 + length2); \
163    PRINT_CURRENT_STACK(); \
164    ShowStatsAndAbort(); \
165  } \
166} while (0)
167
168#define ENSURE_ASAN_INITED() do { \
169  CHECK(!asan_init_is_running); \
170  if (!asan_inited) { \
171    __asan_init(); \
172  } \
173} while (0)
174
175static inline bool IsSpace(int c) {
176  return (c == ' ') || (c == '\n') || (c == '\t') ||
177         (c == '\f') || (c == '\r') || (c == '\v');
178}
179
180static inline bool IsDigit(int c) {
181  return (c >= '0') && (c <= '9');
182}
183
184static inline int ToLower(int c) {
185  return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
186}
187
188// ---------------------- Internal string functions ---------------- {{{1
189
190s64 internal_simple_strtoll(const char *nptr, char **endptr, int base) {
191  CHECK(base == 10);
192  while (IsSpace(*nptr)) nptr++;
193  int sgn = 1;
194  u64 res = 0;
195  bool have_digits = false;
196  char *old_nptr = (char*)nptr;
197  if (*nptr == '+') {
198    sgn = 1;
199    nptr++;
200  } else if (*nptr == '-') {
201    sgn = -1;
202    nptr++;
203  }
204  while (IsDigit(*nptr)) {
205    res = (res <= UINT64_MAX / 10) ? res * 10 : UINT64_MAX;
206    int digit = ((*nptr) - '0');
207    res = (res <= UINT64_MAX - digit) ? res + digit : UINT64_MAX;
208    have_digits = true;
209    nptr++;
210  }
211  if (endptr != 0) {
212    *endptr = (have_digits) ? (char*)nptr : old_nptr;
213  }
214  if (sgn > 0) {
215    return (s64)(Min((u64)INT64_MAX, res));
216  } else {
217    return (res > INT64_MAX) ? INT64_MIN : ((s64)res * -1);
218  }
219}
220
221s64 internal_atoll(const char *nptr) {
222  return internal_simple_strtoll(nptr, (char**)0, 10);
223}
224
225uptr internal_strlen(const char *s) {
226  uptr i = 0;
227  while (s[i]) i++;
228  return i;
229}
230
231uptr internal_strnlen(const char *s, uptr maxlen) {
232#if ASAN_INTERCEPT_STRNLEN
233  if (REAL(strnlen) != 0) {
234    return REAL(strnlen)(s, maxlen);
235  }
236#endif
237  uptr i = 0;
238  while (i < maxlen && s[i]) i++;
239  return i;
240}
241
242char* internal_strchr(const char *s, int c) {
243  while (true) {
244    if (*s == (char)c)
245      return (char*)s;
246    if (*s == 0)
247      return 0;
248    s++;
249  }
250}
251
252void* internal_memchr(const void* s, int c, uptr n) {
253  const char* t = (char*)s;
254  for (uptr i = 0; i < n; ++i, ++t)
255    if (*t == c)
256      return (void*)t;
257  return 0;
258}
259
260int internal_memcmp(const void* s1, const void* s2, uptr n) {
261  const char* t1 = (char*)s1;
262  const char* t2 = (char*)s2;
263  for (uptr i = 0; i < n; ++i, ++t1, ++t2)
264    if (*t1 != *t2)
265      return *t1 < *t2 ? -1 : 1;
266  return 0;
267}
268
269// Should not be used in performance-critical places.
270void* internal_memset(void* s, int c, uptr n) {
271  // The next line prevents Clang from making a call to memset() instead of the
272  // loop below.
273  // FIXME: building the runtime with -ffreestanding is a better idea. However
274  // there currently are linktime problems due to PR12396.
275  char volatile *t = (char*)s;
276  for (uptr i = 0; i < n; ++i, ++t) {
277    *t = c;
278  }
279  return s;
280}
281
282char *internal_strstr(const char *haystack, const char *needle) {
283  // This is O(N^2), but we are not using it in hot places.
284  uptr len1 = internal_strlen(haystack);
285  uptr len2 = internal_strlen(needle);
286  if (len1 < len2) return 0;
287  for (uptr pos = 0; pos <= len1 - len2; pos++) {
288    if (internal_memcmp(haystack + pos, needle, len2) == 0)
289      return (char*)haystack + pos;
290  }
291  return 0;
292}
293
294char *internal_strncat(char *dst, const char *src, uptr n) {
295  uptr len = internal_strlen(dst);
296  uptr i;
297  for (i = 0; i < n && src[i]; i++)
298    dst[len + i] = src[i];
299  dst[len + i] = 0;
300  return dst;
301}
302
303}  // namespace __asan
304
305// ---------------------- Wrappers ---------------- {{{1
306using namespace __asan;  // NOLINT
307
308static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
309  AsanThread *t = (AsanThread*)arg;
310  asanThreadRegistry().SetCurrent(t);
311  return t->ThreadStart();
312}
313
314#ifndef _WIN32
315INTERCEPTOR(int, pthread_create, void *thread,
316    void *attr, void *(*start_routine)(void*), void *arg) {
317  GET_STACK_TRACE_HERE(kStackTraceMax);
318  int current_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
319  AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack);
320  asanThreadRegistry().RegisterThread(t);
321  return REAL(pthread_create)(thread, attr, asan_thread_start, t);
322}
323#endif  // !_WIN32
324
325#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
326INTERCEPTOR(void*, signal, int signum, void *handler) {
327  if (!AsanInterceptsSignal(signum)) {
328    return REAL(signal)(signum, handler);
329  }
330  return 0;
331}
332
333INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
334                            struct sigaction *oldact) {
335  if (!AsanInterceptsSignal(signum)) {
336    return REAL(sigaction)(signum, act, oldact);
337  }
338  return 0;
339}
340#elif ASAN_POSIX
341// We need to have defined REAL(sigaction) on posix systems.
342DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
343    struct sigaction *oldact);
344#endif  // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
345
346INTERCEPTOR(void, longjmp, void *env, int val) {
347  __asan_handle_no_return();
348  REAL(longjmp)(env, val);
349}
350
351#if !defined(_WIN32)
352INTERCEPTOR(void, _longjmp, void *env, int val) {
353  __asan_handle_no_return();
354  REAL(_longjmp)(env, val);
355}
356
357INTERCEPTOR(void, siglongjmp, void *env, int val) {
358  __asan_handle_no_return();
359  REAL(siglongjmp)(env, val);
360}
361#endif
362
363#if ASAN_HAS_EXCEPTIONS == 1
364#ifdef __APPLE__
365extern "C" void __cxa_throw(void *a, void *b, void *c);
366#endif  // __APPLE__
367
368INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
369  CHECK(REAL(__cxa_throw));
370  __asan_handle_no_return();
371  REAL(__cxa_throw)(a, b, c);
372}
373#endif
374
375// intercept mlock and friends.
376// Since asan maps 16T of RAM, mlock is completely unfriendly to asan.
377// All functions return 0 (success).
378static void MlockIsUnsupported() {
379  static bool printed = 0;
380  if (printed) return;
381  printed = true;
382  Printf("INFO: AddressSanitizer ignores mlock/mlockall/munlock/munlockall\n");
383}
384
385extern "C" {
386INTERCEPTOR_ATTRIBUTE
387int mlock(const void *addr, uptr len) {
388  MlockIsUnsupported();
389  return 0;
390}
391
392INTERCEPTOR_ATTRIBUTE
393int munlock(const void *addr, uptr len) {
394  MlockIsUnsupported();
395  return 0;
396}
397
398INTERCEPTOR_ATTRIBUTE
399int mlockall(int flags) {
400  MlockIsUnsupported();
401  return 0;
402}
403
404INTERCEPTOR_ATTRIBUTE
405int munlockall(void) {
406  MlockIsUnsupported();
407  return 0;
408}
409}  // extern "C"
410
411static inline int CharCmp(unsigned char c1, unsigned char c2) {
412  return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
413}
414
415static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
416  int c1_low = ToLower(c1);
417  int c2_low = ToLower(c2);
418  return c1_low - c2_low;
419}
420
421INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
422  ENSURE_ASAN_INITED();
423  unsigned char c1 = 0, c2 = 0;
424  const unsigned char *s1 = (const unsigned char*)a1;
425  const unsigned char *s2 = (const unsigned char*)a2;
426  uptr i;
427  for (i = 0; i < size; i++) {
428    c1 = s1[i];
429    c2 = s2[i];
430    if (c1 != c2) break;
431  }
432  ASAN_READ_RANGE(s1, Min(i + 1, size));
433  ASAN_READ_RANGE(s2, Min(i + 1, size));
434  return CharCmp(c1, c2);
435}
436
437INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
438  // memcpy is called during __asan_init() from the internals
439  // of printf(...).
440  if (asan_init_is_running) {
441    return REAL(memcpy)(to, from, size);
442  }
443  ENSURE_ASAN_INITED();
444  if (FLAG_replace_intrin) {
445    if (to != from) {
446      // We do not treat memcpy with to==from as a bug.
447      // See http://llvm.org/bugs/show_bug.cgi?id=11763.
448      CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
449    }
450    ASAN_WRITE_RANGE(from, size);
451    ASAN_READ_RANGE(to, size);
452  }
453  return REAL(memcpy)(to, from, size);
454}
455
456INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
457  ENSURE_ASAN_INITED();
458  if (FLAG_replace_intrin) {
459    ASAN_WRITE_RANGE(from, size);
460    ASAN_READ_RANGE(to, size);
461  }
462  return REAL(memmove)(to, from, size);
463}
464
465INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
466  // memset is called inside Printf.
467  if (asan_init_is_running) {
468    return REAL(memset)(block, c, size);
469  }
470  ENSURE_ASAN_INITED();
471  if (FLAG_replace_intrin) {
472    ASAN_WRITE_RANGE(block, size);
473  }
474  return REAL(memset)(block, c, size);
475}
476
477INTERCEPTOR(char*, strchr, const char *str, int c) {
478  ENSURE_ASAN_INITED();
479  char *result = REAL(strchr)(str, c);
480  if (FLAG_replace_str) {
481    uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1;
482    ASAN_READ_RANGE(str, bytes_read);
483  }
484  return result;
485}
486
487#ifdef __linux__
488INTERCEPTOR(char*, index, const char *string, int c)
489  ALIAS(WRAPPER_NAME(strchr));
490#else
491DEFINE_REAL(char*, index, const char *string, int c);
492#endif
493
494INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
495  ENSURE_ASAN_INITED();
496  unsigned char c1, c2;
497  uptr i;
498  for (i = 0; ; i++) {
499    c1 = (unsigned char)s1[i];
500    c2 = (unsigned char)s2[i];
501    if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
502  }
503  ASAN_READ_RANGE(s1, i + 1);
504  ASAN_READ_RANGE(s2, i + 1);
505  return CharCaseCmp(c1, c2);
506}
507
508INTERCEPTOR(char*, strcat, char *to, const char *from) {  // NOLINT
509  ENSURE_ASAN_INITED();
510  if (FLAG_replace_str) {
511    uptr from_length = REAL(strlen)(from);
512    ASAN_READ_RANGE(from, from_length + 1);
513    if (from_length > 0) {
514      uptr to_length = REAL(strlen)(to);
515      ASAN_READ_RANGE(to, to_length);
516      ASAN_WRITE_RANGE(to + to_length, from_length + 1);
517      CHECK_RANGES_OVERLAP("strcat", to, to_length + 1, from, from_length + 1);
518    }
519  }
520  return REAL(strcat)(to, from);  // NOLINT
521}
522
523INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
524  if (!asan_inited) {
525    return internal_strcmp(s1, s2);
526  }
527  unsigned char c1, c2;
528  uptr i;
529  for (i = 0; ; i++) {
530    c1 = (unsigned char)s1[i];
531    c2 = (unsigned char)s2[i];
532    if (c1 != c2 || c1 == '\0') break;
533  }
534  ASAN_READ_RANGE(s1, i + 1);
535  ASAN_READ_RANGE(s2, i + 1);
536  return CharCmp(c1, c2);
537}
538
539INTERCEPTOR(char*, strcpy, char *to, const char *from) {  // NOLINT
540  // strcpy is called from malloc_default_purgeable_zone()
541  // in __asan::ReplaceSystemAlloc() on Mac.
542  if (asan_init_is_running) {
543    return REAL(strcpy)(to, from);  // NOLINT
544  }
545  ENSURE_ASAN_INITED();
546  if (FLAG_replace_str) {
547    uptr from_size = REAL(strlen)(from) + 1;
548    CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
549    ASAN_READ_RANGE(from, from_size);
550    ASAN_WRITE_RANGE(to, from_size);
551  }
552  return REAL(strcpy)(to, from);  // NOLINT
553}
554
555INTERCEPTOR(char*, strdup, const char *s) {
556  ENSURE_ASAN_INITED();
557  if (FLAG_replace_str) {
558    uptr length = REAL(strlen)(s);
559    ASAN_READ_RANGE(s, length + 1);
560  }
561  return REAL(strdup)(s);
562}
563
564INTERCEPTOR(uptr, strlen, const char *s) {
565  // strlen is called from malloc_default_purgeable_zone()
566  // in __asan::ReplaceSystemAlloc() on Mac.
567  if (asan_init_is_running) {
568    return REAL(strlen)(s);
569  }
570  ENSURE_ASAN_INITED();
571  uptr length = REAL(strlen)(s);
572  if (FLAG_replace_str) {
573    ASAN_READ_RANGE(s, length + 1);
574  }
575  return length;
576}
577
578INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, uptr n) {
579  ENSURE_ASAN_INITED();
580  unsigned char c1 = 0, c2 = 0;
581  uptr i;
582  for (i = 0; i < n; i++) {
583    c1 = (unsigned char)s1[i];
584    c2 = (unsigned char)s2[i];
585    if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
586  }
587  ASAN_READ_RANGE(s1, Min(i + 1, n));
588  ASAN_READ_RANGE(s2, Min(i + 1, n));
589  return CharCaseCmp(c1, c2);
590}
591
592INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
593  // strncmp is called from malloc_default_purgeable_zone()
594  // in __asan::ReplaceSystemAlloc() on Mac.
595  if (asan_init_is_running) {
596    return REAL(strncmp)(s1, s2, size);
597  }
598  unsigned char c1 = 0, c2 = 0;
599  uptr i;
600  for (i = 0; i < size; i++) {
601    c1 = (unsigned char)s1[i];
602    c2 = (unsigned char)s2[i];
603    if (c1 != c2 || c1 == '\0') break;
604  }
605  ASAN_READ_RANGE(s1, Min(i + 1, size));
606  ASAN_READ_RANGE(s2, Min(i + 1, size));
607  return CharCmp(c1, c2);
608}
609
610INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
611  ENSURE_ASAN_INITED();
612  if (FLAG_replace_str) {
613    uptr from_size = Min(size, internal_strnlen(from, size) + 1);
614    CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
615    ASAN_READ_RANGE(from, from_size);
616    ASAN_WRITE_RANGE(to, size);
617  }
618  return REAL(strncpy)(to, from, size);
619}
620
621#if ASAN_INTERCEPT_STRNLEN
622INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) {
623  ENSURE_ASAN_INITED();
624  uptr length = REAL(strnlen)(s, maxlen);
625  if (FLAG_replace_str) {
626    ASAN_READ_RANGE(s, Min(length + 1, maxlen));
627  }
628  return length;
629}
630#endif  // ASAN_INTERCEPT_STRNLEN
631
632static inline bool IsValidStrtolBase(int base) {
633  return (base == 0) || (2 <= base && base <= 36);
634}
635
636static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
637  CHECK(endptr != 0);
638  if (nptr == *endptr) {
639    // No digits were found at strtol call, we need to find out the last
640    // symbol accessed by strtoll on our own.
641    // We get this symbol by skipping leading blanks and optional +/- sign.
642    while (IsSpace(*nptr)) nptr++;
643    if (*nptr == '+' || *nptr == '-') nptr++;
644    *endptr = (char*)nptr;
645  }
646  CHECK(*endptr >= nptr);
647}
648
649INTERCEPTOR(long, strtol, const char *nptr,  // NOLINT
650            char **endptr, int base) {
651  ENSURE_ASAN_INITED();
652  if (!FLAG_replace_str) {
653    return REAL(strtol)(nptr, endptr, base);
654  }
655  char *real_endptr;
656  long result = REAL(strtol)(nptr, &real_endptr, base);  // NOLINT
657  if (endptr != 0) {
658    *endptr = real_endptr;
659  }
660  if (IsValidStrtolBase(base)) {
661    FixRealStrtolEndptr(nptr, &real_endptr);
662    ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
663  }
664  return result;
665}
666
667INTERCEPTOR(int, atoi, const char *nptr) {
668  ENSURE_ASAN_INITED();
669  if (!FLAG_replace_str) {
670    return REAL(atoi)(nptr);
671  }
672  char *real_endptr;
673  // "man atoi" tells that behavior of atoi(nptr) is the same as
674  // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
675  // parsed integer can't be stored in *long* type (even if it's
676  // different from int). So, we just imitate this behavior.
677  int result = REAL(strtol)(nptr, &real_endptr, 10);
678  FixRealStrtolEndptr(nptr, &real_endptr);
679  ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
680  return result;
681}
682
683INTERCEPTOR(long, atol, const char *nptr) {  // NOLINT
684  ENSURE_ASAN_INITED();
685  if (!FLAG_replace_str) {
686    return REAL(atol)(nptr);
687  }
688  char *real_endptr;
689  long result = REAL(strtol)(nptr, &real_endptr, 10);  // NOLINT
690  FixRealStrtolEndptr(nptr, &real_endptr);
691  ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
692  return result;
693}
694
695#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
696INTERCEPTOR(long long, strtoll, const char *nptr,  // NOLINT
697            char **endptr, int base) {
698  ENSURE_ASAN_INITED();
699  if (!FLAG_replace_str) {
700    return REAL(strtoll)(nptr, endptr, base);
701  }
702  char *real_endptr;
703  long long result = REAL(strtoll)(nptr, &real_endptr, base);  // NOLINT
704  if (endptr != 0) {
705    *endptr = real_endptr;
706  }
707  // If base has unsupported value, strtoll can exit with EINVAL
708  // without reading any characters. So do additional checks only
709  // if base is valid.
710  if (IsValidStrtolBase(base)) {
711    FixRealStrtolEndptr(nptr, &real_endptr);
712    ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
713  }
714  return result;
715}
716
717INTERCEPTOR(long long, atoll, const char *nptr) {  // NOLINT
718  ENSURE_ASAN_INITED();
719  if (!FLAG_replace_str) {
720    return REAL(atoll)(nptr);
721  }
722  char *real_endptr;
723  long long result = REAL(strtoll)(nptr, &real_endptr, 10);  // NOLINT
724  FixRealStrtolEndptr(nptr, &real_endptr);
725  ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
726  return result;
727}
728#endif  // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
729
730#define ASAN_INTERCEPT_FUNC(name) do { \
731      if (!INTERCEPT_FUNCTION(name) && FLAG_v > 0) \
732        Report("AddressSanitizer: failed to intercept '" #name "'\n"); \
733    } while (0)
734
735#if defined(_WIN32)
736INTERCEPTOR_WINAPI(DWORD, CreateThread,
737                   void* security, uptr stack_size,
738                   DWORD (__stdcall *start_routine)(void*), void* arg,
739                   DWORD flags, void* tid) {
740  GET_STACK_TRACE_HERE(kStackTraceMax);
741  int current_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
742  AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack);
743  asanThreadRegistry().RegisterThread(t);
744  return REAL(CreateThread)(security, stack_size,
745                            asan_thread_start, t, flags, tid);
746}
747
748namespace __asan {
749void InitializeWindowsInterceptors() {
750  ASAN_INTERCEPT_FUNC(CreateThread);
751}
752
753}  // namespace __asan
754#endif
755
756// ---------------------- InitializeAsanInterceptors ---------------- {{{1
757namespace __asan {
758void InitializeAsanInterceptors() {
759  static bool was_called_once;
760  CHECK(was_called_once == false);
761  was_called_once = true;
762  // Intercept mem* functions.
763  ASAN_INTERCEPT_FUNC(memcmp);
764  ASAN_INTERCEPT_FUNC(memmove);
765  ASAN_INTERCEPT_FUNC(memset);
766  if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
767    ASAN_INTERCEPT_FUNC(memcpy);
768  } else {
769    REAL(memcpy) = REAL(memmove);
770  }
771
772  // Intercept str* functions.
773  ASAN_INTERCEPT_FUNC(strcat);  // NOLINT
774  ASAN_INTERCEPT_FUNC(strchr);
775  ASAN_INTERCEPT_FUNC(strcmp);
776  ASAN_INTERCEPT_FUNC(strcpy);  // NOLINT
777  ASAN_INTERCEPT_FUNC(strlen);
778  ASAN_INTERCEPT_FUNC(strncmp);
779  ASAN_INTERCEPT_FUNC(strncpy);
780#if !defined(_WIN32)
781  ASAN_INTERCEPT_FUNC(strcasecmp);
782  ASAN_INTERCEPT_FUNC(strdup);
783  ASAN_INTERCEPT_FUNC(strncasecmp);
784# ifndef __APPLE__
785  ASAN_INTERCEPT_FUNC(index);
786# else
787  CHECK(OVERRIDE_FUNCTION(index, WRAP(strchr)));
788# endif
789#endif
790#if ASAN_INTERCEPT_STRNLEN
791  ASAN_INTERCEPT_FUNC(strnlen);
792#endif
793
794  ASAN_INTERCEPT_FUNC(atoi);
795  ASAN_INTERCEPT_FUNC(atol);
796  ASAN_INTERCEPT_FUNC(strtol);
797#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
798  ASAN_INTERCEPT_FUNC(atoll);
799  ASAN_INTERCEPT_FUNC(strtoll);
800#endif
801
802  // Intecept signal- and jump-related functions.
803  ASAN_INTERCEPT_FUNC(longjmp);
804#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
805  ASAN_INTERCEPT_FUNC(sigaction);
806  ASAN_INTERCEPT_FUNC(signal);
807#endif
808
809#if !defined(_WIN32)
810  ASAN_INTERCEPT_FUNC(_longjmp);
811  INTERCEPT_FUNCTION(__cxa_throw);
812# if !defined(__APPLE__)
813  // On Darwin siglongjmp tailcalls longjmp, so we don't want to intercept it
814  // there.
815  ASAN_INTERCEPT_FUNC(siglongjmp);
816# endif
817#endif
818
819  // Intercept threading-related functions
820#if !defined(_WIN32)
821  ASAN_INTERCEPT_FUNC(pthread_create);
822#endif
823
824  // Some Windows-specific interceptors.
825#if defined(_WIN32)
826  InitializeWindowsInterceptors();
827#endif
828
829  // Some Mac-specific interceptors.
830#if defined(__APPLE__)
831  InitializeMacInterceptors();
832#endif
833
834  if (FLAG_v > 0) {
835    Report("AddressSanitizer: libc interceptors initialized\n");
836  }
837}
838
839}  // namespace __asan
840