startup: prove slot 133 placements without walking the allocation #1766

Manually merged
github merged 9 commits from gh-pr/1766/head into gh-pr/1766/base 2026-11-26 23:31:56 +00:00
Owner

@⁠rasptum on GitHub · 2026-11-24 18:15 UTC · original
rasptum:startup/placed-allocation-cache → main · +597 −12 · 5 files · 9 commits · merged 2026-11-24 21:46 UTC

Problem

Device slot 133 (device_place_resource_x in src/startup_stubs/d3d12_xs_device.cpp) aliases a buffer at a title address only if the memory is private and every byte from the base of the allocation holding the address to the end of the buffer is committed in that allocation. To decide this, it measured the whole committed run from the base, one VirtualQuery per region, and compared the buffer with that length.

VirtualQuery reports RegionSize by scanning page-table entries from the queried address to the end of the run of identical attributes. On a large private region with touched pages, each call therefore costs milliseconds.

Synthetic measurements, Windows 10 19045, ms per call:

Block (one touched page per 64 KiB) at the base from the middle last 64 KiB
1 GiB 5.5 2.4 0.002
2 GiB 11.9 6.0 0.002
4 GiB 26.0 12.9 0.002
1 GiB, every 4 KiB page touched 22.7 12.3 0.002
untouched, any size about 0.01 about 0 about 0

The cost follows the distance to the next attribute change, so even a walk that stops at the end of the buffer pays it in full when the buffer starts inside a large region.

Change

src/startup_stubs/placed_buffer_range.h (new, testable without a device) proves each placement at call time and keeps nothing between calls:

  1. QueryVirtualMemoryInformation(MemoryRegionInfo) (Windows 10 1607 and later, resolved with GetProcAddress) reports the base, size and committed bytes of the allocation holding the address. These come from the allocation's bookkeeping, without the scan: about 0.0004 ms per call on the blocks above. When the allocation is private and its committed bytes equal its size, every page is committed, so the full walk's committed run is the whole allocation. The decision then follows exactly from base and size.
  2. Anything else takes the original walk from the base, stopped at the end of the buffer, because regions after it cannot change the answer. "Anything else" means a partly committed or mapped allocation, a free address, or the export missing (older systems, compatibility layers).

The decision equals the full walk's in every case. A page decommitted, released or replaced directly through the Windows API is seen by the next placement. Unchanged: 64 KiB offset alignment, MEM_PRIVATE only, the acceleration-structure refusal, existing-heap reuse, and slot 133's log lines.

The five-second x2w-d3d12 views noted line in stderr.txt ends with placed buffers <n> (region-information proofs <n>, region queries <n>). docs/startup-runtime.md describes this.

Committed-bytes exactness: for private allocations, the committed-bytes value matched a full VirtualQuery count of committed pages in every case tried:

  • decommit and recommit of 64 KiB and 4 KiB, each repeated;
  • commit of already committed pages;
  • PAGE_NOACCESS and PAGE_GUARD;
  • MEM_RESET;
  • reserve-then-commit;
  • a private allocation replacing a placeholder piece.

A mapped view reports flags 0x8 with no committed bytes, and a free address makes the call fail. Both take the walk.

History: the first version of this PR cached measured extents and validated hits with VirtualQuery at the buffer's first and last byte. Review showed that this does not prove a contiguous committed prefix, so the cache was removed. A walk stopped at the buffer end was exact, but slower than the original code in a startup run, for the reason above. This version keeps that walk only as the fallback.

Measured in a run

In a local startup run on Windows 10 (RTX 3060), the time from launch to the first interactive screen was 72.6 s, against 81 to 94 s with the earlier cache and 197 to 252 s with the bounded walk alone. Of 3,388 placements, 3,259 were proven by region information and 129 fell back to the walk (343 VirtualQuery calls in total).

Tests

startup_placed_buffer_range runs on the CPU only, with no device. It compares every alias or refusal, base and type with the full walk (the old code, kept in the test as the reference) on the same memory at the same moment. Each case places a buffer, then changes the memory, then places again, and also asserts the expected decision.

  1. 256 buffers over a 64 MiB block whose alternating 64 KiB pages have different protections, plus misaligned, past-the-end and base placements.
  2. A middle page decommitted: a buffer after the gap, across it, before it and on it.
  3. The same page committed again.
  4. Page 1 of eight decommitted, buffer on page 6 and on pages 6 and 7.
  5. A reviewed repro: 1 MiB committed, a 64 KiB buffer at +0x80000, then 0x10000 bytes at +0x10000 decommitted. The buffer is refused.
  6. Same-base replacement: 64 MiB released, 1 MiB committed at its base. A buffer inside, one ending past the new end and one past it.
  7. A mapped view.
  8. A reservation committed in two steps.
  9. A placeholder piece replaced by committed private memory: a buffer inside it and one ending in the placeholder after it.
  10. Other private allocations:
    • MEM_WRITE_WATCH, MEM_TOP_DOWN and PAGE_EXECUTE_READWRITE allocations, proven by region information;
    • a large heap block, whose allocation starts with reserved pages, so both the proof and the full walk refuse it;
    • an AWE (MEM_PHYSICAL) reservation, with nothing committed;
    • MEM_LARGE_PAGES when the process may lock pages (otherwise reported as not run).
  11. A 512 MiB committed block with one touched page per 64 KiB: 64 placements must be proven with no VirtualQuery call. This is a call count, not a time. The check prints the time per placement: full walk 4.7 ms, bounded walk 4.6 ms, region proof 0.0004 ms (dev; release was similar).

Command (Windows 10 19045, MSVC 19.44, RTX 3060; _warp tests use WARP, _hardware tests the RTX 3060), run in both dev and release:

ctest --preset release --no-tests=error -R '^(startup_placed_buffer_range.*|startup_placed_vertex_.*|xmem_page_runs_device_.*|startup_placement_.*|cpu_placed_draw_.*)$' -V

Observed: 12 of 12 passed, 0 failed, 0 skipped, in both presets. Besides the new check and its two controls, the selection covers the existing device tests that exercise slot 133 and the neighbouring placement checks: startup_placed_vertex_{warp,hardware}, xmem_page_runs_device_{warp,hardware}, cpu_placed_draw_{warp,hardware} and startup_placement_{alloc,publish,publish_shrink_control}. They pass unchanged. tests/docs_links_check.py passes. The branch merges current main (db924b76).

Negative controls

Both controls are registered with WILL_FAIL. Each exits 1 only when it fails for its stated reason and nothing else fails, and exits 77 (a capability skip) if an allocation or mapping is refused.

  • startup_placed_buffer_range_region_only_control (--mutate region-only) trusts the allocation size without the committed-bytes check. Observed: 10 placements differ from the full walk, all in the partly committed cases (2, 4, 5, 8, the heap block and the AWE reservation), and none elsewhere.
  • startup_placed_buffer_range_walk_only_control (--mutate walk-only) never uses the region information. Observed: decisions all match, and only the call count of the 512 MiB case fails (0 of 64 region proofs, 128 VirtualQuery calls).

Not covered

  • Partly committed private allocations still take the walk and pay the VirtualQuery cost (129 of 3,388 placements in the run above).
  • MEM_LARGE_PAGES needs the lock-pages privilege and was not run here. An AWE region with physical pages mapped needs the same privilege and was not tried; an AWE reservation reports the private flag with no committed bytes and takes the walk.
  • The device tests exercise the proof only indirectly. The counter shows the split between region-information proofs and region queries in a run.
  • This change does not establish that anything starts or renders.

AI-assisted (Claude Code).
🤖 Generated with Claude Code

**@⁠rasptum** on GitHub · 2026-11-24 18:15 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1766) `rasptum:startup/placed-allocation-cache` → `main` · +597 −12 · 5 files · 9 commits · merged 2026-11-24 21:46 UTC ## Problem Device slot 133 (`device_place_resource_x` in `src/startup_stubs/d3d12_xs_device.cpp`) aliases a buffer at a title address only if the memory is private and every byte from the base of the allocation holding the address to the end of the buffer is committed in that allocation. To decide this, it measured the whole committed run from the base, one `VirtualQuery` per region, and compared the buffer with that length. `VirtualQuery` reports `RegionSize` by scanning page-table entries from the queried address to the end of the run of identical attributes. On a large private region with touched pages, each call therefore costs milliseconds. Synthetic measurements, Windows 10 19045, ms per call: | Block (one touched page per 64 KiB) | at the base | from the middle | last 64 KiB | |---|---|---|---| | 1 GiB | 5.5 | 2.4 | 0.002 | | 2 GiB | 11.9 | 6.0 | 0.002 | | 4 GiB | 26.0 | 12.9 | 0.002 | | 1 GiB, every 4 KiB page touched | 22.7 | 12.3 | 0.002 | | untouched, any size | about 0.01 | about 0 | about 0 | The cost follows the distance to the next attribute change, so even a walk that stops at the end of the buffer pays it in full when the buffer starts inside a large region. ## Change `src/startup_stubs/placed_buffer_range.h` (new, testable without a device) proves each placement at call time and keeps nothing between calls: 1. **`QueryVirtualMemoryInformation(MemoryRegionInfo)`** (Windows 10 1607 and later, resolved with `GetProcAddress`) reports the base, size and committed bytes of the allocation holding the address. These come from the allocation's bookkeeping, without the scan: about 0.0004 ms per call on the blocks above. When the allocation is private and its committed bytes equal its size, every page is committed, so the full walk's committed run is the whole allocation. The decision then follows exactly from base and size. 2. **Anything else** takes the original walk from the base, stopped at the end of the buffer, because regions after it cannot change the answer. "Anything else" means a partly committed or mapped allocation, a free address, or the export missing (older systems, compatibility layers). The decision equals the full walk's in every case. A page decommitted, released or replaced directly through the Windows API is seen by the next placement. Unchanged: 64 KiB offset alignment, `MEM_PRIVATE` only, the acceleration-structure refusal, existing-heap reuse, and slot 133's log lines. The five-second `x2w-d3d12 views noted` line in `stderr.txt` ends with `placed buffers <n> (region-information proofs <n>, region queries <n>)`. `docs/startup-runtime.md` describes this. **Committed-bytes exactness:** for private allocations, the committed-bytes value matched a full `VirtualQuery` count of committed pages in every case tried: - decommit and recommit of 64 KiB and 4 KiB, each repeated; - commit of already committed pages; - `PAGE_NOACCESS` and `PAGE_GUARD`; - `MEM_RESET`; - reserve-then-commit; - a private allocation replacing a placeholder piece. A mapped view reports flags 0x8 with no committed bytes, and a free address makes the call fail. Both take the walk. **History:** the first version of this PR cached measured extents and validated hits with `VirtualQuery` at the buffer's first and last byte. Review showed that this does not prove a contiguous committed prefix, so the cache was removed. A walk stopped at the buffer end was exact, but slower than the original code in a startup run, for the reason above. This version keeps that walk only as the fallback. ## Measured in a run In a local startup run on Windows 10 (RTX 3060), the time from launch to the first interactive screen was 72.6 s, against 81 to 94 s with the earlier cache and 197 to 252 s with the bounded walk alone. Of 3,388 placements, 3,259 were proven by region information and 129 fell back to the walk (343 `VirtualQuery` calls in total). ## Tests `startup_placed_buffer_range` runs on the CPU only, with no device. It compares every alias or refusal, base and type with the full walk (the old code, kept in the test as the reference) on the same memory at the same moment. Each case places a buffer, then changes the memory, then places again, and also asserts the expected decision. 1. 256 buffers over a 64 MiB block whose alternating 64 KiB pages have different protections, plus misaligned, past-the-end and base placements. 2. A middle page decommitted: a buffer after the gap, across it, before it and on it. 3. The same page committed again. 4. Page 1 of eight decommitted, buffer on page 6 and on pages 6 and 7. 5. A reviewed repro: 1 MiB committed, a 64 KiB buffer at +0x80000, then 0x10000 bytes at +0x10000 decommitted. The buffer is refused. 6. Same-base replacement: 64 MiB released, 1 MiB committed at its base. A buffer inside, one ending past the new end and one past it. 7. A mapped view. 8. A reservation committed in two steps. 9. A placeholder piece replaced by committed private memory: a buffer inside it and one ending in the placeholder after it. 10. Other private allocations: - `MEM_WRITE_WATCH`, `MEM_TOP_DOWN` and `PAGE_EXECUTE_READWRITE` allocations, proven by region information; - a large heap block, whose allocation starts with reserved pages, so both the proof and the full walk refuse it; - an AWE (`MEM_PHYSICAL`) reservation, with nothing committed; - `MEM_LARGE_PAGES` when the process may lock pages (otherwise reported as not run). 11. A 512 MiB committed block with one touched page per 64 KiB: 64 placements must be proven with no `VirtualQuery` call. This is a call count, not a time. The check prints the time per placement: full walk 4.7 ms, bounded walk 4.6 ms, region proof 0.0004 ms (`dev`; `release` was similar). Command (Windows 10 19045, MSVC 19.44, RTX 3060; `_warp` tests use WARP, `_hardware` tests the RTX 3060), run in both `dev` and `release`: ``` ctest --preset release --no-tests=error -R '^(startup_placed_buffer_range.*|startup_placed_vertex_.*|xmem_page_runs_device_.*|startup_placement_.*|cpu_placed_draw_.*)$' -V ``` Observed: 12 of 12 passed, 0 failed, 0 skipped, in both presets. Besides the new check and its two controls, the selection covers the existing device tests that exercise slot 133 and the neighbouring placement checks: `startup_placed_vertex_{warp,hardware}`, `xmem_page_runs_device_{warp,hardware}`, `cpu_placed_draw_{warp,hardware}` and `startup_placement_{alloc,publish,publish_shrink_control}`. They pass unchanged. `tests/docs_links_check.py` passes. The branch merges current main (db924b76). ## Negative controls Both controls are registered with `WILL_FAIL`. Each exits 1 only when it fails for its stated reason and nothing else fails, and exits 77 (a capability skip) if an allocation or mapping is refused. - `startup_placed_buffer_range_region_only_control` (`--mutate region-only`) trusts the allocation size without the committed-bytes check. Observed: 10 placements differ from the full walk, all in the partly committed cases (2, 4, 5, 8, the heap block and the AWE reservation), and none elsewhere. - `startup_placed_buffer_range_walk_only_control` (`--mutate walk-only`) never uses the region information. Observed: decisions all match, and only the call count of the 512 MiB case fails (0 of 64 region proofs, 128 `VirtualQuery` calls). ## Not covered - Partly committed private allocations still take the walk and pay the `VirtualQuery` cost (129 of 3,388 placements in the run above). - `MEM_LARGE_PAGES` needs the lock-pages privilege and was not run here. An AWE region with physical pages mapped needs the same privilege and was not tried; an AWE reservation reports the private flag with no committed bytes and takes the walk. - The device tests exercise the proof only indirectly. The counter shows the split between region-information proofs and region queries in a run. - This change does not establish that anything starts or renders. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Device slot 133 measured the allocation holding a placed buffer with a
VirtualQuery walk over every region from its base, on every placement.
An allocation with many regions (alternating protections) cost one query
per region each time. src/startup_stubs/placed_allocation_cache.h now
remembers the measured extent of private allocations; a buffer that lies
inside a remembered extent is served without a walk. Every other
placement is measured as before. The 64 KiB offset alignment, the
buffer-inside-allocation rule, MEM_PRIVATE only, the acceleration-
structure refusal and the existing-heap reuse are unchanged.

Stale entries: validated on use, not invalidated from release paths.
A hit is trusted only if VirtualQuery at the buffer's address reports a
committed region of the remembered allocation base with the remembered
type, and VirtualQuery at the buffer's last byte (skipped when the first
region already covers the buffer) reports a committed region of the same
base. Release hooks were not chosen because the title can free or
replace memory through calls the stand-ins do not own, and the existing
release paths live in another module. A single query at the address was
not enough: a block released and replaced by a smaller one at the same
base still owns the address, so only the last-byte query refuses a
buffer that now ends past the new block. A failed check drops the entry
and measures again. Not detected: a MEM_DECOMMIT strictly between the
two queried regions; no stand-in decommits part of an allocation.

The five-second "x2w-d3d12 views noted" line reports
"placed allocations <entries> (walks <n>, walks saved <n>)". The cache
uses an SRW lock: shared for lookup, exclusive for insert and erase;
VirtualQuery runs outside the lock.

Test: startup_placed_allocation_cache (CPU only, no device): 256
placements into a 64 MiB private block with 1024 regions match the
uncached walk in extent and decision with one walk; a block replaced by
a 1 MiB block at the same base refuses a buffer ending past and one
starting past the new end; a mapped view is refused and never
remembered; pages committed later are seen. Negative controls (WILL_FAIL)
run the same check with validation off (--mutate none) and with the
address query only (--mutate address-only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The two WILL_FAIL controls passed on any nonzero exit, so a refused
allocation or an unrelated mismatch in another case would also have
counted as the expected failure. Each control now exits 1 only when its
own stale acceptance happens in the replaced-block case and nothing else
fails: --mutate none must serve the placement past the new end from the
stale extent; --mutate address-only must serve the placement that ends
past the new end while still refusing the one past it. Anything else
exits 0, so the control fails. A refused allocation or mapping is a
capability skip (77) in the controls (SKIP_RETURN_CODE) and stays a
failure in the main check. No runtime code changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review found that the cached extent was not exact: the first and last
VirtualQuery results do not prove a contiguous committed range. After a
private allocation was remembered, decommitting a middle page left both
endpoints committed with the same AllocationBase, so the cache kept
accepting a buffer after the gap that the full walk refuses, and a gap
before the requested start changed the full walk's answer without
being seen.

Of the two remedies, invalidation tied to the allocator lifecycle cannot
be made complete: the consumer can call VirtualFree(MEM_DECOMMIT) or
release and reallocate memory through the Windows API directly, and the
stand-ins never see those calls. A per-hit proof has to show that
[allocation base, end of buffer) is committed in one allocation, which
is a region walk over exactly that range. That walk needs no cache at
all, and no cached state can make an exact answer cheaper, because any
remembered fact about [base, address) can be invalidated by a direct
decommit. So the cache is removed and slot 133 runs the original walk
from the base but stops at the end of the buffer
(src/startup_stubs/placed_buffer_range.h). Regions after the buffer
cannot change the alias/refusal decision, so the decision equals the
full walk's in every case. The committed byte count from the base is no
longer computed; slot 133 used it only for this decision.

Cost: in the CPU-only check, 256 buffers spread over a 64 MiB block with
1024 regions take 513.5 region queries per placement, against 1026.0 for
the full walk (about half). The removed cache needed 1 to 2 queries per
hit there, so most of the saving it reported is gone; what remains is
proportional to how far the buffer ends before the allocation does.

The five-second totals line now reports "placed buffers <n> (region
queries <n>)" instead of entries/walks/walks saved.

Test: startup_placed_buffer_range replaces startup_placed_allocation_cache
and its two mutation controls. It compares every alias/refusal, base and
type with the full walk on the same memory: the striped block, a middle
page decommitted under, before and five pages before a buffer, the page
committed again, a block replaced by a smaller one at the same base, a
mapped view, and a reservation committed in two steps. Each case places
a buffer before changing the memory so that state kept between
placements would show up. Run locally against the removed endpoint-only
cache, the check fails 4 placements (the two decommit refusals and the
two gap-before-start refusals are accepted); the replacement, recommit,
mapped and two-step cases pass on both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 MiB committed, a 64 KiB buffer at +0x80000 placed, then 0x10000 bytes
at +0x10000 decommitted: the next placement of the same buffer must be
refused, as the full walk refuses it. The removed endpoint-only cache
accepted it (checked locally against 229f7b13).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A startup run measured the bounded walk at about 16 ms per VirtualQuery
(3,388 placements, 6,861 queries, about 110 s), with decisions identical
to the cached version. VirtualQuery reports RegionSize by scanning the
page-table entries from the queried address to the end of the run of
identical attributes, so on a large private region with touched pages
every call costs milliseconds. Synthetic, Windows 10 19045, ms per call:
one touched page per 64 KiB, at the base: 1 GiB 5.5, 2 GiB 11.9, 4 GiB
26.0; from the middle about half; in the last 64 KiB 0.002; 1 GiB with
every 4 KiB page touched 22.7; untouched blocks of any size about 0.01.
The cost follows the distance to the next attribute change, not the
allocation size, so no choice of query address bounds it for a buffer
at the start of a large region, and the walk from the base pays it in
full.

QueryVirtualMemoryInformation(MemoryRegionInfo) (Windows 10 1607 and
later) reports, for the allocation holding the address, its base, size
and committed bytes from the allocation's bookkeeping: about 0.0004 ms
per call on the same blocks. CommitSize matched a full VirtualQuery
count of committed pages for private allocations after decommit and
recommit (64 KiB and 4 KiB, repeated), commit of committed pages,
PAGE_NOACCESS, PAGE_GUARD, MEM_RESET, reserve-then-commit and a private
allocation replacing a placeholder piece. A mapped view reports flags
0x8 with commit 0, and a free address makes the call fail.

measure_placed_buffer therefore asks for the region information first.
When the allocation is private and its committed bytes equal its size,
every page is committed, so the full walk's run is the whole allocation
and the decision follows from base and size exactly. Anything else (a
partly committed or mapped allocation, a free address, the export
missing, resolved by GetProcAddress) takes the walk from the base that
stops at the end of the buffer. Nothing is cached, so the reviewed
decommit cases stay exact.

Totals: "placed buffers <n> (region-information proofs <n>, region
queries <n>)".

Test: startup_placed_buffer_range keeps every earlier decision case,
including both reviewers' decommit repros, and adds a placeholder piece
and a 512 MiB committed block with one touched page per 64 KiB: 64
placements must be proven with no VirtualQuery call (a call count, not a
time). Measured there: full walk 4.7 ms, bounded walk 4.6 ms, region
proof 0.0004 ms per placement. Negative controls (WILL_FAIL, exit 1 only
for their stated reason, 77 on a refused allocation): --mutate
region-only trusts the allocation size without the committed-bytes check
and fails 8 placements, all in the partly committed cases; --mutate
walk-only never uses the region information and fails only the call
count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each is compared with the full walk on the same memory. MEM_LARGE_PAGES
needs the lock-pages privilege; without it the case is reported as not
run instead of failing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A large heap block's allocation starts with reserved pages, so the full
walk's committed run from its base is empty and every buffer in it is
refused; the case expected an alias. Both walks agreed, so this was the
case's expectation, not a divergence. The AWE reservation has nothing
committed and is grouped with the partly committed cases for the
region-only control.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:31:55 +00:00
github manually merged commit 20f8e1d57a into gh-pr/1766/base 2026-11-26 23:31:56 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 18:38 UTC · original

The first and last VirtualQuery results do not establish a contiguous committed range. For example, after caching a private three-page allocation, the middle page can be decommitted while the first and last pages remain committed with the same AllocationBase. The uncached walk stops at the gap, but still_valid accepts both endpoints and returns the old extent. A gap before the requested start also changes the original walk's answer. Before using a hit, prove the full committed prefix needed by slot 133 or use reliable invalidation tied to the allocator lifecycle. Please add a source-built middle-page decommit/recommit control and compare the alias/refusal decision with the uncached path. Keep the same-base replacement controls.

**@⁠R0Y00** on GitHub · 2026-11-24 18:38 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1766#issuecomment-7140919088) The first and last VirtualQuery results do not establish a contiguous committed range. For example, after caching a private three-page allocation, the middle page can be decommitted while the first and last pages remain committed with the same AllocationBase. The uncached walk stops at the gap, but still_valid accepts both endpoints and returns the old extent. A gap before the requested start also changes the original walk's answer. Before using a hit, prove the full committed prefix needed by slot 133 or use reliable invalidation tied to the allocator lifecycle. Please add a source-built middle-page decommit/recommit control and compare the alias/refusal decision with the uncached path. Keep the same-base replacement controls.
Author
Owner

@⁠rasptum on GitHub · 2026-11-24 21:36 UTC · original

Thanks, you are right. Checking only the endpoints did not prove a contiguous committed prefix. After a private allocation was cached, decommitting a middle page left both endpoints committed with the same AllocationBase, so the cache kept accepting a buffer after the gap that the uncached walk refuses. A gap before the requested start went unseen in the same way.

What changed: the cache is gone, and nothing is kept between placements. Lifecycle invalidation cannot be made complete, because a consumer can call VirtualFree(MEM_DECOMMIT), or release and reallocate memory, directly through the Windows API, and the stand-ins never see that.

Each placement is now proven at call time (src/startup_stubs/placed_buffer_range.h):

  • First: QueryVirtualMemoryInformation(MemoryRegionInfo) reports the base, size and committed bytes of the allocation holding the address. When the allocation is private and its committed bytes equal its size, every page is committed, so the full walk's committed run is the whole allocation. The decision then follows exactly from base and size.
  • Otherwise: a partly committed or mapped allocation, a free address, or a missing export takes the original walk from the base, stopped at the end of the buffer.

In a local startup run on Windows 10 (RTX 3060), the time from launch to the first interactive screen was 72.6 s, against 81 to 94 s with the earlier cache and 197 to 252 s with the bounded walk alone. Of 3,388 placements, 3,259 were proven by region information and 129 fell back to the walk (343 VirtualQuery calls in total).

Why not only the bounded walk: in a startup run it was slower than the original code. VirtualQuery scans page-table entries up to the next attribute change, which costs milliseconds on large touched regions (synthetic: about 5 ms per GiB with one touched page per 64 KiB). The region-information call does not scan; it costs about 0.0004 ms. Its committed-bytes count matched a full VirtualQuery count for private allocations after decommit and recommit, double decommit, commit of committed pages, guard and no-access pages, MEM_RESET, reserve-then-commit and placeholder pieces.

startup_placed_buffer_range (replaces startup_placed_allocation_cache) compares every alias or refusal, base and type with the full walk on the same memory. Each case places a buffer first, then changes the memory, then places again:

  • a middle page decommitted under a buffer, before a buffer, and five pages before a buffer, then committed again;
  • the reviewed repro: 1 MiB committed, a buffer at +0x80000 for 0x10000 bytes, then +0x10000 decommitted for 0x10000 bytes;
  • the same-base replacement;
  • a mapped view;
  • a reservation committed in two steps;
  • a placeholder piece;
  • write-watch, top-down, executable, heap-block, AWE and (with the lock-pages privilege) large-page allocations;
  • a 64 MiB striped block;
  • a 512 MiB touched block, which must be proven with no VirtualQuery call.

The controls (WILL_FAIL) must fail only for their stated reason. --mutate region-only trusts the allocation size without the committed-bytes check and fails 10 placements, all in the partly committed cases. --mutate walk-only never uses the region information and fails only the call count.

@⁠tedin7: your repro is a case in startup_placed_buffer_range with exactly those numbers. The endpoint-only cache at 229f7b13 accepted the second placement, while the current code refuses it, the same answer as the full walk. It is refused because a decommitted page makes the allocation's committed bytes smaller than its size, so the placement falls back to the walk from the base, which stops at the gap.

ctest --preset release --no-tests=error -R '^(startup_placed_buffer_range.*|startup_placed_vertex_.*|xmem_page_runs_device_.*|startup_placement_.*|cpu_placed_draw_.*)$' -V

Windows 10 19045, MSVC 19.44, RTX 3060 (WARP for _warp), dev and release, head 20f8e1d5 (merges current main): 12 of 12 passed, 0 failed, 0 skipped.

AI-assisted (Claude Code).

**@⁠rasptum** on GitHub · 2026-11-24 21:36 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1766#issuecomment-7145152543) Thanks, you are right. Checking only the endpoints did not prove a contiguous committed prefix. After a private allocation was cached, decommitting a middle page left both endpoints committed with the same AllocationBase, so the cache kept accepting a buffer after the gap that the uncached walk refuses. A gap before the requested start went unseen in the same way. What changed: the cache is gone, and nothing is kept between placements. Lifecycle invalidation cannot be made complete, because a consumer can call `VirtualFree(MEM_DECOMMIT)`, or release and reallocate memory, directly through the Windows API, and the stand-ins never see that. Each placement is now proven at call time (`src/startup_stubs/placed_buffer_range.h`): - **First:** `QueryVirtualMemoryInformation(MemoryRegionInfo)` reports the base, size and committed bytes of the allocation holding the address. When the allocation is private and its committed bytes equal its size, every page is committed, so the full walk's committed run is the whole allocation. The decision then follows exactly from base and size. - **Otherwise:** a partly committed or mapped allocation, a free address, or a missing export takes the original walk from the base, stopped at the end of the buffer. In a local startup run on Windows 10 (RTX 3060), the time from launch to the first interactive screen was 72.6 s, against 81 to 94 s with the earlier cache and 197 to 252 s with the bounded walk alone. Of 3,388 placements, 3,259 were proven by region information and 129 fell back to the walk (343 `VirtualQuery` calls in total). Why not only the bounded walk: in a startup run it was slower than the original code. `VirtualQuery` scans page-table entries up to the next attribute change, which costs milliseconds on large touched regions (synthetic: about 5 ms per GiB with one touched page per 64 KiB). The region-information call does not scan; it costs about 0.0004 ms. Its committed-bytes count matched a full `VirtualQuery` count for private allocations after decommit and recommit, double decommit, commit of committed pages, guard and no-access pages, `MEM_RESET`, reserve-then-commit and placeholder pieces. `startup_placed_buffer_range` (replaces `startup_placed_allocation_cache`) compares every alias or refusal, base and type with the full walk on the same memory. Each case places a buffer first, then changes the memory, then places again: - a middle page decommitted under a buffer, before a buffer, and five pages before a buffer, then committed again; - the reviewed repro: 1 MiB committed, a buffer at +0x80000 for 0x10000 bytes, then +0x10000 decommitted for 0x10000 bytes; - the same-base replacement; - a mapped view; - a reservation committed in two steps; - a placeholder piece; - write-watch, top-down, executable, heap-block, AWE and (with the lock-pages privilege) large-page allocations; - a 64 MiB striped block; - a 512 MiB touched block, which must be proven with no `VirtualQuery` call. The controls (`WILL_FAIL`) must fail only for their stated reason. `--mutate region-only` trusts the allocation size without the committed-bytes check and fails 10 placements, all in the partly committed cases. `--mutate walk-only` never uses the region information and fails only the call count. @⁠tedin7: your repro is a case in `startup_placed_buffer_range` with exactly those numbers. The endpoint-only cache at 229f7b13 accepted the second placement, while the current code refuses it, the same answer as the full walk. It is refused because a decommitted page makes the allocation's committed bytes smaller than its size, so the placement falls back to the walk from the base, which stops at the gap. ``` ctest --preset release --no-tests=error -R '^(startup_placed_buffer_range.*|startup_placed_vertex_.*|xmem_page_runs_device_.*|startup_placement_.*|cpu_placed_draw_.*)$' -V ``` Windows 10 19045, MSVC 19.44, RTX 3060 (WARP for `_warp`), `dev` and `release`, head 20f8e1d5 (merges current main): 12 of 12 passed, 0 failed, 0 skipped. AI-assisted (Claude Code).
Author
Owner

@⁠tedin7 on GitHub · 2026-11-24 19:05 UTC · original · reviewed

Peer review of head 229f7b1350f3057bc75c3f5d0eacf11d422376fc (PRODUCTION_REVIEW_ONLY, technical comment only).

One P2 finding (a documented limitation that is wider than documented). No P1.

P2: src/startup_stubs/placed_allocation_cache.h, measure() / still_valid() (lines ~66-70 and ~101-113).
The uncached walk counts committed bytes contiguously from the allocation base and stops at the first hole. A cache hit only re-queries the region at the buffer's first byte and the region at its last byte. Pages decommitted between the base and the first queried region are therefore not noticed either, not only pages between the two queried regions as docs/startup-runtime.md states. The cached extent.bytes stays too large, so slot 133 accepts a placement the uncached path refuses (offset > allocation_bytes gives E_INVALIDARG).

Reproduction (local, mingw-w64 build of placed_allocation_cache.h run under wine):

  1. VirtualAlloc 1 MiB committed.
  2. measure(p+0x80000, 0x10000) remembers the extent (0x100000).
  3. VirtualFree(p+0x10000, 0x10000, MEM_DECOMMIT).
  4. measure(p+0x80000, 0x10000) is a cache hit and returns bytes=0x100000. measure_allocation() on the same address returns bytes=0x10000, so the original code returns E_INVALIDARG.

Impact: the placement then reaches CreatePlacedResource, which the code comment says throws inside D3D12Core, instead of being skipped. The comment in the docs says no stand-in decommits part of an allocation, so this is low-likelihood. Either widen the doc sentence to "anywhere before the buffer's last byte", or add a cheap guard (for example drop the entry on VirtualFree or MEM_DECOMMIT hooks, or bound the cache lifetime).

Everything else checked without findings:

  • The lookup (upper_bound, then last - base < bytes) and the overflow guard on start + span - 1.
  • remember() drops overlapping entries, so a block replaced at the same base is handled.
  • Locking: a shared lock for lookup, exclusive for change, and the OOM path falls back to measuring again.
  • The call site keeps the E_POINTER / desc checks before desc->Width is read.
  • The second query is skipped only when one region covers the whole buffer.

Local mutation (restored with git checkout -- .): on a cache hit, out = known changed to out = known; out.bytes += 0x10000;. startup_placed_allocation_cache_check fails with "258 failure(s)" ("partly committed: inside the larger extent: expected bytes=0x200000, got 0x210000"). A second mutation, last - base < bytes changed to start - base < bytes, survives. It is masked by the last-byte region query in still_valid(), so the behaviour is equivalent; I am not asking for a new test for it.

Commands run:

  • git diff --check <merge-base>...HEAD: clean
  • tests/bidi_controls_check.py: pass
  • docs_links_check: not applicable, docs only gained a section
  • mingw-w64 g++ -std=c++17 -O2 build of tests/startup_placed_allocation_cache_check.cpp, run under wine: default run passes ("all cases match the uncached path", 256 of 256 accepted, 2 walks, 256 saved). --mutate none and --mutate address-only fail for the stated reasons.
  • Reproduction program for the finding (same build).
  • Windows Server 2025 (build 26100, MSVC 14.44, WARP) at the same commit 229f7b13, target startup_placed_allocation_cache_check, regex ^startup_placed_allocation_cache:
    • Debug (--preset dev): 3/3 tests passed
    • Release /O2 (--preset release): 3/3 tests passed
  • The PR merges cleanly with current main (git merge-tree).

AI-assisted; commands listed were executed locally.

**@⁠tedin7** on GitHub · 2026-11-24 19:05 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1766#pullrequestreview-6045574078) · **reviewed** Peer review of head `229f7b1350f3057bc75c3f5d0eacf11d422376fc` (PRODUCTION_REVIEW_ONLY, technical comment only). **One P2 finding (a documented limitation that is wider than documented). No P1.** **P2: `src/startup_stubs/placed_allocation_cache.h`, `measure()` / `still_valid()` (lines ~66-70 and ~101-113).** The uncached walk counts committed bytes contiguously from the allocation base and stops at the first hole. A cache hit only re-queries the region at the buffer's first byte and the region at its last byte. Pages decommitted between the base and the first queried region are therefore not noticed either, not only pages between the two queried regions as docs/startup-runtime.md states. The cached `extent.bytes` stays too large, so slot 133 accepts a placement the uncached path refuses (`offset > allocation_bytes` gives `E_INVALIDARG`). Reproduction (local, mingw-w64 build of `placed_allocation_cache.h` run under wine): 1. `VirtualAlloc` 1 MiB committed. 2. `measure(p+0x80000, 0x10000)` remembers the extent (0x100000). 3. `VirtualFree(p+0x10000, 0x10000, MEM_DECOMMIT)`. 4. `measure(p+0x80000, 0x10000)` is a cache hit and returns bytes=0x100000. `measure_allocation()` on the same address returns bytes=0x10000, so the original code returns `E_INVALIDARG`. Impact: the placement then reaches `CreatePlacedResource`, which the code comment says throws inside D3D12Core, instead of being skipped. The comment in the docs says no stand-in decommits part of an allocation, so this is low-likelihood. Either widen the doc sentence to "anywhere before the buffer's last byte", or add a cheap guard (for example drop the entry on `VirtualFree` or `MEM_DECOMMIT` hooks, or bound the cache lifetime). Everything else checked without findings: - The lookup (`upper_bound`, then `last - base < bytes`) and the overflow guard on `start + span - 1`. - `remember()` drops overlapping entries, so a block replaced at the same base is handled. - Locking: a shared lock for lookup, exclusive for change, and the OOM path falls back to measuring again. - The call site keeps the `E_POINTER` / `desc` checks before `desc->Width` is read. - The second query is skipped only when one region covers the whole buffer. Local mutation (restored with `git checkout -- .`): on a cache hit, `out = known` changed to `out = known; out.bytes += 0x10000;`. `startup_placed_allocation_cache_check` fails with "258 failure(s)" ("partly committed: inside the larger extent: expected bytes=0x200000, got 0x210000"). A second mutation, `last - base < bytes` changed to `start - base < bytes`, survives. It is masked by the last-byte region query in `still_valid()`, so the behaviour is equivalent; I am not asking for a new test for it. Commands run: - `git diff --check <merge-base>...HEAD`: clean - `tests/bidi_controls_check.py`: pass - docs_links_check: not applicable, docs only gained a section - mingw-w64 g++ -std=c++17 -O2 build of `tests/startup_placed_allocation_cache_check.cpp`, run under wine: default run passes ("all cases match the uncached path", 256 of 256 accepted, 2 walks, 256 saved). `--mutate none` and `--mutate address-only` fail for the stated reasons. - Reproduction program for the finding (same build). - Windows Server 2025 (build 26100, MSVC 14.44, WARP) at the same commit `229f7b13`, target `startup_placed_allocation_cache_check`, regex `^startup_placed_allocation_cache`: - Debug (`--preset dev`): 3/3 tests passed - Release `/O2` (`--preset release`): 3/3 tests passed - The PR merges cleanly with current `main` (`git merge-tree`). AI-assisted; commands listed were executed locally.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 21:32 UTC · original · requested changes

The endpoint checks can both pass after a middle page of the same MEM_PRIVATE allocation is decommitted. For a three-page fully committed allocation, warm the cache, decommit the middle page, then query the original full span: the old walk stops after page one, but the proposed hit still reports the old full extent. A gap can also sit before the placement start because the alias decision relies on the committed run from AllocationBase. Please require a current contiguous committed range from the base, or tie the cache to tracked allocation generations and page-state invalidation. Retain the existing walk for addresses without that lifetime contract. Add a source-built interior-decommit control with committed endpoints; both dispositions must agree with the original walk.

**@⁠R0Y00** on GitHub · 2026-11-24 21:32 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1766#pullrequestreview-6047754318) · **requested changes** The endpoint checks can both pass after a middle page of the same MEM_PRIVATE allocation is decommitted. For a three-page fully committed allocation, warm the cache, decommit the middle page, then query the original full span: the old walk stops after page one, but the proposed hit still reports the old full extent. A gap can also sit before the placement start because the alias decision relies on the committed run from AllocationBase. Please require a current contiguous committed range from the base, or tie the cache to tracked allocation generations and page-state invalidation. Retain the existing walk for addresses without that lifetime contract. Add a source-built interior-decommit control with committed endpoints; both dispositions must agree with the original walk.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 21:46 UTC · original · approved these changes

Rechecked the replacement without a persistent extent cache. The Windows Release range test and both negative controls pass on this exact head. A partially committed allocation now walks from AllocationBase, so the middle decommit and gap before placement agree with the old walk.

**@⁠R0Y00** on GitHub · 2026-11-24 21:46 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1766#pullrequestreview-6047928662) · **approved these changes** Rechecked the replacement without a persistent extent cache. The Windows Release range test and both negative controls pass on this exact head. A partially committed allocation now walks from AllocationBase, so the middle decommit and gap before placement agree with the old walk.
This discussion has been locked. Commenting is limited to contributors.
No description provided.