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 182d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#if !defined(__linux__) && !defined(__FreeBSD__) && \ 192d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines !defined(__APPLE__) && !defined(_WIN32) 205b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# error "Interception doesn't work on this operating system." 215b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#endif 225b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 23216719b23134b9719bbf3e1de4a7d5d351c21fa8Alexey Samsonov#include "sanitizer_common/sanitizer_internal_defs.h" 246afa1b0406f5cce7256d4f8717bfe394a16999b5Kostya Serebryany 256afa1b0406f5cce7256d4f8717bfe394a16999b5Kostya Serebryany// These typedefs should be used only in the interceptor definitions to replace 266afa1b0406f5cce7256d4f8717bfe394a16999b5Kostya Serebryany// the standard system types (e.g. SSIZE_T instead of ssize_t) 276fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::uptr SIZE_T; 286fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::sptr SSIZE_T; 296fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::sptr PTRDIFF_T; 306fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::s64 INTMAX_T; 316fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::OFF_T OFF_T; 326fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanytypedef __sanitizer::OFF64_T OFF64_T; 336afa1b0406f5cce7256d4f8717bfe394a16999b5Kostya Serebryany 345b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// How to add an interceptor: 355b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// Suppose you need to wrap/replace system function (generally, from libc): 365b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// int foo(const char *bar, double baz); 375b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// You'll need to: 385b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in 396a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// your source file. See the notes below for cases when 406a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// INTERCEPTOR_WITH_SUFFIX(...) should be used instead. 415b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo". 425b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was 435b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// intercepted successfully. 445b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// You can access original function by calling REAL(foo)(bar, baz). 455b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// By default, REAL(foo) will be visible only inside your interceptor, and if 465b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// you want to use it in other parts of RTL, you'll need to: 47b4fefa713da3dabda1cd83ae4182c71f1683f02cAlexey Samsonov// 3a) add DECLARE_REAL(int, foo, const char*, double) to a 485b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// header file. 495b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// However, if the call "INTERCEPT_FUNCTION(foo)" and definition for 505b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to: 51b4fefa713da3dabda1cd83ae4182c71f1683f02cAlexey Samsonov// 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double) 525b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// to a header file. 535b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 5450a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or 55b4fefa713da3dabda1cd83ae4182c71f1683f02cAlexey Samsonov// DECLARE_REAL(...) are located inside namespaces. 5650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to 575b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// effectively redirect calls from "foo" to "zoo". In this case 585b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// you aren't required to implement 59b4fefa713da3dabda1cd83ae4182c71f1683f02cAlexey Samsonov// INTERCEPTOR(int, foo, const char *bar, double baz) {...} 605b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// but instead you'll have to add 6150a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// DECLARE_REAL(int, foo, const char *bar, double baz) in your 625b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// source file (to define a pointer to overriden function). 636a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// 3. Some Mac functions have symbol variants discriminated by 646a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// additional suffixes, e.g. _$UNIX2003 (see 656a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html 666a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// for more details). To intercept such functions you need to use the 676a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// INTERCEPTOR_WITH_SUFFIX(...) macro. 685b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 695b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// How it works: 70580469d7e40e39319cb2d3750edac4bccca18105Dmitry Vyukov// To replace system functions on Linux we just need to declare functions 715b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// with same names in our library and then obtain the real function pointers 72580469d7e40e39319cb2d3750edac4bccca18105Dmitry Vyukov// using dlsym(). 73580469d7e40e39319cb2d3750edac4bccca18105Dmitry Vyukov// There is one complication. A user may also intercept some of the functions 74f54c0e3321e2381fca3f02faefaaa6639d59c7cfDmitry Vyukov// we intercept. To resolve this we declare our interceptors with __interceptor_ 75f54c0e3321e2381fca3f02faefaaa6639d59c7cfDmitry Vyukov// prefix, and then make actual interceptors weak aliases to __interceptor_ 76580469d7e40e39319cb2d3750edac4bccca18105Dmitry Vyukov// functions. 7750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// 78580469d7e40e39319cb2d3750edac4bccca18105Dmitry Vyukov// This is not so on Mac OS, where the two-level namespace makes 795b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// our replacement functions invisible to other libraries. This may be overcomed 805b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared 8169563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// libraries in Chromium were noticed when doing so. 8269563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// Instead we create a dylib containing a __DATA,__interpose section that 8369563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// associates library functions with their wrappers. When this dylib is 8469563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all 8569563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// the calls to interposed functions done through stubs to the wrapper 8669563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko// functions. 8750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// As it's decided at compile time which functions are to be intercepted on Mac, 8850a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// INTERCEPT_FUNCTION() is effectively a no-op on this system. 895b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 905b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#if defined(__APPLE__) 916a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko#include <sys/cdefs.h> // For __DARWIN_ALIAS_C(). 9250a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 9350a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// Just a pair of pointers. 9450a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenkostruct interpose_substitution { 9550a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko const uptr replacement; 9650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko const uptr original; 9750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko}; 9850a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 9950a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// For a function foo() create a global pair of pointers { wrap_foo, foo } in 10050a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// the __DATA,__interpose section. 10150a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// As a result all the calls to foo() will be routed to wrap_foo() at runtime. 10250a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#define INTERPOSER(func_name) __attribute__((used)) \ 10350a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenkoconst interpose_substitution substitution_##func_name[] \ 10450a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko __attribute__((section("__DATA, __interpose"))) = { \ 10550a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko { reinterpret_cast<const uptr>(WRAP(func_name)), \ 10650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko reinterpret_cast<const uptr>(func_name) } \ 10750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko} 10850a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 10950a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// For a function foo() and a wrapper function bar() create a global pair 11050a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// of pointers { bar, foo } in the __DATA,__interpose section. 11150a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// As a result all the calls to foo() will be routed to bar() at runtime. 11250a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \ 11350a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenkoconst interpose_substitution substitution_##func_name[] \ 11450a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko __attribute__((section("__DATA, __interpose"))) = { \ 11550a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko { reinterpret_cast<const uptr>(wrapper_name), \ 11650a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko reinterpret_cast<const uptr>(func_name) } \ 11750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko} 11850a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 1195b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define WRAP(x) wrap_##x 1205b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define WRAPPER_NAME(x) "wrap_"#x 1215b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define INTERCEPTOR_ATTRIBUTE 122b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov# define DECLARE_WRAPPER(ret_type, func, ...) 12350a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 1245b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#elif defined(_WIN32) 1252716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# if defined(_DLL) // DLL CRT 1262716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# define WRAP(x) x 1272716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# define WRAPPER_NAME(x) #x 1282716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# define INTERCEPTOR_ATTRIBUTE 1292716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# else // Static CRT 1302d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines# define WRAP(x) __asan_wrap_##x 1312d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines# define WRAPPER_NAME(x) "__asan_wrap_"#x 1322d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines# define INTERCEPTOR_ATTRIBUTE __declspec(dllexport) 1332716a61d085a8fdf13a099822720e320414cc4dcTimur Iskhodzhanov# endif 1349213e07a7f95b3e634b97a7bba70849badcf2c8aTimur Iskhodzhanov# define DECLARE_WRAPPER(ret_type, func, ...) \ 1359213e07a7f95b3e634b97a7bba70849badcf2c8aTimur Iskhodzhanov extern "C" ret_type func(__VA_ARGS__); 1369213e07a7f95b3e634b97a7bba70849badcf2c8aTimur Iskhodzhanov# define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \ 1379213e07a7f95b3e634b97a7bba70849badcf2c8aTimur Iskhodzhanov extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__); 1385d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines#elif defined(__FreeBSD__) 1395d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines# define WRAP(x) __interceptor_ ## x 1405d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines# define WRAPPER_NAME(x) "__interceptor_" #x 1415d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default"))) 1425d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines// FreeBSD's dynamic linker (incompliantly) gives non-weak symbols higher 1435d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines// priority than weak ones so weak aliases won't work for indirect calls 1445d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines// in position-independent (-fPIC / -fPIE) mode. 1455d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines# define DECLARE_WRAPPER(ret_type, func, ...) \ 1465d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines extern "C" ret_type func(__VA_ARGS__) \ 1475d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines __attribute__((alias("__interceptor_" #func), visibility("default"))); 1485b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#else 149f54c0e3321e2381fca3f02faefaaa6639d59c7cfDmitry Vyukov# define WRAP(x) __interceptor_ ## x 150f54c0e3321e2381fca3f02faefaaa6639d59c7cfDmitry Vyukov# define WRAPPER_NAME(x) "__interceptor_" #x 1515b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default"))) 152b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov# define DECLARE_WRAPPER(ret_type, func, ...) \ 153b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov extern "C" ret_type func(__VA_ARGS__) \ 154ba362a7f0e98b369758f07aec8794c61e688b82dAlexey Samsonov __attribute__((weak, alias("__interceptor_" #func), visibility("default"))); 1555b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#endif 1565b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 15769563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko#if !defined(__APPLE__) 1580ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define PTR_TO_REAL(x) real_##x 1590ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define REAL(x) __interception::PTR_TO_REAL(x) 1600ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define FUNC_TYPE(x) x##_f 1610ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko 1620ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define DECLARE_REAL(ret_type, func, ...) \ 1630ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ 1640ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko namespace __interception { \ 1650ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko extern FUNC_TYPE(func) PTR_TO_REAL(func); \ 1660ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko } 16769563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko#else // __APPLE__ 1680ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define REAL(x) x 1690ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko# define DECLARE_REAL(ret_type, func, ...) \ 1700ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko extern "C" ret_type func(__VA_ARGS__); 17169563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko#endif // __APPLE__ 1725b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 1736bae39d1db13f60d3e9b8393e5b9d9eb2ab1b5c0Chandler Carruth#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \ 174c27279a4a08e0661f204c3e5dc65aafce79dec8aAlexey Samsonov DECLARE_REAL(ret_type, func, __VA_ARGS__) \ 1755b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov extern "C" ret_type WRAP(func)(__VA_ARGS__); 1765b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 177b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR 178b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov// macros does its job. In exceptional cases you may need to call REAL(foo) 179b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov// without defining INTERCEPTOR(..., foo, ...). For example, if you override 180b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov// foo with an interceptor for other function. 18169563986ca570ce750111a82264d51ddbf4107baAlexander Potapenko#if !defined(__APPLE__) 182b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov# define DEFINE_REAL(ret_type, func, ...) \ 183b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ 1840ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko namespace __interception { \ 1850ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko FUNC_TYPE(func) PTR_TO_REAL(func); \ 1860ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko } 1870ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko#else 188b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov# define DEFINE_REAL(ret_type, func, ...) 1890ef531054fc25c11e372cbab1384f10954984219Alexander Potapenko#endif 190600972e3427173cc8904d741decd1af0ed5de9fdTimur Iskhodzhanov 19150a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#if !defined(__APPLE__) 192b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov#define INTERCEPTOR(ret_type, func, ...) \ 193b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov DEFINE_REAL(ret_type, func, __VA_ARGS__) \ 194b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \ 1955b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov extern "C" \ 1965b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov INTERCEPTOR_ATTRIBUTE \ 197b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov ret_type WRAP(func)(__VA_ARGS__) 1986a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko 1996a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko// We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now. 2006a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko#define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \ 2016a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko INTERCEPTOR(ret_type, func, __VA_ARGS__) 2026a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko 20350a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#else // __APPLE__ 2046a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko 2056a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko#define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \ 2066a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko extern "C" ret_type func(__VA_ARGS__) suffix; \ 20750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko extern "C" ret_type WRAP(func)(__VA_ARGS__); \ 20850a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko INTERPOSER(func); \ 20950a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__) 21050a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko 2116a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko#define INTERCEPTOR(ret_type, func, ...) \ 2126a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__) 2136a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko 2146a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko#define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \ 2156a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__) 2166a659dfd8e717a598f54867aa36c2e4af09d031bAlexander Potapenko 21750a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko// Override |overridee| with |overrider|. 21850a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#define OVERRIDE_FUNCTION(overridee, overrider) \ 21950a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko INTERPOSER_2(overridee, WRAP(overrider)) 22050a002ecad3f0a10c136496e5b6289bd3c71590eAlexander Potapenko#endif 221600972e3427173cc8904d741decd1af0ed5de9fdTimur Iskhodzhanov 222600972e3427173cc8904d741decd1af0ed5de9fdTimur Iskhodzhanov#if defined(_WIN32) 223600972e3427173cc8904d741decd1af0ed5de9fdTimur Iskhodzhanov# define INTERCEPTOR_WINAPI(ret_type, func, ...) \ 224b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \ 225b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov namespace __interception { \ 226b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov FUNC_TYPE(func) PTR_TO_REAL(func); \ 227b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov } \ 2289213e07a7f95b3e634b97a7bba70849badcf2c8aTimur Iskhodzhanov DECLARE_WRAPPER_WINAPI(ret_type, func, __VA_ARGS__) \ 229b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov extern "C" \ 230b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov INTERCEPTOR_ATTRIBUTE \ 231b66bfd1e28776f0a350b9c197926c2c172678603Alexey Samsonov ret_type __stdcall WRAP(func)(__VA_ARGS__) 232600972e3427173cc8904d741decd1af0ed5de9fdTimur Iskhodzhanov#endif 2335b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 234592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov// ISO C++ forbids casting between pointer-to-function and pointer-to-object, 235592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov// so we use casting via an integral type __interception::uptr, 236592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov// assuming that system is POSIX-compliant. Using other hacks seem 237592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov// challenging, as we don't even pass function type to 238592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov// INTERCEPT_FUNCTION macro, only its name. 239592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonovnamespace __interception { 240b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonov#if defined(_WIN64) 241b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonovtypedef unsigned long long uptr; // NOLINT 242b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonov#else 243592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonovtypedef unsigned long uptr; // NOLINT 244b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonov#endif // _WIN64 245592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov} // namespace __interception 246592d3f707e02968c75fd6e90d06d24f5df99c8b9Alexey Samsonov 2475b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#define INCLUDED_FROM_INTERCEPTION_LIB 2485b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 2492d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#if defined(__linux__) || defined(__FreeBSD__) 2505b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# include "interception_linux.h" 2512d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func) 2525e2d3776a314629680921abd1d55d89d95a2da90Alexey Samsonov# define INTERCEPT_FUNCTION_VER(func, symver) \ 2532d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver) 2545b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#elif defined(__APPLE__) 2555b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# include "interception_mac.h" 2565b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func) 2575e2d3776a314629680921abd1d55d89d95a2da90Alexey Samsonov# define INTERCEPT_FUNCTION_VER(func, symver) \ 2585e2d3776a314629680921abd1d55d89d95a2da90Alexey Samsonov INTERCEPT_FUNCTION_VER_MAC(func, symver) 2595b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#else // defined(_WIN32) 26007bb9f1e3600195119aec1aae1aa48a6ed2f5febTimur Iskhodzhanov# include "interception_win.h" 26107bb9f1e3600195119aec1aae1aa48a6ed2f5febTimur Iskhodzhanov# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func) 2625e2d3776a314629680921abd1d55d89d95a2da90Alexey Samsonov# define INTERCEPT_FUNCTION_VER(func, symver) \ 2635e2d3776a314629680921abd1d55d89d95a2da90Alexey Samsonov INTERCEPT_FUNCTION_VER_WIN(func, symver) 2645b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#endif 2655b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 2665b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#undef INCLUDED_FROM_INTERCEPTION_LIB 2675b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 2685b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov#endif // INTERCEPTION_H 269