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

Open
opened 2026-11-23 23:29:55 +00:00 by mastrr9 · 1 comment
mastrr9 commented 2026-11-23 23:29:55 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by mastrr9 on 2026-11-23T23:29:55Z.


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).

<!-- greyfinch-sync:ivall/xcl:issue:6571021023 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/328). Originally posted by [mastrr9](https://github.com/mastrr9) on 2026-11-23T23:29:55Z. --- 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).
DavidM1911 commented 2026-11-24 07:04:57 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T00:24:37Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7118038416 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/328#issuecomment-7118038416). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T00:24:37Z. --- 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.
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#325
No description provided.