diff options
| author | marquezini <felipemarquesini10@gmail.com> | 2026-11-22 05:50:20 -0300 |
|---|---|---|
| committer | marquezini <felipemarquesini10@gmail.com> | 2026-11-22 05:50:20 -0300 |
| commit | 68a4073154ad1a5ab2ba8bafc3d48c48f47459f4 (patch) | |
| tree | effceec1bcc694145da8896aef3438567147ba8c /runtime/src/pe_loader.cpp | |
| parent | 2aed14455052d973f53a3a986fa2fad77f81b659 (diff) | |
| parent | 0d61c357656d92334eef97facec1ae4f7b2b60b1 (diff) | |
Merge main (0d61c35) into runtime/compute-path
Resolved along the lines in #291 (comment by ItsNotRexed, 2026-11-22 08:27):
- Default-heap buffers stay in the GPU address arena: a VA is an arena offset,
and every buffer's device address is heap_base + VA (upload and readback
buffers: device address minus heap_base, as before).
- Acceleration structures: a buffer created in RAYTRACING_ACCELERATION_STRUCTURE
state on a ray-tracing device stays out of the arena and keeps main's model,
its VA being the VkAccelerationStructureKHR's device address, so BLAS
addresses in TLAS instance descriptions are what Vulkan reads. It is
registered by that VA. BuildRaytracingAccelerationStructure turns build
inputs and scratch into heap_base + VA, and the arena gets the build-input
usage on ray-tracing devices. Resource keeps one va_ (gpu_va_/buffer_va_
removed).
- buffer_at keeps the size& signature; main's only caller (WriteBufferImmediate)
is the branch's version, and ExecuteIndirect resolves its buffers by id.
- ~Resource keeps arena_release and main's defer_destroy.
- Main's split context locks: pipeline registration and heap_base/zero page
take resource_mutex_ (prepare looks pipelines up under its shared lock);
VulkanPresenter takes resource_mutex_ shared, plus tracker_mutex_ while it
records the back buffer's transition.
- Standalone device: main's single instance-extension list and DXGI swapchain
enable; info.presentation follows the same swapchain/surface condition, so
create_window_presenter keeps working. Main's DeferredDeletionQueue,
calibrated timestamps and ray-tracing extensions are kept next to the
branch's presentation and feature enables.
- Command IR: Dispatch and Draw follow main's DebugMarker, ExecuteIndirect and
BuildAccelerationStructure in the variant.
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 58 |
1 files changed, 47 insertions, 11 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index ef6fab7a8..15a62d9b4 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -47,6 +47,11 @@ std::string read_string(const std::string& name, uint8_t* base, uint32_t image_s | |||
| 47 | return {s, end}; | 47 | return {s, end}; |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | // PE/COFF: NumberOfRvaAndSizes counts the data directories; entries past it are not directories, whatever they hold. | ||
| 51 | IMAGE_DATA_DIRECTORY directory(const IMAGE_NT_HEADERS64* nt, unsigned index) { | ||
| 52 | return index < nt->OptionalHeader.NumberOfRvaAndSizes ? nt->OptionalHeader.DataDirectory[index] : IMAGE_DATA_DIRECTORY{}; | ||
| 53 | } | ||
| 54 | |||
| 50 | // The TLS directory holds virtual addresses, which relocations have already moved to this mapping. | 55 | // The TLS directory holds virtual addresses, which relocations have already moved to this mapping. |
| 51 | uint64_t rva_in(const Module& m, uint64_t va, const char* what) { | 56 | uint64_t rva_in(const Module& m, uint64_t va, const char* what) { |
| 52 | const uint64_t base = reinterpret_cast<uint64_t>(m.base); | 57 | const uint64_t base = reinterpret_cast<uint64_t>(m.base); |
| @@ -77,9 +82,20 @@ struct TlsHostModule { | |||
| 77 | 82 | ||
| 78 | TlsHostModule load_tls_host() { | 83 | TlsHostModule load_tls_host() { |
| 79 | TlsHostModule host; | 84 | TlsHostModule host; |
| 80 | wchar_t exe[MAX_PATH] = {}; | 85 | // Dynamic buffer loop for paths that exceed MAX_PATH |
| 81 | if (!GetModuleFileNameW(nullptr, exe, MAX_PATH)) return host; | 86 | std::wstring exe_path; |
| 82 | const std::filesystem::path path = std::filesystem::path(exe).parent_path() / L"gta6rt_tls_host.dll"; | 87 | DWORD size = MAX_PATH; |
| 88 | while (true) { | ||
| 89 | exe_path.resize(size); | ||
| 90 | DWORD len = GetModuleFileNameW(nullptr, exe_path.data(), size); | ||
| 91 | if (len == 0) return host; | ||
| 92 | if (len < size - 1) { | ||
| 93 | exe_path.resize(len); | ||
| 94 | break; | ||
| 95 | } | ||
| 96 | size *= 2; | ||
| 97 | } | ||
| 98 | const std::filesystem::path path = std::filesystem::path(exe_path).parent_path() / L"gta6rt_tls_host.dll"; | ||
| 83 | // The helper is linked with the static CRT (/MT), so it needs only system DLLs. Restricting the dependency search | 99 | // The helper is linked with the static CRT (/MT), so it needs only system DLLs. Restricting the dependency search |
| 84 | // to System32 keeps a game-supplied vcruntime next to the executable from being picked up. | 100 | // to System32 keeps a game-supplied vcruntime next to the executable from being picked up. |
| 85 | host.module = LoadLibraryExW(path.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); | 101 | host.module = LoadLibraryExW(path.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); |
| @@ -150,6 +166,26 @@ Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) | |||
| 150 | } | 166 | } |
| 151 | 167 | ||
| 152 | Loader::~Loader() { | 168 | Loader::~Loader() { |
| 169 | // Importers must release their resources before their dependencies. Keep every mapping, unwind table and TLS | ||
| 170 | // block alive throughout the notifications: a detach handler can still call an imported function or free an | ||
| 171 | // FLS slot whose destructor lives in another mapped image. | ||
| 172 | for (auto it = init_order_.rbegin(); it != init_order_.rend(); ++it) { | ||
| 173 | Module& m = **it; | ||
| 174 | if (!m.initialised) continue; // a map-only load never opts in to running image code | ||
| 175 | m.initialised = false; | ||
| 176 | try { | ||
| 177 | run_tls_callbacks(m, DLL_PROCESS_DETACH); | ||
| 178 | } catch (const std::exception& e) { | ||
| 179 | // A module may have changed its callback table since attach. A destructor cannot propagate the validation | ||
| 180 | // error; report it and still let DllMain release resources before the image is freed. | ||
| 181 | std::fprintf(stderr, "[rt] TLS process detach failed for %s: %s\n", m.name.c_str(), e.what()); | ||
| 182 | } | ||
| 183 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | ||
| 184 | if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) { | ||
| 185 | auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva); | ||
| 186 | entry(m.base, DLL_PROCESS_DETACH, nullptr); // explicit unload; the return value is ignored | ||
| 187 | } | ||
| 188 | } | ||
| 153 | shims::remove_module_lookup(this); | 189 | shims::remove_module_lookup(this); |
| 154 | for (auto* m : order_) { | 190 | for (auto* m : order_) { |
| 155 | // TLS state must go away before the image and its TLS block are unmapped. | 191 | // TLS state must go away before the image and its TLS block are unmapped. |
| @@ -190,8 +226,7 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 190 | // allowed. No entry in that set uses the indirect form (UnwindData bit 0). | 226 | // allowed. No entry in that set uses the indirect form (UnwindData bit 0). |
| 191 | void Loader::register_unwind(Module& m) { | 227 | void Loader::register_unwind(Module& m) { |
| 192 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 228 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); |
| 193 | if (nt->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_EXCEPTION) return; | 229 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_EXCEPTION); |
| 194 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION]; | ||
| 195 | // Like the system loader, a trailing partial entry is ignored. | 230 | // Like the system loader, a trailing partial entry is ignored. |
| 196 | const uint32_t count = dir.Size / sizeof(RUNTIME_FUNCTION); | 231 | const uint32_t count = dir.Size / sizeof(RUNTIME_FUNCTION); |
| 197 | if (!count) return; | 232 | if (!count) return; |
| @@ -276,7 +311,7 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 276 | m->base = static_cast<uint8_t*>( | 311 | m->base = static_cast<uint8_t*>( |
| 277 | VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 312 | VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| 278 | if (!m->base) { | 313 | if (!m->base) { |
| 279 | auto& relocs = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; | 314 | const auto relocs = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); |
| 280 | if (!relocs.Size) | 315 | if (!relocs.Size) |
| 281 | throw std::runtime_error(m->name + ": preferred base is taken and the image has no relocations"); | 316 | throw std::runtime_error(m->name + ": preferred base is taken and the image has no relocations"); |
| 282 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 317 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| @@ -297,7 +332,7 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 297 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); | 332 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); |
| 298 | if (delta) { | 333 | if (delta) { |
| 299 | m->relocated = true; | 334 | m->relocated = true; |
| 300 | auto& dir = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; | 335 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); |
| 301 | if (!dir.Size) throw std::runtime_error(m->name + ": the image has no relocations and isn't at its base"); | 336 | if (!dir.Size) throw std::runtime_error(m->name + ": the image has no relocations and isn't at its base"); |
| 302 | require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); | 337 | require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); |
| 303 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); | 338 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); |
| @@ -455,7 +490,7 @@ void Loader::run_tls_callbacks(Module& m, uint32_t reason) { | |||
| 455 | 490 | ||
| 456 | void Loader::parse_exports(Module& m) { | 491 | void Loader::parse_exports(Module& m) { |
| 457 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 492 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); |
| 458 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; | 493 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_EXPORT); |
| 459 | if (!dir.Size) return; | 494 | if (!dir.Size) return; |
| 460 | require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_EXPORT_DIRECTORY), m.size, "the export directory"); | 495 | require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_EXPORT_DIRECTORY), m.size, "the export directory"); |
| 461 | auto* ed = at<IMAGE_EXPORT_DIRECTORY>(m.base, dir.VirtualAddress); | 496 | auto* ed = at<IMAGE_EXPORT_DIRECTORY>(m.base, dir.VirtualAddress); |
| @@ -509,8 +544,9 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d | |||
| 509 | uint32_t ordinal = 0; | 544 | uint32_t ordinal = 0; |
| 510 | if (by_ordinal) { | 545 | if (by_ordinal) { |
| 511 | for (size_t i = 1; i < symbol.size(); ++i) { | 546 | for (size_t i = 1; i < symbol.size(); ++i) { |
| 512 | if (symbol[i] < '0' || symbol[i] > '9' || ordinal > 0xFFFF) return nullptr; | 547 | if (symbol[i] < '0' || symbol[i] > '9') return nullptr; |
| 513 | ordinal = ordinal * 10 + static_cast<uint32_t>(symbol[i] - '0'); | 548 | ordinal = ordinal * 10 + static_cast<uint32_t>(symbol[i] - '0'); |
| 549 | if (ordinal > 0xFFFF) return nullptr; // checked after the digit, so "#65537" can't truncate to 1 | ||
| 514 | } | 550 | } |
| 515 | } | 551 | } |
| 516 | 552 | ||
| @@ -595,7 +631,7 @@ void Loader::bind_imports(Module& m) { | |||
| 595 | } | 631 | } |
| 596 | }; | 632 | }; |
| 597 | 633 | ||
| 598 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; | 634 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_IMPORT); |
| 599 | if (dir.Size) { | 635 | if (dir.Size) { |
| 600 | for (uint64_t i = 0;; ++i) { | 636 | for (uint64_t i = 0;; ++i) { |
| 601 | uint64_t rva = dir.VirtualAddress + i * sizeof(IMAGE_IMPORT_DESCRIPTOR); | 637 | uint64_t rva = dir.VirtualAddress + i * sizeof(IMAGE_IMPORT_DESCRIPTOR); |
| @@ -608,7 +644,7 @@ void Loader::bind_imports(Module& m) { | |||
| 608 | } | 644 | } |
| 609 | } | 645 | } |
| 610 | // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. | 646 | // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. |
| 611 | auto& ddir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT]; | 647 | const auto ddir = directory(nt, IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT); |
| 612 | if (ddir.Size) { | 648 | if (ddir.Size) { |
| 613 | for (uint64_t i = 0;; ++i) { | 649 | for (uint64_t i = 0;; ++i) { |
| 614 | uint64_t rva = ddir.VirtualAddress + i * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR); | 650 | uint64_t rva = ddir.VirtualAddress + i * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR); |