fix(libc): reopen a stream onto /dev/random or /dev/urandom with freopen #10620
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10620"
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 Retr0G28 on 2026-10-24T13:19:25Z.
What
fopen("/dev/urandom", "r")andfopen("/dev/random", "r")return a stream whosefreadfills the buffer with host entropy, butfreopenonto the same paths went through path resolution and failed withENOENT, as the debt entry said. FreeBSD 11freopenopens the path with_openasfopendoes, so the two calls reach the same device.freopennow checks for those exact paths before resolving the path, asfopendoes. In a read mode it reopens the stream onto the host null device throughFileStream::Reopenand marks it as a random device stream, so the stream behaves as one fromfopen. A write or update mode throws, asfopendoes for these paths. The null-device open is shared withfopenthroughOpenNullDevice, and the path check throughIsRandomDevicePath.Reopenalready clears the random device mark, so a laterfreopenonto a regular file reads that file.The debt entry now names
fopenandfreopen, and theENOENTsentence is removed.Test: in the
/dev/urandomand/dev/randomloop ofguest_filesystem, a stream opened on a regular file is reopened onto the device withr. Two 64-byte reads must differ, with no end-of-file or error indicator. Reopened onto the file withrb, it must read the file's bytes and then reach end of file. It is then reopened onto the device withrband closed.Tested
Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release,
-DBUILD_TESTING=ON, rebased onmain6cd36e6517.maina23f8fc9fb: withmain's libc, the newguest_filesystemchecks fail (exit 0xc0000409 from the failedRequire). With this PR they pass, andguest_pollalso passes.guest_filesystem,file_streams,guest_freopen_host_standard,guest_random,guest_posix_random,guest_random_stateandguest_sandbox_random_wordpass.main3a2b919816(heade465f47d92), where the debt entry above this one was rewritten:guest_filesystem,file_streams,guest_freopen_host_standard,guest_random,guest_posix_randomandguest_standard_streamspass.main7ceda1326c(head9586a81211), where asetvbuf and setbufentry was added right after this one: the same six pass.python tools/check_conventions.py --base origin/mainreports no errors.std::freopen("/dev/null")and the same test runs. No title or homebrew run.Overlap:
tools/pr_overlap.pyfinds no open PR that conflicts on the changed lines. Of the PRs it could not compare:/dev/randomdebt entry, to addfgetcand the other single-byte reads on the stream, and keeps the sentence "freopenonto these paths still fails withENOENT". Its conflicts inStandardStreams.cppandFileStreams.cppare with currentmain:git merge-tree --write-tree origin/mainon its head reports them, and this PR touches neither file. A stream from this PR'sfreopengets the sameMarkRandomDevice()mark as one fromfopen, so #8150'sIsRandomDevice()branch infgetcandgetc_unlockedcovers it. Proposed order, posted on #8150: this PR first, since it merges cleanly intomain, then #8150 with the combined debt entry given there. If #8150's author prefers to go first, I will rebase this PR and switchDepends onto #8150.freopen_s, which callsfreopen, so it would pick this up. Its conflicts inGuestStdioVariadic.cppand at the end oflibc.mdare also with currentmain, and it changes none of this PR's lines, so neither needs the other first (noted on #9527). #9526 adds_Random_deviceand does not change thefopenpath. #7081 and #7201 touchFileAndHeap.cppelsewhere.Writes on an update streamentry, the line just above this PR's/dev/randomentry inlibc.md, so the two conflict on neighbouring lines. Agreed with its author: #12057 first, then this PR rebases onto it, keeping #12057's wording for that entry and its own/dev/randomentry.freopenof a directory in the sameFileAndHeap.cppandlibc.md.git merge-treeof the two heads reports no conflict, so either can merge first.libc.md, or other parts ofGuestFilesystem.cpp.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T13:21:45Z.
Review: APPROVED
Reviewed exact head
fbf7004b7d3fc94c7c2107b4cd49cfb5871d3b33. The special paths are handled before guest path resolution, read modes reopen through the same null-device helper used byfopen, and the random-device marker is restored only after a successful reopen; regular-file reopen clears that marker throughFileStream::Reopen. The test covers fresh entropy reads, status flags, a subsequent regular-file read, and rejecting update mode. On the exact head,FileAndHeap.cpppasses GCC 12.2 C++20-Wall -Wextra -fsyntax-only; conventions andgit diff --check upstream/main...HEADpass. The PR reports focused Windows tests, but I did not independently run the guest filesystem runtime test; hosted product test legs were skipped by eligibility.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T13:57:28Z.
Review: CHANGES_REQUESTED
Follow-up review of exact head
fbf7004b7d3fc94c7c2107b4cd49cfb5871d3b33: the implementation and focused test review from my prior approval still apply, and the hosted relinker/register-context/write-watch matrix is now green. Product jobs remain skipped by eligibility.I manually compared the current open PR heads with
git merge-tree. The overlap with #8150 is larger than the debt-line rebase described in this PR body: the merge conflicts inStandardStreams.cpp,FileStreams.cpp, anddocs/dev/debt/libc.md. In particular, #8150 adds entropy-awarefgetc/getc_unlockedhandling while this PR adds read-mode stream-state handling aroundfreopen; the combined functions must preserve both behaviors. Please agree merge order with #8150's author, resolve all three conflicts while retaining both tests and debt descriptions, and updateDepends onif one must land first.#9527's
freopen_swrapper delegates tofreopen, so the behavior is compatible, but the current merge-tree also conflicts inGuestStdioVariadic.cppanddocs/dev/debt/libc.md. Please retain both test/debt changes and coordinate merge order under the repository overlap rule.This is a merge-coordination request; I found no standalone defect in the reviewed implementation. The existing exact-head Windows test evidence remains author-reported, and I did not independently run the guest filesystem runtime tests.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T14:04:24Z.
Review: COMMENTED
Correction to my previous review: after rechecking each branch against current
upstream/mainand comparing the actual base-relative patches, theStandardStreams.cpp,FileStreams.cpp, andGuestStdioVariadic.cppconflicts I reported are inherited conflicts withmain, not conflicts caused by combining those changes with this PR. The source changes in this PR do not overlap #8150 or #9527. The real shared change is the/dev/randomdebt entry with #8150; your proposed order and combined wording address it. My earlier merge-coordination request overstated the code/test conflicts. The exact-head implementation and test review remains as in my earlier approval, and I found no blocking issue in this PR.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T14:04:25Z.
Review: APPROVED
Re-reviewed exact head
fbf7004b7d3fc94c7c2107b4cd49cfb5871d3b33. I withdraw the merge-coordination blocker from my prior review: it came from comparing old PR refs against each other rather than evaluating their patches on currentmain. The only material overlap is the shared/dev/randomdebt entry with #8150, and the PR body records an order and combined wording. Thefreopenspecial-path handling restores the random-device marker after successful reopen; normal file reopen behavior and error handling remain intact. The added guest filesystem test covers entropy reads, status, re-opening a regular file, and rejecting update mode. I did not independently run the guest filesystem runtime test; product jobs are skipped by eligibility. No blocking issue remains.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T00:13:16Z.
Review: APPROVED
Re-checked — new head, looks good
The current head retains the previously reviewed implementation, removes an unmeasured device-mode assertion, and records the agreed overlap order with #12057.
What I ran
Reviewed exact head
e465f47d929d32f850d6b9e31ab49c12c9f74eb7on currentmain3a2b91981651569163768854a1fcdc7193b6667b; the merge preview is clean. Compared the new PR patch with the previously approved patch:FileAndHeap.cppis unchanged, and the only code/test delta is removal of the unsupportedfreopen(device, "r+")throw assertion. Environment: Linux x86-64, Git and GitHub CLI. I did not run a local Windows or guest runtime build.Results
FileAndHeap.cpppatch unchanged; one test-only commit removes the unsupported assertionguest_filesystem,file_streams,guest_freopen_host_standard,guest_random,guest_posix_random, andguest_standard_streamspassCommand output
Negative control
main'sFileAndHeap.cppwhile keeping the PR testFindings
Blocking
Non-blocking
Depends on: #12057; this is consistent with the shared debt-file edit.Sources
Not verified
To get this to a good state
No further code change is needed for this review. The documented order keeps #12057 ahead of this PR; if another code head is pushed, I will repeat the same merge, conventions, diff and source review on it.