fix(libkernel): refuse O_CREAT on an existing directory with EISDIR on macOS #10853

Merged
krishalivaibhav merged 1 commit from upstream-pr/10853 into main 2026-10-27 05:18:29 +00:00
krishalivaibhav commented 2026-10-25 05:12:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-10-25T05:12:47Z.


What

On macOS, open() of an existing directory with O_CREAT (without O_EXCL) succeeds, while FreeBSD and Linux fail with EISDIR. libkernel's OpenNative handled this only on Windows, so on macOS sceKernelOpen/open returned a descriptor where the guest expects EISDIR (guest_filesystem aborted at GuestFilesystem.cpp:1290).

Under __APPLE__, after the native open: if O_CREAT was set without O_EXCL and fstat reports a directory, the descriptor is closed and EISDIR is returned. Linux and Windows are unchanged.

The change matches the hunk in iamthehimansh's feat/macos-port (#8658), who is credited as co-author.

Tested

macOS: Apple Silicon M2, macOS 27.2, AppleClang, x86_64 Release, on a local integration of main + #10419 + #10366 + #10369 + #7631 + #7804 + #4630 + #10552 (libkernel does not build on macOS without these) + this commit:

  • guest_filesystem passes (it aborted before at line 1290);
  • guest_fdopendir, guest_kernel_sys, guest_statvfs and guest_file_timestamps pass.

Darwin's behaviour was also checked with a small C program: open(dir, O_CREAT) succeeds.

Linux/Windows: not built locally; the change is __APPLE__-only.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none to review; building libkernel on macOS needs #10552 and the PRs above
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5002033675 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10853). Originally posted by [krishalivaibhav](https://github.com/krishalivaibhav) on 2026-10-25T05:12:47Z. --- ### What On macOS, `open()` of an existing directory with `O_CREAT` (without `O_EXCL`) succeeds, while FreeBSD and Linux fail with `EISDIR`. libkernel's `OpenNative` handled this only on Windows, so on macOS `sceKernelOpen`/`open` returned a descriptor where the guest expects `EISDIR` (`guest_filesystem` aborted at `GuestFilesystem.cpp:1290`). Under `__APPLE__`, after the native open: if `O_CREAT` was set without `O_EXCL` and `fstat` reports a directory, the descriptor is closed and `EISDIR` is returned. Linux and Windows are unchanged. The change matches the hunk in iamthehimansh's `feat/macos-port` (#8658), who is credited as co-author. ### Tested macOS: Apple Silicon M2, macOS 27.2, AppleClang, x86_64 Release, on a local integration of `main` + #10419 + #10366 + #10369 + #7631 + #7804 + #4630 + #10552 (libkernel does not build on macOS without these) + this commit: - `guest_filesystem` passes (it aborted before at line 1290); - `guest_fdopendir`, `guest_kernel_sys`, `guest_statvfs` and `guest_file_timestamps` pass. Darwin's behaviour was also checked with a small C program: `open(dir, O_CREAT)` succeeds. Linux/Windows: not built locally; the change is `__APPLE__`-only. ### 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 - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none to review; building libkernel on macOS needs #10552 and the PRs above - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 05:14:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T05:14:46Z.

Review: APPROVED


Reviewed exact head ed61cbd11bcc0adc9446884ea25e9a5bf8b6eeeb. The Apple-only branch checks the opened descriptor with fstat, so it tests the object actually opened rather than re-resolving a path; it only changes the O_CREAT without O_EXCL case, closes the descriptor and restores errno = EISDIR. GuestFilesystem.cpp already covers creat on the existing root directory and expects EISDIR. The matching prior hunk is credited to iamthehimansh as co-author in the PR metadata; #8658 is closed, and the current intake/conventions checks pass. I could not independently run the macOS test; the M2 integration results in the description are author-reported. Product test jobs are skipped by repository policy. I found no blocker in this focused change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641686621 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10853#pullrequestreview-5641686621). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T05:14:46Z. **Review:** APPROVED --- Reviewed exact head `ed61cbd11bcc0adc9446884ea25e9a5bf8b6eeeb`. The Apple-only branch checks the opened descriptor with `fstat`, so it tests the object actually opened rather than re-resolving a path; it only changes the `O_CREAT` without `O_EXCL` case, closes the descriptor and restores `errno = EISDIR`. `GuestFilesystem.cpp` already covers `creat` on the existing root directory and expects EISDIR. The matching prior hunk is credited to iamthehimansh as co-author in the PR metadata; #8658 is closed, and the current intake/conventions checks pass. I could not independently run the macOS test; the M2 integration results in the description are author-reported. Product test jobs are skipped by repository policy. I found no blocker in this focused change.
imellag commented 2026-10-25 07:16:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-25T07:16:08Z.

Review: APPROVED


Tested head ed61cbd11b on Apple M4 Pro, macOS 27.0.1, Apple clang 21, x86-64 under Rosetta 2. The tree is main 61fc572cee plus the open macOS build PRs (libkernel doesn't build on macOS without them); the patch applies cleanly there. I swapped only Open.cpp and ran guest_filesystem, guest_fdopendir, guest_kernel_sys, guest_path_case, guest_statvfs and guest_file_timestamps twice each:

  • With main's Open.cpp, guest_filesystem aborts, at the open(dir, O_CREAT) check at GuestFilesystem.cpp:1218 on the current main. The other five pass.
  • With this PR, all six pass.
  • They also all pass with the version of this check in mugurc's series (f4704da5b8, carried by #7201), which uses std::filesystem::is_directory on the path. This PR's fstat on the opened descriptor checks the same object that was opened, so I'd keep this one. If this is merged first, that hunk can be dropped from the series.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642041519 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10853#pullrequestreview-5642041519). Originally posted by [imellag](https://github.com/imellag) on 2026-10-25T07:16:08Z. **Review:** APPROVED --- Tested head `ed61cbd11b` on Apple M4 Pro, macOS 27.0.1, Apple clang 21, x86-64 under Rosetta 2. The tree is `main` `61fc572cee` plus the open macOS build PRs (libkernel doesn't build on macOS without them); the patch applies cleanly there. I swapped only `Open.cpp` and ran `guest_filesystem`, `guest_fdopendir`, `guest_kernel_sys`, `guest_path_case`, `guest_statvfs` and `guest_file_timestamps` twice each: - With `main`'s `Open.cpp`, `guest_filesystem` aborts, at the `open(dir, O_CREAT)` check at `GuestFilesystem.cpp:1218` on the current `main`. The other five pass. - With this PR, all six pass. - They also all pass with the version of this check in mugurc's series (`f4704da5b8`, carried by #7201), which uses `std::filesystem::is_directory` on the path. This PR's `fstat` on the opened descriptor checks the same object that was opened, so I'd keep this one. If this is merged first, that hunk can be dropped from the series. AI-assisted: yes
maximilianfv commented 2026-10-25 10:19:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-25T10:19:35Z.

Review: APPROVED


The fix is correct and passes a swap A/B on a second macOS tree. The notes below are non-blocking.

Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)

Tested at ed61cbd11b on mugurc's macOS series (27e968974) merged with main at 2742004ed2, with other open macOS fixes. The patch does not apply there because the series already has its own std::filesystem::is_directory check at the same place in OpenNative. I did a swap A/B instead:

  • Without: I removed only the series' #ifdef __APPLE__ block after NativeOpen.
  • With: I applied this PR on top of that.

Both sides were rebuilt, and each test ran 3 times per side with the same results every time.

guest_filesystem guest_fdopendir, guest_kernel_sys, guest_path_case, guest_statvfs, guest_file_timestamps
Without this PR aborts at GuestFilesystem.cpp:1218 (1290 at this PR's head), the open(dir, O_CREAT) check that expects EISDIR (lldb) Passed
With this PR gets past 1218; aborts later at GuestFilesystem.cpp:1245 (1317 at this PR's head; see 1) Passed
  1. The stop at 1245 is a separate gap, not caused by this PR. The check there calls sceKernelGetdents on a regular file and expects EINVAL. This tree's macOS sceKernelGetdirentries comes from the series; main does not have it. The tree with the series' original check stops at the same line, which fits with imellag's main-based tree passing the whole test. I did not capture the error the call returned. No open PR other than #7201 touches it.

  2. The body says FreeBSD fails O_CREAT on a directory with EISDIR. FreeBSD 11.4 vn_open_cred (sys/kern/vfs_vnops.c:204-270) confirms that, including through a symlink and a trailing slash. But it takes the create path only when (fmode & (O_CREAT | O_DIRECTORY)) == O_CREAT. So O_CREAT | O_DIRECTORY on an existing directory is a plain lookup there and returns a descriptor. Darwin fails the same open with EINVAL; I checked this with a small C probe on macOS 27. The new block runs only when fd >= 0, so it does not make this worse, but it does not fix it either. A follow-up or a line in docs/dev/debt/libkernel.md would cover it.

  3. Open.cpp:238: NativeClose(fd) instead of ::close(fd) would match the rest of the file. The two are identical on POSIX today. This is optional.

  4. Body: the Depends on line reads "none to review; building libkernel on macOS needs #10552 and the PRs above". CONTRIBUTING asks for Depends on: #N when another PR is needed first. imellag already points out that #7201 carries the same check in the series. The checklist line "no other open PR implements the same functions" should name it. The overlap rule in CONTRIBUTING asks for the order to be agreed and stated in Depends on.

AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642714510 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10853#pullrequestreview-5642714510). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-25T10:19:35Z. **Review:** APPROVED --- The fix is correct and passes a swap A/B on a second macOS tree. The notes below are non-blocking. ### Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2) Tested at `ed61cbd11b` on mugurc's macOS series (`27e968974`) merged with `main` at `2742004ed2`, with other open macOS fixes. The patch does not apply there because the series already has its own `std::filesystem::is_directory` check at the same place in `OpenNative`. I did a swap A/B instead: - Without: I removed only the series' `#ifdef __APPLE__` block after `NativeOpen`. - With: I applied this PR on top of that. Both sides were rebuilt, and each test ran 3 times per side with the same results every time. | | `guest_filesystem` | `guest_fdopendir`, `guest_kernel_sys`, `guest_path_case`, `guest_statvfs`, `guest_file_timestamps` | |---|---|---| | Without this PR | aborts at `GuestFilesystem.cpp:1218` (1290 at this PR's head), the `open(dir, O_CREAT)` check that expects `EISDIR` (lldb) | Passed | | With this PR | gets past 1218; aborts later at `GuestFilesystem.cpp:1245` (1317 at this PR's head; see 1) | Passed | 1. The stop at 1245 is a separate gap, not caused by this PR. The check there calls `sceKernelGetdents` on a regular file and expects `EINVAL`. This tree's macOS `sceKernelGetdirentries` comes from the series; `main` does not have it. The tree with the series' original check stops at the same line, which fits with imellag's `main`-based tree passing the whole test. I did not capture the error the call returned. No open PR other than #7201 touches it. 2. The body says FreeBSD fails `O_CREAT` on a directory with `EISDIR`. FreeBSD 11.4 `vn_open_cred` (`sys/kern/vfs_vnops.c:204-270`) confirms that, including through a symlink and a trailing slash. But it takes the create path only when `(fmode & (O_CREAT | O_DIRECTORY)) == O_CREAT`. So `O_CREAT | O_DIRECTORY` on an existing directory is a plain lookup there and returns a descriptor. Darwin fails the same open with `EINVAL`; I checked this with a small C probe on macOS 27. The new block runs only when `fd >= 0`, so it does not make this worse, but it does not fix it either. A follow-up or a line in `docs/dev/debt/libkernel.md` would cover it. 3. `Open.cpp:238`: `NativeClose(fd)` instead of `::close(fd)` would match the rest of the file. The two are identical on POSIX today. This is optional. 4. Body: the `Depends on` line reads "none to review; building libkernel on macOS needs #10552 and the PRs above". CONTRIBUTING asks for `Depends on: #N` when another PR is needed first. imellag already points out that #7201 carries the same check in the series. The checklist line "no other open PR implements the same functions" should name it. The overlap rule in CONTRIBUTING asks for the order to be agreed and stated in `Depends on`. AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).
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!10853
No description provided.