HLE/dsp: use atomic Status fields and release ordering without &mut aliasing #483

Open
AscilCH wants to merge 1 commit from upstream-pr/483 into main
AscilCH commented 2026-11-25 01:23:05 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T01:23:05Z.


Problem

In crates/victory-hle/src/modules/dsp.rs:

  1. Aliasing UB on concurrently polled Status:
    • XDspActivate hands the title a *const Status pointer (status.write(&*new.status)) that title audio/worker threads poll asynchronously.
    • XDspSubmitCommand created exclusive references &mut *stream and &mut *stream.status while title threads could concurrently read *status, causing Rust aliasing UB even with write_volatile.
  2. Missing release barrier and non-atomic ticket progression:
    • XDspSubmitCommand read status.submitted non-atomically and wrote submitted/result/completed via write_volatile (which does not provide inter-thread memory ordering over the preceding write_bytes(command.output, 0, samples) and can lose or regress tickets under concurrent submissions).

Fix

  • Change #[repr(C)] Status fields to AtomicU32 / AtomicI32 with compile-time layout assertions (size_of::<Status>() == 16, align_of::<Status>() == 4).
  • Take shared &*stream in XDspSubmitCommand, serialize ticket increments and status updates under a per-stream Mutex<()>, and publish completed with Ordering::Release after zeroing command.output and storing submitted/result.
  • Add synthetic unit test concurrent_submissions_and_polling_are_monotonic_and_atomic.

Verification

  • cargo test -p victory-hle --locked dsp::tests: 2 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6594943874 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/483). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T01:23:05Z. --- ## Problem In `crates/victory-hle/src/modules/dsp.rs`: 1. **Aliasing UB on concurrently polled `Status`**: - `XDspActivate` hands the title a `*const Status` pointer (`status.write(&*new.status)`) that title audio/worker threads poll asynchronously. - `XDspSubmitCommand` created exclusive references `&mut *stream` and `&mut *stream.status` while title threads could concurrently read `*status`, causing Rust aliasing UB even with `write_volatile`. 2. **Missing release barrier and non-atomic ticket progression**: - `XDspSubmitCommand` read `status.submitted` non-atomically and wrote `submitted`/`result`/`completed` via `write_volatile` (which does not provide inter-thread memory ordering over the preceding `write_bytes(command.output, 0, samples)` and can lose or regress tickets under concurrent submissions). ## Fix - Change `#[repr(C)] Status` fields to `AtomicU32` / `AtomicI32` with compile-time layout assertions (`size_of::<Status>() == 16`, `align_of::<Status>() == 4`). - Take shared `&*stream` in `XDspSubmitCommand`, serialize ticket increments and status updates under a per-stream `Mutex<()>`, and publish `completed` with `Ordering::Release` after zeroing `command.output` and storing `submitted`/`result`. - Add synthetic unit test `concurrent_submissions_and_polling_are_monotonic_and_atomic`. ## Verification - `cargo test -p victory-hle --locked dsp::tests`: 2 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
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/483:upstream-pr/483'
git switch 'upstream-pr/483'

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/483'
git switch 'upstream-pr/483'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/483'
git switch 'upstream-pr/483'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/483'
git switch 'main'
git merge --squash 'upstream-pr/483'
git switch 'main'
git merge --ff-only 'upstream-pr/483'
git switch 'main'
git merge 'upstream-pr/483'
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
fxceless/victory-fork!483
No description provided.