HLE: update shared XMA context fields atomically and publish read offset before clearing valid #3290

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

@⁠fuzeofficlal on GitHub · 2026-11-26 16:06 UTC · original
fuzeofficlal:fix/hle-xma-context-atomic-fields → main · +335 −7 · 1 files · 1 commits · open

Summary

Fix concurrent lost-update races and release-ordering bugs on shared XMA hardware context words in crates/victory-hle/src/modules/audio.rs:

  1. Atomic bitfield updates on shared XMA context words (Context::get, Context::set_field):

    • Retail double-buffered XMA streaming updates the inactive input bank's packet count (word 0 bits 0..11 or word 1 bits 0..11) and sets its valid bit (word 0 bit 20 or 21) concurrently while the XMA decoder thread advances the output write block (word 0 bits 27..31), decrements loop counts (word 0 bits 12..19), clears consumed valid bits (word 0 bits 20..21), and toggles the output-writable flag (word 1 bit 31, also updated by ADVANCE_XMA_READ).
    • Previously, Context::set_field performed a non-atomic read_volatile -> mask -> write_volatile sequence, which silently clobbered concurrent title atomic updates to valid0/valid1 and input bank packet counts (causing live streams to drop queued banks or fail permanently with INPUT_INVALID), and constituted a cross-thread data race without Acquire/Release synchronization.
    • Context now accesses the 4-byte-aligned words in the live XMA context array via AtomicU32 (Acquire loads and AcqRel/Acquire fetch_update CAS loops in set_field), preserving unrelated bitfields written concurrently and synchronizing buffer payloads with context flags.
  2. Publish read_bit reset before clearing valid on bank completion (decode):

    • Previously, decode cleared valid[current] in word 0 before resetting word 2's read bit offset to 0, allowing a title polling valid == 0 to observe a stale non-zero read offset or have a freshly written offset overwritten.
    • decode now writes word 2's updated read offset before clearing the finished bank's valid bit with AcqRel ordering.

Testing

All verification runs executed on Windows (cargo ... --locked), no game data used:

  • Added three synthetic concurrency regression tests in crates/victory-hle/src/modules/audio.rs:
    • xma_set_field_preserves_concurrent_atomic_updates_to_shared_words
    • xma_double_buffered_streaming_preserves_concurrent_bank_refills
    • xma_end_of_input_updates_read_bit_before_clearing_valid
  • Verified all three fail cleanly on 63bcca43 before the fix (lost valid1=9, lost input1 size=1114; lost valid1=224, lost input1 size=176; stale_read_bit=8 in 0.09s) and pass after the fix.
  • cargo test -p victory-hle --lib --locked modules::audio::tests:: — 11 passed (plus 20/20 repeated stress runs of the three new tests).
  • cargo test -p victory-hle --locked and --all-features — 155 lib + integration tests passed.
  • cargo clippy --workspace --all-targets --locked -- -D warnings (default and --all-features) — 0 warnings.
  • cargo test -p xtask --locked policy::tests::repository_passes_policy — passed.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 16:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/3290) `fuzeofficlal:fix/hle-xma-context-atomic-fields` → `main` · +335 −7 · 1 files · 1 commits · open ## Summary Fix concurrent lost-update races and release-ordering bugs on shared XMA hardware context words in `crates/victory-hle/src/modules/audio.rs`: 1. **Atomic bitfield updates on shared XMA context words (`Context::get`, `Context::set_field`)**: - Retail double-buffered XMA streaming updates the inactive input bank's packet count (word 0 bits 0..11 or word 1 bits 0..11) and sets its valid bit (word 0 bit 20 or 21) concurrently while the XMA decoder thread advances the output write block (word 0 bits 27..31), decrements loop counts (word 0 bits 12..19), clears consumed valid bits (word 0 bits 20..21), and toggles the output-writable flag (word 1 bit 31, also updated by `ADVANCE_XMA_READ`). - Previously, `Context::set_field` performed a non-atomic `read_volatile` -> mask -> `write_volatile` sequence, which silently clobbered concurrent title atomic updates to `valid0`/`valid1` and input bank packet counts (causing live streams to drop queued banks or fail permanently with `INPUT_INVALID`), and constituted a cross-thread data race without Acquire/Release synchronization. - `Context` now accesses the 4-byte-aligned words in the live XMA context array via `AtomicU32` (`Acquire` loads and `AcqRel`/`Acquire` `fetch_update` CAS loops in `set_field`), preserving unrelated bitfields written concurrently and synchronizing buffer payloads with context flags. 2. **Publish `read_bit` reset before clearing `valid` on bank completion (`decode`)**: - Previously, `decode` cleared `valid[current]` in word 0 before resetting word 2's read bit offset to `0`, allowing a title polling `valid == 0` to observe a stale non-zero read offset or have a freshly written offset overwritten. - `decode` now writes word 2's updated read offset before clearing the finished bank's valid bit with `AcqRel` ordering. ## Testing All verification runs executed on Windows (`cargo ... --locked`), no game data used: - Added three synthetic concurrency regression tests in `crates/victory-hle/src/modules/audio.rs`: - `xma_set_field_preserves_concurrent_atomic_updates_to_shared_words` - `xma_double_buffered_streaming_preserves_concurrent_bank_refills` - `xma_end_of_input_updates_read_bit_before_clearing_valid` - Verified all three fail cleanly on `63bcca43` before the fix (`lost valid1=9, lost input1 size=1114`; `lost valid1=224, lost input1 size=176`; `stale_read_bit=8` in 0.09s) and pass after the fix. - `cargo test -p victory-hle --lib --locked modules::audio::tests::` — 11 passed (plus 20/20 repeated stress runs of the three new tests). - `cargo test -p victory-hle --locked` and `--all-features` — 155 lib + integration tests passed. - `cargo clippy --workspace --all-targets --locked -- -D warnings` (default and `--all-features`) — 0 warnings. - `cargo test -p xtask --locked policy::tests::repository_passes_policy` — passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:10:16 +00:00
Author
Owner

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

Reviewed the diff and verified locally on Windows 11 x64:

  • Replacing volatile read-modify-write in Context::set_field with AtomicU32::fetch_update (AcqRel/Acquire) and publishing the reset read offset (word 2) before clearing the bank valid bit (word 0) cleanly prevents lost concurrent bank updates and premature bank handoff.
  • cargo test -p victory-hle --lib modules::audio:: -- --test-threads=1: 11 passed, 0 failed (including xma_set_field_preserves_concurrent_atomic_updates_to_shared_words, xma_end_of_input_updates_read_bit_before_clearing_valid, and xma_double_buffered_streaming_preserves_concurrent_bank_refills).
  • 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 16:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/3290#pullrequestreview-6077265319) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64: - Replacing volatile read-modify-write in `Context::set_field` with `AtomicU32::fetch_update` (`AcqRel`/`Acquire`) and publishing the reset read offset (`word 2`) before clearing the bank valid bit (`word 0`) cleanly prevents lost concurrent bank updates and premature bank handoff. - `cargo test -p victory-hle --lib modules::audio:: -- --test-threads=1`: 11 passed, 0 failed (including `xma_set_field_preserves_concurrent_atomic_updates_to_shared_words`, `xma_end_of_input_updates_read_bit_before_clearing_valid`, and `xma_double_buffered_streaming_preserves_concurrent_bank_refills`). - `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 16:55 UTC · original

Independent verification of #3290 (claimed on #1633). Test report only, no code changes. AI use: Claude Code (Claude Sonnet 5.5), run on behalf of oni-swr.

  • Head: 9ad6acbcaaf9d7a0d21190ed514cb8c163f925e0. Control: main = 63bcca43c6a00a758510398fa43096ce7e4f7df0 (the PR's merge base).
  • Windows 11 Pro, CPU only (HLE needs no GPU). No game files. I found no CONTRIBUTING.md in the repository, so I followed the #1633 rules.
  • The existing approving review on this head ran only the modules::audio:: filter with --test-threads=1, and had no control on main, so I did not treat it as covering this.
  • Commands: cargo test -j 4 -p victory-hle --lib --locked on head and on the control; cargo test -j 4 -p victory-hle --locked on head; the 3 new tests run 10 times on head and 6 times on the control.
  • Control = main with only the PR's three new tests copied in (the test block verbatim, plus use std::sync::atomic::{AtomicU32, Ordering}; at the top of audio.rs, which the tests need). No source change.
Run Result
head, lib suite 155 passed, 0 failed
head, whole package (lib + 2 integration + doc) all ok (155 + 1 + 1)
head, the 3 new tests, 10 runs 3 passed every time
main, plain lib suite 152 passed, 0 failed
main + the 3 new tests, lib suite 152 passed, 3 failed
main + the 3 new tests, 3 tests run 5 more times 3 failed every time

All three claimed tests exist at head: xma_set_field_preserves_concurrent_atomic_updates_to_shared_words, xma_double_buffered_streaming_preserves_concurrent_bank_refills, xma_end_of_input_updates_read_bit_before_clearing_valid.

Do they catch a real bug on main? Yes, all three, deterministically in 6 of 6 control runs. Failure messages on main: lost valid1=434, lost input1 size=7329 (set_field test), lost valid1=75, lost input1 size=368 (double-buffered test), and a stale read-bit assertion in the third. The first two simulate the title's updates with their own atomic operations in the test, so what they prove is that the old volatile read-modify-write loses concurrent updates.

No flakes seen in these runs. Not verified: real XMA data or a title, other CPUs or memory models, clippy and policy xtask.

**@⁠oni-swr** on GitHub · 2026-11-26 16:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/3290#issuecomment-7198268971) Independent verification of #3290 (claimed on #1633). Test report only, no code changes. AI use: Claude Code (Claude Sonnet 5.5), run on behalf of oni-swr. - Head: `9ad6acbcaaf9d7a0d21190ed514cb8c163f925e0`. Control: `main` = `63bcca43c6a00a758510398fa43096ce7e4f7df0` (the PR's merge base). - Windows 11 Pro, CPU only (HLE needs no GPU). No game files. I found no CONTRIBUTING.md in the repository, so I followed the #1633 rules. - The existing approving review on this head ran only the `modules::audio::` filter with `--test-threads=1`, and had no control on main, so I did not treat it as covering this. - Commands: `cargo test -j 4 -p victory-hle --lib --locked` on head and on the control; `cargo test -j 4 -p victory-hle --locked` on head; the 3 new tests run 10 times on head and 6 times on the control. - Control = main with only the PR's three new tests copied in (the test block verbatim, plus `use std::sync::atomic::{AtomicU32, Ordering};` at the top of `audio.rs`, which the tests need). No source change. | Run | Result | |---|---| | head, lib suite | 155 passed, 0 failed | | head, whole package (lib + 2 integration + doc) | all ok (155 + 1 + 1) | | head, the 3 new tests, 10 runs | 3 passed every time | | main, plain lib suite | 152 passed, 0 failed | | main + the 3 new tests, lib suite | 152 passed, **3 failed** | | main + the 3 new tests, 3 tests run 5 more times | 3 failed every time | All three claimed tests exist at head: `xma_set_field_preserves_concurrent_atomic_updates_to_shared_words`, `xma_double_buffered_streaming_preserves_concurrent_bank_refills`, `xma_end_of_input_updates_read_bit_before_clearing_valid`. **Do they catch a real bug on main?** Yes, all three, deterministically in 6 of 6 control runs. Failure messages on main: `lost valid1=434, lost input1 size=7329` (set_field test), `lost valid1=75, lost input1 size=368` (double-buffered test), and a stale read-bit assertion in the third. The first two simulate the title's updates with their own atomic operations in the test, so what they prove is that the old volatile read-modify-write loses concurrent updates. No flakes seen in these runs. Not verified: real XMA data or a title, other CPUs or memory models, clippy and policy xtask.
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/3290/head:github-bot/gh-3290'
git switch 'github-bot/gh-3290'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3290'
git switch 'github-bot/gh-3290'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3290'
git switch 'github-bot/gh-3290'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3290'
git switch 'main'
git merge --squash 'github-bot/gh-3290'
git switch 'main'
git merge --ff-only 'github-bot/gh-3290'
git switch 'main'
git merge 'github-bot/gh-3290'
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!3290
No description provided.