diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 10:40:29 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 10:40:29 -0500 |
| commit | 81c6cfe4fd44096e4c2bc52685cdc021eb829e5c (patch) | |
| tree | d720ceeff53b2858d978e96825d0c5dee5900f4c /runtime/shims/library_loader.cpp | |
| parent | 92fa203c165ebb20e83bc4ad740ff52968f710d4 (diff) | |
| parent | 47f4cdc643f3fca3f87a34d9a4718f250d7cd905 (diff) | |
Merge pull request #2021 from jaybeebabageyus
Diffstat (limited to 'runtime/shims/library_loader.cpp')
| -rw-r--r-- | runtime/shims/library_loader.cpp | 104 |
1 files changed, 84 insertions, 20 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp index 585c6a299..b67a38660 100644 --- a/runtime/shims/library_loader.cpp +++ b/runtime/shims/library_loader.cpp | |||
| @@ -92,60 +92,124 @@ const Pseudo* pseudo(HMODULE h) { | |||
| 92 | 92 | ||
| 93 | namespace { | 93 | namespace { |
| 94 | 94 | ||
| 95 | constexpr DWORD kDataFileLoadFlags = | ||
| 96 | LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | LOAD_LIBRARY_AS_IMAGE_RESOURCE; | ||
| 97 | |||
| 98 | HMODULE load_shim_or_bundled(const std::string& path, bool& handled) { | ||
| 99 | handled = false; | ||
| 100 | if (HMODULE h = owned_handle(path)) { | ||
| 101 | handled = true; | ||
| 102 | return h; | ||
| 103 | } | ||
| 104 | const std::string dll = module_name(path); | ||
| 105 | if (!dll.empty()) { | ||
| 106 | if (void* h = load_bundled_module(dll, handled); handled) return reinterpret_cast<HMODULE>(h); | ||
| 107 | } | ||
| 108 | return nullptr; | ||
| 109 | } | ||
| 110 | |||
| 95 | HMODULE WINAPI load_library_a(LPCSTR name) { | 111 | HMODULE WINAPI load_library_a(LPCSTR name) { |
| 96 | if (HMODULE h = name ? owned_handle(name) : nullptr) return h; | 112 | if (name) { |
| 113 | bool handled = false; | ||
| 114 | HMODULE h = load_shim_or_bundled(name, handled); | ||
| 115 | if (handled) return h; | ||
| 116 | } | ||
| 97 | return LoadLibraryA(name); | 117 | return LoadLibraryA(name); |
| 98 | } | 118 | } |
| 99 | 119 | ||
| 100 | HMODULE WINAPI load_library_w(LPCWSTR name) { | 120 | HMODULE WINAPI load_library_w(LPCWSTR name) { |
| 101 | if (HMODULE h = name ? owned_handle(narrow(name)) : nullptr) return h; | 121 | if (name) { |
| 122 | bool handled = false; | ||
| 123 | HMODULE h = load_shim_or_bundled(narrow(name), handled); | ||
| 124 | if (handled) return h; | ||
| 125 | } | ||
| 102 | return LoadLibraryW(name); | 126 | return LoadLibraryW(name); |
| 103 | } | 127 | } |
| 104 | 128 | ||
| 105 | HMODULE WINAPI load_library_ex_a(LPCSTR name, HANDLE file, DWORD flags) { | 129 | HMODULE WINAPI load_library_ex_a(LPCSTR name, HANDLE file, DWORD flags) { |
| 106 | if (HMODULE h = name ? owned_handle(name) : nullptr) return h; | 130 | if (name && (flags & kDataFileLoadFlags) == 0) { |
| 131 | bool handled = false; | ||
| 132 | HMODULE h = load_shim_or_bundled(name, handled); | ||
| 133 | if (handled) return h; | ||
| 134 | } | ||
| 107 | return LoadLibraryExA(name, file, flags); | 135 | return LoadLibraryExA(name, file, flags); |
| 108 | } | 136 | } |
| 109 | 137 | ||
| 110 | HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) { | 138 | HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) { |
| 111 | if (HMODULE h = name ? owned_handle(narrow(name)) : nullptr) return h; | 139 | if (name && (flags & kDataFileLoadFlags) == 0) { |
| 140 | bool handled = false; | ||
| 141 | HMODULE h = load_shim_or_bundled(narrow(name), handled); | ||
| 142 | if (handled) return h; | ||
| 143 | } | ||
| 112 | return LoadLibraryExW(name, file, flags); | 144 | return LoadLibraryExW(name, file, flags); |
| 113 | } | 145 | } |
| 114 | 146 | ||
| 115 | // GetModuleHandle answers for an owned module as if it were loaded: the GDK may probe before it loads. | 147 | // GetModuleHandle answers for an owned module as if it were loaded (the GDK may probe before it loads), and for any |
| 148 | // bundled package module already mapped by the native loader. | ||
| 116 | HMODULE WINAPI get_module_handle_a(LPCSTR name) { | 149 | HMODULE WINAPI get_module_handle_a(LPCSTR name) { |
| 117 | if (HMODULE h = name ? owned_handle(name) : nullptr) return h; | 150 | if (name) { |
| 151 | if (HMODULE h = owned_handle(name)) return h; | ||
| 152 | const std::string dll = module_name(name); | ||
| 153 | if (!dll.empty()) | ||
| 154 | if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h); | ||
| 155 | } | ||
| 118 | return GetModuleHandleA(name); | 156 | return GetModuleHandleA(name); |
| 119 | } | 157 | } |
| 120 | 158 | ||
| 121 | HMODULE WINAPI get_module_handle_w(LPCWSTR name) { | 159 | HMODULE WINAPI get_module_handle_w(LPCWSTR name) { |
| 122 | if (HMODULE h = name ? owned_handle(narrow(name)) : nullptr) return h; | 160 | if (name) { |
| 161 | const std::string path = narrow(name); | ||
| 162 | if (HMODULE h = owned_handle(path)) return h; | ||
| 163 | const std::string dll = module_name(path); | ||
| 164 | if (!dll.empty()) | ||
| 165 | if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h); | ||
| 166 | } | ||
| 123 | return GetModuleHandleW(name); | 167 | return GetModuleHandleW(name); |
| 124 | } | 168 | } |
| 125 | 169 | ||
| 126 | FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { | 170 | FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { |
| 127 | const Pseudo* p = pseudo(module); | 171 | if (const Pseudo* p = pseudo(module)) { |
| 128 | if (!p) return GetProcAddress(module, name); | 172 | if (!name) { |
| 129 | if (!name) { | 173 | SetLastError(ERROR_PROC_NOT_FOUND); |
| 174 | return nullptr; | ||
| 175 | } | ||
| 176 | // A value below 0x10000 is an ordinal (IS_INTRESOURCE), as in the real GetProcAddress. | ||
| 177 | const std::string what = | ||
| 178 | IS_INTRESOURCE(name) ? "#" + std::to_string(reinterpret_cast<uintptr_t>(name)) : name; | ||
| 179 | if (void* fn = find(p->dll, what)) return reinterpret_cast<FARPROC>(fn); | ||
| 180 | { | ||
| 181 | static auto* m = new std::mutex; | ||
| 182 | static auto* seen = new std::set<std::string>; | ||
| 183 | std::lock_guard lock(*m); | ||
| 184 | if (seen->insert(p->dll + "!" + what).second) | ||
| 185 | std::fprintf(stderr, "[rt] loader: GetProcAddress(%s, %s): not served\n", p->dll.c_str(), what.c_str()); | ||
| 186 | } | ||
| 130 | SetLastError(ERROR_PROC_NOT_FOUND); | 187 | SetLastError(ERROR_PROC_NOT_FOUND); |
| 131 | return nullptr; | 188 | return nullptr; |
| 132 | } | 189 | } |
| 133 | // A value below 0x10000 is an ordinal (IS_INTRESOURCE), as in the real GetProcAddress. | 190 | |
| 191 | if (!name) { | ||
| 192 | if (owns_bundled_module_handle(module)) { | ||
| 193 | SetLastError(ERROR_PROC_NOT_FOUND); | ||
| 194 | return nullptr; | ||
| 195 | } | ||
| 196 | return GetProcAddress(module, name); | ||
| 197 | } | ||
| 198 | |||
| 134 | const std::string what = IS_INTRESOURCE(name) ? "#" + std::to_string(reinterpret_cast<uintptr_t>(name)) : name; | 199 | const std::string what = IS_INTRESOURCE(name) ? "#" + std::to_string(reinterpret_cast<uintptr_t>(name)) : name; |
| 135 | if (void* fn = find(p->dll, what)) return reinterpret_cast<FARPROC>(fn); | 200 | bool owns_handle = false; |
| 136 | { | 201 | void* fn = get_bundled_proc_address(module, what, owns_handle); |
| 137 | static auto* m = new std::mutex; | 202 | if (owns_handle) { |
| 138 | static auto* seen = new std::set<std::string>; | 203 | if (fn) return reinterpret_cast<FARPROC>(fn); |
| 139 | std::lock_guard lock(*m); | 204 | SetLastError(ERROR_PROC_NOT_FOUND); |
| 140 | if (seen->insert(p->dll + "!" + what).second) | 205 | return nullptr; |
| 141 | std::fprintf(stderr, "[rt] loader: GetProcAddress(%s, %s): not served\n", p->dll.c_str(), what.c_str()); | ||
| 142 | } | 206 | } |
| 143 | SetLastError(ERROR_PROC_NOT_FOUND); | 207 | return GetProcAddress(module, name); |
| 144 | return nullptr; | ||
| 145 | } | 208 | } |
| 146 | 209 | ||
| 147 | BOOL WINAPI free_library(HMODULE module) { | 210 | BOOL WINAPI free_library(HMODULE module) { |
| 148 | if (pseudo(module)) return TRUE; // owned modules are never unloaded | 211 | if (pseudo(module)) return TRUE; // owned modules are never unloaded |
| 212 | if (owns_bundled_module_handle(module)) return TRUE; // mapped modules stay live until ~Loader detaches them | ||
| 149 | return FreeLibrary(module); | 213 | return FreeLibrary(module); |
| 150 | } | 214 | } |
| 151 | 215 | ||