feat(libkernel): implement dup and dup2 #2478
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2478"
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 itsbo5 on 2026-10-09T22:41:07Z.
What
Implement
dupanddup2inlibkernel.dup/dup2(_dup/_dup2on Windows).GuestSockets::FirstDescriptor) share the underlying socket state.-1withEBADFfor invalid descriptors as in FreeBSDkern_dup.Tested
Full build with
BUILD_TESTING=ON:GuestDuptests pass: pipe duplicate outlives original, dup2 target replacement, invalid descriptors, and socket duplication.tools/check_conventions.py --base upstream/mainpassed.Checklist
main; no other open PR implements the same functionsImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T22:48:10Z.
Checked: rebased on current main, full build and ctest green (
guest_duppasses, and fails to link without the change). Error returns match FreeBSDkern_dup; unmodelled combinations throw.Added as our own commit on your branch:
dup/dup2throw on, the shared guest-socket object and the descriptor numbering.Not verified here: Windows runtime behaviour of
_dup/_dup2(code review only).Merging once CI is green.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T23:00:44Z.
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 github-actions[bot] on 2026-10-09T23:37:06Z.
Progress report (
197ffac→df131b9)📈 System libraries: 84.56% (+0.01%, +2 implemented functions)
✅ 2 implemented
dup2_nid_postfixdup_nid_postfixImported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-10T01:41:30Z.
Windows runtime check, since the review above left it at code review: this PR (
5234b196) merged locally ontomainc9da0f93(clean), Windows 11, MinGW-w64 GCC from WinLibs, Release.guest_duppasses 5/5 (--repeat until-fail:5). This runs the_dup,_dup2and_get_osfhandlepaths; no CRT assertion dialog or abort.dup2. Onmainit stops atFAIL: unresolved ELF import wdUufa9g-D8 (dup2) from libkernel.prx; with this PR it stops atFAIL: unresolved ELF import D8cuU4d72xM from libSceSysmodule.prx(sceSysmoduleGetModuleHandleInternal).qlWiRfOJx1A(#545) on thismainbefore it reachesdup, with or without this PR.Linux (Ubuntu 24.04, GCC 13) on the same merge gives the same:
guest_dup5/5, Prospero Winundefined symbol: D8cuU4d72xM, XPSemuundefined symbol: qlWiRfOJx1A.Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-10T04:51:49Z.
Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head
5234b196, alone onmainafb8cd22.guest_duppasses 3 of 3.video_out_flip_lifetime, which also crashes intermittently onmainwithout this PR (#2540).No title was run.
🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by sandexzx on 2026-10-10T08:28:40Z.
Review:
dup/dup2do not maintain the random-device registration invariant/dev/randomand/dev/urandomare emulated:OpenRandomDevice()opens the host/dev/nulland records the fd ing_descriptors(core/libs/prx/libkernel/File/src/RandomDevice.cpp:30), and reads are intercepted by fd number (RandomDevice.cpp:49). The invariant is: every host fd backing an emulated random device is present ing_descriptors, and is removed on close/replace.dup_nid_postfixanddup2_nid_postfixcreate fd aliases but do not update that set, so both break the invariant. Reproduced locally on Linux (lavapipe, GCC, Release) by exercising the exported functions of the builtlibkernel.prx.Bug 1 —
dupof a random-device fd returns EOFcore/libs/prx/libkernel/File/src/Stdio.cpp:311-316: after a successful::dup(d), the duplicate is never added tog_descriptors. It points at the host/dev/null, soReadRandomDevicedoes not intercept reads and they return EOF.Bug 2 —
dup2leaves a stale registration on the replaced targetcore/libs/prx/libkernel/File/src/Stdio.cpp:338-339:::dup2(from, to)closes the oldto, butForgetRandomDevice(to)is not called. Iftowas a random-device fd,IsRandomDevice(to)stays true and subsequent reads ontoreturn random bytes instead of the new object's content — a silent data corruption. The reverse case (fromis a random device) is the mirror of Bug 1: the new target is never registered.Suggested fix (scoped to this PR)
void RememberRandomDevice(int fd)(register an existing fd) next toOpenRandomDeviceinRandomDevice.hpp/.cpp.dup_nid_postfix, after the success/range checks:if (File::IsRandomDevice(d)) File::RememberRandomDevice(duplicate);.dup2_nid_postfix, snapshotconst bool sourceIsRandom = File::IsRandomDevice(from);before the native call, and only on success:File::ForgetRandomDevice(to); if (sourceIsRandom) File::RememberRandomDevice(to);. Keep the target untouched on failure, and keep thefrom == tono-op from losing the registration._dup/_dup2paths), plusFile::ForgetFileLock(to)next to the existingForgetDirectoryDescriptor(to)(Stdio.cpp:335) — the current code leaves a stale entry ing_locks, andFile::Flockreturns success for it without actually locking the new file (File/src/FileLock.cpp:53-54).core/libs/tests/GuestDup.cppdoes not cover random devices at all — please add scenarios fordup(random),dup2(pipe_read, random_fd),dup2(random, host_fd),dup2(random, random), and the error/from == tocases.Out of scope (separate issues, not blockers for this PR)
fdopen/fclosebypassForgetRandomDevice:libc/src/FileAndHeap.cpp:47-66wraps the fd with nativefdopenwithout duplicating, andFileStream::Close()callsstd::fclosedirectly (libc/include/FileStream.hpp:102-104);freopenlikewise (FileStream.hpp:69-73). Reproduced: afterfclose, a reused fd number still reads as random. Pre-existing.ReadRandomIovecs(File/src/Stdio.cpp:618) ignores thefalsereturn ofReadRandomDeviceand still adds the length.Remember/Forgetonly protect thestd::set, so a concurrent reader can still observe the target as random between the nativedup2and the bookkeeping. The fix above does not address this; flagging it as a known limitation.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T10:01:41Z.
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.
main)dup2_nid_postfixdup_nid_postfixcore/libs/tests/GuestDup.cppImported from boykopovar/AnyPS5. Originally posted by itsbo5 on 2026-10-10T11:59:05Z.
Addressed the review feedback in a new commit and rebased onto latest
main:File::RememberRandomDevice(fd)inRandomDevice.hpp/RandomDevice.cpp.dup_nid_postfix, registered duplicate fds backing random devices viaFile::RememberRandomDevice(duplicate).dup2_nid_postfix, snapshotted whether source is random prior to calling native dup2; on success cleaned up stale registrations viaFile::ForgetRandomDevice(to)andFile::ForgetFileLock(to)(Windows), and re-registered the target when the source was random.from == tocase without dropping registrations.core/libs/tests/GuestDup.cppexercisingdup(random),dup2(random, random),dup2(pipe, random),dup2(random, host_fd), and error handling without mutating the target.Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-10T12:49:01Z.
Rechecked the new head (
df131b9c, with the random device bookkeeping change, based onmain197ffac9), Linux (Ubuntu 24.04, GCC 13) and Windows 11 (MinGW-w64 GCC 15.2 from WinLibs), Release.guest_duppasses 5/5 on both (--repeat until-fail:5).mainwith each title's ownlibc.prxloaded (required since #2605, details in #1578), three catalog titles now stop atdupordup2, and all three get past it on both:mainiiQjzvfWDq0(dup)G-MYv5erXaU(sceKernelGetAppInfo)wdUufa9g-D8(dup2)0NJDcQ2B8vE(execvp)wdUufa9g-D8(dup2)D8cuU4d72xM(sceSysmoduleGetModuleHandleInternal)Example lines for OpenRCT2:
mainWindowsFAIL: unresolved ELF import wdUufa9g-D8 (dup2) from libkernel.prx; with this PR, Linuxapp.elf: symbol lookup error: app.elf: undefined symbol: 0NJDcQ2B8vE, WindowsFAIL: unresolved ELF import 0NJDcQ2B8vE (execvp) from libkernel.prx.Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-10T13:39:50Z.
Two more titles from a triage of the whole homebrew catalog on
maincf8b129e2(Windows 11, MinGW-w64 GCC 15.2.0; each release ZIP checked against its catalog sha256, relinked withrelinker --windows --registry, run for 12 s). Both stop at an import this PR adds, and both move on with it:maindupG-MYv5erXaU(sceKernelGetAppInfo)wdUufa9g-D8(dup2)0NJDcQ2B8vE(execvp)The "with this PR" column comes from one build of
mainwith this PR plus #1872, #2765 and #2024, which add unrelated imports (pthread_set_name_np,alarm,utime).AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by Amoenus on 2026-10-10T14:15:57Z.
Independent Windows validation on Sonic Unleashed Recompiled (PPSA99902),
content version 01.003.000.
I replayed the exact delta from head
df131b9c39onto clean main
b5f5362d3d. Local validationcommit: 7d5f45351e995fb8a0b3c4b228e84841a104ee54.
Windows 11 Enterprise build 26200; WinLibs MinGW-w64 GCC 15.2.0 r7;
Release, BUILD_TESTING=ON.
(ctest --repeat until-fail:5).
FAIL: unresolved ELF import iiQjzvfWDq0 from libkernel.prx (dup).
FAIL: unresolved ELF import cjt-4ySmYTs from libkernel_sys.prx (fork).
Both default conversion and unused-filter=2 produce the same respective stop.
All runs exit 0xC0000139 during import resolution, before guest entry.
Important control: the bundled libc contains a zero-return export shim. With
#2705's shim-rejection relinker, the first stop is hGljHZEfF0U (flockfile), both
with and without this dup/dup2 patch. The dup-only result therefore validates a
loader dependency; it is not evidence that guest code executed dup or that the
whole title starts.
No full ctest suite, Linux run, console behavior check or gameplay is claimed.
AI-assisted investigation; results reproduced locally. Logs available for review.