fix(libc): size CopyGuestString's buffer by the bytes it reads #20678
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/20678"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-15T12:16:41Z.
What
CopyGuestString_nid_no_patchresized its buffer to the wholemaximumbefore 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 / 2works, reading only up to the terminator the waystrlenwould.There were no direct tests, so this adds
GuestCopyGuestString.cpp, registered through atests/cmakefragment gated onBUILD_TESTINGthe waylibc_runtime_assertion_tests.cmakeis.Tested
11-21,
1ed2a4caa8(rebased onmain19729fa854): the buffer resize and the final assign inCopyGuestString_nid_no_patchalso return 12 when they throwstd::length_error, not onlystd::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_stringpasses 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.
guest_copy_guest_string:0x10) sources return 14 and leave the output untouched;"guest"return 63, and caps of 6 and 16 copy it;""with cap 1 copies an empty string;SIZE_MAX / 2, both"guest"and the long string copy.main'sGeneral.cpp, every case before theSIZE_MAX / 2cap passes, and that cap fails (it returns 12).CopyGuestStringstill pass: the 109 matching path, kernel_sys, event-flag, equeue, file, socket and user-service tests (oneNot Runwas an unbuilt target, built and passed afterwards);guest_system_exit*,guest_np_manager,guest_resolverandguest_sce_v2_exceptions.git merge-treeagainst the open PRs that touchGeneral.cpp(#20315, #18112, #15973, #11446, #9371): no new conflicts.tools/check_conventions.pypasses on the branch and this body.a831dfc189merged intomain, 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 onmainthere.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 parent1ed2a4caa8merged ontomainfa86dfcd3b):guest_copy_guest_stringpasses in 3 of 3 runs,main'sGeneral.cppfails 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.c90b8d8aa6adds 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
mainadacf0bd83:guest_copy_guest_stringpasses 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_allocforced insidelibc.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 ontomain95702cd8d8:guest_copy_guest_stringpasses in 3 of 3 runs, and without the page alignment it fails at the first guard-page case.Run by @ligmaSec (review): Linux: the
GuestCopyGuestStringfixture passes, covering long strings, the bounded and unbounded copies, a terminator at the end of a readable page and unchanged output on failure.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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
a831dfc189659317cb556339c886523546203fe4using 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
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.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T09:52:32Z.
Review: APPROVED
Reviewed — looks good
The change at head
1ed2a4caa895c9b4267b59c0ae75e1ef47f60066reads 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
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.jsonwas not available; the included-file inventory was checked in the manifest and reader listing.To get this to a good state
Nothing is required.
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, thereforeoffset + 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
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
c90b8d8aa603b66958d61696a71fe0d140cfc063usingrepo_read.pyto inspect the prepared diff and source files. No tests or build commands were run.Results
Negative control
Not run.
Findings
Blocking
None.
Non-blocking
None.
Sources
Reviewed
General.cppat this head, the new test registration fragment, andGuestCopyGuestString.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_stringtest on the target platforms.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
GuestCopyGuestStringfixture 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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.