Engine robustness re-review follow-ups: crash-log flush ring, cast refusal, dump hold cap

- The native crash logger keeps the last 16 first-chance records in a static ring
  (pushed in the VEH: no I/O, no allocation, no loader call). A flush writes the ones
  not yet written, oldest first, each record once. Flushed from the last-chance filter
  and, through the new export RC_FlushCrashLog, from the engine's ExitProcess /
  TerminateProcess / RtlExitUserProcess / NtTerminateProcess detours on a crash code
  (raw unmanaged function pointer, resolved and primed at install). Recovers the fatal
  repeat of an address seen before, which the dedupe skips and which never reaches the
  last-chance filter when a handler terminates the process.
- EnqueueCast refuses during a world transition (CastSlot, pure) and counts the
  refusal in TransitionRejectedCount. The cast slot now owns the stop-wait note the
  cast drain keeps per cast (CastSlot.StopWaitNoted, reset by each queued cast before
  the publishing Volatile.Write of Pending), and DrainCasts reads the queue time once.
- The launcher's dump hold is capped at 15 min per client (DumpInProgressRule.Decide);
  past it the kill checks resume.
- CrashExitRule: ExitProcess(-1) (0xFFFFFFFF) is no longer a crash exit; the rest of
  the 0xC0000000-and-up range is unchanged, so CLR/C++ unhandled-exception exits
  (0xE0434352, 0xE06D7363) still skip managed work.
- The heartbeat line carries qrej=<TransitionRejectedCount>.

Tests: EngineRobustnessTests (adds CastSlot stop-wait and ActionRing.TryPeekAt checks),
CrashLogFilterTests.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-10-10 21:53:42 -07:00
commit 636f5d307a
14 changed files with 585 additions and 60 deletions

View file

@ -33,6 +33,13 @@
* 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.
* - The last CLF_RING first-chance records, written or not, are kept in a ring (no I/O
* at exception time). A flush writes those that were not written one by one: the
* last-chance filter flushes, and so does the engine's crash-exit path (TerminateProcess
* and friends, through RC_FlushCrashLog). That recovers the case the dedupe alone
* cannot: the fatal hit of an address seen before, at a count that is not due a
* write, followed by a terminate from a handler, so no last-chance filter ever runs
* (checked with 5, 100 and 1000 contained hits before the fatal one).
*
* Times are GetTickCount milliseconds (32-bit; unsigned subtraction survives the wrap).
*/
@ -203,6 +210,62 @@ static clf_decision clf_first_chance(clf_state* s, unsigned int code, unsigned i
return clf_new(s, code, addr, cls, nowMs);
}
#define CLF_RING 16
/* One first-chance record kept for a later flush. */
typedef struct {
unsigned int seq; /* 1-based push number; 0 = empty */
unsigned int addr, code, eip, tid, tickMs, hits;
unsigned char written; /* written as an entry of its own when it happened */
} clf_rec;
/*
* The last CLF_RING first-chance records. Each record is offered to one flush only:
* a second flush (one exit path calling another) writes nothing new. Sequence numbers
* are 32-bit; a process would need 4 billion logged-code exceptions to wrap them.
*/
typedef struct {
clf_rec r[CLF_RING];
unsigned int pushed; /* records pushed in total */
unsigned int flushedTo; /* every record up to this seq has been offered to a flush */
} clf_ring;
static void clf_ring_push(clf_ring* g, unsigned int addr, unsigned int code, unsigned int eip,
unsigned int tid, unsigned int tickMs, unsigned int hits, int written)
{
clf_rec* x = &g->r[g->pushed % CLF_RING];
g->pushed++;
x->seq = g->pushed;
x->addr = addr;
x->code = code;
x->eip = eip;
x->tid = tid;
x->tickMs = tickMs;
x->hits = hits;
x->written = (unsigned char)(written ? 1 : 0);
}
/*
* Flush: copies into out[CLF_RING], oldest first, the held records that were not written
* and not offered to an earlier flush, and marks everything pushed so far as offered.
* Returns how many were copied. *older gets the records pushed since the last flush that
* the ring no longer holds (written or not).
*/
static int clf_ring_take(clf_ring* g, clf_rec* out, unsigned int* older)
{
unsigned int first = g->pushed > CLF_RING ? g->pushed - CLF_RING : 0; /* held: first+1..pushed */
unsigned int from = g->flushedTo, s;
int n = 0;
if (older) *older = from < first ? first - from : 0;
if (from < first) from = first;
for (s = from + 1; s <= g->pushed; s++) {
const clf_rec* x = &g->r[(s - 1) % CLF_RING];
if (!x->written) out[n++] = *x;
}
g->flushedTo = g->pushed;
return n;
}
/* The last-chance (unhandled) filter. Any code: whatever reaches it is fatal. */
static clf_decision clf_last_chance(clf_state* s)
{

View file

@ -277,6 +277,8 @@ 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 clf_ring g_ring; /* under g_logOwner: recent first-chance records */
static clf_rec g_flushRecs[CLF_RING];/* 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 */
@ -458,22 +460,10 @@ static void cl_write(const char* p, DWORD n)
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)
/* Append " pid=N tid=N date=YYYY-MM-DD time=HH:MM:SS" (local time). */
static char* cl_stamp(char* p)
{
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=");
@ -487,6 +477,25 @@ static void cl_write_entry(PEXCEPTION_POINTERS ep, const char* kind, unsigned in
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);
return p;
}
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;
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_stamp(p);
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];
@ -545,6 +554,53 @@ static void cl_write_entry(PEXCEPTION_POINTERS ep, const char* kind, unsigned in
}
}
/*
* Under the lock: write the recent first-chance records that were not written one by one
* (clf_ring_take), oldest first, so the log ends with the newest, usually the fatal hit.
* Module names from the cache only: no loader call on a dying process.
*/
static void cl_flush_locked(const char* reason, unsigned int code)
{
unsigned int older = 0;
int n = clf_ring_take(&g_ring, g_flushRecs, &older), i;
char* p;
if (n == 0 || g_logHandle == INVALID_HANDLE_VALUE) return;
p = g_buf;
p = cl_str(p, "\r\n==== NATIVE CRASH LOGGER (flush: ");
p = cl_str(p, reason);
p = cl_str(p, ") ====\r\n code=");
p = cl_hex(p, code);
p = cl_stamp(p);
p = cl_str(p, "\r\n recent first-chance records not written one by one, oldest first (");
p = cl_u(p, (unsigned int)n);
p = cl_str(p, " held");
if (older) { p = cl_str(p, ", "); p = cl_u(p, older); p = cl_str(p, " older ones no longer held"); }
p = cl_str(p, "):\r\n");
cl_write(g_buf, (DWORD)(p - g_buf));
for (i = 0; i < n; i++) {
const clf_rec* x = &g_flushRecs[i];
p = g_buf;
p = cl_str(p, " exAddr=");
p = cl_sym(p, x->addr, 0);
p = cl_str(p, " code=");
p = cl_hex(p, x->code);
p = cl_str(p, " eip=");
p = cl_sym(p, x->eip, 0);
p = cl_str(p, " tid=");
p = cl_u(p, x->tid);
p = cl_str(p, " tick=");
p = cl_u(p, x->tickMs);
p = cl_str(p, " repeat=");
p = cl_u(p, x->hits);
p = cl_str(p, "\r\n");
cl_write(g_buf, (DWORD)(p - g_buf));
}
{
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;
@ -556,11 +612,14 @@ static LONG CALLBACK CrashVeh(PEXCEPTION_POINTERS ep)
unsigned int exAddr = (unsigned int)(UINT_PTR)ep->ExceptionRecord->ExceptionAddress;
unsigned int now = GetTickCount();
unsigned int hits = 1;
int written = 0;
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)
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);
written = 1;
}
} else {
unsigned int base;
clf_class faultCls = CLF_CLASS_AC;
@ -574,7 +633,10 @@ static LONG CALLBACK CrashVeh(PEXCEPTION_POINTERS ep)
cl_write_entry(ep, cls == CLF_CLASS_NETFX
? "first-chance, .NET Framework noise, not counted"
: "first-chance, managed (CoreCLR) noise, not counted", 1);
written = d == CLF_LOG_COUNTED || d == CLF_LOG_NOISE;
}
/* Kept for a flush (memory only): the fatal hit is often one not written here. */
clf_ring_push(&g_ring, exAddr, code, ep->ContextRecord->Eip, GetCurrentThreadId(), now, hits, written);
}
cl_unlock();
return EXCEPTION_CONTINUE_SEARCH;
@ -584,6 +646,7 @@ static LONG CALLBACK CrashVeh(PEXCEPTION_POINTERS ep)
static LONG WINAPI CrashLastChance(PEXCEPTION_POINTERS ep)
{
if (cl_lock()) {
cl_flush_locked("last chance", ep->ExceptionRecord->ExceptionCode);
if (clf_last_chance(&g_filter) == CLF_LOG_LAST_CHANCE)
cl_write_entry(ep, "LAST-CHANCE, unhandled", 1);
cl_unlock();
@ -594,6 +657,22 @@ static LONG WINAPI CrashLastChance(PEXCEPTION_POINTERS ep)
return EXCEPTION_CONTINUE_SEARCH;
}
/*
* A crash exit is under way (the engine's TerminateProcess / NtTerminateProcess /
* ExitProcess / RtlExitUserProcess detours, for a crash code): write the recent
* first-chance records not written one by one. Each record is written once, so the
* second of two nested exit paths writes nothing. exitCode 0 is the engine's priming
* call and does nothing.
*/
__declspec(dllexport) void __cdecl
RC_FlushCrashLog(unsigned int exitCode)
{
if (exitCode == 0) return;
if (!cl_lock()) return;
cl_flush_locked("crash exit", exitCode);
cl_unlock();
}
/*
* 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

View file

@ -3,6 +3,7 @@
* filter). Build and run with Run-CrashLogFilterTests.ps1 (x86 MSVC). Exit 0 = pass.
*/
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include "../CrashLogFilter.h"
@ -164,8 +165,69 @@ static void full_table_evicts_oldest_noise(void)
check(clf_find(&s, 0x0055FA24u) != 0, "AC slots are never evicted");
}
#define FATAL_ADDR 0x00401234u
/* N contained hits at one address, then the fatal one, then
* a terminate from a handler, so no last-chance filter runs. The fatal hit (N+1) is not due
* a write, so only the ring's flush can put it in the log. */
static void fatal_repeat_recovered_by_flush(unsigned int n)
{
clf_state s;
clf_ring g;
clf_rec out[CLF_RING];
unsigned int hits, i, older, now = 5000;
clf_decision d;
int got;
memset(&s, 0, sizeof s);
memset(&g, 0, sizeof g);
for (i = 0; i < n; i++) {
d = FC(&s, AV, FATAL_ADDR, CLF_CLASS_AC, now + i);
clf_ring_push(&g, FATAL_ADDR, AV, FATAL_ADDR, 77, now + i, hits, is_write(d));
}
d = FC(&s, AV, FATAL_ADDR, CLF_CLASS_AC, now + n + 1500); /* the fatal hit, 1.5 s later */
check(d == CLF_SKIP_REPEAT && hits == n + 1, "fatal hit N+1 is not due a write: the dedupe alone loses it");
clf_ring_push(&g, FATAL_ADDR, AV, FATAL_ADDR, 77, now + n + 1500, hits, is_write(d));
got = clf_ring_take(&g, out, &older);
check(got >= 1, "the flush has records");
check(got >= 1 && out[got - 1].addr == FATAL_ADDR && out[got - 1].hits == n + 1
&& out[got - 1].tickMs == now + n + 1500, "the flush ends with the fatal hit (N+1)");
for (i = 0; i < (unsigned int)got; i++)
check(!out[i].written, "a flush never repeats a record written on its own");
check(clf_ring_take(&g, out, &older) == 0, "a second flush writes nothing");
}
static void ring_order_overflow_and_written(void)
{
clf_ring g;
clf_rec out[CLF_RING];
unsigned int i, older;
int got;
memset(&g, 0, sizeof g);
clf_ring_push(&g, 0x10, AV, 0, 1, 1, 1, 1); /* written */
clf_ring_push(&g, 0x20, AV, 0, 1, 2, 2, 0);
clf_ring_push(&g, 0x30, AV, 0, 1, 3, 1, 1); /* written */
clf_ring_push(&g, 0x40, AV, 0, 1, 4, 3, 0);
got = clf_ring_take(&g, out, &older);
check(got == 2 && out[0].addr == 0x20 && out[1].addr == 0x40, "only unwritten records, oldest first");
check(older == 0, "nothing lost under the ring size");
for (i = 0; i < 40; i++) clf_ring_push(&g, 0x1000 + i, AV, 0, 1, 100 + i, 2, 0);
got = clf_ring_take(&g, out, &older);
check(got == CLF_RING, "40 unwritten since the last flush: the ring holds the newest 16");
check(older == 24, "and reports the 24 it no longer holds");
check(out[0].addr == 0x1000 + 24 && out[CLF_RING - 1].addr == 0x1000 + 39, "newest 16, oldest first");
clf_ring_push(&g, 0x9999, AV, 0, 1, 500, 2, 0);
got = clf_ring_take(&g, out, &older);
check(got == 1 && out[0].addr == 0x9999 && older == 0, "after a flush, only what came since");
}
int main(void)
{
fatal_repeat_recovered_by_flush(5);
fatal_repeat_recovered_by_flush(100);
fatal_repeat_recovered_by_flush(1000);
ring_order_overflow_and_written();
netfx_noise_never_spends_the_limit();
known_address_going_fatal_is_written();
ac_faults_limited_per_distinct_address();