Skip to content

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

Open
AlexOfTheForest wants to merge 3 commits into
boykopovar:mainfrom
AlexOfTheForest:fix/lseek-data-hole
Open

AlexOfTheForest wants to merge 3 commits into
boykopovar:mainfrom
AlexOfTheForest:fix/lseek-data-hole

Conversation

@AlexOfTheForest

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4563 core/libs/prx/libkernel/CMakeLists.txt
PR 6149 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7121 core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7447 core/libs/prx/libkernel/File/src/ArchivedFile.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 8041 core/libs/prx/libkernel/CMakeLists.txt
PR 8605 docs/dev/debt/libkernel.md
PR 9156 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9293 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9297 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9302 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9383 core/libs/prx/libkernel/CMakeLists.txt
PR 9389 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9557 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9566 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9938 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 10558 docs/dev/debt/libkernel.md
PR 10645 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11628 docs/dev/debt/libkernel.md
PR 11661 docs/dev/debt/libkernel.md
PR 11689 docs/dev/debt/libkernel.md
PR 11698 docs/dev/debt/libkernel.md
PR 11896 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 12544 core/libs/prx/libkernel/CMakeLists.txt
PR 12682 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 12688 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 12700 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 12965 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 14103 docs/dev/debt/libkernel.md
PR 14108 docs/dev/debt/libkernel.md
PR 15037 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 15040 docs/dev/debt/libkernel.md
PR 16372 core/libs/prx/libkernel/CMakeLists.txt
PR 16790 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 16826 docs/dev/debt/libkernel.md
PR 17438 core/libs/prx/libkernel/CMakeLists.txt
PR 17439 core/libs/prx/libkernel/CMakeLists.txt
PR 17463 core/libs/prx/libkernel/CMakeLists.txt
PR 17477 core/libs/prx/libkernel/CMakeLists.txt
PR 17484 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 17489 core/libs/prx/libkernel/CMakeLists.txt
PR 17493 core/libs/prx/libkernel/CMakeLists.txt
PR 17513 core/libs/prx/libkernel/CMakeLists.txt
PR 17521 core/libs/prx/libkernel/CMakeLists.txt
PR 17638 docs/dev/debt/libkernel.md
PR 18112 core/libs/prx/libkernel/CMakeLists.txt
PR 18190 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 18197 docs/dev/debt/libkernel.md
PR 18293 core/libs/prx/libkernel/CMakeLists.txt
PR 18324 docs/dev/debt/libkernel.md
PR 18462 docs/dev/debt/libkernel.md
PR 18915 core/libs/prx/libkernel/File/src/Open.cpp
PR 19181 core/libs/prx/libkernel/CMakeLists.txt
PR 19433 core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 20021 docs/dev/debt/libkernel.md
PR 20161 docs/dev/debt/libkernel.md
PR 20349 docs/dev/debt/libkernel.md
PR 20865 core/libs/prx/libkernel/CMakeLists.txt
PR 20899 core/libs/prx/libkernel/CMakeLists.txt
PR 20957 core/libs/prx/libkernel/CMakeLists.txt
PR 21020 core/libs/prx/libkernel/CMakeLists.txt
PR 21022 core/libs/prx/libkernel/CMakeLists.txt
PR 21024 core/libs/prx/libkernel/CMakeLists.txt
PR 21026 core/libs/prx/libkernel/CMakeLists.txt
PR 21041 core/libs/prx/libkernel/CMakeLists.txt
PR 21051 core/libs/prx/libkernel/CMakeLists.txt
PR 21058 core/libs/prx/libkernel/CMakeLists.txt
PR 21119 core/libs/prx/libkernel/CMakeLists.txt
PR 21132 core/libs/prx/libkernel/CMakeLists.txt
PR 21135 core/libs/prx/libkernel/CMakeLists.txt
PR 21604 core/libs/prx/libkernel/CMakeLists.txt
PR 21619 core/libs/prx/libkernel/CMakeLists.txt
PR 21625 core/libs/prx/libkernel/CMakeLists.txt
PR 21626 core/libs/prx/libkernel/CMakeLists.txt
PR 21647 core/libs/prx/libkernel/CMakeLists.txt
PR 21655 core/libs/prx/libkernel/CMakeLists.txt
PR 21663 core/libs/prx/libkernel/CMakeLists.txt
PR 21672 core/libs/prx/libkernel/CMakeLists.txt
PR 22191 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 22391 core/libs/prx/libkernel/CMakeLists.txt
PR 22395 core/libs/tests/GuestKernelAppArchive.cpp
PR 22396 core/libs/tests/GuestKernelAppArchive.cpp
PR 22479 core/libs/prx/libkernel/CMakeLists.txt
PR 22483 core/libs/prx/libkernel/CMakeLists.txt
PR 23555 core/libs/prx/libkernel/CMakeLists.txt
PR 24166 core/libs/prx/libkernel/CMakeLists.txt
PR 24322 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 24343 core/libs/prx/libkernel/CMakeLists.txt
PR 24487 core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 24488 core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 24641 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 24826 core/libs/prx/libkernel/CMakeLists.txt
PR 24910 core/libs/prx/libkernel/CMakeLists.txt
PR 24917 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 24924 core/libs/prx/libkernel/CMakeLists.txt
PR 24960 core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 25468 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 25491 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 25949 core/libs/prx/libkernel/CMakeLists.txt
PR 25956 core/libs/prx/libkernel/File/src/Open.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 26174 core/libs/prx/libkernel/CMakeLists.txt
PR 26206 core/libs/prx/libkernel/CMakeLists.txt
PR 26228 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 26300 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 26453 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 26695 core/libs/prx/libkernel/CMakeLists.txt

@AlexOfTheForest AlexOfTheForest changed the title Fix/lseek data hole feat(libkernel): support SEEK_DATA and SEEK_HOLE in lseek Oct 16, 2026
@cadenasynyster

Copy link
Copy Markdown
Contributor

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 8392e0c merged onto main f3263df (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

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 18, 2026
@adriadam10 adriadam10 self-assigned this Oct 19, 2026
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 19, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 20, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 20, 2026
…of throwing

sceKernelLseek threw on a whence other than SET, CUR and END, and lseek returned EINVAL before looking at the descriptor. FreeBSD 11.4's kern_lseek fetches the file first and dispatches to its fo_seek: a closed descriptor is EBADF, a pipe ESPIPE whatever the whence, and vn_seek rejects the unknown whence with EINVAL. Both calls now resolve the descriptor before the whence.
FreeBSD 11.4 accepts whence 3 and 4: vn_seek forwards them to FIOSEEKDATA and FIOSEEKHOLE, which UFS answers through vn_bmap_seekhole. An offset at or past the end of a regular file is ENXIO, a directory ENOTTY and a pipe ESPIPE; inside the file SEEK_DATA finds the next data and SEEK_HOLE the next hole, the end of the file counting as one. Linux and macOS ask the host for the same regions; Windows has no holes to report, so SEEK_DATA keeps the offset and SEEK_HOLE moves to the size. A negative offset, shared memory and devices throw, as the source does not settle them.
An archived descriptor fell through to AppArchiveDescriptorSeek, which throws on any whence but SET, CUR and END. An archived file has no holes: SEEK_DATA keeps an offset inside the file, SEEK_HOLE moves to its size and an offset at or past the end is ENXIO, as for a regular file.
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@AlexOfTheForest this needs a rebase onto main: the branch conflicts in Stdio.cpp, CMakeLists and the debt doc. Please push the rebased head so CI can run on it, since no test job has run on this head.

The whence values (3 and 4) and the Windows emulation read correctly to me: DATA returns the offset, HOLE returns the file size, and an offset at or past EOF gives ENXIO. One thing to check while you are in there: on an archived fd with an unknown whence, a failed stat with errno 0 looks like it could return 0 instead of an error.

@adriadam10 adriadam10 removed their assignment Nov 17, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs libkernel needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants