d3d12: hold the title's address range below 2^37 (D3D12-TITLE-RANGE-RESERVE) #3154

Merged
enlight3d merged 1 commit from upstream-pr/3154 into main 2026-12-02 22:30:13 +00:00
enlight3d commented 2026-12-02 14:34:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-02T14:34:19Z.


Claimed in the coordination issue as: D3D12-TITLE-RANGE-RESERVE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7343459657)

What and why

A translated shader that reads or writes title memory by address finds it through the directory of 64 KiB pages below 2^37 (leonida_shader's MEMORY_PAGES; the last page is the empty one an access out of range lands on). So the title's memory (XMemVirtualAlloc) and the lists' upload arenas (leonida-gpu/src/upload.rs, behind allocate_upload) have to lie in [4 GiB, 2^37 - 64 KiB). Nothing kept that range for them: they got it because nothing else in the process asked for it first. A library that reserves address space early can take it. NVIDIA's cuInit reserves 68 GiB and 28 GiB at the first free 4 GiB-aligned places from 8 GiB, and a third-party DLSS frame generation unlocker calls it on RTX 30 cards. With it, 2 of the first 2 launches stopped while the world loaded (2 of 6 over the day, see In-game): the upload arenas fell from 29 to 8, allocate_upload returned null without a log line, and the title wrote through it. The same run with 64-128 GiB reserved in the process before cuInit (from outside, no access, nothing written) reached the world and ran its 420 s with 29 arenas.

This change:

  • leonida-gpu/src/title_range.rs (new): at the start of leonida run (apps/leonida/src/main.rs), every free place of [4 GiB, 2^37 - 64 KiB) is reserved with no access, in plain reservations of at most 1 GiB. Nothing is committed. A request takes the lowest place held that fits (the highest with MEM_TOP_DOWN, which the title does not use): the pieces under it are released, the request is made inside them at once, and what it leaves of them is reserved again; what the title frees in the range is held again too. If another thread takes the place between the release and the request, the next place is tried, 4 times at most. A request not placed in the range (no room, taken by other threads, or refused in a free place) is said in the log, rate-limited, and made of the host as before; one refused at its own address fails with its own error. A warning says when much of the range was taken before it was held. Plain reservations rather than placeholders: write-watched memory made in a placeholder cannot be turned back into one when it is freed (the host refuses it with ERROR_INVALID_PARAMETER, measured on Windows 11), and the title makes placeholders of its own in the range.
  • leonida-hle/src/modules/xmem.rs: XMemVirtualAlloc reservations of an address of our choice whose bounds take in the range, or at an address the range holds, are made there. The title's own VirtualAlloc is now ours (it was the host's, and still is for everything else): a reservation at an address the range holds is made there, so what the title frees and asks for again at the same address still works. Its VirtualFree gives what it releases in the range back to it.
  • leonida-gpu/src/upload.rs: the arenas come from the range, and end before the directory's empty last page (they could take it before). When no arena can be had, an upload comes from the process heap with one log line instead of null: shaders that read it by address read zeros, but the title no longer writes through null.
  • LEONIDA_TITLE_RANGE=0: nothing is held, and every request goes to the host as before. The arenas' end before the empty page and their heap fallback stay (they are right either way). d3d12/docs/switches.md regenerated (tools/d3d12_switches.py --out, then --check).

The title's images are mapped by the loader with ASLR far above 2^37 (checked in the runs' logs), so holding the range moves none of them.

Tested

Rust, on 854fc3441 (one commit on main bf7886746), from d3d12/, --target x86_64-pc-windows-msvc:

this branch
cargo test -p leonida-gpu --lib --locked -- title_range upload:: 15 passed, 3 runs in a row
cargo test -p leonida-hle --lib --locked -- xmem::tests::title_requests xmem::tests::without_a_held xmem::tests::the_titles_virtual_alloc 3 passed, 3 runs in a row
cargo test -p leonida-hle --locked 251 passed, 0 failed, 2 ignored (and 2 passed in its other test binaries)
cargo test -p leonida-gpu --locked 704 passed, 0 failed, 59 ignored
cargo test -p leonida-gpu --features diagnostics --locked 785 passed, 0 failed, 59 ignored (and 1 passed, 1 ignored in its other test binaries)
cargo clippy -p leonida-gpu -p leonida-hle -p leonida --all-targets --locked no warning in the changed lines (1 elsewhere, in host.rs)
same with --features leonida/diagnostics no warning in the changed lines (the others are in files this change does not touch)

Red and green. The new API makes the tests fail to compile on main itself, so they were first run on a seam with no change of behaviour (the range type holding nothing, XMemVirtualAlloc and the arenas going to the host as on main, an upload with no arena handed out as null). Each failed for the reason it states:

  • with the range held, another library's reservation in it was let in (a_held_range_keeps_other_reservations_out_until_dropped; title_requests_take_the_held_range_and_others_are_kept_out, as cuInit asks); an arena came from 4 GiB rather than the held range (arenas_take_the_held_range_and_keep_others_out);
  • requests of the title and of the arenas were not placed in the range, at the lowest place, at a held address, nor tried again in another place when another thread took the first (a_request_takes_the_lowest_place_and_the_rest_stays_held, a_request_at_a_held_address_is_made_there, a_place_taken_first_by_another_thread_is_tried_again_elsewhere, a_request_refused_in_a_free_place_is_not_made_elsewhere, the_lowest_place_that_fits_is_found, top_down_finds_the_highest_place);
  • with no arena, the upload was null (uploads_come_from_the_heap_when_no_arena_can_be_had);
  • the range reached into the directory's empty page (the_range_ends_where_the_directory_does);
  • LEONIDA_TITLE_RANGE=0 held the range (the_switch_is_on_unless_0).
    Tests added after the reviews: the_titles_virtual_alloc_reserves_at_held_addresses fails without the VirtualAlloc export (the host refuses the held address); a_refusal_at_an_address_keeps_its_error passes with and without the change and guards the error a refusal at the title's own address reports. without_a_held_range_* (xmem and upload) pass on main too: they check that, the range not held, requests go to the host as before. No test depends on global state: each holds its own range at its own place and releases what it reserves.

Thirteen mutations, each built and run on 854fc3441, are each caught by at least one test:

  • the reservation removed (the range holds nothing): caught by 9 tests (7 in leonida-gpu, 2 in leonida-hle);
  • the heap fallback removed (null again);
  • the range's end at 2^37 (the empty page in it), and at 2^38;
  • the switch inverted;
  • no second place tried when another thread takes the first;
  • a refusal in a free place taken for a theft and tried elsewhere;
  • what is freed not held again (give_back doing nothing; the title's VirtualFree not giving back);
  • the rest of a released piece not held again;
  • XMemVirtualAlloc not placed in the range; the title's VirtualAlloc not routed to it; the arenas not placed in it.
    Not covered by a unit test: that a refusal at the title's own address is final rather than asked of the host again (it would need the host to refuse a commit for want of room); the reviewers checked it by reading.

python -I scripts/check_no_game_data.py passes on the changed files.

In-game

RTX 3090, NVIDIA 621.40, Series S settings at 1080 lines, -FrameGen 2, the third-party unlocker installed as winmm.dll beside the executable, no outside reservation; one executable of this commit (diagnostics,dlss,mimalloc), the switch on (default) or off (LEONIDA_TITLE_RANGE=0). Runs of 420 s, or stopped 120 s after reaching the world; nothing measured beyond what is listed.

Launches Crashed while the world loaded Title pool placed Upload arenas (lines) Range held
without this change (main and main + #3062 this morning; this commit with =0 this afternoon): 6 2 (both this morning, at 83-85 s) above the range (at 140-144 GiB) in 3 of the 5 where it was logged down to 8 when it was no
this change, default: 4 0 always in the range (about 44-46 GiB) 26-29 yes: 124-126 pieces, about 126,970 MB, every time
  • The full run with this change (420 s) reached the world at 74 s, logged no line saying a request or an arena was not placed in the range, no crash report, and frame generation presented two frames per rendered frame throughout (the window title showed 25 / 49, 24 / 47 and 23 / 46 frames a second, rendered / shown, at 120, 280 and 400 s).
  • Without the change, where the library's reservation lands varies from one launch to the next, and a squeezed set of arenas does not always break the load: one launch with the title pool pushed high and only 8 arenas still reached the world. The crash is the case where the title then needs an upload allocate_upload can no longer place.
  • The fall-back to the heap instead of a null upload, which applies with the switch off too, was not reached in these runs.

Overlaps

  • #2856 (D3D12-XMEM-GRAPHICS-37BIT, xmem.rs, open) caps graphics reservations below the same end (GPU_DIRECTORY_END = 2^37 - 64 KiB) and falls back to the console's range when that fails. The two agree: a graphics request's bounds are the held range, so it is placed there; with no room, in_title_range gives None, the host's 37-bit attempt fails and #2856's fallback applies. Textually, the second to merge resolves three spots in xmem.rs: both insert after address_range (keep both blocks), both change the VirtualAlloc2 call (keep let held = in_title_range(...), make it let mut at = held.unwrap_or_else(|| unsafe { VirtualAlloc2(...) });, then #2856's fallback block unchanged), and both insert at the start of mod tests (keep both). After both, GPU_DIRECTORY_END can be title_range::HIGH, which a test ties to MEMORY_PAGES. One difference of intent: #2856 leaves CPU-only memory to the whole console range, while the held range serves it first, as the host placed it on main (lowest first from 4 GiB).
  • #2643 (zmasi, D3D12-RT-FG-ARENA-VA-STORM, open) also pre-reserves low space for uploads from main.rs and adds a heap fallback in upload.rs; whichever merges second has to take the other into account.

GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40, Windows 11 Pro (build 26300): the Rust suites and the in-game runs above.

AI-assisted: yes (Claude Code, Claude Opus 5.5; reviews by two independent agents, Claude Opus 5.5 and Claude Fable 5.2)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator change.)

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5608565914 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3154). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-12-02T14:34:19Z. --- Claimed in the coordination issue as: D3D12-TITLE-RANGE-RESERVE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7343459657) ### What and why A translated shader that reads or writes title memory by address finds it through the directory of 64 KiB pages below 2^37 (`leonida_shader`'s `MEMORY_PAGES`; the last page is the empty one an access out of range lands on). So the title's memory (`XMemVirtualAlloc`) and the lists' upload arenas (`leonida-gpu/src/upload.rs`, behind `allocate_upload`) have to lie in [4 GiB, 2^37 - 64 KiB). Nothing kept that range for them: they got it because nothing else in the process asked for it first. A library that reserves address space early can take it. NVIDIA's `cuInit` reserves 68 GiB and 28 GiB at the first free 4 GiB-aligned places from 8 GiB, and a third-party DLSS frame generation unlocker calls it on RTX 30 cards. With it, 2 of the first 2 launches stopped while the world loaded (2 of 6 over the day, see In-game): the upload arenas fell from 29 to 8, `allocate_upload` returned null without a log line, and the title wrote through it. The same run with 64-128 GiB reserved in the process before `cuInit` (from outside, no access, nothing written) reached the world and ran its 420 s with 29 arenas. This change: - `leonida-gpu/src/title_range.rs` (new): at the start of `leonida run` (`apps/leonida/src/main.rs`), every free place of [4 GiB, 2^37 - 64 KiB) is reserved with no access, in plain reservations of at most 1 GiB. Nothing is committed. A request takes the lowest place held that fits (the highest with `MEM_TOP_DOWN`, which the title does not use): the pieces under it are released, the request is made inside them at once, and what it leaves of them is reserved again; what the title frees in the range is held again too. If another thread takes the place between the release and the request, the next place is tried, 4 times at most. A request not placed in the range (no room, taken by other threads, or refused in a free place) is said in the log, rate-limited, and made of the host as before; one refused at its own address fails with its own error. A warning says when much of the range was taken before it was held. Plain reservations rather than placeholders: write-watched memory made in a placeholder cannot be turned back into one when it is freed (the host refuses it with `ERROR_INVALID_PARAMETER`, measured on Windows 11), and the title makes placeholders of its own in the range. - `leonida-hle/src/modules/xmem.rs`: `XMemVirtualAlloc` reservations of an address of our choice whose bounds take in the range, or at an address the range holds, are made there. The title's own `VirtualAlloc` is now ours (it was the host's, and still is for everything else): a reservation at an address the range holds is made there, so what the title frees and asks for again at the same address still works. Its `VirtualFree` gives what it releases in the range back to it. - `leonida-gpu/src/upload.rs`: the arenas come from the range, and end before the directory's empty last page (they could take it before). When no arena can be had, an upload comes from the process heap with one log line instead of null: shaders that read it by address read zeros, but the title no longer writes through null. - `LEONIDA_TITLE_RANGE=0`: nothing is held, and every request goes to the host as before. The arenas' end before the empty page and their heap fallback stay (they are right either way). `d3d12/docs/switches.md` regenerated (`tools/d3d12_switches.py --out`, then `--check`). The title's images are mapped by the loader with ASLR far above 2^37 (checked in the runs' logs), so holding the range moves none of them. ### Tested **Rust**, on `854fc3441` (one commit on `main` `bf7886746`), from `d3d12/`, `--target x86_64-pc-windows-msvc`: | | this branch | |---|---| | `cargo test -p leonida-gpu --lib --locked -- title_range upload::` | 15 passed, 3 runs in a row | | `cargo test -p leonida-hle --lib --locked -- xmem::tests::title_requests xmem::tests::without_a_held xmem::tests::the_titles_virtual_alloc` | 3 passed, 3 runs in a row | | `cargo test -p leonida-hle --locked` | 251 passed, 0 failed, 2 ignored (and 2 passed in its other test binaries) | | `cargo test -p leonida-gpu --locked` | 704 passed, 0 failed, 59 ignored | | `cargo test -p leonida-gpu --features diagnostics --locked` | 785 passed, 0 failed, 59 ignored (and 1 passed, 1 ignored in its other test binaries) | | `cargo clippy -p leonida-gpu -p leonida-hle -p leonida --all-targets --locked` | no warning in the changed lines (1 elsewhere, in `host.rs`) | | same with `--features leonida/diagnostics` | no warning in the changed lines (the others are in files this change does not touch) | Red and green. The new API makes the tests fail to compile on `main` itself, so they were first run on a seam with no change of behaviour (the range type holding nothing, `XMemVirtualAlloc` and the arenas going to the host as on `main`, an upload with no arena handed out as null). Each failed for the reason it states: - with the range held, another library's reservation in it was let in (`a_held_range_keeps_other_reservations_out_until_dropped`; `title_requests_take_the_held_range_and_others_are_kept_out`, as `cuInit` asks); an arena came from 4 GiB rather than the held range (`arenas_take_the_held_range_and_keep_others_out`); - requests of the title and of the arenas were not placed in the range, at the lowest place, at a held address, nor tried again in another place when another thread took the first (`a_request_takes_the_lowest_place_and_the_rest_stays_held`, `a_request_at_a_held_address_is_made_there`, `a_place_taken_first_by_another_thread_is_tried_again_elsewhere`, `a_request_refused_in_a_free_place_is_not_made_elsewhere`, `the_lowest_place_that_fits_is_found`, `top_down_finds_the_highest_place`); - with no arena, the upload was null (`uploads_come_from_the_heap_when_no_arena_can_be_had`); - the range reached into the directory's empty page (`the_range_ends_where_the_directory_does`); - `LEONIDA_TITLE_RANGE=0` held the range (`the_switch_is_on_unless_0`). Tests added after the reviews: `the_titles_virtual_alloc_reserves_at_held_addresses` fails without the `VirtualAlloc` export (the host refuses the held address); `a_refusal_at_an_address_keeps_its_error` passes with and without the change and guards the error a refusal at the title's own address reports. `without_a_held_range_*` (xmem and upload) pass on `main` too: they check that, the range not held, requests go to the host as before. No test depends on global state: each holds its own range at its own place and releases what it reserves. Thirteen mutations, each built and run on `854fc3441`, are each caught by at least one test: - the reservation removed (the range holds nothing): caught by 9 tests (7 in `leonida-gpu`, 2 in `leonida-hle`); - the heap fallback removed (null again); - the range's end at 2^37 (the empty page in it), and at 2^38; - the switch inverted; - no second place tried when another thread takes the first; - a refusal in a free place taken for a theft and tried elsewhere; - what is freed not held again (`give_back` doing nothing; the title's `VirtualFree` not giving back); - the rest of a released piece not held again; - `XMemVirtualAlloc` not placed in the range; the title's `VirtualAlloc` not routed to it; the arenas not placed in it. Not covered by a unit test: that a refusal at the title's own address is final rather than asked of the host again (it would need the host to refuse a commit for want of room); the reviewers checked it by reading. `python -I scripts/check_no_game_data.py` passes on the changed files. ### In-game RTX 3090, NVIDIA 621.40, Series S settings at 1080 lines, `-FrameGen 2`, the third-party unlocker installed as `winmm.dll` beside the executable, no outside reservation; one executable of this commit (`diagnostics,dlss,mimalloc`), the switch on (default) or off (`LEONIDA_TITLE_RANGE=0`). Runs of 420 s, or stopped 120 s after reaching the world; nothing measured beyond what is listed. | Launches | Crashed while the world loaded | Title pool placed | Upload arenas (lines) | Range held | |---|---|---|---|---| | without this change (`main` and `main` + #3062 this morning; this commit with `=0` this afternoon): 6 | **2** (both this morning, at 83-85 s) | above the range (at 140-144 GiB) in 3 of the 5 where it was logged | down to 8 when it was | no | | this change, default: 4 | **0** | always in the range (about 44-46 GiB) | 26-29 | yes: 124-126 pieces, about 126,970 MB, every time | - The full run with this change (420 s) reached the world at 74 s, logged no line saying a request or an arena was not placed in the range, no crash report, and frame generation presented two frames per rendered frame throughout (the window title showed 25 / 49, 24 / 47 and 23 / 46 frames a second, rendered / shown, at 120, 280 and 400 s). - Without the change, where the library's reservation lands varies from one launch to the next, and a squeezed set of arenas does not always break the load: one launch with the title pool pushed high and only 8 arenas still reached the world. The crash is the case where the title then needs an upload `allocate_upload` can no longer place. - The fall-back to the heap instead of a null upload, which applies with the switch off too, was not reached in these runs. ### Overlaps - #2856 (D3D12-XMEM-GRAPHICS-37BIT, `xmem.rs`, open) caps graphics reservations below the same end (`GPU_DIRECTORY_END` = 2^37 - 64 KiB) and falls back to the console's range when that fails. The two agree: a graphics request's bounds are the held range, so it is placed there; with no room, `in_title_range` gives `None`, the host's 37-bit attempt fails and #2856's fallback applies. Textually, the second to merge resolves three spots in `xmem.rs`: both insert after `address_range` (keep both blocks), both change the `VirtualAlloc2` call (keep `let held = in_title_range(...)`, make it `let mut at = held.unwrap_or_else(|| unsafe { VirtualAlloc2(...) });`, then #2856's fallback block unchanged), and both insert at the start of `mod tests` (keep both). After both, `GPU_DIRECTORY_END` can be `title_range::HIGH`, which a test ties to `MEMORY_PAGES`. One difference of intent: #2856 leaves CPU-only memory to the whole console range, while the held range serves it first, as the host placed it on `main` (lowest first from 4 GiB). - #2643 (zmasi, D3D12-RT-FG-ARENA-VA-STORM, open) also pre-reserves low space for uploads from `main.rs` and adds a heap fallback in `upload.rs`; whichever merges second has to take the other into account. GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40, Windows 11 Pro (build 26300): the Rust suites and the in-game runs above. AI-assisted: yes (Claude Code, Claude Opus 5.5; reviews by two independent agents, Claude Opus 5.5 and Claude Fable 5.2) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (No translator change.) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Kal0s-design commented 2026-12-02 15:55:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T15:55:49Z.


Ran natively on Windows 11, head 854fc3441 merged into current main (83046a713, clean merge). main has since moved to 3c106eb2f. The PR still merges cleanly there, and none of the six files it touches changed in between. CPU-only tests, debug build unless noted.

Suites on the merge

Command Result
cargo test -p leonida-gpu --lib -- title_range upload:: 15 passed, 3 runs in a row
cargo test -p leonida-hle --lib (the 3 new xmem tests) 3 passed
cargo test -p leonida-gpu -p leonida-hle 709 passed, 59 ignored (gpu); 251 passed, 2 ignored + 1 + 1 (hle)
cargo test -p leonida-gpu --features diagnostics 790 passed, 59 ignored (+1 passed, 1 ignored in the other binaries)
cargo test -p leonida 43 + 2 + 2 + 1 passed, 0 failed
cargo clippy -p leonida-gpu -p leonida-hle -p leonida --all-targets only the known unused_mut in host.rs
same with --features leonida/diagnostics none in the changed lines (13 elsewhere, all in code this change does not touch)

Extra probes

Throwaway tests, not pushed. Each holds its own test range far from the others, or the real [4 GiB, 2^37 - 64 KiB) alone in the process.

The stated case holds up. A cuInit-like request tries fixed 4 GiB-aligned addresses from 8 GiB, as the description says.

68 GiB lands at 28 GiB lands at Range held
range not held (as on main) 8 GiB (1st try) 76 GiB (18th try) n/a
request made first, then Range::reserve(LOW, HIGH) same same 28,671 of 126,975 MB, so the 15/16 warning in keep() would fire
range held first 2^37 (31st try) 196 GiB (48th try) 126,975 MB in 124 pieces, about 0.1-0.2 ms to hold

Three things I found while probing the edges.

  1. give_back holds again only the free run that starts at the freed address. The title's VirtualFree runs before freed() takes the lock, so another thread can reserve in between. Measured with a 1 MiB block.
Foreign 64 KiB reservation made between VirtualFree and give_back Held again Left free and not held
at the freed address 0 KiB 960 KiB
in the middle of the block 512 KiB 448 KiB

The gap stays unheld until a later free next to it happens to cover it. Holding again over the freed allocation's whole span (its size, queried before the free) would use hold's own skip of what others took. It needs another thread to reserve in that short window, so it should be rare.

  1. A take releases every piece under the place, and pieces are up to 1 GiB. A 64 KiB request opened a 1 GiB window. A foreign 64 KiB reservation made 900 MiB away from the request, inside the callback, was let in. Everything else was held again afterwards (0 KiB lost). Then a stress run with 4 title threads taking 64 KiB-1 MiB blocks and one thread reserving 64 KiB anywhere in a 4 GiB test range in a loop (adversarial, nothing like a one-time cuInit).
Foreign thread Its tries that found a free place Title requests that missed
releases each reservation at once 6,782 of 6,916 0 of 1,600
keeps up to 16 8,106 of 8,299 1 of 1,600 (Refused, the place was free again when checked, so it would go to the host)
keeps everything (3 runs) about 3.98-3.99 GiB of the 4 GiB taken 1,563-1,590 of 1,600

So the range keeps out a reservation made while no title request is in flight, not one made alongside the title's own allocations. Splitting only the piece under the request (or a smaller STEP) would narrow the window.

  1. Commit-only at a held address now succeeds. VirtualAlloc(at, 64 KiB, MEM_COMMIT, PAGE_READWRITE) with no MEM_RESERVE goes to the host and commits inside our own no-access piece.
Case Result
title's VirtualAlloc (virtual_alloc_in), range not held (as on main) null, error 487
title's VirtualAlloc (virtual_alloc_in), range held succeeds
raw VirtualAlloc on a held test range committed (VirtualQuery), a byte written
then a take in the same 1 GiB piece the page is back to reserved, its contents gone

Only buggy code commits without reserving, so this is low priority. But the new behaviour is "works, then silently loses the page" where main fails at once.

Smaller notes, measured.

  • Pieces are never joined. After 20,000 random takes and frees in a 16 GiB range everything came back (16 GiB of 16 GiB held), but in 3,039 pieces instead of 16. take cost grows with the piece count. Release build 2 us at 8 pieces, 11 us at 1,008, 24 us at 4,008 (debug 3, 28, 112 us). That is a model of fragmentation. The real range starts at 124 pieces, and how many a long session reaches is not measured.
  • A request Where::At(HIGH - 64 KiB) of 128 KiB is made and ends at 2^37, so it covers the directory's empty page. Only an address the title itself asks for can do that (the arenas and Anywhere requests stop below HIGH).

Not checked: any in-game run, the title's own VirtualAlloc2 or MapViewOfFile3 at held addresses (not routed through the range), other GPU vendors' drivers, and the merge with #2856 or #2643.

GPUs tested: n/a (CPU-only tests).

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7351164358 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3154#issuecomment-7351164358). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-02T15:55:49Z. --- Ran natively on Windows 11, head `854fc3441` merged into current `main` (`83046a713`, clean merge). `main` has since moved to `3c106eb2f`. The PR still merges cleanly there, and none of the six files it touches changed in between. CPU-only tests, debug build unless noted. **Suites on the merge** | Command | Result | |---|---| | `cargo test -p leonida-gpu --lib -- title_range upload::` | 15 passed, 3 runs in a row | | `cargo test -p leonida-hle --lib` (the 3 new xmem tests) | 3 passed | | `cargo test -p leonida-gpu -p leonida-hle` | 709 passed, 59 ignored (gpu); 251 passed, 2 ignored + 1 + 1 (hle) | | `cargo test -p leonida-gpu --features diagnostics` | 790 passed, 59 ignored (+1 passed, 1 ignored in the other binaries) | | `cargo test -p leonida` | 43 + 2 + 2 + 1 passed, 0 failed | | `cargo clippy -p leonida-gpu -p leonida-hle -p leonida --all-targets` | only the known `unused_mut` in `host.rs` | | same with `--features leonida/diagnostics` | none in the changed lines (13 elsewhere, all in code this change does not touch) | **Extra probes** Throwaway tests, not pushed. Each holds its own test range far from the others, or the real [4 GiB, 2^37 - 64 KiB) alone in the process. The stated case holds up. A cuInit-like request tries fixed 4 GiB-aligned addresses from 8 GiB, as the description says. | | 68 GiB lands at | 28 GiB lands at | Range held | |---|---|---|---| | range not held (as on `main`) | 8 GiB (1st try) | 76 GiB (18th try) | n/a | | request made first, then `Range::reserve(LOW, HIGH)` | same | same | 28,671 of 126,975 MB, so the 15/16 warning in `keep()` would fire | | range held first | 2^37 (31st try) | 196 GiB (48th try) | 126,975 MB in 124 pieces, about 0.1-0.2 ms to hold | Three things I found while probing the edges. 1. `give_back` holds again only the free run that starts at the freed address. The title's `VirtualFree` runs before `freed()` takes the lock, so another thread can reserve in between. Measured with a 1 MiB block. | Foreign 64 KiB reservation made between `VirtualFree` and `give_back` | Held again | Left free and not held | |---|---|---| | at the freed address | 0 KiB | 960 KiB | | in the middle of the block | 512 KiB | 448 KiB | The gap stays unheld until a later free next to it happens to cover it. Holding again over the freed allocation's whole span (its size, queried before the free) would use `hold`'s own skip of what others took. It needs another thread to reserve in that short window, so it should be rare. 2. A `take` releases every piece under the place, and pieces are up to 1 GiB. A 64 KiB request opened a 1 GiB window. A foreign 64 KiB reservation made 900 MiB away from the request, inside the callback, was let in. Everything else was held again afterwards (0 KiB lost). Then a stress run with 4 title threads taking 64 KiB-1 MiB blocks and one thread reserving 64 KiB anywhere in a 4 GiB test range in a loop (adversarial, nothing like a one-time `cuInit`). | Foreign thread | Its tries that found a free place | Title requests that missed | |---|---|---| | releases each reservation at once | 6,782 of 6,916 | 0 of 1,600 | | keeps up to 16 | 8,106 of 8,299 | 1 of 1,600 (`Refused`, the place was free again when checked, so it would go to the host) | | keeps everything (3 runs) | about 3.98-3.99 GiB of the 4 GiB taken | 1,563-1,590 of 1,600 | So the range keeps out a reservation made while no title request is in flight, not one made alongside the title's own allocations. Splitting only the piece under the request (or a smaller `STEP`) would narrow the window. 3. Commit-only at a held address now succeeds. `VirtualAlloc(at, 64 KiB, MEM_COMMIT, PAGE_READWRITE)` with no `MEM_RESERVE` goes to the host and commits inside our own no-access piece. | Case | Result | |---|---| | title's `VirtualAlloc` (`virtual_alloc_in`), range not held (as on `main`) | null, error 487 | | title's `VirtualAlloc` (`virtual_alloc_in`), range held | succeeds | | raw `VirtualAlloc` on a held test range | committed (`VirtualQuery`), a byte written | | then a `take` in the same 1 GiB piece | the page is back to reserved, its contents gone | Only buggy code commits without reserving, so this is low priority. But the new behaviour is "works, then silently loses the page" where `main` fails at once. Smaller notes, measured. - Pieces are never joined. After 20,000 random takes and frees in a 16 GiB range everything came back (16 GiB of 16 GiB held), but in 3,039 pieces instead of 16. `take` cost grows with the piece count. Release build 2 us at 8 pieces, 11 us at 1,008, 24 us at 4,008 (debug 3, 28, 112 us). That is a model of fragmentation. The real range starts at 124 pieces, and how many a long session reaches is not measured. - A request `Where::At(HIGH - 64 KiB)` of 128 KiB is made and ends at 2^37, so it covers the directory's empty page. Only an address the title itself asks for can do that (the arenas and `Anywhere` requests stop below `HIGH`). Not checked: any in-game run, the title's own `VirtualAlloc2` or `MapViewOfFile3` at held addresses (not routed through the range), other GPU vendors' drivers, and the merge with #2856 or #2643. GPUs tested: n/a (CPU-only tests). AI-assisted: yes (Claude Code).
fiestorra commented 2026-12-02 16:24:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T16:24:49Z.


Verified here: it applies, and all ten of the new title_range tests pass.

git apply of refs/pull/3154/head on main   (6 files, including the new title_range.rs)

cargo test -p leonida-gpu --lib -- title_range     exit 0
  title_range::tests::the_range_ends_where_the_directory_does ... ok
  title_range::tests::the_lowest_place_that_fits_is_found ... ok
  title_range::tests::top_down_finds_the_highest_place ... ok
  title_range::tests::a_held_range_keeps_other_reservations_out_until_dropped ... ok
  title_range::tests::a_request_takes_the_lowest_place_and_the_rest_stays_held ... ok
  title_range::tests::a_request_at_a_held_address_is_made_there ... ok
  title_range::tests::a_place_taken_first_by_another_thread_is_tried_again_elsewhere ... ok
  title_range::tests::a_request_refused_in_a_free_place_is_not_made_elsewhere ... ok
  title_range::tests::a_refusal_at_an_address_keeps_its_error ... ok
  title_range::tests::the_switch_is_on_unless_0 ... ok
  test result: ok. 10 passed; 0 failed; 0 ignored

What the set covers is the right set for an address reservation: where the range ends, lowest and highest fit, a held range keeping other reservations out until dropped, a request at an address already held, two threads racing for the same place, and the refusal that must not be retried elsewhere - the last one is the one that would corrupt a title's memory if it were wrong, and it is a test.

the_switch_is_on_unless_0 is worth naming for a merge: it says this is on by default, so what a player gets is the reserved range, not the old behaviour - the opposite of the usual "off until measured" and worth a line in the body if it is deliberate.

What it does not cover: the title's real mappings. Everything above is the reservation bookkeeping; whether holding the range below 2^37 keeps the title's own load addresses inside it needs a run, and this machine cannot start the title. A -Log run that shows the range line would settle it.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7351907308 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3154#issuecomment-7351907308). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T16:24:49Z. --- **Verified here: it applies, and all ten of the new `title_range` tests pass.** ``` git apply of refs/pull/3154/head on main (6 files, including the new title_range.rs) cargo test -p leonida-gpu --lib -- title_range exit 0 title_range::tests::the_range_ends_where_the_directory_does ... ok title_range::tests::the_lowest_place_that_fits_is_found ... ok title_range::tests::top_down_finds_the_highest_place ... ok title_range::tests::a_held_range_keeps_other_reservations_out_until_dropped ... ok title_range::tests::a_request_takes_the_lowest_place_and_the_rest_stays_held ... ok title_range::tests::a_request_at_a_held_address_is_made_there ... ok title_range::tests::a_place_taken_first_by_another_thread_is_tried_again_elsewhere ... ok title_range::tests::a_request_refused_in_a_free_place_is_not_made_elsewhere ... ok title_range::tests::a_refusal_at_an_address_keeps_its_error ... ok title_range::tests::the_switch_is_on_unless_0 ... ok test result: ok. 10 passed; 0 failed; 0 ignored ``` **What the set covers is the right set for an address reservation**: **where the range ends**, **lowest and highest fit**, **a held range keeping other reservations out until dropped**, **a request at an address already held**, **two threads racing for the same place**, and **the refusal that must not be retried elsewhere** - **the last one is the one that would corrupt a title's memory if it were wrong, and it is a test.** **`the_switch_is_on_unless_0` is worth naming for a merge**: it says **this is on by default**, so what a player gets is the reserved range, not the old behaviour - **the opposite of the usual "off until measured" and worth a line in the body if it is deliberate.** **What it does not cover**: **the title's real mappings.** Everything above is the reservation bookkeeping; **whether holding the range below 2^37 keeps the title's own load addresses inside it needs a run**, and this machine cannot start the title. **A `-Log` run that shows the range line would settle it.** **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
MoonEyes commented 2026-12-02 18:18:15 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T18:18:15Z.


Verification of #3154 (third party, code review on Linux; not built: Windows-only leonida-gpu / leonida-hle)

  • Head verified: 854fc3441c70fcafc108d900e21fed8ffeebed66 (6 files, +1013/-18, new title_range.rs).
  • Merge base bf7886746. git merge-tree --write-tree origin/main pr/3154 against main 45dc41f95 is clean
    (83 commits behind). CI on the head: 6/6 success.
  • python -I scripts/check_no_game_data.py on the 6 files (on the merge): exit 0.
  • Cross-PR: git merge-tree pr/3154 pr/2856 conflicts in leonida-hle/src/modules/xmem.rs. The body's
    "Overlaps" section explains how the two agree in behaviour. Whichever lands second needs a textual merge. It
    merges cleanly with #2982 and #3113.

Review

  • Root cause: shaders address title memory through the 64 KiB-page directory below 2^37, and nothing stopped a
    library (the body names CUDA's cuInit) from reserving that range first. Reserving the free parts of
    [4 GiB, 2^37 - 64 KiB) at start (keep(), called from main.rs:248) and carving allocations out of the
    reservation fixes the cause. the_range_ends_where_the_directory_does ties HIGH to
    leonida_shader::emit::MEMORY_PAGES, so the two cannot drift apart.
  • Range::take (title_range.rs) releases the held pieces that overlap the target, allocates, then re-holds
    the remainders. Two points:
    1. Race window wider than needed: release frees whole held pieces, up to 1 GiB each (STEP), not just
      [start, stop). Between that VirtualFree and the re-hold, any thread's VirtualAlloc(NULL, ..) or a
      library's placed reservation can land anywhere in that GiB, which is what the range exists to prevent. The
      ATTEMPTS loop recovers for the caller but not for the stranger who got in. Splitting the piece first, so
      that only the requested span is released (VirtualFree(MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER), or
      placeholders with VirtualAlloc2(MEM_REPLACE_PLACEHOLDER)), would close it. Non-blocking if the authors
      judge the window rare, but the code should say so.
    2. Hot-path cost: every XMemVirtualAlloc and every upload arena now runs under the one KEPT mutex with
      fit (it rebuilds a Vec of runs over all pieces on each call), plus VirtualFree, VirtualAlloc and one
      VirtualQuery per re-held piece. The queries are on free or reserved regions, not on the committed pools that
      #3113 measured at 21-36 ms, so they should be cheap. A per-call timing in findings.md would confirm it.
      Fragmentation grows pieces over a session, and fit is linear in it.
  • LEONIDA_TITLE_RANGE=0 turns it off, as switches.md documents. The held < 15/16 warning makes a library
    that loaded first visible in the log.
  • Tests: ten title_range tests (fit lowest and top-down, foreign reservations refused at and inside a held
    range, give-back) and upload tests that place arenas inside the range. Kal0s-design and fiestorra ran them
    natively. The body's in-game runs show the crash case.

Process

  • Claim and AI disclosure (Claude Opus 5.5, reviewers Claude Opus and Claude Fable 5.2) are present.

Verdict: ready on review (not built here). The release window (point 1) is worth a follow-up or a code
comment. Coordinate the xmem.rs merge with #2856.

AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.


Generated by Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7354329908 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3154#issuecomment-7354329908). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T18:18:15Z. --- **Verification of #3154 (third party, code review on Linux; not built: Windows-only `leonida-gpu` / `leonida-hle`)** - Head verified: `854fc3441c70fcafc108d900e21fed8ffeebed66` (6 files, +1013/-18, new `title_range.rs`). - Merge base `bf7886746`. `git merge-tree --write-tree origin/main pr/3154` against `main` `45dc41f95` is clean (83 commits behind). CI on the head: 6/6 success. - `python -I scripts/check_no_game_data.py` on the 6 files (on the merge): exit 0. - Cross-PR: `git merge-tree pr/3154 pr/2856` **conflicts** in `leonida-hle/src/modules/xmem.rs`. The body's "Overlaps" section explains how the two agree in behaviour. Whichever lands second needs a textual merge. It merges cleanly with #2982 and #3113. **Review** - Root cause: shaders address title memory through the 64 KiB-page directory below 2^37, and nothing stopped a library (the body names CUDA's `cuInit`) from reserving that range first. Reserving the free parts of `[4 GiB, 2^37 - 64 KiB)` at start (`keep()`, called from `main.rs:248`) and carving allocations out of the reservation fixes the cause. `the_range_ends_where_the_directory_does` ties `HIGH` to `leonida_shader::emit::MEMORY_PAGES`, so the two cannot drift apart. - `Range::take` (`title_range.rs`) releases the held pieces that overlap the target, allocates, then re-holds the remainders. Two points: 1. **Race window wider than needed:** `release` frees whole held pieces, up to 1 GiB each (`STEP`), not just `[start, stop)`. Between that `VirtualFree` and the re-`hold`, any thread's `VirtualAlloc(NULL, ..)` or a library's placed reservation can land anywhere in that GiB, which is what the range exists to prevent. The `ATTEMPTS` loop recovers for the caller but not for the stranger who got in. Splitting the piece first, so that only the requested span is released (`VirtualFree(MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)`, or placeholders with `VirtualAlloc2(MEM_REPLACE_PLACEHOLDER)`), would close it. Non-blocking if the authors judge the window rare, but the code should say so. 2. **Hot-path cost:** every `XMemVirtualAlloc` and every upload arena now runs under the one `KEPT` mutex with `fit` (it rebuilds a `Vec` of runs over all pieces on each call), plus `VirtualFree`, `VirtualAlloc` and one `VirtualQuery` per re-held piece. The queries are on free or reserved regions, not on the committed pools that #3113 measured at 21-36 ms, so they should be cheap. A per-call timing in `findings.md` would confirm it. Fragmentation grows `pieces` over a session, and `fit` is linear in it. - `LEONIDA_TITLE_RANGE=0` turns it off, as `switches.md` documents. The `held < 15/16` warning makes a library that loaded first visible in the log. - Tests: ten `title_range` tests (fit lowest and top-down, foreign reservations refused at and inside a held range, give-back) and upload tests that place arenas inside the range. Kal0s-design and fiestorra ran them natively. The body's in-game runs show the crash case. **Process** - Claim and AI disclosure (Claude Opus 5.5, reviewers Claude Opus and Claude Fable 5.2) are present. **Verdict: ready on review** (not built here). The release window (point 1) is worth a follow-up or a code comment. Coordinate the `xmem.rs` merge with #2856. AI-assisted verification: Claude Opus (Claude Code), separate agent per batch. --- _Generated by [Claude Code](https://claude.ai/code)_
lixonik commented 2026-12-02 19:23:27 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-02T19:23:27Z.


RUN (in game) -- unit tests + 300 s render-path gate run for PR #3154:

  • PR head SHA: 854fc3441
  • Merged onto main SHA: f1a559851 (git merge --no-ff, clean merge)
  • Clean-room & switch-table checks:
    • python -I scripts/check_no_game_data.py PASS
    • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md PASS
  • Unit tests (Windows 10 x64, Rust 1.98.1):
    • cargo test -p leonida-gpu --features diagnostics --lib --locked title_range:: -- 10 passed, 0 failed
    • cargo test -p leonida-hle --lib --locked xmem:: -- 27 passed, 0 failed, 1 ignored
  • Build: diag profile (diagnostics,dlss,mimalloc), Series S profile, DLSS on, Frame Gen off, same restored save snapshot
  • GPU / Driver / OS: NVIDIA GeForce RTX 5070 (12 GB), driver 595.79, Windows 10 x64
  • Observed reservation log:
    • INFO leonida_gpu::title_range: title range held: the free places of [4 GiB, 128 GiB - 64 KiB) reserved with no access, for the title's memory and the lists' upload arenas (LEONIDA_TITLE_RANGE) held_mb=126975 pieces=124
Run Time in world World samples Device lost / crashes / errors Median rendered FPS (ms) Slowest / fastest tenth (fps) Median draws
PR #3154 (854fc3441 on f1a559851) 315 s (437.0 s total) 64 0 / 0 / 0 27.0 fps (37.0 ms) 22.2 / 28.3 4,404
main alone (f1a559851) 303.2 s (480.0 s total) 62 0 / 0 / 0 27.2 fps (36.8 ms) 21.8 / 28.7 4,385

Loaded cleanly into the world with held_mb=126975 reserved below 2^37, stayed up past 300 s in-world with 0 device lost / 0 crashes / 0 ERROR lines, and closed cleanly. Rendered FPS matches main (27.0 vs 27.2 fps).

Verdict: PASS.

AI-assisted: yes (Antigravity).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7356097140 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3154#issuecomment-7356097140). Originally posted by [lixonik](https://github.com/lixonik) on 2026-12-02T19:23:27Z. --- RUN (in game) -- unit tests + 300 s render-path gate run for PR #3154: - **PR head SHA:** `854fc3441` - **Merged onto `main` SHA:** `f1a559851` (`git merge --no-ff`, clean merge) - **Clean-room & switch-table checks:** - `python -I scripts/check_no_game_data.py` PASS - `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md` PASS - **Unit tests (Windows 10 x64, Rust 1.98.1):** - `cargo test -p leonida-gpu --features diagnostics --lib --locked title_range::` -- **10 passed, 0 failed** - `cargo test -p leonida-hle --lib --locked xmem::` -- **27 passed, 0 failed, 1 ignored** - **Build:** `diag` profile (`diagnostics,dlss,mimalloc`), Series S profile, DLSS on, Frame Gen off, same restored save snapshot - **GPU / Driver / OS:** NVIDIA GeForce RTX 5070 (12 GB), driver 595.79, Windows 10 x64 - **Observed reservation log:** - `INFO leonida_gpu::title_range: title range held: the free places of [4 GiB, 128 GiB - 64 KiB) reserved with no access, for the title's memory and the lists' upload arenas (LEONIDA_TITLE_RANGE) held_mb=126975 pieces=124` | Run | Time in world | World samples | Device lost / crashes / errors | Median rendered FPS (ms) | Slowest / fastest tenth (fps) | Median draws | | --- | ---: | ---: | --- | ---: | ---: | ---: | | **PR #3154 (`854fc3441` on `f1a559851`)** | 315 s (437.0 s total) | 64 | **0 / 0 / 0** | **27.0 fps** (37.0 ms) | 22.2 / 28.3 | 4,404 | | **`main` alone (`f1a559851`)** | 303.2 s (480.0 s total) | 62 | **0 / 0 / 0** | **27.2 fps** (36.8 ms) | 21.8 / 28.7 | 4,385 | Loaded cleanly into the world with `held_mb=126975` reserved below `2^37`, stayed up past 300 s in-world with 0 device lost / 0 crashes / 0 `ERROR` lines, and closed cleanly. Rendered FPS matches `main` (`27.0` vs `27.2` fps). Verdict: **PASS**. AI-assisted: yes (Antigravity).
MoonEyes commented 2026-12-02 22:17:41 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T22:17:41Z.


Merging. Head 854fc3441 merges cleanly with main f3865cae8 and does not force a rebase of #2566. In-game run on this head by lixonik (NVIDIA GeForce RTX 5070 (12 GB), driver 595.79, Windows 10 x64, 315 s in the world, 0 device lost: https://github.com/ProjectLeonida/mandy/pull/3154#issuecomment-7356097140) and an independent red/green on this head, both checked by a separate judge. Merged by MoonEyes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7360494490 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3154#issuecomment-7360494490). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T22:17:41Z. --- Merging. Head `854fc3441` merges cleanly with `main` `f3865cae8` and does not force a rebase of #2566. In-game run on this head by lixonik (NVIDIA GeForce RTX 5070 (12 GB), driver 595.79, Windows 10 x64, 315 s in the world, 0 device lost: https://github.com/ProjectLeonida/mandy/pull/3154#issuecomment-7356097140) and an independent red/green on this head, both checked by a separate judge. Merged by MoonEyes. AI-assisted (Claude Code).
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
ProjectLeonida/mandy!3154
No description provided.