Skip to content

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

Backlog

Description

@ahmetbagci8

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.

Activity

  1. oni-swr commented on Nov 25, 2026

    @oni-swr
    Contributor

    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.

  2. oni-swr commented on Nov 25, 2026

    @oni-swr
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions