feat(libkernel): implement dup2 #1870

Closed
TheAggie wants to merge 2 commits from upstream-pr/1870 into main
TheAggie commented 2026-10-08 22:27:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-08T22:27:46Z.


What

dup2 was not exported. ccleste (PPSA59322) calls it to move its standard error onto a pipe that it forwards to the kernel log.

Two commits:

  1. dup2. Guest file descriptors are host CRT descriptors, so dup2 duplicates through the host (_dup2 on Windows, dup2 elsewhere).
    • A negative or closed source, or a negative target, fails with EBADF. Duplicating a descriptor onto itself returns it.
    • On Windows, a directory descriptor's path entry for the target is forgotten, because the target now names something else.
    • Socket descriptors live in their own table, so a socket on either side throws. On Windows a directory source throws too, for the same reason.
  2. sceKernelDebugOutText keeps the host's standard error. Because guest descriptors are host descriptors, a guest that moves descriptor 2 also moves the stream sceKernelDebugOutText writes to. ccleste forwards its standard error into the kernel log, so it read its own debug output back and logged it again without end. Before the guest's first dup2 onto descriptor 2, the host's standard error is duplicated and kept, and sceKernelDebugOutText writes there from then on.

Tests (guest_filesystem):

  • A pipe's write end duplicated to descriptor 60 still writes into the pipe after the original is closed. Same-descriptor and the three EBADF cases are covered.
  • With the host's standard error captured to a file and a guest pipe moved onto descriptor 2, sceKernelDebugOutText output reaches the captured host stream, and a guest write(2, ...) reaches the pipe.

Not covered: on Windows, the CRT's _dup2 onto descriptor 2 also changes the process's STD_ERROR_HANDLE, so AnyPS5's own crash reports written through that handle end up in the guest's pipe too. Restoring the Win32 handle would be a separate change.

Tested

Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release.

  • The full suite (485 tests) passes on current main with this PR and four other small libc/libkernel PRs of mine merged together (atol, getprogname, dup2, ctime/rewind, pthread_set_name_np). It was run from the generated CTestTestfile.cmake commands because antivirus blocks ctest.exe on this machine.
  • tools/check_conventions.py --base origin/main passes on this branch alone.

Not tested on Linux or macOS.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4793542820 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1870). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-08T22:27:46Z. --- ### What `dup2` was not exported. ccleste (PPSA59322) calls it to move its standard error onto a pipe that it forwards to the kernel log. Two commits: 1. **`dup2`.** Guest file descriptors are host CRT descriptors, so `dup2` duplicates through the host (`_dup2` on Windows, `dup2` elsewhere). - A negative or closed source, or a negative target, fails with `EBADF`. Duplicating a descriptor onto itself returns it. - On Windows, a directory descriptor's path entry for the target is forgotten, because the target now names something else. - Socket descriptors live in their own table, so a socket on either side throws. On Windows a directory source throws too, for the same reason. 2. **`sceKernelDebugOutText` keeps the host's standard error.** Because guest descriptors are host descriptors, a guest that moves descriptor 2 also moves the stream `sceKernelDebugOutText` writes to. ccleste forwards its standard error into the kernel log, so it read its own debug output back and logged it again without end. Before the guest's first `dup2` onto descriptor 2, the host's standard error is duplicated and kept, and `sceKernelDebugOutText` writes there from then on. Tests (`guest_filesystem`): - A pipe's write end duplicated to descriptor 60 still writes into the pipe after the original is closed. Same-descriptor and the three `EBADF` cases are covered. - With the host's standard error captured to a file and a guest pipe moved onto descriptor 2, `sceKernelDebugOutText` output reaches the captured host stream, and a guest `write(2, ...)` reaches the pipe. Not covered: on Windows, the CRT's `_dup2` onto descriptor 2 also changes the process's `STD_ERROR_HANDLE`, so AnyPS5's own crash reports written through that handle end up in the guest's pipe too. Restoring the Win32 handle would be a separate change. ### Tested Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. - The full suite (485 tests) passes on current `main` with this PR and four other small libc/libkernel PRs of mine merged together (atol, getprogname, dup2, ctime/rewind, pthread_set_name_np). It was run from the generated `CTestTestfile.cmake` commands because antivirus blocks `ctest.exe` on this machine. - `tools/check_conventions.py --base origin/main` passes on this branch alone. Not tested on Linux or macOS. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-08 22:32:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T22:32:24Z.


Progress report (ce64d8e → bf51014)

➖ System libraries: 84.76% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel dup2_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6070380840 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1870#issuecomment-6070380840). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T22:32:24Z. --- <!-- progress-report --> ### Progress report (ce64d8e → bf51014) ➖ **System libraries**: 84.76% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `dup2_nid_postfix` | </details>
github-actions[bot] commented 2026-10-09 09:04:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T09:04:08Z.


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
#1755 core/libs/tests/GuestFilesystem.cpp L29
#1764 core/libs/tests/GuestFilesystem.cpp L29
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6077843339 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1870#issuecomment-6077843339). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T09:04:08Z. --- <!-- 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 | |---|---|---|---| | #1755 | `core/libs/tests/GuestFilesystem.cpp` [L29](https://github.com/boykopovar/AnyPS5/blob/d9484c719b2a5ac6201e27f8657e0fb0f01e648c/core/libs/tests/GuestFilesystem.cpp#L29) | | | | #1764 | `core/libs/tests/GuestFilesystem.cpp` [L29](https://github.com/boykopovar/AnyPS5/blob/d9484c719b2a5ac6201e27f8657e0fb0f01e648c/core/libs/tests/GuestFilesystem.cpp#L29) | | |
oneandonlydean commented 2026-10-09 09:30:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:30:21Z.


Closing: ccleste (PPSA59322) stops at shm_open on main before it calls dup2. Reopen when dup2 is its next stop on main, with the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078239868 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1870#issuecomment-6078239868). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:30:21Z. --- Closing: ccleste (PPSA59322) stops at `shm_open` on main before it calls `dup2`. Reopen when `dup2` is its next stop on main, with the log line.
Recapi commented 2026-10-09 13:50:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Recapi on 2026-10-09T13:50:59Z.


@TheAggie dup2 is now a title's next stop on main: Prospero Win (PPSA99995, catalog record 0.1.1, sha256 checked) stops at it on main d70b8998, with nothing else merged:

./app.elf: symbol lookup error: ./app.elf: undefined symbol: wdUufa9g-D8

Linux (Ubuntu, GCC 15.2.0), eboot.bin unwrapped from its SELF container, relinked with relinker --registry, run in a bwrap sandbox. tools/import_audit.py lists 9 absent imports for it; besides dup2 they are dup, seteuid, sigemptyset, getdents, sigaction, geteuid, sceKernelGetModuleInfo and sceSysmoduleGetModuleHandleInternal, so it should stop at the others afterwards.

That meets the reopen condition in the comment above, if you want to pick it back up. No open PR implements dup2 (checked against the 555 open PRs).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6082278396 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1870#issuecomment-6082278396). Originally posted by [Recapi](https://github.com/Recapi) on 2026-10-09T13:50:59Z. --- @TheAggie `dup2` is now a title's next stop on `main`: Prospero Win (PPSA99995, [catalog](https://github.com/blackbearreloaded/ps5-homebrew-catalog) record 0.1.1, sha256 checked) stops at it on `main` d70b8998, with nothing else merged: ``` ./app.elf: symbol lookup error: ./app.elf: undefined symbol: wdUufa9g-D8 ``` Linux (Ubuntu, GCC 15.2.0), `eboot.bin` unwrapped from its SELF container, relinked with `relinker --registry`, run in a `bwrap` sandbox. `tools/import_audit.py` lists 9 absent imports for it; besides `dup2` they are `dup`, `seteuid`, `sigemptyset`, `getdents`, `sigaction`, `geteuid`, `sceKernelGetModuleInfo` and `sceSysmoduleGetModuleHandleInternal`, so it should stop at the others afterwards. That meets the reopen condition in the comment above, if you want to pick it back up. No open PR implements `dup2` (checked against the 555 open PRs).
rmz-24 commented 2026-10-09 16:01:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rmz-24 on 2026-10-09T16:01:27Z.


Windows confirmation of the above, in case it helps reopening: Windows 11 Pro 25H2 (10.0.26200.9457), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Prospero Win PPSA99995 (catalog record 0.1.1, sha256 checked), eboot.bin unwrapped from its SELF and relinked with --windows.

  • main d70b8998, nothing else merged: FAIL: unresolved ELF import wdUufa9g-D8 from libkernel.prx (dup2), the first stop.
  • main + this branch (bf510143, both commits cherry-pick cleanly): the title gets past dup2 and stops at the next absent import, +F7C-hdk7+E (sigemptyset, #1641).
  • guest_filesystem passes, and the full ctest is 510/510.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084525306 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1870#issuecomment-6084525306). Originally posted by [rmz-24](https://github.com/rmz-24) on 2026-10-09T16:01:27Z. --- Windows confirmation of the above, in case it helps reopening: Windows 11 Pro 25H2 (10.0.26200.9457), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Prospero Win PPSA99995 (catalog record 0.1.1, sha256 checked), `eboot.bin` unwrapped from its SELF and relinked with `--windows`. - `main` d70b8998, nothing else merged: `FAIL: unresolved ELF import wdUufa9g-D8 from libkernel.prx` (`dup2`), the first stop. - `main` + this branch (bf510143, both commits cherry-pick cleanly): the title gets past `dup2` and stops at the next absent import, `+F7C-hdk7+E` (`sigemptyset`, #1641). - `guest_filesystem` passes, and the full ctest is 510/510. AI-assisted: yes
TheAggie commented 2026-10-10 04:49:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-10T04:49:14Z.


Thanks @Recapi and @rmz-24 for the Prospero Win runs. Since this was closed, #2478 by @itsbo5 implements both dup and dup2 (with guest sockets) and has a Windows check from @babyhuey, so it covers more than this PR. I'll leave this one closed in favour of #2478.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6093921653 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1870#issuecomment-6093921653). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-10T04:49:14Z. --- Thanks @Recapi and @rmz-24 for the Prospero Win runs. Since this was closed, #2478 by @itsbo5 implements both `dup` and `dup2` (with guest sockets) and has a Windows check from @babyhuey, so it covers more than this PR. I'll leave this one closed in favour of #2478. AI-assisted (Claude Code).

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