1 //===-- sanitizer_win_dll_thunk.cpp ---------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // This file defines a family of thunks that should be statically linked into 9 // the DLLs that have instrumentation in order to delegate the calls to the 10 // shared runtime that lives in the main binary. 11 // See https://github.com/google/sanitizers/issues/209 for the details. 12 //===----------------------------------------------------------------------===// 13 14 #ifdef SANITIZER_DLL_THUNK 15 #include "sanitizer_win_defs.h" 16 #include "sanitizer_win_dll_thunk.h" 17 #include "interception/interception.h" 18 19 extern "C" { 20 void *WINAPI GetModuleHandleA(const char *module_name); 21 void abort(); 22 } 23 24 namespace __sanitizer { 25 uptr dllThunkGetRealAddrOrDie(const char *name) { 26 uptr ret = 27 __interception::InternalGetProcAddress((void *)GetModuleHandleA(0), name); 28 if (!ret) 29 abort(); 30 return ret; 31 } 32 33 int dllThunkIntercept(const char* main_function, uptr dll_function) { 34 uptr wrapper = dllThunkGetRealAddrOrDie(main_function); 35 if (!__interception::OverrideFunction(dll_function, wrapper, 0)) 36 abort(); 37 return 0; 38 } 39 40 int dllThunkInterceptWhenPossible(const char* main_function, 41 const char* default_function, uptr dll_function) { 42 uptr wrapper = __interception::InternalGetProcAddress( 43 (void *)GetModuleHandleA(0), main_function); 44 if (!wrapper) 45 wrapper = dllThunkGetRealAddrOrDie(default_function); 46 if (!__interception::OverrideFunction(dll_function, wrapper, 0)) 47 abort(); 48 return 0; 49 } 50 } // namespace __sanitizer 51 52 // Include Sanitizer Common interface. 53 #define INTERFACE_FUNCTION(Name) INTERCEPT_SANITIZER_FUNCTION(Name) 54 #define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name) 55 #include "sanitizer_common_interface.inc" 56 57 #pragma section(".DLLTH$A", read) 58 #pragma section(".DLLTH$Z", read) 59 60 typedef void (*DllThunkCB)(); 61 extern "C" { 62 __declspec(allocate(".DLLTH$A")) DllThunkCB __start_dll_thunk; 63 __declspec(allocate(".DLLTH$Z")) DllThunkCB __stop_dll_thunk; 64 } 65 66 // Disable compiler warnings that show up if we declare our own version 67 // of a compiler intrinsic (e.g. strlen). 68 #pragma warning(disable: 4391) 69 #pragma warning(disable: 4392) 70 71 extern "C" int __dll_thunk_init() { 72 static bool flag = false; 73 // __dll_thunk_init is expected to be called by only one thread. 74 if (flag) return 0; 75 flag = true; 76 77 for (DllThunkCB *it = &__start_dll_thunk; it < &__stop_dll_thunk; ++it) 78 if (*it) 79 (*it)(); 80 81 // In DLLs, the callbacks are expected to return 0, 82 // otherwise CRT initialization fails. 83 return 0; 84 } 85 86 // We want to call dll_thunk_init before C/C++ initializers / constructors are 87 // executed, otherwise functions like memset might be invoked. 88 #pragma section(".CRT$XIB", long, read) 89 __declspec(allocate(".CRT$XIB")) int (*__dll_thunk_preinit)() = 90 __dll_thunk_init; 91 92 static void WINAPI dll_thunk_thread_init(void *mod, unsigned long reason, 93 void *reserved) { 94 if (reason == /*DLL_PROCESS_ATTACH=*/1) __dll_thunk_init(); 95 } 96 97 #pragma section(".CRT$XLAB", long, read) 98 __declspec(allocate(".CRT$XLAB")) void (WINAPI *__dll_thunk_tls_init)(void *, 99 unsigned long, void *) = dll_thunk_thread_init; 100 101 #endif // SANITIZER_DLL_THUNK 102