1 //===-- asan_win.cc -------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // Windows-specific details.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
22 #include "asan_interceptors.h"
23 #include "asan_internal.h"
24 #include "asan_report.h"
25 #include "asan_thread.h"
26 #include "sanitizer_common/sanitizer_libc.h"
27 #include "sanitizer_common/sanitizer_mutex.h"
30 SANITIZER_INTERFACE_ATTRIBUTE
31 int __asan_should_detect_stack_use_after_return() {
33 return __asan_option_detect_stack_use_after_return;
39 // ---------------------- TSD ---------------- {{{1
40 static bool tsd_key_inited = false;
42 static __declspec(thread) void *fake_tsd = 0;
44 void AsanTSDInit(void (*destructor)(void *tsd)) {
45 // FIXME: we're ignoring the destructor for now.
46 tsd_key_inited = true;
50 CHECK(tsd_key_inited);
54 void AsanTSDSet(void *tsd) {
55 CHECK(tsd_key_inited);
59 void PlatformTSDDtor(void *tsd) {
60 AsanThread::TSDDtor(tsd);
62 // ---------------------- Various stuff ---------------- {{{1
63 void DisableReexec() {
64 // No need to re-exec on Windows.
68 // No need to re-exec on Windows.
71 void *AsanDoesNotSupportStaticLinkage() {
73 #error Please build the runtime with a non-debug CRT: /MD or /MT
78 void AsanCheckDynamicRTPrereqs() {}
80 void AsanCheckIncompatibleRT() {}
82 void AsanPlatformThreadInit() {
83 // Nothing here for now.
86 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
90 void AsanOnSIGSEGV(int, void *siginfo, void *context) {
94 static LPTOP_LEVEL_EXCEPTION_FILTER default_seh_handler;
96 SignalContext SignalContext::Create(void *siginfo, void *context) {
97 EXCEPTION_RECORD *exception_record = (EXCEPTION_RECORD*)siginfo;
98 CONTEXT *context_record = (CONTEXT*)context;
100 uptr pc = (uptr)exception_record->ExceptionAddress;
102 uptr bp = (uptr)context_record->Rbp;
103 uptr sp = (uptr)context_record->Rsp;
105 uptr bp = (uptr)context_record->Ebp;
106 uptr sp = (uptr)context_record->Esp;
108 uptr access_addr = exception_record->ExceptionInformation[1];
110 return SignalContext(context, access_addr, pc, sp, bp);
113 static long WINAPI SEHHandler(EXCEPTION_POINTERS *info) {
114 EXCEPTION_RECORD *exception_record = info->ExceptionRecord;
115 CONTEXT *context = info->ContextRecord;
117 if (exception_record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
118 exception_record->ExceptionCode == EXCEPTION_IN_PAGE_ERROR) {
119 const char *description =
120 (exception_record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
123 SignalContext sig = SignalContext::Create(exception_record, context);
124 ReportSIGSEGV(description, sig);
127 // FIXME: Handle EXCEPTION_STACK_OVERFLOW here.
129 return default_seh_handler(info);
132 // We want to install our own exception handler (EH) to print helpful reports
133 // on access violations and whatnot. Unfortunately, the CRT initializers assume
134 // they are run before any user code and drop any previously-installed EHs on
135 // the floor, so we can't install our handler inside __asan_init.
136 // (See crt0dat.c in the CRT sources for the details)
138 // Things get even more complicated with the dynamic runtime, as it finishes its
139 // initialization before the .exe module CRT begins to initialize.
141 // For the static runtime (-MT), it's enough to put a callback to
142 // __asan_set_seh_filter in the last section for C initializers.
144 // For the dynamic runtime (-MD), we want link the same
145 // asan_dynamic_runtime_thunk.lib to all the modules, thus __asan_set_seh_filter
146 // will be called for each instrumented module. This ensures that at least one
147 // __asan_set_seh_filter call happens after the .exe module CRT is initialized.
148 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
149 int __asan_set_seh_filter() {
150 // We should only store the previous handler if it's not our own handler in
151 // order to avoid loops in the EH chain.
152 auto prev_seh_handler = SetUnhandledExceptionFilter(SEHHandler);
153 if (prev_seh_handler != &SEHHandler)
154 default_seh_handler = prev_seh_handler;
159 // Put a pointer to __asan_set_seh_filter at the end of the global list
160 // of C initializers, after the default EH is set by the CRT.
161 #pragma section(".CRT$XIZ", long, read) // NOLINT
162 static __declspec(allocate(".CRT$XIZ"))
163 int (*__intercept_seh)() = __asan_set_seh_filter;
166 } // namespace __asan