fix(libSceAmpr): dynamically expand command buffer capacity on append #11727

Merged
camezaro-intc merged 1 commit from upstream-pr/11727 into main 2026-10-29 19:35:28 +00:00
camezaro-intc commented 2026-10-27 05:58:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by camezaro-intc on 2026-10-27T05:58:09Z.


What

When appending commands to an Apr::CommandBufferObject in libSceAmpr, dynamically expand the backing storage using GuestHeapAllocate_nid_postfix / GuestHeapReallocate_nid_postfix when an append would exceed the buffer's initial capacity (buffer->size), rather than failing immediately with SCE_KERNEL_ERROR_EBUSY. Track expanded buffers and free dynamic storage in sceAmprCommandBufferDestructor, sceAmprAprCommandBufferDestructor, sceAmprCommandBufferClearBuffer, sceAmprCommandBufferSetBuffer, and sceAmprAmmCommandBufferDestructor.

AnyPS5 uses higher-level synthetic command structs (e.g. ReadFileCommand at 40 bytes and WriteAddressCommand at 32 bytes) that exceed the byte footprint of native PS5 console command dwords. Fixed-size buffers sized by guest titles for console command sizes would otherwise run out of space on multi-chunk command streams and drop completion writes.

Tested

Windows 11 Pro, Call of Duty: Modern Warfare III (PPSA24263). Verified that fastfile decompression reading advances past slot 0 (offset 0x500000 / chunk 10) through 0x580000+, loading eng_code_post_gfx.ff and progressing through code_reloadable.ff up to offset 0x900000 and reaching sceGameUpdateCreateRequest. Passed ctest --test-dir build -R guest_ampr -V (3/3 tests pass).

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: #11725
  • Fixes: none
  • Unblocks: PPSA24263 #11591
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5029349376 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11727). Originally posted by [camezaro-intc](https://github.com/camezaro-intc) on 2026-10-27T05:58:09Z. --- ### What When appending commands to an `Apr::CommandBufferObject` in `libSceAmpr`, dynamically expand the backing storage using `GuestHeapAllocate_nid_postfix` / `GuestHeapReallocate_nid_postfix` when an append would exceed the buffer's initial capacity (`buffer->size`), rather than failing immediately with `SCE_KERNEL_ERROR_EBUSY`. Track expanded buffers and free dynamic storage in `sceAmprCommandBufferDestructor`, `sceAmprAprCommandBufferDestructor`, `sceAmprCommandBufferClearBuffer`, `sceAmprCommandBufferSetBuffer`, and `sceAmprAmmCommandBufferDestructor`. AnyPS5 uses higher-level synthetic command structs (e.g. `ReadFileCommand` at 40 bytes and `WriteAddressCommand` at 32 bytes) that exceed the byte footprint of native PS5 console command dwords. Fixed-size buffers sized by guest titles for console command sizes would otherwise run out of space on multi-chunk command streams and drop completion writes. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Pro, Call of Duty: Modern Warfare III (PPSA24263). Verified that fastfile decompression reading advances past slot 0 (offset 0x500000 / chunk 10) through 0x580000+, loading `eng_code_post_gfx.ff` and progressing through `code_reloadable.ff` up to offset 0x900000 and reaching `sceGameUpdateCreateRequest`. Passed `ctest --test-dir build -R guest_ampr -V` (3/3 tests pass). ### 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: #11725 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: PPSA24263 #11591 <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
MarcZX10 commented 2026-10-27 06:19:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T06:19:42Z.

Review: APPROVED


Reviewed exact head aa0fa80b8283c3fee155c301dbd4faacb01f2738. The full guest_ampr_tests target builds locally. Normal CTest startup stops at the repository's CPU-feature guard because this Xeon lacks AVX2; with only that guard bypassed in the disposable review worktree, ctest -R '^guest_ampr_markers$' passed 1/1. I restored the guard and rebuilt the test target; the review worktree is clean. git diff --check and current-base conventions pass. The author reports the Windows title reproduction and 3/3 focused CTests; hosted product checks were skipped, so I did not independently verify Windows or the title. I found no blocker and approve.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5663004718 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11727#pullrequestreview-5663004718). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T06:19:42Z. **Review:** APPROVED --- Reviewed exact head aa0fa80b8283c3fee155c301dbd4faacb01f2738. The full `guest_ampr_tests` target builds locally. Normal CTest startup stops at the repository's CPU-feature guard because this Xeon lacks AVX2; with only that guard bypassed in the disposable review worktree, `ctest -R '^guest_ampr_markers$'` passed 1/1. I restored the guard and rebuilt the test target; the review worktree is clean. `git diff --check` and current-base conventions pass. The author reports the Windows title reproduction and 3/3 focused CTests; hosted product checks were skipped, so I did not independently verify Windows or the title. I found no blocker and approve.
adriadam10 commented 2026-10-28 09:02:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T09:02:03Z.

Review: CHANGES_REQUESTED


Thanks, the previous points (AMM submit resolution, constructor cleanup, debt entry) are addressed. Remaining:

  • Export.cpp:188-189: AppendNop (and so Nop, NopWithData, ConstructNop) still returns SCE_KERNEL_ERROR_EBUSY at the original size, while every other append now grows. A buffer filled up to its bound by Nops behaves differently from the rest. Route it through EnsureCommandCapacity, with a test.
  • Growth is unbounded (up to 4 GiB, doubling) and is freed only if the guest calls a destructor, ClearBuffer or SetBuffer. Please state a cap (an append past it keeps returning EBUSY, which is also the console's contract for a full buffer that titles use to flush and submit) and give peak RSS on the MW3 load before/after in the PR (docs/dev/CONVENTIONS and the memory rules for caches/kept-alive allocations).
  • Export.cpp:44, Api.hpp:156: a test hook exported from the production library. Please use a seam that does not ship in the export surface (for example a test-only allocator override compiled into the test, or make the failure reachable through the cap above).
  • Export.cpp:111-118: ResolveBufferBase matches any registered original base. If the guest reuses that memory for another buffer without destroying the expanded one, a submit of the new buffer is redirected to the stale allocation. Add a test with a rebind of the same memory through a second buffer object, or key on the buffer as well.
  • Debt entry (docs/dev/debt/libSceAmpr.md:8): also record that a full buffer no longer reports EBUSY below the cap and that growth places commands in memory the guest did not allocate. On the design question: growth is acceptable as the stop-gap given the points above; moving the encoding to console sizes can follow separately.

Please rerun ctest -R guest_ampr after these and note the result.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5680111872 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11727#pullrequestreview-5680111872). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-28T09:02:03Z. **Review:** CHANGES_REQUESTED --- Thanks, the previous points (AMM submit resolution, constructor cleanup, debt entry) are addressed. Remaining: - `Export.cpp:188-189`: `AppendNop` (and so `Nop`, `NopWithData`, `ConstructNop`) still returns `SCE_KERNEL_ERROR_EBUSY` at the original size, while every other append now grows. A buffer filled up to its bound by Nops behaves differently from the rest. Route it through `EnsureCommandCapacity`, with a test. - Growth is unbounded (up to 4 GiB, doubling) and is freed only if the guest calls a destructor, `ClearBuffer` or `SetBuffer`. Please state a cap (an append past it keeps returning `EBUSY`, which is also the console's contract for a full buffer that titles use to flush and submit) and give peak RSS on the MW3 load before/after in the PR (docs/dev/CONVENTIONS and the memory rules for caches/kept-alive allocations). - `Export.cpp:44`, `Api.hpp:156`: a test hook exported from the production library. Please use a seam that does not ship in the export surface (for example a test-only allocator override compiled into the test, or make the failure reachable through the cap above). - `Export.cpp:111-118`: `ResolveBufferBase` matches any registered original base. If the guest reuses that memory for another buffer without destroying the expanded one, a submit of the new buffer is redirected to the stale allocation. Add a test with a rebind of the same memory through a second buffer object, or key on the buffer as well. - Debt entry (`docs/dev/debt/libSceAmpr.md:8`): also record that a full buffer no longer reports `EBUSY` below the cap and that growth places commands in memory the guest did not allocate. On the design question: growth is acceptable as the stop-gap given the points above; moving the encoding to console sizes can follow separately. Please rerun `ctest -R guest_ampr` after these and note the result.
adriadam10 commented 2026-10-28 17:45:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T17:45:29Z.

Review: CHANGES_REQUESTED


Thanks, the Nop path, the 32 MiB cap, the removed export and the rebind handling look right. Remaining:

  • Export.cpp:46: if (required > MAX_EXPANDED_BUFFER_CAPACITY) return EBUSY runs before if (required <= buffer->size) return 0. A buffer the guest bound with more than 32 MiB now returns EBUSY for appends that fit in its own memory, which worked before this PR. Check the cap only when growth is needed (after the required <= buffer->size check). Add a test with a bound size above the cap.
  • Peak RSS: still missing. Please post peak RSS before/after on the MW3 load (or whichever scene hit the EBUSY), as asked in the previous round (docs/dev/CONVENTIONS, memory rules for kept-alive allocations). Also say how many buffers can be expanded at once, since the cap is per buffer.
  • The ENOMEM path (heap allocation or realloc failure leaves base/size/offset untouched) lost its test with the hook. If there is no seam that does not ship in the export surface, say so in the PR; otherwise please cover it.
  • Export.cpp:549: SetBuffer on a second object with the same memory releases the first object's expansion and resets its offset/numCommands to 0, dropping its recorded commands. Add that to the debt entry.
  • The PR comment lists guest_ampr_markers, guest_ampr_threads and guest_ampr_cpp_error; please confirm the first one ran on this head, since that is the one that holds the new tests.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5687163340 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11727#pullrequestreview-5687163340). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-28T17:45:29Z. **Review:** CHANGES_REQUESTED --- Thanks, the Nop path, the 32 MiB cap, the removed export and the rebind handling look right. Remaining: - `Export.cpp:46`: `if (required > MAX_EXPANDED_BUFFER_CAPACITY) return EBUSY` runs before `if (required <= buffer->size) return 0`. A buffer the guest bound with more than 32 MiB now returns `EBUSY` for appends that fit in its own memory, which worked before this PR. Check the cap only when growth is needed (after the `required <= buffer->size` check). Add a test with a bound size above the cap. - Peak RSS: still missing. Please post peak RSS before/after on the MW3 load (or whichever scene hit the `EBUSY`), as asked in the previous round (docs/dev/CONVENTIONS, memory rules for kept-alive allocations). Also say how many buffers can be expanded at once, since the cap is per buffer. - The `ENOMEM` path (heap allocation or realloc failure leaves `base`/`size`/`offset` untouched) lost its test with the hook. If there is no seam that does not ship in the export surface, say so in the PR; otherwise please cover it. - `Export.cpp:549`: `SetBuffer` on a second object with the same memory releases the first object's expansion and resets its `offset`/`numCommands` to 0, dropping its recorded commands. Add that to the debt entry. - The PR comment lists `guest_ampr_markers`, `guest_ampr_threads` and `guest_ampr_cpp_error`; please confirm the first one ran on this head, since that is the one that holds the new tests.
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!11727
No description provided.