GPU: clamp Staging::detile slices and harden page readability checks #165

Open
github-bot wants to merge 2 commits from github-bot/gh-165 into main AGit
Owner

@⁠HailLucifer666 on GitHub · 2026-11-24 22:06 UTC · original
HailLucifer666:fix/texture-staging-bounds-and-page-guards → main · +140 −10 · 1 files · 2 commits · open

Problem

  • Staging::detile() built a mutable slice of pitch * rows * depth even when GetCopyableFootprints omits trailing row-pitch padding on the final row, exceeding self.bytes (UB).
  • readable() used at.saturating_add(len - 1), which saturates to usize::MAX on address overflow and loops across the 64-bit address space instead of rejecting the range.
  • committed_by_query() used unchecked p + len, and both VirtualQuery paths (committed_by_query and mapped_bytes) counted PAGE_NOACCESS and PAGE_GUARD pages as readable committed memory.

Fix

  • Clamp Staging::detile() slices via subresource_len(..., self.bytes).
  • Use checked_add in readable(), committed_by_query(), and mapped_bytes().
  • Share is_readable_commit() across both VirtualQuery paths to reject PAGE_NOACCESS and PAGE_GUARD.

Verification

  • Added unit tests for subresource_len and real VirtualAlloc PAGE_NOACCESS / PAGE_GUARD / PAGE_READWRITE + usize::MAX overflow cases.
  • cargo clippy --workspace --all-targets --locked -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
  • cargo test -p victory-gpu texture:: --locked (4/4 passed)
  • cargo test -p xtask --locked (4/4 passed)
**@⁠HailLucifer666** on GitHub · 2026-11-24 22:06 UTC · [original](https://github.com/fxceless/victory-fork/pull/165) `HailLucifer666:fix/texture-staging-bounds-and-page-guards` → `main` · +140 −10 · 1 files · 2 commits · open ### Problem - `Staging::detile()` built a mutable slice of `pitch * rows * depth` even when `GetCopyableFootprints` omits trailing row-pitch padding on the final row, exceeding `self.bytes` (UB). - `readable()` used `at.saturating_add(len - 1)`, which saturates to `usize::MAX` on address overflow and loops across the 64-bit address space instead of rejecting the range. - `committed_by_query()` used unchecked `p + len`, and both `VirtualQuery` paths (`committed_by_query` and `mapped_bytes`) counted `PAGE_NOACCESS` and `PAGE_GUARD` pages as readable committed memory. ### Fix - Clamp `Staging::detile()` slices via `subresource_len(..., self.bytes)`. - Use `checked_add` in `readable()`, `committed_by_query()`, and `mapped_bytes()`. - Share `is_readable_commit()` across both `VirtualQuery` paths to reject `PAGE_NOACCESS` and `PAGE_GUARD`. ### Verification - Added unit tests for `subresource_len` and real `VirtualAlloc` `PAGE_NOACCESS` / `PAGE_GUARD` / `PAGE_READWRITE` + `usize::MAX` overflow cases. - `cargo clippy --workspace --all-targets --locked -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` - `cargo test -p victory-gpu texture:: --locked` (4/4 passed) - `cargo test -p xtask --locked` (4/4 passed)
THE BUG.
Staging::detile built a mutable slice of pitch * rows * depth even when GetCopyableFootprints omitted trailing row padding on the final row, exceeding self.bytes. In addition, readable() saturated address overflow to usize::MAX instead of rejecting it, and committed_by_query() / mapped_bytes() counted PAGE_NOACCESS and PAGE_GUARD pages as readable committed memory (with unchecked p + len in committed_by_query).

WHY NOT HYPOTHETICAL.
texture.rs already tests a 4-row texture whose staging buffer is 896 bytes while pitch * rows is 1,024 bytes. Passing 1,024 bytes to from_raw_parts_mut is immediate UB, and an overflowing range in readable() loops across the entire 64-bit address space.

THE FIX.
Clamp Staging::detile slices via subresource_len() against self.bytes, use checked_add in readable() / committed_by_query() / mapped_bytes(), and share is_readable_commit() across both VirtualQuery paths to reject PAGE_NOACCESS and PAGE_GUARD.

SCOPE.
Only texture.rs staging slice bounds and page readability helpers. Detiling math and upload scheduling are unchanged.
A third of this branch was untested, and the test looked like it covered it.
`saturating_add(len - 1)` used to saturate the end of the range to the top of
the address space; `checked_add` refuses instead. For the input the test used
(`readable(usize::MAX - 8, 64, true)`) both give `(0, 1)`, because a range
that starts one page below the top covers one page either way. So the
assertion passed against the defect it was written for: break
`checked_add` back into `saturating_add` and the test stays green.

The input that tells them apart is a range that *starts low* and ends past
the top. Saturating turns `page_span(0x1000, usize::MAX)` into pages 1
through 4,503,599,627,370,495 -- four and a half quadrillion `VirtualQuery`
calls and a working-set query for each, from one call to `readable`. That is
a hang, not a wrong answer, which is why it went unnoticed and why the
assertion is on the decision rather than on the answer: `page_span` is now a
function of its own and is asserted to return `None`.

`len == 0` is refused by the same function; before, `len - 1` underflowed,
which panicked in a debug build and wrapped to `usize::MAX` in a release one
-- the same quadrillion-page walk, from a zero-length range.

Probe: make `page_span` saturate -> the new test fails with
`left: Some((1, 4503599627370495)), right: None` -> restored byte-identical
(sha256 d83cdb9a441a8359). Probed separately, reverting the staging clamp
still fails `subresource_len_stays_inside_staging_bytes` with
`left: 1024, right: 896`.

cargo test -p victory-gpu --lib texture::: 5 passed, 0 failed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:37:21 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/165/head:github-bot/gh-165'
git switch 'github-bot/gh-165'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-165'
git switch 'github-bot/gh-165'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-165'
git switch 'github-bot/gh-165'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-165'
git switch 'main'
git merge --squash 'github-bot/gh-165'
git switch 'main'
git merge --ff-only 'github-bot/gh-165'
git switch 'main'
git merge 'github-bot/gh-165'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.