feat(libSceIcu): copy the UTF-8 conversion operands through guest memory #25526

Merged
pinewhite merged 1 commit from upstream-pr/25526 into main 2026-11-24 17:47:19 +00:00
pinewhite commented 2026-11-22 07:23:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pinewhite on 2026-11-22T07:23:51Z.


What

u_strFromUTF8, u_strFromUTF8WithSub, u_strToUTF8 and u_strToUTF8WithSub now convert between a source copied in from guest memory and an output buffer written once, instead of dereferencing the guest source directly and writing the destination unit by unit behind a GuestArena::HostWrite guard that threw NotImplemented_nid_no_patch when it could not open.

  • The source is copied in through GuestCopyIn in page-sized chunks, stopping after the terminator for a -1 length, as Utf32.cpp and JavaUtf8.cpp already do. An unreadable source throws std::invalid_argument instead of faulting on the guest address.
  • The output is built in a host vector and written once through GuestCopyOut. The write guard over the destination, the destination length, the substitution count and the status is still opened before anything is read or written, so a refused write throws before any output changes.
  • The substitution count and the destination length are stored before termination, and the status word is re-read from the guest the way ICU's aliased pointers see it, so a caller that aliases pDestLength, pNumSubstitutions and pErrorCode gets the same final word as ICU.
  • Error codes, termination and the truncated-substitution geometry follow ICU 68.2 ustrtrns.cpp, which differs from the pinned ICU 78.1 copy only in pointer and type spellings for these functions. Malformed sequences consume the maximal subpart through the in-tree utf8_nextCharSafeBody_nid_postfix, implemented from utf_impl.cpp in Utf8.cpp.

u_strFromUTF8Lenient is untouched because #22460 rewrites that function.

python3 tools/progress.py --module libSceIcu: done 321 -> 325 of 2766 (11.61% -> 11.75%). The four exports no longer reach NotImplemented_nid_no_patch; nothing else moved.

Tested

Host proof on Ubuntu 24.04 aarch64, g++ 13.3.0, -std=c++20 -O1 -ffunction-sections -fdata-sections -Icore/libs, linked with -Wl,--gc-sections:

  • core/libs/prx/libSceIcu/UtfConversions.cpp and Utf8.cpp compiled by hand into a runner. Shimmed seams: GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch (memcpy over host memory plus a registry of inaccessible ranges), GuestArenaBeginHostWrite_nid_postfix, GuestArenaEndHostWrite_nid_postfix and NotImplementedAt_nid_no_patch.
  • core/libs/tests/GuestIcuUtfConversions.cpp with the new cases: exit 0 against the new object, exit 0 against the pre-change object.
  • Differential against host ICU 74 (u_strFromUTF8WithSub_74, u_strToUTF8WithSub_74): 20,000 randomized decode and encode cases with capacities 0-14, lengths 0-12, -1 and counted sources, and substitutions -1, U+FFFD, NUL and U+1F642, plus 200 aliased-scalar layouts; return, length, status, substitution count and every output byte agree.
  • Pre-change body with an unmapped source buffer: SIGSEGV, exit 139. New body: std::invalid_argument, exit 0.

Not run: the CMake/ctest guest suites (no PS5 toolchain, ICU 78.1 build or emulator run in this environment), the Windows write-watch path this guard also serves, and any console comparison.

Note: PR #22460 rewrites u_strFromUTF8Lenient in the same file and edits the same test file. The lenient body here is byte-for-byte unchanged, so the overlap is adjacent hunks plus the test insertion point and needs a rebase whichever merges second.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5436856706 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25526). Originally posted by [pinewhite](https://github.com/pinewhite) on 2026-11-22T07:23:51Z. --- ### What `u_strFromUTF8`, `u_strFromUTF8WithSub`, `u_strToUTF8` and `u_strToUTF8WithSub` now convert between a source copied in from guest memory and an output buffer written once, instead of dereferencing the guest source directly and writing the destination unit by unit behind a `GuestArena::HostWrite` guard that threw `NotImplemented_nid_no_patch` when it could not open. - The source is copied in through `GuestCopyIn` in page-sized chunks, stopping after the terminator for a `-1` length, as `Utf32.cpp` and `JavaUtf8.cpp` already do. An unreadable source throws `std::invalid_argument` instead of faulting on the guest address. - The output is built in a host vector and written once through `GuestCopyOut`. The write guard over the destination, the destination length, the substitution count and the status is still opened before anything is read or written, so a refused write throws before any output changes. - The substitution count and the destination length are stored before termination, and the status word is re-read from the guest the way ICU's aliased pointers see it, so a caller that aliases `pDestLength`, `pNumSubstitutions` and `pErrorCode` gets the same final word as ICU. - Error codes, termination and the truncated-substitution geometry follow ICU 68.2 `ustrtrns.cpp`, which differs from the pinned ICU 78.1 copy only in pointer and type spellings for these functions. Malformed sequences consume the maximal subpart through the in-tree `utf8_nextCharSafeBody_nid_postfix`, implemented from `utf_impl.cpp` in `Utf8.cpp`. `u_strFromUTF8Lenient` is untouched because #22460 rewrites that function. `python3 tools/progress.py --module libSceIcu`: done 321 -> 325 of 2766 (11.61% -> 11.75%). The four exports no longer reach `NotImplemented_nid_no_patch`; nothing else moved. ### Tested Host proof on Ubuntu 24.04 aarch64, g++ 13.3.0, `-std=c++20 -O1 -ffunction-sections -fdata-sections -Icore/libs`, linked with `-Wl,--gc-sections`: - `core/libs/prx/libSceIcu/UtfConversions.cpp` and `Utf8.cpp` compiled by hand into a runner. Shimmed seams: `GuestCopyIn_nid_no_patch` and `GuestCopyOut_nid_no_patch` (memcpy over host memory plus a registry of inaccessible ranges), `GuestArenaBeginHostWrite_nid_postfix`, `GuestArenaEndHostWrite_nid_postfix` and `NotImplementedAt_nid_no_patch`. - `core/libs/tests/GuestIcuUtfConversions.cpp` with the new cases: exit 0 against the new object, exit 0 against the pre-change object. - Differential against host ICU 74 (`u_strFromUTF8WithSub_74`, `u_strToUTF8WithSub_74`): 20,000 randomized decode and encode cases with capacities 0-14, lengths 0-12, `-1` and counted sources, and substitutions `-1`, U+FFFD, NUL and U+1F642, plus 200 aliased-scalar layouts; return, length, status, substitution count and every output byte agree. - Pre-change body with an unmapped source buffer: SIGSEGV, exit 139. New body: `std::invalid_argument`, exit 0. Not run: the CMake/ctest guest suites (no PS5 toolchain, ICU 78.1 build or emulator run in this environment), the Windows write-watch path this guard also serves, and any console comparison. Note: [PR #22460](https://github.com/boykopovar/AnyPS5/pull/22460) rewrites `u_strFromUTF8Lenient` in the same file and edits the same test file. The lenient body here is byte-for-byte unchanged, so the overlap is adjacent hunks plus the test insertion point and needs a rebase whichever merges second. ### Checklist - [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
PrivacyOnion commented 2026-11-22 07:26:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-22T07:26:12Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_icu_utf_conversions_tests built cleanly.
  • CTest (guest_icu_utf_conversions):
    1/1 Test #4098: guest_icu_utf_conversions ........   Passed    0.62 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceIcu/UtfConversions.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4098: guest_icu_utf_conversions ........   Passed    0.16 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Audited FromUtf8, ToUtf8, and Decode in core/libs/prx/libSceIcu/UtfConversions.cpp against ICU 68.2/78.1 ustrtrns.cpp specifications and utf8_nextCharSafeBody_nid_postfix semantics.
    • Audited memory safety and GuestCopyIn/GuestCopyOut page-boundary handling in ReadSource, Complete, CopyOutput, and OutputGuard.
    • Checked error code priority and status pointer aliasing with destinationLength and substitutions in Complete and WriteStatus.
    • Audited AGENTS.md compliance, verifying technical debt fragment docs/dev/debt/changes/feat-icu-string-utf8-conversions.md, absence of non-conforming comments, and convention gate results.
    • Verified test suite coverage in core/libs/tests/GuestIcuUtfConversions.cpp covering substitution geometry, empty inputs, page-straddling long strings, and error codes.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6011043415 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25526#pullrequestreview-6011043415). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-22T07:26:12Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `a9e370a789`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_icu_utf_conversions_tests` built cleanly. - **CTest** (`guest_icu_utf_conversions`): ``` 1/1 Test #4098: guest_icu_utf_conversions ........ Passed 0.62 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceIcu/UtfConversions.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4098: guest_icu_utf_conversions ........ Passed 0.16 sec 100% tests passed, 0 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Audited FromUtf8, ToUtf8, and Decode in core/libs/prx/libSceIcu/UtfConversions.cpp against ICU 68.2/78.1 ustrtrns.cpp specifications and utf8_nextCharSafeBody_nid_postfix semantics. - Audited memory safety and GuestCopyIn/GuestCopyOut page-boundary handling in ReadSource, Complete, CopyOutput, and OutputGuard. - Checked error code priority and status pointer aliasing with destinationLength and substitutions in Complete and WriteStatus. - Audited AGENTS.md compliance, verifying technical debt fragment docs/dev/debt/changes/feat-icu-string-utf8-conversions.md, absence of non-conforming comments, and convention gate results. - Verified test suite coverage in core/libs/tests/GuestIcuUtfConversions.cpp covering substitution geometry, empty inputs, page-straddling long strings, and error codes. Looks Good To Me
MarcZX10 commented 2026-11-22 07:41:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T07:41:21Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this change based on the inspected code; the host experiments and differential results below are author-reported, and console behavior remains unverified.

What I ran

Reviewed head a9e370a789c26ef061d9323c289a3d84b5a2fb0c using the prepared diff and the pinned repo_read.py reader. No tests or build commands were run.

Files read: review/description.md, review/commits.md, review/manifest.json, review/diff.patch, core/libs/prx/libSceIcu/UtfConversions.cpp (reader output was truncated after line 100), core/libs/tests/GuestIcuUtfConversions.cpp (reader output was truncated after line 100), and docs/dev/debt/changes/feat-icu-string-utf8-conversions.md. All three changed files are listed as reviewed in the manifest; the changed source and test files were not fully read, so this review is incomplete.

Results

Check Result Note
Diff and targeted code review not run Reader output truncated
Host experiments not run Reported by author
Guest suites and console comparison not run Reported by author

Findings

Blocking

None identified in the portions inspected.

Non-blocking

None.

Not verified

The full changed source and test files were not read; reader output stopped at line 100. The CMake/ctest guest suites, Windows write-watch path, and console comparison were not run, as also stated in the PR description. The author's reported host proof and ICU differential were not independently verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6011086088 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25526#pullrequestreview-6011086088). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T07:41:21Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this change based on the inspected code; the host experiments and differential results below are author-reported, and console behavior remains unverified. ## What I ran Reviewed head `a9e370a789c26ef061d9323c289a3d84b5a2fb0c` using the prepared diff and the pinned `repo_read.py` reader. No tests or build commands were run. Files read: `review/description.md`, `review/commits.md`, `review/manifest.json`, `review/diff.patch`, `core/libs/prx/libSceIcu/UtfConversions.cpp` (reader output was truncated after line 100), `core/libs/tests/GuestIcuUtfConversions.cpp` (reader output was truncated after line 100), and `docs/dev/debt/changes/feat-icu-string-utf8-conversions.md`. All three changed files are listed as reviewed in the manifest; the changed source and test files were not fully read, so this review is incomplete. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted code review | not run | Reader output truncated | | Host experiments | not run | Reported by author | | Guest suites and console comparison | not run | Reported by author | ## Findings ### Blocking None identified in the portions inspected. ### Non-blocking None. ## Not verified The full changed source and test files were not read; reader output stopped at line 100. The CMake/ctest guest suites, Windows write-watch path, and console comparison were not run, as also stated in the PR description. The author's reported host proof and ICU differential were not independently verified. ## To get this to a good state Nothing is required.
throny commented 2026-11-22 16:57:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by throny on 2026-11-22T16:57:00Z.

Review: COMMENTED


Review: ⚠️ the conversions match ICU 78.1, but the new tests also pass on main

Basis: code read against 3rdparty/icu 049e0d6a42 (ustrtrns.cpp) · head a9e370a789

Checked

  • An incoming error status returns early (UtfConversions.cpp:195, :245), and an incoming warning carries on. Argument validation, pDestLength on overflow, U_STRING_NOT_TERMINATED_WARNING/U_BUFFER_OVERFLOW_ERROR and the invalid-character partial output all follow ICU's slow paths. The existing GuestIcuUtfMemory alias matrix still holds.
  • The copy-out writes only output.size() (never more than the capacity), plus a terminator only when it fits. The copy-in for -1 stops at the page that holds the NUL. No files overlap with #25528 or #25502.

Findings

  1. The negative control posted on the PR shows guest_icu_utf_conversions Passed with UtfConversions.cpp reverted, and the PR body says the same ("exit 0 against the pre-change object"). Nothing pins the change, which is that an unreadable source throws instead of faulting. Please add a GuestIcuUtfMemory case with:
    • a counted source that runs into a PROT_NONE page, expecting a throw
    • a -1 source whose NUL sits on the readable page, expecting success
  2. :204-205 and :254-255: the substitution count is zeroed before ReadSource. An unreadable source therefore throws after an output has already changed, and when the count aliases status an incoming -124 is cleared. ICU stores in the same order, so the code is fine, but the fragment's claim that it throws "before any output changes" is not accurate.
  3. Every conversion now allocates and copies the source and the output. Resource-string conversion may be hot; speed was not measured.

Suggested action: add the test (1) and fix the fragment wording (2); then merge

AI-assisted review (Claude Code) · head a9e370a789 · base main 23b8772462

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6013385230 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25526#pullrequestreview-6013385230). Originally posted by [throny](https://github.com/throny) on 2026-11-22T16:57:00Z. **Review:** COMMENTED --- ### Review: ⚠️ the conversions match ICU 78.1, but the new tests also pass on `main` **Basis:** code read against `3rdparty/icu` `049e0d6a42` (`ustrtrns.cpp`) · head `a9e370a789` **Checked** - An incoming error status returns early (`UtfConversions.cpp:195`, `:245`), and an incoming warning carries on. Argument validation, `pDestLength` on overflow, `U_STRING_NOT_TERMINATED_WARNING`/`U_BUFFER_OVERFLOW_ERROR` and the invalid-character partial output all follow ICU's slow paths. The existing `GuestIcuUtfMemory` alias matrix still holds. - The copy-out writes only `output.size()` (never more than the capacity), plus a terminator only when it fits. The copy-in for -1 stops at the page that holds the NUL. No files overlap with #25528 or #25502. **Findings** 1. The negative control posted on the PR shows `guest_icu_utf_conversions` **Passed** with `UtfConversions.cpp` reverted, and the PR body says the same ("exit 0 against the pre-change object"). Nothing pins the change, which is that an unreadable source throws instead of faulting. Please add a `GuestIcuUtfMemory` case with: - a counted source that runs into a `PROT_NONE` page, expecting a throw - a -1 source whose NUL sits on the readable page, expecting success 2. `:204-205` and `:254-255`: the substitution count is zeroed before `ReadSource`. An unreadable source therefore throws after an output has already changed, and when the count aliases `status` an incoming -124 is cleared. ICU stores in the same order, so the code is fine, but the fragment's claim that it throws "before any output changes" is not accurate. 3. Every conversion now allocates and copies the source and the output. Resource-string conversion may be hot; speed was not measured. **Suggested action:** add the test (1) and fix the fragment wording (2); then merge <sub>AI-assisted review (Claude Code) · head `a9e370a789` · base `main` `23b8772462`</sub>
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!25526
No description provided.