feat(libkernel_sys): implement fchmodat #4553

Merged
Nixitov merged 2 commits from upstream-pr/4553 into main 2026-10-13 15:03:44 +00:00
Nixitov commented 2026-10-12 15:32:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nixitov on 2026-10-12T15:32:21Z.


What

Implement fchmodat in libkernel_sys next to openat. 2 Ship 2 Harkinian (PPSA99621) imports nsBDTu22CBU (fchmodat, through std::filesystem::permissions) and stops at start-up on main.

AT_FDCWD (-100) and absolute paths go through the path resolver; any other relative path is joined to the directory descriptor's host path, the same way as for openat and fchdir. A flag other than 0 or AT_SYMLINK_NOFOLLOW (0x200) returns EINVAL; a symbolic link under AT_SYMLINK_NOFOLLOW throws, while a regular file is changed either way. The mode is masked to 07777.

A failing host chmod is translated to the FreeBSD number (ENAMETOOLONG 63, ELOOP 62, ENOENT 2, EACCES 13, ENOTDIR 20, EINVAL 22, EPERM 1, EROFS 30, otherwise EIO 5), so the guest does not see Linux-only numbers. The host path join and the Windows read-only mapping are recorded in docs/dev/debt/libkernel_sys.md.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release.

  • guest_kernel_sys passes: the checks cover the AT_FDCWD and directory-descriptor cases, the resulting mode on the host, the invalid-flag EINVAL, ENOENT, EFAULT and EBADF, a null path, and (Linux only) ELOOP -> 62 and ENAMETOOLONG -> 63 through a self-referential symlink and a 300-byte component.
  • 2 Ship 2 Harkinian (PPSA99621, mshivam019/tmm64-ps5 release v1.1.0, asset tmm-ps5-2160p120-v1.1.0-camera-controls-windows.zip), eboot.bin unwrapped from its SELF and relinked with the built relinker --windows: on main it stops at FAIL: unresolved ELF import nsBDTu22CBU from libkernel_sys.prx; with this change it passes fchmodat and stops at tCFbw1wGrqk (unlinkat).
  • python tools/check_conventions.py --base origin/main passes.
  • (Thanks to @nicolasfalesy for the Linux run and the errno note this revision addresses.)

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4831578526 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4553). Originally posted by [Nixitov](https://github.com/Nixitov) on 2026-10-12T15:32:21Z. --- ### What Implement `fchmodat` in libkernel_sys next to `openat`. 2 Ship 2 Harkinian (PPSA99621) imports `nsBDTu22CBU` (`fchmodat`, through `std::filesystem::permissions`) and stops at start-up on `main`. `AT_FDCWD` (-100) and absolute paths go through the path resolver; any other relative path is joined to the directory descriptor's host path, the same way as for `openat` and `fchdir`. A flag other than `0` or `AT_SYMLINK_NOFOLLOW` (0x200) returns `EINVAL`; a symbolic link under `AT_SYMLINK_NOFOLLOW` throws, while a regular file is changed either way. The mode is masked to `07777`. A failing host `chmod` is translated to the FreeBSD number (`ENAMETOOLONG` 63, `ELOOP` 62, `ENOENT` 2, `EACCES` 13, `ENOTDIR` 20, `EINVAL` 22, `EPERM` 1, `EROFS` 30, otherwise `EIO` 5), so the guest does not see Linux-only numbers. The host path join and the Windows read-only mapping are recorded in `docs/dev/debt/libkernel_sys.md`. ### Tested Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. - `guest_kernel_sys` passes: the checks cover the `AT_FDCWD` and directory-descriptor cases, the resulting mode on the host, the invalid-flag `EINVAL`, `ENOENT`, `EFAULT` and `EBADF`, a null path, and (Linux only) `ELOOP` -> 62 and `ENAMETOOLONG` -> 63 through a self-referential symlink and a 300-byte component. - 2 Ship 2 Harkinian (PPSA99621, `mshivam019/tmm64-ps5` release `v1.1.0`, asset `tmm-ps5-2160p120-v1.1.0-camera-controls-windows.zip`), `eboot.bin` unwrapped from its SELF and relinked with the built relinker `--windows`: on `main` it stops at `FAIL: unresolved ELF import nsBDTu22CBU from libkernel_sys.prx`; with this change it passes `fchmodat` and stops at `tCFbw1wGrqk` (`unlinkat`). - `python tools/check_conventions.py --base origin/main` passes. - (Thanks to @nicolasfalesy for the Linux run and the errno note this revision addresses.) ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
github-actions[bot] commented 2026-10-12 15:35:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T15:35:14Z.


Progress report (759c665 → 603d94a)

📉 System libraries: 88.01% (-0.03%, +1 declared functions)

🆕 1 declared as stubs
Library Name
libkernel_sys fchmodat_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6127465754 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4553#issuecomment-6127465754). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T15:35:14Z. --- <!-- progress-report --> ### Progress report (759c665 → 603d94a) 📉 **System libraries**: 88.01% (-0.03%, +1 declared functions) <details> <summary>🆕 1 declared as stubs</summary> | Library | Name | | - | - | | libkernel_sys | `fchmodat_nid_postfix` | </details>
Nixitov commented 2026-10-12 16:52:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nixitov on 2026-10-12T16:52:02Z.


Thanks bot. This is a same-file overlap only, not a duplicate function:

  • #4503 (pathconf) and #4592 both edit core/libs/prx/libkernel_sys/Export.cpp at the include block and the GuestAtFdcwd constant, which fchmodat also adds next to.
  • No shared exports — fchmodat is distinct from pathconf.

Happy to rebase after either lands; nothing here depends on them.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6128703534 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4553#issuecomment-6128703534). Originally posted by [Nixitov](https://github.com/Nixitov) on 2026-10-12T16:52:02Z. --- Thanks bot. This is a same-file overlap only, not a duplicate function: - **#4503** (`pathconf`) and **#4592** both edit `core/libs/prx/libkernel_sys/Export.cpp` at the include block and the `GuestAtFdcwd` constant, which `fchmodat` also adds next to. - No shared exports — `fchmodat` is distinct from `pathconf`. Happy to rebase after either lands; nothing here depends on them. AI-assisted: yes
nicolasfalesy commented 2026-10-13 06:09:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-13T06:09:08Z.


Linux test (Arch, GCC 16.2.1, Release), this PR merged onto main 7e181fcc3: guest_kernel_sys passes.

One Linux-only gap: the failure path returns the host errno as is (Fail(errno) after ::chmod), and Linux numbers above 34 are not FreeBSD's. With a local check added to GuestKernelSys.cpp (not pushed):

fchmodat(base, "loop/x", 0600, 0)   // "loop" is a symlink to itself
  -> -1, guest errno 40              // Linux ELOOP; on FreeBSD 40 is EMSGSIZE, ELOOP is 62
fchmodat(base, <300 x 'a'>, 0600, 0)
  -> -1, guest errno 36              // Linux ENAMETOOLONG; on FreeBSD 36 is EINPROGRESS, ENAMETOOLONG is 63

The errno values a chmod can return that are the same number on both (ENOENT, EACCES, ENOTDIR, EPERM, EROFS, EIO) are fine. Mapping the host error the way DirectoryError in core/libs/prx/libc/src/Directory.cpp does (ENAMETOOLONG to 63, ELOOP to 62) would cover the two above. Windows' _wchmod only sets ENOENT/EACCES/EINVAL, so it is not affected.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6139426056 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4553#issuecomment-6139426056). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-13T06:09:08Z. --- Linux test (Arch, GCC 16.2.1, Release), this PR merged onto `main` 7e181fcc3: `guest_kernel_sys` passes. One Linux-only gap: the failure path returns the host `errno` as is (`Fail(errno)` after `::chmod`), and Linux numbers above 34 are not FreeBSD's. With a local check added to `GuestKernelSys.cpp` (not pushed): ``` fchmodat(base, "loop/x", 0600, 0) // "loop" is a symlink to itself -> -1, guest errno 40 // Linux ELOOP; on FreeBSD 40 is EMSGSIZE, ELOOP is 62 fchmodat(base, <300 x 'a'>, 0600, 0) -> -1, guest errno 36 // Linux ENAMETOOLONG; on FreeBSD 36 is EINPROGRESS, ENAMETOOLONG is 63 ``` The `errno` values a `chmod` can return that are the same number on both (`ENOENT`, `EACCES`, `ENOTDIR`, `EPERM`, `EROFS`, `EIO`) are fine. Mapping the host error the way `DirectoryError` in `core/libs/prx/libc/src/Directory.cpp` does (`ENAMETOOLONG` to 63, `ELOOP` to 62) would cover the two above. Windows' `_wchmod` only sets `ENOENT`/`EACCES`/`EINVAL`, so it is not affected.
Nixitov commented 2026-10-13 14:00:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nixitov on 2026-10-13T14:00:13Z.


Good catch — the failure path passed the host errno straight through, so a Linux ELOOP (40) or ENAMETOOLONG (36) would have reached the guest as a number FreeBSD does not use.

fchmodat now translates the host error before returning: ENOENT 2, EACCES 13, ENOTDIR 20, EINVAL 22, EPERM 1, EROFS 30, ENAMETOOLONG 63, ELOOP 62, otherwise EIO 5 — the same shape as Directory.cpp's mapping for the path-length and symlink-loop cases. Windows _wchmod only sets ENOENT/EACCES/EINVAL, so it is unaffected.

Added a Linux-only test (a self-referential symlink and a 300-byte path component) asserting ELOOP → 62 and ENAMETOOLONG → 63. Thanks for the Linux run.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6147280405 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4553#issuecomment-6147280405). Originally posted by [Nixitov](https://github.com/Nixitov) on 2026-10-13T14:00:13Z. --- Good catch — the failure path passed the host errno straight through, so a Linux `ELOOP` (40) or `ENAMETOOLONG` (36) would have reached the guest as a number FreeBSD does not use. `fchmodat` now translates the host error before returning: `ENOENT` 2, `EACCES` 13, `ENOTDIR` 20, `EINVAL` 22, `EPERM` 1, `EROFS` 30, `ENAMETOOLONG` 63, `ELOOP` 62, otherwise `EIO` 5 — the same shape as `Directory.cpp`'s mapping for the path-length and symlink-loop cases. Windows `_wchmod` only sets `ENOENT`/`EACCES`/`EINVAL`, so it is unaffected. Added a Linux-only test (a self-referential symlink and a 300-byte path component) asserting `ELOOP` → 62 and `ENAMETOOLONG` → 63. Thanks for the Linux run.
Nixitov commented 2026-10-13 14:09:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nixitov on 2026-10-13T14:09:30Z.


Rebased onto current main (renameat/unlinkat/mkdirat now sit next to fchmodat; the conflicts were placement only) and re-verified: guest_kernel_sys passes and check_conventions.py is clean on the rebased head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6147464989 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4553#issuecomment-6147464989). Originally posted by [Nixitov](https://github.com/Nixitov) on 2026-10-13T14:09:30Z. --- Rebased onto current `main` (`renameat`/`unlinkat`/`mkdirat` now sit next to `fchmodat`; the conflicts were placement only) and re-verified: `guest_kernel_sys` passes and `check_conventions.py` is clean on the rebased head.
adriadam10 commented 2026-10-13 14:21:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:21:00Z.


Checked fchmodat against the diff: path resolution, flag handling, host→FreeBSD errno mapping (ELOOP 62, ENAMETOOLONG 63) and the debt entry; the new checks in guest_kernel_sys pass on Linux.

Added one commit on top: after the latest main the test declared longName twice (GuestKernelSys.cpp), so the file no longer compiled; the fchmodat check now reuses the existing 256-byte name (which also gives ENAMETOOLONG 63).

Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6147707625 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4553#issuecomment-6147707625). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T14:21:00Z. --- Checked `fchmodat` against the diff: path resolution, flag handling, host→FreeBSD errno mapping (ELOOP 62, ENAMETOOLONG 63) and the debt entry; the new checks in `guest_kernel_sys` pass on Linux. Added one commit on top: after the latest `main` the test declared `longName` twice (`GuestKernelSys.cpp`), so the file no longer compiled; the fchmodat check now reuses the existing 256-byte name (which also gives ENAMETOOLONG 63). Merging once the full Build is green.
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!4553
No description provided.