fix(libSceNpCommon): store the high hex digit before checking the low one in sceNpStrParseHex #12913

Merged
hypermoist merged 1 commit from upstream-pr/12913 into main 2026-10-30 08:25:42 +00:00
hypermoist commented 2026-10-29 14:12:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-29T14:12:15Z.


What

Fixes sceNpStrParseHex and sceNpStrnParseHex for input with a bad low digit. In libSceNpCommon.sprx of firmware 13.60 (read from disassembly, not measured on a console), both functions store each byte in two steps:

  1. they check the high digit and store high << 4;
  2. then they check the low digit and store (high << 4) | low.

So when the high digit is valid and the low digit is refused, the call returns 0x80558009 and leaves that byte as high << 4. AnyPS5 checked both digits before writing, so it left that byte unchanged. Example: "0A5G" gives 0A 50 on the firmware, but 0A and the old byte on main.

The fix keeps the shared ParseHex helper and only reorders the store. Everything else is unchanged: the odd-length check, the capacity check (0x80558008), and the accepted characters (0-9a-fA-F).

Tested

Linux (CachyOS, GCC 16.2, Release): ctest --test-dir build -R 'np_common|public_headers' --output-on-failure, 70/70 passed. TestHexAndCopies gets two rows:

  • a refused low digit stores the high nibble;
  • a refused high digit stores nothing.

Comparison against the firmware code: I relinked the 13.60 module with this repo's relinker and ran the functions under unicorn (x86-64 emulation on a PC, not a console).

  • Corpus: 9,000 cases:
    • 6,000 for the two parse functions: hex strings of 0 to 300 characters with 0 to 2 invalid characters, odd and even lengths, and capacities around length / 2;
    • 3,000 for sceNpStrBuildHex, unchanged here, as a regression check on its vectorised path.
  • What was recorded: the result and the whole output buffer plus 4 guard bytes.
  • Result:
    • main gives 449 mismatches, all of them parse cases with a refused low digit;
    • this branch gives 0 mismatches;
    • 20 corrupted expectations give 20 mismatches.

The generator and harness are np_hex_make_cases.py / np_hex_replay.cpp in https://gist.github.com/hypermoist/1834cf221fd737cdb4cca272cc1156d1. The gist has no module or recorded output. emucall.py there now also emulates the AVX2 byte instructions that sceNpStrBuildHex uses. It also zeroes the upper half of a register after a VEX.128 instruction that unicorn runs natively. With that change, the earlier corpora in the gist regenerate byte for byte.

Windows not built. Not run on a console.

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 (pr_overlap reports no overlap with open pull requests)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Assisted With AI: Yes. All tests were run locally on computer hardware (AMD Ryzen 5 5600G, 32 GB RAM, NVIDIA GeForce RTX 3060, CachyOS Linux 7.3, GCC 16.2), not on an actual console.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5069584844 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12913). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-29T14:12:15Z. --- ### What Fixes `sceNpStrParseHex` and `sceNpStrnParseHex` for input with a bad low digit. In `libSceNpCommon.sprx` of firmware 13.60 (read from disassembly, not measured on a console), both functions store each byte in two steps: 1. they check the high digit and store `high << 4`; 2. then they check the low digit and store `(high << 4) | low`. So when the high digit is valid and the low digit is refused, the call returns `0x80558009` and leaves that byte as `high << 4`. AnyPS5 checked both digits before writing, so it left that byte unchanged. Example: `"0A5G"` gives `0A 50` on the firmware, but `0A` and the old byte on main. The fix keeps the shared `ParseHex` helper and only reorders the store. Everything else is unchanged: the odd-length check, the capacity check (`0x80558008`), and the accepted characters (`0-9a-fA-F`). ### Tested Linux (CachyOS, GCC 16.2, Release): `ctest --test-dir build -R 'np_common|public_headers' --output-on-failure`, 70/70 passed. `TestHexAndCopies` gets two rows: - a refused low digit stores the high nibble; - a refused high digit stores nothing. **Comparison against the firmware code:** I relinked the 13.60 module with this repo's relinker and ran the functions under unicorn (x86-64 emulation on a PC, not a console). - **Corpus:** 9,000 cases: - 6,000 for the two parse functions: hex strings of 0 to 300 characters with 0 to 2 invalid characters, odd and even lengths, and capacities around `length / 2`; - 3,000 for `sceNpStrBuildHex`, unchanged here, as a regression check on its vectorised path. - **What was recorded:** the result and the whole output buffer plus 4 guard bytes. - **Result:** - main gives 449 mismatches, all of them parse cases with a refused low digit; - this branch gives **0 mismatches**; - 20 corrupted expectations give 20 mismatches. The generator and harness are `np_hex_make_cases.py` / `np_hex_replay.cpp` in https://gist.github.com/hypermoist/1834cf221fd737cdb4cca272cc1156d1. The gist has no module or recorded output. `emucall.py` there now also emulates the AVX2 byte instructions that `sceNpStrBuildHex` uses. It also zeroes the upper half of a register after a VEX.128 instruction that unicorn runs natively. With that change, the earlier corpora in the gist regenerate byte for byte. Windows not built. Not run on a console. ### 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 (`pr_overlap` reports no overlap with open pull requests) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes Assisted With AI: Yes. All tests were run locally on computer hardware (AMD Ryzen 5 5600G, 32 GB RAM, NVIDIA GeForce RTX 3060, CachyOS Linux 7.3, GCC 16.2), not on an actual console. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-29 14:14:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T14:14:18Z.

Review: APPROVED


Reviewed — looks good

The high-nibble partial write matches the reported firmware sequence, and an exact-head focused harness confirmed the new result-buffer behavior.

What I ran

Reviewed head 67bab6bdfee157a31d73b198b9aa2478c1ceae77 against main at bb04a4f1ea0175e9e4f149f0359a764d62ff3296; the merge preview is clean (tree d15042efbd108e0eee3905a0ac9c37fc8b9029bd). Environment: Linux 6.1.0-54-amd64, GCC 12.2.0.

I compiled the changed Strings.cpp with a small harness that checks both the low-digit partial write and the high-digit no-write case:

#include "prx/libSceNpCommon/include/Api.hpp"
#include <cstdint>
#include <iostream>
int main() {
    std::uint8_t bytes[2] = {0xAA, 0xFF};
    const int result = sceNpStrParseHex("0A5G", bytes, sizeof(bytes));
    if (result != static_cast<int>(0x80558009) || bytes[0] != 0x0A || bytes[1] != 0x50) return 1;
    bytes[0] = 0xAA; bytes[1] = 0xFF;
    const int highError = sceNpStrnParseHex("0AG5", 4, bytes, sizeof(bytes));
    if (highError != static_cast<int>(0x80558009) || bytes[1] != 0xFF) return 2;
    std::cout << "sceNpStrParseHex partial-byte behavior passed\\n";
}
g++ -std=c++23 -O2 -Wall -Wextra -Werror -ffunction-sections -fdata-sections \
  -Icore/libs -Icore/libs/prx/libSceNpCommon/include \
  core/libs/prx/libSceNpCommon/Strings.cpp /tmp/np-hex-12913.cpp \
  -Wl,--gc-sections -o /tmp/np-hex-12913
/tmp/np-hex-12913
python3 tools/check_conventions.py --base upstream/main --head HEAD
git diff --check upstream/main...HEAD

Results

Check Result
Focused exact-head harness Passed
Conventions Passed
Diff check Passed (exit 0; no output)
Hosted eligibility, conventions, intake, labels, report Pending at last check
Unedited output
$ /tmp/np-hex-12913
sceNpStrParseHex partial-byte behavior passed

$ python3 tools/check_conventions.py --base upstream/main --head HEAD
All conventions checks passed.

$ git diff --check upstream/main...HEAD

Negative control

Assertion Change made to the input Outcome
A refused high digit does not write that output byte. Used "0AG5" with the second output byte initialized to 0xFF. The function returned 0x80558009 and left the byte at 0xFF.

Findings

Blocking

None found in the reviewed diff.

Non-blocking

The author reports a 13.60 firmware disassembly comparison and a 9,000-case Unicorn differential run in the PR description; I did not independently reproduce that corpus or run on a console. Full CTest and Windows were not run locally.

Sources

Not verified

The full np_common CTest target, the author's 9,000-case differential harness, other operating systems, and PS5 console behavior were not independently verified here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5699174566 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12913#pullrequestreview-5699174566). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T14:14:18Z. **Review:** APPROVED --- # Reviewed — looks good The high-nibble partial write matches the reported firmware sequence, and an exact-head focused harness confirmed the new result-buffer behavior. ## What I ran Reviewed head `67bab6bdfee157a31d73b198b9aa2478c1ceae77` against `main` at `bb04a4f1ea0175e9e4f149f0359a764d62ff3296`; the merge preview is clean (tree `d15042efbd108e0eee3905a0ac9c37fc8b9029bd`). Environment: Linux 6.1.0-54-amd64, GCC 12.2.0. I compiled the changed `Strings.cpp` with a small harness that checks both the low-digit partial write and the high-digit no-write case: ```cpp #include "prx/libSceNpCommon/include/Api.hpp" #include <cstdint> #include <iostream> int main() { std::uint8_t bytes[2] = {0xAA, 0xFF}; const int result = sceNpStrParseHex("0A5G", bytes, sizeof(bytes)); if (result != static_cast<int>(0x80558009) || bytes[0] != 0x0A || bytes[1] != 0x50) return 1; bytes[0] = 0xAA; bytes[1] = 0xFF; const int highError = sceNpStrnParseHex("0AG5", 4, bytes, sizeof(bytes)); if (highError != static_cast<int>(0x80558009) || bytes[1] != 0xFF) return 2; std::cout << "sceNpStrParseHex partial-byte behavior passed\\n"; } ``` ```sh g++ -std=c++23 -O2 -Wall -Wextra -Werror -ffunction-sections -fdata-sections \ -Icore/libs -Icore/libs/prx/libSceNpCommon/include \ core/libs/prx/libSceNpCommon/Strings.cpp /tmp/np-hex-12913.cpp \ -Wl,--gc-sections -o /tmp/np-hex-12913 /tmp/np-hex-12913 python3 tools/check_conventions.py --base upstream/main --head HEAD git diff --check upstream/main...HEAD ``` ## Results | Check | Result | |---|---| | Focused exact-head harness | Passed | | Conventions | Passed | | Diff check | Passed (exit 0; no output) | | Hosted eligibility, conventions, intake, labels, report | Pending at last check | <details> <summary>Unedited output</summary> ```text $ /tmp/np-hex-12913 sceNpStrParseHex partial-byte behavior passed $ python3 tools/check_conventions.py --base upstream/main --head HEAD All conventions checks passed. $ git diff --check upstream/main...HEAD ``` </details> ## Negative control | Assertion | Change made to the input | Outcome | |---|---|---| | A refused high digit does not write that output byte. | Used `"0AG5"` with the second output byte initialized to `0xFF`. | The function returned `0x80558009` and left the byte at `0xFF`. | ## Findings ### Blocking None found in the reviewed diff. ### Non-blocking The author reports a 13.60 firmware disassembly comparison and a 9,000-case Unicorn differential run in the PR description; I did not independently reproduce that corpus or run on a console. Full CTest and Windows were not run locally. ## Sources - [PR-head `ParseHex` implementation](https://github.com/boykopovar/AnyPS5/blob/67bab6bdfee157a31d73b198b9aa2478c1ceae77/core/libs/prx/libSceNpCommon/Strings.cpp#L131-L144) - [PR-head regression assertions](https://github.com/boykopovar/AnyPS5/blob/67bab6bdfee157a31d73b198b9aa2478c1ceae77/core/libs/tests/GuestNpCommonRuntime.cpp#L417-L428) ## Not verified The full `np_common` CTest target, the author's 9,000-case differential harness, other operating systems, and PS5 console behavior were not independently verified here.
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!12913
No description provided.