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
2 changes: 2 additions & 0 deletions core/libs/prx/libc/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
64 changes: 64 additions & 0 deletions core/libs/prx/libc/src/FileAndHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileBuffer*>(self);
FileStream* current = nullptr;
if (state == nullptr || !GuestCopyIn_nid_no_patch(&current, &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<std::uint32_t, const char*> 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<FileStream, decltype(&fclose_nid_postfix)> 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<std::size_t>::max() / size) { errno = EINVAL; return 0; }
Expand Down
Original file line number Diff line number Diff line change
@@ -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:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
endif()
39 changes: 39 additions & 0 deletions core/libs/tests/FileStreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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]);
Expand Down
5 changes: 5 additions & 0 deletions docs/dev/debt/changes/feat-libc-filebuf-open.md
Original file line number Diff line number Diff line change
@@ -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.
Loading