fix(libkernel): return EBADF from fstat on a descriptor that is not open on Windows #6897

Merged
tv7 merged 1 commit from upstream-pr/6897 into main 2026-10-19 13:14:34 +00:00
tv7 commented 2026-10-16 17:08:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tv7 on 2026-10-16T17:08:10Z.


What

On Windows, sceKernelFstat and fstat pass the descriptor to the UCRT's _fstat64. For a descriptor that is not open, the UCRT raises its invalid-parameter handler. The crash report installs that handler as fatal, so the calling thread dies with FATAL: UCRT invalid parameter, where the console returns EBADF.

DoFstat now looks the descriptor up with WindowsDescriptorHandle_nid_no_patch (which already silences the handler for that lookup) and fails with EBADF when it is not open, before calling _fstat64. sceKernelFstat then returns SCE_KERNEL_ERROR_EBADF (0x80020009) and fstat returns -1 with errno 9, like the other descriptor calls the test already checks (fchmod, futimes, fsync, fdatasync).

Dying Light 2 (PPSA02262) hits this on a loading thread right after its sign-in step:

[SAVEDATA:native] mount3 user=268435456 dir=DATA mode=0x1 blocks=0 -> 0x809f0008 point= status=0
FATAL: UCRT invalid parameter on thread 12600
  stack return address candidates:
    ... libkernel.prx+0x20ee0   (File::FillFileStatFromDescriptor)
    ... libkernel.prx+0x29845   (sceKernelFstat)
    ... libc.prx.guest.prx+0x1a706

Tested

Windows 11, RTX 3070, GCC 15.2.0 UCRT, Release, on main 66e290f1c.

  • guest_filesystem: new checks that sceKernelFstat on a closed file descriptor, a closed socket and 0x7fffffff returns 0x80020009 without raising the invalid-parameter handler (counted with a recording handler, as guest_dup does for close). On main the handler is raised (the check fails at that line); with this change it passes.
  • ctest: all pass except two that fail the same way on main without this change: agc_driver_gpu_indirect_arguments_misaligned_writer_ahead does not compile with MinGW (alignment 65536 exceeds object file maximum 8192), and guest_kqueue exits with 0xc0000409.
  • tools/check_conventions.py passes.
  • Dying Light 2 (PPSA02262), with a local change past its PSN sign-in (see #1794): without this change the loading thread dies as above; with it there is no fatal report. The title still waits after sign-in for a separate reason.

Checklist

  • Based on current main; no other open PR implements the same functions (#6228 edits the line above in DoFstat, a different change)
  • 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
  • Unblocks: PPSA02262 #1794 (one step)
  • GPUs tested: n/a
  • AI-assisted: yes, Claude Code

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4891722862 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6897). Originally posted by [tv7](https://github.com/tv7) on 2026-10-16T17:08:10Z. --- ### What On Windows, `sceKernelFstat` and `fstat` pass the descriptor to the UCRT's `_fstat64`. For a descriptor that is not open, the UCRT raises its invalid-parameter handler. The crash report installs that handler as fatal, so the calling thread dies with `FATAL: UCRT invalid parameter`, where the console returns `EBADF`. `DoFstat` now looks the descriptor up with `WindowsDescriptorHandle_nid_no_patch` (which already silences the handler for that lookup) and fails with `EBADF` when it is not open, before calling `_fstat64`. `sceKernelFstat` then returns `SCE_KERNEL_ERROR_EBADF` (`0x80020009`) and `fstat` returns -1 with errno 9, like the other descriptor calls the test already checks (`fchmod`, `futimes`, `fsync`, `fdatasync`). Dying Light 2 (PPSA02262) hits this on a loading thread right after its sign-in step: ``` [SAVEDATA:native] mount3 user=268435456 dir=DATA mode=0x1 blocks=0 -> 0x809f0008 point= status=0 FATAL: UCRT invalid parameter on thread 12600 stack return address candidates: ... libkernel.prx+0x20ee0 (File::FillFileStatFromDescriptor) ... libkernel.prx+0x29845 (sceKernelFstat) ... libc.prx.guest.prx+0x1a706 ``` ### Tested Windows 11, RTX 3070, GCC 15.2.0 UCRT, Release, on main `66e290f1c`. - `guest_filesystem`: new checks that `sceKernelFstat` on a closed file descriptor, a closed socket and `0x7fffffff` returns `0x80020009` without raising the invalid-parameter handler (counted with a recording handler, as `guest_dup` does for `close`). On main the handler is raised (the check fails at that line); with this change it passes. - ctest: all pass except two that fail the same way on main without this change: `agc_driver_gpu_indirect_arguments_misaligned_writer_ahead` does not compile with MinGW (`alignment 65536 exceeds object file maximum 8192`), and `guest_kqueue` exits with `0xc0000409`. - `tools/check_conventions.py` passes. - Dying Light 2 (PPSA02262), with a local change past its PSN sign-in (see #1794): without this change the loading thread dies as above; with it there is no fatal report. The title still waits after sign-in for a separate reason. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (#6228 edits the line above in `DoFstat`, a different change) - [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 - [x] Unblocks: PPSA02262 #1794 (one step) - [x] GPUs tested: n/a - [x] AI-assisted: yes, Claude Code 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-16 17:38:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T17:38:37Z.


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 7202 core/libs/prx/libkernel/File/src/NativeStat.cpp L11

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/File/src/NativeStat.cpp
PR 6044 core/libs/tests/GuestFilesystem.cpp
PR 6879 core/libs/prx/libkernel/File/src/NativeStat.cpp
PR 7201 core/libs/prx/libkernel/File/src/NativeStat.cpp
PR 7332 core/libs/prx/libkernel/File/src/NativeStat.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6221209963 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6897#issuecomment-6221209963). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T17:38:37Z. --- <!-- 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 7202](https://www.github.com/boykopovar/AnyPS5/pull/7202) | `core/libs/prx/libkernel/File/src/NativeStat.cpp` [L11](https://github.com/boykopovar/AnyPS5/blob/f3bb6a4f8d33a4738a8a166c4a567e819361a1a7/core/libs/prx/libkernel/File/src/NativeStat.cpp#L11) | | | 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/File/src/NativeStat.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestFilesystem.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/File/src/NativeStat.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/File/src/NativeStat.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libkernel/File/src/NativeStat.cpp` |
karimibrahim07 commented 2026-10-17 03:52:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-17T03:52:28Z.

Review: COMMENTED


Windows check of head 6a402a145 merged onto main e3a8e4430 (clean merge). Windows 11 Pro (26300), Ryzen 5 3600, WinLibs GCC 15.2.0 r7 (UCRT), Ninja Release.

  • guest_filesystem, guest_dup, guest_fstatfs, guest_file_timestamps, guest_file_vector and the six guest_file_lock_* tests pass, --repeat until-fail:3.
  • Negative control: with main's NativeStat.cpp and the new test kept, guest_filesystem fails at line 628, Require(!g_invalidParameter). The three sceKernelFstat return-value checks just above it pass on main too: with the test's recording handler the UCRT returns and _fstat64 fails with EBADF anyway. So the handler check is what makes this test fail on main. That matches the description: only the fatal handler that the crash report installs turns this into the Dying Light 2 stop.

Code: WindowsDescriptorHandle_nid_no_patch is already how DescriptorControl.cpp checks descriptors (6 uses), so fstat now validates the same way as fcntl. The check comes after the directory-descriptor lookup, so directory descriptors are unaffected.

Overlap: the description says #6228 "edits the line above" in DoFstat. It deletes that line (the DirectoryDescriptorPath early return), so git reports a conflict between the two PRs in NativeStat.cpp. The resolution is to keep this PR's check and drop the directory line. One thing to check when that happens: after #6228, directory descriptors reach this check, so WindowsDescriptorHandle_nid_no_patch must return a valid handle for them, or fstat on a directory would become EBADF. I couldn't test the two together, because #6228 currently also conflicts with main in Stdio.cpp.

AI-assisted: yes. I ran the build, the tests and the negative control above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5555933632 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6897#pullrequestreview-5555933632). Originally posted by [karimibrahim07](https://github.com/karimibrahim07) on 2026-10-17T03:52:28Z. **Review:** COMMENTED --- Windows check of head `6a402a145` merged onto `main` `e3a8e4430` (clean merge). Windows 11 Pro (26300), Ryzen 5 3600, WinLibs GCC 15.2.0 r7 (UCRT), Ninja Release. - `guest_filesystem`, `guest_dup`, `guest_fstatfs`, `guest_file_timestamps`, `guest_file_vector` and the six `guest_file_lock_*` tests pass, `--repeat until-fail:3`. - Negative control: with `main`'s `NativeStat.cpp` and the new test kept, `guest_filesystem` fails at line 628, `Require(!g_invalidParameter)`. The three `sceKernelFstat` return-value checks just above it pass on `main` too: with the test's recording handler the UCRT returns and `_fstat64` fails with `EBADF` anyway. So the handler check is what makes this test fail on `main`. That matches the description: only the fatal handler that the crash report installs turns this into the Dying Light 2 stop. **Code:** `WindowsDescriptorHandle_nid_no_patch` is already how `DescriptorControl.cpp` checks descriptors (6 uses), so `fstat` now validates the same way as `fcntl`. The check comes after the directory-descriptor lookup, so directory descriptors are unaffected. **Overlap:** the description says #6228 "edits the line above" in `DoFstat`. It deletes that line (the `DirectoryDescriptorPath` early return), so git reports a conflict between the two PRs in `NativeStat.cpp`. The resolution is to keep this PR's check and drop the directory line. One thing to check when that happens: after #6228, directory descriptors reach this check, so `WindowsDescriptorHandle_nid_no_patch` must return a valid handle for them, or `fstat` on a directory would become `EBADF`. I couldn't test the two together, because #6228 currently also conflicts with `main` in `Stdio.cpp`. AI-assisted: yes. I ran the build, the tests and the negative control above.
adriadam10 commented 2026-10-19 13:15:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T13:15:55Z.


Rebased onto current main (the only conflict was in DoFstat, where #6228 removed the directory-descriptor line; kept main's version plus the EBADF guard). Checked that the new test pins the invalid-parameter handler count and that the libs/libkernel ctest set passes on Linux; Windows is covered by @karimibrahim07's run and the queue's Windows job; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6268120014 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6897#issuecomment-6268120014). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T13:15:55Z. --- Rebased onto current `main` (the only conflict was in `DoFstat`, where #6228 removed the directory-descriptor line; kept main's version plus the `EBADF` guard). Checked that the new test pins the invalid-parameter handler count and that the libs/libkernel ctest set passes on Linux; Windows is covered by @karimibrahim07's run and the queue's Windows job; 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!6897
No description provided.