fix(kernel): keep the file offset in Windows AIO reads and writes #1135

Merged
orbz22 merged 1 commit from upstream-pr/1135 into main 2026-10-09 23:09:52 +00:00
orbz22 commented 2026-10-07 13:30:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orbz22 on 2026-10-07T13:30:11Z.


What

On Windows, NativePread/NativePwrite in core/libs/prx/libkernel/Aio/src/Aio.cpp seeked a _dup of the descriptor and read or wrote through it. _dup duplicates the OS handle, and a duplicated handle refers to the same file object, so it shares the file position: every AIO request moved the title's own descriptor to the end of the request. The Linux path uses pread/pwrite and leaves the offset alone.

The Windows path now saves the descriptor's offset, seeks the descriptor itself, transfers, and restores the offset (throwing if the restore fails). Both functions share one helper; the existing size limits and error reporting (EBADF for an invalid descriptor, as the batch test with fd == -1 checks) are unchanged. The save and restore is not atomic with respect to another thread reading or writing the same descriptor at the same time.

GuestAio.cpp now seeks the batch descriptor to offset 2 and checks that it is still 2 after the write batch and after the read batch.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), CMake 4.4.4, Ninja 1.13.2, on e79c971f.

  • With the new checks and Aio.cpp from main, guest_aio_tests aborts with Aio check failed at line 109 (the check right after the write batch at offset 8).
  • With this change, ctest -R '^guest_aio$' --repeat until-fail:3: passed 3/3.
  • Full build, --target libs, full ctest: 393/393 passed.
  • Linux: not run locally. The Linux code is unchanged; the new checks hold there because pread/pwrite do not move the offset.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01UTjpbMX7BUFMZZaxJE98zV

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4773370421 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1135). Originally posted by [orbz22](https://github.com/orbz22) on 2026-10-07T13:30:11Z. --- ### What On Windows, `NativePread`/`NativePwrite` in `core/libs/prx/libkernel/Aio/src/Aio.cpp` seeked a `_dup` of the descriptor and read or wrote through it. `_dup` duplicates the OS handle, and a duplicated handle refers to the same file object, so it shares the file position: every AIO request moved the title's own descriptor to the end of the request. The Linux path uses `pread`/`pwrite` and leaves the offset alone. The Windows path now saves the descriptor's offset, seeks the descriptor itself, transfers, and restores the offset (throwing if the restore fails). Both functions share one helper; the existing size limits and error reporting (`EBADF` for an invalid descriptor, as the batch test with `fd == -1` checks) are unchanged. The save and restore is not atomic with respect to another thread reading or writing the same descriptor at the same time. `GuestAio.cpp` now seeks the batch descriptor to offset 2 and checks that it is still 2 after the write batch and after the read batch. ### Tested Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), CMake 4.4.4, Ninja 1.13.2, on e79c971f. - With the new checks and `Aio.cpp` from `main`, `guest_aio_tests` aborts with `Aio check failed at line 109` (the check right after the write batch at offset 8). - With this change, `ctest -R '^guest_aio$' --repeat until-fail:3`: passed 3/3. - Full build, `--target libs`, full `ctest`: 393/393 passed. - Linux: not run locally. The Linux code is unchanged; the new checks hold there because `pread`/`pwrite` do not move the offset. ### 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) - [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) - [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 - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01UTjpbMX7BUFMZZaxJE98zV
adriadam10 commented 2026-10-08 08:02:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T08:02:18Z.

Review: CHANGES_REQUESTED


Checked: build + full ctest green, guest_aio passes, Linux/Windows CI green on the head. Two things before merging:

  • Stdio.cpp already has NativePositioned doing the same save/seek/restore on Windows (and #1131 extends it). Please reuse that one from Aio.cpp (expose it from libkernel) instead of adding a second copy; ideally rebase this on #1131.
  • The restored file pointer can be seen moved by a concurrent read/write on the same descriptor. Please record that under ### Functional in TechnicalDebt (#1131 has the same line for the vectored calls).
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5453560561 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1135#pullrequestreview-5453560561). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T08:02:18Z. **Review:** CHANGES_REQUESTED --- Checked: build + full ctest green, `guest_aio` passes, Linux/Windows CI green on the head. Two things before merging: - `Stdio.cpp` already has `NativePositioned` doing the same save/seek/restore on Windows (and #1131 extends it). Please reuse that one from `Aio.cpp` (expose it from libkernel) instead of adding a second copy; ideally rebase this on #1131. - The restored file pointer can be seen moved by a concurrent `read`/`write` on the same descriptor. Please record that under `### Functional` in TechnicalDebt (#1131 has the same line for the vectored calls).
orbz22 commented 2026-10-09 02:20:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orbz22 on 2026-10-09T02:20:57Z.


@adriadam10 thanks for the review. Rebased on current main and reworked as you asked (6335c068):

  • main now has 0fb192c4, which gives Aio.cpp its own overlapped NativePositioned. That version does not restore the file pointer, so an AIO request still moves the descriptor's offset. I removed it and Aio.cpp now calls the one in Stdio.cpp. That function is exposed as NativePositioned_nid_no_patch and declared in File/include/File.hpp, inside extern "C" like the other _nid_no_patch helpers. Aio.cpp keeps the offset < 0 -> EINVAL check that 0fb192c4 added.
  • TechnicalDebt ### Functional has a new line for the AIO submit calls, next to the pread/preadv line from #1131: a concurrent read/write on the same descriptor can see the moved pointer.
  • GuestAio sets the offset to 2 before the write batch and checks that it is still 2 afterwards.

Tested on Windows 11 (WinLibs GCC 15.2): guest_aio_tests, guest_file_vector_tests and file_position_tests pass. tools/check_conventions.py --base upstream/main passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6072936131 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1135#issuecomment-6072936131). Originally posted by [orbz22](https://github.com/orbz22) on 2026-10-09T02:20:57Z. --- @adriadam10 thanks for the review. Rebased on current `main` and reworked as you asked (6335c068): - `main` now has 0fb192c4, which gives `Aio.cpp` its own overlapped `NativePositioned`. That version does not restore the file pointer, so an AIO request still moves the descriptor's offset. I removed it and `Aio.cpp` now calls the one in `Stdio.cpp`. That function is exposed as `NativePositioned_nid_no_patch` and declared in `File/include/File.hpp`, inside `extern "C"` like the other `_nid_no_patch` helpers. `Aio.cpp` keeps the `offset < 0` -> `EINVAL` check that 0fb192c4 added. - TechnicalDebt `### Functional` has a new line for the AIO submit calls, next to the pread/preadv line from #1131: a concurrent `read`/`write` on the same descriptor can see the moved pointer. - `GuestAio` sets the offset to 2 before the write batch and checks that it is still 2 afterwards. Tested on Windows 11 (WinLibs GCC 15.2): `guest_aio_tests`, `guest_file_vector_tests` and `file_position_tests` pass. `tools/check_conventions.py --base upstream/main` passes.
adriadam10 commented 2026-10-09 22:26:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T22:26:05Z.

Review: APPROVED


Checked: AIO now goes through NativePositioned from Stdio.cpp (no second copy), the offset < 0 -> EINVAL check is kept, and the TechnicalDebt line is there. Rebased on current main (only a TechnicalDebt adjacency conflict, both lines kept); full build + ctest green, guest_aio passes, Windows/Linux CI green on the previous head. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5475910838 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1135#pullrequestreview-5475910838). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T22:26:05Z. **Review:** APPROVED --- Checked: AIO now goes through `NativePositioned` from `Stdio.cpp` (no second copy), the `offset < 0` -> `EINVAL` check is kept, and the TechnicalDebt line is there. Rebased on current `main` (only a TechnicalDebt adjacency conflict, both lines kept); full build + ctest green, `guest_aio` passes, Windows/Linux CI green on the previous head. Merging.
github-actions[bot] commented 2026-10-09 22:57:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T22:57:16Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#2143 core/libs/tests/GuestAio.cpp L101
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090675540 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1135#issuecomment-6090675540). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T22:57:16Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #2143 | `core/libs/tests/GuestAio.cpp` [L101](https://github.com/boykopovar/AnyPS5/blob/dc4b7104e5012cb587b51e5c3c73a1dccd89bbf4/core/libs/tests/GuestAio.cpp#L101) | | |
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!1135
No description provided.