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

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

@⁠AscilCH on GitHub · 2026-11-25 02:17 UTC · original
AscilCH:fix/hle-apu-unaligned-pcm-output-write → main · +126 −10 · 1 files · 1 commits · open

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

**@⁠AscilCH** on GitHub · 2026-11-25 02:17 UTC · [original](https://github.com/fxceless/victory-fork/pull/567) `AscilCH:fix/hle-apu-unaligned-pcm-output-write` → `main` · +126 −10 · 1 files · 1 commits · open ## 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.)*
- Replace `slice::from_raw_parts_mut(output.cast::<f32>(), ...)` in
  `apu::process()` with `ptr::copy_nonoverlapping` (interleaved) and
  `ptr::write_unaligned` (deinterleaved stereo) so arbitrary non-4-byte-aligned
  `*mut u8` output buffers from `#[repr(C, packed)] Process` never trigger
  Rust slice-alignment UB or debug-build panics.
- Validate `(1..=2).contains(&channels)` before calling `OpusDecoder::new`.
- Check for `CreateEventW` failure in `XApuConnect` when `RESULT_EVENT` is set,
  freeing the allocated slab and returning `E_OUTOFMEMORY`.
- Add synthetic unit test
  `unaligned_output_buffer_writes_interleaved_and_deinterleaved_pcm_safely`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:46:31 +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/567/head:github-bot/gh-567'
git switch 'github-bot/gh-567'

Merge

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