feat(libSceNpCommon): implement 95 more of the module's exports #9063

Merged
hypermoist merged 11 commits from upstream-pr/9063 into main 2026-10-23 22:50:08 +00:00
hypermoist commented 2026-10-21 11:24:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-21T11:24:52Z.


What

Implements 95 more of the 150 libSceNpCommon exports declared by #8983, alongside #9037 (now merged), in new Runtime.cpp and Strings.cpp files (#9037's Export.cpp is unchanged):

  • Synchronisation and threads: condition wait, broadcast and signal-to, lightweight conditions, event flags, semaphores, thread create and id, 32-bit atomics, with the kernel's busy and timeout errors mapped to the module's own codes. The mutexes, the other condition calls and join come from #9037 and are used as it defines them.
  • Memory: heaps over a caller range, memory heaps on named flexible memory, the global heap owned by sceNpArchInit/sceNpArchTerm, the IPC client heap, kernel memory allocation and the allocator tables.
  • Callout contexts: a worker thread that runs queued callouts in deadline order.
  • Time, SDK version, process id, user id list and random numbers.
  • sceNpCmpNpId, added to main as a throwing stub after this PR opened: it checks both ids for null (0x80550003) and the flag byte at 0x1C (0x80550605), compares the online id and the words at 0x14 and 0x18, and treats an empty first byte at 0x18 on either id as matching (0x80550609 otherwise).
  • Base64 and base64url (RFC 4648), hex, integer formatting and parsing, bounded string copies, JSON escaping and unescaping, single UTF-8 character reads, form URL encoding, online id checks, language codes (BCP 47 tags), title ids, debug ids and a binary dump.

Behaviour, argument checks, error codes and their order come from libSceNpCommon.sprx, firmware 13.60, read from disassembly, not measured on a console. No code, tables or strings were copied from it. The tables are rebuilt from public standards.

The other 35 exports still throw, and docs/dev/debt/libSceNpCommon.md says why:

  • 14 read registry keys through libSceRegMgr, which is not implemented.
  • 3 forward to libkernel calls that still throw.
  • 7 depend on console platform tags, network domain names or a console version string.
  • 11 are the schema-checked streaming JSON parser.

The same file lists where implemented exports differ from the console.

Tested

Linux (CachyOS, GCC). New guest_np_common_runtime test (core/libs/tests/GuestNpCommonRuntime.cpp, registered next to #9037's) covers:

  • every implemented group, including error codes and buffer-size edges;
  • callout ordering and cancellation, repeated ten times;
  • heap and allocator round trips;
  • the global and IPC heap lifecycles.

ctest -R guest_np_common passes, including #9037's own suites. Rerun on a merge of main e3d03fbae6 (16aae7d289): the four guest_np_common suites pass on Linux and, cross-built with WinLibs GCC 15.2.0 and run under Wine, on Windows, and the patched libSceNpCommon.prx builds.

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

Assisted With AI: Yes But all test done locally on actual hardware

https://claude.ai/code/session_013XFS3FUfBtqCVfwmBrRQ6q

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4950760859 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9063). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-21T11:24:52Z. --- ### What Implements 95 more of the 150 libSceNpCommon exports declared by #8983, alongside #9037 (now merged), in new `Runtime.cpp` and `Strings.cpp` files (#9037's `Export.cpp` is unchanged): - Synchronisation and threads: condition wait, broadcast and signal-to, lightweight conditions, event flags, semaphores, thread create and id, 32-bit atomics, with the kernel's busy and timeout errors mapped to the module's own codes. The mutexes, the other condition calls and join come from #9037 and are used as it defines them. - Memory: heaps over a caller range, memory heaps on named flexible memory, the global heap owned by `sceNpArchInit`/`sceNpArchTerm`, the IPC client heap, kernel memory allocation and the allocator tables. - Callout contexts: a worker thread that runs queued callouts in deadline order. - Time, SDK version, process id, user id list and random numbers. - `sceNpCmpNpId`, added to `main` as a throwing stub after this PR opened: it checks both ids for null (`0x80550003`) and the flag byte at 0x1C (`0x80550605`), compares the online id and the words at 0x14 and 0x18, and treats an empty first byte at 0x18 on either id as matching (`0x80550609` otherwise). - Base64 and base64url (RFC 4648), hex, integer formatting and parsing, bounded string copies, JSON escaping and unescaping, single UTF-8 character reads, form URL encoding, online id checks, language codes (BCP 47 tags), title ids, debug ids and a binary dump. Behaviour, argument checks, error codes and their order come from `libSceNpCommon.sprx`, firmware 13.60, read from disassembly, not measured on a console. No code, tables or strings were copied from it. The tables are rebuilt from public standards. The other 35 exports still throw, and `docs/dev/debt/libSceNpCommon.md` says why: - 14 read registry keys through libSceRegMgr, which is not implemented. - 3 forward to libkernel calls that still throw. - 7 depend on console platform tags, network domain names or a console version string. - 11 are the schema-checked streaming JSON parser. The same file lists where implemented exports differ from the console. ### Tested Linux (CachyOS, GCC). New `guest_np_common_runtime` test (`core/libs/tests/GuestNpCommonRuntime.cpp`, registered next to #9037's) covers: - every implemented group, including error codes and buffer-size edges; - callout ordering and cancellation, repeated ten times; - heap and allocator round trips; - the global and IPC heap lifecycles. `ctest -R guest_np_common` passes, including #9037's own suites. Rerun on a merge of `main` e3d03fbae6 (16aae7d289): the four `guest_np_common` suites pass on Linux and, cross-built with WinLibs GCC 15.2.0 and run under Wine, on Windows, and the patched `libSceNpCommon.prx` builds. ### 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 Assisted With AI: Yes But all test done locally on actual hardware https://claude.ai/code/session_013XFS3FUfBtqCVfwmBrRQ6q
pipe1os commented 2026-10-21 17:00:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pipe1os on 2026-10-21T17:00:23Z.


Built and ran this on Windows, since there's no Windows run yet.

Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 (ucrt-posix-seh r7), Ninja, Release, BUILD_TESTING=ON. 736ee67caf merged onto main cecb540a63 without conflicts, so #9037 is in too. libSceNpCommon and both test executables build with no warnings.

guest_np_common_runtime fails on Windows:

the length is reported before the 32-bit range check
FATAL: abort() (SIGABRT)

That's GuestNpCommonRuntime.cpp:446. sceNpStrToInt32 parses with std::strtol into a long (Strings.cpp:360-364), and long is 32 bits on Windows. So strtol("3000000000") saturates to LONG_MAX with ERANGE and ParseInteger returns before writing parsedLength. On Linux the 64-bit long holds the value and the int32 check afterwards catches it. Using long long / std::strtoll / LLONG_MIN / LLONG_MAX there, like sceNpStrToInt64 already does, fixes it. With just that change all four tests pass, three runs each (ctest -R ^guest_np_common --repeat until-fail:3). ids, sync and clock pass on the PR as is.

Without the change: with core/libs/prx/libSceNpCommon back to main, guest_np_common_ids, _sync and _clock abort with sceNpCmpOnlineId not implemented, sceNpMutexInit not implemented and sceNpGetSystemClockUsec not implemented. guest_np_common_runtime_tests doesn't even link on main. The sceNpUtilJsonUnescape stub there sits after the } that closes extern "C" in PendingExports.cpp (line 755), so it gets a mangled C++ name and an import of it can't resolve. This PR fixes that on the way, since the function moves into Strings.cpp. With that brace moved locally, the runtime test aborts on sceNpAtomicInc32 not implemented.

I also compared the callouts with shadPS4 a808e76 (np_common.cpp:405-554). Context layout (0x28, same offsets), the three error codes (0x80558001, 0x80558002, 0x80558006), ordering on equal deadlines, the 32-bit delay of sceNpCalloutStartOnCtx, stop and term all match. One difference: NpCallout here is 0x28 with a trailing reserved (Runtime.cpp:69-76), and sceNpCalloutStartOnCtx64 does *callout = {}, which clears all 0x28 bytes. shadPS4's OrbisNpCalloutEntry is 0x20 (np_common.h:25-30, asserted in np_common.cpp:23) and only writes its four fields. If the console's entry is 0x20, a caller that allocates exactly that gets the 8 bytes after its entry zeroed. Does the 0x28 come from the module, i.e. does it write at +0x20?

Not run: the full suite or any title.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6321714796 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9063#issuecomment-6321714796). Originally posted by [pipe1os](https://github.com/pipe1os) on 2026-10-21T17:00:23Z. --- Built and ran this on Windows, since there's no Windows run yet. Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 (ucrt-posix-seh r7), Ninja, Release, `BUILD_TESTING=ON`. 736ee67caf merged onto `main` cecb540a63 without conflicts, so #9037 is in too. `libSceNpCommon` and both test executables build with no warnings. `guest_np_common_runtime` fails on Windows: ``` the length is reported before the 32-bit range check FATAL: abort() (SIGABRT) ``` That's `GuestNpCommonRuntime.cpp:446`. `sceNpStrToInt32` parses with `std::strtol` into a `long` (`Strings.cpp:360-364`), and `long` is 32 bits on Windows. So `strtol("3000000000")` saturates to `LONG_MAX` with `ERANGE` and `ParseInteger` returns before writing `parsedLength`. On Linux the 64-bit `long` holds the value and the int32 check afterwards catches it. Using `long long` / `std::strtoll` / `LLONG_MIN` / `LLONG_MAX` there, like `sceNpStrToInt64` already does, fixes it. With just that change all four tests pass, three runs each (`ctest -R ^guest_np_common --repeat until-fail:3`). `ids`, `sync` and `clock` pass on the PR as is. Without the change: with `core/libs/prx/libSceNpCommon` back to `main`, `guest_np_common_ids`, `_sync` and `_clock` abort with `sceNpCmpOnlineId not implemented`, `sceNpMutexInit not implemented` and `sceNpGetSystemClockUsec not implemented`. `guest_np_common_runtime_tests` doesn't even link on `main`. The `sceNpUtilJsonUnescape` stub there sits after the `}` that closes `extern "C"` in `PendingExports.cpp` (line 755), so it gets a mangled C++ name and an import of it can't resolve. This PR fixes that on the way, since the function moves into `Strings.cpp`. With that brace moved locally, the runtime test aborts on `sceNpAtomicInc32 not implemented`. I also compared the callouts with shadPS4 a808e76 (`np_common.cpp:405-554`). Context layout (0x28, same offsets), the three error codes (`0x80558001`, `0x80558002`, `0x80558006`), ordering on equal deadlines, the 32-bit delay of `sceNpCalloutStartOnCtx`, stop and term all match. One difference: `NpCallout` here is 0x28 with a trailing `reserved` (`Runtime.cpp:69-76`), and `sceNpCalloutStartOnCtx64` does `*callout = {}`, which clears all 0x28 bytes. shadPS4's `OrbisNpCalloutEntry` is 0x20 (`np_common.h:25-30`, asserted in `np_common.cpp:23`) and only writes its four fields. If the console's entry is 0x20, a caller that allocates exactly that gets the 8 bytes after its entry zeroed. Does the 0x28 come from the module, i.e. does it write at +0x20? Not run: the full suite or any title.
hypermoist commented 2026-10-22 01:19:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-22T01:19:10Z.


@pipe1os thanks for the Windows run. Fixed in 962b88a9e1: sceNpStrToInt32 now parses with std::strtoll into a long long and checks against LLONG_MIN/LLONG_MAX, as sceNpStrToInt64 does. Nothing else in the module uses the host's long. ctest -R guest_np_common passes on Linux (4 of 4).

On the callout size: 0x28 comes from the module. In libSceNpCommon.sprx (firmware 12.00, read from disassembly, not measured on a console), sceNpCalloutStartOnCtx64 clears the entry with a 32-byte store at +0 and then an 8-byte store of 0 at +0x20, before it fills in the deadline, handler and argument. So the console also clears all 0x28 bytes, and a 0x20-byte entry would have its next 8 bytes cleared there too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330335779 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9063#issuecomment-6330335779). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-22T01:19:10Z. --- @pipe1os thanks for the Windows run. Fixed in 962b88a9e1: `sceNpStrToInt32` now parses with `std::strtoll` into a `long long` and checks against `LLONG_MIN`/`LLONG_MAX`, as `sceNpStrToInt64` does. Nothing else in the module uses the host's `long`. `ctest -R guest_np_common` passes on Linux (4 of 4). On the callout size: 0x28 comes from the module. In `libSceNpCommon.sprx` (firmware 12.00, read from disassembly, not measured on a console), `sceNpCalloutStartOnCtx64` clears the entry with a 32-byte store at +0 and then an 8-byte store of 0 at +0x20, before it fills in the deadline, handler and argument. So the console also clears all 0x28 bytes, and a 0x20-byte entry would have its next 8 bytes cleared there too.
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!9063
No description provided.