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>
This commit is contained in:
chrg6
2026-11-13 17:11:26 -05:00
parent e39f71df39
commit 8f952d2841
7 changed files with 199 additions and 7 deletions

View File

@@ -80,7 +80,10 @@ static void fatal_end(int code, const char *fmt, va_list ap) {
rtss_limit_restore();
fflush(stdout);
fflush(stderr);
_Exit(code);
// Not _Exit: in the C runtime it is ExitProcess, which stops the other threads wherever they are and then runs every
// library's detach on this thread. A driver's detach that faults there turned a fatal error's end into a crash
// report with another code (platform/win32.c plat_exit names one such detach), and one that hangs held the process.
plat_terminate(code);
}
void rt_fatal(const char *fmt, ...) {

View File

@@ -1927,6 +1927,8 @@ void plat_exit(int code) {
_exit(code);
}
void plat_terminate(int code) { _exit(code); }
int plat_setenv(const char *name, const char *value) {
return (value ? setenv(name, value, 1) : unsetenv(name)) == 0;
}

View File

@@ -331,6 +331,10 @@ void plat_aligned_free(void *p);
// ---- process
_Noreturn void plat_exit(int code); // ends the process at once: other threads stop, no atexit handlers run
extern void (*plat_exit_hook)(void); // called first by plat_exit when set (main.c: the queued log lines)
// Ends the process at once with code, and nothing else: no exit hook, no stream flush, and no library's detach call
// (ExitProcess and _Exit run every one). For the ends of a failed run (dispatch.c fatal_end, vk_device.c
// lost_wait_exit), which have written their lines themselves and must not wait on a lock another thread holds.
_Noreturn void plat_terminate(int code);
int plat_setenv(const char *name, const char *value); // value NULL removes it; 1 on success
uint32_t plat_process_id(void);
// CPU time of the process since it started (user and kernel), and wall time since it started, in ns; 0 when unknown.

View File

@@ -1380,6 +1380,11 @@ void plat_exit(int code) {
ExitProcess((UINT)code); // not reached
}
void plat_terminate(int code) {
TerminateProcess(GetCurrentProcess(), (UINT)code);
ExitProcess((UINT)code); // not reached
}
// _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; }

View File

@@ -868,21 +868,28 @@ static void device_fault_report(void) {
// the run, and nothing else touches the device meanwhile: the paths that would read vk_lost and return, and a thread
// that gets the loss as a result of its own stops here for good.
// LOST_EXIT_MS bounds the end. The first thread writes the report and waits for a save commit (fs_host.c
// fs_save_crash_wait, 2 s at most); should it hang on a lock that a stopped thread holds, the watchdog ends the
// process with the same code, and the lines already written are in the log (rt_fatal_code wrote them itself).
// fs_save_crash_wait, 2 s at most); should it hang on a lock that a stopped thread holds, the watchdog (arg non-NULL)
// says so in one line and ends the process with the same code, and the lines already written are in the log
// (rt_fatal_code wrote them itself). The end is plat_terminate: ExitProcess would stop the watchdog too and then run
// every library's detach, and a driver's detach that hangs on a lost device would hold the process for good. A thread
// that stops here otherwise ends the process a little later, should the watchdog not have started.
#define LOST_EXIT_MS 10000
volatile int32_t vk_lost;
static void lost_wait_exit(void *arg) {
(void)arg;
for (int t = 0; t < LOST_EXIT_MS; t += 50) plat_sleep_ms(50);
_Exit(RT_LOST_EXIT);
int watchdog = arg != NULL;
for (int t = 0; t < LOST_EXIT_MS + (watchdog ? 0 : 2000); t += 50) plat_sleep_ms(50);
if (watchdog) {
rt_log_sync(); // the queue's locks only tried (bounded): their holder may be the thread that hangs
rt_log("vk: the end of the run did not finish in %d s; ending it", LOST_EXIT_MS / 1000);
}
plat_terminate(RT_LOST_EXIT);
}
void vk_device_lost_check(VkResult r, const char *where) {
if (r != VK_ERROR_DEVICE_LOST) return;
if (plat_xchg32(&vk_lost, 1)) lost_wait_exit(NULL); // another thread is ending the run: this one stops here
plat_thread_spawn(lost_wait_exit, NULL, "vk lost exit", 0);
plat_thread_spawn(lost_wait_exit, (void *)1, "vk lost exit", 0);
device_fault_report();
rt_fatal_code(RT_LOST_EXIT, "vk: the GPU device was lost (seen at: %s). The run ends here; the lines above name what "
"the GPU was doing", where);

62
tests/fatal_exit_check.c Normal file
View File

@@ -0,0 +1,62 @@
// 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;
}

109
tests/fatal_exit_check.py Normal file
View File

@@ -0,0 +1,109 @@
"""Build and run tests/fatal_exit_check.c: the end of a fatal error (runtime/dispatch.c fatal_end) with the platform
layer linked. The process ends with its own code and its FATAL line, and without the libraries' detach calls that
ExitProcess makes (a driver's detach that faulted there turned the end into a crash report with another code). Also
checks statically that no runtime file outside the platform layer ends the process with _Exit, and that the fatal
error's end and the lost device's (runtime/vk_device.c lost_wait_exit) end with plat_terminate. No game files, no GPU.
Usage: fatal_exit_check.py
"""
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RT = ROOT / 'runtime'
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
LAYOUT = '''// A stand-in for the generated layout.h (tests/fatal_exit_check.py).
#pragma once
#define MOD_MAIN_BASE 0x7000000000ull
#define MOD_MAIN_SIZE 0x4ea0000ull
'''
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def function(source, signature):
start = source.index(signature)
brace = source.index('{', start)
depth, end = 1, brace + 1
while depth:
depth += (source[end] == '{') - (source[end] == '}')
end += 1
return source[start:end]
def static_checks():
"""_Exit is ExitProcess in the C runtime: the platform layer alone ends the process (plat_exit, plat_terminate)."""
bad = 0
for path in sorted(RT.rglob('*')):
rel = path.relative_to(RT).as_posix()
if path.suffix not in ('.c', '.cpp', '.h', '.inc') or rel.startswith(('platform/', 'third_party/')):
continue
code = re.sub(r'//[^\n]*', '', path.read_text(encoding='utf-8', errors='replace'))
for m in re.finditer(r'\b_Exit\s*\(', code):
print(f'FAIL: runtime/{rel}:{code.count(chr(10), 0, m.start()) + 1} ends the process with _Exit '
'(ExitProcess: every library\'s detach runs); use plat_terminate or plat_exit')
bad += 1
for file, sig in (('dispatch.c', 'static void fatal_end(int code, const char *fmt, va_list ap) {'),
('vk_device.c', 'static void lost_wait_exit(void *arg)')):
body = re.sub(r'//[^\n]*', '', function((RT / file).read_text(encoding='utf-8'), sig))
if 'plat_terminate(' not in body:
print(f'FAIL: runtime/{file} {sig.split("(")[0].split()[-1]} does not end with plat_terminate')
bad += 1
print(f'static: no _Exit outside the platform layer, fatal_end and lost_wait_exit end with plat_terminate: '
f'{"FAILED" if bad else "ok"}', flush=True)
return bad
CASES = [
# mode, exit code, lines that must be there, lines that must not
('fatal', 70, ['FATAL: the test\'s fatal error 1'], ['detach:']),
]
def main():
bad = static_checks()
if os.name != 'nt':
print('fatal_exit_check: the program part runs on Windows only')
return 1 if bad else 0
work = ROOT / 'build' / 'test' / 'fatal_exit'
work.mkdir(parents=True, exist_ok=True)
(work / 'layout.h').write_text(LAYOUT)
exe = work / 'fatal_exit_check.exe'
env = dict(os.environ)
if MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
plat = [RT / 'platform' / 'win32.c', RT / 'platform' / 'common.c']
libs = ['-lbcrypt', '-lws2_32', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32']
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', '-fno-strict-aliasing',
'-march=x86-64-v3', '-I', str(RT), '-I', str(work), str(ROOT / 'tests' / 'fatal_exit_check.c'),
*map(str, plat), '-o', str(exe), '-lm', *libs], env=env)
for mode, code, want, unwanted in CASES:
p = subprocess.run([str(exe), mode], env=env, capture_output=True, text=True, errors='replace', timeout=120)
out = p.stdout + p.stderr
missing = [w for w in want if w not in out]
extra = [u for u in unwanted if u in out]
fatal_lines = out.count('FATAL:')
ok = p.returncode == code and not missing and not extra
print(f'{mode}: exit code {p.returncode} (want {code}), {fatal_lines} FATAL line(s)'
+ (f', missing {missing}' if missing else '') + (f', unexpected {extra}' if extra else '')
+ ('' if ok else ': FAIL'), flush=True)
if not ok:
print(out[-3000:])
bad += not ok
print(f'fatal_exit_check: {"FAILED" if bad else "ok"}')
return 1 if bad else 0
if __name__ == '__main__':
sys.exit(main())