feat(libSceNpCommon): implement the sce::np::Object new and delete operators #12836

Merged
hypermoist merged 3 commits from upstream-pr/12836 into main 2026-10-30 09:49:47 +00:00
hypermoist commented 2026-10-29 12:02:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-29T12:02:23Z.


What

Implements the ten sce::np::Object allocation operators from libSceNpCommon.sprx of firmware 13.60 (read from disassembly, not measured on a console). Five were NotImplemented stubs with placeholder int (void) declarations. The other five are exported by the module but were missing from AnyPS5:

  • the stubs: nw…R16SceNpAllocatorEx, na…R16SceNpAllocatorEx, dl…R16SceNpAllocatorEx, da…R14SceNpAllocator and daEPv;
  • the missing exports: nw…R14SceNpAllocator, na…R14SceNpAllocator, dl…R14SceNpAllocator, da…R16SceNpAllocatorEx and dlEPv.

The contract, using the repo's existing NpAllocator layout (allocate at 0, release at 0x10, context at 0x18):

  • new / new[] (size_t size, SceNpAllocator& or SceNpAllocatorEx&):
    • calls allocate(size + 16, context) and returns null if it fails;
    • otherwise stores the allocator's address in the first 8 bytes of the block and returns the block plus 16;
    • the extended forms store the address of the SceNpAllocatorEx itself, whose first member is the allocator (the module's _sceNpAllocatorExConvertAllocator returns its argument).
  • delete / delete[] (with or without an allocator):
    • a null object does nothing;
    • otherwise calls release(object - 16, context) through the allocator stored in the header;
    • the allocator argument is ignored, as in the module.

size + 16 is not checked for overflow, as in the module. The debt file states both points and drops the five "signature unknown" entries.

Tested

Linux (CachyOS, GCC, Release): ctest --test-dir build -R 'np_common|public_headers' --output-on-failure, 70/70 passed. TestObjects in GuestNpCommonRuntime.cpp uses a recording allocator to check:

  • the header and the requested size;
  • that delete frees through the stored allocator, not the one passed;
  • the extended forms;
  • the null object and a failed allocation.

Comparison against the firmware code: I relinked the 13.60 module with this repo's relinker and ran all ten operators under unicorn (x86-64 emulation on a PC, not a console). The allocator's allocate and release pointers were bound to recording handlers.

  • Corpus: 2,040 cases. Each operator ran with 51 sizes (edges, random sizes, and 2^64 - 16 and 2^64 - 1, where size + 16 wraps), with a succeeding and a failing allocator, and for delete with a null and a real object. The header held a different allocator from the argument.
  • What was recorded: the result, every allocate and release call with its arguments, and the 16 header bytes.
  • Result: replaying the same cases through this implementation gives 0 mismatches. 11 corrupted expectations give 11 mismatches.

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. It removes stubs next to the ones #12825 removes, so whichever of the two lands second needs a trivial rebase; I will do it.
  • 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:5067619993 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12836). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-29T12:02:23Z. --- ### What Implements the ten `sce::np::Object` allocation operators from `libSceNpCommon.sprx` of firmware 13.60 (read from disassembly, not measured on a console). Five were `NotImplemented` stubs with placeholder `int (void)` declarations. The other five are exported by the module but were missing from AnyPS5: - the stubs: `nw…R16SceNpAllocatorEx`, `na…R16SceNpAllocatorEx`, `dl…R16SceNpAllocatorEx`, `da…R14SceNpAllocator` and `daEPv`; - the missing exports: `nw…R14SceNpAllocator`, `na…R14SceNpAllocator`, `dl…R14SceNpAllocator`, `da…R16SceNpAllocatorEx` and `dlEPv`. The contract, using the repo's existing `NpAllocator` layout (`allocate` at 0, `release` at 0x10, `context` at 0x18): - **`new` / `new[]` (`size_t size, SceNpAllocator&` or `SceNpAllocatorEx&`):** - calls `allocate(size + 16, context)` and returns null if it fails; - otherwise stores the allocator's address in the first 8 bytes of the block and returns the block plus 16; - the extended forms store the address of the `SceNpAllocatorEx` itself, whose first member is the allocator (the module's `_sceNpAllocatorExConvertAllocator` returns its argument). - **`delete` / `delete[]` (with or without an allocator):** - a null object does nothing; - otherwise calls `release(object - 16, context)` through the allocator stored in the header; - the allocator argument is ignored, as in the module. `size + 16` is not checked for overflow, as in the module. The debt file states both points and drops the five "signature unknown" entries. ### Tested Linux (CachyOS, GCC, Release): `ctest --test-dir build -R 'np_common|public_headers' --output-on-failure`, 70/70 passed. `TestObjects` in `GuestNpCommonRuntime.cpp` uses a recording allocator to check: - the header and the requested size; - that delete frees through the stored allocator, not the one passed; - the extended forms; - the null object and a failed allocation. **Comparison against the firmware code:** I relinked the 13.60 module with this repo's relinker and ran all ten operators under unicorn (x86-64 emulation on a PC, not a console). The allocator's `allocate` and `release` pointers were bound to recording handlers. - **Corpus:** 2,040 cases. Each operator ran with 51 sizes (edges, random sizes, and `2^64 - 16` and `2^64 - 1`, where `size + 16` wraps), with a succeeding and a failing allocator, and for delete with a null and a real object. The header held a different allocator from the argument. - **What was recorded:** the result, every allocate and release call with its arguments, and the 16 header bytes. - **Result:** replaying the same cases through this implementation gives **0 mismatches**. 11 corrupted expectations give 11 mismatches. 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. It removes stubs next to the ones #12825 removes, so whichever of the two lands second needs a trivial rebase; I will do it. - [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)
rooklz commented 2026-10-29 12:09:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T12:09:00Z.

Review: COMMENTED


I reviewed 72cde05a64 on macOS arm64 (Apple clang, ASan + UBSan, -fno-sanitize-recover=all). I built the new Runtime.cpp hunk and TestObjects as a standalone program against the NpAllocator / NpAllocatorEx layouts from Api.hpp. I didn't run the full CMake lane.

  • TestObjects passes as written.
  • tools/nid_names.py gives the same NIDs for the five replaced stubs as the debt entries this PR removes (1x0jThSUr4w, bKMVqRcCQ1U, iwDNdnEGyhI, 4il4PZAZOnQ, orRb69nSo64). The five new names hash to 0syNkhJANVw, V75N47uYdQc, 05KEwpDf4Ls, q2USyzLF4kI and CnDHI7sU+l0.
  • The PR is based on current main 5454cd2e45. git merge-tree against main, git diff --check and check_conventions.py --head are all clean.
  • pr_overlap.py --branch (1,395 PRs probed) finds #12825, #12829 and #12830, all from the stub-removal hunks you already disclose; #12829 and #12830 are stacked on #12825. It also finds #12839, which has the same ten exports because it is stacked on this PR and says so.

Test coverage. I ran 13 single-change mutants of the new code. TestObjects kills 3: the header offset, the null-object check and storing a copy of the inner allocator. The other 10 change behaviour, and the test still passes:

  1. delete[] (void*, SceNpAllocator&) does nothing. It is only called with a null object.
  2. delete (void*, SceNpAllocatorEx&) does nothing. It is only called with a null object.
    3–5. The three delete forms that take an allocator free through the argument instead of the header. Only dl…R14SceNpAllocator is tested with a different allocator; dl…R16, da…R14 and da…R16 are not.
  3. new zeroes the second 8 bytes of the header. The test doesn't check that they are left alone, and your differential recorded all 16 header bytes.
  4. new returns null when size + 16 wraps. The debt file says the module doesn't check this, but no test pins it.
  5. na…R16SceNpAllocatorEx requests the wrong size. Its requested size is never checked.
    9–10. Neither array new form is run with a failing allocator, so a non-null result on failure survives.

These rows kill all 10. They pass on your head, and the three mutants already killed stay killed:

void TestObjectsThroughStoredAllocator() {
    AllocatorRecord record {};
    NpAllocator allocator {RecordAllocate, nullptr, RecordRelease, &record};
    NpAllocatorEx allocatorEx {allocator, 0x1234};
    AllocatorRecord other {};
    NpAllocator otherAllocator {RecordAllocate, nullptr, RecordRelease, &other};
    NpAllocatorEx otherEx {otherAllocator, 0};

    record.block.fill(0xAA);
    void* object = _ZN3sce2np6ObjectnaEmR16SceNpAllocatorEx(24, &allocatorEx);
    Require(object == record.block.data() + 16 && record.size == 40, "an extended array is allocated behind the header");
    Require(std::all_of(record.block.begin() + 8, record.block.begin() + 16, [](std::uint8_t b) { return b == 0xAA; }),
        "the second half of the header is left as the allocator returned it");
    _ZN3sce2np6ObjectdlEPvR16SceNpAllocatorEx(object, &otherEx);
    Require(record.released == record.block.data() && other.released == nullptr, "delete with an extended allocator frees through the stored one");

    record.released = nullptr;
    _ZN3sce2np6ObjectdaEPvR14SceNpAllocator(_ZN3sce2np6ObjectnwEmR14SceNpAllocator(8, &allocator), &otherAllocator);
    Require(record.released == record.block.data() && other.released == nullptr, "delete[] with an allocator frees through the stored one");

    record.released = nullptr;
    _ZN3sce2np6ObjectdaEPvR16SceNpAllocatorEx(_ZN3sce2np6ObjectnwEmR16SceNpAllocatorEx(8, &allocatorEx), &otherEx);
    Require(record.released == record.block.data() && other.released == nullptr, "delete[] with an extended allocator frees through the stored one");

    record.fail = true;
    Require(_ZN3sce2np6ObjectnaEmR14SceNpAllocator(8, &allocator) == nullptr && _ZN3sce2np6ObjectnaEmR16SceNpAllocatorEx(8, &allocatorEx) == nullptr,
        "a failed array allocation returns null");
    Require(_ZN3sce2np6ObjectnwEmR14SceNpAllocator(SIZE_MAX - 15, &allocator) == nullptr && record.size == 0, "the size plus the header wraps");
}

This needs <algorithm> and <cstdint>. The wrap row assumes a recording allocator fails the 0-byte request; it does here because fail is still set. The row only shows that the request wraps to 0. Your differential already covered 2^64 - 16, so please check this row against it.

Nit: the body has "all test done locally on actual hardware" and also "Not run on a console". It's the same wording as on #12825, #12829 and #12830.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5697416882 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12836#pullrequestreview-5697416882). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T12:09:00Z. **Review:** COMMENTED --- I reviewed 72cde05a64 on macOS arm64 (Apple clang, ASan + UBSan, `-fno-sanitize-recover=all`). I built the new `Runtime.cpp` hunk and `TestObjects` as a standalone program against the `NpAllocator` / `NpAllocatorEx` layouts from `Api.hpp`. I didn't run the full CMake lane. - `TestObjects` passes as written. - `tools/nid_names.py` gives the same NIDs for the five replaced stubs as the debt entries this PR removes (`1x0jThSUr4w`, `bKMVqRcCQ1U`, `iwDNdnEGyhI`, `4il4PZAZOnQ`, `orRb69nSo64`). The five new names hash to `0syNkhJANVw`, `V75N47uYdQc`, `05KEwpDf4Ls`, `q2USyzLF4kI` and `CnDHI7sU+l0`. - The PR is based on current main 5454cd2e45. `git merge-tree` against main, `git diff --check` and `check_conventions.py --head` are all clean. - `pr_overlap.py --branch` (1,395 PRs probed) finds #12825, #12829 and #12830, all from the stub-removal hunks you already disclose; #12829 and #12830 are stacked on #12825. It also finds #12839, which has the same ten exports because it is stacked on this PR and says so. **Test coverage.** I ran 13 single-change mutants of the new code. `TestObjects` kills 3: the header offset, the null-object check and storing a copy of the inner allocator. The other 10 change behaviour, and the test still passes: 1. `delete[] (void*, SceNpAllocator&)` does nothing. It is only called with a null object. 2. `delete (void*, SceNpAllocatorEx&)` does nothing. It is only called with a null object. 3–5. The three delete forms that take an allocator free through the argument instead of the header. Only `dl…R14SceNpAllocator` is tested with a different allocator; `dl…R16`, `da…R14` and `da…R16` are not. 6. `new` zeroes the second 8 bytes of the header. The test doesn't check that they are left alone, and your differential recorded all 16 header bytes. 7. `new` returns null when `size + 16` wraps. The debt file says the module doesn't check this, but no test pins it. 8. `na…R16SceNpAllocatorEx` requests the wrong size. Its requested size is never checked. 9–10. Neither array `new` form is run with a failing allocator, so a non-null result on failure survives. These rows kill all 10. They pass on your head, and the three mutants already killed stay killed: ```cpp void TestObjectsThroughStoredAllocator() { AllocatorRecord record {}; NpAllocator allocator {RecordAllocate, nullptr, RecordRelease, &record}; NpAllocatorEx allocatorEx {allocator, 0x1234}; AllocatorRecord other {}; NpAllocator otherAllocator {RecordAllocate, nullptr, RecordRelease, &other}; NpAllocatorEx otherEx {otherAllocator, 0}; record.block.fill(0xAA); void* object = _ZN3sce2np6ObjectnaEmR16SceNpAllocatorEx(24, &allocatorEx); Require(object == record.block.data() + 16 && record.size == 40, "an extended array is allocated behind the header"); Require(std::all_of(record.block.begin() + 8, record.block.begin() + 16, [](std::uint8_t b) { return b == 0xAA; }), "the second half of the header is left as the allocator returned it"); _ZN3sce2np6ObjectdlEPvR16SceNpAllocatorEx(object, &otherEx); Require(record.released == record.block.data() && other.released == nullptr, "delete with an extended allocator frees through the stored one"); record.released = nullptr; _ZN3sce2np6ObjectdaEPvR14SceNpAllocator(_ZN3sce2np6ObjectnwEmR14SceNpAllocator(8, &allocator), &otherAllocator); Require(record.released == record.block.data() && other.released == nullptr, "delete[] with an allocator frees through the stored one"); record.released = nullptr; _ZN3sce2np6ObjectdaEPvR16SceNpAllocatorEx(_ZN3sce2np6ObjectnwEmR16SceNpAllocatorEx(8, &allocatorEx), &otherEx); Require(record.released == record.block.data() && other.released == nullptr, "delete[] with an extended allocator frees through the stored one"); record.fail = true; Require(_ZN3sce2np6ObjectnaEmR14SceNpAllocator(8, &allocator) == nullptr && _ZN3sce2np6ObjectnaEmR16SceNpAllocatorEx(8, &allocatorEx) == nullptr, "a failed array allocation returns null"); Require(_ZN3sce2np6ObjectnwEmR14SceNpAllocator(SIZE_MAX - 15, &allocator) == nullptr && record.size == 0, "the size plus the header wraps"); } ``` This needs `<algorithm>` and `<cstdint>`. The wrap row assumes a recording allocator fails the 0-byte request; it does here because `fail` is still set. The row only shows that the request wraps to 0. Your differential already covered `2^64 - 16`, so please check this row against it. Nit: the body has "all test done locally on actual hardware" and also "Not run on a console". It's the same wording as on #12825, #12829 and #12830.
rooklz commented 2026-10-29 12:40:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T12:40:56Z.

Review: APPROVED


Re-reviewed 2d12b228ab on macOS arm64 (Apple clang, ASan + UBSan, -fno-sanitize-recover=all). As before, I built the Runtime.cpp hunk and the test as a standalone program, taken verbatim from the PR head with the NpAllocator / NpAllocatorEx layouts from Api.hpp. This time the program covers both TestObjects and TestObjectsThroughStoredAllocator. I didn't run the full CMake lane.

  • Both tests pass. The implementation is unchanged since 72cde05a64; only the test rows are new.
  • I re-ran the 13 mutants from my first review. All 13 are now killed. Each of the 10 that used to survive fails on its own row: the delete no-ops, delete through the argument for dl…R16, da…R14 and da…R16, the zeroed header tail, the added overflow check, the wrong na…R16 size, and non-null on failure for both na forms. The 3 that were already killed still fail.
  • Thanks for checking each row against the 13.60 corpus, especially the 2^64 - 16 case.
  • git merge-tree against main 5454cd2e45, git diff --check and check_conventions.py --head are clean. The overlap picture hasn't changed, because the new commit only touches GuestNpCommonRuntime.cpp.
  • Body nit resolved.

Approving.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5697830085 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12836#pullrequestreview-5697830085). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T12:40:56Z. **Review:** APPROVED --- Re-reviewed 2d12b228ab on macOS arm64 (Apple clang, ASan + UBSan, `-fno-sanitize-recover=all`). As before, I built the `Runtime.cpp` hunk and the test as a standalone program, taken verbatim from the PR head with the `NpAllocator` / `NpAllocatorEx` layouts from `Api.hpp`. This time the program covers both `TestObjects` and `TestObjectsThroughStoredAllocator`. I didn't run the full CMake lane. - Both tests pass. The implementation is unchanged since 72cde05a64; only the test rows are new. - I re-ran the 13 mutants from my first review. All 13 are now killed. Each of the 10 that used to survive fails on its own row: the delete no-ops, delete through the argument for `dl…R16`, `da…R14` and `da…R16`, the zeroed header tail, the added overflow check, the wrong `na…R16` size, and non-null on failure for both `na` forms. The 3 that were already killed still fail. - Thanks for checking each row against the 13.60 corpus, especially the `2^64 - 16` case. - `git merge-tree` against main 5454cd2e45, `git diff --check` and `check_conventions.py --head` are clean. The overlap picture hasn't changed, because the new commit only touches `GuestNpCommonRuntime.cpp`. - Body nit resolved. Approving.
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!12836
No description provided.