upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test
diff options
context:
space:
mode:
authorIAmNotSiob <273832158+IAmNotSiob@users.noreply.github.com>2026-11-21 23:50:12 +0100
committerroot <root@m29531.contaboserver.net>2026-11-21 23:50:31 +0100
commit0ca9bb8bc034ac2f5f322ce5b428af964bffca4e (patch)
treedf89c76031bd3b2dbdc63a2e8172e07d659fb4d1 /runtime/test
parent8bd0087d2d0e45fc18fa78fb40b70ecfc6648854 (diff)
Runtime: gta6rt --run, and a stand-in game executable run through the loader
gta6rt --run <exe> [-- args] maps and binds the executable, initialises its modules and calls its entry point on a game thread. shims/process.cpp answers the process-level imports through the game's own import table: argv/argc and GetCommandLine are the game's (UCRT api-set/ucrtbase and msvcrt forms), and exit/_exit/quick_exit/ExitProcess/abort end the run with that exit code instead of the host. gta6rt then exits with the game's code directly, so the game's atexit handlers don't run during host shutdown. tiny_game.exe (CTest run_tiny_game, run_tiny_game_exit_code) does what a title does in its first second: arguments, a static initializer, a 4 MiB allocation, a file round trip, a thread, and xgameruntime.dll imports, which the shim registry answers instead of the link-time stub. thread_local and a C++ exception are opt-in (LEONIDA_RUN_TLS / LEONIDA_RUN_EH) until #93 and #176.
Diffstat (limited to 'runtime/test')
-rw-r--r--runtime/test/fixtures/tiny_game.cpp106
-rw-r--r--runtime/test/fixtures/xgameruntime_stub.cpp10
2 files changed, 116 insertions, 0 deletions
diff --git a/runtime/test/fixtures/tiny_game.cpp b/runtime/test/fixtures/tiny_game.cpp
new file mode 100644
index 000000000..3cb08937f
--- /dev/null
+++ b/runtime/test/fixtures/tiny_game.cpp
@@ -0,0 +1,106 @@
1// A stand-in game executable for `gta6rt --run`: what a real title does in its first second, run through the
2// loader instead of the Windows one. Test code, not game content. Run as
3// gta6rt --run tiny_game.exe -- hello 42
4// It checks its arguments, a static initializer, the heap, a file round trip, a second thread, and the
5// xgameruntime.dll imports (answered by the shim registry: the stub DLL it links against would fail them), then
6// calls exit() with 0 when everything held. thread_local and a C++ exception need static TLS (#93) and unwind
7// registration (#176) in the loader; LEONIDA_RUN_TLS=1 / LEONIDA_RUN_EH=1 turn those checks on.
8#include <windows.h>
9
10#include <cstdint>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <string>
15#include <thread>
16#include <vector>
17
18extern "C" __declspec(dllimport) HRESULT __stdcall XGameRuntimeInitialize();
19extern "C" __declspec(dllimport) void __stdcall XGameRuntimeUninitialize();
20extern "C" __declspec(dllimport) uint32_t __stdcall XSystemGetDeviceType();
21
22namespace {
23
24int g_failures = 0;
25
26void check(bool ok, const char* what) {
27 std::printf("tiny_game: %s %s\n", ok ? "PASS" : "FAIL", what);
28 if (!ok) ++g_failures;
29}
30
31struct StaticInit {
32 int value;
33 StaticInit() : value(1234) {}
34};
35StaticInit g_static;
36
37bool env_on(const char* name) {
38 char buf[8] = {};
39 return GetEnvironmentVariableA(name, buf, sizeof buf) > 0 && buf[0] == '1';
40}
41
42thread_local int t_counter = 5;
43
44} // namespace
45
46int main(int argc, char** argv) {
47 std::printf("tiny_game: started, argc=%d\n", argc);
48 check(argc == 3 && std::strcmp(argv[1], "hello") == 0 && std::strcmp(argv[2], "42") == 0,
49 "argv is the game's command line (\"hello\" \"42\"), not gta6rt's");
50 check(std::strstr(GetCommandLineA(), "hello 42") != nullptr, "GetCommandLineA is the game's command line");
51 check(g_static.value == 1234, "a static initializer ran before main");
52
53 std::vector<int> v(1 << 20);
54 for (size_t i = 0; i < v.size(); ++i) v[i] = int(i);
55 check(v[123456] == 123456, "a 4 MiB heap allocation works");
56
57 char tmp[MAX_PATH] = {};
58 GetTempPathA(MAX_PATH, tmp);
59 std::string path = std::string(tmp) + "leonida_tiny_game_" + std::to_string(GetCurrentProcessId()) + ".txt";
60 bool file_ok = false;
61 if (FILE* f = std::fopen(path.c_str(), "wb")) {
62 std::fputs("leonida", f);
63 std::fclose(f);
64 char back[16] = {};
65 if (FILE* r = std::fopen(path.c_str(), "rb")) {
66 std::fread(back, 1, 7, r);
67 std::fclose(r);
68 file_ok = std::strcmp(back, "leonida") == 0;
69 }
70 std::remove(path.c_str());
71 }
72 check(file_ok, "a file round trip through the CRT works");
73
74 long long sum = 0;
75 std::thread worker([&] { for (int i = 1; i <= 1000; ++i) sum += i; });
76 worker.join();
77 check(sum == 500500, "a second thread ran and joined");
78
79 check(XGameRuntimeInitialize() == S_OK, "XGameRuntimeInitialize is answered by the gta6rt shim (S_OK)");
80 check(XSystemGetDeviceType() == 7, "XSystemGetDeviceType is answered by the gta6rt shim (Series X)");
81 XGameRuntimeUninitialize();
82
83 if (env_on("LEONIDA_RUN_TLS")) {
84 int seen = -1;
85 std::thread t([&] { seen = t_counter; t_counter = 9; });
86 t.join();
87 check(t_counter == 5 && seen == 5, "thread_local starts at its initializer on each thread");
88 } else {
89 std::printf("tiny_game: SKIP thread_local (needs static TLS, #93; LEONIDA_RUN_TLS=1)\n");
90 }
91 if (env_on("LEONIDA_RUN_EH")) {
92 bool caught = false;
93 try {
94 throw std::string("boom");
95 } catch (const std::string& e) {
96 caught = e == "boom";
97 }
98 check(caught, "a C++ exception is thrown and caught inside the image");
99 } else {
100 std::printf("tiny_game: SKIP C++ exception (needs unwind registration, #176; LEONIDA_RUN_EH=1)\n");
101 }
102
103 std::printf("tiny_game: %s\n", g_failures ? "FAILED" : "OK");
104 std::fflush(stdout);
105 std::exit(g_failures ? 1 : 0); // through the shimmed exit(): ends the run, not gta6rt
106}
diff --git a/runtime/test/fixtures/xgameruntime_stub.cpp b/runtime/test/fixtures/xgameruntime_stub.cpp
new file mode 100644
index 000000000..749eda8fc
--- /dev/null
+++ b/runtime/test/fixtures/xgameruntime_stub.cpp
@@ -0,0 +1,10 @@
1// Link-time stand-in for the GDK's xgameruntime.dll, so tiny_game.exe gets real imports of these names. Under
2// `gta6rt --run` the shim registry answers them first and this DLL never loads; run directly, these failing answers
3// are what tiny_game would see. Test code, not game content.
4#include <windows.h>
5
6#include <cstdint>
7
8extern "C" __declspec(dllexport) HRESULT __stdcall XGameRuntimeInitialize() { return E_FAIL; }
9extern "C" __declspec(dllexport) void __stdcall XGameRuntimeUninitialize() {}
10extern "C" __declspec(dllexport) uint32_t __stdcall XSystemGetDeviceType() { return 0; }