sanitizer_win_dll_thunk.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //===-- sanitizer_win_dll_thunk.h -----------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // This header provide helper macros to delegate calls to the shared runtime
  9. // that lives in the main executable. It should be included to dll_thunks that
  10. // will be linked to the dlls, when the sanitizer is a static library included
  11. // in the main executable.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef SANITIZER_WIN_DLL_THUNK_H
  14. #define SANITIZER_WIN_DLL_THUNK_H
  15. #include "sanitizer_internal_defs.h"
  16. namespace __sanitizer {
  17. uptr dllThunkGetRealAddrOrDie(const char *name);
  18. int dllThunkIntercept(const char* main_function, uptr dll_function);
  19. int dllThunkInterceptWhenPossible(const char* main_function,
  20. const char* default_function, uptr dll_function);
  21. }
  22. extern "C" int __dll_thunk_init();
  23. // ----------------- Function interception helper macros -------------------- //
  24. // Override dll_function with main_function from main executable.
  25. #define INTERCEPT_OR_DIE(main_function, dll_function) \
  26. static int intercept_##dll_function() { \
  27. return __sanitizer::dllThunkIntercept(main_function, (__sanitizer::uptr) \
  28. dll_function); \
  29. } \
  30. __pragma(section(".DLLTH$M", long, read)) \
  31. __declspec(allocate(".DLLTH$M")) int (*__dll_thunk_##dll_function)() = \
  32. intercept_##dll_function;
  33. // Try to override dll_function with main_function from main executable.
  34. // If main_function is not present, override dll_function with default_function.
  35. #define INTERCEPT_WHEN_POSSIBLE(main_function, default_function, dll_function) \
  36. static int intercept_##dll_function() { \
  37. return __sanitizer::dllThunkInterceptWhenPossible(main_function, \
  38. default_function, (__sanitizer::uptr)dll_function); \
  39. } \
  40. __pragma(section(".DLLTH$M", long, read)) \
  41. __declspec(allocate(".DLLTH$M")) int (*__dll_thunk_##dll_function)() = \
  42. intercept_##dll_function;
  43. // -------------------- Function interception macros ------------------------ //
  44. // Special case of hooks -- ASan own interface functions. Those are only called
  45. // after __asan_init, thus an empty implementation is sufficient.
  46. #define INTERCEPT_SANITIZER_FUNCTION(name) \
  47. extern "C" __declspec(noinline) void name() { \
  48. volatile int prevent_icf = (__LINE__ << 8) ^ __COUNTER__; \
  49. static const char function_name[] = #name; \
  50. for (const char* ptr = &function_name[0]; *ptr; ++ptr) \
  51. prevent_icf ^= *ptr; \
  52. (void)prevent_icf; \
  53. __debugbreak(); \
  54. } \
  55. INTERCEPT_OR_DIE(#name, name)
  56. // Special case of hooks -- Weak functions, could be redefined in the main
  57. // executable, but that is not necessary, so we shouldn't die if we can not find
  58. // a reference. Instead, when the function is not present in the main executable
  59. // we consider the default impl provided by asan library.
  60. #define INTERCEPT_SANITIZER_WEAK_FUNCTION(name) \
  61. extern "C" __declspec(noinline) void name() { \
  62. volatile int prevent_icf = (__LINE__ << 8) ^ __COUNTER__; \
  63. static const char function_name[] = #name; \
  64. for (const char* ptr = &function_name[0]; *ptr; ++ptr) \
  65. prevent_icf ^= *ptr; \
  66. (void)prevent_icf; \
  67. __debugbreak(); \
  68. } \
  69. INTERCEPT_WHEN_POSSIBLE(#name, STRINGIFY(WEAK_EXPORT_NAME(name)), name)
  70. // We can't define our own version of strlen etc. because that would lead to
  71. // link-time or even type mismatch errors. Instead, we can declare a function
  72. // just to be able to get its address. Me may miss the first few calls to the
  73. // functions since it can be called before __dll_thunk_init, but that would lead
  74. // to false negatives in the startup code before user's global initializers,
  75. // which isn't a big deal.
  76. #define INTERCEPT_LIBRARY_FUNCTION(name) \
  77. extern "C" void name(); \
  78. INTERCEPT_OR_DIE(WRAPPER_NAME(name), name)
  79. // Use these macros for functions that could be called before __dll_thunk_init()
  80. // is executed and don't lead to errors if defined (free, malloc, etc).
  81. #define INTERCEPT_WRAP_V_V(name) \
  82. extern "C" void name() { \
  83. typedef decltype(name) *fntype; \
  84. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  85. fn(); \
  86. } \
  87. INTERCEPT_OR_DIE(#name, name);
  88. #define INTERCEPT_WRAP_V_W(name) \
  89. extern "C" void name(void *arg) { \
  90. typedef decltype(name) *fntype; \
  91. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  92. fn(arg); \
  93. } \
  94. INTERCEPT_OR_DIE(#name, name);
  95. #define INTERCEPT_WRAP_V_WW(name) \
  96. extern "C" void name(void *arg1, void *arg2) { \
  97. typedef decltype(name) *fntype; \
  98. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  99. fn(arg1, arg2); \
  100. } \
  101. INTERCEPT_OR_DIE(#name, name);
  102. #define INTERCEPT_WRAP_V_WWW(name) \
  103. extern "C" void name(void *arg1, void *arg2, void *arg3) { \
  104. typedef decltype(name) *fntype; \
  105. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  106. fn(arg1, arg2, arg3); \
  107. } \
  108. INTERCEPT_OR_DIE(#name, name);
  109. #define INTERCEPT_WRAP_W_V(name) \
  110. extern "C" void *name() { \
  111. typedef decltype(name) *fntype; \
  112. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  113. return fn(); \
  114. } \
  115. INTERCEPT_OR_DIE(#name, name);
  116. #define INTERCEPT_WRAP_W_W(name) \
  117. extern "C" void *name(void *arg) { \
  118. typedef decltype(name) *fntype; \
  119. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  120. return fn(arg); \
  121. } \
  122. INTERCEPT_OR_DIE(#name, name);
  123. #define INTERCEPT_WRAP_W_WW(name) \
  124. extern "C" void *name(void *arg1, void *arg2) { \
  125. typedef decltype(name) *fntype; \
  126. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  127. return fn(arg1, arg2); \
  128. } \
  129. INTERCEPT_OR_DIE(#name, name);
  130. #define INTERCEPT_WRAP_W_WWW(name) \
  131. extern "C" void *name(void *arg1, void *arg2, void *arg3) { \
  132. typedef decltype(name) *fntype; \
  133. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  134. return fn(arg1, arg2, arg3); \
  135. } \
  136. INTERCEPT_OR_DIE(#name, name);
  137. #define INTERCEPT_WRAP_W_WWWW(name) \
  138. extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4) { \
  139. typedef decltype(name) *fntype; \
  140. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  141. return fn(arg1, arg2, arg3, arg4); \
  142. } \
  143. INTERCEPT_OR_DIE(#name, name);
  144. #define INTERCEPT_WRAP_W_WWWWW(name) \
  145. extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4, \
  146. void *arg5) { \
  147. typedef decltype(name) *fntype; \
  148. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  149. return fn(arg1, arg2, arg3, arg4, arg5); \
  150. } \
  151. INTERCEPT_OR_DIE(#name, name);
  152. #define INTERCEPT_WRAP_W_WWWWWW(name) \
  153. extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4, \
  154. void *arg5, void *arg6) { \
  155. typedef decltype(name) *fntype; \
  156. static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
  157. return fn(arg1, arg2, arg3, arg4, arg5, arg6); \
  158. } \
  159. INTERCEPT_OR_DIE(#name, name);
  160. #endif // SANITIZER_WIN_DLL_THUNK_H