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>
This commit is contained in:
parent
55f7497065
commit
8c7201dc25
41 changed files with 1797 additions and 448 deletions
|
|
@ -15,26 +15,34 @@
|
|||
* 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.
|
||||
* - Each faulting address has a slot with its hit count and the time it was last
|
||||
* written. The first sighting is written. A repeat is written again (with its count)
|
||||
* when the count is a power of two, when 30 s have passed since that address was
|
||||
* last written, or when the code is not an AV. So a contained AV that later recurs
|
||||
* as the fatal one is not swallowed as "seen before": a fatal fault is usually a
|
||||
* terminate from some handler, and then no last-chance filter ever runs.
|
||||
* - Noise is classified, written once per address like the rest, and never charged to
|
||||
* the AC limit:
|
||||
* .NET Framework (Decal plugins): faulting module clr.dll, mscorwks.dll, mscorlib;
|
||||
* managed (the engine's CoreCLR): coreclr.dll, clrjit.dll, System.Private.CoreLib;
|
||||
* or no module at all (JIT code) with the nearest module-resolved code pointer on the
|
||||
* stack in one of those. Only AVs are ever noise.
|
||||
* - A first sighting of anything else (AC's faults) is charged to CLF_MAX_COUNTED, a
|
||||
* flood guard on DISTINCT addresses only.
|
||||
* - The last-chance (unhandled) entry is never charged to that limit and has its own
|
||||
* small one (it happens once in a dying process; the limit only stops a loop).
|
||||
* - A full address table evicts its least recently written NOISE slot. With no noise
|
||||
* slot to evict, an AC address is still written (under the limit), just not tracked.
|
||||
*
|
||||
* Times are GetTickCount milliseconds (32-bit; unsigned subtraction survives the wrap).
|
||||
*/
|
||||
#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_MAX_COUNTED 96
|
||||
#define CLF_MAX_LAST_CHANCE 8
|
||||
#define CLF_ADDR_SLOTS 128
|
||||
#define CLF_RELOG_MS 30000u
|
||||
|
||||
#define CLF_CODE_ACCESS_VIOLATION 0xC0000005u
|
||||
#define CLF_CODE_FAIL_FAST 0xC0000602u
|
||||
|
|
@ -42,23 +50,39 @@
|
|||
#define CLF_CODE_STACK_OVERFLOW 0xC00000FDu
|
||||
#define CLF_CODE_HEAP_CORRUPTION 0xC0000374u
|
||||
|
||||
/* What raised a fault. Anything but CLF_CLASS_AC is noise. */
|
||||
typedef enum {
|
||||
CLF_CLASS_AC = 0, /* AC, Decal native code, the engine's native code: counted */
|
||||
CLF_CLASS_NETFX = 1, /* the .NET Framework (Decal's managed plugins) */
|
||||
CLF_CLASS_MANAGED = 2, /* the engine's CoreCLR runtime and its JIT code */
|
||||
} clf_class;
|
||||
|
||||
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_COUNTED, /* write it; first sighting, charged to the limit */
|
||||
CLF_LOG_NOISE, /* write it; first sighting of noise, not charged */
|
||||
CLF_LOG_REPEAT, /* write it again; a repeat (see hits) */
|
||||
CLF_LOG_LAST_CHANCE, /* write it; unhandled */
|
||||
CLF_SKIP_REPEAT, /* address already written */
|
||||
CLF_SKIP_CAP, /* cap reached */
|
||||
CLF_SKIP_REPEAT, /* a repeat not due to be written */
|
||||
CLF_SKIP_CAP, /* limit 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 addr;
|
||||
unsigned int hits; /* sightings, the first included */
|
||||
unsigned int lastWriteMs;
|
||||
unsigned char cls; /* clf_class */
|
||||
unsigned char used;
|
||||
} clf_slot;
|
||||
|
||||
typedef struct {
|
||||
clf_slot slots[CLF_ADDR_SLOTS];
|
||||
unsigned int counted; /* AC first sightings charged to the limit */
|
||||
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 */
|
||||
unsigned int noiseRepeats; /* noise repeats not written */
|
||||
unsigned int otherRepeats; /* other repeats not written */
|
||||
unsigned int overCap; /* not written: limit reached */
|
||||
unsigned int untracked; /* AC addresses written but not tracked (table full of AC) */
|
||||
} clf_state;
|
||||
|
||||
static int clf_is_logged_code(unsigned int code)
|
||||
|
|
@ -81,55 +105,102 @@ static int clf_wide_ieq(const wchar_t* a, const char* b)
|
|||
}
|
||||
}
|
||||
|
||||
/* Leaf file name of a module: the .NET Framework runtime or its core library. */
|
||||
static int clf_is_netfx_module(const wchar_t* leaf)
|
||||
/* Class of a module, by its leaf file name. */
|
||||
static clf_class clf_module_class(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");
|
||||
if (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"))
|
||||
return CLF_CLASS_NETFX;
|
||||
if (clf_wide_ieq(leaf, "coreclr.dll") || clf_wide_ieq(leaf, "clrjit.dll")
|
||||
|| clf_wide_ieq(leaf, "System.Private.CoreLib.dll"))
|
||||
return CLF_CLASS_MANAGED;
|
||||
return CLF_CLASS_AC;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
* Class of a first-chance exception. faultClass: class of the faulting module (AC when in
|
||||
* no module). faultInNoModule: the faulting address is in no module (JIT code).
|
||||
* firstFrameClass: class of the nearest module-resolved code pointer on the stack.
|
||||
*/
|
||||
static int clf_is_netfx_fault(unsigned int code, int faultModuleNetfx, int faultInNoModule, int firstFrameNetfx)
|
||||
static clf_class clf_classify(unsigned int code, clf_class faultClass, int faultInNoModule, clf_class firstFrameClass)
|
||||
{
|
||||
if (code != CLF_CODE_ACCESS_VIOLATION) return 0;
|
||||
return faultModuleNetfx || (faultInNoModule && firstFrameNetfx);
|
||||
if (code != CLF_CODE_ACCESS_VIOLATION) return CLF_CLASS_AC;
|
||||
if (faultClass != CLF_CLASS_AC) return faultClass;
|
||||
if (faultInNoModule) return firstFrameClass;
|
||||
return CLF_CLASS_AC;
|
||||
}
|
||||
|
||||
static int clf_seen(const clf_state* s, unsigned int addr)
|
||||
static clf_slot* clf_find(clf_state* s, unsigned int addr)
|
||||
{
|
||||
unsigned int i;
|
||||
for (i = 0; i < s->addrCount; i++)
|
||||
if (s->addrs[i] == addr) return 1;
|
||||
int i;
|
||||
for (i = 0; i < CLF_ADDR_SLOTS; i++)
|
||||
if (s->slots[i].used && s->slots[i].addr == addr) return &s->slots[i];
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int clf_remember(clf_state* s, unsigned int addr)
|
||||
static int clf_is_pow2(unsigned int v) { return v != 0 && (v & (v - 1)) == 0; }
|
||||
|
||||
/* A repeat at a tracked address. *hits gets the sighting count. */
|
||||
static clf_decision clf_repeat(clf_state* s, clf_slot* slot, unsigned int code, unsigned int nowMs, unsigned int* hits)
|
||||
{
|
||||
if (s->addrCount >= CLF_ADDR_SLOTS) return 0;
|
||||
s->addrs[s->addrCount++] = addr;
|
||||
return 1;
|
||||
slot->hits++;
|
||||
if (hits) *hits = slot->hits;
|
||||
if (clf_is_pow2(slot->hits) || nowMs - slot->lastWriteMs >= CLF_RELOG_MS || code != CLF_CODE_ACCESS_VIOLATION) {
|
||||
slot->lastWriteMs = nowMs;
|
||||
return CLF_LOG_REPEAT;
|
||||
}
|
||||
if (slot->cls != CLF_CLASS_AC) s->noiseRepeats++; else s->otherRepeats++;
|
||||
return CLF_SKIP_REPEAT;
|
||||
}
|
||||
|
||||
/* A first-chance exception (vectored handler). */
|
||||
static clf_decision clf_first_chance(clf_state* s, unsigned int code, unsigned int exAddr, int netfx)
|
||||
/* A free slot, else the least recently written noise slot, else none. */
|
||||
static clf_slot* clf_take_slot(clf_state* s)
|
||||
{
|
||||
clf_slot* oldest = 0;
|
||||
int i;
|
||||
for (i = 0; i < CLF_ADDR_SLOTS; i++)
|
||||
if (!s->slots[i].used) return &s->slots[i];
|
||||
for (i = 0; i < CLF_ADDR_SLOTS; i++) {
|
||||
clf_slot* c = &s->slots[i];
|
||||
if (c->cls == CLF_CLASS_AC) continue;
|
||||
if (!oldest || (int)(c->lastWriteMs - oldest->lastWriteMs) < 0) oldest = c;
|
||||
}
|
||||
return oldest;
|
||||
}
|
||||
|
||||
/* First sighting of an address (clf_find found nothing). */
|
||||
static clf_decision clf_new(clf_state* s, unsigned int code, unsigned int addr, clf_class cls, unsigned int nowMs)
|
||||
{
|
||||
clf_slot* slot;
|
||||
if (cls == CLF_CLASS_AC) {
|
||||
if (s->counted >= CLF_MAX_COUNTED) { s->overCap++; return CLF_SKIP_CAP; }
|
||||
s->counted++;
|
||||
}
|
||||
slot = clf_take_slot(s);
|
||||
if (slot) {
|
||||
slot->addr = addr;
|
||||
slot->hits = 1;
|
||||
slot->lastWriteMs = nowMs;
|
||||
slot->cls = (unsigned char)cls;
|
||||
slot->used = 1;
|
||||
} else {
|
||||
s->untracked++;
|
||||
}
|
||||
(void)code;
|
||||
return cls == CLF_CLASS_AC ? CLF_LOG_COUNTED : CLF_LOG_NOISE;
|
||||
}
|
||||
|
||||
/* The whole first-chance decision in one call (the logger splits it to skip the module
|
||||
* lookup for repeats). *hits gets the sighting count (1 for a first sighting). */
|
||||
static clf_decision clf_first_chance(clf_state* s, unsigned int code, unsigned int addr, clf_class cls,
|
||||
unsigned int nowMs, unsigned int* hits)
|
||||
{
|
||||
clf_slot* slot;
|
||||
if (hits) *hits = 1;
|
||||
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;
|
||||
slot = clf_find(s, addr);
|
||||
if (slot) return clf_repeat(s, slot, code, nowMs, hits);
|
||||
return clf_new(s, code, addr, cls, nowMs);
|
||||
}
|
||||
|
||||
/* The last-chance (unhandled) filter. Any code: whatever reaches it is fatal. */
|
||||
|
|
|
|||
|
|
@ -230,9 +230,10 @@ SEH_ThiscallIntUintPtr(void* fn, void* this_ptr, unsigned int arg1, void* arg2,
|
|||
* 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.
|
||||
* 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
|
||||
|
|
@ -247,13 +248,46 @@ SEH_ThiscallIntUintPtr(void* fn, void* this_ptr, unsigned int arg1, void* arg2,
|
|||
*/
|
||||
#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)
|
||||
{
|
||||
|
|
@ -292,153 +326,223 @@ static char* cl_u(char* p, unsigned int v)
|
|||
|
||||
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)
|
||||
/*
|
||||
* 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;
|
||||
*out = 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; }
|
||||
*out = hm;
|
||||
return g_wpath + leaf;
|
||||
*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)
|
||||
static char* cl_sym(char* p, unsigned int addr, int allowFallback)
|
||||
{
|
||||
HMODULE hm;
|
||||
const wchar_t* leaf = cl_module(addr, &hm);
|
||||
unsigned int base;
|
||||
clf_class cls;
|
||||
const char* leaf = cl_module(addr, allowFallback, &base, &cls);
|
||||
if (leaf) {
|
||||
while (*leaf) { *p++ = (char)*leaf++; }
|
||||
p = cl_str(p, leaf);
|
||||
p = cl_str(p, "+");
|
||||
p = cl_hex(p, addr - (unsigned int)(UINT_PTR)hm);
|
||||
p = cl_hex(p, addr - base);
|
||||
} 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)
|
||||
/* 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)
|
||||
{
|
||||
int result = 0;
|
||||
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];
|
||||
if (v > 0x00401000u && v < 0x7FFF0000u) {
|
||||
HMODULE hm;
|
||||
const wchar_t* leaf = cl_module(v, &hm);
|
||||
if (leaf) { result = clf_is_netfx_module(leaf); break; }
|
||||
}
|
||||
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 = 0; }
|
||||
__except (EXCEPTION_EXECUTE_HANDLER) { result = CLF_CLASS_AC; }
|
||||
return result;
|
||||
}
|
||||
|
||||
static void cl_write_entry(PEXCEPTION_POINTERS ep, const char* kind)
|
||||
static void cl_write(const char* p, DWORD n)
|
||||
{
|
||||
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);
|
||||
DWORD wr;
|
||||
if (g_logHandle != INVALID_HANDLE_VALUE) WriteFile(g_logHandle, p, n, &wr, NULL);
|
||||
}
|
||||
|
||||
{
|
||||
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;
|
||||
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);
|
||||
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 (");
|
||||
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) {
|
||||
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++;
|
||||
}
|
||||
}
|
||||
__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";
|
||||
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";
|
||||
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)
|
||||
|
|
@ -450,17 +554,27 @@ static LONG CALLBACK CrashVeh(PEXCEPTION_POINTERS ep)
|
|||
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");
|
||||
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;
|
||||
|
|
@ -471,7 +585,7 @@ 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_write_entry(ep, "LAST-CHANCE, unhandled", 1);
|
||||
cl_unlock();
|
||||
}
|
||||
if (g_prevFilter && g_prevFilter != CrashLastChance) {
|
||||
|
|
@ -480,7 +594,11 @@ static LONG WINAPI CrashLastChance(PEXCEPTION_POINTERS ep)
|
|||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
|
||||
/* Install the VEH and the last-chance filter 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, 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)
|
||||
{
|
||||
|
|
@ -489,7 +607,11 @@ RC_InstallCrashLogger(const wchar_t* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
/*
|
||||
* 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)
|
||||
* filter). Build and run with Run-CrashLogFilterTests.ps1 (x86 MSVC). Exit 0 = pass.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
|
@ -18,73 +16,128 @@ static void check(int ok, const char* what)
|
|||
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)
|
||||
#define AV CLF_CODE_ACCESS_VIOLATION
|
||||
#define FC(s, code, addr, cls, now) clf_first_chance((s), (code), (addr), (cls), (now), &hits)
|
||||
|
||||
static int is_write(clf_decision d)
|
||||
{
|
||||
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");
|
||||
return d == CLF_LOG_COUNTED || d == CLF_LOG_NOISE || d == CLF_LOG_REPEAT;
|
||||
}
|
||||
|
||||
static void other_faults_dedupe_per_address(void)
|
||||
/* Seen in a real log: handled .NET Framework AVs at ONE address spent the old cap,
|
||||
* and the fatal AC fault later was never written. */
|
||||
static void netfx_noise_never_spends_the_limit(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");
|
||||
static clf_state s;
|
||||
unsigned int hits, i, written = 0;
|
||||
for (i = 0; i < 500; i++)
|
||||
if (is_write(FC(&s, AV, 0x433620C8u, CLF_CLASS_NETFX, 1000 + i * 10))) written++;
|
||||
/* first + the power-of-two repeats 2,4,8,16,32,64,128,256 = 9 entries for 500 hits */
|
||||
check(written == 9, "500 hits of one JIT-code AV: 9 entries (first + powers of two)");
|
||||
check(s.counted == 0, "and never charged to the limit");
|
||||
check(s.noiseRepeats == 491, "skipped repeats are counted as noise");
|
||||
check(FC(&s, AV, 0x0055FA24u, CLF_CLASS_AC, 7000) == CLF_LOG_COUNTED, "a later AC fault is still written");
|
||||
}
|
||||
|
||||
/* A contained AV at a known address that later goes fatal. */
|
||||
static void known_address_going_fatal_is_written(void)
|
||||
{
|
||||
static clf_state s;
|
||||
unsigned int hits, i;
|
||||
clf_decision d;
|
||||
for (i = 0; i < 3; i++) FC(&s, AV, 0x00416C86u, CLF_CLASS_AC, 1000 + i);
|
||||
d = FC(&s, AV, 0x00416C86u, CLF_CLASS_AC, 1010);
|
||||
check(d == CLF_LOG_REPEAT && hits == 4, "3 contained AVs, then a 4th (fatal) at the same address: written, repeat=4");
|
||||
|
||||
{
|
||||
static clf_state t;
|
||||
unsigned int now = 5000;
|
||||
for (i = 0; i < 1000; i++) FC(&t, AV, 0x00416C86u, CLF_CLASS_AC, now + i); /* within 1 s */
|
||||
d = FC(&t, AV, 0x00416C86u, CLF_CLASS_AC, now + 1000 + 31000);
|
||||
check(d == CLF_LOG_REPEAT && hits == 1001, "1000 repeats, then the same address 31 s later: written");
|
||||
d = FC(&t, AV, 0x00416C86u, CLF_CLASS_AC, now + 1000 + 31001);
|
||||
check(d == CLF_SKIP_REPEAT, "and the next one right after is not");
|
||||
}
|
||||
{
|
||||
static clf_state u;
|
||||
FC(&u, AV, 0x00500000u, CLF_CLASS_AC, 100);
|
||||
FC(&u, AV, 0x00500000u, CLF_CLASS_AC, 101);
|
||||
FC(&u, AV, 0x00500000u, CLF_CLASS_AC, 102); /* hits 3: skipped */
|
||||
d = FC(&u, CLF_CODE_HEAP_CORRUPTION, 0x00500000u, CLF_CLASS_AC, 103);
|
||||
check(d == CLF_LOG_REPEAT, "a non-AV code at a known address is always written");
|
||||
}
|
||||
{
|
||||
static clf_state w; /* the 32-bit tick wrap */
|
||||
FC(&w, AV, 0x00600000u, CLF_CLASS_AC, 0xFFFFF000u);
|
||||
FC(&w, AV, 0x00600000u, CLF_CLASS_AC, 0xFFFFF001u);
|
||||
FC(&w, AV, 0x00600000u, CLF_CLASS_AC, 0xFFFFF002u);
|
||||
d = FC(&w, AV, 0x00600000u, CLF_CLASS_AC, 0x00007000u); /* ~32 s later, past the wrap */
|
||||
check(d == CLF_LOG_REPEAT, "the 30 s relog survives the GetTickCount wrap");
|
||||
}
|
||||
}
|
||||
|
||||
static void ac_faults_limited_per_distinct_address(void)
|
||||
{
|
||||
static clf_state s;
|
||||
unsigned int hits, a;
|
||||
for (a = 0; a < 50; a++) FC(&s, AV, 0x00416C86u, CLF_CLASS_AC, 1000);
|
||||
check(s.counted == 1, "a repeating AC fault is charged once");
|
||||
for (a = 1; a < CLF_MAX_COUNTED; a++)
|
||||
check(clf_first_chance(&s, CLF_CODE_ACCESS_VIOLATION, 0x00500000u + a * 16, 0) == CLF_LOG_COUNTED,
|
||||
check(FC(&s, AV, 0x00500000u + a * 16, CLF_CLASS_AC, 1000) == 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");
|
||||
check(FC(&s, AV, 0x00700000u, CLF_CLASS_AC, 1000) == CLF_SKIP_CAP, "past the limit: skipped");
|
||||
check(FC(&s, AV, 0x00416C86u, CLF_CLASS_AC, 1000) != CLF_SKIP_CAP,
|
||||
"a known address is decided as a repeat, never by the limit");
|
||||
}
|
||||
|
||||
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 clf_state s;
|
||||
unsigned int hits;
|
||||
check(FC(&s, 0xC00000FDu, 0x1000u, CLF_CLASS_AC, 1) == CLF_LOG_COUNTED, "stack overflow is logged");
|
||||
check(FC(&s, 0xC0000374u, 0x2000u, CLF_CLASS_AC, 1) == CLF_LOG_COUNTED, "heap corruption is logged");
|
||||
check(FC(&s, 0xC0000409u, 0x3000u, CLF_CLASS_AC, 1) == CLF_LOG_COUNTED, "stack buffer overrun is logged");
|
||||
check(FC(&s, 0xC0000602u, 0x4000u, CLF_CLASS_AC, 1) == CLF_LOG_COUNTED, "fail-fast is logged");
|
||||
check(FC(&s, 0xE06D7363u, 0x5000u, CLF_CLASS_AC, 1) == CLF_SKIP_CODE, "a C++ exception is not");
|
||||
check(FC(&s, 0x80000003u, 0x6000u, CLF_CLASS_AC, 1) == CLF_SKIP_CODE, "a breakpoint is not");
|
||||
}
|
||||
|
||||
static void netfx_classification(void)
|
||||
static void 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");
|
||||
check(clf_module_class(L"clr.dll") == CLF_CLASS_NETFX && clf_module_class(L"MSCORLIB.NI.DLL") == CLF_CLASS_NETFX
|
||||
&& clf_module_class(L"mscorwks.dll") == CLF_CLASS_NETFX && clf_module_class(L"mscorlib.dll") == CLF_CLASS_NETFX,
|
||||
"the .NET Framework modules, any case");
|
||||
check(clf_module_class(L"coreclr.dll") == CLF_CLASS_MANAGED && clf_module_class(L"clrjit.dll") == CLF_CLASS_MANAGED
|
||||
&& clf_module_class(L"System.Private.CoreLib.dll") == CLF_CLASS_MANAGED,
|
||||
"the engine's CoreCLR modules are managed noise");
|
||||
check(clf_module_class(L"acclient.exe") == CLF_CLASS_AC && clf_module_class(L"clr.dllx") == CLF_CLASS_AC
|
||||
&& clf_module_class(0) == CLF_CLASS_AC, "AC and near-misses are not noise");
|
||||
check(clf_classify(AV, CLF_CLASS_NETFX, 0, CLF_CLASS_AC) == CLF_CLASS_NETFX, "an AV in clr.dll is .NET Framework noise");
|
||||
check(clf_classify(AV, CLF_CLASS_AC, 1, CLF_CLASS_NETFX) == CLF_CLASS_NETFX, "JIT code under clr frames: .NET Framework noise");
|
||||
check(clf_classify(AV, CLF_CLASS_AC, 1, CLF_CLASS_MANAGED) == CLF_CLASS_MANAGED, "JIT code under CoreCLR frames: managed noise");
|
||||
check(clf_classify(AV, CLF_CLASS_MANAGED, 0, CLF_CLASS_AC) == CLF_CLASS_MANAGED, "an AV in coreclr.dll: managed noise");
|
||||
check(clf_classify(AV, CLF_CLASS_AC, 1, CLF_CLASS_AC) == CLF_CLASS_AC, "no module under AC frames: AC");
|
||||
check(clf_classify(AV, CLF_CLASS_AC, 0, CLF_CLASS_NETFX) == CLF_CLASS_AC, "an AV in an AC module is AC whatever is below it");
|
||||
check(clf_classify(CLF_CODE_STACK_OVERFLOW, CLF_CLASS_NETFX, 0, CLF_CLASS_AC) == CLF_CLASS_AC, "a stack overflow is never noise");
|
||||
check(clf_classify(CLF_CODE_HEAP_CORRUPTION, CLF_CLASS_MANAGED, 0, CLF_CLASS_AC) == CLF_CLASS_AC, "heap corruption is never noise");
|
||||
}
|
||||
|
||||
static void managed_noise_is_its_own_bucket(void)
|
||||
{
|
||||
static clf_state s;
|
||||
unsigned int hits, a;
|
||||
for (a = 0; a < 40; a++)
|
||||
check(FC(&s, AV, 0x70000000u + a * 16, CLF_CLASS_MANAGED, 1) == CLF_LOG_NOISE, "managed noise written once per address");
|
||||
check(s.counted == 0, "and never charged to the AC limit");
|
||||
}
|
||||
|
||||
static void last_chance_is_separate(void)
|
||||
{
|
||||
clf_state s = {0};
|
||||
unsigned int a;
|
||||
static clf_state s;
|
||||
unsigned int hits, 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);
|
||||
FC(&s, AV, 0x00700000u + a * 16, CLF_CLASS_AC, 1);
|
||||
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++)
|
||||
|
|
@ -92,27 +145,35 @@ static void last_chance_is_separate(void)
|
|||
check(logged == CLF_MAX_LAST_CHANCE - 1, "last-chance has its own small limit");
|
||||
}
|
||||
|
||||
static void full_table_never_silences_ac_faults(void)
|
||||
static void full_table_evicts_oldest_noise(void)
|
||||
{
|
||||
clf_state s = {0};
|
||||
unsigned int a;
|
||||
static clf_state s;
|
||||
unsigned int hits, a;
|
||||
clf_decision d;
|
||||
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");
|
||||
FC(&s, AV, 0x40000000u + a * 16, CLF_CLASS_NETFX, 1000 + a); /* noise fills it, 0x40000000 oldest */
|
||||
check(s.counted == 0, "table full of .NET Framework addresses");
|
||||
d = FC(&s, AV, 0x0055FA24u, CLF_CLASS_AC, 5000);
|
||||
check(d == CLF_LOG_COUNTED, "an AC fault is written");
|
||||
check(clf_find(&s, 0x0055FA24u) != 0, "and tracked, in the evicted slot");
|
||||
check(clf_find(&s, 0x40000000u) == 0, "the least recently written noise slot was evicted");
|
||||
check(clf_find(&s, 0x40000010u) != 0, "the next one was not");
|
||||
check(FC(&s, AV, 0x0055FA24u, CLF_CLASS_AC, 5001) == CLF_LOG_REPEAT && hits == 2, "so its repeat is deduped");
|
||||
d = FC(&s, AV, 0x41000000u, CLF_CLASS_NETFX, 6000);
|
||||
check(d == CLF_LOG_NOISE, "a new .NET Framework address is written, evicting an older one");
|
||||
check(clf_find(&s, 0x0055FA24u) != 0, "AC slots are never evicted");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
netfx_noise_never_spends_the_cap();
|
||||
other_faults_dedupe_per_address();
|
||||
netfx_noise_never_spends_the_limit();
|
||||
known_address_going_fatal_is_written();
|
||||
ac_faults_limited_per_distinct_address();
|
||||
codes();
|
||||
netfx_classification();
|
||||
classification();
|
||||
managed_noise_is_its_own_bucket();
|
||||
last_chance_is_separate();
|
||||
full_table_never_silences_ac_faults();
|
||||
full_table_evicts_oldest_noise();
|
||||
printf("%d check(s), %d failed.\n", g_checks, g_failed);
|
||||
return g_failed == 0 ? 0 : 1;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue