xmem: opt-in placement of 0x283 startup blocks below 2^40 #1853

Merged
MasterDomino merged 1 commit from upstream-pr/1860 into main 2026-11-24 19:54:35 +00:00
MasterDomino commented 2026-11-24 19:42:30 +00:00 (Migrated from github.com)

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 that XMemVirtualAlloc commits for a null address, type 0x20003000, attribute 0x283 and protection 0x2004 or 0x2404. 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. Today commit_startup_block places these blocks with VirtualAlloc2 and 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 the tools/startup/run.ps1 -StartupBelow40 switch, which only sets it). It is off by default, and with it off the code path is unchanged.
  • It applies only to the traced shape: a null address, type 0x20003000, attribute 0x283 and protection 0x2004 or 0x2404. That shape keeps the existing size rule (a nonzero multiple of 64 KiB). Its VirtualAlloc2 call gets MEM_ADDRESS_REQUIREMENTS{HighestEndingAddress = 2^40 - 1, Alignment = 2 MiB}.
  • After the call, the shim checks that the base is 2 MiB aligned, that bytes <= 2^40 and that base <= 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 with VirtualFree and refused with ERROR_NOT_ENOUGH_MEMORY. The log line reads ... placed at <base> outside the 2 MiB-aligned range below 2^40 (released).
  • If Windows cannot place the block below 2^40, the call returns the existing failure: NULL with the host error. It also logs a distinct line: 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.
  • Each placed block is logged as 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.
  • The other startup pairs (0x183/0x22004, and type 0x20403000 with 0x181/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_40 mutants exist only in the xmem_shim_test_controls build (X2W_XMEM_TEST_CONTROLS). The deployed xmem.dll ignores them.
  • Docs: a switch row in docs/startup-runtime.md and a new docs/experiments.md section, "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 by XMemVirtualAlloc must 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 loads xmem.dll by path and reads the shim's log through X2W_TRAP_LOG. To exhaust the low range deterministically, it reserves every free region below 2^40 (a VirtualQuery walk followed by MEM_RESERVE). A VirtualAlloc2 probe with the same limit must then fail. The test does not depend on where Windows places an unconstrained allocation.

  • Unchanged paths: with the configuration off, the 0x283 pair with both protections succeeds as before. With it on, 0x183/0x22004 and 0x20403000/0x181 succeed. A size that is not a multiple of 64 KiB keeps the 87 refusal. None of these write a below-2^40 log line.
  • Placed: 2 MiB (both protections), 6 MiB (an odd multiple of 2 MiB), 2 MiB + 64 KiB (the last byte is not on a 2 MiB boundary) and 2 GiB (a multi-GiB block). Each is checked for a 2 MiB-aligned base, 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.
  • Exhausted: with all of the low range reserved, a 6 MiB call returns NULL with host error 8 and logs the refusal line. Whole-address-space VirtualQuery snapshots 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.
  • Boundary: with only [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 exactly 0xFFFFC00000, and its last byte, 0xFFFFFFFFFF, is written and read back.
  • Impossible sizes: a 2^40-byte call returns host error 487, and 2^40 + 2 MiB returns 87. Both log the refusal line and leave nothing allocated.

Environment: commit 733ecc40 on 82749f0f, Windows 11 build 26300.9974, MSVC 19.44.35221, dev preset. The release preset builds and passes the five new tests too.

. .\tools\dev.ps1
cmake --preset dev -DINTAKE_PYTHON=<repo>/out/pe-env/Scripts/python.exe
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^xmem_startup_below_40' -V
# 5/5 passed (also 10 repeats with -j 5: no failures)
ctest --preset dev --no-tests=error -R 'xmem|era_|cpu_native_xmem|windows_placeholder|startup_heartbeat_commit' -j 4
# 59/59 passed
ctest --preset dev -R '^startup_' -j 4
# 229 passed, 0 failed, 4 skipped (startup_view_heap_hardware, startup_view_heap_warp,
# startup_logical_view_warp, startup_logical_view_hardware: capability skips, not caused by this change)
python -B tests/docs_links_check.py     # Checked 18 Markdown files and 374 links.
python -B tests/bidi_controls_check.py  # no bidirectional controls
cmake --preset release -DINTAKE_PYTHON=<repo>/out/pe-env/Scripts/python.exe
cmake --build --preset release
ctest --preset release --no-tests=error -R '^xmem_startup_below_40'
# 5/5 passed

Negative controls

These run against xmem_shim_test_controls, with each control set through X2W_XMEM_TEST_BELOW_40:

CTest case Mutant Required output Observed
xmem_startup_below_40_mutant_no_limit address limit dropped check_failed: exhausted_host_refusal; neither exhausted_null nor exhausted_no_leak x2w-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) then check_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_check limit and check dropped check_failed: exhausted_null exhausted: size=0x600000 -> 000001FC8C200000 (error 0), check_failed: exhausted_null, check_failed: exhausted_host_refusal, new allocation 000001FC8C200000, 0x600000 bytes, check_failed: exhausted_no_leak, plus placed_below_limit and boundary_last_byte failures
xmem_startup_below_40_mutant_no_limit_no_release limit dropped, refused block not released check_failed: exhausted_no_leak; not exhausted_null ... placed at 0000025FF8000000 ... (not released), new allocation 0000025FF8000000, 0x600000 bytes, check_failed: exhausted_no_leak
xmem_startup_below_40_deployed_ignores_mutants all three names set against the deployed xmem.dll xmem_startup_below_40: PASS passed

Each mutant run also ends in xmem_startup_below_40: FAIL; the controls fail if PASS is printed.

Title run on this exact candidate (local)

Checkpoint run for #1758: 82749f0f plus 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.

  • Blocks: the three 0x283 startup blocks were placed with their last byte below 2^40, two with protection 0x2004 and one with 0x2404. None was refused.
  • Placed resources: 3,241 title addresses below 2^40. 337 stay above; they belong to shapes this PR deliberately leaves alone.
  • First checkpoint: the builder's 40-bit destination check no longer fails (no E_INVALIDARG).
  • Next checkpoint: the run ends at 299.8 s on E_FAIL with the builder's Failed to allocate upload buffer. That's the command-list slot 148 refusal from #1758, owned by main development.
  • Memory: peak commit 14.7 GiB.
  • Caveat: this build changed the pipeline-cache keys, so the run drew with untranslated pipelines. That doesn't affect the allocation checkpoint. The title run is a confirmation only; the fixture above is the evidence for the contract.

Not covered

  • Other paths that may need high-address handling are separate work: slot 148 and command-buffer writes.
  • With the configuration on, cpu_native_xmem_pool_commit fails only on its "stderr must be empty" assertion, because of the new placement log lines. Its default run (configuration off) passes. Both 0x283 blocks were placed below 2^40 in that run.
  • The console's own placement for these blocks is not established. The configuration is a desktop compatibility policy for one consumer's 40-bit limit.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6589958466 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1860). Originally posted by [MasterDomino](https://github.com/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 that `XMemVirtualAlloc` commits for a null address, type `0x20003000`, attribute `0x283` and protection `0x2004` or `0x2404`. 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. Today `commit_startup_block` places these blocks with `VirtualAlloc2` and 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 the `tools/startup/run.ps1 -StartupBelow40` switch, which only sets it). It is off by default, and with it off the code path is unchanged. - It applies only to the traced shape: a null address, type `0x20003000`, attribute `0x283` and protection `0x2004` or `0x2404`. That shape keeps the existing size rule (a nonzero multiple of 64 KiB). Its `VirtualAlloc2` call gets `MEM_ADDRESS_REQUIREMENTS{HighestEndingAddress = 2^40 - 1, Alignment = 2 MiB}`. - After the call, the shim checks that the base is 2 MiB aligned, that `bytes <= 2^40` and that `base <= 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 with `VirtualFree` and refused with `ERROR_NOT_ENOUGH_MEMORY`. The log line reads `... placed at <base> outside the 2 MiB-aligned range below 2^40 (released)`. - If Windows cannot place the block below 2^40, the call returns the existing failure: NULL with the host error. It also logs a distinct line: `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. - Each placed block is logged as `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. - The other startup pairs (`0x183`/`0x22004`, and type `0x20403000` with `0x181`/`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_40` mutants exist only in the `xmem_shim_test_controls` build (`X2W_XMEM_TEST_CONTROLS`). The deployed `xmem.dll` ignores them. - Docs: a switch row in `docs/startup-runtime.md` and a new `docs/experiments.md` section, "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 by `XMemVirtualAlloc` must 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 loads `xmem.dll` by path and reads the shim's log through `X2W_TRAP_LOG`. To exhaust the low range deterministically, it reserves every free region below 2^40 (a `VirtualQuery` walk followed by `MEM_RESERVE`). A `VirtualAlloc2` probe with the same limit must then fail. The test does not depend on where Windows places an unconstrained allocation. - **Unchanged paths:** with the configuration off, the `0x283` pair with both protections succeeds as before. With it on, `0x183`/`0x22004` and `0x20403000`/`0x181` succeed. A size that is not a multiple of 64 KiB keeps the `87` refusal. None of these write a below-2^40 log line. - **Placed:** 2 MiB (both protections), 6 MiB (an odd multiple of 2 MiB), 2 MiB + 64 KiB (the last byte is not on a 2 MiB boundary) and 2 GiB (a multi-GiB block). Each is checked for a 2 MiB-aligned base, `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. - **Exhausted:** with all of the low range reserved, a 6 MiB call returns NULL with host error `8` and logs the refusal line. Whole-address-space `VirtualQuery` snapshots 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. - **Boundary:** with only `[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 exactly `0xFFFFC00000`, and its last byte, `0xFFFFFFFFFF`, is written and read back. - **Impossible sizes:** a 2^40-byte call returns host error `487`, and 2^40 + 2 MiB returns `87`. Both log the refusal line and leave nothing allocated. Environment: commit `733ecc40` on `82749f0f`, Windows 11 build 26300.9974, MSVC 19.44.35221, `dev` preset. The release preset builds and passes the five new tests too. ```powershell . .\tools\dev.ps1 cmake --preset dev -DINTAKE_PYTHON=<repo>/out/pe-env/Scripts/python.exe cmake --build --preset dev ctest --preset dev --no-tests=error -R '^xmem_startup_below_40' -V # 5/5 passed (also 10 repeats with -j 5: no failures) ctest --preset dev --no-tests=error -R 'xmem|era_|cpu_native_xmem|windows_placeholder|startup_heartbeat_commit' -j 4 # 59/59 passed ctest --preset dev -R '^startup_' -j 4 # 229 passed, 0 failed, 4 skipped (startup_view_heap_hardware, startup_view_heap_warp, # startup_logical_view_warp, startup_logical_view_hardware: capability skips, not caused by this change) python -B tests/docs_links_check.py # Checked 18 Markdown files and 374 links. python -B tests/bidi_controls_check.py # no bidirectional controls cmake --preset release -DINTAKE_PYTHON=<repo>/out/pe-env/Scripts/python.exe cmake --build --preset release ctest --preset release --no-tests=error -R '^xmem_startup_below_40' # 5/5 passed ``` ## Negative controls These run against `xmem_shim_test_controls`, with each control set through `X2W_XMEM_TEST_BELOW_40`: | CTest case | Mutant | Required output | Observed | |---|---|---|---| | `xmem_startup_below_40_mutant_no_limit` | address limit dropped | `check_failed: exhausted_host_refusal`; neither `exhausted_null` nor `exhausted_no_leak` | `x2w-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)` then `check_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_check` | limit and check dropped | `check_failed: exhausted_null` | `exhausted: size=0x600000 -> 000001FC8C200000 (error 0)`, `check_failed: exhausted_null`, `check_failed: exhausted_host_refusal`, `new allocation 000001FC8C200000, 0x600000 bytes`, `check_failed: exhausted_no_leak`, plus `placed_below_limit` and `boundary_last_byte` failures | | `xmem_startup_below_40_mutant_no_limit_no_release` | limit dropped, refused block not released | `check_failed: exhausted_no_leak`; not `exhausted_null` | `... placed at 0000025FF8000000 ... (not released)`, `new allocation 0000025FF8000000, 0x600000 bytes`, `check_failed: exhausted_no_leak` | | `xmem_startup_below_40_deployed_ignores_mutants` | all three names set against the deployed `xmem.dll` | `xmem_startup_below_40: PASS` | passed | Each mutant run also ends in `xmem_startup_below_40: FAIL`; the controls fail if `PASS` is printed. ## Title run on this exact candidate (local) Checkpoint run for #1758: `82749f0f` plus 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`. - **Blocks:** the three `0x283` startup blocks were placed with their last byte below 2^40, two with protection `0x2004` and one with `0x2404`. None was refused. - **Placed resources:** 3,241 title addresses below 2^40. 337 stay above; they belong to shapes this PR deliberately leaves alone. - **First checkpoint:** the builder's 40-bit destination check no longer fails (no `E_INVALIDARG`). - **Next checkpoint:** the run ends at 299.8 s on `E_FAIL` with the builder's `Failed to allocate upload buffer`. That's the command-list slot 148 refusal from #1758, owned by main development. - **Memory:** peak commit 14.7 GiB. - **Caveat:** this build changed the pipeline-cache keys, so the run drew with untranslated pipelines. That doesn't affect the allocation checkpoint. The title run is a confirmation only; the fixture above is the evidence for the contract. ## Not covered - Other paths that may need high-address handling are separate work: slot 148 and command-buffer writes. - With the configuration on, `cpu_native_xmem_pool_commit` fails only on its "stderr must be empty" assertion, because of the new placement log lines. Its default run (configuration off) passes. Both `0x283` blocks were placed below 2^40 in that run. - The console's own placement for these blocks is not established. The configuration is a desktop compatibility policy for one consumer's 40-bit limit. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 20:01:06 +00:00
tedin7 commented 2026-11-24 20:01:07 +00:00 (Migrated from github.com)

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 0
  • python -B tests/bidi_controls_check.py: rc 0

The 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:

  • The change applies only to attributes == 0x283 with the config variable on. supported already limits that attribute to protection 0x2004 or 0x2404, so the other shapes and the config-off path return VirtualAlloc2 unchanged.
  • HighestEndingAddress = 2^40 - 1 is paired with the existing 2 MiB Alignment. The post-check base % 2MiB == 0 && bytes <= 2^40 && base <= 2^40 - bytes cannot overflow.
  • A NULL result restores the host error after logging. A misplaced block is released, logged and returned as NULL with ERROR_NOT_ENOUGH_MEMORY. Nothing is truncated or retried higher, which matches the stated contract.
  • The mutants exist only under X2W_XMEM_TEST_CONTROLS, and the deployed build compiles them to false. The deployed_ignores_mutants test covers that.
  • The regex pairs in CMakeLists.txt match the check names the test prints. In no_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. The no_check and no_release pairs also read correctly.
  • The boundary and impossible-size cases (2^40 - 4 MiB window, 2^40, 2^40 + 2 MiB) exercise the overflow-safe comparison.
  • The #startup-blocks-below-240 anchor resolves (docs_links_check). The wording is title-neutral.

Nits, no change required:

  • In docs/experiments.md, "placed at 0x8000000 (2 GiB at 0x80000000)" reads like a typo or a mixed-up address. Please state the address once.
  • The LowRangeBlockers walk 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046198000 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1860#pullrequestreview-6046198000). Originally posted by [tedin7](https://github.com/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 0 - `python -B tests/bidi_controls_check.py`: rc 0 The 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`: - The change applies only to `attributes == 0x283` with the config variable on. `supported` already limits that attribute to protection `0x2004` or `0x2404`, so the other shapes and the config-off path return `VirtualAlloc2` unchanged. - `HighestEndingAddress = 2^40 - 1` is paired with the existing 2 MiB `Alignment`. The post-check `base % 2MiB == 0 && bytes <= 2^40 && base <= 2^40 - bytes` cannot overflow. - A NULL result restores the host error after logging. A misplaced block is released, logged and returned as NULL with `ERROR_NOT_ENOUGH_MEMORY`. Nothing is truncated or retried higher, which matches the stated contract. - The mutants exist only under `X2W_XMEM_TEST_CONTROLS`, and the deployed build compiles them to `false`. The `deployed_ignores_mutants` test covers that. - The regex pairs in `CMakeLists.txt` match the check names the test prints. In `no_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. The `no_check` and `no_release` pairs also read correctly. - The boundary and impossible-size cases (`2^40 - 4 MiB` window, 2^40, 2^40 + 2 MiB) exercise the overflow-safe comparison. - The `#startup-blocks-below-240` anchor resolves (`docs_links_check`). The wording is title-neutral. Nits, no change required: - In `docs/experiments.md`, "placed at `0x8000000` (2 GiB at `0x80000000`)" reads like a typo or a mixed-up address. Please state the address once. - The `LowRangeBlockers` walk 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.
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
R0Y00/Xbox2Windows!1853
No description provided.