runtime: validate GameInput COM pointers and add contract checks #1867
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1874"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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::readableacross the COM entry points insrc/startup_stubs/gameinput.cpp, and addtests/startup_gameinput_contracts_check.cpp(startup_gameinput_contracts_checkinCMakeLists.txt).GameInputCreate,IUnknown::QueryInterface,GetCurrentReading,GetNextReading,GetPreviousReading,FindDeviceFromId,FindDeviceFromPlatformHandle,FindDeviceFromObject, andFindDeviceFromPlatformStringverifyx2w_local::writable(out, sizeof(*out))and returnE_POINTER(0x80004003) before writing.RegisterDeviceCallback,RegisterReadingCallback,RegisterGuideButtonCallback, andRegisterKeyboardLayoutCallbackallow an optionalnullptrtoken, and returnE_POINTERwhen a non-nulltokenfailsx2w_local::writable(token, sizeof(*token))without invoking the device callback or incrementing token counters.FindDeviceFromIdvalidatesidwithx2w_local::readable(id, 32)before comparing device ID bytes.IGameInputReading::GetDevice,GetGamepadState, andGetKeyStatevalidate their output buffers for their full byte sizes (sizeof(*device),sizeof(*out),count * sizeof(KeyState)).tests/startup_gameinput_contracts_check.cppexercisesnullptr,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 ontoupstream/mainatb509a670), Windows x64 (devDebug andrelease/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.cppis a source-built fixture (MIT) loading the builtGameInput.dlldirectly with no external assets.#1859insrc/startup_stubs/gameinput.cpptouches onlyX2W_INPUT_SCRIPTenvironment parsing and does not conflict with these COM pointer checks.Checklist
AI-assisted.
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::writableandreadableinsrc/startup_stubs/local_log.h).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 = nullptrlines that already followed still run after validation.writableis oneVirtualQueryand 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_statenow clamps tocount, refuses a zero count, checks the destination forcount * sizeof(KeyState)and usessize_tarithmetic.reading_gamepadchecks the destination before the copy.reading_deviceskips the write and theAddReffor an unwritable pointer.input_register_callbackpreviously skipped an unwritable token and still returnedS_OK; it now returnsE_POINTER, which is the better contract.startup_gameinput_keys,startup_gameinput_history), the device-info offset0x68matchesstartup_gameinput_keys.cpp:157and the stub's layout atgameinput.cpp:678-687, andKeyStatehas 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_interfaceignores the IID and returnsS_OKfor 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 giveE_NOINTERFACEwith*out = nullptr.input_find_device_by_idtreats a non-null but unreadableidlike an unknown id and returns the virtual gamepad withS_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.X2W_INPUT_KEYS, but the stub also merges real key presses when the process has focus (gameinput.cpp:216-218), soGetKeyCount() == 2could 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: cleantests/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
writablecheck ininput_current_reading; theGetCurrentReading(PAGE_READONLY)check should then crash or fail.AI-assisted; commands listed were executed locally.
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:
Three mutations to
src/startup_stubs/gameinput.cpp, each rebuilt into GameInput.dll (the test loads the DLL, not theobject) with the DLL timestamp verified to change, then reverted:
IGameInputReading::GetKeyState: drop the array range check, keepcount == 0if (token && !writable(token, sizeof(*token))) return E_POINTER;→if (false)IGameInputReading::GetDevice:if (device && writable(device, sizeof(*device))) {→if (device) {So the
nullptrand unreadable-page negative controls in the new fixture are load-bearing for the entry points I probed, andthe test is reading the right artefact.
Two honest notes on getting there, because both first looked like findings:
startup_gameinput_contracts_check.exeand reportedrebuilt=False— the test loadsGameInput.dll(LoadLibraryA(argv[1])), so the exe never changes. Rebuilding the DLL is what made the mutants visible."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_READONLYand RW→RO page-straddling cases (VirtualProtectaround a boundary) I only saw pass, not mutate: themutations above cover the
nullptrand size paths, and the page-straddling assertions would need a mutation insidex2w_local::writableitself, which lives in shared headers and would affect other stubs. If you want that covered bymutation, it needs its own sweep.
Shape of the change
The
E_POINTERreturns happen before the token counters move and before any callback is invoked — the description says soand 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