Audio capture: guard WASAPI timestamp underflow and stream silence writes #478

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

@⁠nathanbzrr on GitHub · 2026-11-24 03:50 UTC · original
nathanbzrr:fix/audio-capture-timestamps-and-wav-header → main · +131 −13 · 2 files · 1 commits · open

Problem

crates/victory-hle/src/modules/audio/capture.rs had four issues in the diagnostics WAV recorder:

  • header(frames) computed frames * FRAME_BYTES as u64 without saturation and only fell back to u32::MAX - 36 on u32::try_from failure, allowing 36 + data to overflow in u32 when frames * 4 landed within 36 bytes of u32::MAX.
  • The capture loop computed qpc - *first.get_or_insert(qpc) without checking AUDCLNT_BUFFERFLAGS_TIMESTAMP_ERROR, adjusting the initial timestamp anchor if earlier packets had invalid timestamps, guarding backwards QPC values (checked_sub), or widening intermediate u64 math to u128.
  • Silence gaps and AUDCLNT_BUFFERFLAGS_SILENT packets allocated full-gap Vec<u8> buffers instead of streaming fixed chunks.
  • std::slice::from_raw_parts(data, bytes) did not guard against null data pointers from GetBuffer.

What changed (f81e3ff)

  • Clamped header(frames) with frames.saturating_mul(FRAME_BYTES as u64).min(u64::from(u32::MAX - 36)) as u32 so neither frames * FRAME_BYTES nor 36 + data can overflow.
  • Extracted silence_frames to skip AUDCLNT_BUFFERFLAGS_TIMESTAMP_ERROR packets without anchoring first, anchor first relative to already-written frames, guard backwards QPC with checked_sub, and compute frame positions in u128.
  • Extracted write_zeros streaming [0u8; 16 * 1024] chunks for silence gaps and for AUDCLNT_BUFFERFLAGS_SILENT || data.is_null() packets, keeping written advancing with saturating_add.
  • Compiled modules::audio::capture under #[cfg(all(windows, test, not(feature = "diagnostics")))] in crates/victory-hle/src/modules/audio.rs (gating runtime WASAPI items on feature = "diagnostics") so default-feature cargo test --workspace exercises the unit tests too.
  • Added unit tests in modules::audio::capture::tests: timestamp_errors_and_backwards_qpc_do_not_underflow_or_anchor_zero, write_zeros_streams_exact_byte_count, and header_clamps_data_and_riff_sizes_without_overflow.

Verification (f81e3ff, Windows MSVC)

  • cargo run -p xtask --locked -- policy & cargo test -p xtask --locked: clean
  • cargo clippy --workspace --all-targets --locked -- -D warnings (default & --all-features): clean
  • cargo test -p victory-hle --locked audio::capture (default & --all-features): 3 passed, 0 failed

No game files. AI-assisted; reviewed and verified with synthetic unit tests.

**@⁠nathanbzrr** on GitHub · 2026-11-24 03:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/478) `nathanbzrr:fix/audio-capture-timestamps-and-wav-header` → `main` · +131 −13 · 2 files · 1 commits · open ## Problem `crates/victory-hle/src/modules/audio/capture.rs` had four issues in the diagnostics WAV recorder: - `header(frames)` computed `frames * FRAME_BYTES as u64` without saturation and only fell back to `u32::MAX - 36` on `u32::try_from` failure, allowing `36 + data` to overflow in `u32` when `frames * 4` landed within 36 bytes of `u32::MAX`. - The capture loop computed `qpc - *first.get_or_insert(qpc)` without checking `AUDCLNT_BUFFERFLAGS_TIMESTAMP_ERROR`, adjusting the initial timestamp anchor if earlier packets had invalid timestamps, guarding backwards QPC values (`checked_sub`), or widening intermediate `u64` math to `u128`. - Silence gaps and `AUDCLNT_BUFFERFLAGS_SILENT` packets allocated full-gap `Vec<u8>` buffers instead of streaming fixed chunks. - `std::slice::from_raw_parts(data, bytes)` did not guard against null `data` pointers from `GetBuffer`. ## What changed (`f81e3ff`) - Clamped `header(frames)` with `frames.saturating_mul(FRAME_BYTES as u64).min(u64::from(u32::MAX - 36)) as u32` so neither `frames * FRAME_BYTES` nor `36 + data` can overflow. - Extracted `silence_frames` to skip `AUDCLNT_BUFFERFLAGS_TIMESTAMP_ERROR` packets without anchoring `first`, anchor `first` relative to already-written frames, guard backwards QPC with `checked_sub`, and compute frame positions in `u128`. - Extracted `write_zeros` streaming `[0u8; 16 * 1024]` chunks for silence gaps and for `AUDCLNT_BUFFERFLAGS_SILENT || data.is_null()` packets, keeping `written` advancing with `saturating_add`. - Compiled `modules::audio::capture` under `#[cfg(all(windows, test, not(feature = "diagnostics")))]` in `crates/victory-hle/src/modules/audio.rs` (gating runtime WASAPI items on `feature = "diagnostics"`) so default-feature `cargo test --workspace` exercises the unit tests too. - Added unit tests in `modules::audio::capture::tests`: `timestamp_errors_and_backwards_qpc_do_not_underflow_or_anchor_zero`, `write_zeros_streams_exact_byte_count`, and `header_clamps_data_and_riff_sizes_without_overflow`. ## Verification (`f81e3ff`, Windows MSVC) - `cargo run -p xtask --locked -- policy` & `cargo test -p xtask --locked`: clean - `cargo clippy --workspace --all-targets --locked -- -D warnings` (default & `--all-features`): clean - `cargo test -p victory-hle --locked audio::capture` (default & `--all-features`): 3 passed, 0 failed No game files. AI-assisted; reviewed and verified with synthetic unit tests.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:05:26 +00:00
github-bot force-pushed github-bot/gh-478 from 8f7df4890a to 896677fe00 2026-11-26 06:28:23 +00:00 Compare
github-bot force-pushed github-bot/gh-478 from 896677fe00 to f81e3ff02b 2026-11-26 07:27:50 +00:00 Compare
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 17:35 UTC · original

Verification at 7ac2760d317c62ac05622f5e2edc2c7d1586ff54:

cargo test --locked -p victory-hle --features diagnostics --lib capture -- --test-threads=1 passed all 3 selected tests: header_caps_riff_size_without_overflow_and_stays_frame_aligned, timestamp_errors_and_backwards_qpc_do_not_underflow_or_anchor_zero, and write_zeros_streams_exact_byte_count. This exercises WAV-size clamping/frame alignment, timestamp-error and backwards-QPC handling, long-gap silence accounting, and exact chunked zero writes. It does not exercise live WASAPI capture.

This verification was AI-assisted.

**@⁠jeremygold02** on GitHub · 2026-11-24 17:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/478#issuecomment-7139411238) Verification at `7ac2760d317c62ac05622f5e2edc2c7d1586ff54`: `cargo test --locked -p victory-hle --features diagnostics --lib capture -- --test-threads=1` passed all 3 selected tests: `header_caps_riff_size_without_overflow_and_stays_frame_aligned`, `timestamp_errors_and_backwards_qpc_do_not_underflow_or_anchor_zero`, and `write_zeros_streams_exact_byte_count`. This exercises WAV-size clamping/frame alignment, timestamp-error and backwards-QPC handling, long-gap silence accounting, and exact chunked zero writes. It does not exercise live WASAPI capture. This verification was AI-assisted.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:27 UTC · original · edited 2026-11-26 07:20 UTC

Automated triage for the maintainers.

Merge candidate. The new head (f81e3ff) keeps everything in crates/victory-hle/src/modules/audio/capture.rs that was checked last time:

  • silence_frames skips timestamp-error packets without anchoring, uses checked_sub for QPC running backwards, and does its math in u128.
  • write_zeros writes in chunks.
  • Silent or null packets are written as zeros.

header saturates and clamps data to u32::MAX - 36, so 36 + data can't overflow. The PR body now describes that clamp correctly. The cfg(all(windows, test, not(feature = "diagnostics"))) module lets the three tests run under default features. I checked the test values by hand; they are right.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/478#issuecomment-7149571364) · edited 2026-11-26 07:20 UTC <!-- victory-triage v1 head=f81e3ff02b3f7762bf663cde31fdbcb54e734b1d verdict=merge-candidate at=2026-11-26T07:20:31Z --> Automated triage for the maintainers. **Merge candidate.** The new head (`f81e3ff`) keeps everything in `crates/victory-hle/src/modules/audio/capture.rs` that was checked last time: - `silence_frames` skips timestamp-error packets without anchoring, uses `checked_sub` for QPC running backwards, and does its math in `u128`. - `write_zeros` writes in chunks. - Silent or null packets are written as zeros. `header` saturates and clamps data to `u32::MAX - 36`, so `36 + data` can't overflow. The PR body now describes that clamp correctly. The `cfg(all(windows, test, not(feature = "diagnostics")))` module lets the three tests run under default features. I checked the test values by hand; they are right. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠nathanbzrr on GitHub · 2026-11-25 03:14 UTC · original

Addressed the three triage items and rebased onto origin/main (940d32f, new head 8f7df48):

  • Silent / null-data packet branch: removed the frame-dropping bytes > 0 && !data.is_null() check so record writes zeros via write_zeros on AUDCLNT_BUFFERFLAGS_SILENT || data.is_null() and always advances written = written.saturating_add(u64::from(frames)).
  • header cap: reverted header() to origin/main's implementation (and dropped header_caps_riff_size_without_overflow_and_stays_frame_aligned), keeping the diff focused on the timestamp underflow guard and chunked zero writes.
  • CI test coverage without diagnostics: compiled modules::audio::capture under #[cfg(all(windows, test, not(feature = diagnostics)))] in crates/victory-hle/src/modules/audio.rs (keeping pub mod capture gated on diagnostics) and gated runtime WASAPI items with #[cfg(feature = diagnostics)] so default-feature cargo test --workspace runs both capture tests.

Verified on 8f7df48 (Windows MSVC):

  • cargo run -p xtask -- policy --staged: asset policy: 2 files ok
  • cargo clippy --workspace --all-targets -- -D warnings & --all-features: clean
  • cargo test -p victory-hle --locked capture (default features & --all-features): 2 passed, 0 failed
  • cargo test --workspace --locked: 0 failures across all workspace crates
**@⁠nathanbzrr** on GitHub · 2026-11-25 03:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/478#issuecomment-7151719119) Addressed the three triage items and rebased onto `origin/main` (`940d32f`, new head `8f7df48`): - **Silent / null-data packet branch**: removed the frame-dropping `bytes > 0 && !data.is_null()` check so `record` writes zeros via `write_zeros` on `AUDCLNT_BUFFERFLAGS_SILENT || data.is_null()` and always advances `written = written.saturating_add(u64::from(frames))`. - **`header` cap**: reverted `header()` to `origin/main`'s implementation (and dropped `header_caps_riff_size_without_overflow_and_stays_frame_aligned`), keeping the diff focused on the timestamp underflow guard and chunked zero writes. - **CI test coverage without `diagnostics`**: compiled `modules::audio::capture` under `#[cfg(all(windows, test, not(feature = diagnostics)))]` in `crates/victory-hle/src/modules/audio.rs` (keeping `pub mod capture` gated on `diagnostics`) and gated runtime WASAPI items with `#[cfg(feature = diagnostics)]` so default-feature `cargo test --workspace` runs both `capture` tests. Verified on `8f7df48` (Windows MSVC): - `cargo run -p xtask -- policy --staged`: `asset policy: 2 files ok` - `cargo clippy --workspace --all-targets -- -D warnings` & `--all-features`: clean - `cargo test -p victory-hle --locked capture` (default features & `--all-features`): 2 passed, 0 failed - `cargo test --workspace --locked`: 0 failures across all workspace crates
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:08 UTC · original

Code review and Windows 11 verification at head 8f7df4890ad0 merged cleanly onto main (63bcca43c6a0):

  • Code review (crates/victory-hle/src/modules/audio/capture.rs:134-162, 252, audio.rs:507):
    • silence_frames returns 0 on AUDCLNT_BUFFERFLAGS_TIMESTAMP_ERROR without anchoring first, back-computes first with u128 + saturating_sub, guards backwards QPC jumps via checked_sub, and converts timestamps to frames in u128 without overflow.
    • write_zeros streams from a fixed 16 KiB static zero buffer instead of allocating a potentially huge Vec, and record() routes AUDCLNT_BUFFERFLAGS_SILENT || data.is_null() safely through write_zeros. Default vs --features diagnostics cfg gating compiles cleanly with 0 warnings.
  • Verified (Windows 11, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy --locked -p victory-hle --tests -- -D warnings (default + --features diagnostics): 0 warnings
    • victory-hle -- audio: 10/10 passed on both default and --features diagnostics (including both new regression tests).

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/478#issuecomment-7179472194) Code review and Windows 11 verification at head `8f7df4890ad0` merged cleanly onto `main` (`63bcca43c6a0`): - **Code review (`crates/victory-hle/src/modules/audio/capture.rs:134-162, 252`, `audio.rs:507`)**: - `silence_frames` returns `0` on `AUDCLNT_BUFFERFLAGS_TIMESTAMP_ERROR` without anchoring `first`, back-computes `first` with `u128` + `saturating_sub`, guards backwards QPC jumps via `checked_sub`, and converts timestamps to frames in `u128` without overflow. - `write_zeros` streams from a fixed 16 KiB static zero buffer instead of allocating a potentially huge `Vec`, and `record()` routes `AUDCLNT_BUFFERFLAGS_SILENT || data.is_null()` safely through `write_zeros`. Default vs `--features diagnostics` cfg gating compiles cleanly with 0 warnings. - **Verified (Windows 11, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy --locked -p victory-hle --tests -- -D warnings` (default + `--features diagnostics`): 0 warnings - `victory-hle -- audio`: **10/10 passed** on both default and `--features diagnostics` (including both new regression tests). LGTM.
This pull request can be merged automatically.
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/478/head:github-bot/gh-478'
git switch 'github-bot/gh-478'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-478'
git switch 'github-bot/gh-478'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-478'
git switch 'github-bot/gh-478'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-478'
git switch 'main'
git merge --squash 'github-bot/gh-478'
git switch 'main'
git merge --ff-only 'github-bot/gh-478'
git switch 'main'
git merge 'github-bot/gh-478'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!478
No description provided.