[*] Harden Stack.Win32.cpp

Homer running[3]
This commit is contained in:
Reece Wilson 2024-05-26 16:57:14 +01:00
parent 9b3aa12db6
commit 5ab97be4e3

View File

@ -9,11 +9,15 @@
#include "Stack.hpp"
#include "Stack.Win32.hpp"
#include <Dbghelp.h>
#include <Aux_ulib.h>
#include <Source/Process/AuProcessMap.hpp>
#include <Source/Process/AuProcessMap.Win32.hpp>
namespace Aurora::Debug
{
// Do not use a futex here!
static AuMutex gMutex;
static AuUInt GetImageBase(HMODULE mod)
{
if (!mod)
@ -37,6 +41,20 @@ namespace Aurora::Debug
void ParseStack(CONTEXT *ctx, StackTrace &backTrace)
{
BOOL bLocked {};
#if defined(AURORA_PLATFORM_WIN32)
if (AuxUlibIsDLLSynchronizationHeld(&bLocked))
{
if (bLocked)
{
return;
}
}
#endif
AU_LOCK_GUARD(gMutex);
char buffer[sizeof(SYMBOL_INFO) + (MAX_SYM_NAME + 1) * sizeof(char)] = { 0 };
AuString backTraceBuffer;
HMODULE hModule;
@ -76,7 +94,7 @@ namespace Aurora::Debug
#else
IMAGE_FILE_MACHINE_I386,
#endif
INVALID_HANDLE_VALUE,
GetCurrentProcess(),
INVALID_HANDLE_VALUE,
&stack,
&cpy,
@ -130,12 +148,34 @@ namespace Aurora::Debug
{
StackTrace ret;
CONTEXT ctx {};
ctx.ContextFlags = CONTEXT_ALL;
#if defined(AURORA_ARCH_X86)
ctx.ContextFlags = CONTEXT_CONTROL;
__asm
{
Label:
mov [ctx.Ebp], ebp;
mov [ctx.Esp], esp;
mov eax, [Label];
mov [ctx.Eip], eax;
}
#elif defined(AURORA_ARCH_X64) && defined(AURORA_PLATFORM_WIN32)
RtlCaptureContext(&ctx);
#else
ctx.ContextFlags = CONTEXT_CONTROL;
if (!GetThreadContext(GetCurrentThread(), &ctx))
{
return {};
}
#endif
ParseStack(&ctx, ret);
return ret;
}