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>
This commit is contained in:
parent
b0f9d74b21
commit
55f7497065
30 changed files with 2202 additions and 419 deletions
|
|
@ -1,6 +1,9 @@
|
|||
# Build-SehTrampoline.ps1
|
||||
# Compiles RynthCore.SehTrampoline.dll (x86 MSVC) and copies to the runtime.
|
||||
# Run from anywhere; finds VS automatically via vswhere.
|
||||
# -NoDeploy: build into native\SehTrampoline\bin only (no copy to the live Runtime).
|
||||
|
||||
param([switch]$NoDeploy)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
|
|
@ -33,6 +36,7 @@ if ($LASTEXITCODE -ne 0) { Write-Error "Compilation failed."; exit $LASTEXITCODE
|
|||
$size = (Get-Item "$out\RynthCore.SehTrampoline.dll").Length
|
||||
Write-Host "Built: RynthCore.SehTrampoline.dll ($size bytes)"
|
||||
|
||||
if ($NoDeploy) { Write-Host "-NoDeploy: not copied to the runtime."; exit 0 }
|
||||
$runtimeDst = "C:\Games\RynthCore\Runtime\RynthCore.SehTrampoline.dll"
|
||||
Copy-Item "$out\RynthCore.SehTrampoline.dll" $runtimeDst -Force
|
||||
Write-Host "Deployed to $runtimeDst"
|
||||
|
|
|
|||
143
native/SehTrampoline/CrashLogFilter.h
Normal file
143
native/SehTrampoline/CrashLogFilter.h
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* CrashLogFilter.h - which exceptions the native crash logger (SehTrampoline.c) writes.
|
||||
*
|
||||
* Pure decision logic: no Win32 calls, no globals. The logger keeps one clf_state and
|
||||
* calls these under its own lock; native\SehTrampoline\tests\CrashLogFilterTests.c
|
||||
* runs them directly.
|
||||
*
|
||||
* Why: the logger used to write every first-chance AV until a cap of 24 per process.
|
||||
* Decal's .NET Framework plugins raise a handled null-reference AV in JIT code (clr.dll
|
||||
* and mscorlib frames) about once a minute, the SAME address each time, so the cap was
|
||||
* spent within minutes: one client's 24 entries (all within about five minutes) were all
|
||||
* exAddr 0x433620C8, and its fatal AV at 20:25 under GameTickHooks.UseTimeDetour left
|
||||
* nothing in native-crash.log.
|
||||
*
|
||||
* Now:
|
||||
* - Logged codes: access violation, fail-fast, stack buffer overrun, stack overflow,
|
||||
* heap corruption.
|
||||
* - A first-chance AV raised by the .NET Framework (faulting module clr.dll, mscorwks.dll
|
||||
* or mscorlib, OR in no module with the nearest code pointer on the stack in one of
|
||||
* those: JIT code) is logged once per address and never charged to the cap.
|
||||
* - Every other first-chance exception is logged once per address too (an AC fault that
|
||||
* SEH unwind re-raises costs one entry, not five). The limit is per address, not per
|
||||
* entry: CLF_MAX_COUNTED distinct addresses, a flood guard only (a repeating fault
|
||||
* never spends it).
|
||||
* - Stack overflow and heap corruption are never treated as .NET Framework noise.
|
||||
* - The last-chance (unhandled) entry is never charged to the limit and has its own small
|
||||
* one (it happens once in a dying process; the limit only stops a loop).
|
||||
* - When the address table is full: a new .NET Framework address is not written (counted
|
||||
* as a repeat); a new other address is still written while under CLF_MAX_COUNTED,
|
||||
* just no longer de-duplicated, so the logger never goes silent on AC's own faults.
|
||||
*/
|
||||
#ifndef RYNTHCORE_CRASH_LOG_FILTER_H
|
||||
#define RYNTHCORE_CRASH_LOG_FILTER_H
|
||||
|
||||
#define CLF_MAX_COUNTED 96
|
||||
#define CLF_MAX_LAST_CHANCE 8
|
||||
#define CLF_ADDR_SLOTS 128
|
||||
|
||||
#define CLF_CODE_ACCESS_VIOLATION 0xC0000005u
|
||||
#define CLF_CODE_FAIL_FAST 0xC0000602u
|
||||
#define CLF_CODE_STACK_BUFFER 0xC0000409u
|
||||
#define CLF_CODE_STACK_OVERFLOW 0xC00000FDu
|
||||
#define CLF_CODE_HEAP_CORRUPTION 0xC0000374u
|
||||
|
||||
typedef enum {
|
||||
CLF_SKIP_CODE = 0, /* not a code the logger writes */
|
||||
CLF_LOG_COUNTED, /* write it; charged to the cap */
|
||||
CLF_LOG_NETFX, /* write it; .NET Framework first sighting, not charged */
|
||||
CLF_LOG_LAST_CHANCE, /* write it; unhandled */
|
||||
CLF_SKIP_REPEAT, /* address already written */
|
||||
CLF_SKIP_CAP, /* cap reached */
|
||||
} clf_decision;
|
||||
|
||||
typedef struct {
|
||||
unsigned int addrs[CLF_ADDR_SLOTS]; /* first-chance addresses already written */
|
||||
unsigned int addrCount;
|
||||
unsigned int counted; /* entries charged to the cap */
|
||||
unsigned int lastChance;
|
||||
unsigned int netfxRepeats; /* .NET Framework AVs not written (repeats) */
|
||||
unsigned int otherRepeats; /* other repeats not written */
|
||||
unsigned int overCap; /* not written: cap reached */
|
||||
} clf_state;
|
||||
|
||||
static int clf_is_logged_code(unsigned int code)
|
||||
{
|
||||
return code == CLF_CODE_ACCESS_VIOLATION || code == CLF_CODE_FAIL_FAST
|
||||
|| code == CLF_CODE_STACK_BUFFER || code == CLF_CODE_STACK_OVERFLOW
|
||||
|| code == CLF_CODE_HEAP_CORRUPTION;
|
||||
}
|
||||
|
||||
static int clf_wide_ieq(const wchar_t* a, const char* b)
|
||||
{
|
||||
if (!a || !b) return 0;
|
||||
for (;;) {
|
||||
wchar_t ca = *a++;
|
||||
char cb = *b++;
|
||||
if (ca >= L'A' && ca <= L'Z') ca = (wchar_t)(ca - L'A' + L'a');
|
||||
if (cb >= 'A' && cb <= 'Z') cb = (char)(cb - 'A' + 'a');
|
||||
if (ca != (wchar_t)(unsigned char)cb) return 0;
|
||||
if (ca == 0) return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Leaf file name of a module: the .NET Framework runtime or its core library. */
|
||||
static int clf_is_netfx_module(const wchar_t* leaf)
|
||||
{
|
||||
return clf_wide_ieq(leaf, "clr.dll") || clf_wide_ieq(leaf, "mscorwks.dll")
|
||||
|| clf_wide_ieq(leaf, "mscorlib.dll") || clf_wide_ieq(leaf, "mscorlib.ni.dll");
|
||||
}
|
||||
|
||||
/*
|
||||
* Is this first-chance exception .NET Framework noise? faultModuleNetfx: the faulting
|
||||
* address is in a .NET Framework module. faultInNoModule: it is in no module (JIT code).
|
||||
* firstFrameNetfx: the nearest module-resolved code pointer on the stack is in one.
|
||||
*/
|
||||
static int clf_is_netfx_fault(unsigned int code, int faultModuleNetfx, int faultInNoModule, int firstFrameNetfx)
|
||||
{
|
||||
if (code != CLF_CODE_ACCESS_VIOLATION) return 0;
|
||||
return faultModuleNetfx || (faultInNoModule && firstFrameNetfx);
|
||||
}
|
||||
|
||||
static int clf_seen(const clf_state* s, unsigned int addr)
|
||||
{
|
||||
unsigned int i;
|
||||
for (i = 0; i < s->addrCount; i++)
|
||||
if (s->addrs[i] == addr) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int clf_remember(clf_state* s, unsigned int addr)
|
||||
{
|
||||
if (s->addrCount >= CLF_ADDR_SLOTS) return 0;
|
||||
s->addrs[s->addrCount++] = addr;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* A first-chance exception (vectored handler). */
|
||||
static clf_decision clf_first_chance(clf_state* s, unsigned int code, unsigned int exAddr, int netfx)
|
||||
{
|
||||
if (!clf_is_logged_code(code)) return CLF_SKIP_CODE;
|
||||
if (clf_seen(s, exAddr)) {
|
||||
if (netfx) s->netfxRepeats++; else s->otherRepeats++;
|
||||
return CLF_SKIP_REPEAT;
|
||||
}
|
||||
if (netfx) {
|
||||
if (!clf_remember(s, exAddr)) { s->netfxRepeats++; return CLF_SKIP_REPEAT; }
|
||||
return CLF_LOG_NETFX;
|
||||
}
|
||||
if (s->counted >= CLF_MAX_COUNTED) { s->overCap++; return CLF_SKIP_CAP; }
|
||||
s->counted++;
|
||||
clf_remember(s, exAddr); /* full table: still logged, just not de-duplicated */
|
||||
return CLF_LOG_COUNTED;
|
||||
}
|
||||
|
||||
/* The last-chance (unhandled) filter. Any code: whatever reaches it is fatal. */
|
||||
static clf_decision clf_last_chance(clf_state* s)
|
||||
{
|
||||
if (s->lastChance >= CLF_MAX_LAST_CHANCE) return CLF_SKIP_CAP;
|
||||
s->lastChance++;
|
||||
return CLF_LOG_LAST_CHANCE;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -220,25 +220,55 @@ SEH_ThiscallIntUintPtr(void* fn, void* this_ptr, unsigned int arg1, void* arg2,
|
|||
return 1;
|
||||
}
|
||||
|
||||
/* ── Process-wide native crash logger (VEH) ───────────────────────────────
|
||||
/* -- 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 fatal exceptions that bypass
|
||||
* NativeAOT's managed handlers: 0xC0000005 (AV / corrupted-state) and
|
||||
* 0xC0000602 (STATUS_FAIL_FAST from RaiseFailFastException — e.g. an unhandled
|
||||
* managed exception or a stack-walk fail-fast in the plugin/engine).
|
||||
* 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
|
||||
* NativeAOT fail-fast on an AC-owned thread. It only OBSERVES — returns
|
||||
* EXCEPTION_CONTINUE_SEARCH 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. Hardened: stack sweep in
|
||||
* __try/__except, capped log count, dedicated file (no contention with the
|
||||
* managed log). x86 (32-bit) context fields only — matches the WOW64 target.
|
||||
* 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 volatile LONG g_crashCount = 0;
|
||||
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; }
|
||||
|
||||
|
|
@ -260,17 +290,31 @@ static char* cl_u(char* p, unsigned int v)
|
|||
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 = NULL;
|
||||
/* GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS(0x4) | UNCHANGED_REFCOUNT(0x2) */
|
||||
if (GetModuleHandleExW(0x4 | 0x2, (LPCWSTR)(UINT_PTR)addr, &hm) && hm) {
|
||||
wchar_t wpath[MAX_PATH];
|
||||
DWORD n = GetModuleFileNameW(hm, wpath, MAX_PATH);
|
||||
DWORD i, leaf = 0;
|
||||
for (i = 0; i < n; i++) { if (wpath[i] == L'\\' || wpath[i] == L'/') leaf = i + 1; }
|
||||
for (i = leaf; i < n; i++) { *p++ = (char)wpath[i]; }
|
||||
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 {
|
||||
|
|
@ -279,99 +323,164 @@ static char* cl_sym(char* p, unsigned int addr)
|
|||
return p;
|
||||
}
|
||||
|
||||
static LONG CALLBACK CrashVeh(PEXCEPTION_POINTERS ep)
|
||||
/* Nearest module-resolved code pointer on the stack is in the .NET Framework? */
|
||||
static int cl_first_frame_netfx(unsigned int esp)
|
||||
{
|
||||
DWORD code = ep->ExceptionRecord->ExceptionCode;
|
||||
if (code != 0xC0000005 && code != 0xC0000602 && code != 0xC0000409) {
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
if (InterlockedIncrement(&g_crashCount) > 24) {
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
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);
|
||||
|
||||
{
|
||||
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 EXCEPTION_CONTINUE_SEARCH; }
|
||||
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;
|
||||
|
||||
{
|
||||
char buf[4096];
|
||||
char* p = buf;
|
||||
CONTEXT* c = ep->ContextRecord;
|
||||
unsigned int exAddr = (unsigned int)(UINT_PTR)ep->ExceptionRecord->ExceptionAddress;
|
||||
DWORD wr;
|
||||
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);
|
||||
|
||||
p = cl_str(p, "\r\n==== NATIVE CRASH LOGGER (SEH VEH) ====\r\n code=");
|
||||
p = cl_hex(p, code);
|
||||
p = cl_str(p, " exAddr=");
|
||||
p = cl_sym(p, exAddr);
|
||||
p = cl_str(p, " tid=");
|
||||
p = cl_u(p, GetCurrentThreadId());
|
||||
{
|
||||
SYSTEMTIME st; GetLocalTime(&st);
|
||||
p = cl_str(p, " time=");
|
||||
p = cl_u(p, st.wHour); p = cl_str(p, ":");
|
||||
if (st.wMinute < 10) { p = cl_str(p, "0"); } p = cl_u(p, st.wMinute); p = cl_str(p, ":");
|
||||
if (st.wSecond < 10) { p = cl_str(p, "0"); } p = cl_u(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 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, buf, (DWORD)(p - buf), &wr, NULL);
|
||||
|
||||
__try {
|
||||
unsigned int* sp = (unsigned int*)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 = 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, buf, (DWORD)(q - buf), &wr, NULL);
|
||||
emitted++;
|
||||
}
|
||||
__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);
|
||||
__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 once. logPath = wide path to the dedicated crash log. */
|
||||
/* 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)
|
||||
{
|
||||
|
|
@ -382,6 +491,7 @@ RC_InstallCrashLogger(const wchar_t* logPath)
|
|||
g_crashLogPath[i] = 0;
|
||||
}
|
||||
g_vehHandle = AddVectoredExceptionHandler(1, CrashVeh);
|
||||
g_prevFilter = SetUnhandledExceptionFilter(CrashLastChance);
|
||||
}
|
||||
|
||||
/* ── Tagged-text parser guard (native MinHook detour body) ────────────────
|
||||
|
|
|
|||
118
native/SehTrampoline/tests/CrashLogFilterTests.c
Normal file
118
native/SehTrampoline/tests/CrashLogFilterTests.c
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* CrashLogFilterTests.c - offline checks for CrashLogFilter.h (the native crash logger's
|
||||
* filter). Build and run (x86 or x64 cl, from a VS developer prompt):
|
||||
* cl /nologo /W4 /Fe:<scratch>\clf_tests.exe CrashLogFilterTests.c
|
||||
* <scratch>\clf_tests.exe (exit 0 = pass, 1 = fail)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
#include "../CrashLogFilter.h"
|
||||
|
||||
static int g_checks, g_failed;
|
||||
|
||||
static void check(int ok, const char* what)
|
||||
{
|
||||
g_checks++;
|
||||
if (ok) return;
|
||||
g_failed++;
|
||||
printf("[FAIL] %s\n", what);
|
||||
}
|
||||
|
||||
/* Seen in a real log: 24 handled .NET Framework AVs at ONE address spent the cap,
|
||||
* and the fatal AC fault later was never written. */
|
||||
static void netfx_noise_never_spends_the_cap(void)
|
||||
{
|
||||
clf_state s = {0};
|
||||
int i, logged = 0;
|
||||
for (i = 0; i < 500; i++) {
|
||||
clf_decision d = clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x433620C8u, 1);
|
||||
if (d == CLF_LOG_NETFX) logged++;
|
||||
}
|
||||
check(logged == 1, "the repeating JIT-code AV is written once");
|
||||
check(s.counted == 0, "and never charged to the limit");
|
||||
check(s.netfxRepeats == 499, "repeats are counted");
|
||||
check(clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x0055FA24u, 0) == CLF_LOG_COUNTED,
|
||||
"a later AC fault is still written");
|
||||
}
|
||||
|
||||
static void other_faults_dedupe_per_address(void)
|
||||
{
|
||||
clf_state s = {0};
|
||||
unsigned int a;
|
||||
int i;
|
||||
for (i = 0; i < 50; i++)
|
||||
clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x00416C86u, 0);
|
||||
check(s.counted == 1 && s.otherRepeats == 49, "a repeating AC fault costs one entry");
|
||||
for (a = 1; a < CLF_MAX_COUNTED; a++)
|
||||
check(clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x00500000u + a * 16, 0) == CLF_LOG_COUNTED,
|
||||
"distinct addresses written up to the limit");
|
||||
check(clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x00600000u, 0) == CLF_SKIP_CAP,
|
||||
"past the limit: skipped");
|
||||
check(clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x00416C86u, 0) == CLF_SKIP_REPEAT,
|
||||
"a known address is a repeat, not a cap hit");
|
||||
}
|
||||
|
||||
static void codes(void)
|
||||
{
|
||||
clf_state s = {0};
|
||||
check(clf_first_chance(&s, 0xC00000FDu, 0x1000u, 0) == CLF_LOG_COUNTED, "stack overflow is logged");
|
||||
check(clf_first_chance(&s, 0xC0000374u, 0x2000u, 0) == CLF_LOG_COUNTED, "heap corruption is logged");
|
||||
check(clf_first_chance(&s, 0xC0000409u, 0x3000u, 0) == CLF_LOG_COUNTED, "stack buffer overrun is logged");
|
||||
check(clf_first_chance(&s, 0xC0000602u, 0x4000u, 0) == CLF_LOG_COUNTED, "fail-fast is logged");
|
||||
check(clf_first_chance(&s, 0xE06D7363u, 0x5000u, 0) == CLF_SKIP_CODE, "a C++ exception is not");
|
||||
check(clf_first_chance(&s, 0x80000003u, 0x6000u, 0) == CLF_SKIP_CODE, "a breakpoint is not");
|
||||
}
|
||||
|
||||
static void netfx_classification(void)
|
||||
{
|
||||
check(clf_is_netfx_module(L"clr.dll") && clf_is_netfx_module(L"MSCORLIB.NI.DLL")
|
||||
&& clf_is_netfx_module(L"mscorwks.dll") && clf_is_netfx_module(L"mscorlib.dll"),
|
||||
"the .NET Framework modules match, any case");
|
||||
check(!clf_is_netfx_module(L"coreclr.dll") && !clf_is_netfx_module(L"acclient.exe")
|
||||
&& !clf_is_netfx_module(L"clr.dllx") && !clf_is_netfx_module(0),
|
||||
"CoreCLR, AC and near-misses do not");
|
||||
check(clf_is_netfx_fault(CLF_CODE_ACCESS_VIOLATION, 1, 0, 0), "an AV in clr.dll is noise");
|
||||
check(clf_is_netfx_fault(CLF_CODE_ACCESS_VIOLATION, 0, 1, 1), "an AV in JIT code under clr frames is noise");
|
||||
check(!clf_is_netfx_fault(CLF_CODE_ACCESS_VIOLATION, 0, 1, 0), "an AV in no module under AC frames is not");
|
||||
check(!clf_is_netfx_fault(CLF_CODE_STACK_OVERFLOW, 1, 0, 0), "a stack overflow is never noise");
|
||||
check(!clf_is_netfx_fault(CLF_CODE_HEAP_CORRUPTION, 1, 0, 0), "heap corruption is never noise");
|
||||
}
|
||||
|
||||
static void last_chance_is_separate(void)
|
||||
{
|
||||
clf_state s = {0};
|
||||
unsigned int a;
|
||||
int i, logged = 0;
|
||||
for (a = 0; a < CLF_MAX_COUNTED + 5; a++)
|
||||
clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x00700000u + a * 16, 0);
|
||||
check(s.counted == CLF_MAX_COUNTED, "first-chance limit reached");
|
||||
check(clf_last_chance(&s) == CLF_LOG_LAST_CHANCE, "the unhandled one is still written");
|
||||
for (i = 0; i < 20; i++)
|
||||
if (clf_last_chance(&s) == CLF_LOG_LAST_CHANCE) logged++;
|
||||
check(logged == CLF_MAX_LAST_CHANCE - 1, "last-chance has its own small limit");
|
||||
}
|
||||
|
||||
static void full_table_never_silences_ac_faults(void)
|
||||
{
|
||||
clf_state s = {0};
|
||||
unsigned int a;
|
||||
for (a = 0; a < CLF_ADDR_SLOTS; a++)
|
||||
clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x40000000u + a * 16, 1); /* netfx fills it */
|
||||
check(s.addrCount == CLF_ADDR_SLOTS && s.counted == 0, "table full of .NET Framework addresses");
|
||||
check(clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x41000000u, 1) == CLF_SKIP_REPEAT,
|
||||
"a new .NET Framework address is no longer written");
|
||||
check(clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x0055FA24u, 0) == CLF_LOG_COUNTED,
|
||||
"an AC fault still is");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
netfx_noise_never_spends_the_cap();
|
||||
other_faults_dedupe_per_address();
|
||||
codes();
|
||||
netfx_classification();
|
||||
last_chance_is_separate();
|
||||
full_table_never_silences_ac_faults();
|
||||
printf("%d check(s), %d failed.\n", g_checks, g_failed);
|
||||
return g_failed == 0 ? 0 : 1;
|
||||
}
|
||||
28
native/SehTrampoline/tests/Run-CrashLogFilterTests.ps1
Normal file
28
native/SehTrampoline/tests/Run-CrashLogFilterTests.ps1
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# Run-CrashLogFilterTests.ps1
|
||||
# Builds and runs CrashLogFilterTests.c (x86 MSVC, the same toolchain as the DLL).
|
||||
# -OutDir: where the test exe goes (default: native\SehTrampoline\tests\bin). Exit 0 = pass.
|
||||
|
||||
param([string]$OutDir = (Join-Path $PSScriptRoot "bin"))
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$vsPath = & "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" `
|
||||
-latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 `
|
||||
-property installationPath 2>$null
|
||||
if (-not $vsPath) { Write-Error "Visual Studio with C++ tools not found."; exit 1 }
|
||||
|
||||
$vcVer = (Get-ChildItem "$vsPath\VC\Tools\MSVC" | Sort-Object Name -Descending | Select-Object -First 1).Name
|
||||
$vcBase = "$vsPath\VC\Tools\MSVC\$vcVer"
|
||||
$cl = "$vcBase\bin\Hostx86\x86\cl.exe"
|
||||
$sdkVer = (Get-ChildItem "C:\Program Files (x86)\Windows Kits\10\Include" | Sort-Object Name -Descending | Select-Object -First 1).Name
|
||||
$sdkInc = "C:\Program Files (x86)\Windows Kits\10\Include\$sdkVer"
|
||||
$sdkLib = "C:\Program Files (x86)\Windows Kits\10\Lib\$sdkVer"
|
||||
$env:INCLUDE = "$vcBase\include;$sdkInc\ucrt;$sdkInc\um;$sdkInc\shared"
|
||||
$env:LIB = "$vcBase\lib\x86;$sdkLib\ucrt\x86;$sdkLib\um\x86"
|
||||
|
||||
New-Item -ItemType Directory -Path $OutDir -Force | Out-Null
|
||||
$exe = Join-Path $OutDir "CrashLogFilterTests.exe"
|
||||
& $cl /nologo /W4 /WX "$PSScriptRoot\CrashLogFilterTests.c" /Fo"$OutDir\\" /Fe"$exe" /link /MACHINE:X86
|
||||
if ($LASTEXITCODE -ne 0) { Write-Error "Compilation failed."; exit $LASTEXITCODE }
|
||||
& $exe
|
||||
exit $LASTEXITCODE
|
||||
Loading…
Reference in a new issue