From 4ffaf6ea9691b922e104665035da4d2a95f71597 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 23:26:33 -0500 Subject: [PATCH] Hash a texture's storage only while it is held mapped, at the upload and at the unchanged-bytes check. Both hashes read guest memory the game may unmap on its own thread; the unchanged-bytes check had no readability test at all. Co-Authored-By: Claude Fable 5.2 --- runtime/vk_texture.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index ff180206..5c0d7974 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -2269,7 +2269,15 @@ static void upload(Tex *e) { return; } e->chash_ok = e->t.L.size <= CHASH_MAX; // the hash of what this upload copies (make_fresh_snap compares it) - if (e->chash_ok) e->chash = content_hash((const void *)(uintptr_t)e->pub.addr, e->t.L.size); + if (e->chash_ok) { + // read with the storage held mapped, as the copies below: the game unmaps a texture's memory on its own thread + if (mem_read_hold(e->pub.addr, e->t.L.size)) { + e->chash = content_hash((const void *)(uintptr_t)e->pub.addr, e->t.L.size); + mem_read_release(); + } else { + e->chash_ok = 0; + } + } static __thread char ctx[160]; // what this thread copies, for a crash report snprintf(ctx, sizeof ctx, "texture upload %#llx (%llu bytes, fmt %#x %ux%u)", (unsigned long long)e->pub.addr, (unsigned long long)e->t.L.size, d->format, d->width, d->height); @@ -2490,7 +2498,10 @@ static int same_content(const Tex *e) { static int on = -1; // NX2NAVI_TEX_SAME=0: every write to the pages uploads again (diagnosis) if (on < 0) { const char *v = cfg_get("NX2NAVI_TEX_SAME"); on = !(v && !strcmp(v, "0")); } if (!on || !e->chash_ok || e->wt_n || e->t.L.size > CHASH_MAX) return 0; - if (content_hash((const void *)(uintptr_t)e->pub.addr, e->t.L.size) != e->chash) return 0; + if (!mem_read_hold(e->pub.addr, e->t.L.size)) return 0; // unmapped since: not the same; the upload deals with it + uint64_t h = content_hash((const void *)(uintptr_t)e->pub.addr, e->t.L.size); + mem_read_release(); + if (h != e->chash) return 0; ru_same_n++, ru_same_bytes += e->t.L.size; return 1; }