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

Open
nathanbzrr wants to merge 1 commit from upstream-pr/478 into main
nathanbzrr commented 2026-11-24 03:50:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nathanbzrr on 2026-11-24T03:50:27Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6574192799 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/478). Originally posted by [nathanbzrr](https://github.com/nathanbzrr) on 2026-11-24T03:50:27Z. --- ## 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.
mercuryVM commented 2026-11-24 06:53:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T06:04:45Z.

Review: APPROVED


Tested 7ac2760 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed). These are diagnostics-only code paths, and I have not run a live capture.

  • header: MAX_DATA_BYTES = (u32::MAX - 36) & !3 keeps 36 + data from overflowing, and the data length stays a whole number of frames. The test's 4294967256 matches.
  • silence_frames:
    • Packets flagged TIMESTAMP_ERROR neither anchor nor pad.
    • The anchor is qpc minus the time already written, so a late first good timestamp does not insert a burst of silence for frames already written.
    • A backwards QPC (checked_sub) pads nothing instead of underflowing.
  • Streaming write_zeros removes the unbounded vec![0; gap], which a long gap could make huge.
  • A non-silent packet with a null data now writes nothing and does not advance written. The next packet's silence check then makes up the gap, which is a reasonable outcome.

No issues found.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6034458700 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/478#pullrequestreview-6034458700). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T06:04:45Z. **Review:** APPROVED --- Tested 7ac2760 on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (265 passed, 0 failed). These are diagnostics-only code paths, and I have not run a live capture. - `header`: `MAX_DATA_BYTES = (u32::MAX - 36) & !3` keeps `36 + data` from overflowing, and the data length stays a whole number of frames. The test's 4294967256 matches. - `silence_frames`: - Packets flagged `TIMESTAMP_ERROR` neither anchor nor pad. - The anchor is `qpc` minus the time already written, so a late first good timestamp does not insert a burst of silence for frames already written. - A backwards QPC (`checked_sub`) pads nothing instead of underflowing. - Streaming `write_zeros` removes the unbounded `vec![0; gap]`, which a long gap could make huge. - A non-silent packet with a null `data` now writes nothing and does not advance `written`. The next packet's silence check then makes up the gap, which is a reasonable outcome. No issues found. AI-assisted (Claude Code).
jeremygold02 commented 2026-11-24 18:05:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T17:35:06Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139411238 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/478#issuecomment-7139411238). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T17:35:06Z. --- 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.
Marvisak commented 2026-11-25 02:06:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:27:41Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149571364 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/478#issuecomment-7149571364). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:27:41Z. --- <!-- 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)_
upstream-sync changed title from [PR #478] Audio capture: guard WASAPI timestamp errors/underflow, null 0-frame slice UB, unbounded silence allocation, and WAV header u32 overflow to [PR #478] Audio capture: guard WASAPI timestamp underflow and stream silence writes 2026-11-25 04:04:36 +00:00
nathanbzrr commented 2026-11-25 04:06:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nathanbzrr on 2026-11-25T03:14:02Z.


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
<!-- greyfinch-sync:Marvisak/VIctory:comment:7151719119 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/478#issuecomment-7151719119). Originally posted by [nathanbzrr](https://github.com/nathanbzrr) on 2026-11-25T03:14:02Z. --- 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
fuzeofficlal commented 2026-11-26 02:06:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T01:08:24Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179472194 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/478#issuecomment-7179472194). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T01:08:24Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/478:upstream-pr/478'
git switch 'upstream-pr/478'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/478'
git switch 'upstream-pr/478'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/478'
git switch 'upstream-pr/478'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/478'
git switch 'main'
git merge --squash 'upstream-pr/478'
git switch 'main'
git merge --ff-only 'upstream-pr/478'
git switch 'main'
git merge 'upstream-pr/478'
git push origin 'main'
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
Marvisak/VIctory!478
No description provided.