XAudio2: use saturating_mul for avg_bytes in pcm16_for_rate #1239

Merged
b3IKK merged 1 commit from upstream-pr/1249 into main 2026-12-02 17:39:07 +00:00
b3IKK commented 2026-11-24 13:44:14 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T13:44:14Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:5473321477 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1249). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T13:44:14Z. --- `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.
DavidM1911 commented 2026-11-24 14:06:50 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T14:02:08Z.


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

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

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7133730790 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1249#issuecomment-7133730790). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T14:02:08Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - cargo test -p xcl --lib lib xaudio2: 13 passed, 0 failed. Thanks!
github-actions[bot] commented 2026-11-27 21:26:34 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-27T21:26:34Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7228447411 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1249#issuecomment-7228447411). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T21:26:34Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
BBareth commented 2026-11-30 23:20:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T23:20:28Z.


merge-check claim
head: 456c32dec7

<!-- greyfinch-sync:ivall/xcl:comment:7295001738 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1249#issuecomment-7295001738). Originally posted by [BBareth](https://github.com/BBareth) on 2026-11-30T23:20:28Z. --- merge-check claim head: 456c32dec710ae1bc53f511e4ef50cc5ed8f8778
BBareth commented 2026-11-30 23:28:24 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T23:28:24Z.


merge-check
base: d0832e614f
head: 456c32dec7
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 passed, 0 failed, 51 ignored; 28 SKIPPED lines, as on the base; failing: none. The base alone, same run: 2890 passed, 0 failed, 51 ignored, 28 SKIPPED lines (22 no wave64, 2 no heap windows, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 cannot disable the driver cache). The new test pcm16_for_rate_saturates_avg_bytes_on_overflow ran and passed.
review: no blocking finding; one file (+11/-1), read. pcm16_for_rate computes avg_bytes with saturating_mul instead of *, so a format with an absurd sample rate no longer overflows (a panic in a debug build, a wrapped value in release); every rate a real title uses gives the same value as before (the test checks 48 kHz stereo = 192,000). The new test fails without the change in a debug build by construction (8 channels at 0x2000_0000 Hz overflows u32).
performance: none: one multiplication when a voice format is built.
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti 16 GB, driver 621.24 (32.0.16.2124); i7-12700K; Windows 11 26200. Claude Code, Claude Fable 5.2 read the diff, ran the build and the suite and wrote this check.

<!-- greyfinch-sync:ivall/xcl:comment:7295157843 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1249#issuecomment-7295157843). Originally posted by [BBareth](https://github.com/BBareth) on 2026-11-30T23:28:24Z. --- merge-check base: d0832e614fa7d6ae027bb4feb27ccb9121f54a8f head: 456c32dec710ae1bc53f511e4ef50cc5ed8f8778 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 passed, 0 failed, 51 ignored; 28 SKIPPED lines, as on the base; failing: none. The base alone, same run: 2890 passed, 0 failed, 51 ignored, 28 SKIPPED lines (22 no wave64, 2 no heap windows, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 cannot disable the driver cache). The new test `pcm16_for_rate_saturates_avg_bytes_on_overflow` ran and passed. review: no blocking finding; one file (+11/-1), read. `pcm16_for_rate` computes `avg_bytes` with `saturating_mul` instead of `*`, so a format with an absurd sample rate no longer overflows (a panic in a debug build, a wrapped value in release); every rate a real title uses gives the same value as before (the test checks 48 kHz stereo = 192,000). The new test fails without the change in a debug build by construction (8 channels at 0x2000_0000 Hz overflows u32). performance: none: one multiplication when a voice format is built. verdict: safe to merge hardware / tool and model: RTX 5070 Ti 16 GB, driver 621.24 (32.0.16.2124); i7-12700K; Windows 11 26200. Claude Code, Claude Fable 5.2 read the diff, ran the build and the suite and wrote this check.
Foniass commented 2026-12-01 05:14:19 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:14:19Z.


Triage (#1607) – head 456c32d

No open or merged PR makes the same one-line change. #1437 removes the same overflow another way, by clamping the rate to 11 025–192 000 Hz in pcm16_for_rate (along with much more), and conflicts with main; if it lands, this change becomes unnecessary.
The change is correct: a title format with a rate above ~33.5 MHz (via xma_info) overflowed rate * block (panic in debug, wrap in release). The resulting avg_bytes = u32::MAX voice will still be rejected by Windows, which is fine.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7301560880 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1249#issuecomment-7301560880). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:14:19Z. --- **Triage (#1607)** – head 456c32d No open or merged PR makes the same one-line change. #1437 removes the same overflow another way, by clamping the rate to 11 025–192 000 Hz in `pcm16_for_rate` (along with much more), and conflicts with main; if it lands, this change becomes unnecessary. The change is correct: a title format with a rate above ~33.5 MHz (via `xma_info`) overflowed `rate * block` (panic in debug, wrap in release). The resulting `avg_bytes = u32::MAX` voice will still be rejected by Windows, which is fine. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
Decodere commented 2026-12-02 10:19:59 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Decodere on 2026-12-02T10:19:59Z.


merge-check
base: 1c8b551475
head: 456c32dec7
merge: clean (trial-merge commit 6768060cdd20637a9adca56a4db6f74841d26345, git diff --check clean)
build: cargo build --workspace ok
tests: 43 targets combined: 2910 passed, 0 failed, 51 ignored (+1 new test vs main baseline). Specifically, all 42 non-lib workspace targets under cargo test --workspace --no-fail-fast -- --test-threads=4 --nocapture passed 148/0 (20 ignored, including gpu_two_pass_cache_e2e), and cargo test -p xcl --lib -- --test-threads=2 passed 2762/0 (31 ignored; --test-threads=4 hit the unrelated pre-existing ASLR collision on synthetic pointer 0x7c000041 in main's set_pipeline_state_goes_into_a_run_only_with_a_two_pass_pipeline -> real.rs:1237). Targeted new test hle::xaudio2::tests::pcm16_for_rate_saturates_avg_bytes_on_overflow passed with 0 skips. 28 SKIPPED lines (host environment only: 22 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 2 no heap windows, 1 cannot disable the driver cache (Windows Developer Mode required)).
review: Read the full merged diff (+11/-1 in crates/xcl/src/hle/xaudio2.rs). Replaces rate * block as u32 with rate.saturating_mul(block as u32) in pcm16_for_rate, avoiding debug arithmetic overflow panics or release wraparound when computing avg_bytes for extreme sample rates. The new unit test covers both normal (2, 48_000) -> 192_000 and overflowing (8, 0x2000_0000) -> u32::MAX using safe value copies from the packed WaveFormatEx struct. Minimal, exact, no scope creep or UB.
performance: not applicable (Tier 1 — single-line saturation fix in pcm16_for_rate, off hot rendering/GPU paths).
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 4060 8 GB (driver 621.40), Intel Core i5-11400F, 32 GB RAM, Windows 11 Build 26200, Rust 1.100.1 MSVC, DXC 1.9.2602.17 / Antigravity (Gemini 4 Neon)

<!-- greyfinch-sync:ivall/xcl:comment:7342313684 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1249#issuecomment-7342313684). Originally posted by [Decodere](https://github.com/Decodere) on 2026-12-02T10:19:59Z. --- merge-check base: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f head: 456c32dec710ae1bc53f511e4ef50cc5ed8f8778 merge: clean (trial-merge commit 6768060cdd20637a9adca56a4db6f74841d26345, git diff --check clean) build: cargo build --workspace ok tests: 43 targets combined: 2910 passed, 0 failed, 51 ignored (+1 new test vs main baseline). Specifically, all 42 non-lib workspace targets under `cargo test --workspace --no-fail-fast -- --test-threads=4 --nocapture` passed 148/0 (20 ignored, including gpu_two_pass_cache_e2e), and `cargo test -p xcl --lib -- --test-threads=2` passed 2762/0 (31 ignored; `--test-threads=4` hit the unrelated pre-existing ASLR collision on synthetic pointer 0x7c000041 in main's set_pipeline_state_goes_into_a_run_only_with_a_two_pass_pipeline -> real.rs:1237). Targeted new test hle::xaudio2::tests::pcm16_for_rate_saturates_avg_bytes_on_overflow passed with 0 skips. 28 SKIPPED lines (host environment only: 22 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 2 no heap windows, 1 cannot disable the driver cache (Windows Developer Mode required)). review: Read the full merged diff (+11/-1 in crates/xcl/src/hle/xaudio2.rs). Replaces `rate * block as u32` with `rate.saturating_mul(block as u32)` in `pcm16_for_rate`, avoiding debug arithmetic overflow panics or release wraparound when computing `avg_bytes` for extreme sample rates. The new unit test covers both normal `(2, 48_000) -> 192_000` and overflowing `(8, 0x2000_0000) -> u32::MAX` using safe value copies from the packed `WaveFormatEx` struct. Minimal, exact, no scope creep or UB. performance: not applicable (Tier 1 — single-line saturation fix in `pcm16_for_rate`, off hot rendering/GPU paths). verdict: safe to merge hardware / tool and model: NVIDIA GeForce RTX 4060 8 GB (driver 621.40), Intel Core i5-11400F, 32 GB RAM, Windows 11 Build 26200, Rust 1.100.1 MSVC, DXC 1.9.2602.17 / Antigravity (Gemini 4 Neon)
cj-tomlin commented 2026-12-02 16:45:35 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by cj-tomlin on 2026-12-02T16:45:35Z.


Test report, head 456c32dec710ae1bc53f511e4ef50cc5ed8f8778 merged onto main 5eaadb54c14e0fc29597b2a1689d7dbd83491303 (clean, merge 4fe5165e).

  • Hardware: NVIDIA GeForce RTX 3080 10 GB, driver 621.40, AMD Ryzen 7 7800X3D, Windows 11 build 26300, debug build.
  • cargo build --workspace: ok.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 2975 passed, 0 failed, 52 ignored; SKIPPED: 27 (22 no wave64, 2 no heap windows, 3 no D3D12 debug layer / d3d12SDKLayers.dll), the same 27 as plain main (2974 passed, 0 failed, 52 ignored) on this machine.
  • The new pcm16_for_rate_saturates_avg_bytes_on_overflow passes, and fails with attempt to multiply with overflow (xaudio2.rs:389) when I put main's rate * block as u32 back into the merged tree.

Diff read (+11/-1, xaudio2.rs), CPU only.

Tool and model: Claude Code (Claude Opus 5.5) merged, built, ran the tests and drafted this; I checked it.

<!-- greyfinch-sync:ivall/xcl:comment:7352413786 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1249#issuecomment-7352413786). Originally posted by [cj-tomlin](https://github.com/cj-tomlin) on 2026-12-02T16:45:35Z. --- Test report, head `456c32dec710ae1bc53f511e4ef50cc5ed8f8778` merged onto `main` `5eaadb54c14e0fc29597b2a1689d7dbd83491303` (clean, merge `4fe5165e`). - Hardware: NVIDIA GeForce RTX 3080 10 GB, driver 621.40, AMD Ryzen 7 7800X3D, Windows 11 build 26300, debug build. - `cargo build --workspace`: ok. - `cargo test --workspace --no-fail-fast -- --test-threads=4`: 2975 passed, 0 failed, 52 ignored; SKIPPED: 27 (22 no wave64, 2 no heap windows, 3 no D3D12 debug layer / d3d12SDKLayers.dll), the same 27 as plain `main` (2974 passed, 0 failed, 52 ignored) on this machine. - The new `pcm16_for_rate_saturates_avg_bytes_on_overflow` passes, and fails with `attempt to multiply with overflow` (`xaudio2.rs:389`) when I put `main`'s `rate * block as u32` back into the merged tree. Diff read (+11/-1, `xaudio2.rs`), CPU only. Tool and model: Claude Code (Claude Opus 5.5) merged, built, ran the tests and drafted this; I checked it.
Sign in to join this conversation.
No reviewers
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!1239
No description provided.