Repository navigation
fix(libc): read rand_r's state and the _Dtest classifiers' value through checked guest copies - #24467
Conversation
…ugh checked guest copies rand_r read and wrote its state straight through the guest pointer, and _Dtest and _FDtest memcpy'd their value from it, so an unmapped or read-only pointer crashed the host. rand_r now copies the state in, advances the host copy and writes it back with GuestCopyOut_nid_no_patch; _Dtest and _FDtest read the value with GuestCopyIn_nid_no_patch. An unusable pointer throws std::invalid_argument with errno unchanged, as a null pointer and _LDtest already do.
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against dae530d640:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_math_testsbuilt cleanly. - CTest (
guest_math):1/1 Test #1216: guest_math ....................... Passed 0.02 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libc/src/Math.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #1216: guest_math .......................***Exception: SegFault 1.25 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- Verified GuestCopyIn_nid_no_patch / GuestCopyOut_nid_no_patch memory safety and nullptr/unmapped/read-only pointer handling in rand_r_nid_postfix, _FDtest_nid_postfix, and _Dtest_nid_postfix in core/libs/prx/libc/src/Math.cpp.
- Verified exact exception semantics: std::invalid_argument is thrown on null/unreadable/unwritable pointers, leaving errno untouched per libc conventions and existing patterns (e.g. _LDtest, modfl).
- Verified that rand_r correctly reads guest state, advances it via MinimalStandardNext, writes the updated state back to guest memory, and returns the converted value as int.
- Verified cross-platform 32/64-bit portability (sizeof uint32_t, uint64_t, unsigned int) across LP64 and LLP64 targets.
- Verified AGENTS.md compliance: conventions passed, no new comments added, no special-cased values or stubs, and unit test coverage in core/libs/tests/GuestMath.cpp tests valid and invalid pointer paths.
Looks Good To Me
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head dae530d6403750562aefa8d7acdb95706eef0e28. I inspected the prepared diff and targeted source and test ranges through repo_read.py; no build or test commands were run.
Results
| Check | Result | Note |
|---|---|---|
| Diff review | passed | Both changed files inspected |
| Tests | not run | Author-reported results only |
Findings
Blocking
None.
Non-blocking
None.
Not verified
Did not run the reported CTest selection, mutation check, build, pointer audit, or progress script. Read all changed lines in core/libs/prx/libc/src/Math.cpp and core/libs/tests/GuestMath.cpp; no changed files were excluded. Related callers were located by search but not read in full, so their behavior is not verified.
To get this to a good state
Nothing is required.
|
Tested head Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release,
Whole batch: the full This is one combined tree, not a run of this PR alone. AI-assisted: yes (Claude Code) |
|
Windows CPU run: works on Windows. 7 of 7 tests pass on Existing runs:
What this run adds.
Code (line numbers at
Negative control and mutation. All of these ran on
Tests.
Host
AI-assisted: yes |
ligmaSec
left a comment
There was a problem hiding this comment.
Built the submitted Math.cpp and ran the exact GuestMath fixture on Linux. It passed the new rand_r, _Dtest, and _FDtest cases, including read-only input for classification, rejected random state writes, preservation of errno, and the valid seed update.
AI-assisted: yes.
|
@domx4q the guards are right: on main a bad rand_r state or _Dtest/_FDtest pointer is a host crash, and your tests pin the new throws. One regression comes with it. __fpclassifyf still calls _FDtest_nid_postfix(&x) on its own host stack argument, and _FDtest now reads that through GuestCopyIn_nid_no_patch. On main that was a plain memcpy. A host local cannot be a bad guest pointer, so each __fpclassifyf call now pays a fault-copy (or ReadProcessMemory on Windows) for nothing. Please move the bit classification into one static helper, for example ClassifyFloatBits(std::uint32_t bits), keep the null and GuestCopyIn checks only in _FDtest and _Dtest, and make __fpclassifyf_nid_postfix return ClassifyFloatBits(std::bit_caststd::uint32_t(x)). __fpclassifyd already avoids the copy through SonyFpclassifyd, so it needs no change. The tests should still pass as they are. |
|
@oneandonlydean done in 181d879.
Tested on Arch Linux, GCC, Release: AI-assisted: yes (Claude Code) |
|
No actionable comments were generated in the recent review. Walkthrough
Change: Bug fix Merge Risk:
|
ligmaSec
left a comment
There was a problem hiding this comment.
Retested the revised Math.cpp with the exact GuestMath fixture on Linux. It passed the covered rand_r, _Dtest, and _FDtest cases for valid values, read-only classifiers, invalid guest addresses, and preservation of errno.
AI-assisted: yes.
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
The checked copies guard the changed pointer accesses, and the reviewed change has no verified blocking defect.
What I ran
Reviewed head 181d8793bee4ed12148746b5d2f3eb5a9176c08e using the pinned repo_read.py reader. No tests or builds were run. The author reports the listed tests passed on Arch Linux with GCC in Release mode.
python3 repo_read.py search 'bool GuestCopyIn_nid_no_patch'
python3 repo_read.py cat core/libs/prx/libc/src/General.cpp 80 135
python3 repo_read.py cat core/libs/prx/libc/src/General.cpp 870 895
Read the changed files core/libs/prx/libc/src/Math.cpp and core/libs/tests/GuestMath.cpp, plus core/libs/prx/libc/src/General.cpp for checked-copy context. files.json lists no excluded changed files.
Results
| Check | Result | Note |
|---|---|---|
| Source review | passed | All changed files reviewed |
| Tests | not run | Author reports they passed |
| Build | not run | No build performed |
Negative control
Not run; no tests were executed during this review.
Findings
Blocking
None.
Non-blocking
None.
Sources
Math.cppat the reviewed headGuestMath.cppat the reviewed headGeneral.cppchecked-copy implementation at the reviewed head
Not verified
The reported tests were not rerun. Behavior on physical console hardware and other host operating systems was not verified.
To get this to a good state
Nothing is required.
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
|
What
rand_r(Math.cpp) read and wrote its state straight through the guest pointer, and_Dtest/_FDtestmemcpy'd their value from it, so an unmapped or read-only pointer crashed the host. They now follow the libc pattern that_LDtestandmodfluse (#23540):rand_rcopies the state in withGuestCopyIn_nid_no_patch, advances the host copy and writes it back once withGuestCopyOut_nid_no_patch._Dtestand_FDtestread the value with oneGuestCopyIn_nid_no_patch(8 or 4 bytes) in place of thememcpy. Read-only values still classify.std::invalid_argumentand leaveserrnounchanged, as a null pointer already does.Results for valid pointers are unchanged (existing
_Dtest/_FDtestclassification tables and therand_rsequence check inGuestMath.cpp). The internal_Xp_callers inExtendedPrecisionDouble.cpp(libSceLibcInternal) andVersionedExtendedPrecision.cpptake the same checked read; their tests pass unchanged.Tested
Arch Linux, GCC, Release build. Re-run after rebasing onto
origin/main0a025bd.ctest --test-dir build -R '^(guest_math|guest_math_elementary|guest_math_exact|guest_icu_numeric|guest_libc-random-seeding|guest_sce_v2_extended_precision|guest_libc_internal_extended_precision|guest_libc_internal_extended_precision_double|public_headers)$': passed.CheckRandomStateAndClassificationGuestPointersinGuestMath.cpp:rand_rthrows witherrnounchanged for an unmapped (0x10) state and for a read-only (static const) state, and the read-only state stays untouched._Dtestand_FDtestthrow for an unmapped value and classify read-only values.rand_rstill advances a writable state.rand_r'sGuestCopyOut_nid_no_patchresult makes the new test fail.python3 tools/progress.py --module libc: implemented count unchanged (1850).python3 tools/guest_pointer_audit.py libcno longer lists the three functions.tools/pr_overlap.py --branch fix/libc-rand-r-dtest-guest-pointers: #6295 and #7544 conflict withmainand could not be compared; their diffs do not touch these functions.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)Summary by CodeRabbit
errnounchanged when invalid pointers are rejected.