From 55f548d6bcf3cc8c1bc632d4604a52cb901dc1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Mon, 5 Oct 2026 20:28:41 +0200 Subject: [PATCH] feat(libc): implement wcsrtombs_s Co-Authored-By: Claude --- core/libs/prx/libc/CMakeLists.txt | 9 ++++ core/libs/prx/libc/Export.cpp | 6 --- core/libs/prx/libc/src/Multibyte.cpp | 73 ++++++++++++++++++++++++++ core/libs/tests/GuestWcsrtombsS.cpp | 78 ++++++++++++++++++++++++++++ docs/dev/TechnicalDebt.md | 1 + 5 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 core/libs/tests/GuestWcsrtombsS.cpp diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index 070d85afa..f5736578e 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -176,6 +176,15 @@ if(BUILD_TESTING) set_tests_properties(guest_strings PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_wcsrtombs_s_tests ${LIBS_INCLUDE_DIR}/tests/GuestWcsrtombsS.cpp) + target_include_directories(guest_wcsrtombs_s_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_wcsrtombs_s_tests PRIVATE libc) + configure_windows_unwind(guest_wcsrtombs_s_tests) + add_test(NAME guest_wcsrtombs_s COMMAND guest_wcsrtombs_s_tests) + if(WIN32) + set_tests_properties(guest_wcsrtombs_s PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() if(UNIX) add_test_executable(exception_runtime_tests ${LIBS_INCLUDE_DIR}/tests/ExceptionRuntime.cpp) target_include_directories(exception_runtime_tests PRIVATE ${LIBS_INCLUDE_DIR}) diff --git a/core/libs/prx/libc/Export.cpp b/core/libs/prx/libc/Export.cpp index 17128de2e..0c1fcb59f 100644 --- a/core/libs/prx/libc/Export.cpp +++ b/core/libs/prx/libc/Export.cpp @@ -94,12 +94,6 @@ std::uint64_t APS5_VABI libcCyberUnknown03(void) { return 0; } -APS5_EXPORT("79s2tnYQI6I", libcCyberUnknown04); -std::uint64_t APS5_VABI libcCyberUnknown04(void) { - NotImplemented_nid_no_patch("79s2tnYQI6I"); - return 0; -} - APS5_EXPORT("7yMFgcS8EPA", libcCyberUnknown05); std::uint64_t APS5_VABI libcCyberUnknown05(void) { NotImplemented_nid_no_patch("7yMFgcS8EPA"); diff --git a/core/libs/prx/libc/src/Multibyte.cpp b/core/libs/prx/libc/src/Multibyte.cpp index 0ea78acb3..c521ced22 100644 --- a/core/libs/prx/libc/src/Multibyte.cpp +++ b/core/libs/prx/libc/src/Multibyte.cpp @@ -3,6 +3,7 @@ #include #include #include +#include extern "C" { @@ -47,6 +48,78 @@ std::size_t APS5_VABI wcrtomb_nid_postfix(char* destination, std::uint16_t value return 1; } +int APS5_VABI wcsrtombs_s_nid_postfix( + std::size_t* result, char* destination, std::size_t capacity, const std::uint16_t** source, std::size_t limit, + void* state +) { + constexpr int GuestEinval = 22; + constexpr int GuestErange = 34; + constexpr int GuestEilseq = 86; + constexpr auto Failed = static_cast(-1); + if (!result || !source || !state || !*source || (destination && (static_cast(capacity) <= 0 + || static_cast(limit) < 0))) { + if (destination && static_cast(capacity) > 0) destination[0] = '\0'; + if (result) *result = Failed; + return GuestEinval; + } + if (!destination && capacity) { + *result = Failed; + return GuestEinval; + } + const auto* wide = *source; + char bytes[MB_LEN_MAX]; + if (!destination) { + std::size_t total = 0; + for (;; ++wide) { + const auto length = wcrtomb_nid_postfix(bytes, *wide, state); + if (length == Failed) { + *result = Failed; + return GuestEilseq; + } + if (length && bytes[length - 1] == '\0') { + *result = total + length - 1; + return 0; + } + total += length; + } + } + std::size_t written = 0; + while (limit) { + const auto length = wcrtomb_nid_postfix(bytes, *wide, state); + if (length == Failed) { + destination[written] = '\0'; + *result = Failed; + return GuestEilseq; + } + if (length > limit || length > capacity) { + *source = wide; + destination[0] = '\0'; + *result = Failed; + return GuestErange; + } + std::memcpy(destination + written, bytes, length); + written += length; + if (length && destination[written - 1] == '\0') { + *source = nullptr; + *result = written - 1; + return 0; + } + ++wide; + capacity -= length; + if (!capacity) { + *source = wide; + destination[0] = '\0'; + *result = Failed; + return GuestErange; + } + limit -= length; + } + *source = wide; + destination[written] = '\0'; + *result = written; + return 0; +} + std::size_t APS5_VABI mbsrtowcs_nid_postfix(std::uint16_t* destination, const char** source, std::size_t capacity, void*) { if (!destination) return std::strlen(*source); std::size_t converted = 0; diff --git a/core/libs/tests/GuestWcsrtombsS.cpp b/core/libs/tests/GuestWcsrtombsS.cpp new file mode 100644 index 000000000..758ff5d33 --- /dev/null +++ b/core/libs/tests/GuestWcsrtombsS.cpp @@ -0,0 +1,78 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include + +extern "C" int APS5_VABI wcsrtombs_s_nid_postfix( + std::size_t*, char*, std::size_t, const std::uint16_t**, std::size_t, void*); +static void Require(bool value) { if (!value) std::abort(); } + +constexpr auto Failed = static_cast(-1); +constexpr auto Huge = std::size_t{1} << 63; +static const std::uint16_t text[] = {'a', 'b', 'c', 0}; +static const std::uint16_t wide[] = {'a', 0x100, 'b', 0}; + +struct Run { + int status; + std::size_t result = 7; + const std::uint16_t* source; + char out[8]; +}; + +static Run Convert(const std::uint16_t* input, std::size_t capacity, std::size_t limit, bool toBuffer = true) { + Run run{}; + std::memset(run.out, 'x', sizeof(run.out)); + run.source = input; + std::uint64_t state[2] = {}; + run.status = wcsrtombs_s_nid_postfix(&run.result, toBuffer ? run.out : nullptr, capacity, &run.source, limit, state); + return run; +} + +int main() { + auto run = Convert(text, 0, 0, false); + Require(run.status == 0 && run.result == 3 && run.source == text); + run = Convert(wide, 0, Huge, false); + Require(run.status == 86 && run.result == Failed && run.source == wide); + run = Convert(text, 5, 1, false); + Require(run.status == 22 && run.result == Failed && run.source == text); + + run = Convert(text, 8, 8); + Require(run.status == 0 && run.result == 3 && run.source == nullptr && std::memcmp(run.out, "abc\0x", 5) == 0); + run = Convert(text, 4, 4); + Require(run.status == 0 && run.result == 3 && run.source == nullptr && std::memcmp(run.out, "abc\0x", 5) == 0); + run = Convert(text, 8, 2); + Require(run.status == 0 && run.result == 2 && run.source == text + 2 && std::memcmp(run.out, "ab\0x", 4) == 0); + run = Convert(text, 8, 0); + Require(run.status == 0 && run.result == 0 && run.source == text && std::memcmp(run.out, "\0x", 2) == 0); + + run = Convert(text, 3, 8); + Require(run.status == 34 && run.result == Failed && run.source == text + 3 && std::memcmp(run.out, "\0bcx", 4) == 0); + run = Convert(text, 3, 3); + Require(run.status == 34 && run.result == Failed && run.source == text + 3 && std::memcmp(run.out, "\0bcx", 4) == 0); + run = Convert(text, 1, 8); + Require(run.status == 34 && run.result == Failed && run.source == text + 1 && std::memcmp(run.out, "\0x", 2) == 0); + + run = Convert(wide, 8, 8); + Require(run.status == 86 && run.result == Failed && run.source == wide && std::memcmp(run.out, "a\0xx", 4) == 0); + + run = Convert(text, 0, 8); + Require(run.status == 22 && run.result == Failed && run.source == text && run.out[0] == 'x'); + run = Convert(text, Huge, 8); + Require(run.status == 22 && run.result == Failed && run.out[0] == 'x'); + run = Convert(text, 8, Huge); + Require(run.status == 22 && run.result == Failed && run.source == text && run.out[0] == '\0'); + run = Convert(nullptr, 8, 8); + Require(run.status == 22 && run.result == Failed && run.out[0] == '\0'); + + char out[4] = {'x', 'x', 'x', 'x'}; + std::size_t result = 7; + const std::uint16_t* source = text; + std::uint64_t state[2] = {}; + Require(wcsrtombs_s_nid_postfix(nullptr, out, 4, &source, 4, state) == 22 && out[0] == '\0' && source == text); + out[0] = 'x'; + Require(wcsrtombs_s_nid_postfix(&result, out, 4, nullptr, 4, state) == 22 && result == Failed && out[0] == '\0'); + out[0] = 'x'; + result = 7; + Require(wcsrtombs_s_nid_postfix(&result, out, 4, &source, 4, nullptr) == 22 && result == Failed && out[0] == '\0'); + Require(source == text && std::memcmp(out, "\0xxx", 4) == 0); +} diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 02ea84a84..c178995dd 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -104,4 +104,5 @@ Throughout the project, every function at every stage either **does exactly what - The [output modifier](../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) (`mul:2`, `mul:4`, `div:2`) follows the hardware with the IEEE mode off and f32 denormals flushed, the LLVM graphics default; the shader's MODE register is not read, and the hardware ignores the modifier in the other modes. On f16 results it is ignored, as on the hardware while f16 denormals are enabled; with them flushed the hardware applies it. - [libScePlayerInvitationDialog](../../core/libs/prx/libScePlayerInvitationDialog/libScePlayerInvitationDialog.cpp) simulates dialog completion without displaying UI or sending invitations; its parameter ABI remains unverified. - [64-bit LDS atomics](../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) (`ds_*_u64`, `ds_*_b64`, `ds_*_f64`) read and write the two dwords under a workgroup lock stored after the guest LDS, as LDS is a 32-bit array without 64-bit atomics. They are atomic with respect to each other only: 32-bit accesses to the same dwords issued concurrently are not ordered by the lock. 64-bit GDS atomics throw. `ds_min/max(_rtn)_f64` and `ds_cmpst(_rtn)_f64` follow the NaN and signed-zero rules measured for the f32 forms; the f64 forms were not measured on hardware. +- [wcsrtombs_s](../../core/libs/prx/libc/src/Multibyte.cpp) (libc) follows the console's `libc.prx`, but a runtime-constraint violation only returns its error: the default handler there writes the message and a newline to `stderr` before returning, which is not done here. Conversion uses the C locale of `wcrtomb` (`MB_CUR_MAX` 1), so the path where a multibyte character does not fit the remaining space is not reached - [Thread exit destructors](../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) run when a libkernel thread finishes; on the main or a host thread only `_sceLibcInternalThreadDtors` runs them, as `exit` does, else they are dropped