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