fix(libc): safely probe the state of wcstok_s #20588

Closed
milescoviello wants to merge 3 commits from upstream-pr/20588 into main
milescoviello commented 2026-11-15 10:34:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-15T10:34:23Z.


What

wcstok_s read *s1max and *context in its argument check, and stored the remaining count, the context and the terminator it writes after a token, all straight through the guest pointers. So an s1max or context pointing at unmapped memory faulted the host.

s1max and the context are now copied in with GuestCopyIn_nid_no_patch right after the null checks. The remaining count, the context and the terminator are written with GuestCopyOut_nid_no_patch, in the same order as before. An unreadable or unwritable pointer is reported as the "bad argument" runtime-constraint violation (EINVAL) that wcstok_s already raises for a null s1max, context or delimiters. The tokens, the remaining counts, the contexts and the missing-token / missing-delimiter violations for valid pointers are unchanged. The string and the delimiters are still read in place.

Stacked on #20587 (same file, after #20525 and #20526). The debt entry goes in a fragment under docs/dev/debt/changes/ and records this as a host-safety fallback; console behaviour there is not measured.

Tested

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

  • guest_bounds_checking (CheckWcstok): an unreadable s1max (0x10) and an unreadable context (0x10) each return null and raise one "wcstok_s: bad argument" violation with error 22. The existing token, remaining-count, end-of-string, s1max-limit, zero-s1max and null-context cases still pass.
  • Negative control: with #20587's BoundsChecking.cpp and these tests, guest_bounds_checking segfaults.
  • tools/check_conventions.py passes on the branch and this body; tools/debt_fold.py --check passes with this fragment and those of #20525, #20526 and #20587 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: #20587
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5321841567 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20588). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T10:34:23Z. --- ### What `wcstok_s` read `*s1max` and `*context` in its argument check, and stored the remaining count, the context and the terminator it writes after a token, all straight through the guest pointers. So an `s1max` or context pointing at unmapped memory faulted the host. `s1max` and the context are now copied in with `GuestCopyIn_nid_no_patch` right after the null checks. The remaining count, the context and the terminator are written with `GuestCopyOut_nid_no_patch`, in the same order as before. An unreadable or unwritable pointer is reported as the "bad argument" runtime-constraint violation (EINVAL) that `wcstok_s` already raises for a null `s1max`, context or delimiters. The tokens, the remaining counts, the contexts and the missing-token / missing-delimiter violations for valid pointers are unchanged. The string and the delimiters are still read in place. Stacked on #20587 (same file, after #20525 and #20526). The debt entry goes in a fragment under `docs/dev/debt/changes/` and records this as a host-safety fallback; console behaviour there is not measured. ### Tested Linux (Ubuntu 26.04.1), GCC 15.2, Release. - `guest_bounds_checking` (`CheckWcstok`): an unreadable `s1max` (`0x10`) and an unreadable context (`0x10`) each return null and raise one "wcstok_s: bad argument" violation with error 22. The existing token, remaining-count, end-of-string, s1max-limit, zero-s1max and null-context cases still pass. - Negative control: with #20587's `BoundsChecking.cpp` and these tests, `guest_bounds_checking` segfaults. - `tools/check_conventions.py` passes on the branch and this body; `tools/debt_fold.py --check` passes with this fragment and those of #20525, #20526 and #20587 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: #20587 - [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!20588
No description provided.