Create a thread for exception handler for separate stack

This commit is contained in:
Prcuvu
2019-10-04 15:28:17 +08:00
parent 31604f48ce
commit 4baa25710d
3 changed files with 14 additions and 39 deletions
-5
View File
@@ -57,11 +57,6 @@ else()
"archutils/Win32/WindowsDialogBox.cpp" "archutils/Win32/WindowsDialogBox.cpp"
"archutils/Win32/WindowsResources.rc") "archutils/Win32/WindowsResources.rc")
if(MSVC AND SM_WIN32_ARCH MATCHES "x64")
enable_language(ASM_MASM)
list(APPEND SMDATA_OS_SRC "archutils/Win32/ExceptionHandler_x64.asm")
endif()
list(APPEND SMDATA_OS_HPP list(APPEND SMDATA_OS_HPP
"archutils/Win32/AppInstance.h" "archutils/Win32/AppInstance.h"
"archutils/Win32/arch_setup.h" "archutils/Win32/arch_setup.h"
+14 -14
View File
@@ -266,7 +266,7 @@ void RunChild()
} }
} }
/* static */ long MainExceptionHandler( EXCEPTION_POINTERS *pExc ) static DWORD WINAPI MainExceptionHandler( LPVOID lpParameter )
{ {
// Flush the log so it isn't cut off at the end. // Flush the log so it isn't cut off at the end.
/* 1. We can't do regular file access in the crash handler. /* 1. We can't do regular file access in the crash handler.
@@ -283,6 +283,7 @@ void RunChild()
* does this exception occur, and we never unmask it. * does this exception occur, and we never unmask it.
* However, once in a while some driver or library turns evil and unmasks an * However, once in a while some driver or library turns evil and unmasks an
* exception flag on us. If this happens, re-mask it and continue execution. */ * exception flag on us. If this happens, re-mask it and continue execution. */
PEXCEPTION_POINTERS pExc = reinterpret_cast<PEXCEPTION_POINTERS>(lpParameter);
switch( pExc->ExceptionRecord->ExceptionCode ) switch( pExc->ExceptionRecord->ExceptionCode )
{ {
case EXCEPTION_FLT_INVALID_OPERATION: case EXCEPTION_FLT_INVALID_OPERATION:
@@ -360,26 +361,25 @@ void RunChild()
return EXCEPTION_EXECUTE_HANDLER; return EXCEPTION_EXECUTE_HANDLER;
} }
#if !_WIN64
long __stdcall CrashHandler::ExceptionHandler( EXCEPTION_POINTERS *pExc ) long __stdcall CrashHandler::ExceptionHandler( EXCEPTION_POINTERS *pExc )
{ {
/* If the stack overflowed, we have a very limited amount of stack space. /* If the stack overflowed, we have a very limited amount of stack space.
* Allocate a new stack, and run the exception handler in it, to increase * Allocate a new stack, and run the exception handler in it, to increase
* the chances of success. */ * the chances of success. */
static BYTE new_stack[0x8000]; HANDLE hExceptionHandler = CreateThread(nullptr, 1024 * 32, MainExceptionHandler, reinterpret_cast<LPVOID>(pExc), 0, nullptr);
LPBYTE pStack = new_stack + 0x8000; if (hExceptionHandler == NULL)
#if defined(_MSC_VER) {
_asm mov esp, pStack; TerminateProcess(GetCurrentProcess(), 0);
#elif defined(__GNUC__) return EXCEPTION_EXECUTE_HANDLER;
asm volatile ("movl %%esp, %0\n\t" }
: WaitForSingleObject(hExceptionHandler, INFINITE);
: "r" (pStack)
);
#endif
return MainExceptionHandler( pExc ); DWORD ret;
GetExitCodeThread(hExceptionHandler, &ret);
CloseHandle(hExceptionHandler);
return static_cast<long>(ret);
} }
#endif
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@@ -1,20 +0,0 @@
extrn ?MainExceptionHandler@@YAJPEAU_EXCEPTION_POINTERS@@@Z : PROC
.data
pStack db 8000h dup(?) ; Allocate a new stack
.code
; long __stdcall CrashHandler::ExceptionHandler( EXCEPTION_POINTERS *pExc );
?ExceptionHandler@CrashHandler@@YAJPEAU_EXCEPTION_POINTERS@@@Z PROC
push rsp
lea rsp, offset pStack + 7FE0h ; Reserve register parameter stack area for RCX, RDX, R8 and R9
call ?MainExceptionHandler@@YAJPEAU_EXCEPTION_POINTERS@@@Z
pop rsp
ret
?ExceptionHandler@CrashHandler@@YAJPEAU_EXCEPTION_POINTERS@@@Z ENDP
END