XAudio2: validate and clamp voice sample rates and channel counts #1437

Open
github-bot wants to merge 1 commit from github-bot/gh-1437 into main AGit
Owner

@⁠slopgobblercobbler on GitHub · 2026-11-24 15:57 UTC · original
slopgobblercobbler:fix/xaudio2-sample-rate-channel-bounds → main · +310 −8 · 1 files · 1 commits · open

Summary

Audits and bounds XAudio2 voice sample rates and channel counts in crates/xcl/src/hle/xaudio2.rs:

  1. Bounds constants & helpers:
    • Added XAUDIO2_MIN_SAMPLE_RATE (11,025 Hz), XAUDIO2_MAX_SAMPLE_RATE (192,000 Hz), XAUDIO2_MIN_CHANNELS (1), XAUDIO2_MAX_CHANNELS (8), and XAUDIO2_E_INVALID_CALL (0x88960001).
    • Added validation and clamping helpers: is_valid_sample_rate, is_valid_channel_count, clamp_sample_rate (defaults 0 to 48 kHz, clamps to [11025, 192000]), and clamp_channel_count (clamps to [1, 8]).
  2. Format synthesis clamping:
    • In pcm16_for_rate, opus_format, and xma_info: clamp channel counts to 1..=8 and sample rates to 11025..=192000 Hz to prevent invalid parameter rejection by host XAudio2.
  3. Voice creation fallback:
    • In create_source_voice: added fallback clamping for plain PCM formats rejected by host XAudio2 due to out-of-bounds rates/channels, retrying with clamped PCM16 so titles with unusual audio specs continue playing via silent voice rather than failing creation.
    • In create_mastering_voice: sanitized out-of-bounds channels (> 8) or invalid sample rates to default native endpoint format.
  4. Unit tests:
    • Added sample_rate_and_channel_bounds_validation_and_clamping and clamped_voices_accepted_by_windows_xaudio2.

Changes

  • crates/xcl/src/hle/xaudio2.rs:
    • Parameter bounds validation and clamping for sample rates and channels.
    • Creation fallback handling for rejected voice parameters.
    • Unit tests covering boundary values and host XAudio2 acceptance.

Tested

  • Windows 11 x64, Rust 1.85+ stable (MSVC).
  • cargo test -p xcl -- xaudio2: 11 passed, 0 failed (100% pass rate).
  • Full lib test suite: 584 passed, 0 failed.
  • Clean diff: zero trailing whitespace, GPL-2.0-only compliant, zero game data/keys.

Not Tested

  • Multi-channel 7.1 surround sound physical speaker output.

AI Use

Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity

**@⁠slopgobblercobbler** on GitHub · 2026-11-24 15:57 UTC · [original](https://github.com/xcl-dev/xcl/pull/1437) `slopgobblercobbler:fix/xaudio2-sample-rate-channel-bounds` → `main` · +310 −8 · 1 files · 1 commits · open ## Summary Audits and bounds XAudio2 voice sample rates and channel counts in `crates/xcl/src/hle/xaudio2.rs`: 1. **Bounds constants & helpers**: - Added `XAUDIO2_MIN_SAMPLE_RATE` (11,025 Hz), `XAUDIO2_MAX_SAMPLE_RATE` (192,000 Hz), `XAUDIO2_MIN_CHANNELS` (1), `XAUDIO2_MAX_CHANNELS` (8), and `XAUDIO2_E_INVALID_CALL` (0x88960001). - Added validation and clamping helpers: `is_valid_sample_rate`, `is_valid_channel_count`, `clamp_sample_rate` (defaults 0 to 48 kHz, clamps to [11025, 192000]), and `clamp_channel_count` (clamps to [1, 8]). 2. **Format synthesis clamping**: - In `pcm16_for_rate`, `opus_format`, and `xma_info`: clamp channel counts to 1..=8 and sample rates to 11025..=192000 Hz to prevent invalid parameter rejection by host XAudio2. 3. **Voice creation fallback**: - In `create_source_voice`: added fallback clamping for plain PCM formats rejected by host XAudio2 due to out-of-bounds rates/channels, retrying with clamped PCM16 so titles with unusual audio specs continue playing via silent voice rather than failing creation. - In `create_mastering_voice`: sanitized out-of-bounds channels (> 8) or invalid sample rates to default native endpoint format. 4. **Unit tests**: - Added `sample_rate_and_channel_bounds_validation_and_clamping` and `clamped_voices_accepted_by_windows_xaudio2`. ## Changes - `crates/xcl/src/hle/xaudio2.rs`: - Parameter bounds validation and clamping for sample rates and channels. - Creation fallback handling for rejected voice parameters. - Unit tests covering boundary values and host XAudio2 acceptance. ## Tested - Windows 11 x64, Rust 1.85+ stable (MSVC). - `cargo test -p xcl -- xaudio2`: 11 passed, 0 failed (100% pass rate). - Full lib test suite: 584 passed, 0 failed. - Clean diff: zero trailing whitespace, GPL-2.0-only compliant, zero game data/keys. ## Not Tested - Multi-channel 7.1 surround sound physical speaker output. ## AI Use Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity
- Enforce XAudio2 voice sample rate bounds (11,025 to 192,000 Hz) and channel limits (1 to 8 channels)
- Clamp synthesized voices (Opus, XMA2, fallback) to prevent XAUDIO2_E_INVALID_CALL
- Enforce XAUDIO2_QUANTUM_DENOMINATOR (100) multiple on CreateMasteringVoice nonzero InputSampleRate, defaulting non-conforming rates to 0 (native endpoint rate)
- Add unit tests verifying bounds validation, quantum multiple compliance, and live engine voice creation
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:53:51 +00:00
Author
Owner

@⁠Anon64 on GitHub · 2026-11-24 16:24 UTC · original

Regression run for this PR: head ac222f7, based on main cbea872. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, XCL_NETWORK=0, A pressed every 4 s from 20 s, scripts/smoke.ps1 on a retail title for 60 s.

main cbea872:

xcl-smoke v1
commit: cbea87246bc4983a339caa152b2b5ad68697ec50
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1347 (PresentX 1347)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 76435 MB

this PR:

xcl-smoke v1
commit: ac222f75daf64ad3f62c62d172b936afac2ca756
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1522 (PresentX 1522)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 90297 MB

No device removals or crashes; 113 % of main's frames, 118 % of its peak commit.

Peak commit in both runs is mostly the pipeline preload's DXC compiles on this machine (#1035; 23 GB on main with XCL_PSO_PRELOAD=0), so it varies run to run.

Not tested: the whole suite; whether the title reaches the changed code in these 60 s.

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

**@⁠Anon64** on GitHub · 2026-11-24 16:24 UTC · [original](https://github.com/xcl-dev/xcl/pull/1437#issuecomment-7137619518) Regression run for this PR: head `ac222f7`, based on `main` `cbea872`. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, `XCL_NETWORK=0`, A pressed every 4 s from 20 s, `scripts/smoke.ps1` on a retail title for 60 s. main `cbea872`: ``` xcl-smoke v1 commit: cbea87246bc4983a339caa152b2b5ad68697ec50 gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1347 (PresentX 1347) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 76435 MB ``` this PR: ``` xcl-smoke v1 commit: ac222f75daf64ad3f62c62d172b936afac2ca756 gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1522 (PresentX 1522) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 90297 MB ``` No device removals or crashes; 113 % of main's frames, 118 % of its peak commit. Peak commit in both runs is mostly the pipeline preload's DXC compiles on this machine (#1035; 23 GB on `main` with `XCL_PSO_PRELOAD=0`), so it varies run to run. **Not tested:** the whole suite; whether the title reaches the changed code in these 60 s. Written with Claude Code (Claude Opus 5.5) from runs on this machine.
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 16:53 UTC · original · edited 2026-11-24 17:10 UTC

Full-suite run, since this PR has no test report yet. Tested exact head ac222f7 (on its base cbea872).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 758 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: sample_rate_and_channel_bounds_validation_and_clamping, clamped_voices_accepted_by_windows_xaudio2.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 758 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-24 16:53 UTC · [original](https://github.com/xcl-dev/xcl/pull/1437#issuecomment-7138376038) · edited 2026-11-24 17:10 UTC Full-suite run, since this PR has no test report yet. Tested exact head `ac222f7` (on its base `cbea872`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **758 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `sample_rate_and_channel_bounds_validation_and_clamping`, `clamped_voices_accepted_by_windows_xaudio2`. - Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **758 passed, 0 failed, 41 ignored**, no skips. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

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

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 18:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/1437#issuecomment-7141273247) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified `main`.) Thanks!
Author
Owner

@⁠Toons29 on GitHub · 2026-11-25 03:31 UTC · original

Regression run of e49f75d1 on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: e49f75d1552e9d57ff924f2b477ecfa84b397aa9
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1632 (PresentX 1632)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14815 MB

Against the limits in CONTRIBUTING.md: 101 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

**@⁠Toons29** on GitHub · 2026-11-25 03:31 UTC · [original](https://github.com/xcl-dev/xcl/pull/1437#issuecomment-7151989420) Regression run of `e49f75d1` on the retail title, for this PR's **Regression run** section. Both blocks are from `scripts/smoke.ps1` as printed, not edited: `main` on `4178e888`, this PR on its head commit with no uncommitted changes, same title and time. ``` xcl-smoke v1 commit: 4178e888c0b5be50a2887eda45cdb832763b68a4 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 61 60 60 device removals: 0 crashes: 0 peak commit: 14831 MB ``` ``` xcl-smoke v1 commit: e49f75d1552e9d57ff924f2b477ecfa84b397aa9 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1632 (PresentX 1632) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14815 MB ``` Against the limits in CONTRIBUTING.md: 101 % of `main`'s frames, 100 % of its peak commit, no crash, no device removal. AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, `cargo build --release -p xcl`, 60 s, `XCL_NETWORK=0`. This is a regression check only: it does not show whether the run reaches the code this PR changes. AI use: Claude Code (Claude Opus 5.5) built both trees and ran `scripts/smoke.ps1`; I checked the blocks before posting.
Author
Owner

@⁠gut0to on GitHub · 2026-11-24 17:05 UTC · original
On crates/xcl/src/hle/xaudio2.rs line 1032 (outdated):

@@ -969,6 +1029,14 @@ unsafe extern "system" fn create_mastering_voice(
         crate::xlog_info!("XAudio2: CreateMasteringVoice – console device id ignored (default device)");
     }
     let flags = flags & MASTERING_FLAGS;
+    let (channels, rate) = if channels > XAUDIO2_MAX_CHANNELS as u32 || (rate > 0 && !is_valid_sample_rate(rate)) {

CreateMasteringVoice also requires a nonzero InputSampleRate to be a multiple of XAUDIO2_QUANTUM_DENOMINATOR (100). This check treats 11,025 Hz as valid and passes it to orig, but XAudio2 rejects that rate for a mastering voice, so this fallback can still fail. Please default rates that violate the quantum requirement and add a test for 11,025 Hz. AI-assisted review; I checked the CreateMasteringVoice contract and the XAudio2 header.

**@⁠gut0to** on GitHub · 2026-11-24 17:05 UTC · [original](https://github.com/xcl-dev/xcl/pull/1437#discussion_r4683649578) On `crates/xcl/src/hle/xaudio2.rs` line 1032 (outdated): ```diff @@ -969,6 +1029,14 @@ unsafe extern "system" fn create_mastering_voice( crate::xlog_info!("XAudio2: CreateMasteringVoice – console device id ignored (default device)"); } let flags = flags & MASTERING_FLAGS; + let (channels, rate) = if channels > XAUDIO2_MAX_CHANNELS as u32 || (rate > 0 && !is_valid_sample_rate(rate)) { ``` CreateMasteringVoice also requires a nonzero `InputSampleRate` to be a multiple of `XAUDIO2_QUANTUM_DENOMINATOR` (100). This check treats 11,025 Hz as valid and passes it to `orig`, but XAudio2 rejects that rate for a mastering voice, so this fallback can still fail. Please default rates that violate the quantum requirement and add a test for 11,025 Hz. AI-assisted review; I checked the CreateMasteringVoice contract and the XAudio2 header.
Author
Owner

@⁠slopgobblercobbler on GitHub · 2026-11-24 18:25 UTC · original
On crates/xcl/src/hle/xaudio2.rs line 1032 (outdated):

@@ -969,6 +1029,14 @@ unsafe extern "system" fn create_mastering_voice(
         crate::xlog_info!("XAudio2: CreateMasteringVoice – console device id ignored (default device)");
     }
     let flags = flags & MASTERING_FLAGS;
+    let (channels, rate) = if channels > XAUDIO2_MAX_CHANNELS as u32 || (rate > 0 && !is_valid_sample_rate(rate)) {

Addressed in commit e49f75d: updated CreateMasteringVoice to check
ate % XAUDIO2_QUANTUM_DENOMINATOR == 0 for non-zero InputSampleRate, defaulting non-conforming rates (such as 11,025 Hz) to 0 (endpoint native rate). Also added mastering_voice_quantum_sample_rate_defaults_to_native verifying 11,025 Hz defaults to native format and passes voice creation against Windows XAudio2.

**@⁠slopgobblercobbler** on GitHub · 2026-11-24 18:25 UTC · [original](https://github.com/xcl-dev/xcl/pull/1437#discussion_r4684636192) On `crates/xcl/src/hle/xaudio2.rs` line 1032 (outdated): ```diff @@ -969,6 +1029,14 @@ unsafe extern "system" fn create_mastering_voice( crate::xlog_info!("XAudio2: CreateMasteringVoice – console device id ignored (default device)"); } let flags = flags & MASTERING_FLAGS; + let (channels, rate) = if channels > XAUDIO2_MAX_CHANNELS as u32 || (rate > 0 && !is_valid_sample_rate(rate)) { ``` Addressed in commit e49f75d: updated CreateMasteringVoice to check ate % XAUDIO2_QUANTUM_DENOMINATOR == 0 for non-zero InputSampleRate, defaulting non-conforming rates (such as 11,025 Hz) to 0 (endpoint native rate). Also added mastering_voice_quantum_sample_rate_defaults_to_native verifying 11,025 Hz defaults to native format and passes voice creation against Windows XAudio2.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 05:13 UTC · original

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-26 05:13 UTC · [original](https://github.com/xcl-dev/xcl/pull/1437#issuecomment-7183287946) Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1437/head:github-bot/gh-1437'
git switch 'github-bot/gh-1437'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1437'
git switch 'github-bot/gh-1437'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1437'
git switch 'github-bot/gh-1437'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1437'
git switch 'main'
git merge --squash 'github-bot/gh-1437'
git switch 'main'
git merge --ff-only 'github-bot/gh-1437'
git switch 'main'
git merge 'github-bot/gh-1437'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.