PE loader: use wrapping u64 delta and clamp VirtualProtect extents in map_image_ex #1406

Open
b3IKK wants to merge 1 commit from upstream-pr/1416 into main
b3IKK commented 2026-11-24 15:44:27 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T15:44:27Z.


Fixes two overflow and bounds bugs in crates/xcl/src/loader.rs:

  • map_image_ex computed let delta = base as i64 - pe.image_base as i64 and applied (v as i64 + delta), which panicked with signed overflow when pe.image_base was near 0x8000_0000_0000_0000. Using unsigned wrapping_sub / wrapping_add handles every 64-bit relocation and TLS base adjustment cleanly.
  • Step 6 passed unclamped pe.size_of_headers and ((s.mapped_size() as usize) + 0xFFF) & !0xFFF to VirtualProtect, allowing oversized headers or section extents to alter page protections past [base, base + size). Step 2 also used unchecked src + raw / s.va + raw.

This uses wrapping u64 arithmetic for delta, clamps header and section VirtualProtect ranges to [0, size) via section_protect_range(), and uses checked addition in section copy.

What is in it

  • crates/xcl/src/loader.rs: switch delta to wrapping u64 arithmetic; add section_protect_range() and clamp VirtualProtect extents to size; use checked_add for section copy bounds; add unit tests relocates_high_image_base_without_signed_overflow (verified panicking before the fix) and section_protect_range_clamps_to_image_size.

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): 753 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 loader::tests: 4 passed, 0 failed (relocates_high_image_base_without_signed_overflow panicked with signed subtraction overflow 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: eb5c7198c6657689abc46d2b7aadfc164bad9f35
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1643 (PresentX 1643)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14609 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 loader.rs, wrote the fixes and unit tests, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
<!-- greyfinch-sync:ivall/xcl:issue:5475442923 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1416). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T15:44:27Z. --- Fixes two overflow and bounds bugs in `crates/xcl/src/loader.rs`: - `map_image_ex` computed `let delta = base as i64 - pe.image_base as i64` and applied `(v as i64 + delta)`, which panicked with signed overflow when `pe.image_base` was near `0x8000_0000_0000_0000`. Using unsigned `wrapping_sub` / `wrapping_add` handles every 64-bit relocation and TLS base adjustment cleanly. - Step 6 passed unclamped `pe.size_of_headers` and `((s.mapped_size() as usize) + 0xFFF) & !0xFFF` to `VirtualProtect`, allowing oversized headers or section extents to alter page protections past `[base, base + size)`. Step 2 also used unchecked `src + raw` / `s.va + raw`. This uses wrapping `u64` arithmetic for `delta`, clamps header and section `VirtualProtect` ranges to `[0, size)` via `section_protect_range()`, and uses checked addition in section copy. **What is in it** - `crates/xcl/src/loader.rs`: switch `delta` to wrapping `u64` arithmetic; add `section_protect_range()` and clamp `VirtualProtect` extents to `size`; use `checked_add` for section copy bounds; add unit tests `relocates_high_image_base_without_signed_overflow` (verified panicking before the fix) and `section_protect_range_clamps_to_image_size`. **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`): 753 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 loader::tests`: 4 passed, 0 failed (`relocates_high_image_base_without_signed_overflow` panicked with signed subtraction overflow 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: eb5c7198c6657689abc46d2b7aadfc164bad9f35 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1643 (PresentX 1643) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14609 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 `loader.rs`, wrote the fixes and unit tests, ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
futurepro62 commented 2026-11-24 16:07:27 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T15:56:14Z.


Full-suite run, since this PR has no test report yet. Tested exact head eb5c719 (on its base 930e8b4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 753 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: relocates_high_image_base_without_signed_overflow, section_protect_range_clamps_to_image_size.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 753 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:ivall/xcl:comment:7136860289 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1416#issuecomment-7136860289). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-24T15:56:14Z. --- Full-suite run, since this PR has no test report yet. Tested exact head `eb5c719` (on its base `930e8b4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **753 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `relocates_high_image_base_without_signed_overflow`, `section_protect_range_clamps_to_image_size`. - Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **753 passed, 0 failed, 41 ignored**, no skips. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
DavidM1911 commented 2026-11-24 18:08:55 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T17:13:09Z.


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

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored. (One parallel run showed a single flaky failure; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7138864831 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1416#issuecomment-7138864831). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T17:13:09Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. (One parallel run showed a single flaky failure; clean on single-threaded rerun — same flake seen on unmodified `main`.) Thanks!
xpatfussy69x commented 2026-11-25 12:06:32 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by xpatfussy69x on 2026-11-25T11:58:55Z.


Test report for head eb5c719 as pushed (not merged onto current main) – NVIDIA, wave32.

Tested

  • NVIDIA GeForce RTX 3060 12 GB (Ampere, 32-lane waves only), driver 32.0.16.2124; AMD Ryzen 5 7500F; 32 GB RAM; Windows NT 10.0.26300; Rust 1.100.0 (stable-x86_64-pc-windows-msvc); debug; DXC 1.9.2609.5
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 753 passed, 0 failed, 41 ignored
  • SKIPPED GPU tests on this adapter: 2 (no wave64)
  • this PR's tests relocates_high_image_base_without_signed_overflow and section_protect_range_clamps_to_image_size ran and passed
  • no XCL_SAMPLE_* data set (game-data tests not run)

Not tested

  • loading a title's executable; AMD / native wave64; release build; the head merged onto current main

AI use

  • Claude (Claude Opus 5.5) wrote the script that ran the suite on my PC, read the log and wrote and posted this report with my approval; the run is mine.
<!-- greyfinch-sync:ivall/xcl:comment:7161761331 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1416#issuecomment-7161761331). Originally posted by [xpatfussy69x](https://github.com/xpatfussy69x) on 2026-11-25T11:58:55Z. --- Test report for head `eb5c719` as pushed (not merged onto current `main`) – NVIDIA, wave32. **Tested** - NVIDIA GeForce RTX 3060 12 GB (Ampere, 32-lane waves only), driver 32.0.16.2124; AMD Ryzen 5 7500F; 32 GB RAM; Windows NT 10.0.26300; Rust 1.100.0 (stable-x86_64-pc-windows-msvc); debug; DXC 1.9.2609.5 - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): 753 passed, 0 failed, 41 ignored - SKIPPED GPU tests on this adapter: 2 (no wave64) - this PR's tests `relocates_high_image_base_without_signed_overflow` and `section_protect_range_clamps_to_image_size` ran and passed - no `XCL_SAMPLE_*` data set (game-data tests not run) **Not tested** - loading a title's executable; AMD / native wave64; release build; the head merged onto current `main` **AI use** - Claude (Claude Opus 5.5) wrote the script that ran the suite on my PC, read the log and wrote and posted this report with my approval; the run is mine.
github-actions[bot] commented 2026-11-29 07:42:13 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-29T07:42:13Z.


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:7252637328 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1416#issuecomment-7252637328). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-29T07:42:13Z. --- 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:40:30 +00:00 (Migrated from github.com)

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


Triage (#1607) – head eb5c719

Not on main: main's map_image_ex still computes delta as base as i64 - pe.image_base as i64 and protects s.va + round_up(mapped_size) without clamping. #1367 also clamps the section VirtualProtect length to the image (one hunk overlaps), but it does not change the signed delta or the overflow-checked section copy, so the two are not the same change.
No problems found in the diff; the DIR64 test with an image base of 0x8000_0000_0000_0000 exercises the overflow the old i64 arithmetic hit.
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:7302017710 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1416#issuecomment-7302017710). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:40:30Z. --- **Triage (#1607)** – head eb5c719 Not on main: main's `map_image_ex` still computes `delta` as `base as i64 - pe.image_base as i64` and protects `s.va + round_up(mapped_size)` without clamping. #1367 also clamps the section `VirtualProtect` length to the image (one hunk overlaps), but it does not change the signed delta or the overflow-checked section copy, so the two are not the same change. No problems found in the diff; the DIR64 test with an image base of 0x8000_0000_0000_0000 exercises the overflow the old `i64` arithmetic hit. 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/1416:upstream-pr/1416'
git switch 'upstream-pr/1416'

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