fix(libkernel): share the directory state between duplicated descriptors on Windows #3872

Merged
LeandroLP23 merged 1 commit from upstream-pr/3872 into main 2026-10-12 20:18:03 +00:00
LeandroLP23 commented 2026-10-11 18:20:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-11T18:20:52Z.


What

On Windows, dup and dup2 of a directory descriptor threw (duplicating a directory descriptor is not supported on Windows). In FreeBSD's kern_dup, both descriptors refer to one open file, so they share the read position. With this change, the duplicate points at the same DirectoryState that getdents and lseek go through, and keeps it after the original is closed. dup2 onto an open descriptor first drops whatever the target was, as before. The TechnicalDebt dup and dup2 entry no longer lists the case.

This builds on #3452 (merged): it uses the shared DirectoryState that #3452 introduced, and its test extends guest_directory_rewind.

Tested

Radeon 860M, Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, on #3452's head ce0239c94:

  • guest_directory_rewind passes 3/3. The new checks are:
    • dup shares the cursor, in both directions;
    • lseek on one descriptor moves the other;
    • dup2 onto an open file descriptor works, and the result still reads after the source is closed.
  • With #3452's Stdio.cpp, the test fails with the dup_nid_postfix throw.
  • The other guest_ file and kernel tests pass, except guest_filesystem and guest_kernel_sys, which fail the same way on main (#3518, #3510).
  • tools/check_conventions.py passes.

Fork CI: https://github.com/LeandroLP23/AnyPS5/actions/runs/38161248929

  • Windows: guest_directory_rewind passes. The other failures also happen on main: guest_filesystem, guest_kernel_sys and windows_exceptions.
  • Linux: main's agc_driver_emulated_color_compare, agc_driver_image_plane_address_on_line_implicit_lod and guest_kernel_stack_protect fail, plus a timeout in agc_driver_flat_apertures_0 on lavapipe. The change is all inside #ifdef _WIN32, so none of it is built on Linux.

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
  • Unblocks: none
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4821273376 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3872). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-11T18:20:52Z. --- ### What On Windows, `dup` and `dup2` of a directory descriptor threw (`duplicating a directory descriptor is not supported on Windows`). In FreeBSD's `kern_dup`, both descriptors refer to one open file, so they share the read position. With this change, the duplicate points at the same `DirectoryState` that `getdents` and `lseek` go through, and keeps it after the original is closed. `dup2` onto an open descriptor first drops whatever the target was, as before. The TechnicalDebt `dup and dup2` entry no longer lists the case. This builds on #3452 (merged): it uses the shared `DirectoryState` that #3452 introduced, and its test extends `guest_directory_rewind`. ### Tested Radeon 860M, Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, on #3452's head `ce0239c94`: - `guest_directory_rewind` passes 3/3. The new checks are: - `dup` shares the cursor, in both directions; - `lseek` on one descriptor moves the other; - `dup2` onto an open file descriptor works, and the result still reads after the source is closed. - With #3452's `Stdio.cpp`, the test fails with the `dup_nid_postfix` throw. - The other `guest_` file and kernel tests pass, except `guest_filesystem` and `guest_kernel_sys`, which fail the same way on `main` (#3518, #3510). - `tools/check_conventions.py` passes. Fork CI: https://github.com/LeandroLP23/AnyPS5/actions/runs/38161248929 - Windows: `guest_directory_rewind` passes. The other failures also happen on `main`: `guest_filesystem`, `guest_kernel_sys` and `windows_exceptions`. - Linux: `main`'s `agc_driver_emulated_color_compare`, `agc_driver_image_plane_address_on_line_implicit_lod` and `guest_kernel_stack_protect` fail, plus a timeout in `agc_driver_flat_apertures_0` on lavapipe. The change is all inside `#ifdef _WIN32`, so none of it is built on Linux. ### 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] Unblocks: none - [x] AI-assisted: yes (Claude Code)
xXPBSXx commented 2026-10-12 13:43:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T13:43:14Z.


Windows test of head ee8ff43cb merged onto current main (c6efe068a, clean merge; built together with three other open PRs from the same list, so the numbers below are for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors.

guest_directory_rewind (the test this PR changes) passes, with guest_directory, guest_directory_buffer, guest_directory_close, guest_dup and guest_file_* (20 of 20 in the filter audio_out|guest_directory|guest_dup|guest_kernel_sys|guest_sce_net|guest_file).

Not checked: that the test fails without the change, Linux, a title run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6125498327 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3872#issuecomment-6125498327). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T13:43:14Z. --- Windows test of head `ee8ff43cb` merged onto current `main` (`c6efe068a`, clean merge; built together with three other open PRs from the same list, so the numbers below are for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors. `guest_directory_rewind` (the test this PR changes) passes, with `guest_directory`, `guest_directory_buffer`, `guest_directory_close`, `guest_dup` and `guest_file_*` (20 of 20 in the filter `audio_out|guest_directory|guest_dup|guest_kernel_sys|guest_sce_net|guest_file`). Not checked: that the test fails without the change, Linux, a title run. AI-assisted (Claude Code).
github-actions[bot] commented 2026-10-12 19:05:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T19:05:58Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3649 core/libs/prx/libkernel/File/src/Stdio.cpp L551-552
#4391 docs/dev/debt/libkernel.md L126
#4585 docs/dev/debt/libkernel.md L124-126
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6130792207 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3872#issuecomment-6130792207). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T19:05:58Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #3649 | `core/libs/prx/libkernel/File/src/Stdio.cpp` [L551-552](https://github.com/boykopovar/AnyPS5/blob/0428328b79af7820990ffc6964fb9009041bcdcf/core/libs/prx/libkernel/File/src/Stdio.cpp#L551-L552) | | | | #4391 | `docs/dev/debt/libkernel.md` [L126](https://github.com/boykopovar/AnyPS5/blob/0428328b79af7820990ffc6964fb9009041bcdcf/docs/dev/debt/libkernel.md#L126) | | | | #4585 | `docs/dev/debt/libkernel.md` [L124-126](https://github.com/boykopovar/AnyPS5/blob/0428328b79af7820990ffc6964fb9009041bcdcf/docs/dev/debt/libkernel.md#L124-L126) | | |
LeandroLP23 commented 2026-10-12 19:35:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-12T19:35:56Z.


On the overlap comment: none of these depend on this PR or the other way round, and each conflict is resolved by keeping both changes.

  • @pushkarreddyy #3649 adds its pipe-end line right after _dup(d) in dup_nid_postfix, the same spot where this PR adds the directory one. Both lines stay.
  • @MarcZX100 #4391 rewrites the first clause of the dup and dup2 debt entry; this PR drops the last one ("and on Windows duplicating a directory descriptor"). The merged entry keeps #4391's wording without that clause.
  • @jagrat04 #4585 edits the two cpuset_* entries just above that entry, so the conflict is only the neighbouring lines.

Whichever merges first, I'll rebase this one if it's still open.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6131274434 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3872#issuecomment-6131274434). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-12T19:35:56Z. --- On the overlap comment: none of these depend on this PR or the other way round, and each conflict is resolved by keeping both changes. - @pushkarreddyy #3649 adds its pipe-end line right after `_dup(d)` in `dup_nid_postfix`, the same spot where this PR adds the directory one. Both lines stay. - @MarcZX100 #4391 rewrites the first clause of the `dup and dup2` debt entry; this PR drops the last one ("and on Windows duplicating a directory descriptor"). The merged entry keeps #4391's wording without that clause. - @jagrat04 #4585 edits the two `cpuset_*` entries just above that entry, so the conflict is only the neighbouring lines. Whichever merges first, I'll rebase this one if it's still open.
adriadam10 commented 2026-10-12 20:17:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-12T20:17:57Z.


Re-checked after the rebase onto main (#3452 is in): dup/dup2 share the DirectoryState through the existing shared_ptr map, dup2 drops the target's state before duplicating, the original can close without invalidating the duplicate, and the TechnicalDebt entry is updated. Local Linux build and ctest are green; the change is all _WIN32, so the Windows runs reported here (yours and @xXPBSXx's) are what cover the test. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6131908527 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3872#issuecomment-6131908527). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-12T20:17:57Z. --- Re-checked after the rebase onto `main` (#3452 is in): `dup`/`dup2` share the `DirectoryState` through the existing `shared_ptr` map, `dup2` drops the target's state before duplicating, the original can close without invalidating the duplicate, and the TechnicalDebt entry is updated. Local Linux build and ctest are green; the change is all `_WIN32`, so the Windows runs reported here (yours and @xXPBSXx's) are what cover the test. 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!3872
No description provided.