fix(libkernel): fail poll with EINVAL when nfds exceeds the descriptor limit #13738

Merged
Gruzzberry merged 2 commits from upstream-pr/13738 into main 2026-11-01 00:40:46 +00:00
Gruzzberry commented 2026-10-31 03:29:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-31T03:29:14Z.


What

Guest poll with a huge nfds (for example 0xffffffff) allocated a host vector of that many descriptors and crashed the emulator. FreeBSD 11.4 kern_poll checks, after the timeout, nfds > maxfilesperproc && nfds > FD_SETSIZE and returns EINVAL before it reads the array. poll now does the same, keeping the order: bad timeout EINVAL, nfds limit EINVAL, then EFAULT for a null or unreadable array or an unwritable result (both already on main since #12462).

kern.maxfilesperproc is not modelled (the sysctl is still unanswered), so the guest RLIMIT_NOFILE hard limit stands in for it; it is read only when nfds is above 1024, so the common path is unchanged. 8192 on Windows, the host hard limit elsewhere.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0, Ninja, Release, -DBUILD_TESTING=ON, current main.

  • guest_poll: 0xffffffff descriptors with a valid, a null array gives EINVAL; at the limit with a null array gives EFAULT, limit + 1 gives EINVAL even with a 0x10 array. The check on the limit boundary runs when the hard limit is between 1024 and 100000 (it is on Windows). On main the test crashes (0xc0000409) three runs out of three; with this change it passes three runs out of three.
  • Full suite: the only failures are vulkan_validation_gate_reports and check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted, Python tooling tests that fail here because the Microsoft Store python.exe cannot be read (OSError: Invalid argument); they fail alone three runs out of three and have nothing to do with poll. pr_overlap_encoding, agc_host_import_cut_lifetime and agc_driver_depth_plane_partial_store pass alone.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run.
  • Not fixed: a guest can lower its own RLIMIT_NOFILE hard limit and so lower the poll limit, which maxfilesperproc would not follow; on a Linux host with an extreme nr_open a count just below the limit can still fail to allocate.

Overlaps

tools/pr_overlap.py could not compare Udp.cpp with #5021, #7088, #8605, #9713, #10645, #12682, #12688, #12700 and #12965 because they conflict with main themselves; I read their diffs and none touches poll_nid_postfix. #11223 and #11247 change what poll reports for sockets, not argument checks.

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5094102017 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13738). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-31T03:29:14Z. --- ### What Guest `poll` with a huge `nfds` (for example `0xffffffff`) allocated a host vector of that many descriptors and crashed the emulator. FreeBSD 11.4 [`kern_poll`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c) checks, after the timeout, `nfds > maxfilesperproc && nfds > FD_SETSIZE` and returns `EINVAL` before it reads the array. `poll` now does the same, keeping the order: bad timeout `EINVAL`, nfds limit `EINVAL`, then `EFAULT` for a null or unreadable array or an unwritable result (both already on `main` since #12462). `kern.maxfilesperproc` is not modelled (the sysctl is still unanswered), so the guest `RLIMIT_NOFILE` hard limit stands in for it; it is read only when `nfds` is above 1024, so the common path is unchanged. 8192 on Windows, the host hard limit elsewhere. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0, Ninja, Release, `-DBUILD_TESTING=ON`, current `main`. - `guest_poll`: `0xffffffff` descriptors with a valid, a null array gives `EINVAL`; at the limit with a null array gives `EFAULT`, limit + 1 gives `EINVAL` even with a `0x10` array. The check on the limit boundary runs when the hard limit is between 1024 and 100000 (it is on Windows). On `main` the test crashes (`0xc0000409`) three runs out of three; with this change it passes three runs out of three. - Full suite: the only failures are `vulkan_validation_gate_reports` and `check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted`, Python tooling tests that fail here because the Microsoft Store `python.exe` cannot be read (`OSError: Invalid argument`); they fail alone three runs out of three and have nothing to do with `poll`. `pr_overlap_encoding`, `agc_host_import_cut_lifetime` and `agc_driver_depth_plane_partial_store` pass alone. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run. - Not fixed: a guest can lower its own `RLIMIT_NOFILE` hard limit and so lower the `poll` limit, which `maxfilesperproc` would not follow; on a Linux host with an extreme `nr_open` a count just below the limit can still fail to allocate. ### Overlaps `tools/pr_overlap.py` could not compare `Udp.cpp` with #5021, #7088, #8605, #9713, #10645, #12682, #12688, #12700 and #12965 because they conflict with `main` themselves; I read their diffs and none touches `poll_nid_postfix`. #11223 and #11247 change what `poll` reports for sockets, not argument checks. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [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`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source: none added - [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 AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-31 03:41:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T03:41:33Z.

Review: COMMENTED


Tests run and reviewed — Linux path remains unverified

I found no confirmed code defect in head 757c08436833a189e22c7510f536c19591b91ac9, but the Linux product job was skipped, so I am withholding approval pending Linux-specific validation.

What I ran

Reviewed head 757c08436833a189e22c7510f536c19591b91ac9 against PR base 6475267b456de6a0a23b230aa751f0b917d12688; no local build or C++ test was run. Environment: Linux x86_64, GCC 12.2.0, Python 3.11.2.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13738
python3 tools/check_conventions.py --base 6475267b456de6a0a23b230aa751f0b917d12688 --head 757c08436833a189e22c7510f536c19591b91ac9 --title "fix(libkernel): fail poll with EINVAL when nfds exceeds the descriptor limit" --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13738-757c08436833/pr-body.md

Results

Check Result
Exact-head diff and source review Passed; limit check precedes guest-array access and vector allocation.
git diff --check Passed in precheck.
Conventions and PR template Passed.
Dependency/mergeability preview Passed; clean against current main de824c2919ca65086c6324a1e0388866a7327eea.
Hosted Windows product job Passed on this head.
Hosted Linux product job Skipped.
Precheck ABI/test-quality helpers Not executed: isolated checkout did not contain the helper scripts; these are harness errors, not PR failures.
Raw output for locally run convention check and precheck helper errors
All conventions checks passed.
/usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13738-757c08436833/head-757c08436833-96200527/tools/pr_abi_audit.py': [Errno 2] No such file or directory
/usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13738-757c08436833/head-757c08436833-96200527/tools/pr_test_quality.py': [Errno 2] No such file or directory

Findings

Blocking: None confirmed.

Non-blocking: The changed path relies on the guest RLIMIT_NOFILE hard limit on non-Windows hosts; the PR notes this is a substitute for unmodelled kern.maxfilesperproc. Windows CI passed, but the Linux product job was skipped, so the primary non-Windows path has not been independently exercised here. The PR's Windows test results are author-reported, not locally reproduced.

Sources

Not verified

  • Linux or macOS build/runtime behavior; the Linux hosted product job was skipped.
  • C++ GuestPoll test locally; it requires an exact-head build, which was not run.
  • Console/title behavior.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5720979430 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13738#pullrequestreview-5720979430). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T03:41:33Z. **Review:** COMMENTED --- # Tests run and reviewed — Linux path remains unverified I found no confirmed code defect in head `757c08436833a189e22c7510f536c19591b91ac9`, but the Linux product job was skipped, so I am withholding approval pending Linux-specific validation. ## What I ran Reviewed head `757c08436833a189e22c7510f536c19591b91ac9` against PR base `6475267b456de6a0a23b230aa751f0b917d12688`; no local build or C++ test was run. Environment: Linux x86_64, GCC 12.2.0, Python 3.11.2. ```sh python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13738 python3 tools/check_conventions.py --base 6475267b456de6a0a23b230aa751f0b917d12688 --head 757c08436833a189e22c7510f536c19591b91ac9 --title "fix(libkernel): fail poll with EINVAL when nfds exceeds the descriptor limit" --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13738-757c08436833/pr-body.md ``` ## Results | Check | Result | |---|---| | Exact-head diff and source review | Passed; limit check precedes guest-array access and vector allocation. | | `git diff --check` | Passed in precheck. | | Conventions and PR template | Passed. | | Dependency/mergeability preview | Passed; clean against current `main` `de824c2919ca65086c6324a1e0388866a7327eea`. | | Hosted Windows product job | Passed on this head. | | Hosted Linux product job | Skipped. | | Precheck ABI/test-quality helpers | Not executed: isolated checkout did not contain the helper scripts; these are harness errors, not PR failures. | <details> <summary>Raw output for locally run convention check and precheck helper errors</summary> ```text All conventions checks passed. ``` ```text /usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13738-757c08436833/head-757c08436833-96200527/tools/pr_abi_audit.py': [Errno 2] No such file or directory /usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13738-757c08436833/head-757c08436833-96200527/tools/pr_test_quality.py': [Errno 2] No such file or directory ``` </details> ## Findings **Blocking:** None confirmed. **Non-blocking:** The changed path relies on the guest `RLIMIT_NOFILE` hard limit on non-Windows hosts; the PR notes this is a substitute for unmodelled `kern.maxfilesperproc`. Windows CI passed, but the Linux product job was skipped, so the primary non-Windows path has not been independently exercised here. The PR's Windows test results are author-reported, not locally reproduced. ## Sources - [Changed `poll_nid_postfix` and boundary tests at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/757c08436833a189e22c7510f536c19591b91ac9/core/libs/prx/libkernel/Socket/src/Udp.cpp) - [Guest `RLIMIT_NOFILE` implementation at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/757c08436833a189e22c7510f536c19591b91ac9/core/libs/prx/libkernel/System/src/Process.cpp) ## Not verified - Linux or macOS build/runtime behavior; the Linux hosted product job was skipped. - C++ `GuestPoll` test locally; it requires an exact-head build, which was not run. - Console/title behavior.
jotitadev commented 2026-10-31 04:11:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-31T04:11:13Z.

Review: COMMENTED


Linux run of this head (757c084368), merged onto main 6475267b45 (local only). Ubuntu 26.04 LTS (kernel 7.0), Intel Core i7-8700, GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON.

  • guest_poll passes 3 of 3; public_headers passes; ctest -R "poll|socket|epoll|fcntl" 28/28.
  • Negative control: same tree with Udp.cpp back to main (test kept): guest_poll aborts 3 of 3 (~19 s, the 0xffffffff allocation).

The at-limit block does not run on Linux with the default limits. The guest RLIMIT_NOFILE hard limit is the host one, 524288 here (systemd's default nofile hard limit on current distros), so fileLimit.rlim_max < 100000 is false and the three boundary checks are skipped. I confirmed it with a temporary fprintf inside the block:

Host hard limit rlim_max seen by the test Block runs guest_poll
default (524288) 524288 no passes
ulimit -n 4096 4096 yes passes 3 of 3

So the fix itself is fine on Linux, but only the 0xffffffff checks cover it in CI-like setups. On macOS the hard limit is usually unlimited, so the block is skipped there too.

Two ways to make it always run:

  • The upper bound looks unnecessary: with the fix, nullptr at the limit returns EFAULT and limit + 1 returns EINVAL before any allocation, so the block only needs rlim_max to fit in uint32_t (e.g. rlim_max >= 1024 && rlim_max < 0xffffffff). That covers Linux's 524288.
  • For an unlimited hard limit (macOS), the test could lower the host limit first on non-Windows hosts, e.g. rlimit r{}; getrlimit(RLIMIT_NOFILE, &r); r.rlim_max = 4096; r.rlim_cur = std::min<rlim_t>(r.rlim_cur, 4096); setrlimit(RLIMIT_NOFILE, &r); before reading fileLimit (lowering the hard limit needs no privileges).

Also checked together with #13695 (both touch Udp.cpp) on main 2364892547: they merge cleanly, the full build passes, guest_poll and guest_kernel_fcntl pass 3 of 3, the socket/poll/fcntl subset 52/52 and public_headers pass.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5721150555 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13738#pullrequestreview-5721150555). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-31T04:11:13Z. **Review:** COMMENTED --- Linux run of this head (`757c084368`), merged onto `main` `6475267b45` (local only). Ubuntu 26.04 LTS (kernel 7.0), Intel Core i7-8700, GCC 15.2.0, Ninja, Release, `BUILD_TESTING=ON`. - `guest_poll` passes 3 of 3; `public_headers` passes; `ctest -R "poll|socket|epoll|fcntl"` 28/28. - Negative control: same tree with `Udp.cpp` back to `main` (test kept): `guest_poll` aborts 3 of 3 (~19 s, the `0xffffffff` allocation). **The at-limit block does not run on Linux with the default limits.** The guest `RLIMIT_NOFILE` hard limit is the host one, 524288 here (systemd's default `nofile` hard limit on current distros), so `fileLimit.rlim_max < 100000` is false and the three boundary checks are skipped. I confirmed it with a temporary `fprintf` inside the block: | Host hard limit | `rlim_max` seen by the test | Block runs | `guest_poll` | |---|---|---|---| | default (524288) | 524288 | no | passes | | `ulimit -n 4096` | 4096 | yes | passes 3 of 3 | So the fix itself is fine on Linux, but only the `0xffffffff` checks cover it in CI-like setups. On macOS the hard limit is usually `unlimited`, so the block is skipped there too. Two ways to make it always run: - The upper bound looks unnecessary: with the fix, `nullptr` at the limit returns `EFAULT` and limit + 1 returns `EINVAL` before any allocation, so the block only needs `rlim_max` to fit in `uint32_t` (e.g. `rlim_max >= 1024 && rlim_max < 0xffffffff`). That covers Linux's 524288. - For an unlimited hard limit (macOS), the test could lower the host limit first on non-Windows hosts, e.g. `rlimit r{}; getrlimit(RLIMIT_NOFILE, &r); r.rlim_max = 4096; r.rlim_cur = std::min<rlim_t>(r.rlim_cur, 4096); setrlimit(RLIMIT_NOFILE, &r);` before reading `fileLimit` (lowering the hard limit needs no privileges). Also checked together with #13695 (both touch `Udp.cpp`) on `main` `2364892547`: they merge cleanly, the full build passes, `guest_poll` and `guest_kernel_fcntl` pass 3 of 3, the socket/poll/fcntl subset 52/52 and `public_headers` pass. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!13738
No description provided.