From 49b9deeb03ef92844ce826bf52a4e5baeb7fca9b Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Tue, 18 Aug 2020 21:54:08 -0700 Subject: [PATCH 1/2] msvc: add asan support (disabled by default) --- Source/UnitTests/Core/PageFaultTest.cpp | 13 ++++++++++++- Source/VSProps/Base.props | 3 +++ Source/VSProps/Configuration.Base.props | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Source/UnitTests/Core/PageFaultTest.cpp b/Source/UnitTests/Core/PageFaultTest.cpp index 7b6210d7b9..e77fe49fd2 100644 --- a/Source/UnitTests/Core/PageFaultTest.cpp +++ b/Source/UnitTests/Core/PageFaultTest.cpp @@ -49,6 +49,17 @@ public: m_post_unprotect_time; }; +#ifdef _MSC_VER +#define ASAN_DISABLE __declspec(no_sanitize_address) +#else +#define ASAN_DISABLE +#endif + +static void ASAN_DISABLE perform_invalid_access(void* data) +{ + *(volatile int*)data = 5; +} + TEST(PageFault, PageFault) { EMM::InstallExceptionHandler(); @@ -61,7 +72,7 @@ TEST(PageFault, PageFault) pfjit.m_data = data; auto start = std::chrono::high_resolution_clock::now(); - *(volatile int*)data = 5; + perform_invalid_access(data); auto end = std::chrono::high_resolution_clock::now(); #define AS_NS(diff) \ diff --git a/Source/VSProps/Base.props b/Source/VSProps/Base.props index 8b3a3b6d31..79e55ef13d 100644 --- a/Source/VSProps/Base.props +++ b/Source/VSProps/Base.props @@ -133,6 +133,9 @@ MultiThreadedDebugDLL Disabled + + Default + AnySuitable diff --git a/Source/VSProps/Configuration.Base.props b/Source/VSProps/Configuration.Base.props index dfbeedf4de..928c6dad77 100644 --- a/Source/VSProps/Configuration.Base.props +++ b/Source/VSProps/Configuration.Base.props @@ -4,6 +4,11 @@ v142 Unicode x64 + + true From 2ba4fd960e20fc7e50280993967a6cd540fb77ab Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Tue, 18 Aug 2020 21:55:00 -0700 Subject: [PATCH 2/2] small prettification --- Source/Core/Core/MemTools.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Source/Core/Core/MemTools.cpp b/Source/Core/Core/MemTools.cpp index e0e0d6695b..813a2ef195 100644 --- a/Source/Core/Core/MemTools.cpp +++ b/Source/Core/Core/MemTools.cpp @@ -9,6 +9,7 @@ #include #include +#include "Common/Assert.h" #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" #include "Common/MsgHandler.h" @@ -28,30 +29,32 @@ namespace EMM { #ifdef _WIN32 +static PVOID s_veh_handle; + static LONG NTAPI Handler(PEXCEPTION_POINTERS pPtrs) { switch (pPtrs->ExceptionRecord->ExceptionCode) { case EXCEPTION_ACCESS_VIOLATION: { - int accessType = (int)pPtrs->ExceptionRecord->ExceptionInformation[0]; - if (accessType == 8) // Rule out DEP + ULONG_PTR access_type = pPtrs->ExceptionRecord->ExceptionInformation[0]; + if (access_type == 8) // Rule out DEP { - return (DWORD)EXCEPTION_CONTINUE_SEARCH; + return EXCEPTION_CONTINUE_SEARCH; } // virtual address of the inaccessible data - uintptr_t badAddress = (uintptr_t)pPtrs->ExceptionRecord->ExceptionInformation[1]; - CONTEXT* ctx = pPtrs->ContextRecord; + uintptr_t fault_address = (uintptr_t)pPtrs->ExceptionRecord->ExceptionInformation[1]; + SContext* ctx = pPtrs->ContextRecord; - if (JitInterface::HandleFault(badAddress, ctx)) + if (JitInterface::HandleFault(fault_address, ctx)) { - return (DWORD)EXCEPTION_CONTINUE_EXECUTION; + return EXCEPTION_CONTINUE_EXECUTION; } else { // Let's not prevent debugging. - return (DWORD)EXCEPTION_CONTINUE_SEARCH; + return EXCEPTION_CONTINUE_SEARCH; } } @@ -84,18 +87,17 @@ static LONG NTAPI Handler(PEXCEPTION_POINTERS pPtrs) void InstallExceptionHandler() { - // Make sure this is only called once per process execution - // Instead, could make a Uninstall function, but whatever.. - static bool handlerInstalled = false; - if (handlerInstalled) + if (s_veh_handle) return; - AddVectoredExceptionHandler(TRUE, Handler); - handlerInstalled = true; + s_veh_handle = AddVectoredExceptionHandler(TRUE, Handler); + ASSERT(s_veh_handle); } void UninstallExceptionHandler() { + ULONG status = RemoveVectoredExceptionHandler(s_veh_handle); + ASSERT(status); } #elif defined(__APPLE__) && !defined(USE_SIGACTION_ON_APPLE)