kernel: implement sceKernelWritev #1401
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1401
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1401/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@HamsterCL on GitHub · 2026-10-11 03:35 UTC · original
HamsterCL:fix/kernel-writev-truncate→main· +159 −0 · 4 files · 4 commits · closed without mergingProblem
EA SPORTS FC 27 (PPSA34015) imports
sceKernelWritev(kAt6VDbHmro, libkernel). It is not implemented, so it falls into the unresolved-import stub, which returns 0 and writes nothing. This is visible in the log asUnresolved import stub calledfor that NID (3 calls in one run).The game uses
writev()to write theINDEXfile of its settings save data. With the stub, the call reports 0 bytes written, so the file stays empty:On the next launch the game shows an in-game error,
Personal Setting 1 failed to load.Changes
sceKernelWritev(fd, iov, iovcnt)insrc/kernel/fileSystem.cpp, registered inlibkernel_v1next to the other file functions. It validates the vectors with the same helpersceKernelPwritevuses (IOV_MAX, overflow-safe total, null base only with zero length, null array is EFAULT), then writes at the descriptor position under the file mutex, honoursO_APPENDand sync writes, and stops at the first short write. It returns the total written or a negative kernel error. fd 1 and 2 go to stdout/stderr as insceKernelWrite. On a socket the vectors are gathered and sent as a single message, so a datagram socket does not turn onewritev()into several datagrams. Directories, special files, read-only and unknown descriptors return the usual errors.TestWritevintests/KernelFileSystemTests.cpp: multi-iovec writes, an empty iovec,iovcnt0, position advance,O_APPEND, null array and null base,iovcntof -1 and 1025, a length overflow, and bad and read-only descriptors.sceKernelTruncate(WlyEA-sLDf0), which FC 27 also imports, is deliberately left out of this PR because #1170 already covers it.Testing
kernel_file_system_testsbuilds and links on Windows (clang-cl, Release) on top of currentmain.TestWritevruns before the point where the binary stops.mainthat binary still aborts later in the run, inCheckSocketWakeup, withNet unknown socket options return protocol-option errors. That check is unrelated to this change (it is not in the CI test subsets, so it is not exercised there).--amd-cpu: after this changeINDEXis written with 240 bytes (Writev 240 bytes ... INDEXin the log, next toWritev 768774 bytes ... DATA),kAt6VDbHmrono longer appears as an unresolved import, and the next launch no longer showsPersonal Setting 1 failed to load.writev.Notes
The game still asks for its initial setup again on the next launch in my runs. In the log the settings are written only once, right after the first boot (defaults), and no further save is made while I go through the setup, so I believe that is caused by the setup never completing (the game stops at an online-services message), not by this change.
AI use
I used AI assistance (Claude Code) to track down the missing imports, resolve their NIDs and prepare the implementation and tests at my direction. I reviewed the diff, including the validation, locking and error paths, and I ran the manual FC 27 test described above.
@coderabbitai[bot] on GitHub · 2026-10-11 03:38 UTC · original · edited 2026-10-11 12:35 UTC
📝 Walkthrough
Walkthrough
The change adds and exports
KernelWritev. The implementation validates iovec entries and writes to standard streams, sockets, or regular files. Tests cover write ordering, byte counts, descriptor position, append behavior, and error cases.Suggested reviewers:
aspeniniPriority: ⬇️ Low
Change: Bug fix
Merge Risk: 🟡 Moderate · up to
bd8ddConcurrent write and close can crash the process. Coordinate file lifetime across those operations before merging.
Pre-merge checks |
4 |
1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
sceKernelWritevimplementation, the changes, tests, observed results, and out-of-scope work.sceKernelWritevin the kernel.Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@coderabbitai[bot] on GitHub · 2026-10-11 03:38 UTC · original · reviewed
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
944d2c38-e7e7-4c30-9619-ef68e440e02e📥 Commits
Reviewing files that changed from the base of the PR and between
730e1a3cf3and 2358b21adcac0781b8ef2e799d170b2cb7dce61b.📒 Files selected for processing (4)
src/kernel/fileSystem.cppsrc/kernel/fileSystem.hsrc/libs/libKernel.cpptests/KernelFileSystemTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.
@coderabbitai[bot] on GitHub · 2026-10-11 03:38 UTC · original · edited 2026-10-11 12:30 UTC
On
src/kernel/fileSystem.cppline 1064 (outdated):🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Send all vectors as one socket message.
If
dis a datagram socket, eachNet::Sendcall sends a separate datagram. Two nonempty vectors therefore produce two datagrams instead of one gathered message. Use one vectored socket send, or gather the buffers before one send. (man7.org)✅ Addressed in commit
8ed0812@coderabbitai[bot] on GitHub · 2026-10-11 03:38 UTC · original
On
src/kernel/fileSystem.cppline 1364 (outdated):🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Keep the selected file alive through truncation.
GetFile(real_file_name)returns a raw pointer after releasing the descriptor-table lock. If another thread closes that descriptor,KernelClosecan delete the file before this function checksopenedor acquiresopen_file->mutex. A path-based truncate can then access freed memory. Coordinate lookup, truncation, and close through shared ownership or a descriptor-table lifetime lock.@ -1025,0 +1077,4 @@}const auto result = ::Libs::Network::Net::Send(d, gathered.get(), gathered_size, 0);return (result < 0 ? PosixToKernel(*Posix::GetErrorAddr()) : static_cast<int64_t>(result));}@coderabbitai[bot] on GitHub · 2026-10-11 03:45 UTC · original · edited 2026-10-11 03:48 UTC
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4920
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3014
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 2820
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 45669
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 45669
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4110
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 14892
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 875
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3077
Return
KERNEL_ERROR_ENOMEMwhen socket gathering cannot allocate.ValidateIovecsaccepts a valid total up toINT_MAX. For a socket,reserve(total)andinsertcan therefore request almost 2 GiB. If allocation fails,std::bad_allocescapesKernelWritevacross itsKYTY_SYSV_ABIboundary. Catch the allocation failure locally and returnKERNEL_ERROR_ENOMEM. An arbitrary cap would change the accepted iovec contract, andstd::nothrowdoes not handlestd::vectorallocation.🐛 Suggested fix
📝 Committable suggestion
✅ Addressed in commit
cabb11c@coderabbitai[bot] on GitHub · 2026-10-11 03:45 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
944e743c-9add-4449-9212-a76eaebe1d3f📥 Commits
Reviewing files that changed from the base of the PR and between 2358b21adcac0781b8ef2e799d170b2cb7dce61b and
6276278957.📒 Files selected for processing (4)
src/kernel/fileSystem.cppsrc/kernel/fileSystem.hsrc/libs/libKernel.cpptests/KernelFileSystemTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.
@HamsterCL on GitHub · 2026-10-11 03:49 UTC · original
Thanks for the review. I changed the PR since it was opened, so here is where each finding ended up:
Truncateand the lifetime of the selected file (first review): I removedsceKernelTruncatefrom this PR. #1170 already implements it with the same NID, so the PR is nowsceKernelWritevonly (retitled accordingly), and that finding no longer applies here.62762789. On a socket the vectors are gathered into one buffer and sent with a singleNet::Send, so a datagram socket gets one message instead of one datagram per vector. A zero total returns 0, and a send error is translated like before.ENOMEMwhen gathering fails (second review): fixed incabb11cd. The gathering is wrapped in atry/catchforstd::bad_allocand returnsKERNEL_ERROR_ENOMEM, so the exception does not cross the guest ABI boundary. I did not add a size cap, to keep the accepted iovec contract unchanged. There is no test for the allocation failure itself, because forcing a multi-GiB allocation failure is not practical in the test harness.@coderabbitai[bot] on GitHub · 2026-10-11 12:35 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
1c45b902-f56d-4aeb-8859-716039ebe976📥 Commits
Reviewing files that changed from the base of the PR and between
cabb11cd0eandbd8dd6a0d7.📒 Files selected for processing (1)
src/kernel/fileSystem.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
@ -1025,0 +1102,4 @@if (file->append && !file->f.Seek(file->f.Size())) {return KERNEL_ERROR_EIO;}@coderabbitai[bot] on GitHub · 2026-10-11 12:35 UTC · original
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 21918
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3205
Protect the
Filelifetime in the regular-fileKernelWritevpath.GetFile(d)returns a raw pointer after releasing the descriptor-table lock. A concurrent exportedKernelClose(d)can delete thatFilebeforeKernelWritevreadsfile->openedor acquiresfile->mutex. This can cause a use-after-free and crash.Add descriptor-lifetime coordination that covers both
KernelWritevandKernelClose. The existingfile->mutexis insufficient becauseKernelWritevdereferences the pointer before locking it, andKernelClosedoes not use that mutex.@overlordxrz-source on GitHub · 2026-10-15 18:20 UTC · original
NHL 27 needs this too. On #1101, pinkton found that NHL 27 writes its save index through
sceKernelWritev. With the unresolved-import stub the index stays empty, so the save never loads. It's the same pattern as the FC 27INDEXfile here. The other two calls on that path,sceKernelTruncateandsceKernelFdatasync, are in #1170.I read through the file-write path and it matches
sceKernelWriteandsceKernelPwritev: it writes at the current position, honoursO_APPENDand sync writes, and stops at the first short write. It merges cleanly with the rebased #1170. I also ran a native harness on the realfileSystem.cppfrom main + #1170 + this PR, with the NHL 27 index pattern: writev two iovecs over a stale, longer file, truncate it to the written length by path, then read it back. It passed.AI-assisted: yes
@Nmzik on GitHub · 2026-10-15 23:35 UTC · original
Credited
54abbf48f2Pull request closed