diff options
| author | Ash Hopkins <ashleighhopkins87@gmail.com> | 2026-11-22 11:25:05 +0000 |
|---|---|---|
| committer | Ash Hopkins <ashleighhopkins87@gmail.com> | 2026-11-22 12:53:32 +0000 |
| commit | d820ecfa63fa9939209e305d5f94cda59ca7f16f (patch) | |
| tree | 66ef66207632527124aa192dce3552e5f51d8635 /runtime/src | |
| parent | 41f448261e1de8a410b4d760e69a7e5656cef858 (diff) | |
runtime: reserve a relocation-less image's range before arm64 Wine takes it
An EXE without .reloc can only run at its ImageBase (0x140000000 by default; RT-22 asks whether the game's
is one). Under arm64 Wine on macOS (CrossOver's arm64 bottles, x86-64 code under FEX) the process's own
start-up allocations sit at 0x100000000 and up before gta6rt runs, so such an image failed with "preferred
base is taken". gta6rt now starts itself again suspended when the image has no relocations and its range is
not free, reserves [ImageBase, ImageBase + SizeOfImage) in the new process, resumes it and returns its exit
code; the loader commits the image inside that reservation. LEONIDA_BASE_RESERVED tells the new process,
which does not start itself again. Where the range is free at start-up (Windows, x86-64 Wine) nothing
changes.
tiny_game_fixed is the stand-in game linked /FIXED at 0x140000000; run_tiny_game_fixed and
run_tiny_game_fixed_exit_code run it. Arm64 bottle (CrossOver Preview 20260821, M2 Max): it maps at
0x140000000 and both tests pass, --dry-run and --init exit 0; with the relaunch disabled it fails as
before. The Rosetta (x86-64) bottle passes them too. The in-process loader tests that build relocation-less
images (loader, pe_loader_bounds, unimplemented_policy) still fail on arm64.
AI-assisted. No game files.
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/base_reservation.h | 139 | ||||
| -rw-r--r-- | runtime/src/main.cpp | 5 | ||||
| -rw-r--r-- | runtime/src/pe_loader.cpp | 8 |
3 files changed, 150 insertions, 2 deletions
diff --git a/runtime/src/base_reservation.h b/runtime/src/base_reservation.h new file mode 100644 index 000000000..76fb530ce --- /dev/null +++ b/runtime/src/base_reservation.h | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // Keeping a relocation-less game image's preferred range free until the loader maps it there. | ||
| 4 | // | ||
| 5 | // An EXE without a .reloc directory can only run at its ImageBase (0x140000000 by default). Windows and x86-64 Wine | ||
| 6 | // leave that range alone, but arm64 Wine on macOS (CrossOver's arm64 bottles, whose x86-64 code runs under FEX) puts | ||
| 7 | // its own start-up allocations at 0x100000000 and up, over that range, before gta6rt's first line runs. So gta6rt | ||
| 8 | // starts itself again, suspended, reserves [ImageBase, ImageBase + SizeOfImage) in that child before any of its code | ||
| 9 | // runs, and resumes it; the child's loader then commits the image inside the reservation (commit_reserved), so | ||
| 10 | // nothing else can take the range in between. The child is told by LEONIDA_BASE_RESERVED=<base>:<size> (hex), which | ||
| 11 | // also stops it from starting itself again. Measured on CrossOver Preview 20260821 (arm64 bottle, M2 Max): in a | ||
| 12 | // suspended child 0x140000000-0x170000000 is free and reservable; macOS maps its own regions above that (the arm64 | ||
| 13 | // shared region starts at 0x180000000), so a range there can't be had either way. | ||
| 14 | // | ||
| 15 | // Nothing changes where the range is free at start-up (Windows, x86-64 Wine) or where the image has relocations. | ||
| 16 | |||
| 17 | #include <windows.h> | ||
| 18 | |||
| 19 | #include <cstdint> | ||
| 20 | #include <cstdio> | ||
| 21 | #include <cstdlib> | ||
| 22 | #include <cwchar> | ||
| 23 | #include <filesystem> | ||
| 24 | #include <fstream> | ||
| 25 | #include <optional> | ||
| 26 | #include <string> | ||
| 27 | #include <vector> | ||
| 28 | |||
| 29 | namespace rt::base_reservation { | ||
| 30 | |||
| 31 | inline constexpr wchar_t kVariable[] = L"LEONIDA_BASE_RESERVED"; | ||
| 32 | |||
| 33 | struct Range { | ||
| 34 | std::uint64_t base = 0; | ||
| 35 | std::uint64_t size = 0; | ||
| 36 | }; | ||
| 37 | |||
| 38 | // The range this process was started with (LEONIDA_BASE_RESERVED), if any. | ||
| 39 | inline std::optional<Range> inherited() { | ||
| 40 | wchar_t value[64] = {}; | ||
| 41 | const DWORD length = GetEnvironmentVariableW(kVariable, value, 64); | ||
| 42 | if (!length || length >= 64) return std::nullopt; | ||
| 43 | Range r{}; | ||
| 44 | if (std::swscanf(value, L"%llx:%llx", reinterpret_cast<unsigned long long*>(&r.base), | ||
| 45 | reinterpret_cast<unsigned long long*>(&r.size)) != 2 || | ||
| 46 | !r.size) | ||
| 47 | return std::nullopt; | ||
| 48 | return r; | ||
| 49 | } | ||
| 50 | |||
| 51 | // Commits [base, base + size) for the loader when it lies inside the reservation this process was started with and | ||
| 52 | // that reservation is still in place; returns null otherwise (the caller then allocates as usual). The reservation | ||
| 53 | // is the allocation, so VirtualFree(base, 0, MEM_RELEASE) frees it as it frees any image. | ||
| 54 | inline void* commit_reserved(std::uint64_t base, std::uint64_t size) { | ||
| 55 | const auto r = inherited(); | ||
| 56 | if (!r || base != r->base || size > r->size) return nullptr; | ||
| 57 | MEMORY_BASIC_INFORMATION info{}; | ||
| 58 | if (!VirtualQuery(reinterpret_cast<void*>(base), &info, sizeof info) || info.State != MEM_RESERVE || | ||
| 59 | reinterpret_cast<std::uint64_t>(info.AllocationBase) != base) | ||
| 60 | return nullptr; | ||
| 61 | return VirtualAlloc(reinterpret_cast<void*>(base), static_cast<SIZE_T>(size), MEM_COMMIT, PAGE_READWRITE); | ||
| 62 | } | ||
| 63 | |||
| 64 | // The preferred range of `image` when it has no relocations and that range is not free in this process; nullopt | ||
| 65 | // when the image can be mapped as it is (or is not a readable PE32+, which the loader reports itself). | ||
| 66 | inline std::optional<Range> needed(const std::filesystem::path& image) { | ||
| 67 | std::ifstream file(image, std::ios::binary); | ||
| 68 | IMAGE_DOS_HEADER dos{}; | ||
| 69 | if (!file.read(reinterpret_cast<char*>(&dos), sizeof dos) || dos.e_magic != IMAGE_DOS_SIGNATURE || dos.e_lfanew < 0) | ||
| 70 | return std::nullopt; | ||
| 71 | IMAGE_NT_HEADERS64 nt{}; | ||
| 72 | if (!file.seekg(dos.e_lfanew) || !file.read(reinterpret_cast<char*>(&nt), sizeof nt) || | ||
| 73 | nt.Signature != IMAGE_NT_SIGNATURE || nt.OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC || | ||
| 74 | !nt.OptionalHeader.SizeOfImage) | ||
| 75 | return std::nullopt; | ||
| 76 | const auto& oh = nt.OptionalHeader; | ||
| 77 | if (oh.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_BASERELOC && | ||
| 78 | oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size) | ||
| 79 | return std::nullopt; // relocatable: the loader moves it if the base is taken | ||
| 80 | const Range r{oh.ImageBase, (static_cast<std::uint64_t>(oh.SizeOfImage) + 0xffff) & ~std::uint64_t{0xffff}}; | ||
| 81 | for (std::uint64_t at = r.base; at < r.base + r.size;) { | ||
| 82 | MEMORY_BASIC_INFORMATION info{}; | ||
| 83 | if (!VirtualQuery(reinterpret_cast<void*>(at), &info, sizeof info)) return r; | ||
| 84 | if (info.State != MEM_FREE) return r; | ||
| 85 | at = reinterpret_cast<std::uint64_t>(info.BaseAddress) + info.RegionSize; | ||
| 86 | } | ||
| 87 | return std::nullopt; | ||
| 88 | } | ||
| 89 | |||
| 90 | // Runs this program again with the same command line, suspended, reserves `r` in the new process, resumes it and | ||
| 91 | // returns its exit code. On failure prints why and returns 1 (the child, if any, is ended unrun). | ||
| 92 | inline int relaunch(const Range& r) { | ||
| 93 | wchar_t self[MAX_PATH * 4] = {}; | ||
| 94 | if (!GetModuleFileNameW(nullptr, self, MAX_PATH * 4)) { | ||
| 95 | std::fprintf(stderr, "error: GetModuleFileName failed (%lu)\n", GetLastError()); | ||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | wchar_t marker[64]; | ||
| 99 | std::swprintf(marker, 64, L"%llx:%llx", static_cast<unsigned long long>(r.base), | ||
| 100 | static_cast<unsigned long long>(r.size)); | ||
| 101 | SetEnvironmentVariableW(kVariable, marker); // inherited by the child; this process only waits from here on | ||
| 102 | std::wstring command_line = GetCommandLineW(); | ||
| 103 | STARTUPINFOW startup{sizeof startup}; | ||
| 104 | startup.dwFlags = STARTF_USESTDHANDLES; | ||
| 105 | startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | ||
| 106 | startup.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); | ||
| 107 | startup.hStdError = GetStdHandle(STD_ERROR_HANDLE); | ||
| 108 | for (HANDLE h : {startup.hStdInput, startup.hStdOutput, startup.hStdError}) | ||
| 109 | if (h && h != INVALID_HANDLE_VALUE) SetHandleInformation(h, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); | ||
| 110 | PROCESS_INFORMATION process{}; | ||
| 111 | std::fflush(stdout); | ||
| 112 | std::fflush(stderr); | ||
| 113 | if (!CreateProcessW(self, command_line.data(), nullptr, nullptr, TRUE, CREATE_SUSPENDED, nullptr, nullptr, | ||
| 114 | &startup, &process)) { | ||
| 115 | std::fprintf(stderr, "error: starting gta6rt again to reserve the image range failed (%lu)\n", GetLastError()); | ||
| 116 | return 1; | ||
| 117 | } | ||
| 118 | if (!VirtualAllocEx(process.hProcess, reinterpret_cast<void*>(r.base), static_cast<SIZE_T>(r.size), MEM_RESERVE, | ||
| 119 | PAGE_NOACCESS)) { | ||
| 120 | const DWORD error = GetLastError(); | ||
| 121 | TerminateProcess(process.hProcess, 1); | ||
| 122 | CloseHandle(process.hThread); | ||
| 123 | CloseHandle(process.hProcess); | ||
| 124 | std::fprintf(stderr, | ||
| 125 | "error: the image has no relocations and its range %#llx-%#llx is taken, also in a new " | ||
| 126 | "process (VirtualAllocEx %lu)\n", | ||
| 127 | static_cast<unsigned long long>(r.base), static_cast<unsigned long long>(r.base + r.size), error); | ||
| 128 | return 1; | ||
| 129 | } | ||
| 130 | ResumeThread(process.hThread); | ||
| 131 | WaitForSingleObject(process.hProcess, INFINITE); | ||
| 132 | DWORD code = 1; | ||
| 133 | GetExitCodeProcess(process.hProcess, &code); | ||
| 134 | CloseHandle(process.hThread); | ||
| 135 | CloseHandle(process.hProcess); | ||
| 136 | return static_cast<int>(code); | ||
| 137 | } | ||
| 138 | |||
| 139 | } // namespace rt::base_reservation | ||
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 9ceccf53a..475ae6ec7 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp | |||
| @@ -20,6 +20,7 @@ | |||
| 20 | #include <string> | 20 | #include <string> |
| 21 | #include <vector> | 21 | #include <vector> |
| 22 | 22 | ||
| 23 | #include "base_reservation.h" | ||
| 23 | #include "pe_loader.h" | 24 | #include "pe_loader.h" |
| 24 | #include "../shims/d3d12_trace.h" | 25 | #include "../shims/d3d12_trace.h" |
| 25 | #include "../shims/process_shim.h" | 26 | #include "../shims/process_shim.h" |
| @@ -68,6 +69,10 @@ int wmain(int argc, wchar_t** argv) { | |||
| 68 | // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. | 69 | // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. |
| 69 | if (dry_run + init + run != 1) return usage(); | 70 | if (dry_run + init + run != 1) return usage(); |
| 70 | if (game_dir.empty()) game_dir = image.parent_path(); | 71 | if (game_dir.empty()) game_dir = image.parent_path(); |
| 72 | // An image without relocations whose range this process already uses (arm64 Wine on macOS) runs in a new | ||
| 73 | // process that has the range reserved from the start (base_reservation.h); that process does not do this again. | ||
| 74 | if (!rt::base_reservation::inherited()) | ||
| 75 | if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range); | ||
| 71 | rt::d3d12_trace::set_enabled(trace_d3d12); | 76 | rt::d3d12_trace::set_enabled(trace_d3d12); |
| 72 | 77 | ||
| 73 | try { | 78 | try { |
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 1ab9c7801..387051756 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -14,6 +14,7 @@ | |||
| 14 | #include <vector> | 14 | #include <vector> |
| 15 | 15 | ||
| 16 | #include "../shims/shims.h" | 16 | #include "../shims/shims.h" |
| 17 | #include "base_reservation.h" | ||
| 17 | 18 | ||
| 18 | namespace rt { | 19 | namespace rt { |
| 19 | 20 | ||
| @@ -506,8 +507,11 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 506 | by_name_[lower(m->name)] = m; | 507 | by_name_[lower(m->name)] = m; |
| 507 | order_.push_back(m); | 508 | order_.push_back(m); |
| 508 | 509 | ||
| 509 | m->base = static_cast<uint8_t*>( | 510 | // Inside the range gta6rt reserved for this image before the process started (base_reservation.h), else as usual. |
| 510 | VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 511 | m->base = static_cast<uint8_t*>(base_reservation::commit_reserved(oh.ImageBase, oh.SizeOfImage)); |
| 512 | if (!m->base) | ||
| 513 | m->base = static_cast<uint8_t*>(VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, | ||
| 514 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | ||
| 511 | if (!m->base) { | 515 | if (!m->base) { |
| 512 | const auto relocs = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); | 516 | const auto relocs = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); |
| 513 | if (!relocs.Size) | 517 | if (!relocs.Size) |