fix(libSceAudioOut): reject oversized AudioOut2 grains at context creation #14997

Merged
MarcZX10 merged 9 commits from upstream-pr/14997 into main 2026-11-10 19:27:06 +00:00
MarcZX10 commented 2026-11-02 08:57:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T08:57:52Z.


What

sceAudioOut2ContextCreate (core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp) accepted any num_grains from the guest. The mix buffer is grain * 2 floats, so a huge value made the assign throw std::bad_alloc. The exception escaped the extern "C" entry point and the context allocated just before it leaked.

The fix returns SCE_AUDIO_OUT2_ERROR_INVALID_ARGUMENT for num_grains > MAX_GRAIN before any allocation, and holds the new context in a std::unique_ptr until creation succeeds.

Bound: MAX_GRAIN = AUDIO_OUT2_SAMPLE_RATE * MAX_QUEUED_MS / 1000 = 12000 frames (250 ms at 48 kHz). MAX_QUEUED_MS is the existing cap on pending queue time; the push path drops grains beyond it, so a grain longer than the cap is outside what the queue is built to carry. Existing callers use 256 (the default) and 1024 (AudioOut2Timing), both below the bound. The bound is chosen from the file's own constants, not from a console measurement.

Commits (each one compiles on its own):

  1. fix: add the MAX_GRAIN constant (unused until commit 2; the build does not use -Werror).
  2. fix: reject num_grains > MAX_GRAIN before allocation.
  3. fix: hold the context in a std::unique_ptr until success.
  4. test: OversizedGrainIsRefused checks num_grains = 0xFFFFFFFF returns SCE_AUDIO_OUT2_ERROR_INVALID_ARGUMENT and leaves the handle unwritten.
  5. test: same check for num_grains = 12001.
  6. test: num_grains = 12000 (the bound) creates and destroys a context.

Tested

  • Linux, archive build with run_tests.sh, target audio_out2_context_handles_tests, ctest audio_out2_context_handles on head 8f251ae886: BUILD-EXIT 0, "1/1 Test #2068: audio_out2_context_handles ....... Passed 0.10 sec", "100% tests passed, 0 tests failed out of 1" (VERIFY-FAILED line is the known host limitation in unrelated files).
  • The test now covers an unreadable params pointer (PROT_NONE page) and an unwritable ctx slot (read-only page): both return SCE_AUDIO_OUT2_ERROR_INVALID_ARGUMENT, and a later create still works. These cases were run only against the fixed code; they were not run against the unfixed code.
  • Negative control for the earlier oversized-grain fix: with the fix reverted in an archived copy of the tree, the same test binary aborts with std::bad_alloc (exit 134). With the fix it passes.
  • Not run: the other AudioOut2 tests (audio_out2_timing, audio_out2_device_state, port tests) and Windows/macOS.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5117617579 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14997). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T08:57:52Z. --- ### What `sceAudioOut2ContextCreate` (`core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp`) accepted any `num_grains` from the guest. The mix buffer is `grain * 2` floats, so a huge value made the `assign` throw `std::bad_alloc`. The exception escaped the `extern "C"` entry point and the context allocated just before it leaked. The fix returns `SCE_AUDIO_OUT2_ERROR_INVALID_ARGUMENT` for `num_grains > MAX_GRAIN` before any allocation, and holds the new context in a `std::unique_ptr` until creation succeeds. Bound: `MAX_GRAIN = AUDIO_OUT2_SAMPLE_RATE * MAX_QUEUED_MS / 1000` = 12000 frames (250 ms at 48 kHz). `MAX_QUEUED_MS` is the existing cap on pending queue time; the push path drops grains beyond it, so a grain longer than the cap is outside what the queue is built to carry. Existing callers use 256 (the default) and 1024 (`AudioOut2Timing`), both below the bound. The bound is chosen from the file's own constants, not from a console measurement. Commits (each one compiles on its own): 1. `fix`: add the `MAX_GRAIN` constant (unused until commit 2; the build does not use `-Werror`). 2. `fix`: reject `num_grains > MAX_GRAIN` before allocation. 3. `fix`: hold the context in a `std::unique_ptr` until success. 4. `test`: `OversizedGrainIsRefused` checks `num_grains = 0xFFFFFFFF` returns `SCE_AUDIO_OUT2_ERROR_INVALID_ARGUMENT` and leaves the handle unwritten. 5. `test`: same check for `num_grains = 12001`. 6. `test`: `num_grains = 12000` (the bound) creates and destroys a context. ### Tested - Linux, archive build with `run_tests.sh`, target `audio_out2_context_handles_tests`, ctest `audio_out2_context_handles` on head `8f251ae886`: BUILD-EXIT 0, "1/1 Test #2068: audio_out2_context_handles ....... Passed 0.10 sec", "100% tests passed, 0 tests failed out of 1" (VERIFY-FAILED line is the known host limitation in unrelated files). - The test now covers an unreadable `params` pointer (PROT_NONE page) and an unwritable `ctx` slot (read-only page): both return `SCE_AUDIO_OUT2_ERROR_INVALID_ARGUMENT`, and a later create still works. These cases were run only against the fixed code; they were not run against the unfixed code. - Negative control for the earlier oversized-grain fix: with the fix reverted in an archived copy of the tree, the same test binary aborts with `std::bad_alloc` (exit 134). With the fix it passes. - Not run: the other AudioOut2 tests (`audio_out2_timing`, `audio_out2_device_state`, port tests) and Windows/macOS. ### 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
mashingaan commented 2026-11-02 10:22:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mashingaan on 2026-11-02T10:22:58Z.

Review: COMMENTED


The new bound check dereferences params->num_grains directly before any guest-memory validation, and the success path still writes *ctx directly. A non-null invalid params pointer can still crash before the oversized-grain guard, while an invalid output ctx can leak the context inserted into g_contexts after context.release(). Could this copy in the parameter struct first and safely copy out the handle before transferring ownership, with bad-pointer coverage?

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5735094938 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14997#pullrequestreview-5735094938). Originally posted by [mashingaan](https://github.com/mashingaan) on 2026-11-02T10:22:58Z. **Review:** COMMENTED --- The new bound check dereferences `params->num_grains` directly before any guest-memory validation, and the success path still writes `*ctx` directly. A non-null invalid `params` pointer can still crash before the oversized-grain guard, while an invalid output `ctx` can leak the context inserted into `g_contexts` after `context.release()`. Could this copy in the parameter struct first and safely copy out the handle before transferring ownership, with bad-pointer coverage?
mashingaan commented 2026-11-02 11:30:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mashingaan on 2026-11-02T11:30:11Z.

Review: DISMISSED


The remaining guest-pointer handling issue is still present at head ffd7c567660a718d7afd6d28711d7218147bde10. params is dereferenced directly before the new size guard, and *ctx is written directly after inserting and releasing the context. An unreadable non-null params can still crash the host. An unreadable non-null ctx can fault after ownership has been released while the raw pointer remains registered in g_contexts. Please copy in the parameter structure and safely copy out the handle before committing ownership, with bad-pointer coverage.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5735876198 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14997#pullrequestreview-5735876198). Originally posted by [mashingaan](https://github.com/mashingaan) on 2026-11-02T11:30:11Z. **Review:** DISMISSED --- The remaining guest-pointer handling issue is still present at head ffd7c567660a718d7afd6d28711d7218147bde10. `params` is dereferenced directly before the new size guard, and `*ctx` is written directly after inserting and releasing the context. An unreadable non-null `params` can still crash the host. An unreadable non-null `ctx` can fault after ownership has been released while the raw pointer remains registered in `g_contexts`. Please copy in the parameter structure and safely copy out the handle before committing ownership, with bad-pointer coverage.
adriadam10 commented 2026-11-06 22:28:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-11-06T22:28:03Z.

Review: CHANGES_REQUESTED


The guest-pointer points from mashingaan are met at this head (GuestCopyIn/GuestCopyOut, device closed in the deleter, bad-pointer test), and the tests fail without the fix. One point from oneandonlydean is still open:

  • core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp:343 (MAX_GRAIN, line 39): the 12000-frame cap is derived from the host's push-path backpressure (MAX_QUEUED_MS), not from the console's contract. It rejects sizes that worked before (12001, 16384), and AudioOut2ContextHandles.cpp:119-122 pins 12000/12001 as if it were a console fact. Either cite the console's accepted-grain limit and error code, or drop the derived limit and handle allocation failure instead (catch std::bad_alloc around the mix.assign and return an error with the context cleaned up by the unique_ptr; the test then needs a size that fails to allocate rather than a boundary). If you keep a cap as an implementation limit, add a line to docs/dev/debt/libSceAudioOut.md saying it comes from the host queue limit and that the console's maximum and its error code are unverified, and make the test comment-free about it being a console boundary.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5807871685 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14997#pullrequestreview-5807871685). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-11-06T22:28:03Z. **Review:** CHANGES_REQUESTED --- The guest-pointer points from mashingaan are met at this head (`GuestCopyIn`/`GuestCopyOut`, device closed in the deleter, bad-pointer test), and the tests fail without the fix. One point from oneandonlydean is still open: - `core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp:343` (`MAX_GRAIN`, line 39): the 12000-frame cap is derived from the host's push-path backpressure (`MAX_QUEUED_MS`), not from the console's contract. It rejects sizes that worked before (12001, 16384), and `AudioOut2ContextHandles.cpp:119-122` pins 12000/12001 as if it were a console fact. Either cite the console's accepted-grain limit and error code, or drop the derived limit and handle allocation failure instead (catch `std::bad_alloc` around the `mix.assign` and return an error with the context cleaned up by the `unique_ptr`; the test then needs a size that fails to allocate rather than a boundary). If you keep a cap as an implementation limit, add a line to `docs/dev/debt/libSceAudioOut.md` saying it comes from the host queue limit and that the console's maximum and its error code are unverified, and make the test comment-free about it being a console boundary.
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!14997
No description provided.