Log the address range and largest free run when XMemVirtualAlloc is refused #3357
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3357"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
XMemVirtualAllocis refused, the warning now also logsalignment,lowest,highestandlargest_free(base and size of the largest free run inside the bounds, viaVirtualQueryEx, 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-msvcfrom Linux (read-only reviewer), clippy clean onxmem.rs.largest_free_run_clamps_and_joins(new function, so it does not compile onmain); the pure function was also run standalone withrustc --test.python -I scripts/check_no_game_data.py: clean.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
mainand merges cleanly.Generated by Claude Code
Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-03T16:30:05Z.
Ran natively on Windows 11, head
6aba73527merged into currentmain(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 showsNoneandSome(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 calledlargest_free_indirectly instead.Your asks (measured)
cargo clippy -p leonida-hle --all-targets -- -D warningsxmem.rsmain, none in the new codelargest_free_run_clamps_and_joinsmainit doesn't compile (E0425: cannot find function largest_free_run), as expectedcargo test -p leonida-hle --lib --locked, 4 threads, reserving tests skipped (below)main: 227 passed, 0 failed, 1 ignored, plus the same 5 pure xmem testsSkipped for safety: the other 23
modules::xmem::tests. Many of them reserve throughXMemVirtualAllocwith address requirements or placeholders, and I skipped the rest of the module without reading each one. Also skipped: 4dstoragetests 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,
VirtualQueryExover the test process)[4 GiB, 1 TiB)[0, 4 GiB)Nonelowest > highestNone, no query madehighest = usize::MAXVirtualQueryExfails above user space, correctClamping and joining look right.
highest = 0and a non-power-of-two alignment can't reach the walk:address_rangeonly 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.
None, though free space is still thereSo 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
truncatedflag, would fix that.What the line prints.
largestisconstrained.then(|| largest_free_in(..)), printed with{largest:x?}. Formatted the same way in a throwaway test:Some(Some((100000000, debc400000)))Some(None)NoneSo
largest_free=Nonemeans "didn't look" andSome(None)means "looked, nothing free". That's easy to misread in a player's log, and the hex has no0xwhilelowest/higheston the same line do. Flattening it, or printing something likenot walked/none/0x..+0x.., would read better.Read, not run
XMemAllocDefaultwith the 32-bit bit) still goes tocommit_refusedwithout it. A reserve refused at a fixed address, including the one inside the held range (Some(null)fromin_title_range), does reach the new warning. It printslargest_free=Nonethere, pluslowest/highest/alignmentfrom the flags, which were never passed to the host for that call. That could mislead: printing the bounds only whenconstrainedwould avoid it.in_title_rangehas already let go of the range's mutex, andLOW_RESERVATIONSis taken later and only on success.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 hasflags 0x7, which sets the 32-bit bit, so its walk would be[0, 4 GiB), the cheapest case above. If that matters, theis_power_of_twogate used for the "not placed in the title's range" warning would do.LEONIDA_TITLE_RANGEthe held range is reserved, so the walk counts it as taken.largest_freedescribes 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.VirtualQueryExgives it: page-aligned, not rounded to 64 KiB or to the 2 MiB alignment. The 32-bit result above starts at0x7ffe7000. Alargest_freea little oversizecan 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 inswitches.md, the same as onmain. #2856 already conflicts withmaininxmem.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 theVirtualAlloc2hunk. 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).