upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'runtime')
-rw-r--r--runtime/shims/library_loader.cpp13
-rw-r--r--runtime/shims/registry.cpp4
-rw-r--r--runtime/shims/shims.h4
-rw-r--r--runtime/src/pe_loader.cpp33
-rw-r--r--runtime/src/pe_loader.h2
-rw-r--r--runtime/test/fixtures/tiny_bundled_load.cpp9
6 files changed, 43 insertions, 22 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp
index b67a38660..0093e6050 100644
--- a/runtime/shims/library_loader.cpp
+++ b/runtime/shims/library_loader.cpp
@@ -95,7 +95,7 @@ namespace {
95constexpr DWORD kDataFileLoadFlags = 95constexpr DWORD kDataFileLoadFlags =
96 LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | LOAD_LIBRARY_AS_IMAGE_RESOURCE; 96 LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | LOAD_LIBRARY_AS_IMAGE_RESOURCE;
97 97
98HMODULE load_shim_or_bundled(const std::string& path, bool& handled) { 98HMODULE load_shim_or_bundled(const std::string& path, DWORD flags, bool& handled) {
99 handled = false; 99 handled = false;
100 if (HMODULE h = owned_handle(path)) { 100 if (HMODULE h = owned_handle(path)) {
101 handled = true; 101 handled = true;
@@ -103,7 +103,8 @@ HMODULE load_shim_or_bundled(const std::string& path, bool& handled) {
103 } 103 }
104 const std::string dll = module_name(path); 104 const std::string dll = module_name(path);
105 if (!dll.empty()) { 105 if (!dll.empty()) {
106 if (void* h = load_bundled_module(dll, handled); handled) return reinterpret_cast<HMODULE>(h); 106 if (void* h = load_bundled_module(dll, static_cast<uint32_t>(flags), handled); handled)
107 return reinterpret_cast<HMODULE>(h);
107 } 108 }
108 return nullptr; 109 return nullptr;
109} 110}
@@ -111,7 +112,7 @@ HMODULE load_shim_or_bundled(const std::string& path, bool& handled) {
111HMODULE WINAPI load_library_a(LPCSTR name) { 112HMODULE WINAPI load_library_a(LPCSTR name) {
112 if (name) { 113 if (name) {
113 bool handled = false; 114 bool handled = false;
114 HMODULE h = load_shim_or_bundled(name, handled); 115 HMODULE h = load_shim_or_bundled(name, 0, handled);
115 if (handled) return h; 116 if (handled) return h;
116 } 117 }
117 return LoadLibraryA(name); 118 return LoadLibraryA(name);
@@ -120,7 +121,7 @@ HMODULE WINAPI load_library_a(LPCSTR name) {
120HMODULE WINAPI load_library_w(LPCWSTR name) { 121HMODULE WINAPI load_library_w(LPCWSTR name) {
121 if (name) { 122 if (name) {
122 bool handled = false; 123 bool handled = false;
123 HMODULE h = load_shim_or_bundled(narrow(name), handled); 124 HMODULE h = load_shim_or_bundled(narrow(name), 0, handled);
124 if (handled) return h; 125 if (handled) return h;
125 } 126 }
126 return LoadLibraryW(name); 127 return LoadLibraryW(name);
@@ -129,7 +130,7 @@ HMODULE WINAPI load_library_w(LPCWSTR name) {
129HMODULE WINAPI load_library_ex_a(LPCSTR name, HANDLE file, DWORD flags) { 130HMODULE WINAPI load_library_ex_a(LPCSTR name, HANDLE file, DWORD flags) {
130 if (name && (flags & kDataFileLoadFlags) == 0) { 131 if (name && (flags & kDataFileLoadFlags) == 0) {
131 bool handled = false; 132 bool handled = false;
132 HMODULE h = load_shim_or_bundled(name, handled); 133 HMODULE h = load_shim_or_bundled(name, flags, handled);
133 if (handled) return h; 134 if (handled) return h;
134 } 135 }
135 return LoadLibraryExA(name, file, flags); 136 return LoadLibraryExA(name, file, flags);
@@ -138,7 +139,7 @@ HMODULE WINAPI load_library_ex_a(LPCSTR name, HANDLE file, DWORD flags) {
138HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) { 139HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) {
139 if (name && (flags & kDataFileLoadFlags) == 0) { 140 if (name && (flags & kDataFileLoadFlags) == 0) {
140 bool handled = false; 141 bool handled = false;
141 HMODULE h = load_shim_or_bundled(narrow(name), handled); 142 HMODULE h = load_shim_or_bundled(narrow(name), flags, handled);
142 if (handled) return h; 143 if (handled) return h;
143 } 144 }
144 return LoadLibraryExW(name, file, flags); 145 return LoadLibraryExW(name, file, flags);
diff --git a/runtime/shims/registry.cpp b/runtime/shims/registry.cpp
index 27d8d8ec6..f6eb8487c 100644
--- a/runtime/shims/registry.cpp
+++ b/runtime/shims/registry.cpp
@@ -370,11 +370,11 @@ void* find_loaded_bundled_module(const std::string& dll_lower) {
370 return nullptr; 370 return nullptr;
371} 371}
372 372
373void* load_bundled_module(const std::string& dll_lower, bool& handled) { 373void* load_bundled_module(const std::string& dll_lower, uint32_t flags, bool& handled) {
374 handled = false; 374 handled = false;
375 for (const auto& entry : snapshot_bundled_loaders()) { 375 for (const auto& entry : snapshot_bundled_loaders()) {
376 bool loader_handled = false; 376 bool loader_handled = false;
377 void* h = entry.ops->load_bundled(entry.ctx, dll_lower, loader_handled); 377 void* h = entry.ops->load_bundled(entry.ctx, dll_lower, flags, loader_handled);
378 if (loader_handled) { 378 if (loader_handled) {
379 handled = true; 379 handled = true;
380 return h; 380 return h;
diff --git a/runtime/shims/shims.h b/runtime/shims/shims.h
index c20fdecf1..1dbf3a5bf 100644
--- a/runtime/shims/shims.h
+++ b/runtime/shims/shims.h
@@ -59,7 +59,7 @@ bool mapped_image_base(uint64_t address, uint64_t& base);
59// Loader. Registered in Loader::Loader and removed in Loader::~Loader. 59// Loader. Registered in Loader::Loader and removed in Loader::~Loader.
60struct BundledModuleOps { 60struct BundledModuleOps {
61 void* (*find_loaded)(void* ctx, const std::string& dll_lower); 61 void* (*find_loaded)(void* ctx, const std::string& dll_lower);
62 void* (*load_bundled)(void* ctx, const std::string& dll_lower, bool& handled); 62 void* (*load_bundled)(void* ctx, const std::string& dll_lower, uint32_t flags, bool& handled);
63 void* (*get_proc)(void* ctx, void* module, const std::string& sym, bool& owns_handle); 63 void* (*get_proc)(void* ctx, void* module, const std::string& sym, bool& owns_handle);
64 bool (*owns_handle)(void* ctx, void* module); 64 bool (*owns_handle)(void* ctx, void* module);
65}; 65};
@@ -67,7 +67,7 @@ void add_bundled_loader(void* ctx, const BundledModuleOps* ops);
67void remove_bundled_loader(void* ctx); 67void remove_bundled_loader(void* ctx);
68 68
69void* find_loaded_bundled_module(const std::string& dll_lower); 69void* find_loaded_bundled_module(const std::string& dll_lower);
70void* load_bundled_module(const std::string& dll_lower, bool& handled); 70void* load_bundled_module(const std::string& dll_lower, uint32_t flags, bool& handled);
71void* get_bundled_proc_address(void* module, const std::string& sym, bool& owns_handle); 71void* get_bundled_proc_address(void* module, const std::string& sym, bool& owns_handle);
72bool owns_bundled_module_handle(void* module); 72bool owns_bundled_module_handle(void* module);
73 73
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index 558ed2799..ce43936b7 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -350,8 +350,8 @@ void* bundled_find_loaded(void* ctx, const std::string& dll_lower) {
350 return static_cast<Loader*>(ctx)->find_loaded_module(dll_lower); 350 return static_cast<Loader*>(ctx)->find_loaded_module(dll_lower);
351} 351}
352 352
353void* bundled_load(void* ctx, const std::string& dll_lower, bool& handled) { 353void* bundled_load(void* ctx, const std::string& dll_lower, uint32_t flags, bool& handled) {
354 return static_cast<Loader*>(ctx)->load_runtime_module(dll_lower, handled); 354 return static_cast<Loader*>(ctx)->load_runtime_module(dll_lower, flags, handled);
355} 355}
356 356
357void* bundled_get_proc(void* ctx, void* module, const std::string& sym, bool& owns_handle) { 357void* bundled_get_proc(void* ctx, void* module, const std::string& sym, bool& owns_handle) {
@@ -436,16 +436,32 @@ void* Loader::find_loaded_module(const std::string& dll_lower) const {
436 return it == by_name_.end() ? nullptr : static_cast<void*>(it->second->base); 436 return it == by_name_.end() ? nullptr : static_cast<void*>(it->second->base);
437} 437}
438 438
439void* Loader::load_runtime_module(const std::string& dll_lower, bool& handled) { 439void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, bool& handled) {
440 handled = false; 440 handled = false;
441 if (dll_lower.empty() || prefer_host_copy(dll_lower)) return nullptr; 441 if (dll_lower.empty() || prefer_host_copy(dll_lower)) return nullptr;
442 442
443 std::lock_guard lock(mutex_); 443 std::lock_guard lock(mutex_);
444 if (auto it = by_name_.find(dll_lower); it != by_name_.end()) { 444 const auto existing = by_name_.find(dll_lower);
445 handled = true; 445 const std::filesystem::path candidate = game_dir_ / dll_lower;
446 std::error_code ec;
447 const bool is_bundled = existing != by_name_.end() || std::filesystem::is_regular_file(candidate, ec);
448 if (!is_bundled) return nullptr;
449
450 handled = true;
451 // Win32 DONT_RESOLVE_DLL_REFERENCES (0x1) forbids resolving dependencies or running TLS/DllMain; refuse it
452 // explicitly instead of silently performing a normal resolving + initialising load.
453 if ((flags & DONT_RESOLVE_DLL_REFERENCES) != 0) {
454 std::fprintf(stderr,
455 "[rt] loader: LoadLibraryEx(%s, DONT_RESOLVE_DLL_REFERENCES) is not supported for bundled DLLs\n",
456 dll_lower.c_str());
457 SetLastError(ERROR_NOT_SUPPORTED);
458 return nullptr;
459 }
460
461 if (existing != by_name_.end()) {
446 try { 462 try {
447 initialise(); 463 initialise();
448 return static_cast<void*>(it->second->base); 464 return static_cast<void*>(existing->second->base);
449 } catch (const std::exception& e) { 465 } catch (const std::exception& e) {
450 std::fprintf(stderr, "[rt] loader: run-time initialise of %s failed: %s\n", dll_lower.c_str(), e.what()); 466 std::fprintf(stderr, "[rt] loader: run-time initialise of %s failed: %s\n", dll_lower.c_str(), e.what());
451 SetLastError(ERROR_DLL_INIT_FAILED); 467 SetLastError(ERROR_DLL_INIT_FAILED);
@@ -453,11 +469,6 @@ void* Loader::load_runtime_module(const std::string& dll_lower, bool& handled) {
453 } 469 }
454 } 470 }
455 471
456 const std::filesystem::path candidate = game_dir_ / dll_lower;
457 std::error_code ec;
458 if (!std::filesystem::is_regular_file(candidate, ec)) return nullptr;
459
460 handled = true;
461 try { 472 try {
462 Module& m = load(candidate); 473 Module& m = load(candidate);
463 initialise(); 474 initialise();
diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h
index d9f89f542..d8801bc38 100644
--- a/runtime/src/pe_loader.h
+++ b/runtime/src/pe_loader.h
@@ -106,7 +106,7 @@ public:
106 106
107 // Run-time module lookup/loading used by the library_loader shim for bundled package DLLs. 107 // Run-time module lookup/loading used by the library_loader shim for bundled package DLLs.
108 void* find_loaded_module(const std::string& dll_lower) const; 108 void* find_loaded_module(const std::string& dll_lower) const;
109 void* load_runtime_module(const std::string& dll_lower, bool& handled); 109 void* load_runtime_module(const std::string& dll_lower, uint32_t flags, bool& handled);
110 void* resolve_module_export(void* module_handle, const std::string& sym, bool& owns_handle); 110 void* resolve_module_export(void* module_handle, const std::string& sym, bool& owns_handle);
111 bool owns_module_handle(void* module_handle) const; 111 bool owns_module_handle(void* module_handle) const;
112 112
diff --git a/runtime/test/fixtures/tiny_bundled_load.cpp b/runtime/test/fixtures/tiny_bundled_load.cpp
index 51fbaac72..f2a3b7f36 100644
--- a/runtime/test/fixtures/tiny_bundled_load.cpp
+++ b/runtime/test/fixtures/tiny_bundled_load.cpp
@@ -27,6 +27,15 @@ int main() {
27 if (GetModuleHandleW(L"tiny_bundled_dll.dll") != nullptr) 27 if (GetModuleHandleW(L"tiny_bundled_dll.dll") != nullptr)
28 return fail("GetModuleHandleW before LoadLibrary should be NULL", 0); 28 return fail("GetModuleHandleW before LoadLibrary should be NULL", 0);
29 29
30 // DONT_RESOLVE_DLL_REFERENCES must fail explicitly rather than silently resolving imports and running DllMain.
31 SetLastError(0);
32 if (LoadLibraryExW(L"tiny_bundled_dll.dll", nullptr, DONT_RESOLVE_DLL_REFERENCES) != nullptr ||
33 GetLastError() != ERROR_NOT_SUPPORTED) {
34 return fail("LoadLibraryExW(DONT_RESOLVE_DLL_REFERENCES) fails with ERROR_NOT_SUPPORTED", GetLastError());
35 }
36 if (GetModuleHandleW(L"tiny_bundled_dll.dll") != nullptr)
37 return fail("DONT_RESOLVE_DLL_REFERENCES must not map or register the bundled DLL", 0);
38
30 HMODULE mod = LoadLibraryW(L"tiny_bundled_dll.dll"); 39 HMODULE mod = LoadLibraryW(L"tiny_bundled_dll.dll");
31 if (!mod) return fail("LoadLibraryW(tiny_bundled_dll.dll)", GetLastError()); 40 if (!mod) return fail("LoadLibraryW(tiny_bundled_dll.dll)", GetLastError());
32 41