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

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

@⁠RVZO6 on GitHub · 2026-11-26 02:09 UTC · original
RVZO6:rvzo/prepare-reloc-fast → main · +190 −25 · 1 files · 1 commits · open

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).

**@⁠RVZO6** on GitHub · 2026-11-26 02:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/2982) `RVZO6:rvzo/prepare-reloc-fast` → `main` · +190 −25 · 1 files · 1 commits · open `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).
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 image) solely to clear the single entry targeting the TLS
AddressOfCallBacks slot.

Replace it with clear_dir64_relocations(bytes, info, target_rva), which
skips every .reloc block whose [page, page + 0xFFF] window does not
contain target_rva in O(1), checks entries inside matching blocks via a
single u16 compare, allocates zero memory, and guards directory/block
offsets with checked arithmetic.

Measured (--release bench_clear_dir64_relocations, median of 3 runs,
3,000 blocks x 250 entries x 50 iters):
- 600.24 ms -> 0.6024 ms (996x, -99.90%)

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:19:16 +00:00
github-bot force-pushed github-bot/gh-2982 from 46ebcb1f99 to ff0bf23591 2026-11-26 07:27:38 +00:00 Compare
Author
Owner

@⁠pickles976 on GitHub · 2026-11-26 02:44 UTC · original

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.

**@⁠pickles976** on GitHub · 2026-11-26 02:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/2982#issuecomment-7181078837) 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`.
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-26 05:12 UTC · original

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.
**@⁠futurepro62** on GitHub · 2026-11-26 05:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/2982#issuecomment-7183269803) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 05:23 UTC · original · edited 2026-11-26 07:19 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-26 05:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2982#issuecomment-7183438365) · edited 2026-11-26 07:19 UTC <!-- 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)_
Author
Owner

@⁠RVZO6 on GitHub · 2026-11-26 06:56 UTC · original

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.
**@⁠RVZO6** on GitHub · 2026-11-26 06:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/2982#issuecomment-7184937256) 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.
This pull request can be merged automatically.
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/2982/head:github-bot/gh-2982'
git switch 'github-bot/gh-2982'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2982'
git switch 'github-bot/gh-2982'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2982'
git switch 'github-bot/gh-2982'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2982'
git switch 'main'
git merge --squash 'github-bot/gh-2982'
git switch 'main'
git merge --ff-only 'github-bot/gh-2982'
git switch 'main'
git merge 'github-bot/gh-2982'
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!2982
No description provided.