feat(libSceNKWebKit): implement WTF::StringImpl::utf8ForCharacters #14948

Closed
w4rc0n wants to merge 2 commits from upstream-pr/14948 into main
w4rc0n commented 2026-11-02 03:15:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by w4rc0n on 2026-11-02T03:15:05Z.


What

WTF::StringImpl::utf8ForCharacters was a NotImplemented stub in both overloads, (const LChar*, unsigned) and (const UChar*, unsigned, ConversionMode). Both now follow the pinned PS5 WebKit 11.x StringImpl.cpp and tryGetUTF8ForCharacters, and return Expected<CString, UTF8ConversionError>:

  • An input length above INT32_MAX / 2 (Latin-1) or INT32_MAX / 3 (UTF-16) returns OutOfMemory before any character is read, as in the source. As in the pinned allocator, a failed allocation still throws.
  • In strict mode, an unpaired lead surrogate at the end returns SourceExhausted, and any other invalid sequence returns IllegalSource (UTF8ConversionError.h).
  • Lenient mode replaces an unpaired surrogate with U+FFFD, except a lone lead surrogate at the end, which is written as its raw three-byte form (UTF8Conversion.cpp:66-75, StringImpl.cpp:1580-1585). StrictConversionReplacingUnpairedSurrogatesWithFFFD replaces every unpaired surrogate.

Both overloads reuse the UTF-8 conversion that StringImpl::utf8 already uses on main. That conversion gains an optional error output, so its existing callers keep throwing as before. Its lenient path wrote every unpaired surrogate raw. This PR corrects it, so String::utf8, StringImpl::utf8 and StringView::utf8 on main, and #14946's String printing, now also replace unpaired surrogates with U+FFFD in lenient mode, except a lone final lead surrogate, as the pinned source does. Expected<CString, UTF8ConversionError> has the same layout as #14946's Expected<String, …>:

  • a value or error union at offset 0;
  • the has-value flag at offset 8;
  • 16 bytes in total, returned through the caller's buffer.

The debt layout entry records that this layout is unverified against console binaries.

tools/pr_overlap.py on this head, with main at 0eba5db30d: no other open PR changes these exports or adds the same files. For the PRs it couldn't compare because they no longer merge with main, GitHub's file lists show none of them changing these exports either. #13992, #14172 and #15241 change nearby lines in the same files; whichever merges later keeps both sides. The only exports it shares are with #14947, in the same stack.

Tested

Debian 12, GCC 12.2, Linux 6.18, Release, at the head 37f1179ceb27cd82586e020ed3ff3f3f77c4b0c7, which is on #14946's head 59a53bbfcbd5eadd60fe1711a5157d57ca5bb3b2. No local changes.

  • guest_nkwebkit_dates, guest_nkwebkit_numbers, nkwebkit_base, nkwebkit_config, nkwebkit_constants, nkwebkit_fastbitvector, nkwebkit_jsc, nkwebkit_palsessionid, nkwebkit_preferences, nkwebkit_resourceresponse, nkwebkit_webcoregeometry, nkwebkit_wtf, guest_nkwebkit_urls and public_headers pass.
  • The new nkwebkit_wtf cases cover:
    • ASCII, Latin-1, BMP, astral and embedded-NUL input;
    • empty input in every mode;
    • nine malformed surrogate sequences in each of the three modes, including which error wins when there are several;
    • the same nine sequences through the existing String::utf8, StringImpl::utf8 and StringView::utf8 exports in lenient mode, and one older test that expected the raw output, now corrected;
    • four oversized lengths with null buffers, showing that the bound is checked before any read.
  • tools/progress.py: both exports count as implemented (libSceNKWebKit 1348, against 1346 with #14946).
  • tools/check_conventions.py passes.
  • Community check (@BunnyFl, in review): the two length limits were compared with firmware 13.42's own libSceNKWebKit.sprx, read from disassembly only and not measured on a console. utf8ForCharacters(const LChar*, unsigned) (o5VwYgTgLtU) starts with cmp edx, 0x40000000, and the UChar overload (NvD7vqlu22E) with cmp edx, 0x2aaaaaab. These are the same MaxStringLength / 2 and / 3 bounds as this PR. The surrogate handling was not compared with the module.
  • Windows (GitHub-hosted CI in the author's fork): run 41385242010 built this branch's previous head a3b0234339d1e891d111f410cf9a3e7dd30d9ca6. It is AnyPS5's Build workflow, dispatched in the author's fork with select=true: the branch merged into main at 926f7dd492b0b432d9287f4d6faaaf1c9d3c86c5, built on windows-latest with WinLibs GCC 15.2 (MinGW-w64, UCRT). None of the 672 selected tests failed; 6 AGC tests were skipped, which check_skips.py accepts. That includes the nine NKWebKit tests, public_headers and patched_libraries. The later rebases onto main (now 0eba5db30d8a3d03789e52e5c1a9ff853e4e33e4, after #14479, #13766, #14925 and #15911) changed only docs/dev/debt/libSceNKWebKit.md, so git range-diff shows the code and tests under test are the same as at that run. A fork-internal pull request existed only to give the workflow a number.

Not built or run: macOS. No title or homebrew was run.

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: #14946
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5114388151 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14948). Originally posted by [w4rc0n](https://github.com/w4rc0n) on 2026-11-02T03:15:05Z. --- ### What `WTF::StringImpl::utf8ForCharacters` was a `NotImplemented` stub in both overloads, `(const LChar*, unsigned)` and `(const UChar*, unsigned, ConversionMode)`. Both now follow the pinned PS5 WebKit 11.x [`StringImpl.cpp`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/text/StringImpl.cpp#L1533-L1611) and [`tryGetUTF8ForCharacters`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/text/StringImpl.h#L1491-L1560), and return `Expected<CString, UTF8ConversionError>`: - An input length above `INT32_MAX / 2` (Latin-1) or `INT32_MAX / 3` (UTF-16) returns `OutOfMemory` before any character is read, as in the source. As in the pinned allocator, a failed allocation still throws. - In strict mode, an unpaired lead surrogate at the end returns `SourceExhausted`, and any other invalid sequence returns `IllegalSource` ([`UTF8ConversionError.h`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/text/UTF8ConversionError.h#L30-L34)). - Lenient mode replaces an unpaired surrogate with U+FFFD, except a lone lead surrogate at the end, which is written as its raw three-byte form ([`UTF8Conversion.cpp:66-75`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/unicode/UTF8Conversion.cpp#L66-L75), [`StringImpl.cpp:1580-1585`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/text/StringImpl.cpp#L1580-L1585)). `StrictConversionReplacingUnpairedSurrogatesWithFFFD` replaces every unpaired surrogate. Both overloads reuse the UTF-8 conversion that `StringImpl::utf8` already uses on `main`. That conversion gains an optional error output, so its existing callers keep throwing as before. Its lenient path wrote every unpaired surrogate raw. This PR corrects it, so `String::utf8`, `StringImpl::utf8` and `StringView::utf8` on `main`, and #14946's `String` printing, now also replace unpaired surrogates with U+FFFD in lenient mode, except a lone final lead surrogate, as the pinned source does. `Expected<CString, UTF8ConversionError>` has the same layout as #14946's `Expected<String, …>`: - a value or error union at offset 0; - the has-value flag at offset 8; - 16 bytes in total, returned through the caller's buffer. The debt layout entry records that this layout is unverified against console binaries. `tools/pr_overlap.py` on this head, with `main` at `0eba5db30d`: no other open PR changes these exports or adds the same files. For the PRs it couldn't compare because they no longer merge with `main`, GitHub's file lists show none of them changing these exports either. #13992, #14172 and #15241 change nearby lines in the same files; whichever merges later keeps both sides. The only exports it shares are with #14947, in the same stack. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Debian 12, GCC 12.2, Linux 6.18, Release, at the head `37f1179ceb27cd82586e020ed3ff3f3f77c4b0c7`, which is on #14946's head `59a53bbfcbd5eadd60fe1711a5157d57ca5bb3b2`. No local changes. - `guest_nkwebkit_dates`, `guest_nkwebkit_numbers`, `nkwebkit_base`, `nkwebkit_config`, `nkwebkit_constants`, `nkwebkit_fastbitvector`, `nkwebkit_jsc`, `nkwebkit_palsessionid`, `nkwebkit_preferences`, `nkwebkit_resourceresponse`, `nkwebkit_webcoregeometry`, `nkwebkit_wtf`, `guest_nkwebkit_urls` and `public_headers` pass. - The new `nkwebkit_wtf` cases cover: - ASCII, Latin-1, BMP, astral and embedded-NUL input; - empty input in every mode; - nine malformed surrogate sequences in each of the three modes, including which error wins when there are several; - the same nine sequences through the existing `String::utf8`, `StringImpl::utf8` and `StringView::utf8` exports in lenient mode, and one older test that expected the raw output, now corrected; - four oversized lengths with null buffers, showing that the bound is checked before any read. - `tools/progress.py`: both exports count as implemented (libSceNKWebKit 1348, against 1346 with #14946). - `tools/check_conventions.py` passes. - **Community check (@BunnyFl, in review):** the two length limits were compared with firmware 13.42's own `libSceNKWebKit.sprx`, read from disassembly only and not measured on a console. `utf8ForCharacters(const LChar*, unsigned)` (`o5VwYgTgLtU`) starts with `cmp edx, 0x40000000`, and the `UChar` overload (`NvD7vqlu22E`) with `cmp edx, 0x2aaaaaab`. These are the same `MaxStringLength / 2` and `/ 3` bounds as this PR. The surrogate handling was not compared with the module. - **Windows (GitHub-hosted CI in the author's fork):** [run 41385242010](https://github.com/w4rc0n/AnyPS5/actions/runs/41385242010) built this branch's previous head `a3b0234339d1e891d111f410cf9a3e7dd30d9ca6`. It is AnyPS5's `Build` workflow, dispatched in the author's fork with `select=true`: the branch merged into `main` at `926f7dd492b0b432d9287f4d6faaaf1c9d3c86c5`, built on `windows-latest` with WinLibs GCC 15.2 (MinGW-w64, UCRT). None of the 672 selected tests failed; 6 AGC tests were skipped, which `check_skips.py` accepts. That includes the nine NKWebKit tests, `public_headers` and `patched_libraries`. The later rebases onto `main` (now `0eba5db30d8a3d03789e52e5c1a9ff853e4e33e4`, after #14479, #13766, #14925 and #15911) changed only `docs/dev/debt/libSceNKWebKit.md`, so `git range-diff` shows the code and tests under test are the same as at that run. A fork-internal pull request existed only to give the workflow a number. Not built or run: macOS. No title or homebrew was run. ### 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: #14946 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
BunnyFl commented 2026-11-02 16:54:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T16:54:57Z.

Review: APPROVED


Cross-checked the length limits against firmware 13.42's own libSceNKWebKit.sprx (system_ex/common_ex/lib, sha256 e63acdc164592330a287cf6914c265336b27a357098a449c1609f8f82e104409), read from disassembly only, not measured on a console:

  • utf8ForCharacters(const LChar*, unsigned) (o5VwYgTgLtU, 0x1811e0) starts with cmp edx, 0x40000000.
  • utf8ForCharacters(const UChar*, unsigned, ConversionMode) (NvD7vqlu22E, 0x211f00) starts with cmp edx, 0x2aaaaaab.

Those are exactly length > MaxStringLength / 2 and length > MaxStringLength / 3 in WtfString.cpp:1297 and :1302, so the early OutOfMemory bounds match the shipped module, not just the pinned source.

Build and test on Windows (WinLibs GCC, BUILD_TESTING=ON), PR merged into current main: nkwebkit_wtf passes. With the PR's source file taken from main, the test executable doesn't link, since the exports are new, so there's no main control.

I didn't compare the strict and lenient surrogate handling against the module. That part rests on the pinned UTF8Conversion.cpp, and the tests cover it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5740144881 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14948#pullrequestreview-5740144881). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T16:54:57Z. **Review:** APPROVED --- Cross-checked the length limits against firmware 13.42's own `libSceNKWebKit.sprx` (`system_ex/common_ex/lib`, sha256 `e63acdc164592330a287cf6914c265336b27a357098a449c1609f8f82e104409`), read from disassembly only, not measured on a console: - `utf8ForCharacters(const LChar*, unsigned)` (`o5VwYgTgLtU`, `0x1811e0`) starts with `cmp edx, 0x40000000`. - `utf8ForCharacters(const UChar*, unsigned, ConversionMode)` (`NvD7vqlu22E`, `0x211f00`) starts with `cmp edx, 0x2aaaaaab`. Those are exactly `length > MaxStringLength / 2` and `length > MaxStringLength / 3` in `WtfString.cpp:1297` and `:1302`, so the early `OutOfMemory` bounds match the shipped module, not just the pinned source. Build and test on Windows (WinLibs GCC, `BUILD_TESTING=ON`), PR merged into current `main`: `nkwebkit_wtf` passes. With the PR's source file taken from `main`, the test executable doesn't link, since the exports are new, so there's no `main` control. I didn't compare the strict and lenient surrogate handling against the module. That part rests on the pinned `UTF8Conversion.cpp`, and the tests cover it.
BunnyFl commented 2026-11-05 03:18:04 +00:00 (Migrated from github.com)

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

Review: APPROVED


@w4rc0n re-checked after the force-push. 37f1179ceb is a rebase of a3b0234339, the head I approved: git range-diff shows no change to any code or test line. The only differences are hunk context and the dropped edit of the "Additional callable exports" count line in docs/dev/debt/libSceNKWebKit.md, which main has since reworded.

On 37f1179ceb merged onto main 0540ef0ef2 (Windows 11, WinLibs GCC 15.2.0, Release, nkwebkit_wtf_tests rebuilt), nkwebkit_wtf passed, 1 of 1. With main's core/libs/prx/libSceNKWebKit/src/WtfString.cpp put back, nkwebkit_wtf_tests doesn't link. That says nothing about behaviour, and I added a mutation check this time. Mutation check, on the same merge: with the new unpaired-surrogate branch of ToUTF8 always storing error 1 (instead of 2 for a trailing high surrogate), nkwebkit_wtf fails in the new test's error-code check. check_conventions.py passes.

pr_overlap.py --branch: the rows with the same exports are your own #14946 (which this one builds on) and #14947 (also stacked on #14946). The other comparable rows share lines in Export.cpp, the export headers or the debt file but no exports; I haven't checked each of them against this PR. The tool couldn't compare about 50 more PRs, which conflict with main.

Approving again at 37f1179ceb.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5779421921 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14948#pullrequestreview-5779421921). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-05T03:18:04Z. **Review:** APPROVED --- @w4rc0n re-checked after the force-push. `37f1179ceb` is a rebase of `a3b0234339`, the head I approved: `git range-diff` shows no change to any code or test line. The only differences are hunk context and the dropped edit of the "Additional callable exports" count line in `docs/dev/debt/libSceNKWebKit.md`, which `main` has since reworded. On `37f1179ceb` merged onto `main` `0540ef0ef2` (Windows 11, WinLibs GCC 15.2.0, Release, `nkwebkit_wtf_tests` rebuilt), `nkwebkit_wtf` passed, 1 of 1. With `main`'s `core/libs/prx/libSceNKWebKit/src/WtfString.cpp` put back, `nkwebkit_wtf_tests` doesn't link. That says nothing about behaviour, and I added a mutation check this time. Mutation check, on the same merge: with the new unpaired-surrogate branch of `ToUTF8` always storing error 1 (instead of 2 for a trailing high surrogate), `nkwebkit_wtf` fails in the new test's error-code check. `check_conventions.py` passes. `pr_overlap.py --branch`: the rows with the same exports are your own #14946 (which this one builds on) and #14947 (also stacked on #14946). The other comparable rows share lines in `Export.cpp`, the export headers or the debt file but no exports; I haven't checked each of them against this PR. The tool couldn't compare about 50 more PRs, which conflict with `main`. Approving again at `37f1179ceb`. AI-assisted: yes (Claude Code)

Pull request closed

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!14948
No description provided.