fix(victory): refcount host_alias views across section close and drop sections outside PHYSICAL lock #432

Closed
github-bot wants to merge 1 commit from github-bot/gh-432 into main AGit
Owner

@⁠Lewandox on GitHub · 2026-11-26 18:40 UTC · original
Lewandox:fix/victory-section-closed-alias-race → main · +128 −48 · 3 files · 1 commits · closed without merging

Summary

Fixes the section-close / host_alias race conditions introduced in #352 (ee8b92e1) that contribute to #428 (first-world-frame GPU reset / TDR / MMU fault even with VICTORY_SECTION_MB unset).

Root Causes Fixed

  1. Use-after-unmap & VA-reuse corruption in inplace::title_memory_section_closed + host_alias:
    • Previously host_alias returned a raw *mut u8 after dropping ALIASES, while title_memory_section_closed immediately called UnmapViewOfFile on the same view. An in-flight shadow.rs::write_alias could either fault mid-copy or silently write into another mapping/buffer allocated at the recycled virtual address.
    • Wrapped each alias mapping in Arc<AliasView> (whose Drop calls UnmapViewOfFile) and returned a scoped HostAlias guard from host_alias, guaranteeing the view remains mapped until every in-flight writer finishes.
  2. Handle-reuse TOCTOU between VIEWS and ALIASES in host_alias:
    • Previously host_alias dropped VIEWS before locking ALIASES and calling MapViewOfFile(section, ...). If Section::drop ran title_memory_section_closed + CloseHandle(section) in that gap, host_alias could call MapViewOfFile on a closed or recycled Windows kernel handle and cache the stale/wrong view in ALIASES.
    • Enforced a single consistent lock order (VIEWS -> ALIASES) across title_memory_section, title_memory_section_closed, and host_alias, keeping VIEWS held across MapViewOfFile so CloseHandle cannot run concurrently, while dropping replaced Arc<AliasView> values outside both locks.
  3. Dropping Section outside PHYSICAL lock in xmem::XMemFreePhysicalPages:
    • Deferred Slice/Section drops until after PHYSICAL mutex is released so title_memory_section_closed and CloseHandle never run while holding the physical page-table lock.

Tests

  • Added inplace::tests::host_alias_keeps_view_mapped_across_section_close_and_blocks_post_close_mapping verifying that an active HostAlias stays valid and writable across title_memory_section_closed + CloseHandle, while subsequent host_alias lookups return None.

Verification

  • cargo test -p victory-gpu --lib inplace::tests::host_alias_keeps_view_mapped_across_section_close_and_blocks_post_close_mapping (default and --features diagnostics)
  • cargo test -p victory-hle --lib modules::xmem::tests
  • cargo xtask policy

Addresses #428.

**@⁠Lewandox** on GitHub · 2026-11-26 18:40 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/432) `Lewandox:fix/victory-section-closed-alias-race` → `main` · +128 −48 · 3 files · 1 commits · closed without merging ## Summary Fixes the section-close / `host_alias` race conditions introduced in #352 (`ee8b92e1`) that contribute to #428 (first-world-frame GPU reset / TDR / MMU fault even with `VICTORY_SECTION_MB` unset). ### Root Causes Fixed 1. **Use-after-unmap & VA-reuse corruption in `inplace::title_memory_section_closed` + `host_alias`**: - Previously `host_alias` returned a raw `*mut u8` after dropping `ALIASES`, while `title_memory_section_closed` immediately called `UnmapViewOfFile` on the same view. An in-flight `shadow.rs::write_alias` could either fault mid-copy or silently write into another mapping/buffer allocated at the recycled virtual address. - Wrapped each alias mapping in `Arc<AliasView>` (whose `Drop` calls `UnmapViewOfFile`) and returned a scoped `HostAlias` guard from `host_alias`, guaranteeing the view remains mapped until every in-flight writer finishes. 2. **Handle-reuse TOCTOU between `VIEWS` and `ALIASES` in `host_alias`**: - Previously `host_alias` dropped `VIEWS` before locking `ALIASES` and calling `MapViewOfFile(section, ...)`. If `Section::drop` ran `title_memory_section_closed` + `CloseHandle(section)` in that gap, `host_alias` could call `MapViewOfFile` on a closed or recycled Windows kernel handle and cache the stale/wrong view in `ALIASES`. - Enforced a single consistent lock order (`VIEWS` -> `ALIASES`) across `title_memory_section`, `title_memory_section_closed`, and `host_alias`, keeping `VIEWS` held across `MapViewOfFile` so `CloseHandle` cannot run concurrently, while dropping replaced `Arc<AliasView>` values outside both locks. 3. **Dropping `Section` outside `PHYSICAL` lock in `xmem::XMemFreePhysicalPages`**: - Deferred `Slice`/`Section` drops until after `PHYSICAL` mutex is released so `title_memory_section_closed` and `CloseHandle` never run while holding the physical page-table lock. ### Tests - Added `inplace::tests::host_alias_keeps_view_mapped_across_section_close_and_blocks_post_close_mapping` verifying that an active `HostAlias` stays valid and writable across `title_memory_section_closed` + `CloseHandle`, while subsequent `host_alias` lookups return `None`. ## Verification - `cargo test -p victory-gpu --lib inplace::tests::host_alias_keeps_view_mapped_across_section_close_and_blocks_post_close_mapping` (default and `--features diagnostics`) - `cargo test -p victory-hle --lib modules::xmem::tests` - `cargo xtask policy` Addresses #428.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 18:40:38 +00:00
github-bot closed this pull request 2026-11-26 19:34:47 +00:00
Author
Owner

@⁠Lewandox on GitHub · 2026-11-26 19:16 UTC · original

Closing as superseded after #409 (\inplace.rs\ in the combined base no longer uses the pre-#409 \host_alias\ map). Keeping the PR queue clean.

**@⁠Lewandox** on GitHub · 2026-11-26 19:16 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/432#issuecomment-7201388627) Closing as superseded after #409 (\inplace.rs\ in the combined base no longer uses the pre-#409 \host_alias\ map). Keeping the PR queue clean.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.