diff options
| author | admin <s0las16.224@gmail.com> | 2026-11-22 18:24:37 +0000 |
|---|---|---|
| committer | admin <s0las16.224@gmail.com> | 2026-11-22 18:24:37 +0000 |
| commit | 663c4d95060dfe63bb77461976ed8c4734fef345 (patch) | |
| tree | 6820bed683ee6c3d18eae11f1718e264c845a470 /runtime/shims/library_loader.cpp | |
| parent | 0327c9d27a582fa3726f42169b32c008778da924 (diff) | |
Cache host module names using growable heap buffers
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Diffstat (limited to 'runtime/shims/library_loader.cpp')
| -rw-r--r-- | runtime/shims/library_loader.cpp | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp index 0d609fe68..5a7a6c71c 100644 --- a/runtime/shims/library_loader.cpp +++ b/runtime/shims/library_loader.cpp | |||
| @@ -15,9 +15,11 @@ | |||
| 15 | 15 | ||
| 16 | #include <cstdio> | 16 | #include <cstdio> |
| 17 | #include <deque> | 17 | #include <deque> |
| 18 | #include <map> | ||
| 18 | #include <mutex> | 19 | #include <mutex> |
| 19 | #include <set> | 20 | #include <set> |
| 20 | #include <string> | 21 | #include <string> |
| 22 | #include <vector> | ||
| 21 | 23 | ||
| 22 | #include "shims.h" | 24 | #include "shims.h" |
| 23 | 25 | ||
| @@ -67,6 +69,25 @@ std::string module_name(std::string path) { | |||
| 67 | return path; | 69 | return path; |
| 68 | } | 70 | } |
| 69 | 71 | ||
| 72 | std::string host_module_name(HMODULE module) { | ||
| 73 | std::lock_guard lock(loader_mutex()); | ||
| 74 | static auto* names = new std::map<HMODULE, std::string>; | ||
| 75 | if (auto it = names->find(module); it != names->end()) return it->second; | ||
| 76 | |||
| 77 | std::vector<wchar_t> path(MAX_PATH); | ||
| 78 | for (;;) { | ||
| 79 | const DWORD length = GetModuleFileNameW(module, path.data(), static_cast<DWORD>(path.size())); | ||
| 80 | if (!length) return {}; | ||
| 81 | if (length < path.size()) { | ||
| 82 | const std::string name = module_name(narrow(path.data())); | ||
| 83 | names->emplace(module, name); | ||
| 84 | return name; | ||
| 85 | } | ||
| 86 | if (path.size() == 32768) return {}; | ||
| 87 | path.resize(path.size() > 16384 ? 32768 : path.size() * 2); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 70 | } // namespace | 91 | } // namespace |
| 71 | 92 | ||
| 72 | // The pseudo-handle for an owned module, or nullptr when the shims don't own `path`'s module. | 93 | // The pseudo-handle for an owned module, or nullptr when the shims don't own `path`'s module. |
| @@ -207,11 +228,8 @@ FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { | |||
| 207 | } | 228 | } |
| 208 | if (module && !IS_INTRESOURCE(name)) { | 229 | if (module && !IS_INTRESOURCE(name)) { |
| 209 | const DWORD error = GetLastError(); | 230 | const DWORD error = GetLastError(); |
| 210 | wchar_t path[32768]; | 231 | const std::string dll = host_module_name(module); |
| 211 | const DWORD length = GetModuleFileNameW(module, path, static_cast<DWORD>(sizeof(path) / sizeof(path[0]))); | 232 | void* shim = dll.empty() ? nullptr : find(dll, what); |
| 212 | void* shim = nullptr; | ||
| 213 | if (length && length < sizeof(path) / sizeof(path[0])) | ||
| 214 | shim = find(module_name(narrow(path)), what); | ||
| 215 | SetLastError(error); | 233 | SetLastError(error); |
| 216 | if (shim) return reinterpret_cast<FARPROC>(shim); | 234 | if (shim) return reinterpret_cast<FARPROC>(shim); |
| 217 | } | 235 | } |