1 //===-- sanitizer_win_dll_thunk.cc ----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // This file defines a family of thunks that should be statically linked into
10 // the DLLs that have instrumentation in order to delegate the calls to the
11 // shared runtime that lives in the main binary.
12 // See https://github.com/google/sanitizers/issues/209 for the details.
13 //===----------------------------------------------------------------------===//
15 #ifdef SANITIZER_DLL_THUNK
16 #include "sanitizer_win_defs.h"
17 #include "sanitizer_win_dll_thunk.h"
18 #include "interception/interception.h"
21 void *WINAPI GetModuleHandleA(const char *module_name);
25 namespace __sanitizer {
26 uptr dllThunkGetRealAddrOrDie(const char *name) {
28 __interception::InternalGetProcAddress((void *)GetModuleHandleA(0), name);
34 int dllThunkIntercept(const char* main_function, uptr dll_function) {
35 uptr wrapper = dllThunkGetRealAddrOrDie(main_function);
36 if (!__interception::OverrideFunction(dll_function, wrapper, 0))
41 int dllThunkInterceptWhenPossible(const char* main_function,
42 const char* default_function, uptr dll_function) {
43 uptr wrapper = __interception::InternalGetProcAddress(
44 (void *)GetModuleHandleA(0), main_function);
46 wrapper = dllThunkGetRealAddrOrDie(default_function);
47 if (!__interception::OverrideFunction(dll_function, wrapper, 0))
51 } // namespace __sanitizer
53 // Include Sanitizer Common interface.
54 #define INTERFACE_FUNCTION(Name) INTERCEPT_SANITIZER_FUNCTION(Name)
55 #define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name)
56 #include "sanitizer_common_interface.inc"
58 #pragma section(".DLLTH$A", read) // NOLINT
59 #pragma section(".DLLTH$Z", read) // NOLINT
61 typedef void (*DllThunkCB)();
63 __declspec(allocate(".DLLTH$A")) DllThunkCB __start_dll_thunk;
64 __declspec(allocate(".DLLTH$Z")) DllThunkCB __stop_dll_thunk;
67 // Disable compiler warnings that show up if we declare our own version
68 // of a compiler intrinsic (e.g. strlen).
69 #pragma warning(disable: 4391)
70 #pragma warning(disable: 4392)
72 extern "C" int __dll_thunk_init() {
73 static bool flag = false;
74 // __dll_thunk_init is expected to be called by only one thread.
78 for (DllThunkCB *it = &__start_dll_thunk; it < &__stop_dll_thunk; ++it)
82 // In DLLs, the callbacks are expected to return 0,
83 // otherwise CRT initialization fails.
87 // We want to call dll_thunk_init before C/C++ initializers / constructors are
88 // executed, otherwise functions like memset might be invoked.
89 #pragma section(".CRT$XIB", long, read) // NOLINT
90 __declspec(allocate(".CRT$XIB")) int (*__dll_thunk_preinit)() =
93 static void WINAPI dll_thunk_thread_init(void *mod, unsigned long reason,
95 if (reason == /*DLL_PROCESS_ATTACH=*/1) __dll_thunk_init();
98 #pragma section(".CRT$XLAB", long, read) // NOLINT
99 __declspec(allocate(".CRT$XLAB")) void (WINAPI *__dll_thunk_tls_init)(void *,
100 unsigned long, void *) = dll_thunk_thread_init;
102 #endif // SANITIZER_DLL_THUNK