xref: /freebsd/contrib/llvm-project/compiler-rt/lib/asan/asan_interceptors.h (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 //===-- asan_interceptors.h -------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of AddressSanitizer, an address sanity checker.
10 //
11 // ASan-private header for asan_interceptors.cpp
12 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_INTERCEPTORS_H
14 #define ASAN_INTERCEPTORS_H
15 
16 #include "asan_internal.h"
17 #include "asan_interceptors_memintrinsics.h"
18 #include "interception/interception.h"
19 #include "sanitizer_common/sanitizer_platform_interceptors.h"
20 
21 namespace __asan {
22 
23 void InitializeAsanInterceptors();
24 void InitializePlatformInterceptors();
25 
26 #define ENSURE_ASAN_INITED()      \
27   do {                            \
28     CHECK(!asan_init_is_running); \
29     if (UNLIKELY(!asan_inited)) { \
30       AsanInitFromRtl();          \
31     }                             \
32   } while (0)
33 
34 }  // namespace __asan
35 
36 // There is no general interception at all on Fuchsia and RTEMS.
37 // Only the functions in asan_interceptors_memintrinsics.h are
38 // really defined to replace libc functions.
39 #if !SANITIZER_FUCHSIA && !SANITIZER_RTEMS
40 
41 // Use macro to describe if specific function should be
42 // intercepted on a given platform.
43 #if !SANITIZER_WINDOWS
44 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 1
45 # define ASAN_INTERCEPT__LONGJMP 1
46 # define ASAN_INTERCEPT_INDEX 1
47 # define ASAN_INTERCEPT_PTHREAD_CREATE 1
48 #else
49 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 0
50 # define ASAN_INTERCEPT__LONGJMP 0
51 # define ASAN_INTERCEPT_INDEX 0
52 # define ASAN_INTERCEPT_PTHREAD_CREATE 0
53 #endif
54 
55 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
56     SANITIZER_SOLARIS
57 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1
58 #else
59 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0
60 #endif
61 
62 #if (SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_SOLARIS
63 # define ASAN_INTERCEPT_SWAPCONTEXT 1
64 #else
65 # define ASAN_INTERCEPT_SWAPCONTEXT 0
66 #endif
67 
68 #if !SANITIZER_WINDOWS
69 # define ASAN_INTERCEPT_SIGLONGJMP 1
70 #else
71 # define ASAN_INTERCEPT_SIGLONGJMP 0
72 #endif
73 
74 #if SANITIZER_LINUX && !SANITIZER_ANDROID
75 # define ASAN_INTERCEPT___LONGJMP_CHK 1
76 #else
77 # define ASAN_INTERCEPT___LONGJMP_CHK 0
78 #endif
79 
80 #if ASAN_HAS_EXCEPTIONS && !SANITIZER_WINDOWS && !SANITIZER_SOLARIS && \
81     !SANITIZER_NETBSD
82 # define ASAN_INTERCEPT___CXA_THROW 1
83 # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 1
84 # if defined(_GLIBCXX_SJLJ_EXCEPTIONS) || (SANITIZER_IOS && defined(__arm__))
85 #  define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 1
86 # else
87 #  define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 1
88 # endif
89 #else
90 # define ASAN_INTERCEPT___CXA_THROW 0
91 # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 0
92 # define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 0
93 # define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 0
94 #endif
95 
96 #if !SANITIZER_WINDOWS
97 # define ASAN_INTERCEPT___CXA_ATEXIT 1
98 #else
99 # define ASAN_INTERCEPT___CXA_ATEXIT 0
100 #endif
101 
102 #if SANITIZER_NETBSD
103 # define ASAN_INTERCEPT_ATEXIT 1
104 #else
105 # define ASAN_INTERCEPT_ATEXIT 0
106 #endif
107 
108 #if SANITIZER_LINUX && !SANITIZER_ANDROID
109 # define ASAN_INTERCEPT___STRDUP 1
110 #else
111 # define ASAN_INTERCEPT___STRDUP 0
112 #endif
113 
114 #if SANITIZER_LINUX && (defined(__arm__) || defined(__aarch64__) || \
115                         defined(__i386__) || defined(__x86_64__))
116 # define ASAN_INTERCEPT_VFORK 1
117 #else
118 # define ASAN_INTERCEPT_VFORK 0
119 #endif
120 
121 #if SANITIZER_NETBSD
122 # define ASAN_INTERCEPT_PTHREAD_ATFORK 1
123 #else
124 # define ASAN_INTERCEPT_PTHREAD_ATFORK 0
125 #endif
126 
127 DECLARE_REAL(int, memcmp, const void *a1, const void *a2, uptr size)
128 DECLARE_REAL(char*, strchr, const char *str, int c)
129 DECLARE_REAL(SIZE_T, strlen, const char *s)
130 DECLARE_REAL(char*, strncpy, char *to, const char *from, uptr size)
131 DECLARE_REAL(uptr, strnlen, const char *s, uptr maxlen)
132 DECLARE_REAL(char*, strstr, const char *s1, const char *s2)
133 
134 #if !SANITIZER_MAC
135 #define ASAN_INTERCEPT_FUNC(name)                                         \
136   do {                                                                    \
137     if (!INTERCEPT_FUNCTION(name))                                        \
138       VReport(1, "AddressSanitizer: failed to intercept '%s'\n'", #name); \
139   } while (0)
140 #define ASAN_INTERCEPT_FUNC_VER(name, ver)                                  \
141   do {                                                                      \
142     if (!INTERCEPT_FUNCTION_VER(name, ver))                                 \
143       VReport(1, "AddressSanitizer: failed to intercept '%s@@%s'\n", #name, \
144               #ver);                                                        \
145   } while (0)
146 #else
147 // OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
148 #define ASAN_INTERCEPT_FUNC(name)
149 #endif  // SANITIZER_MAC
150 
151 #endif  // !SANITIZER_FUCHSIA
152 
153 #endif  // ASAN_INTERCEPTORS_H
154