Home | History | Annotate | Line # | Download | only in sanitizer_common
      1 //===-- sanitizer_win_dll_thunk.cc ----------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      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 //===----------------------------------------------------------------------===//
     14 
     15 #ifdef SANITIZER_DLL_THUNK
     16 #include "sanitizer_win_defs.h"
     17 #include "sanitizer_win_dll_thunk.h"
     18 #include "interception/interception.h"
     19 
     20 extern "C" {
     21 void *WINAPI GetModuleHandleA(const char *module_name);
     22 void abort();
     23 }
     24 
     25 namespace __sanitizer {
     26 uptr dllThunkGetRealAddrOrDie(const char *name) {
     27   uptr ret =
     28       __interception::InternalGetProcAddress((void *)GetModuleHandleA(0), name);
     29   if (!ret)
     30     abort();
     31   return ret;
     32 }
     33 
     34 int dllThunkIntercept(const char* main_function, uptr dll_function) {
     35   uptr wrapper = dllThunkGetRealAddrOrDie(main_function);
     36   if (!__interception::OverrideFunction(dll_function, wrapper, 0))
     37     abort();
     38   return 0;
     39 }
     40 
     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);
     45   if (!wrapper)
     46     wrapper = dllThunkGetRealAddrOrDie(default_function);
     47   if (!__interception::OverrideFunction(dll_function, wrapper, 0))
     48     abort();
     49   return 0;
     50 }
     51 } // namespace __sanitizer
     52 
     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"
     57 
     58 #pragma section(".DLLTH$A", read)  // NOLINT
     59 #pragma section(".DLLTH$Z", read)  // NOLINT
     60 
     61 typedef void (*DllThunkCB)();
     62 extern "C" {
     63 __declspec(allocate(".DLLTH$A")) DllThunkCB __start_dll_thunk;
     64 __declspec(allocate(".DLLTH$Z")) DllThunkCB __stop_dll_thunk;
     65 }
     66 
     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)
     71 
     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.
     75   if (flag) return 0;
     76   flag = true;
     77 
     78   for (DllThunkCB *it = &__start_dll_thunk; it < &__stop_dll_thunk; ++it)
     79     if (*it)
     80       (*it)();
     81 
     82   // In DLLs, the callbacks are expected to return 0,
     83   // otherwise CRT initialization fails.
     84   return 0;
     85 }
     86 
     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)() =
     91     __dll_thunk_init;
     92 
     93 static void WINAPI dll_thunk_thread_init(void *mod, unsigned long reason,
     94                                          void *reserved) {
     95   if (reason == /*DLL_PROCESS_ATTACH=*/1) __dll_thunk_init();
     96 }
     97 
     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;
    101 
    102 #endif // SANITIZER_DLL_THUNK
    103