RynthCore/native/SehTrampoline/SehTrampoline.c
Daniel Wallace 55f7497065 Engine robustness: appraisal merge, AutoId re-eligibility, freeze pause, OOM dump, crash logger, transition gate
Appraisals
- RequestId slot merges a pending request for the same object (PendingIdQueue), as the
  QueryHealth slot does.
- AutoId: per-object last-sent time kept across client releases; no re-appraisal within
  10 min while the data is cached, unless the server deleted the object (0xF747). Map
  bounded at 8192 with a prune off the create path. The sent mark is set at the real
  send in DrainRequestIds, not at queue time.
- The selected-target priority appraisal is skipped when its data is cached and fresh.

Main thread frozen
- Plugin ticks pause while AC's main thread is frozen (a main-thread message box, or no
  beat for the hang watchdog's threshold); events keep flowing. Exposed through
  GetEngineStatusJson (mainThreadFrozen, mainThreadStallMs, pluginTicksPausedMs) and the
  heartbeat (pump=paused).

Crashes
- AC's OUT OF MEMORY box: one full-memory dump to <install>\Dumps before the box shows,
  newest 3 kept.
- Native crash logger: pid, tid and date per entry; once per address; .NET Framework
  AVs never charged to the limit; last-chance handler; stack overflow and heap
  corruption codes. Build-SehTrampoline gains -NoDeploy.
- ProcessExitHooks: dead fail-fast detours removed; crash exit codes pass straight
  through TerminateProcess/NtTerminateProcess.
- CoreCLR's own crash dump is left to the loader's existing ConfigureCrashDumps
  (Loader.c, Logs\dumps\clr_*.dmp) and Publish-EngineCoreClr's createdump/DAC
  handling; this change adds no second setup.

World transitions
- AcMainThreadQueue holds queued game-mutating work while the player is dead, in portal
  space, or within 5 s after either or after a non-adjacent landblock jump. When the
  window ends, stale casts, uses, selects and appraisals are dropped and stops and
  cancels run in order. Walking across an adjacent landblock border never gates.
  State is re-sampled after AC's tick, before the post-tick drain.
- Inventory panel actions are held during a transition too.

Diagnostics
- Heartbeat valargest= (largest free VA block); free and largest logged before and after
  each plugin load and unload.
- CastSpell comments corrected to the actual main-thread paths.

Tests
- tools\EngineRobustnessTests and native\SehTrampoline\tests\CrashLogFilterTests.c.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-12 17:17:59 -07:00

549 lines
21 KiB
C

/*
* RynthCore.SehTrampoline.dll — x86 native SEH wrapper for dangerous AC API calls.
*
* NativeAOT managed try/catch cannot intercept access violations (hardware
* exceptions / corrupted-state exceptions). When the bot calls AC APIs on
* objects that are being torn down by the client's object lifecycle, the AV
* propagates up and kills acclient.exe.
*
* This DLL provides __try/__except wrappers that live entirely in native code.
* Each export calls a supplied function pointer inside a structured exception
* handler scoped only to that one call — it does not install any process-wide
* handler (VEH/SUEF), so it cannot interfere with AC's or Avalonia's own
* internal exception handling.
*
* Conventions:
* - All exports are __cdecl so the caller controls stack cleanup.
* - Return value: 1 = call completed normally; 0 = AV was caught.
* - out_* parameters are always written (safe default on AV).
* - __thiscall helpers are separate non-__try functions; MSVC does not allow
* inline __asm and __try in the same function on x86.
*
* Build: cl /nologo /O2 /MD /LD SehTrampoline.c /link /MACHINE:X86
* /OUT:RynthCore.SehTrampoline.dll /DEF:SehTrampoline.def
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason, LPVOID lpReserved)
{
(void)hModule; (void)ul_reason; (void)lpReserved;
return TRUE;
}
/* ── Cdecl helpers ─────────────────────────────────────────────────────── */
/* void* __cdecl fn(unsigned int id) — e.g. GetWeenieObject */
typedef void* (__cdecl *Fn_CdeclPtrUint)(unsigned int);
__declspec(dllexport) int __cdecl
SEH_CdeclPtrUint(Fn_CdeclPtrUint fn, unsigned int arg, void** out_result)
{
*out_result = NULL;
__try {
*out_result = fn(arg);
}
__except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
*out_result = NULL;
return 0;
}
return 1;
}
/* void* __cdecl fn() — e.g. GetCombatSystem */
typedef void* (__cdecl *Fn_CdeclPtrVoid)(void);
__declspec(dllexport) int __cdecl
SEH_CdeclPtrVoid(Fn_CdeclPtrVoid fn, void** out_result)
{
*out_result = NULL;
__try {
*out_result = fn();
}
__except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
*out_result = NULL;
return 0;
}
return 1;
}
/* void __cdecl fn(unsigned int, unsigned char) — e.g. CastSpell(spellId, targetIsSelected) */
typedef void (__cdecl *Fn_CdeclVoidUintByte)(unsigned int, unsigned char);
__declspec(dllexport) int __cdecl
SEH_CdeclVoidUintByte(Fn_CdeclVoidUintByte fn, unsigned int arg1, unsigned char arg2)
{
__try {
fn(arg1, arg2);
}
__except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
return 0;
}
return 1;
}
/* void __thiscall fn(this, unsigned int spellId, unsigned int targetId)
* e.g. ClientMagicSystem::FreeHandsAndCastSpell(this, spellId, targetId) @ 0x00567C90.
* Modeled as __fastcall (this in ECX, a dummy EDX slot, spellId+targetId on the stack;
* callee cleans the 8 stack bytes) — the exact, proven binding RynthSuite2 uses. The
* target is an EXPLICIT argument, so this never reads AC's global selection and never
* touches UI state (unlike CastSpell(spellId, targetIsSelected=1) @ 0x00568DE0, which
* reads [magicSysSingleton+0xF4]). __fastcall is a real declarable convention, so this
* needs no inline __asm and can host its own __try. */
typedef void (__fastcall *Fn_FreeHandsCast)(void* ecx, void* edx, unsigned int spellId, unsigned int targetId);
__declspec(dllexport) int __cdecl
SEH_FreeHandsCast(void* fn, void* this_ptr, unsigned int spellId, unsigned int targetId)
{
__try {
((Fn_FreeHandsCast)fn)(this_ptr, 0, spellId, targetId);
}
__except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
return 0;
}
return 1;
}
/* ── Thiscall helpers — inline __asm lives here, NOT in __try functions ── */
/*
* MSVC x86: __thiscall puts 'this' in ECX. C has no __thiscall call-site
* syntax, so we use inline __asm. These helpers must NOT contain __try;
* the outer SEH wrappers call them so the SEH frame is established by the
* caller's stack frame, not by the helper.
*/
/* unsigned char __thiscall fn(unsigned int objectId) */
static unsigned char __cdecl
_helper_thiscall_byte_uint(void* fn_ptr, void* this_ptr, unsigned int arg)
{
unsigned char result = 0;
__asm {
mov ecx, this_ptr
push arg
call fn_ptr
mov result, al
}
return result;
}
/* unsigned int __thiscall fn() — no stack args, this in ECX */
static unsigned int __cdecl
_helper_thiscall_uint_noarg(void* fn_ptr, void* this_ptr)
{
unsigned int result = 0;
__asm {
mov ecx, this_ptr
call fn_ptr
mov result, eax
}
return result;
}
/* ── Thiscall SEH wrappers ─────────────────────────────────────────────── */
/*
* unsigned char __thiscall fn(unsigned int objectId)
* e.g. ClientCombatSystem::ObjectIsAttackable(objectId)
*/
__declspec(dllexport) int __cdecl
SEH_ThiscallByteUint(void* fn, void* this_ptr, unsigned int arg,
unsigned char* out_result)
{
*out_result = 0;
__try {
*out_result = _helper_thiscall_byte_uint(fn, this_ptr, arg);
}
__except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
*out_result = 0;
return 0;
}
return 1;
}
/*
* unsigned int __thiscall fn()
* e.g. ACCWeenieObject::InqType()
*/
__declspec(dllexport) int __cdecl
SEH_ThiscallUintNoArg(void* fn, void* this_ptr, unsigned int* out_result)
{
*out_result = 0;
__try {
*out_result = _helper_thiscall_uint_noarg(fn, this_ptr);
}
__except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
*out_result = 0;
return 0;
}
return 1;
}
/* int __thiscall fn(unsigned int stype, void* outStruct)
* e.g. CACQualities/CWeenieObject::InqAttribute2nd(stype, &SecondaryAttribute).
* this in ECX; stype + outStruct on the stack (callee cleans, thiscall). Reads a
* 2nd-level attribute (health/stam/mana) into the caller's struct. Used to pull a
* MONSTER's real MaxHealth out of its (possibly partially-populated) qualities
* table without crashing if a sub-table is null — the AV that forced the
* AllowNonPlayerQualities gate. */
static int __cdecl
_helper_thiscall_int_uint_ptr(void* fn_ptr, void* this_ptr,
unsigned int arg1, void* arg2)
{
int result = 0;
__asm {
mov ecx, this_ptr
push arg2 /* outStruct (rightmost) */
push arg1 /* stype */
call fn_ptr /* thiscall callee cleans the 8 stack bytes */
mov result, eax
}
return result;
}
__declspec(dllexport) int __cdecl
SEH_ThiscallIntUintPtr(void* fn, void* this_ptr, unsigned int arg1, void* arg2,
int* out_result)
{
*out_result = 0;
__try {
*out_result = _helper_thiscall_int_uint_ptr(fn, this_ptr, arg1, arg2);
}
__except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
*out_result = 0;
return 0;
}
return 1;
}
/* -- Process-wide native crash logger (VEH + last-chance filter) -----------
* Logs the faulting 32-bit context + a module-resolved stack sweep to a
* dedicated file BEFORE the process dies, for the exceptions that bypass the
* engine's managed handlers: 0xC0000005 (AV / corrupted state; CoreCLR will not
* let a managed catch handle one raised in AC code under a managed detour, it
* fail-fasts instead), 0xC0000602 (fail-fast), 0xC0000409 (stack buffer
* overrun), 0xC00000FD (stack overflow) and 0xC0000374 (heap corruption).
* The vectored handler is registered FIRST, so it sees an AV before CoreCLR
* turns it into anything: the entry names the real faulting address and module.
*
* Which entries are written is CrashLogFilter.h's rule (once per address; the
* .NET Framework's handled null references not charged to the cap; a last-chance
* entry always). Each entry carries the process id, date and time.
*
* This is PURE NATIVE: no managed callback / reverse-P/Invoke transition, so
* unlike the removed managed CrashLogger VEH it cannot itself re-trigger a
* fail-fast on an AC-owned thread. It only OBSERVES: both handlers return
* EXCEPTION_CONTINUE_SEARCH (the last-chance one first calls the filter it
* replaced) so AC's own SEH / the runtime still proceed exactly as before. Uses
* only kernel32 APIs + hand-rolled formatting (no CRT stdio / user32) so the
* build link line stays unchanged. All buffers are static and used under one
* spin lock (with its owner's thread id, so a fault inside the logger on the same
* thread is passed on, never waited on): a stack overflow leaves the faulting
* thread about one page of stack. x86 (32-bit) context fields only.
*/
#include "CrashLogFilter.h"
static wchar_t g_crashLogPath[MAX_PATH];
static PVOID g_vehHandle = NULL;
static LPTOP_LEVEL_EXCEPTION_FILTER g_prevFilter = NULL;
static volatile LONG g_logOwner = 0; /* thread id holding the logger, 0 = free */
static clf_state g_filter; /* under g_logOwner */
static char g_buf[4096]; /* under g_logOwner */
static wchar_t g_wpath[MAX_PATH]; /* under g_logOwner */
static int cl_lock(void)
{
LONG me = (LONG)GetCurrentThreadId();
int i;
if (g_logOwner == me) return 0; /* faulted inside the logger: pass it on */
for (i = 0; i < 20000; i++) {
if (InterlockedCompareExchange(&g_logOwner, me, 0) == 0) return 1;
YieldProcessor();
if ((i & 0xFF) == 0xFF) SwitchToThread();
}
return 0; /* owner stuck (suspended?): skip this entry */
}
static void cl_unlock(void) { InterlockedExchange(&g_logOwner, 0); }
static char* cl_str(char* p, const char* s) { while (*s) { *p++ = *s++; } return p; }
static char* cl_hex(char* p, unsigned int v)
{
const char* H = "0123456789ABCDEF";
int i;
*p++ = '0'; *p++ = 'x';
for (i = 28; i >= 0; i -= 4) { *p++ = H[(v >> i) & 0xF]; }
return p;
}
static char* cl_u(char* p, unsigned int v)
{
char tmp[12]; int n = 0;
if (v == 0) { *p++ = '0'; return p; }
while (v) { tmp[n++] = (char)('0' + (v % 10)); v /= 10; }
while (n) { *p++ = tmp[--n]; }
return p;
}
static char* cl_u2(char* p, unsigned int v) { if (v < 10) { *p++ = '0'; } return cl_u(p, v); }
/* Module containing addr, or NULL. Under the lock: fills g_wpath and returns its leaf. */
static const wchar_t* cl_module(unsigned int addr, HMODULE* out)
{
HMODULE hm = NULL;
DWORD n, i, leaf = 0;
*out = NULL;
/* GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS(0x4) | UNCHANGED_REFCOUNT(0x2) */
if (!GetModuleHandleExW(0x4 | 0x2, (LPCWSTR)(UINT_PTR)addr, &hm) || !hm) return NULL;
n = GetModuleFileNameW(hm, g_wpath, MAX_PATH);
if (n >= MAX_PATH) n = MAX_PATH - 1;
g_wpath[n] = 0;
for (i = 0; i < n; i++) { if (g_wpath[i] == L'\\' || g_wpath[i] == L'/') leaf = i + 1; }
*out = hm;
return g_wpath + leaf;
}
/* Append "module.dll+0xRVA", or "0xADDR" if the address is in no module. */
static char* cl_sym(char* p, unsigned int addr)
{
HMODULE hm;
const wchar_t* leaf = cl_module(addr, &hm);
if (leaf) {
while (*leaf) { *p++ = (char)*leaf++; }
p = cl_str(p, "+");
p = cl_hex(p, addr - (unsigned int)(UINT_PTR)hm);
} else {
p = cl_hex(p, addr);
}
return p;
}
/* Nearest module-resolved code pointer on the stack is in the .NET Framework? */
static int cl_first_frame_netfx(unsigned int esp)
{
int result = 0;
__try {
unsigned int* sp = (unsigned int*)(UINT_PTR)esp;
int i;
for (i = 0; i < 512; i++) {
unsigned int v = sp[i];
if (v > 0x00401000u && v < 0x7FFF0000u) {
HMODULE hm;
const wchar_t* leaf = cl_module(v, &hm);
if (leaf) { result = clf_is_netfx_module(leaf); break; }
}
}
}
__except (EXCEPTION_EXECUTE_HANDLER) { result = 0; }
return result;
}
static void cl_write_entry(PEXCEPTION_POINTERS ep, const char* kind)
{
HANDLE h = CreateFileW(g_crashLogPath, FILE_APPEND_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) { return; }
SetFilePointer(h, 0, NULL, FILE_END);
{
char* p = g_buf;
CONTEXT* c = ep->ContextRecord;
DWORD code = ep->ExceptionRecord->ExceptionCode;
unsigned int exAddr = (unsigned int)(UINT_PTR)ep->ExceptionRecord->ExceptionAddress;
DWORD wr;
SYSTEMTIME st;
p = cl_str(p, "\r\n==== NATIVE CRASH LOGGER (");
p = cl_str(p, kind);
p = cl_str(p, ") ====\r\n code=");
p = cl_hex(p, code);
p = cl_str(p, " exAddr=");
p = cl_sym(p, exAddr);
p = cl_str(p, " pid=");
p = cl_u(p, GetCurrentProcessId());
p = cl_str(p, " tid=");
p = cl_u(p, GetCurrentThreadId());
GetLocalTime(&st);
p = cl_str(p, " date=");
p = cl_u(p, st.wYear); p = cl_str(p, "-");
p = cl_u2(p, st.wMonth); p = cl_str(p, "-");
p = cl_u2(p, st.wDay);
p = cl_str(p, " time=");
p = cl_u2(p, st.wHour); p = cl_str(p, ":");
p = cl_u2(p, st.wMinute); p = cl_str(p, ":");
p = cl_u2(p, st.wSecond);
if ((code == 0xC0000005 || code == 0xC0000409) && ep->ExceptionRecord->NumberParameters >= 2) {
unsigned int acc = (unsigned int)ep->ExceptionRecord->ExceptionInformation[0];
unsigned int dataAddr = (unsigned int)ep->ExceptionRecord->ExceptionInformation[1];
p = cl_str(p, " access=");
p = cl_str(p, acc == 1 ? "WRITE" : (acc == 8 ? "EXEC" : "READ"));
p = cl_str(p, " dataAddr=");
p = cl_hex(p, dataAddr); /* the [null+X] value, e.g. 0xC for the parser race */
}
p = cl_str(p, "\r\n logged=");
p = cl_u(p, g_filter.counted);
p = cl_str(p, "/");
p = cl_u(p, CLF_MAX_COUNTED);
p = cl_str(p, " netfxRepeats=");
p = cl_u(p, g_filter.netfxRepeats);
p = cl_str(p, " otherRepeats=");
p = cl_u(p, g_filter.otherRepeats);
p = cl_str(p, " overCap=");
p = cl_u(p, g_filter.overCap);
p = cl_str(p, "\r\n eip="); p = cl_sym(p, c->Eip);
p = cl_str(p, " esp="); p = cl_hex(p, c->Esp);
p = cl_str(p, " ebp="); p = cl_hex(p, c->Ebp);
p = cl_str(p, "\r\n eax="); p = cl_hex(p, c->Eax);
p = cl_str(p, " ebx="); p = cl_hex(p, c->Ebx);
p = cl_str(p, " ecx="); p = cl_hex(p, c->Ecx);
p = cl_str(p, " edx="); p = cl_hex(p, c->Edx);
p = cl_str(p, " esi="); p = cl_hex(p, c->Esi);
p = cl_str(p, " edi="); p = cl_hex(p, c->Edi);
p = cl_str(p, "\r\n stack code pointers (esp -> up):\r\n");
WriteFile(h, g_buf, (DWORD)(p - g_buf), &wr, NULL);
__try {
unsigned int* sp = (unsigned int*)(UINT_PTR)c->Esp;
int i, emitted = 0;
for (i = 0; i < 4096 && emitted < 96; i++) {
unsigned int v = sp[i];
if (v > 0x00401000u && v < 0x7FFF0000u) {
HMODULE hm = NULL;
if (GetModuleHandleExW(0x4 | 0x2, (LPCWSTR)(UINT_PTR)v, &hm) && hm) {
char* q = g_buf;
q = cl_str(q, " +");
q = cl_hex(q, (unsigned int)(i * 4));
q = cl_str(q, " ");
q = cl_sym(q, v);
q = cl_str(q, "\r\n");
WriteFile(h, g_buf, (DWORD)(q - g_buf), &wr, NULL);
emitted++;
}
}
}
}
__except (EXCEPTION_EXECUTE_HANDLER) {
const char* m = " <stack sweep faulted>\r\n";
WriteFile(h, m, (DWORD)lstrlenA(m), &wr, NULL);
}
{
const char* e = "==== END NATIVE CRASH ====\r\n";
WriteFile(h, e, (DWORD)lstrlenA(e), &wr, NULL);
}
}
CloseHandle(h);
}
static LONG CALLBACK CrashVeh(PEXCEPTION_POINTERS ep)
{
DWORD code = ep->ExceptionRecord->ExceptionCode;
if (!clf_is_logged_code(code)) {
return EXCEPTION_CONTINUE_SEARCH; /* cheap exit, no lock, for C++/CLR throws */
}
if (!cl_lock()) { return EXCEPTION_CONTINUE_SEARCH; }
{
unsigned int exAddr = (unsigned int)(UINT_PTR)ep->ExceptionRecord->ExceptionAddress;
HMODULE hm;
const wchar_t* leaf = cl_module(exAddr, &hm);
int netfx = clf_is_netfx_fault(code,
leaf != NULL && clf_is_netfx_module(leaf),
leaf == NULL,
leaf == NULL && code == 0xC0000005 ? cl_first_frame_netfx(ep->ContextRecord->Esp) : 0);
clf_decision d = clf_first_chance(&g_filter, code, exAddr, netfx);
if (d == CLF_LOG_COUNTED)
cl_write_entry(ep, "first-chance");
else if (d == CLF_LOG_NETFX)
cl_write_entry(ep, "first-chance, .NET Framework, once per address, not counted");
}
cl_unlock();
return EXCEPTION_CONTINUE_SEARCH;
}
/* Last chance: nothing handled it. Logged, then the filter this one replaced decides. */
static LONG WINAPI CrashLastChance(PEXCEPTION_POINTERS ep)
{
if (cl_lock()) {
if (clf_last_chance(&g_filter) == CLF_LOG_LAST_CHANCE)
cl_write_entry(ep, "LAST-CHANCE, unhandled");
cl_unlock();
}
if (g_prevFilter && g_prevFilter != CrashLastChance) {
return g_prevFilter(ep);
}
return EXCEPTION_CONTINUE_SEARCH;
}
/* Install the VEH and the last-chance filter once. logPath = wide path to the dedicated crash log. */
__declspec(dllexport) void __cdecl
RC_InstallCrashLogger(const wchar_t* logPath)
{
if (g_vehHandle) { return; }
if (logPath) {
int i = 0;
for (; logPath[i] && i < MAX_PATH - 1; i++) { g_crashLogPath[i] = logPath[i]; }
g_crashLogPath[i] = 0;
}
g_vehHandle = AddVectoredExceptionHandler(1, CrashVeh);
g_prevFilter = SetUnhandledExceptionFilter(CrashLastChance);
}
/* ── Tagged-text parser guard (native MinHook detour body) ────────────────
* Null-guards AC's tag-parser consumer FUN_0067D3C0 (live VA 0x0067D3C0)
* against a null parser-context singleton. DAT_008F881C (AC's "current tag
* parser context") is unsynchronized; when our off-thread pump triggers a
* text-generation path while AC's main thread is mid-parse, it races to null
* and the consumer does `MOV EDI,[EAX+0xC]` with EAX=0 at 0x0067D3DA -> AV
* READ [null+0xC]. That is the recurring text-parser-race crash (captured
* 2026-06-03 native-crash.log: code=0xC0000005 exAddr=acclient.exe+0x27D3DA).
*
* MUST be native: the consumer is called by AC's main thread for EVERY parsed
* tag. A MANAGED MinHook detour here (reverse-P/Invoke per tag) reintroduced a
* NativeAOT fail-fast and crashed in ~7s (2026-05-25, pid 20852). This detour
* has NO managed transition. The engine installs the hook via MinHook and
* hands us the trampoline (original) through RC_SetTagParserOriginal BEFORE it
* enables the hook. If the parser context is null we drop ONE parse run
* (return 0) instead of dereferencing it; the caller (FUN_0067E150) discards
* the return value, so dropping it is safe.
*
* The hooked function is __cdecl: void* FUN_0067D3C0(void* parserContext).
*/
typedef void* (__cdecl *Fn_TagParserConsume)(void*);
static Fn_TagParserConsume g_tagParserOriginal = NULL;
static volatile LONG g_tagParserSkips = 0;
static void* __cdecl RC_TagParserGuard(void* parserContext)
{
if (parserContext == NULL) {
InterlockedIncrement(&g_tagParserSkips);
return NULL; /* drop this parse run — no null deref */
}
if (g_tagParserOriginal == NULL) {
return NULL; /* not wired yet (pre-SetOriginal) — fail safe */
}
return g_tagParserOriginal(parserContext);
}
/* Returns the address of the native detour for the engine to pass to MinHook. */
__declspec(dllexport) void* __cdecl RC_GetTagParserGuardAddress(void)
{
return (void*)(UINT_PTR)&RC_TagParserGuard;
}
/* Engine calls this with the MinHook trampoline (original) BEFORE enabling. */
__declspec(dllexport) void __cdecl RC_SetTagParserOriginal(void* original)
{
g_tagParserOriginal = (Fn_TagParserConsume)original;
}
/* Diagnostic: how many null-context parse runs we've dropped. */
__declspec(dllexport) unsigned int __cdecl RC_TagParserSkipCount(void)
{
return (unsigned int)g_tagParserSkips;
}