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