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

Open
github-bot wants to merge 1 commit from github-bot/gh-2387 into main AGit
Owner

@⁠syfyra on GitHub · 2026-11-26 16:55 UTC · original
syfyra:fix/silent-audio-host-answers → main · +72 −5 · 2 files · 1 commits · draft

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:

  • silent_audio_test gets five checks, matching the table:

    • six malformed formats on a fresh client each: IsFormatSupported and Initialize are both E_INVALIDARG, and nothing comes back in closest;
    • an initialized client with a malformed format answers AUDCLNT_E_ALREADY_INITIALIZED;
    • GetService(IAudioCaptureClient) is AUDCLNT_E_WRONG_ENDPOINT_TYPE;
    • two Resets of an unused stream are S_OK;
    • ReleaseBuffer with DATA_DISCONTINUITY, 0x10, and 0x4 with an oversized count is E_INVALIDARG, and the packet is still released afterwards.

    On main, 5 checks fail, plus one follow-on: main released the packet on the bad flag, so the later release of it fails. With this branch, 134 of 134 pass.

  • Merged together with #2133 and #2134, which also change this file and test: 139 of 139 pass. Both merge cleanly with it (scripts/pr_overlap.py from #2349).

  • scripts/check_no_game_data.py on both files.

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

**@⁠syfyra** on GitHub · 2026-11-26 16:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2387) `syfyra:fix/silent-audio-host-answers` → `main` · +72 −5 · 2 files · 1 commits · draft 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: - `silent_audio_test` gets five checks, matching the table: - six malformed formats on a fresh client each: `IsFormatSupported` and `Initialize` are both `E_INVALIDARG`, and nothing comes back in `closest`; - an initialized client with a malformed format answers `AUDCLNT_E_ALREADY_INITIALIZED`; - `GetService(IAudioCaptureClient)` is `AUDCLNT_E_WRONG_ENDPOINT_TYPE`; - two `Reset`s of an unused stream are `S_OK`; - `ReleaseBuffer` with `DATA_DISCONTINUITY`, `0x10`, and `0x4` with an oversized count is `E_INVALIDARG`, and the packet is still released afterwards. On `main`, 5 checks fail, plus one follow-on: main released the packet on the bad flag, so the later release of it fails. With this branch, 134 of 134 pass. - Merged together with #2133 and #2134, which also change this file and test: 139 of 139 pass. Both merge cleanly with it (`scripts/pr_overlap.py` from #2349). - `scripts/check_no_game_data.py` on both files. Not tested under Wine. GPUs tested: n/a (CPU tests) AI-assisted: yes (Claude Code, Claude Opus 5.5) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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](https://claude.com/claude-code)
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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:58:58 +00:00
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 18:45 UTC · original · reviewed

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.

**@⁠wavehs** on GitHub · 2026-11-26 18:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2387#pullrequestreview-6079134203) · **reviewed** **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 `WORD`s 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2387/head:github-bot/gh-2387'
git switch 'github-bot/gh-2387'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2387'
git switch 'github-bot/gh-2387'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2387'
git switch 'github-bot/gh-2387'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2387'
git switch 'main'
git merge --squash 'github-bot/gh-2387'
git switch 'main'
git merge --ff-only 'github-bot/gh-2387'
git switch 'main'
git merge 'github-bot/gh-2387'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.