Skip to content

fix(libSceFont): safely copy out scaled kerning in sceFontGetRenderScaledKerning - #22100

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Kikoarl:fix/libSceFont-getRenderScaledKerning-copyout
Dec 3, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Kikoarl:fix/libSceFont-getRenderScaledKerning-copyout

Conversation

@Kikoarl

@Kikoarl Kikoarl commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

This pull request ensures sceFontGetRenderScaledKerning (core/libs/prx/libSceFont/src/Render.cpp) safely writes to kerning via CopyOutGuest (GuestCopyOut_nid_no_patch preserving errno) instead of dereferencing kerning directly:

  • Probes and zeroes *kerning via CopyOutGuest upfront before checking the font handle/locks and copies the computed FontKerning via CopyOutGuest, returning SCE_FONT_ERROR_INVALID_PARAMETER on unwritable output while preserving caller errno.
  • Adds an unmapped destination pointer (0x10) and errno == EBUSY preservation assertion in core/libs/tests/GuestFont.cpp.
  • Documents the host-safety fallback and Closes when: / Labels: metadata in docs/dev/debt/changes/fix-libSceFont-getRenderScaledKerning-copyout.md (retaining ../../../ per docs/dev/TechnicalDebt.md relative link base rules).

Tested

  • Built and ran guest_font_tests (ctest --test-dir /tmp/anyps5_build_pad --output-on-failure -R "^guest_font$"): 1/1 passed.
  • Verified conventions with python3 tools/check_conventions.py --base origin/main.

Checklist

  • Commits correspond to isolated fixes or features
  • Code conforms to project conventions
  • Explanation of the problem and proposed solution included above
  • Dependent changes have been merged
  • Verification steps included above
  • Dependabot/renovate PRs are not touched
  • Zero code comments in implementation diffs
  • All unmeasured hardware behavior documented in debt fragment
  • Depends on: none
  • AI-assisted: yes
    Fixes: none
    Unblocks: none
    GPUs tested: none

@PrivacyOnion PrivacyOnion 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.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 3f921d2038:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_font_tests built cleanly.
  • CTest (guest_font):
    1/1 Test #3554: guest_font .......................   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceFont/src/Render.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #3554: guest_font .......................***Exception: SegFault  0.13 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/libs/prx/libSceFont/src/Render.cpp: sceFontGetRenderScaledKerning checks kerning for nullptr and safely copies out local zeroed FontKerning via CopyOutGuest, returning SCE_FONT_ERROR_INVALID_PARAMETER on unmapped/unwritable guest pointers.
    • Verified lock acquisition and release symmetry for both fontLock and cachedLock across all code paths, ensuring no deadlock or leaked mutexes on early exits or copy failure.
    • Verified error return precedence: kerning validation before font handle validation and renderer binding checks, matching sceFontGetKerning and libSceFont conventions.
    • Verified docs/dev/debt/changes/fix-libSceFont-getRenderScaledKerning-copyout.md complies strictly with AGENTS.md technical debt fragment requirements and naming conventions.
    • Verified core/libs/tests/GuestFont.cpp exercises invalid pointer handling and validates caller errno preservation (EBUSY) across the CopyOutGuest boundary.

Looks Good To Me

@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 — no blocking findings

sceFontGetRenderScaledKerning now checks its guest output pointer with CopyOutGuest and writes a local value, and I found no defect in the changed code. Nothing blocks this; the points below are optional.

What I ran

  • Head 3f921d2038db301254e969aec5a1a21d89abb4d7, base main. Read description.md, commits.md, manifest.json, files.json and diff.patch from the review folder.
  • Read the head code with the repository reader (repo_read.py):
repo_read.py cat core/libs/prx/libSceFont/src/Render.cpp 300 345
repo_read.py search CopyOutGuest
repo_read.py cat core/libs/prx/libSceFont/include/FontInternal.hpp 1 50
repo_read.py cat core/libs/tests/GuestFont.cpp 1 30
repo_read.py search errno core/libs/tests/GuestFont.cpp
repo_read.py cat docs/core/libs/prx/libSceFont/src/Render.cpp 1 1
  • No build, unit test or convention script was run.

Results

Check Result Note
Changed hunks at head passed No defect found
Build and guest_font_tests not run Author-reported only
check_conventions.py not run Author-reported only
Relative link in change note failed Target path does not exist at head

Findings

Blocking

None.

Non-blocking

  • docs/dev/debt/changes/fix-libSceFont-getRenderScaledKerning-copyout.md:5: the link ../../../core/libs/prx/libSceFont/src/Render.cpp resolves to docs/core/... from this folder, and that path does not exist at head. The note is four levels below the root, so it needs four ../ to reach core/.

Sources

  • CopyOutGuest saves and restores errno:
    template <typename T>
    bool CopyOutGuest(T* destination, const T& value) {
    const int savedErrno = errno;
    const bool copied = GuestCopyOut_nid_no_patch(destination, &value, sizeof(T));
    errno = savedErrno;
    return copied;
    }
  • Changed function:
    int APS5_VABI sceFontGetRenderScaledKerning(FontHandle fontHandle, std::uint32_t preCode, std::uint32_t code, FontKerning* kerning) {
    if (!kerning) return SCE_FONT_ERROR_INVALID_PARAMETER;
    FontKerning localKerning{};
    if (!CopyOutGuest(kerning, localKerning)) return SCE_FONT_ERROR_INVALID_PARAMETER;
    auto* font = GetNativeFont(fontHandle);
    std::uint32_t fontLock = 0;
    if (!font || font->magic != HANDLE_MAGIC || !AcquireFontLock(font, fontLock)) return SCE_FONT_ERROR_INVALID_FONT_HANDLE;
    std::uint32_t cachedLock = 0;
    if (!AcquireCachedStyleLock(font, cachedLock)) {
    ReleaseFontLock(font, fontLock);
    return SCE_FONT_ERROR_INVALID_FONT_HANDLE;
    }
    int rc = SCE_FONT_ERROR_NOT_BOUND_RENDERER;
    if (font->renderer) {
    float scaleW = 0.0f;
    float scaleH = 0.0f;
    rc = StyleStateGetScalePixel(&font->cached_style.state, &scaleW, &scaleH);
    const FontState* state = TryGetState(fontHandle);
    if (rc == SCE_FONT_OK && state) {
    LoadKerning(*state, scaleW, scaleH, preCode, code, &localKerning);
    if (!CopyOutGuest(kerning, localKerning)) rc = SCE_FONT_ERROR_INVALID_PARAMETER;
    }
    }
    ReleaseCachedStyleLock(font, cachedLock);
    ReleaseFontLock(font, fontLock);
    return rc;
    }

Not verified

  • The build, guest_font_tests and check_conventions.py were not run. The description reports them as passing; that is the author's claim.
  • The new test passes 0x10 as the destination and expects SCE_FONT_ERROR_INVALID_PARAMETER with errno kept. This holds only if GuestCopyOut_nid_no_patch rejects 0x10 in the test fixture. I did not confirm that.
  • Console behaviour for an unwritable destination is not measured; the change note says so.
  • Only parts of two files were read: Render.cpp lines 300–345 of 641, and GuestFont.cpp lines 1–30 plus the changed hunk of 1259. The rest of both files was not read. FontInternal.hpp was read at lines 1–50.

To get this to a good state

Nothing is required.

Optional: in the change note, use four ../ before core/ so the link resolves.

@maskym

maskym commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Windows result for head 3f921d2038, merged alone into main 56c87adbdc (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. main itself fails to compile one test on Windows at this commit (UploadCacheReadOnlyFlush.cpp:177, setenv; #19899 fixes it), so the tree was built with ninja -k 0. Microsoft Defender on this machine quarantines 34 AGC test executables as Trojan:Win64/ShellCode.MKA!MTB when they are linked (the case docs/dev/BUILD.md describes), so their objcopy step fails on both trees and those tests do not run here. This PR adds no other build failure.

guest_font, the test this PR adds or extends, passes 3 of 3 runs. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head 3f921d2038, applied with git apply on main fd4aeed483 together with the other 27 PRs of one batch (#11049, #20072, #20406, #22892, #23774, #24199, #24495, #15314, #21180, #21221, #21486, #22597, #22839, #22895, #23550, #24672, #14573, #16873, #17113, #17879, #19183, #20057, #22066, #22219, #22781, #22958, #24478). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
guest_font, guest_font_ft_aliases, guest_font_ft_backoff and 18 more (21 tests, matched by name to the test file this PR changes) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6522 tests) on the RTX 3080 has 8 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Intermittent in this run (pass when rerun alone, or known flaky): agc_driver_store_over_untracked, agc_storage_write_back_host_retile. Traced to other PRs of the batch, each by reverting it and testing again: #21486 (agc_driver_graphics, agc_graphics_profile_draw), #24672 (guest_kernel_sys_neo_mode); none is in this PR's area. Failing tests I could not trace to a single PR (reverting the likely candidates did not clear them): guest_indexed_draw. The 2125 agc_/video tests on the iGPU have 5 failing: agc_driver_graphics, agc_driver_stencil_plane_memory, agc_driver_store_over_untracked, agc_graphics_profile_draw, agc_storage_write_back_host_retile (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

This was referenced Nov 21, 2026
@Kikoarl
Kikoarl force-pushed the fix/libSceFont-getRenderScaledKerning-copyout branch from 3f921d2 to bce7d3a Compare November 24, 2026 18:25
@Kikoarl

Kikoarl commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Rebased onto origin/main and pushed bce7d3ae95 adding Closes when: and Labels: metadata to the debt fragment. Note on the relative link (../../../core/libs/prx/libSceFont/src/Render.cpp): docs/dev/TechnicalDebt.md#L23 mandates that fragment links resolve relative to the target docs/dev/debt/<name>.md file (docs/dev/debt/libSceFont.md) rather than docs/dev/debt/changes/, and tools/check_conventions.py (L744-L752) enforces that base so folded entries have valid links (../../../../ fails check_conventions.py).

@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

Nothing blocks this change; the review found no verified blocking defects.

What I ran

Reviewed head bce7d3ae957db81434e73bd6e817ee76e01d58f9 using the prepared diff and repo_read.py for targeted source reads. No build or tests were run.

Results

Check Result Note
Diff and targeted source review passed Changed logic and test context inspected
Build and tests not run Author reports guest_font_tests passed
Conventions check not run Author reports it passed

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full test suite, build, conventions check, and physical-console behavior were not verified. All three changed files listed in the manifest were read; none were excluded from the review bundle.

To get this to a good state

Nothing is required.

@JJenkx

JJenkx commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Additional Linux coverage for exact head bce7d3ae957db81434e73bd6e817ee76e01d58f9, integrated alone into main 341a9eec16b77e91eed63c20688a907251d0e694.

Test-only follow-up commit, based directly on this PR head, extends GuestFont.cpp with read-only, wholly inaccessible and guard-page-crossing outputs, plus an exact-sized boundary output. It checks valid/null-font error precedence, errno preservation, untouched canary bytes, and a valid nonzero kerning result after rejected writes. No runtime implementation changes.

On Arch Linux 7.3.5, GCC 16.2.1, Release, the enhanced guest_font passes with the fixed function. The identical test executable fails with SIGSEGV when only that function is restored to main's body. Loader traces confirm the fresh raw font provider in each arm; executable/provider hashes remain unchanged, and neither run skips the case. The same added test lines are verified in the current-main integration and the offered peer-head commit. Public-patch conventions and whitespace checks pass.

Build/test logs, JUnit, loader proof, patches and source pins, SHA-256 eb6a42f3cdda58281be21b288dcdaebbfd7af9834e35114efbd84e534a0f2ba7 (anonymous download and all 26 payload hashes verified). Linux host behavior only; no console, Windows, title, GPU, FPS or patched-font execution claim.

AI-assisted: yes

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants