XAudio2: stale voice-pointer reuse (ABA) when an IXAudio2 engine is released with live voices #328

Open
opened 2026-11-26 04:20:08 +00:00 by github-bot · 1 comment
Owner

@⁠mastrr9 on GitHub · 2026-11-23 23:29 UTC · original

Static audit of crates/xcl/src/hle/xaudio2.rs on main (85bdf21) found a voice-pointer reuse (ABA) hazard across create_source_voice and destroy_voice (xaudio2.rs:791-796, 919-924, 1148-1168):

  • DECODED_VOICES and XMA store entries keyed by raw voice address (voice as usize), and entries are only removed inside destroy_voice.
  • If a title releases an IXAudio2 engine without explicitly calling DestroyVoice on every source voice first (Windows XAudio2 tears down child voices internally when the engine is destroyed), our destroy_voice vtable hook never runs for those voices.
  • Their entries remain in DECODED_VOICES / XMA (and DECODED_LIVE_COUNT stays incremented, plus DecodedCallback leaks).
  • If a later CreateSourceVoice for a plain PCM voice receives a reused heap address from Windows, create_source_voice does not evict stale entries for voice, so subsequent SubmitSourceBuffer calls on that plain PCM voice are intercepted and fed into the stale Opus/XMA decoder (causing silence or garbled audio).

Suggested fix:
On every successful CreateSourceVoice (both the decoded-voice and plain-PCM paths), evict any stale entry for voice as usize from XMA and DECODED_VOICES (decrementing DECODED_LIVE_COUNT and dropping any stale callback handle if present) before inserting the new voice (or returning for plain PCM).

Not tested: on Windows (static code audit on macOS; no game files).

AI use: Audited with Antigravity (Gemini 4 Neon) and Claude Sonnet 5.5 (static code review only).

**@⁠mastrr9** on GitHub · 2026-11-23 23:29 UTC · [original](https://github.com/xcl-dev/xcl/issues/328) Static audit of `crates/xcl/src/hle/xaudio2.rs` on `main` (`85bdf21`) found a voice-pointer reuse (ABA) hazard across `create_source_voice` and `destroy_voice` (`xaudio2.rs:791-796, 919-924, 1148-1168`): - `DECODED_VOICES` and `XMA` store entries keyed by raw voice address (`voice as usize`), and entries are only removed inside `destroy_voice`. - If a title releases an `IXAudio2` engine without explicitly calling `DestroyVoice` on every source voice first (Windows XAudio2 tears down child voices internally when the engine is destroyed), our `destroy_voice` vtable hook never runs for those voices. - Their entries remain in `DECODED_VOICES` / `XMA` (and `DECODED_LIVE_COUNT` stays incremented, plus `DecodedCallback` leaks). - If a later `CreateSourceVoice` for a **plain PCM** voice receives a reused heap address from Windows, `create_source_voice` does not evict stale entries for `voice`, so subsequent `SubmitSourceBuffer` calls on that plain PCM voice are intercepted and fed into the stale Opus/XMA decoder (causing silence or garbled audio). **Suggested fix:** On every successful `CreateSourceVoice` (both the decoded-voice and plain-PCM paths), evict any stale entry for `voice as usize` from `XMA` and `DECODED_VOICES` (decrementing `DECODED_LIVE_COUNT` and dropping any stale callback handle if present) before inserting the new voice (or returning for plain PCM). **Not tested:** on Windows (static code audit on macOS; no game files). **AI use:** Audited with Antigravity (Gemini 4 Neon) and Claude Sonnet 5.5 (static code review only).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:20:08 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 00:24 UTC · original

I'll take this. Plan: factor the decoded-voice teardown (DECODED_VOICES remove + DECODED_LIVE_COUNT decrement + live_buffers clear + callback box drop) into helpers shared by destroy_voice and a new evict_voice(voice); call evict_voice on the address Windows hands back in CreateSourceVoice on both the decoded path (create_decoded_voice) and the plain/silent path, before the new entry is inserted. Unit test inserts a stale XMA + DECODED_VOICES entry at a fake voice address, evicts, and checks both maps and the live count. Windows test suite on RTX 3060 Ti + Ryzen 9 7900X iGPU; smoke run on the retail title per CONTRIBUTING.

**@⁠DavidM1911** on GitHub · 2026-11-24 00:24 UTC · [original](https://github.com/xcl-dev/xcl/issues/328#issuecomment-7118038416) I'll take this. Plan: factor the decoded-voice teardown (`DECODED_VOICES` remove + `DECODED_LIVE_COUNT` decrement + `live_buffers` clear + callback box drop) into helpers shared by `destroy_voice` and a new `evict_voice(voice)`; call `evict_voice` on the address Windows hands back in `CreateSourceVoice` on both the decoded path (`create_decoded_voice`) and the plain/silent path, before the new entry is inserted. Unit test inserts a stale `XMA` + `DECODED_VOICES` entry at a fake voice address, evicts, and checks both maps and the live count. Windows test suite on RTX 3060 Ti + Ryzen 9 7900X iGPU; smoke run on the retail title per CONTRIBUTING.
This discussion has been locked. Commenting is limited to contributors.
No description provided.