startup: check the placement publish lock and width #1496

Merged
Asimmimx merged 1 commit from upstream-pr/1503 into main 2026-11-24 13:30:38 +00:00
Asimmimx commented 2026-11-24 13:13:41 +00:00 (Migrated from github.com)

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


Change

Refs #1149. This adds a device-free check for src/startup_stubs/placement_publish.h. Device slot 133 (d3d12_xs_device.cpp) uses it to register a placed buffer.

The binding lookup placed_buffer_range in d3d12_capture.inl takes note_lock shared and starts its search at address - max_width + 1. It therefore relies on two things:

  • publish_placement inserts the record and its index entry under the exclusive lock;
  • max_width never shrinks below the widest record published so far.

The tests that reach the header are startup_placement_alloc (CPU), startup_placed_vertex_warp and xmem_page_runs_device_warp. They check the inserted records, the ALREADY_EXISTS and out-of-memory paths and the rollback, but neither of those two properties. Three single-point mutants left all three passing:

  • dropping the lock guard;
  • taking the lock shared instead of exclusive;
  • max_width = record.bytes, which lets a later, narrower record shrink the search window. Lookups inside a wider, earlier buffer would then start too late and miss it.

startup_placement_publish includes only the header:

  • It publishes four records of 1 MiB, 128 KiB, 4 MiB and 256 KiB.
  • From inside the records accessor, and from the record's copy constructor (which runs under the lock), it checks that TryAcquireSRWLockShared fails. SRW locks are not reentrant, so this means the lock is held exclusively. It also checks that the lock is free again after the call.
  • It checks max_width after each record against hand-worked values: 0x100000, 0x100000, 0x400000, 0x400000.
  • It checks each record and its index entry.

startup_placement_publish_shrink_control runs --mutate shrink, which expects 0x20000 after the second record, and must fail (WILL_FAIL).

The change is test, CMake and docs/experiments.md only. The paragraph sits next to the existing startup_placement_alloc text, and the CMake block follows the startup_placement_alloc registration.

Verification

Commit 3c6273ac (main) plus this change, dev preset (Debug), Windows 11 Pro build 26300.9974, MSVC 19.51.36260, Windows SDK 10.0.26100.0, WARP 10.0.26100.9818.

ctest --preset dev --no-tests=error -R '^(startup_placement_(alloc|publish|publish_shrink_control)|startup_placed_vertex_warp)$' -V
#   4/4 passed; the control exits 5 with "after owner 2: max_width 0x100000, expected 0x20000"

xmem_page_runs_device_warp also passed on the unmodified header. tests/docs_links_check.py, tests/bidi_controls_check.py and git diff --check passed.

Mutation probe

I applied each mutation to the header alone, rebuilt the affected targets, ran the tests, then reverted. None of this is committed.

Mutant alloc / placed_vertex_warp / xmem_device_warp startup_placement_publish
control: if (!added) → if (added) killed / killed / killed fails
lock guard dropped survived fails
guard takes shared instead of exclusive survived fails
max_width = record.bytes survived fails
insert_or_assign (duplicate replaces the record) survived survives, not pinned
rollback records.erase dropped killed by alloc survives (already covered)
throw; → return S_OK; killed by alloc survives (already covered)
record.bytes - 1 killed by alloc fails
index key start + 1 killed by alloc and placed_vertex fails
length_error catch → domain_error survived survives, not pinned

Two survivors are deliberately not pinned:

  • insert_or_assign. A duplicate owner cannot come from the real caller: each owner is a freshly allocated wrapper that lives for the whole process. The ALREADY_EXISTS return is already checked.
  • domain_error catch. A std::unordered_map throws length_error only above max_size, which is not practically reachable.

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:6582594961 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1503). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-24T13:13:41Z. --- ## Change Refs #1149. This adds a device-free check for `src/startup_stubs/placement_publish.h`. Device slot 133 (`d3d12_xs_device.cpp`) uses it to register a placed buffer. The binding lookup `placed_buffer_range` in `d3d12_capture.inl` takes `note_lock` shared and starts its search at `address - max_width + 1`. It therefore relies on two things: - `publish_placement` inserts the record and its index entry under the exclusive lock; - `max_width` never shrinks below the widest record published so far. The tests that reach the header are `startup_placement_alloc` (CPU), `startup_placed_vertex_warp` and `xmem_page_runs_device_warp`. They check the inserted records, the `ALREADY_EXISTS` and out-of-memory paths and the rollback, but neither of those two properties. Three single-point mutants left all three passing: - dropping the lock guard; - taking the lock shared instead of exclusive; - `max_width = record.bytes`, which lets a later, narrower record shrink the search window. Lookups inside a wider, earlier buffer would then start too late and miss it. `startup_placement_publish` includes only the header: - It publishes four records of 1 MiB, 128 KiB, 4 MiB and 256 KiB. - From inside the records accessor, and from the record's copy constructor (which runs under the lock), it checks that `TryAcquireSRWLockShared` fails. SRW locks are not reentrant, so this means the lock is held exclusively. It also checks that the lock is free again after the call. - It checks `max_width` after each record against hand-worked values: 0x100000, 0x100000, 0x400000, 0x400000. - It checks each record and its index entry. `startup_placement_publish_shrink_control` runs `--mutate shrink`, which expects 0x20000 after the second record, and must fail (`WILL_FAIL`). The change is test, CMake and `docs/experiments.md` only. The paragraph sits next to the existing `startup_placement_alloc` text, and the CMake block follows the `startup_placement_alloc` registration. ## Verification Commit `3c6273ac` (`main`) plus this change, `dev` preset (Debug), Windows 11 Pro build 26300.9974, MSVC 19.51.36260, Windows SDK 10.0.26100.0, WARP 10.0.26100.9818. ```powershell ctest --preset dev --no-tests=error -R '^(startup_placement_(alloc|publish|publish_shrink_control)|startup_placed_vertex_warp)$' -V # 4/4 passed; the control exits 5 with "after owner 2: max_width 0x100000, expected 0x20000" ``` `xmem_page_runs_device_warp` also passed on the unmodified header. `tests/docs_links_check.py`, `tests/bidi_controls_check.py` and `git diff --check` passed. ## Mutation probe I applied each mutation to the header alone, rebuilt the affected targets, ran the tests, then reverted. None of this is committed. | Mutant | alloc / placed_vertex_warp / xmem_device_warp | `startup_placement_publish` | |---|---|---| | control: `if (!added)` → `if (added)` | killed / killed / killed | fails | | lock guard dropped | **survived** | fails | | guard takes shared instead of exclusive | **survived** | fails | | `max_width = record.bytes` | **survived** | fails | | `insert_or_assign` (duplicate replaces the record) | survived | survives, not pinned | | rollback `records.erase` dropped | killed by alloc | survives (already covered) | | `throw;` → `return S_OK;` | killed by alloc | survives (already covered) | | `record.bytes - 1` | killed by alloc | fails | | index key `start + 1` | killed by alloc and placed_vertex | fails | | `length_error` catch → `domain_error` | survived | survives, not pinned | Two survivors are deliberately not pinned: - **`insert_or_assign`.** A duplicate owner cannot come from the real caller: each owner is a freshly allocated wrapper that lives for the whole process. The `ALREADY_EXISTS` return is already checked. - **`domain_error` catch.** A `std::unordered_map` throws `length_error` only above `max_size`, which is not practically reachable. ## 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).
upstream-sync closed this pull request 2026-11-24 14:02:59 +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!1496
No description provided.