fix(libkernel): return errors from file operation failures #2620

Merged
oneandonlydean merged 1 commit from upstream-pr/2620 into main 2026-10-10 08:50:33 +00:00
oneandonlydean commented 2026-10-10 08:28:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T08:28:48Z.


Carrying @justdoston's fix for #2423 while PR intake is paused. The change is theirs (from justdoston/AnyPS5:fix/windows-file-operation-errors, 7c706c88).

What

  • Windows sceKernelPread/sceKernelPwrite get the Linux build's checks and SceErrorFromErrno returns. A zero-length transfer with a null buffer returns 0.
  • NativePositioned reports ERROR_ACCESS_DENIED as EBADF (a pread on an O_WRONLY descriptor is EBADF on FreeBSD and Linux too); other failures stay EIO. The file-pointer handling is unchanged.
  • ftruncate returns EINVAL for a negative length and for a descriptor without write access, and passes other host errors to the guest. sceKernelFtruncate returns the kernel error.

Why

On Windows these calls wrapped the POSIX functions, which throw on every failure, so the guest process aborted with terminate called after throwing ... std::runtime_error (for example pread failed, fd=9999, errno=9) where Linux returns an error. Measured on Windows 11 in the issue:

call before (Windows) now
sceKernelPread(9999, buf, 4, 0) abort 0x80020009
sceKernelPwrite(9999, buf, 4, 0) abort 0x80020009
sceKernelPread(fd, buf, 4, -1) abort 0x80020016
sceKernelPread(fd, NULL, 0, 0) abort 0
sceKernelPwrite on O_RDONLY abort 0x80020009
sceKernelFtruncate(9999, 0) abort 0x80020009
sceKernelFtruncate on O_RDONLY abort 0x80020016

ftruncate threw on host failures on both platforms.

Tested

  • guest_file_vector checks every case in the table, and runs on Windows too.
  • The author: Linux full ctest, no new failures; Windows cross-compiled with MinGW-w64 GCC 16 and checked under Wine 10.0.
  • @sktharunbalaji13-cmd on Windows 11, WinLibs GCC 15.2.0: guest_file_vector passes on a fresh Release build of 7c706c88.
  • tools/check_conventions.py passes, and the branch merges cleanly with current main.

Fixes #2423

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4809894748 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2620). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T08:28:48Z. --- Carrying @justdoston's fix for #2423 while PR intake is paused. The change is theirs (from `justdoston/AnyPS5:fix/windows-file-operation-errors`, 7c706c88). ### What - Windows `sceKernelPread`/`sceKernelPwrite` get the Linux build's checks and `SceErrorFromErrno` returns. A zero-length transfer with a null buffer returns 0. - `NativePositioned` reports `ERROR_ACCESS_DENIED` as `EBADF` (a `pread` on an `O_WRONLY` descriptor is `EBADF` on FreeBSD and Linux too); other failures stay `EIO`. The file-pointer handling is unchanged. - `ftruncate` returns `EINVAL` for a negative length and for a descriptor without write access, and passes other host errors to the guest. `sceKernelFtruncate` returns the kernel error. ### Why On Windows these calls wrapped the POSIX functions, which throw on every failure, so the guest process aborted with `terminate called after throwing ... std::runtime_error` (for example `pread failed, fd=9999, errno=9`) where Linux returns an error. Measured on Windows 11 in the issue: | call | before (Windows) | now | |---|---|---| | `sceKernelPread(9999, buf, 4, 0)` | abort | `0x80020009` | | `sceKernelPwrite(9999, buf, 4, 0)` | abort | `0x80020009` | | `sceKernelPread(fd, buf, 4, -1)` | abort | `0x80020016` | | `sceKernelPread(fd, NULL, 0, 0)` | abort | `0` | | `sceKernelPwrite` on `O_RDONLY` | abort | `0x80020009` | | `sceKernelFtruncate(9999, 0)` | abort | `0x80020009` | | `sceKernelFtruncate` on `O_RDONLY` | abort | `0x80020016` | `ftruncate` threw on host failures on both platforms. ### Tested - `guest_file_vector` checks every case in the table, and runs on Windows too. - The author: Linux full `ctest`, no new failures; Windows cross-compiled with MinGW-w64 GCC 16 and checked under Wine 10.0. - @sktharunbalaji13-cmd on Windows 11, WinLibs GCC 15.2.0: `guest_file_vector` passes on a fresh Release build of 7c706c88. - `tools/check_conventions.py` passes, and the branch merges cleanly with current `main`. Fixes #2423 ### 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
adriadam10 commented 2026-10-10 08:50:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T08:50:31Z.


Checked on Linux: build and ctest green (80 tests in libkernel/libs), guest_file_vector passes and fails when the Stdio.cpp change is reverted. The Windows branches rely on the Windows 11 run by @sktharunbalaji13-cmd and the MinGW/Wine run; merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6095842237 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2620#issuecomment-6095842237). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T08:50:31Z. --- Checked on Linux: build and ctest green (80 tests in libkernel/libs), `guest_file_vector` passes and fails when the `Stdio.cpp` change is reverted. The Windows branches rely on the Windows 11 run by @sktharunbalaji13-cmd and the MinGW/Wine run; merging once CI is green.
reyani-dev commented 2026-10-10 09:25:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-10T09:25:49Z.


Since this landed, main fails two tests on Windows (Linux passes). On main 3d1a7d90, merged with an unrelated libkernel change in my fork CI (run 38040339170), the Windows job reports 2 failed out of 698, and I get the same locally on plain main (WinLibs GCC 15.2.0, Release):

Test #611: guest_file_vector ... Exit code 0xc0000409   File vector check failed at line 174
Test #606: guest_memory ........ Exit code 0xc0000409   Guest memory check failed at .../GuestMemory.cpp:992

Both checks predate this PR and assumed the old Windows sceKernelPread, which went through pread_nid_postfix:

  • GuestMemory.cpp:992 (from 676bbea3) expects sceKernelPread(fd, mapped, page, 0) into a page made read-only by sceKernelMprotect to throw. With this PR, Windows returns an error code instead, as Linux already did, so refused stays false. The check probably wants == SCE_KERNEL_ERROR_EFAULT (0x8002000E) now, or whatever the Linux path returns for that case.

  • GuestFileVector.cpp:174 (from #1777, /dev/urandom) expects sceKernelPread(random, noise + 32, 16, 1000) == 16. A direct probe on Windows main:

    • sceKernelOpen("/dev/urandom") gives fd 3;
    • sceKernelRead returns 16;
    • sceKernelPread at offset 0 or 1000 returns 0x8002001D (ESPIPE).

    The Windows path now goes through NativePositioned_nid_no_patch, which refuses the positioned read on the random device. FreeBSD's pread on a character device succeeds and ignores the offset, which is what the test (and Linux) expects.

sceKernelPreadv(random, pieces, 1, 1000) on line 173 still passes, so the vector path already handles the device. Only the scalar sceKernelPread differs.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096105897 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2620#issuecomment-6096105897). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-10T09:25:49Z. --- Since this landed, `main` fails two tests on Windows (Linux passes). On `main` 3d1a7d90, merged with an unrelated libkernel change in my fork CI ([run 38040339170](https://github.com/AchrafReyani/AnyPS5/actions/runs/38040339170)), the Windows job reports 2 failed out of 698, and I get the same locally on plain `main` (WinLibs GCC 15.2.0, Release): ``` Test #611: guest_file_vector ... Exit code 0xc0000409 File vector check failed at line 174 Test #606: guest_memory ........ Exit code 0xc0000409 Guest memory check failed at .../GuestMemory.cpp:992 ``` Both checks predate this PR and assumed the old Windows `sceKernelPread`, which went through `pread_nid_postfix`: - **`GuestMemory.cpp:992`** (from 676bbea3) expects `sceKernelPread(fd, mapped, page, 0)` into a page made read-only by `sceKernelMprotect` to **throw**. With this PR, Windows returns an error code instead, as Linux already did, so `refused` stays false. The check probably wants `== SCE_KERNEL_ERROR_EFAULT` (`0x8002000E`) now, or whatever the Linux path returns for that case. - **`GuestFileVector.cpp:174`** (from #1777, `/dev/urandom`) expects `sceKernelPread(random, noise + 32, 16, 1000) == 16`. A direct probe on Windows `main`: - `sceKernelOpen("/dev/urandom")` gives fd 3; - `sceKernelRead` returns 16; - `sceKernelPread` at offset 0 or 1000 returns `0x8002001D` (`ESPIPE`). The Windows path now goes through `NativePositioned_nid_no_patch`, which refuses the positioned read on the random device. FreeBSD's `pread` on a character device succeeds and ignores the offset, which is what the test (and Linux) expects. `sceKernelPreadv(random, pieces, 1, 1000)` on line 173 still passes, so the vector path already handles the device. Only the scalar `sceKernelPread` differs. AI-assisted: yes (Claude Code).
LeandroLP23 commented 2026-10-10 09:33:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-10T09:33:06Z.


A fix for both Windows failures @AchrafReyani describes above, ready to cherry-pick: LeandroLP23:fix/windows-pread-tests (5f80f12d, one commit on main 42542127, merges cleanly onto current main). I can't open it as a PR while intake is limited.

  • Stdio.cpp (+1 line): the Windows sceKernelPread now reads the random devices first with File::ReadRandomDevice, at the same point as the Linux version. Without it, a positioned read of /dev/urandom reached NativePositioned and failed with ESPIPE (GuestFileVector.cpp:174).
  • GuestMemory.cpp: the Windows-only check at line 992 expected sceKernelPread into a page made read-only by sceKernelMprotect to throw. Since this PR it returns an SCE error instead, so the check now requires SCE_KERNEL_ERROR_EFAULT, the same result the AIO read of that page two lines below already expects. No production change for this one.

Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release: guest_memory and guest_file_vector pass 3/3 (both fail on plain main), the full non-GPU ctest passes (449/449), conventions pass.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096160863 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2620#issuecomment-6096160863). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-10T09:33:06Z. --- A fix for both Windows failures @AchrafReyani describes above, ready to cherry-pick: [`LeandroLP23:fix/windows-pread-tests`](https://github.com/LeandroLP23/AnyPS5/tree/fix/windows-pread-tests) (`5f80f12d`, one commit on `main` 42542127, merges cleanly onto current `main`). I can't open it as a PR while intake is limited. - `Stdio.cpp` (+1 line): the Windows `sceKernelPread` now reads the random devices first with `File::ReadRandomDevice`, at the same point as the Linux version. Without it, a positioned read of `/dev/urandom` reached `NativePositioned` and failed with `ESPIPE` (`GuestFileVector.cpp:174`). - `GuestMemory.cpp`: the Windows-only check at line 992 expected `sceKernelPread` into a page made read-only by `sceKernelMprotect` to throw. Since this PR it returns an SCE error instead, so the check now requires `SCE_KERNEL_ERROR_EFAULT`, the same result the AIO read of that page two lines below already expects. No production change for this one. Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release: `guest_memory` and `guest_file_vector` pass 3/3 (both fail on plain `main`), the full non-GPU ctest passes (449/449), conventions pass. AI-assisted: yes (Claude Code).
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!2620
No description provided.