Loader/Prepare: zero-alloc block-filtered DIR64 relocation clearing for TLS callbacks #2980

Open
RVZO6 wants to merge 1 commit from upstream-pr/2982 into main
RVZO6 commented 2026-11-26 02:09:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-26T02:09:04Z.


prepare() previously called dir64_relocations(), which decoded every IMAGE_REL_BASED_DIR64 entry across the entire PE .reloc table into a Vec<(usize, u32)> (hundreds of thousands to ~1M entries on a retail title executable) solely to clear the single entry targeting the TLS AddressOfCallBacks field.

Replace it with clear_dir64_relocations(bytes, info, target_rva):

  • Skips every .reloc block whose [page, page + 0xFFF] window does not contain target_rva in O(1) without decoding its entries or allocating memory.
  • Checks entries inside matching blocks via a single u16 comparison ((DIR64 << 12) | wanted_off).
  • Uses checked arithmetic and bounds clamping on directory/block offsets.

Measured (--release bench_clear_dir64_relocations, median of 3 runs, 3,000 blocks × 250 entries × 50 iters)

  • 600.24 ms → 0.6024 ms (996×, -99.90%), zero heap allocations.

Verification

  • Official Windows MSVC + WARP SM 6.6 CI (Rust 1.98.1, default + --tests + --features diagnostics clippy, xtask policy, full workspace tests): RVZO6/VIctory CI run #45738573777 (green, combined Wave 8 check on 63bcca4).
  • Added clear_dir64_relocations_matches_full_scan_and_preserves_neighbors comparing bit-for-bit against the full-table scan across first/middle/last/absent blocks and non-DIR64 entries; existing patches_own_test_binary continues to verify with the full-table scan.
  • Not run: the title; any hardware GPU.

AI-assisted: T3 Code (Antigravity, gemini-4-neon).

<!-- greyfinch-sync:Marvisak/VIctory:issue:5502694632 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2982). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-26T02:09:04Z. --- `prepare()` previously called `dir64_relocations()`, which decoded every `IMAGE_REL_BASED_DIR64` entry across the entire PE `.reloc` table into a `Vec<(usize, u32)>` (hundreds of thousands to ~1M entries on a retail title executable) solely to clear the single entry targeting the TLS `AddressOfCallBacks` field. Replace it with `clear_dir64_relocations(bytes, info, target_rva)`: - Skips every `.reloc` block whose `[page, page + 0xFFF]` window does not contain `target_rva` in O(1) without decoding its entries or allocating memory. - Checks entries inside matching blocks via a single `u16` comparison (`(DIR64 << 12) | wanted_off`). - Uses checked arithmetic and bounds clamping on directory/block offsets. ### Measured (`--release bench_clear_dir64_relocations`, median of 3 runs, 3,000 blocks × 250 entries × 50 iters) - **`600.24 ms → 0.6024 ms` (996×, -99.90%)**, zero heap allocations. ### Verification - Official Windows MSVC + WARP SM 6.6 CI (Rust 1.98.1, default + `--tests` + `--features diagnostics` clippy, `xtask policy`, full workspace tests): [RVZO6/VIctory CI run #45738573777](https://github.com/RVZO6/VIctory/actions/runs/45738573777) (green, combined Wave 8 check on `63bcca4`). - Added `clear_dir64_relocations_matches_full_scan_and_preserves_neighbors` comparing bit-for-bit against the full-table scan across first/middle/last/absent blocks and non-`DIR64` entries; existing `patches_own_test_binary` continues to verify with the full-table scan. - **Not run:** the title; any hardware GPU. AI-assisted: T3 Code (Antigravity, gemini-4-neon).
kotekompotek commented 2026-11-26 02:28:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T02:28:24Z.

Review: COMMENTED


Verified the equivalence: a block only decodes when target_rva lands in its page, and the exact u16 match (DIR64<<12 | offset) selects the same entries the full scan would. The saturating end clamp to bytes.len() is a small robustness win over the old code, and checked_add on field_rva turns a hypothetical overflow into a PrepareError. Oracle test diffs outputs byte-for-byte across first/middle/last/absent-page targets.

<!-- greyfinch-sync:Marvisak/VIctory:review:6067757759 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2982#pullrequestreview-6067757759). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T02:28:24Z. **Review:** COMMENTED --- Verified the equivalence: a block only decodes when target_rva lands in its page, and the exact u16 match (DIR64<<12 | offset) selects the same entries the full scan would. The saturating end clamp to bytes.len() is a small robustness win over the old code, and checked_add on field_rva turns a hypothetical overflow into a PrepareError. Oracle test diffs outputs byte-for-byte across first/middle/last/absent-page targets.
pickles976 commented 2026-11-26 03:05:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pickles976 on 2026-11-26T02:44:38Z.


Independent Windows-target Wine check on a 16 GiB Linux host, exact head 46ebcb1f: relocation parity fixture 1/1 pass and patches_own_test_binary 1/1 pass. The ignored retail-sized synthetic benchmark (3,000 blocks × 250 entries × 50 iterations; debug build) printed old=1256.36 ms, new=19.2844 ms (65.1×). This did not instrument allocations or measure the late-title RAM peak; it does not yet establish that our guarded loading stop at 2,036 MiB available (2,048 MiB floor) is cleared. Logs .compat-runtime/logs/verify-pr2982-wine-{reloc-parity,patch-own-pe,reloc-bench}.log.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7181078837 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2982#issuecomment-7181078837). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-26T02:44:38Z. --- Independent Windows-target Wine check on a 16 GiB Linux host, exact head 46ebcb1f: relocation parity fixture 1/1 pass and `patches_own_test_binary` 1/1 pass. The ignored retail-sized synthetic benchmark (3,000 blocks × 250 entries × 50 iterations; debug build) printed old=1256.36 ms, new=19.2844 ms (65.1×). This did not instrument allocations or measure the *late-title* RAM peak; it does not yet establish that our guarded loading stop at 2,036 MiB available (2,048 MiB floor) is cleared. Logs `.compat-runtime/logs/verify-pr2982-wine-{reloc-parity,patch-own-pe,reloc-bench}.log`.
futurepro62 commented 2026-11-26 06:03:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-26T05:12:00Z.


Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head 46ebcb1 (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, 11 ignored.
  • The new tests ran and passed: clear_dir64_relocations_matches_full_scan_and_preserves_neighbors.
  • --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:7183269803 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2982#issuecomment-7183269803). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-26T05:12:00Z. --- Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head `46ebcb1` (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, 11 ignored**. - The new tests ran and passed: `clear_dir64_relocations_matches_full_scan_and_preserves_neighbors`. - `--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.
Marvisak commented 2026-11-26 06:03:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T05:23:10Z.


Automated triage for the maintainers.

Needs work. Thanks for ff0bf23. The benchmark is gone and there are edge cases for u32::MAX targets and a corrupt block size. clear_dir64_relocations (crates/victory-loader/src/prepare.rs) still reads as equivalent to the old scan.

The question from last time is still open. This step runs once per launch and costs about 12 ms, and no run shows an effect on startup time or peak memory. Without that, the PR replaces a simple scan with a more involved one and adds about 150 lines of test scaffolding: a hand-built PE and a reference scan. Note that this reference is not the removed code; it already includes the new checked arithmetic. The corrupt case also has no assertion. If a startup-time or memory gain can't be shown, a much smaller PR with only the checked_add on the TLS field RVA would be the part worth keeping.

CI: passing (check).


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7183438365 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2982#issuecomment-7183438365). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T05:23:10Z. --- <!-- victory-triage v1 head=ff0bf23591052e69f3ac78c229f5d20223e5e37a verdict=needs-work at=2026-11-26T07:19:28Z --> Automated triage for the maintainers. **Needs work.** Thanks for ff0bf23. The benchmark is gone and there are edge cases for `u32::MAX` targets and a corrupt block size. `clear_dir64_relocations` (`crates/victory-loader/src/prepare.rs`) still reads as equivalent to the old scan. The question from last time is still open. This step runs once per launch and costs about 12 ms, and no run shows an effect on startup time or peak memory. Without that, the PR replaces a simple scan with a more involved one and adds about 150 lines of test scaffolding: a hand-built PE and a reference scan. Note that this reference is not the removed code; it already includes the new checked arithmetic. The `corrupt` case also has no assertion. If a startup-time or memory gain can't be shown, a much smaller PR with only the `checked_add` on the TLS field RVA would be the part worth keeping. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
RVZO6 commented 2026-11-26 07:01:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-26T06:56:42Z.


Addressed triage feedback in ff0bf23:

  • Removed the #[ignore] bench_clear_dir64_relocations microbenchmark from unit tests.
  • Added checked-arithmetic edge-case coverage (target = u32::MAX and corrupt block_size = u32::MAX) to clear_dir64_relocations_matches_full_scan_and_preserves_neighbors.
  • Verified green on Windows MSVC + WARP SM 6.6 CI (run 45768895488): 37 passed, 0 failed in victory-loader, both clippy profiles, and xtask policy. Not run: the title; any hardware GPU.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7184937256 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2982#issuecomment-7184937256). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-26T06:56:42Z. --- Addressed triage feedback in `ff0bf23`: - Removed the `#[ignore]` `bench_clear_dir64_relocations` microbenchmark from unit tests. - Added checked-arithmetic edge-case coverage (`target = u32::MAX` and corrupt `block_size = u32::MAX`) to `clear_dir64_relocations_matches_full_scan_and_preserves_neighbors`. - Verified green on Windows MSVC + WARP SM 6.6 CI ([run 45768895488](https://github.com/RVZO6/VIctory/actions/runs/45768895488)): 37 passed, 0 failed in `victory-loader`, both clippy profiles, and `xtask policy`. **Not run:** the title; any hardware GPU.
cloph-dsp commented 2026-11-27 05:11:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-27T05:11:07Z.


Independent CPU-only verification of PR #2982 (Loader/Prepare: zero-alloc block-filtered DIR64 relocation clearing for TLS callbacks).

  • Windows, rustc/cargo 1.100.0/MSVC, worktree-local CARGO_TARGET_DIR (D:\Victory-Contrib\target-2982-watch), CARGO_BUILD_JOBS=2.
  • Head ff0bf23591052e69f3ac78c229f5d20223e5e37a, merge base 63bcca43c6a00a758510398fa43096ce7e4f7df0.
  • Command: cargo test --locked -p victory-loader --lib -- prepare::tests::clear_dir64_relocations_matches_full_scan_and_preserves_neighbors --exact --test-threads=1 --quiet -> 1 passed; 0 failed; 35 filtered.
  • Negative control: git grep -F for the new test name on 63bcca4 returned no hits (test absent on base).
  • git diff --check 63bcca4 HEAD exit 0 (clean).
  • Only the named synthetic test was replayed; the rest of the suite, Wine/TLS-callback runtime paths and real PE images were not exercised. This PR already has other verification reports (kotekompotek, pickles976, futurepro62); this is an additional Windows/MSVC data point.
  • AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only PE relocation logic). No game data, assets, shaders, DRM or decrypt used.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7210420250 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2982#issuecomment-7210420250). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-27T05:11:07Z. --- Independent CPU-only verification of PR #2982 (`Loader/Prepare: zero-alloc block-filtered DIR64 relocation clearing for TLS callbacks`). - Windows, rustc/cargo 1.100.0/MSVC, worktree-local `CARGO_TARGET_DIR` (`D:\Victory-Contrib\target-2982-watch`), `CARGO_BUILD_JOBS=2`. - Head `ff0bf23591052e69f3ac78c229f5d20223e5e37a`, merge base `63bcca43c6a00a758510398fa43096ce7e4f7df0`. - Command: `cargo test --locked -p victory-loader --lib -- prepare::tests::clear_dir64_relocations_matches_full_scan_and_preserves_neighbors --exact --test-threads=1 --quiet` -> `1 passed; 0 failed; 35 filtered`. - Negative control: `git grep -F` for the new test name on `63bcca4` returned no hits (test absent on base). - `git diff --check 63bcca4 HEAD` exit 0 (clean). - Only the named synthetic test was replayed; the rest of the suite, Wine/TLS-callback runtime paths and real PE images were not exercised. This PR already has other verification reports (kotekompotek, pickles976, futurepro62); this is an additional Windows/MSVC data point. - AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only PE relocation logic). No game data, assets, shaders, DRM or decrypt used.
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/2982:upstream-pr/2982'
git switch 'upstream-pr/2982'

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