fix(libc): size CopyGuestString's buffer by the bytes it reads #20678

Open
milescoviello wants to merge 3 commits from upstream-pr/20678 into main
milescoviello commented 2026-11-15 12:16:41 +00:00 (Migrated from github.com)

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


What

CopyGuestString_nid_no_patch resized its buffer to the whole maximum before reading the first byte. So the cost of a call grew with the cap rather than the string. A cap that couldn't be allocated also returned 12 (ENOMEM) even when the string was a few bytes long, so a caller had to pick a cap it could afford to allocate on every call.

The buffer now grows one page chunk at a time, by exactly the bytes about to be copied. The loop, the page-sized reads, the terminator search, the copied text and the return codes for every cap that could be allocated before (0, 14 for unreadable memory, 63 for no terminator within the cap) are unchanged. A short string with a large cap now costs one small allocation, and a cap like SIZE_MAX / 2 works, reading only up to the terminator the way strlen would.

There were no direct tests, so this adds GuestCopyGuestString.cpp, registered through a tests/cmake fragment gated on BUILD_TESTING the way libc_runtime_assertion_tests.cmake is.

Tested

11-21, 1ed2a4caa8 (rebased on main 19729fa854): the buffer resize and the final assign in CopyGuestString_nid_no_patch also return 12 when they throw std::length_error, not only std::bad_alloc, so neither exception leaves a C API through callers that translate 12 (MarcZX10's point on #23418, which carries this helper). In practice it can't be thrown: the buffer grows a page at a time and the copy stops at the first unreadable page. Linux, GCC 15.2, Ninja Release: guest_copy_guest_string passes in each of 3 runs. Windows 11 VM (GeForce GT 1030, WinLibs GCC 16.2.0 POSIX UCRT, Ninja, Release): it passes in each of 3 runs.

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

  • New guest_copy_guest_string:
    • null and unmapped (0x10) sources return 14 and leave the output untouched;
    • caps of 0 and 5 for "guest" return 63, and caps of 6 and 16 copy it;
    • "" with cap 1 copies an empty string;
    • a 100,000-character string returns 63 with a cap equal to its length and copies with one more byte;
    • with a cap of SIZE_MAX / 2, both "guest" and the long string copy.
  • Negative control: with main's General.cpp, every case before the SIZE_MAX / 2 cap passes, and that cap fails (it returns 12).
  • Callers that go through CopyGuestString still pass: the 109 matching path, kernel_sys, event-flag, equeue, file, socket and user-service tests (one Not Run was an unbuilt target, built and passed afterwards); guest_system_exit*, guest_np_manager, guest_resolver and guest_sce_v2_exceptions.
  • git merge-tree against the open PRs that touch General.cpp (#20315, #18112, #15973, #11446, #9371): no new conflicts.
  • tools/check_conventions.py passes on the branch and this body.
  • ycbuzi's full-suite runs of a831dfc189 merged into main, WinLibs GCC 15.2.0 full build: Windows Server 2025, 6500 of 6500 tests pass in both runs; Windows Server 2022, 2 of 6500 fail in both runs, and the same 2 fail on main there.

11-24, c90b8d8aa6: guard-page cases from Helldiverr's run. Helldiverr's Windows run (Ryzen 9 9950X3D, Windows 11 Pro 26300, WinLibs GCC 15.2, this head's parent 1ed2a4caa8 merged onto main fa86dfcd3b): guest_copy_guest_string passes in 3 of 3 runs, main's General.cpp fails it, and 4 of 5 mutations are caught. The survivor: removing the page alignment still passed, because every test source was readable well past its terminator. c90b8d8aa6 adds cases with a string at the end of a readable page that has an inaccessible page after it: the unbounded and exact caps copy it, and the not-terminated text returns 63 at a cap that ends at the inaccessible page and 14 at one that reaches it.

  • Linux (Ubuntu 26.04.1, GCC 15.2, Release), the head merged onto main adacf0bd83: guest_copy_guest_string passes in 3 of 3 runs, and without the page alignment it aborts.

  • Windows 11 VM (WinLibs GCC 16.2.0, Release), the same merge: passes in 3 of 3 runs.

  • Not verified on Windows: in Helldiverr's run, a bad_alloc forced inside libc.prx (by the negative control and one mutation) stopped the process as an undeliverable exception, so the 12 returns are measured on Linux only.

  • Helldiverr's Windows re-check at c90b8d8aa6, merged onto main 95702cd8d8: guest_copy_guest_string passes in 3 of 3 runs, and without the page alignment it fails at the first guard-page case.

  • Run by @ligmaSec (review): Linux: the GuestCopyGuestString fixture passes, covering long strings, the bounded and unbounded copies, a terminator at the end of a readable page and unchanged output on failure.

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:5322604232 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20678). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T12:16:41Z. --- ### What `CopyGuestString_nid_no_patch` resized its buffer to the whole `maximum` before reading the first byte. So the cost of a call grew with the cap rather than the string. A cap that couldn't be allocated also returned 12 (`ENOMEM`) even when the string was a few bytes long, so a caller had to pick a cap it could afford to allocate on every call. The buffer now grows one page chunk at a time, by exactly the bytes about to be copied. The loop, the page-sized reads, the terminator search, the copied text and the return codes for every cap that could be allocated before (0, 14 for unreadable memory, 63 for no terminator within the cap) are unchanged. A short string with a large cap now costs one small allocation, and a cap like `SIZE_MAX / 2` works, reading only up to the terminator the way `strlen` would. There were no direct tests, so this adds `GuestCopyGuestString.cpp`, registered through a `tests/cmake` fragment gated on `BUILD_TESTING` the way `libc_runtime_assertion_tests.cmake` is. ### Tested **11-21, `1ed2a4caa8` (rebased on `main` `19729fa854`):** the buffer resize and the final assign in `CopyGuestString_nid_no_patch` also return 12 when they throw `std::length_error`, not only `std::bad_alloc`, so neither exception leaves a C API through callers that translate 12 (MarcZX10's [point on #23418](https://github.com/boykopovar/AnyPS5/pull/23418#pullrequestreview-6004331201), which carries this helper). In practice it can't be thrown: the buffer grows a page at a time and the copy stops at the first unreadable page. Linux, GCC 15.2, Ninja Release: `guest_copy_guest_string` passes in each of 3 runs. Windows 11 VM (GeForce GT 1030, WinLibs GCC 16.2.0 POSIX UCRT, Ninja, Release): it passes in each of 3 runs. Linux (Ubuntu 26.04.1), GCC 15.2, Release. - New `guest_copy_guest_string`: - null and unmapped (`0x10`) sources return 14 and leave the output untouched; - caps of 0 and 5 for `"guest"` return 63, and caps of 6 and 16 copy it; - `""` with cap 1 copies an empty string; - a 100,000-character string returns 63 with a cap equal to its length and copies with one more byte; - with a cap of `SIZE_MAX / 2`, both `"guest"` and the long string copy. - Negative control: with `main`'s `General.cpp`, every case before the `SIZE_MAX / 2` cap passes, and that cap fails (it returns 12). - Callers that go through `CopyGuestString` still pass: the 109 matching path, kernel_sys, event-flag, equeue, file, socket and user-service tests (one `Not Run` was an unbuilt target, built and passed afterwards); `guest_system_exit*`, `guest_np_manager`, `guest_resolver` and `guest_sce_v2_exceptions`. - `git merge-tree` against the open PRs that touch `General.cpp` (#20315, #18112, #15973, #11446, #9371): no new conflicts. - `tools/check_conventions.py` passes on the branch and this body. - ycbuzi's [full-suite runs](https://github.com/boykopovar/AnyPS5/pull/20678#issuecomment-7044618758) of `a831dfc189` merged into `main`, WinLibs GCC 15.2.0 full build: Windows Server 2025, 6500 of 6500 tests pass in both runs; Windows Server 2022, 2 of 6500 fail in both runs, and the same 2 fail on `main` there. **11-24, `c90b8d8aa6`: guard-page cases from Helldiverr's run.** Helldiverr's [Windows run](https://github.com/boykopovar/AnyPS5/pull/20678#issuecomment-7135315413) (Ryzen 9 9950X3D, Windows 11 Pro 26300, WinLibs GCC 15.2, this head's parent `1ed2a4caa8` merged onto `main` `fa86dfcd3b`): `guest_copy_guest_string` passes in 3 of 3 runs, `main`'s `General.cpp` fails it, and 4 of 5 mutations are caught. The survivor: removing the page alignment still passed, because every test source was readable well past its terminator. `c90b8d8aa6` adds cases with a string at the end of a readable page that has an inaccessible page after it: the unbounded and exact caps copy it, and the not-terminated text returns 63 at a cap that ends at the inaccessible page and 14 at one that reaches it. - Linux (Ubuntu 26.04.1, GCC 15.2, Release), the head merged onto `main` `adacf0bd83`: `guest_copy_guest_string` passes in 3 of 3 runs, and without the page alignment it aborts. - Windows 11 VM (WinLibs GCC 16.2.0, Release), the same merge: passes in 3 of 3 runs. - Not verified on Windows: in Helldiverr's run, a `bad_alloc` forced inside `libc.prx` (by the negative control and one mutation) stopped the process as an undeliverable exception, so the 12 returns are measured on Linux only. - Helldiverr's [Windows re-check](https://github.com/boykopovar/AnyPS5/pull/20678#issuecomment-7144620667) at `c90b8d8aa6`, merged onto `main` `95702cd8d8`: `guest_copy_guest_string` passes in 3 of 3 runs, and without the page alignment it fails at the first guard-page case. - Run by @ligmaSec ([review](https://github.com/boykopovar/AnyPS5/pull/20678#pullrequestreview-6168774197)): Linux: the `GuestCopyGuestString` fixture passes, covering long strings, the bounded and unbounded copies, a terminator at the end of a readable page and unchanged output on failure. ### 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:04:39 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Reviewed — no blocking findings

The reviewed change adds page-sized incremental reads and focused tests; no code defect was verified, but tests were not run in this review.

What I ran

Reviewed head a831dfc189659317cb556339c886523546203fe4 using the prepared diff and the pinned repository reader. No test or build command was run. The description reports Linux test runs, which I did not independently verify.

Results

Check Result Note
Diff and changed files passed All 3 changed files read
Build and tests not run Author reports successful runs
Changed file exclusions not applicable None listed

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build and test results, including the reported negative control, were not independently verified. All files listed as reviewed in the manifest were read; no changed files were excluded.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5910866091 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20678#pullrequestreview-5910866091). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-15T13:04:39Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The reviewed change adds page-sized incremental reads and focused tests; no code defect was verified, but tests were not run in this review. ## What I ran Reviewed head `a831dfc189659317cb556339c886523546203fe4` using the prepared diff and the pinned repository reader. No test or build command was run. The description reports Linux test runs, which I did not independently verify. ## Results | Check | Result | Note | |---|---|---| | Diff and changed files | passed | All 3 changed files read | | Build and tests | not run | Author reports successful runs | | Changed file exclusions | not applicable | None listed | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Build and test results, including the reported negative control, were not independently verified. All files listed as reviewed in the manifest were read; no changed files were excluded. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-21 09:52:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T09:52:32Z.

Review: APPROVED


Reviewed — looks good

The change at head 1ed2a4caa895c9b4267b59c0ae75e1ef47f60066 reads and allocates one page-bounded chunk at a time, preserving the existing return behavior in the inspected code.

What I ran

Inspected the prepared diff and all three changed files through repo_read.py; no build or tests were run. The reader was pinned to the stated head. No execution environment or toolchain was available for this review.

Results

Check Result Note
Changed code review passed All three changed files read
Build and tests not run Reader-only review
Negative control not run Requires test execution

Negative control

Not run; this review environment exposes the pinned reader but no test runner.

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

No build, tests, or runtime behavior were independently verified. The prepared review bundle listed all three changed files as included, and each was read. files.json was not available; the included-file inventory was checked in the manifest and reader listing.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6004480686 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20678#pullrequestreview-6004480686). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T09:52:32Z. **Review:** APPROVED --- # Reviewed — looks good The change at head `1ed2a4caa895c9b4267b59c0ae75e1ef47f60066` reads and allocates one page-bounded chunk at a time, preserving the existing return behavior in the inspected code. ## What I ran Inspected the prepared diff and all three changed files through `repo_read.py`; no build or tests were run. The reader was pinned to the stated head. No execution environment or toolchain was available for this review. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | All three changed files read | | Build and tests | not run | Reader-only review | | Negative control | not run | Requires test execution | ## Negative control Not run; this review environment exposes the pinned reader but no test runner. ## Findings ### Blocking None. ### Non-blocking None. ## Sources - [General.cpp at reviewed head](https://github.com/boykopovar/AnyPS5/blob/1ed2a4caa895c9b4267b59c0ae75e1ef47f60066/core/libs/prx/libc/src/General.cpp#L627-L657) - [GuestCopyGuestString.cpp at reviewed head](https://github.com/boykopovar/AnyPS5/blob/1ed2a4caa895c9b4267b59c0ae75e1ef47f60066/core/libs/tests/GuestCopyGuestString.cpp) - [guest_copy_guest_string.cmake at reviewed head](https://github.com/boykopovar/AnyPS5/blob/1ed2a4caa895c9b4267b59c0ae75e1ef47f60066/core/libs/prx/libc/tests/cmake/guest_copy_guest_string.cmake) ## Not verified No build, tests, or runtime behavior were independently verified. The prepared review bundle listed all three changed files as included, and each was read. `files.json` was not available; the included-file inventory was checked in the manifest and reader listing. ## To get this to a good state Nothing is required.
malekaifa commented 2026-11-25 00:49:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-11-25T00:49:52Z.

Review: APPROVED


Re-reviewed exact head c90b8d8aa603b66958d61696a71fe0d140cfc063.

The production implementation is unchanged from MarcZX10's approved 1ed2a4caa895c9b4267b59c0ae75e1ef47f60066; the only follow-up commit adds guard-page regression coverage. The incremental buffer growth remains overflow-safe (count <= maximum - offset, therefore offset + count <= maximum), preserves the destination until a terminator is found, and retains page-bounded guest reads. The new unreadable-page cases specifically catch the previously surviving mutation that dropped the source alignment term, while Linux and Windows runs pass on this exact head.

Current hosted Windows/register-context/relinker/ASan/write-watch/edge-watch/eligibility/portability/report/PR/conventions jobs are successful; a duplicate labels/conventions pair is newly queued, so this review does not depend on those duplicate queued instances.

No new blocker on the exact current head.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6049845668 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20678#pullrequestreview-6049845668). Originally posted by [malekaifa](https://github.com/malekaifa) on 2026-11-25T00:49:52Z. **Review:** APPROVED --- Re-reviewed exact head `c90b8d8aa603b66958d61696a71fe0d140cfc063`. The production implementation is unchanged from MarcZX10's approved `1ed2a4caa895c9b4267b59c0ae75e1ef47f60066`; the only follow-up commit adds guard-page regression coverage. The incremental buffer growth remains overflow-safe (`count <= maximum - offset`, therefore `offset + count <= maximum`), preserves the destination until a terminator is found, and retains page-bounded guest reads. The new unreadable-page cases specifically catch the previously surviving mutation that dropped the source alignment term, while Linux and Windows runs pass on this exact head. Current hosted Windows/register-context/relinker/ASan/write-watch/edge-watch/eligibility/portability/report/PR/conventions jobs are successful; a duplicate labels/conventions pair is newly queued, so this review does not depend on those duplicate queued instances. No new blocker on the exact current head. AI-assisted: yes
MarcZX10 commented 2026-11-25 01:45:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T01:45:20Z.

Review: COMMENTED


Reviewed — no blocking findings

The changed code and tests are readable and show no verified blocking defect, but I did not run the checks needed to justify approval.

What I ran

Reviewed head c90b8d8aa603b66958d61696a71fe0d140cfc063 using repo_read.py to inspect the prepared diff and source files. No tests or build commands were run.

Results

Check Result Note
Changed lines reviewed passed All three changed files read
Build and tests not run No execution performed

Negative control

Not run.

Findings

Blocking

None.

Non-blocking

None.

Sources

Reviewed General.cpp at this head, the new test registration fragment, and GuestCopyGuestString.cpp.

Not verified

Build and runtime behavior were not verified. The prepared bundle listed these three changed files and all were read; no files were excluded.

To get this to a good state

Nothing is required. Optional: run the new guest_copy_guest_string test on the target platforms.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6050342323 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20678#pullrequestreview-6050342323). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T01:45:20Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The changed code and tests are readable and show no verified blocking defect, but I did not run the checks needed to justify approval. ## What I ran Reviewed head `c90b8d8aa603b66958d61696a71fe0d140cfc063` using `repo_read.py` to inspect the prepared diff and source files. No tests or build commands were run. ## Results | Check | Result | Note | |---|---|---| | Changed lines reviewed | passed | All three changed files read | | Build and tests | not run | No execution performed | ## Negative control Not run. ## Findings ### Blocking None. ### Non-blocking None. ## Sources Reviewed [`General.cpp` at this head](https://github.com/boykopovar/AnyPS5/blob/c90b8d8aa603b66958d61696a71fe0d140cfc063/core/libs/prx/libc/src/General.cpp), the new test registration fragment, and `GuestCopyGuestString.cpp`. ## Not verified Build and runtime behavior were not verified. The prepared bundle listed these three changed files and all were read; no files were excluded. ## To get this to a good state Nothing is required. Optional: run the new `guest_copy_guest_string` test on the target platforms.
ligmaSec commented 2026-12-03 11:23:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:23:36Z.

Review: COMMENTED


Built the submitted guest string copy implementation and ran the exact GuestCopyGuestString fixture on Linux. It passed the long-string, bounded and unbounded behaviors, including a terminator at the end of a readable page and unchanged output on failure.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6168774197 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20678#pullrequestreview-6168774197). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T11:23:36Z. **Review:** COMMENTED --- Built the submitted guest string copy implementation and ran the exact `GuestCopyGuestString` fixture on Linux. It passed the long-string, bounded and unbounded behaviors, including a terminator at the end of a readable page and unchanged output on failure. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/20678:upstream-pr/20678'
git switch 'upstream-pr/20678'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/20678'
git switch 'upstream-pr/20678'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/20678'
git switch 'upstream-pr/20678'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/20678'
git switch 'main'
git merge --squash 'upstream-pr/20678'
git switch 'main'
git merge --ff-only 'upstream-pr/20678'
git switch 'main'
git merge 'upstream-pr/20678'
git push origin 'main'
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!20678
No description provided.