Skip to content

silent_audio: the silent endpoint answers four argument checks as Windows does (SILENT-AUDIO-HOST-ANSWERS) - #2387

Draft
syfyra wants to merge 1 commit into
ProjectLeonida:mainfrom
syfyra:fix/silent-audio-host-answers
Draft

syfyra wants to merge 1 commit into
ProjectLeonida:mainfrom
syfyra:fix/silent-audio-host-answers

Conversation

@syfyra

@syfyra syfyra commented Nov 26, 2026

Copy link
Copy Markdown

Claimed in the coordination issue as: SILENT-AUDIO-HOST-ANSWERS

What and why

I ran the same IAudioClient / IAudioRenderClient calls against the host's default render endpoint and against the silent endpoint (LEONIDA_AUDIO=silent), side by side in one probe built with silent_audio.cpp, on Windows 11, with a fresh client for each case. Most answers already match. Four calls answer differently:

call host shim on main
Initialize / IsFormatSupported with a malformed WAVEFORMATEX: nChannels, nSamplesPerSec or wBitsPerSample 0, nBlockAlign or nAvgBytesPerSec not following from them, WAVE_FORMAT_EXTENSIBLE with cbSize < 22 E_INVALIDARG AUDCLNT_E_UNSUPPORTED_FORMAT; a wrong nAvgBytesPerSec was taken as a valid format
Initialize with a malformed format on a client that is already initialized AUDCLNT_E_ALREADY_INITIALIZED AUDCLNT_E_UNSUPPORTED_FORMAT (the format was checked first)
GetService(IAudioCaptureClient) on the render client AUDCLNT_E_WRONG_ENDPOINT_TYPE E_NOINTERFACE
Reset when there is nothing to reset (never started, or reset already) S_OK S_FALSE
IAudioRenderClient::ReleaseBuffer(n, flags), n > 0, with any flag but AUDCLNT_BUFFERFLAGS_SILENT and bit 31 (one bit at a time over all 32) E_INVALIDARG, checked before the size, and the packet stays pending flags ignored, released

Each is now answered as the host does. The order of the checks follows the host too:

  • a null format or a null closest is still E_POINTER first;
  • a ReleaseBuffer with nothing pending is still AUDCLNT_E_OUT_OF_ORDER before the flags are read;
  • a 0-frame ReleaseBuffer does not look at the flags (the host returns S_OK there).

Not changed, because the answers depend on the device and engine rather than on the call: which well-formed formats are supported (the shim takes any PCM or float format on purpose), the Initialize stream-flag checks (the host's answers per bit are irregular), and the host's own Initialize refusal of 12-bit or 64-channel formats.

Tested

Windows 11, MSVC 14.44 (Build Tools), RelWithDebInfo:

Not tested under Wine.

GPUs tested: n/a (CPU tests)

AI-assisted: yes (Claude Code, Claude Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

…dows does (SILENT-AUDIO-HOST-ANSWERS)

Measured against the host's default render endpoint on Windows 11, the
same calls on each side:
- A malformed WAVEFORMATEX (no channels, rate or sample size, a block or
  byte rate that does not follow from them, an extensible header without
  room for its extension) is E_INVALIDARG in Initialize and
  IsFormatSupported; the shim said AUDCLNT_E_UNSUPPORTED_FORMAT and took
  a wrong nAvgBytesPerSec as a valid format. An initialized client
  answers AUDCLNT_E_ALREADY_INITIALIZED before it looks at the format.
- GetService(IAudioCaptureClient) is AUDCLNT_E_WRONG_ENDPOINT_TYPE, not
  E_NOINTERFACE.
- Reset is S_OK when there was nothing to reset; the shim said S_FALSE.
- IAudioRenderClient::ReleaseBuffer takes AUDCLNT_BUFFERFLAGS_SILENT and
  bit 31 only; any other flag is E_INVALIDARG, checked before the size,
  and the packet stays pending. The shim ignored the flags.

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

@wavehs wavehs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

macOS arm64 check of #2387 (SILENT-AUDIO-FORMAT-AND-FLAGS-ANSWERS). This is a WASAPI shim and its test, both Windows-only: I could not build or run silent_audio_test here, so this review is a static one plus an extracted-function harness. Everything below that I say I ran, I ran.

Tested head d38ef5384 against main bdbc7e923 (git merge-tree clean; branch 10 commits behind).

Environment: Apple M3 Pro, arm64, macOS 27.2.1, Apple clang 21.0.0, CPython 3.15.2.

Why the test does not run here
silent_audio_test is registered in the WIN32-only part of runtime/CMakeLists.txt (MSVC flags, /OPT:NOREF), and runtime/shims/silent_audio.cpp includes <windows.h>, <mmdeviceapi.h>, <audioclient.h> and <spatialaudioclient.h>; the repository vendors none of them. There is no MSVC or Wine on this machine, so the new check() calls and the malformed-format loop are unexecuted here. The four behaviours the PR adds are all described as measured on Windows 11; I have no Windows host and cannot confirm the measurements themselves.

What I did run: the new classification, extracted verbatim
I copied malformed_format (and valid_format for the "before" answer) into a small C++ harness and fed it the six shapes the new test builds, plus realistic formats:

PCM 16 stereo 48k        not malformed
PCM 24 stereo 48k        not malformed
PCM 8 mono 22050         not malformed
float32 8ch 48k          not malformed
extensible, cbSize 22    not malformed        (32-bit container)
extensible, cbSize 0     malformed

and for the test's six cases, main's path vs the PR's:

no channels        main AUDCLNT_E_UNSUPPORTED_FORMAT   head E_INVALIDARG
no rate            main AUDCLNT_E_UNSUPPORTED_FORMAT   head E_INVALIDARG
no sample size     main AUDCLNT_E_UNSUPPORTED_FORMAT   head E_INVALIDARG
block align 3      main AUDCLNT_E_UNSUPPORTED_FORMAT   head E_INVALIDARG
byte rate 1        main S_OK                           head E_INVALIDARG   <-- main accepted it
extensible cbSize 0 main AUDCLNT_E_UNSUPPORTED_FORMAT  head E_INVALIDARG

That is the negative control I can give without Windows: the byte-rate case in particular was accepted by main, because valid_format never looked at nAvgBytesPerSec, so the test catches a real hole and not just a different HRESULT. wBitsPerSample % 8 is required by valid_format and not by malformed_format, so a 12- or 20-bit container is now E_INVALIDARG where it used to be E_UNSUPPORTED_FORMAT; both are refusals of a format the shim never took, so that is only an HRESULT change.

Reading the rest of the change

  • IsFormatSupported nulls *closest at entry (:273), so the new early return leaves it null and the test's !closest holds.
  • init() has if (!format) return E_POINTER before the lock (:451), so malformed_format never sees null; moving Lock l(lock_) above valid_format is safe.
  • valid_format guards cbSize >= 22 in both places it reads the extension (is_float and its own EXTENSIBLE branch), so the cbSize < 22 case was already not an out-of-bounds read; the new check makes it a refusal with the right HRESULT, not a crash fix.
  • AudioClient::GetService writes *ppv = nullptr first (:641), so returning AUDCLNT_E_WRONG_ENDPOINT_TYPE for IAudioCaptureClient leaves the out pointer null as the test expects, and the E_NOINTERFACE log path is untouched.
  • release_buffer checks flags after the pending_ check and before the size, and returns before touching pending_, which is what the test's "packet stays pending, then ReleaseBuffer(frames, 0) succeeds" relies on.
  • The Reset change removes the S_FALSE early return, and nothing in the runtime calls IAudioClient::Reset (git grep over runtime/ finds only allocator/list Reset), so no other shim depends on the old answer.
  • Note that a malformed format in AUDCLNT_SHAREMODE_EXCLUSIVE still gets AUDCLNT_E_UNSUPPORTED_FORMAT, because the mode branch comes first; the new rule therefore holds for shared mode, which is the only mode the shim accepts anyway.

Findings

  1. Signed overflow on the garbage it is meant to reject - runtime/shims/silent_audio.cpp:128: f->nChannels * f->wBitsPerSample / 8 multiplies two WORDs after integer promotion to int, so a caller passing nChannels = 65535, wBitsPerSample = 65535 overflows int. UBSan on the extracted function aborts with signed integer overflow: 65535 * 65535 cannot be represented in type 'int' before the function can return true. This is reachable from IsFormatSupported and Initialize with a title-supplied struct, in the one function whose job is to classify exactly that input. One cast fixes it and matches the line below: static_cast<uint64_t>(f->nChannels) * f->wBitsPerSample / 8.

  2. The shim's documented answers are now stale - docs/runtime_imports.md:277 enumerates what the silent endpoint answers ("GetService gives IAudioRenderClient, IAudioClock, ISimpleAudioVolume and IAudioStreamVolume", "The client takes any PCM or float format in shared mode"). After this PR the paragraph is missing three measured answers: IAudioCaptureClient on a render endpoint is AUDCLNT_E_WRONG_ENDPOINT_TYPE, a malformed format is E_INVALIDARG rather than AUDCLNT_E_UNSUPPORTED_FORMAT, and a Reset with nothing to reset is S_OK. The file's habit elsewhere is to record the host's answer next to the behaviour, and these are the kind of detail a title's error path turns on, so a sentence there is worth the three lines.

  3. Nit in the new test - runtime/test/silent_audio_test.cpp:385-397: closest is initialised to nullptr and the refusal returns before it can be written, so || closest cannot fail; it would pin "not written" only if it started as a non-null poison value. Harmless, but it is the one check in the loop that cannot bite.

No regressions to measure on this side: no translator, harness or GPU code changes, so the shader fixtures were not re-run; the same-main baseline stands (195 PASS / 3 FAIL / 70 SKIP MoltenVK, 190 / 5 / 73 KosmicKrisp).

python -I scripts/check_no_game_data.py runtime/shims/silent_audio.cpp runtime/test/silent_audio_test.cpp: clean.

No game files. AI-assisted.

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.

2 participants