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

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3319 into main
fuzeofficlal commented 2026-11-26 17:29:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T17:29:36Z.


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
<!-- greyfinch-sync:Marvisak/VIctory:issue:6631981077 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3319). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T17:29:36Z. --- ## 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`
cloph-dsp commented 2026-11-26 17:39:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T17:39:27Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7199402774 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3319#issuecomment-7199402774). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T17:39:27Z. --- 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.
Dukestar69 commented 2026-11-26 17:49:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T17:49:42Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6078615699 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3319#pullrequestreview-6078615699). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T17:49:42Z. **Review:** APPROVED --- 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).
oni-swr commented 2026-11-26 17:55:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T17:55:43Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7199855423 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3319#issuecomment-7199855423). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-26T17:55:43Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3319:upstream-pr/3319'
git switch 'upstream-pr/3319'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3319'
git switch 'upstream-pr/3319'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3319'
git switch 'upstream-pr/3319'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3319'
git switch 'main'
git merge --squash 'upstream-pr/3319'
git switch 'main'
git merge --ff-only 'upstream-pr/3319'
git switch 'main'
git merge 'upstream-pr/3319'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3319
No description provided.