diff options
Diffstat (limited to 'runtime/src/base_reservation.h')
| -rw-r--r-- | runtime/src/base_reservation.h | 139 |
1 files changed, 139 insertions, 0 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 | ||