From 1d20721e12215fddbe9f9ad12721cfd3c020966e Mon Sep 17 00:00:00 2001 From: maximaxi01 Date: Tue, 20 Oct 2026 02:47:22 -0400 Subject: [PATCH] fix(libSceFont): reject undersized render surface strides --- core/libs/prx/libSceFont/CMakeLists.txt | 10 ++ core/libs/prx/libSceFont/src/Render.cpp | 12 ++ core/libs/tests/FontRenderSurface.cpp | 193 ++++++++++++++++++++++++ docs/dev/debt/libSceFont.md | 2 + 4 files changed, 217 insertions(+) create mode 100644 core/libs/tests/FontRenderSurface.cpp diff --git a/core/libs/prx/libSceFont/CMakeLists.txt b/core/libs/prx/libSceFont/CMakeLists.txt index cec16f879bf5..f73f746157ce 100644 --- a/core/libs/prx/libSceFont/CMakeLists.txt +++ b/core/libs/prx/libSceFont/CMakeLists.txt @@ -7,6 +7,16 @@ if(BUILD_TESTING) target_link_libraries(guest_font_tests PRIVATE libSceFont libSceFontFt libc) configure_windows_unwind(guest_font_tests) add_test(NAME guest_font COMMAND guest_font_tests) + add_test_executable(font_render_surface_tests ${LIBS_INCLUDE_DIR}/tests/FontRenderSurface.cpp) + target_include_directories(font_render_surface_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(font_render_surface_tests PRIVATE libSceFont libSceFontFt libc) + configure_windows_unwind(font_render_surface_tests) + add_test(NAME font_render_surface COMMAND font_render_surface_tests) + set_tests_properties(font_render_surface PROPERTIES TIMEOUT 30) + if(WIN32) + set_tests_properties(font_render_surface PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_system_font_tests ${LIBS_INCLUDE_DIR}/tests/GuestSystemFont.cpp) target_include_directories(guest_system_font_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_system_font_tests PRIVATE libSceFont libSceFontFt libc) diff --git a/core/libs/prx/libSceFont/src/Render.cpp b/core/libs/prx/libSceFont/src/Render.cpp index 7ed1af516579..8381c3f85df0 100644 --- a/core/libs/prx/libSceFont/src/Render.cpp +++ b/core/libs/prx/libSceFont/src/Render.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include FT_FREETYPE_H @@ -24,6 +25,15 @@ std::uint16_t ClampToU16(float value) { return static_cast(std::lround(std::min(value, static_cast(std::numeric_limits::max())))); } +void ValidateSurfaceStride(const FontRenderSurface* surface, FontGlyphMetrics* metrics, FontRenderOutput* result) { + if (!surface || !surface->buffer || surface->width <= 0 || surface->height <= 0 || surface->widthByte <= 0) return; + const int bytesPerPixel = surface->pixelSizeByte; + if (bytesPerPixel != 1 && bytesPerPixel != 4) return; + if (surface->width <= surface->widthByte / bytesPerPixel) return; + ClearRenderOutputs(metrics, result); + throw std::runtime_error("libSceFont: render surface stride is smaller than its pixel width"); +} + std::uint8_t* LayoutCacheBytes(FontHandleNative* font) { return reinterpret_cast(&font->cached_style) + offsetof(CachedStyle, layout_cache_bytes); } @@ -108,6 +118,7 @@ int RenderDirectional(FontHandle fontHandle, std::uint32_t code, FontRenderSurfa ClearRenderOutputs(metrics, result); return SCE_FONT_ERROR_INVALID_PARAMETER; } + if (font->renderer) ValidateSurfaceStride(surface, metrics, result); std::uint32_t fontLock = 0; if (!AcquireFontLock(font, fontLock)) { ClearRenderOutputs(metrics, result); @@ -219,6 +230,7 @@ int APS5_VABI sceFontGetRenderScaledKerning(FontHandle fontHandle, std::uint32_t int APS5_VABI sceFontRenderCharGlyphImage(FontHandle fontHandle, std::uint32_t code, FontRenderSurface* surface, float x, float y, FontGlyphMetrics* metrics, FontRenderOutput* result) { auto* font = GetNativeFont(fontHandle); + if (font && font->magic == HANDLE_MAGIC && font->renderer && code != 0 && metrics && result) ValidateSurfaceStride(surface, metrics, result); std::uint32_t fontLock = 0; if (!font || font->magic != HANDLE_MAGIC || !AcquireFontLock(font, fontLock)) { ClearRenderOutputs(metrics, result); diff --git a/core/libs/tests/FontRenderSurface.cpp b/core/libs/tests/FontRenderSurface.cpp new file mode 100644 index 000000000000..feae992d7a8e --- /dev/null +++ b/core/libs/tests/FontRenderSurface.cpp @@ -0,0 +1,193 @@ +#include "prx/libSceFont/include/FontTypes.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI sceFontMemoryInit(FontMemory*, void*, std::uint32_t, const FontMemoryInterface*, void*, FontMemoryDestroyFunction, void*); +int APS5_VABI sceFontMemoryTerm(FontMemory*); +int APS5_VABI sceFontCreateLibrary(const FontMemory*, const void*, FontLibrary*); +int APS5_VABI sceFontDestroyLibrary(FontLibrary*); +int APS5_VABI sceFontSupportExternalFonts(FontLibrary, std::uint32_t, std::uint32_t); +int APS5_VABI sceFontOpenFontMemory(FontLibrary, const void*, std::uint32_t, const FontOpenDetail*, FontHandle*); +int APS5_VABI sceFontCloseFont(FontHandle); +int APS5_VABI sceFontCreateRenderer(const FontMemory*, const void*, FontRenderer*); +int APS5_VABI sceFontDestroyRenderer(FontRenderer*); +int APS5_VABI sceFontBindRenderer(FontHandle, FontRenderer); +int APS5_VABI sceFontUnbindRenderer(FontHandle); +int APS5_VABI sceFontSetupRenderScalePixel(FontHandle, float, float); +void APS5_VABI sceFontRenderSurfaceInit(FontRenderSurface*, void*, int, int, int, int); +int APS5_VABI sceFontRenderCharGlyphImage(FontHandle, std::uint32_t, FontRenderSurface*, float, float, FontGlyphMetrics*, FontRenderOutput*); +int APS5_VABI sceFontRenderCharGlyphImageHorizontal(FontHandle, std::uint32_t, FontRenderSurface*, float, float, FontGlyphMetrics*, FontRenderOutput*); +int APS5_VABI sceFontRenderCharGlyphImageVertical(FontHandle, std::uint32_t, FontRenderSurface*, float, float, FontGlyphMetrics*, FontRenderOutput*); +const void* APS5_VABI sceFontSelectLibraryFt(int); +const void* APS5_VABI sceFontSelectRendererFt(int); +} + +static void Check(bool value, int line) { + if (!value) { + std::fprintf(stderr, "Font surface check failed at line %d\n", line); + std::abort(); + } +} +#define Require(value) Check((value), __LINE__) + +static int allocations = 0; +static void* APS5_VABI Allocate(void*, std::uint32_t size) { + ++allocations; + return std::malloc(size); +} +static void APS5_VABI Release(void*, void* pointer) { + if (pointer) --allocations; + std::free(pointer); +} + +static void Put16(std::vector& out, std::uint32_t value) { + out.push_back(static_cast(value >> 8)); + out.push_back(static_cast(value)); +} + +static void Put32(std::vector& out, std::uint32_t value) { + Put16(out, value >> 16); + Put16(out, value & 0xFFFFu); +} + +static void PutAll16(std::vector& out, std::initializer_list values) { + for (const int value : values) Put16(out, static_cast(static_cast(value))); +} + +static std::vector SquareGlyphFont(std::uint32_t codepoint, int width) { + std::vector head; + Put32(head, 0x00010000u); + Put32(head, 0x00010000u); + Put32(head, 0); + Put32(head, 0x5F0F3CF5u); + PutAll16(head, {0x000B, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, width, 800, 0, 8, 2, 0, 0}); + std::vector hhea; + Put32(hhea, 0x00010000u); + PutAll16(hhea, {800, -200, 0, width, 0, 0, width, 1, 0, 0, 0, 0, 0, 0, 0, 2}); + std::vector maxp; + Put32(maxp, 0x00010000u); + PutAll16(maxp, {2, 4, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}); + std::vector hmtx; + PutAll16(hmtx, {width, 0, width, 100}); + std::vector glyf; + PutAll16(glyf, {1, 100, 0, width - 100, 700, 3, 0}); + glyf.insert(glyf.end(), {1, 1, 1, 1}); + PutAll16(glyf, {100, 0, width - 200, 0, 0, 700, 0, -700}); + std::vector loca; + PutAll16(loca, {0, 0, static_cast(glyf.size() / 2)}); + std::vector cmap; + PutAll16(cmap, {0, 1, 3, 1}); + Put32(cmap, 12); + PutAll16(cmap, {4, 32, 0, 4, 4, 1, 0, static_cast(codepoint), 0xFFFF, 0, static_cast(codepoint), 0xFFFF, 1 - static_cast(codepoint), 1, 0, 0}); + const std::vector*>> tables = { + {"cmap", &cmap}, {"glyf", &glyf}, {"head", &head}, {"hhea", &hhea}, {"hmtx", &hmtx}, {"loca", &loca}, {"maxp", &maxp}}; + std::vector font; + Put32(font, 0x00010000u); + PutAll16(font, {static_cast(tables.size()), 64, 2, static_cast(tables.size()) * 16 - 64}); + std::uint32_t offset = 12 + static_cast(tables.size()) * 16; + for (const auto& [tag, data] : tables) { + font.insert(font.end(), tag, tag + 4); + Put32(font, 0); + Put32(font, offset); + Put32(font, static_cast(data->size())); + offset += (static_cast(data->size()) + 3u) & ~3u; + } + for (const auto& [tag, data] : tables) { + font.insert(font.end(), data->begin(), data->end()); + font.resize((font.size() + 3u) & ~std::size_t{3}); + } + return font; +} + +static void CheckRenderStrides(const FontMemory* memory, FontHandle font) { + FontRenderer renderer = nullptr; + Require(sceFontCreateRenderer(memory, sceFontSelectRendererFt(0), &renderer) == SCE_FONT_OK); + Require(sceFontBindRenderer(font, renderer) == SCE_FONT_OK); + Require(sceFontSetupRenderScalePixel(font, 100.0f, 100.0f) == SCE_FONT_OK); + for (const auto render : {sceFontRenderCharGlyphImage, sceFontRenderCharGlyphImageHorizontal, sceFontRenderCharGlyphImageVertical}) { + for (const int bpp : {1, 4}) { + for (const int invalid : {0, 1, 2}) { + std::vector pixels(1024, 0xA5); + FontRenderSurface surface{}; + sceFontRenderSurfaceInit(&surface, pixels.data(), 16 * bpp, bpp, 80, 1); + if (invalid == 0) surface.buffer = nullptr; + else surface.height = invalid == 1 ? 0 : -1; + FontGlyphMetrics metrics{}; + FontRenderOutput output{}; + Require(render(font, 'A', &surface, 0.0f, 70.0f, &metrics, &output) == SCE_FONT_ERROR_NO_SUPPORT_SURFACE); + Require(std::all_of(pixels.begin(), pixels.end(), [](unsigned char value) { return value == 0xA5; })); + } + for (const auto [width, stride, clipped] : {std::array{80, 16 * bpp, 0}, + {80, 80 * bpp - 1, 0}, {80, 16 * bpp, 1}, {INT_MAX, 200, 0}}) { + std::vector pixels(1024, 0xA5); + FontRenderSurface surface{}; + sceFontRenderSurfaceInit(&surface, pixels.data(), stride, bpp, width, 1); + if (clipped) surface.sc_x1 = 8; + FontGlyphMetrics metrics; + FontRenderOutput output; + std::memset(&metrics, 0x5A, sizeof(metrics)); + std::memset(&output, 0x5A, sizeof(output)); + bool threw = false; + try { + const float y = render == sceFontRenderCharGlyphImage ? -10.0f : 70.0f; + render(font, 'A', &surface, 0.0f, y, &metrics, &output); + } catch (const std::runtime_error& error) { + threw = std::string(error.what()).find("stride") != std::string::npos; + } + if (!threw) { + const bool escaped = std::any_of(pixels.begin() + stride, pixels.end(), [](unsigned char value) { return value != 0xA5; }); + std::fprintf(stderr, "Undersized Font stride: bpp=%d width=%d stride=%d wrote_beyond_buffer=%d\n", bpp, width, stride, escaped); + } + Require(threw); + Require(std::all_of(pixels.begin(), pixels.end(), [](unsigned char value) { return value == 0xA5; })); + const auto* metricBytes = reinterpret_cast(&metrics); + const auto* outputBytes = reinterpret_cast(&output); + Require(std::all_of(metricBytes, metricBytes + sizeof(metrics), [](unsigned char value) { return value == 0; })); + Require(std::all_of(outputBytes, outputBytes + sizeof(output), [](unsigned char value) { return value == 0; })); + for (const int padding : {0, 3}) { + const int validStride = 80 * bpp + padding; + std::vector validPixels(100 * validStride + 128, 0); + sceFontRenderSurfaceInit(&surface, validPixels.data(), validStride, bpp, 80, 100); + const float validY = render == sceFontRenderCharGlyphImage ? 0.0f : 70.0f; + Require(render(font, 'A', &surface, 0.0f, validY, &metrics, &output) == SCE_FONT_OK); + Require(std::any_of(validPixels.begin(), validPixels.begin() + 100 * validStride, [](unsigned char value) { return value != 0; })); + Require(std::all_of(validPixels.begin() + 100 * validStride, validPixels.end(), [](unsigned char value) { return value == 0; })); + for (int row = 0; row < 100; ++row) { + Require(std::all_of(validPixels.begin() + row * validStride + 80 * bpp, + validPixels.begin() + (row + 1) * validStride, [](unsigned char value) { return value == 0; })); + } + } + } + } + } + Require(sceFontUnbindRenderer(font) == SCE_FONT_OK); + Require(sceFontDestroyRenderer(&renderer) == SCE_FONT_OK); +} + +int main() { + const FontMemoryInterface iface{Allocate, Release, nullptr, nullptr, nullptr, nullptr}; + FontMemory memory{}; + Require(sceFontMemoryInit(&memory, nullptr, 0, &iface, nullptr, nullptr, nullptr) == SCE_FONT_OK); + FontLibrary library = nullptr; + Require(sceFontCreateLibrary(&memory, sceFontSelectLibraryFt(0), &library) == SCE_FONT_OK); + Require(sceFontSupportExternalFonts(library, 1, 0x52) == SCE_FONT_OK); + const auto bytes = SquareGlyphFont('A', 900); + FontHandle font = nullptr; + Require(sceFontOpenFontMemory(library, bytes.data(), static_cast(bytes.size()), nullptr, &font) == SCE_FONT_OK); + CheckRenderStrides(&memory, font); + Require(sceFontCloseFont(font) == SCE_FONT_OK); + Require(sceFontDestroyLibrary(&library) == SCE_FONT_OK); + Require(sceFontMemoryTerm(&memory) == SCE_FONT_OK); + Require(allocations == 0); +} diff --git a/docs/dev/debt/libSceFont.md b/docs/dev/debt/libSceFont.md index b1b5e56398b7..6464e9f98f55 100644 --- a/docs/dev/debt/libSceFont.md +++ b/docs/dev/debt/libSceFont.md @@ -2,6 +2,8 @@ ### Unknown function info +- [sceFontRenderCharGlyphImage, sceFontRenderCharGlyphImageHorizontal, sceFontRenderCharGlyphImageVertical](../../../core/libs/prx/libSceFont/src/Render.cpp) (libSceFont) throw for a positive row stride smaller than the declared pixel width times the supported pixel size (1 or 4 bytes), even with a narrower scissor. The console response to this inconsistent surface is unverified. Outputs are cleared before throwing; no surface bytes are written. + - [Typographic design](../../../core/libs/prx/libSceFont/src/Style.cpp): native selector, feature, font-set eligibility and output-clearing rules are recovered. Driver typography selection and queries, feature-cache updates and glyph substitution remain unimplemented and throw instead of storing arbitrary selector/value pairs. - [sceFontSetScriptLanguage and sceFontGetScriptLanguage](../../../core/libs/prx/libSceFont/src/Style.cpp): native script/language ranges, handle-error precedence and clearing of failed getter outputs are implemented. Valid driver queries and updates throw; the previous arbitrary-value map and fabricated zero default are no longer used. Driver selection and actual language-dependent shaping remain unimplemented. - [sceFontGetRenderScaledKerning](../../../core/libs/prx/libSceFont/src/Render.cpp) (libSceFont) - arguments, the cleared output and the bound renderer check taken from `cellFontGetRenderScaledKerning` in [RPCS3](https://github.com/RPCS3/rpcs3/blob/master/rpcs3/Emu/Cell/Modules/cellFont.cpp) (PS3 predecessor of the library), not confirmed on a title. The kerning is the `sceFontGetKerning` one at the render scale