fix(libc): translate host errno from fopen, freopen and fopen_s #7068
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7068"
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 AlexOfTheForest on 2026-10-17T09:57:18Z.
What
libc's stream-opening calls passed the host's errno to the title untranslated. On Linux a symlink loop reached the title as 40 and an over-long name as 36, which are
EMSGSIZEandEINPROGRESSon FreeBSD 11.4 instead ofELOOP(62) andENAMETOOLONG(63).fopen_sreturnedENOENTfor every failure.opendir,realpath,chdir,chmod,utimes,lutimesandchflagsalready return the FreeBSD values; only thefopenfamily leaked.fix(libc):GuestErrnoFromHost_nid_no_patch(libc/src/HostErrno.cpp) maps a host errno to FreeBSD's:EAGAIN, which becomes 35;ELOOP,ENAMETOOLONG,ENOTEMPTY,EDQUOT,EOVERFLOWandEDEADLKget their FreeBSD numbers;EIO.fopenuses it. On WindowsOpenNativealready sets host values fromWindowsOpenError_nid_no_patch, so they pass through the same table.fix(libc):freopenuses the same translation for its failed reopen.fix(libc):fopen_sreturns the errnofopenset. C11 K.3.5.2.1 only requires a nonzero value; Microsoft's CRT, built on Dinkumware like the console's libc, returns errno. Recorded indocs/dev/debt/libc.mdas unverified on console.Tested
Linux x86-64, GCC.
guest_fopen_errorstest (UNIX only, since it creates symlinks):fopen,freopenandfopen_swith a self loop, a path through a loop, a 300-byte name and a path under it;file_streams,file_position,guest_fiopen(including itsfopen_schecks), access, directories,fdopendir, standard streams, working directory, host streams and statvfs.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 github-actions[bot] on 2026-10-17T10:10:55Z.
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)docs/dev/debt/libc.mdL85core/libs/prx/libc/src/FileAndHeap.cppL320-321Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/src/FileAndHeap.cppcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/src/FileAndHeap.cppdocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T14:05:09Z.
Windows run of head
5433b32b7, merged ontomainc9e6c45de(clean; also stacked with #7070).libspass, no warnings inHostErrno.cpporFileAndHeap.cpp.guest_fopen_errorsis registered underif(UNIX), so it is not built on Windows and I could not run it.guest_fiopenandfile_streamspass 3/3.GuestErrnoFromHost_nid_no_patchwith MinGW and ran every constant through it. Against FreeBSD 11.0sys/errno.h:EAGAIN11 -> 35,EDEADLK36 -> 11,ENAMETOOLONG38 -> 63,ENOTEMPTY41 -> 66,ELOOP114 -> 62,EOVERFLOW132 -> 84. The values 1-34 pass through (ENOENT2,EACCES13,EEXIST17,ENOTDIR20,EISDIR21,EINVAL22,EMFILE24,ENOSPC28).WindowsOpenError_nid_no_patchreturns onlyENOENT,EACCES,EEXIST,EMFILE,ENOMEM,ENOSPCandEINVAL, so theEIOfallback is never taken by a Windows open.fopen/freopen/fopen_s. With the PR,fopenandfreopenreturn the same errno asmainin all 15 cases.fopen_sdiffers in 6:mainreturns 2 for every failure, the PR returns 13 (directory opened for writing or reading, read-only file withwandr+), 17 (wxon an existing file) and 22 (invalid mode). WithFileAndHeap.cppreverted tomainthefopen_scolumn goes back to 2.mainas well: a 300-character name gives 2 (FreeBSD 63), a file used as a directory gives 2 (20), and a directory opened for writing gives 13 (21). They come fromWindowsOpenError_nid_no_patch(ERROR_FILENAME_EXCED_RANGEandERROR_PATH_NOT_FOUNDboth giveENOENT,ERROR_ACCESS_DENIEDfor a directory givesEACCES), so theENAMETOOLONGandELOOPfixes do not show on Windows. Not for this PR; mapping those Win32 errors would be a follow-up.ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip'): 884 tests (my throwaway probe included), 872 pass, 2 skipped, 10 failed on the first pass: theconventions_function_bodiesscan and its 5 shards,hardware_oracle_compare,guest_none_relocationsandtls_function_coverage_register_address_loadstimed out under load, andguest_sce_netaborted once at-j8. All pass on serial reruns (shard 4 on its second serial try, 20.5 s against its 30 s limit;guest_sce_net5/5). None of them touches code from these PRstools/check_conventions.py --base c9e6c45deandgit diff --checkpass. There are no new exports.Titles.
fopenis imported by 50 of my local relinked titles andfreopenby 28. I ran four for 60 s each onmainand on this PR with a local diagnostic libc that logs each failedfopen/freopen/fopen_sand the errno returned:fopen(main / PR)/data/PCSX2/boot.log,pf.log,pid.txtfor writing;en.txt,gs.ini,usb-games.txt)/app0/trace.txt,picker/test-run.txt,config/frontend.cfg,picker/picker-test.txt)/download0/etahen_jailbreak, write)No
freopenorfopen_scall in any of them. Every title got 2 (ENOENT) on both sides, which is also FreeBSD's value for these, so the codes this PR fixes did not occur. All runs were killed at the limit with no stop line.Review: no comments, no new exports or NIDs, no title-specific code. Since the symlink and long-name cases can only fail on a Linux host, a variant of the test for Windows (missing file, directory opened for writing, invalid mode,
fopen_s) would give the change some coverage here.Not checked: Linux, macOS, PS5 hardware,
freopenwith a null file name, any title that hitsELOOP/ENAMETOOLONG(none local).Written with Claude Code assistance (validation by a subagent, reviewed in this session).