Skip to content
Open
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
15 changes: 15 additions & 0 deletions core/libs/prx/libSceIcu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ if(BUILD_TESTING)
endif()
endif()

target_sources(libSceIcu PRIVATE DataMemory.cpp)

if(BUILD_TESTING)
add_test_executable(guest_icu_data_memory_tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/DataMemory.cpp)
target_include_directories(guest_icu_data_memory_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_icu_data_memory_tests PRIVATE libSceIcu libc)
configure_windows_unwind(guest_icu_data_memory_tests)
add_test(NAME guest_icu_data_memory COMMAND guest_icu_data_memory_tests)
set_tests_properties(guest_icu_data_memory PROPERTIES TIMEOUT 10)
if(WIN32)
set_tests_properties(guest_icu_data_memory PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceIcu>")
endif()
endif()

if(BUILD_TESTING)
add_test_executable(guest_icu_compare_iter_tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/CompareIter.cpp)
target_include_directories(guest_icu_compare_iter_tests PRIVATE ${LIBS_INCLUDE_DIR})
Expand Down
70 changes: 70 additions & 0 deletions core/libs/prx/libSceIcu/DataMemory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
******************************************************************************
*
* Copyright (C) 1999-2011, International Business Machines
* Corporation and others. All Rights Reserved.
*
******************************************************************************/
// https://github.com/unicode-org/icu/blob/84e1f26ea77152936e70d53178a816dbfbf69989/icu4c/source/common/udatamem.cpp
// https://github.com/unicode-org/icu/blob/84e1f26ea77152936e70d53178a816dbfbf69989/icu4c/source/common/ucmndata.cpp

#include "prx/libSceIcu/include/DataMemory.hpp"
#include "prx/libc/include/General.hpp"
#include <cstddef>
#include <cstdint>
#include <limits>
#include <stdexcept>

namespace {

constexpr std::size_t HeaderPointerOffset = 8;
constexpr std::size_t TotalLengthOffset = 48;
constexpr std::size_t EndiannessOffset = 8;
static_assert(sizeof(void*) == 8);

const void* AtOffset(const void* base, std::size_t offset) {
const auto address = reinterpret_cast<std::uintptr_t>(base);
if (offset > std::numeric_limits<std::uintptr_t>::max() - address) {
throw std::invalid_argument("ICU data memory: address overflow");
}
return reinterpret_cast<const void*>(address + offset);
}

template<class T>
T ReadField(const void* base, std::size_t offset) {
T value;
if (!GuestCopyIn_nid_no_patch(&value, AtOffset(base, offset), sizeof(value))) {
throw std::invalid_argument("ICU data memory: unreadable field");
}
return value;
}

const void* Header(const Icu::DataMemory* data) {
return data ? ReadField<const void*>(data, HeaderPointerOffset) : nullptr;
}

std::uint16_t HeaderSize(const void* header) {
const auto bigEndian = ReadField<std::uint8_t>(header, EndiannessOffset);
const auto size = ReadField<std::uint16_t>(header, 0);
return bigEndian ? static_cast<std::uint16_t>((size << 8) | (size >> 8)) : size;
}

}

extern "C" const void* APS5_VABI udata_getRawMemory_nid_postfix(const Icu::DataMemory* data) {
return Header(data);
}

extern "C" const void* APS5_VABI udata_getMemory_nid_postfix(Icu::DataMemory* data) {
const auto* header = Header(data);
return header ? AtOffset(header, HeaderSize(header)) : nullptr;
}

extern "C" std::int32_t APS5_VABI udata_getLength_nid_postfix(const Icu::DataMemory* data) {
const auto* header = Header(data);
if (!header) return -1;
const auto total = ReadField<std::int32_t>(data, TotalLengthOffset);
return total < 0 ? -1 : total - HeaderSize(header);
}
12 changes: 0 additions & 12 deletions core/libs/prx/libSceIcu/LegacyExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4452,10 +4452,6 @@ int APS5_VABI _ZN6icu_6810NormalizerC1ENS_14ConstChar16PtrEi18UNormalizationMode
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI udata_getRawMemory_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI _ZNK6icu_6815MaybeStackArrayIcLi40EE13getArrayLimitEv_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}
Expand Down Expand Up @@ -5547,10 +5543,6 @@ int APS5_VABI _ZNK6icu_6813ICUServiceKey5getIDEv_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI udata_getLength_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI _ZNK6icu_6822UCharCharacterIterator9current32Ev_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}
Expand Down Expand Up @@ -7031,10 +7023,6 @@ int APS5_VABI _ZNK6icu_6810UnicodeSeteqERKS0__nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI udata_getMemory_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI _ZNK6icu_6814ResourceBundle10getVersionEPh_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}
Expand Down
14 changes: 14 additions & 0 deletions core/libs/prx/libSceIcu/include/DataMemory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "SceTypes.hpp"
#include <cstdint>

namespace Icu {
struct DataMemory;
}

extern "C" {
const void* APS5_VABI udata_getRawMemory_nid_postfix(const Icu::DataMemory* data);
const void* APS5_VABI udata_getMemory_nid_postfix(Icu::DataMemory* data);
std::int32_t APS5_VABI udata_getLength_nid_postfix(const Icu::DataMemory* data);
}
3 changes: 0 additions & 3 deletions core/libs/prx/libSceIcu/include/LegacyExports.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,6 @@ int APS5_VABI uprv_sortArray_nid_postfix();
int APS5_VABI _ZN6icu_6817StringEnumerationC2Ev_nid_postfix();
int APS5_VABI sceIcuLegacyUnknown_R5kS2YcFHgs_nid_no_patch();
int APS5_VABI _ZN6icu_6810NormalizerC1ENS_14ConstChar16PtrEi18UNormalizationMode_nid_postfix();
int APS5_VABI udata_getRawMemory_nid_postfix();
int APS5_VABI _ZNK6icu_6815MaybeStackArrayIcLi40EE13getArrayLimitEv_nid_postfix();
int APS5_VABI _ZN6icu_6810UnicodeSet8copyFromERKS0_a_nid_postfix();
int APS5_VABI _ZNK6icu_686Locale13toLanguageTagERNS_8ByteSinkER10UErrorCode_nid_postfix();
Expand Down Expand Up @@ -1297,7 +1296,6 @@ int APS5_VABI _ZN6icu_6813UnicodeStringD0Ev_nid_postfix();
int APS5_VABI _ZN6icu_687CaseMap7toLowerEPKcjPKDsiPDsiPNS_5EditsER10UErrorCode_nid_postfix();
int APS5_VABI _ZN6icu_686Locale11getJapaneseEv_nid_postfix();
int APS5_VABI _ZNK6icu_6813ICUServiceKey5getIDEv_nid_postfix();
int APS5_VABI udata_getLength_nid_postfix();
int APS5_VABI _ZNK6icu_6822UCharCharacterIterator9current32Ev_nid_postfix();
int APS5_VABI sceIcuLegacyUnknown_YwujtxLOtxE_nid_no_patch();
int APS5_VABI _ZN6icu_6810ICUServiceC2Ev_nid_postfix();
Expand Down Expand Up @@ -1642,7 +1640,6 @@ int APS5_VABI sceIcuLegacyUnknown_iRoy3qT_plus_RDw_nid_no_patch();
int APS5_VABI _ZN6icu_6823StringCharacterIteratorC1ERKNS_13UnicodeStringE_nid_postfix();
int APS5_VABI _ZN6icu_6813LocaleBuilder23setUnicodeLocaleKeywordENS_11StringPieceES1__nid_postfix();
int APS5_VABI _ZNK6icu_6810UnicodeSeteqERKS0__nid_postfix();
int APS5_VABI udata_getMemory_nid_postfix();
int APS5_VABI _ZNK6icu_6814ResourceBundle10getVersionEPh_nid_postfix();
int APS5_VABI uenum_openUCharStringsEnumeration_nid_postfix();
int APS5_VABI _ZN6icu_6814ResourceBundle7getNextER10UErrorCode_nid_postfix();
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceIcu/libSceIcu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ConverterCallbacks.hpp"
#include "CString.hpp"
#include "prx/libSceIcu/include/InventoryExports.hpp"
#include "prx/libSceIcu/include/DataMemory.hpp"
#include "Space.hpp"
#include "LocaleMap.hpp"
#include "Unescape.hpp"
Expand Down
74 changes: 74 additions & 0 deletions core/libs/prx/libSceIcu/tests/DataMemory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "prx/libSceIcu/include/DataMemory.hpp"
#include <array>
#include <cerrno>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <stdexcept>

namespace {

void Check(bool ok, const char* condition) {
if (!ok) {
std::fprintf(stderr, "ICU data memory: %s\n", condition);
std::abort();
}
}

template<class F>
void ExpectUnreadable(F call) {
try {
call();
} catch (const std::invalid_argument&) {
return;
}
Check(false, "expected unreadable input to throw");
}

}

int main() {
Check(!udata_getRawMemory_nid_postfix(nullptr), "null raw memory");
Check(!udata_getMemory_nid_postfix(nullptr), "null payload");
Check(udata_getLength_nid_postfix(nullptr) == -1, "null length");
alignas(8) std::array<std::uint8_t, 56> handle{};
auto* data = reinterpret_cast<Icu::DataMemory*>(handle.data());
Check(!udata_getRawMemory_nid_postfix(data), "missing header");
Check(!udata_getMemory_nid_postfix(data), "missing payload");
Check(udata_getLength_nid_postfix(data) == -1, "missing header length");

std::array<std::uint8_t, 65536> bytes{};
const void* header = bytes.data();
std::memcpy(handle.data() + 8, &header, sizeof(header));
for (const std::uint8_t flag : {0, 1, 2}) {
for (const unsigned size : {0u, 1u, 32u, 255u, 256u, 0x1234u, 65535u}) {
bytes[0] = static_cast<std::uint8_t>(flag ? size >> 8 : size);
bytes[1] = static_cast<std::uint8_t>(flag ? size : size >> 8);
bytes[8] = flag;
for (const std::int32_t length : {std::numeric_limits<std::int32_t>::min(), -2, -1, 0, static_cast<int>(size), 65536, std::numeric_limits<std::int32_t>::max()}) {
std::memcpy(handle.data() + 48, &length, sizeof(length));
errno = EDOM;
Check(udata_getRawMemory_nid_postfix(data) == header, "raw address");
Check(udata_getMemory_nid_postfix(data) == bytes.data() + size, "payload address");
Check(udata_getLength_nid_postfix(data) == (length < 0 ? -1 : length - static_cast<int>(size)), "payload length");
Check(errno == EDOM, "errno changed");
}
}
}

header = reinterpret_cast<const void*>(1);
std::memcpy(handle.data() + 8, &header, sizeof(header));
std::int32_t length = -1;
std::memcpy(handle.data() + 48, &length, sizeof(length));
Check(udata_getRawMemory_nid_postfix(data) == header, "raw must not dereference header");
Check(udata_getLength_nid_postfix(data) == -1, "unknown length must not dereference header");
ExpectUnreadable([&] { udata_getMemory_nid_postfix(data); });
length = 20;
std::memcpy(handle.data() + 48, &length, sizeof(length));
ExpectUnreadable([&] { udata_getLength_nid_postfix(data); });
const auto* invalid = reinterpret_cast<const Icu::DataMemory*>(std::numeric_limits<std::uintptr_t>::max());
ExpectUnreadable([&] { udata_getRawMemory_nid_postfix(invalid); });
std::puts("ICU data memory accessors passed");
}
5 changes: 5 additions & 0 deletions docs/dev/debt/changes/feat-icu-data-memory-readers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libSceIcu

## add Functional

- [ICU data memory accessors](../../../core/libs/prx/libSceIcu/DataMemory.cpp) - `udata_getRawMemory`, `udata_getMemory` and `udata_getLength` read existing guest handles. Their x86-64 [layout](https://github.com/unicode-org/icu/blob/84e1f26ea77152936e70d53178a816dbfbf69989/icu4c/source/common/udatamem.h), [accessor semantics](https://github.com/unicode-org/icu/blob/84e1f26ea77152936e70d53178a816dbfbf69989/icu4c/source/common/udatamem.cpp) and [header byte order](https://github.com/unicode-org/icu/blob/84e1f26ea77152936e70d53178a816dbfbf69989/icu4c/source/common/ucmndata.cpp) are based on ICU 68.2, not console measurements. A required unreadable field or overflowing address throws. Opening and closing a data item are separate exports and are not implemented by these readers.
Loading