feat(libc): implement wcsrtombs_s

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Adrià Franch
2026-10-05 20:28:41 +02:00
parent fdd6519eac
commit 55f548d6bc
5 changed files with 161 additions and 6 deletions
+9
View File
@@ -176,6 +176,15 @@ if(BUILD_TESTING)
set_tests_properties(guest_strings PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
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:$<TARGET_FILE_DIR:libc>")
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})
-6
View File
@@ -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");
+73
View File
@@ -3,6 +3,7 @@
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <climits>
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<std::size_t>(-1);
if (!result || !source || !state || !*source || (destination && (static_cast<std::int64_t>(capacity) <= 0
|| static_cast<std::int64_t>(limit) < 0))) {
if (destination && static_cast<std::int64_t>(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;
+78
View File
@@ -0,0 +1,78 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <cstdint>
#include <cstdlib>
#include <cstring>
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<std::size_t>(-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);
}
+1
View File
@@ -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