Merge the stability and memory pass into the 0.10.0 line.

Conflicts: the settings key lists and section maps take both sides' entries.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-12 21:49:17 -05:00
39 changed files with 2129 additions and 153 deletions

61
tests/boot_warm_check.c Normal file
View File

@@ -0,0 +1,61 @@
// runtime/boot_warm.h: whether a start does the whole warm on the boot screen (runtime/vk_pipeline.c bf_decide).
// Built and run by tests/boot_warm_check.py; prints each failed check and exits 1 on any.
#include <stdio.h>
#include "boot_warm.h"
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
int main(void) {
const char *key = "1002:15bf:00806001", *other = "1002:15bf:00805ffe";
char text[128];
int tries = -1;
// An ordinary start: no file, the pipeline cache is this driver's.
CHECK(boot_warm_decide(NULL, key, 0, &tries) == BOOT_WARM_NO && tries == 0, "a warm without a driver change");
// The first start after a driver update: the warm, and the file gets 1.
CHECK(boot_warm_decide(NULL, key, 1, &tries) == BOOT_WARM_YES && tries == 1, "no warm after a driver change");
// That start was closed during the warm. The next one goes on with it and counts up, whether the pipeline cache's
// file still has the earlier driver's header or not.
for (int n = 1; n < BOOT_WARM_TRIES; n++) {
snprintf(text, sizeof text, "%s %d\n", key, n);
CHECK(boot_warm_decide(text, key, 0, &tries) == BOOT_WARM_YES && tries == n + 1, "an owed warm not taken up at %d", n);
CHECK(boot_warm_decide(text, key, 1, &tries) == BOOT_WARM_YES && tries == n + 1, "the count lost at %d", n);
}
// BOOT_WARM_TRIES starts began it and none ended it: no more, with or without a foreign cache, and nothing to write.
snprintf(text, sizeof text, "%s %d\n", key, BOOT_WARM_TRIES);
CHECK(boot_warm_decide(text, key, 0, &tries) == BOOT_WARM_GAVE_UP && tries == 0, "a fourth try");
CHECK(boot_warm_decide(text, key, 1, &tries) == BOOT_WARM_GAVE_UP && tries == 0, "a fourth try on a foreign cache");
// The warm ended. The cache file keeps the earlier driver's header until the play cache is folded in: those
// starts are ordinary ones, and so are the ones after.
snprintf(text, sizeof text, "%s done\n", key);
CHECK(boot_warm_decide(text, key, 1, &tries) == BOOT_WARM_DONE && tries == 0, "a second warm on the same driver");
CHECK(boot_warm_decide(text, key, 0, &tries) == BOOT_WARM_DONE && tries == 0, "a done file read as stale");
// The file of an earlier driver: stale without a foreign cache, a fresh count with one, whether the earlier
// driver's warm was done or given up.
snprintf(text, sizeof text, "%s %d\n", other, BOOT_WARM_TRIES);
CHECK(boot_warm_decide(text, key, 0, &tries) == BOOT_WARM_NO, "another driver's file taken for this one's");
CHECK(boot_warm_decide(text, key, 1, &tries) == BOOT_WARM_YES && tries == 1, "another driver's count carried over");
snprintf(text, sizeof text, "%s done\n", other);
CHECK(boot_warm_decide(text, key, 0, &tries) == BOOT_WARM_NO, "another driver's done taken for this one's");
CHECK(boot_warm_decide(text, key, 1, &tries) == BOOT_WARM_YES && tries == 1, "another driver's done stops this one's warm");
// A file that is not one: as without it.
CHECK(boot_warm_decide("", key, 0, &tries) == BOOT_WARM_NO, "an empty file owes a warm");
CHECK(boot_warm_decide("garbage", key, 1, &tries) == BOOT_WARM_YES && tries == 1, "a broken file stops the warm");
snprintf(text, sizeof text, "%s -4\n", key);
CHECK(boot_warm_decide(text, key, 0, &tries) == BOOT_WARM_NO, "a negative count owes a warm");
snprintf(text, sizeof text, "%s x\n", key);
CHECK(boot_warm_decide(text, key, 0, &tries) == BOOT_WARM_NO, "a count that is no number owes a warm");
snprintf(text, sizeof text, "%s 2x\n", key);
CHECK(boot_warm_decide(text, key, 0, &tries) == BOOT_WARM_NO, "a count with a tail owes a warm");
printf("boot_warm_check: %s\n", fails ? "FAILED" : "passed");
return fails ? 1 : 0;
}

40
tests/boot_warm_check.py Normal file
View File

@@ -0,0 +1,40 @@
"""Build and run tests/boot_warm_check.c: runtime/boot_warm.h, whether a start does the whole warm on the boot screen
after a driver change (the decision alone; no GPU, no game files).
Usage: boot_warm_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests' / 'diff'))
try:
from common import MSYS_BIN, compiler # noqa: E402
except ImportError: # a Python without the differential tests' packages: the same compiler choice without them
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
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 main():
work = ROOT / 'build' / 'test' / 'boot_warm'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('boot_warm_check.exe' if os.name == 'nt' else 'boot_warm_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
subprocess.check_call([compiler(), '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'boot_warm_check.c'), '-o', str(exe)], env=env)
return subprocess.run([str(exe)], env=env).returncode
if __name__ == '__main__':
sys.exit(main())

63
tests/exit_fault_check.py Normal file
View File

@@ -0,0 +1,63 @@
"""Run the game executable's crash probes and check what a fault does to the way the process ends (runtime/main.c
crash_handler, mode_done).
- --probe-crash-call: a fault inside a library while the mode still runs is a crash: a FATAL line that names the
library, exit code 71.
- --probe-crash-exit: the same fault after the mode has returned, while the process is ending (an atexit handler here;
on a player's PC the detach of a library another program put into the process). The work is done by then, so the
process ends with the mode's own code, 3, and one "exit:" line, with no FATAL.
No game files, no GPU, no window.
Usage: exit_fault_check.py PATH_TO_GAME_EXE
"""
import subprocess
import sys
CRASH_EXIT = 71
PROBE_EXIT = 3
def run(exe, mode):
p = subprocess.run([exe, mode], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, errors='replace',
timeout=120)
return p.returncode, p.stdout.splitlines()
def main():
if len(sys.argv) != 2:
print(__doc__)
return 2
fails = []
def check(ok, what):
if not ok:
fails.append(what)
code, lines = run(sys.argv[1], '--probe-crash-call')
fatal = [ln for ln in lines if ln.startswith('FATAL: ')]
check(code == CRASH_EXIT, '--probe-crash-call: exit code %d, expected %d' % (code, CRASH_EXIT))
check(len(fatal) == 1 and 'access violation' in fatal[0], '--probe-crash-call: %d FATAL lines, expected one: %s'
% (len(fatal), fatal))
check(not any('exit: ' in ln and 'while the process was ending' in ln for ln in lines),
'--probe-crash-call: a fault inside the mode was taken for one at the exit')
code, lines = run(sys.argv[1], '--probe-crash-exit')
fatal = [ln for ln in lines if ln.startswith('FATAL: ')]
said = [ln for ln in lines if 'exit: ' in ln and 'while the process was ending' in ln]
check(any(ln.startswith('probe-crash-exit: returning') for ln in lines), '--probe-crash-exit: the mode did not run')
check(code == PROBE_EXIT, '--probe-crash-exit: exit code %d, expected the mode\'s own %d' % (code, PROBE_EXIT))
check(not fatal, '--probe-crash-exit: a FATAL line for a fault after the work was done: %s' % fatal)
check(len(said) == 1 and 'with its own code %d' % PROBE_EXIT in said[0],
'--probe-crash-exit: %d "exit:" lines, expected one that names the code: %s' % (len(said), said))
for f in fails:
print('exit_fault_check: FAIL:', f)
if not fails:
print('exit_fault_check: passed (a fault in the mode: FATAL and %d; a fault at the exit: the mode\'s code %d)'
% (CRASH_EXIT, PROBE_EXIT))
return 1 if fails else 0
if __name__ == '__main__':
sys.exit(main())

View File

@@ -125,6 +125,29 @@ int main(void) {
CHECK(read_through(a0 + 4096, 1) == dev_of(b0) + 4096, "a plain page is its own twin");
gpu_done();
// a new mirror takes the writes first and the reads once its first copy has run (vk_memory.c mirror_create,
// gpa_switch): until then the pages read the imported buffer and the mirror is their twin
VkBuffer m1 = new_buf();
uint32_t w0 = vk_gpa_remap_writes(a0, a1, r0, m1, a0);
CHECK(w0 && w0 != r0, "a new mirror gets a record for the writes");
expect(a0, a1, b0, a0, "a new mirror before its first copy: reads stay on the imported buffer");
CHECK(read_through(a0 + 4096, 1) == dev_of(m1) + 4096 && read_through(a1 - 4096, 1) == dev_of(m1) + (a1 - 4096 - a0),
"a new mirror before its first copy is the twin, which takes the writes");
gpu_done();
r0 = vk_gpa_remap(a0, a1, w0, m1, a0);
expect(a0, a1, m1, a0, "a new mirror after its first copy: reads move to it");
CHECK(read_through(a0 + 4096, 1) == dev_of(b0) + 4096, "and the imported buffer is the twin again");
// evicted while it still waits for its first copy: the pages go back to the imported buffer alone
r0 = vk_gpa_remap_plain(a0, a1, r0, b0, a0);
gpu_done();
VkBuffer m2 = new_buf();
w0 = vk_gpa_remap_writes(a0, a1, r0, m2, a0);
r0 = vk_gpa_remap_plain(a0, a1, w0, b0, a0);
expect(a0, a1, b0, a0, "a mirror gone before its first copy");
CHECK(read_through(a0 + 4096, 1) == dev_of(b0) + 4096, "leaves the imported buffer as its own twin");
CHECK(!vk_gpa_remap_writes(a0, a1, 0, m2, a0), "a pool without a record gets none for a mirror's writes");
gpu_done();
// an unmapped range reads the zero buffer at once, and its blocks serve no other range while the GPU may still read
// the old L1 entries
uint32_t old_blk[3] = {block_of(a0), block_of(a0 + 16 * MiB), block_of(a0 + 32 * MiB)};

View File

@@ -0,0 +1,111 @@
// Guest memory as a memfd's (runtime/platform/linux.c, NX2NAVI_DMABUF): a dma-buf of a watched range maps the very
// pages the CPU writes; a decommit inside it zeroes them in place (the dma-buf still sees the CPU's pages), one outside
// it reads zero when committed again; the write watch keeps working. No GPU, no game files. Build and run from the
// repository root:
// clang -O2 -ffunction-sections -fdata-sections -Iruntime -c runtime/platform/linux.c -o /tmp/plat.o
// clang -O2 -Wall -Iruntime tests/platform_dmabuf_test.c /tmp/plat.o -Wl,--gc-sections -lpthread -ldl -o /tmp/dmabuf
// /tmp/dmabuf (exit 77: no /dev/udmabuf or no write watch here)
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "platform/platform.h"
#define MiB (1ull << 20)
static uint64_t memfd_bytes(void) { // the guest memfd's allocated bytes
DIR *dir = opendir("/proc/self/fd");
struct dirent *e;
uint64_t got = 0;
while (dir && (e = readdir(dir))) {
char path[64], link[128];
snprintf(path, sizeof path, "/proc/self/fd/%s", e->d_name);
ssize_t k = readlink(path, link, sizeof link - 1);
struct stat st;
if (k > 0 && (link[k] = 0, strstr(link, "memfd:nx2navi-guest")) && !stat(path, &st)) got = (uint64_t)st.st_blocks * 512;
}
if (dir) closedir(dir);
return got;
}
int main(void) {
const uint64_t a = 0x6100000000ull, n = 64 * MiB;
uint64_t pages[16], limit = 0, any = 0;
const char *why = "";
setenv("NX2NAVI_DMABUF", "1", 0);
assert(plat_mem_reserve(a, n, 1));
if (!plat_mem_dmabuf_ok(&limit, &any, &why)) { printf("SKIP: guest memory is not a memfd's here: %s\n", why); return 77; }
assert(any == a);
assert(plat_mem_commit(a, 16 * MiB));
memset((void *)(uintptr_t)a, 0x11, 16 * MiB);
for (uint64_t k = 16; k == 16;) // read and reset until a call has fewer than it could take
if ((k = plat_mem_written(a, n, pages, 16, 1)) == UINT64_MAX) { puts("SKIP: asynchronous write watch unavailable"); return 77; }
assert(plat_mem_written(a, n, pages, 16, 0) == 0);
// a dma-buf of the first 8 MiB maps the guest's own pages
int fd = plat_mem_dmabuf(a, 8 * MiB);
assert(fd >= 0);
volatile uint8_t *d = mmap(NULL, 8 * MiB, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
assert(d != MAP_FAILED);
assert(d[0] == 0x11 && d[8 * MiB - 1] == 0x11);
((volatile uint8_t *)(uintptr_t)a)[4096 + 5] = 0x22;
assert(d[4096 + 5] == 0x22);
d[3 * 4096] = 0x33; // as a GPU write: the guest sees it, the write watch does not
assert(((volatile uint8_t *)(uintptr_t)a)[3 * 4096] == 0x33);
assert(plat_mem_written(a, n, pages, 16, 1) == 1 && pages[0] == a + 4096);
// protection changes (a freeze, an armed lazy range) leave the dma-buf alone
assert(plat_mem_protect(a, 2 * MiB, PLAT_PROT_READ));
assert(d[4096 + 5] == 0x22);
assert(plat_mem_protect(a, 2 * MiB, PLAT_PROT_RW));
// a decommit inside the dma-buf: zero in place, the same pages for both
assert(plat_mem_decommit(a + 4096, 4096));
assert(plat_mem_commit(a + 4096, 4096));
assert(((volatile uint8_t *)(uintptr_t)a)[4096 + 5] == 0 && d[4096 + 5] == 0);
((volatile uint8_t *)(uintptr_t)a)[4096 + 6] = 0x44;
assert(d[4096 + 6] == 0x44);
// a decommit that straddles the dma-buf's end: zero both sides when committed again
assert(plat_mem_decommit(a + 8 * MiB - 4096, 2 * 4096));
assert(plat_mem_commit(a + 8 * MiB - 4096, 2 * 4096));
assert(((volatile uint8_t *)(uintptr_t)(a + 8 * MiB - 1))[0] == 0 && d[8 * MiB - 1] == 0);
assert(((volatile uint8_t *)(uintptr_t)(a + 8 * MiB))[0] == 0);
// a decommit outside any dma-buf: the pages leave the memfd and read zero
assert(plat_mem_decommit(a + 12 * MiB, MiB));
assert(plat_mem_commit(a + 12 * MiB, MiB));
assert(((volatile uint8_t *)(uintptr_t)(a + 12 * MiB))[77] == 0);
// a dma-buf over pages nothing committed yet allocates them; when it is gone they go back, the committed ones stay
uint64_t before = memfd_bytes();
int fd2 = plat_mem_dmabuf(a + 32 * MiB, 8 * MiB);
assert(fd2 >= 0);
uint64_t pinned = memfd_bytes();
close(fd2);
plat_mem_dmabuf_done(a + 32 * MiB, 8 * MiB);
uint64_t after = memfd_bytes();
printf("memfd: %.1f MiB, %.1f MiB with an 8 MiB dma-buf over uncommitted pages, %.1f MiB after it\n", before / 1048576.0,
pinned / 1048576.0, after / 1048576.0);
assert(pinned >= before + 8 * MiB && after == before);
assert(plat_mem_commit(a + 32 * MiB, 4096));
assert(((volatile uint8_t *)(uintptr_t)(a + 32 * MiB))[9] == 0);
munmap((void *)d, 8 * MiB);
close(fd);
plat_mem_dmabuf_done(a, 8 * MiB);
assert(((volatile uint8_t *)(uintptr_t)a)[4096 + 6] == 0x44 && ((volatile uint8_t *)(uintptr_t)a)[2 * MiB] == 0x11);
// outside the reservation, or not whole pages: no dma-buf
assert(plat_mem_dmabuf(a + n - 4096, 2 * 4096) < 0);
assert(plat_mem_dmabuf(a + 1, 4096) < 0);
*(volatile uint8_t *)(uintptr_t)(a + 5 * MiB) = 1;
assert(plat_mem_written(a, n, pages, 16, 1) >= 1);
puts("dma-buf of guest memory: same pages both ways, decommits zero in place inside and punch outside, write watch kept");
return 0;
}

View File

@@ -12,7 +12,8 @@ static int fails, wake_all;
// ---- the runtime kernel.c reaches
unsigned rt_trace;
uint64_t rt_guest_start_ns;
volatile uint32_t prof_replay_tid;
volatile uint32_t prof_replay_tid, prof_main_tid;
int mem_readable(uint64_t a, uint64_t n) { (void)a, (void)n; return 0; } // NX2NAVI_SVC_WAITS walks guest frames: off here
const char *cfg_get(const char *name) { return !strcmp(name, "NX2NAVI_SYNC_WAKE_ALL") && wake_all ? "1" : NULL; }
const char *cfg_value(const char *name) { (void)name; return "0"; } // no asynchronous preemption, no slow-service log
int cfg_bool(const char *name) { (void)name; return 0; }

91
tests/thread_end_check.c Normal file
View File

@@ -0,0 +1,91 @@
// runtime/platform: what a thread keeps for itself is given back when its function returns (win32.c thread_main,
// platform.h plat_thread_end_hook). Threads that sleep through plat_wait_ns and read a file are made and ended by the
// hundred, joined and detached; the end hook must run once on each, and on Windows the process must not hold more
// handles afterwards than a few threads' worth. Built and run by tests/thread_end_check.py.
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "platform/platform.h"
#ifdef _WIN32
#include <windows.h>
static long handles(void) {
DWORD n = 0;
return GetProcessHandleCount(GetCurrentProcess(), &n) ? (long)n : -1;
}
#else
static long handles(void) { return 0; } // no handle per thread there; the hook is what is checked
#endif
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
void rt_log(const char *fmt, ...) { // the platform layer's lines
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
static const char *path;
static volatile int32_t ended, hook_runs, hook_other;
static __thread int worked;
static void hook(void) {
if (worked) plat_inc32(&hook_runs);
else plat_inc32(&hook_other);
worked = 0;
}
static void worker(void *arg) {
(void)arg;
worked = 1;
plat_wait_ns(1200000); // over a millisecond: the thread's own timer
PlatFile *f = plat_file_open(path);
char b[16];
if (!f || plat_file_pread(f, b, sizeof b, 0) != (int64_t)sizeof b) plat_inc32(&hook_other); // counted as a failure
if (f) plat_file_close(f);
plat_inc32(&ended);
}
static void wait_ended(int32_t want) {
for (int i = 0; i < 20000 && __atomic_load_n(&ended, __ATOMIC_ACQUIRE) < want; i++) plat_sleep_ms(1);
plat_sleep_ms(100); // the last ones leave their start function and the system lets go of them
}
int main(int argc, char **argv) {
if (argc != 2) {
printf("usage: thread_end_check FILE_OF_16_BYTES_OR_MORE\n");
return 2;
}
path = argv[1];
plat_thread_end_hook(hook);
enum { WARM = 16, ROUNDS = 25, PER = 16 };
for (int i = 0; i < WARM; i++) plat_thread_spawn(worker, NULL, "warm", 0);
wait_ended(WARM);
long h0 = handles();
int32_t made = WARM;
for (int r = 0; r < ROUNDS; r++) {
PlatThread *t[PER / 2];
for (int i = 0; i < PER / 2; i++) {
t[i] = plat_thread_new(worker, NULL, "joined");
made += t[i] != NULL;
made += plat_thread_spawn(worker, NULL, "detached", 0);
}
for (int i = 0; i < PER / 2; i++)
if (t[i]) plat_thread_join(t[i]);
wait_ended(made);
}
long h1 = handles();
CHECK(made == WARM + ROUNDS * PER, "%d threads made of %d", (int)made, WARM + ROUNDS * PER);
CHECK(__atomic_load_n(&ended, __ATOMIC_ACQUIRE) == made, "%d of %d threads ended", (int)ended, (int)made);
CHECK(hook_runs == made, "the end hook ran on %d of %d threads", (int)hook_runs, (int)made);
CHECK(!hook_other, "%d threads could not read the file, or ran the hook without having worked", (int)hook_other);
// each thread made a timer and a read event: left open, 400 threads would hold 800 more handles
CHECK(h1 - h0 < 64, "%ld handles more after %d ended threads (%ld before, %ld after)", h1 - h0, ROUNDS * PER, h0, h1);
printf("thread_end_check: %s (%d threads, the hook ran on %d, handles %ld before and %ld after)\n",
fails ? "FAILED" : "ok", (int)made, (int)hook_runs, h0, h1);
return fails ? 1 : 0;
}

48
tests/thread_end_check.py Normal file
View File

@@ -0,0 +1,48 @@
"""Build and run tests/thread_end_check.c: what a thread keeps for itself is given back when it ends
(runtime/platform/win32.c thread_main, platform.h plat_thread_end_hook). 416 threads sleep, read a file and end, half
joined and half detached; the end hook must run once on each, and on Windows the process's handle count must be what
it was. No game files, no GPU.
Usage: thread_end_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
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 main():
work = ROOT / 'build' / 'test' / 'thread_end'
work.mkdir(parents=True, exist_ok=True)
win = os.name == 'nt'
exe = work / ('thread_end_check.exe' if win else 'thread_end_check')
env = dict(os.environ)
if win and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
rt = ROOT / 'runtime'
plat = [rt / 'platform' / ('win32.c' if win else 'linux.c'), rt / 'platform' / 'common.c']
libs = ['-lbcrypt', '-lws2_32', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] if win \
else ['-lpthread']
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', '-fno-strict-aliasing',
'-march=x86-64-v3', '-I', str(rt),
str(ROOT / 'tests' / 'thread_end_check.c'), *map(str, plat), '-o', str(exe), '-lm', *libs],
env=env)
data = work / 'read.bin'
data.write_bytes(bytes(range(64)))
return subprocess.run([str(exe), str(data)], env=env, timeout=300).returncode
if __name__ == '__main__':
sys.exit(main())