diff --git a/core/libs/prx/libSceFont/src/Text.cpp b/core/libs/prx/libSceFont/src/Text.cpp index 0b6823f3b3c9..21c7596e18b7 100644 --- a/core/libs/prx/libSceFont/src/Text.cpp +++ b/core/libs/prx/libSceFont/src/Text.cpp @@ -3,7 +3,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include -#include #include #include #include @@ -31,11 +30,6 @@ constexpr std::int32_t WRITING_MASK_NONE = 0; constexpr std::int32_t WRITING_MASK_FORMAT_CHARACTERS = 1; constexpr std::uint64_t CHARACTER_FLAG_FORMAT = 1ull << 42; -struct CharacterStorage { - std::atomic refCount{1}; - std::vector characters; -}; - struct FontStringData { std::uint16_t magic = FONT_STRING_MAGIC; std::uint8_t flags = 0; @@ -48,7 +42,7 @@ struct FontStringData { void* terminateOrder = nullptr; std::uint32_t textCount = 0; std::uint32_t renderCount = 0; - CharacterStorage* storage = nullptr; + FontTextCharacter* characters = nullptr; }; struct FontWritingData { @@ -56,7 +50,6 @@ struct FontWritingData { std::uint16_t reserved0 = 0; std::uint32_t writingForm = WRITING_FORM_HORIZONTAL; FontString string = nullptr; - CharacterStorage* storage = nullptr; FontHandle defaultFont = nullptr; std::size_t nextIndex = 0; std::size_t currentIndex = 0; @@ -66,7 +59,6 @@ struct FontWritingData { bool hasCurrentStep = false; bool hasCurrentCharacter = false; bool maskFormatCharacters = true; - bool ownsStorage = false; bool hasRawGlyphMetrics = false; FontWritingStep step{}; FontWritingMetrics metrics{}; @@ -87,18 +79,6 @@ FontWritingData* GetWritingData(FontWriting* fontWriting) { return data->magic == FONT_WRITING_MAGIC ? data : nullptr; } -void ReleaseStorage(CharacterStorage*& storage) { - if (!storage) return; - if (storage->refCount.fetch_sub(1, std::memory_order_acq_rel) == 1) delete storage; - storage = nullptr; -} - -void ReleaseWritingStorage(FontWritingData& writing) { - if (!writing.ownsStorage) return; - ReleaseStorage(writing.storage); - writing.ownsStorage = false; -} - std::uint16_t TextSourceMagic(const FontTextSource* source) { return static_cast(source->systemUse0 & 0xFFFFu); } @@ -135,15 +115,15 @@ FontTextCharacter MakeCharacter(FontHandle font, std::uint32_t code, void* textO return character; } -void LinkCharacters(std::vector& characters) { - for (std::size_t i = 0; i < characters.size(); ++i) { +void LinkCharacters(FontTextCharacter* characters, std::size_t count) { + for (std::size_t i = 0; i < count; ++i) { characters[i].prev = i == 0 ? nullptr : &characters[i - 1]; - characters[i].next = i + 1 < characters.size() ? &characters[i + 1] : nullptr; + characters[i].next = i + 1 < count ? &characters[i + 1] : nullptr; } } FontTextCharacter* TextCharacters(FontStringData* data) { - return data && data->storage && !data->storage->characters.empty() ? data->storage->characters.data() : nullptr; + return data && data->textCount != 0 ? data->characters : nullptr; } bool IsRightToLeftLetter(std::uint32_t code) { @@ -194,8 +174,9 @@ void FillWritingMetricsFromGlyph(FontWritingData& writing, const FontGlyphMetric } bool PopulateWritingStep(FontWritingData& writing, std::size_t index) { - if (!writing.storage || index >= writing.storage->characters.size()) return false; - const FontTextCharacter& character = writing.storage->characters[index]; + const FontStringData* data = GetStringData(writing.string); + if (!data || index >= data->textCount) return false; + const FontTextCharacter& character = data->characters[index]; const FontHandle font = character.font ? character.font : writing.defaultFont; writing.step.x = writing.penX; writing.step.y = writing.penY; @@ -328,9 +309,9 @@ int APS5_VABI sceFontCreateString(const FontMemory* fontMemory, FontTextSource* data->memory = fontMemory; data->source = textSource; data->defaultFont = textSource->defaultFont; - data->storage = new CharacterStorage{}; if (!data->defaultFont && detail) data->defaultFont = detail->defaultFont; data->writingForm = static_cast(textSource->systemUse0 >> 32); + std::vector characters; std::int32_t state = SCE_FONT_ERROR_INVALID_TEXT_SOURCE; void* textOrder = nullptr; for (std::size_t parsed = 0; parsed < MAX_PARSED_CHARACTERS; ++parsed) { @@ -342,7 +323,7 @@ int APS5_VABI sceFontCreateString(const FontMemory* fontMemory, FontTextSource* state = SCE_FONT_ERROR_INVALID_TEXT_SOURCE; break; } - data->storage->characters.push_back(MakeCharacter(font, result.FontCode.code, textOrder)); + characters.push_back(MakeCharacter(font, result.FontCode.code, textOrder)); continue; } if (state == PARSER_RESULT_TERMINATE) { @@ -357,15 +338,19 @@ int APS5_VABI sceFontCreateString(const FontMemory* fontMemory, FontTextSource* break; } if (state == PARSER_RESULT_FONT_CODE) state = SCE_FONT_ERROR_INVALID_TEXT_SOURCE; + if (state == SCE_FONT_OK && !characters.empty()) { + data->characters = static_cast(fontMemory->iface->alloc(fontMemory->mspace_handle, static_cast(characters.size() * sizeof(FontTextCharacter)))); + if (!data->characters) state = SCE_FONT_ERROR_ALLOCATION_FAILED; + } if (state != SCE_FONT_OK) { - ReleaseStorage(data->storage); - data->~FontStringData(); + data->magic = 0; fontMemory->iface->dealloc(fontMemory->mspace_handle, raw); return state; } - if (data->writingForm == WRITING_FORM_HORIZONTAL) data->writingForm = DefaultStringForm(data->storage->characters); - LinkCharacters(data->storage->characters); - data->textCount = static_cast(data->storage->characters.size()); + if (data->writingForm == WRITING_FORM_HORIZONTAL) data->writingForm = DefaultStringForm(characters); + std::copy(characters.begin(), characters.end(), data->characters); + LinkCharacters(data->characters, characters.size()); + data->textCount = static_cast(characters.size()); data->renderCount = data->textCount; *pFontString = reinterpret_cast(data); return SCE_FONT_OK; @@ -379,8 +364,8 @@ int APS5_VABI sceFontDestroyString(FontString* pFontString) { return SCE_FONT_ERROR_INVALID_STRING; } const FontMemory* memory = data->memory; - ReleaseStorage(data->storage); - data->~FontStringData(); + data->magic = 0; + if (data->characters) memory->iface->dealloc(memory->mspace_handle, data->characters); memory->iface->dealloc(memory->mspace_handle, data); *pFontString = nullptr; return SCE_FONT_OK; @@ -640,25 +625,21 @@ int APS5_VABI sceFontWritingInit(FontWriting* fontWriting, FontString fontString if (!fontWriting || !fontString || !fontCharacter) return SCE_FONT_ERROR_INVALID_PARAMETER; auto* data = GetStringData(fontString); if (!data) return SCE_FONT_ERROR_INVALID_STRING; - if (auto* previous = GetWritingData(fontWriting)) ReleaseWritingStorage(*previous); std::memset(fontWriting, 0, sizeof(*fontWriting)); auto* writing = new (fontWriting->systemUse) FontWritingData{}; writing->string = fontString; - writing->storage = data->storage; writing->defaultFont = data->defaultFont; - if (writing->storage) writing->storage->refCount.fetch_add(1, std::memory_order_relaxed); - writing->ownsStorage = true; writing->writingForm = data->writingForm; const FontTextCharacter* characters = TextCharacters(data); - if (!characters || fontCharacter < characters || fontCharacter >= characters + data->storage->characters.size()) { - ReleaseWritingStorage(*writing); + if (!characters || fontCharacter < characters || fontCharacter >= characters + data->textCount) { + writing->string = nullptr; return SCE_FONT_ERROR_INVALID_PARAMETER; } writing->nextIndex = static_cast(fontCharacter - characters); writing->currentIndex = writing->nextIndex; writing->hasCurrentStep = false; if (!PopulateWritingStep(*writing, writing->nextIndex)) { - ReleaseWritingStorage(*writing); + writing->string = nullptr; return SCE_FONT_ERROR_INVALID_PARAMETER; } writing->penX = 0.0f; @@ -684,8 +665,7 @@ int APS5_VABI sceFontWritingGetRenderMetrics(FontWriting* fontWriting, FontWriti const FontWritingStep* APS5_VABI sceFontWritingRefersRenderStep(FontWriting* fontWriting) { auto* writing = GetWritingData(fontWriting); - if (!writing || !writing->storage || writing->nextIndex >= writing->storage->characters.size()) return nullptr; - if (!PopulateWritingStep(*writing, writing->nextIndex)) return nullptr; + if (!writing || !PopulateWritingStep(*writing, writing->nextIndex)) return nullptr; writing->currentIndex = writing->nextIndex; writing->hasCurrentStep = true; writing->hasCurrentCharacter = true; @@ -697,7 +677,9 @@ const FontWritingStep* APS5_VABI sceFontWritingRefersRenderStep(FontWriting* fon FontTextCharacter* APS5_VABI sceFontWritingRefersRenderStepCharacter(FontWriting* fontWriting, const FontWritingLetterStep** pLetterStep) { if (pLetterStep) *pLetterStep = nullptr; auto* writing = GetWritingData(fontWriting); - if (!writing || !writing->hasCurrentStep || !writing->hasCurrentCharacter || !writing->storage || writing->currentIndex >= writing->storage->characters.size()) return nullptr; + if (!writing || !writing->hasCurrentStep || !writing->hasCurrentCharacter) return nullptr; + auto* data = GetStringData(writing->string); + if (!data || writing->currentIndex >= data->textCount) return nullptr; writing->hasCurrentCharacter = false; auto& letterStep = writing->letterStep; letterStep = {}; @@ -711,7 +693,7 @@ FontTextCharacter* APS5_VABI sceFontWritingRefersRenderStepCharacter(FontWriting letterStep.Components.glyphsIndex = static_cast(writing->currentIndex); letterStep.Components.characterTextCount = static_cast(std::max(writing->step.Profile.characterCount, 1)); if (pLetterStep) *pLetterStep = &letterStep; - return &writing->storage->characters[writing->currentIndex]; + return &data->characters[writing->currentIndex]; } int APS5_VABI sceFontWritingSetMaskInvisible(FontWriting* fontWriting, std::int32_t mask) { diff --git a/core/libs/tests/GuestFont.cpp b/core/libs/tests/GuestFont.cpp index 093c88fe454e..297cb35c7828 100644 --- a/core/libs/tests/GuestFont.cpp +++ b/core/libs/tests/GuestFont.cpp @@ -18,6 +18,9 @@ #include #include #include +#ifdef __GLIBC__ +#include +#endif static void Check(bool value, int line) { if (!value) { @@ -45,6 +48,41 @@ static void APS5_VABI Release(void*, void* pointer) { std::free(pointer); } +struct HeldBlock { + void* pointer; + std::uint32_t size; + bool released; +}; + +static std::vector heldBlocks; +static std::size_t heldBlockLimit = SIZE_MAX; + +static void* APS5_VABI HoldAllocate(void*, std::uint32_t size) { + if (heldBlocks.size() >= heldBlockLimit) return nullptr; + void* pointer = std::malloc(size); + heldBlocks.push_back({pointer, size, false}); + return pointer; +} + +static void APS5_VABI HoldRelease(void*, void* pointer) { + for (HeldBlock& block : heldBlocks) { + if (block.pointer == pointer && !block.released) { + block.released = true; + return; + } + } + Require(false); +} + +static const HeldBlock* HeldBlockOf(const void* pointer) { + const auto address = reinterpret_cast(pointer); + for (const HeldBlock& block : heldBlocks) { + const auto begin = reinterpret_cast(block.pointer); + if (address >= begin && address - begin < block.size) return █ + } + return nullptr; +} + static std::uint32_t APS5_VABI CoarsePixelResolution() { return 16; } @@ -106,6 +144,127 @@ static std::int32_t APS5_VABI ParseCodes(FontTextSource* source, void** order, F return 1; } +static void CheckWritingDoesNotOwnCharacters(const FontMemory* memory, FontHandle font) { + const std::string text(32, 'A'); + ParsedText parsed{text.c_str(), 0, font}; + FontTextSource source{}; + Require(sceFontTextSourceInit(&source, parsed.text, static_cast(text.size()), ParseText, &parsed) == SCE_FONT_OK); + FontTextCharacter* characters = nullptr; + const auto create = [&] { + parsed.at = 0; + FontString string = nullptr; + Require(sceFontCreateString(memory, &source, nullptr, &string) == SCE_FONT_OK); + std::uint32_t count = 0; + characters = sceFontStringRefersTextCharacters(string, &count); + Require(characters != nullptr && count == text.size()); + return string; + }; + const auto steps = [](FontWriting* writing) { + std::size_t count = 0; + while (sceFontWritingRefersRenderStep(writing) != nullptr) ++count; + return count; + }; +#ifdef __GLIBC__ + const auto before = mallinfo2().uordblks; +#endif + const int allocationsBefore = allocations; + for (int i = 0; i < 512; ++i) { + FontString string = create(); + FontWriting writing{}; + Require(sceFontWritingInit(&writing, string, characters) == SCE_FONT_OK && steps(&writing) == text.size()); + Require(sceFontDestroyString(&string) == SCE_FONT_OK); + } + Require(allocations == allocationsBefore); +#ifdef __GLIBC__ + Require(static_cast(mallinfo2().uordblks) - static_cast(before) < 256 * 1024); +#endif + FontString string = create(); + FontWriting first{}; + Require(sceFontWritingInit(&first, string, characters) == SCE_FONT_OK); + FontWriting copy; + std::memcpy(©, &first, sizeof(copy)); + Require(steps(&first) == text.size() && steps(©) == text.size()); + Require(sceFontDestroyString(&string) == SCE_FONT_OK); + string = create(); + Require(sceFontWritingInit(&first, string, characters) == SCE_FONT_OK && sceFontWritingInit(©, string, characters) == SCE_FONT_OK); + Require(steps(&first) == text.size() && steps(©) == text.size()); + Require(sceFontDestroyString(&string) == SCE_FONT_OK); + FontString other = create(); + const FontTextCharacter* otherCharacters = characters; + string = create(); + FontWriting failed{}; + Require(sceFontWritingInit(&failed, string, characters) == SCE_FONT_OK); + Require(sceFontWritingInit(&failed, string, otherCharacters) == SCE_FONT_ERROR_INVALID_PARAMETER && sceFontWritingRefersRenderStep(&failed) == nullptr); + Require(sceFontDestroyString(&string) == SCE_FONT_OK && sceFontDestroyString(&other) == SCE_FONT_OK); +} + +static void CheckWritingAfterStringDestroyed(FontHandle font) { + const FontMemoryInterface iface{HoldAllocate, HoldRelease, nullptr, nullptr, nullptr, nullptr}; + FontMemory memory{}; + Require(sceFontMemoryInit(&memory, nullptr, 0, &iface, nullptr, nullptr, nullptr) == SCE_FONT_OK); + const std::string text(32, 'A'); + ParsedText parsed{text.c_str(), 0, font}; + FontTextSource source{}; + Require(sceFontTextSourceInit(&source, parsed.text, static_cast(text.size()), ParseText, &parsed) == SCE_FONT_OK); + const auto create = [&](FontTextCharacter** characters) { + parsed.at = 0; + FontString string = nullptr; + Require(sceFontCreateString(&memory, &source, nullptr, &string) == SCE_FONT_OK && HeldBlockOf(string) != nullptr); + std::uint32_t count = 0; + *characters = sceFontStringRefersTextCharacters(string, &count); + Require(*characters != nullptr && count == text.size()); + Require(HeldBlockOf(*characters) != nullptr && HeldBlockOf(*characters + count - 1) == HeldBlockOf(*characters)); + return string; + }; + const auto steps = [](FontWriting* writing) { + std::size_t count = 0; + while (sceFontWritingRefersRenderStep(writing) != nullptr) ++count; + return count; + }; + const FontWritingLetterStep unset{}; + const FontWritingLetterStep* letter = nullptr; + FontTextCharacter* characters = nullptr; + parsed.at = 0; + FontString string = nullptr; + const std::size_t blocksBefore = heldBlocks.size(); + heldBlockLimit = blocksBefore + 1; + Require(sceFontCreateString(&memory, &source, nullptr, &string) == SCE_FONT_ERROR_ALLOCATION_FAILED && string == nullptr); + heldBlockLimit = SIZE_MAX; + Require(heldBlocks.size() == blocksBefore + 1 && heldBlocks.back().released); + FontString failedString = static_cast(heldBlocks.back().pointer); + Require(sceFontDestroyString(&failedString) == SCE_FONT_ERROR_INVALID_STRING && failedString == nullptr); + string = create(&characters); + FontString staleHandle = string; + const void* destroyed = string; + FontWriting writing{}; + Require(sceFontWritingInit(&writing, string, characters) == SCE_FONT_OK && sceFontWritingRefersRenderStep(&writing) != nullptr); + Require(sceFontWritingRefersRenderStepCharacter(&writing, &letter) == characters && letter != nullptr); + Require(sceFontWritingRefersRenderStep(&writing) != nullptr); + FontWriting copy; + std::memcpy(©, &writing, sizeof(copy)); + Require(sceFontDestroyString(&string) == SCE_FONT_OK && string == nullptr); + Require(HeldBlockOf(destroyed)->released && HeldBlockOf(characters)->released); + for (FontWriting* stale : {&writing, ©}) { + letter = &unset; + Require(sceFontWritingRefersRenderStepCharacter(stale, &letter) == nullptr && letter == nullptr); + Require(sceFontWritingRefersRenderStep(stale) == nullptr); + } + Require(sceFontWritingInit(©, staleHandle, characters) == SCE_FONT_ERROR_INVALID_STRING && sceFontWritingRefersRenderStep(©) == nullptr); + Require(sceFontDestroyString(&staleHandle) == SCE_FONT_ERROR_INVALID_STRING && staleHandle == nullptr); + FontTextCharacter* fresh = nullptr; + string = create(&fresh); + Require(sceFontWritingInit(&writing, string, fresh + 1) == SCE_FONT_OK && sceFontWritingInit(©, string, fresh) == SCE_FONT_OK); + Require(steps(&writing) == text.size() - 1 && sceFontWritingRefersRenderStepCharacter(&writing, &letter) == fresh + text.size() - 1); + Require(steps(©) == text.size()); + Require(sceFontDestroyString(&string) == SCE_FONT_OK); + for (const HeldBlock& block : heldBlocks) { + Require(block.released); + std::free(block.pointer); + } + heldBlocks.clear(); + Require(sceFontMemoryTerm(&memory) == SCE_FONT_OK); +} + static void CheckTextCodes(const FontMemory* memory, FontHandle font) { ParsedText parsed{"AVA", 0, font}; FontTextSource source{}; @@ -697,6 +856,8 @@ int main() { Require(sceFontWritingRefersRenderStepCharacter(&writing, &out.letter) == characters + 1 && out.letter == retainedLetter && out.guard == guard); Require(sceFontWritingRefersRenderStepCharacter(&writing, &out.letter) == nullptr && out.letter == nullptr); Require(sceFontDestroyString(&string) == SCE_FONT_OK); + CheckWritingDoesNotOwnCharacters(&memory, font); + CheckWritingAfterStringDestroyed(font); CheckTextCodes(&memory, font); Require(sceFontCloseFont(font) == SCE_FONT_OK); diff --git a/docs/dev/debt/libSceFont.md b/docs/dev/debt/libSceFont.md index 1b5466bbaf61..a0f4c548a1f8 100644 --- a/docs/dev/debt/libSceFont.md +++ b/docs/dev/debt/libSceFont.md @@ -32,6 +32,7 @@ - [sceFontGetFontMetrics](../../../core/libs/prx/libSceFont/src/Layout.cpp) (libSceFont) - the 64-byte output, the form fallbacks (2 and 3 fall back to the `hhea` values when OS/2 is missing or gives an empty range, 4 builds vertical values from `vhea` or OS/2) and the raw weight deltas and slant copied from the style are not measured on a console. For a font set the values are those of its primary file; which file the console reports is not recovered. Closes when: measured on a PS5 - [sceFontGetFontStyleInformation](../../../core/libs/prx/libSceFont/src/Layout.cpp) (libSceFont) - the 24-byte output (FreeType family and style names, italic and bold flags, OS/2 weight class when it is a multiple of 100 from 100 to 900, width class below 10, and 1 plus a right-to-left `head` direction plus 0x80 for vertical metrics) and the font-set record path are not measured on a console. A face without a `head` table, such as a BDF font, throws, as its console result is unknown. Closes when: measured on a PS5 - [sceFontGetFontGlyphsOutlineProfile](../../../core/libs/prx/libSceFont/src/Layout.cpp) (libSceFont) - the 16-byte output (the `maxp` maximum contours, points, composite contours and composite points) and `SCE_FONT_ERROR_NO_SUPPORT_FUNCTION` for a `maxp` older than 1.0 are not measured on a console. A handle with a font-set record throws, as the maximum over the record's system font entries is not modelled. A face without a `maxp` table throws, as its console result is unknown. Closes when: measured on a PS5 +- [sceFontWritingRefersRenderStep and sceFontWritingRefersRenderStepCharacter](../../../core/libs/prx/libSceFont/src/Text.cpp) on a writing whose string was destroyed: what the console returns there and on a second destroy, and where it allocates a string's characters, is not measured. Here [sceFontCreateString](../../../core/libs/prx/libSceFont/src/Text.cpp) allocates the characters through the string's `FontMemory`, as it does the string, and [sceFontDestroyString](../../../core/libs/prx/libSceFont/src/Text.cpp) clears the string's magic and frees both. A writing keeps only a pointer to its string and checks the magic on each step, so both calls return null after the destroy unless that memory holds the magic again, as when a new string is allocated there. A second destroy through another copy of the handle returns `SCE_FONT_ERROR_INVALID_STRING` unless the magic is back. Character pointers taken from the string then point into freed guest memory. Closes when: measured on a PS5 - [Font writing lines](../../../core/libs/prx/libSceFont/src/WritingLine.cpp): visual ordering and spacing originated from PS4 measurements and match the supplied PS5 structure and control flow. Exhaustive floating-point equivalence, including NaN payloads and rounding, has not been verified against a running PS5 module. - [Font graphics and metric signatures](../../../core/libs/prx/libSceFont/src/Unimplemented.cpp): graphics-device/service creation takes font memory, parameters and a handle output; destruction takes a handle pointer. Attribute selectors and outputs are 32-bit values. Expansion-buffer outputs remain opaque pending field recovery. Protected implementations are unchanged.