Skip to content

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

Open
nathanbzrr wants to merge 1 commit into
Marvisak:mainfrom
nathanbzrr:fix/audio-capture-timestamps-and-wav-header
Open

nathanbzrr wants to merge 1 commit into
Marvisak:mainfrom
nathanbzrr:fix/audio-capture-timestamps-and-wav-header

Conversation

@nathanbzrr

@nathanbzrr nathanbzrr commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
… errors/underflow, null 0-frame slice UB, unbounded silence allocation, and WAV header u32 overflow

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremygold02

Copy link
Copy Markdown
Contributor

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.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
…rflow, null 0-frame slice UB, unbounded silence allocation, and WAV header u32 overflow
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
…rflow, null 0-frame slice UB, unbounded silence allocation, and WAV header u32 overflow
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
…rflow, null 0-frame slice UB, unbounded silence allocation, and WAV header u32 overflow

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@nathanbzrr
nathanbzrr force-pushed the fix/audio-capture-timestamps-and-wav-header branch from 7ac2760 to 8f7df48 Compare November 25, 2026 03:13
@nathanbzrr nathanbzrr changed the title Audio capture: guard WASAPI timestamp errors/underflow, null 0-frame slice UB, unbounded silence allocation, and WAV header u32 overflow Audio capture: guard WASAPI timestamp underflow and stream silence writes Nov 25, 2026
@nathanbzrr

Copy link
Copy Markdown
Contributor Author

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

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
@fuzeofficlal

Copy link
Copy Markdown
Contributor

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.

@nathanbzrr
nathanbzrr force-pushed the fix/audio-capture-timestamps-and-wav-header branch from 8f7df48 to 896677f Compare November 26, 2026 04:05
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
@nathanbzrr
nathanbzrr force-pushed the fix/audio-capture-timestamps-and-wav-header branch from 896677f to f81e3ff Compare November 26, 2026 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants