Home | History | Annotate | Download | only in interception
      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:
     46 //      3a) add DECLARE_REAL(int, foo, const char*, double); to a
     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:
     50 //      3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double);
     51 //          to a header file.
     52 
     53 // Notes: 1. Things may not work properly if macro INTERCEPT(...) {...} or
     54 //           DECLARE_REAL(...); are located inside namespaces.
     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
     58 //           INTERCEPTOR(int, foo, const char *bar, double baz);
     59 //           but instead you'll have to add
     60 //           DEFINE_REAL(int, foo, const char *bar, double baz); in your
     61 //           source file (to define a pointer to overriden function).
     62 
     63 // How it works:
     64 // To replace weak system functions on Linux we just need to declare functions
     65 // with same names in our library and then obtain the real function pointers
     66 // using dlsym(). This is not so on Mac OS, where the two-level namespace makes
     67 // our replacement functions invisible to other libraries. This may be overcomed
     68 // using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
     69 // libraries in Chromium were noticed when doing so.
     70 // Instead we use mach_override, a handy framework for patching functions at
     71 // runtime. To avoid possible name clashes, our replacement functions have
     72 // the "wrap_" prefix on Mac.
     73 
     74 #if defined(__APPLE__)
     75 # define WRAP(x) wrap_##x
     76 # define WRAPPER_NAME(x) "wrap_"#x
     77 # define INTERCEPTOR_ATTRIBUTE
     78 #elif defined(_WIN32)
     79 # if defined(_DLL)  // DLL CRT
     80 #  define WRAP(x) x
     81 #  define WRAPPER_NAME(x) #x
     82 #  define INTERCEPTOR_ATTRIBUTE
     83 # else  // Static CRT
     84 #  define WRAP(x) wrap_##x
     85 #  define WRAPPER_NAME(x) "wrap_"#x
     86 #  define INTERCEPTOR_ATTRIBUTE
     87 # endif
     88 #else
     89 # define WRAP(x) x
     90 # define WRAPPER_NAME(x) #x
     91 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
     92 #endif
     93 
     94 #define PTR_TO_REAL(x) real_##x
     95 #define REAL(x) __interception::PTR_TO_REAL(x)
     96 #define FUNC_TYPE(x) x##_f
     97 
     98 #define DECLARE_REAL(ret_type, func, ...); \
     99   typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
    100   namespace __interception { \
    101     extern FUNC_TYPE(func) PTR_TO_REAL(func); \
    102   }
    103 
    104 #define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...); \
    105   DECLARE_REAL(ret_type, func, ##__VA_ARGS__); \
    106   extern "C" ret_type WRAP(func)(__VA_ARGS__);
    107 
    108 // FIXME(timurrrr): We might need to add DECLARE_REAL_EX etc to support
    109 // different calling conventions later.
    110 
    111 #define DEFINE_REAL_EX(ret_type, convention, func, ...); \
    112   typedef ret_type (convention *FUNC_TYPE(func))(__VA_ARGS__); \
    113   namespace __interception { \
    114     FUNC_TYPE(func) PTR_TO_REAL(func); \
    115   }
    116 
    117 // Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
    118 // macros does its job. In exceptional cases you may need to call REAL(foo)
    119 // without defining INTERCEPTOR(..., foo, ...). For example, if you override
    120 // foo with an interceptor for other function.
    121 #define DEFAULT_CONVENTION
    122 
    123 #define DEFINE_REAL(ret_type, func, ...); \
    124   DEFINE_REAL_EX(ret_type, DEFAULT_CONVENTION, func, __VA_ARGS__);
    125 
    126 #define INTERCEPTOR_EX(ret_type, convention, func, ...) \
    127   DEFINE_REAL_EX(ret_type, convention, func, __VA_ARGS__); \
    128   extern "C" \
    129   INTERCEPTOR_ATTRIBUTE \
    130   ret_type convention WRAP(func)(__VA_ARGS__)
    131 
    132 #define INTERCEPTOR(ret_type, func, ...) \
    133   INTERCEPTOR_EX(ret_type, DEFAULT_CONVENTION, func, __VA_ARGS__)
    134 
    135 #if defined(_WIN32)
    136 # define INTERCEPTOR_WINAPI(ret_type, func, ...) \
    137   INTERCEPTOR_EX(ret_type, __stdcall, func, __VA_ARGS__)
    138 #endif
    139 
    140 #define INCLUDED_FROM_INTERCEPTION_LIB
    141 
    142 #if defined(__linux__)
    143 # include "interception_linux.h"
    144 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func)
    145 #elif defined(__APPLE__)
    146 # include "interception_mac.h"
    147 # define OVERRIDE_FUNCTION(old_func, new_func) \
    148     OVERRIDE_FUNCTION_MAC(old_func, new_func)
    149 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
    150 #else  // defined(_WIN32)
    151 # include "interception_win.h"
    152 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
    153 #endif
    154 
    155 #undef INCLUDED_FROM_INTERCEPTION_LIB
    156 
    157 #endif  // INTERCEPTION_H
    158