D3D12-HLE-APU-HANDLE-LIFETIME-AND-ALIGNMENT: track live APU handles with Arc registry and unaligned guest I/O #2676

Merged
leonida/mandy
bobcatchris merged 1 commit from fix/d3d12-hle-apu-handle-lifetime into main 2026-11-30 16:21:48 +00:00

Pull request successfully merged and closed

bobcatchris commented 2026-11-30 01:44:36 +00:00

Claimed in #2570 as D3D12-HLE-APU-HANDLE-LIFETIME-AND-ALIGNMENT (#issuecomment-7260917882).

What and why

In d3d12/crates/leonida-hle/src/modules/apu.rs:

  1. Handle lifetime / use-after-free: Raw pointers to Connection were handed out as opaque APU handles without tracking live handles, allowing stale or invalid handles to be dereferenced after release. This change tracks live handles in a registry backed by Arc<Connection> with proper cleanup on close/drop, rejecting unknown or already-closed handles safely.
  2. Unaligned title pointer reads/writes: Title-supplied buffers and out-parameters are accessed via read_unaligned / write_unaligned instead of aligned dereferences, eliminating undefined behavior (0xc0000409 in debug builds) on unaligned guest pointers.

Verification (Red -> Green)

Hardware / OS: native Windows 11 Pro (10.0.26200), AMD Ryzen 7 7800X3D, Rust 1.98.

  • Red (before fix): Unaligned guest pointer test aborted with 0xc0000409 (misaligned pointer dereference) and stale-handle calls were unguarded.
  • Green (after fix, rebased onto main 40e7fe69d):
    • cargo test -p leonida-hle --lib apu — ALL PASS (and full leonida-hle --lib PASS).
    • python -I scripts/check_no_game_data.py d3d12/crates/leonida-hle/src/modules/apu.rs — PASS.

AI-assisted: Antigravity (Gemini 4 Neon)

Claimed in #2570 as `D3D12-HLE-APU-HANDLE-LIFETIME-AND-ALIGNMENT` (`#issuecomment-7260917882`). ### What and why In `d3d12/crates/leonida-hle/src/modules/apu.rs`: 1. **Handle lifetime / use-after-free:** Raw pointers to `Connection` were handed out as opaque APU handles without tracking live handles, allowing stale or invalid handles to be dereferenced after release. This change tracks live handles in a registry backed by `Arc<Connection>` with proper cleanup on close/drop, rejecting unknown or already-closed handles safely. 2. **Unaligned title pointer reads/writes:** Title-supplied buffers and out-parameters are accessed via `read_unaligned` / `write_unaligned` instead of aligned dereferences, eliminating undefined behavior (`0xc0000409` in debug builds) on unaligned guest pointers. ### Verification (Red -> Green) Hardware / OS: native Windows 11 Pro (`10.0.26200`), AMD Ryzen 7 7800X3D, Rust `1.98`. - **Red (before fix):** Unaligned guest pointer test aborted with `0xc0000409` (misaligned pointer dereference) and stale-handle calls were unguarded. - **Green (after fix, rebased onto `main` `40e7fe69d`):** - `cargo test -p leonida-hle --lib apu` — **ALL PASS** (and full `leonida-hle --lib` PASS). - `python -I scripts/check_no_game_data.py d3d12/crates/leonida-hle/src/modules/apu.rs` — **PASS**. AI-assisted: Antigravity (Gemini 4 Neon)
Kal0s-design commented 2026-11-30 11:25:23 +00:00

Independent Windows verification of 24b7bf5f9 on main d367675c7 (clean merge, 4ce4aa96f). Native Windows 11, MSVC x64, Rust 1.98.1, debug cargo test -p leonida-hle --lib. Each new test run alone, since an abort takes the whole binary down.

  • Red: main + only this PR's two test functions.
    • connect_accepts_unaligned_input_output_and_handle aborts with misaligned pointer dereference: address must be a multiple of 0x4 at apu.rs:155, exit 0xc0000409.
    • stale_and_foreign_handles_are_rejected_without_uaf dies with 0xc0000005 (access violation) once the freed handle is passed back in.
  • Green: main + this PR. Both pass; whole crate 220 passed, 2 ignored, 0 failed (main: 218 + these two).

Read the change: a call that holds a cloned Arc while another thread disconnects keeps the memory and event alive until it returns, and the ids never repeat (monotonic, never 0), so a stale handle can't land on a newer connection. The unsafe impl Send/Sync is only needed for the raw memory and event fields; opus-pure's decoder state holds no Rc/Cell, it's plain data behind the Mutex.

GPUs tested: n/a (CPU unit tests). AI-assisted: yes.

**Independent Windows verification** of `24b7bf5f9` on `main` `d367675c7` (clean merge, `4ce4aa96f`). Native Windows 11, MSVC x64, Rust 1.98.1, debug `cargo test -p leonida-hle --lib`. Each new test run alone, since an abort takes the whole binary down. - Red: `main` + only this PR's two test functions. - `connect_accepts_unaligned_input_output_and_handle` aborts with `misaligned pointer dereference: address must be a multiple of 0x4` at `apu.rs:155`, exit `0xc0000409`. - `stale_and_foreign_handles_are_rejected_without_uaf` dies with `0xc0000005` (access violation) once the freed handle is passed back in. - Green: `main` + this PR. Both pass; whole crate 220 passed, 2 ignored, 0 failed (`main`: 218 + these two). Read the change: a call that holds a cloned `Arc` while another thread disconnects keeps the memory and event alive until it returns, and the ids never repeat (monotonic, never 0), so a stale handle can't land on a newer connection. The `unsafe impl Send/Sync` is only needed for the raw `memory` and `event` fields; `opus-pure`'s decoder state holds no `Rc`/`Cell`, it's plain data behind the `Mutex`. GPUs tested: n/a (CPU unit tests). AI-assisted: yes.
KelboLive commented 2026-11-30 16:05:45 +00:00

Independent check on Windows (not the author), at 24b7bf5f9.

  • cargo test --locked -p leonida-hle --lib: 220 passed, 0 failed, 2 ignored in 0.73 s.
  • leonida-hle only; no game run, so this is compile + unit coverage of the handle tracking/unaligned access.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned.

AI-assisted: yes (opencode).

Independent check on Windows (not the author), at `24b7bf5f9`. - `cargo test --locked -p leonida-hle --lib`: **220 passed, 0 failed, 2 ignored** in 0.73 s. - `leonida-hle` only; no game run, so this is compile + unit coverage of the handle tracking/unaligned access. Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned. AI-assisted: yes (opencode).
MoonEyes commented 2026-11-30 16:22:04 +00:00

Merged on head 24b7bf5f9. @Kal0s-design ran the red/green on this head (Windows 11, Rust 1.98.1: both new tests abort on main and pass with this pull request); @KelboLive ran the crate green on this head (220 passed). CPU unit tests, no GPU. CI was green on this head and no test was run by the merger.

Merged by MoonEyes. AI-assisted: yes (Claude Code).

Merged on head `24b7bf5f9`. @Kal0s-design ran the red/green on this head (Windows 11, Rust 1.98.1: both new tests abort on `main` and pass with this pull request); @KelboLive ran the crate green on this head (220 passed). CPU unit tests, no GPU. CI was green on this head and no test was run by the merger. Merged by MoonEyes. AI-assisted: yes (Claude Code).
Sign in to join this conversation.
No description provided.