Skip to content

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

Merged
fxceless merged 1 commit into
fxceless:mainfrom
CwWwWwW:fix/alias-source-held
Nov 26, 2026
Merged

fxceless merged 1 commit into
fxceless:mainfrom
CwWwWwW:fix/alias-source-held

Conversation

@CwWwWwW

@CwWwWwW CwWwWwW commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

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.
@fxceless
fxceless merged commit a463dcb into fxceless:main Nov 26, 2026
KomanderDG88 added a commit to KomanderDG88/VIniVIdiVIci that referenced this pull request Nov 26, 2026
…mended, heap identity, and the switches brought from other projects

Forty-one commits, compiled and tested as 9ef09c2 (victory-shader 267,
victory-gpu 325, victory-loader 51, victory-hle 128, no warnings; build
9ef09c2-diagnostics).

On by default:
- the original's two commits up to 58de9bf: fxceless#927
  (half-precision values through 16-bit buffer views keep their bits) and
  the sign extension of the original project's #3124; the translator
  changes, so shaders are translated anew once;
- the leak of placed-view shapes mended (TaynanGT/Vice-Tay PR fxceless#27);
- fxceless#1324 (VICTORY_HEAP_IDENTITY, =0 goes back),
  fxceless#1333 (VICTORY_REMAP_PER_HEAP, =0 goes back) and fxceless#1336.

Off by default: VICTORY_NAN_SCRUB, VICTORY_SHADER_LOOP_LIMIT_CS,
VICTORY_SHADER_EARLY_DEPTH, VICTORY_UNMAKE_QUIET, VICTORY_PATCH_WAITS,
VICTORY_PATCH_CLZERO, VICTORY_PATCH_CLWB, VICTORY_BVH_NAMED_WRITES,
VICTORY_BVH_BOUNDS, VICTORY_BVH_FENCES, VICTORY_PIPELINE_COMPUTE_WAIT,
VICTORY_CARD_AWAKE, VICTORY_GUARD_OUTSIDE, VICTORY_HEAP_RESCAN_SPREAD,
VICTORY_LAND_ASIDE, VICTORY_GUARD_RUNS, VICTORY_RT_SKIN_SKIP,
VICTORY_WINDOW_SKIP_SAME, VICTORY_MERGE_SUBMISSIONS, VICTORY_MESH_LAUNCH
and the capture switches; =alt for four of them.

Measured by Codex on build 9ef09c2, the ten switches of Status, the
scripted manoeuvre, a quiet machine, one session: 1,819 frames in 274.0 s,
6.64 fps, median frame 144 ms (95th percentile 197), against 6.69 fps and
141 ms on the code of main before it (-0.7 %, one session each). Private
memory in the room 16.8 GiB against 16.1 to 16.7 in six sessions of main
that day; the layer's windows 3,252 MB against 2,844 to 3,336.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
KomanderDG88 added a commit to KomanderDG88/VIniVIdiVIci that referenced this pull request Nov 26, 2026
… part 2)

9613092 alone does not build (its second deep-skips form call lacks the
label 93db653 adds), so the fifth merge goes on to e4245f3, the commit
it was meant to bring; nothing after it (fxceless#1320, fxceless#1321, fxceless#1353, fxceless#1336,
fxceless#1324 of 13:05 are left for later).

- 93db653: build fix for 9613092 (VICTORY_WAVE_WHY form label).
- e4245f3: VICTORY_CARD_HASH_WIDE=alt (1 was already there; default
  still a thread a piece), VICTORY_GATHER_SEGMENTS and
  VICTORY_GATHER_DISPATCHES (off), VICTORY_DIRTY_4K (off),
  VICTORY_MERGE_RUNS_ALT (unset), VICTORY_HEAP_RECHECK_EVERY (0),
  VICTORY_WAVE_WIDE_ONLY (empty); VICTORY_PAGE_WRITES default now
  guards+slots+plain reads' textures (1|2|8; "guards" is the old one);
  host callbacks check due textures first; textures without card room
  asked again (upstream VIctory #3198); a released view heap forgets its
  title-memory run (fxceless#589); shader fixes: mesh corners masked 9 bits,
  two immediate samplers behind a uniform branch, sampleLevel log2 and
  _cl clamp, split wave64 mask moves (fxceless#1112, fxceless#702).

Conflicts, our structure kept: HostList hunks moved to host/host_list.rs;
image hunks to emit/image_buffer.rs; copy_stale takes both COLD_SKIP
marks and DIRTY_4K pieces; Journal keeps cold_skip::Written plus the 4K
map; translate_listed wraps translate_counted; MERGE_RUNS_ALT before our
card_steps split. Duplicates kept once (ours): fxceless#1324 (HEAP_IDENTITY),
fxceless#1002 (SHADER_CASCADE_EXACT), and fxceless#646: upstream's DispatchX thread
counts (USER_DISPATCH_THREADS, same code 0x106) dropped for our port of
fxceless#1292 (USER_THREAD_LIMIT, which also covers the paired 64-lane form).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
grosterr added a commit to grosterr/victory-fork that referenced this pull request Nov 26, 2026
…xp/rtx3060-m

Co-Authored-By: Antigravity <noreply@anthropic.com>
pr0vId3nce added a commit to pr0vId3nce/VIctory-NVIDIA that referenced this pull request Nov 27, 2026
…336)

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.

Original author: cccw, fxceless/victory-fork#1336 (merged in victory-fork main as a463dcb18). Here: object::add_ref_alive added (this tree lacked it), and alias_of keeps its warning for unlike pairs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

(cherry picked from commit a463dcb18c486322bf05812f519c5015cfd871ed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants