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.
This commit is contained in:
Ren committed 2026-07-27 16:20:01 +02:00
1 parent a6e4d691ef
commit 9296bf0a04
2 files changed
+22

No files matched your search

+12
View File
@@ -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);
+10
View File
@@ -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";