Leave a line in the log when a module ends the process with ExitProcess, and write the gathered lines at a focus loss.

Two runs of 2026-11-13 (the 0.10.0 fresh install's nx2navi.1.log, 7,974,927 bytes, and the 0.9.2 test's
nx2navi.3.log) ended seconds after "window lost the focus" with exit code 0, no WER event and no line of their own:
an ExitProcess or exit() called by a module in the process and a kill from outside could not be told apart, and
both lose the up to 250 ms of lines the log thread was gathering. A direct ExitProcess skips perf.c's atexit sync.

(a) ExitProcess stops every other thread and then calls each module's detach, the executable's TLS callbacks
included; TerminateProcess calls none. win32.c exit_note_tls, in the .CRT$XLB list beside _tls_used, writes on
DLL_PROCESS_DETACH what the log queue holds (logq.c logq_peek: the bytes as they stand, no lock) and one line,
"exit: the process is ending through ExitProcess, not one of the game's own ends", with the return addresses that
led there. It runs under the loader lock with the other threads gone, so it uses no C runtime (hex written by hand)
and does not ask the loader: the addresses are named from a module list noted at start and at each focus loss
(plat_exit_note_modules: EnumProcessModules, two tables and a writers' lock the callback never takes), and
RtlCaptureStackBackTrace and WriteFile take no lock of the process. plat_exit, plat_terminate and plat_reexec set
the orderly flag (plat_exit_orderly), and main does once main_run has returned, so a tool mode's exit() leaves no
line; a library's exit() during a game run does, naming ucrtbase and its caller. main.c log_open sets it up for a
game run that writes a log file. Linux has empty stand-ins.

(b) gfx_window_focus_changed calls the new main.c rt_log_focus_lost at a focus loss: logq_nudge makes the log thread
write what it gathers at once (a flag log_main's gathering loop checks) and the module list is noted again.

Not reproduced before the change (the two ends are unexplained). The new tests/exit_tls_check.py links win32.c and
logq.c with a queued line still being gathered: ExitProcess from the program and exit() write the line and the note
naming exit_tls_check.exe; TerminateProcess, plat_exit and a return after plat_exit_orderly write no note.
tests/logq_check.c gains a nudge case, which fails with the flag left unset (252 ms) and passes with it.
thread_end_check, fatal_exit_check, crash_stack_check, exit_order_check, save_share_check and the compile checks of
every runtime file pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:53:30 -05:00
parent dd78132e6d
commit b23acff301
11 changed files with 296 additions and 3 deletions

View File

@@ -104,7 +104,10 @@ void gfx_window_focus_changed(int in) {
plat_local_time(&t);
rt_log("gfx: window %s the focus at %04d-%02d-%02d %02d:%02d:%02d local time", in ? "gained" : "lost",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
if (!in) perf_focus_lost(); // the session's reports, in case the process is ended from outside next (perf.c)
if (!in) { // the session's reports and the log now, should the process be ended from outside next
perf_focus_lost(); // perf.c
rt_log_focus_lost(); // main.c
}
}
// ---- the orderly end: the window's close (gfx_window_closed), the guest's ExitProcess (svc.c) and the end of a

View File

@@ -25,6 +25,7 @@ void rt_log_raw(const char *p, size_t n);
const char *rt_log_path(void); // the log file, NULL when this run writes none
void rt_log_sync(void); // writes the queued log lines now; later lines are written as they are made
void rt_log_ending(void); // the process is ending: its lines reach the log file past its 64 MiB cap
void rt_log_focus_lost(void); // main.c: the gathered lines written now, the module list noted again
void hang_watch_start(void); // hang_watch.c: NX2NAVI_HANG_WATCH_S's thread, in a game run
void kern_wait_report(void); // kernel.c: the guest threads' lock and thread-end waits, for a hang report
void vk_sync_progress(uint64_t *issued, uint64_t *done); // vk_sync.c: submissions issued and done, no lock or device call

View File

@@ -41,7 +41,7 @@ static struct {
char *buf, *out; // LOGQ_CAP bytes each: the queue, and what the log thread is writing
size_t n;
LogqSink sink;
volatile int32_t on, saying;
volatile int32_t on, saying, now; // now: logq_nudge asked for the gathered lines at once
volatile int64_t slow, said, worst_ns, full; // slow writes, those reported, the longest; lines that found no room
volatile uint64_t said_at;
} Q = {.q = PLAT_MUTEX_INIT, .io = PLAT_MUTEX_INIT, .more = PLAT_COND_INIT};
@@ -135,9 +135,10 @@ static void log_main(void *arg) {
for (;;) {
plat_mutex_lock(&Q.q);
while (!Q.n) plat_cond_wait(&Q.more, &Q.q);
for (uint64_t t0 = plat_now_ns(), spent = 0; Q.n && Q.n < LOGQ_GATHER_BYTES && spent < LOGQ_GATHER_NS;
for (uint64_t t0 = plat_now_ns(), spent = 0; Q.n && Q.n < LOGQ_GATHER_BYTES && spent < LOGQ_GATHER_NS && !Q.now;
spent = plat_now_ns() - t0)
plat_cond_wait_ns(&Q.more, &Q.q, LOGQ_GATHER_NS - spent);
Q.now = 0;
plat_mutex_unlock(&Q.q);
drain();
say_slow();
@@ -174,6 +175,24 @@ void logq_flush(void) {
plat_mutex_unlock(&Q.io);
}
// The log thread writes what it gathers now instead of when the gathering time is up: a focus loss, after which the
// process may be ended from outside (main.c rt_log_focus_lost); such an end loses what was being gathered.
void logq_nudge(void) {
if (!__atomic_load_n(&Q.on, __ATOMIC_ACQUIRE)) return;
plat_mutex_lock(&Q.q);
Q.now = 1;
plat_mutex_unlock(&Q.q);
plat_cond_signal(&Q.more);
}
// What is queued, as it stands, with no lock: for the note of an end nobody asked for, written with every other thread
// stopped (platform.h plat_exit_note_setup). Nothing once the queue is off.
void logq_peek(const char **p, size_t *n) {
size_t k = __atomic_load_n(&Q.n, __ATOMIC_RELAXED);
*p = __atomic_load_n(&Q.buf, __ATOMIC_RELAXED);
*n = __atomic_load_n(&Q.on, __ATOMIC_RELAXED) && k <= LOGQ_CAP ? k : 0;
}
void logq_sync(void) {
if (!plat_xchg32(&Q.on, 0)) return;
// Each lock is tried for half a second: the thread that holds it may be the one that crashed, or be stuck in the

View File

@@ -9,4 +9,6 @@ int logq_async(void); // from now on the log thread writes
void logq_write(const char *p, size_t n); // queues the bytes, or writes them when the queue is off or has no room
void logq_sync(void); // writes what is queued, on the calling thread, and turns the queue off
void logq_flush(void); // writes what is queued, on the calling thread, and leaves the queue on
void logq_nudge(void); // the log thread writes what it gathers now (a focus loss)
void logq_peek(const char **p, size_t *n); // what is queued as it stands, no lock (the end nobody asked for)
size_t logq_bytes(void); // the queue's buffers, for the memory report (vk_memory.c system_log)

View File

@@ -140,6 +140,14 @@ static void console_closed(const char *what) {
gfx_window_closed();
}
// The window lost the focus (gfx_present.c gfx_window_focus_changed): a process ended from outside soon after lost the
// lines the log thread was still gathering, and both unexplained ends of 2026-11-13 came seconds after one. The log
// thread writes them at once, and the module list for the note of an ExitProcess is brought up to date.
void rt_log_focus_lost(void) {
logq_nudge();
plat_exit_note_modules();
}
void rt_log_raw(const char *p, size_t n) {
if (__atomic_load_n(&g_log_state, __ATOMIC_ACQUIRE) == LOG_EARLY) {
fwrite(p, 1, n, stderr);
@@ -212,6 +220,10 @@ static void log_open(int game) {
__atomic_store_n(&g_log_state, LOG_FILE, __ATOMIC_RELEASE);
rt_log("log: %s", g_log_path);
}
if (game && g_log_file) { // an ExitProcess nobody of ours asked for leaves a line in the file (platform.h)
plat_exit_note_setup(g_log_file, logq_peek);
plat_exit_note_modules();
}
// A game run writes its log from the log thread (logq.c), whether the lines go to a log file or to stderr alone.
const char *e = cfg_get("NX2NAVI_LOG_ASYNC");
if (!game || (e && !strcmp(e, "0"))) return;
@@ -690,6 +702,7 @@ int main(int argc, char **argv) {
plat_stack_guarantee(); // a stack overflow on the main thread still gets its crash report (platform.h)
mode_rc = main_run(argc, argv);
__atomic_store_n(&mode_done, 1, __ATOMIC_RELEASE); // from here on only the exit runs (mode_done, above)
plat_exit_orderly(); // the C runtime's exit ends the process with ExitProcess: no note of an end nobody asked for
return mode_rc;
}

View File

@@ -1933,6 +1933,11 @@ void plat_exit(int code) {
void plat_terminate(int code) { _exit(code); }
// The note of an end the game did not ask for is the Windows executable's TLS callback (win32.c); nothing here.
void plat_exit_note_setup(PlatAppend *log, void (*queued)(const char **p, size_t *n)) { (void)log, (void)queued; }
void plat_exit_note_modules(void) {}
void plat_exit_orderly(void) {}
void plat_stack_guarantee(void) {} // the crash report runs on the signal stack crash handling sets up
int plat_setenv(const char *name, const char *value) {

View File

@@ -403,6 +403,16 @@ int plat_file_private(const char *path); // only this account m
typedef struct PlatAppend PlatAppend;
PlatAppend *plat_append_open(const char *path); // creates or truncates path; NULL on failure
int plat_append_write(PlatAppend *f, const void *data, size_t n); // 1 when all n bytes landed
// An end the game did not ask for: ExitProcess (or exit()) called by a library, or by the runtime outside plat_exit and
// plat_terminate. Windows calls the executable's TLS callback for it, with every other thread already stopped, and the
// callback then writes what the log queue held (queued(&p, &n): the bytes as they stand, no lock) and one line with
// the return addresses that led to the end into log. Nothing for plat_exit, plat_terminate, TerminateProcess from
// outside, or after plat_exit_orderly (main returning from a tool mode). plat_exit_note_modules notes the loaded
// modules, whose names the line gives the addresses (the callback may not ask the loader): at start and at a focus
// loss. Windows only; elsewhere they do nothing.
void plat_exit_note_setup(PlatAppend *log, void (*queued)(const char **p, size_t *n));
void plat_exit_note_modules(void);
void plat_exit_orderly(void);
// Calls cb(name, ctx) for each file in dir whose name ends in ext (".in"); name has no directory part. cb returns 0 to
// stop. Returns 0 when dir cannot be listed.
int plat_file_list(const char *dir, const char *ext, int (*cb)(const char *name, void *ctx), void *ctx);

View File

@@ -1270,6 +1270,7 @@ int plat_crash_filter_reassert(char *who, size_t n) {
// A fresh process gets a fresh layout: Windows places DLLs, heaps and thread stacks at random addresses. The parent
// waits and exits with the child's code.
void plat_reexec(void) {
plat_exit_orderly(); // this process ends with ExitProcess below, on purpose
fprintf(stderr, "[host] memory: a fixed region is taken; re-running the process\n");
fflush(stdout);
fflush(stderr);
@@ -1390,6 +1391,7 @@ int plat_stderr_write(const void *p, size_t n) {
void (*plat_exit_hook)(void);
void plat_exit(int code) {
plat_exit_orderly();
if (plat_exit_hook) plat_exit_hook();
#ifdef NX2NAVI_PGO_GEN
__llvm_profile_dump();
@@ -1409,10 +1411,106 @@ void plat_exit(int code) {
}
void plat_terminate(int code) {
plat_exit_orderly();
TerminateProcess(GetCurrentProcess(), (UINT)code);
ExitProcess((UINT)code); // not reached
}
// ---- an end the game did not ask for (plat_exit_note_setup)
// Two runs of 2026-11-13 ended seconds after a focus loss with exit code 0, no crash event and no line of their own: an
// ExitProcess by a module in the process, or a kill from outside, could not be told apart. ExitProcess stops every
// other thread and then calls each module's detach, the executable's TLS callbacks included; TerminateProcess calls
// none. The callback runs under the loader lock with the other threads gone, so any lock they held stays held: it
// uses no C runtime (no formatting, allocation or streams: their locks), does not ask the loader (RtlPcToFileHeader
// takes its module lock) and names addresses from the module list noted earlier. RtlCaptureStackBackTrace and
// WriteFile take no lock of the process.
#define EN_MODS 384
typedef struct { uintptr_t base, size; char name[40]; } EnMod;
static EnMod en_mods[2][EN_MODS];
static int en_n[2];
static volatile LONG en_cur = -1; // the table the callback reads, -1 none yet
static PlatMutex en_lock = PLAT_MUTEX_INIT; // the writers of the tables (start, focus losses)
static PlatAppend *volatile en_log;
static void (*volatile en_queued)(const char **p, size_t *n);
static volatile LONG en_orderly;
void plat_exit_orderly(void) { InterlockedExchange(&en_orderly, 1); }
void plat_exit_note_setup(PlatAppend *log, void (*queued)(const char **p, size_t *n)) {
en_queued = queued;
en_log = log;
}
void plat_exit_note_modules(void) {
HMODULE m[EN_MODS];
DWORD need = 0;
if (!EnumProcessModules(GetCurrentProcess(), m, sizeof m, &need)) return;
int n = (int)(need / sizeof m[0] < EN_MODS ? need / sizeof m[0] : EN_MODS), k = 0;
plat_mutex_lock(&en_lock);
int t = InterlockedCompareExchange(&en_cur, 0, 0) == 0 ? 1 : 0; // the table the callback does not read
for (int i = 0; i < n; i++) {
MODULEINFO mi;
if (!GetModuleInformation(GetCurrentProcess(), m[i], &mi, sizeof mi)) continue;
EnMod *e = &en_mods[t][k++];
e->base = (uintptr_t)mi.lpBaseOfDll, e->size = mi.SizeOfImage;
if (!GetModuleBaseNameA(GetCurrentProcess(), m[i], e->name, sizeof e->name)) e->name[0] = 0;
}
en_n[t] = k;
InterlockedExchange(&en_cur, t);
plat_mutex_unlock(&en_lock);
}
static size_t en_put(char *o, size_t at, size_t cap, const char *s) {
while (*s && at < cap) o[at++] = *s++;
return at;
}
static size_t en_hex(char *o, size_t at, size_t cap, uintptr_t v) {
char d[18];
int n = 0;
do d[n++] = "0123456789abcdef"[v & 15], v >>= 4; while (v);
at = en_put(o, at, cap, "0x");
while (n && at < cap) o[at++] = d[--n];
return at;
}
// An executable gets DLL_PROCESS_DETACH only as the process ends (its reserved argument is NULL there all the same).
static void NTAPI exit_note_tls(PVOID h, DWORD reason, PVOID reserved) {
(void)h, (void)reserved;
PlatAppend *log = en_log;
if (reason != DLL_PROCESS_DETACH || en_orderly || !log) return;
const char *q = NULL;
size_t qn = 0;
void (*queued)(const char **, size_t *) = en_queued;
if (queued) queued(&q, &qn);
if (q && qn) plat_append_write(log, q, qn);
char line[2048];
size_t at = en_put(line, 0, sizeof line - 1, "[host] exit: the process is ending through ExitProcess, not one of "
"the game's own ends; the calls that led to it:");
void *f[32];
USHORT n = RtlCaptureStackBackTrace(1, 32, f, NULL);
LONG t = en_cur;
for (USHORT i = 0; i < n; i++) {
uintptr_t a = (uintptr_t)f[i];
const EnMod *m = NULL;
for (int k = 0; t >= 0 && k < en_n[t] && !m; k++)
if (a - en_mods[t][k].base < en_mods[t][k].size) m = &en_mods[t][k];
at = en_put(line, at, sizeof line - 1, " ");
if (m && m->name[0]) {
at = en_put(line, en_put(line, at, sizeof line - 1, m->name), sizeof line - 1, "+");
a -= m->base;
}
at = en_hex(line, at, sizeof line - 1, a);
}
line[at++] = '\n';
plat_append_write(log, line, at);
}
// The callback in the executable's TLS callback list (the .CRT$XL* sections), which the TLS directory the image has
// for its thread-local variables points to; _tls_used is that directory.
__attribute__((section(".CRT$XLB"), used)) PIMAGE_TLS_CALLBACK plat_exit_note_cb = exit_note_tls;
extern const IMAGE_TLS_DIRECTORY _tls_used;
__attribute__((used)) static const void *volatile exit_note_tls_dir = &_tls_used;
// _putenv_s, not SetEnvironmentVariableA: getenv reads the C runtime's copy of the environment, which only the CRT's
// own calls update. An empty value removes the variable.
int plat_setenv(const char *name, const char *value) { return _putenv_s(name, value ? value : "") == 0; }