feat(libkernel): support SEEK_DATA and SEEK_HOLE in lseek #6936

Closed
AlexOfTheForest wants to merge 3 commits from upstream-pr/6936 into main
AlexOfTheForest commented 2026-10-16 20:06:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-16T20:06:05Z.


What

FreeBSD 11.4's lseek accepts SEEK_DATA (3) and SEEK_HOLE (4); AnyPS5 rejected them, sceKernelLseek threw on any unknown whence, and lseek returned EINVAL before looking at the descriptor. Three commits:

  • fix(libkernel): an unknown whence fails as in FreeBSD's kern_lseek. The file is fetched first, so a closed descriptor is EBADF and a pipe ESPIPE whatever the whence. vn_seek rejects the unknown whence with EINVAL. sceKernelLseek no longer throws.
  • feat(libkernel): SEEK_DATA/SEEK_HOLE as vn_seek and vn_bmap_seekhole define them:
    • Regular file: an offset at or past the end is ENXIO. Inside the file, SEEK_DATA finds the next data and SEEK_HOLE the next hole, with the end of the file counting as one.
    • Directory: ENOTTY. Pipe: ESPIPE.
    • Hosts: Linux and macOS ask the host for the same regions. Windows reports no holes, so SEEK_HOLE returns the file size.
    • A negative offset, shared memory and devices throw, as the source does not settle them.
  • feat(libkernel): the same on app0.zar descriptors, which have no holes.

The logic lives in File/src/Seek.cpp. The console's filesystems are unknown (one without FIOSEEKDATA would answer ENOTTY), so this is recorded in docs/dev/debt/libkernel.md. GuestPosixScalarIo expected EINVAL for whence 3; it now checks an unknown whence (5) instead.

Tested

Linux x86-64, GCC.

  • New test guest_lseek_whence:
    • unknown whence: EINVAL, EBADF and ESPIPE, through both sceKernelLseek and lseek;
    • SEEK_DATA/SEEK_HOLE inside a file, at its end and past it, and on an empty file, a directory and a pipe;
    • the position is unchanged after an error, and negative offsets throw.
  • guest_kernel_app_archive covers archived files. guest_posix_scalar_io, guest_filesystem and the archive tests pass.
  • Each commit was built and tested on its own.
  • Not tested on Windows or macOS.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4893659445 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6936). Originally posted by [AlexOfTheForest](https://github.com/AlexOfTheForest) on 2026-10-16T20:06:05Z. --- ### What FreeBSD 11.4's `lseek` accepts `SEEK_DATA` (3) and `SEEK_HOLE` (4); AnyPS5 rejected them, `sceKernelLseek` threw on any unknown whence, and `lseek` returned `EINVAL` before looking at the descriptor. Three commits: - `fix(libkernel)`: an unknown whence fails as in FreeBSD's `kern_lseek`. The file is fetched first, so a closed descriptor is `EBADF` and a pipe `ESPIPE` whatever the whence. `vn_seek` rejects the unknown whence with `EINVAL`. `sceKernelLseek` no longer throws. - `feat(libkernel)`: `SEEK_DATA`/`SEEK_HOLE` as `vn_seek` and `vn_bmap_seekhole` define them: - Regular file: an offset at or past the end is `ENXIO`. Inside the file, `SEEK_DATA` finds the next data and `SEEK_HOLE` the next hole, with the end of the file counting as one. - Directory: `ENOTTY`. Pipe: `ESPIPE`. - Hosts: Linux and macOS ask the host for the same regions. Windows reports no holes, so `SEEK_HOLE` returns the file size. - A negative offset, shared memory and devices throw, as the source does not settle them. - `feat(libkernel)`: the same on `app0.zar` descriptors, which have no holes. The logic lives in `File/src/Seek.cpp`. The console's filesystems are unknown (one without `FIOSEEKDATA` would answer `ENOTTY`), so this is recorded in `docs/dev/debt/libkernel.md`. `GuestPosixScalarIo` expected `EINVAL` for whence 3; it now checks an unknown whence (5) instead. ### Tested Linux x86-64, GCC. - New test `guest_lseek_whence`: - unknown whence: `EINVAL`, `EBADF` and `ESPIPE`, through both `sceKernelLseek` and `lseek`; - `SEEK_DATA`/`SEEK_HOLE` inside a file, at its end and past it, and on an empty file, a directory and a pipe; - the position is unchanged after an error, and negative offsets throw. - `guest_kernel_app_archive` covers archived files. `guest_posix_scalar_io`, `guest_filesystem` and the archive tests pass. - Each commit was built and tested on its own. - Not tested on Windows or macOS. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
cadenasynyster commented 2026-10-17 01:19:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-17T01:19:45Z.


Windows run, since the PR lists Windows as untested and the Windows CI job is skipped on this head:

Windows 11 10.0.26200.9711 (NTFS), WinLibs GCC 15.2.0 posix UCRT r7, Release, BUILD_TESTING=ON, head 8392e0c97 merged onto main f3263dfbf (clean merge, no new build errors).

  • guest_lseek_whence: passes 3/3 (ctest --repeat until-fail:3).
  • guest_posix_scalar_io, guest_filesystem, guest_kernel_app_archive_archive / _plain / _mmap: pass.

Not checked: sparse files with real holes on NTFS, or a title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6227876195 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6936#issuecomment-6227876195). Originally posted by [cadenasynyster](https://github.com/cadenasynyster) on 2026-10-17T01:19:45Z. --- Windows run, since the PR lists Windows as untested and the Windows CI job is skipped on this head: Windows 11 10.0.26200.9711 (NTFS), WinLibs GCC 15.2.0 posix UCRT r7, Release, BUILD_TESTING=ON, head 8392e0c97 merged onto main f3263dfbf (clean merge, no new build errors). - guest_lseek_whence: passes 3/3 (ctest --repeat until-fail:3). - guest_posix_scalar_io, guest_filesystem, guest_kernel_app_archive_archive / _plain / _mmap: pass. Not checked: sparse files with real holes on NTFS, or a title. AI-assisted: yes
CnRJay commented 2026-11-01 19:59:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:59:00Z.

Review: COMMENTED


Correction (2026-11-01): the title-ID/log demand below is not a CONTRIBUTING requirement; Unblocks: none is allowed. The technical points below are unaffected.

The PR names no title that calls lseek with SEEK_DATA or SEEK_HOLE ("Unblocks: none") and shows no log line of a title reaching the change on main. Without that the new Seek.cpp surface (about 250 changed lines) is unreachable from anything that runs, and this kind of PR gets closed. Please add the title ID and the log line.

Two questions on the code:

  1. sceKernelLseek used to throw on an unknown whence and now returns EINVAL for anything outside 0 to 4. The description says this matches kern_lseek; is that checked against a console trace or only against FreeBSD sources? The debt entry says the console's filesystems are unknown, and the same uncertainty applies to which errno the console returns here.
  2. On Windows SEEK_HOLE returns the file size and SEEK_DATA returns the offset, i.e. the whole file is data. That is correct for a filesystem without sparse reporting, but sparse files on NTFS do exist; if a title probes for holes it would see a different layout than on Linux. Is that recorded in docs/dev/debt/libkernel.md, or only the console side?
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729953729 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6936#pullrequestreview-5729953729). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:59:00Z. **Review:** COMMENTED --- **Correction (2026-11-01):** the title-ID/log demand below is not a CONTRIBUTING requirement; `Unblocks: none` is allowed. The technical points below are unaffected. The PR names no title that calls `lseek` with `SEEK_DATA` or `SEEK_HOLE` ("Unblocks: none") and shows no log line of a title reaching the change on `main`. Without that the new `Seek.cpp` surface (about 250 changed lines) is unreachable from anything that runs, and this kind of PR gets closed. Please add the title ID and the log line. Two questions on the code: 1. `sceKernelLseek` used to throw on an unknown whence and now returns `EINVAL` for anything outside 0 to 4. The description says this matches `kern_lseek`; is that checked against a console trace or only against FreeBSD sources? The debt entry says the console's filesystems are unknown, and the same uncertainty applies to which errno the console returns here. 2. On Windows `SEEK_HOLE` returns the file size and `SEEK_DATA` returns the offset, i.e. the whole file is data. That is correct for a filesystem without sparse reporting, but sparse files on NTFS do exist; if a title probes for holes it would see a different layout than on Linux. Is that recorded in `docs/dev/debt/libkernel.md`, or only the console side?
MarcZX10 commented 2026-11-23 02:51:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:51:07Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

Changes are required before merge: SeekArchivedFile treats every SEEK_DATA offset inside an archived file as data, even when the offset is beyond the current file position only because the implementation seeks directly to it; this bypasses the defined region lookup and can report data where none exists if archived files can contain sparse regions.

What I ran

Reviewed head f06949da78cf801f47702e27f0dd987f4b80e184. No commands or tests were run in this review; the author reports Linux x86-64/GCC tests in the PR description.

Results

Check Result Note
Prepared diff and changed code passed Changed implementation and tests read
Programmatic checks not run No build or tests run
Windows and macOS behavior not run Author reports these were not tested

Findings

Blocking

  • core/libs/prx/libkernel/File/src/ArchivedFile.cpp:93: For any archived file offset below node.Size, SEEK_DATA returns that offset as data without checking the file's actual data regions. The implementation must establish that app0.zar files are always fully allocated or resolve the data region correctly; the current description's blanket claim that archived files have no holes does not establish that property.

Non-blocking

None.

Not verified

  • core/libs/prx/libkernel/CMakeLists.txt, core/libs/prx/libkernel/File/include/Seek.hpp, core/libs/tests/GuestKernelAppArchive.cpp, core/libs/tests/GuestPosixScalarIo.cpp, and docs/dev/debt/libkernel.md were listed in the prepared bundle but not read from their full file texts; relevant hunks were inspected in diff.patch.
  • The contents of files.json were not available through the review reader, so excluded files could not be enumerated. No claim is made about those files.
  • No build, tests, or console behavior were independently verified.

To get this to a good state

Confirm app0.zar files are guaranteed to have no holes, with evidence, or implement region resolution that does not assume every in-range offset contains data. Then rerun the archived-file seek tests and the relevant filesystem tests on the new head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016038742 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6936#pullrequestreview-6016038742). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T02:51:07Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested Changes are required before merge: `SeekArchivedFile` treats every `SEEK_DATA` offset inside an archived file as data, even when the offset is beyond the current file position only because the implementation seeks directly to it; this bypasses the defined region lookup and can report data where none exists if archived files can contain sparse regions. ## What I ran Reviewed head `f06949da78cf801f47702e27f0dd987f4b80e184`. No commands or tests were run in this review; the author reports Linux x86-64/GCC tests in the PR description. ## Results | Check | Result | Note | |---|---|---| | Prepared diff and changed code | passed | Changed implementation and tests read | | Programmatic checks | not run | No build or tests run | | Windows and macOS behavior | not run | Author reports these were not tested | ## Findings ### Blocking - `core/libs/prx/libkernel/File/src/ArchivedFile.cpp:93`: For any archived file offset below `node.Size`, `SEEK_DATA` returns that offset as data without checking the file's actual data regions. The implementation must establish that app0.zar files are always fully allocated or resolve the data region correctly; the current description's blanket claim that archived files have no holes does not establish that property. ### Non-blocking None. ## Not verified - `core/libs/prx/libkernel/CMakeLists.txt`, `core/libs/prx/libkernel/File/include/Seek.hpp`, `core/libs/tests/GuestKernelAppArchive.cpp`, `core/libs/tests/GuestPosixScalarIo.cpp`, and `docs/dev/debt/libkernel.md` were listed in the prepared bundle but not read from their full file texts; relevant hunks were inspected in `diff.patch`. - The contents of `files.json` were not available through the review reader, so excluded files could not be enumerated. No claim is made about those files. - No build, tests, or console behavior were independently verified. ## To get this to a good state Confirm app0.zar files are guaranteed to have no holes, with evidence, or implement region resolution that does not assume every in-range offset contains data. Then rerun the archived-file seek tests and the relevant filesystem tests on the new head.

Pull request closed

Sign in to join this conversation.
No reviewers
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!6936
No description provided.