GPU/inplace+watch: guard zero-length ranges and VEH fault overflow #622

Open
github-bot wants to merge 1 commit from github-bot/gh-622 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-25 03:25 UTC · original
AscilCH:fix/gpu-inplace-and-watch-zero-len-panic-and-overflow → main · +90 −4 · 2 files · 1 commits · open

Problem

Zero-length ranges and fault-address edge cases in crates/victory-gpu/src/inplace.rs and crates/victory-gpu/src/watch.rs could panic inside BTreeMap/BTreeSet::range or strip guards from untouched blocks:

  1. inplace::views_gone(at, 0) panicked unconditionally whenever VIEWS was non-empty: with len == 0 and at < usize::MAX, end == at, so v.views.range(at.saturating_add(1)..end) evaluated (at + 1)..at and panicked while holding the global VIEWS mutex (and across extern "system" FFI callers of title_memory_remapped).
  2. watch::title_memory_remapped(at, 0) and watch::writing(at, 0) stripped guards on unaligned addresses: at / BLOCK .. at.div_ceil(BLOCK) evaluates to k..(k + 1) when at is not BLOCK-aligned, erroneously unguarding/clearing an untouched block (and watch::open panicked if start > end).
  3. watch::on_write computed at + 1 unchecked inside the extern "system" VEH, overflowing/wrapping if at == usize::MAX.

Fix

  • Early-return on len == 0 in inplace::views_gone, inplace::title_memory_freeing, watch::title_memory_remapped, and watch::writing (and use base.saturating_add(view.0) in views_gone).
  • Early-return on start >= end in watch::open, and use at.checked_add(1) in watch::on_write.
  • Add synthetic unit tests views_gone_ignores_zero_length_without_panicking and zero_length_remap_and_writing_do_not_panic_or_unguard.

Verification

  • cargo test -p victory-gpu --locked inplace:: -- --test-threads=1: 7 passed, 0 failed
  • cargo test -p victory-gpu --locked watch:: -- --test-threads=1: 8 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings: clean (default-features build on c96b4f2 has only the pre-existing dead_code warning on placed_filler_twins, tracked in #491)

(AI-assisted; synthetic unit tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-25 03:25 UTC · [original](https://github.com/fxceless/victory-fork/pull/622) `AscilCH:fix/gpu-inplace-and-watch-zero-len-panic-and-overflow` → `main` · +90 −4 · 2 files · 1 commits · open ## Problem Zero-length ranges and fault-address edge cases in `crates/victory-gpu/src/inplace.rs` and `crates/victory-gpu/src/watch.rs` could panic inside `BTreeMap`/`BTreeSet::range` or strip guards from untouched blocks: 1. **`inplace::views_gone(at, 0)` panicked unconditionally** whenever `VIEWS` was non-empty: with `len == 0` and `at < usize::MAX`, `end == at`, so `v.views.range(at.saturating_add(1)..end)` evaluated `(at + 1)..at` and panicked while holding the global `VIEWS` mutex (and across `extern "system"` FFI callers of `title_memory_remapped`). 2. **`watch::title_memory_remapped(at, 0)` and `watch::writing(at, 0)` stripped guards on unaligned addresses**: `at / BLOCK .. at.div_ceil(BLOCK)` evaluates to `k..(k + 1)` when `at` is not `BLOCK`-aligned, erroneously unguarding/clearing an untouched block (and `watch::open` panicked if `start > end`). 3. **`watch::on_write` computed `at + 1` unchecked** inside the `extern "system"` VEH, overflowing/wrapping if `at == usize::MAX`. ## Fix - Early-return on `len == 0` in `inplace::views_gone`, `inplace::title_memory_freeing`, `watch::title_memory_remapped`, and `watch::writing` (and use `base.saturating_add(view.0)` in `views_gone`). - Early-return on `start >= end` in `watch::open`, and use `at.checked_add(1)` in `watch::on_write`. - Add synthetic unit tests `views_gone_ignores_zero_length_without_panicking` and `zero_length_remap_and_writing_do_not_panic_or_unguard`. ## Verification - `cargo test -p victory-gpu --locked inplace:: -- --test-threads=1`: 7 passed, 0 failed - `cargo test -p victory-gpu --locked watch:: -- --test-threads=1`: 8 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings`: clean (default-features build on `c96b4f2` has only the pre-existing `dead_code` warning on `placed_filler_twins`, tracked in #491) *(AI-assisted; synthetic unit tests only, no game data.)*
- `inplace::views_gone(at, 0)` evaluated `v.views.range((at + 1)..at)` whenever
  any view was registered, panicking unconditionally inside `BTreeMap::range`
  while holding the global `VIEWS` mutex (and across FFI callers of
  `title_memory_remapped`).
- `watch::title_memory_remapped(at, 0)` and `watch::writing(at, 0)` with an
  unaligned `at` computed `k..(k + 1)`, stripping guard/absent tracking from an
  untouched block (and `watch::open` panicked if `start > end`).
- `watch::on_write` computed `at + 1` inside the `extern "system"` VEH,
  overflowing/wrapping if `at == usize::MAX`.

Guard `len == 0` in `inplace::views_gone`, `inplace::title_memory_freeing`,
`watch::title_memory_remapped`, and `watch::writing`, guard `start >= end` in
`watch::open`, use `checked_add(1)` in `watch::on_write`, and add synthetic
unit tests in both modules.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:47:35 +00:00
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/watch.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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