xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/asan/asan_interceptors.h (revision a7c257b03e4462df2b1020128fb82716512d7856)
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 "asan_interceptors_memintrinsics.h"
19 #include "interception/interception.h"
20 #include "sanitizer_common/sanitizer_platform_interceptors.h"
21 
22 namespace __asan {
23 
24 void InitializeAsanInterceptors();
25 void InitializePlatformInterceptors();
26 
27 #define ENSURE_ASAN_INITED()      \
28   do {                            \
29     CHECK(!asan_init_is_running); \
30     if (UNLIKELY(!asan_inited)) { \
31       AsanInitFromRtl();          \
32     }                             \
33   } while (0)
34 
35 }  // namespace __asan
36 
37 // There is no general interception at all on Fuchsia and RTEMS.
38 // Only the functions in asan_interceptors_memintrinsics.h are
39 // really defined to replace libc functions.
40 #if !SANITIZER_FUCHSIA && !SANITIZER_RTEMS
41 
42 // Use macro to describe if specific function should be
43 // intercepted on a given platform.
44 #if !SANITIZER_WINDOWS
45 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 1
46 # define ASAN_INTERCEPT__LONGJMP 1
47 # define ASAN_INTERCEPT_INDEX 1
48 # define ASAN_INTERCEPT_PTHREAD_CREATE 1
49 #else
50 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 0
51 # define ASAN_INTERCEPT__LONGJMP 0
52 # define ASAN_INTERCEPT_INDEX 0
53 # define ASAN_INTERCEPT_PTHREAD_CREATE 0
54 #endif
55 
56 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
57     SANITIZER_SOLARIS
58 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1
59 #else
60 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0
61 #endif
62 
63 #if (SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_SOLARIS
64 # define ASAN_INTERCEPT_SWAPCONTEXT 1
65 #else
66 # define ASAN_INTERCEPT_SWAPCONTEXT 0
67 #endif
68 
69 #if !SANITIZER_WINDOWS
70 # define ASAN_INTERCEPT_SIGLONGJMP 1
71 #else
72 # define ASAN_INTERCEPT_SIGLONGJMP 0
73 #endif
74 
75 #if SANITIZER_LINUX && !SANITIZER_ANDROID
76 # define ASAN_INTERCEPT___LONGJMP_CHK 1
77 #else
78 # define ASAN_INTERCEPT___LONGJMP_CHK 0
79 #endif
80 
81 #if ASAN_HAS_EXCEPTIONS && !SANITIZER_WINDOWS && !SANITIZER_SOLARIS && \
82     !SANITIZER_NETBSD
83 # define ASAN_INTERCEPT___CXA_THROW 1
84 # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 1
85 # if defined(_GLIBCXX_SJLJ_EXCEPTIONS) || (SANITIZER_IOS && defined(__arm__))
86 #  define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 1
87 # else
88 #  define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 1
89 # endif
90 #else
91 # define ASAN_INTERCEPT___CXA_THROW 0
92 # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 0
93 # define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 0
94 # define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 0
95 #endif
96 
97 #if !SANITIZER_WINDOWS
98 # define ASAN_INTERCEPT___CXA_ATEXIT 1
99 #else
100 # define ASAN_INTERCEPT___CXA_ATEXIT 0
101 #endif
102 
103 #if SANITIZER_LINUX && !SANITIZER_ANDROID
104 # define ASAN_INTERCEPT___STRDUP 1
105 #else
106 # define ASAN_INTERCEPT___STRDUP 0
107 #endif
108 
109 DECLARE_REAL(int, memcmp, const void *a1, const void *a2, uptr size)
110 DECLARE_REAL(char*, strchr, const char *str, int c)
111 DECLARE_REAL(SIZE_T, strlen, const char *s)
112 DECLARE_REAL(char*, strncpy, char *to, const char *from, uptr size)
113 DECLARE_REAL(uptr, strnlen, const char *s, uptr maxlen)
114 DECLARE_REAL(char*, strstr, const char *s1, const char *s2)
115 
116 #if !SANITIZER_MAC
117 #define ASAN_INTERCEPT_FUNC(name)                                        \
118   do {                                                                   \
119     if ((!INTERCEPT_FUNCTION(name) || !REAL(name)))                      \
120       VReport(1, "AddressSanitizer: failed to intercept '" #name "'\n"); \
121   } while (0)
122 #define ASAN_INTERCEPT_FUNC_VER(name, ver)                                     \
123   do {                                                                         \
124     if ((!INTERCEPT_FUNCTION_VER(name, ver) || !REAL(name)))                   \
125       VReport(                                                                 \
126           1, "AddressSanitizer: failed to intercept '" #name "@@" #ver "'\n"); \
127   } while (0)
128 #else
129 // OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
130 #define ASAN_INTERCEPT_FUNC(name)
131 #endif  // SANITIZER_MAC
132 
133 #endif  // !SANITIZER_FUCHSIA
134 
135 #endif  // ASAN_INTERCEPTORS_H
136