feat(libkernel): implement getdtablesize #6201

Merged
bruceblake merged 2 commits from upstream-pr/6201 into main 2026-10-18 19:26:33 +00:00
bruceblake commented 2026-10-15 02:38:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by bruceblake on 2026-10-15T02:38:17Z.


What

Implement getdtablesize in libkernel. It previously threw NotImplemented_nid_no_patch. It now returns the RLIMIT_NOFILE soft limit that getrlimit reports, capped at INT_MAX: FreeBSD's sys_getdtablesize returns the smaller of that limit and maxfilesperproc, and the guest limit never exceeds the hard limit that bounds it here (guest limits cannot be changed; setrlimit rejects changes). The guest test asserts the returned value instead of the throw, and the libkernel debt entry records the semantics and that the console's maxfilesperproc is not measured.

Tested

Linux x86-64 (Arch, WSL2), GCC 16. Configured with -DBUILD_TESTING=ON; built the libs target and guest_getrlimit_tests. ctest: guest_getrlimit, guest_setrlimit and patched_libraries all pass. tools/check_conventions.py --base origin/main passes. No title or homebrew run: the function is covered by the guest test; VLC (PPSA85300) imports it only on a path that never runs (its fork fails with ENOSYS).

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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4867389423 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6201). Originally posted by [bruceblake](https://github.com/bruceblake) on 2026-10-15T02:38:17Z. --- ### What Implement `getdtablesize` in libkernel. It previously threw `NotImplemented_nid_no_patch`. It now returns the `RLIMIT_NOFILE` soft limit that `getrlimit` reports, capped at `INT_MAX`: FreeBSD's `sys_getdtablesize` returns the smaller of that limit and `maxfilesperproc`, and the guest limit never exceeds the hard limit that bounds it here (guest limits cannot be changed; `setrlimit` rejects changes). The guest test asserts the returned value instead of the throw, and the libkernel debt entry records the semantics and that the console's `maxfilesperproc` is not measured. ### Tested Linux x86-64 (Arch, WSL2), GCC 16. Configured with `-DBUILD_TESTING=ON`; built the `libs` target and `guest_getrlimit_tests`. ctest: `guest_getrlimit`, `guest_setrlimit` and `patched_libraries` all pass. `tools/check_conventions.py --base origin/main` passes. No title or homebrew run: the function is covered by the guest test; VLC (PPSA85300) imports it only on a path that never runs (its `fork` fails with `ENOSYS`). ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
jotitadev commented 2026-10-15 03:28:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T03:28:47Z.

Review: COMMENTED


On macOS the RLIMIT_NOFILE soft limit is not a bound on the descriptor table, so this returns more than the process can open. Checked against b6c9bd7f3 on an Apple M2 Max, macOS 27.2, Apple clang 21, standalone snippets built for arm64 and x86_64 (same results on both):

  • kern.maxfilesperproc is 122880. setrlimit(RLIMIT_NOFILE) accepts a soft limit above it, RLIM_INFINITY included, and getrlimit reports what was set.
  • The host's getdtablesize() already returns the smaller of the two, as FreeBSD's sys_getdtablesize does.
  • dup fails with EMFILE at fd 122879 whatever the soft limit says.
Soft limit This PR's formula Host getdtablesize() Highest fd opened
1048576 (my shell) 1048576 122880 122879
RLIM_INFINITY 2147483647 122880 122879
256 (launchd default) 256 256 255

The formula is the PR's: toGuestLimit(rlim_cur) capped at INT_MAX. Guest descriptors are host descriptors here (pipe_nid_postfix calls ::pipe). With an unlimited soft limit, a closefrom-style loop up to getdtablesize() (the VLC case in the debt entry) would run 2^31 iterations instead of about 122k. On macOS, taking the smaller of the guest soft limit and the host's ::getdtablesize() would give the FreeBSD result.

The new assertion in GuestGetrlimit.cpp uses the same formula as the implementation, so it can't catch this. The debt entry says the limit is "raised to the hard limit when libkernel loads", but File/src/OpenFileLimit.cpp only does that under __linux__. On macOS the guest gets whatever soft limit the host process inherited.

Not a full build: main doesn't link libc.dylib on macOS yet.

Written with AI assistance (Claude Code). I ran the checks above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5527688364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6201#pullrequestreview-5527688364). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-15T03:28:47Z. **Review:** COMMENTED --- On macOS the `RLIMIT_NOFILE` soft limit is not a bound on the descriptor table, so this returns more than the process can open. Checked against b6c9bd7f3 on an Apple M2 Max, macOS 27.2, Apple clang 21, standalone snippets built for arm64 and x86_64 (same results on both): - `kern.maxfilesperproc` is 122880. `setrlimit(RLIMIT_NOFILE)` accepts a soft limit above it, `RLIM_INFINITY` included, and `getrlimit` reports what was set. - The host's `getdtablesize()` already returns the smaller of the two, as FreeBSD's `sys_getdtablesize` does. - `dup` fails with `EMFILE` at fd 122879 whatever the soft limit says. | Soft limit | This PR's formula | Host `getdtablesize()` | Highest fd opened | |---|---|---|---| | 1048576 (my shell) | 1048576 | 122880 | 122879 | | `RLIM_INFINITY` | 2147483647 | 122880 | 122879 | | 256 (launchd default) | 256 | 256 | 255 | The formula is the PR's: `toGuestLimit(rlim_cur)` capped at `INT_MAX`. Guest descriptors are host descriptors here (`pipe_nid_postfix` calls `::pipe`). With an unlimited soft limit, a `closefrom`-style loop up to `getdtablesize()` (the VLC case in the debt entry) would run 2^31 iterations instead of about 122k. On macOS, taking the smaller of the guest soft limit and the host's `::getdtablesize()` would give the FreeBSD result. The new assertion in `GuestGetrlimit.cpp` uses the same formula as the implementation, so it can't catch this. The debt entry says the limit is "raised to the hard limit when libkernel loads", but `File/src/OpenFileLimit.cpp` only does that under `__linux__`. On macOS the guest gets whatever soft limit the host process inherited. Not a full build: `main` doesn't link `libc.dylib` on macOS yet. Written with AI assistance (Claude Code). I ran the checks above on my machine.
github-actions[bot] commented 2026-10-15 12:16:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:16:34Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6446 docs/dev/debt/libkernel.md L102-103
PR 6447 core/libs/prx/libkernel/System/src/Process.cpp L667-668
PR 7039 core/libs/prx/libkernel/System/src/Process.cpp L667-668
core/libs/tests/GuestGetrlimit.cpp L177-183
docs/dev/debt/libkernel.md L103
PR 7543 core/libs/tests/GuestGetrlimit.cpp L169-183

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/System/src/Process.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestGetrlimit.cpp
PR 6510 docs/dev/debt/libkernel.md
PR 6519 docs/dev/debt/libkernel.md
PR 6534 docs/dev/debt/libkernel.md
PR 6879 core/libs/prx/libkernel/System/src/Process.cpp
PR 6908 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 6977 docs/dev/debt/libkernel.md
PR 7049 docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/System/src/Process.cpp
PR 7299 docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191830353 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6201#issuecomment-6191830353). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:16:34Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 6446](https://www.github.com/boykopovar/AnyPS5/pull/6446) | `docs/dev/debt/libkernel.md` [L102-103](https://github.com/boykopovar/AnyPS5/blob/c4c2b3aa7371ee988c94ca62f1431ebaaf0688c8/docs/dev/debt/libkernel.md#L102-L103) | | | | [PR 6447](https://www.github.com/boykopovar/AnyPS5/pull/6447) | `core/libs/prx/libkernel/System/src/Process.cpp` [L667-668](https://github.com/boykopovar/AnyPS5/blob/c4c2b3aa7371ee988c94ca62f1431ebaaf0688c8/core/libs/prx/libkernel/System/src/Process.cpp#L667-L668) | | | | [PR 7039](https://www.github.com/boykopovar/AnyPS5/pull/7039) | `core/libs/prx/libkernel/System/src/Process.cpp` [L667-668](https://github.com/boykopovar/AnyPS5/blob/c4c2b3aa7371ee988c94ca62f1431ebaaf0688c8/core/libs/prx/libkernel/System/src/Process.cpp#L667-L668)<br>`core/libs/tests/GuestGetrlimit.cpp` [L177-183](https://github.com/boykopovar/AnyPS5/blob/c4c2b3aa7371ee988c94ca62f1431ebaaf0688c8/core/libs/tests/GuestGetrlimit.cpp#L177-L183)<br>`docs/dev/debt/libkernel.md` [L103](https://github.com/boykopovar/AnyPS5/blob/c4c2b3aa7371ee988c94ca62f1431ebaaf0688c8/docs/dev/debt/libkernel.md#L103) | | | | [PR 7543](https://www.github.com/boykopovar/AnyPS5/pull/7543) | `core/libs/tests/GuestGetrlimit.cpp` [L169-183](https://github.com/boykopovar/AnyPS5/blob/c4c2b3aa7371ee988c94ca62f1431ebaaf0688c8/core/libs/tests/GuestGetrlimit.cpp#L169-L183) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/System/src/Process.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestGetrlimit.cpp` | | [PR 6510](https://www.github.com/boykopovar/AnyPS5/pull/6510) | `docs/dev/debt/libkernel.md` | | [PR 6519](https://www.github.com/boykopovar/AnyPS5/pull/6519) | `docs/dev/debt/libkernel.md` | | [PR 6534](https://www.github.com/boykopovar/AnyPS5/pull/6534) | `docs/dev/debt/libkernel.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/System/src/Process.cpp` | | [PR 6908](https://www.github.com/boykopovar/AnyPS5/pull/6908) | `docs/dev/debt/libkernel.md` | | [PR 6936](https://www.github.com/boykopovar/AnyPS5/pull/6936) | `docs/dev/debt/libkernel.md` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `docs/dev/debt/libkernel.md` | | [PR 7049](https://www.github.com/boykopovar/AnyPS5/pull/7049) | `docs/dev/debt/libkernel.md` | | [PR 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) | `docs/dev/debt/libkernel.md` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `docs/dev/debt/libkernel.md` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `docs/dev/debt/libkernel.md` | | [PR 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) | `docs/dev/debt/libkernel.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/System/src/Process.cpp` | | [PR 7299](https://www.github.com/boykopovar/AnyPS5/pull/7299) | `docs/dev/debt/libkernel.md` | | [PR 7447](https://www.github.com/boykopovar/AnyPS5/pull/7447) | `docs/dev/debt/libkernel.md` | | [PR 7509](https://www.github.com/boykopovar/AnyPS5/pull/7509) | `docs/dev/debt/libkernel.md` |
bruceblake commented 2026-10-15 14:44:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by bruceblake on 2026-10-15T14:44:25Z.


Thanks for checking this against real macOS behavior — the measurements make the problem clear, and you're right on all three points.

What changed in the new commit on this branch:

  • getdtablesize now returns the smaller of the guest RLIMIT_NOFILE soft limit and the host's ::getdtablesize() (still capped at INT_MAX), which is the FreeBSD sys_getdtablesize result — on macOS the value now tracks maxfilesperproc instead of the raw soft limit, so an unlimited soft limit no longer produces a ~2^31-iteration bound for a closefrom-style loop. The host call is compiled only on non-Windows hosts; on Windows the guest limit stays the constant CRT value.
  • The GuestGetrlimit assertion no longer reuses the implementation's formula: the test derives its expectation itself from the guest soft limit and its own host ::getdtablesize() call, so an implementation that ignored the host bound would fail it.
  • The debt entry is corrected: the limit is raised to the hard limit at load only on Linux (OpenFileLimit.cpp is __linux__-only), and on macOS the guest gets whatever soft limit the host process inherited.

Verified on Linux (WSL): full libs build passes, guest_getrlimit and guest_setrlimit tests pass, and the conventions check passes. I can't run the macOS case here — this machine is Linux, where the host getdtablesize() already tracks the soft limit — so the macOS behavior rests on your measurements; happy to adjust if you spot anything off in the new commit.

Written with AI assistance (Claude). I ran the checks above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6194835228 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6201#issuecomment-6194835228). Originally posted by [bruceblake](https://github.com/bruceblake) on 2026-10-15T14:44:25Z. --- Thanks for checking this against real macOS behavior — the measurements make the problem clear, and you're right on all three points. What changed in the new commit on this branch: - `getdtablesize` now returns the smaller of the guest `RLIMIT_NOFILE` soft limit and the host's `::getdtablesize()` (still capped at `INT_MAX`), which is the FreeBSD `sys_getdtablesize` result — on macOS the value now tracks `maxfilesperproc` instead of the raw soft limit, so an unlimited soft limit no longer produces a ~2^31-iteration bound for a `closefrom`-style loop. The host call is compiled only on non-Windows hosts; on Windows the guest limit stays the constant CRT value. - The `GuestGetrlimit` assertion no longer reuses the implementation's formula: the test derives its expectation itself from the guest soft limit and its own host `::getdtablesize()` call, so an implementation that ignored the host bound would fail it. - The debt entry is corrected: the limit is raised to the hard limit at load only on Linux (`OpenFileLimit.cpp` is `__linux__`-only), and on macOS the guest gets whatever soft limit the host process inherited. Verified on Linux (WSL): full `libs` build passes, `guest_getrlimit` and `guest_setrlimit` tests pass, and the conventions check passes. I can't run the macOS case here — this machine is Linux, where the host `getdtablesize()` already tracks the soft limit — so the macOS behavior rests on your measurements; happy to adjust if you spot anything off in the new commit. Written with AI assistance (Claude). I ran the checks above on my machine.
screamm commented 2026-10-15 22:27:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-15T22:27:28Z.


Windows 11 Pro 26220, RTX 4070 (596.21), MinGW GCC 15.2: head 873fbe99a merged on main 44df21632.

  • guest_getrlimit, --repeat until-fail:3: 3 of 3 pass. On Windows the host call is compiled out, so the test covers the guest-limit branch only; the macOS bound from the review is not checked here.
  • Registry check: getdtablesize is NID sZuwaDPATKs in aerolib. Of the relinked registries I have, PPSA85300 (VLC 0.3.1) and PPSA97358 import it from libkernel.prx; no other local title does. libkernel.prx already exported the NID on main (the export count is 1646 before and after), so the PR changes what the call does, not whether the import resolves.
  • PPSA85300 (relinked with --windows-fixed-base 0x400000, 120 s with the display): main and the PR both run to the limit with no stop line, 9541 and 9601 frames, and the same log. As the description says, the call is on a path VLC never runs, so this run is a no-regression check, not a demonstration of the function.
  • tools/check_conventions.py --base origin/main and git diff --check pass.

Not checked: macOS and the full suite on this head.

Written with Claude Code assistance (validation by a subagent, reviewed in this session).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6203567286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6201#issuecomment-6203567286). Originally posted by [screamm](https://github.com/screamm) on 2026-10-15T22:27:28Z. --- Windows 11 Pro 26220, RTX 4070 (596.21), MinGW GCC 15.2: head `873fbe99a` merged on main `44df21632`. - `guest_getrlimit`, `--repeat until-fail:3`: 3 of 3 pass. On Windows the host call is compiled out, so the test covers the guest-limit branch only; the macOS bound from the review is not checked here. - Registry check: `getdtablesize` is NID `sZuwaDPATKs` in aerolib. Of the relinked registries I have, PPSA85300 (VLC 0.3.1) and PPSA97358 import it from `libkernel.prx`; no other local title does. `libkernel.prx` already exported the NID on `main` (the export count is 1646 before and after), so the PR changes what the call does, not whether the import resolves. - PPSA85300 (relinked with `--windows-fixed-base 0x400000`, 120 s with the display): `main` and the PR both run to the limit with no stop line, 9541 and 9601 frames, and the same log. As the description says, the call is on a path VLC never runs, so this run is a no-regression check, not a demonstration of the function. - `tools/check_conventions.py --base origin/main` and `git diff --check` pass. Not checked: macOS and the full suite on this head. Written with Claude Code assistance (validation by a subagent, reviewed in this session).
github-actions[bot] commented 2026-10-18 17:15:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T17:15:23Z.


Progress report (ff23657 → 873fbe9)

📈 System libraries: 88.77% (+0.03%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel getdtablesize_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6252778072 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6201#issuecomment-6252778072). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T17:15:23Z. --- <!-- progress-report --> ### Progress report (ff23657 → 873fbe9) 📈 **System libraries**: 88.77% (+0.03%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `getdtablesize_nid_postfix` | </details>
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!6201
No description provided.