fix: repair CI failures and collateral bugs from PR #2478 #2649

Closed
sridhar777911 wants to merge 4 commits from upstream-pr/2649 into main
sridhar777911 commented 2026-10-10 09:58:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by sridhar777911 on 2026-10-10T09:58:37Z.


What

This PR fixes the failing CI pipeline checks originally present in PR #2478. During the workflow, additional missing code and collateral issues were found and fixed.

Tested

  • Rechecked locally. Passed check_conventions.py
  • Passed ctest suite
  • Built on Windows 11

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:4810359675 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2649). Originally posted by [sridhar777911](https://github.com/sridhar777911) on 2026-10-10T09:58:37Z. --- ### What This PR fixes the failing CI pipeline checks originally present in PR #2478. During the workflow, additional missing code and collateral issues were found and fixed. ### Tested - Rechecked locally. Passed `check_conventions.py` - Passed `ctest` suite - Built on Windows 11 ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [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
github-actions[bot] commented 2026-10-10 10:01:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T10:01:44Z.


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
#2478 dup2_nid_postfix
dup_nid_postfix
core/libs/tests/GuestDup.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096369086 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2649#issuecomment-6096369086). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T10:01:44Z. --- <!-- 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 | |---|---|---|---| | #2478 | | `dup2_nid_postfix`<br>`dup_nid_postfix` | `core/libs/tests/GuestDup.cpp` |
Mouhand-Kaddo commented 2026-10-10 10:19:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-10T10:19:19Z.


Additional platform validation on standalone PR head 99763250a9dc15461ebf6218dafda7d0601381a9:

Linux: passed — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja.

Release / BUILD_TESTING=ON: built the selected test targets; 4 CTest cases, 0 failures, 0 CTest skips.

Tests: guest_filesystem, guest_udp, guest_dup, guest_file_vector.

Reproduce after building the test targets: ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^guest_(dup|filesystem|udp|socket|file_vector|stdio|fd_ownership|file_descriptor)$".

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096499673 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2649#issuecomment-6096499673). Originally posted by [Mouhand-Kaddo](https://github.com/Mouhand-Kaddo) on 2026-10-10T10:19:19Z. --- <!-- anyps5-platform-validation:99763250a9dc15461ebf6218dafda7d0601381a9 --> Additional platform validation on standalone PR head `99763250a9dc15461ebf6218dafda7d0601381a9`: **Linux: passed** — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja. Release / `BUILD_TESTING=ON`: built the selected test targets; 4 CTest cases, 0 failures, 0 CTest skips. Tests: `guest_filesystem`, `guest_udp`, `guest_dup`, `guest_file_vector`. Reproduce after building the test targets: `ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^guest_(dup|filesystem|udp|socket|file_vector|stdio|fd_ownership|file_descriptor)$"`.
sandexzx commented 2026-10-10 11:38:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by sandexzx on 2026-10-10T11:38:57Z.


dup / dup2 do not keep the random-device bookkeeping consistent

I reviewed the code in this PR (it is the same code as #2478; only the merge with main is new). I found one real defect that is not documented anywhere, plus one Windows-only defect.

The kernel keeps per-descriptor tables for the host resources it emulates: g_descriptors in core/libs/prx/libkernel/File/src/RandomDevice.cpp:20 and g_locks in core/libs/prx/libkernel/File/src/FileLock.cpp. close_nid_postfix (core/libs/prx/libkernel/File/src/Stdio.cpp:288-298) removes the entry from each table. dup_nid_postfix and dup2_nid_postfix do not. The new descriptor then has the wrong entry, so it reads the wrong data or takes a lock that does not exist.

1. dup2 onto a random descriptor keeps the stale registration — core/libs/prx/libkernel/File/src/Stdio.cpp:319-341

dup2_nid_postfix makes to refer to the object of from. The host call closes the old to. But g_descriptors still holds to (RandomDevice.cpp:42-44). A later read of to goes through ReadRandomDevice (RandomDevice.cpp:47-49) and returns random bytes, not the data that dup2 moved there. This is silent data corruption.

Reproduce:

r = open("/dev/urandom", O_RDONLY);
f = open("data", O_RDONLY);
dup2(f, r);
read(r, buf, n);   // expected: bytes of "data"; actual: random bytes

The mirror case is also wrong: when from is a random descriptor, the target never enters g_descriptors, so it reads from the host null device.

2. Windows: the file-lock cache is not updated — core/libs/prx/libkernel/File/src/Stdio.cpp:330-336

The Windows branch calls File::ForgetDirectoryDescriptor(to), but not File::ForgetFileLock(to). The old to stays in g_locks. A later flock on the replaced descriptor then hits HeldLock(fd) == mode (FileLock.cpp:53-54) and returns success without calling LockFileEx. The caller believes it holds a lock that does not exist.

3. dup of a random descriptor reads as EOF — Stdio.cpp:304-317

This is already listed in docs/dev/TechnicalDebt.md ("a duped descriptor reads as an empty file"). It is an accepted limitation, not a new bug. I list it only because a fix for point 1 must also handle the source side.

Suggested direction

  • In dup / dup2, read the resource state of from before the host call, and change the tables only after the host call succeeds. close_nid_postfix (Stdio.cpp:288-298) is the model: one cleanup point per descriptor.
  • Windows: add File::ForgetFileLock(to) next to the existing File::ForgetDirectoryDescriptor(to).
  • dup2(fd, fd) and a failed dup2 must leave the tables unchanged.
  • Add regression tests for random descriptors in core/libs/tests/GuestDup.cpp. The current tests cover pipes, plain files and sockets, but not /dev/urandom.

GuestSockets::DuplicateTo (core/libs/prx/libkernel/Socket/src/Udp.cpp:237-250) sets nextDescriptor = target + 1 when target >= nextDescriptor (Udp.cpp:245). With target = INT_MAX - 1 the counter reaches INT_MAX, and every later dup / socket / socketpair / accept fails with EMFILE for the rest of the process (Udp.cpp:227,422,501). Is that range reachable in practice, or should the counter wrap?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097103236 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2649#issuecomment-6097103236). Originally posted by [sandexzx](https://github.com/sandexzx) on 2026-10-10T11:38:57Z. --- ### `dup` / `dup2` do not keep the random-device bookkeeping consistent I reviewed the code in this PR (it is the same code as #2478; only the merge with `main` is new). I found one real defect that is not documented anywhere, plus one Windows-only defect. The kernel keeps per-descriptor tables for the host resources it emulates: `g_descriptors` in `core/libs/prx/libkernel/File/src/RandomDevice.cpp:20` and `g_locks` in `core/libs/prx/libkernel/File/src/FileLock.cpp`. `close_nid_postfix` (`core/libs/prx/libkernel/File/src/Stdio.cpp:288-298`) removes the entry from each table. `dup_nid_postfix` and `dup2_nid_postfix` do not. The new descriptor then has the wrong entry, so it reads the wrong data or takes a lock that does not exist. **1. `dup2` onto a random descriptor keeps the stale registration** — `core/libs/prx/libkernel/File/src/Stdio.cpp:319-341` `dup2_nid_postfix` makes `to` refer to the object of `from`. The host call closes the old `to`. But `g_descriptors` still holds `to` (`RandomDevice.cpp:42-44`). A later read of `to` goes through `ReadRandomDevice` (`RandomDevice.cpp:47-49`) and returns random bytes, not the data that `dup2` moved there. This is silent data corruption. Reproduce: ``` r = open("/dev/urandom", O_RDONLY); f = open("data", O_RDONLY); dup2(f, r); read(r, buf, n); // expected: bytes of "data"; actual: random bytes ``` The mirror case is also wrong: when `from` is a random descriptor, the target never enters `g_descriptors`, so it reads from the host null device. **2. Windows: the file-lock cache is not updated** — `core/libs/prx/libkernel/File/src/Stdio.cpp:330-336` The Windows branch calls `File::ForgetDirectoryDescriptor(to)`, but not `File::ForgetFileLock(to)`. The old `to` stays in `g_locks`. A later `flock` on the replaced descriptor then hits `HeldLock(fd) == mode` (`FileLock.cpp:53-54`) and returns success without calling `LockFileEx`. The caller believes it holds a lock that does not exist. **3. `dup` of a random descriptor reads as EOF** — `Stdio.cpp:304-317` This is already listed in `docs/dev/TechnicalDebt.md` ("a `dup`ed descriptor reads as an empty file"). It is an accepted limitation, not a new bug. I list it only because a fix for point 1 must also handle the source side. ### Suggested direction - In `dup` / `dup2`, read the resource state of `from` before the host call, and change the tables only after the host call succeeds. `close_nid_postfix` (`Stdio.cpp:288-298`) is the model: one cleanup point per descriptor. - Windows: add `File::ForgetFileLock(to)` next to the existing `File::ForgetDirectoryDescriptor(to)`. - `dup2(fd, fd)` and a failed `dup2` must leave the tables unchanged. - Add regression tests for random descriptors in `core/libs/tests/GuestDup.cpp`. The current tests cover pipes, plain files and sockets, but not `/dev/urandom`. ### Related question `GuestSockets::DuplicateTo` (`core/libs/prx/libkernel/Socket/src/Udp.cpp:237-250`) sets `nextDescriptor = target + 1` when `target >= nextDescriptor` (`Udp.cpp:245`). With `target = INT_MAX - 1` the counter reaches `INT_MAX`, and every later `dup` / `socket` / `socketpair` / `accept` fails with `EMFILE` for the rest of the process (`Udp.cpp:227,422,501`). Is that range reachable in practice, or should the counter wrap?
oneandonlydean commented 2026-10-10 18:08:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T18:08:22Z.


Closing: this is the same dup/dup2 change as #2478, which is green and already fixes the random-device and file-lock bookkeeping still missing here. Anything else you found belongs on #2478.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6100604574 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2649#issuecomment-6100604574). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T18:08:22Z. --- Closing: this is the same dup/dup2 change as #2478, which is green and already fixes the random-device and file-lock bookkeeping still missing here. Anything else you found belongs on #2478.

Pull request closed

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!2649
No description provided.