kernelx: use checked size rounding in VirtualAlloc and Uma::sparse_reserve #1185

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

@⁠b3IKK on GitHub · 2026-11-24 12:57 UTC · original
b3IKK:kernelx/virtualalloc-size-overflow → main · +28 −13 · 2 files · 1 commits · open

VirtualAlloc rounded MEM_GRAPHICS sizes with div_ceil(align) * align, and Uma::sparse_reserve / Uma::sparse_alloc_in rounded with (size.max(1) + GRAIN - 1) & !(GRAIN - 1). For sizes near usize::MAX, these overflowed—aborting the process on panic inside extern "system" fn in debug builds and wrapping to 0 (returning a non-null allocation) in release builds.

This switches them to checked_next_multiple_of (matching XMemAllocDefault and Uma::sparse_alloc) and checked_next_power_of_two in sparse_alloc_in.

What is in it

  • crates/xcl/src/hle/kernelx.rs: use checked_next_multiple_of in VirtualAlloc's MEM_GRAPHICS commit and 4 MiB reserve paths; add virtual_alloc_and_sparse_reserve_overflows_fail_cleanly (aborted with 0xc0000409 before the fix).
  • crates/xcl/src/gpu/uma.rs: use checked_next_multiple_of in sparse_reserve and sparse_alloc_in, and checked_next_power_of_two for alignment in sparse_alloc_in.

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 742 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware)
  • cargo test -p xcl --lib virtual_alloc_and_sparse_reserve_overflows_fail_cleanly: 1 passed, 0 failed (panicked/aborted with 0xc0000409 before the fix)

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: 1ba5518d154d00b03c024a52d2bdba9e57346e56
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1638 (PresentX 1638)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14585 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited kernelx.rs and uma.rs, wrote the fix and regression test, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
**@⁠b3IKK** on GitHub · 2026-11-24 12:57 UTC · [original](https://github.com/xcl-dev/xcl/pull/1185) `b3IKK:kernelx/virtualalloc-size-overflow` → `main` · +28 −13 · 2 files · 1 commits · open `VirtualAlloc` rounded `MEM_GRAPHICS` sizes with `div_ceil(align) * align`, and `Uma::sparse_reserve` / `Uma::sparse_alloc_in` rounded with `(size.max(1) + GRAIN - 1) & !(GRAIN - 1)`. For sizes near `usize::MAX`, these overflowed—aborting the process on panic inside `extern "system" fn` in debug builds and wrapping to `0` (returning a non-null allocation) in release builds. This switches them to `checked_next_multiple_of` (matching `XMemAllocDefault` and `Uma::sparse_alloc`) and `checked_next_power_of_two` in `sparse_alloc_in`. **What is in it** - `crates/xcl/src/hle/kernelx.rs`: use `checked_next_multiple_of` in `VirtualAlloc`'s `MEM_GRAPHICS` commit and 4 MiB reserve paths; add `virtual_alloc_and_sparse_reserve_overflows_fail_cleanly` (aborted with `0xc0000409` before the fix). - `crates/xcl/src/gpu/uma.rs`: use `checked_next_multiple_of` in `sparse_reserve` and `sparse_alloc_in`, and `checked_next_power_of_two` for alignment in `sparse_alloc_in`. **Tested** - hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2609.5, debug (`scripts/test.ps1`) and release (`scripts/smoke.ps1`) - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): 742 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 `no wave64` — `gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware`) - `cargo test -p xcl --lib virtual_alloc_and_sparse_reserve_overflows_fail_cleanly`: 1 passed, 0 failed (panicked/aborted with `0xc0000409` before the fix) **Regression run** main: ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1226 (PresentX 1226) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15341 MB ``` PR: ``` xcl-smoke v1 commit: 1ba5518d154d00b03c024a52d2bdba9e57346e56 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1638 (PresentX 1638) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14585 MB ``` **Not tested** - AMD GPUs and native wave64 hardware - other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine **AI use** - OpenCode, Gemini 4 Neon: audited `kernelx.rs` and `uma.rs`, wrote the fix and regression test, ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
VirtualAlloc rounded MEM_GRAPHICS sizes with `div_ceil(align) * align`,
and Uma::sparse_reserve / sparse_alloc_in rounded with
`(size.max(1) + GRAIN - 1) & !(GRAIN - 1)`. Near usize::MAX these
overflowed: aborting on panic in extern "system" fn in debug builds and
wrapping to 0 (returning a non-null allocation) in release builds.

Use checked_next_multiple_of (matching XMemAllocDefault and
Uma::sparse_alloc) and checked_next_power_of_two in sparse_alloc_in,
with a regression test covering VirtualAlloc and Uma::sparse_reserve /
sparse_alloc_in at usize::MAX.

Assisted-by: OpenCode (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:46:22 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 13:08 UTC · original

Independent verification on current main 384f7f7 (branch already contains it — clean).

Machine: Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug.

  • cargo test -p xcl --lib kernelx: 33 passed, 0 failed, 1 ignored.
  • cargo test -p xcl --lib uma: 10 passed, 0 failed — the checked size rounding in VirtualAlloc/Uma::sparse_reserve keeps both suites green.

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

**@⁠DavidM1911** on GitHub · 2026-11-24 13:08 UTC · [original](https://github.com/xcl-dev/xcl/pull/1185#issuecomment-7132409775) Independent verification on current `main` `384f7f7` (branch already contains it — clean). **Machine:** Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug. - `cargo test -p xcl --lib kernelx`: **33 passed, 0 failed, 1 ignored**. - `cargo test -p xcl --lib uma`: **10 passed, 0 failed** — the checked size rounding in `VirtualAlloc`/`Uma::sparse_reserve` keeps both suites green. AI use: test runs by Devin (Cognition, SWE-2); no code changes.
Author
Owner

@⁠JoaoPauluu on GitHub · 2026-11-24 14:34 UTC · original

A run on Linux under Wine.

Tested

  • RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from #257, DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. 1ba5518 as it is (based on 47efa2e), no uncommitted changes. The lib test binary runs under Wine, one test per process (--exact). VirtualAlloc is Wine's here.
  • hle::kernelx::tests::virtual_alloc_and_sparse_reserve_overflows_fail_cleanly: ok, no SKIPPED.

Not tested

  • whether the new tests fail without the change on this stack; the rest of the suite on this head; a retail title; native Windows.

AI use

  • Claude Code (Claude Opus 5.5) built the head, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.
**@⁠JoaoPauluu** on GitHub · 2026-11-24 14:34 UTC · [original](https://github.com/xcl-dev/xcl/pull/1185#issuecomment-7134606634) A run on Linux under Wine. **Tested** - RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from #257, DXC 1.9.2609.5. Release, cross-built for `x86_64-pc-windows-gnu`. `1ba5518` as it is (based on `47efa2e`), no uncommitted changes. The lib test binary runs under Wine, one test per process (`--exact`). `VirtualAlloc` is Wine's here. - `hle::kernelx::tests::virtual_alloc_and_sparse_reserve_overflows_fail_cleanly`: **ok**, no SKIPPED. **Not tested** - whether the new tests fail without the change on this stack; the rest of the suite on this head; a retail title; native Windows. **AI use** - Claude Code (Claude Opus 5.5) built the head, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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