fix(libkernel): append positioned writes on an O_APPEND descriptor on macOS #10543

Merged
maximilianfv merged 1 commit from upstream-pr/10543 into main 2026-10-27 03:52:27 +00:00
maximilianfv commented 2026-10-24 09:18:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-24T09:18:18Z.


What

On macOS, pwrite and pwritev write at the given offset even on a descriptor opened with O_APPEND. FreeBSD's vn_write appends to an O_APPEND regular file whatever the offset and leaves the file offset unchanged, and Linux pwrite appends too. A standalone C probe on macOS shows the difference: on an O_APPEND descriptor, write "AAAA", pwrite "BB" at 0 and pwritev "CC" at 0 leave the file as "CCAA" with the offset at 4 (the probe prints contents=CCAA size=4 pos_after=4).

This adds File::WritevAtEnd (Apple only, declared next to the Windows-only WriteAtEnd). When File::Appends reports the descriptor appends and fstat says it is a regular file, it saves the offset, writes with writev and seeks back, keeping errno from the write; otherwise it returns -2 and the caller falls through to pwrite/pwritev as before. It is called from:

  • the POSIX NativePwrite in Stdio.cpp (sceKernelPwrite, pwrite)
  • sceKernelPwritev (and so pwritev)
  • NativePwrite in Aio.cpp (the AIO write commands)

This is the macOS counterpart of #8359 (merged, Windows, by Evrim Cicek), whose checks in guest_append_writes and guest_aio fail on macOS without it. The design follows #8359; no code is taken from it.

Prior art: the closed whole-port PR #8658 by Himansh Raj (iamthehimansh) carried the same change for macOS in commit f07439ee8d (2026-10-23, "fix(libkernel): append positioned writes on an O_APPEND file on macOS"), at the same three call sites with writev and a seek back, and parts of this change match it closely, so Himansh Raj is credited as co-author on the commit. The differences: #8658 reads fcntl(F_GETFL) instead of the guest flag table (File::Appends), has no regular-file check, uses write in the AIO path, skips the seek back when the first lseek fails, and adds no debt entry. #8658 was closed and split into smaller PRs; none of them, and no open PR, carries this change. #7201 does not touch these paths.

POSIX has no positioned-transfer lock (LockPositionedTransfer exists only on Windows), so another thread seeking or transferring on the same or a duplicated descriptor between the write and the seek back can see the end-of-file offset or have its own seek undone. The new entry in docs/dev/debt/libkernel.md records this.

Tested

Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2. main alone does not build on macOS, so this was tested in a tree made of mugurc's macOS series merged with main at 1e20650e84, plus the macOS build parts of #10419 and other macOS build and test fixes that are not yet PRs. Only this change was toggled between the two runs.

ctest of 14 file I/O tests: guest_append_writes, guest_aio, guest_kernel_sys_aio, file_position, guest_file_vector, guest_file_vector_memory, guest_append_streams, guest_posix_filesystem, guest_sendfile, guest_standard_streams, file_streams, guest_host_streams, file_identity, guest_filesystem.

  • Without this change: 10 pass. guest_append_writes aborts with "append check failed at line 94" and guest_aio with "Aio check failed at line 413"; guest_standard_streams and guest_filesystem abort.
  • With this change: 12 pass. guest_append_writes and guest_aio pass; guest_standard_streams and guest_filesystem still abort at the same places.

The two remaining failures are separate macOS gaps this PR does not touch, and they fail the same way with and without it:

  • guest_standard_streams stops at "fflush of a "r" stream that is reading changed the data read next"; the open #8920 (tkcm01) addresses this.
  • guest_filesystem stops at GuestFilesystem.cpp:884 (rename onto a dangling symbolic link), located with lldb on a -O0 build in both states; the open #9911 (tkcm01) addresses this.

Not tested on Linux or Windows. Their code paths are unchanged: all new code is under __APPLE__.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4994169564 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10543). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-24T09:18:18Z. --- ### What On macOS, `pwrite` and `pwritev` write at the given offset even on a descriptor opened with `O_APPEND`. FreeBSD's `vn_write` appends to an `O_APPEND` regular file whatever the offset and leaves the file offset unchanged, and Linux `pwrite` appends too. A standalone C probe on macOS shows the difference: on an `O_APPEND` descriptor, `write` "AAAA", `pwrite` "BB" at 0 and `pwritev` "CC" at 0 leave the file as "CCAA" with the offset at 4 (the probe prints `contents=CCAA size=4 pos_after=4`). This adds `File::WritevAtEnd` (Apple only, declared next to the Windows-only `WriteAtEnd`). When `File::Appends` reports the descriptor appends and `fstat` says it is a regular file, it saves the offset, writes with `writev` and seeks back, keeping `errno` from the write; otherwise it returns -2 and the caller falls through to `pwrite`/`pwritev` as before. It is called from: - the POSIX `NativePwrite` in `Stdio.cpp` (`sceKernelPwrite`, `pwrite`) - `sceKernelPwritev` (and so `pwritev`) - `NativePwrite` in `Aio.cpp` (the AIO write commands) This is the macOS counterpart of #8359 (merged, Windows, by Evrim Cicek), whose checks in `guest_append_writes` and `guest_aio` fail on macOS without it. The design follows #8359; no code is taken from it. Prior art: the closed whole-port PR #8658 by Himansh Raj (iamthehimansh) carried the same change for macOS in commit f07439ee8d (2026-10-23, "fix(libkernel): append positioned writes on an O_APPEND file on macOS"), at the same three call sites with `writev` and a seek back, and parts of this change match it closely, so Himansh Raj is credited as co-author on the commit. The differences: #8658 reads `fcntl(F_GETFL)` instead of the guest flag table (`File::Appends`), has no regular-file check, uses `write` in the AIO path, skips the seek back when the first `lseek` fails, and adds no debt entry. #8658 was closed and split into smaller PRs; none of them, and no open PR, carries this change. #7201 does not touch these paths. POSIX has no positioned-transfer lock (`LockPositionedTransfer` exists only on Windows), so another thread seeking or transferring on the same or a duplicated descriptor between the write and the seek back can see the end-of-file offset or have its own seek undone. The new entry in `docs/dev/debt/libkernel.md` records this. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2. `main` alone does not build on macOS, so this was tested in a tree made of mugurc's macOS series merged with `main` at 1e20650e84, plus the macOS build parts of #10419 and other macOS build and test fixes that are not yet PRs. Only this change was toggled between the two runs. `ctest` of 14 file I/O tests: `guest_append_writes`, `guest_aio`, `guest_kernel_sys_aio`, `file_position`, `guest_file_vector`, `guest_file_vector_memory`, `guest_append_streams`, `guest_posix_filesystem`, `guest_sendfile`, `guest_standard_streams`, `file_streams`, `guest_host_streams`, `file_identity`, `guest_filesystem`. - Without this change: 10 pass. `guest_append_writes` aborts with "append check failed at line 94" and `guest_aio` with "Aio check failed at line 413"; `guest_standard_streams` and `guest_filesystem` abort. - With this change: 12 pass. `guest_append_writes` and `guest_aio` pass; `guest_standard_streams` and `guest_filesystem` still abort at the same places. The two remaining failures are separate macOS gaps this PR does not touch, and they fail the same way with and without it: - `guest_standard_streams` stops at "fflush of a "r" stream that is reading changed the data read next"; the open #8920 (tkcm01) addresses this. - `guest_filesystem` stops at `GuestFilesystem.cpp:884` (rename onto a dangling symbolic link), located with lldb on a `-O0` build in both states; the open #9911 (tkcm01) addresses this. Not tested on Linux or Windows. Their code paths are unchanged: all new code is under `__APPLE__`. ### 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-24 10:41:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T10:41:05Z.

Review: APPROVED


Reviewed WritevAtEnd and its call sites in regular positioned writes, vector writes, and AIO. The fallback is limited to descriptors marked append and regular files; it preserves the write error and avoids writing when it cannot save the current offset. I also checked the shared-path overlap with #10552: this PR provides the more complete helper and its regular-file/seekability guards, so #10552 should reuse or coordinate with it. The known race with concurrent offset-changing operations is explicitly recorded in the debt entry; this approval is for the tested sequential behavior and does not claim that concurrency limitation is solved. The contributor reports the macOS append/AIO regression tests passing; hosted eligibility, conventions, intake, labels, and report pass, while hosted build jobs were skipped by the eligibility gate. I did not rerun macOS tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5637956337 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10543#pullrequestreview-5637956337). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T10:41:05Z. **Review:** APPROVED --- Reviewed `WritevAtEnd` and its call sites in regular positioned writes, vector writes, and AIO. The fallback is limited to descriptors marked append and regular files; it preserves the write error and avoids writing when it cannot save the current offset. I also checked the shared-path overlap with #10552: this PR provides the more complete helper and its regular-file/seekability guards, so #10552 should reuse or coordinate with it. The known race with concurrent offset-changing operations is explicitly recorded in the debt entry; this approval is for the tested sequential behavior and does not claim that concurrency limitation is solved. The contributor reports the macOS append/AIO regression tests passing; hosted eligibility, conventions, intake, labels, and report pass, while hosted build jobs were skipped by the eligibility gate. I did not rerun macOS tests locally.
imellag commented 2026-10-24 20:00:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-24T20:00:47Z.

Review: APPROVED


Tested head 954a8e1a9d on Apple M4 Pro, macOS 27.0.1, Apple clang 21, x86-64 under Rosetta 2. The tree is main 3fcb5e4d76 plus the open macOS build PRs, with #10552 at its current head e60d9dfa3a, which no longer has its own O_APPEND fallback, and the parts of mugurc's series that #7201 also carries. Only this PR was toggled between the two runs.

On the same 14 file I/O tests as in the description:

  • Without this PR, 12 pass. guest_aio aborts with "Aio check failed at line 456" and guest_append_writes with "append check failed at line 94".
  • With it, all 14 pass. guest_standard_streams and guest_filesystem pass here too, because this tree already has the fixes for them.

One merge note. This PR conflicts textually with #10552 in the __APPLE__ block of sceKernelPwritev in Stdio.cpp. #10552 adds if (iovcnt == 0) return 0; on the same line where this PR adds the File::WritevAtEnd call. I kept both, the iovcnt check first, and that is the tree the results above come from. Whichever of the two lands second needs that resolution.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639737685 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10543#pullrequestreview-5639737685). Originally posted by [imellag](https://github.com/imellag) on 2026-10-24T20:00:47Z. **Review:** APPROVED --- Tested head `954a8e1a9d` on Apple M4 Pro, macOS 27.0.1, Apple clang 21, x86-64 under Rosetta 2. The tree is `main` `3fcb5e4d76` plus the open macOS build PRs, with #10552 at its current head `e60d9dfa3a`, which no longer has its own `O_APPEND` fallback, and the parts of mugurc's series that #7201 also carries. Only this PR was toggled between the two runs. On the same 14 file I/O tests as in the description: - Without this PR, 12 pass. `guest_aio` aborts with "Aio check failed at line 456" and `guest_append_writes` with "append check failed at line 94". - With it, all 14 pass. `guest_standard_streams` and `guest_filesystem` pass here too, because this tree already has the fixes for them. **One merge note.** This PR conflicts textually with #10552 in the `__APPLE__` block of `sceKernelPwritev` in `Stdio.cpp`. #10552 adds `if (iovcnt == 0) return 0;` on the same line where this PR adds the `File::WritevAtEnd` call. I kept both, the `iovcnt` check first, and that is the tree the results above come from. Whichever of the two lands second needs that resolution. 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!10543
No description provided.