2014-09-21 17:33:12 +00:00
|
|
|
//===-- asan_win.cc -------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Windows-specific details.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
|
|
|
#if SANITIZER_WINDOWS
|
2017-04-10 11:32:00 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2014-09-21 17:33:12 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "asan_interceptors.h"
|
|
|
|
#include "asan_internal.h"
|
2015-08-28 15:33:40 +00:00
|
|
|
#include "asan_report.h"
|
2017-04-10 11:32:00 +00:00
|
|
|
#include "asan_stack.h"
|
2014-09-21 17:33:12 +00:00
|
|
|
#include "asan_thread.h"
|
2017-10-07 00:16:47 +00:00
|
|
|
#include "asan_mapping.h"
|
2014-09-21 17:33:12 +00:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
|
|
#include "sanitizer_common/sanitizer_mutex.h"
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
extern "C" {
|
2017-04-10 11:32:00 +00:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
int __asan_should_detect_stack_use_after_return() {
|
|
|
|
__asan_init();
|
|
|
|
return __asan_option_detect_stack_use_after_return;
|
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
uptr __asan_get_shadow_memory_dynamic_address() {
|
|
|
|
__asan_init();
|
|
|
|
return __asan_shadow_memory_dynamic_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------- A workaround for the absence of weak symbols ----- {{{
|
2017-04-10 11:32:00 +00:00
|
|
|
// We don't have a direct equivalent of weak symbols when using MSVC, but we can
|
|
|
|
// use the /alternatename directive to tell the linker to default a specific
|
|
|
|
// symbol to a specific value, which works nicely for allocator hooks and
|
|
|
|
// __asan_default_options().
|
|
|
|
void __sanitizer_default_malloc_hook(void *ptr, uptr size) { }
|
|
|
|
void __sanitizer_default_free_hook(void *ptr) { }
|
|
|
|
const char* __asan_default_default_options() { return ""; }
|
|
|
|
const char* __asan_default_default_suppressions() { return ""; }
|
|
|
|
void __asan_default_on_error() {}
|
2017-10-07 00:16:47 +00:00
|
|
|
// 64-bit msvc will not prepend an underscore for symbols.
|
|
|
|
#ifdef _WIN64
|
|
|
|
#pragma comment(linker, "/alternatename:__sanitizer_malloc_hook=__sanitizer_default_malloc_hook") // NOLINT
|
|
|
|
#pragma comment(linker, "/alternatename:__sanitizer_free_hook=__sanitizer_default_free_hook") // NOLINT
|
|
|
|
#pragma comment(linker, "/alternatename:__asan_default_options=__asan_default_default_options") // NOLINT
|
|
|
|
#pragma comment(linker, "/alternatename:__asan_default_suppressions=__asan_default_default_suppressions") // NOLINT
|
|
|
|
#pragma comment(linker, "/alternatename:__asan_on_error=__asan_default_on_error") // NOLINT
|
|
|
|
#else
|
2017-04-10 11:32:00 +00:00
|
|
|
#pragma comment(linker, "/alternatename:___sanitizer_malloc_hook=___sanitizer_default_malloc_hook") // NOLINT
|
|
|
|
#pragma comment(linker, "/alternatename:___sanitizer_free_hook=___sanitizer_default_free_hook") // NOLINT
|
|
|
|
#pragma comment(linker, "/alternatename:___asan_default_options=___asan_default_default_options") // NOLINT
|
|
|
|
#pragma comment(linker, "/alternatename:___asan_default_suppressions=___asan_default_default_suppressions") // NOLINT
|
|
|
|
#pragma comment(linker, "/alternatename:___asan_on_error=___asan_default_on_error") // NOLINT
|
2017-10-07 00:16:47 +00:00
|
|
|
#endif
|
2017-04-10 11:32:00 +00:00
|
|
|
// }}}
|
|
|
|
} // extern "C"
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
// ---------------------- Windows-specific interceptors ---------------- {{{
|
|
|
|
INTERCEPTOR_WINAPI(void, RtlRaiseException, EXCEPTION_RECORD *ExceptionRecord) {
|
|
|
|
CHECK(REAL(RtlRaiseException));
|
|
|
|
// This is a noreturn function, unless it's one of the exceptions raised to
|
|
|
|
// communicate with the debugger, such as the one from OutputDebugString.
|
|
|
|
if (ExceptionRecord->ExceptionCode != DBG_PRINTEXCEPTION_C)
|
|
|
|
__asan_handle_no_return();
|
|
|
|
REAL(RtlRaiseException)(ExceptionRecord);
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
INTERCEPTOR_WINAPI(void, RaiseException, void *a, void *b, void *c, void *d) {
|
|
|
|
CHECK(REAL(RaiseException));
|
|
|
|
__asan_handle_no_return();
|
|
|
|
REAL(RaiseException)(a, b, c, d);
|
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
#ifdef _WIN64
|
|
|
|
|
|
|
|
INTERCEPTOR_WINAPI(int, __C_specific_handler, void *a, void *b, void *c, void *d) { // NOLINT
|
|
|
|
CHECK(REAL(__C_specific_handler));
|
|
|
|
__asan_handle_no_return();
|
|
|
|
return REAL(__C_specific_handler)(a, b, c, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
INTERCEPTOR(int, _except_handler3, void *a, void *b, void *c, void *d) {
|
|
|
|
CHECK(REAL(_except_handler3));
|
|
|
|
__asan_handle_no_return();
|
|
|
|
return REAL(_except_handler3)(a, b, c, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ASAN_DYNAMIC
|
|
|
|
// This handler is named differently in -MT and -MD CRTs.
|
|
|
|
#define _except_handler4 _except_handler4_common
|
|
|
|
#endif
|
|
|
|
INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) {
|
|
|
|
CHECK(REAL(_except_handler4));
|
|
|
|
__asan_handle_no_return();
|
|
|
|
return REAL(_except_handler4)(a, b, c, d);
|
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
#endif
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
|
|
|
|
AsanThread *t = (AsanThread*)arg;
|
|
|
|
SetCurrentThread(t);
|
|
|
|
return t->ThreadStart(GetTid(), /* signal_thread_is_registered */ nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR_WINAPI(DWORD, CreateThread,
|
|
|
|
void* security, uptr stack_size,
|
|
|
|
DWORD (__stdcall *start_routine)(void*), void* arg,
|
|
|
|
DWORD thr_flags, void* tid) {
|
|
|
|
// Strict init-order checking is thread-hostile.
|
|
|
|
if (flags()->strict_init_order)
|
|
|
|
StopInitOrderChecking();
|
|
|
|
GET_STACK_TRACE_THREAD;
|
|
|
|
// FIXME: The CreateThread interceptor is not the same as a pthread_create
|
|
|
|
// one. This is a bandaid fix for PR22025.
|
|
|
|
bool detached = false; // FIXME: how can we determine it on Windows?
|
|
|
|
u32 current_tid = GetCurrentTidOrInvalid();
|
|
|
|
AsanThread *t =
|
|
|
|
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
|
|
|
|
return REAL(CreateThread)(security, stack_size,
|
|
|
|
asan_thread_start, t, thr_flags, tid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
namespace __asan {
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
void InitializePlatformInterceptors() {
|
|
|
|
ASAN_INTERCEPT_FUNC(CreateThread);
|
2017-10-07 00:16:47 +00:00
|
|
|
|
|
|
|
#ifdef _WIN64
|
|
|
|
ASAN_INTERCEPT_FUNC(__C_specific_handler);
|
|
|
|
#else
|
2017-04-10 11:32:00 +00:00
|
|
|
ASAN_INTERCEPT_FUNC(_except_handler3);
|
|
|
|
ASAN_INTERCEPT_FUNC(_except_handler4);
|
2017-10-07 00:16:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Try to intercept kernel32!RaiseException, and if that fails, intercept
|
|
|
|
// ntdll!RtlRaiseException instead.
|
|
|
|
if (!::__interception::OverrideFunction("RaiseException",
|
|
|
|
(uptr)WRAP(RaiseException),
|
|
|
|
(uptr *)&REAL(RaiseException))) {
|
|
|
|
CHECK(::__interception::OverrideFunction("RtlRaiseException",
|
|
|
|
(uptr)WRAP(RtlRaiseException),
|
|
|
|
(uptr *)&REAL(RtlRaiseException)));
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
|
|
|
|
UNIMPLEMENTED();
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------- TSD ---------------- {{{
|
2014-09-21 17:33:12 +00:00
|
|
|
static bool tsd_key_inited = false;
|
|
|
|
|
|
|
|
static __declspec(thread) void *fake_tsd = 0;
|
|
|
|
|
|
|
|
void AsanTSDInit(void (*destructor)(void *tsd)) {
|
|
|
|
// FIXME: we're ignoring the destructor for now.
|
|
|
|
tsd_key_inited = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *AsanTSDGet() {
|
|
|
|
CHECK(tsd_key_inited);
|
|
|
|
return fake_tsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsanTSDSet(void *tsd) {
|
|
|
|
CHECK(tsd_key_inited);
|
|
|
|
fake_tsd = tsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlatformTSDDtor(void *tsd) {
|
|
|
|
AsanThread::TSDDtor(tsd);
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
// }}}
|
|
|
|
|
|
|
|
// ---------------------- Various stuff ---------------- {{{
|
2014-09-21 17:33:12 +00:00
|
|
|
void *AsanDoesNotSupportStaticLinkage() {
|
|
|
|
#if defined(_DEBUG)
|
|
|
|
#error Please build the runtime with a non-debug CRT: /MD or /MT
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
void AsanCheckDynamicRTPrereqs() {}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
void AsanCheckIncompatibleRT() {}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
|
|
|
void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
void AsanOnDeadlySignal(int, void *siginfo, void *context) {
|
2015-08-28 15:33:40 +00:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
#if SANITIZER_WINDOWS64
|
|
|
|
// Exception handler for dealing with shadow memory.
|
|
|
|
static LONG CALLBACK
|
|
|
|
ShadowExceptionHandler(PEXCEPTION_POINTERS exception_pointers) {
|
|
|
|
uptr page_size = GetPageSizeCached();
|
|
|
|
// Only handle access violations.
|
|
|
|
if (exception_pointers->ExceptionRecord->ExceptionCode !=
|
|
|
|
EXCEPTION_ACCESS_VIOLATION) {
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only handle access violations that land within the shadow memory.
|
|
|
|
uptr addr =
|
|
|
|
(uptr)(exception_pointers->ExceptionRecord->ExceptionInformation[1]);
|
|
|
|
|
|
|
|
// Check valid shadow range.
|
|
|
|
if (!AddrIsInShadow(addr)) return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
|
|
|
|
// This is an access violation while trying to read from the shadow. Commit
|
|
|
|
// the relevant page and let execution continue.
|
|
|
|
|
|
|
|
// Determine the address of the page that is being accessed.
|
|
|
|
uptr page = RoundDownTo(addr, page_size);
|
|
|
|
|
|
|
|
// Query the existing page.
|
|
|
|
MEMORY_BASIC_INFORMATION mem_info = {};
|
|
|
|
if (::VirtualQuery((LPVOID)page, &mem_info, sizeof(mem_info)) == 0)
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
|
|
|
|
// Commit the page.
|
|
|
|
uptr result =
|
|
|
|
(uptr)::VirtualAlloc((LPVOID)page, page_size, MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
if (result != page) return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
|
|
|
|
// The page mapping succeeded, so continue execution as usual.
|
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void InitializePlatformExceptionHandlers() {
|
|
|
|
#if SANITIZER_WINDOWS64
|
|
|
|
// On Win64, we map memory on demand with access violation handler.
|
|
|
|
// Install our exception handler.
|
|
|
|
CHECK(AddVectoredExceptionHandler(TRUE, &ShadowExceptionHandler));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
static LPTOP_LEVEL_EXCEPTION_FILTER default_seh_handler;
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
// Check based on flags if we should report this exception.
|
|
|
|
static bool ShouldReportDeadlyException(unsigned code) {
|
|
|
|
switch (code) {
|
|
|
|
case EXCEPTION_ACCESS_VIOLATION:
|
|
|
|
case EXCEPTION_IN_PAGE_ERROR:
|
|
|
|
return common_flags()->handle_segv;
|
|
|
|
case EXCEPTION_BREAKPOINT:
|
|
|
|
case EXCEPTION_ILLEGAL_INSTRUCTION: {
|
|
|
|
return common_flags()->handle_sigill;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the textual name for this exception.
|
|
|
|
const char *DescribeSignalOrException(int signo) {
|
|
|
|
unsigned code = signo;
|
|
|
|
// Get the string description of the exception if this is a known deadly
|
|
|
|
// exception.
|
|
|
|
switch (code) {
|
|
|
|
case EXCEPTION_ACCESS_VIOLATION:
|
|
|
|
return "access-violation";
|
|
|
|
case EXCEPTION_IN_PAGE_ERROR:
|
|
|
|
return "in-page-error";
|
|
|
|
case EXCEPTION_BREAKPOINT:
|
|
|
|
return "breakpoint";
|
|
|
|
case EXCEPTION_ILLEGAL_INSTRUCTION:
|
|
|
|
return "illegal-instruction";
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
static long WINAPI SEHHandler(EXCEPTION_POINTERS *info) {
|
|
|
|
EXCEPTION_RECORD *exception_record = info->ExceptionRecord;
|
|
|
|
CONTEXT *context = info->ContextRecord;
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
if (ShouldReportDeadlyException(exception_record->ExceptionCode)) {
|
2017-04-10 11:32:00 +00:00
|
|
|
SignalContext sig = SignalContext::Create(exception_record, context);
|
2017-10-07 00:16:47 +00:00
|
|
|
ReportDeadlySignal(exception_record->ExceptionCode, sig);
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
|
|
|
|
// FIXME: Handle EXCEPTION_STACK_OVERFLOW here.
|
|
|
|
|
|
|
|
return default_seh_handler(info);
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
// We want to install our own exception handler (EH) to print helpful reports
|
|
|
|
// on access violations and whatnot. Unfortunately, the CRT initializers assume
|
|
|
|
// they are run before any user code and drop any previously-installed EHs on
|
|
|
|
// the floor, so we can't install our handler inside __asan_init.
|
|
|
|
// (See crt0dat.c in the CRT sources for the details)
|
|
|
|
//
|
|
|
|
// Things get even more complicated with the dynamic runtime, as it finishes its
|
|
|
|
// initialization before the .exe module CRT begins to initialize.
|
|
|
|
//
|
|
|
|
// For the static runtime (-MT), it's enough to put a callback to
|
|
|
|
// __asan_set_seh_filter in the last section for C initializers.
|
|
|
|
//
|
|
|
|
// For the dynamic runtime (-MD), we want link the same
|
|
|
|
// asan_dynamic_runtime_thunk.lib to all the modules, thus __asan_set_seh_filter
|
|
|
|
// will be called for each instrumented module. This ensures that at least one
|
|
|
|
// __asan_set_seh_filter call happens after the .exe module CRT is initialized.
|
|
|
|
extern "C" SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
int __asan_set_seh_filter() {
|
|
|
|
// We should only store the previous handler if it's not our own handler in
|
|
|
|
// order to avoid loops in the EH chain.
|
|
|
|
auto prev_seh_handler = SetUnhandledExceptionFilter(SEHHandler);
|
|
|
|
if (prev_seh_handler != &SEHHandler)
|
|
|
|
default_seh_handler = prev_seh_handler;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !ASAN_DYNAMIC
|
2017-10-07 00:16:47 +00:00
|
|
|
// The CRT runs initializers in this order:
|
|
|
|
// - C initializers, from XIA to XIZ
|
|
|
|
// - C++ initializers, from XCA to XCZ
|
|
|
|
// Prior to 2015, the CRT set the unhandled exception filter at priority XIY,
|
|
|
|
// near the end of C initialization. Starting in 2015, it was moved to the
|
|
|
|
// beginning of C++ initialization. We set our priority to XCAB to run
|
|
|
|
// immediately after the CRT runs. This way, our exception filter is called
|
|
|
|
// first and we can delegate to their filter if appropriate.
|
|
|
|
#pragma section(".CRT$XCAB", long, read) // NOLINT
|
|
|
|
__declspec(allocate(".CRT$XCAB"))
|
2015-08-28 15:33:40 +00:00
|
|
|
int (*__intercept_seh)() = __asan_set_seh_filter;
|
|
|
|
#endif
|
2017-04-10 11:32:00 +00:00
|
|
|
// }}}
|
2015-08-28 15:33:40 +00:00
|
|
|
} // namespace __asan
|
2014-09-21 17:33:12 +00:00
|
|
|
|
|
|
|
#endif // _WIN32
|