Skip to content
Closed
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
22 changes: 22 additions & 0 deletions core/libs/prx/libc/include/GuestLocale.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ struct String {
std::uint64_t capacity;
};

struct WideString {
std::uint64_t reserved;
union {
char16_t buffer[8];
char16_t* pointer;
};
std::uint64_t size;
std::uint64_t capacity;
};

struct CollateFacet {
Facet base;
const void* collation;
Expand All @@ -58,6 +68,13 @@ struct CollateVtable {
std::int64_t (APS5_VABI *hash)(const CollateFacet* self, const char* first, const char* last);
};

struct WideCollateVtable {
FacetVtable facet;
int (APS5_VABI *compare)(const CollateFacet* self, const char16_t* first1, const char16_t* last1, const char16_t* first2, const char16_t* last2);
WideString* (APS5_VABI *transform)(WideString* result, const CollateFacet* self, const char16_t* first, const char16_t* last);
std::int64_t (APS5_VABI *hash)(const CollateFacet* self, const char16_t* first, const char16_t* last);
};

struct IosBase {
const void* vtable;
std::int64_t standardStream;
Expand Down Expand Up @@ -85,10 +102,15 @@ static_assert(sizeof(IosBase) == 0x40);
static_assert(offsetof(String, buffer) == 0x8);
static_assert(offsetof(String, size) == 0x18);
static_assert(sizeof(String) == 0x28);
static_assert(offsetof(WideString, buffer) == 0x8);
static_assert(offsetof(WideString, size) == 0x18);
static_assert(sizeof(WideString) == 0x28);
static_assert(offsetof(CollateFacet, collation) == 0x10);
static_assert(sizeof(CollateFacet) == 0x20);
static_assert(offsetof(CollateVtable, compare) == 0x20);
static_assert(offsetof(CollateVtable, hash) == 0x30);
static_assert(offsetof(WideCollateVtable, compare) == 0x20);
static_assert(offsetof(WideCollateVtable, hash) == 0x30);
static_assert(alignof(LocinfoStorage) == 8);

}
Expand Down
59 changes: 57 additions & 2 deletions core/libs/prx/libc/src/LocaleSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ GuestLocale::Facet* g_classicFacets[1]{};
GuestLocale::Implementation g_classicLocale{{&g_localeVtable, 1, 0}, g_classicFacets, 1, 0, false, "C"};

constexpr std::size_t GuestStringInlineCapacity = 15;
constexpr std::size_t GuestWideStringInlineCapacity = 7;
constexpr std::size_t GuestStringLargeAllocation = 0x1000;

void ValidateCharacterRange(const char* first, const char* last, const char* function) {
if ((first == nullptr) != (last == nullptr) || last < first) throw std::invalid_argument(std::string(function) + ": invalid character range");
}

void ValidateWideCharacterRange(const char16_t* first, const char16_t* last, const char* function) {
if ((first == nullptr) != (last == nullptr) || last < first) throw std::invalid_argument(std::string(function) + ": invalid character range");
}

void APS5_VABI DestroyCollate(GuestLocale::Facet*) {}

void APS5_VABI DeleteCollate(GuestLocale::Facet* self) {
Expand Down Expand Up @@ -114,6 +119,20 @@ int APS5_VABI CollateCompare(const GuestLocale::CollateFacet*, const char* first
return length1 < length2 ? -1 : 1;
}

int APS5_VABI WideCollateCompare(const GuestLocale::CollateFacet*, const char16_t* first1, const char16_t* last1, const char16_t* first2, const char16_t* last2) {
ValidateWideCharacterRange(first1, last1, "collate::do_compare");
ValidateWideCharacterRange(first2, last2, "collate::do_compare");
const auto length1 = static_cast<std::size_t>(last1 - first1);
const auto length2 = static_cast<std::size_t>(last2 - first2);
const auto common = std::min(length1, length2);
for (std::size_t i = 0; i < common; ++i) {
if (first1[i] < first2[i]) return -1;
if (first1[i] > first2[i]) return 1;
}
if (length1 == length2) return 0;
return length1 < length2 ? -1 : 1;
}

GuestLocale::String* APS5_VABI CollateTransform(GuestLocale::String* result, const GuestLocale::CollateFacet*, const char* first, const char* last) {
if (result == nullptr) throw std::invalid_argument("collate::do_transform: null result");
ValidateCharacterRange(first, last, "collate::do_transform");
Expand All @@ -134,14 +153,44 @@ GuestLocale::String* APS5_VABI CollateTransform(GuestLocale::String* result, con
return result;
}

GuestLocale::WideString* APS5_VABI WideCollateTransform(GuestLocale::WideString* result, const GuestLocale::CollateFacet*, const char16_t* first, const char16_t* last) {
if (result == nullptr) throw std::invalid_argument("collate::do_transform: null result");
ValidateWideCharacterRange(first, last, "collate::do_transform");
const auto length = static_cast<std::size_t>(last - first);
for (std::size_t i = 0; i < length; ++i) {
if (first[i] == 0) throw std::runtime_error("collate::do_transform: embedded null characters are not supported");
}
if (length + 1 >= GuestStringLargeAllocation) throw std::runtime_error("collate::do_transform: results of 4095 characters or more are not supported");
char16_t* data = result->buffer;
std::size_t capacity = GuestWideStringInlineCapacity;
if (length > GuestWideStringInlineCapacity) {
capacity = length;
data = static_cast<char16_t*>(_Znwm_nid_postfix((capacity + 1) * sizeof(char16_t)));
}
if (length != 0) std::memcpy(data, first, length * sizeof(char16_t));
data[length] = 0;
if (data != result->buffer) result->pointer = data;
result->size = length;
result->capacity = capacity;
return result;
}

std::int64_t APS5_VABI CollateHash(const GuestLocale::CollateFacet*, const char* first, const char* last) {
ValidateCharacterRange(first, last, "collate::do_hash");
std::uint64_t hash = 0xcbf29ce484222325;
for (; first != last; ++first) hash = (hash ^ static_cast<unsigned char>(*first)) * 0x100000001b3;
return static_cast<std::int64_t>(hash);
}

std::int64_t APS5_VABI WideCollateHash(const GuestLocale::CollateFacet*, const char16_t* first, const char16_t* last) {
ValidateWideCharacterRange(first, last, "collate::do_hash");
std::uint64_t hash = 0xcbf29ce484222325;
for (; first != last; ++first) hash = (hash ^ static_cast<std::uint16_t>(*first)) * 0x100000001b3;
return static_cast<std::int64_t>(hash);
}

const GuestLocale::CollateVtable g_collateVtable{{DestroyCollate, DeleteCollate, RetainFacet, ReleaseFacet}, CollateCompare, CollateTransform, CollateHash};
const GuestLocale::WideCollateVtable g_wideCollateVtable{{DestroyCollate, DeleteCollate, RetainFacet, ReleaseFacet}, WideCollateCompare, WideCollateTransform, WideCollateHash};

constexpr std::array<short, 257> MakeClassificationTable() {
std::array<short, 257> table{};
Expand Down Expand Up @@ -257,8 +306,14 @@ GuestLocale::Implementation* APS5_VABI _ZNSt6locale16_GetgloballocaleEv_nid_post
return &g_classicLocale;
}

void APS5_VABI _ZNSt7collateIwE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(GuestLocale::Facet**, const GuestLocale::Implementation*) {
NotImplemented_nid_no_patch(__func__);
std::size_t APS5_VABI _ZNSt7collateIwE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const* locale) {
if (facet != nullptr && *facet == nullptr) {
if (locale == nullptr || *locale == nullptr || (*locale)->name == nullptr || std::strcmp((*locale)->name, "C") != 0) throw std::invalid_argument("collate::_Getcat: only the C locale is supported");
auto* collate = static_cast<GuestLocale::CollateFacet*>(_Znwm_nid_postfix(sizeof(GuestLocale::CollateFacet)));
*collate = {{&g_wideCollateVtable.facet, 0, 0}, nullptr, nullptr};
*facet = &collate->base;
}
return 1;
}

std::size_t APS5_VABI _ZNSt7collateIcE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const* locale) {
Expand Down
90 changes: 90 additions & 0 deletions core/libs/tests/GuestCollate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
extern "C" {
extern GuestLocale::Implementation* _ZSt21_sceLibcClassicLocale_nid_postfix;
std::size_t APS5_VABI _ZNSt7collateIcE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const* locale);
std::size_t APS5_VABI _ZNSt7collateIwE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const* locale);
}

namespace {
Expand Down Expand Up @@ -55,14 +56,26 @@ const GuestLocale::CollateVtable& Vtable(const GuestLocale::Facet* facet) {
return *reinterpret_cast<const GuestLocale::CollateVtable*>(facet->vtable);
}

const GuestLocale::WideCollateVtable& WideVtable(const GuestLocale::Facet* facet) {
return *reinterpret_cast<const GuestLocale::WideCollateVtable*>(facet->vtable);
}

int Compare(const GuestLocale::CollateFacet* facet, const std::string& left, const std::string& right) {
return Vtable(&facet->base).compare(facet, left.data(), left.data() + left.size(), right.data(), right.data() + right.size());
}

int WideCompare(const GuestLocale::CollateFacet* facet, const std::u16string& left, const std::u16string& right) {
return WideVtable(&facet->base).compare(facet, left.data(), left.data() + left.size(), right.data(), right.data() + right.size());
}

std::uint64_t Hash(const GuestLocale::CollateFacet* facet, const std::string& text) {
return static_cast<std::uint64_t>(Vtable(&facet->base).hash(facet, text.data(), text.data() + text.size()));
}

std::uint64_t WideHash(const GuestLocale::CollateFacet* facet, const std::u16string& text) {
return static_cast<std::uint64_t>(WideVtable(&facet->base).hash(facet, text.data(), text.data() + text.size()));
}

}

int main() {
Expand Down Expand Up @@ -152,5 +165,82 @@ int main() {
require(frees == 0);
Vtable(facet).facet.deleteObject(facet);
require(frees == 1 && lastFree == collate);

require(_ZNSt7collateIwE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(nullptr, classic) == 1);
GuestLocale::Facet existingWide{};
GuestLocale::Facet* wideFacet = &existingWide;
require(_ZNSt7collateIwE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(&wideFacet, classic) == 1);
require(wideFacet == &existingWide);

wideFacet = nullptr;
reject([&] { _ZNSt7collateIwE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(&wideFacet, &frenchPointer); });
require(wideFacet == nullptr);

const auto allocationsBefore = allocations;
require(_ZNSt7collateIwE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(&wideFacet, classic) == 1);
require(wideFacet != nullptr && allocations == allocationsBefore + 1 && lastSize == sizeof(GuestLocale::CollateFacet) && lastAllocation == wideFacet);
const auto* wideCollate = reinterpret_cast<const GuestLocale::CollateFacet*>(wideFacet);
require(wideFacet->references == 0 && wideCollate->collation == nullptr && wideCollate->wideCollation == nullptr);

require(WideCompare(wideCollate, u"abc", u"abd") == -1);
require(WideCompare(wideCollate, u"abd", u"abc") == 1);
require(WideCompare(wideCollate, u"a", u"c") == -1);
require(WideCompare(wideCollate, u"abc", u"abc") == 0);
require(WideCompare(wideCollate, u"ab", u"abc") == -1);
require(WideCompare(wideCollate, u"abc", u"ab") == 1);
require(WideCompare(wideCollate, u"", u"") == 0);
require(WideCompare(wideCollate, u"", u"a") == -1);
require(WideCompare(wideCollate, u"a", u"") == 1);
require(WideCompare(wideCollate, std::u16string(u"a\0", 2), u"a") == 1);
require(WideCompare(wideCollate, std::u16string(u"a\0b", 3), std::u16string(u"a\0c", 3)) == -1);
require(WideVtable(wideFacet).compare(wideCollate, nullptr, nullptr, nullptr, nullptr) == 0);
const char16_t wideText[] = u"abc";
reject([&] { WideVtable(wideFacet).compare(wideCollate, wideText + 2, wideText, wideText, wideText + 1); });
reject([&] { WideVtable(wideFacet).compare(wideCollate, nullptr, wideText, wideText, wideText + 1); });

require(WideHash(wideCollate, u"") == 0xcbf29ce484222325ull);
require(WideHash(wideCollate, u"a") == ((0xcbf29ce484222325ull ^ static_cast<std::uint16_t>(u'a')) * 0x100000001b3ull));
require(WideHash(wideCollate, u"foobar") != 0);
reject([&] { WideVtable(wideFacet).hash(wideCollate, wideText + 1, wideText); });

GuestLocale::WideString wideResult;
std::memset(&wideResult, 0xcd, sizeof(wideResult));
const std::u16string wideShort = u"hello";
require(WideVtable(wideFacet).transform(&wideResult, wideCollate, wideShort.data(), wideShort.data() + wideShort.size()) == &wideResult);
require(wideResult.reserved == 0xcdcdcdcdcdcdcdcdull && wideResult.size == 5 && wideResult.capacity == 7 && std::u16string(wideResult.buffer) == wideShort);

std::memset(&wideResult, 0xcd, sizeof(wideResult));
require(WideVtable(wideFacet).transform(&wideResult, wideCollate, nullptr, nullptr) == &wideResult);
require(wideResult.size == 0 && wideResult.capacity == 7 && wideResult.buffer[0] == 0);

const std::u16string seven(7, u'q');
require(WideVtable(wideFacet).transform(&wideResult, wideCollate, seven.data(), seven.data() + seven.size()) == &wideResult);
require(wideResult.size == 7 && wideResult.capacity == 7 && std::u16string(wideResult.buffer) == seven);

const std::u16string wideLong = u"wide collation of a longer text";
require(WideVtable(wideFacet).transform(&wideResult, wideCollate, wideLong.data(), wideLong.data() + wideLong.size()) == &wideResult);
require(wideResult.size == wideLong.size() && wideResult.capacity == wideLong.size() && std::u16string(wideResult.pointer) == wideLong);
std::free(wideResult.pointer);

const std::u16string wideLargest(4094, u'z');
require(WideVtable(wideFacet).transform(&wideResult, wideCollate, wideLargest.data(), wideLargest.data() + wideLargest.size()) == &wideResult);
require(wideResult.size == 4094 && wideResult.capacity == 4094 && std::u16string(wideResult.pointer) == wideLargest);
std::free(wideResult.pointer);

const std::u16string wideTooLong(4095, u'z');
reject([&] { WideVtable(wideFacet).transform(&wideResult, wideCollate, wideTooLong.data(), wideTooLong.data() + wideTooLong.size()); });
const std::u16string wideEmbedded(u"a\0b", 3);
reject([&] { WideVtable(wideFacet).transform(&wideResult, wideCollate, wideEmbedded.data(), wideEmbedded.data() + wideEmbedded.size()); });
reject([&] { WideVtable(wideFacet).transform(nullptr, wideCollate, wideText, wideText + 1); });

WideVtable(wideFacet).facet.retain(wideFacet);
WideVtable(wideFacet).facet.retain(wideFacet);
require(wideFacet->references == 2);
require(WideVtable(wideFacet).facet.release(wideFacet) == nullptr && wideFacet->references == 1);
require(WideVtable(wideFacet).facet.release(wideFacet) == wideFacet && wideFacet->references == 0);
WideVtable(wideFacet).facet.destroy(wideFacet);
const auto freesBefore = frees;
WideVtable(wideFacet).facet.deleteObject(wideFacet);
require(frees == freesBefore + 1 && lastFree == wideCollate);
return 0;
}
2 changes: 1 addition & 1 deletion docs/dev/TechnicalDebt.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ Throughout the project, every function at every stage either **does exactly what
- [DS src2 operations](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) (`ds_*_src2_*`, GFX10.1 only) read `DS[B]`, with `B = A + 4 * sext(offset1[6:0]:offset0)`, and then update `DS[A]` with the matching 32-bit atomic, so the read of `B` is not atomic with the update of `A`. The form that takes the offset from the address (`offset1[7]` set), the 64-bit forms and GDS throw: whether the LDS address keeps `A[31:17]` in that form, and whether the 64-bit forms scale the offset by 4 or 8, is not documented. `ds_min_src2_f32`, `ds_max_src2_f32` and `ds_add_src2_f32` throw: on RDNA2 (gfx1035, the oracle's hardware, which has no src2 opcodes) `ds_min_f32`, `ds_max_f32` and `ds_add_f32` ignore `IEEE_MODE` but depend on whether f32 denormals are flushed (28, 28 and 12 of 196 special rows differ), and that is not modelled for LDS float atomics yet (#981).
- [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
- [The narrow printf family](../../core/libs/prx/libc/src/Formatting.cpp) (libc) passes `%ls` and `%S` to glibc on Linux, which reads them as strings of 32-bit `wchar_t`; the guest's `wchar_t` is 16 bits (UTF-16), so these conversions fail with -1. The Windows path converts them to UTF-8, and `guest_formatting` checks wide conversions on Windows only
- [collate<char>::_Getcat](../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) builds the facet for the C locale only and throws for any other locale name. The console's `libc.prx` stores pointers to its collation state tables at offsets 0x10 and 0x18 of the facet; ours leaves them null, as only the facet's own functions read them. `do_transform` throws for input with embedded null characters, which the console drops depending on the output buffer size, and for results of 4095 characters or more, whose string storage uses the guest's over-aligned large allocation.
- [collate<char>::_Getcat, collate<wchar_t>::_Getcat](../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) build the facet for the C locale only and throw for any other locale name. The console's `libc.prx` stores pointers to its collation state tables at offsets 0x10 and 0x18 of the facet; ours leaves them null, as only the facet's own functions read them. `do_transform` throws for input with embedded null characters, which the console drops depending on the output buffer size, and for results of 4095 characters or more, whose string storage uses the guest's over-aligned large allocation.
- [socketpair](../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - the pair is the host's `AF_UNIX` pair on Linux and, on Windows, a loopback TCP connection for `SOCK_STREAM` or two connected UDP sockets for `SOCK_DGRAM`; `SOCK_SEQPACKET` throws on Windows. A datagram sent to a full receiver gets `ENOBUFS` on the console, waits or gets `EAGAIN` on Linux and is dropped on Windows, and on Windows a datagram `recv` after the peer closed reports `ECONNRESET` instead of the console's `ENOTCONN`, and a stream `send` after the peer closed reports `ECONNABORTED` instead of the console's `EPIPE`. `read` and `write` on a pair end go to the host like on every guest socket. `getpeername` on a pair end succeeds after the peer closed, where the console returns `ENOTCONN`, and `connect`, `bind` and `sendto` with an address on a pair end report an address-family error instead of the console's `EISCONN`. On Windows `poll` on a stream end reports `POLLHUP` without `POLLIN` after the peer's `shutdown(SHUT_WR)` or `close`, where the console reports `POLLIN`, with `POLLHUP` only after `close`
- [poll](../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) waits only on guest sockets, through `WSAPoll` on Windows and `poll` on Linux; the `pollfd` layout and flag values are FreeBSD's. A descriptor below the socket range (files, pipes, stdio) throws, as do event bits other than `POLLIN`, `POLLPRI`, `POLLOUT`, `POLLRDNORM`, `POLLRDBAND` and `POLLWRBAND` (the `POLLERR`, `POLLHUP` and `POLLNVAL` bits are ignored in `events`), `POLLPRI` on Windows, which `WSAPoll` rejects, and an infinite timeout with no socket to wait on. A closed socket descriptor reports `POLLNVAL`; the console's limit on the descriptor count is unknown and not checked
- [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
Expand Down
Loading