fix(libc): match title libc.prx for hexadecimal strtod underflow #12291

Merged
B-Howell merged 4 commits from upstream-pr/12291 into main 2026-10-28 18:26:34 +00:00
B-Howell commented 2026-10-28 07:50:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by B-Howell on 2026-10-28T07:50:05Z.


What

Hexadecimal strtod and wcstod now follow the libc.prx that titles ship when the exact value is below 2^-1074, the last open item of #9691. The rule, measured from God of War Ragnarök's (PPSA08329) bundled libc.prx (SHA-256 a485b34ef160ee635d357f21f4a93586bed1cf499b82918c734dcfd78b04e443):

  • An exact value of at least 2^-1074 is correctly rounded and errno is left alone (unless it overflows, which sets ERANGE).
  • A nonzero exact value below 2^-1074 sets ERANGE. Between 2^-1075 and 2^-1074 the result is 2^-1074 only from 2^-1075 * (1 + 2^-52) up, otherwise a signed zero. So 0x1.8p-1075 and 0x1.0000000000001p-1075 give 2^-1074, but 0x1p-1075, 0x1.00000000000001p-1075 and 0x1.0000000000000fp-1075 give 0.
  • wcstod gives the same bits, end positions and errno as strtod.

How it was measured: a small loader maps that decrypted libc.prx, applies its relocations, supplies __error and calls its strtod and wcstod natively in a Linux (WSL2) host process. This is a title-shipped SDK module run on a PC, not a console measurement. An exact rational model of the rule above matches every result of that libc.prx on 260,055 hexadecimal inputs (200,046 hand-built edge and boundary cases plus 60,009 random forms; 9,735 of them between 2^-1075 and 2^-1074), with 0 mismatches. wcstod output was byte-identical to strtod on both sets.

The main bug: the old check classified only results equal to 2^-1074, and used std::strtold to recover the exact value. That keeps 64 bits, so 0x1.fffffffffffffffffp-1075 looked like 2^-1074 and kept errno, and values just above 2^-1075 were rounded by the host instead of truncated to zero. The new code reads the subject itself: the first 64 significant bits plus a sticky bit, and an exponent that saturates at 2^62 (digit runs add at most about ±2^59, so the sum cannot overflow). strtof, strtold and decimal input are unchanged.

docs/dev/debt/libc.md: the strtod entry records that the hex rule rests on one title's libc.prx, and that results of at least 2^-1074 come from the host strtod, where glibc 2.35 rounds some a unit low (for example 0x3ffffffffffff5p-1077). The _Stod/_Stodx entry records that libSceLibcInternal.sprx is unverified for hexadecimal underflow and follows this rule here.

Tested

  • Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, at 864f3100bc (this branch on main b99bbebacd): guest_strtod_subnormal, guest_math, guest_dinkumware_internals, guest_wide_strings 4/4 passed. Strings.cpp and GuestStrtodSubnormal.cpp rebuilt with 0 warnings.
  • Control: main's Strings.cpp with the new tests aborts guest_strtod_subnormal at "long hexadecimal just below the smallest subnormal".
  • One-off runs, not committed: the 1,048,576-digit tests rerun with 300,000,000-digit runs (integer and fraction digits, at and past the boundaries, saturated exponents; strtod and wcstod) passed in 20.25 s. A further 36 boundary inputs (endptr, 0x with no digits, 0x.p1, uppercase P/X, signs, -0x0p0, leading and fraction zeros, the band edges with sticky bits) passed. They gave the same results as that libc.prx run directly.
  • The Windows host strtod rounds the glibc cases from the debt entry (0x3ffffffffffff5p-1077, 0x20000000000003p-1076 and four others) the same as that libc.prx.
  • Ubuntu 22.04.1 (WSL2), GCC 11.4.0, glibc 2.35, Ninja, Release, at 864f3100bc: the same 4 tests passed, 0 warnings. No other PR was needed.
  • Rebased onto main 660a0b306d (82c8a51391), which only changes libkernel_sys files. tools/check_conventions.py --base origin/main 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: #9691
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5047063514 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12291). Originally posted by [B-Howell](https://github.com/B-Howell) on 2026-10-28T07:50:05Z. --- ### What Hexadecimal `strtod` and `wcstod` now follow the `libc.prx` that titles ship when the exact value is below 2^-1074, the last open item of #9691. The rule, measured from God of War Ragnarök's (PPSA08329) bundled `libc.prx` (SHA-256 `a485b34ef160ee635d357f21f4a93586bed1cf499b82918c734dcfd78b04e443`): - An exact value of at least 2^-1074 is correctly rounded and `errno` is left alone (unless it overflows, which sets `ERANGE`). - A nonzero exact value below 2^-1074 sets `ERANGE`. Between 2^-1075 and 2^-1074 the result is 2^-1074 only from 2^-1075 * (1 + 2^-52) up, otherwise a signed zero. So `0x1.8p-1075` and `0x1.0000000000001p-1075` give 2^-1074, but `0x1p-1075`, `0x1.00000000000001p-1075` and `0x1.0000000000000fp-1075` give 0. - `wcstod` gives the same bits, end positions and `errno` as `strtod`. How it was measured: a small loader maps that decrypted `libc.prx`, applies its relocations, supplies `__error` and calls its `strtod` and `wcstod` natively in a Linux (WSL2) host process. This is a title-shipped SDK module run on a PC, not a console measurement. An exact rational model of the rule above matches every result of that `libc.prx` on 260,055 hexadecimal inputs (200,046 hand-built edge and boundary cases plus 60,009 random forms; 9,735 of them between 2^-1075 and 2^-1074), with 0 mismatches. `wcstod` output was byte-identical to `strtod` on both sets. The main bug: the old check classified only results equal to 2^-1074, and used `std::strtold` to recover the exact value. That keeps 64 bits, so `0x1.fffffffffffffffffp-1075` looked like 2^-1074 and kept `errno`, and values just above 2^-1075 were rounded by the host instead of truncated to zero. The new code reads the subject itself: the first 64 significant bits plus a sticky bit, and an exponent that saturates at 2^62 (digit runs add at most about ±2^59, so the sum cannot overflow). `strtof`, `strtold` and decimal input are unchanged. `docs/dev/debt/libc.md`: the `strtod` entry records that the hex rule rests on one title's `libc.prx`, and that results of at least 2^-1074 come from the host `strtod`, where glibc 2.35 rounds some a unit low (for example `0x3ffffffffffff5p-1077`). The `_Stod`/`_Stodx` entry records that `libSceLibcInternal.sprx` is unverified for hexadecimal underflow and follows this rule here. ### 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. --> - **Windows 11 Pro 10.0.26300**, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, at 864f3100bc (this branch on `main` b99bbebacd): `guest_strtod_subnormal`, `guest_math`, `guest_dinkumware_internals`, `guest_wide_strings` 4/4 passed. Strings.cpp and GuestStrtodSubnormal.cpp rebuilt with 0 warnings. - **Control:** `main`'s Strings.cpp with the new tests aborts `guest_strtod_subnormal` at "long hexadecimal just below the smallest subnormal". - **One-off runs, not committed:** the 1,048,576-digit tests rerun with 300,000,000-digit runs (integer and fraction digits, at and past the boundaries, saturated exponents; `strtod` and `wcstod`) passed in 20.25 s. A further 36 boundary inputs (endptr, `0x` with no digits, `0x.p1`, uppercase `P`/`X`, signs, `-0x0p0`, leading and fraction zeros, the band edges with sticky bits) passed. They gave the same results as that `libc.prx` run directly. - The Windows host `strtod` rounds the glibc cases from the debt entry (`0x3ffffffffffff5p-1077`, `0x20000000000003p-1076` and four others) the same as that `libc.prx`. - **Ubuntu 22.04.1 (WSL2)**, GCC 11.4.0, glibc 2.35, Ninja, Release, at 864f3100bc: the same 4 tests passed, 0 warnings. No other PR was needed. - Rebased onto `main` 660a0b306d (82c8a51391), which only changes libkernel_sys files. `tools/check_conventions.py --base origin/main` 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: #9691 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-28 08:49:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T08:49:36Z.

Review: APPROVED


Reviewed the hexadecimal subject parser and underflow decision against the documented title-bundled libc.prx rule. The 64-bit significand plus sticky bit and saturated exponent retain the boundary information needed for the measured threshold; the tests cover below/at/above the cutoff, signed zero, normal transition, overflow, very long runs, and matching wcstod bits/end pointers/errno. The author reports a direct module comparison over 260,055 inputs and Windows/WSL focused runs; hosted Windows, registration, relinker, ASan, and write-watch checks pass, while hosted Linux is skipped. The PR clearly limits the claim to one title build run on a host, not a console. No blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5679978954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12291#pullrequestreview-5679978954). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T08:49:36Z. **Review:** APPROVED --- Reviewed the hexadecimal subject parser and underflow decision against the documented title-bundled `libc.prx` rule. The 64-bit significand plus sticky bit and saturated exponent retain the boundary information needed for the measured threshold; the tests cover below/at/above the cutoff, signed zero, normal transition, overflow, very long runs, and matching `wcstod` bits/end pointers/errno. The author reports a direct module comparison over 260,055 inputs and Windows/WSL focused runs; hosted Windows, registration, relinker, ASan, and write-watch checks pass, while hosted Linux is skipped. The PR clearly limits the claim to one title build run on a host, not a console. No blocking issue found.
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!12291
No description provided.