runtime: always commit fresh slot in section_for() #3366

Closed
github-bot wants to merge 0 commits from github-bot/gh-3364 into main AGit
Owner

@⁠KelboLive on GitHub · 2026-11-28 14:04 UTC · original
KelboLive:fix/xmem-fresh-slot-clean-2 → main · +1 −1 · 1 files · 1 commits · closed without merging

In \section_for(), when creating a new slab for a fresh page, \make_slab\ commits offset 0 of the slab. The original code only committed the target slot (at \slot.offset) when !first_slot_committed\ was true — which never happens because \make_slab\ returns false if committing offset 0 fails.

This left fresh slots at non-zero offsets uncommitted in non-lazy mode, causing access faults on first use. Remove the !first_slot_committed\ guard so the target slot is always committed before recording it.

Fixes: #3357

**@⁠KelboLive** on GitHub · 2026-11-28 14:04 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3364) `KelboLive:fix/xmem-fresh-slot-clean-2` → `main` · +1 −1 · 1 files · 1 commits · closed without merging In \section_for()\, when creating a new slab for a fresh page, \make_slab\ commits offset 0 of the slab. The original code only committed the target slot (at \slot.offset\) when \!first_slot_committed\ was true — which never happens because \make_slab\ returns false if committing offset 0 fails. This left fresh slots at non-zero offsets uncommitted in non-lazy mode, causing access faults on first use. Remove the \!first_slot_committed\ guard so the target slot is always committed before recording it. Fixes: #3357
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 14:26:21 +00:00
Author
Owner

@⁠R0D4X on GitHub · 2026-11-28 14:13 UTC · original · reviewed

I checked PR 3364 at commit 97fcd1eb27b8f17670990f174e56b4308673e63f on Windows 11 Pro 26H2 (build 10.0.26300.0), MSVC 19.44.35230.0. nvidia-smi reported NVIDIA GeForce RTX 5080, 621.40; D3D WARP/hardware lines reported Microsoft Basic Render Driver, debug layer enabled, driver 10.0.26100.9818, and NVIDIA GeForce RTX 5080, debug layer enabled, driver 32.0.16.2140.

Logic: first_slot_committed is false for fresh slots taken from an existing slab and true only when make_slab commits the first slot, so the old guard did not suppress the commit for fresh slots from existing slabs. The unconditional commit_slot(..., false) is harmless and is covered by the commit, lazy/off, and page-run checks below.

Environment setup and configure:

python -m venv out/pe-env; .\out\pe-env\Scripts\python.exe -m pip install -r requirements-dev.txt
. .\tools\dev.ps1; cmake --preset dev
. .\tools\dev.ps1; cmake --preset release

Build:

. .\tools\dev.ps1; cmake --build --preset dev --target xmem_shim xmem_shim_test_controls xmem_section_lease_check xmem_lazy_imports_check
. .\tools\dev.ps1; cmake --build --preset release --target xmem_shim xmem_shim_test_controls xmem_section_lease_check xmem_lazy_imports_check

Selected tests:

. .\tools\dev.ps1; $env:XBOX2WINDOWS_ADAPTER = 'RTX 5080'; ctest --test-dir out/build/dev --no-tests=error -j 1 -V -R '^(xmem_mapped_pages_commit(_failure)?|xmem_lazy_commit(_off)?|xmem_lazy_imports|xmem_lazy_handler_failure|xmem_section_lease_(single_page|warp|hardware)|xmem_page_batch_inflight_(warp|hardware)|xmem_shim_(mapped_pages|page_runs))$'
. .\tools\dev.ps1; $env:XBOX2WINDOWS_ADAPTER = 'RTX 5080'; ctest --test-dir out/build/release --no-tests=error -j 1 -V -R '^(xmem_mapped_pages_commit(_failure)?|xmem_lazy_commit(_off)?|xmem_lazy_imports|xmem_lazy_handler_failure|xmem_section_lease_(single_page|warp|hardware)|xmem_page_batch_inflight_(warp|hardware)|xmem_shim_(mapped_pages|page_runs))$'

Result: 13/13 passed in dev, 13/13 passed in release, no skips. Hardware lines reported NVIDIA GeForce RTX 5080, debug layer enabled with driver 32.0.16.2140; WARP lines reported debug layer enabled with driver 10.0.26100.9818.

Targeted mutations (all reverted, then clean rebuild and rerun above):

  • if (!defer && !commit_slot(slot.slab, slot.offset, false)) { -> if (!defer && false && !commit_slot(slot.slab, slot.offset, false)) {
. .\tools\dev.ps1; cmake --build --preset dev --target xmem_shim; ctest --test-dir out/build/dev --no-tests=error -j 1 -V -R '^xmem_mapped_pages_commit$'; git checkout -- .

Result: 1/1 Test #49: xmem_mapped_pages_commit .........***Exception: SegFault 0.11 sec, 0% tests passed, 1 tests failed out of 1, 49 - xmem_mapped_pages_commit (SEGFAULT).

  • const bool defer = !need_commit && lazy_commit(); -> const bool defer = !need_commit;
. .\tools\dev.ps1; cmake --build --preset dev --target xmem_shim; ctest --test-dir out/build/dev --no-tests=error -j 1 -V -R '^xmem_lazy_commit_off$'; git checkout -- .

Result: AssertionError: off: a page was left lazy, 0% tests passed, 1 tests failed out of 1.

  • Probe if (!commit_slot(slot.slab, slot.offset, true)) return nullptr; -> if (!commit_slot(slot.slab, slot.offset, false)) return nullptr;
. .\tools\dev.ps1; cmake --build --preset dev --target xmem_shim; ctest --test-dir out/build/dev --no-tests=error -j 1 -V -R '^xmem_shim_page_runs$'; git checkout -- .

Result: 100% tests passed, 0 tests failed out of 1; this probe did not change the selected outcome and was reverted.

No blocking defect found in the change itself.
AI-assisted; commands listed were executed locally.

**@⁠R0D4X** on GitHub · 2026-11-28 14:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3364#pullrequestreview-6098315625) · **reviewed** I checked PR 3364 at commit `97fcd1eb27b8f17670990f174e56b4308673e63f` on Windows 11 Pro 26H2 (build `10.0.26300.0`), MSVC `19.44.35230.0`. `nvidia-smi` reported `NVIDIA GeForce RTX 5080, 621.40`; D3D WARP/hardware lines reported `Microsoft Basic Render Driver, debug layer enabled`, driver `10.0.26100.9818`, and `NVIDIA GeForce RTX 5080, debug layer enabled`, driver `32.0.16.2140`. Logic: `first_slot_committed` is false for fresh slots taken from an existing slab and true only when `make_slab` commits the first slot, so the old guard did not suppress the commit for fresh slots from existing slabs. The unconditional `commit_slot(..., false)` is harmless and is covered by the commit, lazy/off, and page-run checks below. Environment setup and configure: ```powershell python -m venv out/pe-env; .\out\pe-env\Scripts\python.exe -m pip install -r requirements-dev.txt . .\tools\dev.ps1; cmake --preset dev . .\tools\dev.ps1; cmake --preset release ``` Build: ```powershell . .\tools\dev.ps1; cmake --build --preset dev --target xmem_shim xmem_shim_test_controls xmem_section_lease_check xmem_lazy_imports_check . .\tools\dev.ps1; cmake --build --preset release --target xmem_shim xmem_shim_test_controls xmem_section_lease_check xmem_lazy_imports_check ``` Selected tests: ```powershell . .\tools\dev.ps1; $env:XBOX2WINDOWS_ADAPTER = 'RTX 5080'; ctest --test-dir out/build/dev --no-tests=error -j 1 -V -R '^(xmem_mapped_pages_commit(_failure)?|xmem_lazy_commit(_off)?|xmem_lazy_imports|xmem_lazy_handler_failure|xmem_section_lease_(single_page|warp|hardware)|xmem_page_batch_inflight_(warp|hardware)|xmem_shim_(mapped_pages|page_runs))$' . .\tools\dev.ps1; $env:XBOX2WINDOWS_ADAPTER = 'RTX 5080'; ctest --test-dir out/build/release --no-tests=error -j 1 -V -R '^(xmem_mapped_pages_commit(_failure)?|xmem_lazy_commit(_off)?|xmem_lazy_imports|xmem_lazy_handler_failure|xmem_section_lease_(single_page|warp|hardware)|xmem_page_batch_inflight_(warp|hardware)|xmem_shim_(mapped_pages|page_runs))$' ``` Result: 13/13 passed in dev, 13/13 passed in release, no skips. Hardware lines reported `NVIDIA GeForce RTX 5080, debug layer enabled` with driver `32.0.16.2140`; WARP lines reported debug layer enabled with driver `10.0.26100.9818`. Targeted mutations (all reverted, then clean rebuild and rerun above): - `if (!defer && !commit_slot(slot.slab, slot.offset, false)) {` -> `if (!defer && false && !commit_slot(slot.slab, slot.offset, false)) {` ```powershell . .\tools\dev.ps1; cmake --build --preset dev --target xmem_shim; ctest --test-dir out/build/dev --no-tests=error -j 1 -V -R '^xmem_mapped_pages_commit$'; git checkout -- . ``` Result: `1/1 Test #49: xmem_mapped_pages_commit .........***Exception: SegFault 0.11 sec`, `0% tests passed, 1 tests failed out of 1`, `49 - xmem_mapped_pages_commit (SEGFAULT)`. - `const bool defer = !need_commit && lazy_commit();` -> `const bool defer = !need_commit;` ```powershell . .\tools\dev.ps1; cmake --build --preset dev --target xmem_shim; ctest --test-dir out/build/dev --no-tests=error -j 1 -V -R '^xmem_lazy_commit_off$'; git checkout -- . ``` Result: `AssertionError: off: a page was left lazy`, `0% tests passed, 1 tests failed out of 1`. - Probe `if (!commit_slot(slot.slab, slot.offset, true)) return nullptr;` -> `if (!commit_slot(slot.slab, slot.offset, false)) return nullptr;` ```powershell . .\tools\dev.ps1; cmake --build --preset dev --target xmem_shim; ctest --test-dir out/build/dev --no-tests=error -j 1 -V -R '^xmem_shim_page_runs$'; git checkout -- . ``` Result: `100% tests passed, 0 tests failed out of 1`; this probe did not change the selected outcome and was reverted. No blocking defect found in the change itself. AI-assisted; commands listed were executed locally.
Author
Owner

@⁠Leyukaka on GitHub · 2026-11-29 00:10 UTC · original

Reviewed 97fcd1eb by its diff and src/xmem_mapped_pages.cpp / src/xmem_slab_slots.h on main 51472392. The guard removed here does not skip commits of non-zero fresh slots:

  • first_slot_committed is set only in make_slab, and take_fresh calls make_slab only when it opens a new slab, then hands out offset 0, which make_slab has just committed. For a fresh slot taken from an existing slab the flag stays false, and main's line 359 already commits slot.offset.
  • With the guard removed, offset 0 of each new slab (in non-lazy mode) is mapped and committed a second time. If that second commit_slot fails, the slot is unwound and section_for returns nullptr where main returns the slab.
  • The PR adds no test. The && false mutation in the review above disables that line for every fresh slot, including the non-zero ones main already commits there, so it does not exercise the guard; restoring the guard gives main's code.

If the body describes an observed failure, a repro or a fixture that fails on main would settle it. Otherwise a comment at line 359 saying the flag implies slot.offset == 0 would make that explicit. Not mergeable as is at 97fcd1eb.

**@⁠Leyukaka** on GitHub · 2026-11-29 00:10 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3364#issuecomment-7247929755) Reviewed `97fcd1eb` by its diff and `src/xmem_mapped_pages.cpp` / `src/xmem_slab_slots.h` on main `51472392`. The guard removed here does not skip commits of non-zero fresh slots: - `first_slot_committed` is set only in `make_slab`, and `take_fresh` calls `make_slab` only when it opens a new slab, then hands out offset 0, which `make_slab` has just committed. For a fresh slot taken from an existing slab the flag stays false, and main's line 359 already commits `slot.offset`. - With the guard removed, offset 0 of each new slab (in non-lazy mode) is mapped and committed a second time. If that second `commit_slot` fails, the slot is unwound and `section_for` returns `nullptr` where main returns the slab. - The PR adds no test. The `&& false` mutation in the review above disables that line for every fresh slot, including the non-zero ones main already commits there, so it does not exercise the guard; restoring the guard gives main's code. If the body describes an observed failure, a repro or a fixture that fails on main would settle it. Otherwise a comment at line 359 saying the flag implies `slot.offset == 0` would make that explicit. Not mergeable as is at `97fcd1eb`.
Author
Owner

@⁠JohnConnorNPC on GitHub · 2026-11-29 12:52 UTC · original

Re-reviewed at 97fcd1eb27 against main 77d1ba4d (by reading src/xmem_mapped_pages.cpp and src/xmem_slab_slots.h). git merge-tree exits 0. CI: 3 success, 2 skipped. I agree with the analysis above. On main, first_slot_committed is true only when take_fresh has just opened a slab and handed out offset 0, which make_slab has already committed. So no non-zero fresh slot is left uncommitted, and this change adds a second MapViewOfFile/VirtualAlloc/UnmapViewOfFile of offset 0 for each new slab. Points not raised yet:

MUST

  1. This runs on the default path. The second commit runs whenever defer is false (lazy commit not requested, or its handler not installed). If that second commit fails, unwind_fresh runs and section_for returns nullptr, where main returns the slab it has already committed. So the change adds a failure path and fixes no observed fault. Without a repro or a fixture that fails on main, it shouldn't merge. If the goal is clarity, a comment at that line stating "flag ⇒ slot.offset == 0, already committed" keeps main's behaviour.

SHOULD
2. The same commit is also in #3368. Whatever happens here, it should come out of #3368 so the two don't land twice or diverge.

Holding, not merging.

AI-assisted.

**@⁠JohnConnorNPC** on GitHub · 2026-11-29 12:52 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3364#issuecomment-7256130077) Re-reviewed at 97fcd1eb27b8f17670990f174e56b4308673e63f against main 77d1ba4d (by reading `src/xmem_mapped_pages.cpp` and `src/xmem_slab_slots.h`). `git merge-tree` exits 0. CI: 3 success, 2 skipped. I agree with the analysis above. On main, `first_slot_committed` is true only when `take_fresh` has just opened a slab and handed out offset 0, which `make_slab` has already committed. So no non-zero fresh slot is left uncommitted, and this change adds a second `MapViewOfFile`/`VirtualAlloc`/`UnmapViewOfFile` of offset 0 for each new slab. Points not raised yet: **MUST** 1. This runs on the default path. The second commit runs whenever `defer` is false (lazy commit not requested, or its handler not installed). If that second commit fails, `unwind_fresh` runs and `section_for` returns `nullptr`, where main returns the slab it has already committed. So the change adds a failure path and fixes no observed fault. Without a repro or a fixture that fails on main, it shouldn't merge. If the goal is clarity, a comment at that line stating "flag ⇒ `slot.offset == 0`, already committed" keeps main's behaviour. **SHOULD** 2. The same commit is also in #3368. Whatever happens here, it should come out of #3368 so the two don't land twice or diverge. Holding, not merging. AI-assisted.
github-bot closed this pull request 2026-11-29 16:22:42 +00:00
Author
Owner

@⁠KelboLive on GitHub · 2026-11-29 16:20 UTC · original

Closing: the change is not correct, so it should not go in as written.

section_for() creates a new slab through make_slab, which commits offset 0:

first_slot_committed = commit_slot(slab, 0, false);
...
return first_slot_committed;

take_fresh() (src/xmem_slab_slots.h) calls make() only when it opens a slab (slabs_.empty() || fresh_used_ == slots_per_slab), and then resets fresh_used_ = 0, so the slot it returns is at fresh_used_ * slot_bytes_ — offset 0 of the slab just committed. On that path first_slot_committed is true and the later commit is correctly skipped.

A slot at a non-zero offset always comes from an existing slab. There make() does not run, first_slot_committed stays false, and main already commits slot.offset.

So !first_slot_committed is not dead state — it is the guard that stops a second commit_slot of offset 0 for every new slab. Removing it leaves the default xmem path doing one extra map plus commit per new slab and fixes nothing. The PR description reads the condition the other way round.

No fixture showed a slot left uncommitted, which is what this would have needed. If one exists, a test that faults on main and passes here would be the way to reopen this.

**@⁠KelboLive** on GitHub · 2026-11-29 16:20 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3364#issuecomment-7258811490) Closing: the change is not correct, so it should not go in as written. `section_for()` creates a new slab through `make_slab`, which commits offset 0: ```cpp first_slot_committed = commit_slot(slab, 0, false); ... return first_slot_committed; ``` `take_fresh()` (`src/xmem_slab_slots.h`) calls `make()` only when it opens a slab (`slabs_.empty() || fresh_used_ == slots_per_slab`), and then resets `fresh_used_ = 0`, so the slot it returns is at `fresh_used_ * slot_bytes_` — offset 0 of the slab just committed. On that path `first_slot_committed` is true and the later commit is correctly skipped. A slot at a non-zero offset always comes from an existing slab. There `make()` does not run, `first_slot_committed` stays false, and `main` already commits `slot.offset`. So `!first_slot_committed` is not dead state — it is the guard that stops a second `commit_slot` of offset 0 for every new slab. Removing it leaves the default xmem path doing one extra map plus commit per new slab and fixes nothing. The PR description reads the condition the other way round. No fixture showed a slot left uncommitted, which is what this would have needed. If one exists, a test that faults on `main` and passes here would be the way to reopen this.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.