PR c/67432
[official-gcc.git] / libsanitizer / asan / asan_win_dynamic_runtime_thunk.cc
blob1b59677eeffcd1fcff6c5db9ca18b1c31e2348de
1 //===-- asan_win_uar_thunk.cc ---------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // This file defines things that need to be present in the application modules
11 // to interact with the ASan DLL runtime correctly and can't be implemented
12 // using the default "import library" generated when linking the DLL RTL.
14 // This includes:
15 // - forwarding the detect_stack_use_after_return runtime option
16 // - installing a custom SEH handler
18 //===----------------------------------------------------------------------===//
20 // Only compile this code when buidling asan_dynamic_runtime_thunk.lib
21 // Using #ifdef rather than relying on Makefiles etc.
22 // simplifies the build procedure.
23 #ifdef ASAN_DYNAMIC_RUNTIME_THUNK
24 extern "C" {
25 __declspec(dllimport) int __asan_set_seh_filter();
26 __declspec(dllimport) int __asan_should_detect_stack_use_after_return();
28 // Define a copy of __asan_option_detect_stack_use_after_return that should be
29 // used when linking an MD runtime with a set of object files on Windows.
31 // The ASan MD runtime dllexports '__asan_option_detect_stack_use_after_return',
32 // so normally we would just dllimport it. Unfortunately, the dllimport
33 // attribute adds __imp_ prefix to the symbol name of a variable.
34 // Since in general we don't know if a given TU is going to be used
35 // with a MT or MD runtime and we don't want to use ugly __imp_ names on Windows
36 // just to work around this issue, let's clone the a variable that is
37 // constant after initialization anyways.
38 int __asan_option_detect_stack_use_after_return =
39 __asan_should_detect_stack_use_after_return();
41 // Set the ASan-specific SEH handler at the end of CRT initialization of each
42 // module (see asan_win.cc for the details).
44 // Unfortunately, putting a pointer to __asan_set_seh_filter into
45 // __asan_intercept_seh gets optimized out, so we have to use an extra function.
46 static int SetSEHFilter() { return __asan_set_seh_filter(); }
47 #pragma section(".CRT$XIZ", long, read) // NOLINT
48 __declspec(allocate(".CRT$XIZ")) int (*__asan_seh_interceptor)() = SetSEHFilter;
50 #endif // ASAN_DYNAMIC_RUNTIME_THUNK