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