hle/xaudio2: enforce XAudio2 play/loop region invariants in clamp_regions and silent_buffer #89

Manually merged
github merged 1 commit from gh-pr/89/head into gh-pr/89/base 2026-11-26 04:09:17 +00:00
Owner

@⁠certified-retart on GitHub · 2026-11-23 02:50 UTC · original
certified-retart:fix/xaudio2-region-clamping → main · +190 −7 · 1 files · 1 commits · merged 2026-11-23 10:47 UTC

Summary

Hardens hle::xaudio2::clamp_regions and hle::xaudio2::silent_buffer so decoded and fallback buffers always satisfy XAudio2 IXAudio2SourceVoice::SubmitSourceBuffer (XAUDIO2_BUFFER) play/loop region invariants instead of failing with XAUDIO2_E_INVALID_CALL:

  • clamp_regions:
    • Compares play_length > total_frames - play_begin instead of play_begin.saturating_add(play_length) > total_frames, preventing a u32 overflow/panic when total_frames == u32::MAX.
    • Sets explicit play_length = total_frames - play_begin when play_begin > 0 && play_length == 0 (XAudio2 documents PlayLength == 0 requires PlayBegin == 0).
    • Clears stale loop_begin / loop_length when loop_count == 0.
    • Clamps loop regions against play_end (play_begin + play_length when play_length > 0, else total_frames) rather than just total_frames:
      • Disables the loop (loop_begin = loop_length = loop_count = 0) when loop_begin >= play_end or loop_begin + loop_length <= play_begin.
      • Sets explicit loop_length = play_end - loop_begin when loop_length == 0 and either loop_begin > 0 or play_end < total_frames (XAudio2 requires LoopBegin == 0 when LoopLength == 0).
      • Still allows valid pre-roll loops (loop_begin < play_begin with loop_begin + loop_length > play_begin && <= play_end).
  • silent_buffer:
    • Ensures loop_begin > 0 && loop_length == 0 allocates at least loop_begin + 1 frames.
    • Caps max_frames to XAUDIO2_MAX_BUFFER_BYTES (0x8000_0000) / frame_bytes.
    • Runs clamp_regions(&mut nb, frames as u32) before returning, so both normal and truncated/zero-byte silence buffers obey all XAUDIO2_BUFFER invariants (including clearing loop_count when max_bytes == 0).

Verification

  • cargo test -p xcl --lib hle::xaudio2 -- --test-threads=1: 9 passed; 0 failed (live Windows system XAudio2 2.9)
    • Added clamp_regions_enforces_play_end_and_loop_invariants (including u32::MAX overflow and loop_end == play_begin boundary).
    • Added silent_buffer_clamps_regions_and_zero_max_bytes.
    • Added clamped_and_silenced_buffers_accepted_by_windows_xaudio2, which creates a real Windows PCM16 IXAudio2SourceVoice and verifies SubmitSourceBuffer returns S_OK across all clamped and silenced edge cases.
**@⁠certified-retart** on GitHub · 2026-11-23 02:50 UTC · [original](https://github.com/xcl-dev/xcl/pull/89) `certified-retart:fix/xaudio2-region-clamping` → `main` · +190 −7 · 1 files · 1 commits · merged 2026-11-23 10:47 UTC ## Summary Hardens `hle::xaudio2::clamp_regions` and `hle::xaudio2::silent_buffer` so decoded and fallback buffers always satisfy XAudio2 `IXAudio2SourceVoice::SubmitSourceBuffer` (`XAUDIO2_BUFFER`) play/loop region invariants instead of failing with `XAUDIO2_E_INVALID_CALL`: - **`clamp_regions`:** - Compares `play_length > total_frames - play_begin` instead of `play_begin.saturating_add(play_length) > total_frames`, preventing a `u32` overflow/panic when `total_frames == u32::MAX`. - Sets explicit `play_length = total_frames - play_begin` when `play_begin > 0 && play_length == 0` (XAudio2 documents `PlayLength == 0` requires `PlayBegin == 0`). - Clears stale `loop_begin` / `loop_length` when `loop_count == 0`. - Clamps loop regions against `play_end` (`play_begin + play_length` when `play_length > 0`, else `total_frames`) rather than just `total_frames`: - Disables the loop (`loop_begin = loop_length = loop_count = 0`) when `loop_begin >= play_end` or `loop_begin + loop_length <= play_begin`. - Sets explicit `loop_length = play_end - loop_begin` when `loop_length == 0` and either `loop_begin > 0` or `play_end < total_frames` (XAudio2 requires `LoopBegin == 0` when `LoopLength == 0`). - Still allows valid pre-roll loops (`loop_begin < play_begin` with `loop_begin + loop_length > play_begin && <= play_end`). - **`silent_buffer`:** - Ensures `loop_begin > 0 && loop_length == 0` allocates at least `loop_begin + 1` frames. - Caps `max_frames` to `XAUDIO2_MAX_BUFFER_BYTES` (`0x8000_0000`) / `frame_bytes`. - Runs `clamp_regions(&mut nb, frames as u32)` before returning, so both normal and truncated/zero-byte silence buffers obey all `XAUDIO2_BUFFER` invariants (including clearing `loop_count` when `max_bytes == 0`). ## Verification - `cargo test -p xcl --lib hle::xaudio2 -- --test-threads=1`: **9 passed; 0 failed** (live Windows system XAudio2 2.9) - Added `clamp_regions_enforces_play_end_and_loop_invariants` (including `u32::MAX` overflow and `loop_end == play_begin` boundary). - Added `silent_buffer_clamps_regions_and_zero_max_bytes`. - Added `clamped_and_silenced_buffers_accepted_by_windows_xaudio2`, which creates a real Windows PCM16 `IXAudio2SourceVoice` and verifies `SubmitSourceBuffer` returns `S_OK` across all clamped and silenced edge cases.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:09:15 +00:00
github manually merged commit 73fb221c57 into gh-pr/89/base 2026-11-26 04:09:17 +00:00
Author
Owner

@⁠Lum7nt on GitHub · 2026-11-23 05:56 UTC · original

Tested locally against the Windows 11 system XAudio2 2.9 engine — all 9 tests pass cleanly.

Key improvements verified:

  1. XAudio2 specification conformance: Real IXAudio2SourceVoice::SubmitSourceBuffer calls succeed across all boundary conditions (play length clamping to total frames, loop length clamped to play_end, and zeroing out stale loop parameters when loop_count == 0).
  2. Robust silence generation: silent_buffer correctly clamps bounds and respects XAUDIO2_MAX_BUFFER_BYTES, preventing XAUDIO2_E_INVALID_CALL crashes in audio-heavy titles.

Solid bugfix with thorough live-API test coverage.

**@⁠Lum7nt** on GitHub · 2026-11-23 05:56 UTC · [original](https://github.com/xcl-dev/xcl/pull/89#issuecomment-7092863769) Tested locally against the Windows 11 system XAudio2 2.9 engine — all 9 tests pass cleanly. Key improvements verified: 1. **XAudio2 specification conformance**: Real `IXAudio2SourceVoice::SubmitSourceBuffer` calls succeed across all boundary conditions (play length clamping to total frames, loop length clamped to `play_end`, and zeroing out stale loop parameters when `loop_count == 0`). 2. **Robust silence generation**: `silent_buffer` correctly clamps bounds and respects `XAUDIO2_MAX_BUFFER_BYTES`, preventing `XAUDIO2_E_INVALID_CALL` crashes in audio-heavy titles. Solid bugfix with thorough live-API test coverage.
Author
Owner

@⁠ivall on GitHub · 2026-11-23 10:48 UTC · original

Merged. Tested on Windows 11 with an RX 9070 (RDNA4): cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; Unicorn Overlord and XDefiant run with this round merged. Thanks!

**@⁠ivall** on GitHub · 2026-11-23 10:48 UTC · [original](https://github.com/xcl-dev/xcl/pull/89#issuecomment-7098509296) Merged. Tested on Windows 11 with an RX 9070 (RDNA4): `cargo test --workspace` green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with `XCL_SPLIT_WAVE64=1`; Unicorn Overlord and XDefiant run with this round merged. Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.