1 //===-- interception.h ------------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Machinery for providing replacements/wrappers for system functions.
11 //===----------------------------------------------------------------------===//
13 #ifndef INTERCEPTION_H
14 #define INTERCEPTION_H
16 #if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
17 # error "Interception doesn't work on this operating system."
20 #include "sanitizer_common/sanitizer_internal_defs.h"
22 // These typedefs should be used only in the interceptor definitions to replace
23 // the standard system types (e.g. SSIZE_T instead of ssize_t)
24 typedef __sanitizer::uptr SIZE_T
;
25 typedef __sanitizer::sptr SSIZE_T
;
26 typedef __sanitizer::sptr PTRDIFF_T
;
27 typedef __sanitizer::s64 INTMAX_T
;
28 typedef __sanitizer::OFF_T OFF_T
;
29 typedef __sanitizer::OFF64_T OFF64_T
;
31 // How to add an interceptor:
32 // Suppose you need to wrap/replace system function (generally, from libc):
33 // int foo(const char *bar, double baz);
35 // 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
36 // your source file. See the notes below for cases when
37 // INTERCEPTOR_WITH_SUFFIX(...) should be used instead.
38 // 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
39 // INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
40 // intercepted successfully.
41 // You can access original function by calling REAL(foo)(bar, baz).
42 // By default, REAL(foo) will be visible only inside your interceptor, and if
43 // you want to use it in other parts of RTL, you'll need to:
44 // 3a) add DECLARE_REAL(int, foo, const char*, double) to a
46 // However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
47 // INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
48 // 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
51 // Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or
52 // DECLARE_REAL(...) are located inside namespaces.
53 // 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to
54 // effectively redirect calls from "foo" to "zoo". In this case
55 // you aren't required to implement
56 // INTERCEPTOR(int, foo, const char *bar, double baz) {...}
57 // but instead you'll have to add
58 // DECLARE_REAL(int, foo, const char *bar, double baz) in your
59 // source file (to define a pointer to overriden function).
60 // 3. Some Mac functions have symbol variants discriminated by
61 // additional suffixes, e.g. _$UNIX2003 (see
62 // https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html
63 // for more details). To intercept such functions you need to use the
64 // INTERCEPTOR_WITH_SUFFIX(...) macro.
67 // To replace system functions on Linux we just need to declare functions
68 // with same names in our library and then obtain the real function pointers
70 // There is one complication. A user may also intercept some of the functions
71 // we intercept. To resolve this we declare our interceptors with __interceptor_
72 // prefix, and then make actual interceptors weak aliases to __interceptor_
75 // This is not so on Mac OS, where the two-level namespace makes
76 // our replacement functions invisible to other libraries. This may be overcomed
77 // using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
78 // libraries in Chromium were noticed when doing so.
79 // Instead we create a dylib containing a __DATA,__interpose section that
80 // associates library functions with their wrappers. When this dylib is
81 // preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all
82 // the calls to interposed functions done through stubs to the wrapper
84 // As it's decided at compile time which functions are to be intercepted on Mac,
85 // INTERCEPT_FUNCTION() is effectively a no-op on this system.
87 #if defined(__APPLE__)
88 #include <sys/cdefs.h> // For __DARWIN_ALIAS_C().
90 // Just a pair of pointers.
91 struct interpose_substitution
{
92 const uptr replacement
;
96 // For a function foo() create a global pair of pointers { wrap_foo, foo } in
97 // the __DATA,__interpose section.
98 // As a result all the calls to foo() will be routed to wrap_foo() at runtime.
99 #define INTERPOSER(func_name) __attribute__((used)) \
100 const interpose_substitution substitution_##func_name[] \
101 __attribute__((section("__DATA, __interpose"))) = { \
102 { reinterpret_cast<const uptr>(WRAP(func_name)), \
103 reinterpret_cast<const uptr>(func_name) } \
106 // For a function foo() and a wrapper function bar() create a global pair
107 // of pointers { bar, foo } in the __DATA,__interpose section.
108 // As a result all the calls to foo() will be routed to bar() at runtime.
109 #define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \
110 const interpose_substitution substitution_##func_name[] \
111 __attribute__((section("__DATA, __interpose"))) = { \
112 { reinterpret_cast<const uptr>(wrapper_name), \
113 reinterpret_cast<const uptr>(func_name) } \
116 # define WRAP(x) wrap_##x
117 # define WRAPPER_NAME(x) "wrap_"#x
118 # define INTERCEPTOR_ATTRIBUTE
119 # define DECLARE_WRAPPER(ret_type, func, ...)
121 #elif defined(_WIN32)
122 # if defined(_DLL) // DLL CRT
124 # define WRAPPER_NAME(x) #x
125 # define INTERCEPTOR_ATTRIBUTE
127 # define WRAP(x) wrap_##x
128 # define WRAPPER_NAME(x) "wrap_"#x
129 # define INTERCEPTOR_ATTRIBUTE
131 # define DECLARE_WRAPPER(ret_type, func, ...) \
132 extern "C" ret_type func(__VA_ARGS__);
133 # define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \
134 extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__);
136 # define WRAP(x) __interceptor_ ## x
137 # define WRAPPER_NAME(x) "__interceptor_" #x
138 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
139 # define DECLARE_WRAPPER(ret_type, func, ...) \
140 extern "C" ret_type func(__VA_ARGS__) \
141 __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
144 #if !defined(__APPLE__)
145 # define PTR_TO_REAL(x) real_##x
146 # define REAL(x) __interception::PTR_TO_REAL(x)
147 # define FUNC_TYPE(x) x##_f
149 # define DECLARE_REAL(ret_type, func, ...) \
150 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
151 namespace __interception { \
152 extern FUNC_TYPE(func) PTR_TO_REAL(func); \
156 # define DECLARE_REAL(ret_type, func, ...) \
157 extern "C" ret_type func(__VA_ARGS__);
160 #define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
161 DECLARE_REAL(ret_type, func, __VA_ARGS__) \
162 extern "C" ret_type WRAP(func)(__VA_ARGS__);
164 // Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
165 // macros does its job. In exceptional cases you may need to call REAL(foo)
166 // without defining INTERCEPTOR(..., foo, ...). For example, if you override
167 // foo with an interceptor for other function.
168 #if !defined(__APPLE__)
169 # define DEFINE_REAL(ret_type, func, ...) \
170 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
171 namespace __interception { \
172 FUNC_TYPE(func) PTR_TO_REAL(func); \
175 # define DEFINE_REAL(ret_type, func, ...)
178 #if !defined(__APPLE__)
179 #define INTERCEPTOR(ret_type, func, ...) \
180 DEFINE_REAL(ret_type, func, __VA_ARGS__) \
181 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
183 INTERCEPTOR_ATTRIBUTE \
184 ret_type WRAP(func)(__VA_ARGS__)
186 // We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now.
187 #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
188 INTERCEPTOR(ret_type, func, __VA_ARGS__)
192 #define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \
193 extern "C" ret_type func(__VA_ARGS__) suffix; \
194 extern "C" ret_type WRAP(func)(__VA_ARGS__); \
196 extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
198 #define INTERCEPTOR(ret_type, func, ...) \
199 INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__)
201 #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
202 INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__)
204 // Override |overridee| with |overrider|.
205 #define OVERRIDE_FUNCTION(overridee, overrider) \
206 INTERPOSER_2(overridee, WRAP(overrider))
210 # define INTERCEPTOR_WINAPI(ret_type, func, ...) \
211 typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
212 namespace __interception { \
213 FUNC_TYPE(func) PTR_TO_REAL(func); \
215 DECLARE_WRAPPER_WINAPI(ret_type, func, __VA_ARGS__) \
217 INTERCEPTOR_ATTRIBUTE \
218 ret_type __stdcall WRAP(func)(__VA_ARGS__)
221 // ISO C++ forbids casting between pointer-to-function and pointer-to-object,
222 // so we use casting via an integral type __interception::uptr,
223 // assuming that system is POSIX-compliant. Using other hacks seem
224 // challenging, as we don't even pass function type to
225 // INTERCEPT_FUNCTION macro, only its name.
226 namespace __interception
{
228 typedef unsigned long long uptr
; // NOLINT
230 typedef unsigned long uptr
; // NOLINT
232 } // namespace __interception
234 #define INCLUDED_FROM_INTERCEPTION_LIB
236 #if defined(__linux__)
237 # include "interception_linux.h"
238 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func)
239 #elif defined(__APPLE__)
240 # include "interception_mac.h"
241 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
242 #else // defined(_WIN32)
243 # include "interception_win.h"
244 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
247 #undef INCLUDED_FROM_INTERCEPTION_LIB
249 #endif // INTERCEPTION_H