KernelX: reject cross-allocation boundary overruns in map_pages (#1488) #1625

Open
slopgobblercobbler wants to merge 1 commit from upstream-pr/1635 into main
slopgobblercobbler commented 2026-11-24 18:27:07 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by slopgobblercobbler on 2026-11-24T18:27:07Z.


Summary

Rejects cross-allocation boundary overruns in kernelx::map_pages in crates/xcl/src/hle/kernelx.rs (Issue #1488, finding XCL-BT-007):

  1. Physical allocation boundary validation:
    • Factored out page_block(page: usize) -> Option<(usize, usize, usize, usize)> helper to safely retrieve (first_index, count, live, home) from PHYS_BLOCKS.
    • In map_pages: for consecutive page requests (consecutive == true), validates that the requested range [first_idx..first_idx + n] does not cross or overrun the enclosing PHYS_BLOCKS allocation boundary (end_index <= block_end).
    • If an allocation boundary overrun or cross-allocation crossing is detected, sets last error to ERROR_INVALID_PARAMETER (87) and returns null_mut(), preventing commit_homes from committing unallocated memory past allocation bounds.
  2. Unit tests:
    • Added unit test map_pages_rejects_cross_allocation_overrun: verifies valid sub-allocations within boundaries succeed; verifies consecutive ranges extending past block boundaries fail with ERROR_INVALID_PARAMETER; verifies cross-boundary ranges fail; and verifies overruns with explicit virtual addresses fail.

Changes

  • crates/xcl/src/hle/kernelx.rs:
    • page_block helper for allocation bounds queries.
    • Range validation against enclosing allocation bounds in map_pages.
    • Synthetic unit test verifying boundary enforcement.

Tested

  • Windows 11 x64, Rust MSVC stable.
  • cargo test -p xcl -- kernelx: 34 passed, 0 failed.
  • Clean diff: zero trailing whitespace, clean rebase on latest upstream/main, GPL-2.0-only compliant, zero retail game data or keys.

Not Tested

  • Retail title physical page mapping under severe low-memory page compaction.

AI Use

Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity

<!-- greyfinch-sync:ivall/xcl:issue:5477983270 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1635). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-11-24T18:27:07Z. --- ## Summary Rejects cross-allocation boundary overruns in `kernelx::map_pages` in `crates/xcl/src/hle/kernelx.rs` (Issue #1488, finding `XCL-BT-007`): 1. **Physical allocation boundary validation**: - Factored out `page_block(page: usize) -> Option<(usize, usize, usize, usize)>` helper to safely retrieve `(first_index, count, live, home)` from `PHYS_BLOCKS`. - In `map_pages`: for consecutive page requests (`consecutive == true`), validates that the requested range `[first_idx..first_idx + n]` does not cross or overrun the enclosing `PHYS_BLOCKS` allocation boundary (`end_index <= block_end`). - If an allocation boundary overrun or cross-allocation crossing is detected, sets last error to `ERROR_INVALID_PARAMETER` (87) and returns `null_mut()`, preventing `commit_homes` from committing unallocated memory past allocation bounds. 2. **Unit tests**: - Added unit test `map_pages_rejects_cross_allocation_overrun`: verifies valid sub-allocations within boundaries succeed; verifies consecutive ranges extending past block boundaries fail with `ERROR_INVALID_PARAMETER`; verifies cross-boundary ranges fail; and verifies overruns with explicit virtual addresses fail. ## Changes - `crates/xcl/src/hle/kernelx.rs`: - `page_block` helper for allocation bounds queries. - Range validation against enclosing allocation bounds in `map_pages`. - Synthetic unit test verifying boundary enforcement. ## Tested - Windows 11 x64, Rust MSVC stable. - `cargo test -p xcl -- kernelx`: 34 passed, 0 failed. - Clean diff: zero trailing whitespace, clean rebase on latest `upstream/main`, GPL-2.0-only compliant, zero retail game data or keys. ## Not Tested - Retail title physical page mapping under severe low-memory page compaction. ## AI Use Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity
gut0to commented 2026-11-24 18:31:11 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-24T18:31:11Z.

Review: COMMENTED


AI-assisted review; I checked the unavailable-UMA path against the GPU test guidance.

<!-- greyfinch-sync:ivall/xcl:review:6045085250 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1635#pullrequestreview-6045085250). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-24T18:31:11Z. **Review:** COMMENTED --- AI-assisted review; I checked the unavailable-UMA path against the GPU test guidance.
gut0to commented 2026-11-24 19:08:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-24T18:31:11Z.

Code review location: crates/xcl/src/hle/kernelx.rs; line 2147

@@ -2113,6 +2139,64 @@ mod tests {
         }
     }
 
+    /// Consecutive physical page mapping rejects ranges that cross or overrun distinct
+    /// PHYS_BLOCKS allocation boundaries, setting ERROR_INVALID_PARAMETER (finding XCL-BT-007).
+    #[test]
+    fn map_pages_rejects_cross_allocation_overrun() {
+        unsafe {
+            let Some(_u) = crate::gpu::uma::get() else { return };

When UMA is unavailable this test returns normally, so the test harness counts it as passed rather than skipped. CONTRIBUTING.md asks GPU-dependent tests to call gpu::recomp::runner::skip_test(...) before returning when the adapter cannot run them. Could this report an appropriate skip reason here so the workspace totals distinguish skipped coverage from a pass? AI-assisted review; I verified the test path and guide.

<!-- greyfinch-sync:ivall/xcl:review-comment:4684703714 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1635#discussion_r4684703714). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-24T18:31:11Z. **Code review location:** `crates/xcl/src/hle/kernelx.rs`; line 2147 ~~~~diff @@ -2113,6 +2139,64 @@ mod tests { } } + /// Consecutive physical page mapping rejects ranges that cross or overrun distinct + /// PHYS_BLOCKS allocation boundaries, setting ERROR_INVALID_PARAMETER (finding XCL-BT-007). + #[test] + fn map_pages_rejects_cross_allocation_overrun() { + unsafe { + let Some(_u) = crate::gpu::uma::get() else { return }; ~~~~ --- When UMA is unavailable this test returns normally, so the test harness counts it as passed rather than skipped. CONTRIBUTING.md asks GPU-dependent tests to call `gpu::recomp::runner::skip_test(...)` before returning when the adapter cannot run them. Could this report an appropriate skip reason here so the workspace totals distinguish skipped coverage from a pass? AI-assisted review; I verified the test path and guide.
Anon64 commented 2026-11-24 21:08:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Anon64 on 2026-11-24T19:33:18Z.


Regression run for this PR: head 52d87de, based on main 9e3831e. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, XCL_NETWORK=0, A pressed every 4 s from 20 s, scripts/smoke.ps1 on a retail title for 60 s, in one batch with the main run.

main 9e3831e:

xcl-smoke v1
commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1536 (PresentX 1536)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 97380 MB

this PR:

xcl-smoke v1
commit: 52d87def317c507a2970380a271cd4546b60f4e5
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1528 (PresentX 1529)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 96637 MB

No device removals or crashes; 99 % of main's frames, 99 % of its peak commit.

Peak commit on this machine is mostly the pipeline preload's DXC compiles (#1035) and varies from run to run, so it says little about this PR.

Not tested: the whole suite; whether the title reaches the changed code in these 60 s.

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

<!-- greyfinch-sync:ivall/xcl:comment:7142264164 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1635#issuecomment-7142264164). Originally posted by [Anon64](https://github.com/Anon64) on 2026-11-24T19:33:18Z. --- Regression run for this PR: head `52d87de`, based on `main` `9e3831e`. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, `XCL_NETWORK=0`, A pressed every 4 s from 20 s, `scripts/smoke.ps1` on a retail title for 60 s, in one batch with the `main` run. main `9e3831e`: ``` xcl-smoke v1 commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485 gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1536 (PresentX 1536) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 97380 MB ``` this PR: ``` xcl-smoke v1 commit: 52d87def317c507a2970380a271cd4546b60f4e5 gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1528 (PresentX 1529) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 96637 MB ``` No device removals or crashes; 99 % of main's frames, 99 % of its peak commit. Peak commit on this machine is mostly the pipeline preload's DXC compiles (#1035) and varies from run to run, so it says little about this PR. **Not tested:** the whole suite; whether the title reaches the changed code in these 60 s. Written with Claude Code (Claude Opus 5.5) from runs on this machine.
DavidM1911 commented 2026-11-24 21:09:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T20:56:13Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~22 ignored.

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7144255045 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1635#issuecomment-7144255045). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T20:56:13Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~22 ignored. Thanks!
luckyteagirl commented 2026-11-26 09:40:09 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T09:40:09Z.

Review: COMMENTED


On 52d87de, the new same-allocation guard runs before the mapper chooses between its contiguous-home fast path and the per-page path. If two live allocations happen to receive adjacent page IDs, [A.last, B.first] is rejected even when the caller provides a separate reserved non-home destination. That destination already follows the per-page mapping path, which resolves each page's home independently; the same two allocations with nonconsecutive IDs still reach that path. The guard is needed for commit_homes, but this location also blocks a valid explicit mapping. The new test's explicit-address case uses an overrun/nonexistent ID, which does not cover two live adjacent blocks.

Could the same-allocation check apply only when selecting the home fast path, leaving explicit non-home VA to the per-page mapper? Open PR #1143 already has a contiguous_home helper that scopes this check and may be useful to coordinate with. A fixture with known adjacent live IDs should map both into a separate reserved VA and verify their contents/order, while a null/home request for the same pair and a genuinely nonexistent ID still fail. This is exact-head source review; I did not run a UMA fixture.

<!-- greyfinch-sync:ivall/xcl:review:6071716903 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1635#pullrequestreview-6071716903). Originally posted by [luckyteagirl](https://github.com/luckyteagirl) on 2026-11-26T09:40:09Z. **Review:** COMMENTED --- On `52d87de`, the new same-allocation guard runs before the mapper chooses between its contiguous-home fast path and the per-page path. If two live allocations happen to receive adjacent page IDs, `[A.last, B.first]` is rejected even when the caller provides a separate reserved non-home destination. That destination already follows the per-page mapping path, which resolves each page's home independently; the same two allocations with nonconsecutive IDs still reach that path. The guard is needed for `commit_homes`, but this location also blocks a valid explicit mapping. The new test's explicit-address case uses an *overrun/nonexistent* ID, which does not cover two live adjacent blocks. Could the same-allocation check apply only when selecting the home fast path, leaving explicit non-home VA to the per-page mapper? Open PR #1143 already has a `contiguous_home` helper that scopes this check and may be useful to coordinate with. A fixture with known adjacent live IDs should map both into a separate reserved VA and verify their contents/order, while a null/home request for the same pair and a genuinely nonexistent ID still fail. This is exact-head source review; I did not run a UMA fixture.
Foniass commented 2026-12-01 06:21:02 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T06:21:02Z.


Triage (#1607) – head 52d87de

#1143 (opened earlier) fixes the same map_pages bug: consecutive page numbers from two separate AllocateTitlePhysicalPages blocks are treated as one home. Main still has the unfixed check.
The two PRs differ. #1143 treats such a run as scattered pages and maps them one by one when an address is given. This PR returns ERROR_INVALID_PARAMETER for any consecutive run that crosses a block, even with an explicit address, where each page is valid on its own. It also returns ERROR_INVALID_PARAMETER when the first page of a consecutive run is not live, where main went on to the scattered path (ERROR_NOT_SUPPORTED without an address).
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7302771387 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1635#issuecomment-7302771387). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T06:21:02Z. --- **Triage (#1607)** – head 52d87de #1143 (opened earlier) fixes the same `map_pages` bug: consecutive page numbers from two separate `AllocateTitlePhysicalPages` blocks are treated as one home. Main still has the unfixed check. The two PRs differ. #1143 treats such a run as scattered pages and maps them one by one when an address is given. This PR returns ERROR_INVALID_PARAMETER for any consecutive run that crosses a block, even with an explicit address, where each page is valid on its own. It also returns ERROR_INVALID_PARAMETER when the first page of a consecutive run is not live, where main went on to the scattered path (ERROR_NOT_SUPPORTED without an address). Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
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/1635:upstream-pr/1635'
git switch 'upstream-pr/1635'

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/1635'
git switch 'upstream-pr/1635'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1635'
git switch 'upstream-pr/1635'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1635'
git switch 'main'
git merge --squash 'upstream-pr/1635'
git switch 'main'
git merge --ff-only 'upstream-pr/1635'
git switch 'main'
git merge 'upstream-pr/1635'
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
xcl-dev/xcl!1625
No description provided.