upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/library_loader.cpp
diff options
context:
space:
mode:
authorc4k3ss <77441265+c4k3ss@users.noreply.github.com>2026-11-23 18:47:01 +0200
committerGitHub <noreply@github.com>2026-11-23 10:47:01 -0600
commit0656b0b200590d355dea4f7a9fdb1d141051ce55 (patch)
treed9604f944110950cf3d95e8752a1ab3b2552a207 /runtime/shims/library_loader.cpp
parent5c56d61e2a518d3a5495caae4fe626e73c9dcd8f (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/shims/library_loader.cpp')
-rw-r--r--runtime/shims/library_loader.cpp54
1 files changed, 47 insertions, 7 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp
index 2fa04c75f..82dd75493 100644
--- a/runtime/shims/library_loader.cpp
+++ b/runtime/shims/library_loader.cpp
@@ -58,6 +58,28 @@ std::string narrow(const wchar_t* w) {
58 return s; 58 return s;
59} 59}
60 60
61bool is_ascii(const wchar_t* w) {
62 for (; w && *w; ++w)
63 if (*w >= 0x80) return false;
64 return true;
65}
66
67// module_name for a name narrow() cannot spell: the last path component as given (the loader matches it
68// case-insensitively, as Windows does), ".dll" added when there is no extension.
69std::wstring module_name_w(std::wstring path) {
70 const size_t slash = path.find_last_of(L"\\/");
71 if (slash != std::wstring::npos) path.erase(0, slash + 1);
72 if (!path.empty() && path.back() == L'.') path.pop_back();
73 else if (path.find(L'.') == std::wstring::npos) path += L".dll";
74 return path;
75}
76
77// Whether the module a wide name refers to has an ASCII name. Only the last component decides: the directory may be
78// outside ASCII (narrow() turns it into '?', which the basename-only lookups never see) while the module is a shim's.
79bool ascii_basename(const wchar_t* name) {
80 return is_ascii(module_name_w(name).c_str());
81}
82
61// LoadLibrary's file-name rules for the module a path names: the last path component, lower case, and ".dll" added 83// LoadLibrary's file-name rules for the module a path names: the last path component, lower case, and ".dll" added
62// when there is no extension (a trailing '.' means "no extension, add nothing"). 84// when there is no extension (a trailing '.' means "no extension, add nothing").
63std::string module_name(std::string path) { 85std::string module_name(std::string path) {
@@ -139,10 +161,20 @@ HMODULE WINAPI load_library_a(LPCSTR name) {
139 return LoadLibraryA(name); 161 return LoadLibraryA(name);
140} 162}
141 163
164// A name with a character outside ASCII names no shim (their names are ASCII) and narrow() would turn it into '?',
165// so it goes to the bundled loader by its wide basename: an image already mapped under that name is returned, as
166// Windows returns the loaded module for LoadLibraryW of its own file.
167HMODULE load_bundled_w(LPCWSTR name, DWORD flags, bool& handled) {
168 handled = false;
169 const std::wstring dll = module_name_w(name);
170 if (dll.empty()) return nullptr;
171 return reinterpret_cast<HMODULE>(load_bundled_module_w(dll, static_cast<uint32_t>(flags), handled));
172}
173
142HMODULE WINAPI load_library_w(LPCWSTR name) { 174HMODULE WINAPI load_library_w(LPCWSTR name) {
143 if (name) { 175 if (name) {
144 bool handled = false; 176 bool handled = false;
145 HMODULE h = load_shim_or_bundled(narrow(name), 0, handled); 177 HMODULE h = ascii_basename(name) ? load_shim_or_bundled(narrow(name), 0, handled) : load_bundled_w(name, 0, handled);
146 if (handled) return h; 178 if (handled) return h;
147 } 179 }
148 return LoadLibraryW(name); 180 return LoadLibraryW(name);
@@ -160,7 +192,8 @@ HMODULE WINAPI load_library_ex_a(LPCSTR name, HANDLE file, DWORD flags) {
160HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) { 192HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) {
161 if (name && (flags & kDataFileLoadFlags) == 0) { 193 if (name && (flags & kDataFileLoadFlags) == 0) {
162 bool handled = false; 194 bool handled = false;
163 HMODULE h = load_shim_or_bundled(narrow(name), flags, handled); 195 HMODULE h = ascii_basename(name) ? load_shim_or_bundled(narrow(name), flags, handled)
196 : load_bundled_w(name, flags, handled);
164 if (handled) return h; 197 if (handled) return h;
165 } 198 }
166 return LoadLibraryExW(name, file, flags); 199 return LoadLibraryExW(name, file, flags);
@@ -180,11 +213,18 @@ HMODULE WINAPI get_module_handle_a(LPCSTR name) {
180 213
181HMODULE WINAPI get_module_handle_w(LPCWSTR name) { 214HMODULE WINAPI get_module_handle_w(LPCWSTR name) {
182 if (name) { 215 if (name) {
183 const std::string path = narrow(name); 216 if (ascii_basename(name)) {
184 if (HMODULE h = owned_handle(path)) return h; 217 const std::string path = narrow(name);
185 const std::string dll = module_name(path); 218 if (HMODULE h = owned_handle(path)) return h;
186 if (!dll.empty()) 219 const std::string dll = module_name(path);
187 if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h); 220 if (!dll.empty())
221 if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h);
222 } else {
223 // Outside ASCII: by the wide basename (narrow() would turn it into '?'), as for load_library_w.
224 const std::wstring dll = module_name_w(name);
225 if (!dll.empty())
226 if (void* h = find_loaded_bundled_module_w(dll)) return reinterpret_cast<HMODULE>(h);
227 }
188 } 228 }
189 return GetModuleHandleW(name); 229 return GetModuleHandleW(name);
190} 230}