Skip to content

xmem: keep the space below 4 GiB for the title's 32-bit memory - #1009

Merged
Marvisak merged 3 commits into
Marvisak:mainfrom
NuraadGit:xmem/guard-32bit-window
Nov 25, 2026
Merged

Marvisak merged 3 commits into
Marvisak:mainfrom
NuraadGit:xmem/guard-32bit-window

Conversation

@NuraadGit

@NuraadGit NuraadGit commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #790.

What

At startup Grand Theft Auto VI asks XMemVirtualAlloc for a 185 MB placeholder with the 32-bit address flag (size=193986560 kind=0x42000 flags=0x7, so address_range gives 0..4 GiB). Windows places allocations bottom up, so the host's own early memory (the driver, heap segments, the image read in run, the caches) can fill the space below 4 GiB first. VirtualAlloc2 then fails with ERROR_NOT_ENOUGH_MEMORY, main doesn't log it, and the title stops at its own assertion (0x80000003 at +0x19f4f58) about 3 s after the device is made.

This keeps that space for the title:

  • xmem::low_guards::install() runs at the start of run when executing, before the image read, the device and the caches. It reserves the free space below 4 GiB except the lowest 256 MB, which is left to the host.
  • A refused 32-bit XMemVirtualAlloc releases the smallest guard that fits it (else the largest) and retries the same call, then guards again whatever is still free of that hole.
  • The normal path is unchanged. It's only a retry on refusal, so write-watch and placeholder semantics stay as they are.
  • Test: low_guards::tests::a_released_guard_is_guarded_again_around_what_took_it.

Measured (RTX 2080, Windows 11, 32 GB)

A temporary probe in the refusal path (not part of this PR) found only 22 MB free below 4 GiB at the failure, in pieces, with a largest hole of 14 MB.

before with this
startups with a large pipeline cache failed 2 of 3 passed 5 of 5 (3.7 GB below 4 GiB kept for the title)
startups with VICTORY_D3D12_DEBUG=1 failed every time start and reach the world

The second row matters for debugging: the D3D12 debug layer's memory caused the same failure, so on machines like this one the debug layer and GPU-based validation were unusable. With the guards, the debug layer has since named a device-removal cause in one run (see my comment on #55).

cargo test -p victory-hle --lib low_guards passes, and cargo clippy -p victory-hle -p victory --all-targets -- -D warnings is clean.

AI-assisted (Claude Code): the probe, fix, test and runs were automated. The hardware, title copy and runs are the user's.

A real-world trigger (from @ryosen0, RTX 3060 Ti)

After a driver TDR, the NVIDIA driver maps its ~1 GB shader cache (%LOCALAPPDATA%\NVIDIA\DXCache\*.nvph) into every new process. That mapping can take the space below 4 GiB, so the title's 185 MB ADDRESS_32BIT placeholder is refused and startup stops at an int3 about 3 s in. It fails every time until the file is removed or the machine reboots. With this PR, startup succeeds with the file in place (report).

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested merged cleanly onto main (ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC):

  • cargo test -p victory-hle: 95 passed, 0 failed (plus no_dll_planting: 1 passed)
  • cargo clippy -p victory-hle --all-targets: 0 warnings
  • cargo xtask policy: ok

Clean fix preserving low 32-bit virtual address space below 4 GiB for title allocations. Approving.

AI-assisted (Gemini 4 Neon).

@MichaelSmithAI

Copy link
Copy Markdown

The guest successfully allocated and released the 185 MiB placeholder in an isolated Wine 11.22 process. We also checked the retry path and opened a follow-up on your branch that tries each original guard at most once per request. Both guard controls passed, including restoration after a simulated allocation refusal. Our Wine result covers the allocation calls; the native startup A/B in this PR remains the title evidence.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at ab749fd: cargo test -p victory -p victory-hle passes, 97 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

@NuraadGit

Copy link
Copy Markdown
Contributor Author

Updated with @MichaelSmithAI's follow-up from NuraadGit#1 (75f1e99). A refused retry used to be able to loop on the same guard forever. It now tries each existing guard once, under the guard lock, and keeps the allocation's error. I added 7ea9112 on top: safety comments for clippy::undocumented_unsafe_blocks, and rustfmt. cargo test -p victory-hle --lib low_guards (2 passed), clippy with -D warnings and rustfmt are clean.

@ryosen0

ryosen0 commented Nov 24, 2026

Copy link
Copy Markdown

@NuraadGit RTX 3060 Ti (Ampere) confirmation, including the post-TDR case and a real driver-cache pressure source. Windows 11 Pro 22631, RTX 3060 Ti 8 GB, driver 617.42 (32.0.16.1742), title 1.0.2.1 (b99f876b), diag build.

The chain, one run per step:

  1. After a driver TDR (nvlddmkm Event 153), the title failed 8 of 8 startups with a first-chance 0x80000003 at title+0x19f4f58 about 3 s in, before the xmem hooks. The log up to the crash was byte-for-byte identical to a successful run. Quarantining VIctory's caches, saves and telemetry changed nothing.
  2. The trigger turned out to be %LOCALAPPDATA%\NVIDIA\DXCache\408da9dbd953f4b6.nvph, a 1 GB file the driver had written during the long run that ended in the TDR. Quarantining that single file let the title start (17 min, 30 fps, 0 device removals). The large driver-cache mapping takes the space below 4 GiB, the 185 MB ADDRESS_32BIT placeholder is refused (unlogged on main), and the bring-up gate hits the int3. This also explains "broken forever after a TDR" without a reboot: the driver maps the cache into every new process.
  3. The 1 GB file restored to its place, plus a build with this PR (7ea9112 on ffe310e + GPU: bind root SRVs as one-view tables bounded to their host copy #493 + GPU: make a structured buffer view off its host copy's elements a raw view #206 + Loader: opt-in VICTORY_NO_WATCHDOG hook for the title's watchdog (build 1.0.2.1) #454 + GPU: copy title writes under ranges a submission's shaders write #1322): low_guards: space below 4 GiB kept for the title guards=2 mb=3796, startup succeeds. A direct confirmation of the fix under real pressure, not only the debug layer.

AI-assisted; measurements are from a real RTX 3060 Ti run.

@NuraadGit

Copy link
Copy Markdown
Contributor Author

@ryosen0 thank you, that's the real-world trigger this PR was missing, and it explains why a machine can stay broken after one TDR until a reboot.

To restate the chain for anyone landing here:

  1. A long run ends in a TDR.
  2. The NVIDIA driver has written a ~1 GB shader cache file (%LOCALAPPDATA%\NVIDIA\DXCache\*.nvph), and it maps that file into every new process.
  3. The mapping lands below 4 GiB, so the title's 185 MB ADDRESS_32BIT placeholder is refused. On main that refusal isn't logged, and the title stops at the int3 about 3 s in.

The workarounds without this PR are to quarantine that one cache file or to reboot. With 7ea9112 the space below 4 GiB is kept for the title early (low_guards … guards=2 mb=3796), and your run starts with the file in place.

I'll add the DXCache case to the PR description as the motivating example.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate.

The rebase onto main resolved the conflict. Reviewed at 48a40d2.

  • xmem::low_guards::install is called from run before the image read and before the device is created. It reserves the free space below 4 GiB, except for the lowest 256 MB, which stays available to the host.
  • When XMemVirtualAlloc is refused for a constrained 32-bit request (constrained && highest < FOUR_GIB), low_guards::retry releases one guard at a time. It tries each existing guard once, smallest fitting first, and re-guards whatever stays free in that guard. It also restores the allocation's own last error, so the refusal log that main added still reports the right cause.
  • Successful allocations follow the same path as before. Both unit tests cover the bounded retry and the re-guarding.

Evidence: native A/B startup runs on two machines, including the NVIDIA DXCache case after a TDR, plus a Wine check of the allocation calls.

One thing for the maintainer to keep in mind: only 256 MB below 4 GiB is left for host components that need low addresses.

CI: failing. The only failures are emit::synthetic_tests::gathers_at_a_level_match_the_hardware and permutes_read_the_lane_named_and_zero_from_lanes_left_out in victory-shader, and main's own CI fails both of them too. The xmem tests pass.


Generated by Claude Code

NuraadGit and others added 3 commits November 24, 2026 19:33
XMemVirtualAlloc with the 32-bit address flag has to fit below 4 GiB, where
Windows also puts the host's own memory first (it allocates bottom up). With
a large pipeline cache read at startup, the driver's, heap and cache memory
left 22 MB there, in pieces of at most 14 MB, when Grand Theft Auto VI asked
for a 185 MB 32-bit placeholder; the request was refused
(ERROR_NOT_ENOUGH_MEMORY) and the title stopped at an assertion
(+0x19f4f58). The D3D12 debug layer's memory did the same.

`low_guards::install`, at the start of `run`, reserves the free space below
4 GiB but the lowest 256 MB (left to the host). A 32-bit request that finds
no room releases the smallest guard that fits it (else the largest) and
tries again, then guards what is still free of it.

Tested: the new test releases a guard, takes part of the hole and checks the
rest is guarded again. In the title (RTX 2080, a build with a large cache):
without this the startup failed 2 of 3 times; with it, 5 of 5 starts passed,
3.7 GB below 4 GiB kept for the title.
@NuraadGit

Copy link
Copy Markdown
Contributor Author

Rebased onto main (4118f33). The only conflict was in XMemVirtualAlloc: main now logs a refused allocation, and this PR retries a 32-bit allocation after releasing the low guards. Both stay, in that order: retry first, then log if it still fails, with the error kept from the allocation attempt itself (low_guards::retry preserves it across its own system calls). cargo test -p victory-hle --lib xmem: 12 passed. cargo clippy -p victory-hle --all-targets adds no warnings.

@NuraadGit
NuraadGit force-pushed the xmem/guard-32bit-window branch from 7ea9112 to 48a40d2 Compare November 25, 2026 01:01
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
Vyto-Line pushed a commit to Vyto-Line/VIctory that referenced this pull request Nov 25, 2026
naixue2233 pushed a commit to naixue2233/Victory-naixue2233 that referenced this pull request Nov 25, 2026
…32, #854, #768, #812, #860, #864, #868, #896, #922, #936, #964)

Takes in the fork of this tree that targets 8 GB NVIDIA cards (Wave32),
which carries those pull requests merged on top of `0b0074e` plus one fix
of its own: `xmem` keeps its guards below the low 4 GiB on machines with
more than 32 GB (`Marvisak/VIctory#1009`).

Conflicts, and what was kept:

- `crates/victory-gpu/src/descriptor/host.rs`: both tests (our tiled slot
  test and their dynamic run across the cursor), and our `cbv_size` —
  `checked_next_multiple_of(256)` before the 64 KiB clamp, so a size near
  `u32::MAX` cannot wrap.
- `crates/victory-gpu/src/shadow.rs`: one `use` for the merge tests, ours
  and theirs joined (`SNAPS`, `give_back`, `pooled`, `take_snapshots` on
  top of `PIECE`, `back_piece`, `changed_runs`, `join_runs`).
- `crates/victory-shader/src/emit/mod.rs`: their `exactly(...)` returns for
  the loose-LDS forms. Their tree has the same `exactly` closure ours does
  (an approximate form reported as such), so this is the form ours wants
  too. `with_loose_lds` and its `LDS_LOOSE` were defined twice after the
  automatic merge; one copy is left.
- `crates/victory-gpu/src/watch.rs`: their `VICTORY_VIEW_KNOWN` view set
  (off by default) beside our `views` ranges, and our `title_memory_
  committed` alongside their parked-block handling.
- `crates/victory-gpu/src/pipeline/host.rs`: one `integer_format` and one
  `host_blend` — ours, which keeps a logic op on an integer target valid
  when that target does not blend (#922 as we have it).
- `crates/victory-gpu/src/texture.rs`: their `VICTORY_TEXTURE_DROP_MIPS`
  test, moved to our staging signatures (`staged_memory` returns the mips,
  `refresh_unlocked` takes the frame, `fingerprint` the mips, `Staging`
  wants `first_mip` and `title_bytes`).
- `README.md`: our title, their measured result and the list of PRs taken
  in, under one heading.

Defaults are theirs where they changed one, except that
`VICTORY_SKIP_WC_COMPARE` stays off (it breaks the hull physics of the
first chapter) and `VICTORY_TEXTURE_DROP_MIPS` stays 0: it is worth
`=2` only where VRAM is short, and it costs resolution everywhere.
WHOISJARDAS pushed a commit to WHOISJARDAS/GROJE-VI that referenced this pull request Nov 25, 2026
A port of Marvisak/VIctory#1009 (NuraadGit, with the bounded retry from
MichaelSmithAI). At startup the title asks XMemVirtualAlloc for a 185 MB
placeholder below 4 GiB. Windows places the host's own memory bottom up,
so the drivers' mapped shader caches, heaps and the image read can fill
that space first; the request is refused and the title stops at its own
assertion (int3 at +0x19f4f58), with nothing in the log. With the
system's bottom-up ASLR turned off (Exploit protection) it happens on
every start.

The free space below 4 GiB (but the lowest 256 MB) is reserved at the
start of `run`; a 32-bit request that finds no room releases guards one
at a time and tries again, guarding again what stays free. A request
still refused is logged.

RX 9070 XT, bottom-up ASLR off, 2.3 GB of the driver's shader cache
mapped below 4 GiB: this fork's 4cbe9d7 stopped at the assertion at
start; with this, it reaches the menus (3.7 GB kept for the title).

Co-authored-by: NuraadGit <81594937+NuraadGit@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Marvisak
Marvisak merged commit d1dfbbd into Marvisak:main Nov 25, 2026
1 check failed
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
notquiteog pushed a commit to notquiteog/victory-nvidia that referenced this pull request Nov 26, 2026
…sak#1009)

* xmem: keep the space below 4 GiB for the title's 32-bit memory

XMemVirtualAlloc with the 32-bit address flag has to fit below 4 GiB, where
Windows also puts the host's own memory first (it allocates bottom up). With
a large pipeline cache read at startup, the driver's, heap and cache memory
left 22 MB there, in pieces of at most 14 MB, when Grand Theft Auto VI asked
for a 185 MB 32-bit placeholder; the request was refused
(ERROR_NOT_ENOUGH_MEMORY) and the title stopped at an assertion
(+0x19f4f58). The D3D12 debug layer's memory did the same.

`low_guards::install`, at the start of `run`, reserves the free space below
4 GiB but the lowest 256 MB (left to the host). A 32-bit request that finds
no room releases the smallest guard that fits it (else the largest) and
tries again, then guards what is still free of it.

Tested: the new test releases a guard, takes part of the hole and checks the
rest is guarded again. In the title (RTX 2080, a build with a large cache):
without this the startup failed 2 of 3 times; with it, 5 of 5 starts passed,
3.7 GB below 4 GiB kept for the title.

* Bound low-address allocation retries to existing guards

* xmem: safety comments and formatting for the bounded guard retry

---------

Co-authored-by: swarm <swarm@swarm.swarm>
Co-authored-by: NuraadGit <NuraadGit@users.noreply.github.com>
(cherry picked from commit d1dfbbd)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Steenax6 added a commit to Steenax6/VIctory-NVID that referenced this pull request Nov 26, 2026
XMemVirtualAlloc with the 32-bit address flag has to fit below 4 GiB,
where Windows also puts the host's own memory first (bottom up). With a
large pipeline cache read at start-up only 22 MB was left there, in
pieces, when the title asked for a 185 MB 32-bit placeholder; the request
failed and the title stopped at an assertion.

`low_guards::install`, at the start of `run` when the title is to be
executed, reserves the free space below 4 GiB but the lowest 256 MB (left
to the host). A 32-bit request that finds no room releases the smallest
guard that fits it (else the largest), tries again, and guards what is
still free of it.

From Marvisak/VIctory d1dfbbd (Marvisak#1009, Nuraad), merged upstream; measured
there on an RTX 2080: 2 of 3 starts failed without it, 5 of 5 passed with
it. Not seen to fail here, a robustness fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xmem: host memory fills the space below 4 GiB, so the title's 32-bit placeholder is refused at startup (fix on a branch)

6 participants