PE loader: relocation targets outside the image fail the load #291

Open
github-bot wants to merge 1 commit from github-bot/gh-291 into main AGit
Owner

@⁠honestlyBroke on GitHub · 2026-11-23 22:28 UTC · original
honestlyBroke:pe-loader-reloc-bounds → main · +39 −0 · 1 files · 1 commits · open

Hardening, not a playability fix: no title is known to hit this path.

map_image patched base + rva for every relocation without checking the target against the mapping. The entries come from the file, so a target past SizeOfImage, or a DIR64 whose 8 bytes straddle its end, wrote outside the image. #178 made the RVA arithmetic overflow-safe but did not bound the targets. The check is in the loader, not in Pe::relocations, because that also reads file-layout images and has no mapping size.

Part of #106.

What is in it

  • crates/xcl/src/loader.rs: before patching, the target plus its width (8 for DIR64, 4 for HIGHLOW) must fit in SizeOfImage. Otherwise the load fails with "relocation at … outside the image" and the mapping is freed.
  • crates/xcl/src/loader.rs (tests): relocations_outside_the_image_are_errors uses tiny_dll with its preferred address taken. Covers a target past the end, a DIR64 straddling the end, and a control target inside the image that is still relocated by the right delta.

Tested

  • RTX 4060 (driver 617.42), Intel Core i5-14400F, 32 GB RAM, Windows 11 24H2 (26100), Rust 1.100.0, debug
  • cargo test --workspace -- --test-threads=4: 652 passed, 0 failed, 40 ignored. SKIPPED GPU tests: gpu_wave32_program_in_wave64_hardware, gpu_geometry_wave32_prefix_count_over_a_mask (no wave64)
  • Without the check, the new test crashes the test process with STATUS_ACCESS_VIOLATION (0xc0000005). With the check it passes.

Regression run
Not run yet. I'll add both xcl-smoke blocks (main and this PR's head) and mark the PR ready once I have a title folder to run against. Until then it stays a draft.

Not tested

  • The scripts/smoke.ps1 regression run on a real title (see above)
  • AMD GPUs, and wave64 GPU tests (NVIDIA here)
  • No real title loads an image that hits the new error path. The regression run will only check that normal loads are unchanged.

AI use

  • Claude Code (Claude Opus 5.5) wrote the fix, the test and this description. I reviewed the diff and ran the tests above.
**@⁠honestlyBroke** on GitHub · 2026-11-23 22:28 UTC · [original](https://github.com/xcl-dev/xcl/pull/291) `honestlyBroke:pe-loader-reloc-bounds` → `main` · +39 −0 · 1 files · 1 commits · open Hardening, not a playability fix: no title is known to hit this path. `map_image` patched `base + rva` for every relocation without checking the target against the mapping. The entries come from the file, so a target past `SizeOfImage`, or a DIR64 whose 8 bytes straddle its end, wrote outside the image. #178 made the RVA arithmetic overflow-safe but did not bound the targets. The check is in the loader, not in `Pe::relocations`, because that also reads file-layout images and has no mapping size. Part of #106. **What is in it** - `crates/xcl/src/loader.rs`: before patching, the target plus its width (8 for DIR64, 4 for HIGHLOW) must fit in `SizeOfImage`. Otherwise the load fails with "relocation at … outside the image" and the mapping is freed. - `crates/xcl/src/loader.rs` (tests): `relocations_outside_the_image_are_errors` uses `tiny_dll` with its preferred address taken. Covers a target past the end, a DIR64 straddling the end, and a control target inside the image that is still relocated by the right delta. **Tested** - RTX 4060 (driver 617.42), Intel Core i5-14400F, 32 GB RAM, Windows 11 24H2 (26100), Rust 1.100.0, debug - `cargo test --workspace -- --test-threads=4`: 652 passed, 0 failed, 40 ignored. SKIPPED GPU tests: `gpu_wave32_program_in_wave64_hardware`, `gpu_geometry_wave32_prefix_count_over_a_mask` (no wave64) - Without the check, the new test crashes the test process with STATUS_ACCESS_VIOLATION (0xc0000005). With the check it passes. **Regression run** Not run yet. I'll add both `xcl-smoke` blocks (main and this PR's head) and mark the PR ready once I have a title folder to run against. Until then it stays a draft. **Not tested** - The `scripts/smoke.ps1` regression run on a real title (see above) - AMD GPUs, and wave64 GPU tests (NVIDIA here) - No real title loads an image that hits the new error path. The regression run will only check that normal loads are unchanged. **AI use** - Claude Code (Claude Opus 5.5) wrote the fix, the test and this description. I reviewed the diff and ran the tests above.
The relocation entries come from the file, and map_image patched base + rva
without checking it against the mapping: a target past SizeOfImage, or a DIR64
whose 8 bytes straddle its end, wrote outside the image (an access violation
in the new test without the check). The check is in the loader rather than in
Pe::relocations, which also reads file-layout images and has no mapping size.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:18:58 +00:00
Author
Owner

@⁠mastrr9 on GitHub · 2026-11-23 22:52 UTC · original

Independent static code audit of 227e1a01e (code inspection only — cargo test not run on macOS; no game files):

Summary: Looks solid

  • r.rva as usize + width > size catches both past-end targets and straddling DIR64 (8 B) / HIGHLOW (4 B) writes before dereferencing, and cleans up the mapping via fail(...).

Minor non-blocking notes

  • Using (r.rva as usize).checked_add(width).is_none_or(|end| end > size) makes overflow-safety explicit (already safe on 64-bit since r.rva is u32).
  • Pre-existing right below (loader.rs:121, 125): (v as i64 + delta) can panic on signed overflow in debug builds if hardening against crafted relocations (wrapping_add avoids that).

(AI-assisted)

**@⁠mastrr9** on GitHub · 2026-11-23 22:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/291#issuecomment-7116288197) Independent static code audit of `227e1a01e` (code inspection only — `cargo test` not run on macOS; no game files): ### Summary: Looks solid - `r.rva as usize + width > size` catches both past-end targets and straddling `DIR64` (8 B) / `HIGHLOW` (4 B) writes before dereferencing, and cleans up the mapping via `fail(...)`. ### Minor non-blocking notes - Using `(r.rva as usize).checked_add(width).is_none_or(|end| end > size)` makes overflow-safety explicit (already safe on 64-bit since `r.rva` is `u32`). - Pre-existing right below (`loader.rs:121, 125`): `(v as i64 + delta)` can panic on signed overflow in debug builds if hardening against crafted relocations (`wrapping_add` avoids that). *(AI-assisted)*
Author
Owner

@⁠MesshM on GitHub · 2026-11-24 04:27 UTC · original

Test run of c1776d808d0490f4cda7b3e0e3c90c2c24435cdf on AMD RDNA4. I merged it onto current main 64b3f35 locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 702 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 661 passed, 0 failed, 21 ignored
  • 0 SKIPPED; relocations_outside_the_image_are_errors passes.

Review: A relocation whose target plus width (8 for DIR64, 4 for HIGHLOW) goes past SizeOfImage now fails the load instead of writing outside the mapping; ABSOLUTE entries have width 0 and pass as before. I found nothing to change.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the PR's head on its old base was not run, only its merge onto main.

Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

**@⁠MesshM** on GitHub · 2026-11-24 04:27 UTC · [original](https://github.com/xcl-dev/xcl/pull/291#issuecomment-7122044352) Test run of `c1776d808d0490f4cda7b3e0e3c90c2c24435cdf` on AMD RDNA4. I merged it onto current `main` `64b3f35` locally (clean merge) and tested that tree. **Tested** - AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 `stable-x86_64-pc-windows-msvc`, debug - `cargo build --workspace` + `cargo test --workspace -- --test-threads=4` on the merge: **702 passed, 0 failed, 41 ignored**, exit code 0; `xcl` lib tests 661 passed, 0 failed, 21 ignored - **0 `SKIPPED`**; `relocations_outside_the_image_are_errors` passes. **Review:** A relocation whose target plus width (8 for DIR64, 4 for HIGHLOW) goes past `SizeOfImage` now fails the load instead of writing outside the mapping; `ABSOLUTE` entries have width 0 and pass as before. I found nothing to change. **Not tested:** no retail title or regression run (I have no unpacked game), no `XCL_SAMPLE_*` tests, release build; the PR's head on its old base was not run, only its merge onto `main`. Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.
Author
Owner

@⁠einfachama on GitHub · 2026-11-24 11:46 UTC · original

Regression run on a retail title for #291, since Not tested lists it: head c1776d808d0490f4cda7b3e0e3c90c2c24435cdf merged onto main d208e14 (clean merge, local merge commit b0de7d3). Normal loads are unchanged: my retail title loads its 8 images (6 of them relocated) exactly as on main and runs at its first menu as before.

Tested

  • NVIDIA GeForce RTX 4060 8 GB (Ada, 32-lane waves), driver 621.24; Ryzen 7 5800X; 32 GB RAM; Windows 11 Pro 26300; Rust 1.100.0 MSVC; DXC 1.9.2602.17; debug for the tests, release for the title runs.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 705 passed, 0 failed, 41 ignored (main d208e14 in its clean run: 704 / 0 / 41). SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • My retail title, scripts/smoke.ps1 -Seconds 60, unattended, the learned window in both runs. XCL's log shows the same image loads on both (8 loaded, 6 relocated, no relocation error):
xcl-smoke v1
commit: d208e14b765f469f4b56301b78afa8dac5378339
gpu: NVIDIA GeForce RTX 4060
title: title
seconds: 60
exit: 0x0
frames shown: 1616 (PresentX 1616)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14635 MB
xcl-smoke v1
commit: b0de7d37625c46a02e809d2b46ce83fd2db801a4
gpu: NVIDIA GeForce RTX 4060
title: title
seconds: 60
exit: 0x0
frames shown: 1605 (PresentX 1605)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14578 MB

The second block is the local merge onto main, not the PR's head commit, so it can't stand in for the PR's own Regression run (pr_check.py wants the head commit there). It shows that real loads are unchanged here.

Not tested

  • The new error path with a real image (none of my title's images hits it, as the PR says).
  • The PR at its own base; anything past the title's first menu.

AI use

  • Claude Code with Claude Opus 5.5 ran the builds, tests and title runs in an unattended session and wrote this report from the logs; I checked it before posting.
**@⁠einfachama** on GitHub · 2026-11-24 11:46 UTC · [original](https://github.com/xcl-dev/xcl/pull/291#issuecomment-7130522155) Regression run on a retail title for #291, since **Not tested** lists it: head `c1776d808d0490f4cda7b3e0e3c90c2c24435cdf` merged onto `main` `d208e14` (clean merge, local merge commit `b0de7d3`). **Normal loads are unchanged**: my retail title loads its 8 images (6 of them relocated) exactly as on `main` and runs at its first menu as before. **Tested** - NVIDIA GeForce RTX 4060 8 GB (Ada, 32-lane waves), driver 621.24; Ryzen 7 5800X; 32 GB RAM; Windows 11 Pro 26300; Rust 1.100.0 MSVC; DXC 1.9.2602.17; debug for the tests, release for the title runs. - `cargo test --workspace --no-fail-fast -- --test-threads=4`: **705 passed, 0 failed, 41 ignored** (`main` `d208e14` in its clean run: 704 / 0 / 41). SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - My retail title, `scripts/smoke.ps1 -Seconds 60`, unattended, the learned window in both runs. XCL's log shows the same image loads on both (8 loaded, 6 relocated, no relocation error): ``` xcl-smoke v1 commit: d208e14b765f469f4b56301b78afa8dac5378339 gpu: NVIDIA GeForce RTX 4060 title: title seconds: 60 exit: 0x0 frames shown: 1616 (PresentX 1616) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14635 MB ``` ``` xcl-smoke v1 commit: b0de7d37625c46a02e809d2b46ce83fd2db801a4 gpu: NVIDIA GeForce RTX 4060 title: title seconds: 60 exit: 0x0 frames shown: 1605 (PresentX 1605) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14578 MB ``` The second block is the local merge onto `main`, not the PR's head commit, so it can't stand in for the PR's own **Regression run** (`pr_check.py` wants the head commit there). It shows that real loads are unchanged here. **Not tested** - The new error path with a real image (none of my title's images hits it, as the PR says). - The PR at its own base; anything past the title's first menu. **AI use** - Claude Code with Claude Opus 5.5 ran the builds, tests and title runs in an unattended session and wrote this report from the logs; I checked it before posting.
Author
Owner

@⁠Toons29 on GitHub · 2026-11-25 02:53 UTC · original

Regression run of c1776d80 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: c1776d808d0490f4cda7b3e0e3c90c2c24435cdf
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1458 (PresentX 1458)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 14901 MB

Against the limits in CONTRIBUTING.md: 90 % of main's frames, 100 % 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.

**@⁠Toons29** on GitHub · 2026-11-25 02:53 UTC · [original](https://github.com/xcl-dev/xcl/pull/291#issuecomment-7151316614) Regression run of `c1776d80` 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: c1776d808d0490f4cda7b3e0e3c90c2c24435cdf gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1458 (PresentX 1458) presents per 2 s (last 3): 60 60 61 device removals: 0 crashes: 0 peak commit: 14901 MB ``` Against the limits in CONTRIBUTING.md: 90 % of `main`'s frames, 100 % 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.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 14:43 UTC · original

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

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-25 14:43 UTC · [original](https://github.com/xcl-dev/xcl/pull/291#issuecomment-7165721310) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/291/head:github-bot/gh-291'
git switch 'github-bot/gh-291'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-291'
git switch 'github-bot/gh-291'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-291'
git switch 'github-bot/gh-291'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-291'
git switch 'main'
git merge --squash 'github-bot/gh-291'
git switch 'main'
git merge --ff-only 'github-bot/gh-291'
git switch 'main'
git merge 'github-bot/gh-291'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.