feat(libkernel_sys): implement __getcwd #10332

Merged
MarcZX10 merged 5 commits from upstream-pr/10332 into main 2026-11-04 17:34:56 +00:00
MarcZX10 commented 2026-10-23 18:11:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T18:11:33Z.


What

Add the missing libkernel_sys raw __getcwd export (Qsxbb+aty0U). It reuses the guest-root-aware cwd implementation, requires caller-provided storage (EFAULT for null), returns 0 on success, and returns -1 with the guest errno on failure. The raw syscall signature is int __getcwd(char *buf, size_t buflen); the FreeBSD syscall table defines it as syscall 326. A PS5 port report notes that PS5 libc getcwd calls this libkernel_sys export.

Tested

  • Linux, GCC 12.2: strict C++20 syntax checks (-Wall -Wextra -Werror) passed for Export.cpp and GuestKernelSys.cpp.
  • A temporary focused runtime harness with a mocked libc helper passed success, canary, errno-forwarding, zero-size, and null-buffer checks. The harness is not part of the PR.
  • python3 tools/check_conventions.py --base upstream/main and git diff --check passed.
  • python3 tools/pr_overlap.py --branch codex/libkernel-sys-getcwd reported no confirmed overlap. Conflicted legacy PRs were checked manually; no open PR adds this export or NID.
  • The guest_kernel_sys_tests target was not run: CMake configuration stops because several pinned third-party submodules are absent (including ffmpeg-core, freetype, glslang, and LibAtrac9). No PS5 hardware test was run.

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:4987257648 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10332). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T18:11:33Z. --- ### What Add the missing `libkernel_sys` raw `__getcwd` export (`Qsxbb+aty0U`). It reuses the guest-root-aware cwd implementation, requires caller-provided storage (`EFAULT` for null), returns 0 on success, and returns -1 with the guest errno on failure. The raw syscall signature is `int __getcwd(char *buf, size_t buflen)`; the [FreeBSD syscall table](https://cgit.freebsd.org/src/tree/sys/kern/syscalls.master) defines it as syscall 326. A [PS5 port report](https://github.com/FermataRest/PS5_RetroArch/blob/main/docs/RPCS3_PORT.md) notes that PS5 libc `getcwd` calls this `libkernel_sys` export. ### Tested - Linux, GCC 12.2: strict C++20 syntax checks (`-Wall -Wextra -Werror`) passed for `Export.cpp` and `GuestKernelSys.cpp`. - A temporary focused runtime harness with a mocked libc helper passed success, canary, errno-forwarding, zero-size, and null-buffer checks. The harness is not part of the PR. - `python3 tools/check_conventions.py --base upstream/main` and `git diff --check` passed. - `python3 tools/pr_overlap.py --branch codex/libkernel-sys-getcwd` reported no confirmed overlap. Conflicted legacy PRs were checked manually; no open PR adds this export or NID. - The `guest_kernel_sys_tests` target was not run: CMake configuration stops because several pinned third-party submodules are absent (including `ffmpeg-core`, `freetype`, `glslang`, and `LibAtrac9`). No PS5 hardware test was run. ### 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 01:04:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T01:04:23Z.

Review: COMMENTED


Checked against main 11d6efa06c and FreeBSD 11.4 sys/kern/vfs_cache.c (sys___getcwd, kern___getcwd, vn_fullpath1) and lib/libc/gen/getcwd.c:

  • This still merges cleanly. Success behaves correctly: it returns 0 and writes the guest path ("/" relative to the guest root) with its NUL, because it reuses main's getcwd_nid_postfix.
  • The errors come from libc getcwd, not the syscall. FreeBSD's kern___getcwd starts with if (buflen < 2) return (EINVAL), so __getcwd(buf, 1) should fail with EINVAL, not the ERANGE that GuestKernelSys.cpp:191 asserts. Size 1 → ERANGE is done by libc's getcwd.c before it calls __getcwd.
  • With a buffer of 2 or more that is still too short (for example cwd /app0, buflen 3), FreeBSD's vn_fullpath1 returns ENOMEM. This PR returns ERANGE, because main's getcwd uses 34 for any short buffer (General.cpp:770).
  • Smaller point: __getcwd(NULL, 0) hits the buflen < 2 check first in FreeBSD and gives EINVAL. Here it returns EFAULT.
  • Could the length checks be done in __getcwd_nid_postfix itself (< 2 → EINVAL, too short → ENOMEM), with the test updated? Otherwise, could the debt entry say which errors differ, since it currently says it follows sys___getcwd?

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5635987009 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10332#pullrequestreview-5635987009). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T01:04:23Z. **Review:** COMMENTED --- Checked against main 11d6efa06c and FreeBSD 11.4 `sys/kern/vfs_cache.c` (`sys___getcwd`, `kern___getcwd`, `vn_fullpath1`) and `lib/libc/gen/getcwd.c`: - This still merges cleanly. Success behaves correctly: it returns 0 and writes the guest path ("/" relative to the guest root) with its NUL, because it reuses main's `getcwd_nid_postfix`. - The errors come from libc `getcwd`, not the syscall. FreeBSD's `kern___getcwd` starts with `if (buflen < 2) return (EINVAL)`, so `__getcwd(buf, 1)` should fail with `EINVAL`, not the `ERANGE` that `GuestKernelSys.cpp:191` asserts. Size 1 → ERANGE is done by libc's `getcwd.c` before it calls `__getcwd`. - With a buffer of 2 or more that is still too short (for example cwd `/app0`, buflen 3), FreeBSD's `vn_fullpath1` returns `ENOMEM`. This PR returns `ERANGE`, because main's `getcwd` uses 34 for any short buffer (`General.cpp:770`). - Smaller point: `__getcwd(NULL, 0)` hits the `buflen < 2` check first in FreeBSD and gives `EINVAL`. Here it returns `EFAULT`. - Could the length checks be done in `__getcwd_nid_postfix` itself (`< 2` → EINVAL, too short → ENOMEM), with the test updated? Otherwise, could the debt entry say which errors differ, since it currently says it follows `sys___getcwd`? Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
XairRrR commented 2026-10-29 11:55:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T11:55:31Z.

Review: APPROVED


Reviewed — approved

__getcwd merges cleanly onto current main, builds, its new test passes on Linux and fails when the null-buffer check is removed, and the remaining errno differences from FreeBSD are recorded in the debt file as agreed in the earlier review thread.

What I ran

Head a2e7b248ae589f2c804d0767e9731aeab1de8c2c, merged with --no-ff onto main ba5b5330b37724bca4b99f30faab504653635a04 in a clean worktree. Arch Linux 7.2.11-zen, GCC 16.2.1, Release, Ninja, BUILD_TESTING=ON, Intel Core i9-14900KF.

git fetch -q origin main pull/10332/head:refs/review/10332
git checkout -q --detach origin/main
git merge -q --no-ff --no-edit refs/review/10332
git diff --check origin/main...refs/review/10332
python3 tools/check_conventions.py --base origin/main
cmake --build build -j10
cmake --build build -j10 --target libs
ctest --test-dir build -j4 --output-on-failure -R '^(guest_kernel_sys|public_headers)'

# negative control 1: production files from main
git checkout origin/main -- core/libs/prx/libkernel_sys/Export.cpp core/libs/prx/libkernel_sys/include/Api.hpp
cmake --build build -j10 --target guest_kernel_sys_tests
# negative control 2: merged tree minus the `buffer == nullptr` line in __getcwd_nid_postfix
cmake --build build -j10
ctest --test-dir build --output-on-failure -R '^guest_kernel_sys$'
# restored the merged files, rebuilt, reran guest_kernel_sys

Results

Check Result
Merge onto main Clean (Api.hpp auto-merged)
git diff --check Clean
tools/check_conventions.py --base origin/main All conventions checks passed.
Full build, libs target OK
guest_kernel_sys* (23 tests) and public_headers* (65 tests) 88/88 passed, 17.4 s total
guest_kernel_sys after restoring the merged files Passed
ctest tail
86/88 Test #459: guest_kernel_sys_aio ..............................   Passed    0.02 sec
87/88 Test #345: public_headers_compile_18 .........................   Passed    1.57 sec
88/88 Test #457: guest_kernel_sys_ktimer ...........................   Passed    0.12 sec

100% tests passed out of 88

Total Test time (real) =  17.41 sec

Negative control

  1. With Export.cpp and Api.hpp taken from main, GuestKernelSys.cpp does not compile (__getcwd_nid_postfix was not declared in this scope). As expected, but that only shows that the test uses the new export.
  2. For a behavioural check I kept the PR and removed only the if (buffer == nullptr) return ... Fail(GuestEfault) line. guest_kernel_sys then fails (Subprocess aborted): with that line gone, getcwd_nid_postfix allocates a buffer and the call returns 0 where CheckRawGetcwd expects EFAULT. With the line restored, it passes again.

Findings

Blocking

None.

Non-blocking

  • One more FreeBSD difference is missing from the debt list. I agree with harisibrar1's reading of FreeBSD 11.4. In sys___getcwd / kern___getcwd, buflen < 2 returns EINVAL before any other check. That source also shows a difference the new entry at docs/dev/debt/libkernel_sys.md:18 does not list: sys___getcwd passes MAXPATHLEN as path_max, and buflen is clamped to it, so a working directory of 1024 bytes or more fails with ENOMEM however large the buffer is. Here __getcwd_nid_postfix (core/libs/prx/libkernel_sys/Export.cpp:710-713) passes the caller's size to getcwd_nid_postfix, which succeeds for any length. Paths that long are unlikely under the guest root, but the entry presents its list as the reviewed differences, so this one belongs there too. You could add it to the list, or clamp size to 1024 in the wrapper.
  • Mapping the errors may be cheaper than recording them. All three listed differences can be fixed in the wrapper. Put size < 2 → EINVAL first, then the null check. If getcwd_nid_postfix fails with 34, ERANGE, rewrite it to 12, ENOMEM. A short buffer is the only case where the libc helper sets 34 (core/libs/prx/libc/src/General.cpp:932). The Error() == 34 assertion at core/libs/tests/GuestKernelSys.cpp:912 would then expect 22. The debt entry could then be cut down to the remaining "unverified on hardware" note. This is optional; a collaborator already chose recording over mapping.
  • The debt entry has no source link. It names FreeBSD sys___getcwd without a link. The neighbouring entries (kenv, lchmod) link the FreeBSD 11.4 source at a fixed revision. The link above would do.

Not verified

  • Windows and macOS builds. Windows runs are reported by other reviewers in the comments; I did not repeat them.
  • Console behaviour of __getcwd (errno values, path format, MAXPATHLEN clamp). No hardware was available.
  • That NID Qsxbb+aty0U resolves to this export at relink time. I only know that the progress-report bot counts it as a newly implemented libkernel_sys export; I did not relink a title.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5697251173 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10332#pullrequestreview-5697251173). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-29T11:55:31Z. **Review:** APPROVED --- # Reviewed — approved `__getcwd` merges cleanly onto current `main`, builds, its new test passes on Linux and fails when the null-buffer check is removed, and the remaining errno differences from FreeBSD are recorded in the debt file as agreed in the earlier review thread. ## What I ran Head `a2e7b248ae589f2c804d0767e9731aeab1de8c2c`, merged with `--no-ff` onto `main` `ba5b5330b37724bca4b99f30faab504653635a04` in a clean worktree. Arch Linux 7.2.11-zen, GCC 16.2.1, Release, Ninja, `BUILD_TESTING=ON`, Intel Core i9-14900KF. ```sh git fetch -q origin main pull/10332/head:refs/review/10332 git checkout -q --detach origin/main git merge -q --no-ff --no-edit refs/review/10332 git diff --check origin/main...refs/review/10332 python3 tools/check_conventions.py --base origin/main cmake --build build -j10 cmake --build build -j10 --target libs ctest --test-dir build -j4 --output-on-failure -R '^(guest_kernel_sys|public_headers)' # negative control 1: production files from main git checkout origin/main -- core/libs/prx/libkernel_sys/Export.cpp core/libs/prx/libkernel_sys/include/Api.hpp cmake --build build -j10 --target guest_kernel_sys_tests # negative control 2: merged tree minus the `buffer == nullptr` line in __getcwd_nid_postfix cmake --build build -j10 ctest --test-dir build --output-on-failure -R '^guest_kernel_sys$' # restored the merged files, rebuilt, reran guest_kernel_sys ``` ## Results | Check | Result | | - | - | | Merge onto `main` | Clean (`Api.hpp` auto-merged) | | `git diff --check` | Clean | | `tools/check_conventions.py --base origin/main` | `All conventions checks passed.` | | Full build, `libs` target | OK | | `guest_kernel_sys*` (23 tests) and `public_headers*` (65 tests) | 88/88 passed, 17.4 s total | | `guest_kernel_sys` after restoring the merged files | Passed | <details><summary>ctest tail</summary> ``` 86/88 Test #459: guest_kernel_sys_aio .............................. Passed 0.02 sec 87/88 Test #345: public_headers_compile_18 ......................... Passed 1.57 sec 88/88 Test #457: guest_kernel_sys_ktimer ........................... Passed 0.12 sec 100% tests passed out of 88 Total Test time (real) = 17.41 sec ``` </details> ## Negative control 1. With `Export.cpp` and `Api.hpp` taken from `main`, `GuestKernelSys.cpp` does not compile (`__getcwd_nid_postfix` was not declared in this scope). As expected, but that only shows that the test uses the new export. 2. For a behavioural check I kept the PR and removed only the `if (buffer == nullptr) return ... Fail(GuestEfault)` line. `guest_kernel_sys` then fails (`Subprocess aborted`): with that line gone, `getcwd_nid_postfix` allocates a buffer and the call returns 0 where `CheckRawGetcwd` expects `EFAULT`. With the line restored, it passes again. ## Findings ### Blocking None. ### Non-blocking - **One more FreeBSD difference is missing from the debt list.** I agree with harisibrar1's reading of FreeBSD 11.4. In [`sys___getcwd` / `kern___getcwd`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_cache.c#L2131-L2153), `buflen < 2` returns `EINVAL` before any other check. That source also shows a difference the new entry at `docs/dev/debt/libkernel_sys.md:18` does not list: `sys___getcwd` passes `MAXPATHLEN` as `path_max`, and `buflen` is clamped to it, so a working directory of 1024 bytes or more fails with `ENOMEM` however large the buffer is. Here `__getcwd_nid_postfix` (`core/libs/prx/libkernel_sys/Export.cpp:710-713`) passes the caller's size to `getcwd_nid_postfix`, which succeeds for any length. Paths that long are unlikely under the guest root, but the entry presents its list as the reviewed differences, so this one belongs there too. You could add it to the list, or clamp `size` to 1024 in the wrapper. - **Mapping the errors may be cheaper than recording them.** All three listed differences can be fixed in the wrapper. Put `size < 2` → `EINVAL` first, then the null check. If `getcwd_nid_postfix` fails with 34, `ERANGE`, rewrite it to 12, `ENOMEM`. A short buffer is the only case where the libc helper sets 34 (`core/libs/prx/libc/src/General.cpp:932`). The `Error() == 34` assertion at `core/libs/tests/GuestKernelSys.cpp:912` would then expect 22. The debt entry could then be cut down to the remaining "unverified on hardware" note. This is optional; a collaborator already chose recording over mapping. - **The debt entry has no source link.** It names FreeBSD `sys___getcwd` without a link. The neighbouring entries (`kenv`, `lchmod`) link the FreeBSD 11.4 source at a fixed revision. The link above would do. ## Not verified - Windows and macOS builds. Windows runs are reported by other reviewers in the comments; I did not repeat them. - Console behaviour of `__getcwd` (errno values, path format, `MAXPATHLEN` clamp). No hardware was available. - That NID `Qsxbb+aty0U` resolves to this export at relink time. I only know that the progress-report bot counts it as a newly implemented `libkernel_sys` export; I did not relink a title.
MarcZX10 commented 2026-11-02 04:57:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T04:57:13Z.

Review: COMMENTED


Reviewed — three non-blocking points checked, one still open

The errno differences are recorded in the debt entry as oneandonlydean said, but the entry omits the MAXPATHLEN difference raised in review and has no FreeBSD source link.

What I ran

Head a2e7b248ae589f2c804d0767e9731aeab1de8c2c (live, OPEN). Read-only: gh pr view, gh api GET on comments and reviews, git grep and git show at the head, including libc/src/General.cpp. Nothing was built or run.

Results

Check Result Note
Hosted checks 5 success, none failing mergeStateStatus CLEAN
Debt entry lists the three errno differences Confirmed docs/dev/debt/libkernel_sys.md:18
FreeBSD source link in the entry Missing Entry names FreeBSD sys___getcwd without a link
MAXPATHLEN clamp in wrapper Absent Wrapper passes size through

Findings

  • XairRrR (2026-10-29 11:55Z) asks for the MAXPATHLEN difference to be added to the list. Code check: __getcwd_nid_postfix at Export.cpp:710-713 checks null first, then passes size to getcwd_nid_postfix with no 1024-byte clamp. The debt entry does not mention a clamp. The FreeBSD 11.4 source reading the reviewer cites was not fetched, so the clamp itself is not verified.
  • XairRrR's second point: map the errors in the wrapper instead of recording them. Optional. The current order (null check before size) matches the entry; General.cpp:932 sets errno 34 for a short buffer, which the entry describes.
  • XairRrR's third point: add a FreeBSD source link. Confirmed missing. The reviewer's link is a suggestion you can take as written.
  • oneandonlydean (2026-10-29 11:50Z) says the errno differences are listed in the entry instead of mapped. Confirmed at line 18.

Sources

Not verified

  • FreeBSD 11.4 sys___getcwd behavior, including the MAXPATHLEN clamp and the EINVAL order.
  • Console behavior of __getcwd.
  • The Error() == 34 assertion the reviewer cites at GuestKernelSys.cpp:912.
  • Whether NID Qsxbb+aty0U resolves at relink.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5732247999 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10332#pullrequestreview-5732247999). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T04:57:13Z. **Review:** COMMENTED --- # Reviewed — three non-blocking points checked, one still open The errno differences are recorded in the debt entry as oneandonlydean said, but the entry omits the MAXPATHLEN difference raised in review and has no FreeBSD source link. ## What I ran Head `a2e7b248ae589f2c804d0767e9731aeab1de8c2c` (live, OPEN). Read-only: `gh pr view`, `gh api` GET on comments and reviews, `git grep` and `git show` at the head, including `libc/src/General.cpp`. Nothing was built or run. ## Results | Check | Result | Note | |---|---|---| | Hosted checks | 5 success, none failing | mergeStateStatus CLEAN | | Debt entry lists the three errno differences | Confirmed | `docs/dev/debt/libkernel_sys.md:18` | | FreeBSD source link in the entry | Missing | Entry names FreeBSD `sys___getcwd` without a link | | MAXPATHLEN clamp in wrapper | Absent | Wrapper passes `size` through | ## Findings - XairRrR (2026-10-29 11:55Z) asks for the MAXPATHLEN difference to be added to the list. Code check: `__getcwd_nid_postfix` at `Export.cpp:710-713` checks null first, then passes `size` to `getcwd_nid_postfix` with no 1024-byte clamp. The debt entry does not mention a clamp. The FreeBSD 11.4 source reading the reviewer cites was not fetched, so the clamp itself is not verified. - XairRrR's second point: map the errors in the wrapper instead of recording them. Optional. The current order (null check before size) matches the entry; `General.cpp:932` sets errno 34 for a short buffer, which the entry describes. - XairRrR's third point: add a FreeBSD source link. Confirmed missing. The reviewer's link is a suggestion you can take as written. - oneandonlydean (2026-10-29 11:50Z) says the errno differences are listed in the entry instead of mapped. Confirmed at line 18. ## Sources - https://github.com/boykopovar/AnyPS5/blob/a2e7b248ae589f2c804d0767e9731aeab1de8c2c/docs/dev/debt/libkernel_sys.md#L18 - https://github.com/boykopovar/AnyPS5/blob/a2e7b248ae589f2c804d0767e9731aeab1de8c2c/core/libs/prx/libkernel_sys/Export.cpp#L710-L713 - https://github.com/boykopovar/AnyPS5/blob/a2e7b248ae589f2c804d0767e9731aeab1de8c2c/core/libs/prx/libc/src/General.cpp#L932 ## Not verified - FreeBSD 11.4 `sys___getcwd` behavior, including the MAXPATHLEN clamp and the EINVAL order. - Console behavior of `__getcwd`. - The `Error() == 34` assertion the reviewer cites at `GuestKernelSys.cpp:912`. - Whether NID `Qsxbb+aty0U` resolves at relink.
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!10332
No description provided.