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

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

@⁠AscilCH on GitHub · 2026-11-25 01:23 UTC · original
AscilCH:fix/hle-dsp-atomic-status-no-mut-aliasing → main · +130 −21 · 1 files · 1 commits · open

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

**@⁠AscilCH** on GitHub · 2026-11-25 01:23 UTC · [original](https://github.com/fxceless/victory-fork/pull/483) `AscilCH:fix/hle-dsp-atomic-status-no-mut-aliasing` → `main` · +130 −21 · 1 files · 1 commits · open ## 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.)*
- Replace plain integer fields in `#[repr(C)] Status` with `AtomicU32` and
  `AtomicI32` (preserving the exact 16-byte, 4-byte aligned ABI layout) and
  borrow `&*stream` instead of `&mut *stream` / `&mut *stream.status` in
  `XDspSubmitCommand` so title threads concurrently polling the returned
  `*const Status` never trigger Rust `&mut` aliasing UB.
- Serialize ticket allocation and status updates with a per-stream mutex and
  publish `completed` with `Ordering::Release` after zeroing `command.output`
  and writing `result`, ensuring monotonic tickets and proper happens-before
  visibility across threads.
- Add a multi-threaded unit test exercising concurrent `XDspSubmitCommand`
  calls alongside a polling reader thread.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:44:24 +00:00
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/483/head:github-bot/gh-483'
git switch 'github-bot/gh-483'

Merge

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