From a14ddbc63c9bf4b7588ceb024f70cee45959ab9d Mon Sep 17 00:00:00 2001 From: Shrey Ratna Date: Mon, 23 Nov 2026 00:24:49 +0530 Subject: gta6rt: GAME: and the overlay are absolute, so a bare image name has a game folder `gta6rt --run game.exe`, started from the game folder, set the game folder to the image's parent path, which is empty for a bare name: the run printed `console paths: GAME: -> , writes -> overlay` and every GAME: open failed with ERROR_PATH_NOT_FOUND. A relative --game-dir or --overlay worked only until the title changed its current directory. main.cpp now makes both absolute at startup (keeping the path as given if absolute() fails). New CTest run_tiny_game_dir: tiny_game's opt-in `game-file` check moves its current directory, then reads GAME:\tiny_game_probe.txt; check_game_dir.cmake runs it by bare name and with `--game-dir .`. Without the fix both fail (open errors 3 and 2); with it both pass. Co-Authored-By: Claude Opus 5.5 --- runtime/src/main.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'runtime/src/main.cpp') diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 06baae387..ae80b5dff 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -19,8 +19,10 @@ #include #include +#include #include #include +#include #include #include "base_reservation.h" @@ -76,7 +78,16 @@ int wmain(int argc, wchar_t** argv) { if (image.empty()) return usage(); // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. if (dry_run + init + run != 1) return usage(); - if (game_dir.empty()) game_dir = image.parent_path(); + // The console folders are fixed once, as absolute paths. A bare image name (`gta6rt --run game.exe` from the + // game folder) has an empty parent path, which left GAME: with no root; and a relative --game-dir or --overlay + // would follow the title's later SetCurrentDirectory. If absolute() fails the path stays as given. + const auto absolute_or_same = [](const std::filesystem::path& p) { + std::error_code ec; + std::filesystem::path a = std::filesystem::absolute(p, ec); + return ec ? p : a; + }; + game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); + if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir); // An image without relocations whose range this process already uses (arm64 Wine on macOS) runs in a new // process that has the range reserved from the start (base_reservation.h); that process does not do this again. if (!rt::base_reservation::inherited()) -- cgit v1.2.3 From 1188e6406e31870119a7e50c5972599db554a406 Mon Sep 17 00:00:00 2001 From: jaybeebabageyus Date: Sun, 22 Nov 2026 20:54:47 +0100 Subject: runtime: scope the TLS clone purge to ownerless files, clean clones before ExitProcess, per-slot thread-event errors The startup purge deleted every leonida_tls_host_*.dll in the temp directory, so it could remove the clone of a concurrent process that had copied its helper but not reached LoadLibraryExW yet. Clones are now copied to a staging name and renamed into place while the copy step's guard handle is open, so a final-name clone never exists without a handle, and the purge skips files that fail an exclusive open or whose owner process (from the file name) is still alive. std::atexit cleanup never ran on the --run path because main ends with ExitProcess; the loader now exposes cleanup_tls_clones() and main calls it before ExitProcess (atexit still covers normal exits). on_host_thread_event wrapped the whole slot loop in one try/catch, so one throwing slot killed the events for every other module. The catch is now per slot, and the detach path releases the slot even when the callbacks threw, instead of leaking the block to the OS loader. README and the initialize_tls comment no longer claim only one mapped TLS module can be live; the loader_tls_test gained purge-ownership and explicit-cleanup checks. --- runtime/README.md | 7 ++- runtime/src/main.cpp | 2 + runtime/src/pe_loader.cpp | 126 ++++++++++++++++++++++++++++++--------- runtime/src/pe_loader.h | 8 ++- runtime/test/loader_tls_test.cpp | 108 +++++++++++++++++++++++++++++++++ 5 files changed, 218 insertions(+), 33 deletions(-) (limited to 'runtime/src/main.cpp') diff --git a/runtime/README.md b/runtime/README.md index 7060f4368..f7e25831e 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -118,9 +118,10 @@ process lifetime and answers as Windows does when no `Loader` is alive. different index space from the `TlsAlloc` slots, and only the OS loader can allocate a module index. The runtime therefore ships a minimal `gta6rt_tls_host.dll`; the loader reuses the index the OS assigns it, writes that index into the image's `AddressOfIndex`, and copies the image's TLS template into the -slot. This reserves one index, so **one manually mapped TLS module can be live at a time**; a second -one is rejected with an error instead of sharing the slot. Images without a TLS directory (or with an -all-zero one) load normally. +slot. This reserves one index; every additional simultaneous TLS module gets its own by mapping a fresh +clone of the helper from `%TEMP%\leonida_tls_host__.dll`, so up to 16 modules can be live at +once and a further one is rejected with an error instead of sharing a slot. Images without a TLS +directory (or with an all-zero one) load normally. Current limits, by design: diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 560f68349..c8875d853 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -129,6 +129,8 @@ int wmain(int argc, wchar_t** argv) { std::fflush(stderr); // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its // handlers would run against a game that has stopped) and whose last word decides the exit status. + // ExitProcess also skips atexit, so the TLS clone cleanup registered there is run here instead. + rt::cleanup_tls_clones(); ExitProcess(static_cast(code)); } catch (const std::exception& e) { std::fprintf(stderr, "error: %s\n", e.what()); diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 113ca23ea..ec87ab7ee 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -123,7 +123,10 @@ std::array, kMaxMappedTlsModules> g_tls_slot_generations{} std::atomic g_next_tls_generation{1}; std::vector g_tls_clones; -void cleanup_tls_clones_at_exit() { +// Frees every TLS helper clone this process loaded and deletes its files. load() registers the wrapper with atexit; +// the --run path in main() must also call this directly before ExitProcess, which skips the atexit table. Call it +// only when no mapped code can run again: it unmaps the helper images. +void cleanup_tls_clones_internal() { for (const auto& rec : g_tls_clones) { if (rec.module) FreeLibrary(rec.module); if (!rec.path.empty()) DeleteFileW(rec.path.c_str()); @@ -131,16 +134,54 @@ void cleanup_tls_clones_at_exit() { g_tls_clones.clear(); } +void cleanup_tls_clones_at_exit() { cleanup_tls_clones_internal(); } + +// The owner process named by "leonida_tls_host__.dll"; 0 when the name does not carry one. +DWORD clone_owner_pid(const wchar_t* name) { + constexpr wchar_t prefix[] = L"leonida_tls_host_"; + const wchar_t* p = name; + for (size_t i = 0; i < sizeof(prefix) / sizeof(wchar_t) - 1; ++i) + if (*p++ != prefix[i]) return 0; + if (*p < L'0' || *p > L'9') return 0; + unsigned long pid = 0; + while (*p >= L'0' && *p <= L'9') { + pid = pid * 10 + (*p - L'0'); + if (pid > 0xFFFFFFFFul) return 0; + ++p; + } + return static_cast(pid); +} + +bool process_alive(DWORD pid) { + // PROCESS_QUERY_LIMITED_INFORMATION needs no handle rights a normal process could deny; a failure means the + // process is gone. A still-mapped or still-open file is caught by the checks below even for a misjudged PID. + HANDLE p = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid); + if (!p) return false; + CloseHandle(p); + return true; +} + +// Deletes clones left behind by processes that have exited. Deletion is scoped to files whose owner is gone: a +// clone of a live process is either still open by its copy step's handle or mapped as an image, so it is skipped +// (or DeleteFileW fails harmlessly), and a concurrent process between CopyFileW and LoadLibraryExW keeps its file. void sweep_stale_tls_clones(const std::filesystem::path& dir) { WIN32_FIND_DATAW fd{}; const std::filesystem::path pattern = dir / L"leonida_tls_host_*.dll"; HANDLE h = FindFirstFileW(pattern.c_str(), &fd); if (h == INVALID_HANDLE_VALUE) return; do { - if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { - // Fails harmlessly for clones still mapped in live processes; deletes orphaned ones from exited processes. - DeleteFileW((dir / fd.cFileName).c_str()); - } + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; + const std::filesystem::path path = dir / fd.cFileName; + const DWORD owner = clone_owner_pid(fd.cFileName); + if (owner && owner != GetCurrentProcessId() && process_alive(owner)) continue; + // An exclusive open (no sharing) conflicts with any handle another process holds on the file, so a failure + // means the copy->load sequence or a mapped image still owns it. Only unowned files reach DeleteFileW, and + // that stays harmless for a mapped image. + HANDLE probe = + CreateFileW(path.c_str(), GENERIC_READ, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + if (probe == INVALID_HANDLE_VALUE) continue; + CloseHandle(probe); + DeleteFileW(path.c_str()); } while (FindNextFileW(h, &fd)); FindClose(h); } @@ -208,13 +249,27 @@ TlsHostModule ensure_tls_host(size_t slot) { const std::wstring name = L"leonida_tls_host_" + std::to_wstring(GetCurrentProcessId()) + L"_" + std::to_wstring(slot) + L".dll"; const std::filesystem::path clone_path = dir / name; - if (!CopyFileW(primary.c_str(), clone_path.c_str(), FALSE)) return {}; - - // Hold a read-only-shared handle across LoadLibraryExW so another process cannot overwrite the clone in between. - HANDLE guard = CreateFileW(clone_path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, nullptr, + // The staging name must not match the sweep pattern, which only covers final clone names ending in .dll. + const std::filesystem::path staging = clone_path.wstring() + L".new"; + if (!CopyFileW(primary.c_str(), staging.c_str(), FALSE)) return {}; + + // Open the guard before the clone exists under its final name and hold it across LoadLibraryExW: the rename + // below needs DELETE access, which the handle's FILE_SHARE_DELETE grants, and no purge can remove the clone + // while the handle is open (an exclusive open conflicts with it) or once the image is mapped. Copying straight + // to the final name would leave a window where a concurrent sweep sees an unowned file. + HANDLE guard = CreateFileW(staging.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + if (guard == INVALID_HANDLE_VALUE) { + DeleteFileW(staging.c_str()); + return {}; + } + if (!MoveFileExW(staging.c_str(), clone_path.c_str(), MOVEFILE_REPLACE_EXISTING)) { + CloseHandle(guard); + DeleteFileW(staging.c_str()); + return {}; + } TlsHostModule loaded = load_tls_host_image(clone_path, false); - if (guard != INVALID_HANDLE_VALUE) CloseHandle(guard); + CloseHandle(guard); if (loaded.index == TLS_OUT_OF_INDEXES) { DeleteFileW(clone_path.c_str()); @@ -350,30 +405,39 @@ void run_callbacks(Module& m, uint32_t reason) { // only after the module's DLL_PROCESS_ATTACH (initialise()), as the Windows loader orders them. void __cdecl on_host_thread_event(DWORD reason) { SharedTlsLock lock; - try { - if (reason == DLL_THREAD_ATTACH) { - for (size_t slot = 0; slot < kMaxMappedTlsModules; ++slot) { - Module* m = g_tls_slot_owners[slot].load(); - if (!m) continue; + // The try/catch is per slot: a throw from one module's install or callbacks must not take the thread events + // away from the other slots. Nothing may leave DllMain. + if (reason == DLL_THREAD_ATTACH) { + for (size_t slot = 0; slot < kMaxMappedTlsModules; ++slot) { + Module* m = g_tls_slot_owners[slot].load(); + if (!m) continue; + try { install_thread_block(*m); if (m->initialised) run_callbacks(*m, DLL_THREAD_ATTACH); + } catch (const std::exception& e) { + // The thread runs on without this module's TLS; later slots still get theirs. + std::fprintf(stderr, "[rt] loader: thread attach, slot %zu (%s): %s\n", slot, m->name.c_str(), + e.what()); } - } else if (reason == DLL_THREAD_DETACH) { - for (size_t i = kMaxMappedTlsModules; i-- > 0;) { - Module* m = g_tls_slot_owners[i].load(); - if (m && g_thread_tls_slots[i].module == m && - g_thread_tls_slots[i].generation == m->tls_generation) { + } + } else if (reason == DLL_THREAD_DETACH) { + for (size_t i = kMaxMappedTlsModules; i-- > 0;) { + Module* m = g_tls_slot_owners[i].load(); + if (m && g_thread_tls_slots[i].module == m && + g_thread_tls_slots[i].generation == m->tls_generation) { + try { if (m->initialised) run_callbacks(*m, DLL_THREAD_DETACH); - uninstall_thread_block(*m); - } else { - release_thread_slot(i); // installed by a module whose Loader is gone (or nothing to do) + } catch (const std::exception& e) { + std::fprintf(stderr, "[rt] loader: thread detach, slot %zu (%s): %s\n", i, m->name.c_str(), + e.what()); } + // Released even when the callbacks threw: a leaked block would be handed to the OS loader as the + // helper's own TLS block and freed with it at thread exit. + uninstall_thread_block(*m); + } else { + release_thread_slot(i); // installed by a module whose Loader is gone (or nothing to do) } } - } catch (const std::exception& e) { - // Nothing may leave DllMain. The thread runs on without the module's TLS (attach) or keeps the copy (detach). - std::fprintf(stderr, "[rt] loader: thread %s: %s\n", reason == DLL_THREAD_ATTACH ? "attach" : "detach", - e.what()); } } @@ -452,6 +516,9 @@ void redirect_pc_to_file_header_at(const void* resolved) { } // namespace +// Declared in pe_loader.h; the body needs the internal clone registry above. +void cleanup_tls_clones() { cleanup_tls_clones_internal(); } + namespace { bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t& rva) { for (const Module* m : static_cast(ctx)->modules()) { @@ -701,8 +768,9 @@ Module& Loader::map_image(const std::filesystem::path& path) { // zero-fill size; it has no field naming the TLS block. The Windows loader allocates a block per module per thread and // puts it in TEB->ThreadLocalStoragePointer[module index], which is what __declspec(thread) code reads via _tls_index. // Only the OS loader can allocate that module index, so the loader reuses the index the OS assigned to -// gta6rt_tls_host.dll. That makes one manually mapped TLS module work; a second one is rejected rather than silently -// sharing (and corrupting) the same slot. +// gta6rt_tls_host.dll and maps a fresh clone of the helper for every additional simultaneous TLS module, each clone +// getting its own index. All kMaxMappedTlsModules slots can be live at once; a further module is rejected rather than +// silently sharing (and corrupting) a slot. void Loader::initialize_tls(Module& m) { auto* tls = tls_directory(m); if (!tls) return; diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h index 42a2719f2..0861259ab 100644 --- a/runtime/src/pe_loader.h +++ b/runtime/src/pe_loader.h @@ -99,7 +99,8 @@ public: // Installs/removes this module's template copy in the calling thread's TEB slot. load() does this for its caller // and the thread events for threads started after it; both are no-ops on a thread that already has this module's // copy. Call install on a thread that existed before load(), before mapped code runs there, and uninstall before - // that thread exits. Only one TLS module can be installed in a thread. These do not run TLS callbacks (use + // that thread exits. Each module's copy sits in its own TEB slot, so one thread can hold copies of several + // modules. These do not run TLS callbacks (use // run_tls_callbacks with DLL_THREAD_ATTACH/DETACH for that). void install_tls_for_thread(Module& m); void uninstall_tls_for_thread(Module& m); @@ -122,6 +123,11 @@ private: std::vector init_order_; // dependencies first, see initialisation_order() }; +// Frees the TLS helper clones load() created and deletes their files (Module::tls_index background). load() +// registers this with atexit; a caller that ends with ExitProcess must invoke it directly, because ExitProcess +// skips the atexit table. Call it when no mapped code can run again: it unmaps the helper images. +void cleanup_tls_clones(); + std::string lower(std::string s); } // namespace rt diff --git a/runtime/test/loader_tls_test.cpp b/runtime/test/loader_tls_test.cpp index 8b1d6d6e1..a4962d4d1 100644 --- a/runtime/test/loader_tls_test.cpp +++ b/runtime/test/loader_tls_test.cpp @@ -492,6 +492,107 @@ void test_orphan_slot_generation_reuse(const fs::path& dll) { check(td() == 0, "orphaned worker exit does not dispatch DLL_THREAD_DETACH to the new generation's module"); } +// Spawns cmd.exe to obtain one guaranteed-dead PID and one guaranteed-live one for the purge-ownership checks. +struct ChildProcess { + HANDLE process = nullptr; + DWORD pid = 0; + explicit ChildProcess(const wchar_t* args) { + STARTUPINFOW si{}; + si.cb = sizeof(si); + PROCESS_INFORMATION pi{}; + std::wstring cmd = std::wstring(L"cmd.exe ") + args; + if (!CreateProcessW(nullptr, cmd.data(), nullptr, nullptr, FALSE, CREATE_NO_WINDOW, nullptr, nullptr, &si, + &pi)) + throw std::runtime_error("CreateProcessW for cmd.exe failed"); + CloseHandle(pi.hThread); + process = pi.hProcess; + pid = pi.dwProcessId; + } + ChildProcess(const ChildProcess&) = delete; + ChildProcess& operator=(const ChildProcess&) = delete; + void wait_exit() { + WaitForSingleObject(process, INFINITE); + CloseHandle(process); + process = nullptr; + } + ~ChildProcess() { + if (process) { + TerminateProcess(process, 0); + WaitForSingleObject(process, INFINITE); + CloseHandle(process); + } + } +}; + +// Clone files live in the temp root (the loader's clone directory), named with this process's PID. +std::vector own_clone_files() { + const std::wstring own = L"leonida_tls_host_" + std::to_wstring(GetCurrentProcessId()) + L"_"; + std::vector found; + WIN32_FIND_DATAW fd{}; + HANDLE h = FindFirstFileW((fs::temp_directory_path() / (own + L"*.dll")).c_str(), &fd); + if (h == INVALID_HANDLE_VALUE) return found; + do { + if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) + found.push_back(fs::temp_directory_path() / fd.cFileName); + } while (FindNextFileW(h, &fd)); + FindClose(h); + return found; +} + +// The startup purge must delete only clones whose owning process is gone and that nothing holds open: a concurrent +// process between its CopyFileW and its LoadLibraryExW must keep its file. Runs before any other clone-creating +// test, because the sweep fires once per process. +void test_purge_ownership_scoping(const fs::path& dll, const fs::path& temp) { + std::printf("\n-- startup purge deletes only ownerless clones --\n"); + ChildProcess dead(L"/c exit 0"); + dead.wait_exit(); + ChildProcess live(L"/c timeout /t 30 /nobreak > nul"); + + const fs::path clone_root = fs::temp_directory_path(); + const fs::path dead_file = clone_root / (L"leonida_tls_host_" + std::to_wstring(dead.pid) + L"_31.dll"); + const fs::path held_file = clone_root / (L"leonida_tls_host_" + std::to_wstring(dead.pid) + L"_32.dll"); + const fs::path live_file = clone_root / (L"leonida_tls_host_" + std::to_wstring(live.pid) + L"_33.dll"); + const char bytes[8] = "purgeXX"; + write_file(dead_file, bytes, 8); + write_file(held_file, bytes, 8); + write_file(live_file, bytes, 8); + // An open handle without FILE_SHARE_DELETE stands in for another process's guard or mapped image: the purge's + // exclusive-open probe must fail against it. + HANDLE held = CreateFileW(held_file.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, nullptr); + check(held != INVALID_HANDLE_VALUE, "opened the held clone stand-in without FILE_SHARE_DELETE"); + + const fs::path second = temp / "tiny_tls_purge_second.dll"; + check(CopyFileW(dll.c_str(), second.c_str(), FALSE) != 0, "copied the TLS fixture for the purge test"); + { + rt::Loader loader(dll.parent_path()); + rt::Module& first = loader.load(dll); // slot 0: primary helper, no clone + rt::Module& mapped_second = loader.load(second); // slot 1: clone; the purge sweep runs here + check(first.tls_index != rt::kNoTlsIndex && mapped_second.tls_index != rt::kNoTlsIndex, + "the purge test loads two simultaneous TLS modules"); + } + const auto clone_of_this_process = own_clone_files(); + check(clone_of_this_process.size() == 1, "this process owns exactly one clone file after the two-module load"); + + const bool dead_pid_still_free = [&] { + HANDLE p = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, dead.pid); + if (!p) return true; + CloseHandle(p); + return false; + }(); + if (dead_pid_still_free) + check(!fs::exists(dead_file), "the purge deleted the clone whose owner process is gone"); + else + std::printf("SKIP dead-owner deletion check: the child's PID was reused meanwhile\n"); + check(fs::exists(held_file), "the purge skipped the clone another handle holds open"); + check(fs::exists(live_file), "the purge skipped the clone whose owner process is still alive"); + + CloseHandle(held); + std::error_code ec; + fs::remove(held_file, ec); + fs::remove(live_file, ec); +} + void print_result() { if (g_failures == 0) std::printf("\nloader_tls_test: OK (%d checks)\n", g_checks); @@ -519,11 +620,18 @@ int main(int argc, char** argv) { } TempDir temp; + test_purge_ownership_scoping(tls_dll, temp.path); // first: the sweep fires once per process test_tls(tls_dll); test_thread_events(tls_dll); test_thread_outlives_loader(tls_dll); test_multiple_tls_modules(tls_dll, temp.path); test_orphan_slot_generation_reuse(tls_dll); + + // The --run path calls cleanup_tls_clones() before ExitProcess because atexit never runs there. + std::printf("\n-- explicit clone cleanup (the ExitProcess path) --\n"); + check(!own_clone_files().empty(), "TLS helper clone files exist before explicit cleanup"); + rt::cleanup_tls_clones(); + check(own_clone_files().empty(), "cleanup_tls_clones deletes every clone file of this process"); } catch (const std::exception& e) { ++g_checks; ++g_failures; -- cgit v1.2.3 From 35afd618e5f58f13012432ad2fc15b1ba9ed7d40 Mon Sep 17 00:00:00 2001 From: Doffi4 <206447286+Doffi4@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:02:32 +0200 Subject: Runtime: preserve Unicode in the console path diagnostic --- runtime/CMakeLists.txt | 3 +++ runtime/src/main.cpp | 17 ++++++++++++++++- runtime/test/check_console_paths.cmake | 10 ++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 runtime/test/check_console_paths.cmake (limited to 'runtime/src/main.cpp') diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 7104d50cd..ef0a91add 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -664,6 +664,9 @@ if(BUILD_TESTING) "-DLOADER=$" "-DGUEST=$" "-DRUNNER=${CMAKE_CROSSCOMPILING_EMULATOR}" -P "${CMAKE_CURRENT_SOURCE_DIR}/test/check_loaded_bundled.cmake") add_test(NAME run_tiny_game COMMAND gta6rt --run $ -- hello 42) + add_test(NAME run_tiny_game_console_paths COMMAND ${CMAKE_COMMAND} + "-DLOADER=$" "-DGUEST=$" + "-DRUNNER=${CMAKE_CROSSCOMPILING_EMULATOR}" -P "${CMAKE_CURRENT_SOURCE_DIR}/test/check_console_paths.cmake") add_test(NAME run_tiny_game_exit_code COMMAND tiny_game_exit_code_test "$" "$") # Without relocations it can only run at 0x140000000 (base_reservation.h), so passing means it got there; the diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 06baae387..6229ad210 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -129,7 +129,22 @@ int wmain(int argc, wchar_t** argv) { return 1; } rt::process::set_command_line(image.filename().wstring(), game_args); - std::printf("console paths: GAME: -> %ls, writes -> %ls\n", game_dir.c_str(), overlay_dir.c_str()); + const std::wstring paths = L"console paths: GAME: -> " + game_dir.native() + L", writes -> " + overlay_dir.native(); + const HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD mode; + bool printed = false; + if (GetConsoleMode(out, &mode)) { + std::fflush(stdout); + const std::wstring line = paths + L"\r\n"; + DWORD written; + printed = WriteConsoleW(out, line.data(), static_cast(line.size()), &written, nullptr) != FALSE; + } + if (!printed) { + const auto game = game_dir.u8string(); + const auto overlay = overlay_dir.u8string(); + std::printf("console paths: GAME: -> %s, writes -> %s\n", + reinterpret_cast(game.c_str()), reinterpret_cast(overlay.c_str())); + } std::printf("running %s\n", main_module.name.c_str()); std::fflush(stdout); const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); diff --git a/runtime/test/check_console_paths.cmake b/runtime/test/check_console_paths.cmake new file mode 100644 index 000000000..bfbfe2a96 --- /dev/null +++ b/runtime/test/check_console_paths.cmake @@ -0,0 +1,10 @@ +# Redirect the loader's stdout so both console paths must survive as UTF-8. +execute_process( + COMMAND ${RUNNER} "${LOADER}" --game-dir "game-Игра" --overlay "overlay-保存" + --run "${GUEST}" -- hello 42 + TIMEOUT 90 ENCODING UTF-8 + RESULT_VARIABLE code OUTPUT_VARIABLE output ERROR_VARIABLE error) +if(NOT code STREQUAL "0" OR + NOT output MATCHES "console paths: GAME: -> [^\n]*game-Игра, writes -> [^\n]*overlay-保存\r?\n") + message(FATAL_ERROR "Runtime did not report both Unicode paths (exit ${code})\n${output}\n${error}") +endif() -- cgit v1.2.3