fix(libkernel): rewind Windows directory descriptors on lseek #3452

Merged
LeandroLP23 merged 4 commits from upstream-pr/3452 into main 2026-10-11 21:05:29 +00:00
LeandroLP23 commented 2026-10-11 10:51:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-11T10:51:55Z.


Carrying @husseinkad's fix for #3102 (they can't open PRs yet). The change is theirs: the commits of husseinkad:fix/kernel-windows-directory-rewind cherry-picked onto current main without conflicts, authorship kept.

What

On Windows a guest directory descriptor sits on the NUL device, so lseek on it did nothing and a second getdents after lseek(fd, 0, SEEK_SET) returned 0 entries. File::SeekDirectoryDescriptor now handles lseek on directory descriptors (rewinding the snapshot, as FreeBSD's getdirentries(2) allows), outside the shared lock and with offset overflow reported; TechnicalDebt describes the Windows snapshot lifetime. Linux hosts already used the real lseek. Details and the reproduction are in #3102.

Tested

Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, on main 24cb0d24: guest_directory_rewind passes 3/3 and fails with main's DirectoryDescriptor.cpp and Open.cpp. With this PR and the other two of this batch merged together, ctest -E agc_ passes 453/454; the one failure, windows_exceptions, fails the same way on plain main (#3411). Conventions and test registration pass.

Fork CI (run): the three relinker jobs pass. Windows fails only windows_exceptions, which fails on plain main too (#3411). Linux fails agc_driver_image_address_dimension, agc_driver_image_plane_address_on_line_implicit_lod and guest_kernel_stack_protect, the same three in all three runs of this batch (#3106, #3102 and #3067 fixes, unrelated changes), so they come from main. This run also had one guest_ampr_markers failure on Windows, the intermittent #3371.

Fixes #3102

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 (Claude Code, for carrying and the Windows run; see the issue for the author's disclosure)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4818518961 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3452). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-11T10:51:55Z. --- Carrying @husseinkad's fix for #3102 (they can't open PRs yet). The change is theirs: the commits of [`husseinkad:fix/kernel-windows-directory-rewind`](https://github.com/husseinkad/AnyPS5/tree/fix/kernel-windows-directory-rewind) cherry-picked onto current `main` without conflicts, authorship kept. ### What On Windows a guest directory descriptor sits on the NUL device, so `lseek` on it did nothing and a second `getdents` after `lseek(fd, 0, SEEK_SET)` returned 0 entries. `File::SeekDirectoryDescriptor` now handles `lseek` on directory descriptors (rewinding the snapshot, as FreeBSD's `getdirentries(2)` allows), outside the shared lock and with offset overflow reported; TechnicalDebt describes the Windows snapshot lifetime. Linux hosts already used the real `lseek`. Details and the reproduction are in #3102. ### Tested Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, on `main` 24cb0d24: `guest_directory_rewind` passes 3/3 and fails with `main`'s `DirectoryDescriptor.cpp` and `Open.cpp`. With this PR and the other two of this batch merged together, `ctest -E agc_` passes 453/454; the one failure, `windows_exceptions`, fails the same way on plain `main` (#3411). Conventions and test registration pass. Fork CI ([run](https://github.com/LeandroLP23/AnyPS5/actions/runs/38131629079)): the three relinker jobs pass. Windows fails only `windows_exceptions`, which fails on plain `main` too (#3411). Linux fails `agc_driver_image_address_dimension`, `agc_driver_image_plane_address_on_line_implicit_lod` and `guest_kernel_stack_protect`, the same three in all three runs of this batch (#3106, #3102 and #3067 fixes, unrelated changes), so they come from `main`. This run also had one `guest_ampr_markers` failure on Windows, the intermittent #3371. Fixes #3102 ### 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 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 (Claude Code, for carrying and the Windows run; see the issue for the author's disclosure)
MarcZX10 commented 2026-10-11 11:05:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T11:05:10Z.

Code review location: core/libs/prx/libkernel/File/src/Open.cpp; line 220

@@ -214,6 +214,14 @@ std::int64_t APS5_VABI sceKernelLseek(int d, std::int64_t offset, int whence) {
     if (whence < 0 || whence > 2) {
         throw std::invalid_argument(std::string(__func__) + ": invalid whence=" + std::to_string(whence));
     }
+#ifdef _WIN32
+    if (const auto directory = File::SeekDirectoryDescriptor(d, offset, whence)) {
+        if (*directory < 0) {
+            throw std::runtime_error(std::string(__func__) + ": lseek failed, fd=" + std::to_string(d) + ", errno=" + std::to_string(errno));

sceKernelLseek maps native failures through SceIoErrorFromErrno(errno) below this branch. Throwing here leaks a host exception across the guest API and skips that established error mapping. Could this return the same guest error value for directory seeks, and have the negative/overflow tests assert that result instead of expecting std::runtime_error?

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4241330028 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3452#discussion_r4241330028). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T11:05:10Z. **Code review location:** `core/libs/prx/libkernel/File/src/Open.cpp`; line 220 ~~~~diff @@ -214,6 +214,14 @@ std::int64_t APS5_VABI sceKernelLseek(int d, std::int64_t offset, int whence) { if (whence < 0 || whence > 2) { throw std::invalid_argument(std::string(__func__) + ": invalid whence=" + std::to_string(whence)); } +#ifdef _WIN32 + if (const auto directory = File::SeekDirectoryDescriptor(d, offset, whence)) { + if (*directory < 0) { + throw std::runtime_error(std::string(__func__) + ": lseek failed, fd=" + std::to_string(d) + ", errno=" + std::to_string(errno)); ~~~~ --- `sceKernelLseek` maps native failures through `SceIoErrorFromErrno(errno)` below this branch. Throwing here leaks a host exception across the guest API and skips that established error mapping. Could this return the same guest error value for directory seeks, and have the negative/overflow tests assert that result instead of expecting `std::runtime_error`?
adriadam10 commented 2026-10-11 11:49:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T11:49:09Z.

Review: CHANGES_REQUESTED


Thanks for carrying this. Checked the diff against main; build and ctest are green on Linux, but guest_directory_rewind is Windows-only so it can't run here. One thing to fix, which @MarcZX100 raised inline and is still open:

  • core/libs/prx/libkernel/File/src/Open.cpp:220: a negative target or SEEK_CUR overflow throws std::runtime_error, while every other sceKernelLseek failure returns SceIoErrorFromErrno(errno) (line 226). Return that value for directory seeks too (EINVAL for a negative target, EOVERFLOW for the overflow), and have GuestDirectoryRewind.cpp:93-98 assert the returned error instead of expecting an exception.
  • DirectoryDescriptor.cpp:102: errno = GuestEoverflow (84) is a guest value, not the host's. Set the host EOVERFLOW so SceIoErrorFromErrno maps it to 84 (as it does at Open.cpp:129-131).
  • docs/dev/TechnicalDebt.md:365: "throws like any other lseek failure" is wrong, and will be wrong in a different way after the change. Say that a negative target returns EINVAL and an overflow EOVERFLOW, and keep the SEEK_END throw.

The rest (snapshot rewind outside the shared lock, getdents after lseek(fd, 0, SEEK_SET), descriptor reuse in the test) looks right.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5483184910 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3452#pullrequestreview-5483184910). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T11:49:09Z. **Review:** CHANGES_REQUESTED --- Thanks for carrying this. Checked the diff against `main`; build and ctest are green on Linux, but `guest_directory_rewind` is Windows-only so it can't run here. One thing to fix, which @MarcZX100 raised inline and is still open: - `core/libs/prx/libkernel/File/src/Open.cpp:220`: a negative target or `SEEK_CUR` overflow throws `std::runtime_error`, while every other `sceKernelLseek` failure returns `SceIoErrorFromErrno(errno)` (line 226). Return that value for directory seeks too (`EINVAL` for a negative target, `EOVERFLOW` for the overflow), and have `GuestDirectoryRewind.cpp:93-98` assert the returned error instead of expecting an exception. - `DirectoryDescriptor.cpp:102`: `errno = GuestEoverflow` (84) is a guest value, not the host's. Set the host `EOVERFLOW` so `SceIoErrorFromErrno` maps it to 84 (as it does at `Open.cpp:129-131`). - `docs/dev/TechnicalDebt.md:365`: "throws like any other `lseek` failure" is wrong, and will be wrong in a different way after the change. Say that a negative target returns `EINVAL` and an overflow `EOVERFLOW`, and keep the `SEEK_END` throw. The rest (snapshot rewind outside the shared lock, `getdents` after `lseek(fd, 0, SEEK_SET)`, descriptor reuse in the test) looks right.
LeandroLP23 commented 2026-10-11 12:03:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-11T12:03:35Z.


Thanks @adriadam10 and @MarcZX100, all three addressed in 99fa580d (on top of husseinkad's commits):

  • sceKernelLseek returns SceIoErrorFromErrno(errno) for a failed directory seek instead of throwing, like every other lseek failure.
  • SeekDirectoryDescriptor sets the host EOVERFLOW, so the mapping gives 84; the now unused GuestEoverflow constant is gone.
  • GuestDirectoryRewind.cpp asserts the returned errors: 0x80020016 (EINVAL) for lseek(fd, -1, SEEK_SET) and lseek(fd, -4, SEEK_CUR), 0x80020054 (EOVERFLOW) for the SEEK_CUR overflow.
  • TechnicalDebt now says a negative target returns EINVAL and the overflow EOVERFLOW; the SEEK_END throw is kept.

Windows 11, WinLibs GCC 15.2.0 r7, Release: guest_directory_rewind passes 3/3, and fails with the previous Open.cpp (the throw); guest_filesystem and the getdents tests pass; ctest -E agc_ passes apart from windows_exceptions (#3411, also on main).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6108819794 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3452#issuecomment-6108819794). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-11T12:03:35Z. --- Thanks @adriadam10 and @MarcZX100, all three addressed in 99fa580d (on top of husseinkad's commits): - `sceKernelLseek` returns `SceIoErrorFromErrno(errno)` for a failed directory seek instead of throwing, like every other `lseek` failure. - `SeekDirectoryDescriptor` sets the host `EOVERFLOW`, so the mapping gives 84; the now unused `GuestEoverflow` constant is gone. - `GuestDirectoryRewind.cpp` asserts the returned errors: `0x80020016` (`EINVAL`) for `lseek(fd, -1, SEEK_SET)` and `lseek(fd, -4, SEEK_CUR)`, `0x80020054` (`EOVERFLOW`) for the `SEEK_CUR` overflow. - TechnicalDebt now says a negative target returns `EINVAL` and the overflow `EOVERFLOW`; the `SEEK_END` throw is kept. Windows 11, WinLibs GCC 15.2.0 r7, Release: `guest_directory_rewind` passes 3/3, and fails with the previous `Open.cpp` (the throw); `guest_filesystem` and the `getdents` tests pass; `ctest -E agc_` passes apart from `windows_exceptions` (#3411, also on `main`).
MarcZX10 commented 2026-10-11 12:18:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T12:18:04Z.


Thanks for addressing the error mapping and test expectations I raised. The Windows run on the rebased head confirms the directory-seek errors are returned correctly, and the prior behavior failed at the same checks. That resolves my review point.

The current red relinker checks appear to come from the missing 3rdparty/imgui submodule URL on main, also reported on other PRs; they do not look specific to this change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6108937536 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3452#issuecomment-6108937536). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T12:18:04Z. --- Thanks for addressing the error mapping and test expectations I raised. The Windows run on the rebased head confirms the directory-seek errors are returned correctly, and the prior behavior failed at the same checks. That resolves my review point. The current red relinker checks appear to come from the missing `3rdparty/imgui` submodule URL on `main`, also reported on other PRs; they do not look specific to this change.
adriadam10 commented 2026-10-11 21:05:26 +00:00 (Migrated from github.com)

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

Review: APPROVED


Checked the diff against main, the error mapping from the earlier review (sceKernelLseek returns SceIoErrorFromErrno for directory seeks, EINVAL/EOVERFLOW covered in the test) and the TechnicalDebt entry. Build and ctest are green on Linux; guest_directory_rewind is Windows-only and was confirmed on Windows 11 by you and @MarcZX100. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5484946178 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3452#pullrequestreview-5484946178). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:05:26Z. **Review:** APPROVED --- Checked the diff against `main`, the error mapping from the earlier review (`sceKernelLseek` returns `SceIoErrorFromErrno` for directory seeks, `EINVAL`/`EOVERFLOW` covered in the test) and the TechnicalDebt entry. Build and ctest are green on Linux; `guest_directory_rewind` is Windows-only and was confirmed on Windows 11 by you and @MarcZX100. Merging.
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!3452
No description provided.