XAudio2: refuse invalid decoded buffers before they reach the decoder #1210

Open
emirduran707 wants to merge 1 commit from upstream-pr/1219 into main
emirduran707 commented 2026-11-24 13:19:12 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by emirduran707 on 2026-11-24T13:19:12Z.


For a decoded source voice (XMA or Opus, played through a Windows PCM16 voice), submit_decoded_buffer decoded the title's buffer first and only then passed the PCM to Windows' SubmitSourceBuffer. When Windows refused it, only XCL's copy of the PCM was dropped, and the decoder kept its new state: XMA packets consumed, the overlap tail held, the initial skip used up, and on END_OF_STREAM a flush. The title gets the error back and submits the buffer again, which is then decoded against a stream that has already eaten it. XMA packets are fed twice, and an Opus end of stream is flushed twice.

Microsoft's documentation of SubmitSourceBuffer gives two refusals that can be seen before decoding: more than XAUDIO2_MAX_QUEUED_BUFFERS (64) buffers queued, and a LoopCount that is neither 1–XAUDIO2_MAX_LOOP_COUNT (254) nor XAUDIO2_LOOP_INFINITE (255). Windows' xaudio2_9 returns XAUDIO2_E_INVALID_CALL for both; the new test checks that on the unchanged path. These two are now refused before the decoder sees the buffer. The queue count comes from the voice's own GetState (XAUDIO2_VOICE_NOSAMPLESPLAYED), since XCL's buffer list shrinks only in OnBufferEnd.

What is in it

  • crates/xcl/src/hle/xaudio2.rs:
    • the two checks at the top of submit_decoded_buffer;
    • test refused_decoded_submit_leaves_the_decoder_untouched, which skips without an audio device. It covers a LoopCount 300 buffer, and a 65th buffer while 64 are queued. After each refusal, the next accepted buffer must decode to the same PCM as a decoder that never saw the refused one;
    • a test-only lock shared with xma2_source_voice_decodes_and_manages_lifetime, which counts the global XMA statistics.

Tested

  • AMD Radeon RX 9060 XT (driver 32.0.31044.16), Ryzen 5 5600, Windows 11 Pro 26300, Rust 1.100.0; tests debug, smoke release
  • cargo test --workspace -- --test-threads=4 on 4a01d21: 742 passed, 0 failed, 41 ignored; no SKIPPED GPU tests (the audio test ran; an audio device is present)
  • the new test without the checks fails ("a refused LoopCount 300 buffer reached the decoder"); with only the loop-count check it fails at the 65th buffer; with both, cargo test -p xcl --lib xaudio2 passes, 10 passed

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1541 (PresentX 1541)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19650 MB

this PR:

xcl-smoke v1
commit: 4a01d21172b965e323d98e4e2bf12342a8bea836
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1547 (PresentX 1547)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19615 MB

Not tested

  • A title that submits until it gets an error; the smoke title's run is a regression check only.
  • A buffer that ends on the audio thread between the queue check and the submit makes a 65th buffer refused that Windows would just have accepted. The title gets an error XAudio2 may return, and the decoder is untouched.
  • NVIDIA and Intel GPUs.

AI use

  • Claude Code with Claude Opus 5.5: found the problem, wrote the change, the test and this description; I reviewed the diff and ran the tests and smoke runs above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ivall/xcl:issue:5472913524 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1219). Originally posted by [emirduran707](https://github.com/emirduran707) on 2026-11-24T13:19:12Z. --- For a decoded source voice (XMA or Opus, played through a Windows PCM16 voice), `submit_decoded_buffer` decoded the title's buffer first and only then passed the PCM to Windows' `SubmitSourceBuffer`. When Windows refused it, only XCL's copy of the PCM was dropped, and the decoder kept its new state: XMA packets consumed, the overlap tail held, the initial skip used up, and on `END_OF_STREAM` a flush. The title gets the error back and submits the buffer again, which is then decoded against a stream that has already eaten it. XMA packets are fed twice, and an Opus end of stream is flushed twice. Microsoft's documentation of `SubmitSourceBuffer` gives two refusals that can be seen before decoding: more than `XAUDIO2_MAX_QUEUED_BUFFERS` (64) buffers queued, and a `LoopCount` that is neither 1–`XAUDIO2_MAX_LOOP_COUNT` (254) nor `XAUDIO2_LOOP_INFINITE` (255). Windows' xaudio2_9 returns `XAUDIO2_E_INVALID_CALL` for both; the new test checks that on the unchanged path. These two are now refused before the decoder sees the buffer. The queue count comes from the voice's own `GetState` (`XAUDIO2_VOICE_NOSAMPLESPLAYED`), since XCL's buffer list shrinks only in `OnBufferEnd`. **What is in it** - `crates/xcl/src/hle/xaudio2.rs`: - the two checks at the top of `submit_decoded_buffer`; - test `refused_decoded_submit_leaves_the_decoder_untouched`, which skips without an audio device. It covers a `LoopCount` 300 buffer, and a 65th buffer while 64 are queued. After each refusal, the next accepted buffer must decode to the same PCM as a decoder that never saw the refused one; - a test-only lock shared with `xma2_source_voice_decodes_and_manages_lifetime`, which counts the global XMA statistics. **Tested** - AMD Radeon RX 9060 XT (driver 32.0.31044.16), Ryzen 5 5600, Windows 11 Pro 26300, Rust 1.100.0; tests debug, smoke release - `cargo test --workspace -- --test-threads=4` on 4a01d21: 742 passed, 0 failed, 41 ignored; no SKIPPED GPU tests (the audio test ran; an audio device is present) - the new test without the checks fails ("a refused LoopCount 300 buffer reached the decoder"); with only the loop-count check it fails at the 65th buffer; with both, `cargo test -p xcl --lib xaudio2` passes, 10 passed **Regression run** main: ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: AMD Radeon RX 9060 XT title: retail-title seconds: 60 exit: 0x0 frames shown: 1541 (PresentX 1541) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 19650 MB ``` this PR: ``` xcl-smoke v1 commit: 4a01d21172b965e323d98e4e2bf12342a8bea836 gpu: AMD Radeon RX 9060 XT title: retail-title seconds: 60 exit: 0x0 frames shown: 1547 (PresentX 1547) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 19615 MB ``` **Not tested** - A title that submits until it gets an error; the smoke title's run is a regression check only. - A buffer that ends on the audio thread between the queue check and the submit makes a 65th buffer refused that Windows would just have accepted. The title gets an error XAudio2 may return, and the decoder is untouched. - NVIDIA and Intel GPUs. **AI use** - Claude Code with Claude Opus 5.5: found the problem, wrote the change, the test and this description; I reviewed the diff and ran the tests and smoke runs above. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DavidM1911 commented 2026-11-24 14:06:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T13:47:04Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib xaudio2: 10 passed, 0 failed.

Note: the full d3d12x lib filter currently aborts with 0xc0000005 on unmodified main too (pre-existing, unrelated to this PR); the PR's own tests were run by name and pass.

<!-- greyfinch-sync:ivall/xcl:comment:7133331450 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1219#issuecomment-7133331450). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T13:47:04Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - cargo test -p xcl --lib xaudio2: 10 passed, 0 failed. Note: the full `d3d12x` lib filter currently aborts with 0xc0000005 on unmodified `main` too (pre-existing, unrelated to this PR); the PR's own tests were run by name and pass.
DavidM1911 commented 2026-11-26 04:07:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T04:05:30Z.


Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

<!-- greyfinch-sync:ivall/xcl:comment:7182285680 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1219#issuecomment-7182285680). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T04:05:30Z. --- Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
github-actions[bot] commented 2026-11-30 08:42:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-30T08:42:38Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7273246300 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1219#issuecomment-7273246300). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-30T08:42:38Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
BBareth commented 2026-11-30 23:11:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T23:11:30Z.


merge-check claim
head: 4a01d21172

<!-- greyfinch-sync:ivall/xcl:comment:7294820852 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1219#issuecomment-7294820852). Originally posted by [BBareth](https://github.com/BBareth) on 2026-11-30T23:11:30Z. --- merge-check claim head: 4a01d21172b965e323d98e4e2bf12342a8bea836
BBareth commented 2026-11-30 23:19:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T23:19:48Z.


merge-check
base: 86e86fcb33
head: 4a01d21172
merge: clean
build: cargo build --workspace -> ok; the lib test target does not compile (below)
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> did not run: cargo test stops at error[E0308]: mismatched types in crates/xcl/src/hle/xaudio2.rs:1752 of the merge result, in the new test refused_decoded_submit_leaves_the_decoder_untouched: the closure pcm_of is declared -> Vec<u8> and returns b._pcm.to_vec(), which is Vec<i16> on current main. 0 tests ran. The base alone, same run: 2889 passed, 0 failed, 51 ignored, 28 SKIPPED lines (22 no wave64, 2 no heap windows, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 cannot disable the driver cache).
review: blocking: the merge is textually clean but the PR's own test no longer compiles against main, so the suite cannot run on the merge result (the PR's green CI is from an older base). The comparison two lines below (pcm_of(0x11) == want with want built as bytes) needs the same adjustment. The runtime part was read and looks right for what it says: submit_decoded_buffer returns XAUDIO2_E_INVALID_CALL for a LoopCount above 254 that is not 255, and when the voice already has 64 queued buffers, before the decoder sees the data; it asks the real voice with GetState(XAUDIO2_VOICE_NOSAMPLESPLAYED), one extra call per decoded submit. Not tested here for the reason above.
performance: one extra GetState call per submit to a decoded (XMA / Opus) voice; not measured.
verdict: not yet: the new test does not compile after merging into current main (xaudio2.rs:1752, Vec<i16> where Vec<u8> is declared); needs a rebase and a rerun
hardware / tool and model: RTX 5070 Ti 16 GB, driver 621.24 (32.0.16.2124); i7-12700K; Windows 11 26200. Claude Code, Claude Fable 5.2 read the diff, ran the build and the suite and wrote this check.

<!-- greyfinch-sync:ivall/xcl:comment:7294988516 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1219#issuecomment-7294988516). Originally posted by [BBareth](https://github.com/BBareth) on 2026-11-30T23:19:48Z. --- merge-check base: 86e86fcb33f94fa5d0c809d9b50af7b84bf49cce head: 4a01d21172b965e323d98e4e2bf12342a8bea836 merge: clean build: cargo build --workspace -> ok; the lib test target does not compile (below) tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> did not run: `cargo test` stops at `error[E0308]: mismatched types` in `crates/xcl/src/hle/xaudio2.rs:1752` of the merge result, in the new test `refused_decoded_submit_leaves_the_decoder_untouched`: the closure `pcm_of` is declared `-> Vec<u8>` and returns `b._pcm.to_vec()`, which is `Vec<i16>` on current main. 0 tests ran. The base alone, same run: 2889 passed, 0 failed, 51 ignored, 28 SKIPPED lines (22 no wave64, 2 no heap windows, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 cannot disable the driver cache). review: blocking: the merge is textually clean but the PR's own test no longer compiles against `main`, so the suite cannot run on the merge result (the PR's green CI is from an older base). The comparison two lines below (`pcm_of(0x11) == want` with `want` built as bytes) needs the same adjustment. The runtime part was read and looks right for what it says: `submit_decoded_buffer` returns `XAUDIO2_E_INVALID_CALL` for a `LoopCount` above 254 that is not 255, and when the voice already has 64 queued buffers, before the decoder sees the data; it asks the real voice with `GetState(XAUDIO2_VOICE_NOSAMPLESPLAYED)`, one extra call per decoded submit. Not tested here for the reason above. performance: one extra `GetState` call per submit to a decoded (XMA / Opus) voice; not measured. verdict: not yet: the new test does not compile after merging into current main (xaudio2.rs:1752, `Vec<i16>` where `Vec<u8>` is declared); needs a rebase and a rerun hardware / tool and model: RTX 5070 Ti 16 GB, driver 621.24 (32.0.16.2124); i7-12700K; Windows 11 26200. Claude Code, Claude Fable 5.2 read the diff, ran the build and the suite and wrote this check.
Foniass commented 2026-12-01 05:09:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:09:30Z.


Triage (#1607) – head 4a01d21

No open or merged PR makes the same change: main's submit_decoded_buffer (hle/xaudio2.rs) still decodes before XAudio2 can refuse the buffer, and #1698 only uses the 64-buffer limit for its GetState context queue. The 38 added lines that also appear on main are test boilerplate (engine/voice setup), not the fix.
Minor: the queue-depth check reads GetState and then submits, so two threads submitting to the same voice concurrently could still both pass at 63 queued; harmless for single-threaded submitters.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7301479261 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1219#issuecomment-7301479261). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:09:30Z. --- **Triage (#1607)** – head 4a01d21 No open or merged PR makes the same change: main's `submit_decoded_buffer` (hle/xaudio2.rs) still decodes before XAudio2 can refuse the buffer, and #1698 only uses the 64-buffer limit for its GetState context queue. The 38 added lines that also appear on main are test boilerplate (engine/voice setup), not the fix. Minor: the queue-depth check reads `GetState` and then submits, so two threads submitting to the same voice concurrently could still both pass at 63 queued; harmless for single-threaded submitters. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1219:upstream-pr/1219'
git switch 'upstream-pr/1219'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1219'
git switch 'upstream-pr/1219'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1219'
git switch 'upstream-pr/1219'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1219'
git switch 'main'
git merge --squash 'upstream-pr/1219'
git switch 'main'
git merge --ff-only 'upstream-pr/1219'
git switch 'main'
git merge 'upstream-pr/1219'
git push origin 'main'
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
xcl-dev/xcl!1210
No description provided.