GPU: pin the texture a placed view aliases while the copy uses it #1336

Manually merged
github merged 1 commit from github-bot/gh-1336 into gh-pr/1336/base 2026-11-26 18:09:55 +00:00 AGit
Owner

@⁠CwWwWwW on GitHub · 2026-11-26 16:14 UTC · original
CwWwWwW:fix/alias-source-held → main · +63 −5 · 2 files · 1 commits · merged 2026-11-26 18:05 UTC

Problem

crate::resource::alias_of (called from the draw's placed-view preparation) takes TARGETS_AT's lock, looks up the title target texture that lies in the placed view's memory, validates it, and returns the bare pointer. The lock is dropped on return; the caller then passes the pointer to texture::alias, which dereferences it - host_resource(source) locks the resource's host.made, then the host copy is used in copies and barriers.

The only thing that made the lookup safe is Resource::drop removing the TARGETS_AT entry under the same lock before freeing. Once the lock is released that protection is gone: a title that releases the target texture between the lookup and the copy (recording threads release textures once their last use is recorded; the same texture can be recreated at the same address) makes the copy read a freed Box<Resource> - heap use-after-free, or a silent copy from the next texture allocated there.

The fork already has the exact idiom for title-object pointers crossing a thread: resource::hold returns a Held guard that pins the object with object::add_ref_alive and releases it on drop. alias_of was the one lookup that returned a naked pointer.

Change

  • alias_of now returns Option<Held>: under TARGETS_AT's lock, after the same checks, it pins the found texture with crate::object::add_ref_alive and returns the existing Held guard. If the texture's last reference is already being released, it returns None (safe: the draw falls back to the title-memory upload, which is what the alias copy replaces).
  • Held gains get() -> This.
  • draw.rs holds the guard across texture::alias(list, placed, source.get()); it drops right after the copy is recorded.
  • The existing alias_after_title_upload_copies_the_gpu_atlas test uses the held API.

Tests

  • New an_alias_source_is_held_after_the_title_releases_it: releases the title's reference to the alias source, then checks the source is still in the texture table and host_resource still returns its copy; dropping the held handle frees it. Without the pin the source is freed by the release, so the following copy would read freed memory.
  • cargo test -p victory-gpu --lib --locked: 217 passed, 0 failed, 6 ignored (default build; the new test is diagnostics-gated like the existing alias test).
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --lib --features diagnostics resource:: -- --test-threads=1: 11 passed, 0 failed (the alias tests run on the WARP device).
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings and --all-features: clean.
  • No game or GPU needed.

Notes

  • The race window is small; the test proves the pin (the source stays alive after the title lets go), not the race itself. A deterministic reproduction would need a test seam between the lookup and the copy; the failure mechanism is the raw pointer escaping the only lock that protects it.
  • Found by this fork's deep memory/lifetime audit; not a port of upstream or another fork.

(CI note: the check job on main is currently red on the asset-policy violation; #1322 fixes that, after which this can be rebased.)

**@⁠CwWwWwW** on GitHub · 2026-11-26 16:14 UTC · [original](https://github.com/fxceless/victory-fork/pull/1336) `CwWwWwW:fix/alias-source-held` → `main` · +63 −5 · 2 files · 1 commits · merged 2026-11-26 18:05 UTC ## Problem `crate::resource::alias_of` (called from the draw's placed-view preparation) takes `TARGETS_AT`'s lock, looks up the title target texture that lies in the placed view's memory, validates it, and returns the **bare pointer**. The lock is dropped on return; the caller then passes the pointer to `texture::alias`, which dereferences it - `host_resource(source)` locks the resource's `host.made`, then the host copy is used in copies and barriers. The only thing that made the lookup safe is `Resource::drop` removing the `TARGETS_AT` entry under the same lock before freeing. Once the lock is released that protection is gone: a title that releases the target texture between the lookup and the copy (recording threads release textures once their last use is recorded; the same texture can be recreated at the same address) makes the copy read a freed `Box<Resource>` - heap use-after-free, or a silent copy from the next texture allocated there. The fork already has the exact idiom for title-object pointers crossing a thread: `resource::hold` returns a `Held` guard that pins the object with `object::add_ref_alive` and releases it on drop. `alias_of` was the one lookup that returned a naked pointer. ## Change - `alias_of` now returns `Option<Held>`: under `TARGETS_AT`'s lock, after the same checks, it pins the found texture with `crate::object::add_ref_alive` and returns the existing `Held` guard. If the texture's last reference is already being released, it returns `None` (safe: the draw falls back to the title-memory upload, which is what the alias copy replaces). - `Held` gains `get() -> This`. - `draw.rs` holds the guard across `texture::alias(list, placed, source.get())`; it drops right after the copy is recorded. - The existing `alias_after_title_upload_copies_the_gpu_atlas` test uses the held API. ## Tests - New `an_alias_source_is_held_after_the_title_releases_it`: releases the title's reference to the alias source, then checks the source is still in the texture table and `host_resource` still returns its copy; dropping the held handle frees it. Without the pin the source is freed by the release, so the following copy would read freed memory. - `cargo test -p victory-gpu --lib --locked`: **217 passed, 0 failed, 6 ignored** (default build; the new test is diagnostics-gated like the existing alias test). - `VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --lib --features diagnostics resource:: -- --test-threads=1`: **11 passed, 0 failed** (the alias tests run on the WARP device). - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` and `--all-features`: clean. - No game or GPU needed. ## Notes - The race window is small; the test proves the pin (the source stays alive after the title lets go), not the race itself. A deterministic reproduction would need a test seam between the lookup and the copy; the failure mechanism is the raw pointer escaping the only lock that protects it. - Found by this fork's deep memory/lifetime audit; not a port of upstream or another fork. (CI note: the `check` job on main is currently red on the asset-policy violation; #1322 fixes that, after which this can be rebased.)
alias_of looked a title target texture up under TARGETS_AT's lock, checked
the format and description, and returned the bare pointer. draw then passed
it to texture::alias, which dereferences it (host copy, barriers) after the
lock is gone. The title may drop its last reference in between - its
recording threads release textures once their last use is recorded - and
the lookup's only protection (Resource::drop removes the table entry before
freeing) ends with the lock: a heap use-after-free, or a copy from the next
texture allocated at the same address.

alias_of now pins the texture with add_ref_alive under the lock and returns
the crate's existing Held guard; the draw holds it until the alias copy is
recorded. A texture already being released is not returned (the draw falls
back to the title-memory upload).

Test: an_alias_source_is_held_after_the_title_releases_it (releases the
title's reference, checks the source is still in the texture table and
copyable, and freed when the held handle drops). The existing
alias_after_title_upload_copies_the_gpu_atlas test uses the held API.
cargo test -p victory-gpu --lib: 217 passed, 0 failed, 6 ignored; the
resource tests with diagnostics and WARP: 11 passed; clippy default and
--all-features clean.

Found by the deep GPU memory/lifetime audit of this fork (task-7); no
upstream/other-fork port involved.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:18:24 +00:00
github-bot changed target branch from main to gh-pr/1336/base 2026-11-26 18:09:54 +00:00
github manually merged commit f8fab6fb8f into gh-pr/1336/base 2026-11-26 18:09:55 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.