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

Merged
NuraadGit merged 3 commits from upstream-pr/1009 into main 2026-11-25 21:49:38 +00:00
NuraadGit commented 2026-11-24 14:56:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by NuraadGit on 2026-11-24T14:56:09Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6584641488 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1009). Originally posted by [NuraadGit](https://github.com/NuraadGit) on 2026-11-24T14:56:09Z. --- 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://github.com/Marvisak/VIctory/pull/1009#issuecomment-7141696156)).
DenRakEiw commented 2026-11-24 15:03:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T15:02:22Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6041717518 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1009#pullrequestreview-6041717518). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T15:02:22Z. **Review:** APPROVED --- 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 commented 2026-11-24 16:05:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by MichaelSmithAI on 2026-11-24T15:04:40Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7135420090 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7135420090). Originally posted by [MichaelSmithAI](https://github.com/MichaelSmithAI) on 2026-11-24T15:04:40Z. --- 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.
Zipettte commented 2026-11-24 16:05:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T15:12:48Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7135644470 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7135644470). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T15:12:48Z. --- 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 commented 2026-11-24 16:05:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by NuraadGit on 2026-11-24T15:16:13Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7135742068 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7135742068). Originally posted by [NuraadGit](https://github.com/NuraadGit) on 2026-11-24T15:16:13Z. --- 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.
ryosen0 commented 2026-11-24 20:03:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ryosen0 on 2026-11-24T19:10:01Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141696156 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7141696156). Originally posted by [ryosen0](https://github.com/ryosen0) on 2026-11-24T19:10:01Z. --- @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.
NuraadGit commented 2026-11-24 20:03:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by NuraadGit on 2026-11-24T19:35:26Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7142314621 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7142314621). Originally posted by [NuraadGit](https://github.com/NuraadGit) on 2026-11-24T19:35:26Z. --- @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 2026-11-25 01:08:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:32:15Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148609375 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7148609375). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:32:15Z. --- <!-- 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)_
NuraadGit commented 2026-11-25 01:08:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by NuraadGit on 2026-11-25T01:01:09Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149121867 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1009#issuecomment-7149121867). Originally posted by [NuraadGit](https://github.com/NuraadGit) on 2026-11-25T01:01:09Z. --- 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.
upstream-sync closed this pull request 2026-11-25 22:02:46 +00:00
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1009
No description provided.