HLE: XApu process creates misaligned f32 slice UB on unaligned output pointers (fix on a fork) #839

Closed
opened 2026-11-26 04:14:02 +00:00 by github-bot · 2 comments
Owner

@⁠AndyLatimer on GitHub · 2026-11-24 12:02 UTC · original

Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a fork branch off main (ffe310e).

Bug

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

let out = unsafe { std::slice::from_raw_parts_mut(output.cast::<f32>(), frames * channels) };

command.output is a *mut u8 from a #[repr(C, packed)] title Process command with no 4-byte alignment guarantee. Calling slice::from_raw_parts_mut on output.cast::<f32>() with any non-4-byte-aligned pointer (even when frames == 0) is Rust undefined behavior and triggers an immediate non-unwinding debug panic / abort (0xc0000409) across extern "system" fn XApuEnqueueCommand.

Two minor guards in the same module were also tightened:

  • XApuConnect did not check for a null handle from CreateEventW when RESULT_EVENT was requested (returning S_OK with a null result_event instead of freeing memory and returning E_OUTOFMEMORY).
  • ACTIVATE called OpusDecoder::new(48_000, channels as usize) before checking (1..=2).contains(&channels).

Fix

  • Write decoded PCM samples through a byte slice (std::slice::from_raw_parts_mut(output, bytes) + as_chunks_mut::<4>() + f32::to_le_bytes()) only when bytes > 0, eliminating alignment UB for both interleaved and DEINTERLEAVED_OUTPUT stereo paths.
  • Validate (1..=2).contains(&channels) before constructing OpusDecoder, and deallocate memory + return E_OUTOFMEMORY if CreateEventW returns null.
  • Added synthetic regression test modules::apu::tests::decodes_into_unaligned_output_interleaved_and_deinterleaved testing unaligned output pointers (both interleaved and deinterleaved stereo, bit-for-bit identical to aligned output), zero-frame decode at a misaligned pointer, and invalid channel counts (0, 3, u32::MAX).

Verification

  • Before fix: decodes_into_unaligned_output_interleaved_and_deinterleaved aborted with unsafe precondition(s) violated: slice::from_raw_parts_mut requires the pointer to be aligned and non-null (0xc0000409) at apu.rs:344.
  • After fix (Windows 11):
    • cargo test -p victory-hle: 95 unit tests + 1 integration test passed, 0 failed.
    • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
    • cargo check -p victory --features diagnostics: clean.
    • rustfmt --edition 2024 --check crates/victory-hle/src/modules/apu.rs: clean.
    • cargo xtask policy: 154 files ok.

Disclosure

Synthetic tests only; no game data or console SDK assets were used. Assisted by Google Antigravity.

**@⁠AndyLatimer** on GitHub · 2026-11-24 12:02 UTC · [original](https://github.com/Marvisak/VIctory/issues/839) Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a fork branch off `main` (`ffe310e`). - **Branch:** https://github.com/AndyLatimer/VIctory/tree/fix/hle-apu-unaligned-output-ub - **Commit:** https://github.com/AndyLatimer/VIctory/commit/13df0dad39c78af7c6479116cc185ab22ce44f05 ### Bug In `crates/victory-hle/src/modules/apu.rs` (`process`): ```rust let out = unsafe { std::slice::from_raw_parts_mut(output.cast::<f32>(), frames * channels) }; ``` `command.output` is a `*mut u8` from a `#[repr(C, packed)]` title `Process` command with no 4-byte alignment guarantee. Calling `slice::from_raw_parts_mut` on `output.cast::<f32>()` with any non-4-byte-aligned pointer (even when `frames == 0`) is Rust undefined behavior and triggers an immediate non-unwinding debug panic / abort (`0xc0000409`) across `extern "system" fn XApuEnqueueCommand`. Two minor guards in the same module were also tightened: - `XApuConnect` did not check for a null handle from `CreateEventW` when `RESULT_EVENT` was requested (returning `S_OK` with a null `result_event` instead of freeing `memory` and returning `E_OUTOFMEMORY`). - `ACTIVATE` called `OpusDecoder::new(48_000, channels as usize)` before checking `(1..=2).contains(&channels)`. ### Fix - Write decoded PCM samples through a byte slice (`std::slice::from_raw_parts_mut(output, bytes)` + `as_chunks_mut::<4>()` + `f32::to_le_bytes()`) only when `bytes > 0`, eliminating alignment UB for both interleaved and `DEINTERLEAVED_OUTPUT` stereo paths. - Validate `(1..=2).contains(&channels)` before constructing `OpusDecoder`, and deallocate `memory` + return `E_OUTOFMEMORY` if `CreateEventW` returns null. - Added synthetic regression test `modules::apu::tests::decodes_into_unaligned_output_interleaved_and_deinterleaved` testing unaligned output pointers (both interleaved and deinterleaved stereo, bit-for-bit identical to aligned output), zero-frame decode at a misaligned pointer, and invalid channel counts (`0`, `3`, `u32::MAX`). ### Verification - **Before fix:** `decodes_into_unaligned_output_interleaved_and_deinterleaved` aborted with `unsafe precondition(s) violated: slice::from_raw_parts_mut requires the pointer to be aligned and non-null` (`0xc0000409`) at `apu.rs:344`. - **After fix (Windows 11):** - `cargo test -p victory-hle`: 95 unit tests + 1 integration test passed, 0 failed. - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo check -p victory --features diagnostics`: clean. - `rustfmt --edition 2024 --check crates/victory-hle/src/modules/apu.rs`: clean. - `cargo xtask policy`: 154 files ok. ### Disclosure Synthetic tests only; no game data or console SDK assets were used. Assisted by Google Antigravity.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:02 +00:00
Author
Owner

@⁠nopsled0x on GitHub · 2026-11-24 12:33 UTC · original

I checked branch fix/hle-apu-unaligned-output-ub (13df0da, on top of ffe310e) on Windows 10, cargo debug build:

  • cargo test -p victory-hle: 95 unit + 1 integration tests pass, including decodes_into_unaligned_output_interleaved_and_deinterleaved.
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • From reading the diff: the only f32 cast is now a u8 slice, the byte length fits in output_length, and the interleaved/deinterleaved layout is unchanged. The channels guard and the null CreateEventW guard shouldn't change game-visible behavior, and the error path frees memory with the same layout.
  • git log ffe310e..origin/main -- crates/victory-hle/src/modules/apu.rs is empty, so main hasn't touched that file since the fork's base.

Not verified: that the new test fails on the old code (I only read the diff), rustfmt, xtask policy, or in-game audio.

AI-assisted verification (Claude); tests were run locally by me.

**@⁠nopsled0x** on GitHub · 2026-11-24 12:33 UTC · [original](https://github.com/Marvisak/VIctory/issues/839#issuecomment-7131606237) I checked branch `fix/hle-apu-unaligned-output-ub` (13df0da, on top of ffe310e) on Windows 10, cargo debug build: - `cargo test -p victory-hle`: 95 unit + 1 integration tests pass, including `decodes_into_unaligned_output_interleaved_and_deinterleaved`. - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - From reading the diff: the only `f32` cast is now a `u8` slice, the byte length fits in `output_length`, and the interleaved/deinterleaved layout is unchanged. The `channels` guard and the null `CreateEventW` guard shouldn't change game-visible behavior, and the error path frees `memory` with the same `layout`. - `git log ffe310e..origin/main -- crates/victory-hle/src/modules/apu.rs` is empty, so `main` hasn't touched that file since the fork's base. Not verified: that the new test fails on the old code (I only read the diff), rustfmt, `xtask policy`, or in-game audio. _AI-assisted verification (Claude); tests were run locally by me._
Author
Owner

@⁠wanchudao on GitHub · 2026-11-24 15:48 UTC · original

Tested AndyLatimer/VIctory's fix/hle-apu-unaligned-output-ub branch at 13df0da on
Windows 11 Pro build 26300, Ryzen 5 5500, rustc 1.98.1, MSVC 14.44.35207,
and Windows SDK 10.0.26100.0.

cargo test -p victory-hle --lib decodes_into_unaligned -- --nocapture

Result: 1 passed, 0 failed, 0 ignored, 94 filtered out. This is an additional
Windows result for the CPU-side Opus test with interleaved and deinterleaved output.

AI use — DeepSeek Harness (DeepSeek V4.1 Flash) ran the test and drafted this report.
OpenAI Codex (GPT-6.2 Sol) checked the archived result and revised it.

**@⁠wanchudao** on GitHub · 2026-11-24 15:48 UTC · [original](https://github.com/Marvisak/VIctory/issues/839#issuecomment-7136632197) Tested AndyLatimer/VIctory's fix/hle-apu-unaligned-output-ub branch at 13df0da on Windows 11 Pro build 26300, Ryzen 5 5500, rustc 1.98.1, MSVC 14.44.35207, and Windows SDK 10.0.26100.0. `cargo test -p victory-hle --lib decodes_into_unaligned -- --nocapture` Result: 1 passed, 0 failed, 0 ignored, 94 filtered out. This is an additional Windows result for the CPU-side Opus test with interleaved and deinterleaved output. AI use — DeepSeek Harness (DeepSeek V4.1 Flash) ran the test and drafted this report. OpenAI Codex (GPT-6.2 Sol) checked the archived result and revised it.
This discussion has been locked. Commenting is limited to contributors.
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#839
No description provided.