feat(libkernel_sys): implement getfsstat #11031

Merged
Gruzzberry merged 1 commit from upstream-pr/11031 into main 2026-11-08 04:19:19 +00:00
Gruzzberry commented 2026-10-25 14:32:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-25T14:32:58Z.


What

getfsstat was a throwing stub in libkernel_sys. It now follows FreeBSD 11.4's sys_getfsstat and kern_getfsstat: a negative bufsize or a mode other than MNT_WAIT (1) and MNT_NOWAIT (2) fails with EINVAL; a null buffer or a bufsize of 0 writes nothing and returns the filesystem count (1); a non-null buffer too small for one struct statfs (472 bytes) writes nothing and returns 0, as kern_getfsstat caps the count at maxcount; otherwise one statfs is copied out and 1 returned, with an unwritable buffer failing with EFAULT. bufsize is the guest's 64-bit long, taken as std::int64_t so the Windows build does not read it as 32 bits.

The guest has one filesystem, the host volume of the guest root, filled in by the existing _fstatfs. The debt entry records that this is not the console's mount table, that MNT_NOWAIT is not served from a cache, and that jail filtering is not modelled.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on main 7a25f87620.

  • guest_kernel_sys: new CheckGetfsstat: modes 0, 3, -1 and 4, and negative sizes, fail with EINVAL and write nothing; size 0 returns 1 and writes nothing; sizes 1 and 471 with a buffer return 0 and write nothing; a null buffer returns 1 at sizes 471, 472 and 2^32 + 472; -2^32 + 472 fails with EINVAL (a 32-bit read would see 472); the unreadable 0x10 with a full-size buffer fails with EFAULT; with room for two structs, both modes return 1, the second slot stays untouched, and the first matches _fstatfs on /. On main the test does not compile (the stub has a (void) signature); on this branch it passes 3 runs out of 3.
  • Full suite: 2356 tests. The only failures are Python tooling tests that time out under load (pass alone) and agc_host_import_cut_lifetime, agc_driver_depth_plane_partial_store, pr_overlap_encoding, which fail the same way on an unrelated checkout in the same environment; agc_driver_depth_clear_hle and guest_select flaked in parallel and pass alone.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, a console, a title run.

Overlaps

  • #11032 (kenv, mine) adds code at the same place in Export.cpp (L343) and GuestKernelSys.cpp (L524): #11032 goes first, then this PR rebases.
  • #10427 (GuestKernelSys.cpp L524) and #10799 (libkernel_sys.md debt L40) touch neighbouring lines: proposed on each that it goes first and this PR rebases.
  • No open PR implements getfsstat or getmntinfo or changes _fstatfs. #9161 (statfs) shares no lines.

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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: #11032; merge order: after #11032, #10427 and #10799 (neighbouring lines)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5005642817 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11031). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-25T14:32:58Z. --- ### What `getfsstat` was a throwing stub in libkernel_sys. It now follows FreeBSD 11.4's [`sys_getfsstat`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_syscalls.c#L371-L520) and `kern_getfsstat`: a negative `bufsize` or a mode other than `MNT_WAIT` (1) and `MNT_NOWAIT` (2) fails with `EINVAL`; a null buffer or a `bufsize` of 0 writes nothing and returns the filesystem count (1); a non-null buffer too small for one `struct statfs` (472 bytes) writes nothing and returns 0, as `kern_getfsstat` caps the count at `maxcount`; otherwise one `statfs` is copied out and 1 returned, with an unwritable buffer failing with `EFAULT`. `bufsize` is the guest's 64-bit `long`, taken as `std::int64_t` so the Windows build does not read it as 32 bits. The guest has one filesystem, the host volume of the guest root, filled in by the existing `_fstatfs`. The debt entry records that this is not the console's mount table, that `MNT_NOWAIT` is not served from a cache, and that jail filtering is not modelled. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on `main` 7a25f87620. - `guest_kernel_sys`: new `CheckGetfsstat`: modes 0, 3, -1 and 4, and negative sizes, fail with `EINVAL` and write nothing; size 0 returns 1 and writes nothing; sizes 1 and 471 with a buffer return 0 and write nothing; a null buffer returns 1 at sizes 471, 472 and `2^32 + 472`; `-2^32 + 472` fails with `EINVAL` (a 32-bit read would see 472); the unreadable `0x10` with a full-size buffer fails with `EFAULT`; with room for two structs, both modes return 1, the second slot stays untouched, and the first matches `_fstatfs` on `/`. On `main` the test does not compile (the stub has a `(void)` signature); on this branch it passes 3 runs out of 3. - Full suite: 2356 tests. The only failures are Python tooling tests that time out under load (pass alone) and `agc_host_import_cut_lifetime`, `agc_driver_depth_plane_partial_store`, `pr_overlap_encoding`, which fail the same way on an unrelated checkout in the same environment; `agc_driver_depth_clear_hle` and `guest_select` flaked in parallel and pass alone. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, a console, a title run. ### Overlaps - #11032 (`kenv`, mine) adds code at the same place in `Export.cpp` (L343) and `GuestKernelSys.cpp` (L524): #11032 goes first, then this PR rebases. - #10427 (`GuestKernelSys.cpp` L524) and #10799 (`libkernel_sys.md` debt L40) touch neighbouring lines: proposed on each that it goes first and this PR rebases. - No open PR implements `getfsstat` or `getmntinfo` or changes `_fstatfs`. #9161 (`statfs`) shares no lines. ### 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`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source: none added - [x] Generic behaviour, not specific to one title - [x] Depends on: #11032; merge order: after #11032, #10427 and #10799 (neighbouring lines) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 14:34:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:34:48Z.

Review: CHANGES_REQUESTED


Requesting a correction to the short-buffer return value and its regression. For a non-null buffer with 0 < bufsize < sizeof(struct statfs), FreeBSD 11.4 returns 0 because no records fit. In kern_getfsstat, maxcount is zero; the mount loop still counts the filesystem, but the final if (sfsp != NULL && count > maxcount) caps the result to maxcount (0). By contrast, bufsize == 0 sets sfsp to null and returns the total count (1). The current implementation returns 1 for every size below one structure, and the test asserts 1 for Size - 1 with a non-null buffer, so this case disagrees with the cited reference.

Please distinguish zero capacity from a nonzero undersized buffer and update the tests for both. The reference is kern_getfsstat at lines 2911–2919 and 3095–3115: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_syscalls.c#L2911-L3115.

Exact-head local checks: git diff --check, conventions, and C++20 syntax checks for Export.cpp and GuestKernelSys.cpp pass. The hosted product jobs are skipped; the author reports the Windows focused suite, which I have not independently run.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643686014 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11031#pullrequestreview-5643686014). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T14:34:48Z. **Review:** CHANGES_REQUESTED --- Requesting a correction to the short-buffer return value and its regression. For a non-null buffer with `0 < bufsize < sizeof(struct statfs)`, FreeBSD 11.4 returns 0 because no records fit. In `kern_getfsstat`, `maxcount` is zero; the mount loop still counts the filesystem, but the final `if (sfsp != NULL && count > maxcount)` caps the result to `maxcount` (0). By contrast, `bufsize == 0` sets `sfsp` to null and returns the total count (1). The current implementation returns 1 for every size below one structure, and the test asserts 1 for `Size - 1` with a non-null buffer, so this case disagrees with the cited reference. Please distinguish zero capacity from a nonzero undersized buffer and update the tests for both. The reference is `kern_getfsstat` at lines 2911–2919 and 3095–3115: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_syscalls.c#L2911-L3115. Exact-head local checks: `git diff --check`, conventions, and C++20 syntax checks for `Export.cpp` and `GuestKernelSys.cpp` pass. The hosted product jobs are skipped; the author reports the Windows focused suite, which I have not independently run.
MarcZX10 commented 2026-10-25 15:20:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T15:20:33Z.

Review: DISMISSED


One additional ABI issue to address alongside the existing short-buffer finding: getfsstat_nid_postfix declares bufsize as C++ long in both Api.hpp and its definition. On Windows x64 (LLP64), long is 32-bit, but the PS5/FreeBSD amd64 syscall ABI uses a 64-bit long for this argument. That makes this exported function decode the guest argument with the wrong width on the Windows host build and can truncate/misinterpret values above LONG_MAX (or a negative 64-bit value whose low 32 bits differ). Please use an explicit 64-bit guest ABI type consistently in the declaration/definition and add a regression covering the 64-bit boundary/sign-extension behavior on Windows. The repository’s Windows guest tests are a good place to validate this. The FreeBSD 11.4 syscall prototype also declares long bufsize: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_syscalls.c#L371-L378.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643838575 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11031#pullrequestreview-5643838575). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T15:20:33Z. **Review:** DISMISSED --- One additional ABI issue to address alongside the existing short-buffer finding: `getfsstat_nid_postfix` declares `bufsize` as C++ `long` in both `Api.hpp` and its definition. On Windows x64 (LLP64), `long` is 32-bit, but the PS5/FreeBSD amd64 syscall ABI uses a 64-bit `long` for this argument. That makes this exported function decode the guest argument with the wrong width on the Windows host build and can truncate/misinterpret values above `LONG_MAX` (or a negative 64-bit value whose low 32 bits differ). Please use an explicit 64-bit guest ABI type consistently in the declaration/definition and add a regression covering the 64-bit boundary/sign-extension behavior on Windows. The repository’s Windows guest tests are a good place to validate this. The FreeBSD 11.4 syscall prototype also declares `long bufsize`: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_syscalls.c#L371-L378.
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!11031
No description provided.