Files
NX2navi/tests/fatal_exit_check.c
chrg6 8f952d2841 End fatal errors and a lost device with TerminateProcess instead of _Exit, which ran every library's detach.
dispatch.c fatal_end and vk_device.c lost_wait_exit ended with _Exit, which in the C runtime is ExitProcess: it
stops the other threads wherever they are and then runs every library's DLL_PROCESS_DETACH on the ending thread
(the audit's probes show the exe's TLS callback getting it after _Exit and not after TerminateProcess). That is
the detach plat_exit already avoids, after NGX's faulted once at the end of a clean run. After a FATAL such a
fault would enter the crash handler and replace code 70 or 72 with a crash report and code 71; after a device
loss a driver detach that hangs would hold the process for good, since ExitProcess had stopped the watchdog too.

The new platform plat_terminate ends the process with the code and nothing else (no exit hook, no stream flush:
the failing paths have written their lines and flushed). fatal_end calls it after its existing log sync and
flushes. lost_wait_exit tells its watchdog (spawned with a non-NULL argument) from the threads that stop there:
after LOST_EXIT_MS the watchdog alone writes "vk: the end of the run did not finish in 10 s; ending it" through
rt_log_sync's bounded trylocks, and the others end 2 s later only should the watchdog not have started. Exit
codes stay 70 and 72.

Not reproduced before the change (no detach fault on a fatal path has been seen). tests/fatal_exit_check.py
(new: dispatch.c and the platform layer with a TLS callback; rt_fatal ends with 70, its FATAL line and no detach
call, which it showed with _Exit; and statically, no _Exit in runtime outside the platform layer, and both ends
call plat_terminate) and the compile checks pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 18:42:10 -05:00

63 lines
2.2 KiB
C

// runtime/dispatch.c's end of a fatal error (fatal_end), with the platform layer linked and the rest of the runtime as
// stand-ins that write straight to stdout. Built and run by tests/fatal_exit_check.py, which reads what each mode
// printed and its exit code. The exe has a TLS callback that prints a line when the process's libraries get their
// detach call, which ExitProcess (and _Exit, which is ExitProcess in the C runtime) makes and TerminateProcess does not.
//
// fatal_exit_check fatal rt_fatal: exit code 70, the FATAL line, no detach line
#include "dispatch.c"
#include <stdio.h>
#include <windows.h>
static void say(const char *s) {
DWORD w;
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), s, (DWORD)strlen(s), &w, NULL);
}
static void NTAPI tls_cb(PVOID h, DWORD reason, PVOID reserved) {
(void)h, (void)reserved;
if (reason == DLL_PROCESS_DETACH) say("detach: the libraries' detach calls ran\n");
}
__attribute__((section(".CRT$XLB"), used)) PIMAGE_TLS_CALLBACK p_tls_cb = tls_cb;
__thread int tls_var; // with _tls_used below: the image gets its TLS directory, so the callback is called
extern const IMAGE_TLS_DIRECTORY _tls_used;
const void *volatile keep_tls = &_tls_used;
// ---- the runtime fatal_end reaches
void rt_log_sync(void) {}
void rt_log_raw(const char *p, size_t n) {
DWORD w;
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), p, (DWORD)n, &w, NULL);
}
void rt_vlogf(const char *fmt, va_list ap) {
char b[1024];
int n = vsnprintf(b, sizeof b, fmt, ap);
if (n > 0) rt_log_raw(b, (size_t)n < sizeof b ? (size_t)n : sizeof b - 1);
}
void rt_logf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
rt_vlogf(fmt, ap);
va_end(ap);
}
void rt_log(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
rt_vlogf(fmt, ap);
va_end(ap);
rt_log_raw("\n", 1);
}
void kern_free_cores_note(void) {}
void prof_sampler_write(void) {}
void rtss_limit_restore(void) {}
void kern_checkpoint(void) {}
int main(int argc, char **argv) {
setvbuf(stdout, NULL, _IONBF, 0);
const char *mode = argc > 1 ? argv[1] : "";
tls_var = 1;
if (!strcmp(mode, "fatal")) rt_fatal("the test's fatal error %d", 1);
printf("usage: fatal_exit_check fatal\n");
return 2;
}