From 345b393acc5d732c6b07535c036ff7d0080e658f Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 22:25:01 -0500 Subject: [PATCH] Give a texture handle asked for with an id out of range the reserved slot 0, inside the pool arrays. The handle named slot 1,048,575, past the 32,768 entries of the pool arrays shaders index. A draw that binds the handle resolved it to the stand-in, but a handle the game stores in a uniform buffer would have indexed those arrays out of bounds on the GPU. Slot 0 is one of the 256 slots the device reports as reserved, which no game registers into: it holds the stand-in in the arrays, and a bound handle of 0 is "no texture" already. Co-Authored-By: Claude Fable 5.2 --- runtime/nvn_api_core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/runtime/nvn_api_core.c b/runtime/nvn_api_core.c index caad7a04..74f17a4a 100644 --- a/runtime/nvn_api_core.c +++ b/runtime/nvn_api_core.c @@ -236,17 +236,20 @@ NVN_API(nvnDeviceRegisterFastClearDepth) { ret_x(c, 0); } NVN_API(nvnDeviceGetImageHandle) { ret_x(c, (uint64_t)(int64_t)(int)arg_x(c, 1)); } // NVNtextureHandle nvnDeviceGetTextureHandle(const NVNdevice *d, int texture_id, int sampler_id): // texture id (20 bits) | sampler id << 20, the encoding the replay decodes (NVN_HANDLE_TEX / NVN_HANDLE_SAMP) -// An id out of range (-1: the game had no texture to give, as when its render-target heap is full) gets the handle of -// the last texture slot, which no game registers (its pool has 32,768), so a draw through it samples the stand-in as -// for any slot without a texture, and sampler 0. Ending the game here turned one missing texture into a crash. +// An id out of range (-1: the game had no texture to give, as when its render-target heap is full) gets texture +// slot 0 and sampler slot 0: the first 256 slots of both pools are reserved (nvnDeviceGetInteger 0x11 and 0x12 above) +// and no game registers into them, so slot 0 holds the stand-in. That holds for a draw that binds the handle, where +// handle 0 is "no texture" (vk_exec_draw.c), and for a shader that takes the handle out of a uniform buffer and +// indexes the pool arrays with it: slot 0 is inside their VK_POOL_TEX entries, which a slot past them is not. +// Ending the game here turned one missing texture into a crash. NVN_API(nvnDeviceGetTextureHandle) { int tid = (int)arg_x(c, 1), sid = (int)arg_x(c, 2); if (tid < 0 || tid >= (1 << 20) || sid < 0 || sid >= (1 << 12)) { static int said; if (said < 8 && ++said) - rt_log("nvn: texture handle asked for with ids out of range (texture %d, sampler %d): the handle of an " - "empty slot is given, which draws the stand-in", tid, sid); - if (tid < 0 || tid >= (1 << 20)) tid = (1 << 20) - 1; + rt_log("nvn: texture handle asked for with ids out of range (texture %d, sampler %d): the handle of the " + "reserved slot 0 is given, which draws the stand-in", tid, sid); + if (tid < 0 || tid >= (1 << 20)) tid = 0; if (sid < 0 || sid >= (1 << 12)) sid = 0; } ret_x(c, (uint64_t)tid | (uint64_t)sid << 20);