upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
authorHiiva <32042291+Hiiva@users.noreply.github.com>2026-11-23 16:23:32 +0200
committerGitHub <noreply@github.com>2026-11-23 08:23:32 -0600
commit3092fdfc4b5c2b75ad82ad6485c74a24830b8cb3 (patch)
treeada716c971abd9ed12d083680a0abd36e5227a81 /runtime/src/pe_loader.cpp
parent14b6deeb5817dce22085014222d8b6a10f352420 (diff)
runtime: preserve Unicode module basenames (#6287)
Squash-merged after diff review of PR #6287 - BUILD-2035: load mapped images with Unicode basenames
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp70
1 files changed, 56 insertions, 14 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index e8c504f0f..bfeab4c7f 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -13,6 +13,7 @@
13#include <fstream> 13#include <fstream>
14#include <mutex> 14#include <mutex>
15#include <stdexcept> 15#include <stdexcept>
16#include <system_error>
16#include <vector> 17#include <vector>
17 18
18#include "../shims/shims.h" 19#include "../shims/shims.h"
@@ -28,9 +29,36 @@ std::string lower(std::string s) {
28 29
29namespace { 30namespace {
30 31
32std::filesystem::path::string_type module_key(const std::filesystem::path& path) {
33 // Keep identity in the platform's native path representation. Only the existing ASCII case folding is
34 // applied here; representable names also retain their old narrow-byte alias below.
35 auto key = path.filename().native();
36 for (auto& c : key)
37 if (c >= 'A' && c <= 'Z') c = static_cast<std::filesystem::path::value_type>(c + ('a' - 'A'));
38 return key;
39}
40
41std::string narrow_module_key(const std::filesystem::path& path) {
42 const auto filename = path.filename();
43 try {
44 const auto narrow = filename.string();
45 // A lossy/default-character alias must not collapse two distinct Unicode basenames.
46 if (std::filesystem::path(narrow).native() == filename.native()) return lower(narrow);
47 } catch (const std::system_error&) {
48 // Some Windows standard libraries throw when a basename is outside the current narrow code page.
49 // It still has a native identity; it simply has no alias in the existing narrow lookup API.
50 }
51 return {};
52}
53
54std::string path_utf8(const std::filesystem::path& path) {
55 const auto bytes = path.u8string();
56 return {reinterpret_cast<const char*>(bytes.data()), bytes.size()};
57}
58
31std::vector<uint8_t> read_file(const std::filesystem::path& p) { 59std::vector<uint8_t> read_file(const std::filesystem::path& p) {
32 std::ifstream f(p, std::ios::binary); 60 std::ifstream f(p, std::ios::binary);
33 if (!f) throw std::runtime_error("cannot open " + p.string()); 61 if (!f) throw std::runtime_error("cannot open " + path_utf8(p));
34 return {std::istreambuf_iterator<char>(f), {}}; 62 return {std::istreambuf_iterator<char>(f), {}};
35} 63}
36 64
@@ -930,11 +958,13 @@ Loader::~Loader() {
930 958
931Module& Loader::load(const std::filesystem::path& image) { 959Module& Loader::load(const std::filesystem::path& image) {
932 std::lock_guard lock(mutex_); 960 std::lock_guard lock(mutex_);
933 std::string key = lower(image.filename().string()); 961 const auto key = module_key(image);
934 if (auto it = by_name_.find(key); it != by_name_.end()) return *it->second; 962 if (auto it = by_name_.find(key); it != by_name_.end()) return *it->second;
963 const auto narrow_key = narrow_module_key(image);
964 if (Module* existing = find_narrow_module(narrow_key)) return *existing;
935 Module* mapped = nullptr; 965 Module* mapped = nullptr;
936 try { 966 try {
937 mapped = &map_image(image); 967 mapped = &map_image(image, narrow_key);
938 bind_imports(*mapped); // loads every bundled module m imports first, so they land in init_order_ before m 968 bind_imports(*mapped); // loads every bundled module m imports first, so they land in init_order_ before m
939 protect_sections(*mapped); 969 protect_sections(*mapped);
940 register_unwind(*mapped); 970 register_unwind(*mapped);
@@ -954,10 +984,19 @@ Module& Loader::load(const std::filesystem::path& image) {
954 return m; 984 return m;
955} 985}
956 986
987Module* Loader::find_narrow_module(const std::string& dll_lower) const {
988 if (dll_lower.empty()) return nullptr;
989 // library_loader and PE import names keep their existing narrow-byte contract. Do not reinterpret them
990 // as UTF-8 or widen already-folded bytes (which could be part of a multibyte code-page character).
991 for (const auto& entry : by_name_)
992 if (entry.second->narrow_key == dll_lower) return entry.second;
993 return nullptr;
994}
995
957void* Loader::find_loaded_module(const std::string& dll_lower) const { 996void* Loader::find_loaded_module(const std::string& dll_lower) const {
958 std::lock_guard lock(mutex_); 997 std::lock_guard lock(mutex_);
959 auto it = by_name_.find(dll_lower); 998 Module* m = find_narrow_module(dll_lower);
960 return it == by_name_.end() ? nullptr : static_cast<void*>(it->second->base); 999 return m ? static_cast<void*>(m->base) : nullptr;
961} 1000}
962 1001
963void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, bool& handled) { 1002void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, bool& handled) {
@@ -965,10 +1004,10 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags,
965 if (dll_lower.empty() || prefer_host_copy(dll_lower)) return nullptr; 1004 if (dll_lower.empty() || prefer_host_copy(dll_lower)) return nullptr;
966 1005
967 std::lock_guard lock(mutex_); 1006 std::lock_guard lock(mutex_);
968 const auto existing = by_name_.find(dll_lower); 1007 Module* existing = find_narrow_module(dll_lower);
969 const std::filesystem::path candidate = game_dir_ / dll_lower; 1008 const std::filesystem::path candidate = game_dir_ / dll_lower;
970 std::error_code ec; 1009 std::error_code ec;
971 const bool is_bundled = existing != by_name_.end() || std::filesystem::is_regular_file(candidate, ec); 1010 const bool is_bundled = existing || std::filesystem::is_regular_file(candidate, ec);
972 if (!is_bundled) return nullptr; 1011 if (!is_bundled) return nullptr;
973 1012
974 handled = true; 1013 handled = true;
@@ -982,10 +1021,10 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags,
982 return nullptr; 1021 return nullptr;
983 } 1022 }
984 1023
985 if (existing != by_name_.end()) { 1024 if (existing) {
986 try { 1025 try {
987 initialise(); 1026 initialise();
988 return static_cast<void*>(existing->second->base); 1027 return static_cast<void*>(existing->base);
989 } catch (const std::exception& e) { 1028 } catch (const std::exception& e) {
990 std::fprintf(stderr, "[rt] loader: run-time initialise of %s failed: %s\n", dll_lower.c_str(), e.what()); 1029 std::fprintf(stderr, "[rt] loader: run-time initialise of %s failed: %s\n", dll_lower.c_str(), e.what());
991 SetLastError(ERROR_DLL_INIT_FAILED); 1030 SetLastError(ERROR_DLL_INIT_FAILED);
@@ -993,15 +1032,17 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags,
993 } 1032 }
994 } 1033 }
995 1034
1035 Module* mapped = nullptr;
996 try { 1036 try {
997 Module& m = load(candidate); 1037 Module& m = load(candidate);
1038 mapped = &m;
998 initialise(); 1039 initialise();
999 std::fprintf(stderr, "[rt] loader: %s mapped from game_dir (run-time load)\n", m.name.c_str()); 1040 std::fprintf(stderr, "[rt] loader: %s mapped from game_dir (run-time load)\n", m.name.c_str());
1000 return static_cast<void*>(m.base); 1041 return static_cast<void*>(m.base);
1001 } catch (const std::exception& e) { 1042 } catch (const std::exception& e) {
1002 // load() drops a module that failed to map or bind (dependencies included); this also drops one whose 1043 // load() drops a module that failed to map or bind (dependencies included); this also drops one whose
1003 // initialise() failed, so later GetModuleHandle/LoadLibrary calls don't see it. ~Loader frees it via order_. 1044 // initialise() failed, so later GetModuleHandle/LoadLibrary calls don't see it. ~Loader frees it via order_.
1004 by_name_.erase(dll_lower); 1045 by_name_.erase(module_key(mapped ? mapped->path : candidate));
1005 std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", dll_lower.c_str(), e.what()); 1046 std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", dll_lower.c_str(), e.what());
1006 SetLastError(ERROR_DLL_INIT_FAILED); 1047 SetLastError(ERROR_DLL_INIT_FAILED);
1007 return nullptr; 1048 return nullptr;
@@ -1129,12 +1170,12 @@ void Loader::set_initialised(Module& m, bool value) {
1129 m.initialised = value; 1170 m.initialised = value;
1130} 1171}
1131 1172
1132Module& Loader::map_image(const std::filesystem::path& path) { 1173Module& Loader::map_image(const std::filesystem::path& path, const std::string& narrow_key) {
1133 auto file = read_file(path); 1174 auto file = read_file(path);
1134 const std::string name = path.filename().string(); 1175 const std::string name = path_utf8(path.filename());
1135 auto* dos = reinterpret_cast<IMAGE_DOS_HEADER*>(file.data()); 1176 auto* dos = reinterpret_cast<IMAGE_DOS_HEADER*>(file.data());
1136 if (file.size() < sizeof(IMAGE_DOS_HEADER) || dos->e_magic != IMAGE_DOS_SIGNATURE) 1177 if (file.size() < sizeof(IMAGE_DOS_HEADER) || dos->e_magic != IMAGE_DOS_SIGNATURE)
1137 throw std::runtime_error(path.string() + ": not an MZ image"); 1178 throw std::runtime_error(path_utf8(path) + ": not an MZ image");
1138 if (dos->e_lfanew < 0 || static_cast<uint64_t>(dos->e_lfanew) + sizeof(IMAGE_NT_HEADERS64) > file.size()) 1179 if (dos->e_lfanew < 0 || static_cast<uint64_t>(dos->e_lfanew) + sizeof(IMAGE_NT_HEADERS64) > file.size())
1139 throw std::runtime_error(name + ": the NT headers are outside the file"); 1180 throw std::runtime_error(name + ": the NT headers are outside the file");
1140 auto* nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(file.data() + dos->e_lfanew); 1181 auto* nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(file.data() + dos->e_lfanew);
@@ -1158,11 +1199,12 @@ Module& Loader::map_image(const std::filesystem::path& path) {
1158 1199
1159 auto* m = new Module; 1200 auto* m = new Module;
1160 m->name = name; 1201 m->name = name;
1202 m->narrow_key = narrow_key;
1161 m->path = path; 1203 m->path = path;
1162 m->preferred_base = oh.ImageBase; 1204 m->preferred_base = oh.ImageBase;
1163 m->size = oh.SizeOfImage; 1205 m->size = oh.SizeOfImage;
1164 m->entry_rva = oh.AddressOfEntryPoint; 1206 m->entry_rva = oh.AddressOfEntryPoint;
1165 by_name_[lower(m->name)] = m; 1207 by_name_[module_key(path)] = m;
1166 order_.push_back(m); 1208 order_.push_back(m);
1167 1209
1168 // Inside the range gta6rt reserved for this image before the process started (base_reservation.h), else as usual. 1210 // Inside the range gta6rt reserved for this image before the process started (base_reservation.h), else as usual.