Skip to content

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

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
xXPBSXx:fix/transfer-descriptor-first-fb
Oct 21, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
xXPBSXx:fix/transfer-descriptor-first-fb

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

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

@cadenasynyster

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 20, 2026
…te before their buffer and offset

A null buffer, or for pread and pwrite a negative offset, was answered with
EFAULT or EINVAL before the descriptor was looked at, so a descriptor that is not
open (EBADF) or a pipe (ESPIPE) was hidden. FreeBSD 11.4 sys_generic.c looks
the descriptor up first (kern_readv, kern_preadv: fget, then the seekable
check, then the offset) and only reaches the buffer when it copies. The check
runs only when the buffer or offset is invalid, so a normal transfer costs nothing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xXPBSXx
xXPBSXx force-pushed the fix/transfer-descriptor-first-fb branch from 77ec6f1 to c2b9249 Compare October 20, 2026 23:45
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 21, 2026
@xXPBSXx

xXPBSXx commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

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 adriadam10 self-assigned this Oct 21, 2026
@adriadam10
adriadam10 merged commit ee67ebc into boykopovar:main Oct 21, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix error Closes an issue (Fixes #N) libkernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libkernel: a null buffer or negative offset hides EBADF and ESPIPE in read, write, pread and pwrite

3 participants