fix(libkernel_sys): answer raw __getcwd errors in FreeBSD's order and kinds #16370

Merged
Ragbie merged 3 commits from upstream-pr/16370 into main 2026-11-10 11:55:52 +00:00
Ragbie commented 2026-11-06 12:32:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-11-06T12:32:49Z.


What

The raw __getcwd syscall in libkernel_sys now answers the errors FreeBSD 11.4's kern___getcwd answers, in the same order. The libkernel_sys.md debt entry listed these differences as reviewed and not implemented; #10332's review asked for them to be mapped or listed, and they were listed.

  • sys___getcwd passes buflen as a u_int with MAXPATHLEN as the limit. A size below 2 answers EINVAL before the buffer is looked at, so size 1 and a null buffer with size 0 now answer EINVAL (22), where they answered ERANGE (34) and EFAULT (14). The size is truncated to 32 bits first, as the kernel does.
  • A size above MAXPATHLEN (1024) is lowered to it; a path that does not fit answers ENOMEM (12) from vn_fullpath1, where it answered ERANGE.
  • The buffer is written with copyout only after the path is found, so a null or unwritable buffer answers EFAULT. The guest call now copies through GuestCopyOut_nid_no_patch, so an unwritable buffer, or one that runs into an unmapped page, answers EFAULT instead of faulting in the host. On success it still writes only strlen + 1 bytes.
  • The path still comes from the libc getcwd model, with its errors (for example ENOENT for a removed directory) passed through. libc getcwd itself is unchanged.
  • The debt entry now describes the FreeBSD behaviour instead of the differences.

Tested

guest_kernel_sys: CheckRawGetcwd expects EINVAL for size 1. The new CheckRawGetcwdErrors moves into a fresh guest directory, so the path is at least 2 bytes long, and checks ENOMEM for a buffer one byte short with the buffer untouched, success with an exact fit, EINVAL for a null buffer with size 0 and 1 and for size 2^32 + 1, and EFAULT for an unmapped buffer and for one that crosses into an unmapped page.

  • Without the fix: Kernel sys check failed at line 1229 (guest errno 34).
  • With the fix: Windows 11 Pro, Intel Core i5-14600KF, NVIDIA GPU, WinLibs GCC 15.2.0, full Release build with libs: the 33 tests matching ^guest_kernel_sys pass, twice in a row; no directory is left behind. Full ctest: 99% tests passed, 6 tests failed out of 4329. All six fail on main on this machine too:
    • test_registration and select_tests_MissingCommandTests fail with WinError 1314, because this user cannot create symbolic links.
    • agc_driver_direct_write_completion, agc_driver_image_plane_address_on_line, agc_driver_image_sample_lod_clamp and agc_driver_image_sample_lod_clamp_shader_lane are GPU tests on an NVIDIA card.
  • check_skips.py reports one skip, guest_fopen_errors, which skips itself here because this user cannot create symbolic links.
  • Linux (Ubuntu 24.04 container, GCC 13.3.0): the 35 tests matching ^guest_kernel_sys pass.
  • macOS: not tested. The change has no host-specific code; GuestCopyOut_nid_no_patch has its own macOS path.

The full runs were on main 6185bdcfc4; after rebasing onto 8b9958e681 the ^guest_kernel_sys tests were run again on Windows.

Overlaps: #11031 and #16149 add functions next to CheckRawGetcwd in GuestKernelSys.cpp; the new test function sits before it, so trial merges with both have no conflicts (git merge-tree). #15545 changes libc getcwd for size 1; this call checks the size before reaching libc, so the result does not depend on the order the two land in, and a trial merge has no conflicts.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5187876020 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16370). Originally posted by [Ragbie](https://github.com/Ragbie) on 2026-11-06T12:32:49Z. --- ### What The raw `__getcwd` syscall in libkernel_sys now answers the errors FreeBSD 11.4's [`kern___getcwd`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_cache.c#L2139-L2179) answers, in the same order. The `libkernel_sys.md` debt entry listed these differences as reviewed and not implemented; #10332's review asked for them to be mapped or listed, and they were listed. - `sys___getcwd` passes `buflen` as a `u_int` with `MAXPATHLEN` as the limit. A size below 2 answers `EINVAL` before the buffer is looked at, so size 1 and a null buffer with size 0 now answer `EINVAL` (22), where they answered `ERANGE` (34) and `EFAULT` (14). The size is truncated to 32 bits first, as the kernel does. - A size above `MAXPATHLEN` (1024) is lowered to it; a path that does not fit answers `ENOMEM` (12) from `vn_fullpath1`, where it answered `ERANGE`. - The buffer is written with `copyout` only after the path is found, so a null or unwritable buffer answers `EFAULT`. The guest call now copies through `GuestCopyOut_nid_no_patch`, so an unwritable buffer, or one that runs into an unmapped page, answers `EFAULT` instead of faulting in the host. On success it still writes only `strlen + 1` bytes. - The path still comes from the libc `getcwd` model, with its errors (for example `ENOENT` for a removed directory) passed through. libc `getcwd` itself is unchanged. - The debt entry now describes the FreeBSD behaviour instead of the differences. ### Tested `guest_kernel_sys`: `CheckRawGetcwd` expects `EINVAL` for size 1. The new `CheckRawGetcwdErrors` moves into a fresh guest directory, so the path is at least 2 bytes long, and checks `ENOMEM` for a buffer one byte short with the buffer untouched, success with an exact fit, `EINVAL` for a null buffer with size 0 and 1 and for size `2^32 + 1`, and `EFAULT` for an unmapped buffer and for one that crosses into an unmapped page. - **Without the fix:** `Kernel sys check failed at line 1229 (guest errno 34)`. - **With the fix:** Windows 11 Pro, Intel Core i5-14600KF, NVIDIA GPU, WinLibs GCC 15.2.0, full Release build with `libs`: the 33 tests matching `^guest_kernel_sys` pass, twice in a row; no directory is left behind. Full `ctest`: `99% tests passed, 6 tests failed out of 4329`. All six fail on `main` on this machine too: - `test_registration` and `select_tests_MissingCommandTests` fail with `WinError 1314`, because this user cannot create symbolic links. - `agc_driver_direct_write_completion`, `agc_driver_image_plane_address_on_line`, `agc_driver_image_sample_lod_clamp` and `agc_driver_image_sample_lod_clamp_shader_lane` are GPU tests on an NVIDIA card. - `check_skips.py` reports one skip, `guest_fopen_errors`, which skips itself here because this user cannot create symbolic links. - Linux (Ubuntu 24.04 container, GCC 13.3.0): the 35 tests matching `^guest_kernel_sys` pass. - macOS: not tested. The change has no host-specific code; `GuestCopyOut_nid_no_patch` has its own macOS path. The full runs were on `main` `6185bdcfc4`; after rebasing onto `8b9958e681` the `^guest_kernel_sys` tests were run again on Windows. **Overlaps:** #11031 and #16149 add functions next to `CheckRawGetcwd` in `GuestKernelSys.cpp`; the new test function sits before it, so trial merges with both have no conflicts (`git merge-tree`). #15545 changes libc `getcwd` for size 1; this call checks the size before reaching libc, so the result does not depend on the order the two land in, and a trial merge has no conflicts. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes
ahmadmuj commented 2026-11-06 13:22:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-11-06T13:22:22Z.

Review: COMMENTED


Head: c9708515bdeffcd9d6dc89bb154e5620f9d9abcd. PR base: 921621976c52e841b3f88da29add24aa7bb27e8e. Compared with main e8b1bd3d7f1f639ff455b17d9bee1469bdeb4e9e.

Could you add coverage for the internal path limit? CheckRawGetcwdErrors exercises the later required > limit check. It does not reach the libc ERANGE mapping.

I checked the production libc and libkernel_sys modules with a separate Linux probe. Four 200-byte directory components, then sibling components of 218/219 bytes, give guest paths of 1023/1024 bytes. These lengths include the leading slash and exclude the NUL. Short relative guest chdir calls enter each component. Libc getcwd(..., 4096) first returns each exact path, so the fixture is verified.

  • Head: length 1023 succeeds with exact bytes and NUL; every remaining sentinel stays unchanged.
  • Head: length 1024 returns -1/ENOMEM (12), with all 4096 sentinels unchanged. A null buffer also returns ENOMEM.
  • Main: length 1024 succeeds; the null-buffer case returns EFAULT (14).
  • Replacing only Fail(error == GuestErange ? GuestEnomem : error) with Fail(error) leaves guest_kernel_sys passing. The new probe fails with errno 34 for both length-1024 calls.
  • Restoring the mapping makes both tests pass again. Fixture cleanup passes in every run.

This agrees with FreeBSD 11.4's buffer cap and copyout order. This is a coverage suggestion; I found no defect in this boundary behavior.

Tested: Ubuntu 24.04 under WSL2, GCC 13.3, Release. Separate main/head builds use their exact project sources, plus the isolated probe and its registration, with shared dependency sources. Five configuration-only dependencies differ from their gitlinks; none appears in these targets' build commands. Commands: cmake --build <build> --target guest_kernel_sys_tests raw_getcwd_cap_probe -j 16, then ctest --test-dir <build> -R '^(guest_kernel_sys|raw_getcwd_cap_probe)$' -V; each test finishes below one second. python3 tools/check_conventions.py --base origin/main passes. Full suite, Windows and console behavior are not tested here. GPUs tested: n/a.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5800644658 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16370#pullrequestreview-5800644658). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-11-06T13:22:22Z. **Review:** COMMENTED --- Head: `c9708515bdeffcd9d6dc89bb154e5620f9d9abcd`. PR base: `921621976c52e841b3f88da29add24aa7bb27e8e`. Compared with main `e8b1bd3d7f1f639ff455b17d9bee1469bdeb4e9e`. Could you add coverage for the internal path limit? `CheckRawGetcwdErrors` exercises the later `required > limit` check. It does not reach the libc `ERANGE` mapping. I checked the production libc and libkernel_sys modules with a separate Linux probe. Four 200-byte directory components, then sibling components of 218/219 bytes, give guest paths of 1023/1024 bytes. These lengths include the leading slash and exclude the NUL. Short relative guest `chdir` calls enter each component. Libc `getcwd(..., 4096)` first returns each exact path, so the fixture is verified. - Head: length 1023 succeeds with exact bytes and NUL; every remaining sentinel stays unchanged. - Head: length 1024 returns -1/ENOMEM (12), with all 4096 sentinels unchanged. A null buffer also returns ENOMEM. - Main: length 1024 succeeds; the null-buffer case returns EFAULT (14). - Replacing only `Fail(error == GuestErange ? GuestEnomem : error)` with `Fail(error)` leaves `guest_kernel_sys` passing. The new probe fails with errno 34 for both length-1024 calls. - Restoring the mapping makes both tests pass again. Fixture cleanup passes in every run. This agrees with [FreeBSD 11.4's buffer cap and copyout order](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_cache.c#L2140-L2177). This is a coverage suggestion; I found no defect in this boundary behavior. Tested: Ubuntu 24.04 under WSL2, GCC 13.3, Release. Separate main/head builds use their exact project sources, plus the isolated probe and its registration, with shared dependency sources. Five configuration-only dependencies differ from their gitlinks; none appears in these targets' build commands. Commands: `cmake --build <build> --target guest_kernel_sys_tests raw_getcwd_cap_probe -j 16`, then `ctest --test-dir <build> -R '^(guest_kernel_sys|raw_getcwd_cap_probe)$' -V`; each test finishes below one second. `python3 tools/check_conventions.py --base origin/main` passes. Full suite, Windows and console behavior are not tested here. GPUs tested: n/a.
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!16370
No description provided.