XAudio2: GetState race leaks freed internal DecodedBuffer pointer in pCurrentBufferContext #2386

Open
opened 2026-11-26 05:29:47 +00:00 by github-bot · 2 comments
Owner

@⁠ahmetbagci8 on GitHub · 2026-11-25 17:39 UTC · original

Summary

In crates/xcl/src/hle/xaudio2.rs, submit_decoded_buffer submits each decoded buffer with the internal Box<DecodedBuffer> heap address (token) as XAUDIO2_BUFFER::pContext and stores it in voice_state.live_buffers.

When the title calls IXAudio2SourceVoice::GetState (get_state, lines 1138-1157):

orig(this, state_out, flags);
if !state_out.is_null() && DECODED_LIVE_COUNT.load(Ordering::Acquire) != 0 {
    let voice_state = DECODED_VOICES
        .lock()
        .unwrap()
        .as_ref()
        .and_then(|m| m.get(&(this as usize)).map(|h| Arc::clone(&h.state)));
    if let Some(voice_state) = voice_state {
        let cur = (*state_out).current_buffer_context as usize;
        if cur != 0 {
            if let Some(buf) = voice_state.live_buffers.lock().unwrap().get(&cur) {
                (*state_out).current_buffer_context = buf.orig_context as *mut c_void;
            }
        }
    }
}

If XAudio2's audio thread completes that buffer and runs decoded_cb_buffer_end (lines 611-617, which removes token from live_buffers and drops the Box<DecodedBuffer>) between orig(this, state_out, flags) and voice_state.live_buffers.lock(), then live_buffers.get(&cur) returns None. get_state leaves (*state_out).current_buffer_context set to the freed internal DecodedBuffer pointer instead of the title's orig_context or null, causing a use-after-free / type-confusion crash if the title dereferences pCurrentBufferContext.

Suggested fix

Clear current_buffer_context to null when the token is no longer in live_buffers (or look it up in a small recently-ended ring):

if cur != 0 {
    (*state_out).current_buffer_context = voice_state
        .live_buffers
        .lock()
        .unwrap()
        .get(&cur)
        .map(|buf| buf.orig_context as *mut c_void)
        .unwrap_or(std::ptr::null_mut());
}

Not tested

  • Read from commit 082944135cd2b96288470a595861fa608eee08dc without a new runtime test.

AI use

  • Antigravity harness (Gemini 4 Neon) audited xaudio2.rs and drafted this report; the author reviewed it before posting.
**@⁠ahmetbagci8** on GitHub · 2026-11-25 17:39 UTC · [original](https://github.com/xcl-dev/xcl/issues/2386) ### Summary In `crates/xcl/src/hle/xaudio2.rs`, `submit_decoded_buffer` submits each decoded buffer with the internal `Box<DecodedBuffer>` heap address (`token`) as `XAUDIO2_BUFFER::pContext` and stores it in `voice_state.live_buffers`. When the title calls `IXAudio2SourceVoice::GetState` (`get_state`, lines 1138-1157): ```rust orig(this, state_out, flags); if !state_out.is_null() && DECODED_LIVE_COUNT.load(Ordering::Acquire) != 0 { let voice_state = DECODED_VOICES .lock() .unwrap() .as_ref() .and_then(|m| m.get(&(this as usize)).map(|h| Arc::clone(&h.state))); if let Some(voice_state) = voice_state { let cur = (*state_out).current_buffer_context as usize; if cur != 0 { if let Some(buf) = voice_state.live_buffers.lock().unwrap().get(&cur) { (*state_out).current_buffer_context = buf.orig_context as *mut c_void; } } } } ``` If XAudio2's audio thread completes that buffer and runs `decoded_cb_buffer_end` (lines 611-617, which removes `token` from `live_buffers` and drops the `Box<DecodedBuffer>`) between `orig(this, state_out, flags)` and `voice_state.live_buffers.lock()`, then `live_buffers.get(&cur)` returns `None`. `get_state` leaves `(*state_out).current_buffer_context` set to the freed internal `DecodedBuffer` pointer instead of the title's `orig_context` or null, causing a use-after-free / type-confusion crash if the title dereferences `pCurrentBufferContext`. ### Suggested fix Clear `current_buffer_context` to null when the token is no longer in `live_buffers` (or look it up in a small recently-ended ring): ```rust if cur != 0 { (*state_out).current_buffer_context = voice_state .live_buffers .lock() .unwrap() .get(&cur) .map(|buf| buf.orig_context as *mut c_void) .unwrap_or(std::ptr::null_mut()); } ``` ### Not tested - Read from commit `082944135cd2b96288470a595861fa608eee08dc` without a new runtime test. ### AI use - Antigravity harness (Gemini 4 Neon) audited `xaudio2.rs` and drafted this report; the author reviewed it before posting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:29:47 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 18:04 UTC · original

Claiming #2386: fixing the IXAudio2SourceVoice::GetState race on pCurrentBufferContext (crates/xcl/src/hle/xaudio2.rs) with a deterministic failing-first CPU unit test, then queuing the paired RX 7800 XT smoke run before opening the PR.
AI use: Claude Code (Claude Sonnet 5.5) will implement, test, and draft the PR.

**@⁠oni-swr** on GitHub · 2026-11-25 18:04 UTC · [original](https://github.com/xcl-dev/xcl/issues/2386#issuecomment-7170405829) Claiming #2386: fixing the `IXAudio2SourceVoice::GetState` race on `pCurrentBufferContext` (`crates/xcl/src/hle/xaudio2.rs`) with a deterministic failing-first CPU unit test, then queuing the paired RX 7800 XT smoke run before opening the PR. AI use: Claude Code (Claude Sonnet 5.5) will implement, test, and draft the PR.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 18:21 UTC · original

Releasing claim on #2386 — competing PR #2410 already implements this fix with a unit test and paired smoke run.
AI use: orchestrated via Antigravity / Claude Sonnet 5.5.

**@⁠oni-swr** on GitHub · 2026-11-25 18:21 UTC · [original](https://github.com/xcl-dev/xcl/issues/2386#issuecomment-7170796991) Releasing claim on #2386 — competing PR #2410 already implements this fix with a unit test and paired smoke run. AI use: orchestrated via Antigravity / Claude Sonnet 5.5.
This discussion has been locked. Commenting is limited to contributors.
No description provided.