libkernel: open with O_RDONLY|O_TRUNC fails with EINVAL on Windows, where FreeBSD truncates the file; fix branch ready #5095
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 DavidSamir on 2026-10-13T11:27:56Z.
On Windows,
sceKernelOpen(path, O_RDONLY | O_TRUNC)andopenwith the same flags fail withEINVAL(0x80020016) and leave the file as it was. On FreeBSD the open succeeds, the file is emptied and the descriptor is read-only:vn_open_vnodeasks for write permission on the file whenO_TRUNCis set, whatever the access mode, andkern_openatthen truncates it. The Linux build does the same.Cause.
NativeOpenmapsO_TRUNCwithoutO_CREATtoTRUNCATE_EXISTINGandO_RDONLYtoGENERIC_READ.CreateFileWrejects that pair withERROR_INVALID_PARAMETER:TRUNCATE_EXISTINGneedsGENERIC_WRITE.Observed on
mainbb9a4d8a2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. The file holds ten bytes:mainon WindowssceKernelOpen(file, O_RDONLY|O_TRUNC)0x80020016, ten bytes leftO_APPEND,O_NONBLOCKorO_EXCLadded0x80020016open(file, O_RDONLY|O_TRUNC)0x800200160x8002000D(EACCES), file keptO_RDONLY|O_CREAT|O_TRUNCI have not found a title that stops on this. It came out of running one probe of the file functions on the Windows build and on the Linux build and comparing the two outputs.
Fix, ready to cherry-pick.
DavidSamir:fix/libkernel-open-rdonly-trunc-windows(c076185f3, three commits onmainbb9a4d8a2, three files, +46/-2). I can't open it as a PR while #4532 is open (one open PR without a merged one).fix(libkernel): truncate on open with O_RDONLY and O_TRUNC on Windows:Open.cpp.test(libkernel): open a file with O_RDONLY and O_TRUNC:guest_filesystem.docs(debt): describe open with O_RDONLY and O_TRUNC on Windows.Windows only, in
NativeOpen. ForO_RDONLYwithO_TRUNCand withoutO_CREAT, the file is opened withGENERIC_READ | GENERIC_WRITE, which letsTRUNCATE_EXISTINGthrough and, like FreeBSD, refuses a read-only file withEACCES. The handle is then narrowed toFILE_GENERIC_READwithDuplicateHandleandDUPLICATE_CLOSE_SOURCE, so the descriptor is read-only: a write fails withEBADF,ftruncatewithEINVAL, andF_GETFLreportsO_RDONLY. The path is chosen from the flags before the call; nothing is tried twice. Every other combination of flags takes the same calls as before.One difference is left and is recorded in the debt file: the narrowed handle is the same open file, which Windows still counts as a writer in its sharing checks, so another host process that opens the file without
FILE_SHARE_WRITEis refused while the descriptor is open. Opens made by the guest share write access and are not affected.guest_filesystemgainsTestReadOnlyTruncatingOpen: with the file open through a second descriptor,O_RDONLY|O_TRUNCmust succeed and empty it (seen through the host and throughfstaton the other descriptor); a write through the new descriptor must fail withEBADFandftruncatewithEINVAL; bytes written through the other descriptor must be readable through it; a missing file must giveENOENTand not be created, a directoryEISDIR, and a 0444 fileEACCESwith its contents kept.Checked on Windows (c076185f3 on
mainbb9a4d8a2, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):Open.cppas onmainand the test kept,guest_filesystemfails (exit 0xc0000409,Filesystem check failed at line 213 (guest errno 22, Win32 error 87)). With the fix it passes 5/5 (--repeat until-fail:5), andguest_kernel_fcntl,guest_truncate,guest_dup,guest_file_vector,guest_file_lock_conversion,guest_file_lock_duplicate,file_streams,guest_standard_streams,guest_posix_filesystemandguest_fdopendirpass 3/3.O_RDONLY|O_TRUNCon a file, on an empty file, withO_APPEND, withO_NONBLOCK, withO_EXCL, withO_CREATon an existing and on a missing file and withO_CREAT|O_EXCL: the result, the file afterwards,F_GETFL,fstat,lseek,read,write,pwrite,ftruncate,fsync,flock,pread, unlinking the file while open, andclose. Then a 0444 file, a directory with and withoutO_DIRECTORY,O_DIRECTORYon a file, a missing directory,/dev/null,/dev/urandom, theO_WRONLY,O_RDWRand plainO_RDONLYopens for comparison,openthrough libc, a file another descriptor holds (its size, its offset, a write through it read back through the new descriptor, a write through adupof the new descriptor failing), a file another descriptor holds aflockon, a name removed while open, and 3000 truncating opens in a row. 212 of the 214 lines equal the Linux build's. The two that do not are the same withmain's libraries and are not about truncation:O_DIRECTORYon a file returns the rightsceKernelerror without setting errno, and a path under a regular file givesENOENTwhere Linux givesENOTDIR. The 3000 opens leave the process handle count unchanged.O_WRONLY,O_RDWR, plainO_RDONLYandO_CREATopens is identical withmain's libraries and the branch's.flock, 44 over pipe streams, 15 overfcntlstatus flags, 127 overfreopen, 130 over directory descriptors, 117 over reads and writes on streams opened the other way, and 313 overfdopen, run with both sets of libraries: the only lines that differ are the two in the first probe that open withO_RDONLY|O_TRUNC, and a file size in the last that changes from run to run with either set.O_RDONLY76 to 92 µs withmain's libraries and 81 to 98 µs with the branch's;O_RDWR77 to 93 and 79 to 100;O_WRONLY|O_TRUNC103 to 105 and 99 to 125;O_RDWR|O_CREAT|O_TRUNC143 to 174 and 125 to 157. No difference I can separate from the spread.radv/ps5 submissionsline in one of the two runs. SwanStationPS5 (PPSA98510) and PSXS5 (PPSA97510) stop onpoll: POLLPRI is not supported by WSAPollin both (#4478).ctest --parallel 4: 1051 of 1051 pass, as withmainbb9a4d8a2built without this change.guest_filesystem,guest_kernel_fcntl,guest_truncate,guest_dupandfile_streamspass 3/3.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchreports no overlap with open pull requests.Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release):
libsbuilds, andguest_filesystemandguest_kernel_fcntlpass 3/3, so the new test matches the existing Linux behaviour. The changed code is Windows only.Not checked on a console or with a commercial title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T06:27:40Z.
Opened #5684 carrying David's Windows
O_RDONLY|O_TRUNCfix, tests, and debt note with his commit authorship preserved. On Linux GCC 12.2 Release, the build succeeded andguest_filesystemplusguest_kernel_fcntlpassed (2/2); conventions passed. The detailed Windows matrix is in this issue and will receive additional hosted CI coverage.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:54:03Z.
Closing: no title is known to reach this, and functions are only changed when a title needs them. The catalog titles in the report run the same with and without the change. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.