From e526f1f52809a305ecfb7710f36ee0f1337707ea Mon Sep 17 00:00:00 2001 From: boykopovar Date: Sun, 27 Sep 2026 09:37:01 +0300 Subject: [PATCH] feat(libc): replace guest locale implementation with std::locale --- core/libs/CMakeLists.txt | 6 - core/libs/prx/libc/include/GuestLocale.hpp | 66 ------- core/libs/prx/libc/src/LocaleSupport.cpp | 216 ++++++++------------- core/libs/tests/GuestCharacter.cpp | 4 +- core/libs/tests/GuestLocale.cpp | 158 --------------- 5 files changed, 79 insertions(+), 371 deletions(-) delete mode 100644 core/libs/prx/libc/include/GuestLocale.hpp delete mode 100644 core/libs/tests/GuestLocale.cpp diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index ac01dac0e..933cd9093 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -361,12 +361,6 @@ add_executable(application_heap_tests EXCLUDE_FROM_ALL tests/ApplicationHeap.cpp target_include_directories(application_heap_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) set_target_properties(application_heap_tests PROPERTIES CXX_EXTENSIONS OFF) -add_executable(guest_locale_tests EXCLUDE_FROM_ALL tests/GuestLocale.cpp) -target_include_directories(guest_locale_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -set_target_properties(guest_locale_tests PROPERTIES CXX_EXTENSIONS OFF) -target_link_libraries(guest_locale_tests PRIVATE libc) -configure_windows_unwind(guest_locale_tests) - if(MINGW) foreach(runtimeLinkFlag IN ITEMS -static-libgcc -static-libstdc++ -static) string(REPLACE "${runtimeLinkFlag}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") diff --git a/core/libs/prx/libc/include/GuestLocale.hpp b/core/libs/prx/libc/include/GuestLocale.hpp deleted file mode 100644 index 1e46c9210..000000000 --- a/core/libs/prx/libc/include/GuestLocale.hpp +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_GUESTLOCALE_HPP -#define CORE_LIBS_PRX_LIBC_INCLUDE_GUESTLOCALE_HPP - -#include -#include -#include "general/VabiMacros.hpp" - -namespace GuestLocale { - -struct LocinfoStorage { - alignas(std::uint64_t) unsigned char bytes[64]; -}; - -struct Facet; - -struct FacetVtable { - void (APS5_VABI *destroy)(Facet* self); - void (APS5_VABI *deleteObject)(Facet* self); - void (APS5_VABI *retain)(Facet* self); - Facet* (APS5_VABI *release)(Facet* self); -}; - -struct Facet { - const FacetVtable* vtable; - std::uint32_t references; - std::uint32_t reserved; -}; - -struct Implementation { - Facet base; - Facet** facets; - std::uint64_t facetCount; - std::uint32_t category; - bool transparent; - const char* name; -}; - -struct IosBase { - const void* vtable; - std::int64_t standardStream; - std::int32_t state; - std::int32_t exceptions; - std::int32_t flags; - std::int32_t precision; - std::int32_t width; - std::uint32_t reserved; - void* storage; - void* callbacks; - Implementation** locale; -}; - -static_assert(sizeof(Facet) == 0x10); -static_assert(offsetof(Implementation, facets) == 0x10); -static_assert(offsetof(Implementation, facetCount) == 0x18); -static_assert(offsetof(Implementation, transparent) == 0x24); -static_assert(offsetof(Implementation, name) == 0x28); -static_assert(offsetof(IosBase, state) == 0x10); -static_assert(offsetof(IosBase, storage) == 0x28); -static_assert(offsetof(IosBase, callbacks) == 0x30); -static_assert(offsetof(IosBase, locale) == 0x38); -static_assert(sizeof(IosBase) == 0x40); -static_assert(alignof(LocinfoStorage) == 8); - -} - -#endif diff --git a/core/libs/prx/libc/src/LocaleSupport.cpp b/core/libs/prx/libc/src/LocaleSupport.cpp index 0d178f899..8d3d0e7b4 100644 --- a/core/libs/prx/libc/src/LocaleSupport.cpp +++ b/core/libs/prx/libc/src/LocaleSupport.cpp @@ -1,203 +1,141 @@ +#include +#include #include -#include +#include #include #include #include -#include -#include -#include -#include -#include -#include +#include +#include #include "prx/libc/include/General.hpp" #include "prx/libc/include/ApplicationHeap.hpp" -#include "prx/libc/include/GuestLocale.hpp" namespace { -std::mutex g_localeInitMutex; -bool g_localeInitialized = false; -std::vector g_registeredFacets; - -void APS5_VABI DestroyClassicLocale(GuestLocale::Facet*) { - throw std::runtime_error("Cannot destroy the classic locale"); +void ValidateCharacter(int value) { + if (value != EOF && (value < 0 || value > UCHAR_MAX)) throw std::invalid_argument("Invalid character value"); } -void APS5_VABI RetainLocale(GuestLocale::Facet* self) { - if (self == nullptr) throw std::invalid_argument("locale retain: null facet"); - std::atomic_ref references(self->references); - auto count = references.load(); - do { - if (count == 0 || count == UINT32_MAX) throw std::runtime_error("locale retain: invalid reference count"); - } while (!references.compare_exchange_weak(count, count + 1)); +int ClassifyCharacter(int value, std::ctype_base::mask mask) { + ValidateCharacter(value); + if (value == EOF) return 0; + return std::use_facet>(std::locale::classic()).is(mask, static_cast(value)); } -GuestLocale::Facet* APS5_VABI ReleaseLocale(GuestLocale::Facet* self) { - if (self == nullptr) throw std::invalid_argument("locale release: null facet"); - std::atomic_ref references(self->references); - auto count = references.load(); - do { - if (count <= 1) throw std::runtime_error("classic locale: unbalanced release"); - } while (!references.compare_exchange_weak(count, count - 1)); - return nullptr; +int ConvertCharacter(int value, bool upper) { + ValidateCharacter(value); + if (value == EOF) return EOF; + const auto& facet = std::use_facet>(std::locale::classic()); + const auto character = static_cast(value); + return static_cast(upper ? facet.toupper(character) : facet.tolower(character)); } -const GuestLocale::FacetVtable g_localeVtable{DestroyClassicLocale, DestroyClassicLocale, RetainLocale, ReleaseLocale}; -GuestLocale::Facet* g_classicFacets[1]{}; -GuestLocale::Implementation g_classicLocale{{&g_localeVtable, 1, 0}, g_classicFacets, 1, 0, false, "C"}; - -constexpr std::array MakeClassificationTable() { - std::array table{}; - for (unsigned int value = 0; value < 256; ++value) { - short mask = 0; - if (value < 32 || value == 127) mask |= 0x80; - if (value == ' ') mask |= 0x04; - if (value >= '\t' && value <= '\r') mask |= 0x40; - if (value >= 'A' && value <= 'Z') mask |= 0x02; - if (value >= 'a' && value <= 'z') mask |= 0x10; - if (value >= '0' && value <= '9') mask |= 0x20; - if ((value >= '0' && value <= '9') || (value >= 'A' && value <= 'F') || (value >= 'a' && value <= 'f')) mask |= 0x01; - if (value >= 33 && value <= 126 && (mask & 0x232) == 0) mask |= 0x08; - table[value + 1] = mask; - } +std::array MakeCaseTable(bool upper) { + std::array table{}; + table[0] = EOF; + for (int value = 0; value <= UCHAR_MAX; ++value) table[value + 1] = ConvertCharacter(value, upper); return table; } -constexpr std::array MakeCaseTable(bool upper) { - std::array table{}; - table[0] = -1; - for (unsigned int value = 0; value < 256; ++value) { - auto converted = value; - if (upper && value >= 'a' && value <= 'z') converted -= 'a' - 'A'; - if (!upper && value >= 'A' && value <= 'Z') converted += 'a' - 'A'; - table[value + 1] = static_cast(converted); - } - return table; -} - -constexpr auto g_classificationTable = MakeClassificationTable(); -constexpr auto g_lowerTable = MakeCaseTable(false); -constexpr auto g_upperTable = MakeCaseTable(true); - } extern "C" { -// The runtime currently exposes the classic C locale. Keep byte classification -// independent of any locale selected by host-side libraries. -int APS5_VABI isupper_nid_postfix(int c) { return c >= 'A' && c <= 'Z'; } -int APS5_VABI islower_nid_postfix(int c) { return c >= 'a' && c <= 'z'; } -int APS5_VABI isalpha_nid_postfix(int c) { return isupper_nid_postfix(c) || islower_nid_postfix(c); } -int APS5_VABI isdigit_nid_postfix(int c) { return c >= '0' && c <= '9'; } -int APS5_VABI isalnum_nid_postfix(int c) { return isalpha_nid_postfix(c) || isdigit_nid_postfix(c); } -int APS5_VABI isspace_nid_postfix(int c) { return c == ' ' || (c >= '\t' && c <= '\r'); } -int APS5_VABI isblank_nid_postfix(int c) { return c == ' ' || c == '\t'; } -int APS5_VABI iscntrl_nid_postfix(int c) { return (c >= 0 && c < 32) || c == 127; } -int APS5_VABI isprint_nid_postfix(int c) { return c >= 32 && c <= 126; } -int APS5_VABI isgraph_nid_postfix(int c) { return c >= 33 && c <= 126; } -int APS5_VABI ispunct_nid_postfix(int c) { return isgraph_nid_postfix(c) && !isalnum_nid_postfix(c); } -int APS5_VABI isxdigit_nid_postfix(int c) { - return isdigit_nid_postfix(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); -} -int APS5_VABI toupper_nid_postfix(int c) { return islower_nid_postfix(c) ? c - ('a' - 'A') : c; } -int APS5_VABI tolower_nid_postfix(int c) { return isupper_nid_postfix(c) ? c + ('a' - 'A') : c; } - -std::uint64_t _ZNSt5ctypeIcE2idE_nid_postfix = 0; -std::uint64_t _ZNSt5ctypeIwE2idE_nid_postfix = 0; -std::uint64_t _ZNSt7collateIwE2idE_nid_postfix = 0; -std::uint64_t _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix = 0; -std::uintptr_t _ZTVSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE_nid_postfix[12] {}; +int APS5_VABI isupper_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::upper); } +int APS5_VABI islower_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::lower); } +int APS5_VABI isalpha_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::alpha); } +int APS5_VABI isdigit_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::digit); } +int APS5_VABI isalnum_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::alnum); } +int APS5_VABI isspace_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::space); } +int APS5_VABI isblank_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::blank); } +int APS5_VABI iscntrl_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::cntrl); } +int APS5_VABI isprint_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::print); } +int APS5_VABI isgraph_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::graph); } +int APS5_VABI ispunct_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::punct); } +int APS5_VABI isxdigit_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::xdigit); } +int APS5_VABI toupper_nid_postfix(int c) { return ConvertCharacter(c, true); } +int APS5_VABI tolower_nid_postfix(int c) { return ConvertCharacter(c, false); } std::streamoff _ZSt7_BADOFF_nid_postfix = -1; -std::fpos_t _ZSt4_Fpz_nid_postfix {}; -std::int32_t _ZNSt6locale2id7_Id_cntE_nid_postfix = 0; +std::fpos_t _ZSt4_Fpz_nid_postfix{}; -GuestLocale::Implementation* _ZSt21_sceLibcClassicLocale_nid_postfix = &g_classicLocale; +const std::locale* _ZSt21_sceLibcClassicLocale_nid_postfix = &std::locale::classic(); -void APS5_VABI _ZNSt8ios_baseD2Ev_nid_postfix(GuestLocale::IosBase* self) { +void APS5_VABI _ZNSt8ios_baseD2Ev_nid_postfix(std::ios_base* self) { if (self == nullptr) throw std::invalid_argument("ios_base destructor: null object"); - if (self->standardStream != 0 || self->storage != nullptr || self->callbacks != nullptr) throw std::runtime_error("ios_base destructor: unsupported stream storage or callbacks"); - if (self->locale != &_ZSt21_sceLibcClassicLocale_nid_postfix) throw std::runtime_error("ios_base destructor: unsupported locale ownership"); - self->locale = nullptr; + self->std::ios_base::~ios_base(); } -GuestLocale::Implementation* APS5_VABI _ZNSt6locale5_InitEv_nid_postfix() { - std::lock_guard lock(g_localeInitMutex); - if (!g_localeInitialized) { - g_localeInitialized = true; - } - return &g_classicLocale; +const std::locale* APS5_VABI _ZNSt6locale5_InitEv_nid_postfix() { + return &std::locale::classic(); } -void APS5_VABI _ZNSt6locale5facet9_RegisterEv_nid_postfix(GuestLocale::Facet* self) { - if (self == nullptr || self->vtable == nullptr) throw std::invalid_argument("locale register: invalid facet"); - std::lock_guard lock(g_localeInitMutex); - for (const auto* facet : g_registeredFacets) { - if (facet == self) throw std::runtime_error("locale register: duplicate facet"); - } - g_registeredFacets.push_back(self); +void APS5_VABI _ZNSt6locale5facet9_RegisterEv_nid_postfix(std::locale::facet*) { + NotImplemented_nid_no_patch("locale::facet::_Register"); } -GuestLocale::Implementation* APS5_VABI _ZNSt6locale16_GetgloballocaleEv_nid_postfix() { - return &g_classicLocale; +const std::locale* APS5_VABI _ZNSt6locale16_GetgloballocaleEv_nid_postfix() { + thread_local std::locale locale; + locale = std::locale(); + return &locale; } -void APS5_VABI _ZNSt7collateIwE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(GuestLocale::Facet**, const GuestLocale::Implementation*) { - NotImplemented_nid_no_patch(__func__); +void APS5_VABI _ZNSt7collateIwE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(const std::locale::facet**, const std::locale*) { + NotImplemented_nid_no_patch("collate::_Getcat"); } -void APS5_VABI _ZNSt8_LocinfoC1EPKc_nid_postfix(GuestLocale::LocinfoStorage* self, const char* localeName) { - if (self == nullptr || localeName == nullptr || std::strcmp(localeName, "C") != 0) throw std::invalid_argument("_Locinfo: only the C locale is supported"); - new (self) GuestLocale::LocinfoStorage{}; +void APS5_VABI _ZNSt8_LocinfoC1EPKc_nid_postfix(std::locale* self, const char* localeName) { + if (self == nullptr || localeName == nullptr) throw std::invalid_argument("Locale constructor: null argument"); + std::construct_at(self, localeName); } -void APS5_VABI _ZNSt8_LocinfoD1Ev_nid_postfix(GuestLocale::LocinfoStorage* self) { - if (self == nullptr) throw std::invalid_argument("_Locinfo destructor: null object"); +void APS5_VABI _ZNSt8_LocinfoD1Ev_nid_postfix(std::locale* self) { + if (self == nullptr) throw std::invalid_argument("Locale destructor: null object"); + std::destroy_at(self); } -int APS5_VABI _Mbtowcx_nid_postfix(std::uint16_t* dst, const char* src, std::size_t count, mbstate_t* st) { - if (dst == nullptr || src == nullptr || st == nullptr || count == 0) throw std::invalid_argument("_Mbtowcx: invalid conversion arguments"); - wchar_t converted{}; - const auto result = std::mbrtowc(&converted, src, count, st); +std::size_t APS5_VABI _Mbtowcx_nid_postfix(wchar_t* dst, const char* src, std::size_t count, std::mbstate_t* state) { + if (dst == nullptr || src == nullptr || state == nullptr || count == 0) throw std::invalid_argument("_Mbtowcx: invalid conversion arguments"); + const auto result = std::mbrtowc(dst, src, count, state); if (result == static_cast(-1)) throw std::runtime_error("_Mbtowcx: invalid multibyte character"); if (result == static_cast(-2)) throw std::runtime_error("_Mbtowcx: incomplete multibyte character"); - if (result > static_cast(std::numeric_limits::max()) || static_cast(converted) > 0xffff) throw std::runtime_error("_Mbtowcx: conversion exceeds guest character limits"); - *dst = static_cast(converted); - return static_cast(result); + return result; } -int APS5_VABI _Wctombx_nid_postfix(char* dst, std::uint16_t src, mbstate_t* st) { - if (dst == nullptr || st == nullptr) throw std::invalid_argument("_Wctombx: invalid conversion arguments"); - const auto result = std::wcrtomb(dst, static_cast(src), st); +std::size_t APS5_VABI _Wctombx_nid_postfix(char* dst, wchar_t src, std::mbstate_t* state) { + if (dst == nullptr || state == nullptr) throw std::invalid_argument("_Wctombx: invalid conversion arguments"); + const auto result = std::wcrtomb(dst, src, state); if (result == static_cast(-1)) throw std::runtime_error("_Wctombx: invalid wide character"); - if (result > static_cast(std::numeric_limits::max())) throw std::runtime_error("_Wctombx: conversion size exceeds guest limits"); - return static_cast(result); + return result; } -const short* APS5_VABI _Getpctype_nid_postfix() { - return g_classificationTable.data() + 1; +const std::ctype::mask* APS5_VABI _Getpctype_nid_postfix() { + return std::ctype::classic_table(); } -const short* APS5_VABI _Getptolower_nid_postfix() { - return g_lowerTable.data() + 1; +const int* APS5_VABI _Getptolower_nid_postfix() { + static const auto table = MakeCaseTable(false); + return table.data() + 1; } -const short* APS5_VABI _Getptoupper_nid_postfix() { - return g_upperTable.data() + 1; +const int* APS5_VABI _Getptoupper_nid_postfix() { + static const auto table = MakeCaseTable(true); + return table.data() + 1; } -mbstate_t* APS5_VABI _Getpmbstate_nid_postfix() { - thread_local mbstate_t state {}; +std::mbstate_t* APS5_VABI _Getpmbstate_nid_postfix() { + thread_local std::mbstate_t state{}; return &state; } -mbstate_t* APS5_VABI _Getpwcstate_nid_postfix() { - thread_local mbstate_t state {}; +std::mbstate_t* APS5_VABI _Getpwcstate_nid_postfix() { + thread_local std::mbstate_t state{}; return &state; } -wint_t APS5_VABI _Towctrans_nid_postfix(wint_t c, wctrans_t desc) { +std::wint_t APS5_VABI _Towctrans_nid_postfix(std::wint_t c, std::wctrans_t desc) { + if (desc == std::wctrans_t{}) throw std::invalid_argument("_Towctrans: invalid transformation"); return std::towctrans(c, desc); } diff --git a/core/libs/tests/GuestCharacter.cpp b/core/libs/tests/GuestCharacter.cpp index 60331fc87..ab299a450 100644 --- a/core/libs/tests/GuestCharacter.cpp +++ b/core/libs/tests/GuestCharacter.cpp @@ -18,8 +18,8 @@ int APS5_VABI ispunct_nid_postfix(int); int APS5_VABI isxdigit_nid_postfix(int); int APS5_VABI toupper_nid_postfix(int); int APS5_VABI tolower_nid_postfix(int); -const short* APS5_VABI _Getptolower_nid_postfix(); -const short* APS5_VABI _Getptoupper_nid_postfix(); +const int* APS5_VABI _Getptolower_nid_postfix(); +const int* APS5_VABI _Getptoupper_nid_postfix(); } static void Require(bool value) { if (!value) std::abort(); } int main() { diff --git a/core/libs/tests/GuestLocale.cpp b/core/libs/tests/GuestLocale.cpp deleted file mode 100644 index dc1635dd9..000000000 --- a/core/libs/tests/GuestLocale.cpp +++ /dev/null @@ -1,158 +0,0 @@ -#include "prx/libc/include/GuestLocale.hpp" -#include -#include -#include -#include -#include -#include -#include -#include - -extern "C" { -extern GuestLocale::Implementation* _ZSt21_sceLibcClassicLocale_nid_postfix; -GuestLocale::Implementation* APS5_VABI _ZNSt6locale16_GetgloballocaleEv_nid_postfix(); -GuestLocale::Implementation* APS5_VABI _ZNSt6locale5_InitEv_nid_postfix(); -void APS5_VABI _ZNSt6locale5facet9_RegisterEv_nid_postfix(GuestLocale::Facet* self); -void APS5_VABI _ZNSt8_LocinfoC1EPKc_nid_postfix(GuestLocale::LocinfoStorage* self, const char* localeName); -void APS5_VABI _ZNSt8_LocinfoD1Ev_nid_postfix(GuestLocale::LocinfoStorage* self); -void APS5_VABI _ZNSt8ios_baseD2Ev_nid_postfix(GuestLocale::IosBase* self); -const short* APS5_VABI _Getpctype_nid_postfix(); -const short* APS5_VABI _Getptolower_nid_postfix(); -const short* APS5_VABI _Getptoupper_nid_postfix(); -int APS5_VABI _Mbtowcx_nid_postfix(std::uint16_t* dst, const char* src, std::size_t count, std::mbstate_t* st); -int APS5_VABI _Wctombx_nid_postfix(char* dst, std::uint16_t src, std::mbstate_t* st); -void APS5_VABI _Locksyslock_nid_postfix(); -void APS5_VABI _Unlocksyslock_nid_postfix(); - -} - -static void Require(bool condition) { - if (!condition) throw std::runtime_error("Guest locale check failed"); -} - -template -static void RequireException(TAction action) { - bool caught = false; - try { - action(); - } catch (const std::exception&) { - caught = true; - } - Require(caught); -} - -static void CheckGuestCalls() { - auto* locale = _ZSt21_sceLibcClassicLocale_nid_postfix; - Require(locale != nullptr && locale == _ZNSt6locale16_GetgloballocaleEv_nid_postfix()); - Require(_ZNSt6locale5_InitEv_nid_postfix() == locale); - Require(_ZNSt6locale5_InitEv_nid_postfix() == locale); - const std::byte* table = nullptr; - std::memcpy(&table, locale, sizeof(table)); - void (APS5_VABI *retain)(GuestLocale::Facet*) = nullptr; - GuestLocale::Facet* (APS5_VABI *release)(GuestLocale::Facet*) = nullptr; - std::memcpy(&retain, table + 0x10, sizeof(retain)); - std::memcpy(&release, table + 0x18, sizeof(release)); - const auto initial = locale->base.references; - retain(&locale->base); - Require(locale->base.references == initial + 1); - Require(release(&locale->base) == nullptr); - Require(locale->base.references == initial); - RequireException([&] { release(&locale->base); }); - RequireException([&] { retain(nullptr); }); - RequireException([&] { locale->base.vtable->deleteObject(&locale->base); }); - Require(locale->base.references == initial); - std::vector threads; - for (int thread = 0; thread < 4; ++thread) { - threads.emplace_back([&] { - for (int iteration = 0; iteration < 10000; ++iteration) { - retain(&locale->base); - release(&locale->base); - } - }); - } - for (auto& thread : threads) thread.join(); - Require(locale->base.references == initial); - Require(locale->facetCount == 1 && locale->facets[0] == nullptr); - Require(std::strcmp(locale->name, "C") == 0 && !locale->transparent); -} - -static void CheckLocinfoAlignment() { - alignas(16) std::array bytes; - bytes.fill(std::byte{0x5a}); - auto* storage = reinterpret_cast(bytes.data() + 8); - _ZNSt8_LocinfoC1EPKc_nid_postfix(storage, "C"); - for (std::size_t index = 0; index < bytes.size(); ++index) Require(bytes[index] == (index >= 8 && index < 72 ? std::byte{} : std::byte{0x5a})); - _ZNSt8_LocinfoD1Ev_nid_postfix(storage); - RequireException([&] { _ZNSt8_LocinfoC1EPKc_nid_postfix(storage, "unknown-locale"); }); - RequireException([] { _ZNSt8_LocinfoC1EPKc_nid_postfix(nullptr, "C"); }); -} - -static void CheckCharacterTables() { - const auto* classification = _Getpctype_nid_postfix(); - const auto* lower = _Getptolower_nid_postfix(); - const auto* upper = _Getptoupper_nid_postfix(); - Require(classification[-1] == 0 && lower[-1] == -1 && upper[-1] == -1); - Require(classification['A'] == 0x03 && classification['a'] == 0x11); - Require(classification['G'] == 0x02 && classification['0'] == 0x21); - Require(classification[' '] == 0x04 && classification['\t'] == 0xc0); - Require(classification['\n'] == 0xc0 && classification['!'] == 0x08); - for (int value = 0; value < 256; ++value) { - Require(lower[value] == (value >= 'A' && value <= 'Z' ? value + 32 : value)); - Require(upper[value] == (value >= 'a' && value <= 'z' ? value - 32 : value)); - if (value >= 128) Require(classification[value] == 0); - } -} - -static void CheckCharacterConversions() { - const auto* classification = _Getpctype_nid_postfix(); - for (std::uint16_t value = 0; value < 128; ++value) { - std::array bytes{}; - bytes.fill('!'); - std::mbstate_t encodeState{}; - Require(_Wctombx_nid_postfix(bytes.data(), value, &encodeState) == 1); - Require(bytes[0] == static_cast(value) && bytes[1] == '!'); - std::array wide{0xffff, 0x1234}; - std::mbstate_t decodeState{}; - Require(_Mbtowcx_nid_postfix(wide.data(), bytes.data(), 2, &decodeState) == (value == 0 ? 0 : 1)); - Require(wide[0] == value && wide[1] == 0x1234); - const bool alnum = (value >= '0' && value <= '9') || (value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z'); - Require(((classification[static_cast(bytes[0])] & 0x232) != 0) == alnum); - } - std::mbstate_t state{}; - char byte{}; - std::uint16_t wide{}; - RequireException([&] { _Mbtowcx_nid_postfix(&wide, "]", 0, &state); }); - RequireException([&] { _Mbtowcx_nid_postfix(nullptr, "]", 1, &state); }); - RequireException([&] { _Mbtowcx_nid_postfix(&wide, nullptr, 1, &state); }); - RequireException([&] { _Mbtowcx_nid_postfix(&wide, "]", 1, nullptr); }); - RequireException([&] { _Wctombx_nid_postfix(nullptr, ']', &state); }); - RequireException([&] { _Wctombx_nid_postfix(&byte, ']', nullptr); }); -} - -static void CheckStreamDestruction() { - struct GuardedStream { - std::uint64_t before = 0x12345678; - GuestLocale::IosBase stream{}; - std::uint64_t after = 0x87654321; - } object; - object.stream.locale = &_ZSt21_sceLibcClassicLocale_nid_postfix; - _ZNSt8ios_baseD2Ev_nid_postfix(&object.stream); - Require(object.stream.locale == nullptr && object.before == 0x12345678 && object.after == 0x87654321); - object.stream.locale = &_ZSt21_sceLibcClassicLocale_nid_postfix; - object.stream.callbacks = &object; - RequireException([&] { _ZNSt8ios_baseD2Ev_nid_postfix(&object.stream); }); - Require(object.stream.locale == &_ZSt21_sceLibcClassicLocale_nid_postfix); -} - -int main() { - CheckGuestCalls(); - CheckLocinfoAlignment(); - CheckCharacterTables(); - CheckCharacterConversions(); - CheckStreamDestruction(); - _Locksyslock_nid_postfix(); - _Locksyslock_nid_postfix(); - _Unlocksyslock_nid_postfix(); - _Unlocksyslock_nid_postfix(); - std::cout << "Guest locale checks passed\n"; -}