Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/libs/prx/libkernel/File/src/Open.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ std::int64_t APS5_VABI sceKernelRead(int d, void* buf, std::size_t nbytes) {
return n < 0 ? SceKernelError(*__error_nid_postfix()) : n;
}
if (buf == nullptr) {
if (nbytes != 0) return SceIoErrorFromErrno(EFAULT);
if (nbytes != 0) return SceIoErrorFromErrno(DescriptorIsOpen_nid_no_patch(d) ? EFAULT : EBADF);
char empty = 0;
return sceKernelRead(d, &empty, 0);
}
Expand All @@ -332,7 +332,7 @@ std::int64_t APS5_VABI sceKernelWrite(int d, const void* buf, std::size_t nbytes
return n < 0 ? SceKernelError(*__error_nid_postfix()) : n;
}
if (buf == nullptr) {
if (nbytes != 0) return SceIoErrorFromErrno(EFAULT);
if (nbytes != 0) return SceIoErrorFromErrno(DescriptorIsOpen_nid_no_patch(d) ? EFAULT : EBADF);
const char empty = 0;
return sceKernelWrite(d, &empty, 0);
}
Expand Down
48 changes: 32 additions & 16 deletions core/libs/prx/libkernel/File/src/Stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,28 @@ static bool FailPosixPositionedGuestSocket(int descriptor) {
return true;
}

static bool IsPipeDescriptor(int descriptor) {
#ifdef _WIN32
const auto handle = reinterpret_cast<HANDLE>(WithoutParameterHandler([descriptor] { return ::_get_osfhandle(descriptor); }));
return handle != INVALID_HANDLE_VALUE && ::GetFileType(handle) == FILE_TYPE_PIPE;
#else
struct stat status;
return ::fstat(descriptor, &status) == 0 && S_ISFIFO(status.st_mode);
#endif
}

static int PositionedArgumentError(int descriptor, bool nullBuffer, std::int64_t offset) {
if (!nullBuffer && offset >= 0) return 0;
if (!GuestSockets::IsOpen(descriptor) && !DescriptorIsOpen_nid_no_patch(descriptor)) return GUEST_EBADF;
if (GuestSockets::IsOpen(descriptor) || IsPipeDescriptor(descriptor)) return GUEST_ESPIPE;
return offset < 0 ? GUEST_EINVAL : GUEST_EFAULT;
}

static int TransferArgumentError(int descriptor, bool nullBuffer) {
if (!nullBuffer) return 0;
return DescriptorIsOpen_nid_no_patch(descriptor) ? GUEST_EFAULT : GUEST_EBADF;
}

static int SceErrorFromErrno(int error) {
int guest = error > 0 && error <= 34 ? error : error == GUEST_ENOTEMPTY ? error : GUEST_EIO;
if (error == EAGAIN) guest = GUEST_EWOULDBLOCK;
Expand Down Expand Up @@ -957,8 +979,7 @@ int APS5_VABI _open_nid_postfix(const char* path, int flags, ...) {

int64_t APS5_VABI pread_nid_postfix(int d, void* buf, size_t nbytes, int64_t offset) {
if (ExceedsTransferLimit(nbytes)) return PosixFailure(GUEST_EINVAL);
if (buf == nullptr && nbytes != 0) return PosixFailure(GUEST_EFAULT);
if (offset < 0) return PosixFailure(GUEST_EINVAL);
if (const int error = PositionedArgumentError(d, buf == nullptr && nbytes != 0, offset)) return PosixFailure(error);
if (FailPosixPositionedGuestSocket(d)) return -1;
const GuestArena::HostWrite destination(buf, nbytes);
if (!destination.Open()) return PosixFailure(GUEST_EFAULT);
Expand All @@ -971,8 +992,7 @@ int64_t APS5_VABI pread_nid_postfix(int d, void* buf, size_t nbytes, int64_t off

int64_t APS5_VABI pwrite_nid_disambig1_nid_postfix(int d, const void* buf, size_t nbytes, int64_t offset) {
if (ExceedsTransferLimit(nbytes)) return PosixFailure(GUEST_EINVAL);
if (buf == nullptr && nbytes != 0) return PosixFailure(GUEST_EFAULT);
if (offset < 0) return PosixFailure(GUEST_EINVAL);
if (const int error = PositionedArgumentError(d, buf == nullptr && nbytes != 0, offset)) return PosixFailure(error);
if (FailPosixPositionedGuestSocket(d)) return -1;
GuestArena::GuestArenaPrepareHostRead_nid_no_patch(buf, nbytes);
char empty = 0;
Expand All @@ -983,31 +1003,31 @@ int64_t APS5_VABI pwrite_nid_disambig1_nid_postfix(int d, const void* buf, size_
int64_t APS5_VABI read_nid_postfix(int d, void* buf, uint64_t nbytes) {
if (ExceedsTransferLimit(nbytes)) return PosixFailure(GUEST_EINVAL);
if (GuestSockets::IsOpen(d)) return GuestSockets::Read(d, buf, static_cast<size_t>(nbytes));
if (buf == nullptr && nbytes != 0) return PosixFailure(GUEST_EFAULT);
if (const int error = TransferArgumentError(d, buf == nullptr && nbytes != 0)) return PosixFailure(error);
char emptyBuffer = 0;
return PosixResult(sceKernelRead(d, buf == nullptr ? &emptyBuffer : buf, static_cast<size_t>(nbytes)));
}

std::int64_t APS5_VABI _read_nid_postfix(int descriptor, void* buffer, std::size_t count) {
if (ExceedsTransferLimit(count)) return PosixFailure(GUEST_EINVAL);
if (GuestSockets::IsOpen(descriptor)) return GuestSockets::Read(descriptor, buffer, count);
if (buffer == nullptr && count != 0) return PosixFailure(GUEST_EFAULT);
if (const int error = TransferArgumentError(descriptor, buffer == nullptr && count != 0)) return PosixFailure(error);
char emptyBuffer = 0;
return PosixResult(sceKernelRead(descriptor, buffer == nullptr ? &emptyBuffer : buffer, count));
}

int64_t APS5_VABI write_nid_postfix(int d, const char* str, int64_t size) {
if (size < 0) return PosixFailure(GUEST_EINVAL);
if (GuestSockets::IsOpen(d)) return GuestSockets::Write(d, str, static_cast<std::size_t>(size));
if (str == nullptr && size != 0) return PosixFailure(GUEST_EFAULT);
if (const int error = TransferArgumentError(d, str == nullptr && size != 0)) return PosixFailure(error);
const char emptyBuffer = 0;
return PosixResult(sceKernelWrite(d, str == nullptr ? &emptyBuffer : str, static_cast<size_t>(size)));
}

std::int64_t APS5_VABI _write_nid_postfix(int descriptor, const void* buffer, std::size_t count) {
if (ExceedsTransferLimit(count)) return PosixFailure(GUEST_EINVAL);
if (GuestSockets::IsOpen(descriptor)) return GuestSockets::Write(descriptor, buffer, count);
if (buffer == nullptr && count != 0) return PosixFailure(GUEST_EFAULT);
if (const int error = TransferArgumentError(descriptor, buffer == nullptr && count != 0)) return PosixFailure(error);
const char emptyBuffer = 0;
return PosixResult(sceKernelWrite(descriptor, buffer == nullptr ? &emptyBuffer : buffer, count));
}
Expand Down Expand Up @@ -1220,8 +1240,7 @@ static void PrepareWriteIovecs(const KernelIovec* iov, int iovcnt) {
#ifdef _WIN32

int64_t APS5_VABI sceKernelPread(int d, void* buf, size_t nbytes, int64_t offset) {
if (buf == nullptr && nbytes != 0) return SceErrorFromErrno(GUEST_EFAULT);
if (offset < 0) return SceErrorFromErrno(GUEST_EINVAL);
if (const int error = PositionedArgumentError(d, buf == nullptr && nbytes != 0, offset)) return SceErrorFromErrno(error);
if (const auto error = PositionedGuestSocketError(d); error != 0) return error;
const GuestArena::HostWrite destination(buf, nbytes);
if (!destination.Open()) return SceErrorFromErrno(GUEST_EFAULT);
Expand All @@ -1233,8 +1252,7 @@ int64_t APS5_VABI sceKernelPread(int d, void* buf, size_t nbytes, int64_t offset
}

int64_t APS5_VABI sceKernelPwrite(int d, const void* buf, size_t nbytes, int64_t offset) {
if (buf == nullptr && nbytes != 0) return SceErrorFromErrno(GUEST_EFAULT);
if (offset < 0) return SceErrorFromErrno(GUEST_EINVAL);
if (const int error = PositionedArgumentError(d, buf == nullptr && nbytes != 0, offset)) return SceErrorFromErrno(error);
if (const auto error = PositionedGuestSocketError(d); error != 0) return error;
GuestArena::GuestArenaPrepareHostRead_nid_no_patch(buf, nbytes);
char empty = 0;
Expand Down Expand Up @@ -1319,8 +1337,7 @@ int64_t APS5_VABI sceKernelPwritev(int d, const KernelIovec* iov, int iovcnt, in
#else

int64_t APS5_VABI sceKernelPread(int d, void* buf, size_t nbytes, int64_t offset) {
if (buf == nullptr && nbytes != 0) return SceErrorFromErrno(GUEST_EFAULT);
if (offset < 0) return SceErrorFromErrno(GUEST_EINVAL);
if (const int error = PositionedArgumentError(d, buf == nullptr && nbytes != 0, offset)) return SceErrorFromErrno(error);
if (const auto error = PositionedGuestSocketError(d); error != 0) return error;
const GuestArena::HostWrite destination(buf, nbytes);
if (!destination.Open()) return SceErrorFromErrno(GUEST_EFAULT);
Expand All @@ -1331,8 +1348,7 @@ int64_t APS5_VABI sceKernelPread(int d, void* buf, size_t nbytes, int64_t offset
}

int64_t APS5_VABI sceKernelPwrite(int d, const void* buf, size_t nbytes, int64_t offset) {
if (buf == nullptr && nbytes != 0) return SceErrorFromErrno(GUEST_EFAULT);
if (offset < 0) return SceErrorFromErrno(GUEST_EINVAL);
if (const int error = PositionedArgumentError(d, buf == nullptr && nbytes != 0, offset)) return SceErrorFromErrno(error);
if (const auto error = PositionedGuestSocketError(d); error != 0) return error;
GuestArena::GuestArenaPrepareHostRead_nid_no_patch(buf, nbytes);
const auto result = NativePwrite(d, buf, nbytes, offset);
Expand Down
20 changes: 20 additions & 0 deletions core/libs/tests/GuestFilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ int APS5_VABI dup2_nid_postfix(int, int);
std::int64_t APS5_VABI lseek_nid_postfix(int, std::int64_t, int);
std::int64_t APS5_VABI read_nid_postfix(int, void*, std::size_t);
std::int64_t APS5_VABI write_nid_postfix(int, const void*, std::size_t);
std::int64_t APS5_VABI pread_nid_postfix(int, void*, std::size_t, std::int64_t);
std::int64_t APS5_VABI sceKernelWrite(int, const void*, std::size_t);
int APS5_VABI sceKernelDebugOutText(int, const char*);
}
#ifdef _WIN32
Expand Down Expand Up @@ -1037,6 +1039,24 @@ int main() {
Require(lseek_nid_postfix(-1, 0, 7) == -1 && *__error_nid_postfix() == 9);
Require(lseek_nid_postfix(0x7fffffff, 0, -1) == -1 && *__error_nid_postfix() == 9);
Require(lseek_nid_postfix(locked, 0, 7) == -1 && *__error_nid_postfix() == 22);
{
char transfer[4]{};
int transferPipe[2] = {-1, -1};
Require(pipe_nid_postfix(transferPipe) == 0);
Require(read_nid_postfix(-1, nullptr, 1) == -1 && *__error_nid_postfix() == 9);
Require(write_nid_postfix(-1, nullptr, 1) == -1 && *__error_nid_postfix() == 9);
Require(sceKernelRead(-1, nullptr, 1) == static_cast<int>(0x80020009u));
Require(sceKernelWrite(-1, nullptr, 1) == static_cast<int>(0x80020009u));
Require(read_nid_postfix(locked, nullptr, 1) == -1 && *__error_nid_postfix() == 14);
Require(pread_nid_postfix(-1, transfer, 1, -1) == -1 && *__error_nid_postfix() == 9);
Require(pread_nid_postfix(-1, nullptr, 1, 0) == -1 && *__error_nid_postfix() == 9);
Require(sceKernelPread(-1, transfer, 1, -1) == static_cast<int>(0x80020009u));
Require(pread_nid_postfix(transferPipe[0], transfer, 1, -1) == -1 && *__error_nid_postfix() == 29);
Require(pread_nid_postfix(locked, transfer, 1, -1) == -1 && *__error_nid_postfix() == 22);
Require(pread_nid_postfix(locked, nullptr, 1, -1) == -1 && *__error_nid_postfix() == 22);
Require(pread_nid_postfix(locked, nullptr, 1, 0) == -1 && *__error_nid_postfix() == 14);
Require(close_nid_postfix(transferPipe[0]) == 0 && close_nid_postfix(transferPipe[1]) == 0);
}
Require(flock_nid_postfix(-1, 2 | 4) == -1 && *__error_nid_postfix() == 9);
Require(flock_nid_postfix(0x7fffffff, 1) == -1 && *__error_nid_postfix() == 9);
int flockPipe[2] = {-1, -1};
Expand Down
Loading