From 9296bf0a0425f3d2ef82dcd9951750d6255606d4 Mon Sep 17 00:00:00 2001 From: Ren Date: Mon, 27 Jul 2026 16:20:01 +0200 Subject: [PATCH] Savestate: don't let stale HLE helper threads mutate restored kernel state On savestate load, two sites delete a pre-load host object that owns an HLEHelperThread without calling Forget() first, so ~HLEHelperThread runs __KernelDeleteThread and kernelMemory.Free() with thread ids and block addresses from before the load, against the freshly restored kernel state: - sceUtility: Do(p, accessThread) deletes the stale accessThread inside DoClass before recreating it from the stream. - scePsmf: Do(p, psmfPlayerMap) deletes every existing PsmfPlayer, and ~PsmfPlayer -> AbortFinish() deletes its finishThread raw. When the stale id or block happens to be absent from the restored state this only logs errors ("... does not exist" / "BlockAllocator: invalid free"). When it has been recycled, a live thread is terminated or a live allocation is freed, silently corrupting the loaded state. Easiest to hit by loading a state while a savedata operation or PSMF player is active, into a session where those ids were reused. __IoDoState and __PsmfShutdown already Forget() before deleting; do the same at these two sites. Worst case behavior change is a leaked kernel-side thread record where one was previously (incorrectly) freed. --- Core/HLE/scePsmf.cpp | 12 ++++++++++++ Core/HLE/sceUtility.cpp | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/Core/HLE/scePsmf.cpp b/Core/HLE/scePsmf.cpp index 7a9474d1a0..a8f3bf48b6 100644 --- a/Core/HLE/scePsmf.cpp +++ b/Core/HLE/scePsmf.cpp @@ -701,6 +701,18 @@ void __PsmfPlayerDoState(PointerWrap &p) { if (!s) return; + if (p.mode == p.MODE_READ) { + // The map serializer deletes every existing host PsmfPlayer before + // loading. ~PsmfPlayer -> AbortFinish() deletes its helper thread + // without Forget(), mutating the freshly restored kernel state with + // stale ids/blocks from before the load (see the matching fix in + // __UtilityDoState). Forget the helpers first; the kernel-side + // objects belong to the restored state, not to these host wrappers. + for (auto &it : psmfPlayerMap) { + if (it.second && it.second->finishThread) + it.second->finishThread->Forget(); + } + } Do(p, psmfPlayerMap); Do(p, videoPixelMode); Do(p, videoLoopStatus); diff --git a/Core/HLE/sceUtility.cpp b/Core/HLE/sceUtility.cpp index 70c71c9f2b..2f351f7442 100644 --- a/Core/HLE/sceUtility.cpp +++ b/Core/HLE/sceUtility.cpp @@ -361,6 +361,16 @@ void __UtilityDoState(PointerWrap &p) { if (s >= 4) { Do(p, hasAccessThread); if (hasAccessThread) { + if (p.mode == p.MODE_READ && accessThread) { + // Do() below would delete the stale host object without Forget(), + // letting ~HLEHelperThread run __KernelDeleteThread and free kernel + // memory using pre-load ids/blocks against the restored kernel + // state. If an id or block was recycled, that kills a live thread + // or frees a live allocation. Same pattern as __IoDoState. + accessThread->Forget(); + delete accessThread; + accessThread = nullptr; + } Do(p, accessThread); if (p.mode == p.MODE_READ) accessThreadState = "from save state";