fix(libc): safely probe the output pointers of modfl and remquol #20589

Merged
milescoviello merged 2 commits from upstream-pr/20589 into main 2026-11-15 11:57:28 +00:00
milescoviello commented 2026-11-15 10:36:41 +00:00 (Migrated from github.com)

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


What

modfl passed the guest pointer to the host std::modf, and remquol stored the quotient bits straight through it, so an output pointing at unmapped memory faulted the host.

Both now compute the value on the host and write it with GuestCopyOut_nid_no_patch. They have no error result, so an unwritable output is skipped and the return value is still correct, the way the other no-error-channel exports handle an unwritable output. The values, errno (EDOM for remquol) and the floating-point exceptions for valid pointers are unchanged.

The float and double versions (modf, modff, remquo, remquof, sincosf) keep the direct store. They run per element in game math loops, and the probe is a process_vm_writev per store on Linux: measured below, it took sincosf from about 4 ns to about 670 ns a call. The long double versions are rarely called.

The debt entry goes in a fragment under docs/dev/debt/changes/. It records this as a host-safety fallback (console behaviour on unmapped memory isn't measured) and why the float and double versions keep the direct store.

Tested

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

  • guest_math_exact: remquol(-7, 2) with an unmapped quotient (0x10) returns 1; modfl(2.5) stores 2 and returns 0.5, and returns 0.5 with an unmapped integral. The existing signalling-NaN, EDOM and quotient checks still pass. guest_math still passes.

  • Negative control: with MathExact.cpp from main and these tests, guest_math_exact segfaults.

  • Per-call cost, 1M calls with a valid pointer on one pinned core (taskset -c 4), three interleaved runs per side:

    main this PR
    sincosf 3.8-4.0 ns 3.8 ns (unchanged)
    modfl 10.1-10.2 ns 343-349 ns
    remquol 26.2-28.0 ns 372-375 ns

    The first version of this PR probed every output; there sincosf measured 665-676 ns (vs 3.7-3.8), modf / modff 332-340 ns (vs 1.3-1.4), and remquo / remquof 349-357 ns (vs 12.9-13.3).

  • git merge-tree against the open PRs that touch MathExact.cpp or GuestMathExact.cpp (#18524, #15143): no new conflicts.

  • tools/check_conventions.py passes on the branch and this body; tools/debt_fold.py --check passes with the fragment 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5321856986 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20589). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T10:36:41Z. --- ### What `modfl` passed the guest pointer to the host `std::modf`, and `remquol` stored the quotient bits straight through it, so an output pointing at unmapped memory faulted the host. Both now compute the value on the host and write it with `GuestCopyOut_nid_no_patch`. They have no error result, so an unwritable output is skipped and the return value is still correct, the way the other no-error-channel exports handle an unwritable output. The values, `errno` (`EDOM` for `remquol`) and the floating-point exceptions for valid pointers are unchanged. The float and double versions (`modf`, `modff`, `remquo`, `remquof`, `sincosf`) keep the direct store. They run per element in game math loops, and the probe is a `process_vm_writev` per store on Linux: measured below, it took `sincosf` from about 4 ns to about 670 ns a call. The long double versions are rarely called. The debt entry goes in a fragment under `docs/dev/debt/changes/`. It records this as a host-safety fallback (console behaviour on unmapped memory isn't measured) and why the float and double versions keep the direct store. ### Tested Linux (Ubuntu 26.04.1), GCC 15.2, Release. - `guest_math_exact`: `remquol(-7, 2)` with an unmapped quotient (`0x10`) returns 1; `modfl(2.5)` stores 2 and returns 0.5, and returns 0.5 with an unmapped integral. The existing signalling-NaN, `EDOM` and quotient checks still pass. `guest_math` still passes. - Negative control: with `MathExact.cpp` from `main` and these tests, `guest_math_exact` segfaults. - Per-call cost, 1M calls with a valid pointer on one pinned core (`taskset -c 4`), three interleaved runs per side: | | `main` | this PR | |---|---|---| | `sincosf` | 3.8-4.0 ns | 3.8 ns (unchanged) | | `modfl` | 10.1-10.2 ns | 343-349 ns | | `remquol` | 26.2-28.0 ns | 372-375 ns | The first version of this PR probed every output; there `sincosf` measured 665-676 ns (vs 3.7-3.8), `modf` / `modff` 332-340 ns (vs 1.3-1.4), and `remquo` / `remquof` 349-357 ns (vs 12.9-13.3). - `git merge-tree` against the open PRs that touch `MathExact.cpp` or `GuestMathExact.cpp` (#18524, #15143): no new conflicts. - `tools/check_conventions.py` passes on the branch and this body; `tools/debt_fold.py --check` passes with the fragment 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: 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!20589
No description provided.