Merge pull request #15692 from B-Howell/feat/lci-string

feat(libSceLibcInternal): implement basic_string erase, append, assign, _Xran and _Xlen
This commit is contained in:
Dean authored and GitHub committed 2026-11-08 09:15:22 +13:00
commit ef9dc27c65
10 files changed
+476 -81

No files matched your search

@@ -0,0 +1,135 @@
#include "prx/libSceLibcInternal/include/Api.hpp"
#include "prx/libc/include/Api.hpp"
#include "prx/libc/include/GuestLocale.hpp"
#include "prx/libc/GuestStringStorage.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <limits>
namespace {
using GuestLocale::String;
constexpr std::size_t NoPosition = std::numeric_limits<std::size_t>::max();
constexpr std::size_t MaxSize = NoPosition - 1;
char* Data(String* string) {
return string->capacity > GuestLocale::StringInlineCapacity ? string->pointer : string->buffer;
}
const char* Data(const String* string) {
return string->capacity > GuestLocale::StringInlineCapacity ? string->pointer : string->buffer;
}
void SetSize(String* string, std::size_t size) {
string->size = size;
Data(string)[size] = 0;
}
void Reserve(String* string, std::size_t size) {
if (size > MaxSize) _ZNKSs5_XlenEv_nid_postfix(string);
if (size <= string->capacity) return;
const std::size_t capacity = std::min(size | GuestLocale::StringCapacityMask, MaxSize);
auto* data = static_cast<char*>(GuestStringStorage::Allocate(capacity + 1));
char* old = Data(string);
std::memcpy(data, old, string->size + 1);
if (string->capacity > GuestLocale::StringInlineCapacity) GuestStringStorage::Deallocate(old, string->capacity + 1);
string->pointer = data;
string->capacity = capacity;
}
std::size_t Clamp(const String* source, std::size_t position, std::size_t count) {
if (position > source->size) _ZNKSs5_XranEv_nid_postfix(source);
return std::min(count, source->size - position);
}
void EraseRange(String* string, std::size_t position, std::size_t count) {
char* data = Data(string);
const std::size_t tail = string->size - position - count;
if (count != 0 && tail != 0) std::memmove(data + position, data + position + count, tail);
SetSize(string, string->size - count);
}
void Assign(String* string, const char* source, std::size_t count) {
if (count <= string->capacity) {
if (count != 0) std::memmove(Data(string), source, count);
SetSize(string, count);
return;
}
if (count > MaxSize) _ZNKSs5_XlenEv_nid_postfix(string);
const std::size_t capacity = std::min(count | GuestLocale::StringCapacityMask, MaxSize);
auto* data = static_cast<char*>(GuestStringStorage::Allocate(capacity + 1));
std::memcpy(data, source, count);
data[count] = 0;
char* old = Data(string);
const std::size_t oldCapacity = string->capacity;
if (oldCapacity > GuestLocale::StringInlineCapacity) GuestStringStorage::Deallocate(old, oldCapacity + 1);
string->pointer = data;
string->capacity = capacity;
string->size = count;
}
}
extern "C" {
[[noreturn]] void APS5_VABI _ZNKSs5_XranEv_nid_postfix(const String*) {
_ZSt14_Xout_of_rangePKc_nid_postfix("invalid string position");
}
[[noreturn]] void APS5_VABI _ZNKSs5_XlenEv_nid_postfix(const String*) {
_ZSt14_Xlength_errorPKc_nid_postfix("string too long");
}
String* APS5_VABI _ZNSs5eraseEmm_nid_postfix(String* self, std::size_t position, std::size_t count) {
EraseRange(self, position, Clamp(self, position, count));
return self;
}
String* APS5_VABI _ZNSs6appendERKSsmm_nid_postfix(String* self, const String* source, std::size_t position, std::size_t count) {
count = Clamp(source, position, count);
if (count >= NoPosition - self->size) _ZNKSs5_XlenEv_nid_postfix(self);
if (count == 0) return self;
const std::size_t size = self->size;
Reserve(self, size + count);
std::memmove(Data(self) + size, Data(source) + position, count);
SetSize(self, size + count);
return self;
}
String* APS5_VABI _ZNSs6appendEmc_nid_postfix(String* self, std::size_t count, char character) {
if (count >= NoPosition - self->size) _ZNKSs5_XlenEv_nid_postfix(self);
if (count == 0) return self;
const std::size_t size = self->size;
Reserve(self, size + count);
std::memset(Data(self) + size, character, count);
SetSize(self, size + count);
return self;
}
String* APS5_VABI _ZNSs6assignERKSsmm_nid_postfix(String* self, const String* source, std::size_t position, std::size_t count) {
count = Clamp(source, position, count);
if (source != self) {
Assign(self, Data(source) + position, count);
return self;
}
EraseRange(self, position + count, self->size - position - count);
EraseRange(self, 0, position);
return self;
}
String* APS5_VABI _ZNSs6assignEPKcm_nid_postfix(String* self, const char* source, std::size_t count) {
Assign(self, source, count);
return self;
}
String* APS5_VABI _ZNSs6assignEmc_nid_postfix(String* self, std::size_t count, char character) {
Reserve(self, count);
if (count != 0) std::memset(Data(self), character, count);
SetSize(self, count);
return self;
}
}
@@ -206,6 +206,22 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibcInternal>")
endif()
endif()
target_sources(libSceLibcInternal PRIVATE BasicString.cpp)
if(BUILD_TESTING AND (MINGW OR CMAKE_SYSTEM_NAME STREQUAL "Linux"))
enable_language(ASM)
add_test_executable(guest_libc_internal_basic_string_tests
${LIBS_INCLUDE_DIR}/tests/GuestLibcInternalBasicString.cpp
${LIBS_INCLUDE_DIR}/tests/GuestSceV2Exceptions.S)
target_include_directories(guest_libc_internal_basic_string_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libc_internal_basic_string_tests PRIVATE libSceLibcInternal libkernel libc)
configure_windows_unwind(guest_libc_internal_basic_string_tests)
add_test(NAME guest_libc_internal_basic_string COMMAND guest_libc_internal_basic_string_tests)
set_tests_properties(guest_libc_internal_basic_string PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_libc_internal_basic_string PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibcInternal>")
endif()
endif()
target_sources(libSceLibcInternal PRIVATE PlacementDelete.cpp)
target_sources(libSceLibcInternal PRIVATE RemPio2.cpp)
set_source_files_properties(RemPio2.cpp PROPERTIES COMPILE_OPTIONS $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-ffp-contract=off>)
@@ -610,42 +610,6 @@ int APS5_VABI sceLibcInternalPending4_109_nid_no_patch() {
}
APS5_EXPORT("9HILqEoh24E", sceLibcInternalPending4_109_nid_no_patch);
int APS5_VABI sceLibcInternalPending4_110_nid_no_patch() {
NotImplemented_nid_no_patch(__func__);
return 0;
}
APS5_EXPORT("0Ir3jiT4V6Q", sceLibcInternalPending4_110_nid_no_patch);
int APS5_VABI sceLibcInternalPending4_111_nid_no_patch() {
NotImplemented_nid_no_patch(__func__);
return 0;
}
APS5_EXPORT("qiR-4jx1abE", sceLibcInternalPending4_111_nid_no_patch);
int APS5_VABI sceLibcInternalPending4_112_nid_no_patch() {
NotImplemented_nid_no_patch(__func__);
return 0;
}
APS5_EXPORT("QqBWUNEfIAo", sceLibcInternalPending4_112_nid_no_patch);
int APS5_VABI sceLibcInternalPending4_113_nid_no_patch() {
NotImplemented_nid_no_patch(__func__);
return 0;
}
APS5_EXPORT("xSxPHmpcNzY", sceLibcInternalPending4_113_nid_no_patch);
int APS5_VABI sceLibcInternalPending4_114_nid_no_patch() {
NotImplemented_nid_no_patch(__func__);
return 0;
}
APS5_EXPORT("pGxNI4JKfmY", sceLibcInternalPending4_114_nid_no_patch);
int APS5_VABI sceLibcInternalPending4_115_nid_no_patch() {
NotImplemented_nid_no_patch(__func__);
return 0;
}
APS5_EXPORT("ikjnoeemspQ", sceLibcInternalPending4_115_nid_no_patch);
int APS5_VABI sceLibcInternalPending4_116_nid_no_patch() {
NotImplemented_nid_no_patch(__func__);
return 0;
@@ -407,18 +407,6 @@ int APS5_VABI sceLibcInternalUnknown_GvYZax3i_Qk_nid_no_patch() {
}
APS5_EXPORT("GvYZax3i-Qk", sceLibcInternalUnknown_GvYZax3i_Qk_nid_no_patch);
int APS5_VABI sceLibcInternalUnknown_pDtTdJ2sIz0_nid_no_patch() {
NotImplemented_nid_no_patch(__func__);
return 0;
}
APS5_EXPORT("pDtTdJ2sIz0", sceLibcInternalUnknown_pDtTdJ2sIz0_nid_no_patch);
int APS5_VABI sceLibcInternalUnknown_AzBnOt1DouU_nid_no_patch() {
NotImplemented_nid_no_patch(__func__);
return 0;
}
APS5_EXPORT("AzBnOt1DouU", sceLibcInternalUnknown_AzBnOt1DouU_nid_no_patch);
int APS5_VABI sceLibcInternalUnknown_WMw8eIs0kjM_nid_no_patch() {
NotImplemented_nid_no_patch(__func__);
return 0;
@@ -154,6 +154,14 @@ void APS5_VABI sceLibcInternalAlignedArrayDelete_nid_no_patch(void* pointer, std
void APS5_VABI sceLibcInternalFutureErrorBaseDestructor_nid_no_patch(void* self);
void APS5_VABI sceLibcInternalOverflowErrorBaseDestructor_nid_no_patch(void* self);
void APS5_VABI sceLibcInternalUnderflowErrorBaseDestructor_nid_no_patch(void* self);
[[noreturn]] void APS5_VABI _ZNKSs5_XranEv_nid_postfix(const GuestLocale::String* self);
[[noreturn]] void APS5_VABI _ZNKSs5_XlenEv_nid_postfix(const GuestLocale::String* self);
GuestLocale::String* APS5_VABI _ZNSs5eraseEmm_nid_postfix(GuestLocale::String* self, std::size_t position, std::size_t count);
GuestLocale::String* APS5_VABI _ZNSs6appendERKSsmm_nid_postfix(GuestLocale::String* self, const GuestLocale::String* source, std::size_t position, std::size_t count);
GuestLocale::String* APS5_VABI _ZNSs6appendEmc_nid_postfix(GuestLocale::String* self, std::size_t count, char character);
GuestLocale::String* APS5_VABI _ZNSs6assignEPKcm_nid_postfix(GuestLocale::String* self, const char* source, std::size_t count);
GuestLocale::String* APS5_VABI _ZNSs6assignERKSsmm_nid_postfix(GuestLocale::String* self, const GuestLocale::String* source, std::size_t position, std::size_t count);
GuestLocale::String* APS5_VABI _ZNSs6assignEmc_nid_postfix(GuestLocale::String* self, std::size_t count, char character);
void APS5_VABI sceLibcInternalFundamentalTypeInfoBaseDestructor_nid_no_patch(void* self);
void APS5_VABI sceLibcInternalFundamentalTypeInfoCompleteDestructor_nid_no_patch(void* self);
void APS5_VABI sceLibcInternalFundamentalTypeInfoDeletingDestructor_nid_no_patch(void* self);
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include "prx/libc/include/Api.hpp"
#include <cstddef>
#include <cstdint>
#include <limits>
#include <new>
namespace GuestStringStorage {
constexpr std::size_t LargeAllocation = 0x1000;
constexpr std::size_t AllocationPadding = 0x27;
constexpr std::uintptr_t AllocationAlignment = 0x20;
static inline void* Allocate(std::size_t bytes) {
if (bytes < LargeAllocation) return _Znwm_nid_postfix(bytes);
if (bytes > std::numeric_limits<std::size_t>::max() - AllocationPadding) throw std::bad_alloc();
auto* raw = static_cast<unsigned char*>(_Znwm_nid_postfix(bytes + AllocationPadding));
const auto aligned = (reinterpret_cast<std::uintptr_t>(raw) + AllocationPadding) & ~(AllocationAlignment - 1);
reinterpret_cast<void**>(aligned)[-1] = raw;
return reinterpret_cast<void*>(aligned);
}
static inline void Deallocate(void* storage, std::size_t bytes) {
if (bytes >= LargeAllocation) storage = static_cast<void**>(storage)[-1];
_ZdlPv_nid_postfix(storage);
}
}
@@ -77,6 +77,9 @@ struct String {
std::uint64_t capacity;
};
constexpr std::size_t StringInlineCapacity = 15;
constexpr std::size_t StringCapacityMask = 15;
void* AllocateStringStorage(std::size_t bytes);
void ReleaseStringStorage(String& string);
+12 -26
View File
@@ -25,6 +25,7 @@
#include "prx/libc/include/SonyCrt.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"
#include "prx/libc/include/GuestLocale.hpp"
#include "prx/libc/GuestStringStorage.hpp"
#include "SceTypes.hpp"
extern "C" {
@@ -90,12 +91,6 @@ const GuestLocale::FacetVtable g_copiedLocaleVtable{DestroyCopiedLocale, Destroy
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 GuestStringCapacityMask = 15;
constexpr std::size_t GuestStringLargeAllocation = 0x1000;
constexpr std::size_t GuestLargeAllocationPadding = 0x27;
constexpr std::uintptr_t GuestLargeAllocationAlignment = 0x20;
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");
}
@@ -136,9 +131,9 @@ GuestLocale::String* APS5_VABI CollateTransform(GuestLocale::String* result, con
const auto length = static_cast<std::size_t>(last - first);
if (length != 0 && std::memchr(first, 0, length) != nullptr) throw std::runtime_error("collate::do_transform: embedded null characters are not supported");
char* data = result->buffer;
std::size_t capacity = GuestStringInlineCapacity;
if (length > GuestStringInlineCapacity) {
capacity = length | GuestStringCapacityMask;
std::size_t capacity = GuestLocale::StringInlineCapacity;
if (length > GuestLocale::StringInlineCapacity) {
capacity = length | GuestLocale::StringCapacityMask;
data = static_cast<char*>(GuestLocale::AllocateStringStorage(capacity + 1));
}
if (length != 0) std::memcpy(data, first, length);
@@ -220,9 +215,9 @@ GuestLocale::String* APS5_VABI AssignCString(GuestLocale::String* result, const
if (result == nullptr || source == nullptr) throw std::invalid_argument("numpunct string: null argument");
const std::size_t length = std::strlen(source);
char* data = result->buffer;
std::size_t capacity = GuestStringInlineCapacity;
if (length > GuestStringInlineCapacity) {
capacity = length | GuestStringCapacityMask;
std::size_t capacity = GuestLocale::StringInlineCapacity;
if (length > GuestLocale::StringInlineCapacity) {
capacity = length | GuestLocale::StringCapacityMask;
data = static_cast<char*>(GuestLocale::AllocateStringStorage(capacity + 1));
}
if (length != 0) std::memcpy(data, source, length);
@@ -389,9 +384,9 @@ const GuestLocale::NumpunctFacet* RequireNumpunct(const GuestLocale::NumpunctFac
std::string TakeString(GuestLocale::String* (APS5_VABI* method)(GuestLocale::String*, const GuestLocale::NumpunctFacet*), const GuestLocale::Facet* facet) {
GuestLocale::String value{};
method(&value, AsNumpunct(facet));
const bool onHeap = value.capacity > GuestStringInlineCapacity;
const bool onHeap = value.capacity > GuestLocale::StringInlineCapacity;
std::string text(onHeap ? value.pointer : value.buffer, value.size);
if (onHeap) _ZdlPv_nid_postfix(value.capacity + 1 < GuestStringLargeAllocation ? static_cast<void*>(value.pointer) : reinterpret_cast<void**>(value.pointer)[-1]);
if (onHeap) GuestStringStorage::Deallocate(value.pointer, value.capacity + 1);
return text;
}
@@ -593,22 +588,13 @@ int ConvertCharacter(int value, bool upper) {
}
void* GuestLocale::AllocateStringStorage(std::size_t bytes) {
if (bytes < GuestStringLargeAllocation) return _Znwm_nid_postfix(bytes);
if (bytes > std::numeric_limits<std::size_t>::max() - GuestLargeAllocationPadding) throw std::bad_alloc();
auto* raw = static_cast<unsigned char*>(_Znwm_nid_postfix(bytes + GuestLargeAllocationPadding));
const auto aligned = (reinterpret_cast<std::uintptr_t>(raw) + GuestLargeAllocationPadding) & ~(GuestLargeAllocationAlignment - 1);
reinterpret_cast<void**>(aligned)[-1] = raw;
return reinterpret_cast<void*>(aligned);
return GuestStringStorage::Allocate(bytes);
}
void GuestLocale::ReleaseStringStorage(String& string) {
if (string.capacity > GuestStringInlineCapacity) {
void* storage = string.pointer;
if (string.capacity + 1 >= GuestStringLargeAllocation) storage = static_cast<void**>(storage)[-1];
_ZdlPv_nid_postfix(storage);
}
if (string.capacity > StringInlineCapacity) GuestStringStorage::Deallocate(string.pointer, string.capacity + 1);
string = {};
string.capacity = GuestStringInlineCapacity;
string.capacity = StringInlineCapacity;
}
extern "C" {
@@ -0,0 +1,271 @@
#include "prx/libSceLibcInternal/include/Api.hpp"
#include "prx/libc/include/Api.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"
#include "prx/libc/include/GuestLocale.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <limits>
#include <string>
struct TypeRecord { const void* vtable; const char* name; const TypeRecord* base; };
using Thrower = void (APS5_VABI *)(const void*);
using GuestWhat = const char* (APS5_VABI *)(const void*);
extern "C" {
extern const TypeRecord _ZTISt12out_of_range_nid_postfix;
extern const TypeRecord _ZTISt12length_error_nid_postfix;
void APS5_VABI SceV2Catch(Thrower thrower, const void* argument);
const void* SceV2CatchType = nullptr;
const void* SceV2OtherType = nullptr;
int SceV2Selector = 0;
}
namespace {
char caught[128];
long live = 0;
void* APS5_VABI TestAllocate(std::size_t size) {
++live;
return std::malloc(size);
}
void APS5_VABI TestFree(void* pointer) {
if (pointer != nullptr) --live;
std::free(pointer);
}
void* APS5_VABI TestCalloc(std::size_t count, std::size_t size) {
++live;
return std::calloc(count, size);
}
void* APS5_VABI TestReallocate(void* pointer, std::size_t size) {
if (pointer == nullptr) ++live;
return std::realloc(pointer, size);
}
void* APS5_VABI TestAlign(std::size_t, std::size_t size) {
++live;
return std::malloc(size);
}
void* APS5_VABI TestRealign(void* pointer, std::size_t size, std::size_t) {
if (pointer == nullptr) ++live;
return std::realloc(pointer, size);
}
int APS5_VABI TestPosixAlign(void** pointer, std::size_t, std::size_t size) {
++live;
*pointer = std::malloc(size);
return *pointer ? 0 : 12;
}
}
extern "C" void APS5_VABI SceV2Inspect(void* object) {
const auto* vtable = *static_cast<const void* const* const*>(object);
const auto what = reinterpret_cast<GuestWhat>(const_cast<void*>(vtable[2]));
std::snprintf(caught, sizeof(caught), "%s|%s", static_cast<const TypeRecord*>(vtable[-1])->name, what(object));
}
namespace {
using GuestLocale::String;
constexpr std::size_t NoPosition = std::numeric_limits<std::size_t>::max();
constexpr std::uint64_t Allocator = 0x5a5a5a5a5a5a5a5a;
void Require(bool condition, const char* message) {
if (condition) return;
std::fprintf(stderr, "libc internal basic_string: %s\n", message);
std::abort();
}
String Empty() {
String string{};
string.reserved = Allocator;
string.capacity = GuestLocale::StringInlineCapacity;
return string;
}
const char* Data(const String& string) {
return string.capacity > GuestLocale::StringInlineCapacity ? string.pointer : string.buffer;
}
bool Holds(const String& string, const char* text) {
const std::size_t length = std::strlen(text);
return string.reserved == Allocator && string.size == length && std::memcmp(Data(string), text, length) == 0 && Data(string)[length] == 0;
}
String Make(const char* text) {
String string = Empty();
_ZNSs6assignEPKcm_nid_postfix(&string, text, std::strlen(text));
return string;
}
void APS5_VABI Invoke(const void* call) {
(*static_cast<const std::function<void()>*>(call))();
}
void RequireThrow(const std::function<void()>& call, int selector, const char* expected, const char* message) {
SceV2CatchType = &_ZTISt12out_of_range_nid_postfix;
SceV2OtherType = &_ZTISt12length_error_nid_postfix;
SceV2Selector = 0;
caught[0] = 0;
SceV2Catch(Invoke, &call);
Require(SceV2Selector == selector && std::strcmp(caught, expected) == 0, message);
}
void RequireOutOfRange(const std::function<void()>& call, const char* message) {
RequireThrow(call, 2, "St12out_of_range|invalid string position", message);
}
void RequireLengthError(const std::function<void()>& call, const char* message) {
RequireThrow(call, 1, "St12length_error|string too long", message);
}
void CheckReporters() {
String string = Empty();
RequireOutOfRange([&] { _ZNKSs5_XranEv_nid_postfix(&string); }, "_Xran throws out_of_range");
RequireLengthError([&] { _ZNKSs5_XlenEv_nid_postfix(&string); }, "_Xlen throws length_error");
}
void CheckAssignCharacters() {
String string = Empty();
Require(_ZNSs6assignEmc_nid_postfix(&string, 3, 'x') == &string, "assign(n, c) returns *this");
Require(Holds(string, "xxx") && string.capacity == 15, "assign(n, c) stays in the inline buffer");
_ZNSs6assignEmc_nid_postfix(&string, 20, 'y');
Require(Holds(string, std::string(20, 'y').c_str()) && string.capacity == 31, "assign(n, c) grows to size | 15");
_ZNSs6assignEmc_nid_postfix(&string, 0, 'z');
Require(Holds(string, "") && string.capacity == 31, "assign(0, c) keeps the storage");
RequireLengthError([&] { _ZNSs6assignEmc_nid_postfix(&string, NoPosition, 'a'); }, "assign(npos, c) throws length_error");
Require(Holds(string, ""), "a failed assign leaves the string unchanged");
GuestLocale::ReleaseStringStorage(string);
}
void CheckAssignPointer() {
String string = Empty();
const char text[] = "hello\0world";
Require(_ZNSs6assignEPKcm_nid_postfix(&string, text, 11) == &string, "assign(s, n) returns *this");
Require(string.size == 11 && std::memcmp(string.buffer, text, 12) == 0, "assign(s, n) copies embedded nulls");
_ZNSs6assignEPKcm_nid_postfix(&string, "abcdefghijklmnopqrstuvwxyz", 26);
Require(Holds(string, "abcdefghijklmnopqrstuvwxyz") && string.capacity == 31, "assign(s, n) moves to the heap");
char* storage = string.pointer;
_ZNSs6assignEPKcm_nid_postfix(&string, string.pointer + 3, 4);
Require(Holds(string, "defg") && string.pointer == storage && string.capacity == 31, "assign(s, n) from inside *this reuses the storage");
_ZNSs6assignEPKcm_nid_postfix(&string, string.pointer, 2);
Require(Holds(string, "de"), "assign(s, n) from the start of *this truncates");
GuestLocale::ReleaseStringStorage(string);
String terminator = Make("abc");
_ZNSs6assignEPKcm_nid_postfix(&terminator, Data(terminator) + 1, 3);
Require(terminator.size == 3 && std::memcmp(Data(terminator), "bc\0", 3) == 0 && Data(terminator)[3] == 0,
"assign(s, n) preserves the initialized terminator in an overlapping range");
GuestLocale::ReleaseStringStorage(terminator);
String inlineGrowth = Make("abcdefghijklmno");
_ZNSs6assignEPKcm_nid_postfix(&inlineGrowth, Data(inlineGrowth), 16);
Require(inlineGrowth.size == 16 && inlineGrowth.capacity == 31
&& std::memcmp(Data(inlineGrowth), "abcdefghijklmno\0", 16) == 0 && Data(inlineGrowth)[16] == 0,
"assign(s, n) copies an overlapping inline range before growing");
GuestLocale::ReleaseStringStorage(inlineGrowth);
String heapOverlap = Make("abcdefghijklmnopqrs");
heapOverlap.pointer[20] = 'x';
_ZNSs6assignEPKcm_nid_postfix(&heapOverlap, heapOverlap.pointer + 1, 20);
Require(heapOverlap.size == 20 && std::memcmp(Data(heapOverlap), "bcdefghijklmnopqrs\0x", 20) == 0
&& Data(heapOverlap)[20] == 0,
"assign(s, n) moves an overlapping heap range beyond the logical terminator");
GuestLocale::ReleaseStringStorage(heapOverlap);
}
void CheckAssignSubstring() {
String source = Make("0123456789abcdefghij");
String string = Make("old");
Require(_ZNSs6assignERKSsmm_nid_postfix(&string, &source, 4, 3) == &string, "assign(str, pos, n) returns *this");
Require(Holds(string, "456"), "assign(str, pos, n) copies the substring");
_ZNSs6assignERKSsmm_nid_postfix(&string, &source, 2, NoPosition);
Require(Holds(string, "23456789abcdefghij") && string.capacity == 31, "assign(str, pos, npos) copies to the end");
_ZNSs6assignERKSsmm_nid_postfix(&string, &source, 20, 5);
Require(Holds(string, ""), "assign(str, size(), n) is empty");
RequireOutOfRange([&] { _ZNSs6assignERKSsmm_nid_postfix(&string, &source, 21, 1); }, "assign(str, pos > size(), n) throws out_of_range");
_ZNSs6assignERKSsmm_nid_postfix(&source, &source, 5, 6);
Require(Holds(source, "56789a") && source.capacity == 31, "assign(*this, pos, n) keeps the storage");
_ZNSs6assignERKSsmm_nid_postfix(&source, &source, 0, NoPosition);
Require(Holds(source, "56789a"), "assign(*this, 0, npos) is unchanged");
GuestLocale::ReleaseStringStorage(source);
GuestLocale::ReleaseStringStorage(string);
}
void CheckAppendSubstring() {
String source = Make("ABCDEFGH");
String string = Make("ab");
Require(_ZNSs6appendERKSsmm_nid_postfix(&string, &source, 1, 3) == &string, "append(str, pos, n) returns *this");
Require(Holds(string, "abBCD") && string.capacity == 15, "append(str, pos, n) appends the substring");
_ZNSs6appendERKSsmm_nid_postfix(&string, &source, 6, NoPosition);
Require(Holds(string, "abBCDGH"), "append(str, pos, npos) appends to the end");
RequireOutOfRange([&] { _ZNSs6appendERKSsmm_nid_postfix(&string, &source, 9, 0); }, "append(str, pos > size(), n) throws out_of_range");
_ZNSs6appendERKSsmm_nid_postfix(&string, &string, 0, NoPosition);
Require(Holds(string, "abBCDGHabBCDGH") && string.capacity == 15, "append(*this) within the inline buffer");
_ZNSs6appendERKSsmm_nid_postfix(&string, &string, 2, 5);
Require(Holds(string, "abBCDGHabBCDGHBCDGH") && string.capacity == 31, "append(*this) across a reallocation");
GuestLocale::ReleaseStringStorage(source);
GuestLocale::ReleaseStringStorage(string);
}
void CheckAppendCharacters() {
String string = Make("go");
Require(_ZNSs6appendEmc_nid_postfix(&string, 3, 'o') == &string, "append(n, c) returns *this");
Require(Holds(string, "goooo"), "append(n, c) appends the characters");
_ZNSs6appendEmc_nid_postfix(&string, 0, 'x');
Require(Holds(string, "goooo"), "append(0, c) is unchanged");
RequireLengthError([&] { _ZNSs6appendEmc_nid_postfix(&string, NoPosition - 5, 'x'); }, "append past max_size() throws length_error");
_ZNSs6appendEmc_nid_postfix(&string, 5000, '!');
Require(string.size == 5005 && string.capacity == (5005 | 15) && string.reserved == Allocator, "append(n, c) grows a large allocation");
Require((reinterpret_cast<std::uintptr_t>(string.pointer) & 0x1f) == 0, "large storage is 32-byte aligned");
Require(std::memcmp(string.pointer, "goooo!", 6) == 0 && string.pointer[5004] == '!' && string.pointer[5005] == 0, "append(n, c) keeps the prefix and terminates");
GuestLocale::ReleaseStringStorage(string);
}
void CheckErase() {
String string = Make("0123456789abcdefghij");
Require(_ZNSs5eraseEmm_nid_postfix(&string, 2, 3) == &string, "erase returns *this");
Require(Holds(string, "0156789abcdefghij") && string.capacity == 31, "erase(pos, n) removes the range");
_ZNSs5eraseEmm_nid_postfix(&string, 10, NoPosition);
Require(Holds(string, "0156789abc"), "erase(pos, npos) truncates");
_ZNSs5eraseEmm_nid_postfix(&string, 10, 4);
Require(Holds(string, "0156789abc"), "erase(size(), n) is unchanged");
RequireOutOfRange([&] { _ZNSs5eraseEmm_nid_postfix(&string, 11, 0); }, "erase(pos > size(), n) throws out_of_range");
_ZNSs5eraseEmm_nid_postfix(&string, 0, NoPosition);
Require(Holds(string, "") && string.capacity == 31, "erase(0, npos) keeps the storage");
GuestLocale::ReleaseStringStorage(string);
String small = Make("abc");
_ZNSs5eraseEmm_nid_postfix(&small, 0, 1);
Require(Holds(small, "bc") && small.capacity == 15, "erase works on the inline buffer");
}
}
int main() {
const std::array<void*, 10> api{
reinterpret_cast<void*>(&TestAllocate), reinterpret_cast<void*>(&TestFree),
reinterpret_cast<void*>(&TestCalloc), reinterpret_cast<void*>(&TestReallocate),
reinterpret_cast<void*>(&TestAlign), reinterpret_cast<void*>(&TestRealign),
reinterpret_cast<void*>(&TestPosixAlign), nullptr, nullptr, nullptr};
ApplicationHeapRegister_nid_no_patch(api.data());
const long before = live;
CheckReporters();
CheckAssignCharacters();
CheckAssignPointer();
CheckAssignSubstring();
CheckAppendSubstring();
CheckAppendCharacters();
CheckErase();
Require(live == before, "every string allocation is released");
std::puts("libc internal basic_string: ok");
return 0;
}
+2 -7
View File
@@ -331,6 +331,7 @@ The named entries below use provider-specific NID aliases to avoid interposing h
- [posix_spawn and posix_spawnp](../../../core/libs/prx/libSceLibcInternal/Spawn.cpp) (libSceLibcInternal) have unverified console sandbox and error equivalence. POSIX errors missing from the shared translator fall back to `EIO`. Only converted programs under `/app0/` run (`/app0/eboot.bin` uses the running image). Windows open/duplication actions redirect only descriptors 0 to 2; nonzero attribute flags throw. POSIX hosts implement only `POSIX_SPAWN_RESETIDS` and `POSIX_SPAWN_SETPGROUP`; other flags throw. Guest IDs are host IDs, a null `argv` returns `EFAULT`, and null or uninitialized objects throw where FreeBSD dereferences them. A null `envp` inherits the parent environment. `posix_spawnp` follows FreeBSD 11 `_execvpe` path search; PPSA99305 reaches it while SDL searches for `zenity`
- [getopt, getopt_long](../../../core/libs/prx/libSceLibcInternal/Getopt.cpp) (libSceLibcInternal) follow FreeBSD 11 [getopt.c](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdlib/getopt.c) and [getopt_long.c](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdlib/getopt_long.c) with `GNU_COMPATIBLE`: `getopt` does not permute, `getopt_long` permutes unless `POSIXLY_CORRECT` is set in the guest environment or the option string starts with `+`, and the exported `opterr`, `optind`, `optopt`, `optreset` and `optarg` start at 1, 1, 0, 0 and null. Warnings go to the guest's current `stderr` prefixed with `__progname`, where FreeBSD's `warnx` keeps the `stderr` of its first call. A null `getopt` option string, a null argument vector or argument, a negative `optind` and an `optind` moved inside a pending permutation throw where FreeBSD faults. `getopt_long_only` is not exported. That the console's `libSceLibcInternal` uses this FreeBSD code is unverified
- [_ZNSt4_PadD1Ev](../../../core/libs/prx/libSceLibcInternal/Pad.cpp) (libSceLibcInternal, NID `XyJPhPqpzMw`) is the complete-object destructor of `_Pad`, which has no virtual bases, so the [Itanium C++ ABI](https://itanium-cxx-abi.github.io/cxx-abi/abi.html#definitions) makes it do the same work as the base-object `D2` destructor; it runs the `libc` export of that `D2` name, which unlocks and destroys the pad's mutex and condition and throws from a thread that does not hold the mutex. The console's `libSceLibcInternal.sprx` was not read and no title was seen calling it
- [basic_string<char> erase, append, assign, _Xran and _Xlen](../../../core/libs/prx/libSceLibcInternal/BasicString.cpp) (libSceLibcInternal, NIDs `0Ir3jiT4V6Q`, `qiR-4jx1abE`, `QqBWUNEfIAo`, `xSxPHmpcNzY`, `pGxNI4JKfmY`, `ikjnoeemspQ`, `AzBnOt1DouU`, `pDtTdJ2sIz0`) follow ISO C++ `[string.erase]`, `[string.append]` and `[string.assign]` on the 0x28-byte `GuestLocale::String`. The console's `libSceLibcInternal.sprx` was not read: a grown string gets capacity `size | 15`, as the `libc` string constructors give, where the console may grow geometrically; `max_size()` is taken as `SIZE_MAX - 1`; the `_Xran` and `_Xlen` messages are `invalid string position` and `string too long`
- [__kernel_rem_pio2](../../../core/libs/prx/libSceLibcInternal/RemPio2.cpp) (libSceLibcInternal, NID `GLNDoAYNlLQ`) - the console module was not read, so its use of this code, signature and table is unverified. It is FreeBSD 11.4's [`k_rem_pio2.c`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/msun/src/k_rem_pio2.c#L293-L443) ([prototype](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/msun/src/math_private.h#L794)) with the 690-chunk 2/pi table amd64's `LDBL_MAX_EXP` of 16384 selects, bit for bit with that source on x86-64. Inputs on which that code reads outside its arrays or overflows throw: precision outside 0 to 3, fewer than 1 or more than `20 - jk` chunks, an all-zero input, an infinite or NaN chunk, an `e0` past the table, null `x` or `y`
- [posix_spawnattr_getflags, getpgroup, getschedparam, getschedpolicy, getsigdefault, getsigmask, setpgroup, setschedparam, setschedpolicy and setsigmask](../../../core/libs/prx/libSceLibcInternal/Spawn.cpp) (libSceLibcInternal) read and write the 0x30-byte attribute object at the offsets of the console's `libSceLibcInternal.sprx` (SDK 4.5x build of `J03950001`, 0x8D860 to 0x8D910: flags at 0, process group at 4, scheduling parameter at 8, policy at 0xC, default and masked signal sets at 0x10 and 0x20), which never fail there and return 0; the layout matched on a PS5 (firmware 13.60). Uninitialized attributes and null pointers, which the console dereferences, throw as the existing setters do
- [_ZdlPvS_, _ZdaPvS_](../../../core/libs/prx/libSceLibcInternal/PlacementDelete.cpp) (libSceLibcInternal, NIDs `1vo6qqqa9F4`, `Suc8W0QPxjw`) are the placement forms `operator delete(void*, void*)` and `operator delete[](void*, void*)`, whose effect the C++ standard defines as "intentionally performs no action" ([new.delete.placement](https://eel.is/c++draft/new.delete.placement)); they return without reading or freeing either pointer. The console's `libSceLibcInternal.sprx` was not read and no title was seen calling them
@@ -365,7 +366,7 @@ The named entries below use provider-specific NID aliases to avoid interposing h
- [Additional 3.20 imports](../../../core/libs/prx/libSceLibcInternal/InventoryExports2.cpp) (libSceLibcInternal) - `_Xp_setw` (NID `bIfFaqUwy3I`), `_Xp_sqrtx` (NID `m0uSPrCsVdk`), `_Xp_subx` (NID `w178uGYbIJs`); signatures and behavior are unverified and calls throw through `NotImplemented_nid_no_patch`.
- [Additional 3.20 imports](../../../core/libs/prx/libSceLibcInternal/InventoryExports2.cpp) (libSceLibcInternal) - `_ZN6Dinkum7codecvt10_Cvt_checkEmm` (NID `yc-vi84-2aE`), `_ZN6Dinkum7threads10lock_errorD0Ev` (NID `PG-2ZeVVWZE`); signatures and behavior are unverified and calls throw through `NotImplemented_nid_no_patch`.
- [Additional 3.20 imports](../../../core/libs/prx/libSceLibcInternal/InventoryExports2.cpp) (libSceLibcInternal) - `_ZN6Dinkum7threads10lock_errorD1Ev` (NID `vX+NfOHOI5w`), `_ZN6Dinkum7threads17_Throw_lock_errorEv` (NID `o27+xO5NBZ8`), `_ZN6Dinkum7threads21_Throw_resource_errorEv` (NID `cjmJLdYnT5A`), `_ZN6Dinkum7threads21thread_resource_errorD0Ev` (NID `Q+ZnPMGI4M0`), `_ZN6Dinkum7threads21thread_resource_errorD1Ev` (NID `NOaB7a1PZl8`); signatures and behavior are unverified and calls throw through `NotImplemented_nid_no_patch`.
- [Additional 3.20 imports](../../../core/libs/prx/libSceLibcInternal/InventoryExports2.cpp) (libSceLibcInternal) - `_ZNKSbIwSt11char_traitsIwESaIwEE5_XlenEv` (NID `ZRRBeuLmHjc`), `_ZNKSbIwSt11char_traitsIwESaIwEE5_XranEv` (NID `GvYZax3i-Qk`), `_ZNKSs5_XlenEv` (NID `pDtTdJ2sIz0`), `_ZNKSs5_XranEv` (NID `AzBnOt1DouU`), `_ZNKSt10bad_typeid8_DoraiseEv` (NID `WMw8eIs0kjM`); signatures and behavior are unverified and calls throw through `NotImplemented_nid_no_patch`.
- [Additional 3.20 imports](../../../core/libs/prx/libSceLibcInternal/InventoryExports2.cpp) (libSceLibcInternal) - `_ZNKSbIwSt11char_traitsIwESaIwEE5_XlenEv` (NID `ZRRBeuLmHjc`), `_ZNKSbIwSt11char_traitsIwESaIwEE5_XranEv` (NID `GvYZax3i-Qk`), `_ZNKSt10bad_typeid8_DoraiseEv` (NID `WMw8eIs0kjM`); signatures and behavior are unverified and calls throw through `NotImplemented_nid_no_patch`.
- [Additional 3.20 imports](../../../core/libs/prx/libSceLibcInternal/InventoryExports2.cpp) (libSceLibcInternal) - `_ZNKSt12codecvt_base11do_encodingEv` (NID `MwAySqTo+-M`), `_ZNKSt12codecvt_base13do_max_lengthEv` (NID `FOsY+JAyXow`), `_ZNKSt12future_error4whatEv` (NID `5sfbtNAdt-M`), `_ZNKSt12future_error8_DoraiseEv` (NID `-syPONaWjqw`), `_ZNKSt12system_error8_DoraiseEv` (NID `uWZBRxLAvEg`); signatures and behavior are unverified and calls throw through `NotImplemented_nid_no_patch`.
- [Additional 3.20 imports](../../../core/libs/prx/libSceLibcInternal/InventoryExports2.cpp) (libSceLibcInternal) - `_ZNKSt13bad_exception8_DoraiseEv` (NID `kTlQY47fo88`), `_ZNKSt18bad_variant_access4whatEv` (NID `+5IOQncui3c`), `_ZNKSt19istreambuf_iteratorIcSt11char_traitsIcEE5equalERKS2_` (NID `u6UfGT9+HEA`), `_ZNKSt19istreambuf_iteratorIwSt11char_traitsIwEE5equalERKS2_` (NID `jZmLD-ASDto`), `_ZNKSt22_Future_error_category4nameEv` (NID `Q0VsWTapQ4M`); signatures and behavior are unverified and calls throw through `NotImplemented_nid_no_patch`.
- [Additional 3.20 imports](../../../core/libs/prx/libSceLibcInternal/InventoryExports2.cpp) (libSceLibcInternal) - `_ZNKSt22_Future_error_category7messageEi` (NID `nWfZplDjbxQ`), `_ZNKSt23_Generic_error_category4nameEv` (NID `xvVR0CBPFAM`), `_ZNKSt24_Iostream_error_category4nameEv` (NID `sb2vivqtLS0`), `_ZNKSt24_Iostream_error_category7messageEi` (NID `n9-NJEULZ-0`), `_ZNKSt5ctypeIcE10do_tolowerEPcPKc` (NID `2w+4Mo2DPro`); signatures and behavior are unverified and calls throw through `NotImplemented_nid_no_patch`.
@@ -796,12 +797,6 @@ The named entries below use provider-specific NID aliases to avoid interposing h
- [_ZNSoD0Ev](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `lTTrDj5OIwQ` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSoD1Ev](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `HpCeP12cuNY` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSs5_CopyEmm](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `9HILqEoh24E` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSs5eraseEmm](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `0Ir3jiT4V6Q` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSs6appendERKSsmm](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `qiR-4jx1abE` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSs6appendEmc](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `QqBWUNEfIAo` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSs6assignEPKcm](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `xSxPHmpcNzY` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSs6assignERKSsmm](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `pGxNI4JKfmY` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSs6assignEmc](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `ikjnoeemspQ` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSs6insertEmmc](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `KDgQWX1eDeo` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSt10filesystem11_EquivalentEPKcS1_](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `XD9FmX1mavU` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.
- [_ZNSt10filesystem12_Current_setEPKc](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - NID `Yl10kSufa5k` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only.