fix(libSceNpUniversalDataSystem): copy record keys, string values and binary values through checked reads #20812

Closed
milescoviello wants to merge 2 commits from upstream-pr/20812 into main
milescoviello commented 2026-11-15 15:59:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-15T15:59:01Z.


What

The IntRecord* builders in Record.cpp read guest strings and bytes in place: object keys (key[0], then std::string_view(key) inside Set), the IntRecord*SetString values (std::string(value)), the IntCreateRecordData record type, and the IntRecord*SetBinary bytes (Uds::Binary(bytes, bytes + size)). An unmapped key, value or byte buffer faulted the host.

What changes

  • Keys, string values and the record type are copied with CopyGuestString_nid_no_patch before use.
    • The cap is SIZE_MAX, because the direct read had no length limit and this keeps that. That relies on #20678, which grows CopyGuestString's buffer one page at a time instead of allocating the cap up front, so this PR depends on it.
    • Each key is copied once and the parameter then points at the host copy (key = name.c_str(), the same pattern sigaltstack uses for stack). The Set calls are therefore unchanged.
  • Binary values are copied into the Uds::Binary vector with one GuestCopyIn_nid_no_patch.

Errors

  • An unreadable key returns 0x80553114, an unreadable record type 0x8055310D and an unreadable value 0x80553115. These are the codes a null one already returns, and nothing is stored.
  • A string too large for host memory returns 0x80553101, the file's out-of-memory code.
  • Valid calls behave as before, including empty-key rejection and very long keys and values.

Relation to #19606
#19606 (gnavarro24) covers this file's other guest pointers: the IntInitialize parameter, the record-handle outputs and the value_ptr outputs. This PR leaves all of those to it and touches none of the lines it changes.

The debt entry goes in a fragment under docs/dev/debt/changes/ and marks these codes as host-safety fallbacks. Console behaviour here is not measured.

Tested

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

  • guest_np_uds_records, new cases:
    • A 5000-byte key with a 10000-byte value is stored and read back intact, which spans several pages and shows no cap was added.
    • Unmapped (0x10) inputs return the codes above with errno left at 71, and the array and object sizes are unchanged afterwards:
      • a CreateRecordData record type;
      • array and object string values;
      • array and object binary bytes;
      • object keys for SetBool, SetObject and SetArray.
  • All six libSceNpUniversalDataSystem tests pass: np_uds_events, guest_np_trophy_unlocks, guest_np_trophy_data, guest_np_trophy_stats, guest_np_uds_records and guest_np_uds_queries.
  • Negative control: Record.cpp from main with these tests dies with SIGSEGV at address 0x10. The long-key case runs first and passes on main too.
  • Merges:
    • git merge-tree is clean against main, #19606 and #13606, the open PRs touching this library.
    • With #19606 merged in as well, all six tests and debt_fold.py --check pass.
  • tools/check_conventions.py passes on the branch and this body.
  • maskym's Windows run of c588fe39ba merged into main 56c87adbdc (Windows 11, i7-12700K, WinLibs GCC 15.2.0, Release): guest_copy_guest_string, guest_np_uds_records, pass 2/2, three runs.

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: #20678
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5324480702 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20812). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T15:59:01Z. --- ### What The `IntRecord*` builders in `Record.cpp` read guest strings and bytes in place: object keys (`key[0]`, then `std::string_view(key)` inside `Set`), the `IntRecord*SetString` values (`std::string(value)`), the `IntCreateRecordData` record type, and the `IntRecord*SetBinary` bytes (`Uds::Binary(bytes, bytes + size)`). An unmapped key, value or byte buffer faulted the host. **What changes** - Keys, string values and the record type are copied with `CopyGuestString_nid_no_patch` before use. - The cap is `SIZE_MAX`, because the direct read had no length limit and this keeps that. That relies on #20678, which grows `CopyGuestString`'s buffer one page at a time instead of allocating the cap up front, so this PR depends on it. - Each key is copied once and the parameter then points at the host copy (`key = name.c_str()`, the same pattern `sigaltstack` uses for `stack`). The `Set` calls are therefore unchanged. - Binary values are copied into the `Uds::Binary` vector with one `GuestCopyIn_nid_no_patch`. **Errors** - An unreadable key returns `0x80553114`, an unreadable record type `0x8055310D` and an unreadable value `0x80553115`. These are the codes a null one already returns, and nothing is stored. - A string too large for host memory returns `0x80553101`, the file's out-of-memory code. - Valid calls behave as before, including empty-key rejection and very long keys and values. **Relation to #19606** #19606 (gnavarro24) covers this file's other guest pointers: the `IntInitialize` parameter, the record-handle outputs and the `value_ptr` outputs. This PR leaves all of those to it and touches none of the lines it changes. The debt entry goes in a fragment under `docs/dev/debt/changes/` and marks these codes as host-safety fallbacks. Console behaviour here is not measured. ### Tested Linux (Ubuntu 26.04.1), GCC 15.2, Release. - `guest_np_uds_records`, new cases: - A 5000-byte key with a 10000-byte value is stored and read back intact, which spans several pages and shows no cap was added. - Unmapped (`0x10`) inputs return the codes above with `errno` left at 71, and the array and object sizes are unchanged afterwards: - a `CreateRecordData` record type; - array and object string values; - array and object binary bytes; - object keys for `SetBool`, `SetObject` and `SetArray`. - All six `libSceNpUniversalDataSystem` tests pass: `np_uds_events`, `guest_np_trophy_unlocks`, `guest_np_trophy_data`, `guest_np_trophy_stats`, `guest_np_uds_records` and `guest_np_uds_queries`. - Negative control: `Record.cpp` from `main` with these tests dies with SIGSEGV at address `0x10`. The long-key case runs first and passes on `main` too. - Merges: - `git merge-tree` is clean against `main`, #19606 and #13606, the open PRs touching this library. - With #19606 merged in as well, all six tests and `debt_fold.py --check` pass. - `tools/check_conventions.py` passes on the branch and this body. - maskym's [Windows run](https://github.com/boykopovar/AnyPS5/pull/20812#issuecomment-6975370596) of `c588fe39ba` merged into `main` `56c87adbdc` (Windows 11, i7-12700K, WinLibs GCC 15.2.0, Release): `guest_copy_guest_string`, `guest_np_uds_records`, pass 2/2, three runs. ### 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: #20678 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes

Pull request closed

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!20812
No description provided.