interception.h revision 74d0540ceae99c550b00f75883a737f4a1de6351
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#include "sanitizer_common/sanitizer_internal_defs.h" 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) 26typedef __sanitizer::uptr SIZE_T; 27typedef __sanitizer::sptr SSIZE_T; 28typedef __sanitizer::sptr PTRDIFF_T; 29typedef __sanitizer::s64 INTMAX_T; 30// WARNING: OFF_T may be different from OS type off_t, depending on the value of 31// _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls 32// like pread and mmap, as opposed to pread64 and mmap64. 33// Mac is special. 34#ifdef __APPLE__ 35typedef __sanitizer::u64 OFF_T; 36#else 37typedef __sanitizer::uptr OFF_T; 38#endif 39typedef __sanitizer::u64 OFF64_T; 40 41// How to add an interceptor: 42// Suppose you need to wrap/replace system function (generally, from libc): 43// int foo(const char *bar, double baz); 44// You'll need to: 45// 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in 46// your source file. 47// 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo". 48// INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was 49// intercepted successfully. 50// You can access original function by calling REAL(foo)(bar, baz). 51// By default, REAL(foo) will be visible only inside your interceptor, and if 52// you want to use it in other parts of RTL, you'll need to: 53// 3a) add DECLARE_REAL(int, foo, const char*, double) to a 54// header file. 55// However, if the call "INTERCEPT_FUNCTION(foo)" and definition for 56// INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to: 57// 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double) 58// to a header file. 59 60// Notes: 1. Things may not work properly if macro INTERCEPT(...) {...} or 61// DECLARE_REAL(...) are located inside namespaces. 62// 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo);" to 63// effectively redirect calls from "foo" to "zoo". In this case 64// you aren't required to implement 65// INTERCEPTOR(int, foo, const char *bar, double baz) {...} 66// but instead you'll have to add 67// DEFINE_REAL(int, foo, const char *bar, double baz) in your 68// source file (to define a pointer to overriden function). 69 70// How it works: 71// To replace system functions on Linux we just need to declare functions 72// with same names in our library and then obtain the real function pointers 73// using dlsym(). 74// There is one complication. A user may also intercept some of the functions 75// we intercept. To resolve this we declare our interceptors with __interceptor_ 76// prefix, and then make actual interceptors weak aliases to __interceptor_ 77// functions. 78// This is not so on Mac OS, where the two-level namespace makes 79// our replacement functions invisible to other libraries. This may be overcomed 80// using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared 81// libraries in Chromium were noticed when doing so. 82// Instead we create a dylib containing a __DATA,__interpose section that 83// associates library functions with their wrappers. When this dylib is 84// preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all 85// the calls to interposed functions done through stubs to the wrapper 86// functions. 87 88#if defined(__APPLE__) 89# define WRAP(x) wrap_##x 90# define WRAPPER_NAME(x) "wrap_"#x 91# define INTERCEPTOR_ATTRIBUTE 92# define DECLARE_WRAPPER(ret_type, func, ...) 93#elif defined(_WIN32) 94# if defined(_DLL) // DLL CRT 95# define WRAP(x) x 96# define WRAPPER_NAME(x) #x 97# define INTERCEPTOR_ATTRIBUTE 98# else // Static CRT 99# define WRAP(x) wrap_##x 100# define WRAPPER_NAME(x) "wrap_"#x 101# define INTERCEPTOR_ATTRIBUTE 102# endif 103# define DECLARE_WRAPPER(ret_type, func, ...) 104#else 105# define WRAP(x) __interceptor_ ## x 106# define WRAPPER_NAME(x) "__interceptor_" #x 107# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default"))) 108# define DECLARE_WRAPPER(ret_type, func, ...) \ 109 extern "C" ret_type func(__VA_ARGS__) \ 110 __attribute__((weak, alias("__interceptor_" #func), visibility("default"))); 111#endif 112 113#if !defined(__APPLE__) 114# define PTR_TO_REAL(x) real_##x 115# define REAL(x) __interception::PTR_TO_REAL(x) 116# define FUNC_TYPE(x) x##_f 117 118# define DECLARE_REAL(ret_type, func, ...) \ 119 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ 120 namespace __interception { \ 121 extern FUNC_TYPE(func) PTR_TO_REAL(func); \ 122 } 123#else // __APPLE__ 124# define REAL(x) x 125# define DECLARE_REAL(ret_type, func, ...) \ 126 extern "C" ret_type func(__VA_ARGS__); 127#endif // __APPLE__ 128 129#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \ 130 DECLARE_REAL(ret_type, func, __VA_ARGS__) \ 131 extern "C" ret_type WRAP(func)(__VA_ARGS__); 132 133// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR 134// macros does its job. In exceptional cases you may need to call REAL(foo) 135// without defining INTERCEPTOR(..., foo, ...). For example, if you override 136// foo with an interceptor for other function. 137#if !defined(__APPLE__) 138# define DEFINE_REAL(ret_type, func, ...) \ 139 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ 140 namespace __interception { \ 141 FUNC_TYPE(func) PTR_TO_REAL(func); \ 142 } 143#else 144# define DEFINE_REAL(ret_type, func, ...) 145#endif 146 147#define INTERCEPTOR(ret_type, func, ...) \ 148 DEFINE_REAL(ret_type, func, __VA_ARGS__) \ 149 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \ 150 extern "C" \ 151 INTERCEPTOR_ATTRIBUTE \ 152 ret_type WRAP(func)(__VA_ARGS__) 153 154#if defined(_WIN32) 155# define INTERCEPTOR_WINAPI(ret_type, func, ...) \ 156 typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \ 157 namespace __interception { \ 158 FUNC_TYPE(func) PTR_TO_REAL(func); \ 159 } \ 160 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \ 161 extern "C" \ 162 INTERCEPTOR_ATTRIBUTE \ 163 ret_type __stdcall WRAP(func)(__VA_ARGS__) 164#endif 165 166// ISO C++ forbids casting between pointer-to-function and pointer-to-object, 167// so we use casting via an integral type __interception::uptr, 168// assuming that system is POSIX-compliant. Using other hacks seem 169// challenging, as we don't even pass function type to 170// INTERCEPT_FUNCTION macro, only its name. 171namespace __interception { 172#if defined(_WIN64) 173typedef unsigned long long uptr; // NOLINT 174#else 175typedef unsigned long uptr; // NOLINT 176#endif // _WIN64 177} // namespace __interception 178 179#define INCLUDED_FROM_INTERCEPTION_LIB 180 181#if defined(__linux__) 182# include "interception_linux.h" 183# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func) 184#elif defined(__APPLE__) 185# include "interception_mac.h" 186# define OVERRIDE_FUNCTION(old_func, new_func) \ 187 OVERRIDE_FUNCTION_MAC(old_func, new_func) 188# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func) 189#else // defined(_WIN32) 190# include "interception_win.h" 191# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func) 192#endif 193 194#undef INCLUDED_FROM_INTERCEPTION_LIB 195 196#endif // INTERCEPTION_H 197