interception.h revision 9213e07a7f95b3e634b97a7bba70849badcf2c8a
15b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov//===-- interception.h ------------------------------------------*- C++ -*-===// 25b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// 35b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// The LLVM Compiler Infrastructure 45b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// 55b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// This file is distributed under the University of Illinois Open Source 65b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// License. See LICENSE.TXT for details. 75b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// 85b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov//===----------------------------------------------------------------------===// 95b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// 105b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// This file is a part of AddressSanitizer, an address sanity checker. 115b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// 125b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// Machinery for providing replacements/wrappers for system functions. 135b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov//===----------------------------------------------------------------------===// 145b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 155b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#ifndef INTERCEPTION_H 165b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#define INTERCEPTION_H 175b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 185b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) 195b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# error "Interception doesn't work on this operating system." 205b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#endif 215b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 22216719b23134b9719bbf3e1de4a7d5d351c21fa8Alexey Samsonov#include "sanitizer_common/sanitizer_internal_defs.h" 236afa1b0406f5cce7256d4f8717bfe394a16999b5Kostya Serebryany 246afa1b0406f5cce7256d4f8717bfe394a16999b5Kostya Serebryany// These typedefs should be used only in the interceptor definitions to replace 256afa1b0406f5cce7256d4f8717bfe394a16999b5Kostya Serebryany// the standard system types (e.g. SSIZE_T instead of ssize_t) 266fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::uptr SIZE_T; 276fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::sptr SSIZE_T; 286fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::sptr PTRDIFF_T; 296fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::s64 INTMAX_T; 306fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::OFF_T OFF_T; 316fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::OFF64_T OFF64_T; 326afa1b0406f5cce7256d4f8717bfe394a16999b5Kostya Serebryany 335b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// How to add an interceptor: 345b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// Suppose you need to wrap/replace system function (generally, from libc): 355b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// int foo(const char *bar, double baz); 365b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// You'll need to: 375b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in 386a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// your source file. See the notes below for cases when 396a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// INTERCEPTOR_WITH_SUFFIX(...) should be used instead. 405b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo". 415b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was 425b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// intercepted successfully. 435b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// You can access original function by calling REAL(foo)(bar, baz). 445b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// By default, REAL(foo) will be visible only inside your interceptor, and if 455b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// you want to use it in other parts of RTL, you'll need to: 46b4fefa713da3dabda1cd83ae4182c71f1683f02cAlexey Samsonov// 3a) add DECLARE_REAL(int, foo, const char*, double) to a 475b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// header file. 485b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// However, if the call "INTERCEPT_FUNCTION(foo)" and definition for 495b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to: 50b4fefa713da3dabda1cd83ae4182c71f1683f02cAlexey Samsonov// 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double) 515b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// to a header file. 525b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 5350a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or 54b4fefa713da3dabda1cd83ae4182c71f1683f02cAlexey Samsonov// DECLARE_REAL(...) are located inside namespaces. 5550a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to 565b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// effectively redirect calls from "foo" to "zoo". In this case 575b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// you aren't required to implement 58b4fefa713da3dabda1cd83ae4182c71f1683f02cAlexey Samsonov// INTERCEPTOR(int, foo, const char *bar, double baz) {...} 595b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// but instead you'll have to add 6050a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// DECLARE_REAL(int, foo, const char *bar, double baz) in your 615b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// source file (to define a pointer to overriden function). 626a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// 3. Some Mac functions have symbol variants discriminated by 636a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// additional suffixes, e.g. _$UNIX2003 (see 646a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html 656a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// for more details). To intercept such functions you need to use the 666a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// INTERCEPTOR_WITH_SUFFIX(...) macro. 675b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 685b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// How it works: 69580469d7e40e39319cb2d3750edac4bccca18105Dmitry Vyukov// To replace system functions on Linux we just need to declare functions 705b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// with same names in our library and then obtain the real function pointers 71580469d7e40e39319cb2d3750edac4bccca18105Dmitry Vyukov// using dlsym(). 72580469d7e40e39319cb2d3750edac4bccca18105Dmitry Vyukov// There is one complication. A user may also intercept some of the functions 73f54c0e3321e2381fca3f02faefaaa6639d59c7cfDmitry Vyukov// we intercept. To resolve this we declare our interceptors with __interceptor_ 74f54c0e3321e2381fca3f02faefaaa6639d59c7cfDmitry Vyukov// prefix, and then make actual interceptors weak aliases to __interceptor_ 75580469d7e40e39319cb2d3750edac4bccca18105Dmitry Vyukov// functions. 7650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// 77580469d7e40e39319cb2d3750edac4bccca18105Dmitry Vyukov// This is not so on Mac OS, where the two-level namespace makes 785b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// our replacement functions invisible to other libraries. This may be overcomed 795b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared 8069563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// libraries in Chromium were noticed when doing so. 8169563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// Instead we create a dylib containing a __DATA,__interpose section that 8269563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// associates library functions with their wrappers. When this dylib is 8369563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all 8469563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// the calls to interposed functions done through stubs to the wrapper 8569563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// functions. 8650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// As it's decided at compile time which functions are to be intercepted on Mac, 8750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// INTERCEPT_FUNCTION() is effectively a no-op on this system. 885b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 895b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#if defined(__APPLE__) 906a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko#include <sys/cdefs.h> // For __DARWIN_ALIAS_C(). 9150a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 9250a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// Just a pair of pointers. 9350a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenkostruct interpose_substitution { 9450a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko const uptr replacement; 9550a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko const uptr original; 9650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko}; 9750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 9850a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// For a function foo() create a global pair of pointers { wrap_foo, foo } in 9950a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// the __DATA,__interpose section. 10050a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// As a result all the calls to foo() will be routed to wrap_foo() at runtime. 10150a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#define INTERPOSER(func_name) __attribute__((used)) \ 10250a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenkoconst interpose_substitution substitution_##func_name[] \ 10350a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko __attribute__((section("__DATA, __interpose"))) = { \ 10450a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko { reinterpret_cast<const uptr>(WRAP(func_name)), \ 10550a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko reinterpret_cast<const uptr>(func_name) } \ 10650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko} 10750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 10850a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// For a function foo() and a wrapper function bar() create a global pair 10950a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// of pointers { bar, foo } in the __DATA,__interpose section. 11050a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// As a result all the calls to foo() will be routed to bar() at runtime. 11150a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \ 11250a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenkoconst interpose_substitution substitution_##func_name[] \ 11350a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko __attribute__((section("__DATA, __interpose"))) = { \ 11450a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko { reinterpret_cast<const uptr>(wrapper_name), \ 11550a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko reinterpret_cast<const uptr>(func_name) } \ 11650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko} 11750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 1185b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define WRAP(x) wrap_##x 1195b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define WRAPPER_NAME(x) "wrap_"#x 1205b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define INTERCEPTOR_ATTRIBUTE 121b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov# define DECLARE_WRAPPER(ret_type, func, ...) 12250a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 1235b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#elif defined(_WIN32) 1242716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# if defined(_DLL) // DLL CRT 1252716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# define WRAP(x) x 1262716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# define WRAPPER_NAME(x) #x 1272716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# define INTERCEPTOR_ATTRIBUTE 1282716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# else // Static CRT 1292716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# define WRAP(x) wrap_##x 1302716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# define WRAPPER_NAME(x) "wrap_"#x 1312716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# define INTERCEPTOR_ATTRIBUTE 1322716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# endif 1339213e07a7f95b3e634b97a7bba70849badcf2c8aTimur Iskhodzhanov# define DECLARE_WRAPPER(ret_type, func, ...) \ 1349213e07a7f95b3e634b97a7bba70849badcf2c8aTimur Iskhodzhanov extern "C" ret_type func(__VA_ARGS__); 1359213e07a7f95b3e634b97a7bba70849badcf2c8aTimur Iskhodzhanov# define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \ 1369213e07a7f95b3e634b97a7bba70849badcf2c8aTimur Iskhodzhanov extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__); 1375b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#else 138f54c0e3321e2381fca3f02faefaaa6639d59c7cfDmitry Vyukov# define WRAP(x) __interceptor_ ## x 139f54c0e3321e2381fca3f02faefaaa6639d59c7cfDmitry Vyukov# define WRAPPER_NAME(x) "__interceptor_" #x 1405b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default"))) 141b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov# define DECLARE_WRAPPER(ret_type, func, ...) \ 142b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov extern "C" ret_type func(__VA_ARGS__) \ 143ba362a7f0e98b369758f07aec8794c61e688b82dAlexey Samsonov __attribute__((weak, alias("__interceptor_" #func), visibility("default"))); 1445b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#endif 1455b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 14669563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko#if !defined(__APPLE__) 1470ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define PTR_TO_REAL(x) real_##x 1480ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define REAL(x) __interception::PTR_TO_REAL(x) 1490ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define FUNC_TYPE(x) x##_f 1500ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko 1510ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define DECLARE_REAL(ret_type, func, ...) \ 1520ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ 1530ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko namespace __interception { \ 1540ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko extern FUNC_TYPE(func) PTR_TO_REAL(func); \ 1550ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko } 15669563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko#else // __APPLE__ 1570ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define REAL(x) x 1580ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define DECLARE_REAL(ret_type, func, ...) \ 1590ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko extern "C" ret_type func(__VA_ARGS__); 16069563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko#endif // __APPLE__ 1615b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 1626bae39d1db13f60d3e9b8393e5b9d9eb2ab1b5c0Chandler Carruth#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \ 163c27279a4a08e0661f204c3e5dc65aafce79dec8aAlexey Samsonov DECLARE_REAL(ret_type, func, __VA_ARGS__) \ 1645b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov extern "C" ret_type WRAP(func)(__VA_ARGS__); 1655b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 166b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR 167b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov// macros does its job. In exceptional cases you may need to call REAL(foo) 168b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov// without defining INTERCEPTOR(..., foo, ...). For example, if you override 169b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov// foo with an interceptor for other function. 17069563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko#if !defined(__APPLE__) 171b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov# define DEFINE_REAL(ret_type, func, ...) \ 172b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ 1730ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko namespace __interception { \ 1740ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko FUNC_TYPE(func) PTR_TO_REAL(func); \ 1750ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko } 1760ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko#else 177b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov# define DEFINE_REAL(ret_type, func, ...) 1780ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko#endif 179600972e3427173cc8904d741decd1af0ed5de9fdTimur Iskhodzhanov 18050a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#if !defined(__APPLE__) 181b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov#define INTERCEPTOR(ret_type, func, ...) \ 182b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov DEFINE_REAL(ret_type, func, __VA_ARGS__) \ 183b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \ 1845b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov extern "C" \ 1855b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov INTERCEPTOR_ATTRIBUTE \ 186b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov ret_type WRAP(func)(__VA_ARGS__) 1876a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko 1886a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now. 1896a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko#define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \ 1906a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko INTERCEPTOR(ret_type, func, __VA_ARGS__) 1916a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko 19250a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#else // __APPLE__ 1936a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko 1946a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko#define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \ 1956a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko extern "C" ret_type func(__VA_ARGS__) suffix; \ 19650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko extern "C" ret_type WRAP(func)(__VA_ARGS__); \ 19750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko INTERPOSER(func); \ 19850a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__) 19950a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 2006a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko#define INTERCEPTOR(ret_type, func, ...) \ 2016a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__) 2026a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko 2036a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko#define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \ 2046a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__) 2056a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko 20650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// Override |overridee| with |overrider|. 20750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#define OVERRIDE_FUNCTION(overridee, overrider) \ 20850a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko INTERPOSER_2(overridee, WRAP(overrider)) 20950a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#endif 210600972e3427173cc8904d741decd1af0ed5de9fdTimur Iskhodzhanov 211600972e3427173cc8904d741decd1af0ed5de9fdTimur Iskhodzhanov#if defined(_WIN32) 212600972e3427173cc8904d741decd1af0ed5de9fdTimur Iskhodzhanov# define INTERCEPTOR_WINAPI(ret_type, func, ...) \ 213b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \ 214b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov namespace __interception { \ 215b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov FUNC_TYPE(func) PTR_TO_REAL(func); \ 216b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov } \ 2179213e07a7f95b3e634b97a7bba70849badcf2c8aTimur Iskhodzhanov DECLARE_WRAPPER_WINAPI(ret_type, func, __VA_ARGS__) \ 218b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov extern "C" \ 219b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov INTERCEPTOR_ATTRIBUTE \ 220b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov ret_type __stdcall WRAP(func)(__VA_ARGS__) 221600972e3427173cc8904d741decd1af0ed5de9fdTimur Iskhodzhanov#endif 2225b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 223592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov// ISO C++ forbids casting between pointer-to-function and pointer-to-object, 224592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov// so we use casting via an integral type __interception::uptr, 225592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov// assuming that system is POSIX-compliant. Using other hacks seem 226592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov// challenging, as we don't even pass function type to 227592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov// INTERCEPT_FUNCTION macro, only its name. 228592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonovnamespace __interception { 229b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonov#if defined(_WIN64) 230b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonovtypedef unsigned long long uptr; // NOLINT 231b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonov#else 232592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonovtypedef unsigned long uptr; // NOLINT 233b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonov#endif // _WIN64 234592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov} // namespace __interception 235592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov 2365b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#define INCLUDED_FROM_INTERCEPTION_LIB 2375b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 2385b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#if defined(__linux__) 2395b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# include "interception_linux.h" 2405b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func) 2415b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#elif defined(__APPLE__) 2425b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# include "interception_mac.h" 2435b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func) 2445b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#else // defined(_WIN32) 24507bb9f1e3600195119aec1aae1aa48a6ed2f5febTimur Iskhodzhanov# include "interception_win.h" 24607bb9f1e3600195119aec1aae1aa48a6ed2f5febTimur Iskhodzhanov# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func) 2475b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#endif 2485b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 2495b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#undef INCLUDED_FROM_INTERCEPTION_LIB 2505b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 2515b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#endif // INTERCEPTION_H 252