diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 08:38:40 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 08:38:40 -0500 |
| commit | 4e0abc668cf46c1fdce9c4c8fd59b57258d2cab4 (patch) | |
| tree | 75aca1742bfba0d7619daa40aa6a635ca93f090d /runtime/shims/library_loader.cpp | |
| parent | 230c0f63dccdda8840fc3247f5aacacee8188791 (diff) | |
| parent | f90a9ead2c150ad519f2bcfe27fa038c70fa95e7 (diff) | |
Merge pull request #1713 from totonii
Diffstat (limited to 'runtime/shims/library_loader.cpp')
| -rw-r--r-- | runtime/shims/library_loader.cpp | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp index 4bcc57704..585c6a299 100644 --- a/runtime/shims/library_loader.cpp +++ b/runtime/shims/library_loader.cpp | |||
| @@ -32,10 +32,16 @@ struct Pseudo { | |||
| 32 | 32 | ||
| 33 | namespace { | 33 | namespace { |
| 34 | 34 | ||
| 35 | std::mutex g_mutex; | 35 | // Intentionally immortal: mapped-DLL DLL_PROCESS_DETACH and earlier atexit handlers can call GetProcAddress or |
| 36 | // FreeLibrary on pseudo-handles during process teardown, after normal statics would have been destroyed. | ||
| 37 | std::mutex& loader_mutex() { | ||
| 38 | static auto* m = new std::mutex; | ||
| 39 | return *m; | ||
| 40 | } | ||
| 41 | |||
| 36 | std::deque<Pseudo>& pseudos() { // a deque never moves its elements, so handed-out handles stay valid | 42 | std::deque<Pseudo>& pseudos() { // a deque never moves its elements, so handed-out handles stay valid |
| 37 | static std::deque<Pseudo> d; | 43 | static auto* d = new std::deque<Pseudo>; |
| 38 | return d; | 44 | return *d; |
| 39 | } | 45 | } |
| 40 | 46 | ||
| 41 | std::string lower_ascii(std::string s) { | 47 | std::string lower_ascii(std::string s) { |
| @@ -67,7 +73,7 @@ std::string module_name(std::string path) { | |||
| 67 | HMODULE owned_handle(const std::string& path) { | 73 | HMODULE owned_handle(const std::string& path) { |
| 68 | const std::string dll = module_name(path); | 74 | const std::string dll = module_name(path); |
| 69 | if (dll.empty() || !owns_dll(dll)) return nullptr; | 75 | if (dll.empty() || !owns_dll(dll)) return nullptr; |
| 70 | std::lock_guard lock(g_mutex); | 76 | std::lock_guard lock(loader_mutex()); |
| 71 | for (auto& p : pseudos()) | 77 | for (auto& p : pseudos()) |
| 72 | if (p.dll == dll) return reinterpret_cast<HMODULE>(&p); | 78 | if (p.dll == dll) return reinterpret_cast<HMODULE>(&p); |
| 73 | pseudos().push_back(Pseudo{}); | 79 | pseudos().push_back(Pseudo{}); |
| @@ -78,7 +84,7 @@ HMODULE owned_handle(const std::string& path) { | |||
| 78 | 84 | ||
| 79 | // The owned module a handle names, or nullptr for a host module. | 85 | // The owned module a handle names, or nullptr for a host module. |
| 80 | const Pseudo* pseudo(HMODULE h) { | 86 | const Pseudo* pseudo(HMODULE h) { |
| 81 | std::lock_guard lock(g_mutex); | 87 | std::lock_guard lock(loader_mutex()); |
| 82 | for (auto& p : pseudos()) | 88 | for (auto& p : pseudos()) |
| 83 | if (reinterpret_cast<HMODULE>(&p) == h) return &p; | 89 | if (reinterpret_cast<HMODULE>(&p) == h) return &p; |
| 84 | return nullptr; | 90 | return nullptr; |
| @@ -120,14 +126,18 @@ HMODULE WINAPI get_module_handle_w(LPCWSTR name) { | |||
| 120 | FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { | 126 | FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { |
| 121 | const Pseudo* p = pseudo(module); | 127 | const Pseudo* p = pseudo(module); |
| 122 | if (!p) return GetProcAddress(module, name); | 128 | if (!p) return GetProcAddress(module, name); |
| 129 | if (!name) { | ||
| 130 | SetLastError(ERROR_PROC_NOT_FOUND); | ||
| 131 | return nullptr; | ||
| 132 | } | ||
| 123 | // A value below 0x10000 is an ordinal (IS_INTRESOURCE), as in the real GetProcAddress. | 133 | // A value below 0x10000 is an ordinal (IS_INTRESOURCE), as in the real GetProcAddress. |
| 124 | const std::string what = IS_INTRESOURCE(name) ? "#" + std::to_string(reinterpret_cast<uintptr_t>(name)) : name; | 134 | const std::string what = IS_INTRESOURCE(name) ? "#" + std::to_string(reinterpret_cast<uintptr_t>(name)) : name; |
| 125 | if (void* fn = find(p->dll, what)) return reinterpret_cast<FARPROC>(fn); | 135 | if (void* fn = find(p->dll, what)) return reinterpret_cast<FARPROC>(fn); |
| 126 | { | 136 | { |
| 127 | static std::mutex m; | 137 | static auto* m = new std::mutex; |
| 128 | static std::set<std::string> seen; | 138 | static auto* seen = new std::set<std::string>; |
| 129 | std::lock_guard lock(m); | 139 | std::lock_guard lock(*m); |
| 130 | if (seen.insert(p->dll + "!" + what).second) | 140 | if (seen->insert(p->dll + "!" + what).second) |
| 131 | std::fprintf(stderr, "[rt] loader: GetProcAddress(%s, %s): not served\n", p->dll.c_str(), what.c_str()); | 141 | std::fprintf(stderr, "[rt] loader: GetProcAddress(%s, %s): not served\n", p->dll.c_str(), what.c_str()); |
| 132 | } | 142 | } |
| 133 | SetLastError(ERROR_PROC_NOT_FOUND); | 143 | SetLastError(ERROR_PROC_NOT_FOUND); |