feat(libSceFreeType): expose raw SFNT table access #9420

Merged
Ondra97 merged 6 commits from upstream-pr/9420 into main 2026-10-27 08:05:45 +00:00
Ondra97 commented 2026-10-21 21:21:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-21T21:21:22Z.


What

Add FT_Sfnt_Table_Info and FT_Load_Sfnt_Table to the guest FreeType bridge. Callers can enumerate the SFNT directory, query table/file lengths, and read raw table or whole-font bytes. The adapter preserves LP64 tags, lengths and offsets, including high-bit table tags, aliased outputs and partially written buffers on short-read errors.

The length field is written only for zero-length queries, so an aliased destination is not overwritten after reading. Each successful FT_Sfnt_Table_Info metadata output and the zero-length table query now open GuestArena::HostWrite for the exact eight-byte output before storing; separate scopes preserve native tag-then-length ordering when the pointers alias. Data reads continue to hold HostWrite over the possible destination writes. A requested length of zero queries metadata without touching the data buffer. On 32-bit-long hosts, memory-backed tagged reads recover the absolute table position with a native one-byte probe, restore the original cursor, then read at the validated absolute file position. This prevents 32-bit offset wrap without rejecting positions above signed 32 bits. Tagged reads through native stream callbacks throw on those hosts because probing could change callback state; the limitation is in debt.

The SFNT declarations live in the public include/Api.hpp reached by libSceFreeType.hpp. The regression test includes that umbrella and uses its typed GuestLibrary* API without local export declarations. Supported wide-stream reads and error/output checks remain covered.

Names/NIDs oiRP-fp3SN4 and 09vKaCuNyqI are in the PS5 3.20 inventory. Signatures, errors and stream behavior follow bundled FreeType 2.13.3: public entry points, table access and stream reads. Console equivalence remains unverified. No matching implementation was found in main. The current open-PR overlap scan reports only conflicted #9848; manual inspection of its shared CMake and debt changes confirms that it addresses macOS symbol visibility and separate import tests, without duplicating SFNT access.

Tested

Current revision: base af49a0788576ca8878a5bcd8a8e6eac01316b81f, head b58440f8b1980ba2fd2befa81e41cb126983123c. Windows 11 Pro x64, WinLibs MinGW-w64 GCC 15.2.0 r7, Release, BUILD_TESTING=ON; Ryzen 9 7900X, 32 GB RAM, RTX 4070 Ti SUPER (CPU tests).

  • cmake --build ../build --parallel 10: complete default all-target build passed, without a source overlay. The build uses the repository-pinned dependencies built from source.
  • ctest --test-dir ../build -R '^public_headers($|_compile_)' -j 4 --output-on-failure: 65/65 passed, no skips, 55.22 s. The audit verifies 368 library umbrellas and 12,177 exports, and all registered header compilation batches passed.
  • Before the correction, the public-header audit on merged revision 5839f54a7cb4dec9a0d04cbbe498cacd46733bba failed on the four duplicate declarations and two SFNT declarations found only in the test. The corrected revision passes the same audit and includes the library umbrella in the SFNT runtime test.
  • ctest --test-dir ../build -R '^(guest_freetype.*|guest_.*font.*|font_.*|patched_libraries)$' -V: 19/19 passed, no skips, 1.24 s. All 368 patched libraries loaded, with 0 failures. The SFNT fixture retains 44 directory comparisons and 479 read comparisons, memory-backed tagged reads, whole-file callback reads above 2 GiB, canaries, error/output checks, alias handling and watched metadata cases. The test no longer requires tagged callback reads to throw on hosts with a 32-bit long; the production limitation and debt remain unchanged.
  • python tools/check_compile.py --build ../build core/libs/prx/libSceFreeType/src/Sfnt.cpp core/libs/tests/GuestFreeTypeSfnt.cpp: both translation units passed.
  • python tools/check_test_registration.py ../build: 1,328 test executables, none built and unregistered. python tools/check_conventions.py --base origin/main and git diff --check passed.
  • python tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch codex/pr9420-review-20261025: the sole unresolved automated comparison is conflicted #9848, sharing CMake and debt files. Manual comparison confirms independent macOS visibility/import-test changes, with no duplicate SFNT implementation or required merge order.

The full CTest suite was not run. Linux, macOS, games and PS5 hardware were not tested here.

Earlier controls for the original SFNT implementation: at 0c6a5bd5938fde7bbc0bf720927c4584bd3e4700, omitting src/Sfnt.cpp while keeping the expanded test registered failed to link the missing guest functions. An earlier compiled mutation that wrote the requested length back after reading corrupted an overlapping destination and failed the regression; restoring the adapter passed. These controls were not repeated for this declaration/test cleanup.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4958537579 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9420). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-21T21:21:22Z. --- ### What Add `FT_Sfnt_Table_Info` and `FT_Load_Sfnt_Table` to the guest FreeType bridge. Callers can enumerate the SFNT directory, query table/file lengths, and read raw table or whole-font bytes. The adapter preserves LP64 tags, lengths and offsets, including high-bit table tags, aliased outputs and partially written buffers on short-read errors. The length field is written only for zero-length queries, so an aliased destination is not overwritten after reading. Each successful `FT_Sfnt_Table_Info` metadata output and the zero-length table query now open `GuestArena::HostWrite` for the exact eight-byte output before storing; separate scopes preserve native tag-then-length ordering when the pointers alias. Data reads continue to hold `HostWrite` over the possible destination writes. A requested length of zero queries metadata without touching the data buffer. On 32-bit-`long` hosts, memory-backed tagged reads recover the absolute table position with a native one-byte probe, restore the original cursor, then read at the validated absolute file position. This prevents 32-bit offset wrap without rejecting positions above signed 32 bits. Tagged reads through native stream callbacks throw on those hosts because probing could change callback state; the limitation is in debt. The SFNT declarations live in the public `include/Api.hpp` reached by `libSceFreeType.hpp`. The regression test includes that umbrella and uses its typed `GuestLibrary*` API without local export declarations. Supported wide-stream reads and error/output checks remain covered. Names/NIDs `oiRP-fp3SN4` and `09vKaCuNyqI` are in the [PS5 3.20 inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceFreeTypeOt.c). Signatures, errors and stream behavior follow bundled FreeType 2.13.3: [public entry points](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/ftobjs.c#L4396), [table access](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/sfnt/ttload.c#L568) and [stream reads](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/ftstream.c#L118). Console equivalence remains unverified. No matching implementation was found in main. The current open-PR overlap scan reports only conflicted #9848; manual inspection of its shared CMake and debt changes confirms that it addresses macOS symbol visibility and separate import tests, without duplicating SFNT access. ### Tested Current revision: base `af49a0788576ca8878a5bcd8a8e6eac01316b81f`, head `b58440f8b1980ba2fd2befa81e41cb126983123c`. Windows 11 Pro x64, WinLibs MinGW-w64 GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`; Ryzen 9 7900X, 32 GB RAM, RTX 4070 Ti SUPER (CPU tests). - `cmake --build ../build --parallel 10`: complete default all-target build passed, without a source overlay. The build uses the repository-pinned dependencies built from source. - `ctest --test-dir ../build -R '^public_headers($|_compile_)' -j 4 --output-on-failure`: **65/65 passed, no skips**, 55.22 s. The audit verifies 368 library umbrellas and 12,177 exports, and all registered header compilation batches passed. - Before the correction, the public-header audit on merged revision `5839f54a7cb4dec9a0d04cbbe498cacd46733bba` failed on the four duplicate declarations and two SFNT declarations found only in the test. The corrected revision passes the same audit and includes the library umbrella in the SFNT runtime test. - `ctest --test-dir ../build -R '^(guest_freetype.*|guest_.*font.*|font_.*|patched_libraries)$' -V`: **19/19 passed, no skips**, 1.24 s. All 368 patched libraries loaded, with 0 failures. The SFNT fixture retains 44 directory comparisons and 479 read comparisons, memory-backed tagged reads, whole-file callback reads above 2 GiB, canaries, error/output checks, alias handling and watched metadata cases. The test no longer requires tagged callback reads to throw on hosts with a 32-bit `long`; the production limitation and debt remain unchanged. - `python tools/check_compile.py --build ../build core/libs/prx/libSceFreeType/src/Sfnt.cpp core/libs/tests/GuestFreeTypeSfnt.cpp`: both translation units passed. - `python tools/check_test_registration.py ../build`: 1,328 test executables, none built and unregistered. `python tools/check_conventions.py --base origin/main` and `git diff --check` passed. - `python tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch codex/pr9420-review-20261025`: the sole unresolved automated comparison is conflicted #9848, sharing CMake and debt files. Manual comparison confirms independent macOS visibility/import-test changes, with no duplicate SFNT implementation or required merge order. The full CTest suite was not run. Linux, macOS, games and PS5 hardware were not tested here. Earlier controls for the original SFNT implementation: at `0c6a5bd5938fde7bbc0bf720927c4584bd3e4700`, omitting `src/Sfnt.cpp` while keeping the expanded test registered failed to link the missing guest functions. An earlier compiled mutation that wrote the requested length back after reading corrupted an overlapping destination and failed the regression; restoring the adapter passed. These controls were not repeated for this declaration/test cleanup. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes
MarcZX10 commented 2026-10-22 04:59:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:59:12Z.

Code review location: core/libs/prx/libSceFreeType/src/Sfnt.cpp; line 37

@@ -0,0 +1,88 @@
+#include "prx/libSceFreeType/include/GuestObjects.hpp"
+#include "prx/libc/include/GuestArena.hpp"
+
+#include FT_TRUETYPE_TABLES_H
+
+#include <algorithm>
+#include <bit>
+#include <cstdint>
+#include <limits>
+#include <stdexcept>
+
+using namespace FreeType;
+
+namespace {
+
+FT_Error QueryTableLength(FT_Face face, std::uint64_t tag, FT_ULong& length) {
+    length = 0;
+    if constexpr (sizeof(FT_ULong) < sizeof(tag)) {
+        if (tag > std::numeric_limits<FT_ULong>::max()) {
+            const auto error = FT_Load_Sfnt_Table(face, 0, 0, nullptr, &length);
+            return error != 0 ? error : FT_Err_Table_Missing;
+        }
+    }
+    return FT_Load_Sfnt_Table(face, static_cast<FT_ULong>(tag), 0, nullptr, &length);
+}
+
+}
+
+extern "C" {
+
+int APS5_VABI FT_Sfnt_Table_Info_nid_postfix(GuestFaceRec* face, std::uint32_t tableIndex, std::uint64_t* tag, std::uint64_t* length) {
+    const GuestFace* owner = FaceOf(face);
+    FT_ULong nativeTag = 0;
+    FT_ULong nativeLength = 0;
+    const auto error = FT_Sfnt_Table_Info(owner ? owner->host : nullptr, tableIndex, tag ? &nativeTag : nullptr, length ? &nativeLength : nullptr);
+    if (error == 0) {
+        if (tag) *tag = nativeTag;

These guest output stores bypass GuestArena::HostWrite, which is used for the data buffer below. On the Apple write-watch path, GuestWriteWatch::BeginHostWrite opens protected guest pages and records the host write; a direct store to tag/length in a watched guest page can instead fault or miss that write tracking. The zero-length query also writes *length directly at line 51. Please guard the metadata output ranges with HostWrite (handling aliased outputs) and add a regression that places the outputs in a write-watched GuestArena region; the current stack-array fixtures do not exercise this case.

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4344706357 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9420#discussion_r4344706357). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T04:59:12Z. **Code review location:** `core/libs/prx/libSceFreeType/src/Sfnt.cpp`; line 37 ~~~~diff @@ -0,0 +1,88 @@ +#include "prx/libSceFreeType/include/GuestObjects.hpp" +#include "prx/libc/include/GuestArena.hpp" + +#include FT_TRUETYPE_TABLES_H + +#include <algorithm> +#include <bit> +#include <cstdint> +#include <limits> +#include <stdexcept> + +using namespace FreeType; + +namespace { + +FT_Error QueryTableLength(FT_Face face, std::uint64_t tag, FT_ULong& length) { + length = 0; + if constexpr (sizeof(FT_ULong) < sizeof(tag)) { + if (tag > std::numeric_limits<FT_ULong>::max()) { + const auto error = FT_Load_Sfnt_Table(face, 0, 0, nullptr, &length); + return error != 0 ? error : FT_Err_Table_Missing; + } + } + return FT_Load_Sfnt_Table(face, static_cast<FT_ULong>(tag), 0, nullptr, &length); +} + +} + +extern "C" { + +int APS5_VABI FT_Sfnt_Table_Info_nid_postfix(GuestFaceRec* face, std::uint32_t tableIndex, std::uint64_t* tag, std::uint64_t* length) { + const GuestFace* owner = FaceOf(face); + FT_ULong nativeTag = 0; + FT_ULong nativeLength = 0; + const auto error = FT_Sfnt_Table_Info(owner ? owner->host : nullptr, tableIndex, tag ? &nativeTag : nullptr, length ? &nativeLength : nullptr); + if (error == 0) { + if (tag) *tag = nativeTag; ~~~~ --- These guest output stores bypass `GuestArena::HostWrite`, which is used for the data buffer below. On the Apple write-watch path, `GuestWriteWatch::BeginHostWrite` opens protected guest pages and records the host write; a direct store to `tag`/`length` in a watched guest page can instead fault or miss that write tracking. The zero-length query also writes `*length` directly at line 51. Please guard the metadata output ranges with `HostWrite` (handling aliased outputs) and add a regression that places the outputs in a write-watched GuestArena region; the current stack-array fixtures do not exercise this case.
MarcZX10 commented 2026-10-22 04:59:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:59:12Z.

Review: DISMISSED


The fixtures exercise a broad set of FreeType edge cases, but the current branch is 417 commits behind current main 524398151f1e (merge base 0c6a5bd5938f). The repository requires PRs to stay current with main; please rebase and rerun the focused font tests, conventions check and overlap scan on the rebased result. The current hosted checks are policy checks only; the Windows build/test results in the description are author-run on the old base. I did not run local tests for this head because there is no matching configured build here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609456920 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9420#pullrequestreview-5609456920). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T04:59:12Z. **Review:** DISMISSED --- The fixtures exercise a broad set of FreeType edge cases, but the current branch is 417 commits behind current `main` `524398151f1e` (merge base `0c6a5bd5938f`). The repository requires PRs to stay current with `main`; please rebase and rerun the focused font tests, conventions check and overlap scan on the rebased result. The current hosted checks are policy checks only; the Windows build/test results in the description are author-run on the old base. I did not run local tests for this head because there is no matching configured build here.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!9420
No description provided.