PE loader: saturate TlsDir::total_len and bound rva_to_offset in Layout::File #1397

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

@⁠b3IKK on GitHub · 2026-11-24 15:34 UTC · original
b3IKK:pe/tls-len-and-rva-file-bounds → main · +43 −3 · 1 files · 1 commits · open

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

  • TlsDir::total_len added self.template_len() + self.size_of_zero_fill as u64 without saturating, panicking in debug or wrapping in release when end_raw - start_raw is near u64::MAX.
  • Pe::rva_to_offset in Layout::File returned Some(rva as usize) (for headers) or Some(s.raw_ptr as usize + delta as usize) (for sections) without checking that the offset lies within self.data.len(), unlike Layout::Mapped.

This uses saturating_add in TlsDir::total_len and checks off < self.data.len() (with checked addition) in Layout::File.

What is in it

  • crates/xcl/src/pe.rs: saturate TlsDir::total_len; bounds-check Layout::File offsets in Pe::rva_to_offset; add unit tests tls_dir_total_len_saturates_instead_of_overflowing and rva_to_offset_in_file_layout_rejects_offsets_past_eof (including exact raw_ptr + delta == data.len() boundary check; both verified failing 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): 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 pe::tests: 12 passed, 0 failed (both new tests failed 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: 1e920a01147c135577d1e6dae3b185f5d72de5c6
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1644 (PresentX 1644)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14468 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 pe.rs, wrote the fixes and unit tests, added the exact raw_ptr + delta == data.len() boundary assertion per review feedback, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
**@⁠b3IKK** on GitHub · 2026-11-24 15:34 UTC · [original](https://github.com/xcl-dev/xcl/pull/1397) `b3IKK:pe/tls-len-and-rva-file-bounds` → `main` · +43 −3 · 1 files · 1 commits · open Fixes two bounds/overflow bugs in `crates/xcl/src/pe.rs`: - `TlsDir::total_len` added `self.template_len() + self.size_of_zero_fill as u64` without saturating, panicking in debug or wrapping in release when `end_raw - start_raw` is near `u64::MAX`. - `Pe::rva_to_offset` in `Layout::File` returned `Some(rva as usize)` (for headers) or `Some(s.raw_ptr as usize + delta as usize)` (for sections) without checking that the offset lies within `self.data.len()`, unlike `Layout::Mapped`. This uses `saturating_add` in `TlsDir::total_len` and checks `off < self.data.len()` (with checked addition) in `Layout::File`. **What is in it** - `crates/xcl/src/pe.rs`: saturate `TlsDir::total_len`; bounds-check `Layout::File` offsets in `Pe::rva_to_offset`; add unit tests `tls_dir_total_len_saturates_instead_of_overflowing` and `rva_to_offset_in_file_layout_rejects_offsets_past_eof` (including exact `raw_ptr + delta == data.len()` boundary check; both verified failing 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`): 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 pe::tests`: 12 passed, 0 failed (both new tests failed 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: 1e920a01147c135577d1e6dae3b185f5d72de5c6 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1644 (PresentX 1644) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14468 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 `pe.rs`, wrote the fixes and unit tests, added the exact `raw_ptr + delta == data.len()` boundary assertion per review feedback, ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
Two bounds/overflow bugs in pe.rs:
- TlsDir::total_len added `template_len() + size_of_zero_fill as u64`
  without saturating, panicking in debug or wrapping in release when
  `end_raw - start_raw` is near u64::MAX.
- Pe::rva_to_offset in Layout::File returned `Some(rva)` (for headers)
  or `Some(raw_ptr + delta)` (for sections) without checking that the
  offset lies within `self.data.len()`, unlike Layout::Mapped.

Use saturating_add in TlsDir::total_len and validate `off < self.data.len()`
(with checked addition) in Layout::File, with unit tests for both.

Assisted-by: OpenCode (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:52:48 +00:00
Author
Owner

@⁠pickles976 on GitHub · 2026-11-24 15:37 UTC · original

Review: correct and well-scoped — saturating TLS total_len kills the wrap-to-small-length case (allocation sized from the overflowed value), and bounding rva_to_offset at raw_ptr + delta >= data.len() is the consumer-side half of the parse-time invariant I proposed in #836; the truncated-image test is exactly the right regression harness. One edge row worth adding while the test is fresh: raw_ptr + delta landing exactly on data.len() — the message already specifies >= so the implementation reads right, but pinning the boundary value makes a future off-by-one regression fail loudly. Combined with #1367 (parser-side directory validation), this pair implements the full invariant: five independent finds across three loaders today were all instances of these two gaps.

**@⁠pickles976** on GitHub · 2026-11-24 15:37 UTC · [original](https://github.com/xcl-dev/xcl/pull/1397#issuecomment-7136329225) Review: correct and well-scoped — `saturating` TLS `total_len` kills the wrap-to-small-length case (allocation sized from the overflowed value), and bounding `rva_to_offset` at `raw_ptr + delta >= data.len()` is the consumer-side half of the parse-time invariant I proposed in #836; the truncated-image test is exactly the right regression harness. One edge row worth adding while the test is fresh: `raw_ptr + delta` landing **exactly on** `data.len()` — the message already specifies `>=` so the implementation reads right, but pinning the boundary value makes a future off-by-one regression fail loudly. Combined with #1367 (parser-side directory validation), this pair implements the full invariant: five independent finds across three loaders today were all instances of these two gaps.
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 15:54 UTC · original · edited 2026-11-24 16:02 UTC

Full-suite run, since this PR has no test report yet. Tested exact head 6cb357b (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: tls_dir_total_len_saturates_instead_of_overflowing, rva_to_offset_in_file_layout_rejects_offsets_past_eof.
  • 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.
**@⁠futurepro62** on GitHub · 2026-11-24 15:54 UTC · [original](https://github.com/xcl-dev/xcl/pull/1397#issuecomment-7136806592) · edited 2026-11-24 16:02 UTC Full-suite run, since this PR has no test report yet. Tested exact head `6cb357b` (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: `tls_dir_total_len_saturates_instead_of_overflowing`, `rva_to_offset_in_file_layout_rejects_offsets_past_eof`. - 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.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 17:01 UTC · original

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

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

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 17:01 UTC · [original](https://github.com/xcl-dev/xcl/pull/1397#issuecomment-7138573392) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. Thanks!
Author
Owner

@⁠b3IKK on GitHub · 2026-11-24 18:50 UTC · original

Thanks @⁠pickles976 — pinned the exact boundary in \1e920a0\ (
aw_ptr + delta == data.len() - 1\ returns \Some,
aw_ptr + delta == data.len()\ returns \None) and re-ran \scripts/test.ps1\ and the 60 s \scripts/smoke.ps1\ run.

**@⁠b3IKK** on GitHub · 2026-11-24 18:50 UTC · [original](https://github.com/xcl-dev/xcl/pull/1397#issuecomment-7141237068) Thanks @⁠pickles976 — pinned the exact boundary in \1e920a0\ (\ aw_ptr + delta == data.len() - 1\ returns \Some\, \ aw_ptr + delta == data.len()\ returns \None\) and re-ran \scripts/test.ps1\ and the 60 s \scripts/smoke.ps1\ run.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 05:03 UTC · original

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-26 05:03 UTC · [original](https://github.com/xcl-dev/xcl/pull/1397#issuecomment-7183140495) Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
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/1397/head:github-bot/gh-1397'
git switch 'github-bot/gh-1397'

Merge

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