* configure.ac: Change target-libasan to target-libsanitizer.
[official-gcc.git] / libsanitizer / interception / interception.h
blob8094fe5d34512ed35f81b4812409716ddf686597
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 #if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
17 # error "Interception doesn't work on this operating system."
18 #endif
20 // How to use this library:
21 // 1) Include this header to define your own interceptors
22 // (see details below).
23 // 2) Build all *.cc files and link against them.
24 // On Mac you will also need to:
25 // 3) Provide your own implementation for the following functions:
26 // mach_error_t __interception::allocate_island(void **ptr,
27 // size_t size,
28 // void *hint);
29 // mach_error_t __interception::deallocate_island(void *ptr);
30 // See "interception_mac.h" for more details.
32 // How to add an interceptor:
33 // Suppose you need to wrap/replace system function (generally, from libc):
34 // int foo(const char *bar, double baz);
35 // You'll need to:
36 // 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
37 // your source file.
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
45 // header file.
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)
49 // to a header file.
51 // Notes: 1. Things may not work properly if macro INTERCEPT(...) {...} 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 // DEFINE_REAL(int, foo, const char *bar, double baz) in your
59 // source file (to define a pointer to overriden function).
61 // How it works:
62 // To replace system functions on Linux we just need to declare functions
63 // with same names in our library and then obtain the real function pointers
64 // using dlsym().
65 // There is one complication. A user may also intercept some of the functions
66 // we intercept. To resolve this we declare our interceptors with __interceptor_
67 // prefix, and then make actual interceptors weak aliases to __interceptor_
68 // functions.
69 // This is not so on Mac OS, where the two-level namespace makes
70 // our replacement functions invisible to other libraries. This may be overcomed
71 // using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
72 // libraries in Chromium were noticed when doing so. Instead we use
73 // mach_override, a handy framework for patching functions at runtime.
74 // To avoid possible name clashes, our replacement functions have
75 // the "wrap_" prefix on Mac.
76 // An alternative to function patching is to create a dylib containing a
77 // __DATA,__interpose section that associates library functions with their
78 // wrappers. When this dylib is preloaded before an executable using
79 // DYLD_INSERT_LIBRARIES, it routes all the calls to interposed functions done
80 // through stubs to the wrapper functions. Such a library is built with
81 // -DMAC_INTERPOSE_FUNCTIONS=1.
83 #if !defined(MAC_INTERPOSE_FUNCTIONS) || !defined(__APPLE__)
84 # define MAC_INTERPOSE_FUNCTIONS 0
85 #endif
87 #if defined(__APPLE__)
88 # define WRAP(x) wrap_##x
89 # define WRAPPER_NAME(x) "wrap_"#x
90 # define INTERCEPTOR_ATTRIBUTE
91 # define DECLARE_WRAPPER(ret_type, func, ...)
92 #elif defined(_WIN32)
93 # if defined(_DLL) // DLL CRT
94 # define WRAP(x) x
95 # define WRAPPER_NAME(x) #x
96 # define INTERCEPTOR_ATTRIBUTE
97 # else // Static CRT
98 # define WRAP(x) wrap_##x
99 # define WRAPPER_NAME(x) "wrap_"#x
100 # define INTERCEPTOR_ATTRIBUTE
101 # endif
102 # define DECLARE_WRAPPER(ret_type, func, ...)
103 #else
104 # define WRAP(x) __interceptor_ ## x
105 # define WRAPPER_NAME(x) "__interceptor_" #x
106 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
107 # define DECLARE_WRAPPER(ret_type, func, ...) \
108 extern "C" ret_type func(__VA_ARGS__) \
109 __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
110 #endif
112 #if !MAC_INTERPOSE_FUNCTIONS
113 # define PTR_TO_REAL(x) real_##x
114 # define REAL(x) __interception::PTR_TO_REAL(x)
115 # define FUNC_TYPE(x) x##_f
117 # define DECLARE_REAL(ret_type, func, ...) \
118 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
119 namespace __interception { \
120 extern FUNC_TYPE(func) PTR_TO_REAL(func); \
122 #else // MAC_INTERPOSE_FUNCTIONS
123 # define REAL(x) x
124 # define DECLARE_REAL(ret_type, func, ...) \
125 extern "C" ret_type func(__VA_ARGS__);
126 #endif // MAC_INTERPOSE_FUNCTIONS
128 #define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
129 DECLARE_REAL(ret_type, func, __VA_ARGS__) \
130 extern "C" ret_type WRAP(func)(__VA_ARGS__);
132 // Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
133 // macros does its job. In exceptional cases you may need to call REAL(foo)
134 // without defining INTERCEPTOR(..., foo, ...). For example, if you override
135 // foo with an interceptor for other function.
136 #if !MAC_INTERPOSE_FUNCTIONS
137 # define DEFINE_REAL(ret_type, func, ...) \
138 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
139 namespace __interception { \
140 FUNC_TYPE(func) PTR_TO_REAL(func); \
142 #else
143 # define DEFINE_REAL(ret_type, func, ...)
144 #endif
146 #define INTERCEPTOR(ret_type, func, ...) \
147 DEFINE_REAL(ret_type, func, __VA_ARGS__) \
148 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
149 extern "C" \
150 INTERCEPTOR_ATTRIBUTE \
151 ret_type WRAP(func)(__VA_ARGS__)
153 #if defined(_WIN32)
154 # define INTERCEPTOR_WINAPI(ret_type, func, ...) \
155 typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
156 namespace __interception { \
157 FUNC_TYPE(func) PTR_TO_REAL(func); \
159 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
160 extern "C" \
161 INTERCEPTOR_ATTRIBUTE \
162 ret_type __stdcall WRAP(func)(__VA_ARGS__)
163 #endif
165 // ISO C++ forbids casting between pointer-to-function and pointer-to-object,
166 // so we use casting via an integral type __interception::uptr,
167 // assuming that system is POSIX-compliant. Using other hacks seem
168 // challenging, as we don't even pass function type to
169 // INTERCEPT_FUNCTION macro, only its name.
170 namespace __interception {
171 #if defined(_WIN64)
172 typedef unsigned long long uptr; // NOLINT
173 #else
174 typedef unsigned long uptr; // NOLINT
175 #endif // _WIN64
176 } // namespace __interception
178 #define INCLUDED_FROM_INTERCEPTION_LIB
180 #if defined(__linux__)
181 # include "interception_linux.h"
182 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func)
183 #elif defined(__APPLE__)
184 # include "interception_mac.h"
185 # define OVERRIDE_FUNCTION(old_func, new_func) \
186 OVERRIDE_FUNCTION_MAC(old_func, new_func)
187 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
188 #else // defined(_WIN32)
189 # include "interception_win.h"
190 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
191 #endif
193 #undef INCLUDED_FROM_INTERCEPTION_LIB
195 #endif // INTERCEPTION_H