1 //===-- sanitizer_win_dll_thunk.cc ----------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
7 // This file defines a family of thunks that should be statically linked into
8 // the DLLs that have instrumentation in order to delegate the calls to the
9 // shared runtime that lives in the main binary.
10 // See https://github.com/google/sanitizers/issues/209 for the details.
11 //===----------------------------------------------------------------------===//
13 #ifdef SANITIZER_DLL_THUNK
14 #include "sanitizer_win_defs.h"
15 #include "sanitizer_win_dll_thunk.h"
16 #include "interception/interception.h"
19 void *WINAPI
GetModuleHandleA(const char *module_name
);
23 namespace __sanitizer
{
24 uptr
dllThunkGetRealAddrOrDie(const char *name
) {
26 __interception::InternalGetProcAddress((void *)GetModuleHandleA(0), name
);
32 int dllThunkIntercept(const char* main_function
, uptr dll_function
) {
33 uptr wrapper
= dllThunkGetRealAddrOrDie(main_function
);
34 if (!__interception::OverrideFunction(dll_function
, wrapper
, 0))
39 int dllThunkInterceptWhenPossible(const char* main_function
,
40 const char* default_function
, uptr dll_function
) {
41 uptr wrapper
= __interception::InternalGetProcAddress(
42 (void *)GetModuleHandleA(0), main_function
);
44 wrapper
= dllThunkGetRealAddrOrDie(default_function
);
45 if (!__interception::OverrideFunction(dll_function
, wrapper
, 0))
49 } // namespace __sanitizer
51 // Include Sanitizer Common interface.
52 #define INTERFACE_FUNCTION(Name) INTERCEPT_SANITIZER_FUNCTION(Name)
53 #define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name)
54 #include "sanitizer_common_interface.inc"
56 #pragma section(".DLLTH$A", read) // NOLINT
57 #pragma section(".DLLTH$Z", read) // NOLINT
59 typedef void (*DllThunkCB
)();
61 __declspec(allocate(".DLLTH$A")) DllThunkCB __start_dll_thunk
;
62 __declspec(allocate(".DLLTH$Z")) DllThunkCB __stop_dll_thunk
;
65 // Disable compiler warnings that show up if we declare our own version
66 // of a compiler intrinsic (e.g. strlen).
67 #pragma warning(disable: 4391)
68 #pragma warning(disable: 4392)
70 extern "C" int __dll_thunk_init() {
71 static bool flag
= false;
72 // __dll_thunk_init is expected to be called by only one thread.
76 for (DllThunkCB
*it
= &__start_dll_thunk
; it
< &__stop_dll_thunk
; ++it
)
80 // In DLLs, the callbacks are expected to return 0,
81 // otherwise CRT initialization fails.
85 // We want to call dll_thunk_init before C/C++ initializers / constructors are
86 // executed, otherwise functions like memset might be invoked.
87 #pragma section(".CRT$XIB", long, read) // NOLINT
88 __declspec(allocate(".CRT$XIB")) int (*__dll_thunk_preinit
)() =
91 static void WINAPI
dll_thunk_thread_init(void *mod
, unsigned long reason
,
93 if (reason
== /*DLL_PROCESS_ATTACH=*/1) __dll_thunk_init();
96 #pragma section(".CRT$XLAB", long, read) // NOLINT
97 __declspec(allocate(".CRT$XLAB")) void (WINAPI
*__dll_thunk_tls_init
)(void *,
98 unsigned long, void *) = dll_thunk_thread_init
;
100 #endif // SANITIZER_DLL_THUNK