fix(libSceJson2): safely probe the text of the Value text constructor and set #20682

Merged
milescoviello merged 1 commit from upstream-pr/20682 into main 2026-11-15 13:17:33 +00:00
milescoviello commented 2026-11-15 12:21:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-15T12:21:46Z.


What

sce::Json::Value(const char*) and Value::set(const char*) wrapped the guest text in a std::string_view, which ran strlen over it in place, so non-null text pointing at unmapped memory faulted the host.

Both now measure the text first with a small GuestTextLength helper. It copies one page-aligned chunk at a time into a 4 KiB stack buffer with GuestCopyIn_nid_no_patch and stops at the terminator. Then they build the string from the guest text with that length. The text is not copied to the host heap: guest_json_float_failure requires recovery values to use only the guest allocator, so the existing guest-allocated ScopedString still does the copy. Strings of any length still work, since the helper walks page by page rather than to a fixed cap.

Unreadable text is handled the way each call already handles null text: the constructor makes an empty string, and set throws std::invalid_argument ("unreadable text"). The values, the allocator-failure behaviour that guest_json_copy_failure pins, and every result for readable text are unchanged.

There were no tests for these two calls on plain text, so this adds GuestJsonValueText.cpp. The debt entry goes in a fragment under docs/dev/debt/changes/ and records this as unmeasured.

Tested

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

  • New guest_json_value_text:
    • Value("text") serializes as "text";
    • null text and unmapped text (0x10) both serialize as "";
    • a 100,000-character text is kept whole;
    • set("updated") replaces it, and set(0x10) throws std::invalid_argument and keeps "updated".
  • All 206 guest_json* tests pass, including guest_json_copy_failure (allocation-budget sweep) and guest_json_float_failure (guest-allocator-only recovery values).
  • Negative control: with main's Export.cpp and this test, guest_json_value_text segfaults.
  • git merge-tree against #16207 (the other open PR on libSceJson2/Export.cpp): no conflicts.
  • tools/check_conventions.py passes on the branch and this body; tools/debt_fold.py --check passes with the fragment applied to main.

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:5322647251 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20682). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T12:21:46Z. --- ### What `sce::Json::Value(const char*)` and `Value::set(const char*)` wrapped the guest text in a `std::string_view`, which ran `strlen` over it in place, so non-null text pointing at unmapped memory faulted the host. Both now measure the text first with a small `GuestTextLength` helper. It copies one page-aligned chunk at a time into a 4 KiB stack buffer with `GuestCopyIn_nid_no_patch` and stops at the terminator. Then they build the string from the guest text with that length. The text is not copied to the host heap: `guest_json_float_failure` requires recovery values to use only the guest allocator, so the existing guest-allocated `ScopedString` still does the copy. Strings of any length still work, since the helper walks page by page rather than to a fixed cap. Unreadable text is handled the way each call already handles null text: the constructor makes an empty string, and `set` throws `std::invalid_argument` ("unreadable text"). The values, the allocator-failure behaviour that `guest_json_copy_failure` pins, and every result for readable text are unchanged. There were no tests for these two calls on plain text, so this adds `GuestJsonValueText.cpp`. The debt entry goes in a fragment under `docs/dev/debt/changes/` and records this as unmeasured. ### Tested Linux (Ubuntu 26.04.1), GCC 15.2, Release. - New `guest_json_value_text`: - `Value("text")` serializes as `"text"`; - null text and unmapped text (`0x10`) both serialize as `""`; - a 100,000-character text is kept whole; - `set("updated")` replaces it, and `set(0x10)` throws `std::invalid_argument` and keeps `"updated"`. - All 206 `guest_json*` tests pass, including `guest_json_copy_failure` (allocation-budget sweep) and `guest_json_float_failure` (guest-allocator-only recovery values). - Negative control: with `main`'s `Export.cpp` and this test, `guest_json_value_text` segfaults. - `git merge-tree` against #16207 (the other open PR on `libSceJson2/Export.cpp`): no conflicts. - `tools/check_conventions.py` passes on the branch and this body; `tools/debt_fold.py --check` passes with the fragment applied to `main`. ### 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
MarcZX10 commented 2026-11-15 13:00:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-15T13:00:03Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected implementation and tests support merging.

What I ran

Reviewed head dd545ae147a8f541d7d3dcadd3ffd1fba10c6b60. I read all four changed files listed in review/manifest.json, along with the prepared description, commits, and diff. I did not run commands or tests.

Results

Check Result Note
Diff and changed files passed All four changed files read
Tests not run Reported results are author supplied

Findings

Blocking

None.

Non-blocking

None.

Not verified

The reported Linux test runs and negative control were not independently verified. review/files.json was unavailable through the reader; the changed-file list and inclusion status were confirmed with the review directory listing. No files were excluded per the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5910850236 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20682#pullrequestreview-5910850236). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-15T13:00:03Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected implementation and tests support merging. ## What I ran Reviewed head `dd545ae147a8f541d7d3dcadd3ffd1fba10c6b60`. I read all four changed files listed in `review/manifest.json`, along with the prepared description, commits, and diff. I did not run commands or tests. ## Results | Check | Result | Note | |---|---|---| | Diff and changed files | passed | All four changed files read | | Tests | not run | Reported results are author supplied | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The reported Linux test runs and negative control were not independently verified. `review/files.json` was unavailable through the reader; the changed-file list and inclusion status were confirmed with the review directory listing. No files were excluded per the manifest. ## 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!20682
No description provided.