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

Copy link
Copy Markdown
Contributor

Problem

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

  • header(frames) multiplied frames * FRAME_BYTES without saturating arithmetic, could overflow 36 + data in u32 for frame counts just below 4 GiB, and used an unaligned fallback (u32::MAX - 36) that violated 4-byte nBlockAlign.
  • 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 against 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 zero-byte or null data pointers from GetBuffer.

What changed

  • Capped header data byte counts with saturating_mul and 4-byte-aligned MAX_DATA_BYTES = (u32::MAX - 36) & !(FRAME_BYTES as u32 - 1).
  • Extracted pure helper 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 both silence gaps and AUDCLNT_BUFFERFLAGS_SILENT packets.
  • Guarded std::slice::from_raw_parts with bytes > 0 && !data.is_null() and usize::try_from(bytes).
  • Added synthetic unit tests in modules::audio::capture::tests.

Tests (isolated branch 7ac2760)

  • rustfmt --edition 2024 --check crates/victory-hle/src/modules/audio/capture.rs: 0 diffs in all modified/added lines (only pre-existing lines 15-26 WAIT_OBJECT_0 import ordering left untouched to avoid conflicting with Build: Linux workspace, contributor setup and CI #112).
  • cargo test -p victory-hle --features diagnostics --lib audio::capture: 3 passed, 0 failed, 86 filtered out (modules::audio::capture::tests::header_caps_riff_size_without_overflow_and_stays_frame_aligned, modules::audio::capture::tests::timestamp_errors_and_backwards_qpc_do_not_underflow_or_anchor_zero, modules::audio::capture::tests::write_zeros_streams_exact_byte_count).
  • cargo test -p victory-hle --lib: 86 passed, 0 failed.
  • cargo clippy -p victory-hle --all-targets --all-features: passed (exit 0; 0 errors/warnings in capture.rs).
  • cargo xtask policy: asset policy: 151 files ok.

No game files. AI-assisted; reviewed and verified with the tests above.

@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. In crates/victory-hle/src/modules/audio/capture.rs, record now uses silence_frames, which skips packets with a timestamp error, anchors first relative to the frames already written, and uses checked_sub against a backwards QPC. This removes the underflow and the huge allocation that qpc - *first.get_or_insert(qpc) can cause on main. write_zeros writes silence in fixed 16 KiB chunks, and a null data packet now writes zeros and still advances written. header is back to main's version. The new #[cfg(all(windows, test, not(feature = "diagnostics")))] mod capture in audio.rs lets the two tests run under CI's default features; Win32_Media_Audio is already an unconditional windows feature. CI passes.

On your reply: all three points are addressed as described, checked on head 8f7df48. The many cfg(feature = "diagnostics") attributes are noisy but correct.


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

4 participants