From 79edb20f17283422c56eebbc01969d18f5f47b56 Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Fri, 25 Sep 2026 07:00:45 +1000 Subject: [PATCH] fix(libc): preserve 64-bit guest stream offsets --- core/libs/prx/libc/src/FileAndHeap.cpp | 37 ++++++++++++++++++------ core/libs/tests/GuestStandardStreams.cpp | 23 +++++++++++++++ docs/YAMAGI_QUAKE2.md | 17 +++++++++++ 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/core/libs/prx/libc/src/FileAndHeap.cpp b/core/libs/prx/libc/src/FileAndHeap.cpp index adfaff080..2bc2d6cb7 100644 --- a/core/libs/prx/libc/src/FileAndHeap.cpp +++ b/core/libs/prx/libc/src/FileAndHeap.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "prx/libc/include/FileStream.hpp" #include "prx/libc/include/ApplicationHeap.hpp" @@ -70,19 +71,37 @@ size_t APS5_VABI fwrite_nid_postfix(const void* buffer, size_t size, size_t coun return result; } -int APS5_VABI fseek_nid_postfix(FileStream* stream, long offset, int origin) { - if (origin != SEEK_SET && origin != SEEK_CUR && origin != SEEK_END) throw std::runtime_error("fseek: invalid origin"); - if (std::fseek(GetNativeStream(stream), offset, origin) != 0) throw std::runtime_error("fseek: seek failed"); +int APS5_VABI fseeko_nid_postfix(FileStream* stream, std::int64_t offset, int origin) { + if (origin != SEEK_SET && origin != SEEK_CUR && origin != SEEK_END) { errno = 22; return -1; } +#ifdef _WIN32 + const int result = _fseeki64(GetNativeStream(stream), offset, origin); +#else + static_assert(sizeof(off_t) == 8); + const int result = ::fseeko(GetNativeStream(stream), offset, origin); +#endif + const int nativeError = errno; stream->SyncStatus(); - return 0; -} - -long APS5_VABI ftell_nid_postfix(FileStream* stream) { - const auto result = std::ftell(GetNativeStream(stream)); - if (result == -1L) throw std::runtime_error("ftell: position query failed"); + if (result) errno = nativeError == EOVERFLOW ? 84 : nativeError; return result; } +std::int64_t APS5_VABI ftello_nid_postfix(FileStream* stream) { +#ifdef _WIN32 + const auto result = _ftelli64(GetNativeStream(stream)); +#else + static_assert(sizeof(off_t) == 8); + const auto result = ::ftello(GetNativeStream(stream)); +#endif + if (result == -1 && errno == EOVERFLOW) errno = 84; + return result; +} + +int APS5_VABI fseek_nid_postfix(FileStream* stream, std::int64_t offset, int origin) { + return fseeko_nid_postfix(stream, offset, origin); +} + +std::int64_t APS5_VABI ftell_nid_postfix(FileStream* stream) { return ftello_nid_postfix(stream); } + int APS5_VABI fputs_nid_postfix(const char* str, FileStream* stream) { if (!str) throw std::runtime_error("fputs: null string"); const int result = std::fputs(str, GetNativeStream(stream)); diff --git a/core/libs/tests/GuestStandardStreams.cpp b/core/libs/tests/GuestStandardStreams.cpp index add7614aa..f4d85607e 100644 --- a/core/libs/tests/GuestStandardStreams.cpp +++ b/core/libs/tests/GuestStandardStreams.cpp @@ -5,6 +5,11 @@ #include #include extern "C" { +int APS5_VABI fseeko_nid_postfix(FileStream*, std::int64_t, int); +std::int64_t APS5_VABI ftello_nid_postfix(FileStream*); +int APS5_VABI fseek_nid_postfix(FileStream*, std::int64_t, int); +std::int64_t APS5_VABI ftell_nid_postfix(FileStream*); +int* APS5_VABI __error_nid_postfix(); extern FileStream* __stdinp_nid_postfix; extern FileStream* __stdoutp_nid_postfix; extern FileStream* __stderrp_nid_postfix; @@ -79,4 +84,22 @@ int main() { Require(fgets_nid_postfix(output, sizeof(output), &formatted) == output); Require(std::strcmp(output, " 3.50:end") == 0); formatted.Close(); + + FileStream positioned(std::tmpfile()); + constexpr std::int64_t largeOffset = INT64_C(4294967313); + Require(fseeko_nid_postfix(&positioned, largeOffset, SEEK_SET) == 0); + Require(ftello_nid_postfix(&positioned) == largeOffset); + Require(ftell_nid_postfix(&positioned) == largeOffset); + Require(fseek_nid_postfix(&positioned, -9, SEEK_CUR) == 0); + Require(ftello_nid_postfix(&positioned) == largeOffset - 9); + Require(fseek_nid_postfix(&positioned, largeOffset, SEEK_SET) == 0); + Require(ftell_nid_postfix(&positioned) == largeOffset); + Require(fseeko_nid_postfix(&positioned, 0, 12345) == -1 && *__error_nid_postfix() == 22); + Require(ftello_nid_postfix(&positioned) == largeOffset); + Require(fseeko_nid_postfix(&positioned, 0, SEEK_END) == 0); + Require(ftello_nid_postfix(&positioned) == 0); // Seeking alone did not extend the file. + Require(fgetc_nid_postfix(&positioned) == EOF && feof_nid_postfix(&positioned)); + Require(fseeko_nid_postfix(&positioned, 0, SEEK_SET) == 0); + Require(!feof_nid_postfix(&positioned)); + positioned.Close(); } diff --git a/docs/YAMAGI_QUAKE2.md b/docs/YAMAGI_QUAKE2.md index 10495b95a..dcdc3e9a5 100644 --- a/docs/YAMAGI_QUAKE2.md +++ b/docs/YAMAGI_QUAKE2.md @@ -374,3 +374,20 @@ libraries rather than compiling implementation copies into the test executable. All twenty-one suites pass. The unchanged converted game resolves toupper and basename and now stops at `5qP1iVQkdck` (`ftello`). Guest entry-point execution and rendered output remain unverified. + +## Follow-up: 64-bit stream positioning + +Added ftello/fseeko and corrected fseek/ftell to use explicit 64-bit guest +offsets and return values. Windows uses _fseeki64/_ftelli64, avoiding native +32-bit long truncation. Invalid origins and native seek failures return errors +rather than throwing; successful seeks refresh the guest stream status. + +Tests seek past 4 GiB without writing a large file, verify both API pairs, +relative seeks, invalid-origin position preservation, SEEK_END, and clearing +EOF after seeking. All twenty-one suites pass and libraries rebuild. + +Latest unchanged-game startup resolves ftello and stops at `JoBqSQt1yyA` +(`sceSystemServiceLoadExec`). Inspection of the released port's entry source +shows it requests the console's `exit` operation from its quit path. This is +source evidence only; the guest entry point still has not executed and no frame +has been rendered.