10b57cec5SDimitry Andric //===-- interception.h ------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file is a part of AddressSanitizer, an address sanity checker. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric // Machinery for providing replacements/wrappers for system functions. 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #ifndef INTERCEPTION_H 150b57cec5SDimitry Andric #define INTERCEPTION_H 160b57cec5SDimitry Andric 1706c3fb27SDimitry Andric #include "sanitizer_common/sanitizer_asm.h" 180b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_internal_defs.h" 190b57cec5SDimitry Andric 2081ad6265SDimitry Andric #if !SANITIZER_LINUX && !SANITIZER_FREEBSD && !SANITIZER_APPLE && \ 21fe6060f1SDimitry Andric !SANITIZER_NETBSD && !SANITIZER_WINDOWS && !SANITIZER_FUCHSIA && \ 22fe6060f1SDimitry Andric !SANITIZER_SOLARIS 230b57cec5SDimitry Andric # error "Interception doesn't work on this operating system." 240b57cec5SDimitry Andric #endif 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric // These typedefs should be used only in the interceptor definitions to replace 270b57cec5SDimitry Andric // the standard system types (e.g. SSIZE_T instead of ssize_t) 280b57cec5SDimitry Andric typedef __sanitizer::uptr SIZE_T; 290b57cec5SDimitry Andric typedef __sanitizer::sptr SSIZE_T; 300b57cec5SDimitry Andric typedef __sanitizer::sptr PTRDIFF_T; 310b57cec5SDimitry Andric typedef __sanitizer::s64 INTMAX_T; 320b57cec5SDimitry Andric typedef __sanitizer::u64 UINTMAX_T; 330b57cec5SDimitry Andric typedef __sanitizer::OFF_T OFF_T; 340b57cec5SDimitry Andric typedef __sanitizer::OFF64_T OFF64_T; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric // How to add an interceptor: 370b57cec5SDimitry Andric // Suppose you need to wrap/replace system function (generally, from libc): 380b57cec5SDimitry Andric // int foo(const char *bar, double baz); 390b57cec5SDimitry Andric // You'll need to: 400b57cec5SDimitry Andric // 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in 410b57cec5SDimitry Andric // your source file. See the notes below for cases when 420b57cec5SDimitry Andric // INTERCEPTOR_WITH_SUFFIX(...) should be used instead. 430b57cec5SDimitry Andric // 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo". 440b57cec5SDimitry Andric // INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was 450b57cec5SDimitry Andric // intercepted successfully. 460b57cec5SDimitry Andric // You can access original function by calling REAL(foo)(bar, baz). 470b57cec5SDimitry Andric // By default, REAL(foo) will be visible only inside your interceptor, and if 480b57cec5SDimitry Andric // you want to use it in other parts of RTL, you'll need to: 490b57cec5SDimitry Andric // 3a) add DECLARE_REAL(int, foo, const char*, double) to a 500b57cec5SDimitry Andric // header file. 510b57cec5SDimitry Andric // However, if the call "INTERCEPT_FUNCTION(foo)" and definition for 520b57cec5SDimitry Andric // INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to: 530b57cec5SDimitry Andric // 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double) 540b57cec5SDimitry Andric // to a header file. 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric // Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or 570b57cec5SDimitry Andric // DECLARE_REAL(...) are located inside namespaces. 580b57cec5SDimitry Andric // 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to 590b57cec5SDimitry Andric // effectively redirect calls from "foo" to "zoo". In this case 600b57cec5SDimitry Andric // you aren't required to implement 610b57cec5SDimitry Andric // INTERCEPTOR(int, foo, const char *bar, double baz) {...} 620b57cec5SDimitry Andric // but instead you'll have to add 630b57cec5SDimitry Andric // DECLARE_REAL(int, foo, const char *bar, double baz) in your 640b57cec5SDimitry Andric // source file (to define a pointer to overriden function). 650b57cec5SDimitry Andric // 3. Some Mac functions have symbol variants discriminated by 660b57cec5SDimitry Andric // additional suffixes, e.g. _$UNIX2003 (see 670b57cec5SDimitry Andric // https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html 680b57cec5SDimitry Andric // for more details). To intercept such functions you need to use the 690b57cec5SDimitry Andric // INTERCEPTOR_WITH_SUFFIX(...) macro. 700b57cec5SDimitry Andric 7106c3fb27SDimitry Andric // How it works on Linux 7206c3fb27SDimitry Andric // --------------------- 730b57cec5SDimitry Andric // 7406c3fb27SDimitry Andric // To replace system functions on Linux we just need to declare functions with 7506c3fb27SDimitry Andric // the same names in our library and then obtain the real function pointers 7606c3fb27SDimitry Andric // using dlsym(). 7706c3fb27SDimitry Andric // 7806c3fb27SDimitry Andric // There is one complication: a user may also intercept some of the functions we 7906c3fb27SDimitry Andric // intercept. To allow for up to 3 interceptors (including ours) of a given 8006c3fb27SDimitry Andric // function "func", the interceptor implementation is in ___interceptor_func, 8106c3fb27SDimitry Andric // which is aliased by a weak function __interceptor_func, which in turn is 8206c3fb27SDimitry Andric // aliased (via a trampoline) by weak wrapper function "func". 8306c3fb27SDimitry Andric // 8406c3fb27SDimitry Andric // Most user interceptors should define a foreign interceptor as follows: 8506c3fb27SDimitry Andric // 8606c3fb27SDimitry Andric // - provide a non-weak function "func" that performs interception; 8706c3fb27SDimitry Andric // - if __interceptor_func exists, call it to perform the real functionality; 8806c3fb27SDimitry Andric // - if it does not exist, figure out the real function and call it instead. 8906c3fb27SDimitry Andric // 9006c3fb27SDimitry Andric // In rare cases, a foreign interceptor (of another dynamic analysis runtime) 9106c3fb27SDimitry Andric // may be defined as follows (on supported architectures): 9206c3fb27SDimitry Andric // 9306c3fb27SDimitry Andric // - provide a non-weak function __interceptor_func that performs interception; 9406c3fb27SDimitry Andric // - if ___interceptor_func exists, call it to perform the real functionality; 9506c3fb27SDimitry Andric // - if it does not exist, figure out the real function and call it instead; 9606c3fb27SDimitry Andric // - provide a weak function "func" that is an alias to __interceptor_func. 9706c3fb27SDimitry Andric // 9806c3fb27SDimitry Andric // With this protocol, sanitizer interceptors, foreign user interceptors, and 9906c3fb27SDimitry Andric // foreign interceptors of other dynamic analysis runtimes, or any combination 10006c3fb27SDimitry Andric // thereof, may co-exist simultaneously. 10106c3fb27SDimitry Andric // 10206c3fb27SDimitry Andric // How it works on Mac OS 10306c3fb27SDimitry Andric // ---------------------- 10406c3fb27SDimitry Andric // 10506c3fb27SDimitry Andric // This is not so on Mac OS, where the two-level namespace makes our replacement 10606c3fb27SDimitry Andric // functions invisible to other libraries. This may be overcomed using the 10706c3fb27SDimitry Andric // DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared libraries in 10806c3fb27SDimitry Andric // Chromium were noticed when doing so. 10906c3fb27SDimitry Andric // 1100b57cec5SDimitry Andric // Instead we create a dylib containing a __DATA,__interpose section that 1110b57cec5SDimitry Andric // associates library functions with their wrappers. When this dylib is 11206c3fb27SDimitry Andric // preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all the 11306c3fb27SDimitry Andric // calls to interposed functions done through stubs to the wrapper functions. 11406c3fb27SDimitry Andric // 1150b57cec5SDimitry Andric // As it's decided at compile time which functions are to be intercepted on Mac, 1160b57cec5SDimitry Andric // INTERCEPT_FUNCTION() is effectively a no-op on this system. 1170b57cec5SDimitry Andric 11881ad6265SDimitry Andric #if SANITIZER_APPLE 1190b57cec5SDimitry Andric #include <sys/cdefs.h> // For __DARWIN_ALIAS_C(). 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric // Just a pair of pointers. 1220b57cec5SDimitry Andric struct interpose_substitution { 1230b57cec5SDimitry Andric const __sanitizer::uptr replacement; 1240b57cec5SDimitry Andric const __sanitizer::uptr original; 1250b57cec5SDimitry Andric }; 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric // For a function foo() create a global pair of pointers { wrap_foo, foo } in 1280b57cec5SDimitry Andric // the __DATA,__interpose section. 1290b57cec5SDimitry Andric // As a result all the calls to foo() will be routed to wrap_foo() at runtime. 1300b57cec5SDimitry Andric #define INTERPOSER(func_name) __attribute__((used)) \ 1310b57cec5SDimitry Andric const interpose_substitution substitution_##func_name[] \ 1320b57cec5SDimitry Andric __attribute__((section("__DATA, __interpose"))) = { \ 1330b57cec5SDimitry Andric { reinterpret_cast<const uptr>(WRAP(func_name)), \ 1340b57cec5SDimitry Andric reinterpret_cast<const uptr>(func_name) } \ 1350b57cec5SDimitry Andric } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric // For a function foo() and a wrapper function bar() create a global pair 1380b57cec5SDimitry Andric // of pointers { bar, foo } in the __DATA,__interpose section. 1390b57cec5SDimitry Andric // As a result all the calls to foo() will be routed to bar() at runtime. 1400b57cec5SDimitry Andric #define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \ 1410b57cec5SDimitry Andric const interpose_substitution substitution_##func_name[] \ 1420b57cec5SDimitry Andric __attribute__((section("__DATA, __interpose"))) = { \ 1430b57cec5SDimitry Andric { reinterpret_cast<const uptr>(wrapper_name), \ 1440b57cec5SDimitry Andric reinterpret_cast<const uptr>(func_name) } \ 1450b57cec5SDimitry Andric } 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric # define WRAP(x) wrap_##x 14806c3fb27SDimitry Andric # define TRAMPOLINE(x) WRAP(x) 1490b57cec5SDimitry Andric # define INTERCEPTOR_ATTRIBUTE 1500b57cec5SDimitry Andric # define DECLARE_WRAPPER(ret_type, func, ...) 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric #elif SANITIZER_WINDOWS 1530b57cec5SDimitry Andric # define WRAP(x) __asan_wrap_##x 15406c3fb27SDimitry Andric # define TRAMPOLINE(x) WRAP(x) 1550b57cec5SDimitry Andric # define INTERCEPTOR_ATTRIBUTE __declspec(dllexport) 1560b57cec5SDimitry Andric # define DECLARE_WRAPPER(ret_type, func, ...) \ 1570b57cec5SDimitry Andric extern "C" ret_type func(__VA_ARGS__); 1580b57cec5SDimitry Andric # define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \ 1590b57cec5SDimitry Andric extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__); 16006c3fb27SDimitry Andric #elif !SANITIZER_FUCHSIA // LINUX, FREEBSD, NETBSD, SOLARIS 1610b57cec5SDimitry Andric # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default"))) 16206c3fb27SDimitry Andric # if ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT 16306c3fb27SDimitry Andric // Weak aliases of weak aliases do not work, therefore we need to set up a 16406c3fb27SDimitry Andric // trampoline function. The function "func" is a weak alias to the trampoline 16506c3fb27SDimitry Andric // (so that we may check if "func" was overridden), which calls the weak 16606c3fb27SDimitry Andric // function __interceptor_func, which in turn aliases the actual interceptor 16706c3fb27SDimitry Andric // implementation ___interceptor_func: 16806c3fb27SDimitry Andric // 16906c3fb27SDimitry Andric // [wrapper "func": weak] --(alias)--> [TRAMPOLINE(func)] 17006c3fb27SDimitry Andric // | 17106c3fb27SDimitry Andric // +--------(tail call)-------+ 17206c3fb27SDimitry Andric // | 17306c3fb27SDimitry Andric // v 17406c3fb27SDimitry Andric // [__interceptor_func: weak] --(alias)--> [WRAP(func)] 17506c3fb27SDimitry Andric // 17606c3fb27SDimitry Andric // We use inline assembly to define most of this, because not all compilers 17706c3fb27SDimitry Andric // support functions with the "naked" attribute with every architecture. 17806c3fb27SDimitry Andric # define WRAP(x) ___interceptor_ ## x 17906c3fb27SDimitry Andric # define TRAMPOLINE(x) __interceptor_trampoline_ ## x 18006c3fb27SDimitry Andric # if SANITIZER_FREEBSD || SANITIZER_NETBSD 1810b57cec5SDimitry Andric // FreeBSD's dynamic linker (incompliantly) gives non-weak symbols higher 1820b57cec5SDimitry Andric // priority than weak ones so weak aliases won't work for indirect calls 1830b57cec5SDimitry Andric // in position-independent (-fPIC / -fPIE) mode. 184*8a4dda33SDimitry Andric # define __ASM_WEAK_WRAPPER(func) ".globl " #func "\n" 18506c3fb27SDimitry Andric # else 18606c3fb27SDimitry Andric # define __ASM_WEAK_WRAPPER(func) ".weak " #func "\n" 18706c3fb27SDimitry Andric # endif // SANITIZER_FREEBSD || SANITIZER_NETBSD 18806c3fb27SDimitry Andric // Keep trampoline implementation in sync with sanitizer_common/sanitizer_asm.h 1890b57cec5SDimitry Andric # define DECLARE_WRAPPER(ret_type, func, ...) \ 19006c3fb27SDimitry Andric extern "C" ret_type func(__VA_ARGS__); \ 19106c3fb27SDimitry Andric extern "C" ret_type TRAMPOLINE(func)(__VA_ARGS__); \ 19206c3fb27SDimitry Andric extern "C" ret_type __interceptor_##func(__VA_ARGS__) \ 19306c3fb27SDimitry Andric INTERCEPTOR_ATTRIBUTE __attribute__((weak)) ALIAS(WRAP(func)); \ 19406c3fb27SDimitry Andric asm( \ 19506c3fb27SDimitry Andric ".text\n" \ 19606c3fb27SDimitry Andric __ASM_WEAK_WRAPPER(func) \ 19706c3fb27SDimitry Andric ".set " #func ", " SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n" \ 19806c3fb27SDimitry Andric ".globl " SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n" \ 19906c3fb27SDimitry Andric ".type " SANITIZER_STRINGIFY(TRAMPOLINE(func)) ", %function\n" \ 20006c3fb27SDimitry Andric SANITIZER_STRINGIFY(TRAMPOLINE(func)) ":\n" \ 20106c3fb27SDimitry Andric SANITIZER_STRINGIFY(CFI_STARTPROC) "\n" \ 20206c3fb27SDimitry Andric SANITIZER_STRINGIFY(ASM_TAIL_CALL) " __interceptor_" \ 20306c3fb27SDimitry Andric SANITIZER_STRINGIFY(ASM_PREEMPTIBLE_SYM(func)) "\n" \ 20406c3fb27SDimitry Andric SANITIZER_STRINGIFY(CFI_ENDPROC) "\n" \ 20506c3fb27SDimitry Andric ".size " SANITIZER_STRINGIFY(TRAMPOLINE(func)) ", " \ 20606c3fb27SDimitry Andric ".-" SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n" \ 20706c3fb27SDimitry Andric ); 20806c3fb27SDimitry Andric # else // ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT 20906c3fb27SDimitry Andric // Some architectures cannot implement efficient interceptor trampolines with 21006c3fb27SDimitry Andric // just a plain jump due to complexities of resolving a preemptible symbol. In 21106c3fb27SDimitry Andric // those cases, revert to just this scheme: 21206c3fb27SDimitry Andric // 21306c3fb27SDimitry Andric // [wrapper "func": weak] --(alias)--> [WRAP(func)] 21406c3fb27SDimitry Andric // 2150b57cec5SDimitry Andric # define WRAP(x) __interceptor_ ## x 21606c3fb27SDimitry Andric # define TRAMPOLINE(x) WRAP(x) 21706c3fb27SDimitry Andric # if SANITIZER_FREEBSD || SANITIZER_NETBSD 21806c3fb27SDimitry Andric # define __ATTRIBUTE_WEAK_WRAPPER 21906c3fb27SDimitry Andric # else 22006c3fb27SDimitry Andric # define __ATTRIBUTE_WEAK_WRAPPER __attribute__((weak)) 22106c3fb27SDimitry Andric # endif // SANITIZER_FREEBSD || SANITIZER_NETBSD 2220b57cec5SDimitry Andric # define DECLARE_WRAPPER(ret_type, func, ...) \ 2230b57cec5SDimitry Andric extern "C" ret_type func(__VA_ARGS__) \ 22406c3fb27SDimitry Andric INTERCEPTOR_ATTRIBUTE __ATTRIBUTE_WEAK_WRAPPER ALIAS(WRAP(func)); 22506c3fb27SDimitry Andric # endif // ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT 2260b57cec5SDimitry Andric #endif 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric #if SANITIZER_FUCHSIA 2290b57cec5SDimitry Andric // There is no general interception at all on Fuchsia. 2300b57cec5SDimitry Andric // Sanitizer runtimes just define functions directly to preempt them, 2310b57cec5SDimitry Andric // and have bespoke ways to access the underlying libc functions. 2320b57cec5SDimitry Andric # include <zircon/sanitizer.h> 2330b57cec5SDimitry Andric # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default"))) 2340b57cec5SDimitry Andric # define REAL(x) __unsanitized_##x 2350b57cec5SDimitry Andric # define DECLARE_REAL(ret_type, func, ...) 23681ad6265SDimitry Andric #elif !SANITIZER_APPLE 2370b57cec5SDimitry Andric # define PTR_TO_REAL(x) real_##x 2380b57cec5SDimitry Andric # define REAL(x) __interception::PTR_TO_REAL(x) 2390b57cec5SDimitry Andric # define FUNC_TYPE(x) x##_type 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric # define DECLARE_REAL(ret_type, func, ...) \ 2420b57cec5SDimitry Andric typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ 2430b57cec5SDimitry Andric namespace __interception { \ 2440b57cec5SDimitry Andric extern FUNC_TYPE(func) PTR_TO_REAL(func); \ 2450b57cec5SDimitry Andric } 2460b57cec5SDimitry Andric # define ASSIGN_REAL(dst, src) REAL(dst) = REAL(src) 24781ad6265SDimitry Andric #else // SANITIZER_APPLE 2480b57cec5SDimitry Andric # define REAL(x) x 2490b57cec5SDimitry Andric # define DECLARE_REAL(ret_type, func, ...) \ 2500b57cec5SDimitry Andric extern "C" ret_type func(__VA_ARGS__); 2510b57cec5SDimitry Andric # define ASSIGN_REAL(x, y) 25281ad6265SDimitry Andric #endif // SANITIZER_APPLE 2530b57cec5SDimitry Andric 254fe6060f1SDimitry Andric #if !SANITIZER_FUCHSIA 2550b57cec5SDimitry Andric # define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \ 2560b57cec5SDimitry Andric DECLARE_REAL(ret_type, func, __VA_ARGS__) \ 25706c3fb27SDimitry Andric extern "C" ret_type TRAMPOLINE(func)(__VA_ARGS__); \ 2580b57cec5SDimitry Andric extern "C" ret_type WRAP(func)(__VA_ARGS__); 2590b57cec5SDimitry Andric // Declare an interceptor and its wrapper defined in a different translation 2600b57cec5SDimitry Andric // unit (ex. asm). 2610b57cec5SDimitry Andric # define DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(ret_type, func, ...) \ 26206c3fb27SDimitry Andric extern "C" ret_type TRAMPOLINE(func)(__VA_ARGS__); \ 2630b57cec5SDimitry Andric extern "C" ret_type WRAP(func)(__VA_ARGS__); \ 2640b57cec5SDimitry Andric extern "C" ret_type func(__VA_ARGS__); 2650b57cec5SDimitry Andric #else 2660b57cec5SDimitry Andric # define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) 2670b57cec5SDimitry Andric # define DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(ret_type, func, ...) 2680b57cec5SDimitry Andric #endif 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric // Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR 2710b57cec5SDimitry Andric // macros does its job. In exceptional cases you may need to call REAL(foo) 2720b57cec5SDimitry Andric // without defining INTERCEPTOR(..., foo, ...). For example, if you override 2730b57cec5SDimitry Andric // foo with an interceptor for other function. 27481ad6265SDimitry Andric #if !SANITIZER_APPLE && !SANITIZER_FUCHSIA 2750b57cec5SDimitry Andric # define DEFINE_REAL(ret_type, func, ...) \ 2760b57cec5SDimitry Andric typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ 2770b57cec5SDimitry Andric namespace __interception { \ 2780b57cec5SDimitry Andric FUNC_TYPE(func) PTR_TO_REAL(func); \ 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric #else 2810b57cec5SDimitry Andric # define DEFINE_REAL(ret_type, func, ...) 2820b57cec5SDimitry Andric #endif 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric #if SANITIZER_FUCHSIA 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric // We need to define the __interceptor_func name just to get 2870b57cec5SDimitry Andric // sanitizer_common/scripts/gen_dynamic_list.py to export func. 2880b57cec5SDimitry Andric // But we don't need to export __interceptor_func to get that. 2890b57cec5SDimitry Andric #define INTERCEPTOR(ret_type, func, ...) \ 2900b57cec5SDimitry Andric extern "C"[[ gnu::alias(#func), gnu::visibility("hidden") ]] ret_type \ 2910b57cec5SDimitry Andric __interceptor_##func(__VA_ARGS__); \ 2920b57cec5SDimitry Andric extern "C" INTERCEPTOR_ATTRIBUTE ret_type func(__VA_ARGS__) 2930b57cec5SDimitry Andric 29481ad6265SDimitry Andric #elif !SANITIZER_APPLE 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric #define INTERCEPTOR(ret_type, func, ...) \ 2970b57cec5SDimitry Andric DEFINE_REAL(ret_type, func, __VA_ARGS__) \ 2980b57cec5SDimitry Andric DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \ 29906c3fb27SDimitry Andric extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__) 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric // We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now. 3020b57cec5SDimitry Andric #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \ 3030b57cec5SDimitry Andric INTERCEPTOR(ret_type, func, __VA_ARGS__) 3040b57cec5SDimitry Andric 30581ad6265SDimitry Andric #else // SANITIZER_APPLE 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric #define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \ 3080b57cec5SDimitry Andric extern "C" ret_type func(__VA_ARGS__) suffix; \ 3090b57cec5SDimitry Andric extern "C" ret_type WRAP(func)(__VA_ARGS__); \ 3100b57cec5SDimitry Andric INTERPOSER(func); \ 3110b57cec5SDimitry Andric extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__) 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric #define INTERCEPTOR(ret_type, func, ...) \ 3140b57cec5SDimitry Andric INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__) 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \ 3170b57cec5SDimitry Andric INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__) 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric // Override |overridee| with |overrider|. 3200b57cec5SDimitry Andric #define OVERRIDE_FUNCTION(overridee, overrider) \ 3210b57cec5SDimitry Andric INTERPOSER_2(overridee, WRAP(overrider)) 3220b57cec5SDimitry Andric #endif 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric #if SANITIZER_WINDOWS 3250b57cec5SDimitry Andric # define INTERCEPTOR_WINAPI(ret_type, func, ...) \ 3260b57cec5SDimitry Andric typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \ 3270b57cec5SDimitry Andric namespace __interception { \ 3280b57cec5SDimitry Andric FUNC_TYPE(func) PTR_TO_REAL(func); \ 3290b57cec5SDimitry Andric } \ 33006c3fb27SDimitry Andric extern "C" INTERCEPTOR_ATTRIBUTE ret_type __stdcall WRAP(func)(__VA_ARGS__) 3310b57cec5SDimitry Andric #endif 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric // ISO C++ forbids casting between pointer-to-function and pointer-to-object, 3340b57cec5SDimitry Andric // so we use casting via an integral type __interception::uptr, 3350b57cec5SDimitry Andric // assuming that system is POSIX-compliant. Using other hacks seem 3360b57cec5SDimitry Andric // challenging, as we don't even pass function type to 3370b57cec5SDimitry Andric // INTERCEPT_FUNCTION macro, only its name. 3380b57cec5SDimitry Andric namespace __interception { 3390b57cec5SDimitry Andric #if defined(_WIN64) 34068d75effSDimitry Andric typedef unsigned long long uptr; 3410b57cec5SDimitry Andric #else 34268d75effSDimitry Andric typedef unsigned long uptr; 3430b57cec5SDimitry Andric #endif // _WIN64 3440b57cec5SDimitry Andric } // namespace __interception 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric #define INCLUDED_FROM_INTERCEPTION_LIB 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \ 349e8d8bef9SDimitry Andric SANITIZER_SOLARIS 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric # include "interception_linux.h" 3520b57cec5SDimitry Andric # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func) 3530b57cec5SDimitry Andric # define INTERCEPT_FUNCTION_VER(func, symver) \ 3540b57cec5SDimitry Andric INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver) 35581ad6265SDimitry Andric #elif SANITIZER_APPLE 3560b57cec5SDimitry Andric # include "interception_mac.h" 3570b57cec5SDimitry Andric # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func) 3580b57cec5SDimitry Andric # define INTERCEPT_FUNCTION_VER(func, symver) \ 3590b57cec5SDimitry Andric INTERCEPT_FUNCTION_VER_MAC(func, symver) 3600b57cec5SDimitry Andric #elif SANITIZER_WINDOWS 3610b57cec5SDimitry Andric # include "interception_win.h" 3620b57cec5SDimitry Andric # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func) 3630b57cec5SDimitry Andric # define INTERCEPT_FUNCTION_VER(func, symver) \ 3640b57cec5SDimitry Andric INTERCEPT_FUNCTION_VER_WIN(func, symver) 3650b57cec5SDimitry Andric #endif 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric #undef INCLUDED_FROM_INTERCEPTION_LIB 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric #endif // INTERCEPTION_H 370