GPU: harden untracked texture refresh against TOCTOU and zero-size layouts #641
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!641
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/641/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@nathanbzrr on GitHub · 2026-11-24 06:06 UTC · original
nathanbzrr:gpu/partial-resident-texture-upload→main· +581 −22 · 1 files · 1 commits · closed without mergingSummary
Commit
c75fdbamovedCopyPageMappingsBatchXphysical-page views (MEM_MAPPED) and placeholders (MEM_RESERVE) onto thetrackedpath (watch::track(..., true)+host::copy_mapped), resolving the original streamed-texture motivation of this PR.This PR has been narrowed (rebased on
main4118f33, head2ef9a75) to address the remaining gaps in thetracked: Nonefallback path ofStaging::refresh_nowincrates/victory-gpu/src/texture.rs:size == 0: short-circuit zero-sized layouts inStaging::refresh_now()before locking or touchingself.memory.VirtualQuery: replacecommitted(self.memory, size)+unsafe { std::slice::from_raw_parts(self.memory, size) }withread_mapped(self.memory, &mut last.read):crate::host::read_into(ReadProcessMemory) in a single call with zeroVirtualQueryoverhead and no raw-pointer TOCTOU race against concurrent unmaps/decommits.[src, src + len)viaVirtualQuery(withchecked_addoverflow guard andProtect == 0 | PAGE_NOACCESS | PAGE_GUARDrejection), copies readable regions viaread_into(falling back to 4 KiB pages on mid-region concurrent unmap), and zeroes unreadable gaps so partially committed non-runtimeMEM_PRIVATEallocations still read safely without stale bytes.resident == 0, resetlast.hash = Noneso remapping identical bytes re-uploads cleanly.self.trackedpath andpub(crate) fn committed(...)unchanged.Verification
Verified on
2ef9a75(based onmain4118f33, Windows MSVC):cargo clippy --workspace --all-targets --locked -- -D warnings: cleancargo clippy --workspace --all-targets --all-features --locked -- -D warnings: cleancargo test --workspace --locked: 700 passed, 0 failed, 10 ignored (includes new teststexture::tests::read_mapped_handles_unmapped_partial_eviction_and_overflowandtexture::tests::untracked_refresh_now_handles_zero_size_partial_residency_and_remap)VICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics --locked -- --test-threads=1: 146 passed, 0 failed, 2 ignoredcargo xtask policy: 182 files okNo game files. AI-assisted; reviewed and verified with the checks above.
@DeerSilver on GitHub · 2026-11-24 10:31 UTC · original
Tested head
f887f6a3fmerged ontomainffe310e(clean merge) on NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1: pass. No game files: the test suite only.cargo test -p victory-gpu partial):partial_residency_reads_only_mapped_regions_and_detects_new_mipspartially_resident_mip_tail_uploads_to_host_texturepartially_resident_mip_tail_uploads_to_host_texturereturns early when there is no host GPU. Here it ran on the RTX 5060, which is DXGI adapter 0 on this laptop, so the mip-tail upload into a real D3D12 texture is checked on NVIDIA.cargo test --workspace --locked: 344 passed, 0 failed, 6 ignored, against 342 passed / 6 ignored onmainat the same commit. The difference is the two new tests.cargo clippy --workspace --all-targets --locked -- -D warnings: clean.I didn't run a negative control: the new tests call the new
read_resident/Residencycode, so they don't build againstmain'stexture.rs. I don't have the title, so I can't confirm that the black streamed textures in the world are fixed.AI-assisted (Claude Code).
@nathanbzrr on GitHub · 2026-11-24 22:08 UTC · original
Rebased onto
origin/main(70564ae, new headf96c56f):origin/main'strackedwrite-watch/mapped copy path (c75fdba) inStaging::refresh_now() -> (usize, bool)alongsidepub(crate) fn committed(...).Staging::refresh_now() -> (usize, bool)to handleResidency::Unmapped,Residency::Full, andResidency::Partial, hashing only resident intervals and zeroing evicted gaps before detiling.texture::tests::refresh_now_reports_resident_bytes_and_changed_flagcoveringUnmapped -> (0, false),Partial -> (resident_bytes, true/false), andFull -> (size, true/false)withtracked: None.Verified on
f96c56f(Windows MSVC):rustfmt --edition 2024 --check crates/victory-gpu/src/texture.rs: all rebased/new hunks clean (only pre-existingorigin/maindiffs inhost_written/alias_copy).cargo clippy --workspace --all-targets --locked -- -D warnings: clean.cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean.cargo test --workspace --locked:479 passed, 0 failed, 7 ignored(victory-gpu:81 passed, 0 failed, 1 ignored;xtask:4 passed).@Marvisak on GitHub · 2026-11-25 00:26 UTC · original
Automated triage for the maintainers. Closing because this change is already in
main, ormainhas since replaced the code it patches.If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.
@nathanbzrr on GitHub · 2026-11-25 00:49 UTC · original
Good catch by triage —
c75fdba(watch::track(..., true)+host::copy_mapped) already resolved the originalCopyPageMappingsBatchX/MEM_MAPPEDstreamed-texture case onmain.However, the
tracked: Nonefallback path inStaging::refresh_now(crates/victory-gpu/src/texture.rs) still had a few remaining gaps onmain:committed(self.memory, size)and then dereferencesunsafe { std::slice::from_raw_parts(self.memory, size) }, which can fault if another thread unmaps/decommits the private allocation betweenVirtualQueryandblake3::hash/detile(and incursVirtualQueryon every submission).size == 0guard:refresh_now()does not short-circuitsize == 0before callingcommitted()/from_raw_parts().MEM_PRIVATErange has a committed first page (tracked == None) and uncommitted/unreadable tail pages,committed()rejects the whole texture, and full unmaps do not resetlast.hash = None.I have narrowed the PR scope to only these remaining items, rebased cleanly onto
main(4118f33), updated the PR title/description, and pushed2ef9a75:tracked: Noneraw-slice path withread_mapped()(singlecrate::host::read_intofast path,VirtualQuery+ 4 KiB page fallback zeroing unreadable gaps,checked_addoverflow guard, andlast.hash = Nonewhenresident == 0).size == 0.read_mapped_handles_unmapped_partial_eviction_and_overflowanduntracked_refresh_now_handles_zero_size_partial_residency_and_remap.Verified on
2ef9a75(Windows MSVC):cargo clippy --workspace --all-targets --locked -- -D warnings: cleancargo clippy --workspace --all-targets --all-features --locked -- -D warnings: cleancargo test --workspace --locked: 700 passed, 0 failed, 10 ignoredVICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics --locked -- --test-threads=1: 146 passed, 0 failed, 2 ignoredcargo xtask policy: 182 files okIf you'd like these remaining hardening fixes in the
tracked: Nonefallback, please feel free to reopen and take another look.Pull request closed