mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UTjpbMX7BUFMZZaxJE98zV
This commit is contained in:
committed by
Adrià Franch
parent
a38df0f020
commit
87f4444441
@@ -132,6 +132,10 @@ static std::int64_t NativeTransfer(int descriptor, void* buf, std::size_t nbytes
|
||||
}
|
||||
return write ? ::_write(descriptor, buf, static_cast<unsigned int>(nbytes)) : ::_read(descriptor, buf, static_cast<unsigned int>(nbytes));
|
||||
}
|
||||
static bool NativeIsDisk(int descriptor) {
|
||||
const auto handle = reinterpret_cast<HANDLE>(::_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::size_t>(std::numeric_limits<std::int64_t>::max()) - total) return SceErrorFromErrno(GUEST_EINVAL);
|
||||
total += iov[i].length;
|
||||
}
|
||||
if (offset != nullptr && total > static_cast<std::size_t>(std::numeric_limits<std::int64_t>::max() - *offset)) return SceErrorFromErrno(GUEST_EINVAL);
|
||||
std::deque<GuestArena::HostWrite> destinations;
|
||||
if (!write && !OpenIovecs(iov, iovcnt, destinations)) return SceErrorFromErrno(GUEST_EFAULT);
|
||||
std::vector<char> buffer(std::max<std::size_t>(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<char*>(iov[i].base);
|
||||
for (std::size_t position = 0; position < iov[i].length;) {
|
||||
const auto chunk = std::min<std::size_t>(iov[i].length - position, std::numeric_limits<int>::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<std::size_t>(result);
|
||||
if (static_cast<std::size_t>(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<std::size_t>(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) {
|
||||
|
||||
@@ -10,6 +10,16 @@
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#ifdef _WIN32
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
static int MakePipe(int* ends) { return ::_pipe(ends, 64, _O_BINARY); }
|
||||
static int ClosePipe(int end) { return ::_close(end); }
|
||||
#else
|
||||
#include <unistd.h>
|
||||
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<int>(0x80020009u);
|
||||
static constexpr std::int64_t ErrorEfault = static_cast<int>(0x8002000Eu);
|
||||
static constexpr std::int64_t ErrorEinval = static_cast<int>(0x80020016u);
|
||||
static constexpr std::int64_t ErrorEspipe = static_cast<int>(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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user