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:
authorVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-22 15:51:40 +0100
committerVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-22 15:51:40 +0100
commitdf91c6624b69ee4e6c6eeca20ed68d331ed0e728 (patch)
tree323c88b9df89bf8abaa2b98d10808474ab675dc2 /runtime/shims/library_loader.cpp
parentb40d8039a98f57b26bd05860febbabbe59b97873 (diff)
parent6a6704df1592e1dc4a3215ddf1423a78adb52045 (diff)
Merge main into test/valu-int-misc (#1492)
Conflicted files: runtime/test/shaders/check.py, scripts/run_gpu_tests.py AI-assisted.
Diffstat (limited to 'runtime/shims/library_loader.cpp')
-rw-r--r--runtime/shims/library_loader.cpp28
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
33namespace { 33namespace {
34 34
35std::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.
37std::mutex& loader_mutex() {
38 static auto* m = new std::mutex;
39 return *m;
40}
41
36std::deque<Pseudo>& pseudos() { // a deque never moves its elements, so handed-out handles stay valid 42std::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
41std::string lower_ascii(std::string s) { 47std::string lower_ascii(std::string s) {
@@ -67,7 +73,7 @@ std::string module_name(std::string path) {
67HMODULE owned_handle(const std::string& path) { 73HMODULE 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.
80const Pseudo* pseudo(HMODULE h) { 86const 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) {
120FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { 126FARPROC 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);