XAudio2: use saturating_mul for avg_bytes in pcm16_for_rate #1249

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

@⁠b3IKK on GitHub · 2026-11-24 13:44 UTC · original
b3IKK:xaudio2/pcm16-rate-overflow → main · +11 −1 · 1 files · 1 commits · open

pcm16_for_rate computed rate * block as u32 without overflow checks. A malformed voice format with a large sample rate (e.g. 8 channels and rate >= 0x1000_0000) panicked in debug builds (aborting across the extern "system" fn boundary in CreateSourceVoice) and wrapped to a bogus avg_bytes in release builds.

This switches avg_bytes to rate.saturating_mul(block as u32).

What is in it

  • crates/xcl/src/hle/xaudio2.rs: use rate.saturating_mul(block as u32) in pcm16_for_rate; add unit test pcm16_for_rate_saturates_avg_bytes_on_overflow (panicked on multiplication overflow before the fix).

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 752 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware)
  • cargo test -p xcl --lib pcm16_for_rate_saturates_avg_bytes_on_overflow: 1 passed, 0 failed (panicked with multiplication overflow before the fix)

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: 456c32dec710ae1bc53f511e4ef50cc5ed8f8778
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1643 (PresentX 1643)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14604 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited xaudio2.rs, wrote the fix and regression test, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
**@⁠b3IKK** on GitHub · 2026-11-24 13:44 UTC · [original](https://github.com/xcl-dev/xcl/pull/1249) `b3IKK:xaudio2/pcm16-rate-overflow` → `main` · +11 −1 · 1 files · 1 commits · open `pcm16_for_rate` computed `rate * block as u32` without overflow checks. A malformed voice format with a large sample rate (e.g. 8 channels and `rate >= 0x1000_0000`) panicked in debug builds (aborting across the `extern "system" fn` boundary in `CreateSourceVoice`) and wrapped to a bogus `avg_bytes` in release builds. This switches `avg_bytes` to `rate.saturating_mul(block as u32)`. **What is in it** - `crates/xcl/src/hle/xaudio2.rs`: use `rate.saturating_mul(block as u32)` in `pcm16_for_rate`; add unit test `pcm16_for_rate_saturates_avg_bytes_on_overflow` (panicked on multiplication overflow before the fix). **Tested** - hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2609.5, debug (`scripts/test.ps1`) and release (`scripts/smoke.ps1`) - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): 752 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 `no wave64` — `gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware`) - `cargo test -p xcl --lib pcm16_for_rate_saturates_avg_bytes_on_overflow`: 1 passed, 0 failed (panicked with multiplication overflow before the fix) **Regression run** main: ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1226 (PresentX 1226) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15341 MB ``` PR: ``` xcl-smoke v1 commit: 456c32dec710ae1bc53f511e4ef50cc5ed8f8778 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1643 (PresentX 1643) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14604 MB ``` **Not tested** - AMD GPUs and native wave64 hardware - other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine **AI use** - OpenCode, Gemini 4 Neon: audited `xaudio2.rs`, wrote the fix and regression test, ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
pcm16_for_rate computed `rate * block as u32` without overflow checks.
A malformed voice format with a large sample rate (e.g. 8 channels and
rate >= 0x1000_0000) panicked in debug builds (aborting across the
extern "system" fn boundary in CreateSourceVoice) and wrapped to a bogus
avg_bytes in release builds.

Use rate.saturating_mul(block as u32) instead, with a unit test.

Assisted-by: OpenCode (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:48:26 +00:00
Author
Owner

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

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

  • cargo test -p xcl --lib lib xaudio2: 13 passed, 0 failed.

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 14:02 UTC · [original](https://github.com/xcl-dev/xcl/pull/1249#issuecomment-7133730790) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - cargo test -p xcl --lib lib xaudio2: 13 passed, 0 failed. Thanks!
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/1249/head:github-bot/gh-1249'
git switch 'github-bot/gh-1249'

Merge

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