mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(libc): read guest wide strings as 16-bit units
The guest's wchar_t is 16 bits, but wcslen, wcscmp, wcsncmp, wcscpy, wcsncpy, wcschr, wcsrchr, wcsstr, wcspbrk, wcsspn and the wmem functions forwarded to the host's wchar_t functions, which use 32-bit units on Linux. Implement them on char16_t. The wide conversions that still use the host's wchar_t are listed in TechnicalDebt.
This commit is contained in:
@@ -92,6 +92,16 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(guest_wide_formatting PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
|
||||
add_test_executable(guest_wide_string_tests ${LIBS_INCLUDE_DIR}/tests/GuestWideStrings.cpp)
|
||||
target_include_directories(guest_wide_string_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(guest_wide_string_tests PRIVATE libc)
|
||||
configure_windows_unwind(guest_wide_string_tests)
|
||||
add_test(NAME guest_wide_strings COMMAND guest_wide_string_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_wide_strings PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_test_executable(guest_character_tests ${LIBS_INCLUDE_DIR}/tests/GuestCharacter.cpp)
|
||||
target_include_directories(guest_character_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(guest_character_tests PRIVATE libc)
|
||||
|
||||
@@ -9,6 +9,27 @@
|
||||
|
||||
#include "prx/libc/include/General.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
int CompareUnits(char16_t left, char16_t right) {
|
||||
return left < right ? -1 : 1;
|
||||
}
|
||||
|
||||
bool Contains(const char16_t* set, char16_t character) {
|
||||
for (; *set != 0; ++set) {
|
||||
if (*set == character) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t Length(const char16_t* s) {
|
||||
size_t length = 0;
|
||||
while (s[length] != 0) ++length;
|
||||
return length;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
void* APS5_VABI memset_nid_postfix(void* s, int c, size_t n) {
|
||||
@@ -117,20 +138,28 @@ std::div_t APS5_VABI div_nid_postfix(int numerator, int denominator) {
|
||||
return std::div(numerator, denominator);
|
||||
}
|
||||
|
||||
const wchar_t* APS5_VABI wmemchr_nid_postfix(const wchar_t* s, wchar_t c, size_t n) {
|
||||
return std::wmemchr(s, c, n);
|
||||
const char16_t* APS5_VABI wmemchr_nid_postfix(const char16_t* s, char16_t c, size_t n) {
|
||||
for (; n != 0; ++s, --n) {
|
||||
if (*s == c) return s;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int APS5_VABI wmemcmp_nid_postfix(const wchar_t* s1, const wchar_t* s2, size_t n) {
|
||||
return std::wmemcmp(s1, s2, n);
|
||||
int APS5_VABI wmemcmp_nid_postfix(const char16_t* s1, const char16_t* s2, size_t n) {
|
||||
for (; n != 0; ++s1, ++s2, --n) {
|
||||
if (*s1 != *s2) return CompareUnits(*s1, *s2);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
wchar_t* APS5_VABI wmemcpy_nid_postfix(wchar_t* dest, const wchar_t* src, size_t n) {
|
||||
return std::wmemcpy(dest, src, n);
|
||||
char16_t* APS5_VABI wmemcpy_nid_postfix(char16_t* dest, const char16_t* src, size_t n) {
|
||||
if (n != 0) std::memcpy(dest, src, n * sizeof(char16_t));
|
||||
return dest;
|
||||
}
|
||||
|
||||
wchar_t* APS5_VABI wmemmove_nid_postfix(wchar_t* dest, const wchar_t* src, size_t n) {
|
||||
return std::wmemmove(dest, src, n);
|
||||
char16_t* APS5_VABI wmemmove_nid_postfix(char16_t* dest, const char16_t* src, size_t n) {
|
||||
if (n != 0) std::memmove(dest, src, n * sizeof(char16_t));
|
||||
return dest;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -274,48 +303,79 @@ unsigned long long APS5_VABI _Stoull_nid_postfix(const char* str, char** endptr,
|
||||
return std::strtoull(str, endptr, base);
|
||||
}
|
||||
|
||||
size_t APS5_VABI wcslen_nid_postfix(const wchar_t* s) {
|
||||
return std::wcslen(s);
|
||||
size_t APS5_VABI wcslen_nid_postfix(const char16_t* s) {
|
||||
return Length(s);
|
||||
}
|
||||
|
||||
int APS5_VABI wcscmp_nid_postfix(const wchar_t* s1, const wchar_t* s2) {
|
||||
return std::wcscmp(s1, s2);
|
||||
int APS5_VABI wcscmp_nid_postfix(const char16_t* s1, const char16_t* s2) {
|
||||
for (; *s1 == *s2; ++s1, ++s2) {
|
||||
if (*s1 == 0) return 0;
|
||||
}
|
||||
return CompareUnits(*s1, *s2);
|
||||
}
|
||||
|
||||
int APS5_VABI wcsncmp_nid_postfix(const wchar_t* s1, const wchar_t* s2, size_t n) {
|
||||
return std::wcsncmp(s1, s2, n);
|
||||
int APS5_VABI wcsncmp_nid_postfix(const char16_t* s1, const char16_t* s2, size_t n) {
|
||||
for (; n != 0; ++s1, ++s2, --n) {
|
||||
if (*s1 != *s2) return CompareUnits(*s1, *s2);
|
||||
if (*s1 == 0) return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
wchar_t* APS5_VABI wcscpy_nid_postfix(wchar_t* dest, const wchar_t* src) {
|
||||
return std::wcscpy(dest, src);
|
||||
char16_t* APS5_VABI wcscpy_nid_postfix(char16_t* dest, const char16_t* src) {
|
||||
std::memcpy(dest, src, (Length(src) + 1) * sizeof(char16_t));
|
||||
return dest;
|
||||
}
|
||||
|
||||
wchar_t* APS5_VABI wcsncpy_nid_postfix(wchar_t* dest, const wchar_t* src, size_t n) {
|
||||
return std::wcsncpy(dest, src, n);
|
||||
char16_t* APS5_VABI wcsncpy_nid_postfix(char16_t* dest, const char16_t* src, size_t n) {
|
||||
size_t index = 0;
|
||||
for (; index < n && src[index] != 0; ++index) dest[index] = src[index];
|
||||
for (; index < n; ++index) dest[index] = 0;
|
||||
return dest;
|
||||
}
|
||||
|
||||
const wchar_t* APS5_VABI wcschr_nid_postfix(const wchar_t* s, wchar_t c) {
|
||||
return std::wcschr(s, c);
|
||||
const char16_t* APS5_VABI wcschr_nid_postfix(const char16_t* s, char16_t c) {
|
||||
for (;; ++s) {
|
||||
if (*s == c) return s;
|
||||
if (*s == 0) return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
const wchar_t* APS5_VABI wcsrchr_nid_postfix(const wchar_t* s, wchar_t c) {
|
||||
return std::wcsrchr(s, c);
|
||||
const char16_t* APS5_VABI wcsrchr_nid_postfix(const char16_t* s, char16_t c) {
|
||||
const char16_t* found = nullptr;
|
||||
for (;; ++s) {
|
||||
if (*s == c) found = s;
|
||||
if (*s == 0) return found;
|
||||
}
|
||||
}
|
||||
|
||||
const wchar_t* APS5_VABI wcsstr_nid_postfix(const wchar_t* haystack, const wchar_t* needle) {
|
||||
return std::wcsstr(haystack, needle);
|
||||
const char16_t* APS5_VABI wcsstr_nid_postfix(const char16_t* haystack, const char16_t* needle) {
|
||||
const size_t length = Length(needle);
|
||||
if (length == 0) return haystack;
|
||||
for (; *haystack != 0; ++haystack) {
|
||||
size_t matched = 0;
|
||||
while (matched < length && haystack[matched] == needle[matched]) ++matched;
|
||||
if (matched == length) return haystack;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const wchar_t* APS5_VABI wcspbrk_nid_postfix(const wchar_t* s, const wchar_t* accept) {
|
||||
return std::wcspbrk(s, accept);
|
||||
const char16_t* APS5_VABI wcspbrk_nid_postfix(const char16_t* s, const char16_t* accept) {
|
||||
for (; *s != 0; ++s) {
|
||||
if (Contains(accept, *s)) return s;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t APS5_VABI wcsspn_nid_postfix(const wchar_t* s, const wchar_t* accept) {
|
||||
return std::wcsspn(s, accept);
|
||||
size_t APS5_VABI wcsspn_nid_postfix(const char16_t* s, const char16_t* accept) {
|
||||
size_t count = 0;
|
||||
while (s[count] != 0 && Contains(accept, s[count])) ++count;
|
||||
return count;
|
||||
}
|
||||
|
||||
wchar_t* APS5_VABI wmemset_nid_postfix(wchar_t* s, wchar_t c, size_t n) {
|
||||
return std::wmemset(s, c, n);
|
||||
char16_t* APS5_VABI wmemset_nid_postfix(char16_t* s, char16_t c, size_t n) {
|
||||
for (size_t index = 0; index < n; ++index) s[index] = c;
|
||||
return s;
|
||||
}
|
||||
|
||||
double APS5_VABI wcstod_nid_postfix(const wchar_t* str, wchar_t** endptr) {
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
|
||||
extern "C" {
|
||||
const char16_t* APS5_VABI wmemchr_nid_postfix(const char16_t* s, char16_t c, std::size_t n);
|
||||
int APS5_VABI wmemcmp_nid_postfix(const char16_t* s1, const char16_t* s2, std::size_t n);
|
||||
char16_t* APS5_VABI wmemcpy_nid_postfix(char16_t* dest, const char16_t* src, std::size_t n);
|
||||
char16_t* APS5_VABI wmemmove_nid_postfix(char16_t* dest, const char16_t* src, std::size_t n);
|
||||
std::size_t APS5_VABI wcslen_nid_postfix(const char16_t* s);
|
||||
int APS5_VABI wcscmp_nid_postfix(const char16_t* s1, const char16_t* s2);
|
||||
int APS5_VABI wcsncmp_nid_postfix(const char16_t* s1, const char16_t* s2, std::size_t n);
|
||||
char16_t* APS5_VABI wcscpy_nid_postfix(char16_t* dest, const char16_t* src);
|
||||
char16_t* APS5_VABI wcsncpy_nid_postfix(char16_t* dest, const char16_t* src, std::size_t n);
|
||||
const char16_t* APS5_VABI wcschr_nid_postfix(const char16_t* s, char16_t c);
|
||||
const char16_t* APS5_VABI wcsrchr_nid_postfix(const char16_t* s, char16_t c);
|
||||
const char16_t* APS5_VABI wcsstr_nid_postfix(const char16_t* haystack, const char16_t* needle);
|
||||
const char16_t* APS5_VABI wcspbrk_nid_postfix(const char16_t* s, const char16_t* accept);
|
||||
std::size_t APS5_VABI wcsspn_nid_postfix(const char16_t* s, const char16_t* accept);
|
||||
char16_t* APS5_VABI wmemset_nid_postfix(char16_t* s, char16_t c, std::size_t n);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
void require(bool condition) {
|
||||
if (!condition) std::abort();
|
||||
}
|
||||
|
||||
bool same(const char16_t* left, const char16_t* right, std::size_t count) {
|
||||
for (std::size_t index = 0; index < count; ++index) {
|
||||
if (left[index] != right[index]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
const char16_t split[] = {u'a', u'b', 0, u'c', 0};
|
||||
require(wcslen_nid_postfix(u"") == 0);
|
||||
require(wcslen_nid_postfix(u"héllo") == 5);
|
||||
require(wcslen_nid_postfix(split) == 2);
|
||||
|
||||
require(wcscmp_nid_postfix(u"abc", u"abc") == 0);
|
||||
require(wcscmp_nid_postfix(u"abc", u"abd") < 0);
|
||||
require(wcscmp_nid_postfix(u"abd", u"abc") > 0);
|
||||
require(wcscmp_nid_postfix(u"ab", u"abc") < 0);
|
||||
require(wcscmp_nid_postfix(u"", u"a") > 0);
|
||||
require(wcscmp_nid_postfix(u"耀", u"翿") > 0);
|
||||
require(wcsncmp_nid_postfix(u"abcx", u"abcy", 3) == 0);
|
||||
require(wcsncmp_nid_postfix(u"abcx", u"abcy", 4) < 0);
|
||||
require(wcsncmp_nid_postfix(u"ab", u"ab", 10) == 0);
|
||||
require(wcsncmp_nid_postfix(u"a", u"b", 0) == 0);
|
||||
|
||||
char16_t buffer[8];
|
||||
wmemset_nid_postfix(buffer, 0xaaaa, 8);
|
||||
require(wcscpy_nid_postfix(buffer, u"wide") == buffer);
|
||||
require(same(buffer, u"wide", 5) && buffer[5] == 0xaaaa);
|
||||
|
||||
wmemset_nid_postfix(buffer, 0xaaaa, 8);
|
||||
require(wcsncpy_nid_postfix(buffer, u"ab", 5) == buffer);
|
||||
const char16_t padded[] = {u'a', u'b', 0, 0, 0, 0xaaaa};
|
||||
require(same(buffer, padded, 6));
|
||||
wmemset_nid_postfix(buffer, 0xaaaa, 8);
|
||||
wcsncpy_nid_postfix(buffer, u"abcdef", 3);
|
||||
const char16_t truncated[] = {u'a', u'b', u'c', 0xaaaa};
|
||||
require(same(buffer, truncated, 4));
|
||||
|
||||
const char16_t* text = u"a世b世c";
|
||||
require(wcschr_nid_postfix(text, u'世') == text + 1);
|
||||
require(wcsrchr_nid_postfix(text, u'世') == text + 3);
|
||||
require(wcschr_nid_postfix(text, u'z') == nullptr);
|
||||
require(wcsrchr_nid_postfix(text, u'z') == nullptr);
|
||||
require(wcschr_nid_postfix(text, 0) == text + 5);
|
||||
require(wcsrchr_nid_postfix(text, 0) == text + 5);
|
||||
|
||||
const char16_t* haystack = u"one two two";
|
||||
require(wcsstr_nid_postfix(haystack, u"two") == haystack + 4);
|
||||
require(wcsstr_nid_postfix(haystack, u"") == haystack);
|
||||
require(wcsstr_nid_postfix(haystack, u"three") == nullptr);
|
||||
require(wcsstr_nid_postfix(u"tw", u"two") == nullptr);
|
||||
|
||||
require(wcspbrk_nid_postfix(haystack, u"wt") == haystack + 4);
|
||||
require(wcspbrk_nid_postfix(haystack, u"xyz") == nullptr);
|
||||
require(wcsspn_nid_postfix(u"aabbc", u"ab") == 4);
|
||||
require(wcsspn_nid_postfix(u"abc", u"") == 0);
|
||||
|
||||
const char16_t units[] = {u'x', 0, u'', u'y'};
|
||||
require(wmemchr_nid_postfix(units, u'', 4) == units + 2);
|
||||
require(wmemchr_nid_postfix(units, u'y', 3) == nullptr);
|
||||
const char16_t lower[] = {u'x', 0, u'\u0001', u'y'};
|
||||
require(wmemcmp_nid_postfix(units, units, 4) == 0);
|
||||
require(wmemcmp_nid_postfix(units, lower, 4) > 0);
|
||||
require(wmemcmp_nid_postfix(lower, units, 4) < 0);
|
||||
require(wmemcmp_nid_postfix(units, lower, 2) == 0);
|
||||
|
||||
char16_t copy[4] = {};
|
||||
require(wmemcpy_nid_postfix(copy, units, 4) == copy && same(copy, units, 4));
|
||||
char16_t overlap[] = {u'1', u'2', u'3', u'4', u'5'};
|
||||
require(wmemmove_nid_postfix(overlap + 1, overlap, 4) == overlap + 1);
|
||||
const char16_t shifted[] = {u'1', u'1', u'2', u'3', u'4'};
|
||||
require(same(overlap, shifted, 5));
|
||||
require(wmemset_nid_postfix(copy, u'世', 3) == copy && copy[0] == u'世' && copy[2] == u'世' && copy[3] == u'y');
|
||||
return 0;
|
||||
}
|
||||
@@ -229,6 +229,7 @@ Throughout the project, every function at every stage either **does exactly what
|
||||
|
||||
- [libSceHttp2 asynchronous requests](../../core/libs/prx/libSceHttp2/Export.cpp) (libSceHttp2) never reach the network: `sceHttp2SendRequestAsync` accepts the request, queues a completion whose result is the library's network error and triggers the user event named by the kqueue option (equeue at offset 0, event id at 8, user data at 16, layout inferred from PPSA17221); `sceHttp2WaitAsync` returns that completion. The completion's `event_type` value is unknown and left 0. PPSA17221 completes its request object only after `sceHttp2SendRequestAsync` returns, and its handler for a synchronous failure calls `shared_from_this` before that, so a send must not fail synchronously. A kqueue option or user event that is missing, an extra option, and a wait with no completed operation throw
|
||||
- [sceKernelWriteThrottlingStatus](../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) reports no write throttling: the host SSD has no write budget, so the first qword of the 32-byte status is set to `UINT32_MAX` and the rest is zeroed. PPSA17221 reads only that qword: it multiplies it by 64 KiB and compares the product with byte thresholds up to 0x4AF00000 to pick one of five tiers. When the call fails the title assumes 0x4B000001 bytes, which selects the same top tier as `UINT32_MAX`. What the value measures on hardware (a remaining budget or a rate) and the meaning of the other three qwords are unknown
|
||||
- [Wide-character conversions](../../core/libs/prx/libc/src/Strings.cpp) (libc) - `wcstod`, `wcstof`, `wcstold`, `wcstol`, `wcstoll`, `wcstoul`, `wcstoull`, `wcscoll`, `wcsxfrm`, `wcsrtombs` and [`swprintf`](../../core/libs/prx/libc/src/Formatting.cpp) still pass guest strings to the host's `wchar_t` functions. The guest's `wchar_t` is 16 bits, so on Linux, where the host's is 32 bits, they misread and miswrite guest strings
|
||||
- [sceNetSendmsg and sceNetRecvmsg](../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) copy the scatter/gather list through one host buffer and accept only the flags `sceNetSend`/`sceNetRecv` accept. Sending control data throws; a receive never returns control data (`msg_controllen` 0), since no socket option that queues it is supported
|
||||
- [Additional content](../../core/libs/prx/libSceAppContent/Export.cpp) (libSceAppContent) is not installed or mounted: `sceAppContentAddcontMount` answers `NOT_FOUND` for every entitlement label and `sceAppContentAddcontUnmount` for every mount point, as no add-content mount is ever handed out; `sceAppContentGetAddcontInfo` answers `DRM_NO_ENTITLEMENT` for every label and `sceAppContentGetAddcontInfoList` reports 0 entries
|
||||
- [sceKernelInstallExceptionHandler](../../core/libs/prx/libkernel/System/src/Exception.cpp) records the handler, but a host fault still ends the process: only signals raised with `sceKernelRaiseException` are delivered. Unity's PS5Util module installs a SIGUSR1 handler that reads `uc_mcontext.mc_rsp` (offset 0xf8 of the context) and raises SIGUSR1 on other threads to stop them for garbage collection
|
||||
|
||||
Reference in New Issue
Block a user