diff options
| author | c4k3ss <77441265+c4k3ss@users.noreply.github.com> | 2026-11-23 18:47:01 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 10:47:01 -0600 |
| commit | 0656b0b200590d355dea4f7a9fdb1d141051ce55 (patch) | |
| tree | d9604f944110950cf3d95e8752a1ab3b2552a207 /runtime/src/pe_loader.cpp | |
| parent | 5c56d61e2a518d3a5495caae4fe626e73c9dcd8f (diff) | |
Loader: W lookups find a mapped image by its non-ASCII name; ANSI lookups ignore case outside ASCII (#6754)
Squash-merged after diff review of PR #6754
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 5514edba6..e5becb472 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()}; |
| @@ -898,11 +916,21 @@ bool bundled_owns_handle(void* ctx, void* module) { | |||
| 898 | return static_cast<Loader*>(ctx)->owns_module_handle(module); | 916 | return static_cast<Loader*>(ctx)->owns_module_handle(module); |
| 899 | } | 917 | } |
| 900 | 918 | ||
| 919 | void* bundled_find_loaded_w(void* ctx, const std::wstring& basename) { | ||
| 920 | return static_cast<Loader*>(ctx)->find_loaded_module_w(basename); | ||
| 921 | } | ||
| 922 | |||
| 923 | void* bundled_load_w(void* ctx, const std::wstring& basename, uint32_t flags, bool& handled) { | ||
| 924 | return static_cast<Loader*>(ctx)->load_runtime_module_w(basename, flags, handled); | ||
| 925 | } | ||
| 926 | |||
| 901 | const shims::BundledModuleOps kBundledOps{ | 927 | const shims::BundledModuleOps kBundledOps{ |
| 902 | &bundled_find_loaded, | 928 | &bundled_find_loaded, |
| 903 | &bundled_load, | 929 | &bundled_load, |
| 904 | &bundled_get_proc, | 930 | &bundled_get_proc, |
| 905 | &bundled_owns_handle, | 931 | &bundled_owns_handle, |
| 932 | &bundled_find_loaded_w, | ||
| 933 | &bundled_load_w, | ||
| 906 | }; | 934 | }; |
| 907 | 935 | ||
| 908 | // Marks what a run-time load maps as a dynamic load for as long as it is in scope (the caller holds the mutex). | 936 | // Marks what a run-time load maps as a dynamic load for as long as it is in scope (the caller holds the mutex). |
| @@ -996,13 +1024,35 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 996 | 1024 | ||
| 997 | Module* Loader::find_narrow_module(const std::string& dll_lower) const { | 1025 | Module* Loader::find_narrow_module(const std::string& dll_lower) const { |
| 998 | if (dll_lower.empty()) return nullptr; | 1026 | if (dll_lower.empty()) return nullptr; |
| 999 | // library_loader and PE import names keep their existing narrow-byte contract. Do not reinterpret them | 1027 | // library_loader and PE import names keep their existing narrow-byte contract: the exact compare below is |
| 1000 | // as UTF-8 or widen already-folded bytes (which could be part of a multibyte code-page character). | 1028 | // bytewise, and the key is never reinterpreted as UTF-8. |
| 1001 | for (const auto& entry : by_name_) | 1029 | for (const auto& entry : by_name_) |
| 1002 | if (entry.second->narrow_key == dll_lower) return entry.second; | 1030 | if (entry.second->narrow_key == dll_lower) return entry.second; |
| 1031 | // The key is ASCII case-folded only. A name with letters outside ASCII (Cyrillic in code page 1251, say) | ||
| 1032 | // compares case-insensitively through the code page, as GetModuleHandleA does: GUEST_ДА.EXE finds guest_да.exe. | ||
| 1033 | // Widening the folded key is safe here: rt::lower() folds ASCII letters only, and no ANSI code page's multibyte | ||
| 1034 | // sequences contain ASCII letter bytes, so both sides widen to the same characters they were spelt with. | ||
| 1035 | if (const std::wstring wide = acp_to_wide(dll_lower); !wide.empty()) | ||
| 1036 | for (const auto& entry : by_name_) | ||
| 1037 | if (!entry.second->narrow_key.empty() && | ||
| 1038 | same_module_name(acp_to_wide(entry.second->narrow_key).c_str(), wide.c_str())) | ||
| 1039 | return entry.second; | ||
| 1040 | return nullptr; | ||
| 1041 | } | ||
| 1042 | |||
| 1043 | Module* Loader::find_wide_module(const std::wstring& basename) const { | ||
| 1044 | if (basename.empty()) return nullptr; | ||
| 1045 | for (const auto& entry : by_name_) | ||
| 1046 | if (same_module_name(entry.first.c_str(), basename.c_str())) return entry.second; | ||
| 1003 | return nullptr; | 1047 | return nullptr; |
| 1004 | } | 1048 | } |
| 1005 | 1049 | ||
| 1050 | void* Loader::find_loaded_module_w(const std::wstring& basename) const { | ||
| 1051 | std::lock_guard lock(mutex_); | ||
| 1052 | Module* m = find_wide_module(basename); | ||
| 1053 | return m ? static_cast<void*>(m->base) : nullptr; | ||
| 1054 | } | ||
| 1055 | |||
| 1006 | void* Loader::find_loaded_module(const std::string& dll_lower) const { | 1056 | void* Loader::find_loaded_module(const std::string& dll_lower) const { |
| 1007 | std::lock_guard lock(mutex_); | 1057 | std::lock_guard lock(mutex_); |
| 1008 | Module* m = find_narrow_module(dll_lower); | 1058 | Module* m = find_narrow_module(dll_lower); |
| @@ -1022,12 +1072,34 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, | |||
| 1022 | if (!is_bundled) return nullptr; | 1072 | if (!is_bundled) return nullptr; |
| 1023 | 1073 | ||
| 1024 | handled = true; | 1074 | handled = true; |
| 1075 | return load_runtime_candidate(existing, candidate, flags, dll_lower); | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | void* Loader::load_runtime_module_w(const std::wstring& basename, uint32_t flags, bool& handled) { | ||
| 1079 | handled = false; | ||
| 1080 | if (basename.empty()) return nullptr; | ||
| 1081 | // A name the code page can spell takes the narrow path's host-copy rule as well. | ||
| 1082 | if (const std::string narrow = narrow_module_key(std::filesystem::path(basename)); !narrow.empty()) | ||
| 1083 | if (prefer_host_copy(narrow)) return nullptr; | ||
| 1084 | |||
| 1085 | std::lock_guard lock(mutex_); | ||
| 1086 | Module* existing = find_wide_module(basename); | ||
| 1087 | const std::filesystem::path candidate = game_dir_ / basename; | ||
| 1088 | std::error_code ec; | ||
| 1089 | if (!existing && !std::filesystem::is_regular_file(candidate, ec)) return nullptr; | ||
| 1090 | |||
| 1091 | handled = true; | ||
| 1092 | return load_runtime_candidate(existing, candidate, flags, path_utf8(basename)); | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | void* Loader::load_runtime_candidate(Module* existing, const std::filesystem::path& candidate, uint32_t flags, | ||
| 1096 | const std::string& display) { | ||
| 1025 | // Win32 DONT_RESOLVE_DLL_REFERENCES (0x1) forbids resolving dependencies or running TLS/DllMain; refuse it | 1097 | // Win32 DONT_RESOLVE_DLL_REFERENCES (0x1) forbids resolving dependencies or running TLS/DllMain; refuse it |
| 1026 | // explicitly instead of silently performing a normal resolving + initialising load. | 1098 | // explicitly instead of silently performing a normal resolving + initialising load. |
| 1027 | if ((flags & DONT_RESOLVE_DLL_REFERENCES) != 0) { | 1099 | if ((flags & DONT_RESOLVE_DLL_REFERENCES) != 0) { |
| 1028 | std::fprintf(stderr, | 1100 | std::fprintf(stderr, |
| 1029 | "[rt] loader: LoadLibraryEx(%s, DONT_RESOLVE_DLL_REFERENCES) is not supported for bundled DLLs\n", | 1101 | "[rt] loader: LoadLibraryEx(%s, DONT_RESOLVE_DLL_REFERENCES) is not supported for bundled DLLs\n", |
| 1030 | dll_lower.c_str()); | 1102 | display.c_str()); |
| 1031 | SetLastError(ERROR_NOT_SUPPORTED); | 1103 | SetLastError(ERROR_NOT_SUPPORTED); |
| 1032 | return nullptr; | 1104 | return nullptr; |
| 1033 | } | 1105 | } |
| @@ -1037,7 +1109,7 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, | |||
| 1037 | initialise(); | 1109 | initialise(); |
| 1038 | return static_cast<void*>(existing->base); | 1110 | return static_cast<void*>(existing->base); |
| 1039 | } catch (const std::exception& e) { | 1111 | } catch (const std::exception& e) { |
| 1040 | std::fprintf(stderr, "[rt] loader: run-time initialise of %s failed: %s\n", dll_lower.c_str(), e.what()); | 1112 | std::fprintf(stderr, "[rt] loader: run-time initialise of %s failed: %s\n", display.c_str(), e.what()); |
| 1041 | SetLastError(ERROR_DLL_INIT_FAILED); | 1113 | SetLastError(ERROR_DLL_INIT_FAILED); |
| 1042 | return nullptr; | 1114 | return nullptr; |
| 1043 | } | 1115 | } |
| @@ -1054,7 +1126,7 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, | |||
| 1054 | // load() drops a module that failed to map or bind (dependencies included); this also drops one whose | 1126 | // load() drops a module that failed to map or bind (dependencies included); this also drops one whose |
| 1055 | // initialise() failed, so later GetModuleHandle/LoadLibrary calls don't see it. ~Loader frees it via order_. | 1127 | // initialise() failed, so later GetModuleHandle/LoadLibrary calls don't see it. ~Loader frees it via order_. |
| 1056 | by_name_.erase(module_key(mapped ? mapped->path : candidate)); | 1128 | by_name_.erase(module_key(mapped ? mapped->path : candidate)); |
| 1057 | std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", dll_lower.c_str(), e.what()); | 1129 | std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", display.c_str(), e.what()); |
| 1058 | SetLastError(ERROR_DLL_INIT_FAILED); | 1130 | SetLastError(ERROR_DLL_INIT_FAILED); |
| 1059 | return nullptr; | 1131 | return nullptr; |
| 1060 | } | 1132 | } |