Log the address range and largest free run when XMemVirtualAlloc is refused #3357

Merged
MoonEyes merged 2 commits from upstream-pr/3357 into main 2026-12-03 17:46:35 +00:00
MoonEyes commented 2026-12-03 16:15:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-03T16:15:30Z.


Claimed in the coordination issue as: #3337 (claim comment in #3312)

Refs: #3337

What and why

When XMemVirtualAlloc is refused, the warning now also logs alignment, lowest, highest and largest_free (base and size of the largest free run inside the bounds, via VirtualQueryEx, capped at 65536 regions). It tells a fragmented range from a full one. The walk only runs on the refusal path of a bounded reservation, the success path is unchanged, the message prefix is unchanged, and no new switch is added.

Tested

  • cargo check -p leonida-hle --tests --locked --target x86_64-pc-windows-msvc from Linux (read-only reviewer), clippy clean on xmem.rs.
  • Unit test largest_free_run_clamps_and_joins (new function, so it does not compile on main); the pure function was also run standalone with rustc --test.
  • python -I scripts/check_no_game_data.py: clean.
  • Not run on Windows: the unit test and the refusal log are left to CI.

GPUs tested: n/a

AI-assisted: yes. Written with Claude Sonnet 5.5; independently reviewed read-only by Claude Opus 5.5 (verdict OK, three nits applied).

Checklist

  • Based on current main and merges cleanly.
  • One topic.
  • No game data, keys or decryption.
  • Not a translator rule.

Generated by Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5630347104 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3357). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-03T16:15:30Z. --- Claimed in the coordination issue as: #3337 (claim comment in #3312) Refs: #3337 ### What and why When `XMemVirtualAlloc` is refused, the warning now also logs `alignment`, `lowest`, `highest` and `largest_free` (base and size of the largest free run inside the bounds, via `VirtualQueryEx`, capped at 65536 regions). It tells a fragmented range from a full one. The walk only runs on the refusal path of a bounded reservation, the success path is unchanged, the message prefix is unchanged, and no new switch is added. ### Tested - `cargo check -p leonida-hle --tests --locked --target x86_64-pc-windows-msvc` from Linux (read-only reviewer), clippy clean on `xmem.rs`. - Unit test `largest_free_run_clamps_and_joins` (new function, so it does not compile on `main`); the pure function was also run standalone with `rustc --test`. - `python -I scripts/check_no_game_data.py`: clean. - Not run on Windows: the unit test and the refusal log are left to CI. GPUs tested: n/a AI-assisted: yes. Written with Claude Sonnet 5.5; independently reviewed read-only by Claude Opus 5.5 (verdict OK, three nits applied). ### Checklist - [x] Based on current `main` and merges cleanly. - [x] One topic. - [x] No game data, keys or decryption. - [x] Not a translator rule. --- _Generated by [Claude Code](https://claude.ai/code)_
Kal0s-design commented 2026-12-03 16:30:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-03T16:30:05Z.


Ran natively on Windows 11, head 6aba73527 merged into current main (3f98bb01f, clean merge).

It builds, clippy is clean, the new test passes, and the walk does what it says on Windows. Two things worth changing. When the 65536 cap is hit, the result is silently partial, and it can come out as "no free run". And the logged value is an Option<Option<..>>, so the line shows None and Some(None), which mean opposite things. I did not provoke a refusal on purpose. Tests that reserve with address requirements have hung a test machine before, so I called largest_free_in directly instead.

Your asks (measured)

check result
cargo clippy -p leonida-hle --all-targets -- -D warnings clean
rustfmt on xmem.rs 91 hunks on the merge, 91 on main, none in the new code
largest_free_run_clamps_and_joins passes on the merge. On main it doesn't compile (E0425: cannot find function largest_free_run), as expected
cargo test -p leonida-hle --lib --locked, 4 threads, reserving tests skipped (below) merge: 227 passed, 0 failed, 1 ignored, plus the 6 pure xmem tests incl. the new one. main: 227 passed, 0 failed, 1 ignored, plus the same 5 pure xmem tests

Skipped for safety: the other 23 modules::xmem::tests. Many of them reserve through XMemVirtualAlloc with address requirements or placeholders, and I skipped the rest of the module without reading each one. Also skipped: 4 dstorage tests that do the same (half_a_queue_waits_for_the_submit, a_list_that_changes_during_the_write_has_its_new_pages_written, physical_page_destinations, zlib_into_pages_in_place_and_across_allocations). The change doesn't touch the success path those tests cover.

The walk, called directly (throwaway test, debug build, VirtualQueryEx over the test process)

bounds regions in bounds result time
whole user range 425 the large free run at the top 0.3 ms
default range for flags 0, [4 GiB, 1 TiB) 27 correct 17 µs
32-bit, [0, 4 GiB) 5 correct 1 µs
window inside a 64 MB reservation 1 None <1 µs
window from the middle of that reservation into the free region after it 2 the free part only, from the region's base to the bound <1 µs
window starting and ending inside one free region 1 exactly the window <1 µs
lowest > highest 0 None, no query made -
highest = usize::MAX 428 stops where VirtualQueryEx fails above user space, correct 0.3 ms

Clamping and joining look right. highest = 0 and a non-power-of-two alignment can't reach the walk: address_range only gives (4 GiB, 2^n - 1) or (0, 4 GiB - 1), and the walk doesn't use the alignment anyway.

The cap. To get a lot of regions I reserved 64 MB blocks and committed every other 4 KiB page, which gives 16384 regions per block.

bounds regions in bounds time per walk result
span of 1 block 16,384 5 ms correct
whole user range, 3 blocks 49,590 15 ms correct: the free run at the top of user space
whole user range, 4 blocks 65,974 (just over the cap) 20 ms a smaller run lower down. The top run is past the cap and missed
span of the blocks, 4 blocks 65,594 20 ms a free run of about 1.7 GiB between blocks
same span, 5 blocks (the 5th landed in it) 81,978 25 ms None, though free space is still there
whole user range, plus 9,990 separate 64 KiB reservations 92,348 21-24 ms partial, same as the 4-block row

So the cost stays around 0.3 µs a region and the cap holds it to about 20-25 ms. That's fine for a refusal. But past the cap the log can't be told apart from a real answer, and "no free run" there means the opposite of what it says. Logging the number of regions walked, or a truncated flag, would fix that.

What the line prints. largest is constrained.then(|| largest_free_in(..)), printed with {largest:x?}. Formatted the same way in a throwaway test:

case printed
walked, run found Some(Some((100000000, debc400000)))
walked, nothing found, or the cap was hit first Some(None)
not walked (non-null base) None

So largest_free=None means "didn't look" and Some(None) means "looked, nothing free". That's easy to misread in a player's log, and the hex has no 0x while lowest/highest on the same line do. Flattening it, or printing something like not walked / none / 0x..+0x.., would read better.

Read, not run

  • The walk is only on the reserve-only refusal path with a null base. A refused reserve+commit (for example XMemAllocDefault with the 32-bit bit) still goes to commit_refused without it. A reserve refused at a fixed address, including the one inside the held range (Some(null) from in_title_range), does reach the new warning. It prints largest_free=None there, plus lowest/highest/alignment from the flags, which were never passed to the host for that call. That could mislead: printing the bounds only when constrained would avoid it.
  • No lock is held there. in_title_range has already let go of the range's mutex, and LOW_RESERVATIONS is taken later and only on success.
  • The warning isn't rate-limited, as on main. A title that retries a refused reservation in a loop now pays up to about 20 ms per try in a very fragmented process. In the default [4 GiB, 1 TiB) range it's microseconds. The 185 MB request in #3337 has flags 0x7, which sets the 32-bit bit, so its walk would be [0, 4 GiB), the cheapest case above. If that matters, the is_power_of_two gate used for the "not placed in the title's range" warning would do.
  • With LEONIDA_TITLE_RANGE the held range is reserved, so the walk counts it as taken. largest_free describes the host's free space, not the free pieces inside the held range. In the #3337 Linux log the 16 GiB and 1.3 GB requests fail on the "not placed in the title's range" path, which this PR doesn't change. So it wouldn't have shown those pieces.
  • The run is reported as VirtualQueryEx gives it: page-aligned, not rounded to 64 KiB or to the 2 MiB alignment. The 32-bit result above starts at 0x7ffe7000. A largest_free a little over size can still be refused. The comment says "aligned free run", so the log value is a slight overestimate.

Conflicts. Four other open PRs touch xmem.rs. #3256 and #2982 merge cleanly on top of this merge. #3039 conflicts only in switches.md, the same as on main. #2856 already conflicts with main in xmem.rs (4 hunks). On this merge it's the same 4 hunks, but the new test lands inside the test-module hunk and the refusal warning sits right after the VirtualAlloc2 hunk. So it's a little more to resolve there, nothing new.

Not checked: a real refusal, so the log line itself on a refused VirtualAlloc2. A title, and how many regions a real game process has in its bounds. --release. Wine.

GPUs tested: n/a (CPU-only tests).
AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7384943852 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3357#issuecomment-7384943852). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-03T16:30:05Z. --- Ran natively on Windows 11, head `6aba73527` merged into current `main` (`3f98bb01f`, clean merge). It builds, clippy is clean, the new test passes, and the walk does what it says on Windows. Two things worth changing. When the 65536 cap is hit, the result is silently partial, and it can come out as "no free run". And the logged value is an `Option<Option<..>>`, so the line shows `None` and `Some(None)`, which mean opposite things. I did not provoke a refusal on purpose. Tests that reserve with address requirements have hung a test machine before, so I called `largest_free_in` directly instead. **Your asks** (measured) | check | result | |---|---| | `cargo clippy -p leonida-hle --all-targets -- -D warnings` | clean | | rustfmt on `xmem.rs` | 91 hunks on the merge, 91 on `main`, none in the new code | | `largest_free_run_clamps_and_joins` | passes on the merge. On `main` it doesn't compile (`E0425: cannot find function largest_free_run`), as expected | | `cargo test -p leonida-hle --lib --locked`, 4 threads, reserving tests skipped (below) | merge: 227 passed, 0 failed, 1 ignored, plus the 6 pure xmem tests incl. the new one. `main`: 227 passed, 0 failed, 1 ignored, plus the same 5 pure xmem tests | Skipped for safety: the other 23 `modules::xmem::tests`. Many of them reserve through `XMemVirtualAlloc` with address requirements or placeholders, and I skipped the rest of the module without reading each one. Also skipped: 4 `dstorage` tests that do the same (`half_a_queue_waits_for_the_submit`, `a_list_that_changes_during_the_write_has_its_new_pages_written`, `physical_page_destinations`, `zlib_into_pages_in_place_and_across_allocations`). The change doesn't touch the success path those tests cover. **The walk, called directly** (throwaway test, debug build, `VirtualQueryEx` over the test process) | bounds | regions in bounds | result | time | |---|---|---|---| | whole user range | 425 | the large free run at the top | 0.3 ms | | default range for flags 0, `[4 GiB, 1 TiB)` | 27 | correct | 17 µs | | 32-bit, `[0, 4 GiB)` | 5 | correct | 1 µs | | window inside a 64 MB reservation | 1 | `None` | <1 µs | | window from the middle of that reservation into the free region after it | 2 | the free part only, from the region's base to the bound | <1 µs | | window starting and ending inside one free region | 1 | exactly the window | <1 µs | | `lowest > highest` | 0 | `None`, no query made | - | | `highest = usize::MAX` | 428 | stops where `VirtualQueryEx` fails above user space, correct | 0.3 ms | Clamping and joining look right. `highest = 0` and a non-power-of-two alignment can't reach the walk: `address_range` only gives `(4 GiB, 2^n - 1)` or `(0, 4 GiB - 1)`, and the walk doesn't use the alignment anyway. **The cap.** To get a lot of regions I reserved 64 MB blocks and committed every other 4 KiB page, which gives 16384 regions per block. | bounds | regions in bounds | time per walk | result | |---|---|---|---| | span of 1 block | 16,384 | 5 ms | correct | | whole user range, 3 blocks | 49,590 | 15 ms | correct: the free run at the top of user space | | whole user range, 4 blocks | 65,974 (just over the cap) | 20 ms | a smaller run lower down. The top run is past the cap and missed | | span of the blocks, 4 blocks | 65,594 | 20 ms | a free run of about 1.7 GiB between blocks | | same span, 5 blocks (the 5th landed in it) | 81,978 | 25 ms | `None`, though free space is still there | | whole user range, plus 9,990 separate 64 KiB reservations | 92,348 | 21-24 ms | partial, same as the 4-block row | So the cost stays around 0.3 µs a region and the cap holds it to about 20-25 ms. That's fine for a refusal. But past the cap the log can't be told apart from a real answer, and "no free run" there means the opposite of what it says. Logging the number of regions walked, or a `truncated` flag, would fix that. **What the line prints.** `largest` is `constrained.then(|| largest_free_in(..))`, printed with `{largest:x?}`. Formatted the same way in a throwaway test: | case | printed | |---|---| | walked, run found | `Some(Some((100000000, debc400000)))` | | walked, nothing found, or the cap was hit first | `Some(None)` | | not walked (non-null base) | `None` | So `largest_free=None` means "didn't look" and `Some(None)` means "looked, nothing free". That's easy to misread in a player's log, and the hex has no `0x` while `lowest`/`highest` on the same line do. Flattening it, or printing something like `not walked` / `none` / `0x..+0x..`, would read better. **Read, not run** - The walk is only on the reserve-only refusal path with a null base. A refused reserve+commit (for example `XMemAllocDefault` with the 32-bit bit) still goes to `commit_refused` without it. A reserve refused at a fixed address, including the one inside the held range (`Some(null)` from `in_title_range`), does reach the new warning. It prints `largest_free=None` there, plus `lowest`/`highest`/`alignment` from the flags, which were never passed to the host for that call. That could mislead: printing the bounds only when `constrained` would avoid it. - No lock is held there. `in_title_range` has already let go of the range's mutex, and `LOW_RESERVATIONS` is taken later and only on success. - The warning isn't rate-limited, as on `main`. A title that retries a refused reservation in a loop now pays up to about 20 ms per try in a very fragmented process. In the default `[4 GiB, 1 TiB)` range it's microseconds. The 185 MB request in #3337 has `flags 0x7`, which sets the 32-bit bit, so its walk would be `[0, 4 GiB)`, the cheapest case above. If that matters, the `is_power_of_two` gate used for the "not placed in the title's range" warning would do. - With `LEONIDA_TITLE_RANGE` the held range is reserved, so the walk counts it as taken. `largest_free` describes the host's free space, not the free pieces inside the held range. In the #3337 Linux log the 16 GiB and 1.3 GB requests fail on the "not placed in the title's range" path, which this PR doesn't change. So it wouldn't have shown those pieces. - The run is reported as `VirtualQueryEx` gives it: page-aligned, not rounded to 64 KiB or to the 2 MiB alignment. The 32-bit result above starts at `0x7ffe7000`. A `largest_free` a little over `size` can still be refused. The comment says "aligned free run", so the log value is a slight overestimate. **Conflicts.** Four other open PRs touch `xmem.rs`. #3256 and #2982 merge cleanly on top of this merge. #3039 conflicts only in `switches.md`, the same as on `main`. #2856 already conflicts with `main` in `xmem.rs` (4 hunks). On this merge it's the same 4 hunks, but the new test lands inside the test-module hunk and the refusal warning sits right after the `VirtualAlloc2` hunk. So it's a little more to resolve there, nothing new. Not checked: a real refusal, so the log line itself on a refused `VirtualAlloc2`. A title, and how many regions a real game process has in its bounds. `--release`. Wine. GPUs tested: n/a (CPU-only tests). AI-assisted: yes (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!3357
No description provided.