RynthCore/native/SehTrampoline/SehTrampoline.c
Daniel Wallace 8c7201dc25 Engine robustness review fixes: crash-log repeats, teardown reserve, mutator gate, OOM dump safety
Native crash logger
- Per-address slots keep hit counts and a last-write tick; a repeat relogs at powers of
  two, after 30 s, or for any non-AV code. The oldest noise slot is evicted when the
  table is full.
- The logger checks its dedupe first, caches the module table, and opens the log
  handle once at install. Managed-noise bucket (coreclr, clrjit, System.Private.CoreLib).

Main-thread queue
- The action ring (ActionRing) rejects non-teardown work during a world transition and
  reserves its top 32 slots for teardown, so a stop is never lost behind held work.
  IsTeardownKind is shared by the enqueue and drain sides. ActionRing.TryPeekAt lets the
  cast drain's StopQueuedAtOrBefore walk the ring it no longer owns directly; the walk
  now checks every queued entry, since an entry's time is taken before the ring's lock.
- MutatorGate: every AC-mutating helper opens with the same gate,
  AcMainThreadQueue.MustQueue(teardown: X); IsDraining and ShouldDefer added; teardown
  helpers are exempt from the hold. tools\check_offthread_mutators.py accepts MustQueue
  as the gate (it asks MainThreadGuard.IsOnMainThread itself).
- MasteryFeed and VaultFeed pause during a transition. The heading slot drops a stale
  heading; the overlay select is kept.
- RequestId never merges into an entry queued at or before the last transition hold;
  EnqueueRequestId returns false during a transition.
- 0xF751 PlayerTeleport starts the 5 s settle window.
- Cast slot: the queue time and Pending are published with Volatile.Write (a plain 64-bit
  store can tear on x86 and the drain reads both without the lock); the stop-wait flag is
  reset before the publishing store.

Appraisals
- Appraisal data survives a client release for 10 min (bounded at 4096); evicted on
  server delete (0xF747) and at session clear. 0xF747 is detected in SmartBox dispatch,
  independent of capture.

Dumps
- Dump start/end markers in the log; the launcher holds its wedge kill while a dump is
  in progress (DumpInProgressRule). The in-progress flag is cleared only when the writer
  thread exits.
- The OOM dump triggers only on the main thread with AC's exact text, before stacks are
  built. Dumps are pruned only after a successful write, and other live clients' dumps
  are kept. CrashDump reads the file length before Delete.
- ExitProcess/RtlExitUserProcess detours forward crash codes first.
- The message box count is decremented first in a finally; 30 s safety reset when no box
  is visible on the main thread.
- UsageStats counts the loader's CoreCLR fatal-error dumps (Logs\dumps\clr_*.dmp) as
  crashes alongside crash_*.dmp.

Tests: EngineRobustnessTests, CrashLogFilterTests, UsageStatsTests.

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

671 lines
26 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, a repeat
* again at a power-of-two count or after 30 s; .NET Framework and CoreCLR noise not
* charged to the limit; a last-chance entry always). Each entry carries the process id,
* thread 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"
/*
* Exception time does as little as it can: the address dedupe runs first (most
* exceptions are repeats and stop there), module names come from a cache built
* outside exception time (install, then RC_RefreshCrashLoggerModules from the engine's
* heartbeat), and the log file is opened once at install, so writing an entry is
* formatting plus WriteFile. Only an address the cache does not cover (a module loaded
* since the last refresh) falls back to GetModuleHandleExW, for the faulting address
* and eip only.
*/
#define CL_MOD_MAX 384
typedef struct {
unsigned int base, end; /* [base, end) */
unsigned char cls; /* clf_class */
char leaf[48]; /* ASCII leaf name, truncated */
} cl_mod;
typedef struct {
cl_mod mods[CL_MOD_MAX];
int count;
} cl_mod_table;
static wchar_t g_crashLogPath[MAX_PATH];
static HANDLE g_logHandle = INVALID_HANDLE_VALUE;
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 char g_fallbackLeaf[48]; /* under g_logOwner */
static cl_mod_table g_modTables[2]; /* double buffer: refresh builds the inactive one */
static volatile LONG g_modActive = -1; /* index of the published table, -1 = none */
static volatile LONG g_modRefreshing = 0;
typedef BOOL (WINAPI *PFN_EnumMods)(HANDLE, HMODULE*, DWORD, LPDWORD);
typedef BOOL (WINAPI *PFN_ModInfo)(HANDLE, HMODULE, void*, DWORD);
typedef DWORD (WINAPI *PFN_BaseName)(HANDLE, HMODULE, LPWSTR, DWORD);
typedef struct { LPVOID lpBaseOfDll; DWORD SizeOfImage; LPVOID EntryPoint; } cl_modinfo;
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); }
/*
* Rebuild the module cache (NOT at exception time: it takes the loader lock). Builds the
* table no reader is using and publishes it with one interlocked store; refreshes are
* serialized and seconds apart, so a reader never sees its table rebuilt under it.
*/
__declspec(dllexport) void __cdecl
RC_RefreshCrashLoggerModules(void)
{
static PFN_EnumMods pEnum;
static PFN_ModInfo pInfo;
static PFN_BaseName pName;
HMODULE mods[CL_MOD_MAX];
DWORD needed = 0, n, i;
HANDLE self = GetCurrentProcess();
LONG active, next;
cl_mod_table* t;
if (InterlockedCompareExchange(&g_modRefreshing, 1, 0) != 0) return;
if (!pEnum) {
HMODULE k32 = GetModuleHandleW(L"kernel32.dll");
pEnum = (PFN_EnumMods)GetProcAddress(k32, "K32EnumProcessModules");
pInfo = (PFN_ModInfo)GetProcAddress(k32, "K32GetModuleInformation");
pName = (PFN_BaseName)GetProcAddress(k32, "K32GetModuleBaseNameW");
}
if (!pEnum || !pInfo || !pName || !pEnum(self, mods, sizeof(mods), &needed)) {
InterlockedExchange(&g_modRefreshing, 0);
return;
}
n = needed / sizeof(HMODULE);
if (n > CL_MOD_MAX) n = CL_MOD_MAX;
active = g_modActive;
next = active == 0 ? 1 : 0;
t = &g_modTables[next];
t->count = 0;
for (i = 0; i < n; i++) {
cl_modinfo mi;
wchar_t name[64];
DWORD len, k;
cl_mod* m;
if (!pInfo(self, mods[i], &mi, sizeof(mi))) continue;
len = pName(self, mods[i], name, 63);
name[len < 63 ? len : 63] = 0;
m = &t->mods[t->count++];
m->base = (unsigned int)(UINT_PTR)mi.lpBaseOfDll;
m->end = m->base + mi.SizeOfImage;
m->cls = (unsigned char)clf_module_class(name);
for (k = 0; k < sizeof(m->leaf) - 1 && name[k]; k++)
m->leaf[k] = name[k] < 128 ? (char)name[k] : '?';
m->leaf[k] = 0;
}
InterlockedExchange(&g_modActive, next);
InterlockedExchange(&g_modRefreshing, 0);
}
/* Cached module containing addr, or NULL. No API calls. */
static const cl_mod* cl_mod_cached(unsigned int addr)
{
LONG a = g_modActive;
const cl_mod_table* t;
int i;
if (a < 0) return NULL;
t = &g_modTables[a];
for (i = 0; i < t->count; i++)
if (addr >= t->mods[i].base && addr < t->mods[i].end) return &t->mods[i];
return NULL;
}
/*
* Module of addr: the cache, then (allowFallback) the loader. Under the lock. Returns
* the leaf and sets *base and *cls, or NULL (no module: JIT code or garbage).
*/
static const char* cl_module(unsigned int addr, int allowFallback, unsigned int* base, clf_class* cls)
{
const cl_mod* m = cl_mod_cached(addr);
HMODULE hm = NULL;
DWORD n, i, leaf = 0, k;
if (m) { *base = m->base; *cls = (clf_class)m->cls; return m->leaf; }
if (!allowFallback) return 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; }
*base = (unsigned int)(UINT_PTR)hm;
*cls = clf_module_class(g_wpath + leaf);
for (k = 0; k < sizeof(g_fallbackLeaf) - 1 && g_wpath[leaf + k]; k++)
g_fallbackLeaf[k] = g_wpath[leaf + k] < 128 ? (char)g_wpath[leaf + k] : '?';
g_fallbackLeaf[k] = 0;
return g_fallbackLeaf;
}
/* Append "module.dll+0xRVA", or "0xADDR" if the address is in no module. */
static char* cl_sym(char* p, unsigned int addr, int allowFallback)
{
unsigned int base;
clf_class cls;
const char* leaf = cl_module(addr, allowFallback, &base, &cls);
if (leaf) {
p = cl_str(p, leaf);
p = cl_str(p, "+");
p = cl_hex(p, addr - base);
} else {
p = cl_hex(p, addr);
}
return p;
}
/* Class of the nearest cached-module code pointer on the stack (AC if none found). */
static clf_class cl_first_frame_class(unsigned int esp)
{
clf_class result = CLF_CLASS_AC;
__try {
unsigned int* sp = (unsigned int*)(UINT_PTR)esp;
int i;
for (i = 0; i < 512; i++) {
unsigned int v = sp[i];
const cl_mod* m;
if (v <= 0x00401000u || v >= 0x7FFF0000u) continue;
m = cl_mod_cached(v);
if (m) { result = (clf_class)m->cls; break; }
}
}
__except (EXCEPTION_EXECUTE_HANDLER) { result = CLF_CLASS_AC; }
return result;
}
static void cl_write(const char* p, DWORD n)
{
DWORD wr;
if (g_logHandle != INVALID_HANDLE_VALUE) WriteFile(g_logHandle, p, n, &wr, NULL);
}
static void cl_write_entry(PEXCEPTION_POINTERS ep, const char* kind, unsigned int hits)
{
char* p = g_buf;
CONTEXT* c = ep->ContextRecord;
DWORD code = ep->ExceptionRecord->ExceptionCode;
unsigned int exAddr = (unsigned int)(UINT_PTR)ep->ExceptionRecord->ExceptionAddress;
SYSTEMTIME st;
if (g_logHandle == INVALID_HANDLE_VALUE) return;
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, 1);
if (hits > 1) { p = cl_str(p, " repeat="); p = cl_u(p, hits); }
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, " noiseRepeats=");
p = cl_u(p, g_filter.noiseRepeats);
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, 1);
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");
cl_write(g_buf, (DWORD)(p - g_buf));
__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 && cl_mod_cached(v)) {
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, 0);
q = cl_str(q, "\r\n");
cl_write(g_buf, (DWORD)(q - g_buf));
emitted++;
}
}
}
__except (EXCEPTION_EXECUTE_HANDLER) {
const char* m = " <stack sweep faulted>\r\n";
cl_write(m, (DWORD)lstrlenA(m));
}
{
const char* e = "==== END NATIVE CRASH ====\r\n";
cl_write(e, (DWORD)lstrlenA(e));
}
}
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;
unsigned int now = GetTickCount();
unsigned int hits = 1;
clf_slot* slot = clf_find(&g_filter, exAddr);
if (slot) {
/* A repeat: decided without touching any module. */
if (clf_repeat(&g_filter, slot, code, now, &hits) == CLF_LOG_REPEAT)
cl_write_entry(ep, slot->cls != CLF_CLASS_AC ? "first-chance, repeat, noise" : "first-chance, repeat", hits);
} else {
unsigned int base;
clf_class faultCls = CLF_CLASS_AC;
const char* leaf = cl_module(exAddr, 1, &base, &faultCls);
clf_class cls = clf_classify(code, leaf ? faultCls : CLF_CLASS_AC, leaf == NULL,
leaf == NULL && code == CLF_CODE_ACCESS_VIOLATION ? cl_first_frame_class(ep->ContextRecord->Esp) : CLF_CLASS_AC);
clf_decision d = clf_new(&g_filter, code, exAddr, cls, now);
if (d == CLF_LOG_COUNTED)
cl_write_entry(ep, "first-chance", 1);
else if (d == CLF_LOG_NOISE)
cl_write_entry(ep, cls == CLF_CLASS_NETFX
? "first-chance, .NET Framework noise, not counted"
: "first-chance, managed (CoreCLR) noise, not counted", 1);
}
}
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", 1);
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, opened here for the life of the process (append, shared for reading and
* deleting). Also builds the first module cache.
*/
__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_logHandle = CreateFileW(g_crashLogPath, FILE_APPEND_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
}
RC_RefreshCrashLoggerModules();
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;
}