fix(libc): preserve 64-bit guest stream offsets

This commit is contained in:
SamG-Coder
2026-09-25 07:00:45 +10:00
parent 39b81364b7
commit 79edb20f17
3 changed files with 68 additions and 9 deletions
+28 -9
View File
@@ -6,6 +6,7 @@
#include <filesystem>
#include <limits>
#include <utility>
#include <cerrno>
#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));
+23
View File
@@ -5,6 +5,11 @@
#include <cstdarg>
#include <cstdint>
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();
}
+17
View File
@@ -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.