Memory: reject invalid coherent-memory release requests #1347

Open
anakatt wants to merge 1 commit from upstream-pr/1357 into main
anakatt commented 2026-11-24 15:12:07 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by anakatt on 2026-11-24T15:12:07Z.


I fixed coherent-memory VirtualFree accepting invalid release requests. Unlike the sparse path, it called the allocator's free operation before checking the release size or mutually exclusive release/decommit flags.

What is in it

  • Match the sparse path's validation: reject nonzero MEM_RELEASE size, both release/decommit flags, or neither flag, with ERROR_INVALID_PARAMETER.
  • Keep the allocation and its bytes unchanged on rejection.
  • Exercise the coherent backend with a private real Windows allocation, independent of global UMA/GPU state; retain checks for valid partial decommit and release. Update README.

Tested

I ran cargo test --locked -p xcl --lib hle::kernelx::tests::coherent_release_rejects_invalid_requests_without_freeing -- --exact --test-threads=4 in a Windows 11 Pro ARM64 UTM VM (10.0.26100.1742, 4 GiB RAM, Red Hat VirtIO GPU DOD 22.7.38.43), with Rust 1.100.0, x86_64-pc-windows-msvc and Windows 11 SDK.

  • Before the fix, after extracting the original backend unchanged: 0 passed, 1 failed, 0 ignored, 719 filtered out. MEM_RELEASE with size 65536 returned 1 instead of 0.
  • After the fix: 1 passed, 0 failed, 0 ignored, 719 filtered out. No SKIPPED output.

Source: main 47efa2ecea0a12934719ae69b81930cf02a4b1c3 archive with the changed kernelx.rs copied in before committing 9aa114cda968072c7165578da16313ec0616e646; not a clean head checkout. Shared target directory cleaned for xcl before reproduction; one build job; dev/test debug info disabled.

Not tested

Full workspace suite, native x64 Windows hardware, GPU execution and real-title runs. I did not build or run XCL on macOS. Coherent decommit range and zero-size behavior are unchanged by this PR.

Regression run

The required clean main/head real-title scripts/smoke.ps1 runs have not been performed. No xcl-smoke blocks are available; this remains a draft.

AI use

omp, openai-codex/gpt-6-astra: investigated, wrote the fix and regression test, ran the Windows checks, and prepared this PR.

<!-- greyfinch-sync:ivall/xcl:issue:5474864925 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1357). Originally posted by [anakatt](https://github.com/anakatt) on 2026-11-24T15:12:07Z. --- I fixed coherent-memory VirtualFree accepting invalid release requests. Unlike the sparse path, it called the allocator's free operation before checking the release size or mutually exclusive release/decommit flags. ## What is in it - Match the sparse path's validation: reject nonzero MEM_RELEASE size, both release/decommit flags, or neither flag, with ERROR_INVALID_PARAMETER. - Keep the allocation and its bytes unchanged on rejection. - Exercise the coherent backend with a private real Windows allocation, independent of global UMA/GPU state; retain checks for valid partial decommit and release. Update README. ## Tested I ran `cargo test --locked -p xcl --lib hle::kernelx::tests::coherent_release_rejects_invalid_requests_without_freeing -- --exact --test-threads=4` in a Windows 11 Pro ARM64 UTM VM (10.0.26100.1742, 4 GiB RAM, Red Hat VirtIO GPU DOD 22.7.38.43), with Rust 1.100.0, x86_64-pc-windows-msvc and Windows 11 SDK. - Before the fix, after extracting the original backend unchanged: **0 passed, 1 failed, 0 ignored, 719 filtered out**. MEM_RELEASE with size 65536 returned 1 instead of 0. - After the fix: **1 passed, 0 failed, 0 ignored, 719 filtered out**. No `SKIPPED` output. Source: main `47efa2ecea0a12934719ae69b81930cf02a4b1c3` archive with the changed kernelx.rs copied in before committing `9aa114cda968072c7165578da16313ec0616e646`; not a clean head checkout. Shared target directory cleaned for xcl before reproduction; one build job; dev/test debug info disabled. ## Not tested Full workspace suite, native x64 Windows hardware, GPU execution and real-title runs. I did not build or run XCL on macOS. Coherent decommit range and zero-size behavior are unchanged by this PR. ## Regression run The required clean main/head real-title `scripts/smoke.ps1` runs have not been performed. No `xcl-smoke` blocks are available; this remains a draft. ## AI use omp, openai-codex/gpt-6-astra: investigated, wrote the fix and regression test, ran the Windows checks, and prepared this PR.
DavidM1911 commented 2026-11-24 17:06:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T16:28:12Z.


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

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

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7137714712 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1357#issuecomment-7137714712). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T16:28:12Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. Thanks!
Toons29 commented 2026-11-25 04:10:27 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Toons29 on 2026-11-25T03:25:38Z.


Regression run of 9aa114cd on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: 9aa114cda968072c7165578da16313ec0616e646
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1632 (PresentX 1632)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14686 MB

Against the limits in CONTRIBUTING.md: 101 % of main's frames, 99 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

<!-- greyfinch-sync:ivall/xcl:comment:7151903631 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1357#issuecomment-7151903631). Originally posted by [Toons29](https://github.com/Toons29) on 2026-11-25T03:25:38Z. --- Regression run of `9aa114cd` on the retail title, for this PR's **Regression run** section. Both blocks are from `scripts/smoke.ps1` as printed, not edited: `main` on `4178e888`, this PR on its head commit with no uncommitted changes, same title and time. ``` xcl-smoke v1 commit: 4178e888c0b5be50a2887eda45cdb832763b68a4 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 61 60 60 device removals: 0 crashes: 0 peak commit: 14831 MB ``` ``` xcl-smoke v1 commit: 9aa114cda968072c7165578da16313ec0616e646 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1632 (PresentX 1632) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14686 MB ``` Against the limits in CONTRIBUTING.md: 101 % of `main`'s frames, 99 % of its peak commit, no crash, no device removal. AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, `cargo build --release -p xcl`, 60 s, `XCL_NETWORK=0`. This is a regression check only: it does not show whether the run reaches the code this PR changes. AI use: Claude Code (Claude Opus 5.5) built both trees and ran `scripts/smoke.ps1`; I checked the blocks before posting.
gut0to commented 2026-11-26 16:10:18 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-26T16:10:18Z.

Review: COMMENTED


AI-assisted review (OpenAI GPT-6). I checked the changed coherent-memory validation against the Win32 VirtualFree contract; I did not run tests.

<!-- greyfinch-sync:ivall/xcl:review:6077306286 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1357#pullrequestreview-6077306286). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-26T16:10:18Z. **Review:** COMMENTED --- AI-assisted review (OpenAI GPT-6). I checked the changed coherent-memory validation against the Win32 VirtualFree contract; I did not run tests.
gut0to commented 2026-11-26 16:10:18 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-26T16:10:18Z.

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

@@ -450,6 +444,22 @@ pub unsafe extern "system" fn VirtualFree(addr: *mut c_void, size: usize, t: u32
     ok
 }
 
+unsafe fn coherent_virtual_free(u: &crate::gpu::uma::Uma, addr: *mut c_void, size: usize, t: u32) -> BOOL {
+    let release = t & mem::MEM_RELEASE != 0;
+    let decommit = t & mem::MEM_DECOMMIT != 0;
+    if release == decommit || (release && size != 0) {
+        fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER);
+        return 0;
+    }
+    if release {
+        return u.free(addr as usize) as BOOL;
+    }
+    if size > 0 {

For the allocation base, Win32 defines MEM_DECOMMIT with size == 0 as decommitting the whole allocation. This path currently reports success while doing no write or state change, so callers can believe the memory was decommitted when it remains live. Could this case be implemented for the allocation or explicitly rejected with ERROR_INVALID_PARAMETER?

<!-- greyfinch-sync:ivall/xcl:review-comment:4710904947 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1357#discussion_r4710904947). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-26T16:10:18Z. **Code review location:** `crates/xcl/src/hle/kernelx.rs`; line 457 ~~~~diff @@ -450,6 +444,22 @@ pub unsafe extern "system" fn VirtualFree(addr: *mut c_void, size: usize, t: u32 ok } +unsafe fn coherent_virtual_free(u: &crate::gpu::uma::Uma, addr: *mut c_void, size: usize, t: u32) -> BOOL { + let release = t & mem::MEM_RELEASE != 0; + let decommit = t & mem::MEM_DECOMMIT != 0; + if release == decommit || (release && size != 0) { + fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER); + return 0; + } + if release { + return u.free(addr as usize) as BOOL; + } + if size > 0 { ~~~~ --- For the allocation base, Win32 defines `MEM_DECOMMIT` with `size == 0` as decommitting the whole allocation. This path currently reports success while doing no write or state change, so callers can believe the memory was decommitted when it remains live. Could this case be implemented for the allocation or explicitly rejected with `ERROR_INVALID_PARAMETER`?
github-actions[bot] commented 2026-12-01 02:35:02 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-12-01T02:35:02Z.


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:7298772232 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1357#issuecomment-7298772232). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-12-01T02:35:02Z. --- 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:30:55 +00:00 (Migrated from github.com)

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


Triage (#1607) – head 9aa114c

#1463 is stacked on this PR (its first commit is this one, 9aa114c) and adds whole-page decommit on top; that is a stack, not a duplicate. Main's coherent branch of VirtualFree still frees on any MEM_RELEASE without validating size or flags.
Main now does release-side bookkeeping (forget_title_mappings, mirrors::place_gone, source_going) in that branch; the rebase must keep it inside coherent_virtual_free.
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:7301851993 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1357#issuecomment-7301851993). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:30:55Z. --- **Triage (#1607)** – head 9aa114c #1463 is stacked on this PR (its first commit is this one, 9aa114c) and adds whole-page decommit on top; that is a stack, not a duplicate. Main's coherent branch of `VirtualFree` still frees on any MEM_RELEASE without validating size or flags. Main now does release-side bookkeeping (`forget_title_mappings`, `mirrors::place_gone`, `source_going`) in that branch; the rebase must keep it inside `coherent_virtual_free`. 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/1357:upstream-pr/1357'
git switch 'upstream-pr/1357'

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