Remove redundant std::move on const object
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_win_dll_thunk.cc
blob6577a3606d116c587ce12451a5f46d3cf9ee2e63
1 //===-- sanitizer_win_dll_thunk.cc ----------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
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"
18 extern "C" {
19 void *WINAPI GetModuleHandleA(const char *module_name);
20 void abort();
23 namespace __sanitizer {
24 uptr dllThunkGetRealAddrOrDie(const char *name) {
25 uptr ret =
26 __interception::InternalGetProcAddress((void *)GetModuleHandleA(0), name);
27 if (!ret)
28 abort();
29 return ret;
32 int dllThunkIntercept(const char* main_function, uptr dll_function) {
33 uptr wrapper = dllThunkGetRealAddrOrDie(main_function);
34 if (!__interception::OverrideFunction(dll_function, wrapper, 0))
35 abort();
36 return 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);
43 if (!wrapper)
44 wrapper = dllThunkGetRealAddrOrDie(default_function);
45 if (!__interception::OverrideFunction(dll_function, wrapper, 0))
46 abort();
47 return 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)();
60 extern "C" {
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.
73 if (flag) return 0;
74 flag = true;
76 for (DllThunkCB *it = &__start_dll_thunk; it < &__stop_dll_thunk; ++it)
77 if (*it)
78 (*it)();
80 // In DLLs, the callbacks are expected to return 0,
81 // otherwise CRT initialization fails.
82 return 0;
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)() =
89 __dll_thunk_init;
91 static void WINAPI dll_thunk_thread_init(void *mod, unsigned long reason,
92 void *reserved) {
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