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) |
| 26 | typedef __sanitizer::uptr SIZE_T; |
| 27 | typedef __sanitizer::sptr SSIZE_T; |
Evgeniy Stepanov | 222076e | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 28 | typedef __sanitizer::sptr PTRDIFF_T; |
| 29 | typedef __sanitizer::s64 INTMAX_T; |
Kostya Serebryany | 07bb392 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 30 | typedef __sanitizer::u64 OFF_T; |
| 31 | typedef __sanitizer::u64 OFF64_T; |
| 32 | |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 33 | // How to use this library: |
| 34 | // 1) Include this header to define your own interceptors |
| 35 | // (see details below). |
| 36 | // 2) Build all *.cc files and link against them. |
| 37 | // On Mac you will also need to: |
| 38 | // 3) Provide your own implementation for the following functions: |
| 39 | // mach_error_t __interception::allocate_island(void **ptr, |
| 40 | // size_t size, |
| 41 | // void *hint); |
| 42 | // mach_error_t __interception::deallocate_island(void *ptr); |
| 43 | // See "interception_mac.h" for more details. |
| 44 | |
| 45 | // How to add an interceptor: |
| 46 | // Suppose you need to wrap/replace system function (generally, from libc): |
| 47 | // int foo(const char *bar, double baz); |
| 48 | // You'll need to: |
| 49 | // 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in |
| 50 | // your source file. |
| 51 | // 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo". |
| 52 | // INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was |
| 53 | // intercepted successfully. |
| 54 | // You can access original function by calling REAL(foo)(bar, baz). |
| 55 | // By default, REAL(foo) will be visible only inside your interceptor, and if |
| 56 | // 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] | 57 | // 3a) add DECLARE_REAL(int, foo, const char*, double) to a |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 58 | // header file. |
| 59 | // However, if the call "INTERCEPT_FUNCTION(foo)" and definition for |
| 60 | // INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to: |
Alexey Samsonov | 70386aa | 2012-06-28 08:27:24 +0000 | [diff] [blame] | 61 | // 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double) |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 62 | // to a header file. |
| 63 | |
| 64 | // Notes: 1. Things may not work properly if macro INTERCEPT(...) {...} or |
Alexey Samsonov | 70386aa | 2012-06-28 08:27:24 +0000 | [diff] [blame] | 65 | // DECLARE_REAL(...) are located inside namespaces. |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 66 | // 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo);" to |
| 67 | // effectively redirect calls from "foo" to "zoo". In this case |
| 68 | // you aren't required to implement |
Alexey Samsonov | 70386aa | 2012-06-28 08:27:24 +0000 | [diff] [blame] | 69 | // INTERCEPTOR(int, foo, const char *bar, double baz) {...} |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 70 | // but instead you'll have to add |
Alexey Samsonov | 70386aa | 2012-06-28 08:27:24 +0000 | [diff] [blame] | 71 | // DEFINE_REAL(int, foo, const char *bar, double baz) in your |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 72 | // source file (to define a pointer to overriden function). |
| 73 | |
| 74 | // How it works: |
Dmitry Vyukov | 7fb7330 | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 75 | // To replace system functions on Linux we just need to declare functions |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 76 | // 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] | 77 | // using dlsym(). |
| 78 | // 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] | 79 | // we intercept. To resolve this we declare our interceptors with __interceptor_ |
| 80 | // prefix, and then make actual interceptors weak aliases to __interceptor_ |
Dmitry Vyukov | 7fb7330 | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 81 | // functions. |
| 82 | // 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] | 83 | // our replacement functions invisible to other libraries. This may be overcomed |
| 84 | // using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared |
Dmitry Vyukov | 7fb7330 | 2012-05-24 13:54:31 +0000 | [diff] [blame] | 85 | // libraries in Chromium were noticed when doing so. Instead we use |
| 86 | // mach_override, a handy framework for patching functions at runtime. |
| 87 | // To avoid possible name clashes, our replacement functions have |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 88 | // the "wrap_" prefix on Mac. |
Alexander Potapenko | c62210e | 2012-08-17 09:00:08 +0000 | [diff] [blame] | 89 | // An alternative to function patching is to create a dylib containing a |
| 90 | // __DATA,__interpose section that associates library functions with their |
| 91 | // wrappers. When this dylib is preloaded before an executable using |
| 92 | // DYLD_INSERT_LIBRARIES, it routes all the calls to interposed functions done |
| 93 | // through stubs to the wrapper functions. Such a library is built with |
| 94 | // -DMAC_INTERPOSE_FUNCTIONS=1. |
| 95 | |
| 96 | #if !defined(MAC_INTERPOSE_FUNCTIONS) || !defined(__APPLE__) |
| 97 | # define MAC_INTERPOSE_FUNCTIONS 0 |
| 98 | #endif |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 99 | |
| 100 | #if defined(__APPLE__) |
| 101 | # define WRAP(x) wrap_##x |
| 102 | # define WRAPPER_NAME(x) "wrap_"#x |
| 103 | # define INTERCEPTOR_ATTRIBUTE |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 104 | # define DECLARE_WRAPPER(ret_type, func, ...) |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 105 | #elif defined(_WIN32) |
Timur Iskhodzhanov | 2f48b87 | 2012-03-12 11:45:09 +0000 | [diff] [blame] | 106 | # if defined(_DLL) // DLL CRT |
| 107 | # define WRAP(x) x |
| 108 | # define WRAPPER_NAME(x) #x |
| 109 | # define INTERCEPTOR_ATTRIBUTE |
| 110 | # else // Static CRT |
| 111 | # define WRAP(x) wrap_##x |
| 112 | # define WRAPPER_NAME(x) "wrap_"#x |
| 113 | # define INTERCEPTOR_ATTRIBUTE |
| 114 | # endif |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 115 | # define DECLARE_WRAPPER(ret_type, func, ...) |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 116 | #else |
Dmitry Vyukov | bd310f0 | 2012-05-28 07:47:35 +0000 | [diff] [blame] | 117 | # define WRAP(x) __interceptor_ ## x |
| 118 | # define WRAPPER_NAME(x) "__interceptor_" #x |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 119 | # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default"))) |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 120 | # define DECLARE_WRAPPER(ret_type, func, ...) \ |
| 121 | extern "C" ret_type func(__VA_ARGS__) \ |
Alexey Samsonov | fa42dd7 | 2012-06-28 08:07:19 +0000 | [diff] [blame] | 122 | __attribute__((weak, alias("__interceptor_" #func), visibility("default"))); |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
Alexander Potapenko | c62210e | 2012-08-17 09:00:08 +0000 | [diff] [blame] | 125 | #if !MAC_INTERPOSE_FUNCTIONS |
| 126 | # define PTR_TO_REAL(x) real_##x |
| 127 | # define REAL(x) __interception::PTR_TO_REAL(x) |
| 128 | # define FUNC_TYPE(x) x##_f |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 129 | |
Alexander Potapenko | c62210e | 2012-08-17 09:00:08 +0000 | [diff] [blame] | 130 | # define DECLARE_REAL(ret_type, func, ...) \ |
| 131 | typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ |
| 132 | namespace __interception { \ |
| 133 | extern FUNC_TYPE(func) PTR_TO_REAL(func); \ |
| 134 | } |
| 135 | #else // MAC_INTERPOSE_FUNCTIONS |
| 136 | # define REAL(x) x |
| 137 | # define DECLARE_REAL(ret_type, func, ...) \ |
| 138 | extern "C" ret_type func(__VA_ARGS__); |
| 139 | #endif // MAC_INTERPOSE_FUNCTIONS |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 140 | |
Chandler Carruth | bbff278 | 2012-06-25 06:53:10 +0000 | [diff] [blame] | 141 | #define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \ |
Alexey Samsonov | a81d268 | 2012-09-12 09:42:23 +0000 | [diff] [blame] | 142 | DECLARE_REAL(ret_type, func, __VA_ARGS__) \ |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 143 | extern "C" ret_type WRAP(func)(__VA_ARGS__); |
| 144 | |
Timur Iskhodzhanov | 0f9c9a5 | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 145 | // Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR |
| 146 | // macros does its job. In exceptional cases you may need to call REAL(foo) |
| 147 | // without defining INTERCEPTOR(..., foo, ...). For example, if you override |
| 148 | // foo with an interceptor for other function. |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 149 | #if !MAC_INTERPOSE_FUNCTIONS |
| 150 | # define DEFINE_REAL(ret_type, func, ...) \ |
| 151 | typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ |
| 152 | namespace __interception { \ |
| 153 | FUNC_TYPE(func) PTR_TO_REAL(func); \ |
| 154 | } |
| 155 | #else |
| 156 | # define DEFINE_REAL(ret_type, func, ...) |
| 157 | #endif |
Timur Iskhodzhanov | 0f9c9a5 | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 158 | |
| 159 | #define INTERCEPTOR(ret_type, func, ...) \ |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 160 | DEFINE_REAL(ret_type, func, __VA_ARGS__) \ |
| 161 | DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \ |
| 162 | extern "C" \ |
| 163 | INTERCEPTOR_ATTRIBUTE \ |
| 164 | ret_type WRAP(func)(__VA_ARGS__) |
Timur Iskhodzhanov | 0f9c9a5 | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 165 | |
| 166 | #if defined(_WIN32) |
| 167 | # define INTERCEPTOR_WINAPI(ret_type, func, ...) \ |
Alexey Samsonov | 788eaeb | 2012-09-11 15:02:20 +0000 | [diff] [blame] | 168 | typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \ |
| 169 | namespace __interception { \ |
| 170 | FUNC_TYPE(func) PTR_TO_REAL(func); \ |
| 171 | } \ |
| 172 | DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \ |
| 173 | extern "C" \ |
| 174 | INTERCEPTOR_ATTRIBUTE \ |
| 175 | ret_type __stdcall WRAP(func)(__VA_ARGS__) |
Timur Iskhodzhanov | 0f9c9a5 | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 176 | #endif |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 177 | |
Alexey Samsonov | 0f840bd | 2012-08-02 11:19:13 +0000 | [diff] [blame] | 178 | // ISO C++ forbids casting between pointer-to-function and pointer-to-object, |
| 179 | // so we use casting via an integral type __interception::uptr, |
| 180 | // assuming that system is POSIX-compliant. Using other hacks seem |
| 181 | // challenging, as we don't even pass function type to |
| 182 | // INTERCEPT_FUNCTION macro, only its name. |
| 183 | namespace __interception { |
Alexey Samsonov | a555b3f | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 184 | #if defined(_WIN64) |
| 185 | typedef unsigned long long uptr; // NOLINT |
| 186 | #else |
Alexey Samsonov | 0f840bd | 2012-08-02 11:19:13 +0000 | [diff] [blame] | 187 | typedef unsigned long uptr; // NOLINT |
Alexey Samsonov | a555b3f | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 188 | #endif // _WIN64 |
Alexey Samsonov | 0f840bd | 2012-08-02 11:19:13 +0000 | [diff] [blame] | 189 | } // namespace __interception |
| 190 | |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 191 | #define INCLUDED_FROM_INTERCEPTION_LIB |
| 192 | |
| 193 | #if defined(__linux__) |
| 194 | # include "interception_linux.h" |
| 195 | # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func) |
| 196 | #elif defined(__APPLE__) |
| 197 | # include "interception_mac.h" |
| 198 | # define OVERRIDE_FUNCTION(old_func, new_func) \ |
| 199 | OVERRIDE_FUNCTION_MAC(old_func, new_func) |
| 200 | # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func) |
| 201 | #else // defined(_WIN32) |
Timur Iskhodzhanov | 36d297d | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 202 | # include "interception_win.h" |
| 203 | # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func) |
Alexey Samsonov | 8489f2a | 2012-02-08 19:52:01 +0000 | [diff] [blame] | 204 | #endif |
| 205 | |
| 206 | #undef INCLUDED_FROM_INTERCEPTION_LIB |
| 207 | |
| 208 | #endif // INTERCEPTION_H |