Skip to content

fix(libSceFont): clear the device cache ownership flag when an owned buffer is detached - #29126

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
kattkaries:fix/libSceFont-device-cache-ownership-flag
Dec 5, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
kattkaries:fix/libSceFont-device-cache-ownership-flag

Conversation

@kattkaries

@kattkaries kattkaries commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

What

sceFontDettachDeviceCacheBuffer sets the device-cache ownership bit (flags bit 0) when the library allocates the cache buffer itself (core/libs/prx/libSceFont/src/Library.cpp:394, lib->flags |= owned), but never clears it when that owned buffer is detached (:465-470). The bit therefore describes the last owned attach, not the currently attached buffer: after an owned detach, a caller-attached buffer is treated as library-owned, so the next detach returns nullptr/0 instead of the caller's pointer and capacity and calls lib->iface->dealloc on memory the library never allocated (:467). sceFontDestroyLibrary (:222) has the same consequence and frees a caller-owned buffer.

Reproduced on upstream main (2e5e1aab, Release, Linux): after sceFontAttachDeviceCacheBuffer(lib, nullptr, 0x2000) → detach, attaching a caller buffer and detaching it returned buffer=(nil), size=0 and dropped the library's allocation counter 34 -> 33; detaching a caller mmap'd buffer after an owned detach crashed with SIGSEGV (exit 139) inside glibc free, with libSceFont.prx+0x1b7e6 on the stack.

This change clears the bit in the owned detach branch before the cache slot is released, so it also runs when iface->dealloc is unavailable. After the fix the invariant holds: bit 0 is set iff the attached buffer was allocated by the library (:394 sets it only when buffer == nullptr), so the caller-buffer detach returns the caller's pointer and capacity and neither detach nor destroy deallocates caller memory. The flag is used only at :222, :394, :446 and the new :470, and libSceFontFt initialises flags = 0x60000000, so clearing bit 0 leaves the driver flags intact.

The regression extends core/libs/tests/GuestFont.cpp after the existing owned attach/detach checks (:1017-1018): owned attach → detach → caller std::vector attach → detach must return callerCache.data()/0x2000 and leave the allocations counter unchanged.

Overlap note: tools/pr_overlap.py reports open PR #22639 as sharing libSceFont/src/Library.cpp, which is the documented feature-vs-fix false positive. #22639's only hunk in that file is @@ -205,7 +205,10 @@ sceFontDestroyLibrary (term-callback error propagation, :205-215); it does not touch the detach branch at :465-470. The open PRs touching tests/GuestFont.cpp are #28338 (:1296) and #20216 (:557), both far from the insertion at :1019-1027. No open PR or issue mentions sceFontDettachDeviceCacheBuffer (the only merged reference is #20775).

Branch currency: upstream main moved from the commit this was reviewed against (6e3ed265) to 2e5e1aab, then to c0d8d6c2, during preparation. main was merged into the branch (no rebase, no force-push) and the tree was rebuilt and retested after each merge. Both files this PR touches are byte-identical on current main (Library.cpp blob 2dcc4290, GuestFont.cpp blob fd3948d3), so neither merge changed an approved file; the diff against current main is exactly the reviewed 10-line change.

Tested

Linux (container, x86-64), Release build, BUILD_TESTING=ON, full tree built with Ninja.

  • RED, before the fix: with only the production line reverted in a clean tree, ./build/tests/guest_font_tests fails with Font check failed at line 1026 (exit 134) — line 1026 is the new caller-buffer detach assertion.
  • GREEN, after the fix: ctest --test-dir build -R '^guest_font$' -V → 1/1 Test #4460 guest_font Passed; the binary run directly exits 0.
  • Whole font suite: ctest -R '^(guest_font|font_)' -E fontconfig -V → 30 passed, 1 skipped (guest_font_output_pointers_watched, environment-gated: no userfaultfd in this container), 0 failed.
  • Full-tree build on the branch with current main merged in: ninja -C build → exit 0, no failures.
  • Not tested: mixed owned-attach/caller-attach sequences on real PS5 firmware, and the destroy-path consequence (covered by the same invariant but not asserted directly).

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 (no issue; the defect was reproduced independently on main before the fix)
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Improved font cache buffer cleanup so the library correctly tracks ownership after a buffer is released.
    • Fixed detachment behavior for internally allocated and caller-provided buffers, including cases where optional output values are omitted. Caller-provided buffers are returned with their size, without affecting allocation counts.

@coderabbitai

coderabbitai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Font device-cache detachment

Layer / File(s) Summary
Detachment behavior and coverage
core/libs/prx/libSceFont/src/Library.cpp, core/libs/tests/GuestFont.cpp
The code clears the ownership flag after deallocating an owned cache buffer. Tests verify detachment with null outputs and confirm that detaching a caller-owned buffer returns its address and size without changing the allocation count.

Suggested reviewers: marczx10, kikoarl, privacyonion

Change: Bug fix

Merge Risk: Minimal · up to 9989c

This fixes a stale ownership flag that could cause a caller-attached font cache buffer to be wrongly released on detach. The change is small and covered by a regression test, with no remaining merge-blocking concerns identified.

Security architecture risk: Minimal · up to 9989c

libSceFont no longer carries ownership from a detached library-allocated cache into a later caller-owned cache. The fix preserves the existing API and prevents detach or destroy from freeing the later buffer on the basis of stale ownership.

Security review details

Security Blast Radius

  • inferred — The changed behavior is scoped to the device-cache lifecycle of a libSceFont library handle. The merge-base comparison adds no production entrypoint or allocator authority; the range flagged as public API in GuestFont exercises the existing interface.

Trust Boundaries and Controls

  • observed — A non-null caller buffer is borrowed; a null buffer requests allocation through the library allocator interface. The ownership bit governs whether detach or destroy may deallocate the attached buffer. Attach and detach update the slot under the same library lock, and detach clears ownership before another attachment can take the slot.

Resilience and Maintainability Implications

  • observed — A rejected detach or failed output copy preserves the current attachment and its ownership so detach can be retried, though an earlier output write may have succeeded. Once owned detach reaches slot release, the next attachment starts without stale ownership even if detach returns fatal because no deallocator is available. That pre-existing fatal path still drops the slot without reclaiming the old allocation.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 96c53a3c-1ae1-4791-94ce-ca0cd5afcbc7
Commits

Reviewing files that changed from the base of the PR and between c0d8d6c and 9989c49.

Files selected for processing: 2

  • core/libs/prx/libSceFont/src/Library.cpp
  • core/libs/tests/GuestFont.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@github-actions github-actions Bot added the prx label Dec 5, 2026
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 22639 core/libs/prx/libSceFont/src/Library.cpp

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

The ownership flag is cleared after an owned cache buffer is detached, and the added regression checks that a subsequent caller-owned buffer is returned without deallocation.

What I ran

Reviewed head 9989c49c7693d4599a72023ff8e70212d21d5a5a against the prepared diff and pinned source using repo_read.py; no merged-preview check or tests/builds were run.

repo_read.py cat core/libs/prx/libSceFont/src/Library.cpp 200 245
repo_read.py cat core/libs/prx/libSceFont/src/Library.cpp 330 485
repo_read.py search flags core/libs/prx/libSceFont/src/Library.cpp
repo_read.py cat core/libs/tests/GuestFont.cpp 890 940
repo_read.py cat core/libs/tests/GuestFont.cpp 1000 1032
repo_read.py search AttachDeviceCacheBuffer core/libs/tests/GuestFont.cpp

Results

Check Result Note
Changed code review passed Both changed files inspected
GuestFont tests not run The description reports 30 passed and 1 skipped

Negative control

Not run; the reported red test was author-run, not independently verified.

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

Tests and builds were not run. Real PS5 firmware behavior and a direct destroy-path regression were not verified. Read the relevant sections of core/libs/prx/libSceFont/src/Library.cpp and core/libs/tests/GuestFont.cpp; files.json lists no excluded files.

To get this to a good state

Nothing required.

@oneandonlydean
oneandonlydean merged commit f14676e into boykopovar:main Dec 5, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants