PE: map header RVAs and read TLS data from the headers #1639

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

@⁠sandivd on GitHub · 2026-11-24 20:17 UTC · original
sandivd:pe-header-rva → main · +105 −7 · 2 files · 1 commits · open

AI-assisted.

Problem

ImageInfo mapped only section RVAs. The PE headers occupy RVA 0..SizeOfHeaders and are stored one-to-one at the start of the file, so a data directory that points there is valid, but:

  • ImageInfo::rva_to_file_offset returned None for every RVA below SizeOfHeaders, so prepare could not patch a header-backed TLS directory (TLS directory is not file-backed).
  • parse_tls read both the TLS directory and its callback array through SectionTable::pe_data_at, which searches sections only, and failed with TLS directory outside of sections.

Fix

  • Store SizeOfHeaders in ImageInfo and reject an image whose declared header region is larger than the file (the OS could not map that either).
  • Map rva < SizeOfHeaders to the same file offset in rva_to_file_offset.
  • Read the TLS directory and callback array from the headers first, falling back to the sections (file_data_at).
  • Update the ImageInfo literal in the census test helper for the new field.

This does not change callback termination or count rules; #324 and #1456 touch those lines, this change does not.

Verification

Rebased onto ccfb029. Ran on Windows 11 x64, Rust 1.98.1, x86_64-pc-windows-msvc:

  • cargo test -p victory-pe --locked: 39 passed (3 new: header mapping boundaries, TLS directory and callbacks in the headers, SizeOfHeaders bounded by the file).
  • cargo clippy -p victory-pe --all-targets --all-features --locked -- -D warnings: clean.
  • cargo clippy --workspace --all-targets --locked -- -D warnings and the --all-features variant: clean.
  • cargo test -p victory-loader --locked: 38 passed.
  • cargo test --workspace --locked --exclude victory-shader --exclude victory-gpu: 313 passed, 5 ignored.
  • cargo xtask policy: 182 files ok.
  • cargo fmt -p victory-pe -- --check and cargo fmt -p victory -- --check: clean.

Two packages have failures on this machine that reproduce on unmodified ccfb029:

  • victory-gpu: 139 passed, 2 failed (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views).
  • victory-shader: three emit::tests abort with STATUS_ACCESS_VIOLATION.

victory-gpu and victory-shader do not depend on victory-pe.

Scope

This is spec correctness, not a title failure reproduced here: a scan of 14,106 PE32+ images on this machine found no TLS, import, export or delay-import directory inside the headers (two bound_import directories were). No game files were used; the tests build their TLS structures by hand.

**@⁠sandivd** on GitHub · 2026-11-24 20:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/1639) `sandivd:pe-header-rva` → `main` · +105 −7 · 2 files · 1 commits · open AI-assisted. ### Problem `ImageInfo` mapped only section RVAs. The PE headers occupy RVA `0..SizeOfHeaders` and are stored one-to-one at the start of the file, so a data directory that points there is valid, but: - `ImageInfo::rva_to_file_offset` returned `None` for every RVA below `SizeOfHeaders`, so `prepare` could not patch a header-backed TLS directory (`TLS directory is not file-backed`). - `parse_tls` read both the TLS directory and its callback array through `SectionTable::pe_data_at`, which searches sections only, and failed with `TLS directory outside of sections`. ### Fix - Store `SizeOfHeaders` in `ImageInfo` and reject an image whose declared header region is larger than the file (the OS could not map that either). - Map `rva < SizeOfHeaders` to the same file offset in `rva_to_file_offset`. - Read the TLS directory and callback array from the headers first, falling back to the sections (`file_data_at`). - Update the `ImageInfo` literal in the `census` test helper for the new field. This does not change callback termination or count rules; #324 and #1456 touch those lines, this change does not. ### Verification Rebased onto `ccfb029`. Ran on Windows 11 x64, Rust 1.98.1, `x86_64-pc-windows-msvc`: - `cargo test -p victory-pe --locked`: 39 passed (3 new: header mapping boundaries, TLS directory and callbacks in the headers, `SizeOfHeaders` bounded by the file). - `cargo clippy -p victory-pe --all-targets --all-features --locked -- -D warnings`: clean. - `cargo clippy --workspace --all-targets --locked -- -D warnings` and the `--all-features` variant: clean. - `cargo test -p victory-loader --locked`: 38 passed. - `cargo test --workspace --locked --exclude victory-shader --exclude victory-gpu`: 313 passed, 5 ignored. - `cargo xtask policy`: 182 files ok. - `cargo fmt -p victory-pe -- --check` and `cargo fmt -p victory -- --check`: clean. Two packages have failures on this machine that reproduce on unmodified `ccfb029`: - `victory-gpu`: 139 passed, 2 failed (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`). - `victory-shader`: three `emit::tests` abort with `STATUS_ACCESS_VIOLATION`. `victory-gpu` and `victory-shader` do not depend on `victory-pe`. ### Scope This is spec correctness, not a title failure reproduced here: a scan of 14,106 PE32+ images on this machine found no TLS, import, export or delay-import directory inside the headers (two `bound_import` directories were). No game files were used; the tests build their TLS structures by hand.
The headers occupy RVA 0..SizeOfHeaders and are stored one-to-one at
the start of the file, but ImageInfo only mapped section RVAs. A TLS
directory or callback array in the headers failed with "outside of
sections", and image preparation could not patch it either.

Store SizeOfHeaders (rejecting a declared region past the end of the
file, which the OS could not map), map header RVAs in
rva_to_file_offset, and read the TLS directory and its callback array
from the headers or the sections.

The tests build header-backed TLS data by hand.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:39:24 +00:00
Author
Owner

@⁠Headache3318 on GitHub · 2026-11-25 01:32 UTC · original

Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head 0b7a13c applied cleanly onto current main ccfb029.

  • cargo test -p victory-pe --locked: 6 passed, 4 failed. The 4 failures are the image tests that parse the test binary as a PE (expected on Linux — they fail identically on main). All 3 new tests pass on Linux: rva_to_file_offset_maps_headers, tls_directory_and_callbacks_in_headers, bounds_size_of_headers_by_the_file.
  • cargo clippy -p victory-pe --all-targets --all-features --locked -- -D warnings: clean.

Not tested: Windows MSVC build, the title itself. AI-assisted.

**@⁠Headache3318** on GitHub · 2026-11-25 01:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1639#issuecomment-7149661619) Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head `0b7a13c` applied cleanly onto current `main` `ccfb029`. - `cargo test -p victory-pe --locked`: 6 passed, 4 failed. The 4 failures are the `image` tests that parse the test binary as a PE (expected on Linux — they fail identically on main). All 3 new tests pass on Linux: `rva_to_file_offset_maps_headers`, `tls_directory_and_callbacks_in_headers`, `bounds_size_of_headers_by_the_file`. - `cargo clippy -p victory-pe --all-targets --all-features --locked -- -D warnings`: clean. Not tested: Windows MSVC build, the title itself. AI-assisted.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:32 UTC · original · edited 2026-11-25 02:28 UTC

Automated triage for the maintainers.

The change in crates/victory-pe/src/image.rs is consistent with the PE format: rva_to_file_offset maps RVAs below SizeOfHeaders one-to-one, and parse_tls reads the TLS directory and callback array through the new file_data_at. The tests are synthetic and correct. But the description says this fixes no observed failure: a scan of 14,106 images found no TLS directory in the headers, and the title is not affected. It also adds a new way for ImageInfo::parse to refuse an image (SizeOfHeaders larger than the file). Changes to the loader need to be worth it for the images VIctory actually loads. What would change the verdict is an image the project loads (the title or one of the runtime DLLs it maps) whose TLS or another directory sits in the headers. Without one, a maintainer may prefer not to take the extra rejection path.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1639#issuecomment-7149673289) · edited 2026-11-25 02:28 UTC <!-- victory-triage v1 head=ae83bc6fd36194872a462f2a3ee61f1e36283a66 verdict=needs-work at=2026-11-25T02:20:29Z --> Automated triage for the maintainers. The change in `crates/victory-pe/src/image.rs` is consistent with the PE format: `rva_to_file_offset` maps RVAs below `SizeOfHeaders` one-to-one, and `parse_tls` reads the TLS directory and callback array through the new `file_data_at`. The tests are synthetic and correct. But the description says this fixes no observed failure: a scan of 14,106 images found no TLS directory in the headers, and the title is not affected. It also adds a new way for `ImageInfo::parse` to refuse an image (`SizeOfHeaders` larger than the file). Changes to the loader need to be worth it for the images VIctory actually loads. What would change the verdict is an image the project loads (the title or one of the runtime DLLs it maps) whose TLS or another directory sits in the headers. Without one, a maintainer may prefer not to take the extra rejection path. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠sandivd on GitHub · 2026-11-25 01:43 UTC · original

@⁠Marvisak Rebased onto ccfb029. The conflict was #219's section-bounds validation in parse and #555's export changes plus the new ImageInfo literal in apps/victory/src/census.rs, which now passes size_of_headers: 0. Both sides are kept; no behavior from those PRs changed.

Re-ran on Windows 11 x64, Rust 1.98.1 MSVC:

  • cargo test -p victory-pe --locked: 39 passed
  • cargo test -p victory-loader --locked: 38 passed
  • cargo test --workspace --locked --exclude victory-shader --exclude victory-gpu: 313 passed, 5 ignored
  • clippy workspace, default and all features: clean
  • cargo xtask policy: 182 files ok
  • cargo fmt -p victory-pe -- --check and cargo fmt -p victory -- --check: clean

Unrelated to this PR, on this machine: victory-gpu has 2 failures and victory-shader aborts on 3 emit::tests; both reproduce on unmodified ccfb029.

**@⁠sandivd** on GitHub · 2026-11-25 01:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/1639#issuecomment-7149877619) @⁠Marvisak Rebased onto `ccfb029`. The conflict was #219's section-bounds validation in `parse` and #555's export changes plus the new `ImageInfo` literal in `apps/victory/src/census.rs`, which now passes `size_of_headers: 0`. Both sides are kept; no behavior from those PRs changed. Re-ran on Windows 11 x64, Rust 1.98.1 MSVC: - `cargo test -p victory-pe --locked`: 39 passed - `cargo test -p victory-loader --locked`: 38 passed - `cargo test --workspace --locked --exclude victory-shader --exclude victory-gpu`: 313 passed, 5 ignored - clippy workspace, default and all features: clean - `cargo xtask policy`: 182 files ok - `cargo fmt -p victory-pe -- --check` and `cargo fmt -p victory -- --check`: clean Unrelated to this PR, on this machine: `victory-gpu` has 2 failures and `victory-shader` aborts on 3 `emit::tests`; both reproduce on unmodified `ccfb029`.
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/1639/head:github-bot/gh-1639'
git switch 'github-bot/gh-1639'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1639'
git switch 'github-bot/gh-1639'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1639'
git switch 'github-bot/gh-1639'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1639'
git switch 'main'
git merge --squash 'github-bot/gh-1639'
git switch 'main'
git merge --ff-only 'github-bot/gh-1639'
git switch 'main'
git merge 'github-bot/gh-1639'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1639
No description provided.