Skip to content

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

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
Gruzzberry:fix/poll-efault
Nov 1, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
Gruzzberry:fix/poll-efault

Conversation

@Gruzzberry

Copy link
Copy Markdown
Contributor

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

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

github-actions Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

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 11247 docs/dev/debt/libkernel.md L270-272

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 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7135 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 7464 docs/dev/debt/libkernel.md
PR 7507 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7509 docs/dev/debt/libkernel.md
PR 7804 docs/dev/debt/libkernel.md
PR 8605 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9199 docs/dev/debt/libkernel.md
PR 9242 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9326 docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9713 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 9843 docs/dev/debt/libkernel.md
PR 9938 docs/dev/debt/libkernel.md
PR 10025 docs/dev/debt/libkernel.md
PR 10319 docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 10558 docs/dev/debt/libkernel.md
PR 10645 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 11002 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 11939 docs/dev/debt/libkernel.md
PR 12682 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 12688 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 12700 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 12925 docs/dev/debt/libkernel.md
PR 12965 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 12970 docs/dev/debt/libkernel.md
PR 13582 docs/dev/debt/libkernel.md
PR 13587 docs/dev/debt/libkernel.md
PR 14085 docs/dev/debt/libkernel.md
PR 14128 docs/dev/debt/libkernel.md
PR 14212 docs/dev/debt/libkernel.md

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@Gruzzberry Reviewed at 757c084368. The new guard fixes the common oversized-count case, but accepted counts still construct std::vector<GuestPollDescriptor> local(count) without handling allocation failure. At the allowed limit, or with an effectively unlimited hard limit, std::bad_alloc still escapes the guest syscall. Please return a guest error for that failure, following the nearby vector-I/O allocation handling, and cover it with a bounded failure-injection check rather than allocating a huge array.

Please also update the existing poll entry in docs/dev/debt/libkernel.md: it still says the descriptor-count limit is “unknown and not checked”. Record that this PR substitutes guest RLIMIT_NOFILE.rlim_max for FreeBSD’s separate maxfilesperproc, including the host-dependent initial value and the fact that lowering the guest hard limit changes the bound. The PR description explains these limitations, but the in-tree debt currently contradicts the new behavior.

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Gruzzberry and others added 2 commits October 31, 2026 11:16
…r limit

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… be allocated

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@Gruzzberry

Copy link
Copy Markdown
Contributor Author

@oneandonlydean thanks, both points are fixed in ba9200abc0 (rebased on current main).

  • poll now fills the descriptor copy inside a try and returns ENOMEM on std::bad_alloc, the same way the other vector buffers in Udp.cpp do. GuestPoll.cpp has a bounded injection check: a replaced operator new fails exactly one allocation of the 24 bytes a 3-entry array needs, poll returns -1 with ENOMEM, the caller's array is untouched, and the next call succeeds. No large array is allocated. The check is under #ifndef _WIN32 because on Windows libkernel takes operator new from libstdc++-6.dll, so the executable cannot replace it. It runs on Linux and macOS, which I could not run from here, so please read that block on the Linux job.
  • The poll entry in docs/dev/debt/libkernel.md no longer says the limit is unchecked. It now says the guest RLIMIT_NOFILE.rlim_max stands in for FreeBSD 11.4's separate maxfilesperproc, the initial value is host dependent, lowering the guest hard limit changes the bound, a count of 1024 or less is never refused, and a hard limit of infinity (macOS) refuses nothing. It also notes that ENOMEM is the emulator's own outcome, since FreeBSD's malloc waits.
  • Cold review also found that the two 0xffffffff checks in the test would try a 32 GiB allocation where the hard limit is infinite. They are now gated on rlim_max < 0xffffffff.

Tests: guest_poll 3/3 on Windows, full suite matches main apart from the known environment failures (vulkan_validation_gate_reports, response_text_mode) and python-based tools tests that time out only in my environment.

@github-actions github-actions Bot added the docs label Oct 31, 2026
@adriadam10 adriadam10 self-assigned this Oct 31, 2026
@adriadam10
adriadam10 merged commit cae3eda into boykopovar:main Nov 1, 2026
11 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the kern_poll order (bad timeout, nfds limit, then EFAULT) against FreeBSD 11.4, the ENOMEM path on bad_alloc with its single-allocation failure-injection test, and the updated poll debt entry; both of @oneandonlydean's points are met, and @jotitadev's Linux run covers the new guest_poll cases. Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants