1 //===-- sanitizer_win_dll_thunk.cpp ---------------------------------------===//
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
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 //===----------------------------------------------------------------------===//
14 #ifdef SANITIZER_DLL_THUNK
15 #include "sanitizer_win_defs.h"
16 #include "sanitizer_win_dll_thunk.h"
17 #include "interception/interception.h"
20 void *WINAPI
GetModuleHandleA(const char *module_name
);
24 namespace __sanitizer
{
25 uptr
dllThunkGetRealAddrOrDie(const char *name
) {
27 __interception::InternalGetProcAddress((void *)GetModuleHandleA(0), name
);
33 int dllThunkIntercept(const char* main_function
, uptr dll_function
) {
34 uptr wrapper
= dllThunkGetRealAddrOrDie(main_function
);
35 if (!__interception::OverrideFunction(dll_function
, wrapper
, 0))
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
);
45 wrapper
= dllThunkGetRealAddrOrDie(default_function
);
46 if (!__interception::OverrideFunction(dll_function
, wrapper
, 0))
50 } // namespace __sanitizer
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"
57 #pragma section(".DLLTH$A", read)
58 #pragma section(".DLLTH$Z", read)
60 typedef void (*DllThunkCB
)();
62 __declspec(allocate(".DLLTH$A")) DllThunkCB __start_dll_thunk
;
63 __declspec(allocate(".DLLTH$Z")) DllThunkCB __stop_dll_thunk
;
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)
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.
77 for (DllThunkCB
*it
= &__start_dll_thunk
; it
< &__stop_dll_thunk
; ++it
)
81 // In DLLs, the callbacks are expected to return 0,
82 // otherwise CRT initialization fails.
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
)() =
92 static void WINAPI
dll_thunk_thread_init(void *mod
, unsigned long reason
,
94 if (reason
== /*DLL_PROCESS_ATTACH=*/1) __dll_thunk_init();
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
;
101 #endif // SANITIZER_DLL_THUNK