Skip to content

XAudio2: GetState keeps the title's context for a buffer that just ended - #2410

Open
treybertram06 wants to merge 1 commit into
xcl-dev:mainfrom
treybertram06:xaudio2-getstate-ended-context
Open

treybertram06 wants to merge 1 commit into
xcl-dev:mainfrom
treybertram06:xaudio2-getstate-ended-context

Conversation

@treybertram06

@treybertram06 treybertram06 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

On a decoded (XMA / Opus) source voice, XCL submits each buffer with its own token as the context and maps it back to the title's context in GetState. If the buffer ends between XAudio2's GetState and XCL's lookup, OnBufferEnd has already removed the token from live_buffers. The title then gets the token, which is the address of a freed DecodedBuffer, as pCurrentBufferContext. Fixes #2386.

Each voice now keeps its last 64 ended tokens together with the title's context. OnBufferEnd records a token under the live_buffers lock, so a lookup always finds the token in one of the two. The issue suggests clearing the context to null on a miss. That would also clear the context of a buffer the title queued itself, which XCL never wraps, so a context XCL didn't make still passes through unchanged.

What is in it

  • crates/xcl/src/hle/xaudio2.rs:
    • end_buffer and title_context (live tokens, then recently ended ones), used by decoded_cb_buffer_end and get_state;
    • the per-voice ended ring. It holds each ended buffer's allocation, with its PCM freed, so a later buffer can't get the address of a token that's still kept (the ABA race from review);
    • the unit tests get_state_context_survives_buffer_end, which fails without the ring because the token comes back, and ended_token_is_not_reused_by_a_later_buffer, which fails when the ring keeps only the address.

Tested

  • RTX 3080 10 GB (driver 621.40 / 32.0.16.2140), Ryzen 9 5900X, Windows 11 build 26300, Rust 1.100.0, debug tests and release smoke builds.
  • cargo test -p xcl --lib xaudio2: 11 passed, 0 failed. Each new test fails without the part of the change it covers.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 (XCL_DXC_DIR set to DXC 1.9), on the branch rebased onto main e0d93c84: 1423 passed, 0 failed, 46 ignored.
  • 25 SKIPPED:

Regression run
The title used here decodes its audio through XAPU, so these runs do not reach the changed code (see Not tested).

xcl-smoke v1
commit: 68a284a596bd558b6bd9db6eedda6d6aa178c360
gpu: NVIDIA GeForce RTX 3080
title: 4ba6c38e
seconds: 60
exit: 0x0
frames shown: 1497 (PresentX 1497)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15658 MB
xcl-smoke v1
commit: 4767b1a3a3506e6597044152996c5b1ec1ebe01d
gpu: NVIDIA GeForce RTX 3080
title: 4ba6c38e
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14756 MB

Not tested

  • The race itself in a title. I don't have a title that queries GetState on an XMA / Opus voice while its buffers end, and the title in the regression run uses the XAPU path. The unit test covers the lookup after OnBufferEnd, not XAudio2's own threads.
  • AMD and Intel GPUs. The change is CPU-only.

AI use

  • Claude Code with Claude Opus 5.5. It read the issue and the code, wrote the fix, the test and this description, and ran the tests and the smoke runs above. The author reviewed the diff.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

  • cargo test -p xcl --lib: 1 real failure, deterministic (also fails --test-threads=1, 1219/1):
gpu::recomp::lower_dxil::tests::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier
  assertion failed: group barriers in the program (lower_dxil.rs:1155)

The lowered program is missing an expected group barrier (or emits one where none is expected). Needs a fix before merge.

— Devin

@ahmetbagci8

Copy link
Copy Markdown

Tested

  • Commit: adf3f635be1eec43e0bf30383ffcb496fbdb5650
  • Hardware / OS: Intel Core i7-11800H, NVIDIA GeForce RTX 3070 Laptop GPU (driver 32.0.16.1714), Windows 11 Pro (Build 26300), DXC v1.9.2609
  • cargo test -j 2 -p xcl --lib xaudio2 -- --test-threads=4: 10 passed; 0 failed; 0 ignored (0.12s)

Not tested

  • Full workspace suite and scripts/smoke.ps1 (no unpacked title locally).

AI use

  • Antigravity harness (Gemini 4 Neon) ran the targeted unit tests on this commit and drafted this report; the author reviewed it before posting.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 25, 2026
…#2414, xcl-dev#2415

Integrates per-draw mirror sync fast path (xcl-dev#2437), PresentX compositor
hardening (xcl-dev#2418), XAudio2 GetState buffer context fix (xcl-dev#2410), and Wine
test fixes (xcl-dev#2414, xcl-dev#2415).

Assisted-by: Antigravity (Gemini 4 Neon)
@treybertram06

Copy link
Copy Markdown
Contributor Author

Thanks for running it. That failure isn't from this PR. a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier came in with 254a058 on main, and main's own CI failed on it until #2420 (c828d8c) changed the test. The PR's CI run is red for the same reason.

On main at 06055a7, which includes c828d8c, with this branch (adf3f63) merged on top, on an RTX 3080:

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

The diff only touches hle/xaudio2.rs. If your checkout of main predates c828d8c, merging the current main should clear it. I'll rebase the branch onto main so CI runs again.

(Checked and written with Claude Code, Claude Opus 5.5.)

@pablogonz12

Copy link
Copy Markdown
Contributor

Test report from Linux + Wine on AMD: this change does not fix #2386 on this lane, and the assertion it targets is reproducible here exactly. Prepared with AI assistance: DeepSeek V4.1 Flash in OpenCode; the commands were run by it and the output is pasted verbatim.

Setup

  • CachyOS (Arch), kernel 7.3.6-1-cachyos-eevdf, Mesa 26.3.1-5 (RADV GFX1201), AMD Radeon RX 9070 XT 16 GB, Ryzen 9 9900X.
  • GE-Proton11-14 wine (Wine 11.0), scratch prefix, WINEDLLOVERRIDES="d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b", VKD3D_CONFIG=ignore_shared_fence, WINEDEBUG=-all, DXC 1.9.2609.5.
  • Build: RUSTFLAGS="-C target-feature=+crt-static" cargo xwin test --workspace --release --target x86_64-pc-windows-msvc --no-run; run wine <xcl lib exe> --test-threads=1 xaudio2 and wine <e2e exe> --test-threads=1.
  • Base = main 2898511 with this PR merged on top (clean merge, only this PR differs), head = adf3f63.

Result

base 2898511 this PR adf3f63
xaudio2 (lib filter) FAILED: 8 passed, 1 failed FAILED: 9 passed, 1 failed
e2e (integration) FAILED: 3 passed, 1 failed FAILED: 3 passed, 1 failed

The new test passes (get_state_context_survives_buffer_end is the +1). The failure that matters is unchanged, and it is not the ended-buffer race: GetState returns a null context at the first call, while both buffers are still queued.

Base, crates/xcl/src/hle/xaudio2.rs:1599:

thread 'hle::xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime' (856) panicked at crates/xcl/src/hle/xaudio2.rs:1599:13:
assertion `left == right` failed
  left: 0
 right: 12291

Same assertion on this PR (line 1626 after the added code), same values: left: 0, right: 12291 (0x3003 = CTX_VALID). assert_eq!({ st.buffers_queued }, 2) passes just above it, so the context is lost between SubmitSourceBuffer and GetState, not at OnBufferEnd.

The e2e test title shows the same thing, on base and on this PR alike:

[FAIL] XAudio2: console flags/device, XMA2 & Opus voices decoded, PCM passes, callbacks: XMA2 GetState: ctx=0x0, queued=1

(its other [FAIL] on this lane is the window-cloak check, which Wine cannot satisfy). So the ring for ended tokens does not cover what happens here: on Wine the context is already null before any buffer ends. Two ways that could be Wine-only, worth ruling out before this is read as a Windows bug: no audio device in the prefix (playback may complete immediately), or the decoded path ending the buffer during Submit.

Not tested

  • No game run.
  • Windows (whether the same first-GetState null happens there is what would decide if this is a Wine-only difference).

AI use

  • DeepSeek V4.1 Flash (OpenCode) built both commits, ran the tests above and wrote this report; no code was written or changed by it.

@treybertram06

Copy link
Copy Markdown
Contributor Author

Thanks, that's useful, but it's a different problem from #2386. The failing test, xma2_source_voice_decodes_and_manages_lifetime, fails the same way on your base without this PR, and it passes on Windows: 10/10 xaudio2 tests on an RTX 3080 (here) and on a 3070 (ahmetbagci8 above). As you say, the context is null at the first GetState, before any buffer ends, so the ended-token ring isn't involved. That looks like Wine's XAudio2 (FAudio) not reporting pCurrentBufferContext for a voice that hasn't been started; Windows' XAudio2 reports the head buffer's context there. It could be worth its own issue for the Wine lane. This PR only covers the race in #2386: a token read just before OnBufferEnd.

(Claude Code, Claude Opus 5.5.)

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 25, 2026
…#2414, xcl-dev#2415

Integrates per-draw mirror sync fast path (xcl-dev#2437), PresentX compositor
hardening (xcl-dev#2418), XAudio2 GetState buffer context fix (xcl-dev#2410), and Wine
test fixes (xcl-dev#2414, xcl-dev#2415).

Assisted-by: Antigravity (Gemini 4 Neon)

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 ABA bug: end_buffer removes and drops Box while ended retains only (token, orig_context). An in-flight GetState can read A token, audio thread drops A and invokes title callback, callback submits B whose allocation reuses A address, then the hook resolves stale A token through live_buffers and returns B context. Keep ended DecodedBuffers/token reservations alive through eviction and add a forced-address-reuse race test. The fixed 64-entry retention also means sufficiently delayed lookup loses the mapping.

A decoded (XMA / Opus) voice submits each buffer with XCL's own token as
the context and maps it back to the title's in GetState. When the buffer
ends between XAudio2's GetState and XCL's lookup, OnBufferEnd has already
taken the token out of live_buffers, and the title got the token, the
address of a freed DecodedBuffer, as pCurrentBufferContext (xcl-dev#2386).

Each voice now keeps its last 64 ended buffers, their PCM freed; the end
of a buffer moves it there under the live_buffers lock, so a lookup finds
the token in one of the two. Keeping the allocation, not only its address,
holds the token: a buffer submitted after it cannot get the same address
while a stale GetState token can still name it (the ABA race in review).
A context XCL never made (a buffer the title queued itself) still passes
through unchanged: clearing it on a miss, as the issue suggests, would
drop those.

The tests fail without the ended buffers (the token comes back) and
without holding their allocation.

Fixes xcl-dev#2386

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@treybertram06
treybertram06 force-pushed the xaudio2-getstate-ended-context branch from ae0019c to 4767b1a Compare November 25, 2026 21:49
@treybertram06

Copy link
Copy Markdown
Contributor Author

@bmjask Thanks, that's right. Fixed in 4767b1a, squashed into the commit.

  • ended now holds the ended DecodedBuffers themselves, not (token, context) copies. Their PCM is freed, but the allocation stays in the ring until it is evicted. While a token can still be looked up, no buffer submitted after it can get that address. A stale token from a GetState that raced OnBufferEnd therefore resolves to its own buffer's context, never a newer one's. Voice teardown clears the ring along with live_buffers.
  • New test ended_token_is_not_reused_by_a_later_buffer: it ends A, submits more buffers as an OnBufferEnd callback would, and checks two things. The ring holds A's own allocation, and no later buffer gets A's address. Whether the heap would hand a freed address straight back is up to the allocator, so the hold is asserted directly. With the ring keeping copies instead of the allocations, the test fails on that assertion.
  • On the 64-entry limit: a lookup delayed past 64 later buffer ends on the same voice still loses the mapping, and its context then passes through unchanged. Past that point the address can be reused, so it can't be told apart. Holding the reservations for longer would need a time-based retention or an unbounded ring. I think 64 buffer ends is well beyond the window between GetState reading the context and the hook's lookup, but I can make it time-based if you'd rather.

Tested on 4767b1a (RTX 3080, driver 621.40, Windows 11 26300):

  • cargo test -p xcl --lib xaudio2: 11 passed, 0 failed.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 1423 passed, 0 failed, 46 ignored.
  • The smoke block in the description is updated.

(Claude Code, Claude Opus 5.5.)

@DavidM1911

Copy link
Copy Markdown
Contributor

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

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XAudio2: GetState race leaks freed internal DecodedBuffer pointer in pCurrentBufferContext

5 participants