GPU: harden untracked texture refresh against TOCTOU and zero-size layouts #641

Closed
github-bot wants to merge 1 commit from gh-pr/641/head into gh-pr/641/base
Owner

@⁠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 merging

Summary

Commit c75fdba moved CopyPageMappingsBatchX physical-page views (MEM_MAPPED) and placeholders (MEM_RESERVE) onto the tracked path (watch::track(..., true) + host::copy_mapped), resolving the original streamed-texture motivation of this PR.

This PR has been narrowed (rebased on main 4118f33, head 2ef9a75) to address the remaining gaps in the tracked: None fallback path of Staging::refresh_now in crates/victory-gpu/src/texture.rs:

  • Early return on size == 0: short-circuit zero-sized layouts in Staging::refresh_now() before locking or touching self.memory.
  • Eliminate TOCTOU raw-slice read and fast-path VirtualQuery: replace committed(self.memory, size) + unsafe { std::slice::from_raw_parts(self.memory, size) } with read_mapped(self.memory, &mut last.read):
    • Fast path uses crate::host::read_into (ReadProcessMemory) in a single call with zero VirtualQuery overhead and no raw-pointer TOCTOU race against concurrent unmaps/decommits.
    • Fallback path (only when the single read fails) walks [src, src + len) via VirtualQuery (with checked_add overflow guard and Protect == 0 | PAGE_NOACCESS | PAGE_GUARD rejection), copies readable regions via read_into (falling back to 4 KiB pages on mid-region concurrent unmap), and zeroes unreadable gaps so partially committed non-runtime MEM_PRIVATE allocations still read safely without stale bytes.
  • Clear cached hash on full unmap: when resident == 0, reset last.hash = None so remapping identical bytes re-uploads cleanly.
  • Keeps the self.tracked path and pub(crate) fn committed(...) unchanged.

Verification

Verified on 2ef9a75 (based on main 4118f33, Windows MSVC):

  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test --workspace --locked: 700 passed, 0 failed, 10 ignored (includes new tests texture::tests::read_mapped_handles_unmapped_partial_eviction_and_overflow and texture::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 ignored
  • cargo xtask policy: 182 files ok

No game files. AI-assisted; reviewed and verified with the checks above.

**@⁠nathanbzrr** on GitHub · 2026-11-24 06:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/641) `nathanbzrr:gpu/partial-resident-texture-upload` → `main` · +581 −22 · 1 files · 1 commits · closed without merging ## Summary Commit `c75fdba` moved `CopyPageMappingsBatchX` physical-page views (`MEM_MAPPED`) and placeholders (`MEM_RESERVE`) onto the `tracked` path (`watch::track(..., true)` + `host::copy_mapped`), resolving the original streamed-texture motivation of this PR. This PR has been narrowed (rebased on `main` `4118f33`, head `2ef9a75`) to address the remaining gaps in the `tracked: None` fallback path of `Staging::refresh_now` in `crates/victory-gpu/src/texture.rs`: - **Early return on `size == 0`**: short-circuit zero-sized layouts in `Staging::refresh_now()` before locking or touching `self.memory`. - **Eliminate TOCTOU raw-slice read and fast-path `VirtualQuery`**: replace `committed(self.memory, size)` + `unsafe { std::slice::from_raw_parts(self.memory, size) }` with `read_mapped(self.memory, &mut last.read)`: - Fast path uses `crate::host::read_into` (`ReadProcessMemory`) in a single call with zero `VirtualQuery` overhead and no raw-pointer TOCTOU race against concurrent unmaps/decommits. - Fallback path (only when the single read fails) walks `[src, src + len)` via `VirtualQuery` (with `checked_add` overflow guard and `Protect == 0 | PAGE_NOACCESS | PAGE_GUARD` rejection), copies readable regions via `read_into` (falling back to 4 KiB pages on mid-region concurrent unmap), and zeroes unreadable gaps so partially committed non-runtime `MEM_PRIVATE` allocations still read safely without stale bytes. - **Clear cached hash on full unmap**: when `resident == 0`, reset `last.hash = None` so remapping identical bytes re-uploads cleanly. - Keeps the `self.tracked` path and `pub(crate) fn committed(...)` unchanged. ## Verification Verified on `2ef9a75` (based on `main` `4118f33`, Windows MSVC): - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo test --workspace --locked`: 700 passed, 0 failed, 10 ignored (includes new tests `texture::tests::read_mapped_handles_unmapped_partial_eviction_and_overflow` and `texture::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 ignored - `cargo xtask policy`: 182 files ok No game files. AI-assisted; reviewed and verified with the checks above.
# Conflicts:
#	crates/victory-gpu/src/texture.rs
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:11:10 +00:00
github-bot closed this pull request 2026-11-26 04:11:10 +00:00
Author
Owner

@⁠DeerSilver on GitHub · 2026-11-24 10:31 UTC · original

Tested head f887f6a3f merged onto main ffe310e (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.

  • The PR's two tests pass on their own (cargo test -p victory-gpu partial):
    • partial_residency_reads_only_mapped_regions_and_detects_new_mips
    • partially_resident_mip_tail_uploads_to_host_texture
  • partially_resident_mip_tail_uploads_to_host_texture returns 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 on main at 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 / Residency code, so they don't build against main's texture.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).

**@⁠DeerSilver** on GitHub · 2026-11-24 10:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/641#issuecomment-7128887486) Tested head `f887f6a3f` merged onto `main` `ffe310e` (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. - The PR's two tests pass on their own (`cargo test -p victory-gpu partial`): - `partial_residency_reads_only_mapped_regions_and_detects_new_mips` - `partially_resident_mip_tail_uploads_to_host_texture` - `partially_resident_mip_tail_uploads_to_host_texture` returns 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 on `main` at 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` / `Residency` code, so they don't build against `main`'s `texture.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).
Author
Owner

@⁠nathanbzrr on GitHub · 2026-11-24 22:08 UTC · original

Rebased onto origin/main (70564ae, new head f96c56f):

  • Preserved origin/main's tracked write-watch/mapped copy path (c75fdba) in Staging::refresh_now() -> (usize, bool) alongside pub(crate) fn committed(...).
  • Adapted the fallback unwatched/non-runtime-memory path in Staging::refresh_now() -> (usize, bool) to handle Residency::Unmapped, Residency::Full, and Residency::Partial, hashing only resident intervals and zeroing evicted gaps before detiling.
  • Added texture::tests::refresh_now_reports_resident_bytes_and_changed_flag covering Unmapped -> (0, false), Partial -> (resident_bytes, true/false), and Full -> (size, true/false) with tracked: None.

Verified on f96c56f (Windows MSVC):

  • rustfmt --edition 2024 --check crates/victory-gpu/src/texture.rs: all rebased/new hunks clean (only pre-existing origin/main diffs in host_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).
**@⁠nathanbzrr** on GitHub · 2026-11-24 22:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/641#issuecomment-7145851820) Rebased onto `origin/main` (`70564ae`, new head `f96c56f`): - Preserved `origin/main`'s `tracked` write-watch/mapped copy path (`c75fdba`) in `Staging::refresh_now() -> (usize, bool)` alongside `pub(crate) fn committed(...)`. - Adapted the fallback unwatched/non-runtime-memory path in `Staging::refresh_now() -> (usize, bool)` to handle `Residency::Unmapped`, `Residency::Full`, and `Residency::Partial`, hashing only resident intervals and zeroing evicted gaps before detiling. - Added `texture::tests::refresh_now_reports_resident_bytes_and_changed_flag` covering `Unmapped -> (0, false)`, `Partial -> (resident_bytes, true/false)`, and `Full -> (size, true/false)` with `tracked: None`. Verified on `f96c56f` (Windows MSVC): - `rustfmt --edition 2024 --check crates/victory-gpu/src/texture.rs`: all rebased/new hunks clean (only pre-existing `origin/main` diffs in `host_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`).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:26 UTC · original

Automated triage for the maintainers. Closing because this change is already in main, or main has 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.

**@⁠Marvisak** on GitHub · 2026-11-25 00:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/641#issuecomment-7148517489) Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has 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.
Author
Owner

@⁠nathanbzrr on GitHub · 2026-11-25 00:49 UTC · original

Good catch by triage — c75fdba (watch::track(..., true) + host::copy_mapped) already resolved the original CopyPageMappingsBatchX / MEM_MAPPED streamed-texture case on main.

However, the tracked: None fallback path in Staging::refresh_now (crates/victory-gpu/src/texture.rs) still had a few remaining gaps on main:

  1. TOCTOU raw-slice read: it still checks committed(self.memory, size) and then dereferences unsafe { std::slice::from_raw_parts(self.memory, size) }, which can fault if another thread unmaps/decommits the private allocation between VirtualQuery and blake3::hash/detile (and incurs VirtualQuery on every submission).
  2. size == 0 guard: refresh_now() does not short-circuit size == 0 before calling committed() / from_raw_parts().
  3. Untracked partial residency & remap hash reset: if a non-runtime MEM_PRIVATE range has a committed first page (tracked == None) and uncommitted/unreadable tail pages, committed() rejects the whole texture, and full unmaps do not reset last.hash = None.

I have narrowed the PR scope to only these remaining items, rebased cleanly onto main (4118f33), updated the PR title/description, and pushed 2ef9a75:

  • Replaces the tracked: None raw-slice path with read_mapped() (single crate::host::read_into fast path, VirtualQuery + 4 KiB page fallback zeroing unreadable gaps, checked_add overflow guard, and last.hash = None when resident == 0).
  • Adds early return for size == 0.
  • Adds focused unit tests: read_mapped_handles_unmapped_partial_eviction_and_overflow and untracked_refresh_now_handles_zero_size_partial_residency_and_remap.

Verified on 2ef9a75 (Windows MSVC):

  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test --workspace --locked: 700 passed, 0 failed, 10 ignored
  • VICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics --locked -- --test-threads=1: 146 passed, 0 failed, 2 ignored
  • cargo xtask policy: 182 files ok

If you'd like these remaining hardening fixes in the tracked: None fallback, please feel free to reopen and take another look.

**@⁠nathanbzrr** on GitHub · 2026-11-25 00:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/641#issuecomment-7148909743) Good catch by triage — `c75fdba` (`watch::track(..., true)` + `host::copy_mapped`) already resolved the original `CopyPageMappingsBatchX` / `MEM_MAPPED` streamed-texture case on `main`. However, the `tracked: None` fallback path in `Staging::refresh_now` (`crates/victory-gpu/src/texture.rs`) still had a few remaining gaps on `main`: 1. **TOCTOU raw-slice read**: it still checks `committed(self.memory, size)` and then dereferences `unsafe { std::slice::from_raw_parts(self.memory, size) }`, which can fault if another thread unmaps/decommits the private allocation between `VirtualQuery` and `blake3::hash`/`detile` (and incurs `VirtualQuery` on every submission). 2. **`size == 0` guard**: `refresh_now()` does not short-circuit `size == 0` before calling `committed()` / `from_raw_parts()`. 3. **Untracked partial residency & remap hash reset**: if a non-runtime `MEM_PRIVATE` range has a committed first page (`tracked == None`) and uncommitted/unreadable tail pages, `committed()` rejects the whole texture, and full unmaps do not reset `last.hash = None`. I have narrowed the PR scope to only these remaining items, rebased cleanly onto `main` (`4118f33`), updated the PR title/description, and pushed `2ef9a75`: - Replaces the `tracked: None` raw-slice path with `read_mapped()` (single `crate::host::read_into` fast path, `VirtualQuery` + 4 KiB page fallback zeroing unreadable gaps, `checked_add` overflow guard, and `last.hash = None` when `resident == 0`). - Adds early return for `size == 0`. - Adds focused unit tests: `read_mapped_handles_unmapped_partial_eviction_and_overflow` and `untracked_refresh_now_handles_zero_size_partial_residency_and_remap`. Verified on `2ef9a75` (Windows MSVC): - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo test --workspace --locked`: 700 passed, 0 failed, 10 ignored - `VICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics --locked -- --test-threads=1`: 146 passed, 0 failed, 2 ignored - `cargo xtask policy`: 182 files ok If you'd like these remaining hardening fixes in the `tracked: None` fallback, please feel free to reopen and take another look.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!641
No description provided.