asan_win_dynamic_runtime_thunk.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //===-- asan_win_dynamic_runtime_thunk.cpp --------------------------------===//
  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. //
  9. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // This file defines things that need to be present in the application modules
  12. // to interact with the ASan DLL runtime correctly and can't be implemented
  13. // using the default "import library" generated when linking the DLL RTL.
  14. //
  15. // This includes:
  16. // - creating weak aliases to default implementation imported from asan dll.
  17. // - forwarding the detect_stack_use_after_return runtime option
  18. // - working around deficiencies of the MD runtime
  19. // - installing a custom SEH handler
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifdef SANITIZER_DYNAMIC_RUNTIME_THUNK
  23. #define SANITIZER_IMPORT_INTERFACE 1
  24. #include "sanitizer_common/sanitizer_win_defs.h"
  25. #define WIN32_LEAN_AND_MEAN
  26. #include <windows.h>
  27. // Define weak alias for all weak functions imported from asan dll.
  28. #define INTERFACE_FUNCTION(Name)
  29. #define INTERFACE_WEAK_FUNCTION(Name) WIN_WEAK_IMPORT_DEF(Name)
  30. #include "asan_interface.inc"
  31. // First, declare CRT sections we'll be using in this file
  32. #pragma section(".CRT$XIB", long, read)
  33. #pragma section(".CRT$XID", long, read)
  34. #pragma section(".CRT$XCAB", long, read)
  35. #pragma section(".CRT$XTW", long, read)
  36. #pragma section(".CRT$XTY", long, read)
  37. #pragma section(".CRT$XLAB", long, read)
  38. ////////////////////////////////////////////////////////////////////////////////
  39. // Define a copy of __asan_option_detect_stack_use_after_return that should be
  40. // used when linking an MD runtime with a set of object files on Windows.
  41. //
  42. // The ASan MD runtime dllexports '__asan_option_detect_stack_use_after_return',
  43. // so normally we would just dllimport it. Unfortunately, the dllimport
  44. // attribute adds __imp_ prefix to the symbol name of a variable.
  45. // Since in general we don't know if a given TU is going to be used
  46. // with a MT or MD runtime and we don't want to use ugly __imp_ names on Windows
  47. // just to work around this issue, let's clone the variable that is constant
  48. // after initialization anyways.
  49. extern "C" {
  50. __declspec(dllimport) int __asan_should_detect_stack_use_after_return();
  51. int __asan_option_detect_stack_use_after_return;
  52. __declspec(dllimport) void* __asan_get_shadow_memory_dynamic_address();
  53. void* __asan_shadow_memory_dynamic_address;
  54. }
  55. static int InitializeClonedVariables() {
  56. __asan_option_detect_stack_use_after_return =
  57. __asan_should_detect_stack_use_after_return();
  58. __asan_shadow_memory_dynamic_address =
  59. __asan_get_shadow_memory_dynamic_address();
  60. return 0;
  61. }
  62. static void NTAPI asan_thread_init(void *mod, unsigned long reason,
  63. void *reserved) {
  64. if (reason == DLL_PROCESS_ATTACH) InitializeClonedVariables();
  65. }
  66. // Our cloned variables must be initialized before C/C++ constructors. If TLS
  67. // is used, our .CRT$XLAB initializer will run first. If not, our .CRT$XIB
  68. // initializer is needed as a backup.
  69. __declspec(allocate(".CRT$XIB")) int (*__asan_initialize_cloned_variables)() =
  70. InitializeClonedVariables;
  71. __declspec(allocate(".CRT$XLAB")) void (NTAPI *__asan_tls_init)(void *,
  72. unsigned long, void *) = asan_thread_init;
  73. ////////////////////////////////////////////////////////////////////////////////
  74. // For some reason, the MD CRT doesn't call the C/C++ terminators during on DLL
  75. // unload or on exit. ASan relies on LLVM global_dtors to call
  76. // __asan_unregister_globals on these events, which unfortunately doesn't work
  77. // with the MD runtime, see PR22545 for the details.
  78. // To work around this, for each DLL we schedule a call to UnregisterGlobals
  79. // using atexit() that calls a small subset of C terminators
  80. // where LLVM global_dtors is placed. Fingers crossed, no other C terminators
  81. // are there.
  82. extern "C" int __cdecl atexit(void (__cdecl *f)(void));
  83. extern "C" void __cdecl _initterm(void *a, void *b);
  84. namespace {
  85. __declspec(allocate(".CRT$XTW")) void* before_global_dtors = 0;
  86. __declspec(allocate(".CRT$XTY")) void* after_global_dtors = 0;
  87. void UnregisterGlobals() {
  88. _initterm(&before_global_dtors, &after_global_dtors);
  89. }
  90. int ScheduleUnregisterGlobals() {
  91. return atexit(UnregisterGlobals);
  92. }
  93. } // namespace
  94. // We need to call 'atexit(UnregisterGlobals);' as early as possible, but after
  95. // atexit() is initialized (.CRT$XIC). As this is executed before C++
  96. // initializers (think ctors for globals), UnregisterGlobals gets executed after
  97. // dtors for C++ globals.
  98. __declspec(allocate(".CRT$XID"))
  99. int (*__asan_schedule_unregister_globals)() = ScheduleUnregisterGlobals;
  100. ////////////////////////////////////////////////////////////////////////////////
  101. // ASan SEH handling.
  102. // We need to set the ASan-specific SEH handler at the end of CRT initialization
  103. // of each module (see also asan_win.cpp).
  104. extern "C" {
  105. __declspec(dllimport) int __asan_set_seh_filter();
  106. static int SetSEHFilter() { return __asan_set_seh_filter(); }
  107. // Unfortunately, putting a pointer to __asan_set_seh_filter into
  108. // __asan_intercept_seh gets optimized out, so we have to use an extra function.
  109. __declspec(allocate(".CRT$XCAB")) int (*__asan_seh_interceptor)() =
  110. SetSEHFilter;
  111. }
  112. WIN_FORCE_LINK(__asan_dso_reg_hook)
  113. #endif // SANITIZER_DYNAMIC_RUNTIME_THUNK