runtime: check the PnP key length against the buffer capacity #1531

Merged
Asimmimx merged 1 commit from upstream-pr/1538 into main 2026-11-24 14:15:22 +00:00
Asimmimx commented 2026-11-24 13:45:56 +00:00 (Migrated from github.com)

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


Change

adapter_identity::detail::key_length() (src/adapter_identity.{h,cpp}) accepts a PnP key only if it is a nonempty, terminated string that lies inside the buffer. It refuses a reported length past the capacity (written > capacity), and it refuses an embedded terminator (wcsnlen). xmem_gpu_usage relies on this when it matches adapters.

The existing key_past_buffer case puts a second terminator inside the capacity, so the embedded-terminator check also rejects it. The capacity bound is never the only thing under test. Two mutants of that bound left xmem_shim_working_set, _aggregation and _warp passing:

  • dropping written > capacity;
  • written > capacity + 1.

This adds one hand-worked row to key_checks(): key_terminator_past_buffer, a well-formed 32-character key in a 32-character buffer with a reported length of 33. Without the bound, key_length returns 32; with it, the expected result is 0.

Only tests/xmem_shim_check.cpp and one sentence in the xmem_shim_working_set_aggregation row of docs/experiments.md change. There is no new CTest.

Verification

Commit 309cfede (main) plus this change, dev preset (Debug), Windows 11 Pro build 26300.9974, MSVC 19.51.36260, Windows SDK 10.0.26100.0.

ctest --preset dev --no-tests=error -R '^xmem_shim_working_set(_aggregation|_warp)?$' -V
#   3/3 passed; the new row prints key_terminator_past_buffer length=0 ok

tests/docs_links_check.py, tests/bidi_controls_check.py and git diff --check passed.

Mutation probe

I applied each mutation to src/adapter_identity.{h,cpp} alone, rebuilt xmem_shim_check and xmem_shim, ran the three tests, then reverted. None of this is committed.

Mutant Existing With the new row
control: same_device case-sensitive killed killed
drop written > capacity survived killed (key_terminator_past_buffer length=32 MISMATCH)
written > capacity + 1 survived killed
terminator counted in the length killed killed
prefix counts as the same device survived survived (not pinned)
== CSTR_EQUAL → != CSTR_LESS_THAN killed killed
drop the wcsnlen check killed killed
query(): partial device list kept survived survived (needs a device)
query(): software key requested survived survived (needs a device)
query(): length passed as capacity survived survived (needs a device)

Not covered on purpose:

  • Prefix mutant. Real hardware keys are full registry paths ending in \Device Parameters, so one key cannot be a proper prefix of another.
  • query() mutants. query() calls D3DKMT directly, and no input can be injected without changing src/.

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:6583203929 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1538). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-24T13:45:56Z. --- ## Change `adapter_identity::detail::key_length()` (`src/adapter_identity.{h,cpp}`) accepts a PnP key only if it is a nonempty, terminated string that lies inside the buffer. It refuses a reported length past the capacity (`written > capacity`), and it refuses an embedded terminator (`wcsnlen`). `xmem_gpu_usage` relies on this when it matches adapters. The existing `key_past_buffer` case puts a second terminator inside the capacity, so the embedded-terminator check also rejects it. The capacity bound is never the only thing under test. Two mutants of that bound left `xmem_shim_working_set`, `_aggregation` and `_warp` passing: - dropping `written > capacity`; - `written > capacity + 1`. This adds one hand-worked row to `key_checks()`: `key_terminator_past_buffer`, a well-formed 32-character key in a 32-character buffer with a reported length of 33. Without the bound, `key_length` returns 32; with it, the expected result is 0. Only `tests/xmem_shim_check.cpp` and one sentence in the `xmem_shim_working_set_aggregation` row of `docs/experiments.md` change. There is no new CTest. ## Verification Commit `309cfede` (`main`) plus this change, `dev` preset (Debug), Windows 11 Pro build 26300.9974, MSVC 19.51.36260, Windows SDK 10.0.26100.0. ```powershell ctest --preset dev --no-tests=error -R '^xmem_shim_working_set(_aggregation|_warp)?$' -V # 3/3 passed; the new row prints key_terminator_past_buffer length=0 ok ``` `tests/docs_links_check.py`, `tests/bidi_controls_check.py` and `git diff --check` passed. ## Mutation probe I applied each mutation to `src/adapter_identity.{h,cpp}` alone, rebuilt `xmem_shim_check` and `xmem_shim`, ran the three tests, then reverted. None of this is committed. | Mutant | Existing | With the new row | |---|---|---| | control: `same_device` case-sensitive | killed | killed | | drop `written > capacity` | **survived** | killed (`key_terminator_past_buffer length=32 MISMATCH`) | | `written > capacity + 1` | **survived** | killed | | terminator counted in the length | killed | killed | | prefix counts as the same device | survived | survived (not pinned) | | `== CSTR_EQUAL` → `!= CSTR_LESS_THAN` | killed | killed | | drop the `wcsnlen` check | killed | killed | | `query()`: partial device list kept | survived | survived (needs a device) | | `query()`: software key requested | survived | survived (needs a device) | | `query()`: `length` passed as capacity | survived | survived (needs a device) | Not covered on purpose: - **Prefix mutant.** Real hardware keys are full registry paths ending in `\Device Parameters`, so one key cannot be a proper prefix of another. - **`query()` mutants.** `query()` calls D3DKMT directly, and no input can be injected without changing `src/`. ## 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 15:01:55 +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!1531
No description provided.