Loader: skip DIR64 relocation addresses that overflow u32 #3241

Open
andrewpozdnakov7-jpg wants to merge 3 commits from upstream-pr/3243 into main
andrewpozdnakov7-jpg commented 2026-11-26 14:22:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T14:22:14Z.


A malformed BASERELOC block puts the page RVA near u32::MAX; page + u32::from(e & 0xFFF) in dir64_relocations then panics in debug builds and wraps in release ones, returning a relocation address that was never in the image.

reloc_address now uses checked_add, and an entry that does not fit is skipped on its own, so a bad page base does not throw away the well-formed entries after it. end is clamped to bytes.len() with saturating_add; valid files are unchanged, since the directory is inside the file.

  • relocation_addresses_past_u32_are_skipped covers 0x1000 + 0x0A, the last address 0xFFFF_FFF0 + 0xF, the overflowing 0xFFFF_FFF0 + 0x10, and the type/offset split 0xA000 -> 0x2000.
  • cargo test -p victory-loader: 35 passed, 1 failed. The failure is prepare::tests::windows_assigns_static_tls_to_a_prepared_dll ("the test binary uses static TLS"); it fails identically with this patch stashed, so it is local to this machine.
  • cargo clippy -p victory-loader --all-targets -- -D warnings: clean. cargo xtask policy: 192 files ok.

The other two points in #2411 cannot fail on the 64-bit hosts VIctory builds for: start + size and at + block are a usize plus a u32, and reads past the end already stop through layout::read_u32 and bytes.get. #2418 guards those as well, with a proposed close triage for that reason; the reloc_address change is the behavioural difference. The issue's proposed arithmetic came from @pushkarreddyy.

Fixes #2411.

Coordination

<!-- greyfinch-sync:Marvisak/VIctory:issue:5511740499 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3243). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T14:22:14Z. --- A malformed `BASERELOC` block puts the page RVA near `u32::MAX`; `page + u32::from(e & 0xFFF)` in `dir64_relocations` then panics in debug builds and wraps in release ones, returning a relocation address that was never in the image. `reloc_address` now uses `checked_add`, and an entry that does not fit is skipped on its own, so a bad page base does not throw away the well-formed entries after it. `end` is clamped to `bytes.len()` with `saturating_add`; valid files are unchanged, since the directory is inside the file. - `relocation_addresses_past_u32_are_skipped` covers `0x1000 + 0x0A`, the last address `0xFFFF_FFF0 + 0xF`, the overflowing `0xFFFF_FFF0 + 0x10`, and the type/offset split `0xA000 -> 0x2000`. - `cargo test -p victory-loader`: 35 passed, 1 failed. The failure is `prepare::tests::windows_assigns_static_tls_to_a_prepared_dll` ("the test binary uses static TLS"); it fails identically with this patch stashed, so it is local to this machine. - `cargo clippy -p victory-loader --all-targets -- -D warnings`: clean. `cargo xtask policy`: 192 files ok. The other two points in #2411 cannot fail on the 64-bit hosts VIctory builds for: `start + size` and `at + block` are a `usize` plus a `u32`, and reads past the end already stop through `layout::read_u32` and `bytes.get`. #2418 guards those as well, with a `proposed close` triage for that reason; the `reloc_address` change is the behavioural difference. The issue's proposed arithmetic came from @pushkarreddyy. Fixes #2411. [Coordination](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7194721425)
Dukestar69 commented 2026-11-26 14:30:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T14:30:49Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64:

  • Using checked_add in
    eloc_address and clamping end with saturating_add(...).min(bytes.len()) cleanly prevents debug overflow panics and release wraparound on malformed BASERELOC blocks.
  • Minor non-blocking formatting nit in prepare.rs: n dir64_relocations(...) { const DIR64: u16 = 10; is on one line and the doc comment for dir64_relocations was left above
    eloc_address.
  • cargo test -p victory-loader -- --test-threads=1: 36 unit + 4 integration tests passed, 0 failed (including
    elocation_addresses_past_u32_are_skipped).
  • cargo clippy -p victory-loader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6075858988 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3243#pullrequestreview-6075858988). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T14:30:49Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64: - Using checked_add in eloc_address and clamping end with saturating_add(...).min(bytes.len()) cleanly prevents debug overflow panics and release wraparound on malformed BASERELOC blocks. - Minor non-blocking formatting nit in prepare.rs: n dir64_relocations(...) { const DIR64: u16 = 10; is on one line and the doc comment for dir64_relocations was left above eloc_address. - cargo test -p victory-loader -- --test-threads=1: 36 unit + 4 integration tests passed, 0 failed (including elocation_addresses_past_u32_are_skipped). - cargo clippy -p victory-loader --all-targets -- -D warnings: clean. - cargo xtask policy: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
andrewpozdnakov7-jpg commented 2026-11-26 16:05:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T15:39:54Z.


Thanks for catching this. 2b898b6 moves the iterator's doc comment back to dir64_relocations and puts const DIR64 on its own line. The patch changes comments and whitespace. The loader builds with all features.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7196581422 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3243#issuecomment-7196581422). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T15:39:54Z. --- Thanks for catching this. [`2b898b6`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/2b898b60fd4abb45c0048232f3421a7ee2d4f0aa) moves the iterator's doc comment back to `dir64_relocations` and puts `const DIR64` on its own line. The patch changes comments and whitespace. The loader builds with all features.
cloph-dsp commented 2026-11-27 01:42:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-27T01:42:31Z.


Independent local verification (Windows, Rust 1.100.0/MSVC, synthetic inputs only, no game data).

  • PR head: 2b898b6; merge base with origin/main: 63bcca4.
  • Command: cargo test --locked -p victory-loader --lib -- prepare::tests::relocation_addresses_past_u32_are_skipped --exact --test-threads=1 --quiet
  • Result: test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 35 filtered out, exit 0.
  • git diff --check 63bcca4 HEAD: exit 0, clean.
  • Negative control: git grep -n relocation_addresses_past_u32_are_skipped 63bcca4 -> 0 hits; the test is new on this PR.

Caveat: this run exercises the extracted reloc_address helper, not dir64_relocations end-to-end; no malformed-BASERELOC fixture was replayed here.

AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only loader test). No game data, assets or DRM involved.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7207512792 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3243#issuecomment-7207512792). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-27T01:42:31Z. --- Independent local verification (Windows, Rust 1.100.0/MSVC, synthetic inputs only, no game data). - PR head: `2b898b6`; merge base with `origin/main`: `63bcca4`. - Command: `cargo test --locked -p victory-loader --lib -- prepare::tests::relocation_addresses_past_u32_are_skipped --exact --test-threads=1 --quiet` - Result: `test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 35 filtered out`, exit 0. - `git diff --check 63bcca4 HEAD`: exit 0, clean. - Negative control: `git grep -n relocation_addresses_past_u32_are_skipped 63bcca4` -> 0 hits; the test is new on this PR. Caveat: this run exercises the extracted `reloc_address` helper, not `dir64_relocations` end-to-end; no malformed-BASERELOC fixture was replayed here. AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only loader test). No game data, assets or DRM involved.
pushkarreddyy commented 2026-11-27 04:04:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-27T04:04:43Z.


@cloph-dsp Thank you for the independent local verification on 2b898b6!

@Marvisak Please review and merge this PR when convenient. Testers on untested setups are welcome to test.

AI-assisted: yes (Gemini 4 Neon)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7209501503 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3243#issuecomment-7209501503). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-27T04:04:43Z. --- @cloph-dsp Thank you for the independent local verification on `2b898b6`! @Marvisak Please review and merge this PR when convenient. Testers on untested setups are welcome to test. AI-assisted: yes (Gemini 4 Neon)
futurepro62 commented 2026-11-27 23:21:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-27T23:21:56Z.


NVIDIA full-suite run, to go with the reports above by cloph-dsp, since none of them names a GPU run. Tested exact head 2b898b6 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 867 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: relocation_addresses_past_u32_are_skipped.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (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:Marvisak/VIctory:comment:7230084287 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3243#issuecomment-7230084287). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-27T23:21:56Z. --- NVIDIA full-suite run, to go with the reports above by cloph-dsp, since none of them names a GPU run. Tested exact head `2b898b6` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **867 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `relocation_addresses_past_u32_are_skipped`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
pushkarreddyy commented 2026-11-28 07:33:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-28T07:33:21Z.


@futurepro62 Thank you for running the full RTX 5070 Ti test suite on 2b898b6 (867 passed, 0 failed, 10 ignored, including relocation_addresses_past_u32_are_skipped)!

@Marvisak Please review and merge this PR when convenient (CI is green). Testers on untested setups and GPU models are welcome to test.

AI-assisted: yes (Gemini 4 Neon)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7236014914 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3243#issuecomment-7236014914). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-28T07:33:21Z. --- @futurepro62 Thank you for running the full RTX 5070 Ti test suite on `2b898b6` (`867 passed, 0 failed, 10 ignored`, including `relocation_addresses_past_u32_are_skipped`)! @Marvisak Please review and merge this PR when convenient (CI is green). Testers on untested setups and GPU models are welcome to test. AI-assisted: yes (Gemini 4 Neon)
Sal-zinho commented 2026-11-28 16:58:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Sal-zinho on 2026-11-28T16:58:40Z.

Review: COMMENTED


Tested on Windows 11, x86_64-pc-windows-msvc, Rust 1.98.1.

Tests: All 36 unit tests pass (including the new relocation_addresses_past_u32_are_skipped). The pre-existing loads_binds_and_calls_probe failure (OS error 2) is unchanged from main and unrelated to this PR. windows_assigns_static_tls_to_a_prepared_dll passes here.

Clippy: Clean, no warnings.

Formatting: Minor nit — prepare.rs:142 has a formatting diff with the pinned rustfmt 1.98.1 (the .saturating_add().min() chain wants to be broken across lines). crash.rs, lib.rs and publish.rs fail fmt --check identically on main, so those are pre-existing and not from this PR.

<!-- greyfinch-sync:Marvisak/VIctory:review:6099161481 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3243#pullrequestreview-6099161481). Originally posted by [Sal-zinho](https://github.com/Sal-zinho) on 2026-11-28T16:58:40Z. **Review:** COMMENTED --- Tested on Windows 11, x86_64-pc-windows-msvc, Rust 1.98.1. Tests: All 36 unit tests pass (including the new relocation_addresses_past_u32_are_skipped). The pre-existing loads_binds_and_calls_probe failure (OS error 2) is unchanged from main and unrelated to this PR. windows_assigns_static_tls_to_a_prepared_dll passes here. Clippy: Clean, no warnings. Formatting: Minor nit — prepare.rs:142 has a formatting diff with the pinned rustfmt 1.98.1 (the .saturating_add().min() chain wants to be broken across lines). crash.rs, lib.rs and publish.rs fail fmt --check identically on main, so those are pre-existing and not from this PR.
andrewpozdnakov7-jpg commented 2026-11-28 17:12:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T17:12:25Z.


Thanks, @Sal-zinho. The wrapping in prepare.rs now matches the pinned rustfmt in 210dea6. I checked the whitespace-only diff and compiled the loader with all features.

Windows CI passed on this exact head. The next step is maintainer review.

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7242836537 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3243#issuecomment-7242836537). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T17:12:25Z. --- Thanks, @Sal-zinho. The wrapping in `prepare.rs` now matches the pinned rustfmt in `210dea6`. I checked the whitespace-only diff and compiled the loader with all features. [Windows CI](https://github.com/Marvisak/VIctory/actions/runs/46168879615/job/141052519502) passed on this exact head. The next step is maintainer review. AI-assisted with OpenAI Codex.
dux14 commented 2026-11-29 05:27:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by dux14 on 2026-11-29T05:27:05Z.


reloc_address with checked_add fixes the only overflow that can actually occur, page + (e & 0xFFF). It skips just the bad entry, so well-formed entries later in the block still apply. The unit test covers the boundary (0xFFFF_FFF0+0xF is Some, +0x10 is None) and the type/offset split without needing a PE fixture. The end clamp changes nothing on 64-bit hosts, so it can stay as hardening or be dropped. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7251261615 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3243#issuecomment-7251261615). Originally posted by [dux14](https://github.com/dux14) on 2026-11-29T05:27:05Z. --- `reloc_address` with `checked_add` fixes the only overflow that can actually occur, `page + (e & 0xFFF)`. It skips just the bad entry, so well-formed entries later in the block still apply. The unit test covers the boundary (`0xFFFF_FFF0+0xF` is `Some`, `+0x10` is `None`) and the type/offset split without needing a PE fixture. The `end` clamp changes nothing on 64-bit hosts, so it can stay as hardening or be dropped. LGTM.
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/3243:upstream-pr/3243'
git switch 'upstream-pr/3243'

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