feat(libkernel_sys): implement fchdir #3392

Merged
MarcZX10 merged 3 commits from upstream-pr/3392 into main 2026-10-11 10:40:42 +00:00
MarcZX10 commented 2026-10-11 09:40:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T09:40:51Z.


What

Implements fchdir in libkernel_sys for PPSA99764. It resolves an open directory descriptor on Windows from its recorded path and on Linux through /proc/self/fd, then updates the guest working directory. Invalid descriptors return EBADF; open non-directory descriptors return ENOTDIR.

The original implementation commit by Tharun Balaji is preserved with its author attribution. Follow-up commits keep chdir resolution and cwd updates atomic, and use C ABI helpers across PRXs for descriptor-path lookup and cwd updates.

Closes #3288.

Tested

Linux, Debian, GCC 12.2.0, Release, Unix Makefiles, based on current main 99c1c26347:

  • Built the full libs target and the affected test executables.
  • patched_libraries, guest_kernel_sys, guest_access, guest_working_directory, guest_realpath, guest_path_case, guest_directory, and guest_filesystem each passed three consecutive runs (24 test executions).
  • tools/check_conventions.py --base upstream/main and git diff --check passed.

Windows tests were reported for the source commit in the issue, but were not rerun on this rebased head. Console behavior is unverified. Repository CI skips the full Linux/Windows build jobs on ordinary PR runs.

Checklist

  • Based on current main; no open PR implements fchdir
  • One topic; no comments added to code
  • No silent stubs, third-party code, investigation notes, or images added
  • Generic behavior; limitations are recorded in TechnicalDebt
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4818120955 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3392). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T09:40:51Z. --- ### What Implements `fchdir` in `libkernel_sys` for PPSA99764. It resolves an open directory descriptor on Windows from its recorded path and on Linux through `/proc/self/fd`, then updates the guest working directory. Invalid descriptors return `EBADF`; open non-directory descriptors return `ENOTDIR`. The original implementation commit by Tharun Balaji is preserved with its author attribution. Follow-up commits keep `chdir` resolution and cwd updates atomic, and use C ABI helpers across PRXs for descriptor-path lookup and cwd updates. Closes #3288. ### Tested Linux, Debian, GCC 12.2.0, Release, Unix Makefiles, based on current `main` `99c1c26347`: - Built the full `libs` target and the affected test executables. - `patched_libraries`, `guest_kernel_sys`, `guest_access`, `guest_working_directory`, `guest_realpath`, `guest_path_case`, `guest_directory`, and `guest_filesystem` each passed three consecutive runs (24 test executions). - `tools/check_conventions.py --base upstream/main` and `git diff --check` passed. Windows tests were reported for the source commit in the issue, but were not rerun on this rebased head. Console behavior is unverified. Repository CI skips the full Linux/Windows build jobs on ordinary PR runs. ### Checklist - [x] Based on current `main`; no open PR implements `fchdir` - [x] One topic; no comments added to code - [x] No silent stubs, third-party code, investigation notes, or images added - [x] Generic behavior; limitations are recorded in TechnicalDebt - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-11 09:43:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T09:43:40Z.


Progress report (99c1c26 → 1a73d01)

➖ System libraries: 85.76% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel_sys fchdir_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107701446 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3392#issuecomment-6107701446). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T09:43:40Z. --- <!-- progress-report --> ### Progress report (99c1c26 → 1a73d01) ➖ **System libraries**: 85.76% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel_sys | `fchdir_nid_postfix` | </details>
adriadam10 commented 2026-10-11 09:53:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T09:53:24Z.

Review: CHANGES_REQUESTED


Thanks for picking this up. One blocker:

  • patched_libraries fails on this head: libkernel_sys.prx: undefined symbol: _Z29DescriptorIsOpen_nid_no_patchi (core/libs/prx/libkernel_sys/Export.cpp:73). guest_kernel_sys passes only because the test links the libraries statically, but the real libkernel_sys.prx doesn't load, so fchdir would not resolve in a title.
  • Cause: DescriptorIsOpen_nid_no_patch (File.hpp) and File::DirectoryDescriptorPath (DirectoryDescriptor.hpp, used at Export.cpp:74) are plain C++ symbols, not extern "C" exports. Cross-library _nid_no_patch helpers are extern "C" (like ResolvePath_nid_no_patch), and their signatures should avoid C++ types where possible (SetWorkingDirectory_nid_no_patch takes const std::filesystem::path& in an extern "C" function; check the other helpers for how they pass paths, e.g. const char*).
  • Fix: expose one extern "C" helper from libkernel that takes the fd and returns the directory path as a C string or errno (checking open + directory there), and call it from fchdir; then run ctest -R patched_libraries to confirm all patched libraries load.

The rest looks right: EBADF/ENOTDIR match FreeBSD fchdir, the chdir refactor keeps resolution and update under one lock, and the TechnicalDebt line is in place.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5482822812 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3392#pullrequestreview-5482822812). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T09:53:24Z. **Review:** CHANGES_REQUESTED --- Thanks for picking this up. One blocker: - `patched_libraries` fails on this head: `libkernel_sys.prx: undefined symbol: _Z29DescriptorIsOpen_nid_no_patchi` (`core/libs/prx/libkernel_sys/Export.cpp:73`). `guest_kernel_sys` passes only because the test links the libraries statically, but the real `libkernel_sys.prx` doesn't load, so `fchdir` would not resolve in a title. - Cause: `DescriptorIsOpen_nid_no_patch` (`File.hpp`) and `File::DirectoryDescriptorPath` (`DirectoryDescriptor.hpp`, used at `Export.cpp:74`) are plain C++ symbols, not `extern "C"` exports. Cross-library `_nid_no_patch` helpers are `extern "C"` (like `ResolvePath_nid_no_patch`), and their signatures should avoid C++ types where possible (`SetWorkingDirectory_nid_no_patch` takes `const std::filesystem::path&` in an `extern "C"` function; check the other helpers for how they pass paths, e.g. `const char*`). - Fix: expose one `extern "C"` helper from libkernel that takes the fd and returns the directory path as a C string or errno (checking open + directory there), and call it from `fchdir`; then run `ctest -R patched_libraries` to confirm all patched libraries load. The rest looks right: `EBADF`/`ENOTDIR` match FreeBSD `fchdir`, the `chdir` refactor keeps resolution and update under one lock, and the TechnicalDebt line is in place.
MarcZX10 commented 2026-10-11 10:03:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T10:03:08Z.


Rebased the fix onto current main (99c1c26347); the updated head is 1a73d0165.

  • libkernel exposes a C-linkage helper that validates the descriptor and directory, then returns a UTF-8 path or POSIX EBADF / ENOTDIR. libkernel_sys no longer imports C++-mangled descriptor helpers.
  • SetWorkingDirectory_nid_no_patch takes a C string and converts UTF-8 inside libc.
  • I rebuilt the full libs target and affected test executables. patched_libraries, guest_kernel_sys, guest_access, guest_working_directory, guest_realpath, guest_path_case, guest_directory, and guest_filesystem each passed three consecutive runs (24 passes total). Conventions and git diff --check pass.

The new head has fresh GitHub Actions checks running. Could you please re-review it once those checks finish? Windows tests from the source commit have not been rerun on this head, and console behavior remains unverified.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107850020 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3392#issuecomment-6107850020). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T10:03:08Z. --- Rebased the fix onto current `main` (`99c1c26347`); the updated head is [`1a73d0165`](https://github.com/MarcZX100/AnyPS5/commit/1a73d0165e9ba2218c8c9f33ad31a143020c4fed). - `libkernel` exposes a C-linkage helper that validates the descriptor and directory, then returns a UTF-8 path or POSIX `EBADF` / `ENOTDIR`. `libkernel_sys` no longer imports C++-mangled descriptor helpers. - `SetWorkingDirectory_nid_no_patch` takes a C string and converts UTF-8 inside `libc`. - I rebuilt the full `libs` target and affected test executables. `patched_libraries`, `guest_kernel_sys`, `guest_access`, `guest_working_directory`, `guest_realpath`, `guest_path_case`, `guest_directory`, and `guest_filesystem` each passed three consecutive runs (24 passes total). Conventions and `git diff --check` pass. The new head has fresh GitHub Actions checks running. Could you please re-review it once those checks finish? Windows tests from the source commit have not been rerun on this head, and console behavior remains unverified.
adriadam10 commented 2026-10-11 10:40:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T10:40:38Z.

Review: APPROVED


Checked on current main: patched_libraries, guest_kernel_sys and guest_working_directory pass, libkernel_sys.prx now loads, and guest_kernel_sys fails to link without the change. The C-ABI helpers fix the earlier undefined symbol; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5482987063 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3392#pullrequestreview-5482987063). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T10:40:38Z. **Review:** APPROVED --- Checked on current main: `patched_libraries`, `guest_kernel_sys` and `guest_working_directory` pass, `libkernel_sys.prx` now loads, and `guest_kernel_sys` fails to link without the change. The C-ABI helpers fix the earlier undefined symbol; merging.
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!3392
No description provided.