Repository navigation
pe_loader: a static load's DllMain gets a non-NULL lpvReserved - #7307
Merged
Merged
Conversation
Loader::initialise(Module&) called every DllMain(DLL_PROCESS_ATTACH) with lpvReserved NULL. Microsoft's DllMain reference: "If fdwReason is DLL_PROCESS_ATTACH, lpvReserved is NULL for dynamic loads and non-NULL for static loads." The executable gta6rt maps and the DLLs it imports are the static case; a run-time LoadLibrary (load_runtime_module) and a GetProcAddress forwarder (resolve_module_export) are dynamic. Each Module now records whether it was mapped inside a run-time load, and a static module's DllMain gets a pointer to a CONTEXT captured for the call. TLS callbacks and the process detach in ~Loader (an unload: NULL) are unchanged. loader_test: a host load() + initialise() must give a non-NULL lpvReserved (fails on main), the same image through load_runtime_module NULL. The lifecycle observer checks lpvReserved on detach only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devnull34
approved these changes
Nov 23, 2026
devnull34
left a comment
There was a problem hiding this comment.
Verified on Windows 11 (MSVC RelWithDebInfo):
- Built
loader_testcleanly. loader_test.exepassed all 88 checks (0 failures, 100% CTest pass):- Validated static load's
DllMain(DLL_PROCESS_ATTACH)receives non-nulllpvReserved(context pointer), matching Windows PE loader behavior. - Validated dynamic/run-time loads (
load_runtime_module) and detach operations continue receiving nulllpvReserved.
- Validated static load's
- Whitespace clean (
git diff HEAD~1..HEAD --check).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Claim: #7049 (comment) (new RT item, PE loader on the
gta6rt --runboot path)Cause
Loader::initialise(Module&)(runtime/src/pe_loader.cpp:1154onmain08c1aa634) called everyDllMain(DLL_PROCESS_ATTACH)withlpvReserved == NULL. That includes the start-up graph: the imagegta6rt --run/--initmaps withLoader::load()and every bundled DLL it imports, which Windows would load statically.Reference
Microsoft's DllMain page, lpvReserved: "If fdwReason is DLL_PROCESS_ATTACH, lpvReserved is NULL for dynamic loads and non-NULL for static loads." (The detach side, "NULL if FreeLibrary has been called or the DLL load failed", is what
~Loaderalready does, since it unmaps the images after the detach.)Fix
Module::dynamic_load: set inLoader::load()(beforebind_imports, so a module's dependencies get the same answer) while a run-time load is in progress:load_runtime_module()(a guestLoadLibrary*of a bundled DLL) and the forwarder path ofresolve_module_export()(a guestGetProcAddressthat maps another bundled DLL). A small scope guard on a counter under the loader mutex does the marking.CONTEXTcaptured for the call (what Windows points it at is its own; this keeps the pointer readable). A dynamic module's still gets NULL.This is parity with the documented contract, not a measured retail failure: nothing here shows that a package DLL reads
lpvReserved(the MSVC CRT's DllMain does not use it on attach).Tests
runtime/test/loader_test.cpp, hand-written PE images (no game files), DllMain jumping to a host observer:a static load's DllMain gets a non-NULL lpvReserved:Loader::load()+initialise(). Fails onmain("lpvReserved was NULL"), passes with the fix.a run-time load's DllMain gets a NULL lpvReserved: the same image throughload_runtime_module(). Passes before and after.lpvReserved; it now checks it onDLL_PROCESS_DETACHonly, which is what its detach checks are about.Run on Linux (clang-cl 23 + xwin cross build,
RelWithDebInfo,-DLEONIDA_SDL3_FETCH=ON) under Wine 11.20, RTX 5080:loader_test88 checks, 0 failed (main: 1 failed). Fullctest -C RelWithDebInfo -j1 --timeout 300 --output-on-failure: 5 failures, the same 5 asmainin the same prefix (gameinput_host,device_proxy,device_proxy_trace,file_system,run_tiny_game_abort_exit_codes, all environment/Wine).python -I scripts/check_no_game_data.pyon the changed files: clean. Not tested on Windows and not with the retail executable.pe_loader.cppis Windows-only, so the native Linux build does not compile it.git merge-treeagainst the open loader PRs (#3627, #3981, #3790, #5805, #2669, #4313, #2075, #6754, #5731, #7202, #5677): no conflict this change adds beyond the ones they already have withmain.AI-assisted (Claude Code)
cc @shayderrr
🤖 Generated with Claude Code