fix(loader,victory): atomic Clwb patch, unwind-safe LoaderLock/LateDlls rollback, and profile Thread RAII (#321) #350

Manually merged
github merged 1 commit from github-bot/gh-350 into gh-pr/350/base 2026-12-02 15:34:10 +00:00 AGit
Owner

@⁠Androtim on GitHub · 2026-12-02 15:04 UTC · original
Androtim:fix/loader-clwb-lock-profile → main · +173 −45 · 3 files · 1 commits · merged 2026-12-02 15:33 UTC

Fixes #321

Summary

  1. Atomic \ConsoleOnly::Clwb\ patching (\crates/victory-loader/src/console_insn.rs):
    • Extracted \patch_bytes\ (16-byte aligned \cmpxchg16b\ loop + \VirtualProtect\ + \FlushInstructionCache) and routed \ConsoleOnly::Clwb\ through it instead of non-atomic bytewise .fill(0x90). Falls back cleanly to stepping over the instruction without patching if it crosses a 16-byte boundary or lacks \cmpxchg16b.
    • Added unit test \patch_bytes_handles_clwb_alignment_atomically.
  2. \LoaderLock::hold\ unwind safety & \LateDlls::load\ rollback (\pps/victory/src/run.rs):
    • Added an RAII \Guard\ in \LoaderLock::hold\ so the re-entrant lock is always decremented/released and notified even if \ ()\ unwinds.
    • Added rollback (\ orget_image\ + \dll.unload()\ after dropping \self.mapped) when \ind_imports\ fails mid-loop in \LateDlls::load.
    • Added unit test
      eenters_on_same_thread_and_releases_on_panic.
  3. Profiler \Thread\ RAII & checked \GetThreadTimes\ (\crates/victory-loader/src/profile.rs):
    • Implemented \Drop for Thread\ to close non-null handles automatically (removing manual \CloseHandle\ inside \write()).
    • Changed \cpu()\ to return \Option\ checking \GetThreadTimes != 0\ and switched delta/total subtractions in \choose()\ and \write()\ to \saturating_sub.
    • Added unit test \invalid_handle_reports_no_cpu_and_null_thread_drops_cleanly.

Verification (local \x86_64-pc-windows-gnu)

  • \cargo +1.98.1-x86_64-pc-windows-gnu xtask policy\ -> \324 files ok\
  • \cargo +1.98.1-x86_64-pc-windows-gnu test --workspace --exclude victory-gpu --exclude victory-shader --lib --bins --locked\ -> all pass (\MiniVI\ 47, \ ictory_core\ 15, \ ictory_formats\ 69 + 7 ignored, \ ictory_game\ 3, \ ictory_hle\ 236 + 3 ignored + 1 subprocess, \ ictory_loader\ 63, \ ictory_pe\ 36, \ ictory_pipeline\ 13, \xtask\ 8)
  • \cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-loader --features diagnostics profile --locked\ -> 2 passed
  • Integration tests: \ ictory-loader --test title_dlls\ (1 passed), \ ictory-hle --test no_dll_planting\ (1 passed), \ ictory --test census\ (2 passed)
    (Note: \ ictory-loader --test load_probe\ builds \ ests/fixtures/hle-probe\ which passes MSVC /DELAYLOAD:\ and is verified on Windows MSVC CI.)
  • \cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --locked -- -D warnings\ -> pass
  • \cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --features diagnostics --locked -- -D warnings\ -> pass
**@⁠Androtim** on GitHub · 2026-12-02 15:04 UTC · [original](https://github.com/Androtim/MiniVI/pull/350) `Androtim:fix/loader-clwb-lock-profile` → `main` · +173 −45 · 3 files · 1 commits · merged 2026-12-02 15:33 UTC Fixes #321 ### Summary 1. **Atomic \ConsoleOnly::Clwb\ patching (\crates/victory-loader/src/console_insn.rs\):** - Extracted \patch_bytes\ (16-byte aligned \cmpxchg16b\ loop + \VirtualProtect\ + \FlushInstructionCache\) and routed \ConsoleOnly::Clwb\ through it instead of non-atomic bytewise \.fill(0x90)\. Falls back cleanly to stepping over the instruction without patching if it crosses a 16-byte boundary or lacks \cmpxchg16b\. - Added unit test \patch_bytes_handles_clwb_alignment_atomically\. 2. **\LoaderLock::hold\ unwind safety & \LateDlls::load\ rollback (\pps/victory/src/run.rs\):** - Added an RAII \Guard\ in \LoaderLock::hold\ so the re-entrant lock is always decremented/released and notified even if \ ()\ unwinds. - Added rollback (\ orget_image\ + \dll.unload()\ after dropping \self.mapped\) when \ind_imports\ fails mid-loop in \LateDlls::load\. - Added unit test \ eenters_on_same_thread_and_releases_on_panic\. 3. **Profiler \Thread\ RAII & checked \GetThreadTimes\ (\crates/victory-loader/src/profile.rs\):** - Implemented \Drop for Thread\ to close non-null handles automatically (removing manual \CloseHandle\ inside \write()\). - Changed \cpu()\ to return \Option<u64>\ checking \GetThreadTimes != 0\ and switched delta/total subtractions in \choose()\ and \write()\ to \saturating_sub\. - Added unit test \invalid_handle_reports_no_cpu_and_null_thread_drops_cleanly\. ### Verification (local \x86_64-pc-windows-gnu\) - \cargo +1.98.1-x86_64-pc-windows-gnu xtask policy\ -> \324 files ok\ - \cargo +1.98.1-x86_64-pc-windows-gnu test --workspace --exclude victory-gpu --exclude victory-shader --lib --bins --locked\ -> all pass (\MiniVI\ 47, \ ictory_core\ 15, \ ictory_formats\ 69 + 7 ignored, \ ictory_game\ 3, \ ictory_hle\ 236 + 3 ignored + 1 subprocess, \ ictory_loader\ 63, \ ictory_pe\ 36, \ ictory_pipeline\ 13, \xtask\ 8) - \cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-loader --features diagnostics profile --locked\ -> 2 passed - Integration tests: \ ictory-loader --test title_dlls\ (1 passed), \ ictory-hle --test no_dll_planting\ (1 passed), \ ictory --test census\ (2 passed) *(Note: \ ictory-loader --test load_probe\ builds \ ests/fixtures/hle-probe\ which passes MSVC \/DELAYLOAD:\ and is verified on Windows MSVC CI.)* - \cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --locked -- -D warnings\ -> pass - \cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --features diagnostics --locked -- -D warnings\ -> pass
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 15:06:47 +00:00
github-bot changed target branch from main to gh-pr/350/base 2026-12-02 15:34:08 +00:00
github manually merged commit b7ec97440a into gh-pr/350/base 2026-12-02 15:34:10 +00:00
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/Androtim--MiniVI!350
No description provided.