XGameRuntime: avoid signed overflow in XUserCompare #1193

Merged
b3IKK merged 1 commit from upstream-pr/1202 into main 2026-12-02 17:39:02 +00:00
b3IKK commented 2026-11-24 13:11:10 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T13:11:10Z.


XUserCompare computed (a as isize - b as isize).signum() as i32, which overflows when the pointer difference exceeds isize::MAX—aborting on panic inside extern "system" fn in debug builds and wrapping to the inverted sign in release builds.

This replaces the signed subtraction with (a as usize).cmp(&(b as usize)) as i32.

What is in it

  • crates/xcl/src/hle/xgameruntime/mod.rs: compare handles with (a as usize).cmp(&(b as usize)) as i32 in XUserCompare; add unit test xuser_compare_does_not_overflow_pointer_difference (aborted with 0xc0000409 before the fix).

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 xuser_compare_does_not_overflow_pointer_difference: 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: 859e6ae0aaa7ceec963f8c0f7d50978558c52ce4
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1647 (PresentX 1647)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14602 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 xgameruntime/mod.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:5472790986 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1202). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T13:11:10Z. --- `XUserCompare` computed `(a as isize - b as isize).signum() as i32`, which overflows when the pointer difference exceeds `isize::MAX`—aborting on panic inside `extern "system" fn` in debug builds and wrapping to the inverted sign in release builds. This replaces the signed subtraction with `(a as usize).cmp(&(b as usize)) as i32`. **What is in it** - `crates/xcl/src/hle/xgameruntime/mod.rs`: compare handles with `(a as usize).cmp(&(b as usize)) as i32` in `XUserCompare`; add unit test `xuser_compare_does_not_overflow_pointer_difference` (aborted with `0xc0000409` before the fix). **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 xuser_compare_does_not_overflow_pointer_difference`: 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: 859e6ae0aaa7ceec963f8c0f7d50978558c52ce4 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1647 (PresentX 1647) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14602 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 `xgameruntime/mod.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:34 +00:00 (Migrated from github.com)

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


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

Machine: Windows 11, Ryzen 9 7900X; Rust stable MSVC, debug.

  • cargo test -p xcl --lib xuser: 1 passed, 0 failed — xuser_compare_does_not_overflow_pointer_difference covers the signed-overflow fix.

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

<!-- greyfinch-sync:ivall/xcl:comment:7132952590 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1202#issuecomment-7132952590). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T13:31:27Z. --- Independent verification on current `main` `384f7f7` (branch already contains it — clean). **Machine:** Windows 11, Ryzen 9 7900X; Rust stable MSVC, debug. - `cargo test -p xcl --lib xuser`: **1 passed, 0 failed** — `xuser_compare_does_not_overflow_pointer_difference` covers the signed-overflow fix. AI use: test runs by Devin (Cognition, SWE-2); no code changes.
github-actions[bot] commented 2026-11-27 21:26:35 +00:00 (Migrated from github.com)

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


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:7228447795 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1202#issuecomment-7228447795). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T21:26:35Z. --- 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.
BBareth commented 2026-11-30 23:20:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T23:20:30Z.


merge-check claim
head: 859e6ae0aa

<!-- greyfinch-sync:ivall/xcl:comment:7295002391 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1202#issuecomment-7295002391). Originally posted by [BBareth](https://github.com/BBareth) on 2026-11-30T23:20:30Z. --- merge-check claim head: 859e6ae0aaa7ceec963f8c0f7d50978558c52ce4
BBareth commented 2026-11-30 23:28:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T23:28:26Z.


merge-check
base: d0832e614f
head: 859e6ae0aa
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 passed, 0 failed, 51 ignored; 28 SKIPPED lines, as on the base; failing: none. The base alone, same run: 2890 passed, 0 failed, 51 ignored, 28 SKIPPED lines (22 no wave64, 2 no heap windows, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 cannot disable the driver cache). The new test xuser_compare_does_not_overflow_pointer_difference ran and passed.
review: no blocking finding; one file (+16/-1), read. XUserCompare compares the two handles as unsigned addresses (Ordering as i32 is -1 / 0 / 1) instead of taking the sign of a signed difference, which overflows when the two values are more than isize::MAX apart (a panic in a debug build). For any two user-mode addresses the result is the same as before; only values at or above 2^63 change order, and those are not handles XCL gives out.
performance: none.
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti 16 GB, driver 621.24 (32.0.16.2124); i7-12700K; Windows 11 26200. Claude Code, Claude Fable 5.2 read the diff, ran the build and the suite and wrote this check.

<!-- greyfinch-sync:ivall/xcl:comment:7295158434 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1202#issuecomment-7295158434). Originally posted by [BBareth](https://github.com/BBareth) on 2026-11-30T23:28:26Z. --- merge-check base: d0832e614fa7d6ae027bb4feb27ccb9121f54a8f head: 859e6ae0aaa7ceec963f8c0f7d50978558c52ce4 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 passed, 0 failed, 51 ignored; 28 SKIPPED lines, as on the base; failing: none. The base alone, same run: 2890 passed, 0 failed, 51 ignored, 28 SKIPPED lines (22 no wave64, 2 no heap windows, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 cannot disable the driver cache). The new test `xuser_compare_does_not_overflow_pointer_difference` ran and passed. review: no blocking finding; one file (+16/-1), read. `XUserCompare` compares the two handles as unsigned addresses (`Ordering as i32` is -1 / 0 / 1) instead of taking the sign of a signed difference, which overflows when the two values are more than `isize::MAX` apart (a panic in a debug build). For any two user-mode addresses the result is the same as before; only values at or above 2^63 change order, and those are not handles XCL gives out. performance: none. verdict: safe to merge hardware / tool and model: RTX 5070 Ti 16 GB, driver 621.24 (32.0.16.2124); i7-12700K; Windows 11 26200. Claude Code, Claude Fable 5.2 read the diff, ran the build and the suite and wrote this check.
Foniass commented 2026-12-01 05:05:32 +00:00 (Migrated from github.com)

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


Triage (#1607) – head 859e6ae

No open or merged PR changes XUserCompare; main still computes (a as isize - b as isize).signum(), which overflows (and panics in debug) for handles on either side of 0x8000_0000_0000_0000. The test fails without the fix in a debug build.
No problems found in the diff.
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:7301412422 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1202#issuecomment-7301412422). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:05:32Z. --- **Triage (#1607)** – head 859e6ae No open or merged PR changes XUserCompare; main still computes `(a as isize - b as isize).signum()`, which overflows (and panics in debug) for handles on either side of 0x8000_0000_0000_0000. The test fails without the fix in a debug build. No problems found in the diff. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
Decodere commented 2026-12-02 10:17:13 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Decodere on 2026-12-02T10:17:13Z.


merge-check
base: 1c8b551475
head: 859e6ae0aa
merge: clean (trial-merge commit e3c74429bc5ea3450ce3d93cf5f7bedcb01b3706, git diff --check clean)
build: cargo build --workspace ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 --nocapture (43 targets): 2910 passed, 0 failed, 51 ignored (+1 new test vs main baseline). Targeted new test hle::xgameruntime::tests::xuser_compare_does_not_overflow_pointer_difference passed with 0 skips. 28 SKIPPED lines (host environment only: 22 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 2 no heap windows, 1 cannot disable the driver cache (Windows Developer Mode required)).
review: Read the full merged diff (+16/-1 in crates/xcl/src/hle/xgameruntime/mod.rs). Replaces (a as isize - b as isize).signum() as i32 in XUserCompare with (a as usize).cmp(&(b as usize)) as i32, returning -1, 0, or 1 via std::cmp::Ordering without signed subtraction overflow (debug panic) or wraparound sign inversion (release) for handles spanning the high-bit boundary. Unit test covers equal, 0x7FFF_FFFF_FFFF_FFFF vs 0x8000_0000_0000_0000, and null vs usize::MAX. Exact, minimal, no scope creep or UB.
performance: not applicable (Tier 1 — single-line comparison fix in XUserCompare, off hot rendering/GPU paths).
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 4060 8 GB (driver 621.40), Intel Core i5-11400F, 32 GB RAM, Windows 11 Build 26200, Rust 1.100.1 MSVC, DXC 1.9.2602.17 / Antigravity (Gemini 4 Neon)

<!-- greyfinch-sync:ivall/xcl:comment:7342242868 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1202#issuecomment-7342242868). Originally posted by [Decodere](https://github.com/Decodere) on 2026-12-02T10:17:13Z. --- merge-check base: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f head: 859e6ae0aaa7ceec963f8c0f7d50978558c52ce4 merge: clean (trial-merge commit e3c74429bc5ea3450ce3d93cf5f7bedcb01b3706, git diff --check clean) build: cargo build --workspace ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 --nocapture (43 targets): 2910 passed, 0 failed, 51 ignored (+1 new test vs main baseline). Targeted new test hle::xgameruntime::tests::xuser_compare_does_not_overflow_pointer_difference passed with 0 skips. 28 SKIPPED lines (host environment only: 22 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 2 no heap windows, 1 cannot disable the driver cache (Windows Developer Mode required)). review: Read the full merged diff (+16/-1 in crates/xcl/src/hle/xgameruntime/mod.rs). Replaces `(a as isize - b as isize).signum() as i32` in `XUserCompare` with `(a as usize).cmp(&(b as usize)) as i32`, returning -1, 0, or 1 via `std::cmp::Ordering` without signed subtraction overflow (debug panic) or wraparound sign inversion (release) for handles spanning the high-bit boundary. Unit test covers equal, `0x7FFF_FFFF_FFFF_FFFF` vs `0x8000_0000_0000_0000`, and null vs `usize::MAX`. Exact, minimal, no scope creep or UB. performance: not applicable (Tier 1 — single-line comparison fix in `XUserCompare`, off hot rendering/GPU paths). verdict: safe to merge hardware / tool and model: NVIDIA GeForce RTX 4060 8 GB (driver 621.40), Intel Core i5-11400F, 32 GB RAM, Windows 11 Build 26200, Rust 1.100.1 MSVC, DXC 1.9.2602.17 / Antigravity (Gemini 4 Neon)
cj-tomlin commented 2026-12-02 16:54:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by cj-tomlin on 2026-12-02T16:54:00Z.


Test report, head 859e6ae0aaa7ceec963f8c0f7d50978558c52ce4 merged onto main 5eaadb54c14e0fc29597b2a1689d7dbd83491303 (clean, merge 94aef89d).

  • Hardware: NVIDIA GeForce RTX 3080 10 GB, driver 621.40, AMD Ryzen 7 7800X3D, Windows 11 build 26300, debug build.
  • cargo build --workspace: ok.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 2975 passed, 0 failed, 52 ignored; SKIPPED: 27 (22 no wave64, 2 no heap windows, 3 no D3D12 debug layer / d3d12SDKLayers.dll), the same 27 as plain main (2974 passed, 0 failed, 52 ignored) on this machine.
  • An earlier full-suite run of the same merge had 2 failures, both in tests this PR does not touch and that are intermittent on main here: two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs (#3441) and pso_async::used_tests::retained_failed_demand_observes_another_binds_successful_retry (#3180). The numbers above are from the rerun.
  • The new xuser_compare_does_not_overflow_pointer_difference passes, and fails with attempt to subtract with overflow when I put main's (a as isize - b as isize).signum() back into the merged tree.

Diff read (+16/-1, xgameruntime/mod.rs), CPU only.

Tool and model: Claude Code (Claude Opus 5.5) merged, built, ran the tests and drafted this; I checked it.

<!-- greyfinch-sync:ivall/xcl:comment:7352616919 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1202#issuecomment-7352616919). Originally posted by [cj-tomlin](https://github.com/cj-tomlin) on 2026-12-02T16:54:00Z. --- Test report, head `859e6ae0aaa7ceec963f8c0f7d50978558c52ce4` merged onto `main` `5eaadb54c14e0fc29597b2a1689d7dbd83491303` (clean, merge `94aef89d`). - Hardware: NVIDIA GeForce RTX 3080 10 GB, driver 621.40, AMD Ryzen 7 7800X3D, Windows 11 build 26300, debug build. - `cargo build --workspace`: ok. - `cargo test --workspace --no-fail-fast -- --test-threads=4`: 2975 passed, 0 failed, 52 ignored; SKIPPED: 27 (22 no wave64, 2 no heap windows, 3 no D3D12 debug layer / d3d12SDKLayers.dll), the same 27 as plain `main` (2974 passed, 0 failed, 52 ignored) on this machine. - An earlier full-suite run of the same merge had 2 failures, both in tests this PR does not touch and that are intermittent on `main` here: `two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs` (#3441) and `pso_async::used_tests::retained_failed_demand_observes_another_binds_successful_retry` (#3180). The numbers above are from the rerun. - The new `xuser_compare_does_not_overflow_pointer_difference` passes, and fails with `attempt to subtract with overflow` when I put `main`'s `(a as isize - b as isize).signum()` back into the merged tree. Diff read (+16/-1, `xgameruntime/mod.rs`), CPU only. Tool and model: Claude Code (Claude Opus 5.5) merged, built, ran the tests and drafted this; I checked it.
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!1193
No description provided.