feat(libwebrtc): implement rtc::CopyOnWriteBuffer #16485

Merged
AlexWhitehouse merged 1 commit from upstream-pr/16485 into main 2026-11-10 03:56:06 +00:00
AlexWhitehouse commented 2026-11-06 20:59:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-06T20:59:10Z.


What

Implements libwebrtc rtc::CopyOnWriteBuffer's five exports, which threw through NotImplemented_nid_no_patch:

Export NID
CopyOnWriteBuffer(size_t) (_ZN3rtc17CopyOnWriteBufferC1Em) sR-f7IaOCzc
CopyOnWriteBuffer(size_t, size_t) (_ZN3rtc17CopyOnWriteBufferC1Emm) dxzEsYnE9lg
CopyOnWriteBuffer(const CopyOnWriteBuffer&) (_ZN3rtc17CopyOnWriteBufferC1ERKS0_) ZRgDNVxmGck
~CopyOnWriteBuffer() (_ZN3rtc17CopyOnWriteBufferD1Ev) jjig4x39rfk
UnshareAndEnsureCapacity(size_t) (_ZN3rtc17CopyOnWriteBuffer24UnshareAndEnsureCapacityEm) wak9ck3EZGE

Sources, all at M134 8d78f5de:

At M127 (e0b28a6a), the other end of the debt file's revision range, the .cc, buffer.h and ref_counter.h are byte-identical. The header differs only by M134's added const begin()/end(). src/CopyOnWriteBuffer.cpp starts with copy_on_write_buffer.cc's notice copied byte for byte, plus the WebRTC BSD licence. The bodies follow upstream:

  • Constructors: the size constructor allocates BufferT(size, size) only when size > 0. The size-and-capacity constructor allocates when either is non-zero, with capacity max(size, capacity) and the bytes left uninitialised, as new T[capacity_] leaves them.
  • Copy and destroy: the copy constructor shares the backing buffer and adds a reference. The destructor drops one. On the last reference the data array and then the object are freed, as delete this on a FinalRefCountedObject<BufferT<uint8_t, false>> does (no zeroing).
  • UnshareAndEnsureCapacity: returns early when the buffer is unshared and new_capacity <= capacity() - offset_. Otherwise it allocates a new buffer, copies size_ bytes from offset_, adds the new reference before dropping the old one (as scoped_refptr assignment does), and resets offset_ to 0.

ABI. Guest code inlines most of CopyOnWriteBuffer, including AddRef, Release and HasOneRef, so the layout and allocator have to match what guest code does:

  • Layouts:

    • CopyOnWriteBuffer: buffer_, offset_, size_ at offsets 0, 8 and 16 (24 bytes);
    • FinalRefCountedObject<Buffer>, which has no virtual functions: size_, capacity_, data_ and the atomic ref_count_ at 0, 8, 16 and 24 (32 bytes).

    include/CopyOnWriteBuffer.hpp checks both with static_asserts.

  • Reference count: updates use the memory orders ref_counter.h uses: relaxed increment, acq_rel decrement, acquire load for HasOneRef.

  • Allocator: the backing object comes from the guest's operator new/operator delete (_Znwm/_ZdlPv) and its bytes from operator new[]/operator delete[] (_Znam/_ZdaPv), as libc's StdioExtras.cpp provides them. So a buffer this module creates can be freed by guest-inlined delete this, and the reverse. libc's sized operator delete frees through the same heap, so either form of the guest's deletion works.

  • Departures from upstream: RTC_DCHECKs are debug-only and left out. A null object, or unsharing a buffer with no backing storage, throws where upstream would crash.

  • Debt: a new entry under "Public source implementations" in docs/dev/debt/libwebrtc.md records these choices and that nothing is compared with the console. The inventory count is recounted from Export.cpp: it becomes 330, although main lists 339, because that figure was already 4 too high on main. All five exports had names, so the unnamed figure stays 60.

Tested

  • New registered ctest guest_webrtc_copy_on_write_buffer (core/libs/prx/libwebrtc/tests/CopyOnWriteBuffer.cpp), with the guest allocator intercepted so every allocation and free is accounted for:
    • Constructors: both constructors' fields, including capacity below size and zero sizes (no backing buffer); uninitialised bytes are not zeroed.
    • Sharing: the copy constructor shares and counts. Destructors in both orders free the data and the object exactly once.
    • UnshareAndEnsureCapacity: a no-op when unshared and large enough, including from a slice; growth; detaching a shared buffer, after which modifying one copy leaves the other unchanged; detaching a slice copies from its offset and resets it.
    • Allocation failure: no leak.
    • Guest-built buffers: a backing object built by the test in the 32-byte layout, as inlined guest code would build it, is shared, released and finally freed by the test's own inline-style release.
  • Linux x86-64 (Ubuntu 26.04, GCC 15, Release), the libwebrtc group:
    • with main's library, 18 existing tests pass and the new test fails with _ZN3rtc17CopyOnWriteBufferC1Em_nid_postfix not implemented;
    • with this PR (284ba89852), 19/19 pass, and public_headers passes (407 libraries, 30,759 exports).
  • Mutation check: removing the shared-reference condition from UnshareAndEnsureCapacity fails the new test.
  • Windows 11 (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release):
    • ctest -R webrtc passes 19/19 at 284ba89852, with no warnings in the new files;
    • guest_webrtc_copy_on_write_buffer passes 20 consecutive repeats.
  • python3 tools/check_conventions.py --base origin/main: all conventions checks passed.
  • python3 tools/pr_overlap.py --branch feat/libwebrtc-copy-on-write-buffer: no open PR implements these exports. #15154 lists CopyOnWriteBuffer among the families it leaves unimplemented, and keeps only the CMake conflict it already has with main.
    • #15267 (PacketInfo) conflicts in Export.cpp, because it removes the stub directly after a CopyOnWriteBuffer constructor.
    • My #15766 and #16381 merge cleanly in code and conflict only on the inventory count line.
    • Count line: also shared with #15189–#15197, #15211, #15261–#15272, #15274–#15277, #15285 and #15609. Whichever merges second recounts.
    • #14212 (debt spacing) adds a wording conflict on that line, and #15264 keeps its existing conflicts and adds the count line.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5194548071 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16485). Originally posted by [AlexWhitehouse](https://github.com/AlexWhitehouse) on 2026-11-06T20:59:10Z. --- ### What Implements libwebrtc `rtc::CopyOnWriteBuffer`'s five exports, which threw through `NotImplemented_nid_no_patch`: | Export | NID | |---|---| | `CopyOnWriteBuffer(size_t)` (`_ZN3rtc17CopyOnWriteBufferC1Em`) | `sR-f7IaOCzc` | | `CopyOnWriteBuffer(size_t, size_t)` (`_ZN3rtc17CopyOnWriteBufferC1Emm`) | `dxzEsYnE9lg` | | `CopyOnWriteBuffer(const CopyOnWriteBuffer&)` (`_ZN3rtc17CopyOnWriteBufferC1ERKS0_`) | `ZRgDNVxmGck` | | `~CopyOnWriteBuffer()` (`_ZN3rtc17CopyOnWriteBufferD1Ev`) | `jjig4x39rfk` | | `UnshareAndEnsureCapacity(size_t)` (`_ZN3rtc17CopyOnWriteBuffer24UnshareAndEnsureCapacityEm`) | `wak9ck3EZGE` | **Sources**, all at M134 [8d78f5de](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/rtc_base/): - [`copy_on_write_buffer.cc`](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/rtc_base/copy_on_write_buffer.cc) and its header (`RefCountedBuffer = FinalRefCountedObject<Buffer>`, [line 295](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/rtc_base/copy_on_write_buffer.h#295)); - [`buffer.h`](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/rtc_base/buffer.h#91) (`BufferT(size, capacity)`, lines 91–96); - [`ref_counted_object.h`](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/rtc_base/ref_counted_object.h#60) (`FinalRefCountedObject`, lines 60–84); - [`ref_counter.h`](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/rtc_base/ref_counter.h#29) (lines 29, 48 and 65). At M127 ([e0b28a6a](https://webrtc.googlesource.com/src/+/e0b28a6a81a989c1f5c89e30fcd247870047390d/rtc_base/)), the other end of the debt file's revision range, the `.cc`, `buffer.h` and `ref_counter.h` are byte-identical. The header differs only by M134's added const `begin()`/`end()`. `src/CopyOnWriteBuffer.cpp` starts with `copy_on_write_buffer.cc`'s notice copied byte for byte, plus the WebRTC BSD licence. The bodies follow upstream: - **Constructors:** the size constructor allocates `BufferT(size, size)` only when `size > 0`. The size-and-capacity constructor allocates when either is non-zero, with capacity `max(size, capacity)` and the bytes left uninitialised, as `new T[capacity_]` leaves them. - **Copy and destroy:** the copy constructor shares the backing buffer and adds a reference. The destructor drops one. On the last reference the data array and then the object are freed, as `delete this` on a `FinalRefCountedObject<BufferT<uint8_t, false>>` does (no zeroing). - **`UnshareAndEnsureCapacity`:** returns early when the buffer is unshared and `new_capacity <= capacity() - offset_`. Otherwise it allocates a new buffer, copies `size_` bytes from `offset_`, adds the new reference before dropping the old one (as `scoped_refptr` assignment does), and resets `offset_` to 0. **ABI.** Guest code inlines most of `CopyOnWriteBuffer`, including `AddRef`, `Release` and `HasOneRef`, so the layout and allocator have to match what guest code does: - **Layouts:** - `CopyOnWriteBuffer`: `buffer_`, `offset_`, `size_` at offsets 0, 8 and 16 (24 bytes); - `FinalRefCountedObject<Buffer>`, which has no virtual functions: `size_`, `capacity_`, `data_` and the atomic `ref_count_` at 0, 8, 16 and 24 (32 bytes). `include/CopyOnWriteBuffer.hpp` checks both with `static_assert`s. - **Reference count:** updates use the memory orders `ref_counter.h` uses: relaxed increment, acq_rel decrement, acquire load for `HasOneRef`. - **Allocator:** the backing object comes from the guest's `operator new`/`operator delete` (`_Znwm`/`_ZdlPv`) and its bytes from `operator new[]`/`operator delete[]` (`_Znam`/`_ZdaPv`), as libc's `StdioExtras.cpp` provides them. So a buffer this module creates can be freed by guest-inlined `delete this`, and the reverse. libc's sized `operator delete` frees through the same heap, so either form of the guest's deletion works. - **Departures from upstream:** `RTC_DCHECK`s are debug-only and left out. A null object, or unsharing a buffer with no backing storage, throws where upstream would crash. - **Debt:** a new entry under "Public source implementations" in `docs/dev/debt/libwebrtc.md` records these choices and that nothing is compared with the console. The inventory count is recounted from `Export.cpp`: it becomes 330, although `main` lists 339, because that figure was already 4 too high on `main`. All five exports had names, so the unnamed figure stays 60. ### Tested - **New registered ctest `guest_webrtc_copy_on_write_buffer`** (`core/libs/prx/libwebrtc/tests/CopyOnWriteBuffer.cpp`), with the guest allocator intercepted so every allocation and free is accounted for: - **Constructors:** both constructors' fields, including capacity below size and zero sizes (no backing buffer); uninitialised bytes are not zeroed. - **Sharing:** the copy constructor shares and counts. Destructors in both orders free the data and the object exactly once. - **`UnshareAndEnsureCapacity`:** a no-op when unshared and large enough, including from a slice; growth; detaching a shared buffer, after which modifying one copy leaves the other unchanged; detaching a slice copies from its offset and resets it. - **Allocation failure:** no leak. - **Guest-built buffers:** a backing object built by the test in the 32-byte layout, as inlined guest code would build it, is shared, released and finally freed by the test's own inline-style release. - **Linux x86-64** (Ubuntu 26.04, GCC 15, Release), the libwebrtc group: - with `main`'s library, 18 existing tests pass and the new test fails with `_ZN3rtc17CopyOnWriteBufferC1Em_nid_postfix not implemented`; - with this PR (284ba89852), 19/19 pass, and `public_headers` passes (407 libraries, 30,759 exports). - **Mutation check:** removing the shared-reference condition from `UnshareAndEnsureCapacity` fails the new test. - **Windows 11** (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release): - `ctest -R webrtc` passes 19/19 at 284ba89852, with no warnings in the new files; - `guest_webrtc_copy_on_write_buffer` passes 20 consecutive repeats. - `python3 tools/check_conventions.py --base origin/main`: all conventions checks passed. - **`python3 tools/pr_overlap.py --branch feat/libwebrtc-copy-on-write-buffer`:** no open PR implements these exports. #15154 lists `CopyOnWriteBuffer` among the families it leaves unimplemented, and keeps only the CMake conflict it already has with `main`. - **#15267** (PacketInfo) conflicts in `Export.cpp`, because it removes the stub directly after a `CopyOnWriteBuffer` constructor. - **My #15766 and #16381** merge cleanly in code and conflict only on the inventory count line. - **Count line:** also shared with #15189–#15197, #15211, #15261–#15272, #15274–#15277, #15285 and #15609. Whichever merges second recounts. - **#14212** (debt spacing) adds a wording conflict on that line, and #15264 keeps its existing conflicts and adds the count line. ### 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 🤖 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!16485
No description provided.