Update LOCAL_PATCHES after libsanitizer merge.
[official-gcc.git] / libsanitizer / interception / interception.h
blob3d43df804f34b6d2094b77f3d562d2a9b76f94cf
1 //===-- interception.h ------------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // Machinery for providing replacements/wrappers for system functions.
11 //===----------------------------------------------------------------------===//
13 #ifndef INTERCEPTION_H
14 #define INTERCEPTION_H
16 #include "sanitizer_common/sanitizer_internal_defs.h"
18 #if !SANITIZER_LINUX && !SANITIZER_FREEBSD && !SANITIZER_MAC && \
19 !SANITIZER_NETBSD && !SANITIZER_OPENBSD && !SANITIZER_WINDOWS && \
20 !SANITIZER_FUCHSIA && !SANITIZER_RTEMS && !SANITIZER_SOLARIS
21 # error "Interception doesn't work on this operating system."
22 #endif
24 // These typedefs should be used only in the interceptor definitions to replace
25 // the standard system types (e.g. SSIZE_T instead of ssize_t)
26 typedef __sanitizer::uptr SIZE_T;
27 typedef __sanitizer::sptr SSIZE_T;
28 typedef __sanitizer::sptr PTRDIFF_T;
29 typedef __sanitizer::s64 INTMAX_T;
30 typedef __sanitizer::u64 UINTMAX_T;
31 typedef __sanitizer::OFF_T OFF_T;
32 typedef __sanitizer::OFF64_T OFF64_T;
34 // How to add an interceptor:
35 // Suppose you need to wrap/replace system function (generally, from libc):
36 // int foo(const char *bar, double baz);
37 // You'll need to:
38 // 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
39 // your source file. See the notes below for cases when
40 // INTERCEPTOR_WITH_SUFFIX(...) should be used instead.
41 // 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
42 // INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
43 // intercepted successfully.
44 // You can access original function by calling REAL(foo)(bar, baz).
45 // By default, REAL(foo) will be visible only inside your interceptor, and if
46 // you want to use it in other parts of RTL, you'll need to:
47 // 3a) add DECLARE_REAL(int, foo, const char*, double) to a
48 // header file.
49 // However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
50 // INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
51 // 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
52 // to a header file.
54 // Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or
55 // DECLARE_REAL(...) are located inside namespaces.
56 // 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to
57 // effectively redirect calls from "foo" to "zoo". In this case
58 // you aren't required to implement
59 // INTERCEPTOR(int, foo, const char *bar, double baz) {...}
60 // but instead you'll have to add
61 // DECLARE_REAL(int, foo, const char *bar, double baz) in your
62 // source file (to define a pointer to overriden function).
63 // 3. Some Mac functions have symbol variants discriminated by
64 // additional suffixes, e.g. _$UNIX2003 (see
65 // https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html
66 // for more details). To intercept such functions you need to use the
67 // INTERCEPTOR_WITH_SUFFIX(...) macro.
69 // How it works:
70 // To replace system functions on Linux we just need to declare functions
71 // with same names in our library and then obtain the real function pointers
72 // using dlsym().
73 // There is one complication. A user may also intercept some of the functions
74 // we intercept. To resolve this we declare our interceptors with __interceptor_
75 // prefix, and then make actual interceptors weak aliases to __interceptor_
76 // functions.
78 // This is not so on Mac OS, where the two-level namespace makes
79 // our replacement functions invisible to other libraries. This may be overcomed
80 // using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
81 // libraries in Chromium were noticed when doing so.
82 // Instead we create a dylib containing a __DATA,__interpose section that
83 // associates library functions with their wrappers. When this dylib is
84 // preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all
85 // the calls to interposed functions done through stubs to the wrapper
86 // functions.
87 // As it's decided at compile time which functions are to be intercepted on Mac,
88 // INTERCEPT_FUNCTION() is effectively a no-op on this system.
90 #if SANITIZER_MAC
91 #include <sys/cdefs.h> // For __DARWIN_ALIAS_C().
93 // Just a pair of pointers.
94 struct interpose_substitution {
95 const __sanitizer::uptr replacement;
96 const __sanitizer::uptr original;
99 // For a function foo() create a global pair of pointers { wrap_foo, foo } in
100 // the __DATA,__interpose section.
101 // As a result all the calls to foo() will be routed to wrap_foo() at runtime.
102 #define INTERPOSER(func_name) __attribute__((used)) \
103 const interpose_substitution substitution_##func_name[] \
104 __attribute__((section("__DATA, __interpose"))) = { \
105 { reinterpret_cast<const uptr>(WRAP(func_name)), \
106 reinterpret_cast<const uptr>(func_name) } \
109 // For a function foo() and a wrapper function bar() create a global pair
110 // of pointers { bar, foo } in the __DATA,__interpose section.
111 // As a result all the calls to foo() will be routed to bar() at runtime.
112 #define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \
113 const interpose_substitution substitution_##func_name[] \
114 __attribute__((section("__DATA, __interpose"))) = { \
115 { reinterpret_cast<const uptr>(wrapper_name), \
116 reinterpret_cast<const uptr>(func_name) } \
119 # define WRAP(x) wrap_##x
120 # define WRAPPER_NAME(x) "wrap_"#x
121 # define INTERCEPTOR_ATTRIBUTE
122 # define DECLARE_WRAPPER(ret_type, func, ...)
124 #elif SANITIZER_WINDOWS
125 # define WRAP(x) __asan_wrap_##x
126 # define WRAPPER_NAME(x) "__asan_wrap_"#x
127 # define INTERCEPTOR_ATTRIBUTE __declspec(dllexport)
128 # define DECLARE_WRAPPER(ret_type, func, ...) \
129 extern "C" ret_type func(__VA_ARGS__);
130 # define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \
131 extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__);
132 #elif SANITIZER_RTEMS
133 # define WRAP(x) x
134 # define WRAPPER_NAME(x) #x
135 # define INTERCEPTOR_ATTRIBUTE
136 # define DECLARE_WRAPPER(ret_type, func, ...)
137 #elif SANITIZER_FREEBSD || SANITIZER_NETBSD
138 # define WRAP(x) __interceptor_ ## x
139 # define WRAPPER_NAME(x) "__interceptor_" #x
140 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
141 // FreeBSD's dynamic linker (incompliantly) gives non-weak symbols higher
142 // priority than weak ones so weak aliases won't work for indirect calls
143 // in position-independent (-fPIC / -fPIE) mode.
144 # define DECLARE_WRAPPER(ret_type, func, ...) \
145 extern "C" ret_type func(__VA_ARGS__) \
146 __attribute__((alias("__interceptor_" #func), visibility("default")));
147 #elif !SANITIZER_FUCHSIA
148 # define WRAP(x) __interceptor_ ## x
149 # define WRAPPER_NAME(x) "__interceptor_" #x
150 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
151 # define DECLARE_WRAPPER(ret_type, func, ...) \
152 extern "C" ret_type func(__VA_ARGS__) \
153 __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
154 #endif
156 #if SANITIZER_FUCHSIA
157 // There is no general interception at all on Fuchsia.
158 // Sanitizer runtimes just define functions directly to preempt them,
159 // and have bespoke ways to access the underlying libc functions.
160 # include <zircon/sanitizer.h>
161 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
162 # define REAL(x) __unsanitized_##x
163 # define DECLARE_REAL(ret_type, func, ...)
164 #elif SANITIZER_RTEMS
165 # define REAL(x) __real_ ## x
166 # define DECLARE_REAL(ret_type, func, ...) \
167 extern "C" ret_type REAL(func)(__VA_ARGS__);
168 #elif !SANITIZER_MAC
169 # define PTR_TO_REAL(x) real_##x
170 # define REAL(x) __interception::PTR_TO_REAL(x)
171 # define FUNC_TYPE(x) x##_type
173 # define DECLARE_REAL(ret_type, func, ...) \
174 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
175 namespace __interception { \
176 extern FUNC_TYPE(func) PTR_TO_REAL(func); \
178 # define ASSIGN_REAL(dst, src) REAL(dst) = REAL(src)
179 #else // SANITIZER_MAC
180 # define REAL(x) x
181 # define DECLARE_REAL(ret_type, func, ...) \
182 extern "C" ret_type func(__VA_ARGS__);
183 # define ASSIGN_REAL(x, y)
184 #endif // SANITIZER_MAC
186 #if !SANITIZER_FUCHSIA && !SANITIZER_RTEMS
187 #define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
188 DECLARE_REAL(ret_type, func, __VA_ARGS__) \
189 extern "C" ret_type WRAP(func)(__VA_ARGS__);
190 #else
191 #define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...)
192 #endif
194 // Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
195 // macros does its job. In exceptional cases you may need to call REAL(foo)
196 // without defining INTERCEPTOR(..., foo, ...). For example, if you override
197 // foo with an interceptor for other function.
198 #if !SANITIZER_MAC && !SANITIZER_FUCHSIA && !SANITIZER_RTEMS
199 # define DEFINE_REAL(ret_type, func, ...) \
200 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
201 namespace __interception { \
202 FUNC_TYPE(func) PTR_TO_REAL(func); \
204 #else
205 # define DEFINE_REAL(ret_type, func, ...)
206 #endif
208 #if SANITIZER_FUCHSIA
210 // We need to define the __interceptor_func name just to get
211 // sanitizer_common/scripts/gen_dynamic_list.py to export func.
212 // But we don't need to export __interceptor_func to get that.
213 #define INTERCEPTOR(ret_type, func, ...) \
214 extern "C"[[ gnu::alias(#func), gnu::visibility("hidden") ]] ret_type \
215 __interceptor_##func(__VA_ARGS__); \
216 extern "C" INTERCEPTOR_ATTRIBUTE ret_type func(__VA_ARGS__)
218 #elif !SANITIZER_MAC
220 #define INTERCEPTOR(ret_type, func, ...) \
221 DEFINE_REAL(ret_type, func, __VA_ARGS__) \
222 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
223 extern "C" \
224 INTERCEPTOR_ATTRIBUTE \
225 ret_type WRAP(func)(__VA_ARGS__)
227 // We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now.
228 #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
229 INTERCEPTOR(ret_type, func, __VA_ARGS__)
231 #else // SANITIZER_MAC
233 #define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \
234 extern "C" ret_type func(__VA_ARGS__) suffix; \
235 extern "C" ret_type WRAP(func)(__VA_ARGS__); \
236 INTERPOSER(func); \
237 extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
239 #define INTERCEPTOR(ret_type, func, ...) \
240 INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__)
242 #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
243 INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__)
245 // Override |overridee| with |overrider|.
246 #define OVERRIDE_FUNCTION(overridee, overrider) \
247 INTERPOSER_2(overridee, WRAP(overrider))
248 #endif
250 #if SANITIZER_WINDOWS
251 # define INTERCEPTOR_WINAPI(ret_type, func, ...) \
252 typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
253 namespace __interception { \
254 FUNC_TYPE(func) PTR_TO_REAL(func); \
256 extern "C" \
257 INTERCEPTOR_ATTRIBUTE \
258 ret_type __stdcall WRAP(func)(__VA_ARGS__)
259 #endif
261 // ISO C++ forbids casting between pointer-to-function and pointer-to-object,
262 // so we use casting via an integral type __interception::uptr,
263 // assuming that system is POSIX-compliant. Using other hacks seem
264 // challenging, as we don't even pass function type to
265 // INTERCEPT_FUNCTION macro, only its name.
266 namespace __interception {
267 #if defined(_WIN64)
268 typedef unsigned long long uptr; // NOLINT
269 #else
270 typedef unsigned long uptr; // NOLINT
271 #endif // _WIN64
272 } // namespace __interception
274 #define INCLUDED_FROM_INTERCEPTION_LIB
276 #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
277 SANITIZER_OPENBSD || SANITIZER_SOLARIS
279 # include "interception_linux.h"
280 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
281 # define INTERCEPT_FUNCTION_VER(func, symver) \
282 INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver)
283 #elif SANITIZER_MAC
284 # include "interception_mac.h"
285 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
286 # define INTERCEPT_FUNCTION_VER(func, symver) \
287 INTERCEPT_FUNCTION_VER_MAC(func, symver)
288 #elif SANITIZER_WINDOWS
289 # include "interception_win.h"
290 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
291 # define INTERCEPT_FUNCTION_VER(func, symver) \
292 INTERCEPT_FUNCTION_VER_WIN(func, symver)
293 #endif
295 #undef INCLUDED_FROM_INTERCEPTION_LIB
297 #endif // INTERCEPTION_H