HLE reuses freed physical page numbers #1445
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!1445
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1445"
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?
@BaggyALPHA on GitHub · 2026-11-24 18:03 UTC · original
BaggyALPHA:fix/reuse-physical-page-numbers→main· +133 −20 · 1 files · 3 commits · openAddresses item 36 (VIC-BT-036) in #1249. The allocator now reuses freed page numbers and refuses a request if it cannot fit every number into the title's existing 21-bit field. A repeated free cannot add the same number twice. Existing mappings still refer to their original section when a number is reused for a new section.
I seeded the allocator just below the limit in an isolated test process and used the public allocation and free functions. Upstream returned an unrepresentable number on the second allocation despite the first having been freed. The patch reused the freed number. The regression also checks exhaustion with a live allocation, a duplicate free, and an old mapped view keeping its bytes after reuse. All 96 HLE tests, Clippy with all targets and features, and the asset-policy hook passed on Windows.
The follow-up checks the page-count limit before calculating the section size, so oversized requests clear the output count and leave the page array untouched. The synthetic regression now exercises allocation, mapping, aliasing, and physical writes at the limit. After reuse, the old mapping and an alias made from it retain their data, and writes by page number reach the new allocation. The oversized-request assertion failed before the follow-up and passes with it. Windows HLE tests (96), all-targets/all-features HLE Clippy, and the pre-commit asset-policy check passed.
AI-assisted (Codex).
@Daniel333313131 on GitHub · 2026-11-24 20:29 UTC · original
Tested on AMD (RDNA3) together with #431, to see whether this PR removes the one GPU page fault that remains there. In three runs it did not.
Setup: AMD Radeon RX 7900 XTX 24 GB, driver 32.0.32029.2010; Ryzen 7 9800X3D, 32 GB; Windows 11 10.0.26300. Diag build of
mainffe310ewith this PR's headf89051fand #431's head23ced70merged. Each run used its own emptyVICTORY_CACHE_DIR,VICTORY_D3D12_DRED=1, andVICTORY_KEYSset to Enter every 3 s from 8 s to 47 s. Each run was stopped after 180 s.Background: with #431 alone, faults inside freed textures are gone on this GPU (#431). What is left is a page fault where DRED names no resource.
mainshows the same kind in one of three runs.0x4fa396000, no resource named0x505a27000, no resource namedAI use: Claude Code (Claude Opus 5.5) merged and built the commits, ran the title, summarised the log lines and wrote this comment. The numbers come from the runs above.
@Marvisak on GitHub · 2026-11-25 03:28 UTC · original · edited 2026-11-25 20:19 UTC
Automated triage for the maintainers.
The rebased head merges cleanly. In
crates/victory-hle/src/modules/xmem.rs,XMemFreePhysicalPagesnow hands numbers to a free list throughPhysical::free, which only pushes a number that was actually allocated, so freeing twice cannot hand the number out twice.Physical::allocatereuses freed numbers before taking new ones and refuses a request that would go past the 21-bit page-number limit already described next toFRAMESonmain. On refusal and for oversized counts, the function writes 0 tocountand returns 0, which matches the failure pathmainalready has. Reused numbers can come back out of order within one allocation. That is fine:runsgroups pages by section and slice index, not by number. Existing views keep their ownArc<Section>, so a reused number does not change what an older mapping shows. The tests use a private allocator, so they do not change the global one. One small thing: there is a stray second blank line beforeXMemFreePhysicalPages. CI passes.Reply to the author: thanks for the rebase. I reviewed the current head
3f88d1f. The conflict resolution keepsmain'sunknown_pages()and the wording of its doc comment, as you described. Your withdrawn note aboutwatch.rsmatches the code:freetouches no mapping, and bothmapandunmapreport remaps.Generated by Claude Code
@BaggyALPHA on GitHub · 2026-11-25 03:41 UTC · original
Follow-up for the triage concern:
a2defb2removes the child process. The limit regression seeds a privatePhysicalallocator in-process, so parallel HLE tests cannot see the artificial limit. It still checks exhaustion, duplicate free, reuse, and distinct live Windows mappings for the old and new backing sections. A separate test exercises the public allocation API's oversized-request outputs.Windows HLE library tests: 96 passed with default parallel scheduling. Strict Clippy with all targets and features, and the pre-commit asset-policy check, passed. Inputs were synthetic. No game run or relationship to the GPU page fault is claimed.
AI-assisted (OpenAI Codex).
@BaggyALPHA on GitHub · 2026-11-25 04:56 UTC · original
Pre-merge concern worth a maintainer's eyes, found while investigating #2025:
crates/victory-gpu/src/watch.rslines 33-35 document an accepted gap — "memory freed and allocated again while a tracker holds it is not seen." On currentmain,XMemFreePhysicalPagesnever recycles a page number (next_pageonly increments), so that gap is currently unreachable through XMem. This PR's free-list reuse is the first thing that would make a freed-and-reallocated XMem page number reachable while somevictory_gpu::watch::Tracked/Shadowstill holds stale state for the old backing, if the title (or our own view-remapping) reuses a number quickly enough relative to an in-flight GPU submission's copy-back.I have not demonstrated this actually happens — it would need either a live repro with real page churn (I attempted one for #2025 and didn't get far enough on NVIDIA hardware before hitting the unrelated world-hang device-removal) or a synthetic concurrency test exercising both crates together. Flagging it so it's weighed before merge rather than discovered after; happy to help verify if useful.
AI-assisted (Claude Code).
@BaggyALPHA on GitHub · 2026-11-25 15:30 UTC · original
Withdrawing my own pre-merge concern above: page-number reuse cannot reach the
watch.rsgap. Mechanism and a synthetic test below.I flagged that this PR's free-list reuse would be the first thing to make
watch.rs's documented caveat reachable — "memory freed and allocated again while a tracker holds it is not seen". I said then that I had not demonstrated it. Having now tested the gap directly and read the paths, I think the concern does not apply, and it should not sit on this PR unanswered.The gap is real, and reporting the mapping is what closes it. A synthetic test in
victory-gpu's ownwatch::tests(write-watched pool memory, no GPU, no game): track two pages, take them so they are guarded and clean, release the allocation, take the same address back withVirtualAlloc, then write to it.title_memory_remappedfor that range, the same take reports it changed.So the caveat is exactly as the module describes, and the runtime's own mapping report is the thing that closes it.
Why reuse cannot trigger it. The gap needs the bytes behind an already-tracked address to change with no report. Reuse does not do that:
XMemFreePhysicalPagescallsPhysical::free, which removes the number frompagesand pushes it ontofree_pages. It touches no view, no mapping and no address, so nothing behind any tracked address changes.map()resolves for that number. An existing view keeps its own section slice (views hold theirArc<Section>), which is the behaviourXMemFreePhysicalPages' own comment already describes for pages freed while still mapped.map()andunmap(), and both callvictory_gpu::title_memory_remappedfor the range — the call the test shows closes the gap.So the sequence I was worried about (reuse, then a tracker missing the new owner's writes) needs a mapping step, and that step reports itself.
What I ran.
cargo test -p victory-gpu --features diagnostics --lockedonmain940d32fplus the test: 145 passed, 2 failed, against 144 passed, 2 failed formainalone in a separate target directory — the two being the capped-view-heap pair (FIND-9, what #2121 addresses). The new test passed 4 of 4 runs, three of them under default parallelism, one serial. RTX 3080, Windows 11 26300. Synthetic inputs only; no game files.Two notes in the interest of completeness:
main'stitle_memory_aliasedcall inmap(), so it has two notification sites wheremainhas three. That is branch age, not a behaviour difference — the merge is clean and that call comes with it.victory-gpurather than smuggle it in here; say the word and I will, otherwise the result above stands as evidence.AI-assisted (Claude Code); the numbers are from the runs described.
@BaggyALPHA on GitHub · 2026-11-25 20:07 UTC · original
Rebased onto current
main(13f91ca); the branch had gone conflicting asmainmoved. New head3f88d1f, same three commits.One conflict, in
modules/xmem.rs:mainhas since addedunknown_pages()(used bydstorage.rs) next toFRAMES, which this branch had moved to the top of the file besidePAGE_NUMBER_LIMIT. Resolved by keepingunknown_pages()and dropping the now-duplicateFRAMES, withmain's fuller wording of its doc comment kept rather than mine, so the only change to that comment is where it sits.On the rebased head:
cargo test -p victory-hle --locked148 passed, 0 failed, includingfreed_page_numbers_can_be_reused_at_the_limitandoversized_physical_request_clears_count_without_writing_page_numbers;cargo clippy -p victory-hle --all-targets --locked -- -D warningsclean;cargo xtask policy184 files ok. Synthetic inputs only, no game files.Nothing about the change itself is different — it is the same allocator, the same two tests, and the in-process limit test from the earlier follow-up. The concern I raised and then withdrew about page-number reuse reaching
watch.rs's caveat still stands as withdrawn;mainmoving has not changed that reasoning, sincefreestill touches no mapping.AI-assisted (Claude Code).
@fuzeofficlal on GitHub · 2026-11-26 16:12 UTC · original · approved these changes
Reviewed
3f88d1fa62e02046b8a15507ae3d031f1722d452(merged cleanly onto63bcca43c6a00a758510398fa43096ce7e4f7df0) and verified on Windows 11 x64 (rustc 1.98.1MSVC).Audit
crates/victory-hle/src/modules/xmem.rs:364,:365,:465,:518):XMemAllocatePhysicalPagesrejects counts above(PAGE_NUMBER_LIMIT - FIRST_PAGE_NUMBER) / FRAMESbeforechecked_mul(PAGE)orCreateFileMappingW, zeroing*countwithout touchingpages.Physical::allocatechecksfresh = numbers.len().saturating_sub(self.free_pages.len()) <= (PAGE_NUMBER_LIMIT - self.next_page) / FRAMESup front (next_page <= PAGE_NUMBER_LIMITis preserved as an invariant), so exhaustion fails all-or-nothing, leavesnumbersuntouched, and dropsArc<Section>cleanly (closing the section handle).crates/victory-hle/src/modules/xmem.rs:392,:488):Physical::freepushes tofree_pagesonly whenself.pages.remove(&number).is_some(), making duplicate and unknown-PFN frees safe no-ops.Arc<Section>inviews,runs/XMemMapPhysicalPagescoalesce by(section, slice.index)rather than PFN order (so LIFO out-of-order recycled PFNs from one allocation still map in a single run), and virtual mapping changes remain reported throughmap/unmap(title_memory_remapped).XMemFreePhysicalPages.Verification
63bcca43c6a00a758510398fa43096ce7e4f7df0+cargo run -p xtask -- policy(191 files ok)cargo clippy -p victory-hle --all-targets -- -D warnings(default &--all-features)cargo test -p victory-hle --lib -- modules::xmem::: 16/16 passed (includingfreed_page_numbers_can_be_reused_at_the_limitandoversized_physical_request_clears_count_without_writing_page_numbers).max_pages + 1rejection (2_031_617),n == 0/ null-pointer calls, unknown-PFN frees (0,usize::MAX, unallocated) not poisoningfree_pages, duplicate-free idempotency, LIFO reversed PFN reuse preserving sequential slice indices, and atomic failure on exhaustion.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.