From 87f44444412d4c809d4fa701a72dd9c23db9cba1 Mon Sep 17 00:00:00 2001 From: Raihan Abdul Majid Date: Thu, 8 Oct 2026 07:48:57 +0700 Subject: [PATCH] fix(kernel): transfer each iovec directly on Windows Gathering every iovec into one host buffer threw for a total above INT_MAX, which a console readv/writev accepts, and copied the whole transfer twice. Each iovec is now read or written in place, in chunks of at most INT_MAX bytes. A readv on a pipe returns after one read so it does not block once the available bytes are consumed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UTjpbMX7BUFMZZaxJE98zV --- core/libs/prx/libkernel/File/src/Stdio.cpp | 40 ++++++++++++---------- core/libs/tests/GuestFileVector.cpp | 30 ++++++++++++++++ 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/core/libs/prx/libkernel/File/src/Stdio.cpp b/core/libs/prx/libkernel/File/src/Stdio.cpp index 2cdb0ca40..d1aaae10d 100644 --- a/core/libs/prx/libkernel/File/src/Stdio.cpp +++ b/core/libs/prx/libkernel/File/src/Stdio.cpp @@ -132,6 +132,10 @@ static std::int64_t NativeTransfer(int descriptor, void* buf, std::size_t nbytes } return write ? ::_write(descriptor, buf, static_cast(nbytes)) : ::_read(descriptor, buf, static_cast(nbytes)); } +static bool NativeIsDisk(int descriptor) { + const auto handle = reinterpret_cast(::_get_osfhandle(descriptor)); + return handle != INVALID_HANDLE_VALUE && ::GetFileType(handle) == FILE_TYPE_DISK; +} static std::int64_t NativePread(int descriptor, void* buf, std::size_t nbytes, std::int64_t offset) { return NativePositioned(descriptor, buf, nbytes, offset, false); } @@ -534,28 +538,28 @@ static std::int64_t TransferIovecs(int d, const KernelIovec* iov, int iovcnt, co if (iov[i].length > static_cast(std::numeric_limits::max()) - total) return SceErrorFromErrno(GUEST_EINVAL); total += iov[i].length; } + if (offset != nullptr && total > static_cast(std::numeric_limits::max() - *offset)) return SceErrorFromErrno(GUEST_EINVAL); std::deque destinations; if (!write && !OpenIovecs(iov, iovcnt, destinations)) return SceErrorFromErrno(GUEST_EFAULT); - std::vector buffer(std::max(total, 1)); - if (write) { - std::size_t position = 0; - for (int i = 0; i < iovcnt; ++i) { - if (iov[i].length != 0) std::memcpy(buffer.data() + position, iov[i].base, iov[i].length); - position += iov[i].length; + if (total == 0) { + char none = 0; + const auto result = offset != nullptr ? NativePositioned(d, &none, 0, *offset, write) : NativeTransfer(d, &none, 0, write); + return result < 0 ? SceErrorFromErrno(errno) : 0; + } + const bool whole = write || offset != nullptr || NativeIsDisk(d); + std::int64_t done = 0; + for (int i = 0; i < iovcnt; ++i) { + auto* base = static_cast(iov[i].base); + for (std::size_t position = 0; position < iov[i].length;) { + const auto chunk = std::min(iov[i].length - position, std::numeric_limits::max()); + const auto result = offset != nullptr ? NativePositioned(d, base + position, chunk, *offset + done, write) : NativeTransfer(d, base + position, chunk, write); + if (result < 0) return done != 0 ? done : SceErrorFromErrno(errno); + done += result; + position += static_cast(result); + if (static_cast(result) < chunk || !whole) return done; } } - const auto result = offset != nullptr ? NativePositioned(d, buffer.data(), total, *offset, write) : NativeTransfer(d, buffer.data(), total, write); - if (result < 0) return SceErrorFromErrno(errno); - if (!write) { - const auto received = static_cast(result); - std::size_t position = 0; - for (int i = 0; i < iovcnt && position < received; ++i) { - const auto count = std::min(iov[i].length, received - position); - std::memcpy(iov[i].base, buffer.data() + position, count); - position += count; - } - } - return result; + return done; } int64_t APS5_VABI sceKernelReadv(int d, const KernelIovec* iov, int iovcnt) { diff --git a/core/libs/tests/GuestFileVector.cpp b/core/libs/tests/GuestFileVector.cpp index a3cb50f35..1851fb20b 100644 --- a/core/libs/tests/GuestFileVector.cpp +++ b/core/libs/tests/GuestFileVector.cpp @@ -10,6 +10,16 @@ #include #include #include +#ifdef _WIN32 +#include +#include +static int MakePipe(int* ends) { return ::_pipe(ends, 64, _O_BINARY); } +static int ClosePipe(int end) { return ::_close(end); } +#else +#include +static int MakePipe(int* ends) { return ::pipe(ends); } +static int ClosePipe(int end) { return ::close(end); } +#endif struct GuestIovec { void* base; @@ -38,6 +48,7 @@ static void Check(bool value, int line) { static constexpr std::int64_t ErrorEbadf = static_cast(0x80020009u); static constexpr std::int64_t ErrorEfault = static_cast(0x8002000Eu); static constexpr std::int64_t ErrorEinval = static_cast(0x80020016u); +static constexpr std::int64_t ErrorEspipe = static_cast(0x8002001Du); static std::string Contents(const std::filesystem::path& path) { std::ifstream stream(path, std::ios::binary); @@ -81,6 +92,14 @@ int main() { Require(sceKernelLseek(file, 0, 1) == 13); Require(Contents(path) == "01ABCDE7ABCDE"); + GuestIovec gapped[3] = {{first, 2}, {nullptr, 0}, {second, 3}}; + Require(sceKernelPreadv(file, gapped, 3, 0) == 5); + Require(std::memcmp(first, "01", 2) == 0 && std::memcmp(second, "ABC", 3) == 0); + Require(sceKernelLseek(file, 2, 0) == 2); + Require(sceKernelReadv(file, gapped, 3) == 5); + Require(std::memcmp(first, "AB", 2) == 0 && std::memcmp(second, "CDE", 3) == 0); + Require(sceKernelLseek(file, 0, 1) == 7); + GuestIovec empty[1] = {{nullptr, 0}}; Require(sceKernelReadv(file, empty, 1) == 0); Require(sceKernelReadv(file, reads, 0) == 0); @@ -106,6 +125,17 @@ int main() { Require(sceKernelPreadv(file, reads, 2, 0) == ErrorEbadf); Require(sceKernelPwritev(file, writes, 2, 0) == ErrorEbadf); + int ends[2] = {}; + Require(MakePipe(ends) == 0); + char xyz[] = "xyz"; + GuestIovec message[1] = {{xyz, 3}}; + Require(sceKernelWritev(ends[1], message, 1) == 3); + Require(sceKernelReadv(ends[0], reads, 2) == 3); + Require(std::memcmp(first, "xyz", 3) == 0); + Require(sceKernelPreadv(ends[0], reads, 2, 0) == ErrorEspipe); + Require(sceKernelPwritev(ends[1], message, 1, 0) == ErrorEspipe); + Require(ClosePipe(ends[0]) == 0 && ClosePipe(ends[1]) == 0); + std::filesystem::remove_all(root); return 0; }