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

Closed
nathanbzrr wants to merge 1 commit from upstream-pr/641 into main
nathanbzrr commented 2026-11-24 06:06:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nathanbzrr on 2026-11-24T06:06:03Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6575758574 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/641). Originally posted by [nathanbzrr](https://github.com/nathanbzrr) on 2026-11-24T06:06:03Z. --- ## 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.
LAKSHYA-cmd-tech commented 2026-11-24 06:54:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T06:18:59Z.

Review: APPROVED


Tested on native Windows 11 with Rust 1.98.1 (x86_64-pc-windows-msvc).

  • cargo test -p victory-gpu: all 45 unit tests passed cleanly (0 failed, 1 ignored benchmark).
  • partial_residency_reads_only_mapped_regions_and_detects_new_mips and partially_resident_mip_tail_uploads_to_host_texture both passed.
  • cargo xtask policy: 151 files ok.
  • Verified Residency detection, committed interval merging, gap zeroing, and partial mip-tail uploading for streamed unwatched textures.
<!-- greyfinch-sync:Marvisak/VIctory:review:6034583797 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/641#pullrequestreview-6034583797). Originally posted by [LAKSHYA-cmd-tech](https://github.com/LAKSHYA-cmd-tech) on 2026-11-24T06:18:59Z. **Review:** APPROVED --- Tested on native Windows 11 with Rust 1.98.1 (x86_64-pc-windows-msvc). - cargo test -p victory-gpu: all 45 unit tests passed cleanly (0 failed, 1 ignored benchmark). - partial_residency_reads_only_mapped_regions_and_detects_new_mips and partially_resident_mip_tail_uploads_to_host_texture both passed. - cargo xtask policy: 151 files ok. - Verified Residency detection, committed interval merging, gap zeroing, and partial mip-tail uploading for streamed unwatched textures.
DeerSilver commented 2026-11-24 11:04:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DeerSilver on 2026-11-24T10:31:08Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7128887486 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/641#issuecomment-7128887486). Originally posted by [DeerSilver](https://github.com/DeerSilver) on 2026-11-24T10:31:08Z. --- 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).
DenRakEiw commented 2026-11-24 14:04:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T13:52:47Z.

Review: APPROVED


Tested merged cleanly onto main (ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell) and WARP (VICTORY_D3D12_WARP=1):

  • cargo test -p victory-gpu (RTX 5090): 52 passed, 0 failed, 1 ignored
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu (WARP): 52 passed, 0 failed, 1 ignored
  • cargo clippy -p victory-gpu --all-targets: 0 warnings
  • cargo xtask policy: ok

Clean merge, compiles and passes all GPU tests on both Blackwell hardware and WARP with zero warnings. Approving.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6040423283 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/641#pullrequestreview-6040423283). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T13:52:47Z. **Review:** APPROVED --- Tested merged cleanly onto `main` (`ffe310e`) on Windows 11 x64 (Rust 1.98.1 MSVC) with **NVIDIA GeForce RTX 5090** (Blackwell) and **WARP** (`VICTORY_D3D12_WARP=1`): - `cargo test -p victory-gpu` (RTX 5090): **52 passed, 0 failed, 1 ignored** - `VICTORY_D3D12_WARP=1 cargo test -p victory-gpu` (WARP): **52 passed, 0 failed, 1 ignored** - `cargo clippy -p victory-gpu --all-targets`: **0 warnings** - `cargo xtask policy`: ok Clean merge, compiles and passes all GPU tests on both Blackwell hardware and WARP with zero warnings. Approving. AI-assisted (Gemini 4 Neon).
nathanbzrr commented 2026-11-24 23:05:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nathanbzrr on 2026-11-24T22:08:16Z.


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).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7145851820 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/641#issuecomment-7145851820). Originally posted by [nathanbzrr](https://github.com/nathanbzrr) on 2026-11-24T22:08:16Z. --- 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`).
upstream-sync changed title from [PR #641] GPU: upload partially-resident streamed textures to [PR #641] GPU: harden untracked texture refresh against TOCTOU and zero-size layouts 2026-11-25 01:04:35 +00:00
upstream-sync closed this pull request 2026-11-25 01:04:35 +00:00
Marvisak commented 2026-11-25 01:07:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:26:45Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148517489 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/641#issuecomment-7148517489). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:26:45Z. --- 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.
nathanbzrr commented 2026-11-25 01:08:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nathanbzrr on 2026-11-25T00:49:07Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148909743 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/641#issuecomment-7148909743). Originally posted by [nathanbzrr](https://github.com/nathanbzrr) on 2026-11-25T00:49:07Z. --- 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

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
Marvisak/VIctory!641
No description provided.