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