feat(libfontconfig): implement font and object set lifecycles #15283

Merged
hyvvs merged 5 commits from upstream-pr/15283 into main 2026-11-06 09:32:10 +00:00
hyvvs commented 2026-11-02 22:41:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hyvvs on 2026-11-02T22:41:28Z.


What

Implement five typed guest-ABI libfontconfig exports FcFontSetCreate, FcFontSetAdd, FcFontSetDestroy, FcObjectSetBuild and FcObjectSetDestroy, now matching firmware 13.42 libfontconfig.sprx disassembly rather than generic desktop Fontconfig's ownership and allocation rules. Module SHA-256 74d7414445f8687c1d22a92fa3c994d2e5fa304e6699dc84dcc5459af7da8472 was supplied in BunnyFl's review.

  • FcFontSetAdd duplicates the pattern through a correctly typed FcPatternDuplicate(const FcPattern*), leaving caller ownership intact; the set owns that duplicate and FcFontSetDestroy destroys each duplicate. Capacity is stored in bytes, growing by 128 (16 pattern pointers), capped at 8,192 bytes / 1,024 entries. The 1,025th insertion returns false.
  • FcObjectSetBuild counts sentinel-terminated names, allocates the FcObjectSet header and its pointer array in one allocation, stores objects=(set+1), sets sobject=8*n, and duplicates strings in original argument order, including repeated names. Linux and MinGW Windows both read guest SysV variadic arguments. Destruction releases each name and the one header/array allocation.
  • Empty/null destroy and null font insertion are handled safely; the original firmware disassembly suggests some of these cases would dereference null. The firmware contract is disassembly-based, not measured on console.

Dependencies: Nonempty FcFontSetAdd and FcFontSetDestroy require FcPatternDuplicate and FcPatternDestroy from #14435, which itself depends on #14392. The pattern duplicate/destroy symbols still throw in this branch until that PR is merged or rebased in. Do not claim populated font sets work on unmodified main. Test code supplies typed pattern duplicate/destroy mocks. This PR contributes exactly 5 newly callable export definitions on its branch, conditional on dependent pattern support, not to upstream main yet.

Tested

  • Windows 11, MinGW GCC 15.2: g++ -std=c++20 -O2 -DNDEBUG -Wall -Wextra -Werror -pedantic -Icore/libs core/libs/tests/GuestFontconfigSets.cpp core/libs/prx/libfontconfig/Sets.cpp, linked executable ran with exit 0; Release assertions forced active. Verifies 1,024 successful adds, one refused extra, duplicate ownership and independent caller destruction; exact byte capacities 128 through 8192; System V variadics; preserved object-name order and inline pointer array.
  • WSL Linux GCC: same source-linked test with -fsanitize=address,undefined and ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 UBSAN_OPTIONS=halt_on_error=1, exit 0, no sanitizer reports.
  • Public header audit on updated definitions: 405 libraries, 27,952 exports, unique declarations and umbrella coverage verified.
  • git diff --check clean. Conventions and remote CI rechecked on new commit. No console or firmware-runtime execution or full integrated CMake/CTest run performed. No claim of native Fontconfig 2.15.0 matching since firmware semantics intentionally differ.

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 (no new third-party code)
  • Generic behaviour, not specific to one title
  • Depends on: #14435 (#14435 depends on #14392)
  • Fixes: none
  • Unblocks: fontconfig font-set ownership once pattern core lands
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5128595112 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15283). Originally posted by [hyvvs](https://github.com/hyvvs) on 2026-11-02T22:41:28Z. --- ### What Implement five typed guest-ABI libfontconfig exports `FcFontSetCreate`, `FcFontSetAdd`, `FcFontSetDestroy`, `FcObjectSetBuild` and `FcObjectSetDestroy`, now matching firmware 13.42 `libfontconfig.sprx` disassembly rather than generic desktop Fontconfig's ownership and allocation rules. Module SHA-256 `74d7414445f8687c1d22a92fa3c994d2e5fa304e6699dc84dcc5459af7da8472` was supplied in [BunnyFl's review](https://github.com/boykopovar/AnyPS5/pull/15283). - `FcFontSetAdd` duplicates the pattern through a correctly typed `FcPatternDuplicate(const FcPattern*)`, leaving caller ownership intact; the set owns that duplicate and `FcFontSetDestroy` destroys each duplicate. Capacity is stored in **bytes**, growing by 128 (16 pattern pointers), capped at 8,192 bytes / **1,024 entries**. The 1,025th insertion returns false. - `FcObjectSetBuild` counts sentinel-terminated names, allocates the `FcObjectSet` header and its pointer array **in one allocation**, stores `objects=(set+1)`, sets `sobject=8*n`, and duplicates strings in original argument order, including repeated names. Linux and MinGW Windows both read guest SysV variadic arguments. Destruction releases each name and the one header/array allocation. - Empty/null destroy and null font insertion are handled safely; the original firmware disassembly suggests some of these cases would dereference null. The firmware contract is disassembly-based, not measured on console. **Dependencies:** Nonempty `FcFontSetAdd` and `FcFontSetDestroy` require `FcPatternDuplicate` and `FcPatternDestroy` from [#14435](https://github.com/boykopovar/AnyPS5/pull/14435), which itself depends on #14392. The pattern duplicate/destroy symbols still throw in this branch until that PR is merged or rebased in. Do not claim populated font sets work on unmodified main. Test code supplies typed pattern duplicate/destroy mocks. This PR contributes exactly 5 newly callable export definitions on its branch, conditional on dependent pattern support, not to upstream main yet. ### Tested - Windows 11, MinGW GCC 15.2: `g++ -std=c++20 -O2 -DNDEBUG -Wall -Wextra -Werror -pedantic -Icore/libs core/libs/tests/GuestFontconfigSets.cpp core/libs/prx/libfontconfig/Sets.cpp`, linked executable ran with exit 0; Release assertions forced active. Verifies 1,024 successful adds, one refused extra, duplicate ownership and independent caller destruction; exact byte capacities 128 through 8192; System V variadics; preserved object-name order and inline pointer array. - WSL Linux GCC: same source-linked test with `-fsanitize=address,undefined` and `ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 UBSAN_OPTIONS=halt_on_error=1`, exit 0, no sanitizer reports. - Public header audit on updated definitions: 405 libraries, 27,952 exports, unique declarations and umbrella coverage verified. - `git diff --check` clean. Conventions and remote CI rechecked on new commit. No console or firmware-runtime execution or full integrated CMake/CTest run performed. No claim of native Fontconfig 2.15.0 matching since firmware semantics intentionally differ. ### 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 (no new third-party code) - [x] Generic behaviour, not specific to one title - [x] Depends on: #14435 (#14435 depends on #14392) - [x] Fixes: none - [x] Unblocks: fontconfig font-set ownership once pattern core lands - [x] GPUs tested: n/a - [x] AI-assisted: yes
BunnyFl commented 2026-11-03 02:58:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-03T02:58:53Z.

Review: CHANGES_REQUESTED


The PR follows Fontconfig 2.15.0, but firmware 13.42's own system_ex/common_ex/lib/libfontconfig.sprx (sha256 74d7414445f8687c1d22a92fa3c994d2e5fa304e6699dc84dcc5459af7da8472) implements these sets differently. It's a C++ implementation, not upstream fcfs.c/fclist.c. Everything below is from disassembly only, with each body read to its return, and is not measured on a console. The exports are 5-byte jmps to the bodies at 0x65d0 (FcFontSetCreate), 0x6650 (FcFontSetAdd) and 0x65f0 (FcFontSetDestroy). Call names are resolved through the module's PLT/GOT and the public NID database.

1. FcFontSetAdd stores a duplicate, not the caller's pattern. This is the blocking point.

 67a0: 48 89 f7        mov    %rsi,%rdi
 67a3: e8 78 a1 ff ff  call   0x920          # FcPatternDuplicate (DPxdK61lRaE)
 67a8: 48 63 13        movslq (%rbx),%rdx
 67ab: 48 8b 4b 08     mov    0x8(%rbx),%rcx
 67af: 8d 72 01        lea    0x1(%rdx),%esi
 67b2: 89 33           mov    %esi,(%rbx)
 67b4: 48 89 04 d1     mov    %rax,(%rcx,%rdx,8)
 67b8: b8 01 00 00 00  mov    $0x1,%eax

On the console the caller keeps ownership of its pattern. Code written for it may destroy the pattern after adding it. With this PR, the set then holds a freed pointer, and FcFontSetDestroy destroys it a second time.

2. sfont is a byte count, the set grows by 16 entries, and it stops at 1024.

 665e: 44 8b 77 04     mov    0x4(%rdi),%r14d     # sfont
 6662: 44 8b 27        mov    (%rdi),%r12d        # nfont
 6668: 44 89 f0        mov    %r14d,%eax
 666b: c1 f8 03        sar    $0x3,%eax
 666e: 41 39 c4        cmp    %eax,%r12d
 6671: 0f 85 29 01 00 00 jne  0x67a0              # room left: add
 6677: 31 c0           xor    %eax,%eax
 6679: 41 81 fe ff 1f 00 00 cmp $0x1fff,%r14d
 6680: 0f 87 37 01 00 00 ja   0x67bd              # return 0
 6686: 41 81 e6 f8 1f 00 00 and $0x1ff8,%r14d
 6690: 49 83 ee 80     sub    $0xffffffffffffff80,%r14
 6697: e8 14 85 00 00  call   0xebb0              # operator new[]
 6790: e8 0b 84 00 00  call   0xeba0              # operator delete[] (old array)
 6798: 4c 89 7b 08     mov    %r15,0x8(%rbx)
 679c: 44 89 73 04     mov    %r14d,0x4(%rbx)

So a guest reading sfont sees 8 × the capacity. Growth is 0x80 bytes (16 entries), not 32 entries, and the 1025th add returns 0.

3. FcObjectSetBuild (0x35f0) keeps argument order in one allocation. It collects first and the varargs up to NULL into a temporary list. It then makes a single malloc(16 + 8*n) block with nobject = n at +0, sobject = 8*n at +4, and objects pointing at the inline array at +16. Each name is strduped in argument order:

 3767: e8 d4 b3 00 00  call   0xeb40              # malloc(16 + 8*n)
 376c: 44 89 28        mov    %r13d,(%rax)        # nobject = n
 3772: 41 83 c6 f0     add    $0xfffffff0,%r14d
 3779: 44 89 70 04     mov    %r14d,0x4(%rax)     # sobject = 8*n
 377d: 4c 8d 70 10     lea    0x10(%rax),%r14
 3781: 4c 89 70 08     mov    %r14,0x8(%rax)      # objects = inline array
 3790: 49 8b 7d 10     mov    0x10(%r13),%rdi
 3794: e8 b7 b3 00 00  call   0xeb50              # strdup, list order

The PR sorts objects by pointer address and grows in steps of 4 (core/libs/prx/libfontconfig/Sets.cpp:26-45 on this head), so a guest walking objects sees a different order. first is stored without a null check (at 0x369b, mov %r15,0x10(%rax)), so FcObjectSetBuild(NULL) presumably reaches strdup(NULL). I didn't follow that path past 0x3794.

4. Null handling. FcFontSetDestroy (65fa: mov 0x8(%rdi),%rdi) and FcObjectSetDestroy (382b: movslq (%rdi),%r14) dereference their argument with no null check. The PR returns early instead. That's fine as a host-safety choice if docs/dev/debt/libfontconfig.md says so.

FcFontSetCreate matches the PR's layout: operator new(16) zeroed (65d4–65e2), so {nfont, sfont, fonts} at 0/4/8.

Could you rebuild these from the module's contract: duplicate on add, byte-sized sfont/sobject, 16-entry growth and the 1024 cap, argument order? Tests should use main as the negative control. Point 1 also affects #14435: the firmware's patterns are 0x48-byte objects (FcPatternDuplicate, 93b: mov $0x48,%edi before operator new), and FcPatternReference is lock incl (%rdi) (0x9f0), a reference count at offset 0.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5746721174 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15283#pullrequestreview-5746721174). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-03T02:58:53Z. **Review:** CHANGES_REQUESTED --- The PR follows Fontconfig 2.15.0, but firmware 13.42's own `system_ex/common_ex/lib/libfontconfig.sprx` (sha256 `74d7414445f8687c1d22a92fa3c994d2e5fa304e6699dc84dcc5459af7da8472`) implements these sets differently. It's a C++ implementation, not upstream `fcfs.c`/`fclist.c`. Everything below is from disassembly only, with each body read to its return, and is not measured on a console. The exports are 5-byte `jmp`s to the bodies at `0x65d0` (`FcFontSetCreate`), `0x6650` (`FcFontSetAdd`) and `0x65f0` (`FcFontSetDestroy`). Call names are resolved through the module's PLT/GOT and the public NID database. **1. `FcFontSetAdd` stores a duplicate, not the caller's pattern.** This is the blocking point. ``` 67a0: 48 89 f7 mov %rsi,%rdi 67a3: e8 78 a1 ff ff call 0x920 # FcPatternDuplicate (DPxdK61lRaE) 67a8: 48 63 13 movslq (%rbx),%rdx 67ab: 48 8b 4b 08 mov 0x8(%rbx),%rcx 67af: 8d 72 01 lea 0x1(%rdx),%esi 67b2: 89 33 mov %esi,(%rbx) 67b4: 48 89 04 d1 mov %rax,(%rcx,%rdx,8) 67b8: b8 01 00 00 00 mov $0x1,%eax ``` On the console the caller keeps ownership of its pattern. Code written for it may destroy the pattern after adding it. With this PR, the set then holds a freed pointer, and `FcFontSetDestroy` destroys it a second time. **2. `sfont` is a byte count, the set grows by 16 entries, and it stops at 1024.** ``` 665e: 44 8b 77 04 mov 0x4(%rdi),%r14d # sfont 6662: 44 8b 27 mov (%rdi),%r12d # nfont 6668: 44 89 f0 mov %r14d,%eax 666b: c1 f8 03 sar $0x3,%eax 666e: 41 39 c4 cmp %eax,%r12d 6671: 0f 85 29 01 00 00 jne 0x67a0 # room left: add 6677: 31 c0 xor %eax,%eax 6679: 41 81 fe ff 1f 00 00 cmp $0x1fff,%r14d 6680: 0f 87 37 01 00 00 ja 0x67bd # return 0 6686: 41 81 e6 f8 1f 00 00 and $0x1ff8,%r14d 6690: 49 83 ee 80 sub $0xffffffffffffff80,%r14 6697: e8 14 85 00 00 call 0xebb0 # operator new[] 6790: e8 0b 84 00 00 call 0xeba0 # operator delete[] (old array) 6798: 4c 89 7b 08 mov %r15,0x8(%rbx) 679c: 44 89 73 04 mov %r14d,0x4(%rbx) ``` So a guest reading `sfont` sees 8 × the capacity. Growth is 0x80 bytes (16 entries), not 32 entries, and the 1025th add returns 0. **3. `FcObjectSetBuild` (`0x35f0`) keeps argument order in one allocation.** It collects `first` and the varargs up to NULL into a temporary list. It then makes a single `malloc(16 + 8*n)` block with `nobject = n` at +0, `sobject = 8*n` at +4, and `objects` pointing at the inline array at +16. Each name is `strdup`ed in argument order: ``` 3767: e8 d4 b3 00 00 call 0xeb40 # malloc(16 + 8*n) 376c: 44 89 28 mov %r13d,(%rax) # nobject = n 3772: 41 83 c6 f0 add $0xfffffff0,%r14d 3779: 44 89 70 04 mov %r14d,0x4(%rax) # sobject = 8*n 377d: 4c 8d 70 10 lea 0x10(%rax),%r14 3781: 4c 89 70 08 mov %r14,0x8(%rax) # objects = inline array 3790: 49 8b 7d 10 mov 0x10(%r13),%rdi 3794: e8 b7 b3 00 00 call 0xeb50 # strdup, list order ``` The PR sorts `objects` by pointer address and grows in steps of 4 (`core/libs/prx/libfontconfig/Sets.cpp:26-45` on this head), so a guest walking `objects` sees a different order. `first` is stored without a null check (at `0x369b`, `mov %r15,0x10(%rax)`), so `FcObjectSetBuild(NULL)` presumably reaches `strdup(NULL)`. I didn't follow that path past `0x3794`. **4. Null handling.** `FcFontSetDestroy` (`65fa: mov 0x8(%rdi),%rdi`) and `FcObjectSetDestroy` (`382b: movslq (%rdi),%r14`) dereference their argument with no null check. The PR returns early instead. That's fine as a host-safety choice if `docs/dev/debt/libfontconfig.md` says so. `FcFontSetCreate` matches the PR's layout: `operator new(16)` zeroed (`65d4`–`65e2`), so `{nfont, sfont, fonts}` at 0/4/8. Could you rebuild these from the module's contract: duplicate on add, byte-sized `sfont`/`sobject`, 16-entry growth and the 1024 cap, argument order? Tests should use `main` as the negative control. Point 1 also affects #14435: the firmware's patterns are 0x48-byte objects (`FcPatternDuplicate`, `93b: mov $0x48,%edi` before `operator new`), and `FcPatternReference` is `lock incl (%rdi)` (`0x9f0`), a reference count at offset 0.
BunnyFl commented 2026-11-04 03:48:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-04T03:48:25Z.

Review: APPROVED


@hyvvs thanks, all four points from my review are addressed at d7685faff1. Compared with the firmware 13.42 reading I posted:

  • Duplicate on add: FcFontSetAdd stores FcPatternDuplicate(pattern), and FcFontSetDestroy destroys the copies the set owns.
  • Capacity: sfont is a byte count that grows by 128 (16 entries). Adds stop once it reaches 8192, i.e. 1024 entries. That is the module's (sfont & 0x1ff8) + 0x80 growth with its cmp $0x1fff / ja cap.
  • FcObjectSetBuild: one malloc(16 + 8n) with nobject = n, sobject = 8n, the objects inline, and copied names in argument order.
  • Null handling: the debt entry records the host-safety returns for a null add and a null destroy.

What I ran: head d7685faff1 merged onto main 1c8c0efc8c, Windows 11, WinLibs GCC, Release:

  • guest_fontconfig_sets and public_headers pass (2 of 2);
  • mutation, the add storing pattern instead of the duplicate: guest_fontconfig_sets fails at GuestFontconfigSets.cpp:44 (set->fonts[j] != pattern);
  • mutation, growth of 32 entries: it fails at GuestFontconfigSets.cpp:43 (set->sfont == ((j / 16) + 1) * 128);
  • tools/check_conventions.py --base upstream/main on the head: all checks passed.

Two things to know, not blocking:

  • Conflict with #14435. Merged on top of #14435 (d848a0885c), this head conflicts in core/libs/prx/libfontconfig/CMakeLists.txt (add/add), Export.cpp, include/InventoryExports.hpp and docs/dev/debt/libfontconfig.md. So it needs a rebase once #14435 lands, and I couldn't run the two together. tools/pr_overlap.py --branch also lists #14392, #15294 and #15363 as touching the same files.
  • FcObjectSetBuild(NULL). It returns an empty set here, where the module stores first without a null check (0x369b) and presumably reaches strdup(NULL). If you want the debt entry complete, that's a third host-safety case next to null add and null destroy.

Approving.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5762949609 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15283#pullrequestreview-5762949609). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-04T03:48:25Z. **Review:** APPROVED --- @hyvvs thanks, all four points from my review are addressed at `d7685faff1`. Compared with the firmware 13.42 reading I posted: - **Duplicate on add:** `FcFontSetAdd` stores `FcPatternDuplicate(pattern)`, and `FcFontSetDestroy` destroys the copies the set owns. - **Capacity:** `sfont` is a byte count that grows by 128 (16 entries). Adds stop once it reaches 8192, i.e. 1024 entries. That is the module's `(sfont & 0x1ff8) + 0x80` growth with its `cmp $0x1fff` / `ja` cap. - **`FcObjectSetBuild`:** one `malloc(16 + 8n)` with `nobject = n`, `sobject = 8n`, the objects inline, and copied names in argument order. - **Null handling:** the debt entry records the host-safety returns for a null add and a null destroy. **What I ran:** head `d7685faff1` merged onto `main` `1c8c0efc8c`, Windows 11, WinLibs GCC, Release: - `guest_fontconfig_sets` and `public_headers` pass (2 of 2); - mutation, the add storing `pattern` instead of the duplicate: `guest_fontconfig_sets` fails at `GuestFontconfigSets.cpp:44` (`set->fonts[j] != pattern`); - mutation, growth of 32 entries: it fails at `GuestFontconfigSets.cpp:43` (`set->sfont == ((j / 16) + 1) * 128`); - `tools/check_conventions.py --base upstream/main` on the head: all checks passed. **Two things to know, not blocking:** - **Conflict with #14435.** Merged on top of #14435 (`d848a0885c`), this head conflicts in `core/libs/prx/libfontconfig/CMakeLists.txt` (add/add), `Export.cpp`, `include/InventoryExports.hpp` and `docs/dev/debt/libfontconfig.md`. So it needs a rebase once #14435 lands, and I couldn't run the two together. `tools/pr_overlap.py --branch` also lists #14392, #15294 and #15363 as touching the same files. - **`FcObjectSetBuild(NULL)`.** It returns an empty set here, where the module stores `first` without a null check (`0x369b`) and presumably reaches `strdup(NULL)`. If you want the debt entry complete, that's a third host-safety case next to null add and null destroy. Approving.
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!15283
No description provided.