fix(libkernel): serialize positioned file transfers on Windows #3861

Closed
ag6393163475-lab wants to merge 1 commit from upstream-pr/3861 into main
ag6393163475-lab commented 2026-10-11 18:06:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ag6393163475-lab on 2026-10-11T18:06:16Z.


What

Fixes a race condition in NativePositioned_nid_no_patch and NativeTransfer on Windows where concurrent pread and read calls on the same file descriptor corrupt the handle's file position pointer.

On Windows, ReadFile with an OVERLAPPED offset on synchronous CRT file descriptors alters the underlying handle position before it can be restored. When multiple threads (such as Loading.PreloadManager reading 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

  • OS: Windows 11 (x86_64), macOS (Apple Silicon)
  • Title: Hotline Miami (PPSA09682)
  • Flags: --windows --module-dir Media/Modules --module-dir Media/Plugins
  • Passed: python3 tools/check_conventions.py --base origin/main reports 0 findings and 0 errors.

Checklist

Depends on

none

AI-assisted

yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4821178822 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3861). Originally posted by [ag6393163475-lab](https://github.com/ag6393163475-lab) on 2026-10-11T18:06:16Z. --- ### What Fixes a race condition in `NativePositioned_nid_no_patch` and `NativeTransfer` on Windows where concurrent `pread` and `read` calls on the same file descriptor corrupt the handle's file position pointer. On Windows, `ReadFile` with an `OVERLAPPED` offset on synchronous CRT file descriptors alters the underlying handle position before it can be restored. When multiple threads (such as `Loading.PreloadManager` reading 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 - OS: Windows 11 (x86_64), macOS (Apple Silicon) - Title: Hotline Miami (`PPSA09682`) - Flags: `--windows --module-dir Media/Modules --module-dir Media/Plugins` - Passed: `python3 tools/check_conventions.py --base origin/main` reports 0 findings and 0 errors. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [[technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md)](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [[TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs)](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title ### Depends on none ### AI-assisted yes
MarcZX10 commented 2026-10-11 18:07:25 +00:00 (Migrated from github.com)

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 178

@@ -173,6 +175,7 @@ std::int64_t NativePositioned_nid_no_patch(int descriptor, void* buf, std::size_
         errno = EBADF;
         return -1;
     }
+    std::lock_guard lock(g_positionedMutex);

Could the same serialization cover NativeLseek in File/src/Open.cpp? NativePositioned_nid_no_patch saves the shared descriptor position with SetFilePointerEx(FILE_CURRENT) and restores it after the transfer. A concurrent _lseeki64 can run between those operations, after which this restore overwrites the seek result. Locking NativeTransfer handles ordinary reads and writes, but not this seek path.

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4242631356 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3861#discussion_r4242631356). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T18:07:25Z. **Code review location:** `core/libs/prx/libkernel/File/src/Stdio.cpp`; line 178 ~~~~diff @@ -173,6 +175,7 @@ std::int64_t NativePositioned_nid_no_patch(int descriptor, void* buf, std::size_ errno = EBADF; return -1; } + std::lock_guard lock(g_positionedMutex); ~~~~ --- Could the same serialization cover `NativeLseek` in `File/src/Open.cpp`? `NativePositioned_nid_no_patch` saves the shared descriptor position with `SetFilePointerEx(FILE_CURRENT)` and restores it after the transfer. A concurrent `_lseeki64` can run between those operations, after which this restore overwrites the seek result. Locking `NativeTransfer` handles ordinary reads and writes, but not this seek path.
adriadam10 commented 2026-10-11 18:11:57 +00:00 (Migrated from github.com)

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:

  • No evidence it fixes the title. "Tested" lists the OS and title but not the outcome. Please state whether Hotline Miami now gets past 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.
  • No test for the race. A Windows-only test that runs pread against read on one descriptor from two threads and checks the offsets (it fails on main, passes here) would show the bug and the fix, independent of the title.
  • Global lock. g_positionedMutex serializes 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.
  • Other position users. Only these two functions take the lock; any other path that reads or sets the handle position on the same descriptor (e.g. seek) can still interleave. Please check which ones exist and take the same lock there, or explain why they can't race.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5484370427 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3861#pullrequestreview-5484370427). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T18:11:57Z. **Review:** CHANGES_REQUESTED --- Thanks for looking into #3805. A few things before this can merge: - **No evidence it fixes the title.** "Tested" lists the OS and title but not the outcome. Please state whether Hotline Miami now gets past `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. - **No test for the race.** A Windows-only test that runs `pread` against `read` on one descriptor from two threads and checks the offsets (it fails on `main`, passes here) would show the bug and the fix, independent of the title. - **Global lock.** `g_positionedMutex` serializes 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. - **Other position users.** Only these two functions take the lock; any other path that reads or sets the handle position on the same descriptor (e.g. seek) can still interleave. Please check which ones exist and take the same lock there, or explain why they can't race.
ducloc080202 commented 2026-10-12 16:00:23 +00:00 (Migrated from github.com)

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.cpp change (one mutex around the positioned save/restore and around the sequential transfer) onto current main, rebuilt libkernel and 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, contentVersion 01.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 of 0x0) on thread Loading.PreloadManager at app.exe+0x7dfecb, while loading StreamingAssets/aa/PS5/*.bundle — the same shape as #3805 (Hotline Miami, app.exe+0x7a6730, write of 0x24).

After this change: zero 0xC0000005 in 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 (from libc.prx) while opening .bundle files, after which execution continues.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6127884516 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3861#issuecomment-6127884516). Originally posted by [ducloc080202](https://github.com/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.cpp` change (one mutex around the positioned save/restore and around the sequential transfer) onto current `main`, rebuilt `libkernel` and 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, `contentVersion` 01.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 of `0x0`) on thread `Loading.PreloadManager` at `app.exe+0x7dfecb`, while loading `StreamingAssets/aa/PS5/*.bundle` — the same shape as #3805 (Hotline Miami, `app.exe+0x7a6730`, write of `0x24`). **After this change:** zero `0xC0000005` in 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` (from `libc.prx`) while opening `.bundle` files, after which execution continues.
oneandonlydean commented 2026-10-13 07:46:31 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140845522 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3861#issuecomment-6140845522). Originally posted by [oneandonlydean](https://github.com/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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!3861
No description provided.