Skip to content

silent_audio: NaN volumes refused, master volume under the lock, Reset revokes objects (RT-SILENT-AUDIO-VOLUME-AND-SPATIAL) - #2521

Open
Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-silent-audio-volume-and-spatial
Open

Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-silent-audio-volume-and-spatial

Conversation

@Tyrannys

Copy link
Copy Markdown
Contributor

Package RT-SILENT-AUDIO-VOLUME-AND-SPATIAL: item 4 of @kevincommits' finding on #1608 (#1608 (comment)). Thank you for the find. Claim: #1608 (comment).

What changes (runtime/shims/silent_audio.cpp)

  1. NaN volumes are refused. SimpleVolume::SetMasterVolume, StreamVolume::SetChannelVolume and StreamVolume::SetAllVolumes tested level < 0.0f || level > 1.0f. That is false for NaN, so NaN was stored. One helper now checks level >= 0.0f && level <= 1.0f, as SpatialObject::SetVolume already does.
  2. Master volume and mute under the lock. ISimpleAudioVolume read and wrote client_->master_ and mute_ with no lock. They are now private, behind accessors that take lock_, like the channel volumes.
  3. SpatialStream::Reset revokes active objects. Microsoft's reference for ISpatialAudioObjectRenderStreamBase::Reset says resetting "causes all active ISpatialAudioObject instances to be revoked". Now each live object becomes inactive and its type can be activated again. Ends that a pass had queued are released. The stream keeps a list of its live objects (not owned). An object leaves the list first thing in its destructor, so a Reset that holds the lock still sees the whole object.

Not changed: ActivateSpatialAudioObject during an update pass. The finding suggests returning SPTLAUDCLNT_E_OUT_OF_ORDER there. Microsoft's reference for that method lists no such return code. Activating objects inside a Begin/End pass is the documented pattern for dynamic objects, and silent_audio_test itself activates its static objects that way. So this PR leaves it as it is.

Tests (silent_audio_test)

  • SetMasterVolume(NaN), SetChannelVolume(7, NaN) and SetAllVolumes with one NaN level are each refused, and the stored volumes stay as they were.
  • After Stop and Reset, a front-left object that was active reports IsActive == FALSE. GetBuffer on it gives SPTLAUDCLNT_E_RESOURCES_INVALIDATED. Front left can be activated again; on main that gives SPTLAUDCLNT_E_OBJECT_ALREADY_ACTIVE.

On main, 6 of the new checks fail. With the fix, all 135 checks pass (3 runs).

The lock change has no test of its own: a data race on a float does not show up in a test. It now follows the same pattern as the channel volumes.

Profile M: RTX 4070 SUPER, Windows 11, Ryzen 7 9800X3D, MSVC RelWithDebInfo. CPU only. The full suite was not run because a boot was running. git diff --check and scripts/check_no_game_data.py are clean. Open PRs #2133, #2134 and #2387 change other functions in this file. No game files used.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…t revokes objects (RT-SILENT-AUDIO-VOLUME-AND-SPATIAL)

From kevincommits' finding on ProjectLeonida#1608 (item 4):

- SimpleVolume::SetMasterVolume, StreamVolume::SetChannelVolume and
  StreamVolume::SetAllVolumes refuse NaN. They tested
  `level < 0.0f || level > 1.0f`, which is false for NaN, and stored it.
  One helper checks `level >= 0.0f && level <= 1.0f`, as
  SpatialObject::SetVolume does.
- ISimpleAudioVolume's master volume and mute are kept under the client's
  lock, like the channel volumes, instead of read and written bare.
- SpatialStream::Reset revokes every active object, as Microsoft's
  reference for ISpatialAudioObjectRenderStreamBase::Reset says: each is
  inactive, its type can be activated again, and queued ends are dropped.
  The stream keeps a list of its live objects (not owned); an object
  leaves it first thing in its destructor.

Not changed: ActivateSpatialAudioObject during an update pass. The
reference lists no SPTLAUDCLNT_E_OUT_OF_ORDER for it, and activating
objects inside a pass is the documented pattern (this file's own test
does it).

6 new checks in silent_audio_test fail on main and pass with the fix.

AI-assisted (Claude Code).

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

Copy link
Copy Markdown
Contributor

Reviewed at head 53ea681a0 on Windows 11 (MSVC 2022 19.44).

Verification

  • Built silent_audio_test, com_shim_test, and gta6rt.
  • silent_audio_test (5 consecutive runs): 135/135 checks passed on all 5 runs.
  • com_shim_test (5 consecutive runs): 42/42 checks passed on all 5 runs.

Findings (Request changes)

  1. Lock split between active_types_ and objects_ races with Reset() (silent_audio.cpp:775-782, 817-831, 894-897):
    • ActivateSpatialAudioObject() sets active_types_ |= t, releases lock_, allocates SpatialObject, then re-acquires lock_ to objects_.push_back(o). If Reset() runs in the gap, it clears active_types_ = 0 without seeing o in objects_; o is then pushed still active, and a second ActivateSpatialAudioObject(t) succeeds — leaving two live active spatial objects for the same static channel.
    • ~SpatialObject() has the inverse split: stream_->forget(this) and if (take_type()) stream_->release_type(type_) take lock_ separately. If Reset() runs between them and a replacement object of type_ is activated, the dying object's take_type() still returns true and clears the replacement's active_types_ bit.
    • Updating objects_ membership and active_types_ under a single lock acquisition in both activation and destruction closes both windows.
  2. Failed objects_.push_back(o) leaks the object and lets an exception escape a COM HRESULT method (silent_audio.cpp:821-833):
    If objects_.push_back(o) throws std::bad_alloc, o (and its stream_->AddRef() and active_types_ reservation) is leaked and a C++ exception crosses the COM ABI boundary. Reserving/inserting under a try { ... } catch (const std::bad_alloc&) before committing active_types_ |= t (or rolling back and returning E_OUTOFMEMORY) handles both issues together cleanly.

The NaN volume rejection and locked master volume/mute access (silent_audio.cpp:419-435, 620-626) look solid.

AI-assisted review + local MSVC verification.

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2521 | head 53ea681 (merged cleanly with main 09c1539) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^silent_audio$" --output-on-failure | red on main: silent_audio_test fails (135 check(s), 6 failure(s): SetMasterVolume/SetChannelVolume/SetAllVolumes accepted NaN via < 0.0f || > 1.0f, and SpatialStream::Reset() left active SpatialObjects active) | green on PR+main: 1/1 CTest passes (silent_audio 135/135 checks in 0.78s), check_no_game_data.py 0 violations. Source audit note: independently confirmed the two open review items in silent_audio.cpp (ActivateSpatialAudioObject / ~SpatialObject split lock windows between active_types_ and objects_ vs concurrent Reset(), and unguarded objects_.push_back(o) inside a COM HRESULT method) | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

@timbot103 timbot103 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.

Request changes after a static code audit against 53ea681a0 + ec740bcc3. The range checks reject NaN, and the master volume and mute accessors consistently take the client lock.

  • runtime/shims/silent_audio.cpp:821, :830 and :895 — Reset can run between type reservation and registration, or between unregistering and freeing a type. Either window can allow two active objects for one static type. Update the registry and reservation in one critical section on activation and final release.
  • runtime/shims/silent_audio.cpp:830 — A failed registry insertion throws out of the COM method and leaks the object, its stream reference and the type reservation. Use fixed slots or roll back and return E_OUTOFMEMORY.
  • runtime/shims/silent_audio.cpp:844 and :907 — An in-flight SetEndOfStream can enqueue a revoked object after Reset drains the queue, keeping the object and stream alive if no later call drains it. Recheck activity under the stream lock before enqueueing.
  • runtime/shims/silent_audio.cpp:719 — A valid SetVolume call still succeeds on an object revoked by Reset. Return SPTLAUDCLNT_E_RESOURCES_INVALIDATED for that object.

The tests cover NaN refusal and sequential revocation. Cover the Reset races and allocation failure with deterministic tests, and make the existing reactivation check require a successful Begin.

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.

3 participants