From 637b98dea7c01330fb234ee6ce9196655edad926 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 31 Oct 2026 19:56:57 +0300 Subject: [PATCH] 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