fix(libkernel): look up the descriptor of read, write, pread and pwrite before their buffer and offset #8663

Merged
xXPBSXx merged 1 commit from upstream-pr/8663 into main 2026-10-21 18:35:28 +00:00
xXPBSXx commented 2026-10-20 20:17:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T20:17:56Z.


What

New helpers PositionedArgumentError and TransferArgumentError (Stdio.cpp) run only when the buffer is null or the offset is negative: EBADF if the descriptor is not open, ESPIPE for a socket or pipe, EINVAL for a negative offset, otherwise EFAULT. Used by pread, pwrite, read, _read, write, _write, sceKernelPread, sceKernelPwrite (both host variants), sceKernelRead and sceKernelWrite. The size limit and the iovec calls are not changed.
Fixes #8662

git merge-tree against upstream/main and against the open socket and libkernel PRs #8499, #8492, #8474 and #8463 is clean; pr_overlap was not run.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main + head 77ec6f13e2:

  • guest_filesystem (new cases) fails without the changes to Stdio.cpp and Open.cpp and passes with them.
  • The tests that call read, write, pread, pwrite or the sceKernel variants with a null buffer or a negative offset and are built on Windows all pass, 18 of 18: guest_posix_scalar_io, guest_kernel_sys, guest_posix_filesystem, guest_apr_read_*, guest_filesystem, guest_aio, guest_sendfile, guest_shm_open, guest_kernel_app_archive_*, guest_dup, guest_file_vector. No test line had to be changed.
  • GuestDirectoryEntries.cpp:144/145 and the NamedShm tests are Linux-only and were read in the code, not run.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux/macOS (the pipe check uses fstat there), a console, a title run.

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; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4941911080 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8663). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T20:17:56Z. --- ### What New helpers `PositionedArgumentError` and `TransferArgumentError` (Stdio.cpp) run only when the buffer is null or the offset is negative: EBADF if the descriptor is not open, ESPIPE for a socket or pipe, EINVAL for a negative offset, otherwise EFAULT. Used by `pread`, `pwrite`, `read`, `_read`, `write`, `_write`, `sceKernelPread`, `sceKernelPwrite` (both host variants), `sceKernelRead` and `sceKernelWrite`. The size limit and the iovec calls are not changed. Fixes #8662 `git merge-tree` against `upstream/main` and against the open socket and libkernel PRs #8499, #8492, #8474 and #8463 is clean; `pr_overlap` was not run. ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on `upstream/main` + head `77ec6f13e2`: - `guest_filesystem` (new cases) fails without the changes to `Stdio.cpp` and `Open.cpp` and passes with them. - The tests that call `read`, `write`, `pread`, `pwrite` or the `sceKernel` variants with a null buffer or a negative offset and are built on Windows all pass, 18 of 18: `guest_posix_scalar_io`, `guest_kernel_sys`, `guest_posix_filesystem`, `guest_apr_read_*`, `guest_filesystem`, `guest_aio`, `guest_sendfile`, `guest_shm_open`, `guest_kernel_app_archive_*`, `guest_dup`, `guest_file_vector`. No test line had to be changed. - `GuestDirectoryEntries.cpp:144/145` and the NamedShm tests are Linux-only and were read in the code, not run. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux/macOS (the pipe check uses `fstat` there), a console, a title run. ### 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 - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 20:31:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T20:31:24Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 6044 core/libs/tests/GuestFilesystem.cpp
PR 6879 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 6936 core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7201 core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7447 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 8520 core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6300824198 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8663#issuecomment-6300824198). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T20:31:24Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/File/src/Open.cpp`<br>`core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestFilesystem.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 6936](https://www.github.com/boykopovar/AnyPS5/pull/6936) | `core/libs/prx/libkernel/File/src/Open.cpp`<br>`core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/File/src/Open.cpp`<br>`core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 7447](https://www.github.com/boykopovar/AnyPS5/pull/7447) | `core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 8520](https://www.github.com/boykopovar/AnyPS5/pull/8520) | `core/libs/prx/libkernel/File/src/Open.cpp`<br>`core/libs/prx/libkernel/File/src/Stdio.cpp` |
cadenasynyster commented 2026-10-20 22:02:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-20T22:02:46Z.


Independent Windows run of this PR.

Environment: Windows 11 10.0.26300, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON. I built only the test targets involved.

Results:

  • PR head: guest_filesystem, guest_posix_scalar_io, guest_aio, guest_dup and guest_sendfile all pass (5/5).
  • Baseline: I kept the PR's tests and replaced only the production files changed here (File/src/Stdio.cpp and File/src/Open.cpp) with their base versions, then rebuilt. guest_filesystem fails with exit 0xc0000409, matching the description. I restored the PR's files afterwards.

Not run: other platforms, the rest of the 18 tests listed in the description, the full repository CTest, and any title or console run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6302736280 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8663#issuecomment-6302736280). Originally posted by [cadenasynyster](https://github.com/cadenasynyster) on 2026-10-20T22:02:46Z. --- Independent Windows run of this PR. **Environment:** Windows 11 10.0.26300, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON. I built only the test targets involved. **Results:** - PR head: `guest_filesystem`, `guest_posix_scalar_io`, `guest_aio`, `guest_dup` and `guest_sendfile` all pass (5/5). - Baseline: I kept the PR's tests and replaced only the production files changed here (`File/src/Stdio.cpp` and `File/src/Open.cpp`) with their base versions, then rebuilt. `guest_filesystem` fails with exit 0xc0000409, matching the description. I restored the PR's files afterwards. **Not run:** other platforms, the rest of the 18 tests listed in the description, the full repository CTest, and any title or console run. AI-assisted: yes
xXPBSXx commented 2026-10-21 03:40:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T03:40:03Z.


Linux data point for c2b92498b5 (WSL2, Ubuntu 24.04, GCC 13.3, Release, -j4; a virtualised kernel, not bare-metal Linux): the descriptor-order test family (guest_filesystem and the tests that call read/write/pread/pwrite) pass on Linux (17 of 17). This replaces the "Linux not tested" line in the description for this head. Not tested: macOS.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6307450933 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8663#issuecomment-6307450933). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T03:40:03Z. --- Linux data point for `c2b92498b5` (WSL2, Ubuntu 24.04, GCC 13.3, Release, `-j4`; a virtualised kernel, not bare-metal Linux): the descriptor-order test family (guest_filesystem and the tests that call read/write/pread/pwrite) pass on Linux (17 of 17). This replaces the "Linux not tested" line in the description for this head. Not tested: macOS. AI-assisted (Claude Code).
adriadam10 commented 2026-10-21 18:36:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T18:36:25Z.


Checked the descriptor-first order against FreeBSD 11.4 kern_preadv/dofileread (EBADF, ESPIPE, EINVAL for a negative offset, EFAULT last), all twelve call sites, and the new guest_filesystem cases (they expect EBADF/ESPIPE where main returned EFAULT/EINVAL; @cadenasynyster also confirmed them failing on baseline on Windows); merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6323636724 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8663#issuecomment-6323636724). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T18:36:25Z. --- Checked the descriptor-first order against FreeBSD 11.4 `kern_preadv`/`dofileread` (EBADF, ESPIPE, EINVAL for a negative offset, EFAULT last), all twelve call sites, and the new `guest_filesystem` cases (they expect EBADF/ESPIPE where `main` returned EFAULT/EINVAL; @cadenasynyster also confirmed them failing on baseline on Windows); merged.
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!8663
No description provided.