feat(libSceNpCommon): implement sce::np::RingBuf and RingBufMemory #12852
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12852"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-29T12:42:43Z.
What
Implements
sce::np::RingBufandsce::np::RingBufMemoryfromlibSceNpCommon.sprxof firmware 13.60 (read from disassembly, not measured on a console), in a newRingBuf.cpp. That is 30 exports:NotImplementedstubs with placeholderint (void)declarations;C2/D2aliases.RingBuf::IsFullandRingBufMemory::IsInitare left as they are on main, because #12823 implements them.This is stacked on #12839 (itself on #12836):
D0frees throughsce::np::Object's delete, andRingBufMemoryuses the extended allocator. The new commits areba9b4ebcb0,76858c5f19,56551447a9andaac4ad09ba;03fb3cb264only merges #12839.Layout (
NpRingBuf, 0x30 bytes, andNpRingBufMemory, 0x38 bytes): vtable,buf, read pointer, write pointer, data size, buffer size, and forRingBufMemorytheSceNpAllocatorEx*. Both vtables are{0, 0, D1, D0}, as in the module, and the constructors store a pointer to theD1slot.RingBuf
C1/C2set the vtable and clear the fields.ctorclears the fields only, anddtordoes nothing.D1/D2reset the vtable, andD0also frees the object through itssce::np::Objectheader.Init(buf, size)attaches caller storage.Destroyclears the fields, andClearresets both pointers and the size.IsEmpty,GetDataSizeandGetFreeSizereport the counts.CheckoutForWrite(size_t*)returns the contiguous free block, or null when full. When the buffer is empty it first moves both pointers back to the start.CheckinForWrite(n)advances the write pointer and wraps it at the end.CheckoutForRead/CheckinForReadare the read-side pair.CheckinForReaddoes not check its size.Write(data, n):0x80559E0Awhennexceeds the free size;Read(dst, n):n == 0and0x80559E0Bwhennexceeds the data;dstonly drops the data.Peek(offset, dst, n):0x80559E0Aunlessoffset < sizeandoffset + n <= size.RingBufMemory
C1/C2(SceNpAllocatorEx*)store the allocator.D1/D2releasebufand leave the RingBuf vtable, andD0then frees the object.dtorreleasesbuf, andctordoes nothing.Init(size)allocates first and returns the allocator'soutOfMemoryErrorwhen that fails.Destroyreleasesbufand clears the fields.Expand(n)reallocates tobufSize + nand returns0x80559E09when that fails. It moves the data after the read pointer up bynonly when the write pointer is below the read pointer. So, as in the module, expanding a full buffer keeps both pointers together: the next write takes it for empty, starts at the beginning over the queued data, and the following read asserts. The debt entry records this, andTestRingBufpins it.Gaps (debt entry). Where the module asserts and calls
sceNpPanic, this throwsstd::invalid_argumentnaming the check. That covers:Initon a buffer that already has storage, or with null or empty storage;CheckoutForWritesize, and aCheckinForWritepast the end;Writeinto a full buffer;Peek;The debt file drops the 9 "signature unknown" entries.
Tested
Linux (CachyOS, GCC 16.2, Release):
ctest --test-dir build -R 'np_common|public_headers' --output-on-failure, 70/70 passed.TestRingBufinGuestNpCommonRuntime.cppcovers:RingBufMemoryon a memory heap:Init, a wrap, andExpandkeeping the order of wrapped data;Comparison against the firmware code: I relinked the 13.60 module with this repo's relinker and ran both classes under unicorn (x86-64 emulation on a PC, not a console). The allocator callbacks and
printf/abort(reached through the module'ssceNpPanic) were bound to recording handlers.Expandsequences (full with the pointers meeting at 1, full at 0, wrapped but not full, and empty), half of themRingBufon caller storage and halfRingBufMemoryon a recording allocator (with injected allocation failures), 31,279 operations in all. Buffer sizes run from 1 to 64. The operations are writes and reads of random sizes (including 0 and more than fits, and a null destination), peeks, the checkout/checkin pairs (with checkins up to one past the block),Clear, the size queries,Expand, re-InitandDestroy. A firmware assertion is recorded aspanicand ends the sequence; there were 441.panicincluded. 29 corrupted expectations give 29 mismatches.ctoranddtor. I ran those separately on an object behind asce::np::Objectheader, and they match:D0frees the header block, andRingBufMemory's destructors releasebuffirst.The generator and harness are
np_ringbuf_make_cases.py/np_ringbuf_replay.cppin https://gist.github.com/hypermoist/1834cf221fd737cdb4cca272cc1156d1. The gist has no module or recorded output.Windows not built. Not run on a console.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T12:47:02Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The full-buffer expansion path loses the write boundary, so a subsequent valid write corrupts the queue and the next read throws.
What I ran
Reviewed head
56551447a9832cf004baa8bc1ae9c2cbec172424againstmainat5454cd2e455994d992aca39335ce60507eeda1eb;git merge-tree --write-treewas clean (54933ccfa14a16254bcfd74c62637a621b67f78e). Local environment: Debian Linux x86_64, GCC 12.2.0.Commands:
The standalone harness linked the submitted
RingBuf.cppwith malloc/realloc/free callbacks and a no-op object-delete stub. Its operations were: initialize capacity 4; writeABCD; read one byte; writeEto fill the ring and make the pointers meet at offset 1; expand by 2; writeFG; then read the six queued bytes. The expected FIFO data after the first read isBCDEFG.Results
54933ccfa14a16254bcfd74c62637a621b67f78egit diff --checkRaw local output
Negative control
Findings
Blocking
In
Expand, when a full ring has equal read/write pointers at a nonzero offset, the offsets remain equal after the capacity increases. The nextWriteBlocktreats any equal-pointer, non-full state as empty and resets both pointers tobuf. That writes over queued bytes; the read then reaches the internal size/pointer assertion. The deterministic sequence above reproduces this on the submitted head. The PR's test only expands a partially occupied wrapped ring, so it does not cover the equal-pointer case. See WriteBlock and its pointer reset, Expand, and the current expansion test.Please add this full-buffer sequence as a deterministic test and compare it against the firmware module. If the module preserves the FIFO data, adjust the pointer handling so the expanded free span can be written without moving or overwriting queued bytes. If the module itself exhibits this behavior, include the recorded result and document the limitation in technical debt; the current PR does not identify it as a gap, and its test currently implies that expansion preserves usable capacity and data order.
Non-blocking
The PR reports a 1,500-sequence firmware comparison under Unicorn, but its generator/replay files are external and the module/output are not attached. Including the targeted equal-pointer case in the reported comparison would make this edge result easier to verify.
Sources
RingBuf.cpp, pinned to56551447a9832cf004baa8bc1ae9c2cbec17242456551447a9832cf004baa8bc1ae9c2cbec172424Not verified
To get this to a good state
Please add the deterministic full-buffer case, establish its firmware result, and either preserve the FIFO on the new capacity or record and test the confirmed firmware limitation. I will rerun the same local commands and recheck the hosted jobs on the updated head.
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:54:11Z.
Review: COMMENTED
Withdrawn. This review applied a title-ID/log requirement that is not in CONTRIBUTING (
Unblocks: noneis allowed), and it contained no code review.