fix(libc): throw for __std_min/max ranges that fault on a device #12605

Merged
KernelPanicNT merged 6 commits from upstream-pr/12605 into main 2026-11-08 06:42:34 +00:00
KernelPanicNT commented 2026-10-28 22:33:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by KernelPanicNT on 2026-10-28T22:33:53Z.


What

__std_min_{4,8}{i,u} and __std_max_{4,8}{i,u} now throw std::invalid_argument for null, empty, reversed and partial-element ranges instead of calling NotImplemented_nid_no_patch. On a device the same NIDs faulted for every such input the probe tried (table below); other null, reversed or partial ranges are assumed to fault the same way, so there is no return value to reproduce. Throwing where the console faults follows the feholdexcept and feupdateenv entries in the libc debt. Nonempty ranges are unchanged.

Source of the behaviour

Measured with a homebrew probe as a payload process on a device (firmware 13.00). The probe resolved all eight names in the system libSceLibcInternal.sprx and called each one on its own stack arrays, with a fault handler around every call:

Range Device AnyPS5 before AnyPS5 now
Four elements {5, -3, 7, 2} min/max value (-3/7 signed; 2/-3 as unsigned, i.e. 0xfffffffd for 4-byte and 0xfffffffffffffffd for 8-byte) same same
One element that element same same
Four elements starting at an odd address same as aligned same same
first == last on a valid buffer SIGSEGV NotImplemented std::invalid_argument
nullptr, nullptr SIGSEGV NotImplemented std::invalid_argument
Reversed by one element SIGSEGV NotImplemented std::invalid_argument
Two elements plus half an element, or less than one element SIGSEGV NotImplemented std::invalid_argument

The same result held for all eight functions. The empty range faults even when both pointers are inside a valid buffer.

  • Previous behaviour: VectorAlgorithms.cpp#L161 (__std_min_4i; the other seven have the same guard).
  • Precedent for throwing where the console faults: libc.md#L37-L38 (feholdexcept, feupdateenv).
  • Why an invalid range cannot terminate: MSVC STL's _Minmax_impl reads *_First and walks the tail until _Ptr != _Last fails (vector_algorithms.cpp#L3342-L3351); this is the library these helpers come from, not a disassembly of the console module.
  • Console measurement as a source: REVERSE_ENGINEERING.md#L9.

The eight debt entries keep their disassembly notes and are cut to the remaining gap: these inputs throw here where the console faults.

Tested

Linux (Ubuntu, GCC, Release), on main (b11c7bf84f) with this branch applied. guest_vector_algorithms passes. Its new CheckInvalidRanges calls all eight functions with an empty range on a valid buffer, null bounds, a range reversed by one element and two partial-element ranges, and requires std::invalid_argument each time, plus a one-element range that must return its value. With VectorAlgorithms.cpp from main the test aborts (__std_min_4i_nid_postfix not implemented). tools/progress.py: +8 implemented functions (libc 1628/1647 -> 1636/1647). tools/check_conventions.py passes.

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:5058764239 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12605). Originally posted by [KernelPanicNT](https://github.com/KernelPanicNT) on 2026-10-28T22:33:53Z. --- ### What `__std_min_{4,8}{i,u}` and `__std_max_{4,8}{i,u}` now throw `std::invalid_argument` for null, empty, reversed and partial-element ranges instead of calling `NotImplemented_nid_no_patch`. On a device the same NIDs faulted for every such input the probe tried (table below); other null, reversed or partial ranges are assumed to fault the same way, so there is no return value to reproduce. Throwing where the console faults follows the `feholdexcept` and `feupdateenv` entries in the `libc` debt. Nonempty ranges are unchanged. **Source of the behaviour** Measured with a homebrew probe as a payload process on a device (firmware 13.00). The probe resolved all eight names in the system `libSceLibcInternal.sprx` and called each one on its own stack arrays, with a fault handler around every call: | Range | Device | AnyPS5 before | AnyPS5 now | |---|---|---|---| | Four elements `{5, -3, 7, 2}` | min/max value (`-3`/`7` signed; `2`/`-3` as unsigned, i.e. `0xfffffffd` for 4-byte and `0xfffffffffffffffd` for 8-byte) | same | same | | One element | that element | same | same | | Four elements starting at an odd address | same as aligned | same | same | | `first == last` on a valid buffer | SIGSEGV | `NotImplemented` | `std::invalid_argument` | | `nullptr, nullptr` | SIGSEGV | `NotImplemented` | `std::invalid_argument` | | Reversed by one element | SIGSEGV | `NotImplemented` | `std::invalid_argument` | | Two elements plus half an element, or less than one element | SIGSEGV | `NotImplemented` | `std::invalid_argument` | The same result held for all eight functions. The empty range faults even when both pointers are inside a valid buffer. - Previous behaviour: [`VectorAlgorithms.cpp#L161`](https://github.com/boykopovar/AnyPS5/blob/319621ba2e/core/libs/prx/libc/src/VectorAlgorithms.cpp#L161) (`__std_min_4i`; the other seven have the same guard). - Precedent for throwing where the console faults: [`libc.md#L37-L38`](https://github.com/boykopovar/AnyPS5/blob/319621ba2e/docs/dev/debt/libc.md#L37-L38) (`feholdexcept`, `feupdateenv`). - Why an invalid range cannot terminate: MSVC STL's `_Minmax_impl` reads `*_First` and walks the tail until `_Ptr != _Last` fails ([`vector_algorithms.cpp#L3342-L3351`](https://github.com/microsoft/STL/blob/b6f01e438b/stl/src/vector_algorithms.cpp#L3342-L3351)); this is the library these helpers come from, not a disassembly of the console module. - Console measurement as a source: [`REVERSE_ENGINEERING.md#L9`](https://github.com/boykopovar/AnyPS5/blob/319621ba2e/docs/dev/REVERSE_ENGINEERING.md#L9). The eight debt entries keep their disassembly notes and are cut to the remaining gap: these inputs throw here where the console faults. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux (Ubuntu, GCC, Release), on `main` (`b11c7bf84f`) with this branch applied. `guest_vector_algorithms` passes. Its new `CheckInvalidRanges` calls all eight functions with an empty range on a valid buffer, null bounds, a range reversed by one element and two partial-element ranges, and requires `std::invalid_argument` each time, plus a one-element range that must return its value. With `VectorAlgorithms.cpp` from `main` the test aborts (`__std_min_4i_nid_postfix not implemented`). `tools/progress.py`: +8 implemented functions (libc 1628/1647 -> 1636/1647). `tools/check_conventions.py` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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!12605
No description provided.