libkernel: lseek on a pipe succeeds on Windows and reports a position and a size, where FreeBSD fails with ESPIPE; fix branch ready #4670
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-12T18:24:30Z.
On Windows,
lseekandsceKernelLseeksucceed on a pipe and return a position, and withSEEK_ENDthe number of bytes waiting in it. FreeBSD's lseek(2) fails withESPIPEwhen the descriptor "is associated with a pipe, socket, or FIFO", and the Linux build does that. Code that askslseekwhether a descriptor is a file or a stream, or that sizes its input withSEEK_END, therefore treats a pipe as a file on Windows.Cause.
NativeLseekcalls_lseeki64, and Windows accepts a file position on a pipe handle. #3151 already returnsESPIPEfor positioned I/O on a socket andpreadreturns it for a pipe;lseekon a pipe was left.Observed on
main2a5eca11c, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, on a pipe frompipe()holding the 11 byteshello world:sceKernelLseek(read end, 0, SEEK_SET)0x8002001D(ESPIPE)lseek(read end, 5, SEEK_SET)lseek(read end, 5, SEEK_CUR)sceKernelLseek(read end, 0, SEEK_END)0x8002001DsceKernelLseek(write end, 0, SEEK_CUR)0x8002001DsceKernelLseek(dup of the read end, 0, SEEK_CUR)0x8002001DThe data in the pipe is not affected by these calls; the next
readreturnshello. I 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-lseek-pipe-windows(32f49a97a, one commit onmain2a5eca11c, two files, +20/-1). I can't open it as a PR while #4532 is open (one open PR without a merged one).Windows
NativeLseekmakes the same_lseeki64call as before, so a closed or invalid descriptor fails exactly as it did, and then returnsESPIPEwhen the handle's type isFILE_TYPE_PIPE. Shared-memory and directory descriptors return before that point as they do now. Files,/dev/urandomand character devices are unchanged.Cost: the
GetFileTypecall adds about 0.1 µs to each seek on this machine. One millionsceKernelLseekcalls on a file take 159 to 171 ns each withmain's libraries and 264 to 282 ns with the branch's (nine passes each, in three runs). Keeping a table of pipe descriptors instead would avoid that, at the price of keeping it right acrossdup,dup2,fcntland every way a descriptor is closed; I chose the single check. Say if you want the table.Not changed:
fseekandftellon a stream over a pipe (they do not go through this function),ftruncateon a pipe, which returns 0 where the Linux build returnsEINVAL, andlseekon a console.guest_filesystemgains checks: on both ends of a pipe,lseekfails with errno 29 andsceKernelLseekreturns0x8002001DforSEEK_SET,SEEK_CURandSEEK_END; the bytes written before are then read back unchanged; afterclose,lseekfails with errno 9.Checked on Windows (32f49a97a on
main2a5eca11c, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release):Open.cppas onmainand the new checks kept,guest_filesystemfails at the first of them (Filesystem check failed at line 222). With the fix it passes 5/5 (--repeat until-fail:5).main's libraries and the branch's: every pipe line changes toESPIPE; a closed descriptor (EBADF),/dev/urandom, a file withSEEK_SETandSEEK_END, and descriptors 0 and 1 give the same results in both.lseekon a pipe.pollin both, each after a time that varies from run to run.ctest --parallel 4: 973 of 973 pass (7 skipped, no display).guest_standard_streams,file_streams,guest_dup,guest_fiopen,guest_poll,guest_directory_rewind,guest_file_vectorandguest_aiopass 3/3.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchlists #4536 and #4640 for the same two places inGuestFilesystem.cpp(a declaration line and the end of the pipe checks); neither changeslseek.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 milescoviello on 2026-10-12T18:27:08Z.
Carried in #4672, keeping your commit and authorship. I added the full Linux run (Ubuntu 24.04, GCC 13.3): all 306
guest_*tests pass, and the new pipe checks also pass withmain'sOpen.cpp, so Linux is unchanged. The Windows results in the PR are yours from this issue. Thanks for the branch.AI-assisted: yes