xmem: keep a stretch below 4 GiB for the title's 32-bit graphics heap (#385) #511

Open
github-bot wants to merge 2 commits from github-bot/gh-511 into main AGit
Owner

@⁠gabrielcorreabsb on GitHub · 2026-12-06 01:41 UTC · original
gabrielcorreabsb:pr/low-window → main · +274 −16 · 3 files · 2 commits · open

Addresses #385: on an RX 9070 XT the title stops at boot with 0x80000003 at +0x19f4f58 (r11=0xc0000017) with memory to spare. Its 32-bit graphics heap asks once for a placeholder of about 185 MB below 4 GiB, after the title image, its DLLs, the driver and the rest of the process are in place, and by then there can be no free stretch that big.

A run now reserves 256 MB below 4 GiB as a placeholder before anything else and splits the title's heap off its front, letting the rest go. A request that does not fit lets the whole stretch go for the host to place, as before. A refused 32-bit request now logs what holds the addresses below 4 GiB.

LARP_LOW_WINDOW_MB sets the size (default 256); 0 keeps nothing, as before. Documented in docs/environment-variables.md.

Ported from ReVIctory 03d164e (PR #43) by staticman256.

Tested

  • RX 9070 XT, Windows 11, this change on v1.1.6-amd (b7ab1fd): the release build had stopped at boot at +0x19f4f58; with the change the log shows the stretch kept, the 193,986,560-byte heap placed in it, and the title got past boot into its menu (pipelines compiling). The failure is intermittent and this is a small sample from one PC.
  • On this branch (on main): cargo check -p larp and a new unit test (the_title_heap_is_split_off_the_kept_stretch) pass. The main build itself was not run in game.

🤖 Generated with Claude Code

**@⁠gabrielcorreabsb** on GitHub · 2026-12-06 01:41 UTC · [original](https://github.com/gamingwithhomie/larp/pull/511) `gabrielcorreabsb:pr/low-window` → `main` · +274 −16 · 3 files · 2 commits · open Addresses #385: on an RX 9070 XT the title stops at boot with `0x80000003` at `+0x19f4f58` (`r11=0xc0000017`) with memory to spare. Its 32-bit graphics heap asks once for a placeholder of about 185 MB below 4 GiB, after the title image, its DLLs, the driver and the rest of the process are in place, and by then there can be no free stretch that big. A run now reserves 256 MB below 4 GiB as a placeholder before anything else and splits the title's heap off its front, letting the rest go. A request that does not fit lets the whole stretch go for the host to place, as before. A refused 32-bit request now logs what holds the addresses below 4 GiB. `LARP_LOW_WINDOW_MB` sets the size (default 256); `0` keeps nothing, as before. Documented in `docs/environment-variables.md`. Ported from ReVIctory `03d164e` (PR #43) by staticman256. **Tested** - RX 9070 XT, Windows 11, this change on `v1.1.6-amd` (`b7ab1fd`): the release build had stopped at boot at `+0x19f4f58`; with the change the log shows the stretch kept, the 193,986,560-byte heap placed in it, and the title got past boot into its menu (pipelines compiling). The failure is intermittent and this is a small sample from one PC. - On this branch (on `main`): `cargo check -p larp` and a new unit test (`the_title_heap_is_split_off_the_kept_stretch`) pass. The `main` build itself was not run in game. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The title asks once for a placeholder of about 185 MB below 4 GiB, after
the title image, its DLLs, the graphics driver and the rest of the process
are in place. What lands low before then can leave no stretch that big,
and the title stops at boot (0x80000003 at +0x19f4f58, r11=0xc0000017)
with memory to spare.

A run now reserves 256 MB below 4 GiB as a placeholder before anything
else (LARP_LOW_WINDOW_MB; 0: none, as before) and splits the title's heap
off its front, letting the rest go; a request that does not fit lets it
all go for the host to place. A refused 32-bit request logs what holds the
addresses below 4 GiB.

Ported from ReVIctory 03d164e (PR #43) by staticman256.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-06 01:46:49 +00:00
Author
Owner

@⁠christopear on GitHub · 2026-12-06 02:02 UTC · original

I extended the low-window check locally on eb62aa10 and ran it in five fresh Proton Experimental processes. All five passed with a 256 MiB reservation below 4 GiB.

  • 185 MiB split returned the original base. VirtualQuery reported exactly 185 MiB reserved and free space immediately after it.
  • 256 MiB exact fit returned the full reservation.
  • 257 MiB returned null and released the original reservation, allowing the caller to try normal allocation.

The window was consumed only once in each case. This exercises the actual placeholder APIs under Proton. It doesn't reproduce the original fragmented launch or test the caller's subsequent allocation.

Optional test patch for the boundary cases

Run the ignored test by itself in a fresh process with LARP_LOW_WINDOW_MB=256. It shares global state with the existing test and assumes the process can reserve the initial window. If folding this into the regular suite I'd combine the cases in your existing test or serialize them.

--- a/crates/larp-hle/src/modules/xmem.rs
+++ b/crates/larp-hle/src/modules/xmem.rs
@@ -1671,3 +1671,44 @@
         assert_ne!(unsafe { VirtualFree(at, 0, MEM_RELEASE) }, 0);
     }
 }
+
+#[cfg(test)]
+mod low_window_probe {
+    use super::*;
+    use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION};
+
+    fn region(at: usize) -> MEMORY_BASIC_INFORMATION {
+        let mut info = MEMORY_BASIC_INFORMATION::default();
+        // SAFETY: query metadata only, into a full-sized local buffer.
+        assert_ne!(unsafe { VirtualQuery(at as _, &mut info, size_of::<MEMORY_BASIC_INFORMATION>()) }, 0);
+        info
+    }
+
+    #[test]
+    #[ignore = "run in a fresh process with LARP_LOW_WINDOW_MB=256"]
+    fn split_exact_and_oversize() {
+        assert_eq!(low_window_bytes(), 256 * MIB);
+        for mb in [185, 256, 257] {
+            reserve_low_window();
+            let (base, len) = LOW_WINDOW.lock().unwrap().expect("need 256 MiB free below 4 GiB");
+            assert!(base + len <= 1usize << 32);
+            let size = mb * MIB;
+            let got = take_low_window(size, 65536);
+            assert!(LOW_WINDOW.lock().unwrap().is_none());
+            assert!(take_low_window(size, 65536).is_null());
+            if size <= len {
+                assert_eq!(got as usize, base);
+                assert_eq!(region(base).RegionSize, size);
+                assert_eq!(region(base).State, MEM_RESERVE);
+                if size < len {
+                    assert_eq!(region(base + size).State, 0x10000); // MEM_FREE
+                }
+                // SAFETY: release the entire placeholder returned above.
+                assert_ne!(unsafe { VirtualFree(got, 0, MEM_RELEASE) }, 0);
+            } else {
+                assert!(got.is_null());
+            }
+            assert_eq!(region(base).State, 0x10000);
+        }
+    }
+}
**@⁠christopear** on GitHub · 2026-12-06 02:02 UTC · [original](https://github.com/gamingwithhomie/larp/pull/511#issuecomment-7449638296) I extended the low-window check locally on `eb62aa10` and ran it in five fresh Proton Experimental processes. All five passed with a 256 MiB reservation below 4 GiB. * 185 MiB split returned the original base. VirtualQuery reported exactly 185 MiB reserved and free space immediately after it. * 256 MiB exact fit returned the full reservation. * 257 MiB returned null and released the original reservation, allowing the caller to try normal allocation. The window was consumed only once in each case. This exercises the actual placeholder APIs under Proton. It doesn't reproduce the original fragmented launch or test the caller's subsequent allocation. <details> <summary>Optional test patch for the boundary cases</summary> Run the ignored test by itself in a fresh process with `LARP_LOW_WINDOW_MB=256`. It shares global state with the existing test and assumes the process can reserve the initial window. If folding this into the regular suite I'd combine the cases in your existing test or serialize them. ```diff --- a/crates/larp-hle/src/modules/xmem.rs +++ b/crates/larp-hle/src/modules/xmem.rs @@ -1671,3 +1671,44 @@ assert_ne!(unsafe { VirtualFree(at, 0, MEM_RELEASE) }, 0); } } + +#[cfg(test)] +mod low_window_probe { + use super::*; + use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION}; + + fn region(at: usize) -> MEMORY_BASIC_INFORMATION { + let mut info = MEMORY_BASIC_INFORMATION::default(); + // SAFETY: query metadata only, into a full-sized local buffer. + assert_ne!(unsafe { VirtualQuery(at as _, &mut info, size_of::<MEMORY_BASIC_INFORMATION>()) }, 0); + info + } + + #[test] + #[ignore = "run in a fresh process with LARP_LOW_WINDOW_MB=256"] + fn split_exact_and_oversize() { + assert_eq!(low_window_bytes(), 256 * MIB); + for mb in [185, 256, 257] { + reserve_low_window(); + let (base, len) = LOW_WINDOW.lock().unwrap().expect("need 256 MiB free below 4 GiB"); + assert!(base + len <= 1usize << 32); + let size = mb * MIB; + let got = take_low_window(size, 65536); + assert!(LOW_WINDOW.lock().unwrap().is_none()); + assert!(take_low_window(size, 65536).is_null()); + if size <= len { + assert_eq!(got as usize, base); + assert_eq!(region(base).RegionSize, size); + assert_eq!(region(base).State, MEM_RESERVE); + if size < len { + assert_eq!(region(base + size).State, 0x10000); // MEM_FREE + } + // SAFETY: release the entire placeholder returned above. + assert_ne!(unsafe { VirtualFree(got, 0, MEM_RELEASE) }, 0); + } else { + assert!(got.is_null()); + } + assert_eq!(region(base).State, 0x10000); + } + } +} ``` </details>
The test now covers three sizes against the kept stretch: the title's
185 MB (split off the front, the rest free again), the whole stretch (an
exact fit) and one megabyte more (not handed out, the stretch let go for
the host's own search). It checks with VirtualQuery that the placeholder
has exactly the size asked and that the stretch is taken once.

The boundary cases are christopear's, from the review of this pull
request (run by them in five fresh Proton Experimental processes).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Author
Owner

@⁠gabrielcorreabsb on GitHub · 2026-12-06 03:36 UTC · original

Thanks for running this under Proton, it's the first outside check of the change. I folded your three boundary cases (185 MiB split, 256 MiB exact fit, 257 MiB refused and released) into the existing test as one test, so they run in turn on the shared window and need no #[ignore]; it's in 93b2ae8, crediting you. On my side (RX 9070 XT, Windows) the original failure at +0x19f4f58 went away on v1.1.6-amd plus this change. Like you say, neither of us has reproduced the fragmented launch deterministically, so I'd keep #385 open until its reporter confirms.

**@⁠gabrielcorreabsb** on GitHub · 2026-12-06 03:36 UTC · [original](https://github.com/gamingwithhomie/larp/pull/511#issuecomment-7450828210) Thanks for running this under Proton, it's the first outside check of the change. I folded your three boundary cases (185 MiB split, 256 MiB exact fit, 257 MiB refused and released) into the existing test as one test, so they run in turn on the shared window and need no `#[ignore]`; it's in 93b2ae8, crediting you. On my side (RX 9070 XT, Windows) the original failure at `+0x19f4f58` went away on `v1.1.6-amd` plus this change. Like you say, neither of us has reproduced the fragmented launch deterministically, so I'd keep #385 open until its reporter confirms.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/511/head:github-bot/gh-511'
git switch 'github-bot/gh-511'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-511'
git switch 'github-bot/gh-511'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-511'
git switch 'github-bot/gh-511'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-511'
git switch 'main'
git merge --squash 'github-bot/gh-511'
git switch 'main'
git merge --ff-only 'github-bot/gh-511'
git switch 'main'
git merge 'github-bot/gh-511'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.