feat(libkernel): implement dup and dup2 #2441
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2441"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Recapi on 2026-10-09T20:29:36Z.
What
dupanddup2in libkernel.dup/dup2(_dup/_dup2on Windows), which share the open file, offset and flags as FreeBSD does.dup2(fd, fd)returnsfdwhen it is open.GuestSockets::FirstDescriptor):dupgives a new socket number that shares the socket, which stays open until its last number is closed;dup2onto another socket number replaces what was there and keeps later socket numbers above it.kern_dup: a negative or closed source, or a negative target, is -1 withEBADF;EMFILEand other host failures are passed through.Prior work: #1855 (
dup) and #1870 (dup2) were closed because no title stopped at them onmainthen.Log
Linux (Ubuntu, GCC 15.2.0),
main8b393b6ewith nothing else merged; catalog release ZIPs checked against their sha256,eboot.binand bundled modules unwrapped from their SELF containers, relinked withrelinker --registry, run in abwrapsandbox (no network, read-only filesystem). These are the stops for both titles in the catalog table in #1578.XPSemu (PPSA97358, Alpha-2), on
main:With this PR it stops at the next absent import,
__inet_aton(#1848):Prospero Win (PPSA99995, 0.1.1), on
main:With this PR it stops at
sigemptyset(#1641):In their sources both titles use
dup(1)/dup2(logfd, 1)to redirect stdout and stderr to a log (util/log.cin XPSemu,native/ps5log/ps5log.hin Prospero Win). Prospero Win also hasdup2(socketpair end, 1)(native/wine64_main.c,capture_fd), which this PR throws on; it runs after the title loads Wine, past several other absent imports.Tested
Ubuntu, Linux 7.0, GCC 15.2.0, full build with
BUILD_TESTING=ON. New testguest_dup: adupof a pipe end outlives the original,dup2onto an open pipe end closes it (its reader sees EOF),dup2(fd, fd),EBADFfor negative and closed descriptors, socketdupanddup2(including onto an unused socket number, with latersocketpairnumbers above it, and onto an open socket), and the two cross-numbering throws. Patchedlibkernel.prxexportsiiQjzvfWDq0andwdUufa9g-D8. Both titles run before and after as above.The project's Build workflow on the fork, on this commit (a fork-only branch adds the previous
build.yml, since the current one only runs for pull requests andmain): https://github.com/Recapi/AnyPS5/actions/runs/37983997708: linux 521/521 and windows (MinGW) 512/512 withguest_duppassing, relinker on ubuntu, windows and macos passing.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T20:49:46Z.
Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.
Imported from boykopovar/AnyPS5. Originally posted by popeee99 on 2026-10-09T21:22:56Z.
Thanks @Recapi for the socket duplication implementation. I independently tested its allocation-failure paths.
Independent fault-injection finding at
1864f3766e: the new socketdup/dup2paths letstd::bad_allocescape fromsockets.emplace/insert_or_assign. Adjacent socket exports already translate this failure to-1/ guestENOMEM(12).There is also a state-recovery consequence:
dup2(source, INT_MAX-2)leaves one synthetic descriptor available. A failed allocation indup(source)advancesnextDescriptorbefore insertion; retry incorrectly returnsEMFILE(24). This needs only a few sockets, not real memory exhaustion.A proposed minimal correction catches
bad_allocin both methods and advances the counter only after successful insertion. A harness linked against the actual production translation units confirms:guest_dup,guest_socket_pair,guest_file_vectortests pass 3/3 on both versions.Reproduction, proposed patch and complete logs: patch, reproduction instructions and test results.
Tested on Linux/WSL with GCC 15.2. The harness catches the escaping exception; no game-process crash or native PS5 OOM contract is claimed. Original socket usability, successful-alias lifetime and bounded native-FD cleanup were checked; Windows/macOS, concurrency, full-emulator behavior and CI were not validated.
I see intake has paused and this PR was automatically closed. Sharing a focused finding for future use, without opening a PR or requesting an intake exception. AI-assisted with OpenAI Codex.
Imported from boykopovar/AnyPS5. Originally posted by Recapi on 2026-10-09T21:59:57Z.
Thanks @popeee99, confirmed: the other socket exports in
Udp.cpp(socket,socketpair,accept) already returnENOMEMonstd::bad_alloc, andDuplicateadvancednextDescriptorbefore the insert. I applied your patch unchanged to the branch:Recapi:feat/libkernel-dupis now430fcdc2.guest_dup,guest_socket_pairandguest_file_vectorpass locally (Linux, 3 runs), and the Build workflow on the fork passes on it: https://github.com/Recapi/AnyPS5/actions/runs/37993776038 (linux 521/521, windows 512/512).Pull request closed