xmem: use saturating_add for view end bounds in aliases (fixes #2413) #2419

Closed
pushkarreddyy wants to merge 1 commit from upstream-pr/2421 into main
pushkarreddyy commented 2026-11-25 13:12:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-25T13:12:20Z.


Fixes #2413.

Root Cause

In crates/victory-hle/src/modules/xmem.rs (aliases), end is computed safely with at.saturating_add(len), but the preceding-view filter (*base + size > at) and range clamp (end.min(base + size)) used unchecked addition, which can overflow if a view's base + size reaches usize::MAX.

Implementation Details

  • Updated both *base + size and base + size in crates/victory-hle/src/modules/xmem.rs (aliases) to use base.saturating_add(*size).

Verification

  • Added aliases_handles_saturating_range_bounds unit test in crates/victory-hle/src/modules/xmem.rs exercising aliases near usize::MAX.
  • CI check (workspace build, clippy -D warnings, and tests) is green.

Scope & Limitations

  • Only the two unchecked view-end additions in aliases are changed; virtual memory and physical-page behavior are unchanged.
  • No title files or proprietary assets were used.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6605276694 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2421). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-25T13:12:20Z. --- Fixes #2413. ### Root Cause In `crates/victory-hle/src/modules/xmem.rs` (`aliases`), `end` is computed safely with `at.saturating_add(len)`, but the preceding-view filter (`*base + size > at`) and range clamp (`end.min(base + size)`) used unchecked addition, which can overflow if a view's `base + size` reaches `usize::MAX`. ### Implementation Details - Updated both `*base + size` and `base + size` in `crates/victory-hle/src/modules/xmem.rs` (`aliases`) to use `base.saturating_add(*size)`. ### Verification - Added `aliases_handles_saturating_range_bounds` unit test in `crates/victory-hle/src/modules/xmem.rs` exercising `aliases` near `usize::MAX`. - CI `check` (workspace build, clippy `-D warnings`, and tests) is green. ### Scope & Limitations - Only the two unchecked view-end additions in `aliases` are changed; virtual memory and physical-page behavior are unchanged. - No title files or proprietary assets were used.
DenRakEiw commented 2026-11-25 14:06:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T13:18:42Z.

Review: APPROVED


Verified against origin/main (940d32f):

  • Using saturating_add for view end bounds in xmem::aliases cleanly prevents overflow on malformed/near-max ranges (#2413).
  • cargo test -p victory-hle xmem::: 11/11 passed (including aliases_handles_saturating_range_bounds).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6058334948 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2421#pullrequestreview-6058334948). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T13:18:42Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`): - Using `saturating_add` for view end bounds in `xmem::aliases` cleanly prevents overflow on malformed/near-max ranges (`#2413`). - `cargo test -p victory-hle xmem::`: 11/11 passed (including `aliases_handles_saturating_range_bounds`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 14:06:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T13:19:31Z.

Review: COMMENTED


xmem: saturating_add for view end bounds in aliases. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Correct hardening.

<!-- greyfinch-sync:Marvisak/VIctory:review:6058346769 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2421#pullrequestreview-6058346769). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T13:19:31Z. **Review:** COMMENTED --- xmem: saturating_add for view end bounds in aliases. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Correct hardening.
pushkarreddyy commented 2026-11-25 14:07:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-25T13:36:25Z.


Hi @Marvisak — this PR resolves #2413 (using saturating_add for base + size view bounds in xmem::aliases), with unit tests and CI (check) passing. Ready for review and merge whenever convenient!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7164022797 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2421#issuecomment-7164022797). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-25T13:36:25Z. --- Hi @Marvisak — this PR resolves #2413 (using `saturating_add` for `base + size` view bounds in `xmem::aliases`), with unit tests and CI (`check`) passing. Ready for review and merge whenever convenient!
kotekompotek commented 2026-11-25 14:07:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T13:53:21Z.


Reviewed: end was already saturating_add, and this covers the two remaining base + size spots — consistent. The usize::MAX - PAGE + 1 test hits the path that would panic in debug builds.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7164436943 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2421#issuecomment-7164436943). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T13:53:21Z. --- Reviewed: `end` was already `saturating_add`, and this covers the two remaining `base + size` spots — consistent. The `usize::MAX - PAGE + 1` test hits the path that would panic in debug builds.
dpalacios69 commented 2026-11-25 15:04:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by dpalacios69 on 2026-11-25T14:09:02Z.


Static read of d28cb1d against main 940d32f (not run: victory-hle is Windows-only).

On the earlier comment that the usize::MAX - PAGE + 1 test "hits the path that would panic in debug builds": as far as I can tell it does not reach the changed arithmetic. end was already at.saturating_add(len), and base + size (xmem.rs:412, :417) can only overflow when a view in physical().views has base + size > usize::MAX. The test inserts no view, so range(..=at).next_back() finds nothing (or, if another test left a small view in the shared PHYSICAL, base + size is far from overflowing), and starts over at + 1..end is empty. So aliases_handles_saturating_range_bounds should pass on main too, and the approvals above ran it only with the fix applied.

A test that fails before the patch would insert a view near the top, e.g. base usize::MAX - PAGE + 1 with a size of 2 * PAGE, call aliases on that range, and assert the result. Could someone on Windows confirm what the current test does with the two saturating_add lines reverted?

AI-assisted analysis.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7164833688 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2421#issuecomment-7164833688). Originally posted by [dpalacios69](https://github.com/dpalacios69) on 2026-11-25T14:09:02Z. --- Static read of `d28cb1d` against `main` `940d32f` (not run: `victory-hle` is Windows-only). On the earlier comment that the `usize::MAX - PAGE + 1` test "hits the path that would panic in debug builds": as far as I can tell it does not reach the changed arithmetic. `end` was already `at.saturating_add(len)`, and `base + size` (`xmem.rs:412`, `:417`) can only overflow when a view in `physical().views` has `base + size > usize::MAX`. The test inserts no view, so `range(..=at).next_back()` finds nothing (or, if another test left a small view in the shared `PHYSICAL`, `base + size` is far from overflowing), and `starts` over `at + 1..end` is empty. So `aliases_handles_saturating_range_bounds` should pass on `main` too, and the approvals above ran it only with the fix applied. A test that fails before the patch would insert a view near the top, e.g. base `usize::MAX - PAGE + 1` with a size of `2 * PAGE`, call `aliases` on that range, and assert the result. Could someone on Windows confirm what the current test does with the two `saturating_add` lines reverted? AI-assisted analysis.
pushkarreddyy commented 2026-11-25 15:04:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-25T14:18:57Z.


Thank you @dpalacios69 for catching that test gap! Updated the test in c315049 (aliases_handles_saturating_range_bounds) to allocate a page, insert a view into physical().views at usize::MAX - PAGE + 1 with size 2 * PAGE, and call aliases(top + 1, PAGE, &mut out) so both base.saturating_add(*size) paths are directly exercised.

Testers on Windows 10/11 and Wine/Proton setups we haven't tested yet are welcome to verify this fix.

AI-assisted: yes (Gemini 4 Neon)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7165087379 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2421#issuecomment-7165087379). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-25T14:18:57Z. --- Thank you @dpalacios69 for catching that test gap! Updated the test in `c315049` (`aliases_handles_saturating_range_bounds`) to allocate a page, insert a view into `physical().views` at `usize::MAX - PAGE + 1` with size `2 * PAGE`, and call `aliases(top + 1, PAGE, &mut out)` so both `base.saturating_add(*size)` paths are directly exercised. Testers on Windows 10/11 and Wine/Proton setups we haven't tested yet are welcome to verify this fix. AI-assisted: yes (Gemini 4 Neon)
Marvisak commented 2026-11-25 16:09:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:32:18Z.


Automated triage for the maintainers.

Proposed close. This changes *base + size and base + size in crates/victory-hle/src/modules/xmem.rs aliases to saturating_add. The keys of physical().views are the addresses of title views mapped into the process (views.insert(base, (len, slice)) in the mapping path), and their base + size stays inside the user address space, far below usize::MAX, so the overflow cannot happen. The new test reaches the changed lines only by inserting a view at usize::MAX - PAGE + 1 by hand, which xmem never creates. A path through xmem that records a view ending past the top of the address space would change this verdict.

This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166997548 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2421#issuecomment-7166997548). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:32:18Z. --- <!-- victory-triage v1 head=c31504961eb48597b72c30f7ef7cd27141590ff2 verdict=proposed-close at=2026-11-25T15:36:00Z --> Automated triage for the maintainers. **Proposed close.** This changes `*base + size` and `base + size` in `crates/victory-hle/src/modules/xmem.rs` `aliases` to `saturating_add`. The keys of `physical().views` are the addresses of title views mapped into the process (`views.insert(base, (len, slice))` in the mapping path), and their `base + size` stays inside the user address space, far below `usize::MAX`, so the overflow cannot happen. The new test reaches the changed lines only by inserting a view at `usize::MAX - PAGE + 1` by hand, which xmem never creates. A path through xmem that records a view ending past the top of the address space would change this verdict. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-26 16:04:28 +00:00
pushkarreddyy commented 2026-11-26 16:05:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-26T15:19:13Z.


Closing this PR after reviewing the triage verification on main:

  • physical().views only records user-space mapped addresses well below usize::MAX, so base + size in aliases cannot overflow during real xmem execution.

Thank you for the audit.

AI-assisted: yes (Gemini 4 Neon)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7196097724 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2421#issuecomment-7196097724). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-26T15:19:13Z. --- Closing this PR after reviewing the triage verification on `main`: - `physical().views` only records user-space mapped addresses well below `usize::MAX`, so `base + size` in `aliases` cannot overflow during real `xmem` execution. Thank you for the audit. AI-assisted: yes (Gemini 4 Neon)

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!2419
No description provided.