libkernel: lseek returns EINVAL instead of EOVERFLOW when the new offset overflows 64 bits; fix branch ready #5248

Closed
opened 2026-10-13 15:34:40 +00:00 by husseinkad · 2 comments
husseinkad commented 2026-10-13 15:34:40 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T15:34:40Z.


lseek and sceKernelLseek return EINVAL from the host when a positive offset added to the current position (SEEK_CUR) or the file size (SEEK_END) overflows a signed 64-bit offset. FreeBSD 11.4 vn_seek returns EOVERFLOW for that case and keeps EINVAL for a seek that lands before the start of the file. A guest that distinguishes the two (a library that probes the maximum file offset, for example) sees the wrong error.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-lseek-eoverflow (three commits on a recent main: the change, a performance follow-up and a test cleanup from review; File/src/Open.cpp and the new test tests/GuestLseekOverflow.cpp, registered as guest_lseek_overflow). The host seek runs first; only when it fails with EINVAL on a positive relative offset does the code compute base plus offset (the current position, or st_size from fstat for SEEK_END) and return EOVERFLOW if that overflows, otherwise it restores errno and returns the old result. The first version called fstat on every seek and made lseek(SEEK_END, +1) about 9 times slower (48 µs against 5.4 µs per call); the final version keeps the success path at main's cost.

Checks:

  • The new test fails on main and passes with the change; guest_kernel_fcntl and guest_file_vector pass.
  • 179 file seeks against a FreeBSD 11.4 vn_seek model (SEEK_SET, SEEK_CUR, SEEK_END at position 0, mid-file and end of file, an empty file and positions near INT64_MAX, offsets 0, 1, max, max minus size plus or minus one, -1 and min) had zero mismatches. The only change from main is EINVAL becoming EOVERFLOW (84) in the overflow cases (18 of them). A failed seek leaves the position unchanged, and lseek(fd, 0, SEEK_CUR) afterwards is correct. Negative results stay EINVAL, a bad fd stays EBADF, and the directory, pipe, socket, /dev/null and /dev/urandom results match main. 100000 SEEK_END +1 calls took 405 ms on main and 464 to 492 ms on the final head on a busy machine.
  • Two independent reviews. check_conventions.py is clean; pr_overlap.py did not finish within its timeout, and I found no open PR for it by hand (#4672 edits NativeLseek only, in different lines).

Not covered: the test needs a host seek near INT64_MAX; where the host refuses that (ext4), it prints a skip line and runs only the checks that need no huge position, and that path was not run (only Windows was built and run). A directory lseek with SEEK_END and INT64_MAX aborts the process on main as well, which this change does not touch.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5833177898 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5248). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-13T15:34:40Z. --- `lseek` and `sceKernelLseek` return `EINVAL` from the host when a positive offset added to the current position (`SEEK_CUR`) or the file size (`SEEK_END`) overflows a signed 64-bit offset. FreeBSD 11.4 `vn_seek` returns `EOVERFLOW` for that case and keeps `EINVAL` for a seek that lands before the start of the file. A guest that distinguishes the two (a library that probes the maximum file offset, for example) sees the wrong error. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-lseek-eoverflow (three commits on a recent main: the change, a performance follow-up and a test cleanup from review; `File/src/Open.cpp` and the new test `tests/GuestLseekOverflow.cpp`, registered as `guest_lseek_overflow`). The host seek runs first; only when it fails with `EINVAL` on a positive relative offset does the code compute base plus offset (the current position, or `st_size` from `fstat` for `SEEK_END`) and return `EOVERFLOW` if that overflows, otherwise it restores `errno` and returns the old result. The first version called `fstat` on every seek and made `lseek(SEEK_END, +1)` about 9 times slower (48 µs against 5.4 µs per call); the final version keeps the success path at main's cost. Checks: - The new test fails on main and passes with the change; `guest_kernel_fcntl` and `guest_file_vector` pass. - 179 file seeks against a FreeBSD 11.4 `vn_seek` model (`SEEK_SET`, `SEEK_CUR`, `SEEK_END` at position 0, mid-file and end of file, an empty file and positions near `INT64_MAX`, offsets 0, 1, max, max minus size plus or minus one, -1 and min) had zero mismatches. The only change from main is `EINVAL` becoming `EOVERFLOW` (84) in the overflow cases (18 of them). A failed seek leaves the position unchanged, and `lseek(fd, 0, SEEK_CUR)` afterwards is correct. Negative results stay `EINVAL`, a bad fd stays `EBADF`, and the directory, pipe, socket, `/dev/null` and `/dev/urandom` results match main. 100000 `SEEK_END +1` calls took 405 ms on main and 464 to 492 ms on the final head on a busy machine. - Two independent reviews. `check_conventions.py` is clean; `pr_overlap.py` did not finish within its timeout, and I found no open PR for it by hand (#4672 edits `NativeLseek` only, in different lines). Not covered: the test needs a host seek near `INT64_MAX`; where the host refuses that (ext4), it prints a skip line and runs only the checks that need no huge position, and that path was not run (only Windows was built and run). A directory `lseek` with `SEEK_END` and `INT64_MAX` aborts the process on main as well, which this change does not touch. AI-assisted: yes
MarcZX10 commented 2026-10-13 16:15:00 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T16:15:00Z.


Carried the fix in #5274, preserving Hussein Kadhim's original authorship. On Linux, guest_lseek_overflow, guest_file_vector, and guest_kernel_fcntl passed (3/3). The regression test aborts on current main with errno 22 at the overflowing relative seek, then passes with the fix. I did not run Windows locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6149977121 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5248#issuecomment-6149977121). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T16:15:00Z. --- Carried the fix in #5274, preserving Hussein Kadhim's original authorship. On Linux, `guest_lseek_overflow`, `guest_file_vector`, and `guest_kernel_fcntl` passed (3/3). The regression test aborts on current `main` with errno 22 at the overflowing relative seek, then passes with the fix. I did not run Windows locally.
oneandonlydean commented 2026-10-14 06:54:28 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:54:28Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. #5274 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162712976 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5248#issuecomment-6162712976). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:54:28Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. #5274 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#5248
No description provided.