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

Open
AscilCH wants to merge 1 commit from upstream-pr/622 into main
AscilCH commented 2026-11-25 03:25:58 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T03:25:58Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:issue:6596489857 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/622). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T03:25:58Z. --- ## 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.)*
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/622:upstream-pr/622'
git switch 'upstream-pr/622'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/622'
git switch 'upstream-pr/622'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/622'
git switch 'upstream-pr/622'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/622'
git switch 'main'
git merge --squash 'upstream-pr/622'
git switch 'main'
git merge --ff-only 'upstream-pr/622'
git switch 'main'
git merge 'upstream-pr/622'
git push origin 'main'
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
fxceless/victory-fork!622
No description provided.