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

Open
opened 2026-11-25 17:39:18 +00:00 by ahmetbagci8 · 2 comments
ahmetbagci8 commented 2026-11-25 17:39:18 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by ahmetbagci8 on 2026-11-25T17:39:18Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:6610197028 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/2386). Originally posted by [ahmetbagci8](https://github.com/ahmetbagci8) on 2026-11-25T17:39:18Z. --- ### 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.
oni-swr commented 2026-11-25 18:04:49 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by oni-swr on 2026-11-25T18:04:40Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7170405829 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/2386#issuecomment-7170405829). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T18:04:40Z. --- 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 commented 2026-11-25 19:09:02 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by oni-swr on 2026-11-25T18:21:29Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7170796991 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/2386#issuecomment-7170796991). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T18:21:29Z. --- 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 in to join this conversation.
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#2376
No description provided.