fix(libSceIcu): read hashed strings through guest copies #24383

Merged
Bladureau merged 1 commit from upstream-pr/24383 into main 2026-11-21 13:10:29 +00:00
Bladureau commented 2026-11-20 15:32:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bladureau on 2026-11-20T15:32:57Z.


What

ustr_hashUCharsN, ustr_hashCharsN and ustr_hashICharsN passed guest pointers straight to bundled ICU 78.1, so an unreadable string crashed the host instead of throwing.

ICU's STRING_HASH (ustring.cpp, unchanged from 68.2) does not read the whole string: it samples every inc = (length - 32) / 32 + 1-th unit, which is at most 63 units for any length (with length = 32 + 32q + r, 0 <= r < 32, that is 32 + ceil(r / (q + 1))). Copying the whole range would read pages ICU never touches, so the wrappers copy only the sampled units through GuestCopyIn_nid_no_patch and hash that array with the ICU function. For fewer than 64 units ICU's step is 1, so it hashes every copied unit in order and the value is unchanged.

An unreadable sampled unit throws std::invalid_argument, and errno is preserved, as in uprv_strdup (586b9890a3). Null strings and nonpositive lengths still go to ICU, which reads nothing and returns 0.

The existing ustr_hash* checks in tests/Availability.cpp are kept; the new guest_icu_string_hash test covers these exports on their own.

Tested

Linux only (Ubuntu 24.04 container, GCC 13.3, -Wall -Wextra clean): a minimal harness that builds the bundled ICU common sources with the project's definitions, StringHash.cpp and tests/StringHash.cpp, with GuestCopyIn_nid_no_patch backed by process_vm_readv on the own process. No full build and no Windows run, so the VirtualProtect branch of the test is untested.

  • The test passes; against StringHash.cpp from main it crashes with SIGSEGV.
  • New cases: known values; all three exports match a transcription of STRING_HASH at lengths around every step change (31 to 97, 4095 to 4097, 131103/131104, up to 300000); null strings hash to 0 and nonpositive lengths read nothing; unreadable pointers throw and keep errno; a 300000-unit string whose unsampled tail lies in a PROT_NONE page hashes as in ICU, and shifting it by one unit so the last sample lands in that page throws.
  • Four deliberate mutations are all caught: copying every unit, a wrong step, dropping the last sample, and delegating to ICU after a failed copy.
  • python tools/check_conventions.py --base upstream/main and python tools/debt_fold.py --check pass.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5415928710 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24383). Originally posted by [Bladureau](https://github.com/Bladureau) on 2026-11-20T15:32:57Z. --- ### What `ustr_hashUCharsN`, `ustr_hashCharsN` and `ustr_hashICharsN` passed guest pointers straight to bundled ICU 78.1, so an unreadable string crashed the host instead of throwing. ICU's `STRING_HASH` ([ustring.cpp](https://github.com/unicode-org/icu/blob/049e0d6a420629ac7db77256987d083a563287b5/icu4c/source/common/ustring.cpp), unchanged from 68.2) does not read the whole string: it samples every `inc = (length - 32) / 32 + 1`-th unit, which is at most 63 units for any length (with `length = 32 + 32q + r`, `0 <= r < 32`, that is `32 + ceil(r / (q + 1))`). Copying the whole range would read pages ICU never touches, so the wrappers copy only the sampled units through `GuestCopyIn_nid_no_patch` and hash that array with the ICU function. For fewer than 64 units ICU's step is 1, so it hashes every copied unit in order and the value is unchanged. An unreadable sampled unit throws `std::invalid_argument`, and `errno` is preserved, as in `uprv_strdup` (586b9890a3). Null strings and nonpositive lengths still go to ICU, which reads nothing and returns 0. The existing `ustr_hash*` checks in `tests/Availability.cpp` are kept; the new `guest_icu_string_hash` test covers these exports on their own. ### Tested Linux only (Ubuntu 24.04 container, GCC 13.3, `-Wall -Wextra` clean): a minimal harness that builds the bundled ICU `common` sources with the project's definitions, `StringHash.cpp` and `tests/StringHash.cpp`, with `GuestCopyIn_nid_no_patch` backed by `process_vm_readv` on the own process. No full build and no Windows run, so the `VirtualProtect` branch of the test is untested. - The test passes; against `StringHash.cpp` from `main` it crashes with SIGSEGV. - New cases: known values; all three exports match a transcription of `STRING_HASH` at lengths around every step change (31 to 97, 4095 to 4097, 131103/131104, up to 300000); null strings hash to 0 and nonpositive lengths read nothing; unreadable pointers throw and keep `errno`; a 300000-unit string whose unsampled tail lies in a `PROT_NONE` page hashes as in ICU, and shifting it by one unit so the last sample lands in that page throws. - Four deliberate mutations are all caught: copying every unit, a wrong step, dropping the last sample, and delegating to ICU after a failed copy. - `python tools/check_conventions.py --base upstream/main` and `python tools/debt_fold.py --check` pass. ### 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) 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
copilot-pull-request-reviewer[bot] commented 2026-11-20 15:33:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-20T15:33:09Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5995189824 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24383#pullrequestreview-5995189824). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-20T15:33:09Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
PrivacyOnion commented 2026-11-20 16:01:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T16:01:21Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_icu_string_hash_tests built cleanly.
  • CTest (guest_icu_string_hash):
    1/1 Test #4050: guest_icu_string_hash ............   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceIcu/StringHash.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4050: guest_icu_string_hash ............***Exception: SegFault  0.41 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified HashSamples in core/libs/prx/libSceIcu/StringHash.cpp against ICU STRING_HASH algorithm: for length <= 0 or nullptr, correctly delegates to ICU functions which return 0.
    • Checked bounds and sample count: length > 0 with step = (length - 32) / 32 + 1 produces at most 63 samples, preventing buffer overflow on samples[MaximumSamples].
    • Verified GuestCopyIn_nid_no_patch memory safety, error handling with std::invalid_argument, and preservation of errno across copy checks.
    • Verified LP64 vs LLP64 portability and 64-bit step/offset arithmetic in core/libs/prx/libSceIcu/StringHash.cpp and tests/StringHash.cpp.
    • Checked AGENTS.md compliance: no comments in source code, technical debt fragment properly added to docs/dev/debt/changes/, and CMakeLists.txt properly wired with test target and Windows unwind configuration.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5995580501 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24383#pullrequestreview-5995580501). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T16:01:21Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `8450b87ac9`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_icu_string_hash_tests` built cleanly. - **CTest** (`guest_icu_string_hash`): ``` 1/1 Test #4050: guest_icu_string_hash ............ Passed 0.02 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceIcu/StringHash.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4050: guest_icu_string_hash ............***Exception: SegFault 0.41 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified HashSamples in core/libs/prx/libSceIcu/StringHash.cpp against ICU STRING_HASH algorithm: for length <= 0 or nullptr, correctly delegates to ICU functions which return 0. - Checked bounds and sample count: length > 0 with step = (length - 32) / 32 + 1 produces at most 63 samples, preventing buffer overflow on samples[MaximumSamples]. - Verified GuestCopyIn_nid_no_patch memory safety, error handling with std::invalid_argument, and preservation of errno across copy checks. - Verified LP64 vs LLP64 portability and 64-bit step/offset arithmetic in core/libs/prx/libSceIcu/StringHash.cpp and tests/StringHash.cpp. - Checked AGENTS.md compliance: no comments in source code, technical debt fragment properly added to docs/dev/debt/changes/, and CMakeLists.txt properly wired with test target and Windows unwind configuration. Looks Good To Me
MarcZX10 commented 2026-11-21 03:41:26 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the code read supports the reported sampled-copy behavior, and the author reports Linux harness coverage while Windows and the full build remain unverified.

What I ran

Reviewed head 8450b87ac9da4bec8da3f3cacc70311965b04f75. I read description.md, commits.md, manifest.json, diff.patch, and all four changed files listed as reviewed. I did not run build or tests; the Linux harness and convention checks are author-reported.

Results

Check Result Note
Diff and changed files passed All four reviewed files read
Build and tests not run Linux harness results are author-reported
Windows behavior not run VirtualProtect test branch unverified

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests or build were run during this review. Windows behavior and the full project build were not verified. No changed files were excluded; files.json was not available in the review bundle.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002622568 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24383#pullrequestreview-6002622568). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T03:41:26Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the code read supports the reported sampled-copy behavior, and the author reports Linux harness coverage while Windows and the full build remain unverified. ## What I ran Reviewed head `8450b87ac9da4bec8da3f3cacc70311965b04f75`. I read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, and all four changed files listed as reviewed. I did not run build or tests; the Linux harness and convention checks are author-reported. ## Results | Check | Result | Note | |---|---|---| | Diff and changed files | passed | All four reviewed files read | | Build and tests | not run | Linux harness results are author-reported | | Windows behavior | not run | `VirtualProtect` test branch unverified | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests or build were run during this review. Windows behavior and the full project build were not verified. No changed files were excluded; `files.json` was not available in the review bundle. ## To get this to a good state Nothing is required.
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!24383
No description provided.