From 37e6d482865ce7266cb198163c049121f46caeb0 Mon Sep 17 00:00:00 2001 From: Sc4ros <353102343+Sc4ros@users.noreply.github.com> Date: Sun, 22 Nov 2026 17:59:38 +0100 Subject: Tests: the host GameInput.dll path from mapped code docs/gameinput.md says that without the SDL3 shim the loader binds GameInputCreate to the host's GameInput.dll, but nothing tested it. gameinput_host_test maps a CRT-free fixture DLL built against the SDK's gameinput.lib and checks that the import binds to the host copy (the test links no shim table), that GameInputCreate and GetCurrentTimestamp work from mapped code, that GetCurrentReading answers, and, with a pad attached, that GetGamepadState succeeds. It skips (77) where there is no GameInput.dll, and is only built when gameinput.lib is found. Co-Authored-By: Claude Sonnet 5.5 --- runtime/test/gameinput_host_test.cpp | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 runtime/test/gameinput_host_test.cpp (limited to 'runtime/test/gameinput_host_test.cpp') diff --git a/runtime/test/gameinput_host_test.cpp b/runtime/test/gameinput_host_test.cpp new file mode 100644 index 000000000..5673965a9 --- /dev/null +++ b/runtime/test/gameinput_host_test.cpp @@ -0,0 +1,79 @@ +// The host GameInput path: mapped code that imports GameInputCreate from GameInput.dll, with no GameInput shim. +// +// docs/gameinput.md: without the SDL3 shim, the loader binds GameInputCreate to the host's GameInput.dll. This checks +// that path with a synthetic DLL (test/fixtures/gameinput_host.cpp) mapped by rt::Loader: +// - its GameInput.dll import binds to the host's copy (this test links no shim table, so it cannot be a shim); +// - GameInputCreate succeeds from mapped code, and the library answers GetCurrentTimestamp; +// - GetCurrentReading says either "reading" or "no reading found": no crash, no other error. With a gamepad attached +// GetGamepadState must succeed on the reading; the test passes with or without one. +// Skips (exit 77) where there is no GameInput.dll (the import is then unimplemented). No game files. +#include + +#include +#include +#include +#include + +#include "pe_loader.h" + +namespace { + +using ProbeFn = int (*)(long*, unsigned long long*, long*, int*); +constexpr unsigned long kReadingNotFound = 0x838A0003UL; // GAMEINPUT_E_READING_NOT_FOUND + +int g_failures = 0; + +void check(bool ok, const std::string& what) { + std::printf("%s %s\n", ok ? "PASS" : "FAIL", what.c_str()); + std::fflush(stdout); + if (!ok) ++g_failures; +} + +} // namespace + +int main(int argc, char** argv) { + if (argc != 2) { + std::fprintf(stderr, "usage: gameinput_host_test \n"); + return 2; + } + const std::filesystem::path fixture = argv[1]; + try { + rt::Loader loader(fixture.parent_path()); + rt::Module& m = loader.load(fixture); + + // The SDK's import library imports by ordinal, so match the DLL name, not the function name. + rt::Binding binding = rt::Binding::Unimplemented; + int seen = 0; + for (const auto& r : m.imports) + if (rt::lower(r.dll) == "gameinput.dll") { + binding = r.binding; + ++seen; + } + if (seen == 1 && binding == rt::Binding::Unimplemented) { + std::printf("SKIP: no GameInput.dll on this machine\n"); + return 77; + } + check(seen == 1 && binding == rt::Binding::Host, "the GameInput.dll import binds to the host's copy"); + + loader.initialise(); + const auto probe = reinterpret_cast(m.exports.at("gameinput_host_probe")); + long create_hr = -1, reading_hr = -1; + unsigned long long timestamp = 0; + int state_ok = 0; + const int created = probe(&create_hr, ×tamp, &reading_hr, &state_ok); + std::printf(" create hr=0x%08lX, timestamp=%llu, reading hr=0x%08lX\n", static_cast(create_hr), + timestamp, static_cast(reading_hr)); + check(created == 1 && create_hr == 0, "GameInputCreate succeeds from mapped code"); + check(timestamp != 0, "GetCurrentTimestamp returns a time"); + const bool pad = reading_hr == 0; + check(pad || static_cast(reading_hr) == kReadingNotFound, + "GetCurrentReading says either 'reading' or 'no reading found'"); + std::printf(" gamepad reading: %s\n", pad ? "yes (a gamepad is attached)" : "none (no gamepad attached)"); + if (pad) check(state_ok == 1, "GetGamepadState succeeds on a real reading"); + } catch (const std::exception& e) { + std::printf("FAIL unexpected exception: %s\n", e.what()); + ++g_failures; + } + std::printf("gameinput_host_test: %s (%d failures)\n", g_failures ? "FAILED" : "OK", g_failures); + return g_failures ? 1 : 0; +} -- cgit v1.2.3 From a41173a1aed8a04ec128544e2a04bf24095beea9 Mon Sep 17 00:00:00 2001 From: Sc4ros <353102343+Sc4ros@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:15:41 +0100 Subject: Tests: gameinput_host fails instead of skipping when GameInput.dll is present (review) - gameinput_host_test decides the skip before mapping: LoadLibraryExW of GameInput.dll from System32. Only "not found" skips (77); an import that then binds to nothing is a failure. Breaking host ordinal binding in Loader::resolve now fails the test (4 failures) instead of skipping. - The gameinput_host fixture links in Debug: /RTC1 with /NODEFAULTLIB left _RTC_InitBase, _RTC_Shutdown and _RTC_CheckStackVars unresolved, so the fixture defines them as no-ops when runtime checks are on. - The fixture reports E_FAIL, not S_OK, when GameInputCreate fails, and the test counts a gamepad as attached only when the library was created. Co-Authored-By: Claude Sonnet 5.5 --- docs/gameinput.md | 2 +- runtime/CMakeLists.txt | 4 ++-- runtime/test/fixtures/gameinput_host.cpp | 15 +++++++++++++-- runtime/test/gameinput_host_test.cpp | 23 +++++++++++++++++------ 4 files changed, 33 insertions(+), 11 deletions(-) (limited to 'runtime/test/gameinput_host_test.cpp') diff --git a/docs/gameinput.md b/docs/gameinput.md index 39c1dc66d..eb82698dd 100644 --- a/docs/gameinput.md +++ b/docs/gameinput.md @@ -12,7 +12,7 @@ features. That path is tested without the game: `gameinput_host_test` (ctest `ga built against the SDK's `gameinput.lib` and checks that its `GameInput.dll` import binds to the host's copy, that `GameInputCreate` and `GetCurrentTimestamp` work from mapped code, and that `GetCurrentReading` answers; with a pad attached, `GetGamepadState` must succeed on the reading. It passes on Windows 11 build 26300 (GameInput 0.2309) without -a pad attached. The test skips where there is no `GameInput.dll`. The game executable is not available yet, +a pad attached. The test skips only where Windows cannot load `GameInput.dll`; with the DLL present, an import that binds to nothing fails it. The game executable is not available yet, so whether it imports GameInput this way, and under which module, is unverified (see [VERIFY](#verify)). diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 768997f94..3756d023f 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -561,7 +561,7 @@ if(BUILD_TESTING) # The host GameInput path (docs/gameinput.md): mapped code that imports GameInputCreate from GameInput.dll, with no # GameInput shim linked. Needs the SDK's gameinput.lib and GameInput.h to build the fixture, and a GameInput.dll on - # the machine to run (the test skips with 77 otherwise). No game files. + # the machine to run (the test skips with 77 only when Windows cannot load GameInput.dll). No game files. find_library(GAMEINPUT_LIBRARY NAMES gameinput HINTS ${_sdk_lib_hints}) if(GAMEINPUT_LIBRARY) add_library(gameinput_host SHARED test/fixtures/gameinput_host.cpp) @@ -584,7 +584,7 @@ if(BUILD_TESTING) target_compile_options(gameinput_host_test PRIVATE /W4 /permissive- /utf-8 /EHsc) add_dependencies(gameinput_host_test gameinput_host) add_test(NAME gameinput_host COMMAND gameinput_host_test "$") - set_tests_properties(gameinput_host PROPERTIES SKIP_RETURN_CODE 77) # no GameInput.dll on this machine + set_tests_properties(gameinput_host PROPERTIES SKIP_RETURN_CODE 77) # Windows cannot load GameInput.dll endif() # LEONIDA_UNIMPLEMENTED=log|fail|stop: builds its own image with two imports that bind to nothing, calls them in diff --git a/runtime/test/fixtures/gameinput_host.cpp b/runtime/test/fixtures/gameinput_host.cpp index 51274b6bc..f28945045 100644 --- a/runtime/test/fixtures/gameinput_host.cpp +++ b/runtime/test/fixtures/gameinput_host.cpp @@ -8,8 +8,19 @@ extern "C" BOOL WINAPI gameinput_host_main(HINSTANCE, DWORD, LPVOID) { return TRUE; } +#ifdef __MSVC_RUNTIME_CHECKS +// Debug builds compile with /RTC1 (the Visual Studio generator's default), whose checks call into the CRT; /NODEFAULTLIB +// leaves these three unresolved (LNK1120). The checks have nothing to check here, so they do nothing. +extern "C" { +void __cdecl _RTC_InitBase() {} +void __cdecl _RTC_Shutdown() {} +void __fastcall _RTC_CheckStackVars(void*, void*) {} +} +#endif + // Returns 1 when the library was created. reading_hr is S_OK with a gamepad attached and -// GAMEINPUT_E_READING_NOT_FOUND without one; state_ok is 1 when GetGamepadState succeeded on a reading. +// GAMEINPUT_E_READING_NOT_FOUND without one (E_FAIL when the library was not created); state_ok is 1 when +// GetGamepadState succeeded on a reading. extern "C" __declspec(dllexport) int gameinput_host_probe(long* create_hr, unsigned long long* timestamp, long* reading_hr, int* state_ok) { IGameInput* input = nullptr; @@ -17,7 +28,7 @@ extern "C" __declspec(dllexport) int gameinput_host_probe(long* create_hr, unsig const HRESULT hr = GameInputCreate(&input); *create_hr = hr; *timestamp = 0; - *reading_hr = 0; + *reading_hr = E_FAIL; // not S_OK: a failed GameInputCreate must not look like an attached gamepad *state_ok = 0; if (FAILED(hr) || !input) return 0; *timestamp = input->GetCurrentTimestamp(); diff --git a/runtime/test/gameinput_host_test.cpp b/runtime/test/gameinput_host_test.cpp index 5673965a9..cdd071055 100644 --- a/runtime/test/gameinput_host_test.cpp +++ b/runtime/test/gameinput_host_test.cpp @@ -6,7 +6,8 @@ // - GameInputCreate succeeds from mapped code, and the library answers GetCurrentTimestamp; // - GetCurrentReading says either "reading" or "no reading found": no crash, no other error. With a gamepad attached // GetGamepadState must succeed on the reading; the test passes with or without one. -// Skips (exit 77) where there is no GameInput.dll (the import is then unimplemented). No game files. +// Skips (exit 77) only where Windows itself cannot load GameInput.dll (asked before mapping, with LoadLibraryExW). +// When the DLL is there, an import that binds to nothing is a failure, not a skip. No game files. #include #include @@ -37,6 +38,19 @@ int main(int argc, char** argv) { return 2; } const std::filesystem::path fixture = argv[1]; + + // Decide the skip from the machine, not from the loader's answer: a broken host binding must fail the test. + HMODULE const host_gameinput = LoadLibraryExW(L"GameInput.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + if (!host_gameinput) { + const DWORD error = GetLastError(); + if (error == ERROR_MOD_NOT_FOUND || error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) { + std::printf("SKIP: no GameInput.dll on this machine\n"); + return 77; + } + std::printf("FAIL GameInput.dll is present but does not load (error %lu)\n", static_cast(error)); + return 1; + } + try { rt::Loader loader(fixture.parent_path()); rt::Module& m = loader.load(fixture); @@ -49,10 +63,6 @@ int main(int argc, char** argv) { binding = r.binding; ++seen; } - if (seen == 1 && binding == rt::Binding::Unimplemented) { - std::printf("SKIP: no GameInput.dll on this machine\n"); - return 77; - } check(seen == 1 && binding == rt::Binding::Host, "the GameInput.dll import binds to the host's copy"); loader.initialise(); @@ -65,7 +75,7 @@ int main(int argc, char** argv) { timestamp, static_cast(reading_hr)); check(created == 1 && create_hr == 0, "GameInputCreate succeeds from mapped code"); check(timestamp != 0, "GetCurrentTimestamp returns a time"); - const bool pad = reading_hr == 0; + const bool pad = created == 1 && reading_hr == 0; check(pad || static_cast(reading_hr) == kReadingNotFound, "GetCurrentReading says either 'reading' or 'no reading found'"); std::printf(" gamepad reading: %s\n", pad ? "yes (a gamepad is attached)" : "none (no gamepad attached)"); @@ -74,6 +84,7 @@ int main(int argc, char** argv) { std::printf("FAIL unexpected exception: %s\n", e.what()); ++g_failures; } + FreeLibrary(host_gameinput); std::printf("gameinput_host_test: %s (%d failures)\n", g_failures ? "FAILED" : "OK", g_failures); return g_failures ? 1 : 0; } -- cgit v1.2.3