feat(libc): expose guest standard streams and character IO

This commit is contained in:
SamG-Coder
2026-09-25 06:30:30 +10:00
parent 56c7a589db
commit e8ecb29170
7 changed files with 191 additions and 0 deletions
+9
View File
@@ -104,6 +104,15 @@ endforeach()
add_custom_target(libs DEPENDS ${PATCHED_OUTPUTS})
if(BUILD_TESTING)
add_executable(guest_standard_stream_tests tests/GuestStandardStreams.cpp)
target_include_directories(guest_standard_stream_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_standard_stream_tests PRIVATE libc)
configure_windows_unwind(guest_standard_stream_tests)
add_test(NAME guest_standard_streams COMMAND guest_standard_stream_tests)
if(WIN32)
set_tests_properties(guest_standard_streams PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_executable(guest_signal_tests tests/GuestSignals.cpp)
target_include_directories(guest_signal_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_signal_tests PRIVATE libkernel libc)
+1
View File
@@ -8,6 +8,7 @@ target_sources(libc PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/Math.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Time.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/FileAndHeap.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/StandardStreams.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/GuestAllocations.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/GuestHeap.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Mspace.cpp
+44
View File
@@ -4,6 +4,28 @@
#include <cstdio>
#include <stdexcept>
#include <utility>
#include <cstdint>
#include <cstddef>
#include <type_traits>
#ifdef _WIN32
#include <io.h>
#endif
struct GuestFilePrefix {
unsigned char* position = nullptr;
std::int32_t readRemaining = 0;
std::int32_t writeRemaining = 0;
std::int16_t flags = 0;
std::int16_t descriptor = -1;
unsigned char* buffer = nullptr;
std::int32_t bufferSize = 0;
std::int32_t bufferPadding = 0;
std::int32_t lineBufferSize = 0;
};
static_assert(offsetof(GuestFilePrefix, flags) == 16);
static_assert(offsetof(GuestFilePrefix, descriptor) == 18);
static_assert(offsetof(GuestFilePrefix, buffer) == 24);
static_assert(offsetof(GuestFilePrefix, lineBufferSize) == 40);
static constexpr const char* FOPEN_EXT_VERT = ".vert";
static constexpr const char* FOPEN_MSG_NULL_ARG = "null argument";
@@ -11,12 +33,23 @@ static constexpr const char* FOPEN_MSG_NOT_FOUND = "file not found";
static constexpr const char* FOPEN_MSG_OPEN_FAILED = "open failed";
class FileStream {
// Only the macro-accessed FreeBSD prefix is exposed. The remaining guest
// FILE area is reserved; host state lives beyond it and is never a guest FILE*.
GuestFilePrefix _guest{};
std::byte _reserved[256 - sizeof(GuestFilePrefix)]{};
std::FILE* _handle;
bool _dynamic;
public:
explicit FileStream(std::FILE* handle, bool dynamic = false) : _handle(handle), _dynamic(dynamic) {
if (!_handle) throw std::runtime_error("FileStream: null handle");
_guest.flags = handle == stdin ? 4 : handle == stdout || handle == stderr ? 8 : 0x10;
#ifdef _WIN32
const int descriptor = _fileno(handle);
#else
const int descriptor = ::fileno(handle);
#endif
_guest.descriptor = descriptor >= 0 && descriptor <= 32767 ? static_cast<std::int16_t>(descriptor) : -1;
}
FileStream(const FileStream&) = delete;
@@ -31,11 +64,22 @@ public:
return _dynamic;
}
GuestFilePrefix& GuestState() { return _guest; }
void SyncStatus() {
_guest.readRemaining = 0;
_guest.writeRemaining = 0;
_guest.flags = static_cast<std::int16_t>((_guest.flags & ~0x60) |
(std::feof(GetHandle()) ? 0x20 : 0) | (std::ferror(GetHandle()) ? 0x40 : 0));
}
void Close() {
GetHandle();
if (std::fclose(std::exchange(_handle, nullptr)) != 0) throw std::runtime_error("FileStream: close failed");
_guest.flags = 0;
_guest.descriptor = -1;
}
};
static_assert(std::is_standard_layout_v<FileStream>);
inline std::FILE* GetNativeStream(FileStream* stream) {
if (!stream) throw std::runtime_error("FileStream: null stream");
+3
View File
@@ -55,6 +55,7 @@ size_t APS5_VABI fread_nid_postfix(void* buffer, size_t size, size_t count, File
if (size == 0 || count == 0) return 0;
if (!buffer) throw std::runtime_error("fread: null buffer");
const auto result = std::fread(buffer, size, count, handle);
stream->SyncStatus();
if (std::ferror(handle)) throw std::runtime_error("fread: read failed");
return result;
}
@@ -64,6 +65,7 @@ size_t APS5_VABI fwrite_nid_postfix(const void* buffer, size_t size, size_t coun
if (size == 0 || count == 0) return 0;
if (!buffer) throw std::runtime_error("fwrite: null buffer");
const auto result = std::fwrite(buffer, size, count, handle);
stream->SyncStatus();
if (result != count || std::ferror(handle)) throw std::runtime_error("fwrite: write failed");
return result;
}
@@ -71,6 +73,7 @@ size_t APS5_VABI fwrite_nid_postfix(const void* buffer, size_t size, size_t coun
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");
stream->SyncStatus();
return 0;
}
@@ -0,0 +1,68 @@
#include "prx/libc/include/FileStream.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
namespace { FileStream input{stdin}; }
extern "C" {
FileStream* __stdinp_nid_postfix = &input;
FileStream* __stdoutp_nid_postfix = &_Stdout_nid_postfix;
FileStream* __stderrp_nid_postfix = &_Stderr_nid_postfix;
int __isthreaded_nid_postfix = 1;
int APS5_VABI fgetc_nid_postfix(FileStream* stream) {
const int result = std::fgetc(GetNativeStream(stream));
stream->SyncStatus();
return result;
}
int APS5_VABI getc_nid_postfix(FileStream* stream) { return fgetc_nid_postfix(stream); }
int APS5_VABI __srget_nid_postfix(FileStream* stream) { return fgetc_nid_postfix(stream); }
int APS5_VABI getchar_nid_postfix() { return fgetc_nid_postfix(__stdinp_nid_postfix); }
int APS5_VABI fputc_nid_postfix(int value, FileStream* stream) {
const int result = std::fputc(value, GetNativeStream(stream));
stream->SyncStatus();
return result;
}
int APS5_VABI putc_nid_postfix(int value, FileStream* stream) { return fputc_nid_postfix(value, stream); }
int APS5_VABI __swbuf_nid_postfix(int value, FileStream* stream) { return fputc_nid_postfix(value, stream); }
int APS5_VABI putchar_nid_postfix(int value) { return fputc_nid_postfix(value, __stdoutp_nid_postfix); }
int APS5_VABI ungetc_nid_postfix(int value, FileStream* stream) {
const int result = std::ungetc(value, GetNativeStream(stream));
stream->SyncStatus();
return result;
}
char* APS5_VABI fgets_nid_postfix(char* buffer, int size, FileStream* stream) {
auto* result = std::fgets(buffer, size, GetNativeStream(stream));
stream->SyncStatus();
return result;
}
int APS5_VABI feof_nid_postfix(FileStream* stream) {
stream->SyncStatus();
return (stream->GuestState().flags & 0x20) != 0;
}
int APS5_VABI ferror_nid_postfix(FileStream* stream) {
stream->SyncStatus();
return (stream->GuestState().flags & 0x40) != 0;
}
void APS5_VABI clearerr_nid_postfix(FileStream* stream) {
std::clearerr(GetNativeStream(stream));
stream->SyncStatus();
}
int APS5_VABI fileno_nid_postfix(FileStream* stream) {
#ifdef _WIN32
const int descriptor = _fileno(GetNativeStream(stream));
#else
const int descriptor = ::fileno(GetNativeStream(stream));
#endif
stream->GuestState().descriptor = static_cast<std::int16_t>(descriptor);
return descriptor;
}
int APS5_VABI setvbuf_nid_postfix(FileStream* stream, char* buffer, int mode, std::size_t size) {
if (mode < 0 || mode > 2) return -1;
const int native = mode == 0 ? _IOFBF : mode == 1 ? _IOLBF : _IONBF;
return std::setvbuf(GetNativeStream(stream), buffer, native, size);
}
}
+49
View File
@@ -0,0 +1,49 @@
#include "prx/libc/include/FileStream.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <cstdlib>
#include <cstring>
extern "C" {
extern FileStream* __stdinp_nid_postfix;
extern FileStream* __stdoutp_nid_postfix;
extern FileStream* __stderrp_nid_postfix;
extern int __isthreaded_nid_postfix;
int APS5_VABI fgetc_nid_postfix(FileStream*);
int APS5_VABI fputc_nid_postfix(int, FileStream*);
int APS5_VABI __srget_nid_postfix(FileStream*);
int APS5_VABI __swbuf_nid_postfix(int, FileStream*);
int APS5_VABI ungetc_nid_postfix(int, FileStream*);
char* APS5_VABI fgets_nid_postfix(char*, int, FileStream*);
int APS5_VABI feof_nid_postfix(FileStream*);
int APS5_VABI fileno_nid_postfix(FileStream*);
void APS5_VABI clearerr_nid_postfix(FileStream*);
int APS5_VABI setvbuf_nid_postfix(FileStream*, char*, int, std::size_t);
}
static void Require(bool value) { if (!value) std::abort(); }
int main() {
Require(__isthreaded_nid_postfix == 1);
Require(__stdoutp_nid_postfix == &_Stdout_nid_postfix);
Require(__stderrp_nid_postfix == &_Stderr_nid_postfix);
Require(fileno_nid_postfix(__stdinp_nid_postfix) == 0);
Require(fileno_nid_postfix(__stdoutp_nid_postfix) == 1);
Require(fileno_nid_postfix(__stderrp_nid_postfix) == 2);
FileStream stream(std::tmpfile());
auto& guest = *reinterpret_cast<GuestFilePrefix*>(&stream);
Require(&guest == &stream.GuestState());
Require(guest.position == nullptr && guest.readRemaining == 0 && guest.writeRemaining == 0);
Require(guest.descriptor == fileno_nid_postfix(&stream));
Require(setvbuf_nid_postfix(&stream, nullptr, 2, 0) == 0);
Require(fputc_nid_postfix('A', &stream) == 'A');
Require(--guest.writeRemaining < 0 && __swbuf_nid_postfix('\n', &stream) == '\n');
std::rewind(stream.GetHandle());
Require(--guest.readRemaining < 0 && __srget_nid_postfix(&stream) == 'A');
Require(ungetc_nid_postfix('B', &stream) == 'B');
char text[8]{};
Require(fgets_nid_postfix(text, sizeof(text), &stream) == text);
Require(std::strcmp(text, "B\n") == 0);
Require(fgetc_nid_postfix(&stream) == EOF);
Require(feof_nid_postfix(&stream) && (guest.flags & 0x20));
clearerr_nid_postfix(&stream);
Require(!feof_nid_postfix(&stream) && !(guest.flags & 0x20));
stream.Close();
Require(guest.flags == 0 && guest.descriptor == -1);
}
+17
View File
@@ -200,3 +200,20 @@ All thirteen suites pass. Latest game startup resolves these imports and stops
at `zqJhBxAKfsc` (`__stdoutp`). The next work requires the guest FILE layout and
standard stream exports; mapping a host FILE pointer directly would be invalid.
The game still has not executed its entry point or rendered a frame.
## Follow-up: standard stream globals and character I/O
Added __stdinp, __stdoutp, __stderrp, and __isthreaded exports. FileStream now
starts with the verified macro-accessed guest FILE prefix (position, read/write
counts, flags, descriptor, and buffer fields); native stream state follows a
reserved guest area. This is not a complete implementation of FreeBSD's private
FILE operations, locking fields, or wide-stream state.
Added character input/output, __srget/__swbuf fallbacks, fgets, ungetc, EOF/error
queries, clearerr, fileno, and buffering-mode translation. Existing fread/fwrite
and fseek now refresh the guest status fields. Tests exercise stream globals,
field offsets, inline-macro fallback paths, temporary-file I/O, EOF, ungetc,
and closure. All fourteen suites pass and the compatibility libraries rebuild.
Latest startup resolves the stream globals and stops at `8nY19bKoiZk` (`fcntl`).
The game has not reached its entry point or displayed a frame.