diff options
| author | jaybeebabageyus <jaybeebabageyus@users.noreply.github.com> | 2026-11-22 16:21:16 +0100 |
|---|---|---|
| committer | jaybeebabageyus <jaybeebabageyus@users.noreply.github.com> | 2026-11-22 16:21:16 +0100 |
| commit | 47f4cdc643f3fca3f87a34d9a4718f250d7cd905 (patch) | |
| tree | 0ff132d81a5a655525d5a851adaabfaa10ac1fa2 /runtime/test/fixtures/tiny_bundled_load.cpp | |
| parent | 6a6704df1592e1dc4a3215ddf1423a78adb52045 (diff) | |
runtime: route LoadLibrary/GetProcAddress of bundled package DLLs through rt::Loader
Diffstat (limited to 'runtime/test/fixtures/tiny_bundled_load.cpp')
| -rw-r--r-- | runtime/test/fixtures/tiny_bundled_load.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/runtime/test/fixtures/tiny_bundled_load.cpp b/runtime/test/fixtures/tiny_bundled_load.cpp new file mode 100644 index 000000000..51fbaac72 --- /dev/null +++ b/runtime/test/fixtures/tiny_bundled_load.cpp | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | // A mapped program that loads a bundled Xbox DLL (tiny_bundled_dll.dll, which imports xmem.dll) at run time via | ||
| 2 | // LoadLibraryW, GetModuleHandle, GetProcAddress and FreeLibrary — the pattern used by titles that dynamically load | ||
| 3 | // Bink2Scarlett.dll, XboxBvhBuilder.dll or the FidelityFX DLLs. Under `gta6rt --run`, library_loader.cpp routes the | ||
| 4 | // load to rt::Loader so the DLL's xmem.dll imports bind to the shim registry instead of failing under the host loader. | ||
| 5 | // Ordinary test code; no game data. | ||
| 6 | #include <windows.h> | ||
| 7 | |||
| 8 | #include <cstddef> | ||
| 9 | #include <cstdint> | ||
| 10 | #include <cstdio> | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | using AttachCountFn = int(__stdcall*)(); | ||
| 15 | using AllocSumFn = uint32_t(__stdcall*)(); | ||
| 16 | using XMemAllocFn = void*(__stdcall*)(size_t, uint64_t); | ||
| 17 | using XMemFreeFn = void(__stdcall*)(void*, uint64_t); | ||
| 18 | |||
| 19 | int fail(const char* stage, unsigned long value) { | ||
| 20 | std::printf("tiny_bundled_load: FAIL %s (0x%08lx)\n", stage, value); | ||
| 21 | return 1; | ||
| 22 | } | ||
| 23 | |||
| 24 | } // namespace | ||
| 25 | |||
| 26 | int main() { | ||
| 27 | if (GetModuleHandleW(L"tiny_bundled_dll.dll") != nullptr) | ||
| 28 | return fail("GetModuleHandleW before LoadLibrary should be NULL", 0); | ||
| 29 | |||
| 30 | HMODULE mod = LoadLibraryW(L"tiny_bundled_dll.dll"); | ||
| 31 | if (!mod) return fail("LoadLibraryW(tiny_bundled_dll.dll)", GetLastError()); | ||
| 32 | |||
| 33 | // Must be mapped by rt::Loader, not by the host Windows loader. | ||
| 34 | HMODULE host_view = nullptr; | ||
| 35 | if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"tiny_bundled_dll.dll", &host_view) && | ||
| 36 | host_view == mod) { | ||
| 37 | return fail("tiny_bundled_dll.dll came from the host loader, not rt::Loader", 0); | ||
| 38 | } | ||
| 39 | |||
| 40 | if (GetModuleHandleW(L"tiny_bundled_dll.dll") != mod) | ||
| 41 | return fail("GetModuleHandleW after LoadLibrary returns the same handle", 0); | ||
| 42 | if (GetModuleHandleA("TINY_BUNDLED_DLL") != mod) | ||
| 43 | return fail("GetModuleHandleA case-insensitive without .dll returns the same handle", 0); | ||
| 44 | if (LoadLibraryExA("C:\\Fake\\Path\\tiny_bundled_dll.dll", nullptr, 0) != mod) | ||
| 45 | return fail("LoadLibraryExA with full path returns the same already-mapped handle", GetLastError()); | ||
| 46 | |||
| 47 | auto attach_count = reinterpret_cast<AttachCountFn>(GetProcAddress(mod, "bundled_attach_count")); | ||
| 48 | if (!attach_count) return fail("GetProcAddress(bundled_attach_count)", GetLastError()); | ||
| 49 | const int count = attach_count(); | ||
| 50 | if (count != 1) return fail("DllMain(DLL_PROCESS_ATTACH) ran once", static_cast<unsigned long>(count)); | ||
| 51 | |||
| 52 | auto alloc_sum = reinterpret_cast<AllocSumFn>(GetProcAddress(mod, "bundled_alloc_sum")); | ||
| 53 | if (!alloc_sum) return fail("GetProcAddress(bundled_alloc_sum)", GetLastError()); | ||
| 54 | if (reinterpret_cast<void*>(GetProcAddress(mod, MAKEINTRESOURCEA(2))) != reinterpret_cast<void*>(alloc_sum)) | ||
| 55 | return fail("GetProcAddress by ordinal #2 matches bundled_alloc_sum", GetLastError()); | ||
| 56 | const uint32_t sum = alloc_sum(); | ||
| 57 | if (sum != 136u) return fail("bundled_alloc_sum via xmem shim returned 136", sum); | ||
| 58 | |||
| 59 | auto fwd_alloc = reinterpret_cast<XMemAllocFn>(GetProcAddress(mod, "bundled_fwd_alloc")); | ||
| 60 | auto fwd_free = reinterpret_cast<XMemFreeFn>(GetProcAddress(mod, "bundled_fwd_free")); | ||
| 61 | if (!fwd_alloc || !fwd_free) return fail("GetProcAddress follows forwarders to xmem shim", GetLastError()); | ||
| 62 | void* block = fwd_alloc(128, 0); | ||
| 63 | if (!block || (reinterpret_cast<uintptr_t>(block) & 63u) != 0) | ||
| 64 | return fail("forwarded xmem.XMemAlloc returned aligned block", 0); | ||
| 65 | fwd_free(block, 0); | ||
| 66 | |||
| 67 | SetLastError(0); | ||
| 68 | if (GetProcAddress(mod, "NoSuchExport") != nullptr || GetLastError() != ERROR_PROC_NOT_FOUND) | ||
| 69 | return fail("missing export returns NULL with ERROR_PROC_NOT_FOUND", GetLastError()); | ||
| 70 | SetLastError(0); | ||
| 71 | if (GetProcAddress(mod, nullptr) != nullptr || GetLastError() != ERROR_PROC_NOT_FOUND) | ||
| 72 | return fail("null export name returns NULL with ERROR_PROC_NOT_FOUND", GetLastError()); | ||
| 73 | |||
| 74 | if (!FreeLibrary(mod)) return fail("FreeLibrary(tiny_bundled_dll.dll)", GetLastError()); | ||
| 75 | |||
| 76 | // Host modules still resolve through the host loader. | ||
| 77 | HMODULE k32 = LoadLibraryW(L"kernel32.dll"); | ||
| 78 | if (!k32 || GetProcAddress(k32, "GetTickCount") != GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetTickCount")) | ||
| 79 | return fail("kernel32.dll still comes from the host", GetLastError()); | ||
| 80 | |||
| 81 | std::puts("tiny_bundled_load: PASS"); | ||
| 82 | return 0; | ||
| 83 | } | ||