fix(libkernel): reject invalid ftruncate descriptors safely #5980

Merged
Ondra97 merged 1 commit from upstream-pr/5980 into main 2026-10-17 04:53:48 +00:00
Ondra97 commented 2026-10-14 18:02:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-14T18:02:57Z.


What

On Windows, ftruncate and sceKernelFtruncate with an invalid or closed descriptor reach _chsize_s's invalid-parameter handler. With the default handler this aborts the process instead of returning EBADF.

Reject an invalid native handle before resizing and use the existing thread-local CRT suppression around _chsize_s, preserving the caller's handler. Microsoft documents the invalid-descriptor callback that requires this handling. The regression checks both guest APIs with negative, closed and out-of-range descriptors at zero and nonzero lengths, checks handler restoration, and verifies that the real file is intact. The existing shrinking, extension and permission cases still run.

Tested

I tested on Windows 11 Pro, MinGW-w64 GCC 15.2.0 (WinLibs), Release/Ninja, with BUILD_TESTING=ON, outside the sandbox for the native filesystem cases.

  • On main d9bfa0f3a, the new regression reports 12 CRT parameter-handler invocations and fails.
  • With the fix, guest_truncate, guest_filesystem, guest_aio_bad_descriptors and guest_dup pass (4/4, 0.26 s).
  • After rebasing onto main 8ba869d30, full default Windows build, conventions and git diff --check pass.
  • I checked the complete open PR inventory and the 24 shared-file diffs. The 19 AOT diffs are identical in this file and change macOS paths; the remaining changes cover positioned transfers, append writes, archive-backed descriptors and standard-stream closure. #5835's archive check in the public ftruncate wrapper does not implement this Windows native-handle fix. The focused repository overlap check is clean.

Linux/macOS and a retail title were not run; production changes are inside the existing Windows branch.

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 in technical debt
  • 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: none
  • GPUs tested: n/a
  • AI-assisted: yes — Codex
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4862245197 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5980). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-14T18:02:57Z. --- ### What On Windows, `ftruncate` and `sceKernelFtruncate` with an invalid or closed descriptor reach `_chsize_s`'s invalid-parameter handler. With the default handler this aborts the process instead of returning `EBADF`. Reject an invalid native handle before resizing and use the existing thread-local CRT suppression around `_chsize_s`, preserving the caller's handler. [Microsoft documents the invalid-descriptor callback](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/chsize-s) that requires this handling. The regression checks both guest APIs with negative, closed and out-of-range descriptors at zero and nonzero lengths, checks handler restoration, and verifies that the real file is intact. The existing shrinking, extension and permission cases still run. ### Tested I tested on Windows 11 Pro, MinGW-w64 GCC 15.2.0 (WinLibs), Release/Ninja, with `BUILD_TESTING=ON`, outside the sandbox for the native filesystem cases. - On `main` `d9bfa0f3a`, the new regression reports 12 CRT parameter-handler invocations and fails. - With the fix, `guest_truncate`, `guest_filesystem`, `guest_aio_bad_descriptors` and `guest_dup` pass (4/4, 0.26 s). - After rebasing onto main 8ba869d30, full default Windows build, conventions and `git diff --check` pass. - I checked the complete open PR inventory and the 24 shared-file diffs. The 19 AOT diffs are identical in this file and change macOS paths; the remaining changes cover positioned transfers, append writes, archive-backed descriptors and standard-stream closure. #5835's archive check in the public `ftruncate` wrapper does not implement this Windows native-handle fix. The focused repository overlap check is clean. Linux/macOS and a retail title were not run; production changes are inside the existing Windows branch. ### 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 in technical debt - [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: none - [x] GPUs tested: n/a - [x] AI-assisted: yes — Codex
github-actions[bot] commented 2026-10-15 09:26:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T09:26:48Z.


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 6287 core/libs/prx/libkernel/File/src/Stdio.cpp L118-119

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/Stdio.cpp
PR 6044 core/libs/tests/GuestTruncate.cpp
PR 6222 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 6879 core/libs/prx/libkernel/File/src/Stdio.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6188879289 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5980#issuecomment-6188879289). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T09:26:48Z. --- <!-- 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 6287](https://www.github.com/boykopovar/AnyPS5/pull/6287) | `core/libs/prx/libkernel/File/src/Stdio.cpp` [L118-119](https://github.com/boykopovar/AnyPS5/blob/1b1f8483caa7637e637fe841f1c4722977325362/core/libs/prx/libkernel/File/src/Stdio.cpp#L118-L119) | | | 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/Stdio.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestTruncate.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/File/src/Stdio.cpp` |
adriadam10 commented 2026-10-16 19:58:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T19:58:41Z.


Checked the diff against the existing WithoutParameterHandler usage in Stdio.cpp and the new GuestTruncate regression (negative, closed and out-of-range descriptors, both guest APIs, handler restored). Build and ctest are green on Linux; the Windows path is covered by the Windows build run before merge. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6223596822 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5980#issuecomment-6223596822). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T19:58:41Z. --- Checked the diff against the existing `WithoutParameterHandler` usage in `Stdio.cpp` and the new `GuestTruncate` regression (negative, closed and out-of-range descriptors, both guest APIs, handler restored). Build and ctest are green on Linux; the Windows path is covered by the Windows build run before merge. Merging.
adriadam10 commented 2026-10-16 22:23:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T22:23:06Z.


Re-checked against current main: the diff still applies cleanly and NativeFtruncate is unchanged by the recent Stdio.cpp merges. Build and ctest are green on Linux (guest_truncate included); the Windows path is covered by the Windows build run before merge. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6225828981 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5980#issuecomment-6225828981). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T22:23:06Z. --- Re-checked against current main: the diff still applies cleanly and `NativeFtruncate` is unchanged by the recent `Stdio.cpp` merges. Build and ctest are green on Linux (`guest_truncate` included); the Windows path is covered by the Windows build run before merge. Merging.
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!5980
No description provided.