runtime: diagnose failed XMem scatter mappings #2225

Manually merged
github merged 1 commit from gh-pr/2225/head into gh-pr/2225/base 2026-11-27 00:07:26 +00:00
Owner

@⁠R0Y00 on GitHub · 2026-11-25 06:14 UTC · original
R0Y00:codex/xmem-scatter-diagnostics → main · +159 −21 · 2 files · 1 commits · merged 2026-11-25 06:29 UTC

Refs #874.

Report the first failed permissive XMem mapping step with the caller's index, destination address, page ID, Windows error, and virtual-memory region. X2W_XMEM_TRACE_FAILED_BATCH=1 also records the affected address ranges in the failed call so a later fault can be checked for membership. The range trace reports truncation when either operation exceeds 2,048 ranges.

Verification

Built both XMem libraries and the protection fixture in Release with MSVC 19.51.36244.0 on Windows. All 10 selected tests passed.

cmake --preset release -B out/build/xmem-scatter-release -DCMAKE_TRY_COMPILE_CONFIGURATION=Release
cmake --build out/build/xmem-scatter-release --target xmem_shim xmem_shim_test_controls xmem_commit_protection_check
ctest --test-dir out/build/xmem-scatter-release --no-tests=error -R '^(xmem_shim_mapped_pages|xmem_shim_page_runs|xmem_commit_protection(_.+)?)$' --output-on-failure

The source-built control passes reversed destination order, a misaligned address, and a Windows reservation that cannot be replaced. It checks the reported caller index and page ID, host error, affected range, and rollback of the earlier mapping. The previous DLL refuses both calls and permits retry after rollback, but fails the new assertion because it emits only the batch summary.

**@⁠R0Y00** on GitHub · 2026-11-25 06:14 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2225) `R0Y00:codex/xmem-scatter-diagnostics` → `main` · +159 −21 · 2 files · 1 commits · merged 2026-11-25 06:29 UTC Refs #874. Report the first failed permissive XMem mapping step with the caller's index, destination address, page ID, Windows error, and virtual-memory region. `X2W_XMEM_TRACE_FAILED_BATCH=1` also records the affected address ranges in the failed call so a later fault can be checked for membership. The range trace reports truncation when either operation exceeds 2,048 ranges. ### Verification Built both XMem libraries and the protection fixture in Release with MSVC 19.51.36244.0 on Windows. All 10 selected tests passed. ```powershell cmake --preset release -B out/build/xmem-scatter-release -DCMAKE_TRY_COMPILE_CONFIGURATION=Release cmake --build out/build/xmem-scatter-release --target xmem_shim xmem_shim_test_controls xmem_commit_protection_check ctest --test-dir out/build/xmem-scatter-release --no-tests=error -R '^(xmem_shim_mapped_pages|xmem_shim_page_runs|xmem_commit_protection(_.+)?)$' --output-on-failure ``` The source-built control passes reversed destination order, a misaligned address, and a Windows reservation that cannot be replaced. It checks the reported caller index and page ID, host error, affected range, and rollback of the earlier mapping. The previous DLL refuses both calls and permits retry after rollback, but fails the new assertion because it emits only the batch summary.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 00:07:22 +00:00
github manually merged commit d3ebc7b933 into gh-pr/2225/base 2026-11-27 00:07:26 +00:00
Author
Owner

@⁠SuperCraft3M on GitHub · 2026-11-25 20:13 UTC · original

I ran the source-built replacement fixture from bf1c2518 against two Windows 11 x64 Release builds. The baseline was a local integration of acde511e (base 11f11e84). The second build additionally backported the #2225 diagnostics and the scatter preflight from bf1c2518.

$env:X2W_XMEM_PERMISSIVE = '1'
python -B tests/xmem_mapped_pages_check.py <baseline-xmem.dll> --scatter-replacement
Remove-Item Env:X2W_XMEM_PERMISSIVE
ctest --test-dir out/build-c/release --no-tests=error -R '^xmem_shim_(page_runs|mapped_pages)$' --output-on-failure

The baseline returned error 87 and dropped the original view when the batch contained an unowned replacement token. The first failed assertion was ReadProcessMemory on that view. Both CTests passed with preflight. The fixture also confirmed that an unaligned replacement preserves the alias and that a corrected retry succeeds. These checks use synthetic pages and cover the preflight rejection path. Windows mapping failures after preflight remain a separate case.

This is a verification of the existing scatter preflight commit.

**@⁠SuperCraft3M** on GitHub · 2026-11-25 20:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2225#issuecomment-7173354084) I ran the source-built replacement fixture from `bf1c2518` against two Windows 11 x64 Release builds. The baseline was a local integration of `acde511e` (base `11f11e84`). The second build additionally backported the #2225 diagnostics and the scatter preflight from `bf1c2518`. ```powershell $env:X2W_XMEM_PERMISSIVE = '1' python -B tests/xmem_mapped_pages_check.py <baseline-xmem.dll> --scatter-replacement Remove-Item Env:X2W_XMEM_PERMISSIVE ctest --test-dir out/build-c/release --no-tests=error -R '^xmem_shim_(page_runs|mapped_pages)$' --output-on-failure ``` The baseline returned error 87 and dropped the original view when the batch contained an unowned replacement token. The first failed assertion was `ReadProcessMemory` on that view. Both CTests passed with preflight. The fixture also confirmed that an unaligned replacement preserves the alias and that a corrected retry succeeds. These checks use synthetic pages and cover the preflight rejection path. Windows mapping failures after preflight remain a separate case. This is a verification of the existing [scatter preflight commit](https://github.com/R0Y00/Xbox2Windows/commit/bf1c2518a985aeaf501998387954211550ba3afc).
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 06:17 UTC · original · reviewed

Review of head d3ebc7b9339df8d28ce994e6f61214050c94de0a (2 files, +159/-21: src/xmem_mapped_pages.cpp, tests/xmem_mapped_pages_check.py).

Verdict: no blocking findings (P1=0). Two minor notes (P2=2).

State at review time: the Linux checks and the gfx1013 fixtures job pass. The Build and test on Windows job was still pending, and the /O2 job was skipped. I did not run the Windows build.

What I checked

  • map_run now reports failed_address. It points at the failing per-page view after the group fallback, so batch.at(failed_address) in map_pages always finds an entry.
  • MapFailure::record is first-failure-wins. unmap-view is recorded before any restore-view, so the root cause is kept.
  • memory.views.at(at + done) in the restore-view path is safe. The page was found in memory.views earlier in the same loop.
  • unmap_view() is behavior-equivalent to the inlined UnmapViewOfFile2(..., MEM_PRESERVE_PLACEHOLDER) call it replaces. Test injection (test.hit(1)) keeps the ERROR_INVALID_ADDRESS mapping.
  • log_apply_failure index lookup: the last occurrence wins, which matches the apply semantics. The run-containment fallback uses unsigned subtraction addresses[i] - failure.address < view_bytes, which is correct and has no underflow problem.
  • log_failed_ranges is capped at 2048 ranges and prints an explicit truncated omitted_pages= line, as the body says. It is only enabled by X2W_XMEM_TRACE_FAILED_BATCH and only on failure, so the hot path is unchanged.
  • The test uses reversed destination order, so the caller index (0) differs from the sorted position. It also covers a misaligned address and a non-replaceable reservation, and checks rollback by re-mapping base afterward.

Notes

  • P2 tests/xmem_mapped_pages_check.py (scatter_failure/check_scatter_diagnostic): the map-view assertion only requires error to be non-zero, so it does not pin the Windows error. The test never exercises the unmap-view or restore-view stages, or the view_bytes > page_bytes containment branch of log_apply_failure. One more test through the existing test.hit(1) injection would cover those.
  • P2 src/xmem_mapped_pages.cpp (MapFailure::record): SetLastError(code) is called after VirtualQuery. That is deliberate and correct. A short comment saying the call preserves the caller's GetLastError() would help the next reader.

Commands run locally (Linux, in a clean worktree at the head SHA)

git diff --stat origin/main...HEAD          # 2 files, +159/-21
git diff --check origin/main...HEAD         # clean, exit 0
python3 tests/docs_links_check.py           # Self-test passed. Checked 18 Markdown files and 412 links.
python3 tests/bidi_controls_check.py        # Self-test passed. Checked 1505 tracked files: no bidirectional controls.
python3 -m py_compile tests/xmem_mapped_pages_check.py   # ok
  • Title and body are neutral and there is a single commit. The PR is not a draft. The mergeable state was UNKNOWN when queried.
  • Mutation testing: this code is Windows-only (UnmapViewOfFile2, VirtualQuery), so I could not build or run the XMem test on Linux. I did no runtime mutation. Please rely on the Windows CI result for the new assertions.

AI-assisted; commands listed were executed locally.

**@⁠tedin7** on GitHub · 2026-11-25 06:17 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2225#pullrequestreview-6052734337) · **reviewed** Review of head `d3ebc7b9339df8d28ce994e6f61214050c94de0a` (2 files, +159/-21: `src/xmem_mapped_pages.cpp`, `tests/xmem_mapped_pages_check.py`). **Verdict: no blocking findings (P1=0). Two minor notes (P2=2).** State at review time: the Linux checks and the gfx1013 fixtures job pass. The `Build and test on Windows` job was still pending, and the `/O2` job was skipped. I did not run the Windows build. ### What I checked - `map_run` now reports `failed_address`. It points at the failing per-page view after the group fallback, so `batch.at(failed_address)` in `map_pages` always finds an entry. - `MapFailure::record` is first-failure-wins. `unmap-view` is recorded before any `restore-view`, so the root cause is kept. - `memory.views.at(at + done)` in the `restore-view` path is safe. The page was found in `memory.views` earlier in the same loop. - `unmap_view()` is behavior-equivalent to the inlined `UnmapViewOfFile2(..., MEM_PRESERVE_PLACEHOLDER)` call it replaces. Test injection (`test.hit(1)`) keeps the `ERROR_INVALID_ADDRESS` mapping. - `log_apply_failure` index lookup: the last occurrence wins, which matches the apply semantics. The run-containment fallback uses unsigned subtraction `addresses[i] - failure.address < view_bytes`, which is correct and has no underflow problem. - `log_failed_ranges` is capped at 2048 ranges and prints an explicit `truncated omitted_pages=` line, as the body says. It is only enabled by `X2W_XMEM_TRACE_FAILED_BATCH` and only on failure, so the hot path is unchanged. - The test uses reversed destination order, so the caller index (0) differs from the sorted position. It also covers a misaligned address and a non-replaceable reservation, and checks rollback by re-mapping `base` afterward. ### Notes - P2 `tests/xmem_mapped_pages_check.py` (`scatter_failure`/`check_scatter_diagnostic`): the `map-view` assertion only requires `error` to be non-zero, so it does not pin the Windows error. The test never exercises the `unmap-view` or `restore-view` stages, or the `view_bytes > page_bytes` containment branch of `log_apply_failure`. One more test through the existing `test.hit(1)` injection would cover those. - P2 `src/xmem_mapped_pages.cpp` (`MapFailure::record`): `SetLastError(code)` is called after `VirtualQuery`. That is deliberate and correct. A short comment saying the call preserves the caller's `GetLastError()` would help the next reader. ### Commands run locally (Linux, in a clean worktree at the head SHA) ``` git diff --stat origin/main...HEAD # 2 files, +159/-21 git diff --check origin/main...HEAD # clean, exit 0 python3 tests/docs_links_check.py # Self-test passed. Checked 18 Markdown files and 412 links. python3 tests/bidi_controls_check.py # Self-test passed. Checked 1505 tracked files: no bidirectional controls. python3 -m py_compile tests/xmem_mapped_pages_check.py # ok ``` - Title and body are neutral and there is a single commit. The PR is not a draft. The mergeable state was UNKNOWN when queried. - Mutation testing: this code is Windows-only (`UnmapViewOfFile2`, `VirtualQuery`), so I could not build or run the XMem test on Linux. I did no runtime mutation. Please rely on the Windows CI result for the new assertions. AI-assisted; commands listed were executed locally.
This discussion has been locked. Commenting is limited to contributors.
No description provided.