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