diff options
| author | mariuslozo-boop <254631011+mariuslozo-boop@users.noreply.github.com> | 2026-11-23 18:39:59 +0100 |
|---|---|---|
| committer | mariuslozo-boop <254631011+mariuslozo-boop@users.noreply.github.com> | 2026-11-23 18:39:59 +0100 |
| commit | 39479dfa6c0626c2aaf60031fce2216a3fe2a5df (patch) | |
| tree | 77ab5d698ffded81592c3eedc7d70917bb8b6d65 /runtime/src/pe_loader.cpp | |
| parent | b2902830d48ee44530f8f3be113ec477b49da1ff (diff) | |
| parent | dde82f27e994337094afc476ded0b1426f6e87e2 (diff) | |
Merge remote-tracking branch 'origin/main' into rt/exe-tls-slot0
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 82 |
1 files changed, 77 insertions, 5 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 45f3fc671..78c0c394a 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -51,6 +51,24 @@ std::string narrow_module_key(const std::filesystem::path& path) { | |||
| 51 | return {}; | 51 | return {}; |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | // Windows matches module names case-insensitively with its own upcase table (RtlEqualUnicodeString); the | ||
| 55 | // ordinal ignore-case compare is that table. | ||
| 56 | bool same_module_name(const wchar_t* a, const wchar_t* b) { | ||
| 57 | return CompareStringOrdinal(a, -1, b, -1, TRUE) == CSTR_EQUAL; | ||
| 58 | } | ||
| 59 | |||
| 60 | // A narrow (ANSI code page) module name widened for a case-insensitive compare; empty when it is not valid in the | ||
| 61 | // code page. | ||
| 62 | std::wstring acp_to_wide(const std::string& narrow) { | ||
| 63 | if (narrow.empty()) return {}; | ||
| 64 | const int n = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, narrow.data(), static_cast<int>(narrow.size()), | ||
| 65 | nullptr, 0); | ||
| 66 | if (n <= 0) return {}; | ||
| 67 | std::wstring wide(static_cast<size_t>(n), L'\0'); | ||
| 68 | MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, narrow.data(), static_cast<int>(narrow.size()), wide.data(), n); | ||
| 69 | return wide; | ||
| 70 | } | ||
| 71 | |||
| 54 | std::string path_utf8(const std::filesystem::path& path) { | 72 | std::string path_utf8(const std::filesystem::path& path) { |
| 55 | const auto bytes = path.u8string(); | 73 | const auto bytes = path.u8string(); |
| 56 | return {reinterpret_cast<const char*>(bytes.data()), bytes.size()}; | 74 | return {reinterpret_cast<const char*>(bytes.data()), bytes.size()}; |
| @@ -1191,11 +1209,21 @@ bool bundled_owns_handle(void* ctx, void* module) { | |||
| 1191 | return static_cast<Loader*>(ctx)->owns_module_handle(module); | 1209 | return static_cast<Loader*>(ctx)->owns_module_handle(module); |
| 1192 | } | 1210 | } |
| 1193 | 1211 | ||
| 1212 | void* bundled_find_loaded_w(void* ctx, const std::wstring& basename) { | ||
| 1213 | return static_cast<Loader*>(ctx)->find_loaded_module_w(basename); | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | void* bundled_load_w(void* ctx, const std::wstring& basename, uint32_t flags, bool& handled) { | ||
| 1217 | return static_cast<Loader*>(ctx)->load_runtime_module_w(basename, flags, handled); | ||
| 1218 | } | ||
| 1219 | |||
| 1194 | const shims::BundledModuleOps kBundledOps{ | 1220 | const shims::BundledModuleOps kBundledOps{ |
| 1195 | &bundled_find_loaded, | 1221 | &bundled_find_loaded, |
| 1196 | &bundled_load, | 1222 | &bundled_load, |
| 1197 | &bundled_get_proc, | 1223 | &bundled_get_proc, |
| 1198 | &bundled_owns_handle, | 1224 | &bundled_owns_handle, |
| 1225 | &bundled_find_loaded_w, | ||
| 1226 | &bundled_load_w, | ||
| 1199 | }; | 1227 | }; |
| 1200 | 1228 | ||
| 1201 | // Marks what a run-time load maps as a dynamic load for as long as it is in scope (the caller holds the mutex). | 1229 | // Marks what a run-time load maps as a dynamic load for as long as it is in scope (the caller holds the mutex). |
| @@ -1289,13 +1317,35 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 1289 | 1317 | ||
| 1290 | Module* Loader::find_narrow_module(const std::string& dll_lower) const { | 1318 | Module* Loader::find_narrow_module(const std::string& dll_lower) const { |
| 1291 | if (dll_lower.empty()) return nullptr; | 1319 | if (dll_lower.empty()) return nullptr; |
| 1292 | // library_loader and PE import names keep their existing narrow-byte contract. Do not reinterpret them | 1320 | // library_loader and PE import names keep their existing narrow-byte contract: the exact compare below is |
| 1293 | // as UTF-8 or widen already-folded bytes (which could be part of a multibyte code-page character). | 1321 | // bytewise, and the key is never reinterpreted as UTF-8. |
| 1294 | for (const auto& entry : by_name_) | 1322 | for (const auto& entry : by_name_) |
| 1295 | if (entry.second->narrow_key == dll_lower) return entry.second; | 1323 | if (entry.second->narrow_key == dll_lower) return entry.second; |
| 1324 | // The key is ASCII case-folded only. A name with letters outside ASCII (Cyrillic in code page 1251, say) | ||
| 1325 | // compares case-insensitively through the code page, as GetModuleHandleA does: GUEST_ДА.EXE finds guest_да.exe. | ||
| 1326 | // Widening the folded key is safe here: rt::lower() folds ASCII letters only, and no ANSI code page's multibyte | ||
| 1327 | // sequences contain ASCII letter bytes, so both sides widen to the same characters they were spelt with. | ||
| 1328 | if (const std::wstring wide = acp_to_wide(dll_lower); !wide.empty()) | ||
| 1329 | for (const auto& entry : by_name_) | ||
| 1330 | if (!entry.second->narrow_key.empty() && | ||
| 1331 | same_module_name(acp_to_wide(entry.second->narrow_key).c_str(), wide.c_str())) | ||
| 1332 | return entry.second; | ||
| 1333 | return nullptr; | ||
| 1334 | } | ||
| 1335 | |||
| 1336 | Module* Loader::find_wide_module(const std::wstring& basename) const { | ||
| 1337 | if (basename.empty()) return nullptr; | ||
| 1338 | for (const auto& entry : by_name_) | ||
| 1339 | if (same_module_name(entry.first.c_str(), basename.c_str())) return entry.second; | ||
| 1296 | return nullptr; | 1340 | return nullptr; |
| 1297 | } | 1341 | } |
| 1298 | 1342 | ||
| 1343 | void* Loader::find_loaded_module_w(const std::wstring& basename) const { | ||
| 1344 | std::lock_guard lock(mutex_); | ||
| 1345 | Module* m = find_wide_module(basename); | ||
| 1346 | return m ? static_cast<void*>(m->base) : nullptr; | ||
| 1347 | } | ||
| 1348 | |||
| 1299 | void* Loader::find_loaded_module(const std::string& dll_lower) const { | 1349 | void* Loader::find_loaded_module(const std::string& dll_lower) const { |
| 1300 | std::lock_guard lock(mutex_); | 1350 | std::lock_guard lock(mutex_); |
| 1301 | Module* m = find_narrow_module(dll_lower); | 1351 | Module* m = find_narrow_module(dll_lower); |
| @@ -1315,12 +1365,34 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, | |||
| 1315 | if (!is_bundled) return nullptr; | 1365 | if (!is_bundled) return nullptr; |
| 1316 | 1366 | ||
| 1317 | handled = true; | 1367 | handled = true; |
| 1368 | return load_runtime_candidate(existing, candidate, flags, dll_lower); | ||
| 1369 | } | ||
| 1370 | |||
| 1371 | void* Loader::load_runtime_module_w(const std::wstring& basename, uint32_t flags, bool& handled) { | ||
| 1372 | handled = false; | ||
| 1373 | if (basename.empty()) return nullptr; | ||
| 1374 | // A name the code page can spell takes the narrow path's host-copy rule as well. | ||
| 1375 | if (const std::string narrow = narrow_module_key(std::filesystem::path(basename)); !narrow.empty()) | ||
| 1376 | if (prefer_host_copy(narrow)) return nullptr; | ||
| 1377 | |||
| 1378 | std::lock_guard lock(mutex_); | ||
| 1379 | Module* existing = find_wide_module(basename); | ||
| 1380 | const std::filesystem::path candidate = game_dir_ / basename; | ||
| 1381 | std::error_code ec; | ||
| 1382 | if (!existing && !std::filesystem::is_regular_file(candidate, ec)) return nullptr; | ||
| 1383 | |||
| 1384 | handled = true; | ||
| 1385 | return load_runtime_candidate(existing, candidate, flags, path_utf8(basename)); | ||
| 1386 | } | ||
| 1387 | |||
| 1388 | void* Loader::load_runtime_candidate(Module* existing, const std::filesystem::path& candidate, uint32_t flags, | ||
| 1389 | const std::string& display) { | ||
| 1318 | // Win32 DONT_RESOLVE_DLL_REFERENCES (0x1) forbids resolving dependencies or running TLS/DllMain; refuse it | 1390 | // Win32 DONT_RESOLVE_DLL_REFERENCES (0x1) forbids resolving dependencies or running TLS/DllMain; refuse it |
| 1319 | // explicitly instead of silently performing a normal resolving + initialising load. | 1391 | // explicitly instead of silently performing a normal resolving + initialising load. |
| 1320 | if ((flags & DONT_RESOLVE_DLL_REFERENCES) != 0) { | 1392 | if ((flags & DONT_RESOLVE_DLL_REFERENCES) != 0) { |
| 1321 | std::fprintf(stderr, | 1393 | std::fprintf(stderr, |
| 1322 | "[rt] loader: LoadLibraryEx(%s, DONT_RESOLVE_DLL_REFERENCES) is not supported for bundled DLLs\n", | 1394 | "[rt] loader: LoadLibraryEx(%s, DONT_RESOLVE_DLL_REFERENCES) is not supported for bundled DLLs\n", |
| 1323 | dll_lower.c_str()); | 1395 | display.c_str()); |
| 1324 | SetLastError(ERROR_NOT_SUPPORTED); | 1396 | SetLastError(ERROR_NOT_SUPPORTED); |
| 1325 | return nullptr; | 1397 | return nullptr; |
| 1326 | } | 1398 | } |
| @@ -1330,7 +1402,7 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, | |||
| 1330 | initialise(); | 1402 | initialise(); |
| 1331 | return static_cast<void*>(existing->base); | 1403 | return static_cast<void*>(existing->base); |
| 1332 | } catch (const std::exception& e) { | 1404 | } catch (const std::exception& e) { |
| 1333 | std::fprintf(stderr, "[rt] loader: run-time initialise of %s failed: %s\n", dll_lower.c_str(), e.what()); | 1405 | std::fprintf(stderr, "[rt] loader: run-time initialise of %s failed: %s\n", display.c_str(), e.what()); |
| 1334 | SetLastError(ERROR_DLL_INIT_FAILED); | 1406 | SetLastError(ERROR_DLL_INIT_FAILED); |
| 1335 | return nullptr; | 1407 | return nullptr; |
| 1336 | } | 1408 | } |
| @@ -1347,7 +1419,7 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, | |||
| 1347 | // load() drops a module that failed to map or bind (dependencies included); this also drops one whose | 1419 | // load() drops a module that failed to map or bind (dependencies included); this also drops one whose |
| 1348 | // initialise() failed, so later GetModuleHandle/LoadLibrary calls don't see it. ~Loader frees it via order_. | 1420 | // initialise() failed, so later GetModuleHandle/LoadLibrary calls don't see it. ~Loader frees it via order_. |
| 1349 | by_name_.erase(module_key(mapped ? mapped->path : candidate)); | 1421 | by_name_.erase(module_key(mapped ? mapped->path : candidate)); |
| 1350 | std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", dll_lower.c_str(), e.what()); | 1422 | std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", display.c_str(), e.what()); |
| 1351 | SetLastError(ERROR_DLL_INIT_FAILED); | 1423 | SetLastError(ERROR_DLL_INIT_FAILED); |
| 1352 | return nullptr; | 1424 | return nullptr; |
| 1353 | } | 1425 | } |