fix(libkernel): append positioned writes on an O_APPEND descriptor on Windows #8359

Merged
Ragbie merged 1 commit from upstream-pr/8359 into main 2026-10-23 09:23:58 +00:00
Ragbie commented 2026-10-20 10:59:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-10-20T10:59:39Z.


What

On Windows, sceKernelPwrite, pwrite, sceKernelPwritev, pwritev and the AIO write commands on a descriptor opened with O_APPEND write at the given offset, over the start of the file. #5831 made write and writev append on such a descriptor and left pwrite unchanged. On Linux these calls go to the host's pwrite and pwritev, which append.

FreeBSD 11.4 appends too: kern_pwritev calls dofilewrite with FOF_OFFSET, and vn_write sets IO_APPEND for an O_APPEND regular file whether or not an offset is given. ffs_write (and tmpfs) then writes at the end of the file. With FOF_OFFSET the file offset is not updated.

The change, Windows only:

  • NativePositionedTransfer writes on an O_APPEND descriptor (File::Appends) at offset 0xFFFFFFFFFFFFFFFF, Windows' write to the end of the file, which File::WriteAtEnd already uses for write. It still restores the file pointer afterwards, so the descriptor's offset does not move. Handles that are not disk files still fail with ESPIPE, directories with EBADF. sceKernelPwrite, pwrite, pwritev and the AIO write commands all go through this function.
  • TransferIovecs gathers a multi-buffer pwritev on an O_APPEND descriptor into one write, as it already does for writev: FreeBSD appends the whole vector in one VOP_WRITE, under the whole-file range lock vn_io_fault takes for O_APPEND.
  • The technical debt entry for sceKernelPread, sceKernelPwrite, sceKernelPreadv and sceKernelPwritev no longer says these transfer "at the given offset".

Tested

Windows 11, WinLibs GCC 15.2.0:

  • guest_append_writes gets a fifth concurrent writer, which appends with a two-buffer pwritev(fd, ..., 0) on its own descriptor, and then single-threaded sceKernelPwrite(fd, ..., 0) and pwritev(fd, ..., 32) that must append and leave lseek(fd, 0, SEEK_CUR) at 0. guest_aio gets an AIO write at offset 0 on an O_APPEND descriptor that must append.
  • With main's Stdio.cpp: append check failed at line 94 (file size after the concurrent writers: the pwritev writer overwrites other lines) and Aio check failed at line 413. With only the offset change: append check failed at line 102 in 5 of 5 runs (other writers land between the two pwritev buffers). With the change, both pass with --repeat until-fail:10.
  • Full build and ctest: 99% tests passed, 4 tests failed out of 1735. agc_driver_image_array_window_past_layer_limit does not build with MinGW on main (#8310); agc_driver_image_plane_address_on_line and agc_driver_pass_input_hazard fail on main on this machine too; agc_driver_sgpr_pattern_fill_keys failed once in the parallel run and passes 3 of 3 alone.

Linux (Ubuntu 24.04 container, GCC 13.3.0): both tests pass. The change compiles into the Linux build only through these two tests; full ctest: 99% tests passed, 17 tests failed out of 1700, all outside this change (16 agc_* tests on the container's lavapipe and kernel, which lacks UFFD_FEATURE_WP_ASYNC, and guest_system_load_exec_standard_error).

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4934753157 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8359). Originally posted by [Ragbie](https://github.com/Ragbie) on 2026-10-20T10:59:39Z. --- ### What On Windows, `sceKernelPwrite`, `pwrite`, `sceKernelPwritev`, `pwritev` and the AIO write commands on a descriptor opened with `O_APPEND` write at the given offset, over the start of the file. #5831 made `write` and `writev` append on such a descriptor and left `pwrite` unchanged. On Linux these calls go to the host's `pwrite` and `pwritev`, which append. FreeBSD 11.4 appends too: `kern_pwritev` calls `dofilewrite` with `FOF_OFFSET`, and [`vn_write`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_vnops.c#L864-L866) sets `IO_APPEND` for an `O_APPEND` regular file whether or not an offset is given. [`ffs_write`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/ufs/ffs/ffs_vnops.c#L692-L693) (and tmpfs) then writes at the end of the file. With `FOF_OFFSET` the file offset is not updated. The change, Windows only: - `NativePositionedTransfer` writes on an `O_APPEND` descriptor (`File::Appends`) at offset `0xFFFFFFFFFFFFFFFF`, Windows' write to the end of the file, which `File::WriteAtEnd` already uses for `write`. It still restores the file pointer afterwards, so the descriptor's offset does not move. Handles that are not disk files still fail with `ESPIPE`, directories with `EBADF`. `sceKernelPwrite`, `pwrite`, `pwritev` and the AIO write commands all go through this function. - `TransferIovecs` gathers a multi-buffer `pwritev` on an `O_APPEND` descriptor into one write, as it already does for `writev`: FreeBSD appends the whole vector in one `VOP_WRITE`, under the whole-file range lock `vn_io_fault` takes for `O_APPEND`. - The technical debt entry for `sceKernelPread`, `sceKernelPwrite`, `sceKernelPreadv` and `sceKernelPwritev` no longer says these transfer "at the given offset". ### Tested Windows 11, WinLibs GCC 15.2.0: - `guest_append_writes` gets a fifth concurrent writer, which appends with a two-buffer `pwritev(fd, ..., 0)` on its own descriptor, and then single-threaded `sceKernelPwrite(fd, ..., 0)` and `pwritev(fd, ..., 32)` that must append and leave `lseek(fd, 0, SEEK_CUR)` at 0. `guest_aio` gets an AIO write at offset 0 on an `O_APPEND` descriptor that must append. - With `main`'s `Stdio.cpp`: `append check failed at line 94` (file size after the concurrent writers: the `pwritev` writer overwrites other lines) and `Aio check failed at line 413`. With only the offset change: `append check failed at line 102` in 5 of 5 runs (other writers land between the two `pwritev` buffers). With the change, both pass with `--repeat until-fail:10`. - Full build and `ctest`: `99% tests passed, 4 tests failed out of 1735`. `agc_driver_image_array_window_past_layer_limit` does not build with MinGW on `main` (#8310); `agc_driver_image_plane_address_on_line` and `agc_driver_pass_input_hazard` fail on `main` on this machine too; `agc_driver_sgpr_pattern_fill_keys` failed once in the parallel run and passes 3 of 3 alone. Linux (Ubuntu 24.04 container, GCC 13.3.0): both tests pass. The change compiles into the Linux build only through these two tests; full `ctest`: `99% tests passed, 17 tests failed out of 1700`, all outside this change (16 `agc_*` tests on the container's lavapipe and kernel, which lacks `UFFD_FEATURE_WP_ASYNC`, and `guest_system_load_exec_standard_error`). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
cadenasynyster commented 2026-10-21 21:28:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-21T21:28:49Z.


Independent Windows run of this PR (head ad7e1acdb).

Environment: Windows 11 10.0.26300, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 (MinGW), Release, BUILD_TESTING=ON.

Results:

  • PR head: guest_append_writes and guest_aio pass 10/10 (--repeat until-fail:10).
  • Baseline: I kept the PR's tests and replaced only core/libs/prx/libkernel/File/src/Stdio.cpp with the merge-base version (84be26d93). guest_aio fails with Aio check failed at line 413 and guest_append_writes with append check failed at line 94, the same as the description. I restored the PR's file afterwards.

Not run: the offset-only variant from the description, Linux/macOS, the full repository CTest, and any title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6326961836 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8359#issuecomment-6326961836). Originally posted by [cadenasynyster](https://github.com/cadenasynyster) on 2026-10-21T21:28:49Z. --- Independent Windows run of this PR (head ad7e1acdb). **Environment:** Windows 11 10.0.26300, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 (MinGW), Release, BUILD_TESTING=ON. **Results:** - PR head: `guest_append_writes` and `guest_aio` pass 10/10 (`--repeat until-fail:10`). - Baseline: I kept the PR's tests and replaced only `core/libs/prx/libkernel/File/src/Stdio.cpp` with the merge-base version (84be26d93). `guest_aio` fails with `Aio check failed at line 413` and `guest_append_writes` with `append check failed at line 94`, the same as the description. I restored the PR's file afterwards. **Not run:** the offset-only variant from the description, Linux/macOS, the full repository CTest, and any title. AI-assisted: yes
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!8359
No description provided.