1 | //===-- asan_win_common_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. |
14 | // |
15 | // This includes: |
16 | // - Cloning shadow memory dynamic address from ASAN DLL |
17 | // - Creating weak aliases to default implementation imported from asan dll |
18 | // - Forwarding the detect_stack_use_after_return runtime option |
19 | // - installing a custom SEH handler |
20 | // |
21 | //===----------------------------------------------------------------------===// |
22 | |
23 | #if defined(SANITIZER_DYNAMIC_RUNTIME_THUNK) || \ |
24 | defined(SANITIZER_STATIC_RUNTIME_THUNK) |
25 | # define SANITIZER_IMPORT_INTERFACE 1 |
26 | # define WIN32_LEAN_AND_MEAN |
27 | # include "asan_win_common_runtime_thunk.h" |
28 | |
29 | # include <windows.h> |
30 | |
31 | # include "sanitizer_common/sanitizer_win_defs.h" |
32 | # include "sanitizer_common/sanitizer_win_thunk_interception.h" |
33 | |
34 | // Define weak alias for all weak functions imported from asan dll. |
35 | # define INTERFACE_FUNCTION(Name) |
36 | # define INTERFACE_WEAK_FUNCTION(Name) REGISTER_WEAK_FUNCTION(Name) |
37 | # include "asan_interface.inc" |
38 | |
39 | //////////////////////////////////////////////////////////////////////////////// |
40 | // Define a copy of __asan_option_detect_stack_use_after_return that should be |
41 | // used when linking an MD runtime with a set of object files on Windows. |
42 | // |
43 | // The ASan MD runtime dllexports '__asan_option_detect_stack_use_after_return', |
44 | // so normally we would just dllimport it. Unfortunately, the dllimport |
45 | // attribute adds __imp_ prefix to the symbol name of a variable. |
46 | // Since in general we don't know if a given TU is going to be used |
47 | // with a MT or MD runtime and we don't want to use ugly __imp_ names on Windows |
48 | // just to work around this issue, let's clone the variable that is constant |
49 | // after initialization anyways. |
50 | |
51 | extern "C" { |
52 | __declspec(dllimport) int __asan_should_detect_stack_use_after_return(); |
53 | int __asan_option_detect_stack_use_after_return; |
54 | |
55 | __declspec(dllimport) void *__asan_get_shadow_memory_dynamic_address(); |
56 | void *__asan_shadow_memory_dynamic_address; |
57 | |
58 | static void __asan_initialize_cloned_variables() { |
59 | __asan_option_detect_stack_use_after_return = |
60 | __asan_should_detect_stack_use_after_return(); |
61 | __asan_shadow_memory_dynamic_address = |
62 | __asan_get_shadow_memory_dynamic_address(); |
63 | } |
64 | } |
65 | |
66 | static int asan_thunk_init() { |
67 | __asan_initialize_cloned_variables(); |
68 | |
69 | # ifdef SANITIZER_STATIC_RUNTIME_THUNK |
70 | __asan_initialize_static_thunk(); |
71 | # endif |
72 | |
73 | return 0; |
74 | } |
75 | |
76 | static void WINAPI asan_thread_init(void *mod, unsigned long reason, |
77 | void *reserved) { |
78 | if (reason == DLL_PROCESS_ATTACH) { |
79 | asan_thunk_init(); |
80 | } |
81 | } |
82 | |
83 | // Our cloned variables must be initialized before C/C++ constructors. If TLS |
84 | // is used, our .CRT$XLAB initializer will run first. If not, our .CRT$XIB |
85 | // initializer is needed as a backup. |
86 | extern "C" __declspec(allocate(".CRT$XIB" )) int (*__asan_thunk_init)() = |
87 | asan_thunk_init; |
88 | WIN_FORCE_LINK(__asan_thunk_init) |
89 | |
90 | extern "C" __declspec(allocate(".CRT$XLAB" )) void(WINAPI *__asan_tls_init)( |
91 | void *, unsigned long, void *) = asan_thread_init; |
92 | WIN_FORCE_LINK(__asan_tls_init) |
93 | |
94 | //////////////////////////////////////////////////////////////////////////////// |
95 | // ASan SEH handling. |
96 | // We need to set the ASan-specific SEH handler at the end of CRT initialization |
97 | // of each module (see also asan_win.cpp). |
98 | extern "C" { |
99 | __declspec(dllimport) int __asan_set_seh_filter(); |
100 | static int SetSEHFilter() { return __asan_set_seh_filter(); } |
101 | |
102 | // Unfortunately, putting a pointer to __asan_set_seh_filter into |
103 | // __asan_intercept_seh gets optimized out, so we have to use an extra function. |
104 | extern "C" __declspec(allocate(".CRT$XCAB" )) int (*__asan_seh_interceptor)() = |
105 | SetSEHFilter; |
106 | WIN_FORCE_LINK(__asan_seh_interceptor) |
107 | } |
108 | |
109 | WIN_FORCE_LINK(__asan_dso_reg_hook) |
110 | |
111 | #endif // defined(SANITIZER_DYNAMIC_RUNTIME_THUNK) || |
112 | // defined(SANITIZER_STATIC_RUNTIME_THUNK) |
113 | |