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

Open
b3IKK wants to merge 1 commit from upstream-pr/1185 into main
b3IKK commented 2026-11-24 12:57:01 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T12:57:01Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:5472574698 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1185). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T12:57:01Z. --- `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.
DavidM1911 commented 2026-11-24 14:06:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T13:08:39Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7132409775 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1185#issuecomment-7132409775). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T13:08:39Z. --- 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.
JoaoPauluu commented 2026-11-24 15:05:57 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by JoaoPauluu on 2026-11-24T14:34:47Z.


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.
<!-- greyfinch-sync:ivall/xcl:comment:7134606634 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1185#issuecomment-7134606634). Originally posted by [JoaoPauluu](https://github.com/JoaoPauluu) on 2026-11-24T14:34:47Z. --- 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.
github-actions[bot] commented 2026-11-27 21:26:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-27T21:26:38Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7228448589 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1185#issuecomment-7228448589). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T21:26:38Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 05:02:04 +00:00 (Migrated from github.com)

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


Triage (#1607) – head 1ba5518

Part of this is on main already: Uma::sparse_reserve uses checked_next_multiple_of there (commit 237b7181). The rest is not: main's kernelx::VirtualAlloc graphics commit path still does size.max(1).div_ceil(align) * align and the 4 MiB reserve path div_ceil(4 << 20) * (4 << 20), which wrap to 0 in release for sizes near usize::MAX, and Uma::sparse_alloc_in still rounds unchecked. No open PR makes the same change.
No problems found in the diff; a rebase should drop the sparse_reserve hunk.
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:7301354468 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1185#issuecomment-7301354468). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:02:04Z. --- **Triage (#1607)** – head 1ba5518 Part of this is on main already: `Uma::sparse_reserve` uses `checked_next_multiple_of` there (commit 237b7181). The rest is not: main's `kernelx::VirtualAlloc` graphics commit path still does `size.max(1).div_ceil(align) * align` and the 4 MiB reserve path `div_ceil(4 << 20) * (4 << 20)`, which wrap to 0 in release for sizes near usize::MAX, and `Uma::sparse_alloc_in` still rounds unchecked. No open PR makes the same change. No problems found in the diff; a rebase should drop the `sparse_reserve` hunk. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
fromthemoonx commented 2026-12-03 08:11:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-03T08:11:40Z.


merge-check
base: 16c2fd082a
head: 1ba5518d15
merge: clean (sparse_reserve is on main already, so that hunk disappears in the merge)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3009 passed, 4 failed, 56 ignored (base: 3012 passed, 0 failed, 56 ignored, counted on 395405e4; only docs and scripts changed since); SKIPPED: 25, the same as base (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels ("superseded unaligned extra_pages must be reclaimed immediately") and three stream_recheck_* tests that stop at the lock it poisoned. That is the flake #2591 fixes (the test reads a page count of the whole device), not this PR. A second run of the whole lib binary passed: 2857 passed, 0 failed, 36 ignored. An earlier run of the suite ended with 0xc0000005 during the thread-dump test (#3586, on main too)
review: read the whole diff as it merges (2 files, +27 -12). No finding.

  1. With main's rounding put back in the MEM_GRAPHICS commit path, the new test aborts (0xc0000409, "attempt to multiply with overflow"). I did not undo the other two places one by one.
  2. The test asks only for usize::MAX, so I also tried sizes that still round without overflow (scratch test): 9 sizes from 2^47 to usize::MAX, with 6 allocation types each. All 54 calls returned null, and the used bytes of the coherent and sparse windows were the same before and after.
  3. Ordinary sizes round as intended: 1 byte reserves 64 KiB, 64 KiB + 1 reserves 128 KiB, and with 4 MiB pages 1 byte reserves 4 MiB and 5 MiB reserves 8 MiB.
  4. One more div_ceil(align) * align is in VirtualAlloc (XCL_COHERENT_HEAPS=sparse). It cannot overflow: the branch is taken only for sizes up to uma::MAX_SIZE.
  5. Not tested: a retail title (none on this machine), a release build, AMD. The smoke pair in the description is on 47efa2ec, from before most of today's VirtualAlloc.
    performance: none: the same rounding with an overflow check, once per allocation.
    verdict: safe to merge
    hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
<!-- greyfinch-sync:ivall/xcl:comment:7371945745 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1185#issuecomment-7371945745). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-12-03T08:11:40Z. --- merge-check base: 16c2fd082ae2e96967a2ab553bc2e1032e491f70 head: 1ba5518d154d00b03c024a52d2bdba9e57346e56 merge: clean (`sparse_reserve` is on main already, so that hunk disappears in the merge) build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3009 passed, 4 failed, 56 ignored (base: 3012 passed, 0 failed, 56 ignored, counted on 395405e4; only docs and scripts changed since); SKIPPED: 25, the same as base (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: `title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels` ("superseded unaligned extra_pages must be reclaimed immediately") and three `stream_recheck_*` tests that stop at the lock it poisoned. That is the flake #2591 fixes (the test reads a page count of the whole device), not this PR. A second run of the whole lib binary passed: 2857 passed, 0 failed, 36 ignored. An earlier run of the suite ended with 0xc0000005 during the thread-dump test (#3586, on main too) review: read the whole diff as it merges (2 files, +27 -12). No finding. 1) With main's rounding put back in the `MEM_GRAPHICS` commit path, the new test aborts (0xc0000409, "attempt to multiply with overflow"). I did not undo the other two places one by one. 2) The test asks only for `usize::MAX`, so I also tried sizes that still round without overflow (scratch test): 9 sizes from 2^47 to `usize::MAX`, with 6 allocation types each. All 54 calls returned null, and the used bytes of the coherent and sparse windows were the same before and after. 3) Ordinary sizes round as intended: 1 byte reserves 64 KiB, 64 KiB + 1 reserves 128 KiB, and with 4 MiB pages 1 byte reserves 4 MiB and 5 MiB reserves 8 MiB. 4) One more `div_ceil(align) * align` is in `VirtualAlloc` (`XCL_COHERENT_HEAPS=sparse`). It cannot overflow: the branch is taken only for sizes up to `uma::MAX_SIZE`. 5) Not tested: a retail title (none on this machine), a release build, AMD. The smoke pair in the description is on 47efa2ec, from before most of today's `VirtualAlloc`. performance: none: the same rounding with an overflow check, once per allocation. verdict: safe to merge hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
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/1185:upstream-pr/1185'
git switch 'upstream-pr/1185'

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