fix(libkernel): return errors from pread instead of aborting #4719

Merged
gjonn merged 1 commit from upstream-pr/4719 into main 2026-10-14 04:38:49 +00:00
gjonn commented 2026-10-12 19:44:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-12T19:44:24Z.


What

The POSIX pread export threw on a null buffer, a negative offset and any failed host read, which terminates the process. It now returns -1 and sets the guest errno exactly like pwrite and sceKernelPread on main: EFAULT for a null buffer with a non-zero length, EINVAL for a negative offset, and the mapped host error otherwise (EBADF for an invalid or write-only descriptor).

PPSA01317 stops on its first boot on Windows when a read from its save folder fails, at:

terminate called after throwing an instance of 'std::runtime_error'
  what():  pread_nid_postfix: pread failed, fd=..., errno=...

Tested

Windows 11, MinGW, guest_file_vector adds pread/pwrite checks for a null buffer, a negative offset, descriptor 9999 and a write-only descriptor.

On main:

terminate called after throwing an instance of 'std::invalid_argument'
  what():  pread_nid_postfix: invalid argument
1/1 Test #869: guest_file_vector ................Exit code 0xc0000409***Exception

With the argument checks skipped, the invalid descriptor check on main stops at:

terminate called after throwing an instance of 'std::runtime_error'
  what():  pread_nid_postfix: pread failed, fd=9999, errno=9

With this change:

1/1 Test #869: guest_file_vector ................   Passed    0.05 sec
100% tests passed, 0 tests failed out of 1

guest_aio and guest_shm_open also pass.

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 under Silent stubs in their library's technical debt file
  • 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
  • Unblocks: PPSA01317 #4697
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4834215315 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4719). Originally posted by [gjonn](https://github.com/gjonn) on 2026-10-12T19:44:24Z. --- ### What The POSIX `pread` export threw on a null buffer, a negative offset and any failed host read, which terminates the process. It now returns -1 and sets the guest errno exactly like `pwrite` and `sceKernelPread` on main: `EFAULT` for a null buffer with a non-zero length, `EINVAL` for a negative offset, and the mapped host error otherwise (`EBADF` for an invalid or write-only descriptor). PPSA01317 stops on its first boot on Windows when a read from its save folder fails, at: ``` terminate called after throwing an instance of 'std::runtime_error' what(): pread_nid_postfix: pread failed, fd=..., errno=... ``` ### Tested Windows 11, MinGW, `guest_file_vector` adds `pread`/`pwrite` checks for a null buffer, a negative offset, descriptor 9999 and a write-only descriptor. On main: ``` terminate called after throwing an instance of 'std::invalid_argument' what(): pread_nid_postfix: invalid argument 1/1 Test #869: guest_file_vector ................Exit code 0xc0000409***Exception ``` With the argument checks skipped, the invalid descriptor check on main stops at: ``` terminate called after throwing an instance of 'std::runtime_error' what(): pread_nid_postfix: pread failed, fd=9999, errno=9 ``` With this change: ``` 1/1 Test #869: guest_file_vector ................ Passed 0.05 sec 100% tests passed, 0 tests failed out of 1 ``` `guest_aio` and `guest_shm_open` also pass. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Unblocks: PPSA01317 #4697 - [x] AI-assisted: yes
nicolasfalesy commented 2026-10-13 07:36:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-13T07:36:12Z.


Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto main 7e181fcc3 together with #4725, #4719, #4585 and #4582 (no conflicts): guest_file_vector passes. With this PR's source change reverted (test kept), it fails on Linux, so the check guards the change on Linux too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140682390 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4719#issuecomment-6140682390). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-13T07:36:12Z. --- Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto `main` 7e181fcc3 together with #4725, #4719, #4585 and #4582 (no conflicts): `guest_file_vector` passes. With this PR's source change reverted (test kept), it fails on Linux, so the check guards the change on Linux too.
adriadam10 commented 2026-10-13 15:15:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T15:15:48Z.


Rebased onto current main (the partial EBADF handling in pread_nid_postfix overlapped this rewrite; kept this PR's version). guest_file_vector fails without the Stdio.cpp change and passes with it; scoped ctest (libs, libc, libkernel, 217 tests) is green on the rebase; merging once the full build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148823605 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4719#issuecomment-6148823605). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T15:15:48Z. --- Rebased onto current `main` (the partial `EBADF` handling in `pread_nid_postfix` overlapped this rewrite; kept this PR's version). `guest_file_vector` fails without the `Stdio.cpp` change and passes with it; scoped ctest (libs, libc, libkernel, 217 tests) is green on the rebase; merging once the full build is green.
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!4719
No description provided.