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