fix(libkernel): serialize positioned file transfers on Windows #3861
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3861"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by ag6393163475-lab on 2026-10-11T18:06:16Z.
What
Fixes a race condition in
NativePositioned_nid_no_patchandNativeTransferon Windows where concurrentpreadandreadcalls on the same file descriptor corrupt the handle's file position pointer.On Windows,
ReadFilewith anOVERLAPPEDoffset on synchronous CRT file descriptors alters the underlying handle position before it can be restored. When multiple threads (such asLoading.PreloadManagerreading asset archives) perform positioned reads concurrently, interleaved operations corrupt stream offsets and lead to deserialization of malformed object data, triggering null-pointer writes (#3805).Serializes handle offset manipulation during positioned reads and sequential transfers with a mutex lock to preserve consistent file pointer state.
Tested
PPSA09682)--windows --module-dir Media/Modules --module-dir Media/Pluginspython3 tools/check_conventions.py --base origin/mainreports 0 findings and 0 errors.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs).mdfiles or images added to the repository (attach them to this PR)Depends on
none
AI-assisted
yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T18:07:25Z.
Code review location:
core/libs/prx/libkernel/File/src/Stdio.cpp; line 178Could the same serialization cover
NativeLseekinFile/src/Open.cpp?NativePositioned_nid_no_patchsaves the shared descriptor position withSetFilePointerEx(FILE_CURRENT)and restores it after the transfer. A concurrent_lseeki64can run between those operations, after which this restore overwrites the seek result. LockingNativeTransferhandles ordinary reads and writes, but not this seek path.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T18:11:57Z.
Review: CHANGES_REQUESTED
Thanks for looking into #3805. A few things before this can merge:
Loading.PreloadManager(how many runs, vs. the 6/6 faults in #3805), and attach the log to this PR. #3805 itself says the cause is not established (the array object looks unconstructed), so the race is a hypothesis until a run shows the fault gone.preadagainstreadon one descriptor from two threads and checks the offsets (it fails onmain, passes here) would show the bug and the fix, independent of the title.g_positionedMutexserializes every positioned/sequential transfer across all descriptors (Stdio.cpp:168,:178,:205), including large asset reads on unrelated files. A lock per descriptor (or per handle) removes the corruption without that cost.Imported from boykopovar/AnyPS5. Originally posted by ducloc080202 on 2026-10-12T16:00:23Z.
Windows verification with a second title (not Hotline Miami).
Environment. Windows 11, RTX 3050 Laptop (driver 617.42), WinLibs GCC 15.2.0, Release. I applied this PR's
Stdio.cppchange (one mutex around the positioned save/restore and around the sequential transfer) onto currentmain, rebuiltlibkerneland re-ran the NID patcher. Cat Quest III also needs #4296's module-arguments fix to get past its PSN validator, so both were applied for the run below.Title. Cat Quest III (PPSA15065, Unity IL2CPP,
contentVersion01.000.001), relinked with--windows --module-dir Media/Plugins --module-dir Media/Modules.Before this change (with only the #4296 module-arguments fix): deterministic
0xC0000005(write of0x0) on threadLoading.PreloadManageratapp.exe+0x7dfecb, while loadingStreamingAssets/aa/PS5/*.bundle— the same shape as #3805 (Hotline Miami,app.exe+0x7a6730, write of0x24).After this change: zero
0xC0000005in the whole run. The title loads the bundles and reaches Rewired / Unity init (Unity 2022.3.14f1) and keeps running.So the #3805 root cause and this fix reproduce on Windows on a title other than Hotline Miami. One observation, separate from this race: the run still prints a handful of
FATAL: UCRT invalid parameter(fromlibc.prx) while opening.bundlefiles, after which execution continues.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T07:46:31Z.
Closing in favour of #4973: it carries this commit onto current main with your authorship kept, and this branch conflicts in
Stdio.cpp. The open review points (seek path, test) move there. Thanks for finding the race.Pull request closed