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