Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Bladureau:fix/libsceicu-string-hash-guest-copies
Nov 21, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Bladureau:fix/libsceicu-string-hash-guest-copies

Conversation

@Bladureau

Copy link
Copy Markdown
Contributor

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 (586b989). 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

ustr_hashUCharsN, ustr_hashCharsN and ustr_hashICharsN passed guest
pointers straight to ICU, so an unreadable string crashed the host. ICU
only samples every ((length - 32) / 32 + 1)-th unit, at most 63 of them,
so the wrappers now copy just those units through GuestCopyIn_nid_no_patch
and hash them with ICU, which samples every unit of an array that short:
the value is unchanged and no unit ICU skips is read. An unreadable
sampled unit throws std::invalid_argument; errno is preserved. Null
strings and nonpositive lengths still go to ICU, which reads nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 15:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 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 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

@ycbuzi

ycbuzi commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Tested 8450b87ac9 merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 3 tests failed out of 6503; 99% tests passed, 3 tests failed out of 6503. 3 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 6503; 100% tests passed, 0 tests failed out of 6503. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

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

@oneandonlydean
oneandonlydean merged commit ac0e9cb into boykopovar:main Nov 21, 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.

6 participants