Savestate: don't let stale HLE helper threads mutate restored kernel state - #21988
Open
Arkadyzja wants to merge 1 commit into
Open
Savestate: don't let stale HLE helper threads mutate restored kernel state#21988Arkadyzja wants to merge 1 commit into
Arkadyzja wants to merge 1 commit into
Conversation
…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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
On savestate load, two sites destroy a pre-load host object that owns an
HLEHelperThreadwithout callingForget()first.~HLEHelperThreadthen runs__KernelDeleteThread()andkernelMemory.Free()using a thread id and entry-block address from before the load, against the freshly restored kernel state:Do(p, accessThread)—DoClassdeletes the stale host object before recreating it from the stream.Do(p, psmfPlayerMap)— the map serializer deletes every existingPsmfPlayer, and~PsmfPlayer -> AbortFinish()does a rawdelete finishThread.When the stale id/block happens to be absent from the restored state, this only produces error spam (
__KernelDeleteThread: thread ... does not exist,BlockAllocator: invalid free) — annoying but harmless. When the id or block has been recycled by the restored state, a live thread is terminated or a live allocation is freed, silently corrupting the state that was just loaded. Easiest to hit by loading a state while a savedata operation or PSMF player is active in the current session.The fix
__IoDoStateand__PsmfShutdownalready handle this correctly by callingForget()before deletion (which detaches the host wrapper from the kernel-side objects — those belong to the serialized state, not to the wrapper). This PR applies the same pattern to the two remaining sites:sceUtility.cpp:Forget()+ delete the staleaccessThreadbeforeDo(p, accessThread)deserializes the replacement.scePsmf.cpp: beforeDo(p, psmfPlayerMap)onMODE_READ, walk the existing map andForget()each player'sfinishThread.Risk
Load-path only; strictly removes writes to restored kernel state. Worst-case behavior change: a kernel-side thread record that was previously (incorrectly) freed is now left to the restored state to manage — i.e., exactly what the serializer intends.