d3d12-loader: keep multiple clzero stub slabs within rel32 reach (D3D12-LOADER-CLZERO-STUBS-NEAR) #3452

Code

Clone this repository

Checkout

From your project repository, check out a new branch and test the changes.

git fetch -u  "d3d12-loader/clzero-stubs-near:und3rlake-d3d12-loader/clzero-stubs-near"
git switch "und3rlake-d3d12-loader/clzero-stubs-near"
Open
leonida/mandy
und3rlake wants to merge 1 commit from und3rlake/mandy:d3d12-loader/clzero-stubs-near into main

This branch is out-of-date with the base branch

Conversation
Reviewers 0 approvals
  • No reviewers
Details
This pull request is blocked because it's outdated.
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 'd3d12-loader/clzero-stubs-near:und3rlake-d3d12-loader/clzero-stubs-near'
git switch 'und3rlake-d3d12-loader/clzero-stubs-near'
First-time contributor

Claimed in the coordination issue as: D3D12-LOADER-CLZERO-STUBS-NEAR (claim)

What and why

Fixes the intermittent console_insn::tests::a_clzero_loop_becomes_plain_stores failure (assertion left == right failed: a jump, left: 15, right: 233) under parallel cargo test runs, as well as the underlying runtime bug when clzero loops fault in code regions more than 2 GiB apart.

Root cause:

  • console_insn::STUBS held a single process-global 64 KiB Option<(usize, usize)> slab.
  • Once any clzero loop initialized STUBS (for example, crash::tests::clzero_loops_end_like_their_last_pass faulting inside the test binary's .text section on CPUs without hardware clzero, or a separate module mapped > 2 GiB away), patch_clzero never allocated another slab near later faulting addresses.
  • Subsequent patch_clzero calls for distant VirtualAlloc pages failed jump(at, stub) (i32 displacement overflow) and returned false, leaving the original 0x0f (15) opcode instead of 0xe9 (233).

Changes (d3d12/crates/leonida-loader/src/console_insn.rs):

  • Replace STUBS: Mutex<Option<(usize, usize)>> with STUBS: Mutex<Vec<(usize, usize)>> so patch_clzero reuses an existing in-reach slab with room (checking 16-byte-aligned size against STUBS_LEN) or allocates a fresh slab via near(at).
  • Validate alignment ((at & 15) + 5 <= 16) and cmp form before allocating a slab, call FlushInstructionCache on the written stub before exposing the jump, only advance slab used (or retain a newly allocated slab) when patch(...) succeeds, and filter place >= 0x1_0000 in near(at) so at.checked_sub(d) == Some(0) cannot pass NULL to VirtualAlloc.
  • Add deterministic regression test clzero_loops_far_apart_each_get_a_reachable_stub allocating two RWX pages > 2 GiB apart and verifying both patch to 0xe9 and execute cleanly.

Tested

  • Red -> Green (clzero_loops_far_apart_each_get_a_reachable_stub):
    • On main (68427be7e) without the fix: FAILED deterministically with assertion left == right failed: a jump (left: 15, right: 233).
    • With the fix: PASSED (1 passed, 0 failed).
  • cargo test -p leonida-loader --lib --locked -> 99 passed, 0 failed
  • cargo test -p leonida-loader --features diagnostics --lib --locked -> 99 passed, 0 failed
  • 20 consecutive parallel runs of the full leonida-loader unit-test binary -> 20/20 PASS (0 failures)
  • cargo clippy -p leonida-loader --all-targets --locked -- -D warnings -> 0 warnings
  • python -I scripts/check_no_game_data.py -> PASS
  • Not tested: live in-game run (loader unit/patcher fix; no GPU or shader paths changed).

GPUs tested: CPU-only loader tests on Windows 11; host GPU NVIDIA RTX 5070 12 GB

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
Claimed in the coordination issue as: D3D12-LOADER-CLZERO-STUBS-NEAR ([claim](https://git.leonida.now/leonida/mandy/issues/3312#issuecomment-25715)) ### What and why Fixes the intermittent `console_insn::tests::a_clzero_loop_becomes_plain_stores` failure (`assertion left == right failed: a jump`, `left: 15, right: 233`) under parallel `cargo test` runs, as well as the underlying runtime bug when `clzero` loops fault in code regions more than 2 GiB apart. **Root cause:** - `console_insn::STUBS` held a single process-global 64 KiB `Option<(usize, usize)>` slab. - Once any `clzero` loop initialized `STUBS` (for example, `crash::tests::clzero_loops_end_like_their_last_pass` faulting inside the test binary's `.text` section on CPUs without hardware `clzero`, or a separate module mapped > 2 GiB away), `patch_clzero` never allocated another slab near later faulting addresses. - Subsequent `patch_clzero` calls for distant `VirtualAlloc` pages failed `jump(at, stub)` (`i32` displacement overflow) and returned `false`, leaving the original `0x0f` (`15`) opcode instead of `0xe9` (`233`). **Changes (`d3d12/crates/leonida-loader/src/console_insn.rs`):** - Replace `STUBS: Mutex<Option<(usize, usize)>>` with `STUBS: Mutex<Vec<(usize, usize)>>` so `patch_clzero` reuses an existing in-reach slab with room (checking 16-byte-aligned size against `STUBS_LEN`) or allocates a fresh slab via `near(at)`. - Validate alignment (`(at & 15) + 5 <= 16`) and `cmp` form before allocating a slab, call `FlushInstructionCache` on the written stub before exposing the jump, only advance slab `used` (or retain a newly allocated slab) when `patch(...)` succeeds, and filter `place >= 0x1_0000` in `near(at)` so `at.checked_sub(d) == Some(0)` cannot pass `NULL` to `VirtualAlloc`. - Add deterministic regression test `clzero_loops_far_apart_each_get_a_reachable_stub` allocating two RWX pages > 2 GiB apart and verifying both patch to `0xe9` and execute cleanly. ### Tested - **Red -> Green (`clzero_loops_far_apart_each_get_a_reachable_stub`):** - On `main` (`68427be7e`) without the fix: **FAILED** deterministically with `assertion left == right failed: a jump` (`left: 15`, `right: 233`). - With the fix: **PASSED** (`1 passed, 0 failed`). - `cargo test -p leonida-loader --lib --locked` -> **99 passed, 0 failed** - `cargo test -p leonida-loader --features diagnostics --lib --locked` -> **99 passed, 0 failed** - 20 consecutive parallel runs of the full `leonida-loader` unit-test binary -> **20/20 PASS** (`0` failures) - `cargo clippy -p leonida-loader --all-targets --locked -- -D warnings` -> **0 warnings** - `python -I scripts/check_no_game_data.py` -> **PASS** - Not tested: live in-game run (loader unit/patcher fix; no GPU or shader paths changed). GPUs tested: CPU-only loader tests on Windows 11; host GPU NVIDIA RTX 5070 12 GB AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a)
First-time contributor

Independent local verification of this change (I changed no code in this PR).

  • Head bb9fef76, merge base 68427be7. The new test is absent on the base: git grep -F clzero_loops_far_apart_each_get_a_reachable_stub 68427be7 -- d3d12/crates/leonida-loader/src/console_insn.rs -> exit 1.
  • On the PR head: cargo test -p leonida-loader --locked --lib console_insn::tests::clzero_loops_far_apart_each_get_a_reachable_stub -- --exact -> 1 passed; the whole crate cargo test -p leonida-loader --locked --lib -> 99 passed, 0 failed, with a private CARGO_TARGET_DIR.
  • Red/green: inserting only the new test function (not the implementation) into the untouched base module fails at assertion left == right: a jump, so the stub-slab change is required.
  • git diff --check 68427be7 bb9fef76 -> clean. python -I scripts/check_no_game_data.py d3d12/crates/leonida-loader/src/console_insn.rs -> clean.

GPUs tested: none (Windows RWX pages with a synthetic clzero loop, CPU-only emulation test; no device-gated test). Driver: n/a (no graphics driver involved). No game data, assets or extracted shaders were used or attached.

AI-assisted: yes, Hy4 flash (verification only). No approval implied; this is a local test report.

Independent local verification of this change (I changed no code in this PR). - Head `bb9fef76`, merge base `68427be7`. The new test is absent on the base: `git grep -F clzero_loops_far_apart_each_get_a_reachable_stub 68427be7 -- d3d12/crates/leonida-loader/src/console_insn.rs` -> exit 1. - On the PR head: `cargo test -p leonida-loader --locked --lib console_insn::tests::clzero_loops_far_apart_each_get_a_reachable_stub -- --exact` -> 1 passed; the whole crate `cargo test -p leonida-loader --locked --lib` -> 99 passed, 0 failed, with a private `CARGO_TARGET_DIR`. - Red/green: inserting only the new test function (not the implementation) into the untouched base module fails at `assertion left == right: a jump`, so the stub-slab change is required. - `git diff --check 68427be7 bb9fef76` -> clean. `python -I scripts/check_no_game_data.py d3d12/crates/leonida-loader/src/console_insn.rs` -> clean. GPUs tested: none (Windows RWX pages with a synthetic clzero loop, CPU-only emulation test; no device-gated test). Driver: n/a (no graphics driver involved). No game data, assets or extracted shaders were used or attached. AI-assisted: yes, Hy4 flash (verification only). No approval implied; this is a local test report.
First-time contributor

Test report for the original symptom, the parallel run that fails now and then (I changed no code). Not a review of the code.

  • CPU: 13th Gen Intel(R) Core(TM) i7-13700KF (no CLZERO, so the emulated path runs). Windows 11.
  • cargo test -p leonida-loader --lib --locked, default test threads, 25 runs each, same machine, one after the other:
Tree Runs Passed Failed
main 93b9d90a2 25 20 5
head bb9fef768 merged onto 93b9d90a2 (clean merge) 25 25 0
  • All five failures on main are console_insn::tests::a_clzero_loop_becomes_plain_stores, panicking at console_insn.rs:643, the failure this pull request describes.
  • With the merge, each run is 99 passed, 0 failed.
  • 0 of 25 does not prove the failure is gone; at the rate seen on main (5 of 25) that result by chance is under 1%.
  • Not run: clippy, the clean-room check, any in-game run.

GPUs tested: none (CPU-only tests). No game files were used.

AI-assisted: yes (Claude Code, Claude Fable 5.2; verification only).

Test report for the original symptom, the parallel run that fails now and then (I changed no code). Not a review of the code. - CPU: 13th Gen Intel(R) Core(TM) i7-13700KF (no CLZERO, so the emulated path runs). Windows 11. - `cargo test -p leonida-loader --lib --locked`, default test threads, 25 runs each, same machine, one after the other: | Tree | Runs | Passed | Failed | |---|---|---|---| | `main` `93b9d90a2` | 25 | 20 | 5 | | head `bb9fef768` merged onto `93b9d90a2` (clean merge) | 25 | 25 | 0 | - All five failures on `main` are `console_insn::tests::a_clzero_loop_becomes_plain_stores`, panicking at `console_insn.rs:643`, the failure this pull request describes. - With the merge, each run is 99 passed, 0 failed. - 0 of 25 does not prove the failure is gone; at the rate seen on `main` (5 of 25) that result by chance is under 1%. - Not run: clippy, the clean-room check, any in-game run. GPUs tested: none (CPU-only tests). No game files were used. AI-assisted: yes (Claude Code, Claude Fable 5.2; verification only).
Sign in to join this conversation.
No description provided.