loader: boot I/O — digest sidecar for the image cache, batched IAT VirtualProtect #3354
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!3354
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3354"
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?
@borosf on GitHub · 2026-11-26 20:07 UTC · original
borosf:vibe/loader-boot-io-842ca5→main· +972 −28 · 3 files · 6 commits · openWhat
Two boot-time I/O costs in
victory-loader, both measured synthetic-only (no game data):publish::write_if_changedre-read the whole cached image (~114 MB per exe/DLL, per boot) just to compare bytes. It now answers "already there" from a 32-byte blake3 sidecar written at publish time, plus a metadata length check (so a deleted image still republishes). The full read+compare remains only as the legacy fallback for caches from before sidecars, and it publishes the sidecar for next time.windows.rs write_slotsdid twoVirtualProtectper IAT slot. It now groups slots by page, asks each page's protection once (VirtualQuery), merges consecutive same-protection pages into runs, and does one unprotect + one restore per run — splitting where protection changes (delay-load tables in.didat), so per-section protections are preserved exactly.Claimed on #1633 (comment 7202184435).
Measurement (Windows 11 x64, Intel Arc B580 host, synthetic tempfile/VirtualAlloc only)
New tests print the numbers (
cargo test -p victory-loader --locked -- --test-threads=1 --nocapture slots_tests publish):Correctness tests: sidecar hit does not rewrite (mtime unchanged); deleted image under a matching sidecar republishes; changed bytes republish and move the sidecar; legacy cache without sidecar falls back to read+compare and publishes the sidecar; 500 slots across 3 pages with mixed protections are all written and each page's original protection restored (incl. a
PAGE_READONLYmiddle page forcing a run split).Verification
cargo test -p victory-loader --locked --no-fail-fast— all targets ok (37+1+2+1 passed, 0 failed)cargo clippy --workspace --all-targets --locked -- -D warningsand--all-features— cleanrustfmt --config-path rustfmt.toml --edition 2024 --check— windows.rs 0 hunks (baseline 0), publish.rs 1 hunk (baseline 2; only pre-existing hunks remain)cargo xtask policy— 193 files okNo game data used anywhere; synthetic tests only.
@AurumCarlos on GitHub · 2026-11-26 20:18 UTC · original · reviewed
The IAT batching looks right. The sidecar can end up describing a different image than the one on disk, though, and the fast path then trusts it:
write(sidecar, digest(A))lands last, the cache holds image B with sidecar A. A later boot whose bytes are A then gets "already there" (digest matches, and length matches if A and B are the same size, which is common for re-prepared images) and runs with B. A crash between the rename and the sidecar write leaves the same mismatch.@borosf on GitHub · 2026-11-26 20:34 UTC · original
Fixed at
9f8174f— exactly the race you described. Publishers of one path are now serialized with a per-image lock file (LockFileExon.{name}.lock, released when the handle closes, so a crashed publisher doesn't keep the cache locked), and the sidecar is removed before the atomic rename and written after it. Invariant: a sidecar on disk always describes the file on disk; a crash between rename and sidecar write leaves no sidecar, and the next boot takes the full read+compare fallback. The fast path itself is unchanged (sidecar digest + length match).Regression test
concurrent_publishers_keep_the_sidecar_matching_the_file: 4 threads × 50 alternating publishes; after each, if the file is unchanged across the check, a present sidecar must equal the file's digest. (The first version of that assert raced with publishers mid-rename; it now only asserts when the file is stable across both reads.)Verification at
9f8174f(Windows 11, B580):cargo test -p victory-loader --locked --no-fail-fastall ok (38 lib tests incl. the race test); workspace clippy clean with-D warningsdefault and--all-features;cargo xtask policyok; fmt gate 0 hunks on publish.rs;Cargo.lockunchanged (feature-only addition of windows-sysWin32_Storage_FileSystem/Win32_System_IO). Synthetic tests only; no game data.@borosf on GitHub · 2026-11-26 20:56 UTC · original
CI caught a real one at
9f8174f: the sidecar was written with plainfs::write(truncate-then-write), so a concurrent reader saw a torn/empty digest (left: []), and the race test itself could fail its reads with AccessDenied while another publisher renamed over the file. Fixed atad0fb99:write_sidecar), so readers only ever see a complete old or new digest;Re-verified at
ad0fb99(Windows 11, B580): the race test passes 5/5 runs at--test-threads=8(the CI condition), full loader suite ok, workspace clippy clean-D warningsdefault +--all-features, fmt gate at baseline (2 hunks),cargo xtask policyok. Synthetic tests only; no game data.@fuzeofficlal on GitHub · 2026-11-26 21:21 UTC · original · requested changes
Moving sidecar writes to staging + rename (
write_sidecarinad0fb998) cleanly fixes torn/truncated sidecar reads, andwindows.rs's batchedwrite_slotsplus all 38+4victory-loadertests andclippychecks pass. However, two concrete edge cases incrates/victory-loader/src/publish.rsstill allow a stale.blake3sidecar to survive beside a newer image of the same length, causing a subsequentwrite_if_changedcall to hit the fast path and silently serve the wrong image:Unlocked legacy-fallback sidecar write races with
publish_lock(crates/victory-loader/src/publish.rs:123-128):This fallback runs completely outside
publish_lock. Ifpathholds imageAwith no sidecar (either a legacy cache or the window after another publisher removes the sidecar at line 142), publisher 1 can readAat line 123, pause while publisher 2 acquirespublish_lock, renamesBoverpath, publisheshash(B), and releases the lock, and then publisher 1'swrite_sidecar(path, &digest)at line 126 atomically overwrites the sidecar withhash(A).ad0fb998with two concurrent threads publishing 32 MiB imagesA(0x11) andB(0x22): disk ended withB(0x22) beside sidecarblake3(A), and a subsequentwrite_if_changed(&path, &a)returnedOk(())via the fast path while leavingB(0x22) on disk.Ignoring non-
NotFounderrors onremove_file(sidecar)(crates/victory-loader/src/publish.rs:142, 149):If an external handle (e.g., an antivirus scanner or indexer without
FILE_SHARE_DELETE | FILE_SHARE_WRITE) keeps the sidecar open whilewrite_if_changedpublishesB,remove_filefails withERROR_SHARING_VIOLATIONand is silently discarded,rename(&staging, path)replaces the image withB, andwrite_sidecar'srename(&staging, &side)at line 149 also fails silently.ad0fb998: holding.locked.bin.blake3open withGENERIC_READ+FILE_SHARE_READ,write_if_changed(&path, &b)returnedOk(())withB(0xbb) on disk andblake3(A)still in the sidecar; a subsequentwrite_if_changed(&path, &a)then hit the fast path and silently leftB(0xbb) on disk.Suggested fix:
L123-128), acquirepublish_lock(path)?before re-checkingpath(or only backfill the sidecar under_lockafter confirmingpathstill matchesbytes).remove_file(sidecar(path))error other thanErrorKind::NotFound(cleaning upstagingon error) before renamingstagingoverpath, so the invariant "whenever a sidecar exists, it describes the file on disk" holds unconditionally.@borosf on GitHub · 2026-11-26 21:40 UTC · original
Both fixed in
1f8da55(on top ofad0fb99). Thank you for reproducing them — both reproduced here too.1. Legacy fallback sidecar write raced with
publish_lock. The read-back now happens inside the same critical section as the remove/rename/write-after:write_if_changedstages the bytes, takespublish_lock(path), and only then does the read-back-and-backfill. A backfill can no longer land after another publisher replaced the image, because it cannot run while that publisher holds the lock.2.
remove_file(sidecar)errors are no longer dropped. A sidecar that cannot be removed (e.g.ERROR_SHARING_VIOLATIONfrom a reader withoutFILE_SHARE_DELETE) now stops the publication before the rename: the staging file is removed and the error is returned. The image and its sidecar are left matching, and the next attempt retries.Regression tests, each verified to fail on
ad0fb99and pass on1f8da55:a_legacy_sidecar_backfill_waits_for_the_publishers_lock: holdspublish_lockwhile a publisher backfills a legacy cache; asserts the sidecar does not appear until the lock is released. Fails onad0fb99(sidecar appears while the lock is held).a_sidecar_that_cannot_be_removed_stops_the_publish: publishes A, opens the sidecar withGENERIC_READ | FILE_SHARE_READ, publishes B; asserts an error, and that the image still holds A with sidecarblake3(A). Fails onad0fb99(returnsOk(())'with B on disk).Local verification (Windows 11 x64, Arc B580):
cargo test -p victory-loader --lib --locked40 passed / 0 failed (six inpublish::tests);cargo clippy -p victory-loader --all-targets -- -D warningsclean, and with--all-features;cargo xtask policy191 files ok;rustfmt --edition 2024 --checkhunk parity with origin/main (2 = 2). Synthetic files only, no game data.@fuzeofficlal on GitHub · 2026-11-26 22:00 UTC · original · approved these changes
Both stale-sidecar edge cases from the previous review are cleanly fixed in
1f8da553:crates/victory-loader/src/publish.rs:138-145) now executes underpublish_lock(path)?, cleaning upstagingbefore returning, so it can no longer overwrite a newer publisher's sidecar.std::fs::remove_file(sidecar(path))(publish.rs:148-155) now propagates any error other thanErrorKind::NotFound(removingstagingand aborting before the rename), preserving the on-disk invariant when an external handle holds the sidecar without delete sharing.Verification on Windows against
origin/main(63bcca43):cargo xtask policy: ok (191 files ok)cargo clippy --locked -p victory-loader --all-targets -- -D warnings(default and--all-features): cleancargo test --locked -p victory-loader --no-fail-fast: 40 lib + 4 integration tests passed, includinga_legacy_sidecar_backfill_waits_for_the_publishers_lockanda_sidecar_that_cannot_be_removed_stops_the_publish(5,000 IAT slots: 461.6 µs batched vs 18.23 ms per-slot; 64 MB cache hit: 6.93 ms vs 17.72 ms).blake3sidecar matching on every round.@farhankhan8571811-star on GitHub · 2026-11-27 19:15 UTC · original
A data point that may be useful here, from a separate local change that I'm not submitting:
run.rsreads the exe, andvictory_loader::preparecopies it before patching, so it is held twice during preparation. I tried patching the read buffer in place (prepare_owned(Vec<u8>), with the info and hash taken before the first write). Measured on synthetic PEs of 32–256 MB, baseline63bcca4vs that change, read + prepare + load, Windows 11, release builds:write_if_changedreads the published copy back while the prepared bytes are still alive.That warm-cache read-back is what this PR replaces, so the two changes affect the same startup peak. I have not measured them together and am not claiming a combined effect.
Limitations: synthetic fixtures only (no game files), one laptop (Ryzen 7 7445HS, 16 GB), 5–12 runs per case, and noisy cold-cache times from the 114 MB disk write. If it would help once this lands, I can re-measure on top of it.
AI-assisted (Claude Code).
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.