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

Merged
CwWwWwW merged 1 commit from upstream-pr/1336 into main 2026-11-26 18:05:10 +00:00
CwWwWwW commented 2026-11-26 16:14:43 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by CwWwWwW on 2026-11-26T16:14:43Z.


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.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6630723390 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1336). Originally posted by [CwWwWwW](https://github.com/CwWwWwW) on 2026-11-26T16:14:43Z. --- ## 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.)
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
fxceless/victory-fork!1336
No description provided.