Home | History | Annotate | Line # | Download | only in sanitizer_common
      1 //===-- sanitizer_win_dll_thunk.h -----------------------------------------===//
      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 // This header provide helper macros to delegate calls to the shared runtime
     10 // that lives in the main executable. It should be included to dll_thunks that
     11 // will be linked to the dlls, when the sanitizer is a static library included
     12 // in the main executable.
     13 //===----------------------------------------------------------------------===//
     14 #ifndef SANITIZER_WIN_DLL_THUNK_H
     15 #define SANITIZER_WIN_DLL_THUNK_H
     16 #include "sanitizer_internal_defs.h"
     17 
     18 namespace __sanitizer {
     19 uptr dllThunkGetRealAddrOrDie(const char *name);
     20 
     21 int dllThunkIntercept(const char* main_function, uptr dll_function);
     22 
     23 int dllThunkInterceptWhenPossible(const char* main_function,
     24     const char* default_function, uptr dll_function);
     25 }
     26 
     27 extern "C" int __dll_thunk_init();
     28 
     29 // ----------------- Function interception helper macros -------------------- //
     30 // Override dll_function with main_function from main executable.
     31 #define INTERCEPT_OR_DIE(main_function, dll_function)                          \
     32   static int intercept_##dll_function() {                                      \
     33     return __sanitizer::dllThunkIntercept(main_function, (__sanitizer::uptr)   \
     34         dll_function);                                                         \
     35   }                                                                            \
     36   __pragma(section(".DLLTH$M", long, read))                                    \
     37   __declspec(allocate(".DLLTH$M")) int (*__dll_thunk_##dll_function)() =       \
     38     intercept_##dll_function;
     39 
     40 // Try to override dll_function with main_function from main executable.
     41 // If main_function is not present, override dll_function with default_function.
     42 #define INTERCEPT_WHEN_POSSIBLE(main_function, default_function, dll_function) \
     43   static int intercept_##dll_function() {                                      \
     44     return __sanitizer::dllThunkInterceptWhenPossible(main_function,           \
     45         default_function, (__sanitizer::uptr)dll_function);                    \
     46   }                                                                            \
     47   __pragma(section(".DLLTH$M", long, read))                                    \
     48   __declspec(allocate(".DLLTH$M")) int (*__dll_thunk_##dll_function)() =       \
     49     intercept_##dll_function;
     50 
     51 // -------------------- Function interception macros ------------------------ //
     52 // Special case of hooks -- ASan own interface functions.  Those are only called
     53 // after __asan_init, thus an empty implementation is sufficient.
     54 #define INTERCEPT_SANITIZER_FUNCTION(name)                                     \
     55   extern "C" __declspec(noinline) void name() {                                \
     56     volatile int prevent_icf = (__LINE__ << 8) ^ __COUNTER__;                  \
     57     static const char function_name[] = #name;                                 \
     58     for (const char* ptr = &function_name[0]; *ptr; ++ptr)                     \
     59       prevent_icf ^= *ptr;                                                     \
     60     (void)prevent_icf;                                                         \
     61     __debugbreak();                                                            \
     62   }                                                                            \
     63   INTERCEPT_OR_DIE(#name, name)
     64 
     65 // Special case of hooks -- Weak functions, could be redefined in the main
     66 // executable, but that is not necessary, so we shouldn't die if we can not find
     67 // a reference. Instead, when the function is not present in the main executable
     68 // we consider the default impl provided by asan library.
     69 #define INTERCEPT_SANITIZER_WEAK_FUNCTION(name)                                \
     70   extern "C" __declspec(noinline) void name() {                                \
     71     volatile int prevent_icf = (__LINE__ << 8) ^ __COUNTER__;                  \
     72     static const char function_name[] = #name;                                 \
     73     for (const char* ptr = &function_name[0]; *ptr; ++ptr)                     \
     74       prevent_icf ^= *ptr;                                                     \
     75     (void)prevent_icf;                                                         \
     76     __debugbreak();                                                            \
     77   }                                                                            \
     78   INTERCEPT_WHEN_POSSIBLE(#name, STRINGIFY(WEAK_EXPORT_NAME(name)), name)
     79 
     80 // We can't define our own version of strlen etc. because that would lead to
     81 // link-time or even type mismatch errors.  Instead, we can declare a function
     82 // just to be able to get its address.  Me may miss the first few calls to the
     83 // functions since it can be called before __dll_thunk_init, but that would lead
     84 // to false negatives in the startup code before user's global initializers,
     85 // which isn't a big deal.
     86 #define INTERCEPT_LIBRARY_FUNCTION(name)                                       \
     87   extern "C" void name();                                                      \
     88   INTERCEPT_OR_DIE(WRAPPER_NAME(name), name)
     89 
     90 // Use these macros for functions that could be called before __dll_thunk_init()
     91 // is executed and don't lead to errors if defined (free, malloc, etc).
     92 #define INTERCEPT_WRAP_V_V(name)                                               \
     93   extern "C" void name() {                                                     \
     94     typedef decltype(name) *fntype;                                            \
     95     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
     96     fn();                                                                      \
     97   }                                                                            \
     98   INTERCEPT_OR_DIE(#name, name);
     99 
    100 #define INTERCEPT_WRAP_V_W(name)                                               \
    101   extern "C" void name(void *arg) {                                            \
    102     typedef decltype(name) *fntype;                                            \
    103     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
    104     fn(arg);                                                                   \
    105   }                                                                            \
    106   INTERCEPT_OR_DIE(#name, name);
    107 
    108 #define INTERCEPT_WRAP_V_WW(name)                                              \
    109   extern "C" void name(void *arg1, void *arg2) {                               \
    110     typedef decltype(name) *fntype;                                            \
    111     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
    112     fn(arg1, arg2);                                                            \
    113   }                                                                            \
    114   INTERCEPT_OR_DIE(#name, name);
    115 
    116 #define INTERCEPT_WRAP_V_WWW(name)                                             \
    117   extern "C" void name(void *arg1, void *arg2, void *arg3) {                   \
    118     typedef decltype(name) *fntype;                                            \
    119     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
    120     fn(arg1, arg2, arg3);                                                      \
    121   }                                                                            \
    122   INTERCEPT_OR_DIE(#name, name);
    123 
    124 #define INTERCEPT_WRAP_W_V(name)                                               \
    125   extern "C" void *name() {                                                    \
    126     typedef decltype(name) *fntype;                                            \
    127     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
    128     return fn();                                                               \
    129   }                                                                            \
    130   INTERCEPT_OR_DIE(#name, name);
    131 
    132 #define INTERCEPT_WRAP_W_W(name)                                               \
    133   extern "C" void *name(void *arg) {                                           \
    134     typedef decltype(name) *fntype;                                            \
    135     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
    136     return fn(arg);                                                            \
    137   }                                                                            \
    138   INTERCEPT_OR_DIE(#name, name);
    139 
    140 #define INTERCEPT_WRAP_W_WW(name)                                              \
    141   extern "C" void *name(void *arg1, void *arg2) {                              \
    142     typedef decltype(name) *fntype;                                            \
    143     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
    144     return fn(arg1, arg2);                                                     \
    145   }                                                                            \
    146   INTERCEPT_OR_DIE(#name, name);
    147 
    148 #define INTERCEPT_WRAP_W_WWW(name)                                             \
    149   extern "C" void *name(void *arg1, void *arg2, void *arg3) {                  \
    150     typedef decltype(name) *fntype;                                            \
    151     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
    152     return fn(arg1, arg2, arg3);                                               \
    153   }                                                                            \
    154   INTERCEPT_OR_DIE(#name, name);
    155 
    156 #define INTERCEPT_WRAP_W_WWWW(name)                                            \
    157   extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4) {      \
    158     typedef decltype(name) *fntype;                                            \
    159     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
    160     return fn(arg1, arg2, arg3, arg4);                                         \
    161   }                                                                            \
    162   INTERCEPT_OR_DIE(#name, name);
    163 
    164 #define INTERCEPT_WRAP_W_WWWWW(name)                                           \
    165   extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4,        \
    166                         void *arg5) {                                          \
    167     typedef decltype(name) *fntype;                                            \
    168     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
    169     return fn(arg1, arg2, arg3, arg4, arg5);                                   \
    170   }                                                                            \
    171   INTERCEPT_OR_DIE(#name, name);
    172 
    173 #define INTERCEPT_WRAP_W_WWWWWW(name)                                          \
    174   extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4,        \
    175                         void *arg5, void *arg6) {                              \
    176     typedef decltype(name) *fntype;                                            \
    177     static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name);   \
    178     return fn(arg1, arg2, arg3, arg4, arg5, arg6);                             \
    179   }                                                                            \
    180   INTERCEPT_OR_DIE(#name, name);
    181 
    182 #endif // SANITIZER_WIN_DLL_THUNK_H
    183