fix(ajm): preserve live decoder handles across ID wrap #6051

Merged
Ondra97 merged 1 commit from upstream-pr/6051 into main 2026-10-17 04:10:21 +00:00
Ondra97 commented 2026-10-14 20:54:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-14T20:54:08Z.


What

Creating a decoder can replace a live AJM instance when the 14-bit slot counter wraps. On main, keeping two initialized Opus decoders while creating and destroying temporary decoders reuses a retained handle after 16,383 temporary creations. Its next empty-input job reports not-initialized and writes 0 bytes, while the retained reference decoder drains the expected 3328 bytes of PCM.

sceAjmInstanceCreate now searches the existing slot namespace under g_lock and inserts only at a free ID. A full namespace returns the existing OUT_OF_RESOURCES result and leaves the caller's output untouched. The codec prefix and slot zero are preserved. The exhaustion result follows the PS4 reimplementation linked in technical debt; the runtime's slot count and PS5 capacity/result are explicitly distinguished there.

Tested

Windows 11 Pro build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT POSIX SEH r7, Release, BUILD_TESTING=ON, full build-runtime configuration.

  • Main 03ab6c494, unchanged production: standalone public-API reproduction loses pending PCM after the duplicate handle (94 ms). The new registered guest_ajm regression fails with AJM creation replaced an existing live decoder handle (0.21 s).
  • The regression keeps two initialized Opus decoders with pending PCM through 32,768 temporary create/destroy calls, fills every slot uniquely, checks exhaustion with an unchanged sentinel, admits another codec, frees and reuses the sole available slot, then compares the exact pending PCM and the next packet against the reference decoder. It verifies input consumption and cumulative sample counts and completes in 0.07 s locally.
  • Full Windows build passed before and after rebase onto 41cc655b8. The final 33 audio/runtime tests and patched-library audit passed 33/33 in 5.49 s, without skips. Conventions, whitespace, test registration and the repository overlap checker passed.
  • I manually compared the exact implementation and tests in #5517 (mapping family 255), #6040 (gapless resets), #5658 (ATRAC9 configuration) and #6044 (test relocation). #6044's AJM test is a byte-identical rename. None implements this allocation fix. Full file inventories of older large PRs were checked, including beyond the first 100 files.
  • Two independent agent reviews found no actionable issue in the allocation, decoder-state regression or documented verification limits.
  • Retail games, Linux, ASan and PS5 hardware were not tested. No title unblock or measured console instance capacity is claimed.

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
  • Unblocks: none
  • GPUs tested: n/a (AJM decoder allocation only)
  • AI-assisted: yes (Codex); I reviewed the change and ran the local validation.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4864385243 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6051). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-14T20:54:08Z. --- ### What Creating a decoder can replace a live AJM instance when the 14-bit slot counter wraps. On main, keeping two initialized Opus decoders while creating and destroying temporary decoders reuses a retained handle after 16,383 temporary creations. Its next empty-input job reports not-initialized and writes 0 bytes, while the retained reference decoder drains the expected 3328 bytes of PCM. `sceAjmInstanceCreate` now searches the existing slot namespace under `g_lock` and inserts only at a free ID. A full namespace returns the existing `OUT_OF_RESOURCES` result and leaves the caller's output untouched. The codec prefix and slot zero are preserved. The exhaustion result follows the PS4 reimplementation linked in technical debt; the runtime's slot count and PS5 capacity/result are explicitly distinguished there. ### Tested Windows 11 Pro build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT POSIX SEH r7, Release, `BUILD_TESTING=ON`, full `build-runtime` configuration. - Main `03ab6c494`, unchanged production: standalone public-API reproduction loses pending PCM after the duplicate handle (94 ms). The new registered `guest_ajm` regression fails with `AJM creation replaced an existing live decoder handle` (0.21 s). - The regression keeps two initialized Opus decoders with pending PCM through 32,768 temporary create/destroy calls, fills every slot uniquely, checks exhaustion with an unchanged sentinel, admits another codec, frees and reuses the sole available slot, then compares the exact pending PCM and the next packet against the reference decoder. It verifies input consumption and cumulative sample counts and completes in 0.07 s locally. - Full Windows build passed before and after rebase onto `41cc655b8`. The final 33 audio/runtime tests and patched-library audit passed 33/33 in 5.49 s, without skips. Conventions, whitespace, test registration and the repository overlap checker passed. - I manually compared the exact implementation and tests in #5517 (mapping family 255), #6040 (gapless resets), #5658 (ATRAC9 configuration) and #6044 (test relocation). #6044's AJM test is a byte-identical rename. None implements this allocation fix. Full file inventories of older large PRs were checked, including beyond the first 100 files. - Two independent agent reviews found no actionable issue in the allocation, decoder-state regression or documented verification limits. - Retail games, Linux, ASan and PS5 hardware were not tested. No title unblock or measured console instance capacity is claimed. ### Checklist - [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] Unblocks: none - [x] GPUs tested: n/a (AJM decoder allocation only) - [x] AI-assisted: yes (Codex); I reviewed the change and ran the local validation.
coltken95 commented 2026-10-15 00:41:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-15T00:41:38Z.


Tested on Linux x86-64 (Release build), with e60dd9d5a merged into main ab9a47684:

  • With the PR: guest_ajm passes.
  • With main's libSceAjm.native/src/Ajm.cpp: guest_ajm aborts with terminate called after throwing an instance of 'std::runtime_error' what(): AJM creation replaced an existing live decoder handle.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6181697136 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6051#issuecomment-6181697136). Originally posted by [coltken95](https://github.com/coltken95) on 2026-10-15T00:41:38Z. --- Tested on Linux x86-64 (Release build), with e60dd9d5a merged into main ab9a47684: - With the PR: `guest_ajm` passes. - With main's `libSceAjm.native/src/Ajm.cpp`: `guest_ajm` aborts with `terminate called after throwing an instance of 'std::runtime_error' what(): AJM creation replaced an existing live decoder handle`. AI-assisted: yes (Claude Code)
github-actions[bot] commented 2026-10-15 12:13:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:13:54Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6721 docs/dev/debt/libSceAjm.native.md L4

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6044 core/libs/tests/GuestAjm.cpp
PR 6841 core/libs/prx/libSceAjm.native/src/Ajm.cpp
core/libs/tests/GuestAjm.cpp
docs/dev/debt/libSceAjm.native.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191780969 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6051#issuecomment-6191780969). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:13:54Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 6721](https://www.github.com/boykopovar/AnyPS5/pull/6721) | `docs/dev/debt/libSceAjm.native.md` [L4](https://github.com/boykopovar/AnyPS5/blob/aeb8f62b865d893b771b6946084b7cdbad4e0ffd/docs/dev/debt/libSceAjm.native.md#L4) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestAjm.cpp` | | [PR 6841](https://www.github.com/boykopovar/AnyPS5/pull/6841) | `core/libs/prx/libSceAjm.native/src/Ajm.cpp`<br>`core/libs/tests/GuestAjm.cpp`<br>`docs/dev/debt/libSceAjm.native.md` |
adriadam10 commented 2026-10-16 21:20:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T21:20:56Z.


Checked on Linux: the build and scoped ctest (37 tests) pass, including guest_ajm. With the Ajm.cpp change reverted, guest_ajm fails. The merge with current main is clean. The new OUT_OF_RESOURCES behaviour is documented as unverified on PS5 in docs/dev/debt/libSceAjm.native.md. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6224928109 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6051#issuecomment-6224928109). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T21:20:56Z. --- Checked on Linux: the build and scoped ctest (37 tests) pass, including `guest_ajm`. With the `Ajm.cpp` change reverted, `guest_ajm` fails. The merge with current `main` is clean. The new `OUT_OF_RESOURCES` behaviour is documented as unverified on PS5 in `docs/dev/debt/libSceAjm.native.md`. Merging.
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!6051
No description provided.