GPU, HLE: opt-in guarded writes for title memcpy and memset #3375
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3375"
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?
Imported from Marvisak/VIctory. Originally posted by lhnizdo on 2026-11-26T20:51:00Z.
On Windows, a title import of
memcpyormemsetfromVCRUNTIME140.dllcan write through a writable view of the same physical section while its title mapping stays read-only. Enable the experiment withVICTORY_COPY_BESIDE=1. The default keeps the host CRT bindings and the existing section layout.The hook processes one 64 KiB destination page at a time. It pins the view registration, verifies protection, marks the page and its aliases dirty, and lets armed trackers save the old bytes before writing under the guard lock. Ordinary writes run after releasing those locks. A small bit table skips registry lookups for ordinary address regions. A stale bit only causes another lookup. Remapping unregisters the view before unmapping it. The resolver still checks the host allow-list and the presence of the actual export.
Measurements
On a Ryzen 7 2700X, the ignored
guarded_crt_copy_benchmarkalternates 3,200 native and redirected 4 KiB writes per sample on the same 32 freshly guarded pages, including the tracker take. Every round checks the dirty range, and every sample verifies the final bytes. On the standalone branch the three native samples measured 7.36, 9.17 and 9.37 µs/copy. The redirected samples measured 0.70, 0.70 and 0.75 µs/copy.On a separate RTX 2080 integration stack, two five-minute runs used the same diagnostic executable with the BVH builder held back. The enabled run logged a redirected CRT write. Both runs reached the world and ended at the cutoff without device removal. Medians below use complete reporting intervals after world entry with at least 500 draws per frame.
Streaming and draw counts differed. An earlier pair, before the region filter, also varied substantially. These runs establish that the title uses the path and show lower guard overhead in the final pair. They do not establish higher world FPS.
Verification against main
Integration
#3069 targets marked GPU copy-back and has independent evidence about readers overlapping a write. This change targets title CRT calls. It holds the guard lock from the dirty mark through the page chunk's write. A refresh that already obtained its dirty ranges can still overlap title activity. A multi-page CRT copy has ordinary CRT visibility. If #3035 lands, its optimistic snapshot validation will need to account for writes through retained guards and their aliases.
The CRT interception idea follows avatarmanz's VIctory work.
AI-assisted (Codex).
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T21:10:15Z.
Review: APPROVED
LGTM — verified on Windows against
origin/main(63bcca4).memcpy/memsetare cleanly isolated behind the switch and well-covered across concurrent writes, armed aliases, straddling ranges, and host import policy.cargo test -p victory-gpu --lib watch:: -- --test-threads=1: 19 passed, 0 failed (1 ignored).cargo test -p victory-hle --lib host:: -- --test-threads=1: 3 passed, 0 failed (includingcrt_copy_imports_follow_the_switch_and_host_policy).cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T21:11:17Z.
Review: APPROVED
Clean, well-contained opt-in experiment (
VICTORY_COPY_BESIDE=1), keeping default host CRT resolution and section layout unchanged. Verified merged ontoorigin/main(63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090,VICTORY_D3D12_DEBUG=1):PHYSICAL -> BESIDE_VIEWS -> GUARDS -> Changed):xmem::map(crates/victory-hle/src/modules/xmem.rs:861-866) andxmem::unmap(xmem.rs:998-1005) holdPHYSICALwhile updatingBESIDE_VIEWS/GUARDS,watch::bulk_chunk(crates/victory-gpu/src/watch.rs:941-955) holdsBESIDE_VIEWS.read()->GUARDS, andGUARDSnever calls back intoBESIDE_VIEWSorPHYSICAL.xmem::unmapcallstitle_memory_beside_gone(at, size)before unmapping or dropping theSlice/Arc<Section>, soSection.1cannot be unmapped while abulk_chunkwrite is in flight.SEC_RESERVEsection (XMemAllocatePhysicalPages+XMemMapPhysicalPageswithVICTORY_COPY_BESIDE=1, and a standaloneSEC_RESERVEwatch.rstest) that committing the title view commits the shared backing pages inSection.1as well (ours_state = MEM_COMMIT,PAGE_READWRITE), andtracked_memseton a guardedSEC_RESERVEpage succeeds without faulting.cargo xtask policy(191 files ok) andcargo clippy --locked -p victory-gpu -p victory-hle --all-targets -- -D warnings(both default and--features victory-gpu/diagnostics) pass cleanly.victory-gpuwatch::tests::*(19/19 passed, 1 ignored benchmark) andvictory-hlehost::+xmem::(17/17 withVICTORY_COPY_BESIDEunset and 17/17 withVICTORY_COPY_BESIDE=1) all pass.dst == src) no-ops do not dirty pages; multiple consecutive redirected writes followed by a native write fault on an armed page preserve the original pre-first-writesavedbytes; forward (dst > src) and backward (dst < src) overlappingtracked_memcpyacross guarded pages preserve exact bytes and dirty ranges; misaligned multi-page copies spanning 3 guarded pages match byte-for-byte.Minor non-blocking observation if this ever graduates past opt-in:
bulk_chunkexecutes the caller'swriteclosure while holdingBESIDE_VIEWS.read()andguards(). Read faults onsrcwon't deadlockon_write(sinceon_writereturnsEXCEPTION_CONTINUE_SEARCHwhenExceptionInformation[0] != WRITE), though if a title ever caught a fault insidememcpyvia SEH__try/__except, unwinding past anextern "C"frame would skip Rust guard drops. Completely fine for an opt-in experiment.LGTM!
Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T22:55:32Z.
Windows 11,
63bcca4+ this PR, Radeon RX 7900 XTX:cargo test -p victory-gpu --lib: 235 passed / 0 failed / 2 ignored (unpatched63bcca4here: 231 passed / 1 ignored; +5 and one more ignored — the benchmark).cargo test -p victory-hle: lib 153 passed / 0 failed (unpatched: 152; +1), plus the integration suites.cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings: clean.The guarded-write tests (
crt_writes_across_guarded_and_ordinary_pages,crt_write_preserves_armed_alias_and_unregisters_before_remap,crt_fast_check_keeps_registered_pages_and_straddling_ranges,concurrent_crt_writes_are_visible_after_the_last_take) all pass with guarded writes on, and the import-level test (crt_copy_imports_follow_the_switch_and_host_policy) is the victory-hle one. Synthetic only. AI-assisted.Imported from Marvisak/VIctory. Originally posted by kilolimaVI on 2026-11-27T14:11:54Z.
Checked the new tests for stability on head
8712ae3: Windows 11, Ryzen 7 8845HS / Radeon 780M (AMD), toolchain 1.98.1.crt_writes_across_guarded_and_ordinary_pages,crt_write_preserves_armed_alias_and_unregisters_before_remap,crt_fast_check_keeps_registered_pages_and_straddling_ranges,concurrent_crt_writes_are_visible_after_the_last_take(victory-gpu) andcrt_copy_imports_follow_the_switch_and_host_policy(victory-hle).cargo test -p victory-gpu --lib --locked, default parallel threads, 20 runs in a row: 0/20 failures, so the concurrent test doesn't interfere with the other watch tests on this host.AI-assisted (Claude).
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.