GPU: avoid heap allocations and batch unprotects in watch fault hot path #3343
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!3343
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3343"
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?
@danielibabet on GitHub · 2026-11-26 19:19 UTC · original
danielibabet:pr/watch-zero-alloc-faults→main· +51 −4 · 1 files · 2 commits · openSummary
watch::changedfor single-page write faults without aliases (which covers nearly all vectored exception handler faults inon_write), skipping bothVecallocations andalias_of/protectedrange scans under the globalGUARDSmutex.alias_ofiteration inwatch::aliaseswhenalias_ofis empty, and skipalias_ofrange collection inwatch::changedwhenalias_ofis empty.watch::open_guardedinto singleVirtualProtect(..., PAGE_READWRITE)calls instead of one syscall per page, with a single-page fast path.Verification
cargo test -p victory-gpu --locked -- --test-threads=1 --skip dynamic_slots_are_held_until_released --skip lists_share_a_table_and_copies_keep_their_views:229 passed; 0 failed; 1 ignored; 2 filtered outcargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warningscargo run -p xtask --locked -- policy:asset policy: 191 files ok@Dukestar69 on GitHub · 2026-11-26 19:28 UTC · original · reviewed
Tested on Windows against
origin/main(63bcca4).cargo test -p victory-gpu --lib watch:: -- --test-threads=1(15/15),clippy, andxtask policypass, but there is a bug in the batchedopen_guardedpath:open_guarded(crates/victory-gpu/src/watch.rs), the batchedVirtualProtect((start * PAGE) as *const _, (end - start) * PAGE, PAGE_READWRITE, &mut old)ignores its return value after removing all pages instart..endfromg.protected.VirtualProtectfails atomically (0) if[start, end)spans two separateVirtualAlloc/MapViewOfFileallocations (differentAllocationBase) or includes a page that was unmapped. When that happens, none of the still-mapped pages in the run are restored toPAGE_READWRITE, yet they have already been removed fromg.protected, so the next write to any of them will trigger an unhandled access violation instead of being handled by the watch fault handler.protect_notingright above it, which checks theVirtualProtectreturn value and falls back to per-pageVirtualProtecton failure. CheckingVirtualProtect(...) == 0inopen_guardedand falling back to per-pageVirtualProtect(oropen) forstart..endfixes this cleanly.first + 1 == lastcan overflow iffirst == usize::MAX(e.g.first.checked_add(1) == Some(last)is safer).Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@danielibabet on GitHub · 2026-11-26 19:35 UTC · original
Good catch @Dukestar69! Fixed both in
crates/victory-gpu/src/watch.rs:VirtualProtect(...) != 0inopen_guardedbefore removingstart..endfromg.protected, and fall back to per-pageopen(g, page)when the batched call fails (e.g. across allocation boundaries or unmapped pages).first.checked_add(1) == Some(last)inchangedandopen_guardedto avoidusize::MAXoverflow.Re-ran
victory-gputests (229 passed, 0 failed; 2 skipped), strict clippy (--all-targets --all-features -- -D warnings), andxtask policy(191 files ok).@fuzeofficlal on GitHub · 2026-11-26 20:24 UTC · original · approved these changes
Verified locally merged onto
origin/main(63bcca43) at PR head65d26e3c(Windows 11 x64, Ryzen 9 9950X, NVIDIA RTX 5090 + Microsoft WARP,VICTORY_D3D12_DEBUG=1):crates/victory-gpu/src/watch.rs:changed(L717-L722): single-page non-aliased fault fast path avoids heap allocations and range scans underGUARDSwhile preservingmark_changed->openordering and return semantics;first.checked_add(1) == Some(last)preventsusize::MAXoverflow.open_guarded(L746-L787): batches contiguous guarded runs into singleVirtualProtect(..., PAGE_READWRITE)calls, checks!= 0before removingstart..endfromg.protected, and falls back to per-pageopen(g, page)when a batched call fails (e.g. across allocation boundaries or decommitted pages).aliases(L290-L293) andchanged(L724-L728): skipalias_oftraversal when empty.cargo xtask policy: 191 files ok.cargo clippy --locked -p victory-gpu --all-targets -- -D warnings(default and--all-features): 0 warnings.cargo test --locked -p victory-gpu --features diagnostics --lib -- watch::: 15/15 passed on both RTX 5090 and WARP.MEM_DECOMMIT) of a 6-page guarded run beforewriting(p, 6 * PAGE), triggering the batchedVirtualProtectfailure path and confirming per-pageopenremoved all 6 entries fromg.protectedand restoredPAGE_READWRITEon all 5 remaining pages without faulting on subsequent writes.[0, 1]and[4, 5]),usize::MAXboundary calls, and an 8-thread concurrent write-fault stress test across 64 guarded pages x 20 rounds (1,280 faults in 8.67 ms, 100% captured).#3341(both touchwatch.rs): 0 clippy warnings and 26/26watch::+shadow::tests passing.Synthetic tests only; looks ready to merge.
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.