brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.3 KiB · 056e493 Raw
113 lines · cpp
1//===-- asan_win_common_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-exception6//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 modules12// to interact with the ASan DLL runtime correctly and can't be implemented13// using the default "import library" generated when linking the DLL.14//15// This includes:16//  - Cloning shadow memory dynamic address from ASAN DLL17//  - Creating weak aliases to default implementation imported from asan dll18//  - Forwarding the detect_stack_use_after_return runtime option19//  - installing a custom SEH handler20//21//===----------------------------------------------------------------------===//22 23#if defined(SANITIZER_DYNAMIC_RUNTIME_THUNK) || \24    defined(SANITIZER_STATIC_RUNTIME_THUNK)25#  define SANITIZER_IMPORT_INTERFACE 126#  define WIN32_LEAN_AND_MEAN27#  include "asan_win_common_runtime_thunk.h"28 29#  include <windows.h>30 31#  include "sanitizer_common/sanitizer_win_defs.h"32#  include "sanitizer_common/sanitizer_win_thunk_interception.h"33 34// Define weak alias for all weak functions imported from asan dll.35#  define INTERFACE_FUNCTION(Name)36#  define INTERFACE_WEAK_FUNCTION(Name) REGISTER_WEAK_FUNCTION(Name)37#  include "asan_interface.inc"38 39////////////////////////////////////////////////////////////////////////////////40// Define a copy of __asan_option_detect_stack_use_after_return that should be41// used when linking an MD runtime with a set of object files on Windows.42//43// The ASan MD runtime dllexports '__asan_option_detect_stack_use_after_return',44// so normally we would just dllimport it.  Unfortunately, the dllimport45// attribute adds __imp_ prefix to the symbol name of a variable.46// Since in general we don't know if a given TU is going to be used47// with a MT or MD runtime and we don't want to use ugly __imp_ names on Windows48// just to work around this issue, let's clone the variable that is constant49// after initialization anyways.50 51extern "C" {52__declspec(dllimport) int __asan_should_detect_stack_use_after_return();53int __asan_option_detect_stack_use_after_return;54 55__declspec(dllimport) void *__asan_get_shadow_memory_dynamic_address();56void *__asan_shadow_memory_dynamic_address;57 58static void __asan_initialize_cloned_variables() {59  __asan_option_detect_stack_use_after_return =60      __asan_should_detect_stack_use_after_return();61  __asan_shadow_memory_dynamic_address =62      __asan_get_shadow_memory_dynamic_address();63}64}65 66static int asan_thunk_init() {67  __asan_initialize_cloned_variables();68 69#  ifdef SANITIZER_STATIC_RUNTIME_THUNK70  __asan_initialize_static_thunk();71#  endif72 73  return 0;74}75 76static void WINAPI asan_thread_init(void *mod, unsigned long reason,77                                    void *reserved) {78  if (reason == DLL_PROCESS_ATTACH) {79    asan_thunk_init();80  }81}82 83// Our cloned variables must be initialized before C/C++ constructors.  If TLS84// is used, our .CRT$XLAB initializer will run first. If not, our .CRT$XIB85// initializer is needed as a backup.86extern "C" __declspec(allocate(".CRT$XIB")) int (*__asan_thunk_init)() =87    asan_thunk_init;88WIN_FORCE_LINK(__asan_thunk_init)89 90extern "C" __declspec(allocate(".CRT$XLAB")) void(WINAPI *__asan_tls_init)(91    void *, unsigned long, void *) = asan_thread_init;92WIN_FORCE_LINK(__asan_tls_init)93 94////////////////////////////////////////////////////////////////////////////////95// ASan SEH handling.96// We need to set the ASan-specific SEH handler at the end of CRT initialization97// of each module (see also asan_win.cpp).98extern "C" {99__declspec(dllimport) int __asan_set_seh_filter();100static int SetSEHFilter() { return __asan_set_seh_filter(); }101 102// Unfortunately, putting a pointer to __asan_set_seh_filter into103// __asan_intercept_seh gets optimized out, so we have to use an extra function.104extern "C" __declspec(allocate(".CRT$XCAB")) int (*__asan_seh_interceptor)() =105    SetSEHFilter;106WIN_FORCE_LINK(__asan_seh_interceptor)107}108 109WIN_FORCE_LINK(__asan_dso_reg_hook)110 111#endif  // defined(SANITIZER_DYNAMIC_RUNTIME_THUNK) ||112        // defined(SANITIZER_STATIC_RUNTIME_THUNK)113