Loader: VICTORY_PATCH writes bytes into a loaded module after relocation #1059
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!1059
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1059"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Halfect on GitHub · 2026-11-24 15:22 UTC · original
Halfect:fix/loader-patch-knob→main· +303 −6 · 7 files · 2 commits · openWhat
Implements the knob requested in #917:
VICTORY_PATCH=<module>:<rva>:<hex bytes>,...writes bytes into a loaded module after the Windows loader has mapped and relocated it, before TLS redirection and import binding.victory-loaderwith the same comma/hex conventions as the other debugging knobs (VICTORY_WATCHetc.); malformed or out-of-image entries are skipped with a warning.load(), afterLoadLibraryExWhas mapped and relocated the image and before TLS redirection and import binding — the earliest point where every patch site named in #917 is in its final, relocated state.size_of_image; the target pages are temporarily made writable withVirtualProtectand restored afterwards; every applied write is logged at info level with the bytes it replaced, so a run is reproducible from its own log.Fixes #917.
Checks
cargo test --locked -p victory-loader: 30 passed, 0 failed (5 new tests cover parsing, malformed entries, module and bounds matching, and write semantics).cargo clippy --locked -p victory-loader --all-targets -- -D warnings: clean.cargo xtask policy: 155 files ok.CPU-only validation; no title executable, world run, or game data was used. AI-assisted (Kimi).
@Zipettte on GitHub · 2026-11-24 16:30 UTC · original
Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 3530ba9:
cargo test -p victory-loaderpasses, 30 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).Tested with AI assistance (Claude).
@Halfect on GitHub · 2026-11-24 16:50 UTC · original
Thanks @Zipettte for the quick verification on the RX 7700 XT — good to have the loader suite green on a second Windows setup alongside my run. Nothing further from my side on this head; ready for review.
@Marvisak on GitHub · 2026-11-25 01:25 UTC · original · edited 2026-11-26 02:17 UTC
Automated triage for the maintainers.
Merge candidate. Re-reviewed at
d2cff77, which merges cleanly intomain. This adds the diagnostics-onlyVICTORY_PATCH=<module>:<rva>:<hex bytes>knob (#917). The out-of-bounds bug fuzeofficlal found is fixed:writeincrates/victory-loader/src/patch.rsnow writes at the start of the sliceapplyhands it (already offset to the rva), and the unit test passes that offset slice.applynow raises the protection toPAGE_EXECUTE_READWRITE, restores it through a drop guard, and callsFlushInstructionCache, astls.rsandthunks.rsalready do.hexnow rejects a leading+. The bounds check (checked_addagainstsize_of_image), the matching on the original module name, and theall(windows, feature = "diagnostics")gating are unchanged. No new dependencies: thewindows-sysfeatures it uses are already enabled.For the maintainer: CI runs
cargo testwithout--all-features, so the integration testpatch_lookup_uses_the_original_module_nameis not run by CI. The author reports it passing locally with--all-features.CI: passing.
Generated by Claude Code
@Halfect on GitHub · 2026-11-25 02:10 UTC · original
Addressed all three triage items at 70eea30:
mod patchand theapplycall are now gated with#[cfg(all(windows, feature = "diagnostics"))].0x1234and0x234).VICTORY_POKE's repeated writes.Validation on macOS after rebasing onto
940d32f:patch.rsunit tests pass.cargo xtask policy: 183 files ok.cargo test --locked -p victory-loaderhas the same 2 passes and 3preparefailures on this branch and on unmodified main; those tests reject the Mac test binary as a non-PE image. Windows cross-Clippy could not run becauseml64.exeis unavailable.No title executable, world run, or game data was used.
@Calvix2k on GitHub · 2026-11-25 03:36 UTC · original
Independent run on Windows 10 19045, your head
70eea30:cargo test -p victory-loader --lib— 34 passed, 0 failed, including the new patch-parsing tests.Reading the write path: it takes
PAGE_READWRITEfor the range, writes, and restores the previous protection fromVirtualProtect's out parameter rather than assuming a value — that is the part that usually goes wrong with code patches, and it is right here. Theall(windows, feature = "diagnostics")gate matches the other loader knobs, and the README row says what it does without promising more.@Halfect on GitHub · 2026-11-25 06:04 UTC · original
Updated the loader to pass the title's original exe/DLL name through every
loadcall, rather than using the prepared copy's content-hashed file name forVICTORY_PATCHlookup.Added a Windows integration regression with two entries for the same export RVA: one names
GaMe.ExEand returns 42, while the other namesprepared.file_name()and returns 99. The old lookup takes the hashed entry; the fixed lookup must apply the original-name entry case-insensitively. The test restoresVICTORY_PATCHon exit.Local: the five
patch.rsunit tests andcargo xtask policy(183 files) pass. The first pushed head failed default Clippy because the new argument is diagnostics-only; that unused-variable path is fixed in this head. Windows CI is running on the updated commit.@Calvix2k on GitHub · 2026-11-25 08:14 UTC · original
Re-verified on the new head
a464e74, Windows 10 19045:cargo test -p victory-loader --lib— 34 passed, 0 failed, including the integration regression where the patch entry names the title's ownGaMe.ExErather than the prepared copy's hashed file name.That naming detail matters:
VICTORY_PATCHis documented with module file names, and matching the prepared copy would have made the knob silently do nothing for the title itself. Good catch to fix it here rather than in a follow-up.@Halfect on GitHub · 2026-11-25 09:53 UTC · original
Thanks @Calvix2k for re-verifying
a464e74on Windows 10 — good to have the 34-test loader suite green on the final head, including the original-name lookup regression. Nothing further from my side; ready for review.@Halfect on GitHub · 2026-11-25 17:24 UTC · original
Rebased onto current main
13f91caand pushed7f31230; theapps/victory/src/run.rsconflict resolved by keeping both sides — theload(&prepared, &images, &exe_name)call with the title's original name, followed by main's newdrop(prepared)once the image is mapped. The branch merges cleanly again.Validation on
7f31230(Windows, CPU-only):cargo test --locked -p victory-loader: 34 passed, 0 failed (lib), plus the integration suites green, including the original-nameVICTORY_PATCHlookup regression.cargo clippy --locked -p victory-loader -p victory --all-targets -- -D warnings: clean.cargo xtask policy: 185 files ok.git diff --check: clean.No title executable, no game data. AI-assisted (Kimi).
@Halfect on GitHub · 2026-11-25 18:46 UTC · original
Looked at the
checkfailure on7f31230. I could not find a code fault: reproducing all three CI steps locally on that exact head (Windows, Rust 1.98.1, the pinned toolchain):cargo clippy --workspace --all-targets --locked -- -D warnings: clean.cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean — including the diagnostics-gatedpatch.rsand thepatch_lookup_uses_the_original_module_nameintegration test the triage flagged.cargo test --workspace --locked: all green for the crates this PR touches —victory-loader34 lib + theload_probe/crash_log/title_dllsintegration suites,victoryapp, and xtask (repository_passes_policyincluded). The only failures anywhere on this machine are pre-existing D3D12-device environment issues that reproduce identically on unmodified main13f91cahere (threevictory-gpudevice tests, e.g.dynamic_slots_are_held_until_releasedfailing an assertion about the descriptor heap size, plus the knownvictory-shaderdispatch tests); this PR changes novictory-gpuorvictory-shadercode.The failure also did not reproduce in timing: the CI job died in 2m 30s, before the test step could plausibly have run, which points at the runner environment (toolchain setup or cache) rather than the tree. I have re-pushed the identical tree as
0a96bb2to re-run CI. If it fails again, a look at which step the job log shows would be appreciated — everything the workflow runs passes on this head locally.@Halfect on GitHub · 2026-11-25 19:05 UTC · original
CI is green on the re-pushed head:
checkpassed on0a96bb2(identical tree to7f31230) in 2m 14s — run 45678494670. The earlier failure was the runner environment, as suspected; no code change was needed. Ready for review.@fuzeofficlal on GitHub · 2026-11-26 01:25 UTC · original
Code review and Windows 11 verification at head
0a96bb2f78bamerged ontomain(63bcca43c6a0):Two blocking bugs found in
apply()(crates/victory-loader/src/patch.rs), including a live panic reproducing in--all-featurestests:patch.rva > 0(patch.rs:77-81, 122):write(image: &mut [u8], patch: &Patch)slicesimage[patch.rva..patch.rva + patch.bytes.len()](expecting the whole image slice), whereasapply()line 122 passes alen = patch.bytes.len()slice starting atbase + patch.rva:std::slice::from_raw_parts_mut(at as *mut u8, len).cargo test --locked -p victory-loader --all-featuresreproduces this immediately intests/load_probe.rs::patch_lookup_uses_the_original_module_name:panicked at cratesictory-loader\src\patch.rs:78:20: range start index 4512 out of range for slice of length 6write()to take the already-offset sliceimage[..patch.bytes.len()]orapply()to pass the full image slice resolves the panic.FlushInstructionCacheand execute-bit stripping (patch.rs:106-133):apply()writes executable code patches (asload_probeexercises by patchingprobe_pidand calling it immediately) without callingFlushInstructionCache(GetCurrentProcess(), ...), unliketls.rs:76and#2659.PAGE_READWRITEalso strips execute permission while patching and skips restoration if a panic unwinds; preserving execute bits (PAGE_EXECUTE_READWRITE) and restoring via a drop guard is safer.hex()(patch.rs:23) usesusize::from_str_radix(..., 16), which accepts a leading+(e.g."0x+10").cargo xtask policy(192 files ok),clippy(0 warnings), defaultvictory-loadertests 39/39 passed,--all-featuresFAILED onpatch_lookup_uses_the_original_module_name(patch.rs:78:20). (Also confirmed clean merge composition with#2659.)@Halfect on GitHub · 2026-11-26 02:10 UTC · original
Thanks @fuzeofficlal — all three findings were real, fixed in
d2cff77.1. Out-of-bounds panic.
writereceived a slice already offset to the patch's rva (from_raw_parts_mut(base + rva, bytes.len())) yet indexed it atrvaagain — every entry withrva > 0panicked, exactly as your--all-featuresrun showed.writenow writes at the start of the target slice (target[..patch.bytes.len()]), and its unit test passes the offset slice, matching whatapplyhands it. Your reproducerpatch_lookup_uses_the_original_module_name(rva0x11a0) now passes.2. Protection and instruction cache.
applynow lifts the range toPAGE_EXECUTE_READWRITE(the target is code; the execute bit stays on while writable), restores the previous protection through a drop guard so an unwind cannot leave the page writable, and callsFlushInstructionCache(GetCurrentProcess(), ...)after the write — the same patterntls.rsuses for its code rewriting.3.
hex()accepting+.from_str_radixtakes a leading+sign, so"0x+10"parsed as valid.hexnow requires ASCII hex digits only after the optional0x;skips_malformed_entriescovers both+10and0x+10.Validation on
d2cff77(Windows 11, Rust 1.98.1 MSVC, CPU-only):cargo test --locked -p victory-loader --all-features: 41 passed, 0 failed (lib) plus all integration suites green — including the patch test that panicked on0a96bb2.cargo test --locked -p victory-loader(default features): 34 passed, 0 failed, integration suites green.cargo clippy --locked -p victory-loader -p victory --all-targets -- -D warningsand the same with--all-features: clean (fresh check aftercargo clean -p).cargo xtask policy: 185 files ok.git diff --check: clean.The branch still merges cleanly onto current main
63bcca4(verified withgit merge-tree --write-tree). No title executable, no game data. AI-assisted (Kimi).View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.