hle/acphal: fix APU range overflow/overlap, SHAPE rollback, and XApuConnect OOM #90

Manually merged
github merged 1 commit from gh-pr/90/head into gh-pr/90/base 2026-11-26 04:09:23 +00:00
Owner

@⁠certified-retart on GitHub · 2026-11-23 02:57 UTC · original
certified-retart:fix/acphal-overflow-and-oom → main · +316 −32 · 1 files · 1 commits · merged 2026-11-23 10:47 UTC

Summary

Hardens overflow handling, range allocation, rollback cleanup, and OOM propagation in hle::acphal:

  • Non-wrapping, non-overlapping 32-bit APU range allocator (Apu::address):
    • Previously, (size.max(1) + 0xFFF) & !0xFFF overflowed u32 (panicking in debug builds or stalling self.next at +0 in release) and wrapping self.next could cause new APU ranges to cross u32::MAX or overlap live mappings, breaking ApuMapApuAddress reverse lookups.
    • Apu::address is now fallible (Option<u32>), rejects ranges that cannot fit without wrapping u32, scans past live regions when reclaiming space from APU_BASE, reuses existing regions for non-owned mappings only when the full requested span is covered, and always registers owned ApuAlloc entries distinctly (ApuFree also prefers matching owned entries). ApuAlloc frees its VirtualAlloc block on APU range exhaustion.
  • Checked arithmetic & null-pointer validation:
    • Uses checked alignment/reserve arithmetic in alloc_aligned.
    • Rejects null va in ApuMapVirtualAddress with E_INVALIDARG.
    • Uses Instant::checked_sub in Channel::pop instead of panicking now - FRAME * 8.
  • Transactional AcpHalAllocateShapeContexts:
    • Validates all 5 counts[i].checked_mul(CONTEXT_BYTES) products upfront instead of saturating_mul.
    • Rolls back partial ApuAlloc allocations on failure without leaking or disturbing previously live SHAPE contexts, publishes output pointers under SHAPE.lock(), and frees replaced prior allocations outside the lock.
  • XApuConnect OOM & partial-resource cleanup:
    • Propagates E_OUTOFMEMORY instead of silently returning S_OK with output.data == NULL when alloc_aligned, CreateEventW, or channels.try_reserve_exact fails, releasing any already-acquired buffer/event and clearing output and *handle.

Verification

  • cargo test -p xcl --lib hle::acphal -- --test-threads=1: 6 passed; 0 failed
    • Added apu_address_prevents_overflow_overlap_and_rejects_null_va (verifies u32::MAX rejection, non-overlapping wraparound past live regions, owned vs unowned separation, and null va rejection).
    • Added shape_contexts_overflow_and_partial_failure_roll_back (verifies counts[i] * CONTEXT_BYTES overflow, real ApuAlloc rollback on partial failure, preservation of live allocations on failed replacement, and cleanup on successful replacement).
    • Added xapu_connect_returns_oom_and_cleans_up_on_failure (verifies E_OUTOFMEMORY, output clearing, and VirtualQuery MEM_FREE release of the data buffer when event creation fails).
**@⁠certified-retart** on GitHub · 2026-11-23 02:57 UTC · [original](https://github.com/xcl-dev/xcl/pull/90) `certified-retart:fix/acphal-overflow-and-oom` → `main` · +316 −32 · 1 files · 1 commits · merged 2026-11-23 10:47 UTC ## Summary Hardens overflow handling, range allocation, rollback cleanup, and OOM propagation in `hle::acphal`: - **Non-wrapping, non-overlapping 32-bit APU range allocator (`Apu::address`):** - Previously, `(size.max(1) + 0xFFF) & !0xFFF` overflowed `u32` (panicking in debug builds or stalling `self.next` at `+0` in release) and wrapping `self.next` could cause new APU ranges to cross `u32::MAX` or overlap live mappings, breaking `ApuMapApuAddress` reverse lookups. - `Apu::address` is now fallible (`Option<u32>`), rejects ranges that cannot fit without wrapping `u32`, scans past live regions when reclaiming space from `APU_BASE`, reuses existing regions for non-owned mappings only when the full requested span is covered, and always registers owned `ApuAlloc` entries distinctly (`ApuFree` also prefers matching owned entries). `ApuAlloc` frees its `VirtualAlloc` block on APU range exhaustion. - **Checked arithmetic & null-pointer validation:** - Uses checked alignment/reserve arithmetic in `alloc_aligned`. - Rejects null `va` in `ApuMapVirtualAddress` with `E_INVALIDARG`. - Uses `Instant::checked_sub` in `Channel::pop` instead of panicking `now - FRAME * 8`. - **Transactional `AcpHalAllocateShapeContexts`:** - Validates all 5 `counts[i].checked_mul(CONTEXT_BYTES)` products upfront instead of `saturating_mul`. - Rolls back partial `ApuAlloc` allocations on failure without leaking or disturbing previously live SHAPE contexts, publishes output pointers under `SHAPE.lock()`, and frees replaced prior allocations outside the lock. - **`XApuConnect` OOM & partial-resource cleanup:** - Propagates `E_OUTOFMEMORY` instead of silently returning `S_OK` with `output.data == NULL` when `alloc_aligned`, `CreateEventW`, or `channels.try_reserve_exact` fails, releasing any already-acquired buffer/event and clearing `output` and `*handle`. ## Verification - `cargo test -p xcl --lib hle::acphal -- --test-threads=1`: **6 passed; 0 failed** - Added `apu_address_prevents_overflow_overlap_and_rejects_null_va` (verifies `u32::MAX` rejection, non-overlapping wraparound past live regions, owned vs unowned separation, and null `va` rejection). - Added `shape_contexts_overflow_and_partial_failure_roll_back` (verifies `counts[i] * CONTEXT_BYTES` overflow, real `ApuAlloc` rollback on partial failure, preservation of live allocations on failed replacement, and cleanup on successful replacement). - Added `xapu_connect_returns_oom_and_cleans_up_on_failure` (verifies `E_OUTOFMEMORY`, output clearing, and `VirtualQuery` `MEM_FREE` release of the data buffer when event creation fails).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:09:20 +00:00
github manually merged commit 7ac08284fc into gh-pr/90/base 2026-11-26 04:09:23 +00:00
Author
Owner

@⁠Lum7nt on GitHub · 2026-11-23 05:56 UTC · original

Tested this branch locally on Windows — all 6 tests in hle::acphal pass cleanly.

Key improvements verified:

  1. Range allocation safety: Checked arithmetic for 32-bit APU ranges and APU_LIMIT prevention ensures memory mappings do not wrap or overlap live regions, preventing corrupted ApuMapApuAddress reverse lookups.
  2. Transactional SHAPE contexts: Verified that failure during partial allocation cleanly rolls back newly acquired blocks without leaking, while preserving prior active contexts on failure.
  3. Proper OOM cleanup: Verified XApuConnect propagates E_OUTOFMEMORY and releases allocated buffers when event creation or channel reservation fails.

Clean and well-tested hardening for the APU subsystem.

**@⁠Lum7nt** on GitHub · 2026-11-23 05:56 UTC · [original](https://github.com/xcl-dev/xcl/pull/90#issuecomment-7092863393) Tested this branch locally on Windows — all 6 tests in `hle::acphal` pass cleanly. Key improvements verified: 1. **Range allocation safety**: Checked arithmetic for 32-bit APU ranges and `APU_LIMIT` prevention ensures memory mappings do not wrap or overlap live regions, preventing corrupted `ApuMapApuAddress` reverse lookups. 2. **Transactional SHAPE contexts**: Verified that failure during partial allocation cleanly rolls back newly acquired blocks without leaking, while preserving prior active contexts on failure. 3. **Proper OOM cleanup**: Verified `XApuConnect` propagates `E_OUTOFMEMORY` and releases allocated buffers when event creation or channel reservation fails. Clean and well-tested hardening for the APU subsystem.
Author
Owner

@⁠ivall on GitHub · 2026-11-23 10:48 UTC · original

Merged. XDefiant sets up its APU heap / SHAPE contexts / XApuConnect through this at start and still runs. Tested on Windows 11 with an RX 9070 (RDNA4): cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; Unicorn Overlord and XDefiant run with this round merged. Thanks!

**@⁠ivall** on GitHub · 2026-11-23 10:48 UTC · [original](https://github.com/xcl-dev/xcl/pull/90#issuecomment-7098509770) Merged. XDefiant sets up its APU heap / SHAPE contexts / XApuConnect through this at start and still runs. Tested on Windows 11 with an RX 9070 (RDNA4): `cargo test --workspace` green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with `XCL_SPLIT_WAVE64=1`; Unicorn Overlord and XDefiant run with this round merged. Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.