asan_interceptors.cc revision 4e7abdcc8ed92a56561ed34f36295c6d01204e5d
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#include <new>
26
27#if defined(__APPLE__)
28// FIXME(samsonov): Gradually replace system headers with declarations of
29// intercepted functions.
30#include <pthread.h>
31#include <string.h>
32#endif  // __APPLE__
33
34// Use extern declarations of intercepted functions on Mac and Windows
35// to avoid including system headers.
36#if defined(__APPLE__) || (defined(_WIN32) && !defined(_DLL))
37extern "C" {
38// signal.h
39# if !defined(_WIN32)
40struct sigaction;
41int sigaction(int sig, const struct sigaction *act,
42              struct sigaction *oldact);
43void *signal(int signum, void *handler);
44# endif
45
46// setjmp.h
47void longjmp(void* env, int value);
48# if !defined(_WIN32)
49void _longjmp(void *env, int value);
50# endif
51
52// string.h / strings.h
53int memcmp(const void *a1, const void *a2, size_t size);
54void* memmove(void *to, const void *from, size_t size);
55void* memcpy(void *to, const void *from, size_t size);
56void* memset(void *block, int c, size_t size);
57# if defined(__APPLE__)
58char* strchr(const char *str, int c);
59# elif defined(_WIN32)
60char* strchr(const char *s, char c);
61# endif
62char* strcat(char *to, const char* from);  // NOLINT
63char* strcpy(char *to, const char* from);  // NOLINT
64char* strncpy(char *to, const char* from, size_t size);
65int strcmp(const char *s1, const char* s2);
66int strncmp(const char *s1, const char* s2, size_t size);
67# if !defined(__APPLE__)
68size_t strnlen(const char *s, size_t maxlen);
69# endif
70
71// Windows threads.
72# if defined(_WIN32)
73__declspec(dllimport)
74void* __stdcall CreateThread(void *sec, size_t st, void* start,
75                             void *arg, DWORD fl, DWORD *id);
76# endif
77}  // extern "C"
78#endif
79
80namespace __asan {
81
82// Instruments read/write access to a single byte in memory.
83// On error calls __asan_report_error, which aborts the program.
84#define ACCESS_ADDRESS(address, isWrite)   do {         \
85  if (AddressIsPoisoned(address)) {                     \
86    GET_CURRENT_PC_BP_SP;                               \
87    __asan_report_error(pc, bp, sp, address, isWrite, /* access_size */ 1); \
88  } \
89} while (0)
90
91// We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
92// and ASAN_WRITE_RANGE as macro instead of function so
93// that no extra frames are created, and stack trace contains
94// relevant information only.
95
96// Instruments read/write access to a memory range.
97// More complex implementation is possible, for now just
98// checking the first and the last byte of a range.
99#define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \
100  if (size > 0) { \
101    uintptr_t ptr = (uintptr_t)(offset); \
102    ACCESS_ADDRESS(ptr, isWrite); \
103    ACCESS_ADDRESS(ptr + (size) - 1, isWrite); \
104  } \
105} while (0)
106
107#define ASAN_READ_RANGE(offset, size) do { \
108  ACCESS_MEMORY_RANGE(offset, size, false); \
109} while (0)
110
111#define ASAN_WRITE_RANGE(offset, size) do { \
112  ACCESS_MEMORY_RANGE(offset, size, true); \
113} while (0)
114
115// Behavior of functions like "memcpy" or "strcpy" is undefined
116// if memory intervals overlap. We report error in this case.
117// Macro is used to avoid creation of new frames.
118static inline bool RangesOverlap(const char *offset1, size_t length1,
119                                 const char *offset2, size_t length2) {
120  return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
121}
122#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
123  const char *offset1 = (const char*)_offset1; \
124  const char *offset2 = (const char*)_offset2; \
125  if (RangesOverlap(offset1, length1, offset2, length2)) { \
126    Report("ERROR: AddressSanitizer %s-param-overlap: " \
127           "memory ranges [%p,%p) and [%p, %p) overlap\n", \
128           name, offset1, offset1 + length1, offset2, offset2 + length2); \
129    PRINT_CURRENT_STACK(); \
130    ShowStatsAndAbort(); \
131  } \
132} while (0)
133
134#define ENSURE_ASAN_INITED() do { \
135  CHECK(!asan_init_is_running); \
136  if (!asan_inited) { \
137    __asan_init(); \
138  } \
139} while (0)
140
141static inline bool IsSpace(int c) {
142  return (c == ' ') || (c == '\n') || (c == '\t') ||
143         (c == '\f') || (c == '\r') || (c == '\v');
144}
145
146static inline bool IsDigit(int c) {
147  return (c >= '0') && (c <= '9');
148}
149
150static inline int ToLower(int c) {
151  return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
152}
153
154// ---------------------- Internal string functions ---------------- {{{1
155
156int64_t internal_simple_strtoll(const char *nptr, char **endptr, int base) {
157  CHECK(base == 10);
158  while (IsSpace(*nptr)) nptr++;
159  int sgn = 1;
160  uint64_t res = 0;
161  bool have_digits = false;
162  char *old_nptr = (char*)nptr;
163  if (*nptr == '+') {
164    sgn = 1;
165    nptr++;
166  } else if (*nptr == '-') {
167    sgn = -1;
168    nptr++;
169  }
170  while (IsDigit(*nptr)) {
171    res = (res <= UINT64_MAX / 10) ? res * 10 : UINT64_MAX;
172    int digit = ((*nptr) - '0');
173    res = (res <= UINT64_MAX - digit) ? res + digit : UINT64_MAX;
174    have_digits = true;
175    nptr++;
176  }
177  if (endptr != NULL) {
178    *endptr = (have_digits) ? (char*)nptr : old_nptr;
179  }
180  if (sgn > 0) {
181    return (int64_t)(Min((uint64_t)INT64_MAX, res));
182  } else {
183    return (res > INT64_MAX) ? INT64_MIN : ((int64_t)res * -1);
184  }
185}
186
187int64_t internal_atoll(const char *nptr) {
188  return internal_simple_strtoll(nptr, (char**)NULL, 10);
189}
190
191size_t internal_strlen(const char *s) {
192  size_t i = 0;
193  while (s[i]) i++;
194  return i;
195}
196
197size_t internal_strnlen(const char *s, size_t maxlen) {
198#ifndef __APPLE__
199  if (REAL(strnlen) != NULL) {
200    return REAL(strnlen)(s, maxlen);
201  }
202#endif
203  size_t i = 0;
204  while (i < maxlen && s[i]) i++;
205  return i;
206}
207
208char* internal_strchr(const char *s, int c) {
209  while (true) {
210    if (*s == (char)c)
211      return (char*)s;
212    if (*s == 0)
213      return NULL;
214    s++;
215  }
216}
217
218void* internal_memchr(const void* s, int c, size_t n) {
219  const char* t = (char*)s;
220  for (size_t i = 0; i < n; ++i, ++t)
221    if (*t == c)
222      return (void*)t;
223  return NULL;
224}
225
226int internal_memcmp(const void* s1, const void* s2, size_t n) {
227  const char* t1 = (char*)s1;
228  const char* t2 = (char*)s2;
229  for (size_t i = 0; i < n; ++i, ++t1, ++t2)
230    if (*t1 != *t2)
231      return *t1 < *t2 ? -1 : 1;
232  return 0;
233}
234
235char *internal_strstr(const char *haystack, const char *needle) {
236  // This is O(N^2), but we are not using it in hot places.
237  size_t len1 = internal_strlen(haystack);
238  size_t len2 = internal_strlen(needle);
239  if (len1 < len2) return 0;
240  for (size_t pos = 0; pos <= len1 - len2; pos++) {
241    if (internal_memcmp(haystack + pos, needle, len2) == 0)
242      return (char*)haystack + pos;
243  }
244  return 0;
245}
246
247char *internal_strncat(char *dst, const char *src, size_t n) {
248  size_t len = internal_strlen(dst);
249  size_t i;
250  for (i = 0; i < n && src[i]; i++)
251    dst[len + i] = src[i];
252  dst[len + i] = 0;
253  return dst;
254}
255
256int internal_strcmp(const char *s1, const char *s2) {
257  while (true) {
258    unsigned c1 = *s1;
259    unsigned c2 = *s2;
260    if (c1 != c2) return (c1 < c2) ? -1 : 1;
261    if (c1 == 0) break;
262    s1++;
263    s2++;
264  }
265  return 0;
266}
267
268char *internal_strncpy(char *dst, const char *src, size_t n) {
269  size_t i;
270  for (i = 0; i < n && src[i]; i++)
271    dst[i] = src[i];
272  return dst;
273}
274
275}  // namespace __asan
276
277// ---------------------- Wrappers ---------------- {{{1
278using namespace __asan;  // NOLINT
279
280#define OPERATOR_NEW_BODY \
281  GET_STACK_TRACE_HERE_FOR_MALLOC;\
282  return asan_memalign(0, size, &stack);
283
284#ifdef ANDROID
285void *operator new(size_t size) { OPERATOR_NEW_BODY; }
286void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
287#else
288void *operator new(size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
289void *operator new[](size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
290void *operator new(size_t size, std::nothrow_t const&) throw()
291{ OPERATOR_NEW_BODY; }
292void *operator new[](size_t size, std::nothrow_t const&) throw()
293{ OPERATOR_NEW_BODY; }
294#endif
295
296#define OPERATOR_DELETE_BODY \
297  GET_STACK_TRACE_HERE_FOR_FREE(ptr);\
298  asan_free(ptr, &stack);
299
300void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
301void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
302void operator delete(void *ptr, std::nothrow_t const&) throw()
303{ OPERATOR_DELETE_BODY; }
304void operator delete[](void *ptr, std::nothrow_t const&) throw()
305{ OPERATOR_DELETE_BODY;}
306
307static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
308  AsanThread *t = (AsanThread*)arg;
309  asanThreadRegistry().SetCurrent(t);
310  return t->ThreadStart();
311}
312
313#ifndef _WIN32
314INTERCEPTOR(int, pthread_create, void *thread,
315    void *attr, void *(*start_routine)(void*), void *arg) {
316  GET_STACK_TRACE_HERE(kStackTraceMax);
317  int current_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
318  AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack);
319  asanThreadRegistry().RegisterThread(t);
320  return REAL(pthread_create)(thread, attr, asan_thread_start, t);
321}
322#endif  // !_WIN32
323
324#if !defined(ANDROID) && !defined(_WIN32)
325INTERCEPTOR(void*, signal, int signum, void *handler) {
326  if (!AsanInterceptsSignal(signum)) {
327    return REAL(signal)(signum, handler);
328  }
329  return NULL;
330}
331
332INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
333                            struct sigaction *oldact) {
334  if (!AsanInterceptsSignal(signum)) {
335    return REAL(sigaction)(signum, act, oldact);
336  }
337  return 0;
338}
339#endif  // !ANDROID && !_WIN32
340
341INTERCEPTOR(void, longjmp, void *env, int val) {
342  __asan_handle_no_return();
343  REAL(longjmp)(env, val);
344}
345
346#if !defined(_WIN32)
347INTERCEPTOR(void, _longjmp, void *env, int val) {
348  __asan_handle_no_return();
349  REAL(_longjmp)(env, val);
350}
351
352INTERCEPTOR(void, siglongjmp, void *env, int val) {
353  __asan_handle_no_return();
354  REAL(siglongjmp)(env, val);
355}
356#endif
357
358#if ASAN_HAS_EXCEPTIONS == 1
359#ifdef __APPLE__
360extern "C" void __cxa_throw(void *a, void *b, void *c);
361#endif  // __APPLE__
362
363INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
364  CHECK(REAL(__cxa_throw));
365  __asan_handle_no_return();
366  REAL(__cxa_throw)(a, b, c);
367}
368#endif
369
370// intercept mlock and friends.
371// Since asan maps 16T of RAM, mlock is completely unfriendly to asan.
372// All functions return 0 (success).
373static void MlockIsUnsupported() {
374  static bool printed = 0;
375  if (printed) return;
376  printed = true;
377  Printf("INFO: AddressSanitizer ignores mlock/mlockall/munlock/munlockall\n");
378}
379
380extern "C" {
381INTERCEPTOR_ATTRIBUTE
382int mlock(const void *addr, size_t len) {
383  MlockIsUnsupported();
384  return 0;
385}
386
387INTERCEPTOR_ATTRIBUTE
388int munlock(const void *addr, size_t len) {
389  MlockIsUnsupported();
390  return 0;
391}
392
393INTERCEPTOR_ATTRIBUTE
394int mlockall(int flags) {
395  MlockIsUnsupported();
396  return 0;
397}
398
399INTERCEPTOR_ATTRIBUTE
400int munlockall(void) {
401  MlockIsUnsupported();
402  return 0;
403}
404}  // extern "C"
405
406static inline int CharCmp(unsigned char c1, unsigned char c2) {
407  return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
408}
409
410static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
411  int c1_low = ToLower(c1);
412  int c2_low = ToLower(c2);
413  return c1_low - c2_low;
414}
415
416INTERCEPTOR(int, memcmp, const void *a1, const void *a2, size_t size) {
417  ENSURE_ASAN_INITED();
418  unsigned char c1 = 0, c2 = 0;
419  const unsigned char *s1 = (const unsigned char*)a1;
420  const unsigned char *s2 = (const unsigned char*)a2;
421  size_t i;
422  for (i = 0; i < size; i++) {
423    c1 = s1[i];
424    c2 = s2[i];
425    if (c1 != c2) break;
426  }
427  ASAN_READ_RANGE(s1, Min(i + 1, size));
428  ASAN_READ_RANGE(s2, Min(i + 1, size));
429  return CharCmp(c1, c2);
430}
431
432INTERCEPTOR(void*, memcpy, void *to, const void *from, size_t size) {
433  // memcpy is called during __asan_init() from the internals
434  // of printf(...).
435  if (asan_init_is_running) {
436    return REAL(memcpy)(to, from, size);
437  }
438  ENSURE_ASAN_INITED();
439  if (FLAG_replace_intrin) {
440    if (to != from) {
441      // We do not treat memcpy with to==from as a bug.
442      // See http://llvm.org/bugs/show_bug.cgi?id=11763.
443      CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
444    }
445    ASAN_WRITE_RANGE(from, size);
446    ASAN_READ_RANGE(to, size);
447  }
448  return REAL(memcpy)(to, from, size);
449}
450
451INTERCEPTOR(void*, memmove, void *to, const void *from, size_t size) {
452  ENSURE_ASAN_INITED();
453  if (FLAG_replace_intrin) {
454    ASAN_WRITE_RANGE(from, size);
455    ASAN_READ_RANGE(to, size);
456  }
457  return REAL(memmove)(to, from, size);
458}
459
460INTERCEPTOR(void*, memset, void *block, int c, size_t size) {
461  // memset is called inside INTERCEPT_FUNCTION on Mac.
462  if (asan_init_is_running) {
463    return REAL(memset)(block, c, size);
464  }
465  ENSURE_ASAN_INITED();
466  if (FLAG_replace_intrin) {
467    ASAN_WRITE_RANGE(block, size);
468  }
469  return REAL(memset)(block, c, size);
470}
471
472INTERCEPTOR(char*, strchr, const char *str, int c) {
473  ENSURE_ASAN_INITED();
474  char *result = REAL(strchr)(str, c);
475  if (FLAG_replace_str) {
476    size_t bytes_read = (result ? result - str : REAL(strlen)(str)) + 1;
477    ASAN_READ_RANGE(str, bytes_read);
478  }
479  return result;
480}
481
482#ifdef __linux__
483INTERCEPTOR(char*, index, const char *string, int c)
484  ALIAS(WRAPPER_NAME(strchr));
485#else
486DEFINE_REAL(char*, index, const char *string, int c);
487#endif
488
489#ifdef ANDROID
490DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
491    struct sigaction *oldact);
492#endif
493
494INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
495  ENSURE_ASAN_INITED();
496  unsigned char c1, c2;
497  size_t 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    size_t from_length = REAL(strlen)(from);
512    ASAN_READ_RANGE(from, from_length + 1);
513    if (from_length > 0) {
514      size_t 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  size_t 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    size_t 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    size_t length = REAL(strlen)(s);
559    ASAN_READ_RANGE(s, length + 1);
560  }
561  return REAL(strdup)(s);
562}
563
564INTERCEPTOR(size_t, 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  size_t 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, size_t n) {
579  ENSURE_ASAN_INITED();
580  unsigned char c1 = 0, c2 = 0;
581  size_t 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, size_t 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  size_t 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, size_t size) {
611  ENSURE_ASAN_INITED();
612  if (FLAG_replace_str) {
613    size_t 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#ifndef __APPLE__
622INTERCEPTOR(size_t, strnlen, const char *s, size_t maxlen) {
623  ENSURE_ASAN_INITED();
624  size_t 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
631
632#if defined(_WIN32)
633INTERCEPTOR_WINAPI(DWORD, CreateThread,
634                   void* security, size_t stack_size,
635                   DWORD (__stdcall *start_routine)(void*), void* arg,
636                   DWORD flags, void* tid) {
637  GET_STACK_TRACE_HERE(kStackTraceMax);
638  int current_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
639  AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack);
640  asanThreadRegistry().RegisterThread(t);
641  return REAL(CreateThread)(security, stack_size,
642                            asan_thread_start, t, flags, tid);
643}
644
645namespace __asan {
646void InitializeWindowsInterceptors() {
647  CHECK(INTERCEPT_FUNCTION(CreateThread));
648}
649
650}  // namespace __asan
651#endif
652
653// ---------------------- InitializeAsanInterceptors ---------------- {{{1
654namespace __asan {
655void InitializeAsanInterceptors() {
656  static bool was_called_once;
657  CHECK(was_called_once == false);
658  was_called_once = true;
659  // Intercept mem* functions.
660  CHECK(INTERCEPT_FUNCTION(memcmp));
661  CHECK(INTERCEPT_FUNCTION(memmove));
662  CHECK(INTERCEPT_FUNCTION(memset));
663  if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
664    CHECK(INTERCEPT_FUNCTION(memcpy));
665  } else {
666    REAL(memcpy) = REAL(memmove);
667  }
668
669  // Intercept str* functions.
670  CHECK(INTERCEPT_FUNCTION(strcat));  // NOLINT
671  CHECK(INTERCEPT_FUNCTION(strchr));
672  CHECK(INTERCEPT_FUNCTION(strcmp));
673  CHECK(INTERCEPT_FUNCTION(strcpy));  // NOLINT
674  CHECK(INTERCEPT_FUNCTION(strlen));
675  CHECK(INTERCEPT_FUNCTION(strncmp));
676  CHECK(INTERCEPT_FUNCTION(strncpy));
677#if !defined(_WIN32)
678  CHECK(INTERCEPT_FUNCTION(strcasecmp));
679  CHECK(INTERCEPT_FUNCTION(strdup));
680  CHECK(INTERCEPT_FUNCTION(strncasecmp));
681# ifndef __APPLE__
682  CHECK(INTERCEPT_FUNCTION(index));
683# else
684  CHECK(OVERRIDE_FUNCTION(index, WRAP(strchr)));
685# endif
686#endif
687#if !defined(__APPLE__)
688  CHECK(INTERCEPT_FUNCTION(strnlen));
689#endif
690
691  // Intecept signal- and jump-related functions.
692  CHECK(INTERCEPT_FUNCTION(longjmp));
693#if !defined(ANDROID) && !defined(_WIN32)
694  CHECK(INTERCEPT_FUNCTION(sigaction));
695  CHECK(INTERCEPT_FUNCTION(signal));
696#endif
697
698#if !defined(_WIN32)
699  CHECK(INTERCEPT_FUNCTION(_longjmp));
700  INTERCEPT_FUNCTION(__cxa_throw);
701# if !defined(__APPLE__)
702  // On Darwin siglongjmp tailcalls longjmp, so we don't want to intercept it
703  // there.
704  CHECK(INTERCEPT_FUNCTION(siglongjmp));
705# endif
706#endif
707
708  // Intercept threading-related functions
709#if !defined(_WIN32)
710  CHECK(INTERCEPT_FUNCTION(pthread_create));
711#endif
712
713  // Some Windows-specific interceptors.
714#if defined(_WIN32)
715  InitializeWindowsInterceptors();
716#endif
717
718  // Some Mac-specific interceptors.
719#if defined(__APPLE__)
720  InitializeMacInterceptors();
721#endif
722
723  if (FLAG_v > 0) {
724    Report("AddressSanitizer: libc interceptors initialized\n");
725  }
726}
727
728}  // namespace __asan
729