Audio capture: guard WASAPI timestamp underflow and stream silence writes #478
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/478"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by nathanbzrr on 2026-11-24T03:50:27Z.
Problem
crates/victory-hle/src/modules/audio/capture.rshad four issues in the diagnostics WAV recorder:header(frames)computedframes * FRAME_BYTES as u64without saturation and only fell back tou32::MAX - 36onu32::try_fromfailure, allowing36 + datato overflow inu32whenframes * 4landed within 36 bytes ofu32::MAX.qpc - *first.get_or_insert(qpc)without checkingAUDCLNT_BUFFERFLAGS_TIMESTAMP_ERROR, adjusting the initial timestamp anchor if earlier packets had invalid timestamps, guarding backwards QPC values (checked_sub), or widening intermediateu64math tou128.AUDCLNT_BUFFERFLAGS_SILENTpackets allocated full-gapVec<u8>buffers instead of streaming fixed chunks.std::slice::from_raw_parts(data, bytes)did not guard against nulldatapointers fromGetBuffer.What changed (
f81e3ff)header(frames)withframes.saturating_mul(FRAME_BYTES as u64).min(u64::from(u32::MAX - 36)) as u32so neitherframes * FRAME_BYTESnor36 + datacan overflow.silence_framesto skipAUDCLNT_BUFFERFLAGS_TIMESTAMP_ERRORpackets without anchoringfirst, anchorfirstrelative to already-written frames, guard backwards QPC withchecked_sub, and compute frame positions inu128.write_zerosstreaming[0u8; 16 * 1024]chunks for silence gaps and forAUDCLNT_BUFFERFLAGS_SILENT || data.is_null()packets, keepingwrittenadvancing withsaturating_add.modules::audio::captureunder#[cfg(all(windows, test, not(feature = "diagnostics")))]incrates/victory-hle/src/modules/audio.rs(gating runtime WASAPI items onfeature = "diagnostics") so default-featurecargo test --workspaceexercises the unit tests too.modules::audio::capture::tests:timestamp_errors_and_backwards_qpc_do_not_underflow_or_anchor_zero,write_zeros_streams_exact_byte_count, andheader_clamps_data_and_riff_sizes_without_overflow.Verification (
f81e3ff, Windows MSVC)cargo run -p xtask --locked -- policy&cargo test -p xtask --locked: cleancargo clippy --workspace --all-targets --locked -- -D warnings(default &--all-features): cleancargo test -p victory-hle --locked audio::capture(default &--all-features): 3 passed, 0 failedNo game files. AI-assisted; reviewed and verified with synthetic unit tests.
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T06:04:45Z.
Review: APPROVED
Tested
7ac2760on Windows 11, RTX 5070, MSVC:cargo test --workspace --no-fail-fastpasses (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) & !3keeps36 + datafrom overflowing, and the data length stays a whole number of frames. The test's 4294967256 matches.silence_frames:TIMESTAMP_ERRORneither anchor nor pad.qpcminus the time already written, so a late first good timestamp does not insert a burst of silence for frames already written.checked_sub) pads nothing instead of underflowing.write_zerosremoves the unboundedvec![0; gap], which a long gap could make huge.datanow writes nothing and does not advancewritten. The next packet's silence check then makes up the gap, which is a reasonable outcome.No issues found.
AI-assisted (Claude Code).
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=1passed 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, andwrite_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.
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 incrates/victory-hle/src/modules/audio/capture.rsthat was checked last time:silence_framesskips timestamp-error packets without anchoring, useschecked_subfor QPC running backwards, and does its math inu128.write_zeroswrites in chunks.headersaturates and clamps data tou32::MAX - 36, so36 + datacan't overflow. The PR body now describes that clamp correctly. Thecfg(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
[PR #478] Audio capture: guard WASAPI timestamp errors/underflow, null 0-frame slice UB, unbounded silence allocation, and WAV header u32 overflowto [PR #478] Audio capture: guard WASAPI timestamp underflow and stream silence writesImported 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 head8f7df48):bytes > 0 && !data.is_null()check sorecordwrites zeros viawrite_zerosonAUDCLNT_BUFFERFLAGS_SILENT || data.is_null()and always advanceswritten = written.saturating_add(u64::from(frames)).headercap: revertedheader()toorigin/main's implementation (and droppedheader_caps_riff_size_without_overflow_and_stays_frame_aligned), keeping the diff focused on the timestamp underflow guard and chunked zero writes.diagnostics: compiledmodules::audio::captureunder#[cfg(all(windows, test, not(feature = diagnostics)))]incrates/victory-hle/src/modules/audio.rs(keepingpub mod capturegated ondiagnostics) and gated runtime WASAPI items with#[cfg(feature = diagnostics)]so default-featurecargo test --workspaceruns bothcapturetests.Verified on
8f7df48(Windows MSVC):cargo run -p xtask -- policy --staged:asset policy: 2 files okcargo clippy --workspace --all-targets -- -D warnings&--all-features: cleancargo test -p victory-hle --locked capture(default features &--all-features): 2 passed, 0 failedcargo test --workspace --locked: 0 failures across all workspace cratesImported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T01:08:24Z.
Code review and Windows 11 verification at head
8f7df4890ad0merged cleanly ontomain(63bcca43c6a0):crates/victory-hle/src/modules/audio/capture.rs:134-162, 252,audio.rs:507):silence_framesreturns0onAUDCLNT_BUFFERFLAGS_TIMESTAMP_ERRORwithout anchoringfirst, back-computesfirstwithu128+saturating_sub, guards backwards QPC jumps viachecked_sub, and converts timestamps to frames inu128without overflow.write_zerosstreams from a fixed 16 KiB static zero buffer instead of allocating a potentially hugeVec, andrecord()routesAUDCLNT_BUFFERFLAGS_SILENT || data.is_null()safely throughwrite_zeros. Default vs--features diagnosticscfg gating compiles cleanly with 0 warnings.cargo xtask policy:191 files ok;cargo clippy --locked -p victory-hle --tests -- -D warnings(default +--features diagnostics): 0 warningsvictory-hle -- audio: 10/10 passed on both default and--features diagnostics(including both new regression tests).LGTM.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.