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

Manually merged
github merged 3 commits from gh-pr/1009/head into gh-pr/1009/base 2026-11-26 04:16:56 +00:00
Owner

@⁠NuraadGit on GitHub · 2026-11-24 14:56 UTC · original
NuraadGit:xmem/guard-32bit-window → main · +203 −2 · 2 files · 3 commits · merged 2026-11-25 21:49 UTC

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).

**@⁠NuraadGit** on GitHub · 2026-11-24 14:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/1009) `NuraadGit:xmem/guard-32bit-window` → `main` · +203 −2 · 2 files · 3 commits · merged 2026-11-25 21:49 UTC 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](https://git.gtahub.st/github/Marvisak--VIctory/pulls/1009)).
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:16:53 +00:00
github manually merged commit 48a40d2c01 into gh-pr/1009/base 2026-11-26 04:16:56 +00:00
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 15:04 UTC · original

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.

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 15:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7135420090) 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](https://github.com/NuraadGit/VIctory/pull/1) 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.
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 15:12 UTC · original

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).

**@⁠Zipettte** on GitHub · 2026-11-24 15:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7135644470) 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)._
Author
Owner

@⁠NuraadGit on GitHub · 2026-11-24 15:16 UTC · original

Updated with @⁠MichaelSmithAI's follow-up from NuraadGit/VIctory#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.

**@⁠NuraadGit** on GitHub · 2026-11-24 15:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7135742068) Updated with @⁠MichaelSmithAI's follow-up from NuraadGit/VIctory#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.
Author
Owner

@⁠ryosen0 on GitHub · 2026-11-24 19:10 UTC · original

@⁠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 + #493 + #206 + #454 + #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.

**@⁠ryosen0** on GitHub · 2026-11-24 19:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7141696156) @⁠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` + #493 + #206 + #454 + #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.
Author
Owner

@⁠NuraadGit on GitHub · 2026-11-24 19:35 UTC · original

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

**@⁠NuraadGit** on GitHub · 2026-11-24 19:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7142314621) @⁠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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:32 UTC · original · edited 2026-11-25 01:19 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 00:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7148609375) · edited 2026-11-25 01:19 UTC <!-- victory-triage v1 head=48a40d2c0110601840deb3c67308ea60ced053a3 verdict=merge-candidate at=2026-11-25T01:19:04Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠NuraadGit on GitHub · 2026-11-25 01:01 UTC · original

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** on GitHub · 2026-11-25 01:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7149121867) 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.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1009
No description provided.