Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,27 @@ if(BUILD_TESTING)
# Wine serves its builtin wsock32 for a wsock32.dll loaded by path; the test needs its own copy. Windows ignores this.
set_tests_properties(library_loader_names PROPERTIES ENVIRONMENT "WINEDLLOVERRIDES=wsock32=n,b")

# GetModuleFileNameA/W and GetModuleHandleA/W(nullptr) for a mapped executable, bundled DLLs, and owned shims.
add_executable(library_loader_test
test/library_loader_test.cpp
src/pe_loader.cpp
shims/library_loader.cpp
shims/registry.cpp
shims/xmem.cpp
)
target_include_directories(library_loader_test PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/shims
)
target_compile_definitions(library_loader_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(library_loader_test PRIVATE /W4 /permissive- /utf-8 /EHsc)
target_link_options(library_loader_test PRIVATE /OPT:NOREF)
target_link_libraries(library_loader_test PRIVATE psapi)
add_dependencies(library_loader_test tiny_exe tiny_dll gta6rt_tls_host)
add_test(NAME library_loader
COMMAND library_loader_test "$<TARGET_FILE:tiny_exe>" "$<TARGET_FILE:tiny_dll>"
)

# The host GameInput path (docs/gameinput.md): mapped code that imports GameInputCreate from GameInput.dll, with no
# GameInput shim linked. Needs the SDK's gameinput.lib and GameInput.h to build the fixture, and a GameInput.dll on
# the machine to run (the test skips with 77 only when Windows cannot load GameInput.dll). No game files.
Expand Down
111 changes: 88 additions & 23 deletions runtime/shims/library_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,40 +232,103 @@ HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) {
}

// GetModuleHandle answers for a LEONIDA_COMPAT_DIR module as Windows would (its handle once loaded, else NULL, never
// the shim's pseudo-handle), for an owned module as if it were loaded (the GDK may probe before it loads), and for any
// bundled package module already mapped by the native loader.
// the shim's pseudo-handle), for an owned module as if it were loaded (the GDK may probe before it loads), for any
// bundled package module already mapped by the native loader, and for NULL when a mapped executable is active.
HMODULE WINAPI get_module_handle_a(LPCSTR name) {
if (name) {
bool compat = false;
if (HMODULE h = compat_first(widen(name), false, 0, compat); compat) return h;
if (HMODULE h = owned_handle(name)) return h;
const std::string dll = module_name(name);
if (!dll.empty())
if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h);
if (!name) {
if (void* main = main_bundled_module()) {
SetLastError(ERROR_SUCCESS);
return reinterpret_cast<HMODULE>(main);
}
return GetModuleHandleA(nullptr);
}
bool compat = false;
if (HMODULE h = compat_first(widen(name), false, 0, compat); compat) return h;
if (HMODULE h = owned_handle(name)) return h;
const std::string dll = module_name(name);
if (!dll.empty())
if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h);
return GetModuleHandleA(name);
}

HMODULE WINAPI get_module_handle_w(LPCWSTR name) {
if (name) {
bool compat = false;
if (HMODULE h = compat_first(name, false, 0, compat); compat) return h;
if (ascii_basename(name)) {
const std::string path = narrow(name);
if (HMODULE h = owned_handle(path)) return h;
const std::string dll = module_name(path);
if (!dll.empty())
if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h);
} else {
// Outside ASCII: by the wide basename (narrow() would turn it into '?'), as for load_library_w.
const std::wstring dll = module_name_w(name);
if (!dll.empty())
if (void* h = find_loaded_bundled_module_w(dll)) return reinterpret_cast<HMODULE>(h);
if (!name) {
if (void* main = main_bundled_module()) {
SetLastError(ERROR_SUCCESS);
return reinterpret_cast<HMODULE>(main);
}
return GetModuleHandleW(nullptr);
}
bool compat = false;
if (HMODULE h = compat_first(name, false, 0, compat); compat) return h;
if (ascii_basename(name)) {
const std::string path = narrow(name);
if (HMODULE h = owned_handle(path)) return h;
const std::string dll = module_name(path);
if (!dll.empty())
if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h);
} else {
// Outside ASCII: by the wide basename (narrow() would turn it into '?'), as for load_library_w.
const std::wstring dll = module_name_w(name);
if (!dll.empty())
if (void* h = find_loaded_bundled_module_w(dll)) return reinterpret_cast<HMODULE>(h);
}
return GetModuleHandleW(name);
}

bool resolve_module_filename(HMODULE module, std::wstring& wide) {
if (!module) return bundled_module_path(nullptr, wide);
if (const Pseudo* p = pseudo(module)) {
wide.assign(p->dll.begin(), p->dll.end());
return true;
}
return bundled_module_path(module, wide);
}

template <typename CharT>
DWORD copy_module_filename(const std::basic_string<CharT>& text, CharT* out, DWORD size) {
if (size == 0) {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
if (!out) {
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
const size_t n = text.size();
if (n >= static_cast<size_t>(size)) {
const size_t copy = static_cast<size_t>(size) - 1;
if (copy) std::memcpy(out, text.data(), copy * sizeof(CharT));
out[copy] = 0;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return size;
}
std::memcpy(out, text.data(), (n + 1) * sizeof(CharT));
SetLastError(ERROR_SUCCESS);
return static_cast<DWORD>(n);
}

DWORD WINAPI get_module_file_name_w(HMODULE module, LPWSTR filename, DWORD size) {
std::wstring wide;
if (!resolve_module_filename(module, wide)) return GetModuleFileNameW(module, filename, size);
return copy_module_filename(wide, filename, size);
}

DWORD WINAPI get_module_file_name_a(HMODULE module, LPSTR filename, DWORD size) {
std::wstring wide;
if (!resolve_module_filename(module, wide)) return GetModuleFileNameA(module, filename, size);
std::string ansi;
if (!wide.empty()) {
const int needed = WideCharToMultiByte(CP_ACP, 0, wide.data(), static_cast<int>(wide.size()), nullptr, 0,
nullptr, nullptr);
if (needed <= 0) return 0;
ansi.resize(static_cast<size_t>(needed));
WideCharToMultiByte(CP_ACP, 0, wide.data(), static_cast<int>(wide.size()), ansi.data(), needed, nullptr,
nullptr);
}
return copy_module_filename(ansi, filename, size);
}

FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) {
if (const Pseudo* p = pseudo(module)) {
if (!name) {
Expand Down Expand Up @@ -342,6 +405,8 @@ BOOL WINAPI disable_thread_calls(HMODULE module) {
{dll, "LoadLibraryExW", reinterpret_cast<void*>(&rt::shims::library_loader::load_library_ex_w)}, \
{dll, "GetModuleHandleA", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_a)}, \
{dll, "GetModuleHandleW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_w)}, \
{dll, "GetModuleFileNameA", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_file_name_a)}, \
{dll, "GetModuleFileNameW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_file_name_w)}, \
{dll, "GetProcAddress", reinterpret_cast<void*>(&rt::shims::library_loader::get_proc_address)}, \
{dll, "FreeLibrary", reinterpret_cast<void*>(&rt::shims::library_loader::free_library)}, \
{dll, "DisableThreadLibraryCalls", reinterpret_cast<void*>(&rt::shims::library_loader::disable_thread_calls)}
Expand Down
13 changes: 13 additions & 0 deletions runtime/shims/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,19 @@ bool owns_bundled_module_handle(void* module) {
return false;
}

bool bundled_module_path(void* module, std::wstring& path) {
for (const auto& entry : snapshot_bundled_loaders())
if (entry.ops->module_path && entry.ops->module_path(entry.ctx, module, path)) return true;
return false;
}

void* main_bundled_module() {
for (const auto& entry : snapshot_bundled_loaders())
if (entry.ops->main_module)
if (void* h = entry.ops->main_module(entry.ctx)) return h;
return nullptr;
}

void add_xmem_allocation_hooks(const void* owner, const XMemAllocationHooks* hooks) {
if (!owner || !hooks) return;
std::lock_guard lock(xmem_hooks_mutex());
Expand Down
5 changes: 5 additions & 0 deletions runtime/shims/shims.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ struct BundledModuleOps {
void* (*compat_module)(void* ctx, const std::wstring& basename, const std::wstring& name, bool load,
uint32_t flags, bool& handled);
bool (*owns_compat_handle)(void* ctx, void* module);
// Optional path and primary-executable queries for GetModuleFileName* and GetModuleHandle(nullptr). Either may be null.
bool (*module_path)(void* ctx, void* module, std::wstring& path);
void* (*main_module)(void* ctx);
};
void add_bundled_loader(void* ctx, const BundledModuleOps* ops);
void remove_bundled_loader(void* ctx);
Expand All @@ -90,6 +93,8 @@ bool owns_bundled_module_handle(void* module);
void* compat_module(const std::wstring& basename, const std::wstring& name, bool load, uint32_t flags,
bool& handled);
bool owns_compat_module_handle(void* module);
bool bundled_module_path(void* module, std::wstring& path);
void* main_bundled_module();

// Title-exported __XMemAllocationHooks__ (GDK XMEM_ALLOCATION_HOOKS): the console runtime routes internal
// allocations (including D3D12 descriptor heaps) through these callbacks when the mapped image exports the struct.
Expand Down
34 changes: 34 additions & 0 deletions runtime/src/pe_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,14 @@ bool bundled_owns_compat_handle(void* ctx, void* module) {
return static_cast<Loader*>(ctx)->owns_compat_handle(module);
}

bool bundled_module_path_op(void* ctx, void* module, std::wstring& path) {
return static_cast<Loader*>(ctx)->module_path(module, path);
}

void* bundled_main_module_op(void* ctx) {
return static_cast<Loader*>(ctx)->main_module_handle();
}

const shims::BundledModuleOps kBundledOps{
&bundled_find_loaded,
&bundled_load,
Expand All @@ -1367,6 +1375,8 @@ const shims::BundledModuleOps kBundledOps{
&bundled_load_w,
&bundled_compat_module,
&bundled_owns_compat_handle,
&bundled_module_path_op,
&bundled_main_module_op,
};

// Marks what a run-time load maps as a dynamic load for as long as it is in scope (the caller holds the mutex).
Expand Down Expand Up @@ -1635,6 +1645,24 @@ bool Loader::owns_module_handle(void* module_handle) const {
return false;
}

bool Loader::module_path(void* module_handle, std::wstring& out_path) const {
std::lock_guard lock(mutex_);
for (const Module* m : order_) {
if (module_handle ? (m->base == module_handle) : m->is_exe) {
out_path = m->full_path.native();
return true;
}
}
return false;
}

void* Loader::main_module_handle() const {
std::lock_guard lock(mutex_);
for (const Module* m : order_)
if (m->is_exe) return static_cast<void*>(m->base);
return nullptr;
}

// x64 has no frame-pointer chain to fall back on: without its .pdata registered, an exception raised in (or passing
// through) a mapped image can't be dispatched past that frame, and the process dies. Images the system loader maps
// are found through its own module list; ours are VirtualAlloc'd, so they are registered as dynamic function tables.
Expand Down Expand Up @@ -1742,6 +1770,12 @@ Module& Loader::map_image(const std::filesystem::path& path, const std::string&
m->name = name;
m->narrow_key = narrow_key;
m->path = path;
{
std::error_code ec;
const std::filesystem::path abs = std::filesystem::absolute(path, ec);
m->full_path = ec ? path : abs.lexically_normal();
}
m->is_exe = (nt->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0;
m->preferred_base = oh.ImageBase;
m->size = oh.SizeOfImage;
m->entry_rva = oh.AddressOfEntryPoint;
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/pe_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ struct Module {
std::string name; // UTF-8 basename for diagnostics and traces, not a lookup key
std::string narrow_key; // legacy lower-case narrow basename; empty when it cannot round-trip
std::filesystem::path path;
std::filesystem::path full_path; // absolute path captured at map time, for GetModuleFileName*
uint8_t* base = nullptr;
uint64_t preferred_base = 0;
uint32_t size = 0;
uint32_t entry_rva = 0;
bool is_exe = false;
bool relocated = false;
bool initialised = false;

Expand Down Expand Up @@ -145,6 +147,8 @@ class Loader {
void* runtime_compat_module(const std::wstring& basename, const std::wstring& name, bool load, uint32_t flags,
bool& handled);
bool owns_compat_handle(void* module);
bool module_path(void* module_handle, std::wstring& out_path) const;
void* main_module_handle() const;

private:
void initialise(Module& m);
Expand Down
Loading
Loading