Skip to content

fix(libSceFont): accept overlapping render surface rows - #8250

Merged
oneandonlydean merged 4 commits into
boykopovar:mainfrom
vittoriosalvadore:codex/fix-font-render-surface-stride
Nov 1, 2026
Merged

oneandonlydean merged 4 commits into
boykopovar:mainfrom
vittoriosalvadore:codex/fix-font-render-surface-stride

Conversation

@vittoriosalvadore

@vittoriosalvadore vittoriosalvadore commented Oct 20, 2026 •

Copy link
Copy Markdown

What

Accept a zero row stride and preserve short positive strides in Font character rendering. Rows may overlap: for positive dimensions their address range is (height - 1) * stride + width * pixelSize, rather than height * stride. An allocation covering that range is sufficient; the API has no allocation length to validate the caller's actual storage. Clipping by surface dimensions and scissor keeps raster writes inside it.

Remove the earlier short-stride rejection and keep main's style-frame/baseline error precedence. Zero width or height retains main's successful metrics-only result, an empty update rectangle and no pixel writes. Negative strides still return SCE_FONT_ERROR_NO_SUPPORT_SURFACE; the difference from the measured module is explicit in technical debt.

Source: BunnyFl's native x86-64 execution of firmware 13.42 Font/FontFt modules and the collaborator's requested correction. This is a published firmware-module harness result, not our own measurement or a console-hardware run. The reported four-byte surface metadata with one-byte coverage writes remains an unresolved format difference; this patch preserves the existing host four-byte raster and records that gap.

Tested

Windows x86-64, WinLibs GCC 15.2.0 r7 (UCRT/POSIX), CMake 4.3.1 and Ninja 1.13.2; root Release configuration, BUILD_TESTING=ON, relinker-only OFF, system dependencies OFF, pinned bundled submodules. Final head 6b6a14635c38229ac220ea51a2b2b7a8d96458c8, based on main f7f03daf52f11191174b1be089a56e268390a707.

  • Full Windows library/relinker build passed: cmake --build build --parallel 3 --target libs relinker, plus all focused Font test targets.
  • All 17 focused Font/FreeType CTest entries pass with zero failures and zero skips, before and after both negative controls and on the final merged head. Command: ctest --test-dir build -R '^(font_render_surface|guest_font.*|guest_system_font|font_face_lifetime.*|guest_freetype)$' -V --timeout 30 --parallel 2.
  • Negative control 1: keep the regression and restore only main's Layout.cpp. It fails at stride 0 with 0x80460050. Negative control 2: reinsert only the old early stride guards into the current Render.cpp. It fails in the mixed-invalid style-frame case by throwing instead of retaining the earlier error. Restore committed production sources, rebuild and rerun all 17: pass, zero skips. The production baseline used for these controls is byte-identical in the final main snapshot.
  • Source audit: 5074 tracked files match Git after normalizing line endings; all five changed blob SHA-256 hashes match the compiled tree. Header declaration audit passes for 373 libraries / 14255 exports. Conventions and whitespace checks pass. Fresh independent Astra (gpt-6-astra, reasoning high) final review completed before push.

The regression uses an asymmetric generated TrueType glyph with the real Font/FontFt/FreeType libraries. It compares the complete buffer with a wide-stride reference projected into overlapping rows in drawing order, including zero coverage inside the update rectangle. Guards surround the declared extent. Coverage includes all three character-rendering exports; strides 0, 1, 16, 64, 130, exact, padded and row-size-minus-one; one- and four-byte host pixels; scissor; 0x00/0xEE initial buffers; height 1; zero width/height; invalid descriptors; mixed-invalid errors and a missing driver with an active style frame followed immediately by a valid rendering.

The one-byte address rule is checked against the formula reported by BunnyFl. The fixture is not their DFHEI5-SONY2022.ttf, so it does not claim to reproduce that font's 309 changed bytes. Four-byte cases verify the current host raster's addressing and extent, not equivalence with the reported firmware raster.

pr_overlap examined 1490 open PRs and found no confirmed overlap. Its only uncompleted shared-file comparison was #12757's Font debt file; its complete diff was reviewed manually: it adds GetLibrary/SetFontsOpenMode test/debt entries, not surface rendering or this regression. No dependency is required. The collaborator's existing branch merge is preserved in history.

No title, proprietary font, console-hardware run, Linux/macOS execution of this revision, GPU execution or full repository CTest run is claimed. Full upstream Linux/Windows CI requires a maintainer dispatch under CONTRIBUTING.md.

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

@vittoriosalvadore
vittoriosalvadore force-pushed the codex/fix-font-render-surface-stride branch from 3289ac8 to 1d20721 Compare October 22, 2026 05:41
@vittoriosalvadore

Copy link
Copy Markdown
Author

Thank you. Rebased #8250 on main and resolved the documentation conflict. All seven focused Font tests pass with zero skips; the full Windows library/relinker build and negative control also pass. A fresh Astra high review found no issues. The branch is ready for full upstream CI.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 22, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 22, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@vittoriosalvadore #9992 has just merged, and it changes one case your test checks. On main, a render surface with a zero width or height (and a nonnegative stride) now returns SCE_FONT_OK and leaves the buffer untouched, because a title renders a glyph into a zero-extent surface to measure it. Your new FontRenderSurface.cpp:128 still expects NO_SUPPORT_SURFACE for the height-0 surface, so font_render_surface aborts there once this branch includes main.

Could you merge main and change that height-0 row to expect SCE_FONT_OK with the pixels untouched? The stride check this PR adds is unaffected; it only applies to surfaces with positive width and height. There is also a small conflict at the top of docs/dev/debt/libSceFont.md where both PRs add a bullet; keeping both is right.

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

Checked against main 522e32e and core/libs/prx/libSceFont/src/Layout.cpp / Render.cpp on main:

  • Besides the debt-file conflict and the #9992 test row already mentioned, Render.cpp merges cleanly, and the stride guard's early returns (width/height <= 0, stride <= 0) agree with main's zero-extent rule.
  • A question on the premise: the API doesn't pass a buffer size, and main writes at buffer + row*widthByte + column*bpp, clipped to width/height (Layout.cpp:164-188). With height 1, width 80 and stride 16, main only touches bytes 0-79, which is in bounds for an 80-byte buffer, yet this PR throws. With more rows, a short stride makes rows overlap but stays within (h-1)*stride + w*bpp. The "wrote beyond buffer" result comes from the test treating the buffer as stride*height. Could the description and debt entry describe this as a guard for an unverified case rather than an overflow fix? Has any title been seen passing such a surface?
  • Small one: in sceFontRenderCharGlyphImage the guard now runs before the style-frame/baseline errors (preRc), so that error code is replaced by a throw when both conditions hold.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@vittoriosalvadore @harisibrar1 I agree that our earlier overflow claim was too broad. The API has no allocation length; a short stride can overlap rows while staying inside an allocation covering (height - 1) * stride + width * bytesPerPixel. The existing negative control demonstrates an overrun of the test’s stride-times-height allocation, not every possible caller allocation. Please describe this as a conservative guard for an unverified surface layout, and separate the measured test result from the assumed allocation convention.

I still need the guard to preserve earlier error precedence: sceFontRenderCharGlyphImage currently throws before style-frame/baseline preRc failures are checked. Please retain those errors, add a mixed-invalid regression and ensure locks are released on a throw. The rebase/debt conflict and zero-height fixture update after #9992 also remain open. I’m changing the prior overall PASS to CHANGES until these are addressed.

@BunnyFl

BunnyFl commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Data for the open question about the surface layout. I ran firmware 13.42's libSceFont.sprx (sha256 b96390d9b766c05f…) with its libSceFontFt renderer natively on x86-64. The font was the dump's DFHEI5-SONY2022.ttf at 40 px, rendering 'A' at (10, 10) with sceFontRenderCharGlyphImage onto surfaces from sceFontRenderSurfaceInit. The buffer sat inside a canary block that also covered 64 KiB before it. I filled the block with 0x00 and, in a second pass, with 0xEE, then compared every byte. The table shows the 0x00 pass. The 0xEE pass changed fewer bytes, all inside the same ranges. Three runs gave identical output.

surface (stride, pixel size, width × height) result bytes changed offsets from the buffer
128, 1, 128 × 64 (control) 0 309 1810 to 5789
64, 1, 128 × 64 0 309 914 to 2909
16, 1, 80 × 64 0 287 242 to 749
1, 1, 128 × 64 0 43 32 to 74
16, 1, 80 × 1 0 0 (the glyph's rows 14 to 45 are outside a 1-row surface)
512, 4, 128 × 64 (control) 0 309 7186 to 23069
256, 4, 128 × 64 0 309 3602 to 11549
130, 4, 128 × 64 0 309 1838 to 5879
  • No refusal: every surface whose stride is smaller than width × pixel size returned 0 and drew. None of the calls failed.
  • Where pixels land: at 1 byte per pixel, the changed set is exactly row × stride + column of the control image's pixels, for strides 128, 64, 16 and 1. Where rows overlap (strides 16 and 1), some values differ from the control. Every write stayed within the (height − 1) × stride + width × pixel size extent discussed above, and nothing in the 64 KiB before the buffer changed.
  • Pixel size 4: the surface and output kept pixel size 4. The output's image address was buffer + row × stride + column × 4 (+7208 for the control). The 309 changed bytes were the same coverage values at row × stride + column, one byte per pixel, starting at +7186. I didn't look further into this path.
  • sceFontRenderSurfaceInit stored the stride as given (also 0 and −64) and didn't reject any of these. A stride of 0 drew every row into row 0 (20 bytes at +10 to +29) with result 0. A stride of −64 drew the 309 bytes at −2934 to −939, before the buffer pointer, also with result 0.

So 13.42's module treats a short stride as a valid surface: the rows overlap, nothing is refused, and the writes stay in the (height − 1) × stride + width × pixel size extent. On that evidence, the throw guards a case the module accepts and draws, not one it rejects. For the error-order point, these surfaces got no stride error from the module, so there was none to order against the style-frame/baseline errors.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks @BunnyFl, that settles the layout question. @vittoriosalvadore, on 13.42 a stride shorter than width × pixel size is a valid surface: the module draws with overlapping rows and its writes stay within (height − 1) × stride + width × pixel size. So instead of throwing for a short stride, please accept it and bound the writes to that extent, with a test that checks the overlapping-row result against BunnyFl's numbers. A negative stride would write before the buffer, so it can stay an explicit refusal noted in the debt entry. That also removes the error-order question. The zero-height expectation after #9992 is still open.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@vittoriosalvadore this branch now conflicts with main in core/libs/prx/libSceFont/CMakeLists.txt and docs/dev/debt/libSceFont.md. We tried merging main in: both conflicts are simple (keep both sides), but on the merged tree font_render_surface fails at FontRenderSurface.cpp:109.

The reason is a behaviour change on main: #10892 made the glyph render functions accept a surface whose width or height is 0 and return the metrics with an empty update rectangle (see its entry in docs/dev/debt/libSceFont.md). Your test still expects SCE_FONT_ERROR_NO_SUPPORT_SURFACE for height == 0 (the invalid == 1 case). The null-buffer and negative-height cases still agree with main.

Could you merge main, then drop or update the height == 0 case to match #10892's behaviour (and check that your stride check doesn't reject a zero-extent surface that main now accepts)? Everything else in the PR is unchanged by the merge.

@vittoriosalvadore vittoriosalvadore changed the title fix(libSceFont): reject undersized render surface strides fix(libSceFont): accept overlapping render surface rows Oct 30, 2026
@vittoriosalvadore

Copy link
Copy Markdown
Author

Thank you for the measurements and review. #8250 now accepts short/zero strides, preserves earlier errors and covers zero extents. All 17 focused Windows tests pass with zero skips, and the full library/relinker build passes. The four-byte raster gap is documented.

@oneandonlydean Could you run full Linux/Windows CI (full=true, linux=true, select=false)? Thank you.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 30, 2026
@github-actions

github-actions Bot commented Nov 1, 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 14212 docs/dev/debt/libSceFont.md

@oneandonlydean
oneandonlydean merged commit 4a027f1 into boykopovar:main Nov 1, 2026
11 of 12 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.

4 participants