feat(libSceFreeTypeOt): implement the FreeType stream calls #12473

Merged
simnJS merged 2 commits from upstream-pr/12473 into main 2026-10-29 13:44:27 +00:00
simnJS commented 2026-10-28 15:10:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simnJS on 2026-10-28T15:10:08Z.


What

Implements the 36 FreeType stream calls that libSceFreeTypeOt exports and main declares as sceFreeTypeOtUnknown_* throwing aliases. #12457 found their names with tools/nid_names.py; each NID below is compute_nid of its name.

Group Exports
Position and raw reads FT_Stream_OpenMemory, FT_Stream_Close, FT_Stream_Seek, FT_Stream_Skip, FT_Stream_Pos, FT_Stream_Read, FT_Stream_ReadAt, FT_Stream_TryRead
Frames FT_Stream_EnterFrame, FT_Stream_ExitFrame, FT_Stream_ExtractFrame, FT_Stream_ReleaseFrame, FT_Stream_ReadFields
Frame getters FT_Stream_GetChar, GetShort, GetShortLE, GetUShort, GetUShortLE, GetOffset, GetUOffset, GetLong, GetLongLE, GetULong, GetULongLE
Stream readers FT_Stream_ReadChar, ReadShort, ReadShortLE, ReadUShort, ReadUShortLE, ReadOffset, ReadUOffset, ReadLong, ReadLongLE, ReadULong, ReadULongLE
Release FT_Stream_Free

Reference: FreeType 2.4.12 src/base/ftstream.c (L34-L862) and FT_Stream_Free; the record and frame-field layouts are ftsystem.h and internal/ftstream.h. The PS5 3.20 inventory holds both the signed GetShort/GetOffset/GetLong/ReadShort/ReadOffset/ReadLong (+LE) readers and the unsigned ones that replaced them in 2.4.5 (21b1a0de7c); the signed ones follow 2.4.4 ftstream.c, whose bodies differ from 2.4.12 only in the signed FT_NEXT_* macros. The bodies are unchanged through 2.10.4 apart from return types, so the result does not depend on which of those versions the console built.

  • GuestStreamRec is the LP64 FT_StreamRec (80 bytes: unsigned long size and position, pointer descriptors), GuestFrameField is FT_Frame_Field (4 bytes); both are asserted. The allocator record is libSceFreeType's existing GuestMemoryRec. Read, close, allocation and release callbacks are guest functions, called with the guest stream or memory record as FreeType does.
  • Arithmetic is FreeType's: pos + n < size and pos + distance wrap as unsigned long, frame bounds are pointer comparisons, frame allocation passes count as a signed FT_Long (so a count above LONG_MAX gives Invalid_Argument), and a short device frame read releases the block, leaves cursor null and limit at count, and still advances the position. A memory stream with a null base reads 0 at position 0 as FreeType's if ( p ) does. FT_Stream_ReadFields reads integer fields without a frame bound check and stores them by field size (1, 2, 4, otherwise 8 bytes), sign-extending through 32 bits.
  • Where FreeType would dereference a null stream, error pointer, frame pointer, buffer, structure, frame cursor, memory record or memory-stream base, the call throws std::invalid_argument, and it does so before anything is acquired or moved: FT_Stream_ExtractFrame checks its output pointer before entering the frame, a frame allocation needs the allocator and release callbacks before it allocates (FreeType would call the null release on FT_Stream_ExitFrame), FT_Stream_ReadFields exits a frame it entered before refusing a null structure or cursor, and FT_Stream_Free checks the release callback before calling close. FT_Stream_ReadFields with a null stream and non-null fields also throws: it returns Invalid_Argument up to 2.5.3 and Invalid_Stream_Handle from 2.5.4 (0d4aa23e7).
  • The 36 aliases leave Export.cpp; the functions are defined as FT_Stream_*_nid_postfix in libSceFreeTypeOt/Stream.cpp, so the patched module exports them under the same NIDs. Declarations and layouts are in libSceFreeTypeOt/include/Stream.hpp, included by libSceFreeTypeOt.hpp. The sources and tests come in through Stream.cmake, one include line in the library's CMakeLists.txt, placed where none of the open pull requests on that file edits.
  • FT_Stream_New (needs the library object and FT_Open_Args) and FT_Stream_Open (opens a host file) still throw. An entry under Unknown function info records the version and null-pointer gaps. The The 129 sceFreeTypeOtUnknown_* count line is left as it is: many open pull requests edit that line (it is already 126 on main).

Landing order with #12370. #12370 (kyon-eth) keeps the 36 sceFreeTypeOtUnknown_* stream aliases this PR removes, and its CMakeLists.txt and libSceFreeTypeOt.hpp predate Stream.cmake and Stream.hpp. Nothing competes (none of its 68 forwards or 96 declarations is a stream export), but a textual merge of the two heads keeps 20 of the aliases next to the implementations, so the second to land must reconcile Export.cpp. Agreed with kyon-eth (acceptance on #12370, confirmation here): this PR lands first; #12370 declares Depends on: #12473 and, once this lands, rebases to keep Stream.cmake and Stream.hpp and drop the 36 stream aliases.

tools/progress.py: libSceFreeTypeOt 2/148 → 38/148; all libraries 6627/14197 (46.68 %) → 6663/14197 (46.93 %).

Tested

Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), Release, BUILD_TESTING=ON, rebased on main 17f8dfad51. No title run.

  • New guest_freetype_stream and guest_freetype_stream_patched: the same cases run against the raw libSceFreeTypeOt.prx (resolving FT_Stream_*_nid_postfix) and the NID-patched one (resolving the 36 NIDs). They cover:
    • memory streams: OpenMemory fields (limit, memory, descriptor and path name untouched), seek to the end and past it, negative skip with a null stream, skip that wraps unsigned long, Pos of ULONG_MAX as -1, full, short and past-the-end ReadAt/Read/TryRead, null buffers with and without bytes;
    • frames: entering, reading at and past the limit, empty frames, frames past the end, ExtractFrame/ReleaseFrame, every Get* value (signed and unsigned, big- and little-endian, 1/2/3/4 bytes) and the no-frame and null-cursor cases;
    • every Read* value, the bounds (pos + n - 1 >= size), a position of ULONG_MAX that wraps to read base - 1, a null base at position 0 (reads 0) and past it (throws), a null error pointer;
    • device streams with guest-ABI callbacks: seek through read(stream, pos, NULL, 0) without a size check, callback arguments, short reads, frame allocation through the guest allocator, allocation failure (Out_Of_Memory, base cleared), a short frame read (block released, limit left at count), an empty frame with no allocator, a count above LONG_MAX (Invalid_Argument), ReadChar ignoring the size, Close, and FT_Stream_Free reading memory before the close callback clears it, closing before releasing, and not releasing an external stream;
    • ReadFields: every integer operation with 1-, 2-, 4- and 8-byte stores and sign extension, skip, byte sequences, a sequence past the frame, an integer read past the frame, an unknown operation ending the list, a failing frame start, a device frame released on exit, null fields (Invalid_Argument), null stream and null structure (throw);
    • refusals that leave nothing behind: ExtractFrame with a null output on a memory and a device stream (no allocation, no read, no release, base, pos, cursor and limit unchanged), a frame with an allocator but no release callback (refused before allocating or reading; an empty frame and a frame larger than the stream still answer without it), ReadFields refusing a null structure inside a device and a memory frame (the frame is released and exited first), FT_Stream_Free without a release callback (refused before close).
  • All 38 FreeType tests (ctest -R freetype: libSceFreeType, libSceFreeTypeOl and libSceFreeTypeOt, raw and patched) pass, 5 runs each (--repeat until-fail:5). Mutation check: 12 single-line changes to Stream.cpp each make the raw test fail (the 8 listed in the first revision, plus undoing each of the four refusal-order fixes).
  • Control on main's library (same test executable): the raw run fails with missing stream export, the patched run aborts with sceFreeTypeOtUnknown_a7RlARfexoo_nid_no_patch not implemented.
  • public_headers, public_headers_compile_*, conventions_*, test_registration, test_script_registration: 107/107 pass. The new files build without warnings.
  • python tools/check_conventions.py --base origin/main with this title and body: all checks pass.
  • Overlap: for each of the 24 open pull requests that touch libSceFreeTypeOt, git merge-tree of this branch with its head gives exactly the conflicts it already has with main, and no new one. #12370 needs the reconciliation described above.
  • Full suites on GitHub runners for this head (fe446f1a1d, merged into main): Windows run 40720082916 (upstream build.yml, full), 2842/2842 tests pass; Linux run 40720087392 (Ubuntu 24.04, lavapipe), 2726/2726 tests pass. guest_freetype_stream and guest_freetype_stream_patched pass in both.

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 (#12370 lands after this PR and depends on it, as agreed with kyon-eth in this comment)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5052840332 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12473). Originally posted by [simnJS](https://github.com/simnJS) on 2026-10-28T15:10:08Z. --- ### What Implements the 36 FreeType stream calls that libSceFreeTypeOt exports and `main` declares as `sceFreeTypeOtUnknown_*` throwing aliases. #12457 found their names with `tools/nid_names.py`; each NID below is `compute_nid` of its name. | Group | Exports | |---|---| | Position and raw reads | `FT_Stream_OpenMemory`, `FT_Stream_Close`, `FT_Stream_Seek`, `FT_Stream_Skip`, `FT_Stream_Pos`, `FT_Stream_Read`, `FT_Stream_ReadAt`, `FT_Stream_TryRead` | | Frames | `FT_Stream_EnterFrame`, `FT_Stream_ExitFrame`, `FT_Stream_ExtractFrame`, `FT_Stream_ReleaseFrame`, `FT_Stream_ReadFields` | | Frame getters | `FT_Stream_GetChar`, `GetShort`, `GetShortLE`, `GetUShort`, `GetUShortLE`, `GetOffset`, `GetUOffset`, `GetLong`, `GetLongLE`, `GetULong`, `GetULongLE` | | Stream readers | `FT_Stream_ReadChar`, `ReadShort`, `ReadShortLE`, `ReadUShort`, `ReadUShortLE`, `ReadOffset`, `ReadUOffset`, `ReadLong`, `ReadLongLE`, `ReadULong`, `ReadULongLE` | | Release | `FT_Stream_Free` | Reference: FreeType 2.4.12 [`src/base/ftstream.c`](https://github.com/freetype/freetype/blob/cecf93ef90c660a8b0b45e5adbbfb5ea443fb6b9/src/base/ftstream.c) (L34-L862) and [`FT_Stream_Free`](https://github.com/freetype/freetype/blob/cecf93ef90c660a8b0b45e5adbbfb5ea443fb6b9/src/base/ftobjs.c#L205-L219); the record and frame-field layouts are [`ftsystem.h`](https://github.com/freetype/freetype/blob/cecf93ef90c660a8b0b45e5adbbfb5ea443fb6b9/include/freetype/ftsystem.h) and [`internal/ftstream.h`](https://github.com/freetype/freetype/blob/cecf93ef90c660a8b0b45e5adbbfb5ea443fb6b9/include/freetype/internal/ftstream.h). The [PS5 3.20 inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceFreeTypeOt.c) holds both the signed `GetShort`/`GetOffset`/`GetLong`/`ReadShort`/`ReadOffset`/`ReadLong` (+LE) readers and the unsigned ones that replaced them in 2.4.5 ([`21b1a0de7c`](https://github.com/freetype/freetype/commit/21b1a0de7c052dcd25348c4e3597c8a631108f61)); the signed ones follow 2.4.4 [`ftstream.c`](https://github.com/freetype/freetype/blob/9ec31cea16a492bed96a96a7c30c1a466adb9792/src/base/ftstream.c), whose bodies differ from 2.4.12 only in the signed `FT_NEXT_*` macros. The bodies are unchanged through 2.10.4 apart from return types, so the result does not depend on which of those versions the console built. - `GuestStreamRec` is the LP64 `FT_StreamRec` (80 bytes: `unsigned long` size and position, pointer descriptors), `GuestFrameField` is `FT_Frame_Field` (4 bytes); both are asserted. The allocator record is libSceFreeType's existing `GuestMemoryRec`. Read, close, allocation and release callbacks are guest functions, called with the guest stream or memory record as FreeType does. - Arithmetic is FreeType's: `pos + n < size` and `pos + distance` wrap as `unsigned long`, frame bounds are pointer comparisons, frame allocation passes `count` as a signed `FT_Long` (so a count above `LONG_MAX` gives `Invalid_Argument`), and a short device frame read releases the block, leaves `cursor` null and `limit` at `count`, and still advances the position. A memory stream with a null base reads 0 at position 0 as FreeType's `if ( p )` does. `FT_Stream_ReadFields` reads integer fields without a frame bound check and stores them by field size (1, 2, 4, otherwise 8 bytes), sign-extending through 32 bits. - Where FreeType would dereference a null stream, error pointer, frame pointer, buffer, structure, frame cursor, memory record or memory-stream base, the call throws `std::invalid_argument`, and it does so before anything is acquired or moved: `FT_Stream_ExtractFrame` checks its output pointer before entering the frame, a frame allocation needs the allocator and release callbacks before it allocates (FreeType would call the null release on `FT_Stream_ExitFrame`), `FT_Stream_ReadFields` exits a frame it entered before refusing a null structure or cursor, and `FT_Stream_Free` checks the release callback before calling `close`. `FT_Stream_ReadFields` with a null stream and non-null fields also throws: it returns `Invalid_Argument` up to 2.5.3 and `Invalid_Stream_Handle` from 2.5.4 ([`0d4aa23e7`](https://github.com/freetype/freetype/commit/0d4aa23e7)). - The 36 aliases leave `Export.cpp`; the functions are defined as `FT_Stream_*_nid_postfix` in `libSceFreeTypeOt/Stream.cpp`, so the patched module exports them under the same NIDs. Declarations and layouts are in `libSceFreeTypeOt/include/Stream.hpp`, included by `libSceFreeTypeOt.hpp`. The sources and tests come in through `Stream.cmake`, one `include` line in the library's `CMakeLists.txt`, placed where none of the open pull requests on that file edits. - `FT_Stream_New` (needs the library object and `FT_Open_Args`) and `FT_Stream_Open` (opens a host file) still throw. An entry under Unknown function info records the version and null-pointer gaps. The `The 129 sceFreeTypeOtUnknown_*` count line is left as it is: many open pull requests edit that line (it is already 126 on `main`). **Landing order with #12370.** #12370 (kyon-eth) keeps the 36 `sceFreeTypeOtUnknown_*` stream aliases this PR removes, and its `CMakeLists.txt` and `libSceFreeTypeOt.hpp` predate `Stream.cmake` and `Stream.hpp`. Nothing competes (none of its 68 forwards or 96 declarations is a stream export), but a textual merge of the two heads keeps 20 of the aliases next to the implementations, so the second to land must reconcile `Export.cpp`. Agreed with kyon-eth ([acceptance on #12370](https://github.com/boykopovar/AnyPS5/pull/12370#issuecomment-6475285048), [confirmation here](https://github.com/boykopovar/AnyPS5/pull/12473#issuecomment-6475346991)): this PR lands first; #12370 declares `Depends on: #12473` and, once this lands, rebases to keep `Stream.cmake` and `Stream.hpp` and drop the 36 stream aliases. `tools/progress.py`: libSceFreeTypeOt 2/148 → 38/148; all libraries 6627/14197 (46.68 %) → 6663/14197 (46.93 %). ### Tested Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), Release, `BUILD_TESTING=ON`, rebased on `main` `17f8dfad51`. No title run. - New `guest_freetype_stream` and `guest_freetype_stream_patched`: the same cases run against the raw `libSceFreeTypeOt.prx` (resolving `FT_Stream_*_nid_postfix`) and the NID-patched one (resolving the 36 NIDs). They cover: - memory streams: `OpenMemory` fields (limit, memory, descriptor and path name untouched), seek to the end and past it, negative skip with a null stream, skip that wraps `unsigned long`, `Pos` of `ULONG_MAX` as -1, full, short and past-the-end `ReadAt`/`Read`/`TryRead`, null buffers with and without bytes; - frames: entering, reading at and past the limit, empty frames, frames past the end, `ExtractFrame`/`ReleaseFrame`, every `Get*` value (signed and unsigned, big- and little-endian, 1/2/3/4 bytes) and the no-frame and null-cursor cases; - every `Read*` value, the bounds (`pos + n - 1 >= size`), a position of `ULONG_MAX` that wraps to read `base - 1`, a null base at position 0 (reads 0) and past it (throws), a null error pointer; - device streams with guest-ABI callbacks: seek through `read(stream, pos, NULL, 0)` without a size check, callback arguments, short reads, frame allocation through the guest allocator, allocation failure (`Out_Of_Memory`, base cleared), a short frame read (block released, `limit` left at `count`), an empty frame with no allocator, a count above `LONG_MAX` (`Invalid_Argument`), `ReadChar` ignoring the size, `Close`, and `FT_Stream_Free` reading `memory` before the close callback clears it, closing before releasing, and not releasing an external stream; - `ReadFields`: every integer operation with 1-, 2-, 4- and 8-byte stores and sign extension, skip, byte sequences, a sequence past the frame, an integer read past the frame, an unknown operation ending the list, a failing frame start, a device frame released on exit, null fields (`Invalid_Argument`), null stream and null structure (throw); - refusals that leave nothing behind: `ExtractFrame` with a null output on a memory and a device stream (no allocation, no read, no release, `base`, `pos`, `cursor` and `limit` unchanged), a frame with an allocator but no release callback (refused before allocating or reading; an empty frame and a frame larger than the stream still answer without it), `ReadFields` refusing a null structure inside a device and a memory frame (the frame is released and exited first), `FT_Stream_Free` without a release callback (refused before `close`). - All 38 FreeType tests (`ctest -R freetype`: libSceFreeType, libSceFreeTypeOl and libSceFreeTypeOt, raw and patched) pass, 5 runs each (`--repeat until-fail:5`). Mutation check: 12 single-line changes to `Stream.cpp` each make the raw test fail (the 8 listed in the first revision, plus undoing each of the four refusal-order fixes). - Control on `main`'s library (same test executable): the raw run fails with `missing stream export`, the patched run aborts with `sceFreeTypeOtUnknown_a7RlARfexoo_nid_no_patch not implemented`. - `public_headers`, `public_headers_compile_*`, `conventions_*`, `test_registration`, `test_script_registration`: 107/107 pass. The new files build without warnings. - `python tools/check_conventions.py --base origin/main` with this title and body: all checks pass. - Overlap: for each of the 24 open pull requests that touch libSceFreeTypeOt, `git merge-tree` of this branch with its head gives exactly the conflicts it already has with `main`, and no new one. #12370 needs the reconciliation described above. - Full suites on GitHub runners for this head (`fe446f1a1d`, merged into `main`): Windows [run 40720082916](https://github.com/simnJS/AnyPS5/actions/runs/40720082916) (upstream `build.yml`, full), 2842/2842 tests pass; Linux [run 40720087392](https://github.com/simnJS/AnyPS5/actions/runs/40720087392) (Ubuntu 24.04, lavapipe), 2726/2726 tests pass. `guest_freetype_stream` and `guest_freetype_stream_patched` pass in both. ### 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: none (#12370 lands after this PR and depends on it, as agreed with kyon-eth in [this comment](https://github.com/boykopovar/AnyPS5/pull/12370#issuecomment-6475285048)) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-28 15:11:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T15:11:36Z.

Review: CHANGES_REQUESTED


I found one blocking cleanup issue in FT_Stream_ExtractFrame; the rest of this review is limited to the finding below. The branch also still needs review against the latest main head 17f8dfad513b134a4996991109fc9db47a3bb905 before merge, though its current merge tree is clean.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5684968527 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12473#pullrequestreview-5684968527). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T15:11:36Z. **Review:** CHANGES_REQUESTED --- I found one blocking cleanup issue in `FT_Stream_ExtractFrame`; the rest of this review is limited to the finding below. The branch also still needs review against the latest `main` head `17f8dfad513b134a4996991109fc9db47a3bb905` before merge, though its current merge tree is clean.
MarcZX10 commented 2026-10-29 06:19:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T06:19:28Z.

Review: APPROVED


Re-checked — approved

The latest head fe446f1a1dc1e2fb6b640ec08b34b42b7abfe906 fixes the FT_Stream_ExtractFrame cleanup path: the output pointer is validated before frame acquisition, and the new memory/device cases check that failure does not change stream position or leak/release frame state. The additional allocator, ReadFields, and FT_Stream_Free failure-path regressions are also included. The author reports both raw and NID-patched stream test executables passing on Windows.

The #12370 author has now confirmed #12473 first and recorded Depends on: #12473 on #12370; I independently found no duplicate FT_Stream_* provider in the other named export patches. The branch is mergeable against current main; although main has advanced since the last refresh, none of this PR’s touched files changed on main in the interim. Hosted Windows, conventions, labels, and eligibility checks pass. I did not run the stream tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5693641980 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12473#pullrequestreview-5693641980). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T06:19:28Z. **Review:** APPROVED --- ## Re-checked — approved The latest head `fe446f1a1dc1e2fb6b640ec08b34b42b7abfe906` fixes the `FT_Stream_ExtractFrame` cleanup path: the output pointer is validated before frame acquisition, and the new memory/device cases check that failure does not change stream position or leak/release frame state. The additional allocator, `ReadFields`, and `FT_Stream_Free` failure-path regressions are also included. The author reports both raw and NID-patched stream test executables passing on Windows. The #12370 author has now confirmed #12473 first and recorded `Depends on: #12473` on #12370; I independently found no duplicate `FT_Stream_*` provider in the other named export patches. The branch is mergeable against current main; although main has advanced since the last refresh, none of this PR’s touched files changed on main in the interim. Hosted Windows, conventions, labels, and eligibility checks pass. I did not run the stream tests locally.
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!12473
No description provided.