1 //===-- asan_interceptors.h -------------------------------------*- 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 // ASan-private header for asan_interceptors.cc
13 //===----------------------------------------------------------------------===//
14 #ifndef ASAN_INTERCEPTORS_H
15 #define ASAN_INTERCEPTORS_H
16 
17 #include "asan_internal.h"
18 #include "interception/interception.h"
19 #include "sanitizer_common/sanitizer_platform_interceptors.h"
20 
21 // Use macro to describe if specific function should be
22 // intercepted on a given platform.
23 #if !SANITIZER_WINDOWS
24 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 1
25 # define ASAN_INTERCEPT__LONGJMP 1
26 # define ASAN_INTERCEPT_INDEX 1
27 # define ASAN_INTERCEPT_PTHREAD_CREATE 1
28 # define ASAN_INTERCEPT_FORK 1
29 #else
30 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 0
31 # define ASAN_INTERCEPT__LONGJMP 0
32 # define ASAN_INTERCEPT_INDEX 0
33 # define ASAN_INTERCEPT_PTHREAD_CREATE 0
34 # define ASAN_INTERCEPT_FORK 0
35 #endif
36 
37 #if SANITIZER_FREEBSD || SANITIZER_LINUX
38 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1
39 #else
40 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0
41 #endif
42 
43 #if SANITIZER_LINUX && !SANITIZER_ANDROID
44 # define ASAN_INTERCEPT_SWAPCONTEXT 1
45 #else
46 # define ASAN_INTERCEPT_SWAPCONTEXT 0
47 #endif
48 
49 #if !SANITIZER_WINDOWS
50 # define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 1
51 #else
52 # define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 0
53 #endif
54 
55 #if !SANITIZER_WINDOWS
56 # define ASAN_INTERCEPT_SIGLONGJMP 1
57 #else
58 # define ASAN_INTERCEPT_SIGLONGJMP 0
59 #endif
60 
61 // Android bug: https://code.google.com/p/android/issues/detail?id=61799
62 #if ASAN_HAS_EXCEPTIONS && !SANITIZER_WINDOWS && \
63     !(SANITIZER_ANDROID && defined(__i386))
64 # define ASAN_INTERCEPT___CXA_THROW 1
65 #else
66 # define ASAN_INTERCEPT___CXA_THROW 0
67 #endif
68 
69 #if !SANITIZER_WINDOWS
70 # define ASAN_INTERCEPT___CXA_ATEXIT 1
71 #else
72 # define ASAN_INTERCEPT___CXA_ATEXIT 0
73 #endif
74 
75 #if SANITIZER_LINUX && !SANITIZER_ANDROID
76 # define ASAN_INTERCEPT___STRDUP 1
77 #else
78 # define ASAN_INTERCEPT___STRDUP 0
79 #endif
80 
81 DECLARE_REAL(int, memcmp, const void *a1, const void *a2, uptr size)
82 DECLARE_REAL(void*, memcpy, void *to, const void *from, uptr size)
83 DECLARE_REAL(void*, memset, void *block, int c, uptr size)
84 DECLARE_REAL(char*, strchr, const char *str, int c)
85 DECLARE_REAL(SIZE_T, strlen, const char *s)
86 DECLARE_REAL(char*, strncpy, char *to, const char *from, uptr size)
87 DECLARE_REAL(uptr, strnlen, const char *s, uptr maxlen)
88 DECLARE_REAL(char*, strstr, const char *s1, const char *s2)
89 struct sigaction;
DECLARE_REAL(int,sigaction,int signum,const struct sigaction * act,struct sigaction * oldact)90 DECLARE_REAL(int, sigaction, int signum, const struct sigaction *act,
91                              struct sigaction *oldact)
92 
93 #if !SANITIZER_MAC
94 #define ASAN_INTERCEPT_FUNC(name)                                        \
95   do {                                                                   \
96     if ((!INTERCEPT_FUNCTION(name) || !REAL(name)))                      \
97       VReport(1, "AddressSanitizer: failed to intercept '" #name "'\n"); \
98   } while (0)
99 #define ASAN_INTERCEPT_FUNC_VER(name, ver)                                     \
100   do {                                                                         \
101     if ((!INTERCEPT_FUNCTION_VER(name, ver) || !REAL(name)))                   \
102       VReport(                                                                 \
103           1, "AddressSanitizer: failed to intercept '" #name "@@" #ver "'\n"); \
104   } while (0)
105 #else
106 // OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
107 #define ASAN_INTERCEPT_FUNC(name)
108 #endif  // SANITIZER_MAC
109 
110 namespace __asan {
111 
112 void InitializeAsanInterceptors();
113 void InitializePlatformInterceptors();
114 
115 #define ENSURE_ASAN_INITED() do { \
116   CHECK(!asan_init_is_running); \
117   if (UNLIKELY(!asan_inited)) { \
118     AsanInitFromRtl(); \
119   } \
120 } while (0)
121 
122 }  // namespace __asan
123 
124 #endif  // ASAN_INTERCEPTORS_H
125