diff --git a/core/libs/prx/libc/include/Api.hpp b/core/libs/prx/libc/include/Api.hpp index f895a546228a..1716f3db435a 100644 --- a/core/libs/prx/libc/include/Api.hpp +++ b/core/libs/prx/libc/include/Api.hpp @@ -345,6 +345,8 @@ FileStream* APS5_VABI freopen_nid_postfix(const char* filename, const char* mode int APS5_VABI freopen_s_nid_postfix(FileStream** result, const char* filename, const char* mode, FileStream* stream); FileStream* APS5_VABI fopen_nid_postfix(const char* filename, const char* mode); int APS5_VABI fopen_s_nid_postfix(FileStream** result, const char* filename, const char* mode); +void* APS5_VABI _ZNSt3__113basic_filebufIcNS_11char_traitsIcEEE4openEPKcj_nid_postfix( + void* self, const char* filename, std::uint32_t mode); int APS5_VABI fclose_nid_postfix(FileStream* stream); void APS5_VABI fcloseall_nid_postfix(); FileStream* APS5_VABI _ZSt7_FiopenPKcNSt5_IosbIiE9_OpenmodeEi_nid_postfix(const char* filename, int mode, int protection); diff --git a/core/libs/prx/libc/src/FileAndHeap.cpp b/core/libs/prx/libc/src/FileAndHeap.cpp index 726ba5d99804..af8365d5d3c6 100644 --- a/core/libs/prx/libc/src/FileAndHeap.cpp +++ b/core/libs/prx/libc/src/FileAndHeap.cpp @@ -1076,6 +1076,70 @@ FileStream* APS5_VABI _ZSt7_FiopenPKcNSt5_IosbIiE9_OpenmodeEi_nid_postfix(const return nullptr; } +void* APS5_VABI _ZNSt3__113basic_filebufIcNS_11char_traitsIcEEE4openEPKcj_nid_postfix( + void* self, const char* filename, std::uint32_t mode) { + struct FileBuffer { + std::byte prefix[0x78]; + FileStream* file; + std::byte bufferState[0x108]; + std::uint32_t mode; + }; + static_assert(offsetof(FileBuffer, file) == 0x78 && offsetof(FileBuffer, mode) == 0x188); + + auto* const state = static_cast(self); + FileStream* current = nullptr; + if (state == nullptr || !GuestCopyIn_nid_no_patch(¤t, &state->file, sizeof(current))) + throw std::invalid_argument("libc++ filebuf: unreadable file field"); + if (current != nullptr) + return nullptr; + + constexpr std::uint32_t App = 0x01, Ate = 0x02, Binary = 0x04, In = 0x08, Out = 0x10, + Trunc = 0x20, Noreplace = 0x40; + constexpr std::pair modes[]{ + {App, "a"}, {App | Binary, "ab"}, {In, "r"}, {In | App, "a+"}, + {In | Binary, "rb"}, {In | App | Binary, "a+b"}, + {Out, "w"}, {Out | App, "a"}, {Out | Binary, "wb"}, {Out | App | Binary, "ab"}, + {In | Out, "r+"}, {In | Out | App, "a+"}, + {In | Out | Binary, "r+b"}, {In | Out | App | Binary, "a+b"}, + {Out | Trunc, "w"}, {Out | Trunc | Binary, "wb"}, + {In | Out | Trunc, "w+"}, {In | Out | Trunc | Binary, "w+b"}, + {Out | Noreplace, "wx"}, {Out | Noreplace | Binary, "wbx"}, + {Out | Trunc | Noreplace, "wx"}, {Out | Trunc | Noreplace | Binary, "wbx"}, + {In | Out | Trunc | Noreplace, "w+x"}, {In | Out | Trunc | Noreplace | Binary, "w+bx"}, + }; + const char* openMode = nullptr; + for (const auto& [flags, name] : modes) { + if (flags == (mode & ~Ate)) { + openMode = name; + break; + } + } + if (openMode == nullptr) + return nullptr; + + const GuestArena::HostWrite fileAccess(&state->file, sizeof(state->file)); + const GuestArena::HostWrite modeAccess(&state->mode, sizeof(state->mode)); + if (!fileAccess.Open() || !modeAccess.Open() || + !GuestWritable_nid_no_patch(&state->file, sizeof(state->file)) || + !GuestWritable_nid_no_patch(&state->mode, sizeof(state->mode))) + throw std::invalid_argument("libc++ filebuf: unwritable open state"); + + std::unique_ptr file{ + fopen_nid_postfix(filename, openMode), &fclose_nid_postfix, + }; + if (!file) + return nullptr; + + std::memcpy(&state->mode, &mode, sizeof(mode)); + if ((mode & Ate) != 0 && fseek_nid_postfix(file.get(), 0, SEEK_END) != 0) + return nullptr; + + auto* const opened = file.get(); + std::memcpy(&state->file, &opened, sizeof(opened)); + file.release(); + return self; +} + size_t APS5_VABI fread_nid_postfix(void* buffer, size_t size, size_t count, FileStream* stream) { if (size == 0 || count == 0) return 0; if (count > std::numeric_limits::max() / size) { errno = EINVAL; return 0; } diff --git a/core/libs/prx/libc/tests/cmake/guest_libcxx_filebuf_open.cmake b/core/libs/prx/libc/tests/cmake/guest_libcxx_filebuf_open.cmake new file mode 100644 index 000000000000..c656dc876084 --- /dev/null +++ b/core/libs/prx/libc/tests/cmake/guest_libcxx_filebuf_open.cmake @@ -0,0 +1,8 @@ +if(TARGET file_streams_tests) + add_test(NAME guest_libcxx_filebuf_open COMMAND file_streams_tests libcxx_filebuf_open.tmp --libcxx-filebuf-open) + set_tests_properties(guest_libcxx_filebuf_open PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_libcxx_filebuf_open PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$;PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/tests/FileStreams.cpp b/core/libs/tests/FileStreams.cpp index d082b6d72936..560e96f0050d 100644 --- a/core/libs/tests/FileStreams.cpp +++ b/core/libs/tests/FileStreams.cpp @@ -692,6 +692,41 @@ static void ReplaceWithReadOnlyDescriptor(FileStream* stream, const char* path) #endif } +static void CheckLibcxxFilebufOpen(const std::string& base) { + struct FileBuffer { + std::byte prefix[0x78]; + FileStream* file; + std::byte bufferState[0x108]; + std::uint32_t mode; + }; + static_assert(offsetof(FileBuffer, file) == 0x78 && offsetof(FileBuffer, mode) == 0x188); + const auto Open = _ZNSt3__113basic_filebufIcNS_11char_traitsIcEEE4openEPKcj_nid_postfix; + const std::string path = base + ".libcxx-filebuf-open"; + auto* seed = fopen_nid_postfix(path.c_str(), "wb"); + Require(seed != nullptr && fwrite_nid_postfix("abc", 1, 3, seed) == 3 && fclose_nid_postfix(seed) == 0); + FileBuffer buffer{}; + buffer.mode = 0x1234; + Require(Open(&buffer, nullptr, 0) == nullptr && buffer.file == nullptr && buffer.mode == 0x1234); + + Require(Open(&buffer, path.c_str(), 0x0e) == &buffer && buffer.file != nullptr && buffer.mode == 0x0e); + Require(ftell_nid_postfix(buffer.file) == 3); + auto* const original = buffer.file; + Require(Open(&buffer, nullptr, 0x10) == nullptr && buffer.file == original && buffer.mode == 0x0e); + Require(fclose_nid_postfix(buffer.file) == 0); + buffer.file = nullptr; + + Require(Open(&buffer, path.c_str(), 0x54) == nullptr && buffer.file == nullptr && buffer.mode == 0x0e); + Require(Open(&buffer, path.c_str(), 0x01) == &buffer && buffer.file != nullptr && buffer.mode == 0x01); + Require(fputc_nid_postfix('d', buffer.file) == 'd' && fclose_nid_postfix(buffer.file) == 0); + buffer.file = nullptr; + + Require(Open(&buffer, path.c_str(), 0x0c) == &buffer && buffer.file != nullptr && buffer.mode == 0x0c); + char text[4]{}; + Require(fread_nid_postfix(text, 1, sizeof(text), buffer.file) == sizeof(text) && std::memcmp(text, "abcd", 4) == 0); + Require(fclose_nid_postfix(buffer.file) == 0); + Require(std::remove(path.c_str()) == 0); +} + static void CheckExclusiveCppOpen(const std::string& base) { constexpr int Out = 0x02, Binary = 0x80, Noreplace = 0x40; const std::string path = base + ".exclusive"; @@ -2403,6 +2438,10 @@ int main(int argc, char** argv) { if (argc == 3 && std::strcmp(argv[2], "--fcloseall-fclose-race-child") == 0) return FcloseallFcloseRaceChild(argv[1]); if (argc == 3 && std::strcmp(argv[2], "--fcloseall-read-in-progress-child") == 0) return FcloseallReadInProgressChild(); if (argc == 3 && std::strcmp(argv[2], "--freopen-lock-child") == 0) return FreopenLockChild(argv[1]); + if (argc == 3 && std::strcmp(argv[2], "--libcxx-filebuf-open") == 0) { + CheckLibcxxFilebufOpen(argv[1]); + return 0; + } if (argc == 3 && std::strcmp(argv[2], "--owner-locked-close-child") == 0) return OwnerLockedCloseChild(argv[1]); if (argc == 3 && std::strcmp(argv[2], "--closed-reopen-child") == 0) return ClosedStreamReopenChild(argv[1]); if (argc == 3 && std::strcmp(argv[2], "--reopen-snapshot-child") == 0) return ReopenSnapshotChild(argv[1]); diff --git a/docs/dev/debt/changes/feat-libc-filebuf-open.md b/docs/dev/debt/changes/feat-libc-filebuf-open.md new file mode 100644 index 000000000000..78ffc1f3a1ce --- /dev/null +++ b/docs/dev/debt/changes/feat-libc-filebuf-open.md @@ -0,0 +1,5 @@ +# libc + +## add Functional + +- [libc++ file buffer opening](../../../core/libs/prx/libc/src/FileAndHeap.cpp) (libc) - The Azahar import uses the SDK's narrow file buffer layout and open-mode flags. The PS5 Payload SDK v0.42 determines the file and mode offsets and the accepted modes. The firmware provider and full title execution are unverified.