fix(libkernel): return guest errors from file resizing #2041

Closed
STUPIDCONTEM wants to merge 1 commit from upstream-pr/2041 into main
STUPIDCONTEM commented 2026-10-09 05:40:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by STUPIDCONTEM on 2026-10-09T05:40:32Z.


Evidence review (2026-10-09)

Closed by maintainer; not ready to reopen. Required evidence: Title ID and a log showing the title stops at the file-resize throw. Existing throws are intentional pending that need.

Game/title ID: none verified. No PS5 title execution is claimed.

Deferred after maintainer review: reopening requires a title ID and a log showing execution stops at the existing throw. Synthetic error-path tests do not establish that requirement. See maintainer feedback. The patch and test results below are preserved as historical evidence; this change is excluded from the current proposed integration.

What

sceKernelFtruncate and POSIX ftruncate threw exceptions for ordinary invalid arguments and native failures. Return encoded kernel errors from sceKernelFtruncate, preserving errno, and translate those results into -1 plus guest errno in the POSIX wrapper.

On Windows, check the original descriptor's object type and granted write access before _chsize_s. This rejects read-only descriptors even when the requested size is unchanged, distinguishes invalid descriptors from non-regular files, and leaves genuine native resize failures intact. Open guest sockets return EINVAL; closed/invalid socket descriptors return EBADF. Successful resizing retains the existing host implementation.

Error ordering and write-access requirements follow FreeBSD 11 kern_ftruncate and truncate(2). Windows uses documented NtQueryObject access information and _chsize_s. Linux already rejects read-only descriptors with EINVAL and retains its native ftruncate call.

Tested

Windows 10 Pro 19045, WinLibs GCC 15.2.0 UCRT/posix/SEH r7, Ninja Release, BUILD_TESTING=ON, SPIRV-Tools disabled. Based directly on f145139676a48306681d3a4d5fabf83938759daf; independent of #2029 and #2030. An independent review checked the implementation and expanded the success cases.

  • New guest_file_resize fails against upstream with an unhandled invalid_argument exception for negative length.
  • Regression covers invalid/closed descriptors; negative lengths; read-only, directory, pipe and socket rejection; kernel errno preservation; POSIX error translation; shrink, zero-filled growth, same-size and zero-size operations; write-only and append descriptors; native content verification and unchanged file position.
  • Full default build and explicit libs build passed.
  • Full CTest suite: 489/489 passed, 33.36 s.
  • Committed-head conventions, whitespace and test-registration checks passed.

Linux and PS5 hardware/titles were not run. This change does not add concurrent resize guarantees or replace the existing host resize implementation. The open shm_open PR #1860 uses ftruncate but does not implement these functions.

Checklist

  • Based on current main; no other open PR implements the same change (historical base; current readiness not asserted)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no silent stubs added
  • No notes, investigation 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
  • AI-assisted: yes — OpenAI Codex assisted with implementation, independent review, testing, and this description.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4796483705 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2041). Originally posted by [STUPIDCONTEM](https://github.com/STUPIDCONTEM) on 2026-10-09T05:40:32Z. --- ### Evidence review (2026-10-09) Closed by maintainer; not ready to reopen. Required evidence: Title ID and a log showing the title stops at the file-resize throw. Existing throws are intentional pending that need. Game/title ID: **none verified**. No PS5 title execution is claimed. Deferred after maintainer review: reopening requires a title ID and a log showing execution stops at the existing throw. Synthetic error-path tests do not establish that requirement. See [maintainer feedback](https://github.com/boykopovar/AnyPS5/pull/2041#issuecomment-6077722460). The patch and test results below are preserved as historical evidence; this change is excluded from the current proposed integration. ### What `sceKernelFtruncate` and POSIX `ftruncate` threw exceptions for ordinary invalid arguments and native failures. Return encoded kernel errors from `sceKernelFtruncate`, preserving errno, and translate those results into `-1` plus guest errno in the POSIX wrapper. On Windows, check the original descriptor's object type and granted write access before `_chsize_s`. This rejects read-only descriptors even when the requested size is unchanged, distinguishes invalid descriptors from non-regular files, and leaves genuine native resize failures intact. Open guest sockets return EINVAL; closed/invalid socket descriptors return EBADF. Successful resizing retains the existing host implementation. Error ordering and write-access requirements follow [FreeBSD 11 kern_ftruncate](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/sys_generic.c) and [truncate(2)](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/libc/sys/truncate.2). Windows uses documented [NtQueryObject access information](https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntqueryobject) and [_chsize_s](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/chsize-s?view=msvc-170). Linux already rejects read-only descriptors with EINVAL and retains its native ftruncate call. ### Tested Windows 10 Pro 19045, WinLibs GCC 15.2.0 UCRT/posix/SEH r7, Ninja Release, BUILD_TESTING=ON, SPIRV-Tools disabled. Based directly on `f145139676a48306681d3a4d5fabf83938759daf`; independent of #2029 and #2030. An independent review checked the implementation and expanded the success cases. - New `guest_file_resize` fails against upstream with an unhandled invalid_argument exception for negative length. - Regression covers invalid/closed descriptors; negative lengths; read-only, directory, pipe and socket rejection; kernel errno preservation; POSIX error translation; shrink, zero-filled growth, same-size and zero-size operations; write-only and append descriptors; native content verification and unchanged file position. - Full default build and explicit `libs` build passed. - Full CTest suite: **489/489 passed**, 33.36 s. - Committed-head conventions, whitespace and test-registration checks passed. Linux and PS5 hardware/titles were not run. This change does not add concurrent resize guarantees or replace the existing host resize implementation. The open shm_open PR #1860 uses ftruncate but does not implement these functions. ### Checklist - [ ] Based on current `main`; no other open PR implements the same change (historical base; current readiness not asserted) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes — OpenAI Codex assisted with implementation, independent review, testing, and this description.
MarcZX10 commented 2026-10-09 06:08:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T06:08:14Z.

Review: APPROVED


Reviewed the complete diff, including the Windows descriptor checks and guest/POSIX error translation. The regression exercises successful resize behavior and the invalid descriptor, length, file type, and access cases. I built the exact head on Linux with GCC 12; guest_file_resize passed (1/1), and conventions/diff checks pass. The PR description reports a full Windows suite pass; I could not independently reproduce Windows here. GitHub currently reports no upstream check results for this fork head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5466341732 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2041#pullrequestreview-5466341732). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T06:08:14Z. **Review:** APPROVED --- Reviewed the complete diff, including the Windows descriptor checks and guest/POSIX error translation. The regression exercises successful resize behavior and the invalid descriptor, length, file type, and access cases. I built the exact head on Linux with GCC 12; guest_file_resize passed (1/1), and conventions/diff checks pass. The PR description reports a full Windows suite pass; I could not independently reproduce Windows here. GitHub currently reports no upstream check results for this fork head.
oneandonlydean commented 2026-10-09 08:55:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T08:55:56Z.


Closing: on main these paths throw, which is allowed until a title needs the console's error codes. Reopen with the title ID and the log line if one stops at the throw.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6077722460 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2041#issuecomment-6077722460). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T08:55:56Z. --- Closing: on main these paths throw, which is allowed until a title needs the console's error codes. Reopen with the title ID and the log line if one stops at the throw.

Pull request closed

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!2041
No description provided.