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