loader: a run-time LoadLibrary takes the LEONIDA_COMPAT_DIR module too (RT-COMPAT-DIR-RUNTIME-LOADS) #1565

Open
github-bot wants to merge 2 commits from github-bot/gh-1565 into main AGit
Owner

@⁠Mike7581 on GitHub · 2026-11-25 04:42 UTC · original
Mike7581:rt-compat-dir-runtime-loads → main · +496 −37 · 8 files · 2 commits · open

Claimed in the coordination issue as: RT-COMPAT-DIR-RUNTIME-LOADS (#1562, since #14 is at the 2,500-comment cap)

What and why

LEONIDA_COMPAT_DIR only applied to static imports (runtime/README.md: "a run-time LoadLibrary of the same name does not yet"). A mapped image's LoadLibrary of a module the folder holds got the shim's pseudo-handle instead of the drop-in DLL, and GetProcAddress on the compat DLL's real handle looked the name up in the shim of the same module name first. One console module could then be served half by the drop-in and half by the shim, which the static step exists to prevent.

Now shims::BundledModuleOps gains compat_module / owns_compat_handle, answered by rt::Loader from the same compat_handle() its static step (resolve_compat) uses, so both get one HMODULE per module. In shims/library_loader.cpp:

  • LoadLibrary* checks the compat folder before the shim registry and the bundled package DLLs;
  • GetModuleHandle* answers a compat module's handle once loaded and NULL before (never the shim's pseudo-handle);
  • GetProcAddress on a compat handle gives only that DLL's exports;
  • FreeLibrary on it returns TRUE and keeps it loaded (it is the Loader's reference, static imports may be bound through it; compat modules already stay for the process lifetime).

A compat file Windows refuses fails that LoadLibrary with Windows' error (logged) instead of throwing into the title. runtime/README.md is updated.

Tested

  • loader_test now links shims/library_loader.cpp and gains test_compat_runtime_loads, eight cases with a synthetic drop-in DLL and the test shim module: without the folder the shim still answers; with it, GetModuleHandle is NULL before the load, LoadLibraryW returns the compat DLL, GetModuleHandle (with and without .dll) returns it after, GetProcAddress gives Answer (42) and not the shim's F, a static import in the same Loader binds to the same export, FreeLibrary keeps it loaded, and a file Windows refuses (error 193) fails the load without the shim or a throw. loader: 103 ok, 0 FAIL.
  • Full ctest: 376/377; the one failure is ws2_offline under -j 4 (loopback bind failures; it failed the same way here on a run without this change), which passes 3/3 run alone.

GPUs tested: n/a (CPU only). Built with MSVC (VS 2022 Build Tools 17.14) + Ninja via scripts\build_runtime.ps1, Windows 11 Pro 10.0.26200.

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)
**@⁠Mike7581** on GitHub · 2026-11-25 04:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1565) `Mike7581:rt-compat-dir-runtime-loads` → `main` · +496 −37 · 8 files · 2 commits · open Claimed in the coordination issue as: RT-COMPAT-DIR-RUNTIME-LOADS (#1562, since #14 is at the 2,500-comment cap) ### What and why `LEONIDA_COMPAT_DIR` only applied to static imports (`runtime/README.md`: "a run-time `LoadLibrary` of the same name does not yet"). A mapped image's `LoadLibrary` of a module the folder holds got the shim's pseudo-handle instead of the drop-in DLL, and `GetProcAddress` on the compat DLL's real handle looked the name up in the shim of the same module name first. One console module could then be served half by the drop-in and half by the shim, which the static step exists to prevent. Now `shims::BundledModuleOps` gains `compat_module` / `owns_compat_handle`, answered by `rt::Loader` from the same `compat_handle()` its static step (`resolve_compat`) uses, so both get one `HMODULE` per module. In `shims/library_loader.cpp`: - `LoadLibrary*` checks the compat folder before the shim registry and the bundled package DLLs; - `GetModuleHandle*` answers a compat module's handle once loaded and NULL before (never the shim's pseudo-handle); - `GetProcAddress` on a compat handle gives only that DLL's exports; - `FreeLibrary` on it returns TRUE and keeps it loaded (it is the Loader's reference, static imports may be bound through it; compat modules already stay for the process lifetime). A compat file Windows refuses fails that `LoadLibrary` with Windows' error (logged) instead of throwing into the title. `runtime/README.md` is updated. ### Tested - `loader_test` now links `shims/library_loader.cpp` and gains `test_compat_runtime_loads`, eight cases with a synthetic drop-in DLL and the test shim module: without the folder the shim still answers; with it, `GetModuleHandle` is NULL before the load, `LoadLibraryW` returns the compat DLL, `GetModuleHandle` (with and without `.dll`) returns it after, `GetProcAddress` gives `Answer` (42) and not the shim's `F`, a static import in the same Loader binds to the same export, `FreeLibrary` keeps it loaded, and a file Windows refuses (error 193) fails the load without the shim or a throw. `loader`: 103 ok, 0 FAIL. - Full `ctest`: 376/377; the one failure is `ws2_offline` under `-j 4` (loopback bind failures; it failed the same way here on a run without this change), which passes 3/3 run alone. GPUs tested: n/a (CPU only). Built with MSVC (VS 2022 Build Tools 17.14) + Ninja via `scripts\build_runtime.ps1`, Windows 11 Pro 10.0.26200. AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change)
LEONIDA_COMPAT_DIR only applied to static imports: runtime/README.md said "a run-time LoadLibrary of the
same name does not yet". A mapped image's LoadLibrary of a module the folder holds got the shim's
pseudo-handle instead of the drop-in DLL, and GetProcAddress on the compat DLL's real handle looked the
name up in the shim of the same module name first, so one console module could be served half by the
drop-in and half by the shim, which the static step exists to prevent.

Now shims::BundledModuleOps gains compat_module / owns_compat_handle, which rt::Loader answers from the
same compat_handle() its static step (resolve_compat) uses, so both get one HMODULE per module. In
shims/library_loader.cpp:
  - LoadLibrary* checks the compat folder before the shim registry and the bundled package DLLs;
  - GetModuleHandle* answers a compat module's handle once it is loaded and NULL before (never the shim's
    pseudo-handle);
  - GetProcAddress on a compat handle gives only that DLL's exports;
  - FreeLibrary on it returns TRUE and keeps it loaded (it is the Loader's reference, static imports may
    be bound through it; compat modules stay for the process lifetime).
A compat file Windows refuses fails that LoadLibrary with Windows' error (logged) instead of throwing
into the title.

loader_test links library_loader.cpp and gains test_compat_runtime_loads: eight cases with a synthetic
drop-in DLL and the test shim module, no game data.
Review follow-up for #1565:
- LoadLibraryEx with DONT_RESOLVE_DLL_REFERENCES on a LEONIDA_COMPAT_DIR module fails with ERROR_NOT_SUPPORTED, before loading or returning a loaded handle, as for bundled DLLs.
- A compat DLL that Windows loaded as another compat DLL's dependency is recognised: compat_handle() finds it by its full path, and owns_compat_handle() by its folder (read outside mutex_, since GetModuleFileNameW can take the loader lock), so GetModuleHandle and LoadLibrary return it and GetProcAddress never fills in a missing export from the shim.
- A name with a directory gets the compat DLL only when it is that file; any other path goes on as without the folder.
- Names outside ASCII reach the compat lookup (it runs before the ASCII / wide split).
- The folder's file names are listed once per Loader, so lookups no longer touch the disk.

loader_test: test_compat_runtime_edges adds six cases for the first four points, through LoadLibraryExA/W, LoadLibraryW, GetModuleHandleW and GetProcAddress; all six fail on 582e14d. The listing has no functional test.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:56:49 +00:00
Author
Owner

@⁠Andretuta on GitHub · 2026-11-25 08:35 UTC · original · requested changes

The new path works: loader_test, including the new test_compat_runtime_loads, passes. A run-time load of a compat module now returns the same handle a static import uses, and its exports are its own.

Two cases still differ from what Windows does: LoadLibraryEx flags, and compat DLLs that Windows loaded as a dependency. There are also a few nits. Line numbers refer to the PR head 582e14d.

Should-fix: the compat branch ignores LoadLibraryEx flags

load_shim_or_bundled (library_loader.cpp:152) gets flags, but compat_module → compat_handle (pe_loader.cpp:2074) always does a normal LoadLibraryExW. We tested with a synthetic DLL whose initializer refuses normal attachment, loaded with DONT_RESOLVE_DLL_REFERENCES:

Run Result
Windows maps it without running the initializer
Through the shim runs the initializer, returns NULL with ERROR_DLL_INIT_FAILED

The bundled branch already refuses this flag (pe_loader.cpp:1530).

Suggested fix: pass the flags through. Once the compat folder claims the name, refuse the flag the same way, before loading or returning a cached handle.

void* Loader::runtime_compat_module(const std::string& dll_lower, bool load, uint32_t flags, bool& handled) {
    ...
    if (exists && (flags & DONT_RESOLVE_DLL_REFERENCES)) {
        SetLastError(ERROR_NOT_SUPPORTED);  // as the bundled branch
        return nullptr;
    }

Test it through both LoadLibraryExA and LoadLibraryExW.

Should-fix: a compat DLL that Windows loaded as a dependency is not recognized

LoadLibraryExW(..., LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR) lets one compat DLL pull in another one from the same folder. The dependency is then live, but it is not in compat_modules_. This causes two problems:

  • GetModuleHandle on it returns NULL with ERROR_MOD_NOT_FOUND. We reproduced this with a native preload.
  • GetProcAddress on its handle does not count it as a compat module (library_loader.cpp:298). A name the DLL lacks is then filled in from the shim of the same module name, which is the mix the static path forbids.

Suggested fix: when the cache misses, look the module up by its full compat path, and record it.

// compat_handle, cache miss, after is_regular_file:
HMODULE live = nullptr;
if (GetModuleHandleExW(0, candidate.c_str(), &live) && live) {  // takes a reference, kept for the process
    compat_modules_.emplace(key, live);
    return live;
}
if (!load) return nullptr;  // in the folder but not loaded: ERROR_MOD_NOT_FOUND, as now

Add a test with a compat DLL that imports a second compat DLL lacking an export the shim provides.

Nits

  • Fully qualified paths (library_loader.cpp:150, :221): the compat lookup strips the directory. In a synthetic collision, an explicit path gave the DLL exporting 43 on Windows, but the compat DLL exporting 42 through the shim. The shim path already matches by basename on main, so this extends existing behaviour. Consider limiting compat substitution to bare names, and to a full path that is the compat file itself.
  • Non-ASCII wide names (library_loader.cpp:192, :210, :250): these go straight to the bundled lookup and skip the compat folder. Run the compat lookup before the ASCII/wide split, with the wide basename parser.
  • Disk check on every call (pe_loader.cpp:2064, :2098): with LEONIDA_COMPAT_DIR set, every run-time LoadLibrary/GetModuleHandle of any name that misses the cache calls std::filesystem::is_regular_file under the loader's mutex. Snapshot the folder's file names once per Loader, or cache the misses.

What was checked

  • Merge with main: clean.
  • Full build: OK. CTest: 146/150 pass, including loader (with the new compat run-time cases), loader_tls*, run_tiny_bundled_load and run_tiny_gdk_load. The 4 failures (root_sig, xmem, xmem_no_write_watch, root_signature_oracle) are the same on the other branches tested and are in code this PR does not touch.
  • Synthetic DLL probes, comparing Windows with the shim:
    • DONT_RESOLVE_DLL_REFERENCES
    • a dependency loaded by Windows
    • an explicit-path collision
    • a non-ASCII basename

Review done with AI assistance (Claude Code and GPT-6.2 Sol, cross-checked against each other). Every finding above was reproduced with a test probe.

**@⁠Andretuta** on GitHub · 2026-11-25 08:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1565#pullrequestreview-6054229288) · **requested changes** The new path works: `loader_test`, including the new `test_compat_runtime_loads`, passes. A run-time load of a compat module now returns the same handle a static import uses, and its exports are its own. Two cases still differ from what Windows does: `LoadLibraryEx` flags, and compat DLLs that Windows loaded as a dependency. There are also a few nits. Line numbers refer to the PR head `582e14d`. ### Should-fix: the compat branch ignores `LoadLibraryEx` flags `load_shim_or_bundled` (`library_loader.cpp:152`) gets `flags`, but `compat_module` → `compat_handle` (`pe_loader.cpp:2074`) always does a normal `LoadLibraryExW`. We tested with a synthetic DLL whose initializer refuses normal attachment, loaded with `DONT_RESOLVE_DLL_REFERENCES`: | Run | Result | | --- | --- | | Windows | maps it without running the initializer | | Through the shim | runs the initializer, returns NULL with `ERROR_DLL_INIT_FAILED` | The bundled branch already refuses this flag (`pe_loader.cpp:1530`). Suggested fix: pass the flags through. Once the compat folder claims the name, refuse the flag the same way, before loading or returning a cached handle. ```cpp void* Loader::runtime_compat_module(const std::string& dll_lower, bool load, uint32_t flags, bool& handled) { ... if (exists && (flags & DONT_RESOLVE_DLL_REFERENCES)) { SetLastError(ERROR_NOT_SUPPORTED); // as the bundled branch return nullptr; } ``` Test it through both `LoadLibraryExA` and `LoadLibraryExW`. ### Should-fix: a compat DLL that Windows loaded as a dependency is not recognized `LoadLibraryExW(..., LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)` lets one compat DLL pull in another one from the same folder. The dependency is then live, but it is not in `compat_modules_`. This causes two problems: - `GetModuleHandle` on it returns NULL with `ERROR_MOD_NOT_FOUND`. We reproduced this with a native preload. - `GetProcAddress` on its handle does not count it as a compat module (`library_loader.cpp:298`). A name the DLL lacks is then filled in from the shim of the same module name, which is the mix the static path forbids. Suggested fix: when the cache misses, look the module up by its full compat path, and record it. ```cpp // compat_handle, cache miss, after is_regular_file: HMODULE live = nullptr; if (GetModuleHandleExW(0, candidate.c_str(), &live) && live) { // takes a reference, kept for the process compat_modules_.emplace(key, live); return live; } if (!load) return nullptr; // in the folder but not loaded: ERROR_MOD_NOT_FOUND, as now ``` Add a test with a compat DLL that imports a second compat DLL lacking an export the shim provides. ### Nits - **Fully qualified paths (`library_loader.cpp:150`, `:221`):** the compat lookup strips the directory. In a synthetic collision, an explicit path gave the DLL exporting 43 on Windows, but the compat DLL exporting 42 through the shim. The shim path already matches by basename on `main`, so this extends existing behaviour. Consider limiting compat substitution to bare names, and to a full path that is the compat file itself. - **Non-ASCII wide names (`library_loader.cpp:192`, `:210`, `:250`):** these go straight to the bundled lookup and skip the compat folder. Run the compat lookup before the ASCII/wide split, with the wide basename parser. - **Disk check on every call (`pe_loader.cpp:2064`, `:2098`):** with `LEONIDA_COMPAT_DIR` set, every run-time `LoadLibrary`/`GetModuleHandle` of any name that misses the cache calls `std::filesystem::is_regular_file` under the loader's mutex. Snapshot the folder's file names once per `Loader`, or cache the misses. ### What was checked - Merge with `main`: clean. - Full build: OK. CTest: 146/150 pass, including `loader` (with the new compat run-time cases), `loader_tls*`, `run_tiny_bundled_load` and `run_tiny_gdk_load`. The 4 failures (`root_sig`, `xmem`, `xmem_no_write_watch`, `root_signature_oracle`) are the same on the other branches tested and are in code this PR does not touch. - Synthetic DLL probes, comparing Windows with the shim: - `DONT_RESOLVE_DLL_REFERENCES` - a dependency loaded by Windows - an explicit-path collision - a non-ASCII basename Review done with AI assistance (Claude Code and GPT-6.2 Sol, cross-checked against each other). Every finding above was reproduced with a test probe.
Author
Owner

@⁠Mike7581 on GitHub · 2026-11-25 18:18 UTC · original

Thanks for the probes. All points are addressed in 2cca47b:

  • LoadLibraryEx flags reach the compat branch; DONT_RESOLVE_DLL_REFERENCES on a compat module fails with ERROR_NOT_SUPPORTED before loading or returning a loaded handle, as the bundled branch does (A and W tested).
  • A compat DLL Windows loaded as another compat DLL's dependency is recognised: compat_handle finds it by its full path (GetModuleHandleExW, reference kept), and owns_compat_handle by its folder (read outside mutex_, since GetModuleFileNameW can take the loader lock). GetModuleHandle/LoadLibrary return it, and GetProcAddress no longer fills in a missing export from the shim.
  • Nits: a name with a directory gets the compat DLL only when it is that file (a full path to another file of a shim-owned name still gets the shim, as on main); the compat lookup runs before the ASCII/wide split, so non-ASCII basenames reach it; the folder's file names are listed once per Loader, so lookups no longer touch the disk.

test_compat_runtime_edges adds six cases, including a compat DLL importing a second one that lacks the shim's export; all six fail on 582e14d. Full build and CTest pass here (377/377, RTX 4050).

Written with AI assistance (Claude Code, Claude Opus 5.5) and reviewed with GPT-6 Sol High; every change above was checked against the code and the tests were run locally.

**@⁠Mike7581** on GitHub · 2026-11-25 18:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1565#issuecomment-7170721789) Thanks for the probes. All points are addressed in 2cca47b: - `LoadLibraryEx` flags reach the compat branch; `DONT_RESOLVE_DLL_REFERENCES` on a compat module fails with `ERROR_NOT_SUPPORTED` before loading or returning a loaded handle, as the bundled branch does (A and W tested). - A compat DLL Windows loaded as another compat DLL's dependency is recognised: `compat_handle` finds it by its full path (`GetModuleHandleExW`, reference kept), and `owns_compat_handle` by its folder (read outside `mutex_`, since `GetModuleFileNameW` can take the loader lock). `GetModuleHandle`/`LoadLibrary` return it, and `GetProcAddress` no longer fills in a missing export from the shim. - Nits: a name with a directory gets the compat DLL only when it is that file (a full path to another file of a *shim-owned* name still gets the shim, as on `main`); the compat lookup runs before the ASCII/wide split, so non-ASCII basenames reach it; the folder's file names are listed once per `Loader`, so lookups no longer touch the disk. `test_compat_runtime_edges` adds six cases, including a compat DLL importing a second one that lacks the shim's export; all six fail on `582e14d`. Full build and CTest pass here (377/377, RTX 4050). Written with AI assistance (Claude Code, Claude Opus 5.5) and reviewed with GPT-6 Sol High; every change above was checked against the code and the tests were run locally.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1565/head:github-bot/gh-1565'
git switch 'github-bot/gh-1565'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1565'
git switch 'github-bot/gh-1565'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1565'
git switch 'github-bot/gh-1565'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1565'
git switch 'main'
git merge --squash 'github-bot/gh-1565'
git switch 'main'
git merge --ff-only 'github-bot/gh-1565'
git switch 'main'
git merge 'github-bot/gh-1565'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.