Skip to content

audio: fix 64-bit overflow in GetReadPacket's QPC timestamp (SimpleAudioSample, SysVAD) - #1439

Open
Ali Error (aliforever) wants to merge 1 commit into
microsoft:mainfrom
aliforever:fix-getreadpacket-qpc-overflow
Open

Ali Error (aliforever) wants to merge 1 commit into
microsoft:mainfrom
aliforever:fix-getreadpacket-qpc-overflow

Conversation

@aliforever

Copy link
Copy Markdown

Problem

CMiniportWaveRTStream::GetReadPacket in SimpleAudioSample and SysVAD converts the packet time from 100 ns units to performance-counter ticks with:

ULONGLONG timeOfAvailablePacketInQpc = timeOfAvailablePacketInHns * m_ullPerformanceCounterFrequency.QuadPart / 10000000;

The product timeOfAvailablePacketInHns * frequency overflows 64 bits once timeOfAvailablePacketInHns exceeds 2^64 / frequency. With the common 10 MHz frequency (for example Hyper-V guests and many current PCs), that happens after about 2.1 days of uptime. After that, every timestamp GetReadPacket returns is 2^64 / 10^7 ticks (about 51 hours) too early. Capture clients get these values as the QPC position from IAudioCaptureClient::GetBuffer. Audio data is not affected, but anything that relies on capture timestamps (echo cancellation, A/V sync, latency measurement) is.

Repro

On a machine that has been up for more than about 2.1 days, stream into the sample's capture endpoint and compare GetBuffer's QPC position with QueryPerformanceCounter. The difference is about −1.84 × 10^12 ticks instead of a few milliseconds.

Fix

Split the conversion into whole seconds and remainder:

ULONGLONG timeOfAvailablePacketInQpc = (timeOfAvailablePacketInHns / 10000000) * qpcFrequency +
                                       (timeOfAvailablePacketInHns % 10000000) * qpcFrequency / 10000000;

The result is identical to the original (floor(h·f/10^7) = q·f + floor(r·f/10^7) for h = q·10^7 + r), and the intermediate stays below 10^7 · frequency. The same line is fixed in both samples, which share this code.

Testing

  • Built SimpleAudioSample (Debug and Release, x64) and SysVAD's TabletAudioSample (Debug and Release, x64) with no new warnings.
  • Applied the same change in a driver derived from SimpleAudioSample. On a Hyper-V guest up for 4 days, capture timestamps were off by about −184,467,440 ms (2^64 / 10^7 ticks) before the fix. After it, a loopback latency measured from those timestamps came out at about 30 ms, as expected.

🤖 Generated with Claude Code

SimpleAudioSample and SysVAD convert the packet time from 100 ns units
to performance-counter ticks with

    timeOfAvailablePacketInHns * frequency / 10000000

The product overflows 64 bits once timeOfAvailablePacketInHns exceeds
2^64 / frequency, which at the common 10 MHz frequency is about 2.1
days of uptime. From then on every timestamp GetReadPacket returns is
2^64 / 10^7 ticks (about 51 hours) too early, so capture clients get
wrong packet timestamps from IAudioCaptureClient::GetBuffer.

Split the conversion into whole seconds and remainder. The result is
identical (no rounding change) and the intermediate stays below
10^7 * frequency.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aliforever
Ali Error (aliforever) requested a review from a team as a code owner November 26, 2026 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant