HLE/apu: avoid unaligned &mut [f32] slice UB in Opus PCM output and validate channels #567

Open
AscilCH wants to merge 1 commit from upstream-pr/567 into main
AscilCH commented 2026-11-25 02:17:26 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T02:17:26Z.


Problem

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

  1. Unaligned *mut f32 slice creation UB (process()): Process::output is an arbitrary *mut u8 from a #[repr(C, packed)] command struct. process() unconditionally constructed std::slice::from_raw_parts_mut(output.cast::<f32>(), frames * channels) without checking 4-byte alignment — immediate Rust UB (and a debug-precondition abort across FFI) whenever output as usize % 4 != 0.
  2. Channel count validated after OpusDecoder::new: ACTIVATE passed arbitrary channels as usize directly into OpusDecoder::new before checking (1..=2).contains(&channels).
  3. Unchecked CreateEventW failure: When RESULT_EVENT was requested and CreateEventW returned null, XApuConnect still returned S_OK with a null event handle.

Fix

  • Write interleaved PCM via ptr::copy_nonoverlapping and deinterleaved stereo PCM via ptr::write_unaligned, avoiding &mut [f32] creation over title *mut u8 pointers.
  • Reject invalid channels before calling OpusDecoder::new.
  • Free memory and return E_OUTOFMEMORY if CreateEventW returns null when RESULT_EVENT is set.
  • Add synthetic unit test unaligned_output_buffer_writes_interleaved_and_deinterleaved_pcm_safely.

Verification

  • cargo test -p victory-hle --locked apu::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:6595634252 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/567). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T02:17:26Z. --- ## Problem In `crates/victory-hle/src/modules/apu.rs`: 1. **Unaligned `*mut f32` slice creation UB (`process()`)**: `Process::output` is an arbitrary `*mut u8` from a `#[repr(C, packed)]` command struct. `process()` unconditionally constructed `std::slice::from_raw_parts_mut(output.cast::<f32>(), frames * channels)` without checking 4-byte alignment — immediate Rust UB (and a debug-precondition abort across FFI) whenever `output as usize % 4 != 0`. 2. **Channel count validated after `OpusDecoder::new`**: `ACTIVATE` passed arbitrary `channels as usize` directly into `OpusDecoder::new` before checking `(1..=2).contains(&channels)`. 3. **Unchecked `CreateEventW` failure**: When `RESULT_EVENT` was requested and `CreateEventW` returned null, `XApuConnect` still returned `S_OK` with a null event handle. ## Fix - Write interleaved PCM via `ptr::copy_nonoverlapping` and deinterleaved stereo PCM via `ptr::write_unaligned`, avoiding `&mut [f32]` creation over title `*mut u8` pointers. - Reject invalid `channels` before calling `OpusDecoder::new`. - Free `memory` and return `E_OUTOFMEMORY` if `CreateEventW` returns null when `RESULT_EVENT` is set. - Add synthetic unit test `unaligned_output_buffer_writes_interleaved_and_deinterleaved_pcm_safely`. ## Verification - `cargo test -p victory-hle --locked apu::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/567:upstream-pr/567'
git switch 'upstream-pr/567'

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