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