xmem: opt-in placement of 0x283 startup blocks below 2^40 #1853
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1860"
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 R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-24T19:42:30Z.
Refs #1758
Refs #874
What and why
A package acceleration-structure builder refuses a destination address that has bits set above bit 39 (
E_INVALIDARG). In local startup runs, the destination came from the large-page startup blocks thatXMemVirtualAlloccommits for a null address, type0x20003000, attribute0x283and protection0x2004or0x2404. Placing only those blocks below 2^40 passed the builder's check. Lowering the startup reservations, the null-address placeholder reservations or the GPU-view reservation alone did not. Todaycommit_startup_blockplaces these blocks withVirtualAlloc2and a 2 MiB alignment requirement only, and Windows usually puts them above 2^40.As scoped on #1758, this PR adds an explicit startup compatibility configuration for that one allocation path. The earlier experiment that lowered every address was only diagnostic. This PR does not truncate, mask or fall back to a high address.
Change
X2W_XMEM_STARTUP_BELOW_40=1(and thetools/startup/run.ps1 -StartupBelow40switch, which only sets it). It is off by default, and with it off the code path is unchanged.0x20003000, attribute0x283and protection0x2004or0x2404. That shape keeps the existing size rule (a nonzero multiple of 64 KiB). ItsVirtualAlloc2call getsMEM_ADDRESS_REQUIREMENTS{HighestEndingAddress = 2^40 - 1, Alignment = 2 MiB}.bytes <= 2^40and thatbase <= 2^40 - bytes. Together these mean the last byte is below 2^40, and the arithmetic cannot overflow. A block that fails the check is released withVirtualFreeand refused withERROR_NOT_ENOUGH_MEMORY. The log line reads... placed at <base> outside the 2 MiB-aligned range below 2^40 (released).x2w-xmem startup block below 2^40 refused: size=<size> attr=0x283 prot=<prot>, no 2 MiB-aligned range ending below 2^40 (host error <n>). No block is allocated, and the shim does not retry the call higher.x2w-xmem startup block below 2^40: <base> size=<size> last=<last byte> attr=0x283 prot=<prot>, so a run shows where every block went.0x183/0x22004, and type0x20403000with0x181/PAGE_READWRITE), the reserve-only profile, the permissive paths and calls with an explicit address all keep their current paths. A call with an explicit address never reaches the startup commit profile.X2W_XMEM_TEST_BELOW_40mutants exist only in thexmem_shim_test_controlsbuild (X2W_XMEM_TEST_CONTROLS). The deployedxmem.dllignores them.docs/startup-runtime.mdand a newdocs/experiments.mdsection, "Startup blocks below 2^40".Address domain
The builder receives the title's CPU address of the block, not a desktop D3D12 GPU virtual address. The blocks lie outside the title's CPU/GPU reservation pair. As documented in
docs/experiments.md, "CPU/GPU range translation against a desktop D3D12 heap (#874)", addresses outside that pair pass through the title's translation unchanged. So the CPU address range returned byXMemVirtualAllocmust fit in 40 bits. This consumer gets no desktop GPU virtual address, so none is checked here.Tests
The new test is
tests/xmem_startup_below_40_check.cpp. It loadsxmem.dllby path and reads the shim's log throughX2W_TRAP_LOG. To exhaust the low range deterministically, it reserves every free region below 2^40 (aVirtualQuerywalk followed byMEM_RESERVE). AVirtualAlloc2probe with the same limit must then fail. The test does not depend on where Windows places an unconstrained allocation.0x283pair with both protections succeeds as before. With it on,0x183/0x22004and0x20403000/0x181succeed. A size that is not a multiple of 64 KiB keeps the87refusal. None of these write a below-2^40 log line.base + size - 1 < 2^40, a committed private read/write region of exactly that size, zero first and last bytes that are written and read back, and exactly one placement log line.8and logs the refusal line. Whole-address-spaceVirtualQuerysnapshots taken before and after the call show no new allocation. With the configuration off, the same call succeeds above 2^40. After the blockers are released, it succeeds below 2^40.[2^40 - 4 MiB, 2^40)free below 2^40, a 4 MiB + 64 KiB call is refused (8, nothing left allocated). A 4 MiB call lands at exactly0xFFFFC00000, and its last byte,0xFFFFFFFFFF, is written and read back.487, and 2^40 + 2 MiB returns87. Both log the refusal line and leave nothing allocated.Environment: commit
733ecc40on82749f0f, Windows 11 build 26300.9974, MSVC 19.44.35221,devpreset. The release preset builds and passes the five new tests too.Negative controls
These run against
xmem_shim_test_controls, with each control set throughX2W_XMEM_TEST_BELOW_40:xmem_startup_below_40_mutant_no_limitcheck_failed: exhausted_host_refusal; neitherexhausted_nullnorexhausted_no_leakx2w-xmem startup block below 2^40 refused: size=0x600000 attr=0x283 prot=0x2004, placed at 000001F997600000 outside the 2 MiB-aligned range below 2^40 (released)thencheck_failed: exhausted_host_refusal. The check after the call catches the high block and rolls it back.xmem_startup_below_40_mutant_no_limit_no_checkcheck_failed: exhausted_nullexhausted: size=0x600000 -> 000001FC8C200000 (error 0),check_failed: exhausted_null,check_failed: exhausted_host_refusal,new allocation 000001FC8C200000, 0x600000 bytes,check_failed: exhausted_no_leak, plusplaced_below_limitandboundary_last_bytefailuresxmem_startup_below_40_mutant_no_limit_no_releasecheck_failed: exhausted_no_leak; notexhausted_null... placed at 0000025FF8000000 ... (not released),new allocation 0000025FF8000000, 0x600000 bytes,check_failed: exhausted_no_leakxmem_startup_below_40_deployed_ignores_mutantsxmem.dllxmem_startup_below_40: PASSEach mutant run also ends in
xmem_startup_below_40: FAIL; the controls fail ifPASSis printed.Title run on this exact candidate (local)
Checkpoint run for #1758:
82749f0fplus this commit, release build,X2W_XMEM_STARTUP_BELOW_40=1. Same machine as the #1758 runs: RX 9070 XT, driver 32.0.31044.16, Windows 11 build 26300. Same input script as runs r/s,-MemoryGiB 32.0x283startup blocks were placed with their last byte below 2^40, two with protection0x2004and one with0x2404. None was refused.E_INVALIDARG).E_FAILwith the builder'sFailed to allocate upload buffer. That's the command-list slot 148 refusal from #1758, owned by main development.Not covered
cpu_native_xmem_pool_commitfails only on its "stderr must be empty" assertion, because of the new placement log lines. Its default run (configuration off) passes. Both0x283blocks were placed below 2^40 in that run.🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:46:14Z.
Review: COMMENTED
Reviewed head
733ecc4037b3119d86dd22d58bc93480fe89d3a0.Linux checks in a clean worktree:
git diff --check origin/main...HEAD: clean (rc 0)python -B tests/docs_links_check.py: rc 0python -B tests/bidi_controls_check.py: rc 0The C++ tests in this PR are Windows-only. I could not build or run them here, so the rest is source analysis. Win2025 results are still needed.
Findings: 0 P1, 0 P2.
What I checked in
src/xmem_mapped_pages.cpp:attributes == 0x283with the config variable on.supportedalready limits that attribute to protection0x2004or0x2404, so the other shapes and the config-off path returnVirtualAlloc2unchanged.HighestEndingAddress = 2^40 - 1is paired with the existing 2 MiBAlignment. The post-checkbase % 2MiB == 0 && bytes <= 2^40 && base <= 2^40 - bytescannot overflow.ERROR_NOT_ENOUGH_MEMORY. Nothing is truncated or retried higher, which matches the stated contract.X2W_XMEM_TEST_CONTROLS, and the deployed build compiles them tofalse. Thedeployed_ignores_mutantstest covers that.CMakeLists.txtmatch the check names the test prints. Inno_limit, the host-refusal check fails because the log says(released), not(host error N), while the NULL and leak checks pass. The expected and forbidden outputs follow from that. Theno_checkandno_releasepairs also read correctly.2^40 - 4 MiBwindow, 2^40, 2^40 + 2 MiB) exercise the overflow-safe comparison.#startup-blocks-below-240anchor resolves (docs_links_check). The wording is title-neutral.Nits, no change required:
docs/experiments.md, "placed at0x8000000(2 GiB at0x80000000)" reads like a typo or a mixed-up address. Please state the address once.LowRangeBlockerswalk reserves the whole low address space of the test process. This is fine as a serial test. A comment saying it must stay serial would help whoever adds parallel tests later.AI-assisted; commands listed were executed locally.