feat(libSceNpCommon): implement sce::np::RefObject #13431

Merged
hypermoist merged 1 commit from upstream-pr/13431 into main 2026-10-30 21:42:10 +00:00
hypermoist commented 2026-10-30 14:19:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-30T14:19:59Z.


What

Implements sce::np::RefObject (7 exports) from libSceNpCommon.sprx of firmware 13.60 (read from disassembly, not measured on a console).

  • Already stubs on main: 3 had placeholder int (void) declarations: C1, D1 and Release.
  • Missing from AnyPS5: the other 4 are exported by the module but were not declared: C2, D0, D2 and AddRef.

NpRefObject (declared in Api.hpp, 16 bytes): a vtable, then int32_t refCount at +8. The vtable is {0, 0, D1, D0}.

  • C1/C2: one function in the module. They set the vtable and a count of 0.
  • D1/D2: do nothing. D0 frees through sce::np::Object::operator delete(void*).
  • AddRef: sceNpAtomicInc32(&refCount) + 1, the new count.
  • Release: sceNpAtomicDec32(&refCount) - 1. When that is exactly 0, it calls the deleting destructor from vtable slot 1; it returns the new count either way.
  • The counts wrap at the 32-bit limits, as in the module; the arithmetic is done unsigned so wrapping is defined. A count that was already 0 or below goes negative without a delete. The debt file records both.

Tested

The results below are for head 502ef67d9dd. Linux (CachyOS, GCC 16.2, Release):

  • ctest --test-dir build -R 'np_common|public_headers' --output-on-failure passed 70/70.
  • guest_np_common_runtime passed 3 of 3 runs.
  • The full suite (3,006 tests) passed apart from guest_freetype. That one fails here because my local 3rdparty/freetype checkout is not at the pinned commit; this PR does not touch FreeType.

Firmware differential: 7,200 cases, run from the module's code under unicorn and replayed through this implementation, with 0 mismatches.

  • AddRef and Release: 3,000 cases each. Counts include 0, ±1, 2, the 32-bit limits and random values. The object's vtable is a fake whose slot 1 records the destructor call.
  • Compared: the result, the count after the call, the padding after it, and whether the destructor ran on the right object.
  • Constructors and destructors: 1,200 cases. They compare the 8 bytes after the vtable, with the prior count as input.
  • Corruption check: a copy with 3% of the expected outputs corrupted gives 214 mismatches.
  • Generator: np_ref_object_make_cases.py and np_ref_object_replay.cpp in this gist, with the emucall.py runner. It needs a decrypted module, which is not attached.

Guest test: TestRefObjects in GuestNpCommonRuntime.cpp checks:

  • the initial count, and the counts AddRef and Release return;
  • the destructor leaving the count alone;
  • on an object from Object::operator new: a release that is not the last keeps the object, and the last one deletes it through the stored allocator via D0.

Mutation check: I changed 6 single lines, one at a time, and the differential catches all 6 (248–3,000 mismatches each):

  • AddRef returning the old count;
  • Release returning the old count;
  • Release deleting at <= 0;
  • Release never deleting;
  • the constructor not zeroing the count;
  • D1 clearing it.

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.
    • This PR lands before #13120. #13120 declares the 4 exports missing from main (AddRef, C2, D0, D2) as NotImplemented stubs, so it drops them from InventoryExports.cpp after this lands. Agreed with its author (comment): #13120 has removed the four declarations and declares Depends on: #13130, #13270, #13277, #13281, #13345, #13408, #13431.
    • Neighbouring lines only, no shared exports:
  • 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:5085861031 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13431). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-30T14:19:59Z. --- ### What Implements `sce::np::RefObject` (7 exports) from `libSceNpCommon.sprx` of firmware 13.60 (read from disassembly, not measured on a console). - **Already stubs on `main`:** 3 had placeholder `int (void)` declarations: `C1`, `D1` and `Release`. - **Missing from AnyPS5:** the other 4 are exported by the module but were not declared: `C2`, `D0`, `D2` and `AddRef`. **`NpRefObject`** (declared in `Api.hpp`, 16 bytes): a vtable, then `int32_t refCount` at `+8`. The vtable is `{0, 0, D1, D0}`. - **C1/C2:** one function in the module. They set the vtable and a count of 0. - **D1/D2:** do nothing. **D0** frees through `sce::np::Object::operator delete(void*)`. - **`AddRef`:** `sceNpAtomicInc32(&refCount) + 1`, the new count. - **`Release`:** `sceNpAtomicDec32(&refCount) - 1`. When that is exactly 0, it calls the deleting destructor from vtable slot 1; it returns the new count either way. - The counts wrap at the 32-bit limits, as in the module; the arithmetic is done unsigned so wrapping is defined. A count that was already 0 or below goes negative without a delete. The debt file records both. ### Tested The results below are for head `502ef67d9dd`. Linux (CachyOS, GCC 16.2, Release): - `ctest --test-dir build -R 'np_common|public_headers' --output-on-failure` passed 70/70. - `guest_np_common_runtime` passed 3 of 3 runs. - The full suite (3,006 tests) passed apart from `guest_freetype`. That one fails here because my local `3rdparty/freetype` checkout is not at the pinned commit; this PR does not touch FreeType. **Firmware differential:** 7,200 cases, run from the module's code under unicorn and replayed through this implementation, with 0 mismatches. - **`AddRef` and `Release`:** 3,000 cases each. Counts include 0, ±1, 2, the 32-bit limits and random values. The object's vtable is a fake whose slot 1 records the destructor call. - **Compared:** the result, the count after the call, the padding after it, and whether the destructor ran on the right object. - **Constructors and destructors:** 1,200 cases. They compare the 8 bytes after the vtable, with the prior count as input. - **Corruption check:** a copy with 3% of the expected outputs corrupted gives 214 mismatches. - **Generator:** `np_ref_object_make_cases.py` and `np_ref_object_replay.cpp` in [this gist](https://gist.github.com/hypermoist/1834cf221fd737cdb4cca272cc1156d1), with the `emucall.py` runner. It needs a decrypted module, which is not attached. **Guest test:** `TestRefObjects` in `GuestNpCommonRuntime.cpp` checks: - the initial count, and the counts `AddRef` and `Release` return; - the destructor leaving the count alone; - on an object from `Object::operator new`: a release that is not the last keeps the object, and the last one deletes it through the stored allocator via `D0`. **Mutation check:** I changed 6 single lines, one at a time, and the differential catches all 6 (248–3,000 mismatches each): - `AddRef` returning the old count; - `Release` returning the old count; - `Release` deleting at `<= 0`; - `Release` never deleting; - the constructor not zeroing the count; - `D1` clearing it. 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. - **This PR lands before #13120.** #13120 declares the 4 exports missing from `main` (`AddRef`, `C2`, `D0`, `D2`) as `NotImplemented` stubs, so it drops them from `InventoryExports.cpp` after this lands. Agreed with its author ([comment](https://github.com/boykopovar/AnyPS5/pull/13120#issuecomment-6525114044)): #13120 has removed the four declarations and declares `Depends on: #13130, #13270, #13277, #13281, #13345, #13408, #13431`. - **Neighbouring lines only, no shared exports:** - #12823 touches nearby `Api.hpp` and debt lines; - #12852, #12859, #12919, #13130, #13270, #13277, #13281, #13345 and #13408 (all mine) touch nearby stub, declaration, test or debt lines. - Either order works; whichever lands second needs a trivial merge, and I will do it on mine. - [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)
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!13431
No description provided.