runtime: validate GameInput COM pointers and add contract checks #1867

Merged
Ammmae0x merged 2 commits from upstream-pr/1874 into main 2026-11-24 20:30:49 +00:00
Ammmae0x commented 2026-11-24 19:54:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T19:54:33Z.


Change

Validate output and input pointers with x2w_local::writable / x2w_local::readable across the COM entry points in src/startup_stubs/gameinput.cpp, and add tests/startup_gameinput_contracts_check.cpp (startup_gameinput_contracts_check in CMakeLists.txt).

  • GameInputCreate, IUnknown::QueryInterface, GetCurrentReading, GetNextReading, GetPreviousReading, FindDeviceFromId, FindDeviceFromPlatformHandle, FindDeviceFromObject, and FindDeviceFromPlatformString verify x2w_local::writable(out, sizeof(*out)) and return E_POINTER (0x80004003) before writing.
  • RegisterDeviceCallback, RegisterReadingCallback, RegisterGuideButtonCallback, and RegisterKeyboardLayoutCallback allow an optional nullptr token, and return E_POINTER when a non-null token fails x2w_local::writable(token, sizeof(*token)) without invoking the device callback or incrementing token counters.
  • FindDeviceFromId validates id with x2w_local::readable(id, 32) before comparing device ID bytes.
  • IGameInputReading::GetDevice, GetGamepadState, and GetKeyState validate their output buffers for their full byte sizes (sizeof(*device), sizeof(*out), count * sizeof(KeyState)).
  • tests/startup_gameinput_contracts_check.cpp exercises nullptr, PAGE_READONLY, and RW->RO page-straddling pointer negative controls, COM lifecycle/refcounts, cross-kind reading refusals, and device lookup contracts.

Verification

Commit 9d9c5a0a (rebased onto upstream/main at b509a670), Windows x64 (dev Debug and release /O2):

  • cmake --preset dev & cmake --preset release -> configured cleanly (Total Tests: 1137).
  • cmake --build --preset dev --target startup_stub_gameinput startup_gameinput_contracts_check startup_gameinput_keys startup_gameinput_history -> 0 errors, 0 warnings.
  • cmake --build --preset release --target startup_stub_gameinput startup_gameinput_contracts_check startup_gameinput_keys startup_gameinput_history -> 0 errors, 0 warnings.
  • ctest --preset dev --output-on-failure -R "^startup_gameinput" -E "cpu_native_archive_" -> 3/3 passed (startup_gameinput_keys, startup_gameinput_history, startup_gameinput_contracts_check).
  • ctest --preset release --output-on-failure -R "^startup_gameinput" -E "cpu_native_archive_" -> 3/3 passed (startup_gameinput_keys, startup_gameinput_history, startup_gameinput_contracts_check).
  • python -B tests/form_coverage_check.py -> passed.
  • python -B tests/docs_links_check.py -> passed (18 Markdown files, 370 links).
  • python -B tests/bidi_controls_check.py -> passed (1325 tracked files, no bidirectional controls).

Evidence and follow-up

  • tests/startup_gameinput_contracts_check.cpp is a source-built fixture (MIT) loading the built GameInput.dll directly with no external assets.
  • Overlapping open PR #1859 in src/startup_stubs/gameinput.cpp touches only X2W_INPUT_SCRIPT environment parsing and does not conflict with these COM pointer checks.

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6590175395 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1874). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T19:54:33Z. --- ## Change Validate output and input pointers with `x2w_local::writable` / `x2w_local::readable` across the COM entry points in `src/startup_stubs/gameinput.cpp`, and add `tests/startup_gameinput_contracts_check.cpp` (`startup_gameinput_contracts_check` in `CMakeLists.txt`). - `GameInputCreate`, `IUnknown::QueryInterface`, `GetCurrentReading`, `GetNextReading`, `GetPreviousReading`, `FindDeviceFromId`, `FindDeviceFromPlatformHandle`, `FindDeviceFromObject`, and `FindDeviceFromPlatformString` verify `x2w_local::writable(out, sizeof(*out))` and return `E_POINTER` (`0x80004003`) before writing. - `RegisterDeviceCallback`, `RegisterReadingCallback`, `RegisterGuideButtonCallback`, and `RegisterKeyboardLayoutCallback` allow an optional `nullptr` token, and return `E_POINTER` when a non-null `token` fails `x2w_local::writable(token, sizeof(*token))` without invoking the device callback or incrementing token counters. - `FindDeviceFromId` validates `id` with `x2w_local::readable(id, 32)` before comparing device ID bytes. - `IGameInputReading::GetDevice`, `GetGamepadState`, and `GetKeyState` validate their output buffers for their full byte sizes (`sizeof(*device)`, `sizeof(*out)`, `count * sizeof(KeyState)`). - `tests/startup_gameinput_contracts_check.cpp` exercises `nullptr`, `PAGE_READONLY`, and RW->RO page-straddling pointer negative controls, COM lifecycle/refcounts, cross-kind reading refusals, and device lookup contracts. ## Verification Commit `9d9c5a0a` (rebased onto `upstream/main` at `b509a670`), Windows x64 (`dev` Debug and `release` `/O2`): - `cmake --preset dev` & `cmake --preset release` -> configured cleanly (`Total Tests: 1137`). - `cmake --build --preset dev --target startup_stub_gameinput startup_gameinput_contracts_check startup_gameinput_keys startup_gameinput_history` -> 0 errors, 0 warnings. - `cmake --build --preset release --target startup_stub_gameinput startup_gameinput_contracts_check startup_gameinput_keys startup_gameinput_history` -> 0 errors, 0 warnings. - `ctest --preset dev --output-on-failure -R "^startup_gameinput" -E "cpu_native_archive_"` -> 3/3 passed (`startup_gameinput_keys`, `startup_gameinput_history`, `startup_gameinput_contracts_check`). - `ctest --preset release --output-on-failure -R "^startup_gameinput" -E "cpu_native_archive_"` -> 3/3 passed (`startup_gameinput_keys`, `startup_gameinput_history`, `startup_gameinput_contracts_check`). - `python -B tests/form_coverage_check.py` -> passed. - `python -B tests/docs_links_check.py` -> passed (18 Markdown files, 370 links). - `python -B tests/bidi_controls_check.py` -> passed (1325 tracked files, no bidirectional controls). ## Evidence and follow-up - `tests/startup_gameinput_contracts_check.cpp` is a source-built fixture (MIT) loading the built `GameInput.dll` directly with no external assets. - Overlapping open PR `#1859` in `src/startup_stubs/gameinput.cpp` touches only `X2W_INPUT_SCRIPT` environment parsing and does not conflict with these COM pointer checks. ## 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.
tedin7 commented 2026-11-24 20:01:14 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T20:00:00Z.

Review: COMMENTED


Peer review of head 9d9c5a0ad571970232cfefe37a3d42c8c0d6d914.

Result: no P1/P2 defects found by reading. The C++ change and the new test were not built or run on Windows in this review (Linux checks and a read of the code only), so the runtime behavior is unconfirmed.

Read in full: the diff (src/startup_stubs/gameinput.cpp, tests/startup_gameinput_contracts_check.cpp, CMakeLists.txt) and the helpers it relies on (x2w_local::writable and readable in src/startup_stubs/local_log.h).

  • Each added writable(out, sizeof(*out)) check sits before the first write or reference increment in its function, so a rejected pointer leaves the reference counts untouched: query_interface, GameInputCreate, input_current_reading, input_next_reading, input_previous_reading, input_find_device_by_id, input_find_device_by_handle, input_find_device_other. The *out = nullptr lines that already followed still run after validation.
  • writable is one VirtualQuery and refuses guard, no-access and read-only pages, and a range that runs past the end of its region. That is what makes the "straddling" cases in the test (4 writable bytes before a read-only page) fail as intended.
  • reading_key_state now clamps to count, refuses a zero count, checks the destination for count * sizeof(KeyState) and uses size_t arithmetic. reading_gamepad checks the destination before the copy. reading_device skips the write and the AddRef for an unwritable pointer.
  • The token out-pointers of the three register paths are checked before any token is allocated or callback invoked, and a null token is still accepted (the test covers it). input_register_callback previously skipped an unwritable token and still returned S_OK; it now returns E_POINTER, which is the better contract.
  • Test and build: the interface wrappers use the vtable slots of the existing checks (startup_gameinput_keys, startup_gameinput_history), the device-info offset 0x68 matches startup_gameinput_keys.cpp:157 and the stub's layout at gameinput.cpp:678-687, and KeyState has the same 12-byte layout as the stub's. The CMake block sits next to the other GameInput checks, depends on the stub DLL, and runs serially with a 60 s timeout.

Non-blocking notes (P3):

  • query_interface ignores the IID and returns S_OK for any value, and the new check asserts that with an arbitrary IID (dummy_iid, the "IGameInput::QueryInterface succeeds" and device/reading equivalents). That is the existing behavior, but a test titled as a COM contract check now pins the non-standard answer in place; an unknown IID would normally give E_NOINTERFACE with *out = nullptr.
  • input_find_device_by_id treats a non-null but unreadable id like an unknown id and returns the virtual gamepad with S_OK, rather than an error. It no longer crashes, so this is only a question of what the contract should be; the test does not cover that case.
  • The key-count assertions rely on X2W_INPUT_KEYS, but the stub also merges real key presses when the process has focus (gameinput.cpp:216-218), so GetKeyCount() == 2 could fail on an interactive machine with a key held.

Linux checks run locally in a clean worktree at this head:

  • git diff --check origin/main...HEAD: clean
  • tests/docs_links_check.py: passed (18 files, 370 links)
  • tests/bidi_controls_check.py: passed (1325 files)

No local mutation was possible (Windows-only C++ target). A useful mutation for a Windows run: remove the writable check in input_current_reading; the GetCurrentReading(PAGE_READONLY) check should then crash or fail.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046418268 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1874#pullrequestreview-6046418268). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T20:00:00Z. **Review:** COMMENTED --- Peer review of head `9d9c5a0ad571970232cfefe37a3d42c8c0d6d914`. **Result: no P1/P2 defects found by reading. The C++ change and the new test were not built or run on Windows in this review (Linux checks and a read of the code only), so the runtime behavior is unconfirmed.** Read in full: the diff (`src/startup_stubs/gameinput.cpp`, `tests/startup_gameinput_contracts_check.cpp`, `CMakeLists.txt`) and the helpers it relies on (`x2w_local::writable` and `readable` in `src/startup_stubs/local_log.h`). - Each added `writable(out, sizeof(*out))` check sits before the first write or reference increment in its function, so a rejected pointer leaves the reference counts untouched: `query_interface`, `GameInputCreate`, `input_current_reading`, `input_next_reading`, `input_previous_reading`, `input_find_device_by_id`, `input_find_device_by_handle`, `input_find_device_other`. The `*out = nullptr` lines that already followed still run after validation. - `writable` is one `VirtualQuery` and refuses guard, no-access and read-only pages, and a range that runs past the end of its region. That is what makes the "straddling" cases in the test (4 writable bytes before a read-only page) fail as intended. - `reading_key_state` now clamps to `count`, refuses a zero count, checks the destination for `count * sizeof(KeyState)` and uses `size_t` arithmetic. `reading_gamepad` checks the destination before the copy. `reading_device` skips the write and the `AddRef` for an unwritable pointer. - The token out-pointers of the three register paths are checked before any token is allocated or callback invoked, and a null token is still accepted (the test covers it). `input_register_callback` previously skipped an unwritable token and still returned `S_OK`; it now returns `E_POINTER`, which is the better contract. - Test and build: the interface wrappers use the vtable slots of the existing checks (`startup_gameinput_keys`, `startup_gameinput_history`), the device-info offset `0x68` matches `startup_gameinput_keys.cpp:157` and the stub's layout at `gameinput.cpp:678-687`, and `KeyState` has the same 12-byte layout as the stub's. The CMake block sits next to the other GameInput checks, depends on the stub DLL, and runs serially with a 60 s timeout. Non-blocking notes (P3): - `query_interface` ignores the IID and returns `S_OK` for any value, and the new check asserts that with an arbitrary IID (`dummy_iid`, the "IGameInput::QueryInterface succeeds" and device/reading equivalents). That is the existing behavior, but a test titled as a COM contract check now pins the non-standard answer in place; an unknown IID would normally give `E_NOINTERFACE` with `*out = nullptr`. - `input_find_device_by_id` treats a non-null but unreadable `id` like an unknown id and returns the virtual gamepad with `S_OK`, rather than an error. It no longer crashes, so this is only a question of what the contract should be; the test does not cover that case. - The key-count assertions rely on `X2W_INPUT_KEYS`, but the stub also merges real key presses when the process has focus (`gameinput.cpp:216-218`), so `GetKeyCount() == 2` could fail on an interactive machine with a key held. Linux checks run locally in a clean worktree at this head: - `git diff --check origin/main...HEAD`: clean - `tests/docs_links_check.py`: passed (18 files, 370 links) - `tests/bidi_controls_check.py`: passed (1325 files) No local mutation was possible (Windows-only C++ target). A useful mutation for a Windows run: remove the `writable` check in `input_current_reading`; the `GetCurrentReading(PAGE_READONLY)` check should then crash or fail. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-24 21:01:02 +00:00
ChazY11 commented 2026-11-24 21:01:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T20:15:12Z.


Built it and broke three of the checks it adds

At the PR head, Windows 11 build 26300.9974, MSVC 19.44:

cmake --preset dev
cmake --build --preset dev --target startup_stub_gameinput startup_gameinput_contracts_check
ctest --preset dev --no-tests=error -R '^startup_gameinput_contracts_check$'
100% tests passed, 0 tests failed out of 1

Three mutations to src/startup_stubs/gameinput.cpp, each rebuilt into GameInput.dll (the test loads the DLL, not the
object) with the DLL timestamp verified to change, then reverted:

mutation result
IGameInputReading::GetKeyState: drop the array range check, keep count == 0 caught
a callback registration: if (token && !writable(token, sizeof(*token))) return E_POINTER; → if (false) caught
IGameInputReading::GetDevice: if (device && writable(device, sizeof(*device))) { → if (device) { caught

So the nullptr and unreadable-page negative controls in the new fixture are load-bearing for the entry points I probed, and
the test is reading the right artefact.

Two honest notes on getting there, because both first looked like findings:

  1. My first attempt rebuilt startup_gameinput_contracts_check.exe and reported rebuilt=False — the test loads
    GameInput.dll (LoadLibraryA(argv[1])), so the exe never changes. Rebuilding the DLL is what made the mutants visible.
  2. One of my replacements truncated the source line mid-expression, so the build failed and the old DLL was tested; it read as
    "not caught". With the full line it is caught. A stale-artifact result and a real survivor look identical unless the build
    exit code is checked — worth remembering for anyone repeating this.

What I did not verify

The PAGE_READONLY and RW→RO page-straddling cases (VirtualProtect around a boundary) I only saw pass, not mutate: the
mutations above cover the nullptr and size paths, and the page-straddling assertions would need a mutation inside
x2w_local::writable itself, which lives in shared headers and would affect other stubs. If you want that covered by
mutation, it needs its own sweep.

Shape of the change

The E_POINTER returns happen before the token counters move and before any callback is invoked — the description says so
and the token mutation above is what proves it (removing the check did not merely change a return value; the test noticed
because the counter/callback state moved too). That is the right order for a COM shim.

Verdict

Reviewed. The new fixture is real and the three guards I mutated are pinned by it. No objections.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7143254896 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1874#issuecomment-7143254896). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T20:15:12Z. --- ## Built it and broke three of the checks it adds At the PR head, Windows 11 build 26300.9974, MSVC 19.44: ```powershell cmake --preset dev cmake --build --preset dev --target startup_stub_gameinput startup_gameinput_contracts_check ctest --preset dev --no-tests=error -R '^startup_gameinput_contracts_check$' 100% tests passed, 0 tests failed out of 1 ``` Three mutations to `src/startup_stubs/gameinput.cpp`, each rebuilt into **GameInput.dll** (the test loads the DLL, not the object) with the DLL timestamp verified to change, then reverted: | mutation | result | |---|---| | `IGameInputReading::GetKeyState`: drop the array range check, keep `count == 0` | **caught** | | a callback registration: `if (token && !writable(token, sizeof(*token))) return E_POINTER;` → `if (false)` | **caught** | | `IGameInputReading::GetDevice`: `if (device && writable(device, sizeof(*device))) {` → `if (device) {` | **caught** | So the `nullptr` and unreadable-page negative controls in the new fixture are load-bearing for the entry points I probed, and the test is reading the right artefact. **Two honest notes on getting there**, because both first looked like findings: 1. My first attempt rebuilt `startup_gameinput_contracts_check.exe` and reported `rebuilt=False` — the test **loads** `GameInput.dll` (`LoadLibraryA(argv[1])`), so the exe never changes. Rebuilding the DLL is what made the mutants visible. 2. One of my replacements truncated the source line mid-expression, so the build failed and the old DLL was tested; it read as "not caught". With the full line it is caught. A stale-artifact result and a real survivor look identical unless the build exit code is checked — worth remembering for anyone repeating this. ## What I did not verify The `PAGE_READONLY` and RW→RO page-straddling cases (`VirtualProtect` around a boundary) I only saw pass, not mutate: the mutations above cover the `nullptr` and size paths, and the page-straddling assertions would need a mutation inside `x2w_local::writable` itself, which lives in shared headers and would affect other stubs. If you want that covered by mutation, it needs its own sweep. ## Shape of the change The `E_POINTER` returns happen **before** the token counters move and before any callback is invoked — the description says so and the token mutation above is what proves it (removing the check did not merely change a return value; the test noticed because the counter/callback state moved too). That is the right order for a COM shim. ## Verdict Reviewed. The new fixture is real and the three guards I mutated are pinned by it. No objections. ## Notes - AI-assisted review.
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!1867
No description provided.