hle(dsp): synchronize XDspSubmitCommand and publish Status atomically #3319

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

@⁠fuzeofficlal on GitHub · 2026-11-26 17:29 UTC · original
fuzeofficlal:fix/hle-dsp-status-atomic-sync → main · +333 −22 · 1 files · 1 commits · open

Summary

Fix cross-thread undefined behavior, lost command tickets, and backward Status.completed regressions in the chad.dll audio DSP HLE (crates/victory-hle/src/modules/dsp.rs).

Root Cause

XDspActivate hands title code a *const Status pointer that other threads poll for completed and result. Previously:

  1. XDspSubmitCommand forged unique &mut *stream and &mut *stream.status borrows while other threads concurrently polled *const Status or submitted commands, and XDspActivate took &*new.status before moving new into Box::into_raw.
  2. Status stored plain u32/HRESULT fields updated via ptr::write_volatile, which is neither atomic (causing cross-thread data-race UB) nor an Ordering::Release fence ensuring zeroed command.output buffers and result are visible before completed advances.
  3. Unsynchronized read-modify-write of status.submitted and multi-word volatile stores under concurrent XDspSubmitCommand calls dropped ticket increments, issued duplicate tickets, and allowed delayed threads to overwrite completed with older smaller ticket numbers.

Fix

  • Change #[repr(C)] Status fields to AtomicU32 / AtomicI32 and enforce the exact 16-byte / 4-byte-aligned C ABI layout (offsets 0, 4, 8, 12) with compile-time assertions.
  • Add a per-stream submit: Mutex<u32> to serialize XDspSubmitCommand submissions, output buffer zeroing, and status publication.
  • In XDspActivate, call Box::into_raw(new) before deriving *const Status via std::ptr::from_ref(&*(*raw).status).
  • In XDspSubmitCommand, borrow (&*stream, &*command) instead of &mut *stream, advance the ticket under stream.submit, store submitted (Release), zero command.output, store result (Relaxed), publish completed (Release), drop the lock, and write the optional *ticket out-parameter.

Testing

Added deterministic Barrier-synchronized regression tests in crates/victory-hle/src/modules/dsp.rs:

  • concurrent_submit_commands_issue_unique_monotonic_tickets: reproduces lost/duplicate tickets on main (8491 != 32000) and verifies all 32,000 concurrent submissions receive unique sequential tickets 1..=32000.
  • status_poller_observes_monotonic_completion_and_zeroed_output: reproduces backward completed regression on main (completed regressed from 51 to 50) and verifies monotonic completed <= submitted plus cross-thread Acquire/Release visibility of every zeroed output block.
  • status_abi_layout_and_ticket_wraparound: verifies Status C ABI layout and u32::MAX -> 0 -> 1 ticket wraparound.

Verified with:

  • rustfmt --check --edition 2024 crates/victory-hle/src/modules/dsp.rs
  • 20 consecutive runs of cargo test -p victory-hle --lib --locked dsp::tests
  • cargo test -p victory-hle --locked and cargo test -p victory-hle --all-features --locked
  • cargo clippy --workspace --all-targets --locked -- -D warnings (default and --all-features)
  • cargo test -p xtask --locked policy::tests::repository_passes_policy
**@⁠fuzeofficlal** on GitHub · 2026-11-26 17:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3319) `fuzeofficlal:fix/hle-dsp-status-atomic-sync` → `main` · +333 −22 · 1 files · 1 commits · open ## Summary Fix cross-thread undefined behavior, lost command tickets, and backward `Status.completed` regressions in the `chad.dll` audio DSP HLE (`crates/victory-hle/src/modules/dsp.rs`). ## Root Cause `XDspActivate` hands title code a `*const Status` pointer that other threads poll for `completed` and `result`. Previously: 1. `XDspSubmitCommand` forged unique `&mut *stream` and `&mut *stream.status` borrows while other threads concurrently polled `*const Status` or submitted commands, and `XDspActivate` took `&*new.status` before moving `new` into `Box::into_raw`. 2. `Status` stored plain `u32`/`HRESULT` fields updated via `ptr::write_volatile`, which is neither atomic (causing cross-thread data-race UB) nor an `Ordering::Release` fence ensuring zeroed `command.output` buffers and `result` are visible before `completed` advances. 3. Unsynchronized read-modify-write of `status.submitted` and multi-word volatile stores under concurrent `XDspSubmitCommand` calls dropped ticket increments, issued duplicate tickets, and allowed delayed threads to overwrite `completed` with older smaller ticket numbers. ## Fix - Change `#[repr(C)] Status` fields to `AtomicU32` / `AtomicI32` and enforce the exact 16-byte / 4-byte-aligned C ABI layout (`offsets 0, 4, 8, 12`) with compile-time assertions. - Add a per-stream `submit: Mutex<u32>` to serialize `XDspSubmitCommand` submissions, output buffer zeroing, and status publication. - In `XDspActivate`, call `Box::into_raw(new)` before deriving `*const Status` via `std::ptr::from_ref(&*(*raw).status)`. - In `XDspSubmitCommand`, borrow `(&*stream, &*command)` instead of `&mut *stream`, advance the ticket under `stream.submit`, store `submitted` (`Release`), zero `command.output`, store `result` (`Relaxed`), publish `completed` (`Release`), drop the lock, and write the optional `*ticket` out-parameter. ## Testing Added deterministic `Barrier`-synchronized regression tests in `crates/victory-hle/src/modules/dsp.rs`: - `concurrent_submit_commands_issue_unique_monotonic_tickets`: reproduces lost/duplicate tickets on `main` (`8491 != 32000`) and verifies all `32,000` concurrent submissions receive unique sequential tickets `1..=32000`. - `status_poller_observes_monotonic_completion_and_zeroed_output`: reproduces backward `completed` regression on `main` (`completed regressed from 51 to 50`) and verifies monotonic `completed <= submitted` plus cross-thread `Acquire`/`Release` visibility of every zeroed output block. - `status_abi_layout_and_ticket_wraparound`: verifies `Status` C ABI layout and `u32::MAX -> 0 -> 1` ticket wraparound. Verified with: - `rustfmt --check --edition 2024 crates/victory-hle/src/modules/dsp.rs` - 20 consecutive runs of `cargo test -p victory-hle --lib --locked dsp::tests` - `cargo test -p victory-hle --locked` and `cargo test -p victory-hle --all-features --locked` - `cargo clippy --workspace --all-targets --locked -- -D warnings` (default and `--all-features`) - `cargo test -p xtask --locked policy::tests::repository_passes_policy`
Use AtomicU32/AtomicI32 fields in #[repr(C)] Status with compile-time layout assertions, serialize XDspSubmitCommand per stream under Mutex<u32>, borrow &*stream instead of &mut *stream to eliminate cross-thread aliasing UB and data races, derive *const Status after Box::into_raw, and Release-publish completed after zeroing command.output and writing result.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 17:31:54 +00:00
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 17:39 UTC · original

Independent local verification of PR #3319 (hle(dsp): synchronize XDspSubmitCommand and publish Status atomically), head 573c13c0ae.

  • Windows, Rust 1.100.0/MSVC, cargo test --locked, CARGO_BUILD_JOBS=2, CARGO_TARGET_DIR local to the worktree (D:\Victory-Contrib\target-3319).
  • cargo test --locked -p victory-hle --lib -- --exact modules::dsp::tests::concurrent_submit_commands_issue_unique_monotonic_tickets modules::dsp::tests::status_poller_observes_monotonic_completion_and_zeroed_output modules::dsp::tests::status_abi_layout_and_ticket_wraparound -> 3 passed, 0 failed, 152 filtered, exit 0.
  • git diff --check origin/main HEAD -> clean.
  • Negative control: all three test names are absent from the merge base (63bcca43...), so they are genuinely added by this PR.
  • CPU-only synthetic tests (threads and buffers); no real title DSP traffic exercised.

AI-assisted (Hy4 flash). GPU/driver: N/A for these CPU tests. No game data used.

**@⁠cloph-dsp** on GitHub · 2026-11-26 17:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/3319#issuecomment-7199402774) Independent local verification of PR #3319 (`hle(dsp): synchronize XDspSubmitCommand and publish Status atomically`), head 573c13c0aeb8b8f78fe594c07d8775e1c9bd54f4. - Windows, Rust 1.100.0/MSVC, `cargo test --locked`, `CARGO_BUILD_JOBS=2`, `CARGO_TARGET_DIR` local to the worktree (`D:\Victory-Contrib\target-3319`). - `cargo test --locked -p victory-hle --lib -- --exact modules::dsp::tests::concurrent_submit_commands_issue_unique_monotonic_tickets modules::dsp::tests::status_poller_observes_monotonic_completion_and_zeroed_output modules::dsp::tests::status_abi_layout_and_ticket_wraparound` -> 3 passed, 0 failed, 152 filtered, exit 0. - `git diff --check origin/main HEAD` -> clean. - Negative control: all three test names are absent from the merge base (63bcca43...), so they are genuinely added by this PR. - CPU-only synthetic tests (threads and buffers); no real title DSP traffic exercised. AI-assisted (Hy4 flash). GPU/driver: N/A for these CPU tests. No game data used.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 17:49 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64:

  • Switching Status fields to AtomicU32 / AtomicI32 (with compile-time size/align/offset assertions), serializing XDspSubmitCommand via Stream::submit, and deriving the published status pointer after Box::into_raw cleanly eliminates data races and aliasing UB.
  • cargo test -p victory-hle --lib modules::dsp:: -- --test-threads=1: 4 passed, 0 failed (including concurrent_submit_commands_issue_unique_monotonic_tickets, status_abi_layout_and_ticket_wraparound, and status_poller_observes_monotonic_completion_and_zeroed_output).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 17:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/3319#pullrequestreview-6078615699) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64: - Switching `Status` fields to `AtomicU32` / `AtomicI32` (with compile-time size/align/offset assertions), serializing `XDspSubmitCommand` via `Stream::submit`, and deriving the published `status` pointer after `Box::into_raw` cleanly eliminates data races and aliasing UB. - `cargo test -p victory-hle --lib modules::dsp:: -- --test-threads=1`: 4 passed, 0 failed (including `concurrent_submit_commands_issue_unique_monotonic_tickets`, `status_abi_layout_and_ticket_wraparound`, and `status_poller_observes_monotonic_completion_and_zeroed_output`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 17:55 UTC · original

Independent verification of PR head 573c13c0aeb8b8f78fe594c07d8775e1c9bd54f4 against control main 63bcca43c6a00a758510398fa43096ce7e4f7df0 (merge base is main itself, so it merges cleanly).

Environment: Windows 11 x64, CPU only. No physical GPU was used and no game files were involved. -j 4, separate target dir.

Diff read in full (crates/victory-hle/src/modules/dsp.rs only): Status fields become atomics, and the offsets 0/4/8/12 and size 16 are pinned by const asserts. The Stream borrow is now shared (&*stream) and submissions are serialised by a Mutex<u32>. completed is stored with Release after the output buffer is zeroed. The status pointer is derived after Box::into_raw. The three new tests cover concurrent unique monotonic tickets, a poller observing monotonic completion and zeroed output, and ABI layout plus u32 ticket wraparound. I found no bug. One minor note, not a blocker: submitted is published before the output is zeroed, which is fine because pollers key off completed.

Results (PR head / main control):

  • cargo test -p victory-hle --lib dsp: 4 passed, 0 failed, 0 ignored / 1 passed (control has 1 dsp test)
  • cargo test -p victory-hle --lib: PR 154 passed, 1 failed, 0 ignored. Control 151 passed, 1 failed, 0 ignored.
    • The one failure is the same on both: modules::dstorage::tests::a_page_list_filled_after_its_request_starts (dstorage.rs:1642). It is flaky on unmodified main: it failed in 3 of 3 full-lib reruns there and 2 of 5 isolated runs (it passed in the very first control run). It is unrelated to dsp.rs and is not caused by this PR.
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean
  • cargo run -p xtask -- policy: asset policy: 191 files ok

AI disclosure: this verification was run by Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-26 17:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/3319#issuecomment-7199855423) Independent verification of PR head `573c13c0aeb8b8f78fe594c07d8775e1c9bd54f4` against control `main` `63bcca43c6a00a758510398fa43096ce7e4f7df0` (merge base is main itself, so it merges cleanly). **Environment:** Windows 11 x64, CPU only. No physical GPU was used and no game files were involved. `-j 4`, separate target dir. **Diff read in full** (`crates/victory-hle/src/modules/dsp.rs` only): `Status` fields become atomics, and the offsets 0/4/8/12 and size 16 are pinned by const asserts. The `Stream` borrow is now shared (`&*stream`) and submissions are serialised by a `Mutex<u32>`. `completed` is stored with Release after the output buffer is zeroed. The status pointer is derived after `Box::into_raw`. The three new tests cover concurrent unique monotonic tickets, a poller observing monotonic completion and zeroed output, and ABI layout plus u32 ticket wraparound. I found no bug. One minor note, not a blocker: `submitted` is published before the output is zeroed, which is fine because pollers key off `completed`. **Results (PR head / main control):** - `cargo test -p victory-hle --lib dsp`: 4 passed, 0 failed, 0 ignored / 1 passed (control has 1 dsp test) - `cargo test -p victory-hle --lib`: PR 154 passed, 1 failed, 0 ignored. Control 151 passed, 1 failed, 0 ignored. - The one failure is the same on both: `modules::dstorage::tests::a_page_list_filled_after_its_request_starts` (dstorage.rs:1642). It is flaky on unmodified main: it failed in 3 of 3 full-lib reruns there and 2 of 5 isolated runs (it passed in the very first control run). It is unrelated to dsp.rs and is not caused by this PR. - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean - `cargo run -p xtask -- policy`: `asset policy: 191 files ok` AI disclosure: this verification was run by Claude Code (Claude Sonnet 5.5).
This pull request can be merged automatically.
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/3319/head:github-bot/gh-3319'
git switch 'github-bot/gh-3319'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3319'
git switch 'github-bot/gh-3319'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3319'
git switch 'github-bot/gh-3319'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3319'
git switch 'main'
git merge --squash 'github-bot/gh-3319'
git switch 'main'
git merge --ff-only 'github-bot/gh-3319'
git switch 'main'
git merge 'github-bot/gh-3319'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3319
No description provided.