Skip to content

Loader: forwarders honour LEONIDA_COMPAT_DIR; CLZERO scan keeps VirtualSize 0 sections (RT-LOADER-FORWARDER-COMPAT-CLZERO-SCAN) - #2528

Open
Tyrannys wants to merge 5 commits into
ProjectLeonida:mainfrom
Tyrannys:claude/loader-forwarder-compat
Open

Tyrannys wants to merge 5 commits into
ProjectLeonida:mainfrom
Tyrannys:claude/loader-forwarder-compat

Conversation

@Tyrannys

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-LOADER-FORWARDER-COMPAT-CLZERO-SCAN (#1608 (comment)). Found by @kevincommits, item 5 of #1608 (comment). Thanks!

What and why

Two loader bugs in runtime/src/pe_loader.cpp:

  1. Forwarders skipped LEONIDA_COMPAT_DIR. resolve() asks resolve_compat() first, so a drop-in DLL the player put in LEONIDA_COMPAT_DIR owns all of its imports: a module is never served half by a shim and half by the drop-in, which would not share state. resolve_forwarder() never asked it. So an export forwarded into such a module still went to the shim, a bundled copy or System32.
    Fix: resolve_forwarder() asks resolve_compat() before the shim lookup, with the same key and file name rule it already uses (".dll" added when the forwarder string leaves it out). A symbol the drop-in lacks stays unbound, as it does for a direct import. The ordinal is now parsed before the lookups, because resolve_compat() needs it.
  2. The CLZERO scan dropped sections with VirtualSize == 0. protect_sections() sized the scan of an executable section as min(VirtualSize, SizeOfRawData), which is 0 for such a section, so its CLZERO loops were never patched on a CPU without CLZERO. map_image() takes a virtual size of 0 as the raw size and copies those bytes.
    Fix: both now use section_file_bytes() (in pe_loader.h), so the scan covers exactly the bytes map_image() copied.

Item 4 of the later finding (#1608 (comment): clzero_patch::patch() leaves VirtualProtect unchecked after patching) is not here: open #2036 already fixes it, so it is left to that PR.

Tested

  • loader_test, in test_compat_modules (CPU only, synthetic images):
    • A bundled module's export forwarded into the compat DLL binds to the drop-in's export as Host, and it runs.
    • A forwarder into a compat DLL that lacks the symbol (testshim.F, which the test shim table has) is left unimplemented and does not reach the shim. Without LEONIDA_COMPAT_DIR the same forwarder binds to the shim.
    • On main the second case fails ("F was not left unimplemented"); with the fix all 102 checks pass. The first case also passes on main here, because Windows finds the already loaded compat DLL by name through the System32 search, so the second case is the one that shows the bug.
  • section_file_bytes(): static_asserts in loader_test.cpp, including a virtual size of 0. The CLZERO path itself only runs on a CPU without CLZERO; the test machine has it (Zen 5), so that path is covered by the helper it now shares with map_image().
  • Built with MSVC 17 (/W4), Release; no new warnings.

GPUs tested: n/a (CPU test).

AI-assisted: yes (Claude Code)

Checklist

🤖 Generated with Claude Code

IlyassRS3 and others added 2 commits November 25, 2026 00:50
…alSize 0 sections

resolve() asks resolve_compat() first, so a drop-in DLL in
LEONIDA_COMPAT_DIR owns all of its imports. resolve_forwarder() did not,
so an export forwarded into such a module still reached its shim, a
bundled copy or System32: the module was served half by the drop-in and
half by the shim. It now asks resolve_compat() before the shim lookup;
a symbol the drop-in lacks stays unbound, as for a direct import.

protect_sections() sized the CLZERO scan of an executable section as
min(VirtualSize, SizeOfRawData), which is 0 for a section with no
virtual size, so its loops were never patched. map_image() takes a
virtual size of 0 as the raw size; both now share section_file_bytes().

Found by kevincommits (ProjectLeonida#1608).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timbot103

Copy link
Copy Markdown
Contributor

Reviewed at head 472c9638c on Windows 11 (MSVC 2022 19.44).

Verification

  • Built loader_test, pe_loader_test, and gta6rt.
  • ctest -R "^(loader|pe_loader)$": 2/2 passed.

Findings (Request changes)

  1. Owned LEONIDA_COMPAT_DIR export miss in a forwarder can still fall through to System32 (pe_loader.cpp:2017-2024, 2113-2129):
    Direct imports handle an owned compat miss by immediately returning shims::make_unimplemented_thunk(dll, sym) (pe_loader.cpp:2096-2099). In resolve_forwarder(), an owned compat miss returns nullptr, which stops fallback inside resolve_forwarder(), but the caller in resolve() (pe_loader.cpp:2113-2119) treats nullptr the same as any lookup miss and falls through to LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32) (pe_loader.cpp:2122). If a System32 DLL shares the forwarding DLL's name and exports sym, it will silently supply a host implementation despite the compat DLL owning the forwarded module. Returning a distinct owned-miss status (e.g., via an out flag or enum) so resolve() goes directly to make_unimplemented_thunk keeps forwarded and direct compat ownership consistent.
  2. Non-.dll explicit suffixes get .dll appended for compat lookup (pe_loader.cpp:2005, 2019-2020):
    Because shim_key appends ".dll" whenever the module name does not end with ".dll", a forwarder naming a non-.dll extension (e.g., winspool.drv) probes winspool.drv.dll in LEONIDA_COMPAT_DIR, whereas the bundled lookup (pe_loader.cpp:2035) tries module before module + ".dll". Preserving an existing extension (or checking module before module + ".dll" as bundled lookup does) would avoid surprising misses.

The CLZERO scan raw-byte calculation (pe_loader.cpp:2217-2221) cleanly matches the mapper (1737-1743) and clamps to the mapped image boundary.

AI-assisted review + local MSVC verification.

… keep a forwarder's own extension

From review (timbot103):
- resolve_forwarder() returned null for a LEONIDA_COMPAT_DIR module that
  owns the forwarded name but lacks the export, and resolve() then went
  on to LoadLibraryEx of the forwarding module's name in System32, so a
  System32 namesake could still supply it. resolve_forwarder() now
  reports that miss (compat_miss) and resolve() binds a failure thunk,
  as it does for a direct import of the compat DLL.
- The compat file of a forwarded module is looked up as the bundled
  module is: the name as given when it has its own extension
  (winspool.drv), then with .dll, instead of always appending .dll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Tyrannys

Copy link
Copy Markdown
Contributor Author

@timbot103 thank you, both findings were right. They are fixed in e2f9c31:

  1. Owned compat miss in a forwarder. resolve_forwarder() now reports, through a new compat_miss out flag, that a LEONIDA_COMPAT_DIR module owns the forwarded name but lacks the export. resolve() then binds a failure thunk (make_unimplemented_thunk(dll, sym)), as it does for a direct import of the compat DLL, instead of going on to the forwarding module's System32 namesake. The flag is passed through forwarder chains.
  2. Extensions. The compat file of a forwarded module is now looked up the way the bundled module is: the name as given when it has its own extension (winspool.drv), then with .dll. It is no longer always name + ".dll".

New loader_test cases, both failing at the previous head (472c963) and passing now (104 checks, 0 failed):

  • A bundled version.dll whose GetFileVersionInfoSizeA forwards to a compat DLL that lacks the target. The import is unimplemented; before, it bound to System32's export.
  • A forwarder to testcompat.drv.Answer with testcompat.drv in the compat folder. It binds to that file's export, which returns 42.

AI-assisted (Claude Code).

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 28, 2026

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed e2f9c316e and verified the loader test suites on Windows 11.

Code audit:

  • runtime/src/pe_loader.h, runtime/src/pe_loader.cpp: section_file_bytes() cleanly unifies map_image() and protect_sections() so executable sections with VirtualSize == 0 use SizeOfRawData during the CLZERO scan.
  • resolve_forwarder() checks LEONIDA_COMPAT_DIR before shims and bundled modules (preserving an explicit extension like .drv before appending .dll), and propagates compat_miss so a compat drop-in missing a forwarded export binds a failure thunk instead of falling through to System32.

Verification (Windows 11, MSVC 19.44, Ninja Release):

  • Built loader_test and pe_loader_test.
  • ctest -R '^(loader|pe_loader)$': 2/2 passed (loader, pe_loader).
  • python -I scripts/check_no_game_data.py: passed.

AI-assisted (Antigravity, Gemini).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Hi @Tyrannys, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/src/pe_loader.h), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

MoonEyes and others added 2 commits November 28, 2026 21:15
…-hardening

bcpack_tool: single-pass profile, history overwrite guard, and atomic output (BCPACK-TOOL-HARDENING)
…untime/src/pe_loader.h)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 28, 2026
@OreoFR

OreoFR commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR

AI-assisted (Claude Code, Claude Opus 5.5).

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Native Windows 11 + MSVC 19.44 Verification (0cceb06ee8 + origin/main adfb3821a2)

Verdict: PASS (0 blockers, 0 /W4 warnings)

Environment & Merge Check

  • Host: Windows 11 (10.0.26200), MSVC 19.44.35228 (x64, /W4 /permissive- /utf-8 /O2 /EHsc)
  • Merge hygiene: Clean merge of rebased head 0cceb06ee8 onto origin/main (adfb3821a2, git merge-tree --write-tree rc=0), and clean combined merge alongside #1600 (runtime/src/pe_loader.cpp, runtime/test/loader_test.cpp).

Red -> Green Results

  • Red (origin/main runtime/src/pe_loader.cpp + PR #2528 runtime/src/pe_loader.h & runtime/test/loader_test.cpp):
    • Exited 1, failing all 3 forwarder-compat isolation checks:
      • FAIL a forwarder into a compat DLL that lacks the symbol is unimplemented, not shimmed: F was not left unimplemented
      • FAIL a compat DLL's missing forwarded export is not taken from a System32 namesake: the import was bound
      • FAIL a forwarder to a compat module with its own extension finds that file: the forwarded import was not bound as Host
  • Green (PR #2528 + origin/main):
    • loader_test.exe (with gta6rt_tls_host.dll, linked /OPT:NOREF /BASE:0x10000000000): PASS (120 checks, 0 failed, 10.55 s, 0 /W4 warnings).
    • test_pe_loader_bounds.exe: PASS (test_pe_loader_bounds: ALL 60 CHECKS PASSED, 0.94 s).

Diff Audit Notes

  • resolve_forwarder now matches resolve_import's LEONIDA_COMPAT_DIR lookup order and compat_miss short-circuit (so an owned compat DLL missing a forwarded export gets an unimplemented thunk instead of falling through to a shim or System32 namesake), and protect_sections uses section_file_bytes (VirtualSize == 0 -> SizeOfRawData) consistently with map_image.
  • Minor non-blocking test note: rt::section_file_bytes(0, 0x200) == 0x200 is covered via static_assert; an end-to-end synthetic VirtualSize == 0 executable section CLZERO scan case could be added later if desired.

AI-assisted.

@timbot103 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static audit of 0cceb06ee against 48bae6cad. Request changes on the CLZERO scan expansion.

  • Major (runtime/src/pe_loader.cpp:2358, runtime/src/clzero_patch.h:55) The new scan range feeds zero-VirtualSize sections to a byte-pattern matcher that can rewrite inline data or part of an instruction. Gate rewrites on verified instruction boundaries, prefixes and entry points.
  • Major (runtime/src/clzero_patch.h:117) A destination fault while RCX is pushed can make Win64 unwind through saved RCX as if it were the return address. Keep affected sites on instruction emulation until stub faults can unwind correctly.
  • Major, baseline (runtime/src/pe_loader.cpp:2076) The bundled fallback still accepts a path after the compat lookup rejects it. Validate the forwarder module basename before any lookup.
  • Minor, baseline (runtime/src/pe_loader.cpp:775) The MSVC /GA TLS scanner still skips raw executable bytes when VirtualSize is zero. Reuse section_file_bytes() there.

The new import cases cover compat precedence and owned misses. Please cover the expanded scan with a synthetic zero-VirtualSize PE on Windows without CLZERO, including a destination write fault. AI-assisted static review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants