Experimental: Write barriers in the title's code #197
No reviewers
Labels
No labels
accessibility
already-solved
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
merged
needs-decision
needs-game-run
needs-rebase
not-merged
nvidia
obsolete
question
security
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/avatarmanz--VIctory-7900XTX!197
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-197"
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?
@IANSYT-dev on GitHub · 2026-11-26 11:18 UTC · original
IANSYT-dev:barriers→main· +7276 −14 · 26 files · 5 commits · openWrite barriers in the title's code (
VICTORY_BARRIERS=1, off by default)Not yet run with the title. Everything below is what the code does and what its tests measure on one PC (Ryzen 5 7600, Windows 10), not a measurement of the game. With the switch off nothing changes: the new hooks return at once.
What it does
View memory is guarded in 64 KiB blocks, and every first write to a guarded block faults. With barriers on, the first fault at an instruction also patches that instruction: it jumps to a trampoline that does the same store through our own view of the page (
inplace::beside) and marks what it wrote. From then on that instruction never faults, the block stays guarded and clean, and the next asking takes the marks the way it takesput_beside's bits. It istitlewrite's store beside the guard, decided once per instruction instead of at every fault.jmpover it, by a short jump toint3padding nearby, or by moving the instructions after it (or its block) into the trampoline where the scan proves nothing branches into them.int3sites are off by default (VICTORY_BARRIER_INT3=1): anint3traps at every execution, a guard faults once per block per refresh. Anything refused faults as before. Threads stopped inside moved code are moved to its copy; trampolines carry unwind data. A function with a direct jump out of the image (another layer's patch, such as VIctory'swrite_sitesanddescriptor_sitesstubs) is never moved in, and what follows such a jump counts as a branch target.watch::guard_parts), so a refresh copies those parts instead of the block, as it already does for the parts the write watch names. A blockput_besidewrote, or a fault opened, is told whole.memcpy/memmove/memset. With barriers on they aretitlewrite's wrappers, and a piece into a block with a delta goes beside the guard by it and marks its parts: no lock, no share per block.<cache>/barriers/sites.txt(image name, PE time stamp, offset, instruction hash; no code) and the next run patches them as soon as the image is scanned, all under one stop. A line of another build, or whose instruction changed, is left out.Details, switches and what is not done:
docs/barriers.md.Changes to existing code
watch.rson_writeasksbarrier::on_faultbeforetitlewrite::store;publishedandguardtake the marks first;guard_partstells barrier-written blocks by parts;put_besideflags its blocks whole (BESIDE_WHOLE);title_memory_changed/title_memory_freedclear deltas;Bits::set_all. (+213, ~140 of it a test)inplace.rsviews_of_beside, and a table of our views by address kept withwhole(+45)titlewrite.rsapps/victory/src/run.rslib.rs,Cargo.toml,victory-hleNew:
crates/victory-barrier(the patcher, from a separate prototype),victory-gpu/src/barrier.rs(the glue),docs/barriers.md. New dependency:iced-x86(MIT).Verified
crates/victory-barrier/tests/difftest.rs): each instruction the classifier accepts is run as the original and as its trampoline from the same random state; registers, flags, vector state, memory, faults and the marks (taken at 4 KiB parts, as an asking takes them) must agree, and three ways of breaking a trampoline on purpose (marks lost, flags changed, store skipped) must be seen. Default run: this test's code and three Windows DLLs, 250 forms. Every instance of Windows' DLLs and the prototype's stand-in title: 307 forms, 117,862 instances, 471,368 paired runs, no difference. Any images:VICTORY_BARRIER_DIFFTEST=<paths> cargo test -p victory-barrier --release --test difftest -- --ignored.watch(VICTORY_BARRIERS=1 cargo test -p victory-gpu --lib barrier::tests -- --ignored, alone: it patches the test binary): a store into guarded view memory faults once, is patched, and from then on its block stays read-only, the bytes are at both views of a section shown twice,writestells the block at both, the journal names the one 4 KiB part written, and the sites file holds it.victory-gpu's tests withVICTORY_BARRIERS=1pass as without (one,ranges_past_the_address_space_end_do_not_overflow, found a loop in clearing deltas, fixed).main,--no-fail-fast: the same 38 tests fail in the default run and the same 42 with all features, on both (GPU and shader tests, the loader's probe, net and kernel tests, on this PC); this branch passes 14 more (its own). Clippy: no warning in the new code;main'stransmutewithout annotations invictory-loader/src/descriptor_sites.rs:277fails the job on both, left alone here.Measured (synthetic)
VICTORY_BARRIERS=1 cargo test -p victory-gpu --release --lib copies_three_ways -- --ignored --nocapture: copies of 256 bytes into 64 guarded blocks of a section shown twice, then a refresh of both views as a submission makes it (Tracked::take).put_besideThe askings cost a little more than
put_beside's (the marks are turned into bits on the asking thread; ~14 us for 128 blocks here), against megabytes not copied, sent up or hashed again.Expectations
Modest: the frame is bound by the submitting thread and the card, and barriers act on part of each (faults and re-guarding, and the bytes refreshes copy for view memory). Memory the write watch tracks is untouched (below).
Not done
VirtualProtectof view memory: a block it makes read-only keeps its delta, so a store it expects to fault goes beside.A possible next step: write-watched memory
Not in this PR.
XMemVirtualAlloc's memory is private and write-watched, so barriers have nowhere to store beside a guard there (private memory cannot be mapped a second time). Made of sections the wayxmemalready makes physical pages, it would be view memory, and the guards,inplace::beside,kernel_writing, barriers and parts would take it as they are; only what copies take would be guarded, so memory the card never reads would not fault. That would replace its askings (5.4 ms a frame on the submitting thread, 4.4 of it in the kernel). Open, and a run or your logs would answer it: a barrier costs every patched store into it some 25 instructions where the write watch costs nothing (some 29 MB a frame are written there); whether the title decommits inside those allocations (a view cannot be decommitted, so it would have to be emulated); Wine would keep the write watch;VICTORY_INPLACE's private-memory heaps would not open. Details indocs/barriers.md.A first run should look at
barrierslog line (every 10 s): sites patched by strategy, refusals (Int3Offthe most likely), faults seen, blocks given a delta, copy pieces put beside.VICTORY_FAULT_BOOK) and frame times against the same scene without the switch; anything stale on screen.patched_ahead, and fewer faults in the first minutes.@IANSYT-dev on GitHub · 2026-11-26 11:21 UTC · original
It was already mentioned in the PR body but this has not been tested with any real games as I don't have any dumped games to test with. However it has been tested quite extensively with synthetic tests and it will probably work. I would super appreciate any testing from anyone who does have any game files to test with, specifically I'd love to know how it affects performance. Thanks!
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.