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