libkernel: ftruncate succeeds on a pipe, a directory descriptor and /dev/urandom on Windows, where FreeBSD fails with EINVAL; fix branch ready #4921
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-13T04:16:03Z.
On Windows,
ftruncateandsceKernelFtruncatereturn 0 on the write end of a pipe, on a directory descriptor and on/dev/urandom, and change nothing. FreeBSD's ftruncate(2) fails withEINVALwhen the descriptor "references a socket, not a file", and the Linux build fails withEINVALon all three. A caller that sizes a file through a descriptor it was handed is told the resize worked.Cause.
NativeFtruncatecalls the C runtime's_chsize_s, which reports success on a pipe handle it can write and on theNULdevice that stands behind a directory descriptor and/dev/urandom. The read end of a pipe already fails, because that handle cannot be written.Observed on
main495bf91a0, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release:sceKernelFtruncate(fd, 0)on0x80020016(EINVAL)0x800200160x80020016O_DIRECTORY0x80020016/dev/urandom0x800200160x800200160x80020016I 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; #4670 listed the pipe case as not changed.
Fix, ready to cherry-pick.
DavidSamir:fix/libkernel-ftruncate-not-a-file-windows(971fb51ef, one commit onmain495bf91a0, two files, +18/-1). I can't open it as a PR while #4532 is open (one open PR without a merged one).Windows only.
NativeFtruncateasks for the handle's type before the call and returnsEINVALwhen it is not a disk file; a disk file goes to_chsize_sas today, and a closed descriptor still gets_chsize_s'sEBADF. Shared-memory descriptors return before that point as they do now.truncateby path goes through the same function on a file it has just opened, so it is unchanged apart from the oneGetFileType, which #4670 measured at about 0.1 µs on this machine.Does not depend on #4912 (reads on a directory descriptor) and does not touch its lines.
guest_filesystemgains checks: on both ends of a pipe holdingabc,sceKernelFtruncatewith length 0 and 8 must return0x80020016and the three bytes must then be read back; on a directory descriptor and on/dev/urandomit must return0x80020016.Checked on Windows (971fb51ef on
main495bf91a0, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):Stdio.cppas onmainand the checks kept,guest_filesystemfails at the pipe check (Filesystem check failed at line 233). With the fix it passes 5/5 (--repeat until-fail:5), andguest_file_vector,guest_poll,guest_dup,guest_standard_streams,file_streams,guest_aio,guest_kernel_fcntl,guest_directory_rewind,guest_shmandguest_kernel_syncpass 3/3.main's libraries, the branch's and on the Linux build: nine lines change againstmain, every one anftruncateon a directory, a duplicate of one, a pipe's write end or/dev/urandom, and after it noftruncateline differs from the Linux build. The files (lengths 0, 100 and -1, read-write and read-only), the pipe's read end and a closed descriptor give the same results with both sets of libraries.ftruncateon a directory and on a pipe, while itsftruncateto 4 and to 20 on a file and itstruncateby path are identical; none differ in the second.poll: POLLPRI is not supported by WSAPollin both (#4478); its number of stderr lines varies from run to run with either set of libraries.ctest --parallel 4: 1023 of 1023 pass (7 skipped, no display).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):
guest_filesystempasses 3/3, so the new checks match the existing Linux behaviour. Only that target andlibswere built there.Not checked on a console or with a commercial title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T04:23:04Z.
I carried your
971fb51efcommit into PR #4923, preserving your original commit authorship. On currentmain(e51428bf2), Linux GCC 12.2 Release buildsguest_filesystem_tests; CTestguest_filesystempasses (1/1), and conventions, test registration and diff checks pass. Windows runtime evidence remains the report in this issue; I did not rerun it locally. The change also merges cleanly with #4917's separateStdio.cppchanges.