From f90a9ead2c150ad519f2bcfe27fa038c70fa95e7 Mon Sep 17 00:00:00 2001 From: totonii Date: Sun, 22 Nov 2026 10:29:25 -0300 Subject: fix(library_loader): make pseudo-handle state immortal across teardown and reject null proc names --- runtime/shims/library_loader.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'runtime/shims/library_loader.cpp') 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 { namespace { -std::mutex g_mutex; +// Intentionally immortal: mapped-DLL DLL_PROCESS_DETACH and earlier atexit handlers can call GetProcAddress or +// FreeLibrary on pseudo-handles during process teardown, after normal statics would have been destroyed. +std::mutex& loader_mutex() { + static auto* m = new std::mutex; + return *m; +} + std::deque& pseudos() { // a deque never moves its elements, so handed-out handles stay valid - static std::deque d; - return d; + static auto* d = new std::deque; + return *d; } std::string lower_ascii(std::string s) { @@ -67,7 +73,7 @@ std::string module_name(std::string path) { HMODULE owned_handle(const std::string& path) { const std::string dll = module_name(path); if (dll.empty() || !owns_dll(dll)) return nullptr; - std::lock_guard lock(g_mutex); + std::lock_guard lock(loader_mutex()); for (auto& p : pseudos()) if (p.dll == dll) return reinterpret_cast(&p); pseudos().push_back(Pseudo{}); @@ -78,7 +84,7 @@ HMODULE owned_handle(const std::string& path) { // The owned module a handle names, or nullptr for a host module. const Pseudo* pseudo(HMODULE h) { - std::lock_guard lock(g_mutex); + std::lock_guard lock(loader_mutex()); for (auto& p : pseudos()) if (reinterpret_cast(&p) == h) return &p; return nullptr; @@ -120,14 +126,18 @@ HMODULE WINAPI get_module_handle_w(LPCWSTR name) { FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { const Pseudo* p = pseudo(module); if (!p) return GetProcAddress(module, name); + if (!name) { + SetLastError(ERROR_PROC_NOT_FOUND); + return nullptr; + } // A value below 0x10000 is an ordinal (IS_INTRESOURCE), as in the real GetProcAddress. const std::string what = IS_INTRESOURCE(name) ? "#" + std::to_string(reinterpret_cast(name)) : name; if (void* fn = find(p->dll, what)) return reinterpret_cast(fn); { - static std::mutex m; - static std::set seen; - std::lock_guard lock(m); - if (seen.insert(p->dll + "!" + what).second) + static auto* m = new std::mutex; + static auto* seen = new std::set; + std::lock_guard lock(*m); + if (seen->insert(p->dll + "!" + what).second) std::fprintf(stderr, "[rt] loader: GetProcAddress(%s, %s): not served\n", p->dll.c_str(), what.c_str()); } SetLastError(ERROR_PROC_NOT_FOUND); -- cgit v1.2.3