fix(libharfbuzz): guard guest memory access in hb_ft_face_create_cached #27967

Merged
JJenkx merged 1 commit from upstream-pr/27967 into main 2026-12-04 07:13:09 +00:00
JJenkx commented 2026-11-27 22:53:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-11-27T22:53:56Z.


What

Complete guest-pointer guarding in hb_ft_face_create_cached_nid_postfix (core/libs/prx/libharfbuzz/Export.cpp), bringing libharfbuzz to 0 hits in tools/guest_pointer_audit.py:

  • Prepare and verify guest readability (GuestArenaPrepareHostRead_nid_no_patch + GuestReadable_nid_no_patch) before GuestCopyIn_nid_no_patch while preserving errno.
  • Compute the generic slot address via offsetof(FreeType::GuestFaceRec, generic) instead of direct member addressing, and hold an active GuestArenaBeginHostWrite_nid_postfix/GuestArenaEndHostWrite_nid_postfix guard across GuestCopyOut_nid_no_patch (preserving errno across entry and exit) before invoking any prior guest finalizer or writing the cached handle.
  • Add read-only GuestFace and guard-page boundary regression tests to core/libs/tests/GuestHarfBuzzFreeTypeShaping.cpp and record docs/dev/debt/changes/fix-libharfbuzz-ft-face-guest-pointers.md.

Tested

  • Linux (Release, /usr/local/bin/cmake --build /mnt/sas-ssd/AnyPS5-work/agy/build-spec --target guest_harfbuzz_freetype_shaping_tests):
    • ctest -R '^guest_harfbuzz_freetype_shaping(_nid)?$' --output-on-failure: 2/2 passed (guest_harfbuzz_freetype_shaping 0.02 sec, guest_harfbuzz_freetype_shaping_nid 0.02 sec).
  • python3 tools/guest_pointer_audit.py libharfbuzz: 0 direct guest pointer accesses in 0 exported functions.
  • python3 tools/check_conventions.py --base origin/main: All conventions checks passed.
  • python3 tools/pr_overlap.py --branch fix/libharfbuzz-ft-face-guest-pointers: No overlap with open pull requests.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of invalid or read-only font-face data. These inputs are now rejected safely, and error reporting is preserved.
  • Tests
    • Added checks for protected and boundary-adjacent memory cases to verify invalid font-face data is handled safely.
  • Documentation
    • Updated developer documentation to record the behavior and its verification status.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5534135095 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27967). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-11-27T22:53:56Z. --- ### What Complete guest-pointer guarding in `hb_ft_face_create_cached_nid_postfix` (`core/libs/prx/libharfbuzz/Export.cpp`), bringing `libharfbuzz` to 0 hits in `tools/guest_pointer_audit.py`: - Prepare and verify guest readability (`GuestArenaPrepareHostRead_nid_no_patch` + `GuestReadable_nid_no_patch`) before `GuestCopyIn_nid_no_patch` while preserving `errno`. - Compute the `generic` slot address via `offsetof(FreeType::GuestFaceRec, generic)` instead of direct member addressing, and hold an active `GuestArenaBeginHostWrite_nid_postfix`/`GuestArenaEndHostWrite_nid_postfix` guard across `GuestCopyOut_nid_no_patch` (preserving `errno` across entry and exit) before invoking any prior guest finalizer or writing the cached handle. - Add read-only `GuestFace` and guard-page boundary regression tests to `core/libs/tests/GuestHarfBuzzFreeTypeShaping.cpp` and record `docs/dev/debt/changes/fix-libharfbuzz-ft-face-guest-pointers.md`. ### Tested - Linux (Release, `/usr/local/bin/cmake --build /mnt/sas-ssd/AnyPS5-work/agy/build-spec --target guest_harfbuzz_freetype_shaping_tests`): - `ctest -R '^guest_harfbuzz_freetype_shaping(_nid)?$' --output-on-failure`: `2/2` passed (`guest_harfbuzz_freetype_shaping` `0.02 sec`, `guest_harfbuzz_freetype_shaping_nid` `0.02 sec`). - `python3 tools/guest_pointer_audit.py libharfbuzz`: `0 direct guest pointer accesses in 0 exported functions`. - `python3 tools/check_conventions.py --base origin/main`: All conventions checks passed. - `python3 tools/pr_overlap.py --branch fix/libharfbuzz-ft-face-guest-pointers`: No overlap with open pull requests. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Improved handling of invalid or read-only font-face data. These inputs are now rejected safely, and error reporting is preserved. * **Tests** * Added checks for protected and boundary-adjacent memory cases to verify invalid font-face data is handled safely. * **Documentation** * Updated developer documentation to record the behavior and its verification status. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
PrivacyOnion commented 2026-11-27 22:59:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-27T22:59:20Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 43252c83c0:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_harfbuzz_freetype_shaping_tests built cleanly.
  • CTest (guest_harfbuzz_freetype_shaping):
    1/1 Test #6355: guest_harfbuzz_freetype_shaping ...   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libharfbuzz/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #6355: guest_harfbuzz_freetype_shaping ...   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified GuestCopyInPreservingErrno in core/libs/prx/libharfbuzz/Export.cpp properly prepares guest read arena and checks GuestReadable before copying into stack structs
    • Verified HostWriteGuard in core/libs/prx/libharfbuzz/Export.cpp manages GuestArenaBeginHostWrite/GuestArenaEndHostWrite via RAII and checks GuestWritable while preserving errno across operations
    • Verified offsetof(FreeType::GuestFaceRec, generic) correctly computes the generic member offset without direct unsafe guest pointer member access
    • Verified FinalizeCachedFace handles nullptr object and safely copies generic data using offsetof without out-of-bounds guest access
    • Verified cleanup of hb_face_destroy upon failed GuestCopyOutPreservingErrno, preventing resource leakage on write fault
    • Verified GuestHarfBuzzFreeTypeShaping.cpp tests read-only and guard-page boundary conditions for both Linux and Windows (sysconf/mprotect vs GetSystemInfo/VirtualProtect)
    • Verified Technical debt fragment docs/dev/debt/changes/fix-libharfbuzz-ft-face-guest-pointers.md adheres to debt tracking rules without code comments

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6094118344 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27967#pullrequestreview-6094118344). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-27T22:59:20Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `43252c83c0`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_harfbuzz_freetype_shaping_tests` built cleanly. - **CTest** (`guest_harfbuzz_freetype_shaping`): ``` 1/1 Test #6355: guest_harfbuzz_freetype_shaping ... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libharfbuzz/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #6355: guest_harfbuzz_freetype_shaping ... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified GuestCopyInPreservingErrno in core/libs/prx/libharfbuzz/Export.cpp properly prepares guest read arena and checks GuestReadable before copying into stack structs - Verified HostWriteGuard in core/libs/prx/libharfbuzz/Export.cpp manages GuestArenaBeginHostWrite/GuestArenaEndHostWrite via RAII and checks GuestWritable while preserving errno across operations - Verified offsetof(FreeType::GuestFaceRec, generic) correctly computes the generic member offset without direct unsafe guest pointer member access - Verified FinalizeCachedFace handles nullptr object and safely copies generic data using offsetof without out-of-bounds guest access - Verified cleanup of hb_face_destroy upon failed GuestCopyOutPreservingErrno, preventing resource leakage on write fault - Verified GuestHarfBuzzFreeTypeShaping.cpp tests read-only and guard-page boundary conditions for both Linux and Windows (sysconf/mprotect vs GetSystemInfo/VirtualProtect) - Verified Technical debt fragment docs/dev/debt/changes/fix-libharfbuzz-ft-face-guest-pointers.md adheres to debt tracking rules without code comments Looks Good To Me
Plyxen commented 2026-11-28 05:39:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-11-28T05:39:20Z.

Review: APPROVED


Checked head 43252c83c0. All on Windows 11 with WinLibs GCC 15.2 (Release), applied to main 4c7578e0a4 together with #27997, #27996, #27978, #27975, #27973, #27971, #27969, #27964, #27958, #27950, #27949, #27946, #27945, #27943, #27938, #27936. tools/check_conventions.py --head and tools/debt_fold.py --check passed and the full build succeeded. guest_harfbuzz_freetype_shaping and guest_harfbuzz_freetype_shaping_nid pass. In the full CPU suite (5,131 tests) the only failures are unrelated: windows_read_trap_unknown fails on main too (fixed in #28028), and frame_capture_minimize is a timeout under load that passes on its own.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6096090960 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27967#pullrequestreview-6096090960). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-11-28T05:39:20Z. **Review:** APPROVED --- Checked head `43252c83c0`. All on Windows 11 with WinLibs GCC 15.2 (Release), applied to `main` `4c7578e0a4` together with #27997, #27996, #27978, #27975, #27973, #27971, #27969, #27964, #27958, #27950, #27949, #27946, #27945, #27943, #27938, #27936. `tools/check_conventions.py --head` and `tools/debt_fold.py --check` passed and the full build succeeded. `guest_harfbuzz_freetype_shaping` and `guest_harfbuzz_freetype_shaping_nid` pass. In the full CPU suite (5,131 tests) the only failures are unrelated: `windows_read_trap_unknown` fails on `main` too (fixed in #28028), and `frame_capture_minimize` is a timeout under load that passes on its own. AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-11-28 08:20:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T08:20:15Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the code and tests I read support the stated guest-pointer guards, though console fault behavior remains unverified.

What I ran

Reviewed head 43252c83c04fb3629513ad4573b51763d83fb306. I read the prepared description, commits, manifest and diff, plus all three changed files and targeted guest-memory helper implementations at the pinned head. No commands or tests were run; the test results in the description are author-reported.

Results

Check Result Note
Changed code review passed All changed files read
Automated checks not run Results in description are author-reported
Console behavior not run Documentation records this as unmeasured

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run the reported build, ctest, guest-pointer audit, conventions check, or PR-overlap check. I did not verify console fault behavior. The review bundle's files.json was unavailable; the manifest lists three changed files and all three were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6096729229 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27967#pullrequestreview-6096729229). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-28T08:20:15Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the code and tests I read support the stated guest-pointer guards, though console fault behavior remains unverified. ## What I ran Reviewed head `43252c83c04fb3629513ad4573b51763d83fb306`. I read the prepared description, commits, manifest and diff, plus all three changed files and targeted guest-memory helper implementations at the pinned head. No commands or tests were run; the test results in the description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | All changed files read | | Automated checks | not run | Results in description are author-reported | | Console behavior | not run | Documentation records this as unmeasured | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not run the reported build, `ctest`, guest-pointer audit, conventions check, or PR-overlap check. I did not verify console fault behavior. The review bundle's `files.json` was unavailable; the manifest lists three changed files and all three were read. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 09:24:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T09:24:21Z.

Review: COMMENTED


The Linux guest_harfbuzz_freetype_shaping CTest passed with the patched HarfBuzz and guest FreeType libraries. The fixture checks normal shaping through the cached face path and verifies that the read-only face case leaves the generic callback count at zero.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6166887107 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27967#pullrequestreview-6166887107). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T09:24:21Z. **Review:** COMMENTED --- The Linux `guest_harfbuzz_freetype_shaping` CTest passed with the patched HarfBuzz and guest FreeType libraries. The fixture checks normal shaping through the cached face path and verifies that the read-only face case leaves the generic callback count at zero. AI-assisted: yes.
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!27967
No description provided.