fix(libkernel): set the open file limit at load on macOS #6836

Merged
DavidSamir merged 3 commits from upstream-pr/6836 into main 2026-10-19 14:38:19 +00:00
DavidSamir commented 2026-10-16 13:48:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T13:48:30Z.


What

getrlimit(RLIMIT_NOFILE) reports the host's limits, and the libkernel.md entry says that on Linux libkernel raises the soft limit to the hard one at load. That happens in OpenFileLimit.cpp, which is built only for Linux. On macOS the two limits stay different: 1048576 and unlimited in a shell here, 256 and unlimited by default.

XPSemu (PPSA97358) raises the soft limit to the hard one in QEMU's os_setup_limits when they differ. On macOS setrlimit refuses that, and the title stops:

libc++abi: terminating due to uncaught exception of type std::runtime_error: setrlimit: changing the limit of resource 8 is not supported

The soft limit can't simply be raised to the hard one on macOS. macOS fails with EMFILE at kern.maxfilesperproc, 10240 here, whatever the soft limit. A standalone program opened /dev/null up to fd 10239 both with the soft limit unlimited and with both limits at 10240. At load, libkernel now sets both limits to the smaller of the hard limit and kern.maxfilesperproc. So getrlimit reports, as the debt entry describes, the descriptor count after which opening fails. The Linux path still raises the soft limit to the hard one, and Windows is unchanged.

Commits:

  1. The limit at load on macOS.
  2. guest_getrlimit on macOS: the limit against kern.maxfilesperproc, and guest pipes failing with EMFILE at the reported limit (the check Windows already runs, moved into a helper).
  3. The getrlimit debt entry.

Tested

macOS 26.7.1, Apple M2, AppleClang 21.0.0, x86-64 under Rosetta, Release. main doesn't build the system libraries on macOS by itself, so the tests and the title ran in a local tree: main ce991f1ec with the macOS runtime work merged (#4627, #4630, #6124, the macOS relinker PRs, mugurc's port branches). The branch's OpenFileLimit.cpp and GuestGetrlimit.cpp are identical to the ones tested.

  • guest_getrlimit aborts with main's OpenFileLimit.cpp, because soft and hard differ. With this branch it passes 5 runs of 5, and also with the shell limits set to 256/unlimited, 256/4096 and 4096/4096.
  • Wrong version: setting both limits to the hard limit without the kern.maxfilesperproc clamp fails the extended test.
  • All 372 guest_ tests here, with main's file and with this branch: guest_getrlimit is the only difference. The other 22 failures are the same with both.
  • XPSemu (PPSA97358, Alpha-2.1), catalog sha256 checked, relinked with --macos --to-rosetta --macos-fixed-base 0x400000 (#6802), 2 runs of 2:
    • With main's file it stops on the setrlimit refusal above.
    • With this branch it gets past it and stops at sceKernelJitCreateSharedMemory, which this local tree doesn't build on macOS. main's POSIX backing uses memfd_create, which macOS doesn't have. In standalone probes under Rosetta, code in a shared memory mapping didn't run. An shm_open object mapped twice (one view RW, one RX) raised SIGBUS, and so did one mapped once RWX. A temporary file couldn't be mapped executable (EPERM). Only anonymous private memory ran written code.
  • tools/check_conventions.py --base origin/main and git diff --check pass.

Not tested: Linux and Windows. Linux takes the same branch as before: it returns when soft equals hard, and otherwise raises soft to hard. Windows compiles none of the changed lines, apart from the pipe loop moving into a helper.

Overlap:

  • #5902 makes setrlimit(RLIMIT_NOFILE) accept a request whose hard limit doesn't exceed the current one. On macOS here the hard limit is unlimited, so guest_setrlimit's Throws(GUEST_RLIMIT_NOFILE, infinity) aborts with #5902's Process.cpp. With this branch the hard limit is 10240, and guest_setrlimit and guest_getrlimit both pass with #5902's Process.cpp.
  • #5902 and #6201 also edit GuestGetrlimit.cpp and the getrlimit/setrlimit debt entries next to the lines changed here.

Whichever lands second rebases.

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 (the XPSemu run also needs #6802 to load the title on macOS)
  • Unblocks: PPSA97358 #6289 (macOS, with #6802; its next stop, the JIT shared memory, is named above)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4889315512 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6836). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-16T13:48:30Z. --- ### What `getrlimit(RLIMIT_NOFILE)` reports the host's limits, and the `libkernel.md` entry says that on Linux libkernel raises the soft limit to the hard one at load. That happens in `OpenFileLimit.cpp`, which is built only for Linux. On macOS the two limits stay different: 1048576 and unlimited in a shell here, 256 and unlimited by default. XPSemu (PPSA97358) raises the soft limit to the hard one in QEMU's `os_setup_limits` when they differ. On macOS `setrlimit` refuses that, and the title stops: ``` libc++abi: terminating due to uncaught exception of type std::runtime_error: setrlimit: changing the limit of resource 8 is not supported ``` The soft limit can't simply be raised to the hard one on macOS. macOS fails with `EMFILE` at `kern.maxfilesperproc`, 10240 here, whatever the soft limit. A standalone program opened `/dev/null` up to fd 10239 both with the soft limit unlimited and with both limits at 10240. At load, libkernel now sets both limits to the smaller of the hard limit and `kern.maxfilesperproc`. So `getrlimit` reports, as the debt entry describes, the descriptor count after which opening fails. The Linux path still raises the soft limit to the hard one, and Windows is unchanged. Commits: 1. The limit at load on macOS. 2. `guest_getrlimit` on macOS: the limit against `kern.maxfilesperproc`, and guest pipes failing with `EMFILE` at the reported limit (the check Windows already runs, moved into a helper). 3. The `getrlimit` debt entry. ### Tested macOS 26.7.1, Apple M2, AppleClang 21.0.0, x86-64 under Rosetta, Release. `main` doesn't build the system libraries on macOS by itself, so the tests and the title ran in a local tree: `main` ce991f1ec with the macOS runtime work merged (#4627, #4630, #6124, the macOS relinker PRs, mugurc's port branches). The branch's `OpenFileLimit.cpp` and `GuestGetrlimit.cpp` are identical to the ones tested. - **`guest_getrlimit`** aborts with `main`'s `OpenFileLimit.cpp`, because soft and hard differ. With this branch it passes 5 runs of 5, and also with the shell limits set to 256/unlimited, 256/4096 and 4096/4096. - **Wrong version:** setting both limits to the hard limit without the `kern.maxfilesperproc` clamp fails the extended test. - **All 372 `guest_` tests here,** with `main`'s file and with this branch: `guest_getrlimit` is the only difference. The other 22 failures are the same with both. - **XPSemu (PPSA97358, Alpha-2.1),** catalog sha256 checked, relinked with `--macos --to-rosetta --macos-fixed-base 0x400000` (#6802), 2 runs of 2: - With `main`'s file it stops on the `setrlimit` refusal above. - With this branch it gets past it and stops at `sceKernelJitCreateSharedMemory`, which this local tree doesn't build on macOS. `main`'s POSIX backing uses `memfd_create`, which macOS doesn't have. In standalone probes under Rosetta, code in a shared memory mapping didn't run. An `shm_open` object mapped twice (one view RW, one RX) raised SIGBUS, and so did one mapped once RWX. A temporary file couldn't be mapped executable (`EPERM`). Only anonymous private memory ran written code. - `tools/check_conventions.py --base origin/main` and `git diff --check` pass. Not tested: Linux and Windows. Linux takes the same branch as before: it returns when soft equals hard, and otherwise raises soft to hard. Windows compiles none of the changed lines, apart from the pipe loop moving into a helper. Overlap: - #5902 makes `setrlimit(RLIMIT_NOFILE)` accept a request whose hard limit doesn't exceed the current one. On macOS here the hard limit is unlimited, so `guest_setrlimit`'s `Throws(GUEST_RLIMIT_NOFILE, infinity)` aborts with #5902's `Process.cpp`. With this branch the hard limit is 10240, and `guest_setrlimit` and `guest_getrlimit` both pass with #5902's `Process.cpp`. - #5902 and #6201 also edit `GuestGetrlimit.cpp` and the `getrlimit`/`setrlimit` debt entries next to the lines changed here. Whichever lands second rebases. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/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 (the XPSemu run also needs #6802 to load the title on macOS) - [x] Unblocks: PPSA97358 #6289 (macOS, with #6802; its next stop, the JIT shared memory, is named above) - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-16 14:39:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T14:39:45Z.


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 7543 docs/dev/debt/libkernel.md L169-170

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 docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestGetrlimit.cpp
PR 6149 docs/dev/debt/libkernel.md
PR 6446 docs/dev/debt/libkernel.md
PR 6469 docs/dev/debt/libkernel.md
PR 6519 docs/dev/debt/libkernel.md
PR 6740 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 7105 docs/dev/debt/libkernel.md
PR 7135 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/File/src/OpenFileLimit.cpp
PR 7509 docs/dev/debt/libkernel.md
PR 7554 docs/dev/debt/libkernel.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6217900949 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6836#issuecomment-6217900949). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T14:39:45Z. --- <!-- 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 7543](https://www.github.com/boykopovar/AnyPS5/pull/7543) | `docs/dev/debt/libkernel.md` [L169-170](https://github.com/boykopovar/AnyPS5/blob/3647fcc68a7c4fccac634a18222daa345decf3dd/docs/dev/debt/libkernel.md#L169-L170) | | | 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) | `docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestGetrlimit.cpp` | | [PR 6149](https://www.github.com/boykopovar/AnyPS5/pull/6149) | `docs/dev/debt/libkernel.md` | | [PR 6446](https://www.github.com/boykopovar/AnyPS5/pull/6446) | `docs/dev/debt/libkernel.md` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `docs/dev/debt/libkernel.md` | | [PR 6519](https://www.github.com/boykopovar/AnyPS5/pull/6519) | `docs/dev/debt/libkernel.md` | | [PR 6740](https://www.github.com/boykopovar/AnyPS5/pull/6740) | `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 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `docs/dev/debt/libkernel.md` | | [PR 7135](https://www.github.com/boykopovar/AnyPS5/pull/7135) | `docs/dev/debt/libkernel.md` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `docs/dev/debt/libkernel.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/File/src/OpenFileLimit.cpp` | | [PR 7509](https://www.github.com/boykopovar/AnyPS5/pull/7509) | `docs/dev/debt/libkernel.md` | | [PR 7554](https://www.github.com/boykopovar/AnyPS5/pull/7554) | `docs/dev/debt/libkernel.md` |
blawesom commented 2026-10-17 09:46:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-17T09:46:20Z.


Rebase handoff — rebased onto current main (authorship unchanged). One conflict in docs/dev/debt/libkernel.md: your macOS RLIMIT_NOFILE entry is unioned with main's newer entries.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV):

  • builds; tools/check_conventions.py --base origin/main passes
  • full suite: 1335/1336 — the only failure is the known main regression (agc_driver_tessellation segfault, #4727), not this PR

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/6836
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 375614b61.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6232447125 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6836#issuecomment-6232447125). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-17T09:46:20Z. --- **Rebase handoff** — rebased onto current `main` (authorship unchanged). One conflict in `docs/dev/debt/libkernel.md`: your macOS `RLIMIT_NOFILE` entry is unioned with `main`'s newer entries. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): - builds; `tools/check_conventions.py --base origin/main` passes - **full suite: 1335/1336** — the only failure is the known `main` regression (`agc_driver_tessellation` segfault, #4727), not this PR **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/6836 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `375614b61`.
DavidSamir commented 2026-10-17 15:34:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-17T15:34:31Z.


Thanks, @blawesom. I checked your rebase/6836 against this branch: the code commits are identical, and the debt-file commit differs only by the conflict resolution. Rather than force-pushing, I merged current main into the branch with the same resolution, so the history stays as reviewed. The PR's diff against main now matches your rebase. Head is a3fb02df9, and it merges cleanly with main.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6235843956 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6836#issuecomment-6235843956). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-17T15:34:31Z. --- Thanks, @blawesom. I checked your `rebase/6836` against this branch: the code commits are identical, and the debt-file commit differs only by the conflict resolution. Rather than force-pushing, I merged current `main` into the branch with the same resolution, so the history stays as reviewed. The PR's diff against `main` now matches your rebase. Head is `a3fb02df9`, and it merges cleanly with `main`. AI-assisted: yes
adriadam10 commented 2026-10-19 14:39:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T14:39:30Z.


Rebased onto current main (conflicts in GuestGetrlimit.cpp and the getrlimit debt entry after the Windows stdio-limit change): the Windows stream check is now a _WIN32-only helper called after the shared descriptor check, and the debt entry lists Windows, Linux and macOS. Re-checked the macOS clamp, the unchanged Linux path and the extended test; libkernel tests pass; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6269875837 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6836#issuecomment-6269875837). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T14:39:30Z. --- Rebased onto current `main` (conflicts in `GuestGetrlimit.cpp` and the `getrlimit` debt entry after the Windows stdio-limit change): the Windows stream check is now a `_WIN32`-only helper called after the shared descriptor check, and the debt entry lists Windows, Linux and macOS. Re-checked the macOS clamp, the unchanged Linux path and the extended test; libkernel tests pass; merged.
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!6836
No description provided.