annotate libsanitizer/asan/asan_win_dynamic_runtime_thunk.cc @ 120:f93fa5091070

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