2019-08-01 21:43:28 +08:00
|
|
|
//===-- asan_win_dynamic_runtime_thunk.cpp --------------------------------===//
|
2014-09-12 21:21:02 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-09-12 21:21:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// This file defines things that need to be present in the application modules
|
|
|
|
// to interact with the ASan DLL runtime correctly and can't be implemented
|
|
|
|
// using the default "import library" generated when linking the DLL RTL.
|
|
|
|
//
|
|
|
|
// This includes:
|
[sanitizer] Add dynamic_runtime_thunk for different sanitizers.
In Windows, when the sanitizer is implemented as a shared library (DLL), we need
an auxiliary static library dynamic_runtime_thunk that will be linked to the
main executable and dlls.
In the sanitizer DLL, we are exposing weak functions with WIN_WEAK_EXPORT_DEF(),
which exports the default implementation with __dll suffix. For example: for
sanitizer coverage, the default implementation of __sanitizer_cov_trace_cmp is
exported as: __sanitizer_cov_trace_cmp__dll.
In the dynamic_runtime_thunk static library, we include weak aliases to the
imported implementation from the dll, using the macro WIN_WEAK_IMPORT_DEF().
By default, all users's programs that include calls to weak functions like
__sanitizer_cov_trace_cmp, will be redirected to the implementation in the dll,
when linking to dynamic_runtime_thunk.
After this diff, we are able to compile code with sanitizer coverage
instrumentation on Windows. When the instrumented object files are linked with
clang-rt_asan_dynamic_runtime_thunk-arch.lib all the weak symbols will be
resolved to the implementation imported from asan dll.
All the files sanitizer_dynamic_runtime_thunk.cc are independent, so we do not
need to include a specific list of sanitizers.
Now, we compile: [asan|ubsan|sanitizer_coverage]_win_dynamic_runtime_thunk.cc
and sanitizer_win_dynamic_runtime_thunk.cc to generate
asan_dynamic_runtime_thunk.lib, because we include asan, ubsan and sanitizer
coverage in the address sanitizer library.
Differential Revision: https://reviews.llvm.org/D29158
llvm-svn: 293953
2017-02-03 07:01:41 +08:00
|
|
|
// - creating weak aliases to default implementation imported from asan dll.
|
2014-09-12 21:21:02 +08:00
|
|
|
// - forwarding the detect_stack_use_after_return runtime option
|
2015-03-03 03:41:09 +08:00
|
|
|
// - working around deficiencies of the MD runtime
|
2016-08-01 23:08:12 +08:00
|
|
|
// - installing a custom SEH handler
|
2014-09-12 21:21:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[sanitizer] Add dynamic_runtime_thunk for different sanitizers.
In Windows, when the sanitizer is implemented as a shared library (DLL), we need
an auxiliary static library dynamic_runtime_thunk that will be linked to the
main executable and dlls.
In the sanitizer DLL, we are exposing weak functions with WIN_WEAK_EXPORT_DEF(),
which exports the default implementation with __dll suffix. For example: for
sanitizer coverage, the default implementation of __sanitizer_cov_trace_cmp is
exported as: __sanitizer_cov_trace_cmp__dll.
In the dynamic_runtime_thunk static library, we include weak aliases to the
imported implementation from the dll, using the macro WIN_WEAK_IMPORT_DEF().
By default, all users's programs that include calls to weak functions like
__sanitizer_cov_trace_cmp, will be redirected to the implementation in the dll,
when linking to dynamic_runtime_thunk.
After this diff, we are able to compile code with sanitizer coverage
instrumentation on Windows. When the instrumented object files are linked with
clang-rt_asan_dynamic_runtime_thunk-arch.lib all the weak symbols will be
resolved to the implementation imported from asan dll.
All the files sanitizer_dynamic_runtime_thunk.cc are independent, so we do not
need to include a specific list of sanitizers.
Now, we compile: [asan|ubsan|sanitizer_coverage]_win_dynamic_runtime_thunk.cc
and sanitizer_win_dynamic_runtime_thunk.cc to generate
asan_dynamic_runtime_thunk.lib, because we include asan, ubsan and sanitizer
coverage in the address sanitizer library.
Differential Revision: https://reviews.llvm.org/D29158
llvm-svn: 293953
2017-02-03 07:01:41 +08:00
|
|
|
#ifdef SANITIZER_DYNAMIC_RUNTIME_THUNK
|
|
|
|
#define SANITIZER_IMPORT_INTERFACE 1
|
2017-01-21 05:09:36 +08:00
|
|
|
#include "sanitizer_common/sanitizer_win_defs.h"
|
2015-10-30 04:36:55 +08:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2015-02-20 23:34:16 +08:00
|
|
|
#include <windows.h>
|
2014-09-12 21:21:02 +08:00
|
|
|
|
[sanitizer] Add dynamic_runtime_thunk for different sanitizers.
In Windows, when the sanitizer is implemented as a shared library (DLL), we need
an auxiliary static library dynamic_runtime_thunk that will be linked to the
main executable and dlls.
In the sanitizer DLL, we are exposing weak functions with WIN_WEAK_EXPORT_DEF(),
which exports the default implementation with __dll suffix. For example: for
sanitizer coverage, the default implementation of __sanitizer_cov_trace_cmp is
exported as: __sanitizer_cov_trace_cmp__dll.
In the dynamic_runtime_thunk static library, we include weak aliases to the
imported implementation from the dll, using the macro WIN_WEAK_IMPORT_DEF().
By default, all users's programs that include calls to weak functions like
__sanitizer_cov_trace_cmp, will be redirected to the implementation in the dll,
when linking to dynamic_runtime_thunk.
After this diff, we are able to compile code with sanitizer coverage
instrumentation on Windows. When the instrumented object files are linked with
clang-rt_asan_dynamic_runtime_thunk-arch.lib all the weak symbols will be
resolved to the implementation imported from asan dll.
All the files sanitizer_dynamic_runtime_thunk.cc are independent, so we do not
need to include a specific list of sanitizers.
Now, we compile: [asan|ubsan|sanitizer_coverage]_win_dynamic_runtime_thunk.cc
and sanitizer_win_dynamic_runtime_thunk.cc to generate
asan_dynamic_runtime_thunk.lib, because we include asan, ubsan and sanitizer
coverage in the address sanitizer library.
Differential Revision: https://reviews.llvm.org/D29158
llvm-svn: 293953
2017-02-03 07:01:41 +08:00
|
|
|
// Define weak alias for all weak functions imported from asan dll.
|
|
|
|
#define INTERFACE_FUNCTION(Name)
|
|
|
|
#define INTERFACE_WEAK_FUNCTION(Name) WIN_WEAK_IMPORT_DEF(Name)
|
|
|
|
#include "asan_interface.inc"
|
|
|
|
|
2015-03-03 03:41:09 +08:00
|
|
|
// First, declare CRT sections we'll be using in this file
|
2019-09-12 07:19:48 +08:00
|
|
|
#pragma section(".CRT$XIB", long, read)
|
|
|
|
#pragma section(".CRT$XID", long, read)
|
|
|
|
#pragma section(".CRT$XCAB", long, read)
|
|
|
|
#pragma section(".CRT$XTW", long, read)
|
|
|
|
#pragma section(".CRT$XTY", long, read)
|
|
|
|
#pragma section(".CRT$XLAB", long, read)
|
2015-03-03 03:41:09 +08:00
|
|
|
|
2016-09-22 02:22:43 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Define a copy of __asan_option_detect_stack_use_after_return that should be
|
|
|
|
// used when linking an MD runtime with a set of object files on Windows.
|
|
|
|
//
|
|
|
|
// The ASan MD runtime dllexports '__asan_option_detect_stack_use_after_return',
|
|
|
|
// so normally we would just dllimport it. Unfortunately, the dllimport
|
|
|
|
// attribute adds __imp_ prefix to the symbol name of a variable.
|
|
|
|
// Since in general we don't know if a given TU is going to be used
|
|
|
|
// with a MT or MD runtime and we don't want to use ugly __imp_ names on Windows
|
|
|
|
// just to work around this issue, let's clone the variable that is constant
|
|
|
|
// after initialization anyways.
|
|
|
|
extern "C" {
|
|
|
|
__declspec(dllimport) int __asan_should_detect_stack_use_after_return();
|
2016-11-01 01:13:02 +08:00
|
|
|
int __asan_option_detect_stack_use_after_return;
|
2016-10-01 01:47:34 +08:00
|
|
|
|
|
|
|
__declspec(dllimport) void* __asan_get_shadow_memory_dynamic_address();
|
2016-11-01 01:13:02 +08:00
|
|
|
void* __asan_shadow_memory_dynamic_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int InitializeClonedVariables() {
|
|
|
|
__asan_option_detect_stack_use_after_return =
|
|
|
|
__asan_should_detect_stack_use_after_return();
|
|
|
|
__asan_shadow_memory_dynamic_address =
|
2016-10-01 01:47:34 +08:00
|
|
|
__asan_get_shadow_memory_dynamic_address();
|
2016-11-01 01:13:02 +08:00
|
|
|
return 0;
|
2016-09-22 02:22:43 +08:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:36:45 +08:00
|
|
|
static void NTAPI asan_thread_init(void *mod, unsigned long reason,
|
|
|
|
void *reserved) {
|
2016-11-09 04:45:45 +08:00
|
|
|
if (reason == DLL_PROCESS_ATTACH) InitializeClonedVariables();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our cloned variables must be initialized before C/C++ constructors. If TLS
|
|
|
|
// is used, our .CRT$XLAB initializer will run first. If not, our .CRT$XIB
|
|
|
|
// initializer is needed as a backup.
|
|
|
|
__declspec(allocate(".CRT$XIB")) int (*__asan_initialize_cloned_variables)() =
|
|
|
|
InitializeClonedVariables;
|
2016-11-09 16:36:45 +08:00
|
|
|
__declspec(allocate(".CRT$XLAB")) void (NTAPI *__asan_tls_init)(void *,
|
|
|
|
unsigned long, void *) = asan_thread_init;
|
2016-11-01 01:13:02 +08:00
|
|
|
|
2015-02-20 23:34:16 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2015-03-03 03:41:09 +08:00
|
|
|
// For some reason, the MD CRT doesn't call the C/C++ terminators during on DLL
|
|
|
|
// unload or on exit. ASan relies on LLVM global_dtors to call
|
|
|
|
// __asan_unregister_globals on these events, which unfortunately doesn't work
|
|
|
|
// with the MD runtime, see PR22545 for the details.
|
|
|
|
// To work around this, for each DLL we schedule a call to UnregisterGlobals
|
|
|
|
// using atexit() that calls a small subset of C terminators
|
|
|
|
// where LLVM global_dtors is placed. Fingers crossed, no other C terminators
|
|
|
|
// are there.
|
2015-12-10 05:43:03 +08:00
|
|
|
extern "C" int __cdecl atexit(void (__cdecl *f)(void));
|
2015-03-03 03:41:09 +08:00
|
|
|
extern "C" void __cdecl _initterm(void *a, void *b);
|
2015-02-20 23:34:16 +08:00
|
|
|
|
|
|
|
namespace {
|
2015-03-03 03:41:09 +08:00
|
|
|
__declspec(allocate(".CRT$XTW")) void* before_global_dtors = 0;
|
|
|
|
__declspec(allocate(".CRT$XTY")) void* after_global_dtors = 0;
|
2015-02-20 23:34:16 +08:00
|
|
|
|
|
|
|
void UnregisterGlobals() {
|
2015-03-03 03:41:09 +08:00
|
|
|
_initterm(&before_global_dtors, &after_global_dtors);
|
2015-02-20 23:34:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ScheduleUnregisterGlobals() {
|
|
|
|
return atexit(UnregisterGlobals);
|
|
|
|
}
|
2016-10-25 03:56:18 +08:00
|
|
|
} // namespace
|
2015-02-20 23:34:16 +08:00
|
|
|
|
2015-03-03 03:41:09 +08:00
|
|
|
// We need to call 'atexit(UnregisterGlobals);' as early as possible, but after
|
|
|
|
// atexit() is initialized (.CRT$XIC). As this is executed before C++
|
|
|
|
// initializers (think ctors for globals), UnregisterGlobals gets executed after
|
|
|
|
// dtors for C++ globals.
|
2015-02-20 23:34:16 +08:00
|
|
|
__declspec(allocate(".CRT$XID"))
|
2015-03-03 03:41:09 +08:00
|
|
|
int (*__asan_schedule_unregister_globals)() = ScheduleUnregisterGlobals;
|
2015-02-20 23:34:16 +08:00
|
|
|
|
2015-03-03 03:41:09 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ASan SEH handling.
|
2015-02-20 23:34:16 +08:00
|
|
|
// We need to set the ASan-specific SEH handler at the end of CRT initialization
|
2019-08-01 21:43:28 +08:00
|
|
|
// of each module (see also asan_win.cpp).
|
2015-03-03 03:41:09 +08:00
|
|
|
extern "C" {
|
|
|
|
__declspec(dllimport) int __asan_set_seh_filter();
|
|
|
|
static int SetSEHFilter() { return __asan_set_seh_filter(); }
|
|
|
|
|
2014-09-12 22:01:30 +08:00
|
|
|
// Unfortunately, putting a pointer to __asan_set_seh_filter into
|
|
|
|
// __asan_intercept_seh gets optimized out, so we have to use an extra function.
|
2016-03-22 08:53:04 +08:00
|
|
|
__declspec(allocate(".CRT$XCAB")) int (*__asan_seh_interceptor)() =
|
|
|
|
SetSEHFilter;
|
2015-03-03 03:41:09 +08:00
|
|
|
}
|
2015-02-20 23:34:16 +08:00
|
|
|
|
2017-01-21 05:09:36 +08:00
|
|
|
WIN_FORCE_LINK(__asan_dso_reg_hook)
|
2016-11-18 03:02:53 +08:00
|
|
|
|
[sanitizer] Add dynamic_runtime_thunk for different sanitizers.
In Windows, when the sanitizer is implemented as a shared library (DLL), we need
an auxiliary static library dynamic_runtime_thunk that will be linked to the
main executable and dlls.
In the sanitizer DLL, we are exposing weak functions with WIN_WEAK_EXPORT_DEF(),
which exports the default implementation with __dll suffix. For example: for
sanitizer coverage, the default implementation of __sanitizer_cov_trace_cmp is
exported as: __sanitizer_cov_trace_cmp__dll.
In the dynamic_runtime_thunk static library, we include weak aliases to the
imported implementation from the dll, using the macro WIN_WEAK_IMPORT_DEF().
By default, all users's programs that include calls to weak functions like
__sanitizer_cov_trace_cmp, will be redirected to the implementation in the dll,
when linking to dynamic_runtime_thunk.
After this diff, we are able to compile code with sanitizer coverage
instrumentation on Windows. When the instrumented object files are linked with
clang-rt_asan_dynamic_runtime_thunk-arch.lib all the weak symbols will be
resolved to the implementation imported from asan dll.
All the files sanitizer_dynamic_runtime_thunk.cc are independent, so we do not
need to include a specific list of sanitizers.
Now, we compile: [asan|ubsan|sanitizer_coverage]_win_dynamic_runtime_thunk.cc
and sanitizer_win_dynamic_runtime_thunk.cc to generate
asan_dynamic_runtime_thunk.lib, because we include asan, ubsan and sanitizer
coverage in the address sanitizer library.
Differential Revision: https://reviews.llvm.org/D29158
llvm-svn: 293953
2017-02-03 07:01:41 +08:00
|
|
|
#endif // SANITIZER_DYNAMIC_RUNTIME_THUNK
|