diff options
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/shims/library_loader.cpp | 28 | ||||
| -rw-r--r-- | runtime/test/xgameruntime_test.cpp | 34 |
2 files changed, 53 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); |
diff --git a/runtime/test/xgameruntime_test.cpp b/runtime/test/xgameruntime_test.cpp index 755b64381..ac36f40d9 100644 --- a/runtime/test/xgameruntime_test.cpp +++ b/runtime/test/xgameruntime_test.cpp | |||
| @@ -10,6 +10,7 @@ | |||
| 10 | #include <atomic> | 10 | #include <atomic> |
| 11 | #include <cstdint> | 11 | #include <cstdint> |
| 12 | #include <cstdio> | 12 | #include <cstdio> |
| 13 | #include <cstdlib> | ||
| 13 | #include <cstring> | 14 | #include <cstring> |
| 14 | 15 | ||
| 15 | #include <XAsync.h> | 16 | #include <XAsync.h> |
| @@ -98,6 +99,31 @@ HRESULT CALLBACK provider(XAsyncOp op, const XAsyncProviderData* data) { | |||
| 98 | } | 99 | } |
| 99 | } | 100 | } |
| 100 | 101 | ||
| 102 | HMODULE g_teardown_rt_mod = nullptr; | ||
| 103 | FARPROC(WINAPI* g_teardown_get_proc)(HMODULE, LPCSTR) = nullptr; | ||
| 104 | BOOL(WINAPI* g_teardown_free_lib)(HMODULE) = nullptr; | ||
| 105 | |||
| 106 | // Registered with std::atexit before check_run_time_load() constructs any function-local statics in library_loader. | ||
| 107 | // LIFO atexit order means this runs after those statics would have been destroyed, simulating mapped-DLL | ||
| 108 | // DLL_PROCESS_DETACH or earlier atexit handlers calling GetProcAddress/FreeLibrary on a pseudo-handle during exit. | ||
| 109 | void check_late_teardown() { | ||
| 110 | if (!g_teardown_rt_mod || !g_teardown_get_proc || !g_teardown_free_lib) return; | ||
| 111 | if (g_teardown_get_proc(g_teardown_rt_mod, "UninitializeApiImpl") == nullptr) { | ||
| 112 | std::fprintf(stderr, "FAIL late teardown GetProcAddress on pseudo-handle\n"); | ||
| 113 | std::_Exit(1); | ||
| 114 | } | ||
| 115 | SetLastError(0); | ||
| 116 | if (g_teardown_get_proc(g_teardown_rt_mod, "LateTeardownMissingSymbol") != nullptr || | ||
| 117 | GetLastError() != ERROR_PROC_NOT_FOUND) { | ||
| 118 | std::fprintf(stderr, "FAIL late teardown unserved GetProcAddress on pseudo-handle\n"); | ||
| 119 | std::_Exit(1); | ||
| 120 | } | ||
| 121 | if (g_teardown_free_lib(g_teardown_rt_mod) != TRUE) { | ||
| 122 | std::fprintf(stderr, "FAIL late teardown FreeLibrary on pseudo-handle\n"); | ||
| 123 | std::_Exit(1); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 101 | // xgameruntime.lib's thunks don't import the runtime: they LoadLibraryW(L"xgameruntime.dll") and GetProcAddress five | 127 | // xgameruntime.lib's thunks don't import the runtime: they LoadLibraryW(L"xgameruntime.dll") and GetProcAddress five |
| 102 | // names (measured in the package's libHttpClient.GDK.dll and XCurl.dll). Drive that path through the loader shims, | 128 | // names (measured in the package's libHttpClient.GDK.dll and XCurl.dll). Drive that path through the loader shims, |
| 103 | // bound by the api set XCurl.dll imports them from, as a mapped image would be. | 129 | // bound by the api set XCurl.dll imports them from, as a mapped image would be. |
| @@ -136,6 +162,9 @@ void check_run_time_load() { | |||
| 136 | SetLastError(0); | 162 | SetLastError(0); |
| 137 | check(get_proc(rt_mod, "NoSuchRuntimeFunction") == nullptr && GetLastError() == ERROR_PROC_NOT_FOUND, | 163 | check(get_proc(rt_mod, "NoSuchRuntimeFunction") == nullptr && GetLastError() == ERROR_PROC_NOT_FOUND, |
| 138 | "an unserved name is NULL with ERROR_PROC_NOT_FOUND, as from a real module"); | 164 | "an unserved name is NULL with ERROR_PROC_NOT_FOUND, as from a real module"); |
| 165 | SetLastError(0); | ||
| 166 | check(get_proc(rt_mod, nullptr) == nullptr && GetLastError() == ERROR_PROC_NOT_FOUND, | ||
| 167 | "a null proc name is NULL with ERROR_PROC_NOT_FOUND"); | ||
| 139 | 168 | ||
| 140 | struct Options { | 169 | struct Options { |
| 141 | uint32_t unknown; | 170 | uint32_t unknown; |
| @@ -162,6 +191,10 @@ void check_run_time_load() { | |||
| 162 | "GetProcAddress on a host module is the host's"); | 191 | "GetProcAddress on a host module is the host's"); |
| 163 | check(free_lib(rt_mod) == TRUE && load_w(L"xgameruntime.dll") == rt_mod, | 192 | check(free_lib(rt_mod) == TRUE && load_w(L"xgameruntime.dll") == rt_mod, |
| 164 | "FreeLibrary on the runtime succeeds and keeps it loaded"); | 193 | "FreeLibrary on the runtime succeeds and keeps it loaded"); |
| 194 | |||
| 195 | g_teardown_rt_mod = rt_mod; | ||
| 196 | g_teardown_get_proc = get_proc; | ||
| 197 | g_teardown_free_lib = free_lib; | ||
| 165 | } | 198 | } |
| 166 | 199 | ||
| 167 | } // namespace | 200 | } // namespace |
| @@ -262,6 +295,7 @@ int main() { | |||
| 262 | check(init() == S_OK, "XGameRuntimeInitialize works again after balanced uninitialize calls"); | 295 | check(init() == S_OK, "XGameRuntimeInitialize works again after balanced uninitialize calls"); |
| 263 | uninit(); | 296 | uninit(); |
| 264 | 297 | ||
| 298 | std::atexit(check_late_teardown); | ||
| 265 | check_run_time_load(); | 299 | check_run_time_load(); |
| 266 | 300 | ||
| 267 | if (g_failures) { | 301 | if (g_failures) { |