Skip to content

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

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
maximilianfv:fix/libkernel-macos-append-positioned-writes
Oct 27, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
maximilianfv:fix/libkernel-macos-append-positioned-writes

Conversation

@maximilianfv

Copy link
Copy Markdown
Contributor

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 f07439e (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 1e20650, 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:

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

… macOS

macOS pwrite and pwritev write at the given offset even when the
descriptor was 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. On macOS, sceKernelPwrite,
pwrite, sceKernelPwritev, pwritev and the AIO write commands now append
such writes with writev and restore the file offset, as boykopovar#8359 does on
Windows.

Co-authored-by: Himansh Raj <67262056+iamthehimansh@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

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 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 6149 docs/dev/debt/libkernel.md
PR 6936 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 7088 core/libs/prx/libkernel/File/include/File.hpp
docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 core/libs/prx/libkernel/Aio/src/Aio.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7447 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7804 docs/dev/debt/libkernel.md
PR 8605 docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9199 docs/dev/debt/libkernel.md
PR 9242 docs/dev/debt/libkernel.md
PR 9284 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9293 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9297 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9302 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9326 docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9473 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 9491 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9575 docs/dev/debt/libkernel.md
PR 9947 docs/dev/debt/libkernel.md
PR 10192 docs/dev/debt/libkernel.md
PR 10319 docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 10552 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 10558 docs/dev/debt/libkernel.md
PR 10725 docs/dev/debt/libkernel.md
PR 10749 docs/dev/debt/libkernel.md
PR 11002 docs/dev/debt/libkernel.md
PR 11042 docs/dev/debt/libkernel.md
PR 11085 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11223 docs/dev/debt/libkernel.md
PR 11264 docs/dev/debt/libkernel.md
PR 11267 docs/dev/debt/libkernel.md
PR 11341 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 imellag left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@rooklz

rooklz commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Measured on a PS5 (a payload calling the console's pwrite on /data): on an O_APPEND descriptor of an 11-byte file, pwrite(fd, "Q", 1, 0) returns 1 and the byte lands at offset 11 (size 12, the first byte unchanged), and lseek(fd, 0, SEEK_CUR) afterwards still gives 0. That is both halves of what this PR does: append whatever the offset, and leave the file offset unchanged. The same probe built natively on macOS 26.2 writes the byte at offset 0 instead. In 19 other pwrite cases I tried (holes, negative and huge offsets, zero length, null buffer, bad/read-only/directory descriptors, a pipe, an offset past 4 GiB), macOS already matches the console, so O_APPEND is the only difference on that host.

@krishalivaibhav

Copy link
Copy Markdown
Contributor

Test report: #10543 (O_APPEND / WritevAtEnd on macOS)

Tested on: Apple M2 MacBook Air, macOS 27.2 (26B118), x86_64 Release build under Rosetta 2, AppleClang 21.0.0, Ninja.
Tree: origin/main a5a780e merged into a scratch branch holding our open macOS stack (integ8 029564a43: #10552, #10853 and their dependencies, plus the process-parameters, mlock-PROT_NONE and math-exact fixes). Base commit 1ddafb62d. PR head 954a8e1.

Conflict: the PR does not merge cleanly onto this stack (one conflict, with our #10552). Hunk: core/libs/prx/libkernel/File/src/Stdio.cpp, sceKernelPwritev (~L1635), our if (iovcnt == 0) return 0; against the PR's File::WritevAtEnd(...) line. Resolved on the scratch branch only. Taking only the PR's side (as first instructed) dropped the zero-count early return and made guest_file_vector_memory abort (5/5 runs, pwritev with count 0; passes 3/3 on base). The numbers below use the resolution that keeps both lines (early return, then the PR's WritevAtEnd call). The PR itself was not edited.

test (3 runs each) without with
guest_aio abort 3/3 pass 3/3
guest_append_writes abort 3/3 pass 3/3
guest_append_streams pass 3/3 pass 3/3
guest_file_vector_memory pass 3/3 pass 3/3 (keep-both) / abort 5/5 (PR side only)

Broad run: ctest -R "^(guest_|libc_|libkernel_)" = 678 tests. On the base 133 do not pass, 344 of them "Not Run" because their executables do not build here (AGC driver SpirvOptimizer.cpp needs pthread_getattr_np; guest_tls_get_addr and guest_memory test objects fail to compile). Those are Not Run both ways. With the PR (keep-both): 131 do not pass. The only differences from base are guest_aio and guest_append_writes now passing; nothing new fails.

Not checked: Linux/Windows builds; behaviour of O_APPEND on real guest titles; whether the PR as pushed passes guest_file_vector_memory on its own base (only tested on this stack).

AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 mentioned this pull request Oct 25, 2026
12 tasks done
@adriadam10 adriadam10 self-assigned this Oct 26, 2026
@adriadam10
adriadam10 merged commit b986c11 into boykopovar:main Oct 27, 2026
11 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the diff against FreeBSD's vn_write (append on O_APPEND regular files regardless of offset), the macOS-only guard, File::Appends, errno preservation and the debt entry; the macOS results from @MarcZX10, @imellag and @krishalivaibhav and @rooklz's PS5 measurement cover what we can't run here; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants