fix(libc): reject reverse copy lengths that are not a whole number of elements and oversized wmemcpy counts #16308

Merged
MarcZX10 merged 5 commits from upstream-pr/16308 into main 2026-11-19 08:04:11 +00:00
MarcZX10 commented 2026-11-06 06:47:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-06T06:47:48Z.


What

  • fix(libc) (f269df38b3): __std_reverse_copy_trivially_copyable_{2,4,8} (through ReverseCopy<Size>) walked the source down by the element size without checking the range. A length that is not a multiple of the element size, or an inverted range, made the loop step past the begin pointer and read and write outside the source. The export now rejects such ranges with NotImplemented_nid_no_patch, like __std_min_4u and __std_max_4u.
  • test(libc) (81b1022e16): core/libs/tests/GuestVectorAlgorithms.cpp checks that the 2, 4 and 8 byte exports throw for misaligned and inverted ranges and leave the destination untouched, and that whole-element and empty ranges still reverse.
  • fix(libc) (c38d2e3aea): wmemcpy and wmemmove multiplied the guest count by sizeof(char16_t) without a bound. For counts above SIZE_MAX / 2 the product wrapped, so only a few bytes were copied and the destination was returned as if the whole count had been copied. Such counts now reject with NotImplemented_nid_no_patch.
  • test(libc) (8ded3d3838): core/libs/tests/GuestWideStrings.cpp checks that both functions throw for a wrapping count and leave the destination unchanged.

Tested

Linux, throwaway archive build of this head (8ded3d3838): BUILD-EXIT 0, ctest guest_vector_algorithms and guest_wide_strings (targets guest_vector_algorithms_tests and guest_wide_string_tests) passed, 2 of 2. python3 tools/check_conventions.py --base upstream/main and git diff --check are clean. The new cases were not run against the unfixed code; that they fail without the fixes is reasoned from the code. Not run on Windows or macOS.

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
  • 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:5184089097 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16308). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-06T06:47:48Z. --- ### What - `fix(libc)` (f269df38b3): `__std_reverse_copy_trivially_copyable_{2,4,8}` (through `ReverseCopy<Size>`) walked the source down by the element size without checking the range. A length that is not a multiple of the element size, or an inverted range, made the loop step past the begin pointer and read and write outside the source. The export now rejects such ranges with `NotImplemented_nid_no_patch`, like `__std_min_4u` and `__std_max_4u`. - `test(libc)` (81b1022e16): `core/libs/tests/GuestVectorAlgorithms.cpp` checks that the 2, 4 and 8 byte exports throw for misaligned and inverted ranges and leave the destination untouched, and that whole-element and empty ranges still reverse. - `fix(libc)` (c38d2e3aea): `wmemcpy` and `wmemmove` multiplied the guest count by `sizeof(char16_t)` without a bound. For counts above `SIZE_MAX / 2` the product wrapped, so only a few bytes were copied and the destination was returned as if the whole count had been copied. Such counts now reject with `NotImplemented_nid_no_patch`. - `test(libc)` (8ded3d3838): `core/libs/tests/GuestWideStrings.cpp` checks that both functions throw for a wrapping count and leave the destination unchanged. ### Tested Linux, throwaway archive build of this head (`8ded3d3838`): `BUILD-EXIT 0`, ctest `guest_vector_algorithms` and `guest_wide_strings` (targets `guest_vector_algorithms_tests` and `guest_wide_string_tests`) passed, 2 of 2. `python3 tools/check_conventions.py --base upstream/main` and `git diff --check` are clean. The new cases were not run against the unfixed code; that they fail without the fixes is reasoned from the code. Not run on Windows or macOS. ### 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) - [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
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!16308
No description provided.