Skip to content

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

Open
slopgobblercobbler wants to merge 1 commit into
xcl-dev:mainfrom
slopgobblercobbler:fix/phys-pages-boundary
Open

slopgobblercobbler wants to merge 1 commit into
xcl-dev:mainfrom
slopgobblercobbler:fix/phys-pages-boundary

Conversation

@slopgobblercobbler

Copy link
Copy Markdown
Contributor

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 gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

#[test]
fn map_pages_rejects_cross_allocation_overrun() {
unsafe {
let Some(_u) = crate::gpu::uma::get() else { return };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Anon64 commented Nov 24, 2026

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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

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

Thanks!

@throny throny added the goal:world-load Bears on the retail title getting past world loading label Nov 25, 2026
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
xcl-dev#1635; xcl-dev#1705/xcl-dev#1610 verify-landed)

Port the remainder of upstream xcl-dev#1143 (live pr1143head=076d027b, still OPEN;
cae046e already landed contiguous_home + NULL-VA fast path) and the xcl-dev#1635
companion (live pr1635head=52d87def, still OPEN) as Linux-adapted pin tests;
xcl-dev#1705 (pr1705head=cf0cde26) and xcl-dev#1610 (pr1610head=d3e20531) verified already
landed (f0f9ea3, 27d80db), no code.

- xcl-dev#1635 gap half (matches upstream): map_pages_gap_overrun_is_refused — a
  range holding a never-allocated page number refuses with
  ERROR_INVALID_PARAMETER on both the NULL-address and the explicit-VA path.
- xcl-dev#1635 live-span half (DELIBERATE divergence): upstream refuses any
  consecutive span crossing a block end, but the Linux fast path requires
  contiguous_home and the rest is per-page, so no cross-block home is ever
  returned over unrelated memory — cross_allocation_live_span_splits_...
  pins the split (NULL + explicit VA) with per-page contents. No page_block
  refactor: the split is the fix.
- xcl-dev#1143 home == at arm: N/A on Linux (homes are private malloc, targets are
  always gfx-pool, gfx_contains gate) — remap_at_current_mapping_...
  (twin of upstream mapping_page_at_its_own_home...) pins contents
  preserved with the mapping AWAY-recorded by design.
- Failing-first via graft worktree: verbatim upstream xcl-dev#1635 test FAILED
  (split returns non-null where refusal expected) and verbatim xcl-dev#1143
  own-home test FAILED (on the !physical_page_at bookkeeping assert;
  contents/commit asserts passed) — proving split-wins and home==at N/A.
  Graft removed afterwards.
- Test-only diff (+140/-0): no product change; title/retail behavior
  identical by construction.

Gates: cargo check -p xcl --tests + cargo check --workspace 0 errors;
cargo test -p xcl --lib 640/0/1 (637 + 3); e2e harness 11 D3D12.X GPU-only
fails, 0 non-GPU (by-design M2 baseline); retail SIGSEGV +0x2237320
exit 139, stubs 20, xgr XAsync/XNetworking/XUser — HOLDS, M1-RE TERMINAL.

@luckyteagirl luckyteagirl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants