Skip to content

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

Open
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:loader/reloc-delta-and-protect-bounds
Open

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:loader/reloc-delta-and-protect-bounds

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

… map_image_ex

Two overflow/bounds bugs in loader.rs:
- Computing `delta = base as i64 - pe.image_base as i64` and applying
  `(v as i64 + delta)` overflowed signed i64 when `pe.image_base` was
  near `0x8000_0000_0000_0000` (above i64::MAX). Using unsigned
  wrapping_sub / wrapping_add handles every 64-bit base delta 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`.

Clamp header and section VirtualProtect extents to `[0, size)` via
section_protect_range() and use checked additions in section copy, with
unit tests for both.

Assisted-by: OpenCode (Gemini 4 Neon)
@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…ge relocations fail (port upstream xcl-dev#1416 + xcl-dev#291, loader pair)

Windows loader.rs: verbatim xcl-dev#1416 (section_protect_range, wrapping_sub/
wrapping_add delta incl. TLS adj, checked_add section copies, header clamp)
+ xcl-dev#291 (rva+width>size guard). 3 upstream tests land verbatim.

Linux loader_linux.rs twin: wrapping u64 delta in apply_relocations
(unknown-kind guard now runs before warn-continue, matching upstream),
checked_add section copies. 2 Linux-shaped twin tests (high-base wrapping,
outside-image errors); protect_image already clamped via saturating_add.

Failing-first: grafted high-base test on pre-fix tree FAILED (attempt to
subtract with overflow); green post-fix.
@github-actions

Copy link
Copy Markdown

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

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants