diff --git a/runtime/shims/silent_audio.cpp b/runtime/shims/silent_audio.cpp index 0e02186bb3..4931c10527 100644 --- a/runtime/shims/silent_audio.cpp +++ b/runtime/shims/silent_audio.cpp @@ -120,6 +120,16 @@ bool valid_format(const WAVEFORMATEX* f) { return IsEqualGUID(sub, kSubtypeFloat) || IsEqualGUID(sub, kSubtypePcm); } +// A description that is not a format at all. Windows refuses it with E_INVALIDARG, not as an unsupported format +// (measured on Windows 11 with Initialize and IsFormatSupported): no channels, no rate or no sample size, a block or +// byte rate that does not follow from them, or WAVE_FORMAT_EXTENSIBLE without room for its extension. +bool malformed_format(const WAVEFORMATEX* f) { + if (f->nChannels == 0 || f->nSamplesPerSec == 0 || f->wBitsPerSample == 0) return true; + if (f->nBlockAlign != f->nChannels * f->wBitsPerSample / 8) return true; + if (f->nAvgBytesPerSec != static_cast(f->nSamplesPerSec) * f->nBlockAlign) return true; + return f->wFormatTag == WAVE_FORMAT_EXTENSIBLE && f->cbSize < 22; +} + bool is_mix_format(const WAVEFORMATEX* f) { return f->nSamplesPerSec == kSampleRate && f->nChannels == kChannels && f->wBitsPerSample == 32 && is_float(f); } @@ -264,6 +274,7 @@ class AudioClient final : public Unknown { if (mode == AUDCLNT_SHAREMODE_EXCLUSIVE) return AUDCLNT_E_UNSUPPORTED_FORMAT; if (mode != AUDCLNT_SHAREMODE_SHARED) return E_INVALIDARG; if (!closest) return E_POINTER; + if (malformed_format(format)) return E_INVALIDARG; if (!valid_format(format)) return AUDCLNT_E_UNSUPPORTED_FORMAT; if (is_mix_format(format)) return S_OK; *closest = alloc_mix_format(); @@ -308,7 +319,8 @@ class AudioClient final : public Unknown { if (!initialized_) return AUDCLNT_E_NOT_INITIALIZED; if (running_) return AUDCLNT_E_NOT_STOPPED; if (pending_) return AUDCLNT_E_BUFFER_OPERATION_PENDING; - if (written_ == 0 && played_ == 0) return S_FALSE; + // S_OK also when there was nothing to reset (a stream never started, or reset already), as Windows answers + // (measured on Windows 11), not the S_FALSE the reference names. written_ = played_ = played_at_anchor_ = 0; return S_OK; } @@ -383,10 +395,13 @@ class AudioClient final : public Unknown { *data = frames ? scratch_.data() : nullptr; return S_OK; } - HRESULT release_buffer(UINT32 frames) { + HRESULT release_buffer(UINT32 frames, DWORD flags) { Lock l(lock_); if (!initialized_) return AUDCLNT_E_NOT_INITIALIZED; if (!pending_) return frames ? AUDCLNT_E_OUT_OF_ORDER : S_OK; + // A render packet takes AUDCLNT_BUFFERFLAGS_SILENT and nothing else but bit 31: the capture flags and every + // other bit are E_INVALIDARG, checked before the size, and the packet stays pending (measured on Windows 11). + if (frames && (flags & ~(static_cast(AUDCLNT_BUFFERFLAGS_SILENT) | 0x80000000u))) return E_INVALIDARG; if (frames > pending_frames_) return AUDCLNT_E_INVALID_SIZE; // A write after the stream ran dry plays from now on (advance re-anchors), as a device fed silence in the gap. advance(qpc_now()); @@ -438,11 +453,13 @@ class AudioClient final : public Unknown { if (mode == AUDCLNT_SHAREMODE_EXCLUSIVE) return AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED; if (mode != AUDCLNT_SHAREMODE_SHARED) return E_INVALIDARG; if (flags & AUDCLNT_STREAMFLAGS_LOOPBACK) return AUDCLNT_E_WRONG_ENDPOINT_TYPE; + Lock l(lock_); + // A client initialized already says so before it looks at the format, as Windows does (measured). + if (initialized_) return AUDCLNT_E_ALREADY_INITIALIZED; + if (malformed_format(format)) return E_INVALIDARG; // Any PCM or float format is taken, not only the mix format: the samples are thrown away, so there is // nothing to convert, and a title never fails here for asking without AUTOCONVERTPCM. if (!valid_format(format)) return AUDCLNT_E_UNSUPPORTED_FORMAT; - Lock l(lock_); - if (initialized_) return AUDCLNT_E_ALREADY_INITIALIZED; rate_ = format->nSamplesPerSec; channels_ = format->nChannels; block_ = format->nBlockAlign; @@ -528,7 +545,9 @@ class RenderClient final : public Service { public: using Service::Service; HRESULT STDMETHODCALLTYPE GetBuffer(UINT32 frames, BYTE** data) override { return client_->get_buffer(frames, data); } - HRESULT STDMETHODCALLTYPE ReleaseBuffer(UINT32 frames, DWORD) override { return client_->release_buffer(frames); } + HRESULT STDMETHODCALLTYPE ReleaseBuffer(UINT32 frames, DWORD flags) override { + return client_->release_buffer(frames, flags); + } }; class Clock final : public Service { @@ -623,6 +642,7 @@ HRESULT AudioClient::GetService(REFIID riid, void** ppv) { else if (IsEqualIID(riid, __uuidof(IAudioClock))) made = new (std::nothrow) Clock(this); else if (IsEqualIID(riid, __uuidof(ISimpleAudioVolume))) made = new (std::nothrow) SimpleVolume(this); else if (IsEqualIID(riid, __uuidof(IAudioStreamVolume))) made = new (std::nothrow) StreamVolume(this); + else if (IsEqualIID(riid, __uuidof(IAudioCaptureClient))) return AUDCLNT_E_WRONG_ENDPOINT_TYPE; // as on a render device else { log_missing_iid_once("IAudioClient::GetService", riid); return E_NOINTERFACE; diff --git a/runtime/test/silent_audio_test.cpp b/runtime/test/silent_audio_test.cpp index eac3121b9d..a8c3657844 100644 --- a/runtime/test/silent_audio_test.cpp +++ b/runtime/test/silent_audio_test.cpp @@ -367,6 +367,39 @@ void test_render_stream() { void* other = reinterpret_cast(1); check(d->Activate(__uuidof(IMMDeviceEnumerator), CLSCTX_ALL, nullptr, &other) == E_NOINTERFACE && !other, "Activate(another interface) is E_NOINTERFACE"); + // A description that is not a format is E_INVALIDARG in IsFormatSupported and Initialize, as Windows answers it: no + // channels, no rate, no sample size, a block or byte rate that does not follow, an extensible header too short. A + // fresh client each time, so one that took such a format does not carry it into the tests below. + { + bool refused = true; + for (int field = 0; field < 6; ++field) { + WAVEFORMATEX bad{WAVE_FORMAT_PCM, 2, 48000, 48000 * 4, 4, 16, 0}; + switch (field) { + case 0: bad.nChannels = 0; break; + case 1: bad.nSamplesPerSec = 0; break; + case 2: bad.wBitsPerSample = 0; break; + case 3: bad.nBlockAlign = 3; break; + case 4: bad.nAvgBytesPerSec = 1; break; + case 5: bad.wFormatTag = WAVE_FORMAT_EXTENSIBLE; break; + } + IAudioClient* fresh = activate(d, __uuidof(IAudioClient)); + if (!fresh) { + refused = false; + continue; + } + WAVEFORMATEX* closest = nullptr; + const HRESULT supported = fresh->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, &bad, &closest); + const HRESULT init = fresh->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, &bad, nullptr); + fresh->Release(); + if (supported != E_INVALIDARG || init != E_INVALIDARG || closest) { + std::printf(" malformed case %d: IsFormatSupported 0x%08lx, Initialize 0x%08lx\n", field, + static_cast(supported), static_cast(init)); + refused = false; + } + CoTaskMemFree(closest); + } + check(refused, "IsFormatSupported and Initialize refuse a malformed format with E_INVALIDARG"); + } IAudioClient3* c = nullptr; d->Activate(__uuidof(IAudioClient3), CLSCTX_ALL, nullptr, reinterpret_cast(&c)); @@ -400,6 +433,11 @@ void test_render_stream() { "Initialize(shared, event callback, mix format)"); check(c->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, mix, nullptr) == AUDCLNT_E_ALREADY_INITIALIZED, "a second Initialize is refused"); + { + WAVEFORMATEX bad{WAVE_FORMAT_PCM, 0, 48000, 0, 0, 16, 0}; + check(c->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, &bad, nullptr) == AUDCLNT_E_ALREADY_INITIALIZED, + "an initialized client says so before it looks at the format, as on the host"); + } CoTaskMemFree(mix); check(c->GetBufferSize(&frames) == S_OK && frames >= 960, "GetBufferSize: at least two periods"); REFERENCE_TIME latency = 0; @@ -422,6 +460,10 @@ void test_render_stream() { check(c->GetService(__uuidof(IAudioStreamVolume), reinterpret_cast(&stream)) == S_OK && stream, "GetService(IAudioStreamVolume)"); if (!render || !clock || !simple || !stream) return; + void* capture = reinterpret_cast(1); + check(c->GetService(__uuidof(IAudioCaptureClient), &capture) == AUDCLNT_E_WRONG_ENDPOINT_TYPE && !capture, + "GetService(IAudioCaptureClient) on the render endpoint is AUDCLNT_E_WRONG_ENDPOINT_TYPE, as on the host"); + check(c->Reset() == S_OK && c->Reset() == S_OK, "Reset of a stream with nothing to reset is S_OK, as on the host"); UINT32 channels = 0; float level = 0.0f; check(stream->GetChannelCount(&channels) == S_OK && channels == 8 && stream->SetChannelVolume(7, 0.5f) == S_OK && @@ -440,6 +482,11 @@ void test_render_stream() { if (data) std::memset(data, 0x7f, static_cast(frames) * 32); check(render->GetBuffer(1, &data) == AUDCLNT_E_OUT_OF_ORDER, "a second GetBuffer before ReleaseBuffer"); check(render->ReleaseBuffer(frames + 1, 0) == AUDCLNT_E_INVALID_SIZE, "ReleaseBuffer of more than was got"); + // A render packet takes only AUDCLNT_BUFFERFLAGS_SILENT (and bit 31): any other flag is E_INVALIDARG, before the + // size is checked, and the packet stays pending (measured on the host). + check(render->ReleaseBuffer(frames, AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY) == E_INVALIDARG && + render->ReleaseBuffer(frames, 0x10) == E_INVALIDARG && render->ReleaseBuffer(frames + 1, 0x4) == E_INVALIDARG, + "ReleaseBuffer refuses flags a render packet does not take"); check(render->ReleaseBuffer(frames, 0) == S_OK, "ReleaseBuffer(whole buffer)"); UINT32 padding = 0; check(c->GetCurrentPadding(&padding) == S_OK && padding == frames, "padding is the whole buffer before Start");