HLE: update shared XMA context words atomically #1536

Closed
Texbirds wants to merge 1 commit from upstream-pr/1537 into main
Texbirds commented 2026-11-24 18:58:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-24T18:58:10Z.


Problem

In crates/victory-hle/src/modules/audio.rs, Context::set_field updates bitfields in title-shared 32-bit XMA context words using a non-atomic read_volatile -> mask/or -> write_volatile.

Because the title concurrently mutates other bitfields in the same 32-bit words while the XMA decoder thread pumps audio (for example, word 0 packs valid inputs bits 20..21 written by the title alongside write block bits 27..31 written by pump; word 1 packs input 1 size bits 0..11 alongside output writable bit 31), a concurrent title write between the read and write is silently clobbered and can stall a voice.

Fix

  • Access context words via AtomicU32::from_ptr (load(Acquire), store(Release), and fetch_update(AcqRel, Acquire, ...) in Context::set_field).
  • Add regression test context_set_field_does_not_clobber_concurrent_bitfields exercising concurrent updates to disjoint bitfields of word 0 across two threads.

Verification

  • Pending windows-latest CI (cargo clippy default + --all-features and cargo test --workspace --locked).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6589170829 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1537). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-24T18:58:10Z. --- ### Problem In `crates/victory-hle/src/modules/audio.rs`, `Context::set_field` updates bitfields in title-shared 32-bit XMA context words using a non-atomic `read_volatile` -> mask/or -> `write_volatile`. Because the title concurrently mutates other bitfields in the same 32-bit words while the XMA decoder thread pumps audio (for example, word 0 packs `valid inputs` bits 20..21 written by the title alongside `write block` bits 27..31 written by `pump`; word 1 packs `input 1 size` bits 0..11 alongside `output writable` bit 31), a concurrent title write between the read and write is silently clobbered and can stall a voice. ### Fix - Access context words via `AtomicU32::from_ptr` (`load(Acquire)`, `store(Release)`, and `fetch_update(AcqRel, Acquire, ...)` in `Context::set_field`). - Add regression test `context_set_field_does_not_clobber_concurrent_bitfields` exercising concurrent updates to disjoint bitfields of word 0 across two threads. ### Verification - Pending `windows-latest` CI (`cargo clippy` default + `--all-features` and `cargo test --workspace --locked`).
mercuryVM commented 2026-11-24 21:02:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T20:15:37Z.

Review: APPROVED


Tested 9ad1f21 on Windows 11, RTX 5070, MSVC: builds, cargo test --workspace --no-fail-fast 633 passed, 0 failed (on our fork main). The new context_set_field_does_not_clobber_concurrent_bitfields passes.

  • set_field as a fetch_update on an AtomicU32 over the shared word closes our side of the lost-update race: a title write to other bits of the same word between our read and our write is no longer overwritten. from_ptr needs 4-byte alignment, which the old read_volatile on *mut u32 already required, so nothing new is assumed.
  • get/set become acquire loads and release stores, which on x86 compile to the same plain moves as the volatile accesses, so there is no cost.
  • Limit worth noting in the code: if the title itself updates these words with a non-atomic read-modify-write, a write by our decoder in its window can still be lost. Only our half is fixed. That's still strictly better.
  • Nit: the test can only catch the old bug by chance (the race window is a few instructions), so it may pass on the old code too. It's fine as a smoke test.

Merged into our fork's main.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6046645278 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1537#pullrequestreview-6046645278). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T20:15:37Z. **Review:** APPROVED --- Tested 9ad1f21 on Windows 11, RTX 5070, MSVC: builds, `cargo test --workspace --no-fail-fast` 633 passed, 0 failed (on our fork main). The new `context_set_field_does_not_clobber_concurrent_bitfields` passes. - `set_field` as a `fetch_update` on an `AtomicU32` over the shared word closes our side of the lost-update race: a title write to other bits of the same word between our read and our write is no longer overwritten. `from_ptr` needs 4-byte alignment, which the old `read_volatile` on `*mut u32` already required, so nothing new is assumed. - `get`/`set` become acquire loads and release stores, which on x86 compile to the same plain moves as the volatile accesses, so there is no cost. - Limit worth noting in the code: if the title itself updates these words with a non-atomic read-modify-write, a write by our decoder in its window can still be lost. Only our half is fixed. That's still strictly better. - Nit: the test can only catch the old bug by chance (the race window is a few instructions), so it may pass on the old code too. It's fine as a smoke test. Merged into our fork's main. AI-assisted (Claude Code).
Marvisak commented 2026-11-25 04:06:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T03:18:59Z.


Automated triage for the maintainers.

Merge candidate. Context::set_field in crates/victory-hle/src/modules/audio.rs read-modify-wrote shared XMA context words with separate volatile accesses, so a bit the title changed in between was lost. It now uses a fetch_update on AtomicU32::from_ptr, and get and set use acquire and release ordering. I checked that every context access goes through get, set and set_field, and that the words are 4-byte aligned, which from_ptr requires. The change costs nothing when there is no contention. The test checks the title-side bits survive concurrent decoder updates. It can catch the old behaviour only probabilistically, but it cannot fail on the new code.

CI: no check runs reported for this head.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7151799303 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1537#issuecomment-7151799303). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T03:18:59Z. --- <!-- victory-triage v1 head=9ad1f21b64be4ff44f5a76b01cbf3911a0228ba3 verdict=merge-candidate at=2026-11-25T03:25:00Z --> Automated triage for the maintainers. **Merge candidate.** `Context::set_field` in `crates/victory-hle/src/modules/audio.rs` read-modify-wrote shared XMA context words with separate volatile accesses, so a bit the title changed in between was lost. It now uses a `fetch_update` on `AtomicU32::from_ptr`, and `get` and `set` use acquire and release ordering. I checked that every context access goes through `get`, `set` and `set_field`, and that the words are 4-byte aligned, which `from_ptr` requires. The change costs nothing when there is no contention. The test checks the title-side bits survive concurrent decoder updates. It can catch the old behaviour only probabilistically, but it cannot fail on the new code. CI: no check runs reported for this head. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 22:03:03 +00:00
Marvisak commented 2026-11-25 22:04:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:01:41Z.


Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing because the title has no such race. It writes a context's words only during voice setup, before the enable command, and its per-tick code only reads three fields and then moves the read block by a command, which advance_read applies under the same per-voice lock that pump holds. Where a title did write concurrently, its own plain read-modify-write would still clobber ours, so the compare-and-swap would fix half at most; the new test makes both sides use fetch_update, which is not how a title writes.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7175842937 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1537#issuecomment-7175842937). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:01:41Z. --- Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing because the title has no such race. It writes a context's words only during voice setup, before the enable command, and its per-tick code only reads three fields and then moves the read block by a command, which `advance_read` applies under the same per-voice lock that `pump` holds. Where a title did write concurrently, its own plain read-modify-write would still clobber ours, so the compare-and-swap would fix half at most; the new test makes both sides use `fetch_update`, which is not how a title writes. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

Pull request closed

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!1536
No description provided.