feat(libcairo): implement the FreeType font face lifetime exports #18406

Merged
simnJS merged 7 commits from upstream-pr/18406 into main 2026-11-17 23:49:37 +00:00
simnJS commented 2026-11-11 13:29:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simnJS on 2026-11-11T13:29:06Z.


What

Implements cairo_ft_font_face_create_for_ft_face, cairo_font_face_reference, cairo_font_face_destroy, cairo_font_face_get_user_data and cairo_font_face_set_user_data following Cairo 1.18.0. This addresses the review of #16892:

  • Faces are cached by guest FT_Face, face_index and load flags, as in _cairo_ft_unscaled_font_keys_equal and _cairo_ft_font_face_create. A repeated call returns the same face with one more reference and shared user data.
  • The face does not take a reference on the FT_Face; Cairo does not either.
  • An allocation failure returns the nil face (NO_MEMORY). No invented status codes are used.
  • User data follows _cairo_user_data_array_set_data: replacing or clearing a key releases the old data, and an empty slot is reused.
  • The last destroy removes the face from the cache, then releases user data in reverse order, so a create from a finalizer gets a new face, as in Cairo.
  • Like Cairo's __put, the last reference stays while the finalizers run, so a finalizer may reference and destroy the face.
  • cairo_ft_font_options_substitute is not touched (#16895).

Recorded in the debt file:

  • Setting user data on a face from its own finalizer throws, and so does destroying the face there without a reference of the finalizer's own. Cairo modifies or frees what it is finalizing in those cases.
  • Cairo's "zombie" face, kept while a scaled font holds the unscaled font, is not modelled, because the scaled-font exports still throw.

tools/guest_pointer_audit.py reports the read of face->face_index; Cairo reads it directly too.

Tested

  • Linux (Ubuntu 24.04, GCC 13, Release): the 9 guest_cairo_* tests and public_headers pass. guest_cairo_font_face also passed under ASan and UBSan on an earlier stack of this change.
  • The cache, shared user data, reverse destroy order and the create-from-finalizer behaviour were first checked against the host's Cairo 1.18.0 with a real FT_Face.
  • Negative controls: disabling the cache, or releasing user data in forward order, fails guest_cairo_font_face.
  • Not run on Windows.

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:5260343708 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18406). Originally posted by [simnJS](https://github.com/simnJS) on 2026-11-11T13:29:06Z. --- ### What Implements `cairo_ft_font_face_create_for_ft_face`, `cairo_font_face_reference`, `cairo_font_face_destroy`, `cairo_font_face_get_user_data` and `cairo_font_face_set_user_data` following Cairo 1.18.0. This addresses the review of #16892: - Faces are cached by guest `FT_Face`, `face_index` and load flags, as in `_cairo_ft_unscaled_font_keys_equal` and `_cairo_ft_font_face_create`. A repeated call returns the same face with one more reference and shared user data. - The face does not take a reference on the `FT_Face`; Cairo does not either. - An allocation failure returns the nil face (`NO_MEMORY`). No invented status codes are used. - User data follows `_cairo_user_data_array_set_data`: replacing or clearing a key releases the old data, and an empty slot is reused. - The last destroy removes the face from the cache, then releases user data in reverse order, so a create from a finalizer gets a new face, as in Cairo. - Like Cairo's `__put`, the last reference stays while the finalizers run, so a finalizer may reference and destroy the face. - `cairo_ft_font_options_substitute` is not touched (#16895). Recorded in the debt file: - Setting user data on a face from its own finalizer throws, and so does destroying the face there without a reference of the finalizer's own. Cairo modifies or frees what it is finalizing in those cases. - Cairo's "zombie" face, kept while a scaled font holds the unscaled font, is not modelled, because the scaled-font exports still throw. `tools/guest_pointer_audit.py` reports the read of `face->face_index`; Cairo reads it directly too. ### Tested - Linux (Ubuntu 24.04, GCC 13, Release): the 9 `guest_cairo_*` tests and `public_headers` pass. `guest_cairo_font_face` also passed under ASan and UBSan on an earlier stack of this change. - The cache, shared user data, reverse destroy order and the create-from-finalizer behaviour were first checked against the host's Cairo 1.18.0 with a real `FT_Face`. - Negative controls: disabling the cache, or releasing user data in forward order, fails `guest_cairo_font_face`. - Not run on Windows. ### Checklist - [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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-11 15:42:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-11T15:42:15Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 18161f0f80:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_cairo_font_face_tests built cleanly.
  • CTest (guest_cairo_font_face):
    1/1 Test #4230: guest_cairo_font_face ............   Passed    0.00 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libcairo/Export.cpp, core/libs/prx/libcairo/include/InventoryExports.hpp, core/libs/prx/libcairo/libcairo.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified null-pointer handling and boundary checks in cairo_ft_font_face_create_for_ft_face, cairo_font_face_reference, cairo_font_face_destroy, cairo_font_face_get_user_data, and cairo_font_face_set_user_data in core/libs/prx/libcairo/FontFace.cpp.
    • Validated thread safety and CAS loop in cairo_font_face_destroy ensuring CacheMutex() locks cache access and Finalize() executes outside the mutex to prevent deadlocks on re-entrant callbacks.
    • Verified LP64 (Linux) vs LLP64 (Windows) portability in CacheKey by explicitly using std::int64_t for face->face_index matching guest PS5 ABI layout across platforms.
    • Audited user data slot reuse, reverse-order destruction in Finalize(), and memory handling under allocation failures (std::bad_alloc returning kNoMemory or NilFace()).
    • Confirmed strict AGENTS.md compliance: zero code comments in newly added files, proper NotImplemented_nid_no_patch invocations, correct debt documentation in docs/dev/debt/libcairo.md, and clean removal of replaced exports from Export.cpp and InventoryExports.hpp.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5861359352 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18406#pullrequestreview-5861359352). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-11T15:42:15Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `18161f0f80`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_cairo_font_face_tests` built cleanly. - **CTest** (`guest_cairo_font_face`): ``` 1/1 Test #4230: guest_cairo_font_face ............ Passed 0.00 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libcairo/Export.cpp`, `core/libs/prx/libcairo/include/InventoryExports.hpp`, `core/libs/prx/libcairo/libcairo.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified null-pointer handling and boundary checks in cairo_ft_font_face_create_for_ft_face, cairo_font_face_reference, cairo_font_face_destroy, cairo_font_face_get_user_data, and cairo_font_face_set_user_data in core/libs/prx/libcairo/FontFace.cpp. - Validated thread safety and CAS loop in cairo_font_face_destroy ensuring CacheMutex() locks cache access and Finalize() executes outside the mutex to prevent deadlocks on re-entrant callbacks. - Verified LP64 (Linux) vs LLP64 (Windows) portability in CacheKey by explicitly using std::int64_t for face->face_index matching guest PS5 ABI layout across platforms. - Audited user data slot reuse, reverse-order destruction in Finalize(), and memory handling under allocation failures (std::bad_alloc returning kNoMemory or NilFace()). - Confirmed strict AGENTS.md compliance: zero code comments in newly added files, proper NotImplemented_nid_no_patch invocations, correct debt documentation in docs/dev/debt/libcairo.md, and clean removal of replaced exports from Export.cpp and InventoryExports.hpp. - Note: the AI-assisted checkbox can stay. Looks Good To Me
MarcZX10 commented 2026-11-12 14:25:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-12T14:25:40Z.

Review: COMMENTED


Full AI review: FreeType font face lifetime exports — no blocking findings

Nothing blocks this; the points below are optional.

What I ran

  • Head 18161f0f80322a836858afe8f786574d69ae0599, base main. Read-only review: no build, no compiler, no tests run.
  • Read the PR description, the commit list, manifest.json, files.json and the full diff.patch.
  • Searched the head for the names the new code depends on, and traced the three test functions in GuestCairoFontFace.cpp by hand against FontFace.cpp.
  • Checked the destroy path: the compare-and-swap loop, the last-reference hold while finalizers run, and reverse-order release of user data.

Results

Check Result Note
Diff read passed 8 files; 3 in full, 5 from hunks
Build and test suite not run no build in this review
Test assertions, traced by reading passed read only, not executed
Symbols used by FontFace.cpp passed defined or declared at head
Stub removals, other references passed checked cairo_font_face_destroy only
Debt file counts passed 73 − 5 = 68
Commands and searches
repo_read.py search GuestFaceRec
repo_read.py search face_index core/libs/prx/libSceFreeType/include
repo_read.py search CairoUserDataKey
repo_read.py search CairoDestroyFunc core/libs/prx/libcairo
repo_read.py search CairoFontFace core/libs
repo_read.py search cairo_font_face_destroy .
repo_read.py search "NotImplemented_nid_no_patch(const" core/libs/prx/libc

Findings

Blocking

None.

Non-blocking

  1. core/libs/prx/libcairo/FontFace.cpp:134: cairo_font_face_set_user_data calls the old slot's destroy before it replaces the slot. A callback that sets the same key again sees the old entry and releases the same data a second time. Failure: set(key, A, DA), then set(key, B, DB). DA(A) runs and calls set(key, C, DC). The slot still holds A, so DA(A) runs again, and C is then overwritten by B and never released. Swapping the slot to the new value first, then calling the saved old destroy, avoids both problems. Cairo's own ordering is not verified here.

Sources

Not verified

  • Build, the nine guest_cairo_* tests, public_headers, and the ASan and UBSan runs: not run here. The PR description reports them; I did not confirm them.
  • Negative controls (cache disabled, user data released in forward order): author-reported, not run.
  • Parity with Cairo 1.18.0 (cache, reverse destroy, create from a finalizer): author-reported against the host's Cairo; not checked here.
  • tools/guest_pointer_audit.py output for the face_index read: not run.
  • SceTypes.hpp, included by include/FontFace.hpp: not located with the reader.
  • How the runtime binds the removed stubs and the new symbols to their NIDs: not traced. The removals in Export.cpp and InventoryExports.hpp were read as diff only.
  • Concurrency: user data get and set run without the cache mutex. Only the destroy path was analysed.
  • Windows: not run, as the PR description says.
  • Files read in full: FontFace.cpp, include/FontFace.hpp and tests/GuestCairoFontFace.cpp. Export.cpp, CMakeLists.txt, libcairo.hpp, InventoryExports.hpp and docs/dev/debt/libcairo.md were reviewed from their diff hunks only. Nothing was excluded.

To get this to a good state

Nothing is required.

Optional:

  • Clear or replace the slot before calling the old destroy (finding 1), and add a test that replaces a key from inside its own destroy callback.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5876090745 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18406#pullrequestreview-5876090745). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-12T14:25:40Z. **Review:** COMMENTED --- # Full AI review: FreeType font face lifetime exports — no blocking findings Nothing blocks this; the points below are optional. ## What I ran - Head `18161f0f80322a836858afe8f786574d69ae0599`, base `main`. Read-only review: no build, no compiler, no tests run. - Read the PR description, the commit list, `manifest.json`, `files.json` and the full `diff.patch`. - Searched the head for the names the new code depends on, and traced the three test functions in `GuestCairoFontFace.cpp` by hand against `FontFace.cpp`. - Checked the destroy path: the compare-and-swap loop, the last-reference hold while finalizers run, and reverse-order release of user data. ## Results | Check | Result | Note | | --- | --- | --- | | Diff read | passed | 8 files; 3 in full, 5 from hunks | | Build and test suite | not run | no build in this review | | Test assertions, traced by reading | passed | read only, not executed | | Symbols used by `FontFace.cpp` | passed | defined or declared at head | | Stub removals, other references | passed | checked `cairo_font_face_destroy` only | | Debt file counts | passed | 73 − 5 = 68 | <details> <summary>Commands and searches</summary> ``` repo_read.py search GuestFaceRec repo_read.py search face_index core/libs/prx/libSceFreeType/include repo_read.py search CairoUserDataKey repo_read.py search CairoDestroyFunc core/libs/prx/libcairo repo_read.py search CairoFontFace core/libs repo_read.py search cairo_font_face_destroy . repo_read.py search "NotImplemented_nid_no_patch(const" core/libs/prx/libc ``` </details> ## Findings **Blocking** None. **Non-blocking** 1. `core/libs/prx/libcairo/FontFace.cpp:134`: `cairo_font_face_set_user_data` calls the old slot's `destroy` before it replaces the slot. A callback that sets the same key again sees the old entry and releases the same data a second time. Failure: `set(key, A, DA)`, then `set(key, B, DB)`. `DA(A)` runs and calls `set(key, C, DC)`. The slot still holds `A`, so `DA(A)` runs again, and `C` is then overwritten by `B` and never released. Swapping the slot to the new value first, then calling the saved old `destroy`, avoids both problems. Cairo's own ordering is not verified here. ## Sources - [FontFace.cpp:134](https://github.com/boykopovar/AnyPS5/blob/18161f0f80322a836858afe8f786574d69ae0599/core/libs/prx/libcairo/FontFace.cpp#L134): destroy called before the slot update. - [GuestFreeType.hpp:203](https://github.com/boykopovar/AnyPS5/blob/18161f0f80322a836858afe8f786574d69ae0599/core/libs/prx/libSceFreeType/include/GuestFreeType.hpp#L203): `face_index` is a field of the guest face record, as the cache key assumes. - [General.hpp:15](https://github.com/boykopovar/AnyPS5/blob/18161f0f80322a836858afe8f786574d69ae0599/core/libs/prx/libc/include/General.hpp#L15): `NotImplemented_nid_no_patch` is `[[noreturn]]`, as the null-face paths assume. ## Not verified - Build, the nine `guest_cairo_*` tests, `public_headers`, and the ASan and UBSan runs: not run here. The PR description reports them; I did not confirm them. - Negative controls (cache disabled, user data released in forward order): author-reported, not run. - Parity with Cairo 1.18.0 (cache, reverse destroy, create from a finalizer): author-reported against the host's Cairo; not checked here. - `tools/guest_pointer_audit.py` output for the `face_index` read: not run. - `SceTypes.hpp`, included by `include/FontFace.hpp`: not located with the reader. - How the runtime binds the removed stubs and the new symbols to their NIDs: not traced. The removals in `Export.cpp` and `InventoryExports.hpp` were read as diff only. - Concurrency: user data get and set run without the cache mutex. Only the destroy path was analysed. - Windows: not run, as the PR description says. - Files read in full: `FontFace.cpp`, `include/FontFace.hpp` and `tests/GuestCairoFontFace.cpp`. `Export.cpp`, `CMakeLists.txt`, `libcairo.hpp`, `InventoryExports.hpp` and `docs/dev/debt/libcairo.md` were reviewed from their diff hunks only. Nothing was excluded. ## To get this to a good state Nothing is required. Optional: - Clear or replace the slot before calling the old `destroy` (finding 1), and add a test that replaces a key from inside its own destroy callback.
MarcZX10 commented 2026-11-14 01:14:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T01:14:10Z.

Review: COMMENTED


Reviewed — comments

Changes are required before merge: the review is incomplete because I could not run the required reader command or inspect all reviewed files.

What I ran

The prepared diff is pinned to head 679799633831233b812f78f61ed970b6875ff9df. No tests or build commands were run. Repository context was read through the review tools.

Results

Check Result Note
Prepared diff inspection passed Reviewed relevant implementation and tests
Tests and build not run No execution tool was available
Full-file review not run Not all files marked reviewed were read

Findings

Blocking

None verified.

Non-blocking

None.

Not verified

The attempted reader invocation could not run in this environment. I read review/description.md, review/commits.md, review/manifest.json, and review/diff.patch (lines 1–511). I inspected the changed implementation in core/libs/prx/libcairo/FontFace.cpp as shown in the diff and the added test source shown there. I did not read every file listed as reviewed in the manifest, nor verify test results reported in the PR description.

To get this to a good state

Nothing is required. The optional point is to complete review of all files marked reviewed and run the relevant checks before making a formal decision.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5900332571 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18406#pullrequestreview-5900332571). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T01:14:10Z. **Review:** COMMENTED --- # Reviewed — comments Changes are required before merge: the review is incomplete because I could not run the required reader command or inspect all reviewed files. ## What I ran The prepared diff is pinned to head `679799633831233b812f78f61ed970b6875ff9df`. No tests or build commands were run. Repository context was read through the review tools. ## Results | Check | Result | Note | |---|---|---| | Prepared diff inspection | passed | Reviewed relevant implementation and tests | | Tests and build | not run | No execution tool was available | | Full-file review | not run | Not all files marked reviewed were read | ## Findings ### Blocking None verified. ### Non-blocking None. ## Not verified The attempted reader invocation could not run in this environment. I read `review/description.md`, `review/commits.md`, `review/manifest.json`, and `review/diff.patch` (lines 1–511). I inspected the changed implementation in `core/libs/prx/libcairo/FontFace.cpp` as shown in the diff and the added test source shown there. I did not read every file listed as reviewed in the manifest, nor verify test results reported in the PR description. ## To get this to a good state Nothing is required. The optional point is to complete review of all files marked reviewed and run the relevant checks before making a formal decision.
MarcZX10 commented 2026-11-14 23:28:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T23:28:46Z.

Review: COMMENTED


Reviewed — no blocking findings

The implementation is supported by targeted source review, but I did not run the reported tests or inspect every changed file, so a formal approval is not justified.

What I ran

  • Reviewed head 0729def9fab91783bbdae8ef126c78f9ce0d444b using repo_read.py.
  • No tests or build commands were run.

Results

Check Result Note
Source review not run Targeted review only
Tests not run Results in the description were author reported

Findings

Blocking

None identified in the code reviewed.

Non-blocking

None.

Not verified

I read description.md, commits.md, diff.patch, manifest.json, README.md, and the changed source ranges for FontFace.cpp, FontFace.hpp, CMakeLists.txt, and docs/dev/debt/libcairo.md. I did not read all added lines in GuestCairoFontFace.cpp, or the changed file contents for Export.cpp, InventoryExports.hpp, and libcairo.hpp. The author-reported Linux tests, sanitizer run, and Cairo comparison were not run or independently confirmed. Windows and console behavior were not verified.

To get this to a good state

Nothing is required. Optional: complete review of the remaining changed lines and independently run the reported checks before relying on this review as a merge decision.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5907874153 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18406#pullrequestreview-5907874153). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T23:28:46Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The implementation is supported by targeted source review, but I did not run the reported tests or inspect every changed file, so a formal approval is not justified. ## What I ran - Reviewed head `0729def9fab91783bbdae8ef126c78f9ce0d444b` using `repo_read.py`. - No tests or build commands were run. ## Results | Check | Result | Note | | --- | --- | --- | | Source review | not run | Targeted review only | | Tests | not run | Results in the description were author reported | ## Findings ### Blocking None identified in the code reviewed. ### Non-blocking None. ## Not verified I read `description.md`, `commits.md`, `diff.patch`, `manifest.json`, `README.md`, and the changed source ranges for `FontFace.cpp`, `FontFace.hpp`, `CMakeLists.txt`, and `docs/dev/debt/libcairo.md`. I did not read all added lines in `GuestCairoFontFace.cpp`, or the changed file contents for `Export.cpp`, `InventoryExports.hpp`, and `libcairo.hpp`. The author-reported Linux tests, sanitizer run, and Cairo comparison were not run or independently confirmed. Windows and console behavior were not verified. ## To get this to a good state Nothing is required. Optional: complete review of the remaining changed lines and independently run the reported checks before relying on this review as a merge decision.
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!18406
No description provided.