llvm-project

Форк
0
/
asan_win_dynamic_runtime_thunk.cpp 
130 строк · 5.4 Кб
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

23
#ifdef SANITIZER_DYNAMIC_RUNTIME_THUNK
24
#define SANITIZER_IMPORT_INTERFACE 1
25
#include "sanitizer_common/sanitizer_win_defs.h"
26
#define WIN32_LEAN_AND_MEAN
27
#include <windows.h>
28

29
// Define weak alias for all weak functions imported from asan dll.
30
#define INTERFACE_FUNCTION(Name)
31
#define INTERFACE_WEAK_FUNCTION(Name) WIN_WEAK_IMPORT_DEF(Name)
32
#include "asan_interface.inc"
33

34
// First, declare CRT sections we'll be using in this file
35
#pragma section(".CRT$XIB", long, read)
36
#pragma section(".CRT$XID", long, read)
37
#pragma section(".CRT$XCAB", long, read)
38
#pragma section(".CRT$XTW", long, read)
39
#pragma section(".CRT$XTY", long, read)
40
#pragma section(".CRT$XLAB", long, read)
41

42
////////////////////////////////////////////////////////////////////////////////
43
// Define a copy of __asan_option_detect_stack_use_after_return that should be
44
// used when linking an MD runtime with a set of object files on Windows.
45
//
46
// The ASan MD runtime dllexports '__asan_option_detect_stack_use_after_return',
47
// so normally we would just dllimport it.  Unfortunately, the dllimport
48
// attribute adds __imp_ prefix to the symbol name of a variable.
49
// Since in general we don't know if a given TU is going to be used
50
// with a MT or MD runtime and we don't want to use ugly __imp_ names on Windows
51
// just to work around this issue, let's clone the variable that is constant
52
// after initialization anyways.
53
extern "C" {
54
__declspec(dllimport) int __asan_should_detect_stack_use_after_return();
55
int __asan_option_detect_stack_use_after_return;
56

57
__declspec(dllimport) void* __asan_get_shadow_memory_dynamic_address();
58
void* __asan_shadow_memory_dynamic_address;
59
}
60

61
static int InitializeClonedVariables() {
62
  __asan_option_detect_stack_use_after_return =
63
    __asan_should_detect_stack_use_after_return();
64
  __asan_shadow_memory_dynamic_address =
65
    __asan_get_shadow_memory_dynamic_address();
66
  return 0;
67
}
68

69
static void NTAPI asan_thread_init(void *mod, unsigned long reason,
70
    void *reserved) {
71
  if (reason == DLL_PROCESS_ATTACH) InitializeClonedVariables();
72
}
73

74
// Our cloned variables must be initialized before C/C++ constructors.  If TLS
75
// is used, our .CRT$XLAB initializer will run first. If not, our .CRT$XIB
76
// initializer is needed as a backup.
77
__declspec(allocate(".CRT$XIB")) int (*__asan_initialize_cloned_variables)() =
78
    InitializeClonedVariables;
79
__declspec(allocate(".CRT$XLAB")) void (NTAPI *__asan_tls_init)(void *,
80
    unsigned long, void *) = asan_thread_init;
81

82
////////////////////////////////////////////////////////////////////////////////
83
// For some reason, the MD CRT doesn't call the C/C++ terminators during on DLL
84
// unload or on exit.  ASan relies on LLVM global_dtors to call
85
// __asan_unregister_globals on these events, which unfortunately doesn't work
86
// with the MD runtime, see PR22545 for the details.
87
// To work around this, for each DLL we schedule a call to UnregisterGlobals
88
// using atexit() that calls a small subset of C terminators
89
// where LLVM global_dtors is placed.  Fingers crossed, no other C terminators
90
// are there.
91
extern "C" int __cdecl atexit(void (__cdecl *f)(void));
92
extern "C" void __cdecl _initterm(void *a, void *b);
93

94
namespace {
95
__declspec(allocate(".CRT$XTW")) void* before_global_dtors = 0;
96
__declspec(allocate(".CRT$XTY")) void* after_global_dtors = 0;
97

98
void UnregisterGlobals() {
99
  _initterm(&before_global_dtors, &after_global_dtors);
100
}
101

102
int ScheduleUnregisterGlobals() {
103
  return atexit(UnregisterGlobals);
104
}
105
}  // namespace
106

107
// We need to call 'atexit(UnregisterGlobals);' as early as possible, but after
108
// atexit() is initialized (.CRT$XIC).  As this is executed before C++
109
// initializers (think ctors for globals), UnregisterGlobals gets executed after
110
// dtors for C++ globals.
111
__declspec(allocate(".CRT$XID"))
112
int (*__asan_schedule_unregister_globals)() = ScheduleUnregisterGlobals;
113

114
////////////////////////////////////////////////////////////////////////////////
115
// ASan SEH handling.
116
// We need to set the ASan-specific SEH handler at the end of CRT initialization
117
// of each module (see also asan_win.cpp).
118
extern "C" {
119
__declspec(dllimport) int __asan_set_seh_filter();
120
static int SetSEHFilter() { return __asan_set_seh_filter(); }
121

122
// Unfortunately, putting a pointer to __asan_set_seh_filter into
123
// __asan_intercept_seh gets optimized out, so we have to use an extra function.
124
__declspec(allocate(".CRT$XCAB")) int (*__asan_seh_interceptor)() =
125
    SetSEHFilter;
126
}
127

128
WIN_FORCE_LINK(__asan_dso_reg_hook)
129

130
#endif // SANITIZER_DYNAMIC_RUNTIME_THUNK
131

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.