From a554a7c13c8eac3e998c06c2f5e1b0e070ba554e Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 31 Oct 2026 19:56:48 +0300 Subject: [PATCH 1/4] Take the virtual core gate out of the settings menu The row was labelled "Virtual core gate" with the text "Lets only one thread run per virtual core, as on the console", but switching it on wrote free_cores = 1, which lifts the gate. Without the gate guest threads run in parallel on one virtual core, and TotK keeps its per-core data without locks: an Arc B580 player who turned it on crashed at every start with "CallCommands: command handle ... is not a live recording" (#212). free_cores stays an ini key for experiments; the log now says when it is set, and totk.ini.example warns that the game can crash. Co-Authored-By: Claude Opus 5.5 --- runtime/kernel.c | 3 +++ runtime/ui/menu.cpp | 1 - runtime/ui_text.c | 4 ---- runtime/ui_text.h | 1 - totk.ini.example | 3 ++- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/runtime/kernel.c b/runtime/kernel.c index d879de9b..cd75797b 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -306,6 +306,9 @@ static void ap_init(void); static int cores_enabled(void) { if (g_cores_on < 0) { g_cores_on = !cfg_bool("TOTK_FREE_CORES"); // "0" keeps the gate, like unset + if (!g_cores_on) + rt_log("kernel: virtual core gate off (TOTK_FREE_CORES): guest threads run without the limit of one per " + "core that the game's per-core data needs; for experiments only, the game can fail"); diag_init(); ap_init(); } diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index b6c08720..ab351286 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -667,7 +667,6 @@ static void tab_advanced(float scale) { static const UiTextId preempt[] = {UI_MENU_PREEMPT_OFF, UI_MENU_PREEMPT_ON, UI_MENU_PREEMPT_INHERIT}; // 0|1|2 opt_choice("TOTK_ASYNC_PREEMPT", UI_MENU_ASYNC_PREEMPT, preempt, 3, UI_MENU_ASYNC_PREEMPT_DESC); opt_toggle("TOTK_REPLAY_ASYNC", UI_MENU_REPLAY_ASYNC, UI_MENU_REPLAY_ASYNC_DESC); - opt_toggle("TOTK_FREE_CORES", UI_MENU_FREE_CORES, UI_MENU_FREE_CORES_DESC); opt_toggle("TOTK_TIMER_1MS", UI_MENU_TIMER_1MS, UI_MENU_TIMER_1MS_DESC); opt_group(UI_MENU_GROUP_SHADERS); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index a8ac7155..90b5f107 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -178,8 +178,6 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_PREEMPT_OFF] = "Off", [UI_MENU_PREEMPT_ON] = "On", [UI_MENU_PREEMPT_INHERIT] = "Inheritance only", [UI_MENU_REPLAY_ASYNC] = "GPU frame prep on its own thread", [UI_MENU_REPLAY_ASYNC_DESC] = "Prepares frames for the GPU on a separate thread instead of the game thread. Adds up to one frame of latency.", - [UI_MENU_FREE_CORES] = "Virtual core gate", - [UI_MENU_FREE_CORES_DESC] = "Lets only one thread run per virtual core, as on the console.", [UI_MENU_TIMER_1MS] = "1 ms Windows timer", [UI_MENU_TIMER_1MS_DESC] = "Raises the system timer resolution to 1 ms for steadier frame pacing.", [UI_MENU_PIPE_WAIT] = "Waiting draw handling", @@ -427,8 +425,6 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_PREEMPT_OFF] = "Выкл.", [UI_MENU_PREEMPT_ON] = "Вкл.", [UI_MENU_PREEMPT_INHERIT] = "Только наследование", [UI_MENU_REPLAY_ASYNC] = "Подготовка кадра для GPU в отдельном потоке", [UI_MENU_REPLAY_ASYNC_DESC] = "Кадр для GPU готовится в отдельном потоке, а не в потоке игры. Добавляет до одного кадра задержки.", - [UI_MENU_FREE_CORES] = "Ограничение виртуальных ядер", - [UI_MENU_FREE_CORES_DESC] = "На каждом виртуальном ядре работает один поток, как на консоли.", [UI_MENU_TIMER_1MS] = "Таймер Windows 1 мс", [UI_MENU_TIMER_1MS_DESC] = "Повышает точность системного таймера до 1 мс для ровного темпа кадров.", [UI_MENU_PIPE_WAIT] = "Обработка ожидающих отрисовок", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index b9e95194..a50c8f69 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -108,7 +108,6 @@ typedef enum { UI_MENU_ASYNC_PREEMPT, UI_MENU_ASYNC_PREEMPT_DESC, UI_MENU_PREEMPT_OFF, UI_MENU_PREEMPT_ON, UI_MENU_PREEMPT_INHERIT, UI_MENU_REPLAY_ASYNC, UI_MENU_REPLAY_ASYNC_DESC, - UI_MENU_FREE_CORES, UI_MENU_FREE_CORES_DESC, UI_MENU_TIMER_1MS, UI_MENU_TIMER_1MS_DESC, UI_MENU_PIPE_WAIT, UI_MENU_PIPE_WAIT_DESC, UI_MENU_PIPE_WAIT_DEFAULT, UI_MENU_PIPE_WAIT_DRAW, UI_MENU_PIPE_SKIP, UI_MENU_PIPE_SKIP_DESC, UI_MENU_PIPE_SKIP_STRICT, UI_MENU_PIPE_SKIP_ANY, diff --git a/totk.ini.example b/totk.ini.example index 2b8c5d7d..55e0bdbf 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -26,7 +26,8 @@ # async_preempt = 1 # Replay NVN submissions on their own thread, at most one frame behind the game (1 = on). Adds up to a frame of latency. # replay_async = 0 -# 1 lifts the virtual core gate: guest threads run without the console's limit of one thread per core. +# 1 lifts the virtual core gate: guest threads run without the console's limit of one thread per core. For experiments +# only: the game keeps per-core data without locks and can crash. # free_cores = 0 # Windows timer resolution of 1 ms for steadier frame pacing (0 = the Windows default). # timer_1ms = 1 From 637b98dea7c01330fb234ee6ce9196655edad926 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 31 Oct 2026 19:56:57 +0300 Subject: [PATCH 2/4] Check a system font before the menu's font atlas takes it The menu merges the Japanese, Korean and Chinese language names from a system font. Dear ImGui asserts on a font stb_truetype cannot parse, and the assert ends the process with 0xc0000409 (exit code 127 in a shell) and no line in the log, a few seconds after the window opens: the report of an AMD laptop on Windows 11 Home Single Language (#176) matches that, and a test build of our ImGui with a damaged font exits the same way. A cut-off font passes the parse and faults later, when the glyphs are made, because stb_truetype reads tables without bounds checks. fonts_up now runs each system font through a private copy of stb_truetype whose asserts only mark the font bad, after checking that every table of its directory lies inside the file, and leaves out a font that fails with a log line. --probe-window checks that the test takes the embedded Roboto and refuses a cut-off copy, one without a character map and one with an unknown tag. Co-Authored-By: Claude Opus 5.5 --- runtime/gfx_window_probe.c | 1 + runtime/ui/menu.cpp | 55 ++++++++++++++++++++++++++++++++++++++ runtime/ui/menu.h | 3 +++ 3 files changed, 59 insertions(+) diff --git a/runtime/gfx_window_probe.c b/runtime/gfx_window_probe.c index 3ab56b64..0757171b 100644 --- a/runtime/gfx_window_probe.c +++ b/runtime/gfx_window_probe.c @@ -131,6 +131,7 @@ static int present_and_check(Source *s, int first, int i, int *w_out, int *h_out } int probe_window(void) { + if (!menu_font_check()) return fail("the menu's font check takes a damaged font or refuses a good one"); gfx_window_start(); VkCtx *c = vk_ctx(); if (!c) return fail("no Vulkan device"); diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index ab351286..abae5361 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -11,6 +11,7 @@ // 575ad88e02f9a89a9a885d90039d48af33c982f4, MIT, Copyright (c) 2026 Revivedx and the Savage Wind Recomp contributors, // Copyright (c) 2026 Peiikai); the pages are rows of option_rows.cpp. #include +#include #include #include #include @@ -19,6 +20,16 @@ #include "imgui.h" #include "imgui_internal.h" // SetFocusID, NavCursorVisible #include "backends/imgui_impl_vulkan.h" +// A private copy of stb_truetype, the parser Dear ImGui builds its font atlas with, to check a system font before the +// atlas gets it (font_usable). Its asserts only mark the font bad here. +static int g_font_bad; +#define STBTT_STATIC +#define STB_TRUETYPE_IMPLEMENTATION +#define STBTT_assert(x) ((x) ? (void)0 : (void)(g_font_bad = 1)) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-function" +#include "imstb_truetype.h" +#pragma clang diagnostic pop #include "ui/binds_page.h" #include "ui/option_rows.h" @@ -262,6 +273,43 @@ static const struct { const char *lang; UiTextId name; } kCjk[] = { {"zh-Hant", UI_MENU_LANGNAME_ZH_HANT}, }; +static uint32_t be32(const uint8_t *p) { return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; } + +// 1 when Dear ImGui can take this font file: more than the 100 bytes AddFontFromMemoryTTF asserts on, every table of its +// directory inside the file (stb_truetype reads tables without bounds checks, so a cut-off file faults when the glyphs +// are made), and stb_truetype parses it. ImGui asserts on a font it cannot parse, and an assert ends the process with +// 0xc0000409 and no line in the log. +static int font_usable(const uint8_t *d, uint64_t n) { + if (!d || n <= 100 || n > INT_MAX) return 0; + int off = stbtt_GetFontOffsetForIndex(d, 0); + if (off < 0 || (uint64_t)off + 12 > n) return 0; + uint32_t tables = (uint32_t)d[off + 4] << 8 | d[off + 5]; + if ((uint64_t)off + 12 + 16ull * tables > n) return 0; + for (uint32_t i = 0; i < tables; i++) { + const uint8_t *r = d + off + 12 + 16 * i; + if ((uint64_t)be32(r + 8) + be32(r + 12) > n) return 0; + } + stbtt_fontinfo fi; + g_font_bad = 0; + return stbtt_InitFont(&fi, d, off) && !g_font_bad; +} + +extern "C" int menu_font_check(void) { + uint64_t n = sizeof roboto_ttf; + uint8_t *bad = (uint8_t *)malloc(n); + if (!bad) return 0; + memcpy(bad, roboto_ttf, n); + uint32_t tables = (uint32_t)bad[4] << 8 | bad[5]; + for (uint32_t i = 0; i < tables; i++) + if (!memcmp(bad + 12 + 16 * i, "cmap", 4)) memset(bad + 12 + 16 * i, 0, 4); // no character map + int ok = font_usable(roboto_ttf, n) && !font_usable(roboto_ttf, 4096) && !font_usable(roboto_ttf, 100) && + !font_usable(bad, n); + memset(bad, 0, 4); // an unknown font tag + ok = ok && !font_usable(bad, n); + free(bad); + return ok; +} + static int fonts_up(float scale) { enum { N = sizeof kCjk / sizeof *kCjk }; ImFontAtlas *fa = ImGui::GetIO().Fonts; @@ -276,6 +324,13 @@ static int fonts_up(float scale) { char path[512]; uint64_t size = 0; if (!plat_font_path(kCjk[i].lang, path, sizeof path) || !(data[i] = plat_file_read(path, &size))) continue; + if (!font_usable(data[i], size)) { + rt_log("menu: system font %s left out: the font parser cannot read it (%llu bytes)", path, + (unsigned long long)size); + free(data[i]); + data[i] = NULL; + continue; + } merged++; ImFontGlyphRangesBuilder b; b.AddText(ui_text_in("en", kCjk[i].name)); diff --git a/runtime/ui/menu.h b/runtime/ui/menu.h index 0acbf389..ae24384b 100644 --- a/runtime/ui/menu.h +++ b/runtime/ui/menu.h @@ -53,6 +53,9 @@ int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const char **shot); // Records the frame menu_overlay_frame laid out into cb, which is inside dynamic rendering on the image. void menu_overlay_draw(VkCommandBuffer cb); void menu_vk_release(void); // before the device goes away (gfx_window_release_vk) +// The check the font atlas runs on a system font, on the embedded font and damaged copies of it: 1 when it takes the +// whole font and refuses a cut-off one, one without a character map and one with an unknown tag (--probe-window). +int menu_font_check(void); // ---- what the menu asks of the platform side (gfx_window_win.c, gfx_window_sdl.c) void gfx_window_apply_settings(void); // TOTK_FULLSCREEN or TOTK_WINDOW_SIZE changed: the window follows From e451ce6a0d8c6d8b9a65b15eb944203440d89914 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 31 Oct 2026 19:57:09 +0300 Subject: [PATCH 3/4] Report abort and invalid C runtime parameters as crashes on Windows abort() (a failed assert, std::terminate) and an invalid parameter to a C runtime function raise no exception: the C runtime ends the process with 0xc0000409 and the log stops without a FATAL line. A SIGABRT handler and an invalid-parameter handler now send both to the crash hook with the host return addresses in totk.exe, and the report lists them as totk+offset frames after the guest registers. Checked with win32.c in a test program: assert, abort and fclose(NULL) each give one report whose innermost frame is the failing call. lifted_function no longer names the nearest lifted function for an address above the lifted code, so a host frame of the runtime is not reported as game code. Co-Authored-By: Claude Opus 5.5 --- runtime/main.c | 12 +++++++++++- runtime/platform/platform.h | 6 +++++- runtime/platform/win32.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/runtime/main.c b/runtime/main.c index 893a2764..137bb264 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -183,7 +183,8 @@ static int by_host(const void *a, const void *b) { return x < y ? -1 : x > y; } -// The lifted function that contains a host code address, by the nearest function start below it. +// The lifted function that contains a host code address, by the nearest function start below it; 0 outside the lifted +// code. static uint64_t lifted_function(uintptr_t rip) { if (!fn_rev) { static RtModule mods[MAX_MODULES]; @@ -197,6 +198,9 @@ static uint64_t lifted_function(uintptr_t rip) { fn_rev[fn_rev_n++] = (FnRev){(uintptr_t)mods[i].fns[k], mods[i].addrs[k]}; qsort(fn_rev, fn_rev_n, sizeof *fn_rev, by_host); } + // The lifted code is one range with the host code outside it (kernel.c ap_init checks that); an address above the + // last function start is taken for host code, which costs only the last function's body. + if (!fn_rev_n || rip > fn_rev[fn_rev_n - 1].host) return 0; uint64_t lo = 0, hi = fn_rev_n; while (lo < hi) { uint64_t mid = (lo + hi) / 2; @@ -220,6 +224,12 @@ static void crash_handler(const PlatCrash *x) { rt_log_raw("\n", 1); uint64_t f = lifted_function(rip); if (f) rt_logf(" in lifted function f_%llx = %s\n", (unsigned long long)f, ldr_describe(f, d, sizeof d)); + for (int i = 0; i < x->nframes; i++) { // abort and invalid parameter: where the host code called it + uint64_t g = lifted_function(x->frames[i]); + rt_logf(" host frame %2d: totk+%#llx", i, (unsigned long long)(x->frames[i] - (rip - x->offset))); + if (g) rt_logf(" in lifted function f_%llx", (unsigned long long)g); + rt_log_raw("\n", 1); + } Cpu *c = kern_cpu(); if (c) { for (int i = 0; i < 31; i++) diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 5d2c9ef9..d5bf6da7 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -203,13 +203,17 @@ _Noreturn void plat_reexec(void); // A crash in this executable's own code (an access violation, an illegal instruction, a division by zero, a stack // overflow) goes to fn, which reports it and must end the process. Faults in other modules (drivers) are left to the -// OS. The write-fault hook above runs first. +// OS. The write-fault hook above runs first. On Windows abort() (a failed assert, std::terminate) and an invalid +// parameter to a C runtime function go there too, with the host frames in this executable: the C runtime ends the +// process for both with 0xc0000409 and no exception. typedef struct { const char *what; // "access violation", "illegal instruction", ... uint32_t code; // the Windows exception code or the signal number uintptr_t rip, offset; // the faulting instruction, and its offset in the executable int access; // the data access that faulted: 0 read, 1 write, 8 execute; -1 when not an access uint64_t addr; // its address when access >= 0 + int nframes; // abort and invalid parameter: return addresses in this executable, innermost first + uintptr_t frames[16]; // (rip is frames[0]); 0 for a fault, whose rip is exact } PlatCrash; void plat_crash_hook(void (*fn)(const PlatCrash *c)); int plat_mem_read(uint64_t addr, void *out, size_t n); // copies n bytes, 0 instead of faulting on unmapped memory diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 7a65869d..0856db7d 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -582,9 +583,37 @@ static LONG CALLBACK crash_veh(EXCEPTION_POINTERS *ep) { return EXCEPTION_CONTINUE_SEARCH; } +// abort() and an invalid parameter to a C runtime function raise no exception: the C runtime ends the process with +// 0xc0000409 after calling the SIGABRT or invalid-parameter handler. The report names the host frames in this +// executable; the innermost is the caller of abort (assert) or of the C runtime function. Skipped frames: this function +// and the handler that called it. +__attribute__((noinline)) static void crash_from_runtime(const char *what, uint32_t code) { + PlatCrash c = {what, code, 0, 0, -1, 0}; + void *f[62]; + USHORT n = RtlCaptureStackBackTrace(2, 62, f, NULL); + MODULEINFO mi; + if (GetModuleInformation(GetCurrentProcess(), GetModuleHandleW(NULL), &mi, sizeof mi)) { + uintptr_t base = (uintptr_t)mi.lpBaseOfDll; + for (USHORT i = 0; i < n && c.nframes < (int)(sizeof c.frames / sizeof *c.frames); i++) + if ((uintptr_t)f[i] - base < mi.SizeOfImage) c.frames[c.nframes++] = (uintptr_t)f[i]; + if (c.nframes) c.rip = c.frames[0], c.offset = c.rip - base; + } + crash_fn(&c); +} + +static void abort_signal(int sig) { crash_from_runtime("abort", (uint32_t)sig); } + +static void invalid_parameter(const wchar_t *expr, const wchar_t *func, const wchar_t *file, unsigned line, + uintptr_t reserved) { + (void)expr, (void)func, (void)file, (void)line, (void)reserved; // NULL and 0 in the release C runtime + crash_from_runtime("invalid parameter to a C runtime function", 0xc0000417u); // STATUS_INVALID_CRUNTIME_PARAMETER +} + void plat_crash_hook(void (*fn)(const PlatCrash *c)) { crash_fn = fn; AddVectoredExceptionHandler(0, crash_veh); + signal(SIGABRT, abort_signal); + _set_invalid_parameter_handler(invalid_parameter); } // A fresh process gets a fresh layout: Windows places DLLs, heaps and thread stacks at random addresses. The parent From e84c61f16dc7c0e0800d59b2417620b2cb71f9b2 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 31 Oct 2026 19:57:09 +0300 Subject: [PATCH 4/4] Say why a command recording buffer could not grow A player's game ended with "nvn: command recording buffer of 1048576 bytes" during a dive into the Depths (#94): realloc refused 1 MiB with commit to spare. That leaves a buffer pointer that is no block of ours, from a command buffer object the game overwrote or initialized again without Finalize. The fatal line now gives the object, its magic, the old buffer and its size, the bytes used and asked, and Initialize logs (eight times at most) when it replaces an object that was not finalized. Co-Authored-By: Claude Opus 5.5 --- runtime/nvn_api_cmd.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/runtime/nvn_api_cmd.c b/runtime/nvn_api_cmd.c index 157af5f1..9494e259 100644 --- a/runtime/nvn_api_cmd.c +++ b/runtime/nvn_api_cmd.c @@ -82,7 +82,12 @@ static void cmd_reserve(cmd_t *o, size_t need) { if (!cap) cap = 64 << 10; while (used + need + OP_RESERVE > cap) cap *= 2; uint8_t *b = realloc(o->seg, cap); - if (!b) rt_fatal("nvn: command recording buffer of %zu bytes", cap); + // A block this size fails only without commit (the fatal report states it) or when seg is no block of ours: a + // command buffer object the game overwrote or copied shows a wrong magic or more used than its old size. + if (!b) + rt_fatal("nvn: command recording buffer of %zu bytes (command buffer %p, magic %#x, buffer %p of %zu bytes, %zu " + "used, %zu asked)", cap, (void *)o, o->magic, (void *)o->seg, o->seg ? (size_t)(o->end - o->seg) : 0, + used, need); o->seg = b; o->cur = b + used; o->end = b + cap; @@ -312,6 +317,12 @@ static void release_copies(uintptr_t lo, uintptr_t hi) { // at ~0 / 0 so NVN 55 inline code never finds room and calls the API (nvn_obj.h cmd_t) NVN_API(nvnCommandBufferInitialize) { cmd_t *o = cmd_of(c); + // Initialize over an object that was never finalized drops its recording buffer (seg lives in host memory). Logged + // only: the old bytes may be a copy whose buffer another object owns, so freeing it here is not safe. + static uint32_t reinits; + if (o->magic == CMD_MAGIC && o->seg && __atomic_fetch_add(&reinits, 1, __ATOMIC_RELAXED) < 8) + rt_log("nvn: Initialize over the command buffer %p, which was not finalized; its %zu-byte recording buffer %p " + "is dropped", (void *)o, (size_t)(o->end - o->seg), (void *)o->seg); memset(o, 0, sizeof *o); o->magic = CMD_MAGIC; o->dev = arg_p(c, 1);