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 | |
| 18 | #if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) |
| 19 | # error "Interception doesn't work on this operating system." |
| 20 | #endif |
| 21 | |
Alexey Samsonov | 32832e6 | 2013-01-30 14:27:41 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_internal_defs.h" |
Kostya Serebryany | 07bb392 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 23 | |
| 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) |
Kostya Serebryany | f0b8f98 | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 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::OFF_T OFF_T; |
| 31 | typedef __sanitizer::OFF64_T OFF64_T; |
Kostya Serebryany | 07bb392 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 32 | |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 33 | // How to add an interceptor: |
| 34 | // Suppose you need to wrap/replace system function (generally, from libc): |
| 35 | // int foo(const char *bar, double baz); |
| 36 | // You'll need to: |
| 37 | // 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in |
Alexander Potapenko | a15d49c | 2013-05-20 13:32:35 +0000 | [diff] [blame^] | 38 | // your source file. See the notes below for cases when |
| 39 | // INTERCEPTOR_WITH_SUFFIX(...) should be used instead. |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 40 | // 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo". |
| 41 | // INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was |
| 42 | // intercepted successfully. |
| 43 | // You can access original function by calling REAL(foo)(bar, baz). |
| 44 | // By default, REAL(foo) will be visible only inside your interceptor, and if |
| 45 | // 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] | 46 | // 3a) add DECLARE_REAL(int, foo, const char*, double) to a |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 47 | // header file. |
| 48 | // However, if the call "INTERCEPT_FUNCTION(foo)" and definition for |
| 49 | // INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to: |
Alexey Samsonov | 70386aa | 2012-06-28 08:27:24 +0000 | [diff] [blame] | 50 | // 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double) |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 51 | // to a header file. |
| 52 | |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 53 | // Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or |
Alexey Samsonov | 70386aa | 2012-06-28 08:27:24 +0000 | [diff] [blame] | 54 | // DECLARE_REAL(...) are located inside namespaces. |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 55 | // 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 56 | // effectively redirect calls from "foo" to "zoo". In this case |
| 57 | // you aren't required to implement |
Alexey Samsonov | 70386aa | 2012-06-28 08:27:24 +0000 | [diff] [blame] | 58 | // INTERCEPTOR(int, foo, const char *bar, double baz) {...} |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 59 | // but instead you'll have to add |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 60 | // DECLARE_REAL(int, foo, const char *bar, double baz) in your |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 61 | // source file (to define a pointer to overriden function). |
Alexander Potapenko | a15d49c | 2013-05-20 13:32:35 +0000 | [diff] [blame^] | 62 | // 3. Some Mac functions have symbol variants discriminated by |
| 63 | // additional suffixes, e.g. _$UNIX2003 (see |
| 64 | // https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html |
| 65 | // for more details). To intercept such functions you need to use the |
| 66 | // INTERCEPTOR_WITH_SUFFIX(...) macro. |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 67 | |
| 68 | // How it works: |
Dmitry Vyukov | 7fb7330 | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 69 | // To replace system functions on Linux we just need to declare functions |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 70 | // 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] | 71 | // using dlsym(). |
| 72 | // 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] | 73 | // we intercept. To resolve this we declare our interceptors with __interceptor_ |
| 74 | // prefix, and then make actual interceptors weak aliases to __interceptor_ |
Dmitry Vyukov | 7fb7330 | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 75 | // functions. |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 76 | // |
Dmitry Vyukov | 7fb7330 | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 77 | // 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] | 78 | // our replacement functions invisible to other libraries. This may be overcomed |
| 79 | // using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared |
Alexander Potapenko | 34157fc | 2013-02-05 15:57:12 +0000 | [diff] [blame] | 80 | // libraries in Chromium were noticed when doing so. |
| 81 | // Instead we create a dylib containing a __DATA,__interpose section that |
| 82 | // associates library functions with their wrappers. When this dylib is |
| 83 | // preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all |
| 84 | // the calls to interposed functions done through stubs to the wrapper |
| 85 | // functions. |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 86 | // As it's decided at compile time which functions are to be intercepted on Mac, |
| 87 | // INTERCEPT_FUNCTION() is effectively a no-op on this system. |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 88 | |
| 89 | #if defined(__APPLE__) |
Alexander Potapenko | a15d49c | 2013-05-20 13:32:35 +0000 | [diff] [blame^] | 90 | #include <sys/cdefs.h> // For __DARWIN_ALIAS_C(). |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 91 | |
| 92 | // Just a pair of pointers. |
| 93 | struct interpose_substitution { |
| 94 | const uptr replacement; |
| 95 | const uptr original; |
| 96 | }; |
| 97 | |
| 98 | // For a function foo() create a global pair of pointers { wrap_foo, foo } in |
| 99 | // the __DATA,__interpose section. |
| 100 | // As a result all the calls to foo() will be routed to wrap_foo() at runtime. |
| 101 | #define INTERPOSER(func_name) __attribute__((used)) \ |
| 102 | const interpose_substitution substitution_##func_name[] \ |
| 103 | __attribute__((section("__DATA, __interpose"))) = { \ |
| 104 | { reinterpret_cast<const uptr>(WRAP(func_name)), \ |
| 105 | reinterpret_cast<const uptr>(func_name) } \ |
| 106 | } |
| 107 | |
| 108 | // For a function foo() and a wrapper function bar() create a global pair |
| 109 | // of pointers { bar, foo } in the __DATA,__interpose section. |
| 110 | // As a result all the calls to foo() will be routed to bar() at runtime. |
| 111 | #define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \ |
| 112 | const interpose_substitution substitution_##func_name[] \ |
| 113 | __attribute__((section("__DATA, __interpose"))) = { \ |
| 114 | { reinterpret_cast<const uptr>(wrapper_name), \ |
| 115 | reinterpret_cast<const uptr>(func_name) } \ |
| 116 | } |
| 117 | |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 118 | # define WRAP(x) wrap_##x |
| 119 | # define WRAPPER_NAME(x) "wrap_"#x |
| 120 | # define INTERCEPTOR_ATTRIBUTE |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 121 | # define DECLARE_WRAPPER(ret_type, func, ...) |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 122 | |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 123 | #elif defined(_WIN32) |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 +0000 | [diff] [blame] | 124 | # if defined(_DLL) // DLL CRT |
| 125 | # define WRAP(x) x |
| 126 | # define WRAPPER_NAME(x) #x |
| 127 | # define INTERCEPTOR_ATTRIBUTE |
| 128 | # else // Static CRT |
| 129 | # define WRAP(x) wrap_##x |
| 130 | # define WRAPPER_NAME(x) "wrap_"#x |
| 131 | # define INTERCEPTOR_ATTRIBUTE |
| 132 | # endif |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 133 | # define DECLARE_WRAPPER(ret_type, func, ...) |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 134 | #else |
Dmitry Vyukov | bd310f0 | 2012-05-28 07:47:35 +0000 | [diff] [blame] | 135 | # define WRAP(x) __interceptor_ ## x |
| 136 | # define WRAPPER_NAME(x) "__interceptor_" #x |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 137 | # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default"))) |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 138 | # define DECLARE_WRAPPER(ret_type, func, ...) \ |
| 139 | extern "C" ret_type func(__VA_ARGS__) \ |
Alexey Samsonov | fa42dd7 | 2012-06-28 08:07:19 +0000 | [diff] [blame] | 140 | __attribute__((weak, alias("__interceptor_" #func), visibility("default"))); |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
Alexander Potapenko | 34157fc | 2013-02-05 15:57:12 +0000 | [diff] [blame] | 143 | #if !defined(__APPLE__) |
Alexander Potapenko | c62210e | 2012-08-17 09:00:08 +0000 | [diff] [blame] | 144 | # define PTR_TO_REAL(x) real_##x |
| 145 | # define REAL(x) __interception::PTR_TO_REAL(x) |
| 146 | # define FUNC_TYPE(x) x##_f |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 147 | |
Alexander Potapenko | c62210e | 2012-08-17 09:00:08 +0000 | [diff] [blame] | 148 | # define DECLARE_REAL(ret_type, func, ...) \ |
| 149 | typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ |
| 150 | namespace __interception { \ |
| 151 | extern FUNC_TYPE(func) PTR_TO_REAL(func); \ |
| 152 | } |
Alexander Potapenko | 34157fc | 2013-02-05 15:57:12 +0000 | [diff] [blame] | 153 | #else // __APPLE__ |
Alexander Potapenko | c62210e | 2012-08-17 09:00:08 +0000 | [diff] [blame] | 154 | # define REAL(x) x |
| 155 | # define DECLARE_REAL(ret_type, func, ...) \ |
| 156 | extern "C" ret_type func(__VA_ARGS__); |
Alexander Potapenko | 34157fc | 2013-02-05 15:57:12 +0000 | [diff] [blame] | 157 | #endif // __APPLE__ |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 158 | |
Chandler Carruth | bbff278 | 2012-06-25 06:53:10 +0000 | [diff] [blame] | 159 | #define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \ |
Alexey Samsonov | a81d268 | 2012-09-12 09:42:23 +0000 | [diff] [blame] | 160 | DECLARE_REAL(ret_type, func, __VA_ARGS__) \ |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 161 | extern "C" ret_type WRAP(func)(__VA_ARGS__); |
| 162 | |
Timur Iskhodzhanov | 0f9c9a5 | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 163 | // Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR |
| 164 | // macros does its job. In exceptional cases you may need to call REAL(foo) |
| 165 | // without defining INTERCEPTOR(..., foo, ...). For example, if you override |
| 166 | // foo with an interceptor for other function. |
Alexander Potapenko | 34157fc | 2013-02-05 15:57:12 +0000 | [diff] [blame] | 167 | #if !defined(__APPLE__) |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 168 | # define DEFINE_REAL(ret_type, func, ...) \ |
| 169 | typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ |
| 170 | namespace __interception { \ |
| 171 | FUNC_TYPE(func) PTR_TO_REAL(func); \ |
| 172 | } |
| 173 | #else |
| 174 | # define DEFINE_REAL(ret_type, func, ...) |
| 175 | #endif |
Timur Iskhodzhanov | 0f9c9a5 | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 176 | |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 177 | #if !defined(__APPLE__) |
Timur Iskhodzhanov | 0f9c9a5 | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 178 | #define INTERCEPTOR(ret_type, func, ...) \ |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 179 | DEFINE_REAL(ret_type, func, __VA_ARGS__) \ |
| 180 | DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \ |
| 181 | extern "C" \ |
| 182 | INTERCEPTOR_ATTRIBUTE \ |
| 183 | ret_type WRAP(func)(__VA_ARGS__) |
Alexander Potapenko | a15d49c | 2013-05-20 13:32:35 +0000 | [diff] [blame^] | 184 | |
| 185 | // We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now. |
| 186 | #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \ |
| 187 | INTERCEPTOR(ret_type, func, __VA_ARGS__) |
| 188 | |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 189 | #else // __APPLE__ |
Alexander Potapenko | a15d49c | 2013-05-20 13:32:35 +0000 | [diff] [blame^] | 190 | |
| 191 | #define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \ |
| 192 | extern "C" ret_type func(__VA_ARGS__) suffix; \ |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 193 | extern "C" ret_type WRAP(func)(__VA_ARGS__); \ |
| 194 | INTERPOSER(func); \ |
| 195 | extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__) |
| 196 | |
Alexander Potapenko | a15d49c | 2013-05-20 13:32:35 +0000 | [diff] [blame^] | 197 | #define INTERCEPTOR(ret_type, func, ...) \ |
| 198 | INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__) |
| 199 | |
| 200 | #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \ |
| 201 | INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__) |
| 202 | |
Alexander Potapenko | e8ba1c8 | 2013-02-21 14:41:16 +0000 | [diff] [blame] | 203 | // Override |overridee| with |overrider|. |
| 204 | #define OVERRIDE_FUNCTION(overridee, overrider) \ |
| 205 | INTERPOSER_2(overridee, WRAP(overrider)) |
| 206 | #endif |
Timur Iskhodzhanov | 0f9c9a5 | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 207 | |
| 208 | #if defined(_WIN32) |
| 209 | # define INTERCEPTOR_WINAPI(ret_type, func, ...) \ |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 210 | typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \ |
| 211 | namespace __interception { \ |
| 212 | FUNC_TYPE(func) PTR_TO_REAL(func); \ |
| 213 | } \ |
| 214 | DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \ |
| 215 | extern "C" \ |
| 216 | INTERCEPTOR_ATTRIBUTE \ |
| 217 | ret_type __stdcall WRAP(func)(__VA_ARGS__) |
Timur Iskhodzhanov | 0f9c9a5 | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 218 | #endif |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 219 | |
Alexey Samsonov | 0f840bd | 2012-08-02 11:19:13 +0000 | [diff] [blame] | 220 | // ISO C++ forbids casting between pointer-to-function and pointer-to-object, |
| 221 | // so we use casting via an integral type __interception::uptr, |
| 222 | // assuming that system is POSIX-compliant. Using other hacks seem |
| 223 | // challenging, as we don't even pass function type to |
| 224 | // INTERCEPT_FUNCTION macro, only its name. |
| 225 | namespace __interception { |
Alexey Samsonov | a555b3f | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 226 | #if defined(_WIN64) |
| 227 | typedef unsigned long long uptr; // NOLINT |
| 228 | #else |
Alexey Samsonov | 0f840bd | 2012-08-02 11:19:13 +0000 | [diff] [blame] | 229 | typedef unsigned long uptr; // NOLINT |
Alexey Samsonov | a555b3f | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 230 | #endif // _WIN64 |
Alexey Samsonov | 0f840bd | 2012-08-02 11:19:13 +0000 | [diff] [blame] | 231 | } // namespace __interception |
| 232 | |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 233 | #define INCLUDED_FROM_INTERCEPTION_LIB |
| 234 | |
| 235 | #if defined(__linux__) |
| 236 | # include "interception_linux.h" |
| 237 | # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func) |
| 238 | #elif defined(__APPLE__) |
| 239 | # include "interception_mac.h" |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 240 | # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func) |
| 241 | #else // defined(_WIN32) |
Timur Iskhodzhanov | 36d297d | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 242 | # include "interception_win.h" |
| 243 | # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func) |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 244 | #endif |
| 245 | |
| 246 | #undef INCLUDED_FROM_INTERCEPTION_LIB |
| 247 | |
| 248 | #endif // INTERCEPTION_H |