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