Experimental: Write barriers in the title's code #197

Open
github-bot wants to merge 5 commits from github-bot/gh-197 into main AGit
Owner

@⁠IANSYT-dev on GitHub · 2026-11-26 11:18 UTC · original
IANSYT-dev:barriers → main · +7276 −14 · 26 files · 5 commits · open

Write 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 takes put_beside's bits. It is titlewrite's store beside the guard, decided once per instruction instead of at every fault.

  • Patching. Only the title's images (and title DLLs it loads later), and only after a scan of their code on a thread of its own. A store reaches its trampoline by a jmp over it, by a short jump to int3 padding nearby, or by moving the instructions after it (or its block) into the trampoline where the scan proves nothing branches into them. int3 sites are off by default (VICTORY_BARRIER_INT3=1): an int3 traps 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's write_sites and descriptor_sites stubs) is never moved in, and what follows such a jump counts as a branch target.
  • Deltas and marks. A block gets its delta (our view's address minus the title's) at its first write fault, cleared when the title's mapping there changes. Marks go by our view's address, so a write through any view of a page is told at every view; four levels (4 KiB part, block, 4 MiB, GiB), so taking them when there are none reads 64 bytes per 64 GiB slot.
  • Parts. A block written only beside its guard is told with the 4 KiB parts written (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 block put_beside wrote, or a fault opened, is told whole.
  • The title's memcpy/memmove/memset. With barriers on they are titlewrite'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.
  • Sites remembered between runs. Patching stops the other threads for a moment, so the sites patched go to <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

File
watch.rs on_write asks barrier::on_fault before titlewrite::store; published and guard take the marks first; guard_parts tells barrier-written blocks by parts; put_beside flags its blocks whole (BESIDE_WHOLE); title_memory_changed/title_memory_freed clear deltas; Bits::set_all. (+213, ~140 of it a test)
inplace.rs views_of_beside, and a table of our views by address kept with whole (+45)
titlewrite.rs the wrappers bound when barriers are on; a piece goes beside by a block's delta where there is one (31 lines)
apps/victory/src/run.rs starts the scan after the other code patches; late title DLLs registered (+18)
lib.rs, Cargo.toml, victory-hle the module, the dependency, the re-export

New: 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

  • Every store form against the CPU (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.
  • Through 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, writes tells the block at both, the journal names the one 4 KiB part written, and the sites file holds it.
  • victory-gpu's tests with VICTORY_BARRIERS=1 pass as without (one, ranges_past_the_address_space_end_do_not_overflow, found a loop in clearing deltas, fixed).
  • CI's commands on this branch and on 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's transmute without annotations in victory-loader/src/descriptor_sites.rs:277 fails 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).

plain (fault, open, guard again) put_beside barriers
a copy, 1 a block, 1 thread 2.68 us 0.112 us 0.098 us
a copy, 64 a block, 1 thread 0.080 us 0.081 us (share used up: opened) 0.049 us
a refresh takes, 1 copy a block 8192 KiB 8192 KiB 512 KiB
a refresh takes, 64 copies a block (16 KiB) 8192 KiB 8192 KiB 2560 KiB

The 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

  • Memory the write watch tracks (below).
  • The title's own VirtualProtect of 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 way xmem already 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 in docs/barriers.md.

A first run should look at

  • The barriers log line (every 10 s): sites patched by strategy, refusals (Int3Off the most likely), faults seen, blocks given a delta, copy pieces put beside.
  • Write faults a frame (VICTORY_FAULT_BOOK) and frame times against the same scene without the switch; anything stale on screen.
  • A second run: the scan line's patched_ahead, and fewer faults in the first minutes.
**@⁠IANSYT-dev** on GitHub · 2026-11-26 11:18 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/197) `IANSYT-dev:barriers` → `main` · +7276 −14 · 26 files · 5 commits · open ## Write 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 takes `put_beside`'s bits. It is `titlewrite`'s store beside the guard, decided once per instruction instead of at every fault. - **Patching.** Only the title's images (and title DLLs it loads later), and only after a scan of their code on a thread of its own. A store reaches its trampoline by a `jmp` over it, by a short jump to `int3` padding nearby, or by moving the instructions after it (or its block) into the trampoline where the scan proves nothing branches into them. `int3` sites are off by default (`VICTORY_BARRIER_INT3=1`): an `int3` traps 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's `write_sites` and `descriptor_sites` stubs) is never moved in, and what follows such a jump counts as a branch target. - **Deltas and marks.** A block gets its delta (our view's address minus the title's) at its first write fault, cleared when the title's mapping there changes. Marks go by our view's address, so a write through any view of a page is told at every view; four levels (4 KiB part, block, 4 MiB, GiB), so taking them when there are none reads 64 bytes per 64 GiB slot. - **Parts.** A block written only beside its guard is told with the 4 KiB parts written (`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 block `put_beside` wrote, or a fault opened, is told whole. - **The title's `memcpy`/`memmove`/`memset`.** With barriers on they are `titlewrite`'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. - **Sites remembered between runs.** Patching stops the other threads for a moment, so the sites patched go to `<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 | File | | |---|---| | `watch.rs` | `on_write` asks `barrier::on_fault` before `titlewrite::store`; `published` and `guard` take the marks first; `guard_parts` tells barrier-written blocks by parts; `put_beside` flags its blocks whole (`BESIDE_WHOLE`); `title_memory_changed`/`title_memory_freed` clear deltas; `Bits::set_all`. (+213, ~140 of it a test) | | `inplace.rs` | `views_of_beside`, and a table of our views by address kept with `whole` (+45) | | `titlewrite.rs` | the wrappers bound when barriers are on; a piece goes beside by a block's delta where there is one (31 lines) | | `apps/victory/src/run.rs` | starts the scan after the other code patches; late title DLLs registered (+18) | | `lib.rs`, `Cargo.toml`, `victory-hle` | the module, the dependency, the re-export | New: `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 - **Every store form against the CPU** (`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`. - **Through `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, `writes` tells the block at both, the journal names the one 4 KiB part written, and the sites file holds it. - **`victory-gpu`'s tests with `VICTORY_BARRIERS=1`** pass as without (one, `ranges_past_the_address_space_end_do_not_overflow`, found a loop in clearing deltas, fixed). - **CI's commands on this branch and on `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`'s `transmute` without annotations in `victory-loader/src/descriptor_sites.rs:277` fails 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`). | | plain (fault, open, guard again) | `put_beside` | barriers | |---|---:|---:|---:| | a copy, 1 a block, 1 thread | 2.68 us | 0.112 us | 0.098 us | | a copy, 64 a block, 1 thread | 0.080 us | 0.081 us (share used up: opened) | 0.049 us | | a refresh takes, 1 copy a block | 8192 KiB | 8192 KiB | **512 KiB** | | a refresh takes, 64 copies a block (16 KiB) | 8192 KiB | 8192 KiB | **2560 KiB** | The 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 - Memory the write watch tracks (below). - The title's own `VirtualProtect` of 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 way `xmem` already 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 in `docs/barriers.md`. ### A first run should look at - The `barriers` log line (every 10 s): sites patched by strategy, refusals (`Int3Off` the most likely), faults seen, blocks given a delta, copy pieces put beside. - Write faults a frame (`VICTORY_FAULT_BOOK`) and frame times against the same scene without the switch; anything stale on screen. - A second run: the scan line's `patched_ahead`, and fewer faults in the first minutes.
A store of the title's that faults on guarded view memory is patched once
(new crate victory-barrier) to jump to a trampoline that stores beside the
guard, through our own view of the page, and marks the block; the block
stays guarded and clean, and the next asking takes the marks as it takes
put_beside's bits, at every view of the page. A block gets its delta at its
first write fault and loses it when the title's mapping there changes. Only
the title's images are patched, after a scan on a thread of its own; any
store the patcher refuses faults as before.

Hooks: watch's on_write (before titlewrite::store), published and guard
(the marks first), title_memory_changed and title_memory_freed (deltas
cleared); inplace::views_of_beside; run.rs starts the scan after the other
code patches. Tests: victory-barrier's, and barrier::tests (VICTORY_BARRIERS=1,
--ignored, alone). Not yet run with the title: docs/barriers.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- int3 sites only with VICTORY_BARRIER_INT3=1: by default a store only an
  int3 could reach is refused (Int3Off) and faults as before (an int3 traps
  at every execution, a guard once a block per refresh). Test: int3_off.
- With barriers on, the title's memcpy/memmove/memset are titlewrite's
  wrappers, and a piece into a guarded block goes beside the guard by the
  block's delta, then marks it: no lock, no share per block. Measured
  against a fault and against put_beside: watch::tests::copies_three_ways
  (docs/barriers.md).
- The marks' views found by a table kept with inplace's views; BESIDE and
  PUBLISHED set a word at a time.
- tables::clear of a range to the end of the address space looped forever
  (watch's ranges_past_the_address_space_end_do_not_overflow hung with
  barriers on): bounded to user space and to slots with deltas. victory-gpu's
  tests now pass with VICTORY_BARRIERS=1 as without.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Barriers, their string helper and the title's copies mark the 4 KiB parts
they write (a fourth level of marks, taken with the others). A block that
stayed guarded and was written only beside its guard by them is told with
those parts (watch::guard_parts), so the journal holds them and a refresh
copies them, as it does for the parts the kernel's write watch names; a
block put_beside wrote (BESIDE_WHOLE) or a fault opened is told whole.

Measured (watch::tests::copies_three_ways, 64 blocks shown twice, refreshed
through Tracked::take): 512 KiB a frame taken against 8192 for one 256-byte
copy a block, 2560 against 8192 for 16 KiB a block. Tested: the journal
names the one part a patched store wrote (barrier::tests); victory-gpu's
tests pass with VICTORY_BARRIERS=1 as without.

docs/barriers.md: the write-watched memory, what making it of sections would
take and what is not known.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- tests/difftest.rs: every store form the classifier accepts, run as the
  original instruction and as its trampoline from the same random state;
  registers, flags, vector state, memory, faults and the marks (taken at
  4 KiB parts) must agree, and three ways of breaking a trampoline must be
  seen. Windows' DLLs and the prototype's stand-in title: 307 forms, all
  117,862 instances, 471,368 paired runs, no difference
  (VICTORY_BARRIER_DIFFTEST=<images>, --ignored, for any images).
- Its debug build found a jump table whose bound is compared to a register
  read as a bound of 0 (one entry, its other targets missed, so code could be
  moved where the switch jumps): such a table is now read unbounded.
- Patching split into staging and committing; Patcher::patch_many writes any
  number of sites under one stop of the other threads, allocating nothing
  meanwhile. The sites patched are written to <cache>/barriers/sites.txt
  (image name, PE time stamp, offset, instruction hash; no code) and patched
  ahead at the next run once the image is scanned, where build and hash still
  match. Test: ahead.
- Images are scanned outside the patcher's lock (faults meanwhile go the
  guards' way) and patched from then on; title DLLs the title loads itself
  are registered before their DllMain and scanned the same way.
- The prototype's survey, shape printer and dbghelp lookup are gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:22:17 +00:00
Author
Owner

@⁠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!

**@⁠IANSYT-dev** on GitHub · 2026-11-26 11:21 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/197#issuecomment-7190631116) 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!
watch.rs: renderer port 6's guard_shared (VICTORY_PORT_GUARD=1) tells
barrier-written blocks by their 4 KiB parts as guard_parts does; writes()
keeps main's fallback for memory that is no view after all;
title_memory_changed and title_memory_freed clear barrier deltas before
main's view bookkeeping.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This pull request can be merged automatically.
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 origin '+refs/pull/197/head:github-bot/gh-197'
git switch 'github-bot/gh-197'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-197'
git switch 'github-bot/gh-197'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-197'
git switch 'github-bot/gh-197'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-197'
git switch 'main'
git merge --squash 'github-bot/gh-197'
git switch 'main'
git merge --ff-only 'github-bot/gh-197'
git switch 'main'
git merge 'github-bot/gh-197'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.