runtime: check era_physical page ownership #1729

Merged
Asimmimx merged 1 commit from upstream-pr/1736 into main 2026-11-24 20:08:15 +00:00
Asimmimx commented 2026-11-24 17:58:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Asimmimx on 2026-11-24T17:58:13Z.


Refs #1149.

Change

src/era_physical.h is the desktop bookkeeping for title physical pages behind XMemAllocatePhysicalPages/XMemFreePhysicalPages. Pages are opaque nonzero numbers from 1 to 2^18 (16 GiB of 64 KiB pages). Allocation is all or nothing, and a release must name whole live grants. The header also exports owns(page), which XMemMapPhysicalPages/XMemMapPhysicalPagesScatter (src/xmem_mapped_pages.cpp) call for every page token before they map it.

Device-free tests that reach the file today:

  • era_physical_controls and era_physical_lifetime (tests/era_physical_check.cpp): seven release controls against a lenient build, plus exact totals, exhaustion and argument refusals;
  • xmem_shim_physical_pages: the same policy through the xmem.dll exports, including a full 2^18 grant;
  • xmem_shim_mapped_pages, xmem_shim_page_runs, xmem_commit_protection, xmem_page_stats: mapping through xmem.dll.

era_physical_check never calls owns. The mapping tests only show that released page tokens are no longer accepted. 5 of 24 single-point mutants survived all seven tests (table below). Three of them are in owns: the last page number refused, the number compared by its low 32 bits only, and every number owned before the first grant. The fourth is a release on the never-created registry reported as success. Each test runs in its own process and grants pages before it releases any, so no test hits that path.

This PR adds an owns scenario to era_physical_check, registered as era_physical_owns in the existing foreach next to controls and lifetime:

Step Expected What it pins
fresh process, no grant yet owns(0), owns(1), owns(2^18) false; release(1, {1}) is invalid; total 0 the empty registry
grants of 4 and 3 pages every granted page owned owns reads the grant table
the lowest number not granted, the highest granted + 1, 0, 2^18 + 1, all-ones, 2^32 | lowest granted not owned range and full 64-bit compare
release(2, b) (short count) invalid, all of b still owned a refused release changes no ownership
release a whole a not owned, b still owned ownership ends for that grant only
grant 2^18 - 3 more pages b plus the new pages are exactly 1..2^18, each once; owns(1) and owns(2^18) true; all owned the full range, the last page included
one more page exhausted, count 0
release both grants total 0, no page owned

Negative control: era_physical_owns_last_page_control builds the same check against a copy of era_physical.cpp generated at configure time, whose owns uses page < capacity_pages. This follows the startup_gamesave_publish_unlocked pattern. CMake stops with FATAL_ERROR if the anchor is gone, and lists the source as a configure dependency so the copy follows edits. The test uses PASS_REGULAR_EXPRESSION "FAIL: owns: last page", so only that specific report passes it. A crash does not.

The change touches tests/era_physical_check.cpp (one new function and one scenario branch; no existing case changed), the era_physical block of CMakeLists.txt, and two new rows after era_physical_lifetime in the physical-page CTest table of docs/experiments.md. src/ is unchanged.

Mutation probe

I applied each mutant to src/era_physical.cpp or src/era_physical.h alone, rebuilt era_physical_check, xmem_shim, xmem_shim_check, xmem_commit_protection_check and xmem_shim_test_controls, and ran the seven existing tests, then reverted. The "new" column adds era_physical_owns. For that run the configure dependency was left out, because the owns mutants remove the control's anchor. None of this is committed.

Mutant Existing tests With era_physical_owns
O1 owns: page < capacity_pages survived killed
O3 owns: page number cut to 32 bits survived killed
O4 owns: true while the registry is empty survived killed
R6 release on the empty registry returns ok survived killed
A6 search always restarts at page 1 survived survived (not pinned)
O2 owns: any in-range number killed (xmem_shim_mapped_pages) killed
A1 allocate: count >= capacity refused killed killed
A2 allocate: room check >= killed killed
A3 search wraps before the last page killed killed
A4 search wraps to page 0 killed killed
A5 search one step short killed killed
A7 flags masked instead of compared killed killed
A8 flags cut to 32 bits killed killed
A9 exhausted reported as invalid killed killed
A10 count not reset on failure killed killed
R1 release: count >= capacity refused killed killed
R2 release: page >= capacity refused killed killed
R3 duplicate check dropped killed killed
R4 grant size check dropped killed killed
R5 total lowered by count - 1 killed killed
G1 granted_pages returns 0 killed killed
H1 mem_64k_pages = 0x20000000 killed killed
H2 capacity_pages = 2^17 killed killed
H3 page_bytes = 32 KiB killed killed

Not pinned

  • Search order (A6): the header says page numbers can be reused and callers must discard them after a release. It does not say which free numbers are issued, so the check does not pin that the search resumes where the last one stopped.
  • Grant ids: the wrap of the 32-bit grant id after 2^32 grants is not reachable in a test.
  • Allocation failure: the bad_alloc paths in allocate/release would need an allocation-failure harness like era_title_gpu_alloc. They are out of scope.

Verification

Commit c7ea6059 on branch tests/era-physical-check, based on bfe9158d (main). Environment: Windows 11 Pro build 26300.9974, MSVC 19.51.36260 (Visual Studio), Windows SDK 10.0.26100.0, CMake 3.30.4, dev preset (Debug). No GPU is involved.

cmake --preset dev
cmake --build --preset dev --target era_physical_check era_physical_last_page_check xmem_shim xmem_shim_check xmem_commit_protection_check xmem_shim_test_controls
ctest --preset dev --no-tests=error -R '^(era_physical_(controls|lifetime|owns|owns_last_page_control)|xmem_shim_(physical_pages|mapped_pages|page_runs)|xmem_commit_protection|xmem_page_stats)$' -V
#   9/9 passed
#   era_physical_owns: "owns: 2 grants, 262144 pages owned at full capacity", "era_physical owns: PASS"
#   era_physical_owns_last_page_control: "FAIL: owns: last page: the first or last page number is not owned when granted" (passes on the regex)

era_physical_check owns passed 20 of 20 repeated runs. tests/docs_links_check.py (17 Markdown files, 364 links), tests/bidi_controls_check.py and git diff --check passed.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6588133327 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1736). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-24T17:58:13Z. --- Refs #1149. ## Change `src/era_physical.h` is the desktop bookkeeping for title physical pages behind `XMemAllocatePhysicalPages`/`XMemFreePhysicalPages`. Pages are opaque nonzero numbers from 1 to 2^18 (16 GiB of 64 KiB pages). Allocation is all or nothing, and a release must name whole live grants. The header also exports `owns(page)`, which `XMemMapPhysicalPages`/`XMemMapPhysicalPagesScatter` (`src/xmem_mapped_pages.cpp`) call for every page token before they map it. Device-free tests that reach the file today: - `era_physical_controls` and `era_physical_lifetime` (`tests/era_physical_check.cpp`): seven release controls against a lenient build, plus exact totals, exhaustion and argument refusals; - `xmem_shim_physical_pages`: the same policy through the `xmem.dll` exports, including a full 2^18 grant; - `xmem_shim_mapped_pages`, `xmem_shim_page_runs`, `xmem_commit_protection`, `xmem_page_stats`: mapping through `xmem.dll`. `era_physical_check` never calls `owns`. The mapping tests only show that released page tokens are no longer accepted. 5 of 24 single-point mutants survived all seven tests (table below). Three of them are in `owns`: the last page number refused, the number compared by its low 32 bits only, and every number owned before the first grant. The fourth is a release on the never-created registry reported as success. Each test runs in its own process and grants pages before it releases any, so no test hits that path. This PR adds an `owns` scenario to `era_physical_check`, registered as `era_physical_owns` in the existing `foreach` next to `controls` and `lifetime`: | Step | Expected | What it pins | |---|---|---| | fresh process, no grant yet | `owns(0)`, `owns(1)`, `owns(2^18)` false; `release(1, {1})` is `invalid`; total 0 | the empty registry | | grants of 4 and 3 pages | every granted page owned | `owns` reads the grant table | | the lowest number not granted, the highest granted + 1, 0, 2^18 + 1, all-ones, `2^32 \| lowest granted` | not owned | range and full 64-bit compare | | `release(2, b)` (short count) | `invalid`, all of `b` still owned | a refused release changes no ownership | | release `a` whole | `a` not owned, `b` still owned | ownership ends for that grant only | | grant 2^18 - 3 more pages | `b` plus the new pages are exactly 1..2^18, each once; `owns(1)` and `owns(2^18)` true; all owned | the full range, the last page included | | one more page | `exhausted`, count 0 | | | release both grants | total 0, no page owned | | Negative control: `era_physical_owns_last_page_control` builds the same check against a copy of `era_physical.cpp` generated at configure time, whose `owns` uses `page < capacity_pages`. This follows the `startup_gamesave_publish_unlocked` pattern. CMake stops with `FATAL_ERROR` if the anchor is gone, and lists the source as a configure dependency so the copy follows edits. The test uses `PASS_REGULAR_EXPRESSION "FAIL: owns: last page"`, so only that specific report passes it. A crash does not. The change touches `tests/era_physical_check.cpp` (one new function and one scenario branch; no existing case changed), the `era_physical` block of `CMakeLists.txt`, and two new rows after `era_physical_lifetime` in the physical-page CTest table of `docs/experiments.md`. `src/` is unchanged. ## Mutation probe I applied each mutant to `src/era_physical.cpp` or `src/era_physical.h` alone, rebuilt `era_physical_check`, `xmem_shim`, `xmem_shim_check`, `xmem_commit_protection_check` and `xmem_shim_test_controls`, and ran the seven existing tests, then reverted. The "new" column adds `era_physical_owns`. For that run the configure dependency was left out, because the `owns` mutants remove the control's anchor. None of this is committed. | Mutant | Existing tests | With `era_physical_owns` | |---|---|---| | O1 `owns`: `page < capacity_pages` | **survived** | killed | | O3 `owns`: page number cut to 32 bits | **survived** | killed | | O4 `owns`: true while the registry is empty | **survived** | killed | | R6 release on the empty registry returns `ok` | **survived** | killed | | A6 search always restarts at page 1 | **survived** | **survived** (not pinned) | | O2 `owns`: any in-range number | killed (`xmem_shim_mapped_pages`) | killed | | A1 allocate: count `>= capacity` refused | killed | killed | | A2 allocate: room check `>=` | killed | killed | | A3 search wraps before the last page | killed | killed | | A4 search wraps to page 0 | killed | killed | | A5 search one step short | killed | killed | | A7 flags masked instead of compared | killed | killed | | A8 flags cut to 32 bits | killed | killed | | A9 `exhausted` reported as `invalid` | killed | killed | | A10 count not reset on failure | killed | killed | | R1 release: count `>= capacity` refused | killed | killed | | R2 release: page `>= capacity` refused | killed | killed | | R3 duplicate check dropped | killed | killed | | R4 grant size check dropped | killed | killed | | R5 total lowered by `count - 1` | killed | killed | | G1 `granted_pages` returns 0 | killed | killed | | H1 `mem_64k_pages` = `0x20000000` | killed | killed | | H2 `capacity_pages` = 2^17 | killed | killed | | H3 `page_bytes` = 32 KiB | killed | killed | ## Not pinned - **Search order (A6):** the header says page numbers can be reused and callers must discard them after a release. It does not say which free numbers are issued, so the check does not pin that the search resumes where the last one stopped. - **Grant ids:** the wrap of the 32-bit grant id after 2^32 grants is not reachable in a test. - **Allocation failure:** the `bad_alloc` paths in `allocate`/`release` would need an allocation-failure harness like `era_title_gpu_alloc`. They are out of scope. ## Verification Commit `c7ea6059` on branch `tests/era-physical-check`, based on `bfe9158d` (`main`). Environment: Windows 11 Pro build 26300.9974, MSVC 19.51.36260 (Visual Studio), Windows SDK 10.0.26100.0, CMake 3.30.4, `dev` preset (Debug). No GPU is involved. ```powershell cmake --preset dev cmake --build --preset dev --target era_physical_check era_physical_last_page_check xmem_shim xmem_shim_check xmem_commit_protection_check xmem_shim_test_controls ctest --preset dev --no-tests=error -R '^(era_physical_(controls|lifetime|owns|owns_last_page_control)|xmem_shim_(physical_pages|mapped_pages|page_runs)|xmem_commit_protection|xmem_page_stats)$' -V # 9/9 passed # era_physical_owns: "owns: 2 grants, 262144 pages owned at full capacity", "era_physical owns: PASS" # era_physical_owns_last_page_control: "FAIL: owns: last page: the first or last page number is not owned when granted" (passes on the regex) ``` `era_physical_check owns` passed 20 of 20 repeated runs. `tests/docs_links_check.py` (17 Markdown files, 364 links), `tests/bidi_controls_check.py` and `git diff --check` passed. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted (Claude Code).
tedin7 commented 2026-11-24 19:01:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T18:14:36Z.

Review: COMMENTED


Peer review of PR #1736 at head c7ea6059227ff6039849f79d0168f43aa70b4002.

Summary

  • Scope & hygiene: Changes are confined to CMakeLists.txt, docs/experiments.md, and tests/era_physical_check.cpp. No src/ files were modified. Title-neutral, MIT clean-room.
  • Test quality: era_physical_owns exercises era_physical::owns() against the live registry from empty state (!owns(0), !owns(1), !owns(capacity_pages)), through two grants (4 and 3 pages, rejecting unissued pages and out-of-range numbers including 0, capacity_pages + 1, ~0, and (1ULL << 32) | live.front()), refused and valid releases, full-capacity saturation (1..capacity_pages all owned, exhaustion returns Status::exhausted), and cleanup (all released pages unowned).
  • Negative control: era_physical_owns_last_page_control mutates page <= capacity_pages to page < capacity_pages in a build-tree copy of src/era_physical.cpp (guarded by CMAKE_CONFIGURE_DEPENDS and FATAL_ERROR) and requires PASS_REGULAR_EXPRESSION "FAIL: owns: last page".
  • Findings: No P1 or P2 issues found.

Checks executed

  • Linux (prep-verify 1736):
    • python3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py prep-verify 1736 --dir /tmp/rev-x2w-84/1736 --json -> git_diff_check: PASS, docs_links_check: PASS, bidi_controls_check: PASS, py_compile: PASS.
  • Win2025 Debug & Release /O2 (win2025 1736, Build 26100, MSVC 14.44.35207):
    • python3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py win2025 1736 --wait 600 --targets era_physical_check,era_physical_last_page_check --regex "^(era_physical_controls|era_physical_lifetime|era_physical_owns|era_physical_owns_last_page_control)$"
    • Debug (--preset dev): 4/4 passed (era_physical_controls 0.02s, era_physical_lifetime 0.21s, era_physical_owns 0.28s, era_physical_owns_last_page_control 0.28s).
    • Release /O2 (--preset release): 4/4 passed (era_physical_controls 0.02s, era_physical_lifetime 0.08s, era_physical_owns 0.09s, era_physical_owns_last_page_control 0.09s).
  • Mutation check on src/era_physical.cpp (Win2025 Debug):
    • python3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py win2025 1736 --targets era_physical_check --regex "^era_physical_owns$" --skip-release --patch /tmp/rev-x2w-84/mut_1736.patch (dropping && r.grant[page] != 0 in era_physical::owns) -> Mutant killed: era_physical_owns failed with FAIL: owns: a page that was never issued is owned, FAIL: owns: a release did not end ownership of exactly its grant, and FAIL: owns: a released page is still owned.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6044852822 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1736#pullrequestreview-6044852822). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T18:14:36Z. **Review:** COMMENTED --- Peer review of PR #1736 at head `c7ea6059227ff6039849f79d0168f43aa70b4002`. ### Summary - **Scope & hygiene**: Changes are confined to `CMakeLists.txt`, `docs/experiments.md`, and `tests/era_physical_check.cpp`. No `src/` files were modified. Title-neutral, MIT clean-room. - **Test quality**: `era_physical_owns` exercises `era_physical::owns()` against the live registry from empty state (`!owns(0)`, `!owns(1)`, `!owns(capacity_pages)`), through two grants (`4` and `3` pages, rejecting unissued pages and out-of-range numbers including `0`, `capacity_pages + 1`, `~0`, and `(1ULL << 32) | live.front()`), refused and valid releases, full-capacity saturation (`1..capacity_pages` all owned, exhaustion returns `Status::exhausted`), and cleanup (all released pages unowned). - **Negative control**: `era_physical_owns_last_page_control` mutates `page <= capacity_pages` to `page < capacity_pages` in a build-tree copy of `src/era_physical.cpp` (guarded by `CMAKE_CONFIGURE_DEPENDS` and `FATAL_ERROR`) and requires `PASS_REGULAR_EXPRESSION "FAIL: owns: last page"`. - **Findings**: No P1 or P2 issues found. ### Checks executed - **Linux (`prep-verify 1736`)**: - `python3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py prep-verify 1736 --dir /tmp/rev-x2w-84/1736 --json` -> `git_diff_check`: PASS, `docs_links_check`: PASS, `bidi_controls_check`: PASS, `py_compile`: PASS. - **Win2025 Debug & Release `/O2` (`win2025 1736`, Build 26100, MSVC 14.44.35207)**: - `python3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py win2025 1736 --wait 600 --targets era_physical_check,era_physical_last_page_check --regex "^(era_physical_controls|era_physical_lifetime|era_physical_owns|era_physical_owns_last_page_control)$"` - Debug (`--preset dev`): **4/4 passed** (`era_physical_controls` `0.02s`, `era_physical_lifetime` `0.21s`, `era_physical_owns` `0.28s`, `era_physical_owns_last_page_control` `0.28s`). - Release `/O2` (`--preset release`): **4/4 passed** (`era_physical_controls` `0.02s`, `era_physical_lifetime` `0.08s`, `era_physical_owns` `0.09s`, `era_physical_owns_last_page_control` `0.09s`). - **Mutation check on `src/era_physical.cpp` (Win2025 Debug)**: - `python3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py win2025 1736 --targets era_physical_check --regex "^era_physical_owns$" --skip-release --patch /tmp/rev-x2w-84/mut_1736.patch` (dropping `&& r.grant[page] != 0` in `era_physical::owns`) -> **Mutant killed**: `era_physical_owns` failed with `FAIL: owns: a page that was never issued is owned`, `FAIL: owns: a release did not end ownership of exactly its grant`, and `FAIL: owns: a released page is still owned`. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-24 21:00:46 +00:00
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!1729
No description provided.