fix(libSceIcu): copy uloc_* guest status pointers safely #21177

Merged
benjaminmathias merged 1 commit from upstream-pr/21177 into main 2026-11-16 01:53:08 +00:00
benjaminmathias commented 2026-11-15 22:36:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by benjaminmathias on 2026-11-15T22:36:32Z.


What

uloc_getVariant and uloc_setKeywordValue in core/libs/prx/libSceIcu/LocaleSyntax.cpp read the guest UErrorCode* status directly and passed it to ICU, which wrote it in place. Both now read the status once with GuestCopyIn_nid_no_patch, run ICU on a host copy, and write it back with GuestCopyOut_nid_no_patch only when ICU changed it, so a successful call never writes an unchanged status. An unreadable or unwritable status throws the std::invalid_argument that a null status already throws, with errno preserved; it does not call NotImplemented_nid_no_patch, so the functions are not counted as stubs. The console's behaviour on unmapped memory is unmeasured, which the debt fragment docs/dev/debt/changes/fix-icu-uloc-status-guest-copy.md records. The locale, keyword and value strings and the result buffer are still accessed as before. The change adds at most two copies per call.

Tested

Windows 11 Pro, WinLibs GCC 15.2:

  • Pre-fix, with the new test and the old source: ctest --test-dir build -R "^guest_icu_locale_syntax$" fails with Exception: SegFault.
  • Post-fix: ctest --test-dir build -R "^guest_icu_locale_syntax$" --repeat until-fail:5 passes 5/5.
  • The new CheckGuestStatus block covers an unmapped status for both functions (buffer untouched), a read-only status on a successful call (no write needed, result correct), and a read-only status on an error, which throws.
  • ctest -R "libressl|mspace|locale" (175 tests) passes.
  • tools/check_conventions.py --base origin/main passes.
  • Not tested on Linux or macOS. LocaleSyntax.cpp and the test are built outside any if(WIN32) block; the test uses mmap/mprotect there.

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:5327955268 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21177). Originally posted by [benjaminmathias](https://github.com/benjaminmathias) on 2026-11-15T22:36:32Z. --- ### What `uloc_getVariant` and `uloc_setKeywordValue` in `core/libs/prx/libSceIcu/LocaleSyntax.cpp` read the guest `UErrorCode* status` directly and passed it to ICU, which wrote it in place. Both now read the status once with `GuestCopyIn_nid_no_patch`, run ICU on a host copy, and write it back with `GuestCopyOut_nid_no_patch` only when ICU changed it, so a successful call never writes an unchanged status. An unreadable or unwritable status throws the `std::invalid_argument` that a null status already throws, with `errno` preserved; it does not call `NotImplemented_nid_no_patch`, so the functions are not counted as stubs. The console's behaviour on unmapped memory is unmeasured, which the debt fragment `docs/dev/debt/changes/fix-icu-uloc-status-guest-copy.md` records. The locale, keyword and value strings and the result buffer are still accessed as before. The change adds at most two copies per call. ### Tested Windows 11 Pro, WinLibs GCC 15.2: - Pre-fix, with the new test and the old source: `ctest --test-dir build -R "^guest_icu_locale_syntax$"` fails with `Exception: SegFault`. - Post-fix: `ctest --test-dir build -R "^guest_icu_locale_syntax$" --repeat until-fail:5` passes 5/5. - The new `CheckGuestStatus` block covers an unmapped status for both functions (buffer untouched), a read-only status on a successful call (no write needed, result correct), and a read-only status on an error, which throws. - `ctest -R "libressl|mspace|locale"` (175 tests) passes. - `tools/check_conventions.py --base origin/main` passes. - Not tested on Linux or macOS. `LocaleSyntax.cpp` and the test are built outside any `if(WIN32)` block; the test uses `mmap`/`mprotect` there. ### 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
PrivacyOnion commented 2026-11-16 00:11:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T00:11:45Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_icu_locale_syntax_tests built cleanly.
  • CTest (guest_icu_locale_syntax):
    1/1 Test #3605: guest_icu_locale_syntax ..........   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceIcu/LocaleSyntax.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #3605: guest_icu_locale_syntax ..........***Exception: SegFault  2.96 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified ReadStatus and WriteStatus in core/libs/prx/libSceIcu/LocaleSyntax.cpp check null pointers, copy guest status via GuestCopyIn_nid_no_patch/GuestCopyOut_nid_no_patch, preserve errno across probe calls, and skip writing back when outgoing == incoming.
    • Verified uloc_getVariant_nid_postfix and uloc_setKeywordValue_nid_postfix in core/libs/prx/libSceIcu/LocaleSyntax.cpp preserve existing argument validation order (status check, positive status early return, locale/buffer checks) and pass a local UErrorCode to ICU.
    • Verified docs/dev/debt/changes/fix-icu-uloc-status-guest-copy.md documents the guest status copy behavior and remaining in-place string/buffer accesses under libSceIcu.
    • Verified CheckGuestStatus in core/libs/tests/GuestIcuLocaleSyntax.cpp covers unmapped and read-only status pointers for both uloc_getVariant_nid_postfix and uloc_setKeywordValue_nid_postfix, confirming errno preservation, buffer preservation on failure, and proper VirtualFree/munmap cleanup.
    • Verified cross-platform LP64/LLP64 type safety and strict compliance with AGENTS.md rules (no comments, no stub regressions, conventions passed).

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5913681466 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21177#pullrequestreview-5913681466). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T00:11:45Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `0560902988`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_icu_locale_syntax_tests` built cleanly. - **CTest** (`guest_icu_locale_syntax`): ``` 1/1 Test #3605: guest_icu_locale_syntax .......... Passed 0.04 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceIcu/LocaleSyntax.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #3605: guest_icu_locale_syntax ..........***Exception: SegFault 2.96 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified ReadStatus and WriteStatus in core/libs/prx/libSceIcu/LocaleSyntax.cpp check null pointers, copy guest status via GuestCopyIn_nid_no_patch/GuestCopyOut_nid_no_patch, preserve errno across probe calls, and skip writing back when outgoing == incoming. - Verified uloc_getVariant_nid_postfix and uloc_setKeywordValue_nid_postfix in core/libs/prx/libSceIcu/LocaleSyntax.cpp preserve existing argument validation order (status check, positive status early return, locale/buffer checks) and pass a local UErrorCode to ICU. - Verified docs/dev/debt/changes/fix-icu-uloc-status-guest-copy.md documents the guest status copy behavior and remaining in-place string/buffer accesses under libSceIcu. - Verified CheckGuestStatus in core/libs/tests/GuestIcuLocaleSyntax.cpp covers unmapped and read-only status pointers for both uloc_getVariant_nid_postfix and uloc_setKeywordValue_nid_postfix, confirming errno preservation, buffer preservation on failure, and proper VirtualFree/munmap cleanup. - Verified cross-platform LP64/LLP64 type safety and strict compliance with AGENTS.md rules (no comments, no stub regressions, conventions passed). Looks Good To Me
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!21177
No description provided.