Loader: find TLS-array load candidates sixteen offsets at a time (8-9x faster scan) #188

Open
TG0000 wants to merge 1 commit from upstream-pr/188 into main
TG0000 commented 2026-11-24 01:16:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T01:16:24Z.


What

victory-loader's static TLS redirect (tls::patch) rewrites every mov r64, gs:[0x58] in the title's executable sections. Before this change it tested the whole 9-byte pattern at every byte of every executable section, and a large title's sections run to well over 100 MiB.

patch now jumps between candidate offsets that next_candidate finds with SSE2 (part of baseline x86-64, so there is no runtime feature check). A candidate has 65 (gs) at the offset, 8B two bytes later and 25 four bytes later, and the full pattern is tested only there. On other architectures the same search runs as a plain scalar loop.

Why it is exactly equivalent

An offset without those three bytes cannot match the full pattern, so the only offsets skipped are ones the old loop would have rejected. Every other offset goes through the same test and the same +9 / +1 steps, so the bytes written and the returned count are identical. The search also stops at the same last offset (i + 9 <= len), and each 16-byte load ends by i + 20 <= len - 4.

Benchmark (release, synthetic)

cargo test -p victory-loader --release --lib tls_scan_speed -- --ignored --nocapture uses random bytes with one load written per 2 KiB. Best of 5, two runs:

size loads bytewise (old) candidate search (new) speedup
1 MiB 509 0.458 ms 0.048–0.051 ms 9.0–9.6x
16 MiB 8,167 7.55–7.61 ms 0.91–1.04 ms 7.3–8.3x
128 MiB 65,271 60.3–62.0 ms 7.39–7.47 ms 8.2–8.3x

Before each timing the benchmark also checks that both scans produce the same bytes and count.

Measured: only the scan itself, on synthetic data. Not measured: the effect on a real title's load time. Real code has a different byte mix from random noise, but the candidate filter needs three fixed bytes in place, so the hit rate on real code should stay tiny as well.

Tests

  • New candidate_search_patches_exactly_what_the_bytewise_scan_does: compares against the old loop, kept as a test reference, on random code full of gs bytes, with loads, near misses, refused REX/ModRM forms and overlapping loads. It covers lengths 0–63, 255–257, 4093, 4096 and 65537 at 8 start offsets, then a hand-built case: back-to-back loads, then a stray gs pair before a final load that ends the code.
  • cargo test -p victory-loader -- --test-threads=1: 19 + 2 passed, 1 ignored (the benchmark).
  • cargo clippy -p victory-loader --all-targets: clean. rustfmt --check on tls.rs: clean (cargo fmt --check already flags crash.rs, lib.rs and threads.rs on main, which this PR does not touch).
  • cargo run -p xtask -- policy: 151 files ok.
  • Windows 11, x86-64, stable-x86_64-pc-windows-msvc (rustc 1.100.0). The repo pins 1.98.1, which resolves to the GNU host here and cannot link without dlltool. No GPU is involved.

Only crates/victory-loader/src/tls.rs changes, and no open PR touches it. No game data, paths or assets.

AI-assisted (Claude Opus 5.5).

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5464216666 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/188). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T01:16:24Z. --- ## What `victory-loader`'s static TLS redirect (`tls::patch`) rewrites every `mov r64, gs:[0x58]` in the title's executable sections. Before this change it tested the whole 9-byte pattern at **every byte** of every executable section, and a large title's sections run to well over 100 MiB. `patch` now jumps between candidate offsets that `next_candidate` finds with SSE2 (part of baseline x86-64, so there is no runtime feature check). A candidate has `65` (`gs`) at the offset, `8B` two bytes later and `25` four bytes later, and the full pattern is tested only there. On other architectures the same search runs as a plain scalar loop. ## Why it is exactly equivalent An offset without those three bytes cannot match the full pattern, so the only offsets skipped are ones the old loop would have rejected. Every other offset goes through the same test and the same `+9` / `+1` steps, so the bytes written and the returned count are identical. The search also stops at the same last offset (`i + 9 <= len`), and each 16-byte load ends by `i + 20 <= len - 4`. ## Benchmark (release, synthetic) `cargo test -p victory-loader --release --lib tls_scan_speed -- --ignored --nocapture` uses random bytes with one load written per 2 KiB. Best of 5, two runs: | size | loads | bytewise (old) | candidate search (new) | speedup | |---|---|---|---|---| | 1 MiB | 509 | 0.458 ms | 0.048–0.051 ms | 9.0–9.6x | | 16 MiB | 8,167 | 7.55–7.61 ms | 0.91–1.04 ms | 7.3–8.3x | | 128 MiB | 65,271 | 60.3–62.0 ms | 7.39–7.47 ms | 8.2–8.3x | Before each timing the benchmark also checks that both scans produce the same bytes and count. Measured: only the scan itself, on synthetic data. Not measured: the effect on a real title's load time. Real code has a different byte mix from random noise, but the candidate filter needs three fixed bytes in place, so the hit rate on real code should stay tiny as well. ## Tests - New `candidate_search_patches_exactly_what_the_bytewise_scan_does`: compares against the old loop, kept as a test reference, on random code full of `gs` bytes, with loads, near misses, refused REX/ModRM forms and overlapping loads. It covers lengths 0–63, 255–257, 4093, 4096 and 65537 at 8 start offsets, then a hand-built case: back-to-back loads, then a stray `gs` pair before a final load that ends the code. - `cargo test -p victory-loader -- --test-threads=1`: 19 + 2 passed, 1 ignored (the benchmark). - `cargo clippy -p victory-loader --all-targets`: clean. `rustfmt --check` on `tls.rs`: clean (`cargo fmt --check` already flags crash.rs, lib.rs and threads.rs on main, which this PR does not touch). - `cargo run -p xtask -- policy`: 151 files ok. - Windows 11, x86-64, `stable-x86_64-pc-windows-msvc` (rustc 1.100.0). The repo pins 1.98.1, which resolves to the GNU host here and cannot link without dlltool. No GPU is involved. Only `crates/victory-loader/src/tls.rs` changes, and no open PR touches it. No game data, paths or assets. AI-assisted (Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DaviTostes commented 2026-11-24 06:55:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:32:09Z.


Tested 2a6e6a4 (Wine 11.22 + vkd3d-proton 3.1, Windows build of victory-loader): pass.

  • 19 passed, 0 failed, 1 ignored (the ignored one is a benchmark), including the 1 test this PR adds.
  • The Linux build of the loader cannot run these tests (they parse the test binary as PE; it is ELF), so the Windows build under Wine is the check.

No game data used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119195942 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7119195942). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:32:09Z. --- Tested `2a6e6a4` (Wine 11.22 + vkd3d-proton 3.1, Windows build of `victory-loader`): **pass**. - **19 passed, 0 failed, 1 ignored** (the ignored one is a benchmark), including the 1 test this PR adds. - The Linux build of the loader cannot run these tests (they parse the test binary as PE; it is ELF), so the Windows build under Wine is the check. No game data used.
TG0000 commented 2026-11-24 12:04:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T11:48:54Z.


Tested on AMD Radeon RX 6800 (driver 32.0.21045.29003), Ryzen 9 5900X, Windows 11, as part of a combined branch on ffe310e (this PR is the only one touching victory-loader).

  • run without --execute (15 alternating runs each): median 369 ms on main (range 350–398 ms) → 313 ms with this change (range 288–336 ms, −15%, ranges do not overlap).
  • Both patch the exact same 53,246 TLS sites.
  • Full retail title boots cleanly through menus (30 fps) and reaches the world with 0 crashes and 0 device removals.

(Note: measured on the combined branch rather than this PR in isolation. AI-assisted.)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7130586616 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7130586616). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T11:48:54Z. --- Tested on AMD Radeon RX 6800 (driver 32.0.21045.29003), Ryzen 9 5900X, Windows 11, as part of a combined branch on `ffe310e` (this PR is the only one touching `victory-loader`). - `run` without `--execute` (15 alternating runs each): median **369 ms** on `main` (range 350–398 ms) → **313 ms** with this change (range 288–336 ms, **−15%**, ranges do not overlap). - Both patch the exact same **53,246** TLS sites. - Full retail title boots cleanly through menus (30 fps) and reaches the world with 0 crashes and 0 device removals. *(Note: measured on the combined branch rather than this PR in isolation. AI-assisted.)*
yuruiwang233 commented 2026-11-24 15:04:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by yuruiwang233 on 2026-11-24T14:40:33Z.


Independent verification on Windows 11, x86_64-pc-windows-gnu (rustc 1.98.1, MinGW-w64 15.2). Note this is the GNU toolchain, not the MSVC one CI uses.

  • cargo test -p victory-loader --lib tls::: 2 passed, 0 failed, 1 ignored, including candidate_search_patches_exactly_what_the_bytewise_scan_does.
  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings: clean.
  • cargo test -p victory-loader --release --lib -- --ignored --nocapture tls_scan_speed: bytewise vs candidate search, best of five: 1 MiB 0.898 -> 0.071 ms (12.7x), 16 MiB 14.777 -> 1.197 ms (12.3x), 128 MiB 125.313 -> 11.215 ms (11.2x). The patched output buffers compared equal in both scans.

Reading the bounds: with end = code.len() + 1 - LOAD_LEN, the widest read is at(i + 4) ending at i + 20, and i + 16 <= end keeps that inside the slice; the bytewise fallback (i..end) cannot skip a candidate the old loop would have tested. No blocking concerns from me. Caveat: verified on GNU, so the 11-13x numbers are from this toolchain only.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7134763955 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7134763955). Originally posted by [yuruiwang233](https://github.com/yuruiwang233) on 2026-11-24T14:40:33Z. --- Independent verification on Windows 11, x86_64-pc-windows-gnu (rustc 1.98.1, MinGW-w64 15.2). Note this is the GNU toolchain, not the MSVC one CI uses. - `cargo test -p victory-loader --lib tls::`: 2 passed, 0 failed, 1 ignored, including `candidate_search_patches_exactly_what_the_bytewise_scan_does`. - `cargo clippy -p victory-loader --all-targets --locked -- -D warnings`: clean. - `cargo test -p victory-loader --release --lib -- --ignored --nocapture tls_scan_speed`: bytewise vs candidate search, best of five: 1 MiB 0.898 -> 0.071 ms (12.7x), 16 MiB 14.777 -> 1.197 ms (12.3x), 128 MiB 125.313 -> 11.215 ms (11.2x). The patched output buffers compared equal in both scans. Reading the bounds: with `end = code.len() + 1 - LOAD_LEN`, the widest read is `at(i + 4)` ending at `i + 20`, and `i + 16 <= end` keeps that inside the slice; the bytewise fallback `(i..end)` cannot skip a candidate the old loop would have tested. No blocking concerns from me. Caveat: verified on GNU, so the 11-13x numbers are from this toolchain only.
Marvisak commented 2026-11-25 01:07:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:21:43Z.


Automated triage for the maintainers.

Merge candidate. patch in crates/victory-loader/src/tls.rs now jumps between candidate offsets from next_candidate, which tests the gs prefix, 8B at +2 and 25 at +4 for 16 offsets at a time with SSE2, with a scalar fallback; the full pattern test and the +9/+1 steps are unchanged, so it patches the same sites. The unsafe loads stay in bounds (the loop runs only while i + 16 <= len - 8), and a new test compares the result with the old byte-by-byte scan over many lengths and offsets, including near misses and overlapping loads. Reported load time 369 → 313 ms with the same 53,246 sites patched.

CI: no check runs on the head commit.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148429053 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7148429053). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:21:43Z. --- <!-- victory-triage v1 head=2a6e6a4bcea51c3fc4df3c3b6823a15f501b4039 verdict=merge-candidate at=2026-11-25T00:18:15Z --> Automated triage for the maintainers. **Merge candidate.** `patch` in `crates/victory-loader/src/tls.rs` now jumps between candidate offsets from `next_candidate`, which tests the `gs` prefix, `8B` at +2 and `25` at +4 for 16 offsets at a time with SSE2, with a scalar fallback; the full pattern test and the `+9`/`+1` steps are unchanged, so it patches the same sites. The unsafe loads stay in bounds (the loop runs only while `i + 16 <= len - 8`), and a new test compares the result with the old byte-by-byte scan over many lengths and offsets, including near misses and overlapping loads. Reported load time 369 → 313 ms with the same 53,246 sites patched. CI: no check runs on the head commit. --- _Generated by [Claude Code](https://claude.ai/code)_
fuzeofficlal commented 2026-11-26 01:04:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T01:01:28Z.


Code review and Windows 11 release benchmark verification at head 2a6e6a4bcea5 merged cleanly onto main (63bcca43c6a0):

  • SSE2 bounds & equivalence audit (crates/victory-loader/src/tls.rs:78-152):
    • With end = (code.len() + 1).checked_sub(LOAD_LEN)? (LOAD_LEN = 9), the SSE2 loop condition i + 16 <= end guarantees i + 20 <= code.len() - 4 <= code.len() for every unaligned _mm_loadu_si128 at offsets +0, +2, +4.
    • Matching 0x65 (gs), 0x8B (mov), and 0x25 ([disp32] SIB) simultaneously and scanning mask.trailing_zeros() handles cross-16-byte boundaries and arbitrary start offsets identically to the bytewise scan, with the tail i..end handled by the scalar fallback (candidate_search_patches_exactly_what_the_bytewise_scan_does verifies byte-for-byte parity across 0..64, unaligned offsets, back-to-back loads, and end-of-buffer payloads).
  • Verified (Windows 11, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy -p victory-loader --all-targets -- -D warnings: 0 warnings
    • cargo test --locked -p victory-loader --all-targets: 40/40 passed (1 ignored bench)
    • --release tls_scan_speed:
      • small 1 MiB ( 509 loads): 0.304 ms -> 0.039 ms (7.8x)
      • medium 16 MiB ( 8167 loads): 6.266 ms -> 0.665 ms (9.4x)
      • large 128 MiB (65271 loads): 69.933 ms -> 6.653 ms (10.5x)

Clean, bounds-safe, and fast; LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179349608 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7179349608). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T01:01:28Z. --- Code review and Windows 11 release benchmark verification at head `2a6e6a4bcea5` merged cleanly onto `main` (`63bcca43c6a0`): - **SSE2 bounds & equivalence audit (`crates/victory-loader/src/tls.rs:78-152`)**: - With `end = (code.len() + 1).checked_sub(LOAD_LEN)?` (`LOAD_LEN = 9`), the SSE2 loop condition `i + 16 <= end` guarantees `i + 20 <= code.len() - 4 <= code.len()` for every unaligned `_mm_loadu_si128` at offsets `+0`, `+2`, `+4`. - Matching `0x65 (gs)`, `0x8B (mov)`, and `0x25 ([disp32] SIB)` simultaneously and scanning `mask.trailing_zeros()` handles cross-16-byte boundaries and arbitrary start offsets identically to the bytewise scan, with the tail `i..end` handled by the scalar fallback (`candidate_search_patches_exactly_what_the_bytewise_scan_does` verifies byte-for-byte parity across `0..64`, unaligned offsets, back-to-back loads, and end-of-buffer payloads). - **Verified (Windows 11, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy -p victory-loader --all-targets -- -D warnings`: 0 warnings - `cargo test --locked -p victory-loader --all-targets`: **40/40 passed** (1 ignored bench) - `--release` `tls_scan_speed`: - `small 1 MiB ( 509 loads)`: `0.304 ms -> 0.039 ms` (**7.8x**) - `medium 16 MiB ( 8167 loads)`: `6.266 ms -> 0.665 ms` (**9.4x**) - `large 128 MiB (65271 loads)`: `69.933 ms -> 6.653 ms` (**10.5x**) Clean, bounds-safe, and fast; LGTM.
cloph-dsp commented 2026-11-27 13:56:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-27T13:56:16Z.


Independent CPU-only verification on Windows / Rust 1.100.0 / MSVC.

  • PR head: 2a6e6a4bcea51c3fc4df3c3b6823a15f501b4039, merge base d2a4bae1f49533ef5cb512fd1b1ba1513f2e1989.
  • Command: cargo test --locked -p victory-loader --lib -- tls::tests::candidate_search_patches_exactly_what_the_bytewise_scan_does --exact --test-threads=1 --quiet
  • Result: test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 19 filtered out, exit 0.
  • Negative control: git grep -F -c tls::tests::candidate_search_patches_exactly_what_the_bytewise_scan_does d2a4bae1 -- . → exit 1 / 0 hits (test genuinely new).
  • git diff --check d2a4bae1 HEAD → exit 0.
  • Caveat: only the named synthetic test was replayed; the full crate suite and real title TLS patching were not exercised.
  • GPU/driver: N/A (CPU-only test). No game data, assets, extracted shaders or DRM involved.
  • AI-assisted: Hy4 flash.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7220127993 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7220127993). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-27T13:56:16Z. --- Independent CPU-only verification on Windows / Rust 1.100.0 / MSVC. - PR head: `2a6e6a4bcea51c3fc4df3c3b6823a15f501b4039`, merge base `d2a4bae1f49533ef5cb512fd1b1ba1513f2e1989`. - Command: `cargo test --locked -p victory-loader --lib -- tls::tests::candidate_search_patches_exactly_what_the_bytewise_scan_does --exact --test-threads=1 --quiet` - Result: `test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 19 filtered out`, exit 0. - Negative control: `git grep -F -c tls::tests::candidate_search_patches_exactly_what_the_bytewise_scan_does d2a4bae1 -- .` → exit 1 / 0 hits (test genuinely new). - `git diff --check d2a4bae1 HEAD` → exit 0. - Caveat: only the named synthetic test was replayed; the full crate suite and real title TLS patching were not exercised. - GPU/driver: N/A (CPU-only test). No game data, assets, extracted shaders or DRM involved. - AI-assisted: Hy4 flash.
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/188:upstream-pr/188'
git switch 'upstream-pr/188'

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