diff options
| author | cynbun <74268930+cynbun@users.noreply.github.com> | 2026-11-23 17:30:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 10:30:32 -0600 |
| commit | e0816d6af23f42353a3939cf4c228675c6e31305 (patch) | |
| tree | 23c4ca7bf398ba609d3ba1d12f4aaa60dc8abf2b /runtime/test/loader_test.cpp | |
| parent | 14b2e4462fb969ea4ae5c7847db86ed1b64ce6cf (diff) | |
pe_loader: a static load's DllMain gets a non-NULL lpvReserved (#7307)
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>
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index 3ed83747a..aba37cdf3 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -684,9 +684,18 @@ bool g_use_fls = false; | |||
| 684 | DWORD g_fls_index = FLS_OUT_OF_INDEXES; | 684 | DWORD g_fls_index = FLS_OUT_OF_INDEXES; |
| 685 | int g_fls_callbacks = 0; | 685 | int g_fls_callbacks = 0; |
| 686 | 686 | ||
| 687 | // DllMain's lpvReserved for its last DLL_PROCESS_ATTACH, and whether one was seen. | ||
| 688 | void* g_attach_reserved = nullptr; | ||
| 689 | bool g_attach_seen = false; | ||
| 690 | |||
| 687 | void observe_notification(void* base, DWORD reason, void* reserved, bool tls) { | 691 | void observe_notification(void* base, DWORD reason, void* reserved, bool tls) { |
| 688 | g_notifications.push_back({base, reason, tls}); | 692 | g_notifications.push_back({base, reason, tls}); |
| 689 | if (reserved) g_detach_state_live = false; | 693 | if (reason == DLL_PROCESS_ATTACH && !tls) { |
| 694 | g_attach_reserved = reserved; | ||
| 695 | g_attach_seen = true; | ||
| 696 | } | ||
| 697 | // ~Loader unmaps the image after the detach, which is FreeLibrary's case: lpvReserved is NULL there. | ||
| 698 | if (reason == DLL_PROCESS_DETACH && reserved) g_detach_state_live = false; | ||
| 690 | if (reason != DLL_PROCESS_DETACH) return; | 699 | if (reason != DLL_PROCESS_DETACH) return; |
| 691 | for (const auto& m : g_live_modules) { | 700 | for (const auto& m : g_live_modules) { |
| 692 | MEMORY_BASIC_INFORMATION info{}; | 701 | MEMORY_BASIC_INFORMATION info{}; |
| @@ -732,6 +741,8 @@ void reset_notifications() { | |||
| 732 | g_fail_attach = false; | 741 | g_fail_attach = false; |
| 733 | g_use_fls = false; | 742 | g_use_fls = false; |
| 734 | g_fls_callbacks = 0; | 743 | g_fls_callbacks = 0; |
| 744 | g_attach_reserved = nullptr; | ||
| 745 | g_attach_seen = false; | ||
| 735 | } | 746 | } |
| 736 | 747 | ||
| 737 | void expect_notifications(const char* what, const std::vector<Notification>& expected) { | 748 | void expect_notifications(const char* what, const std::vector<Notification>& expected) { |
| @@ -863,6 +874,37 @@ void test_process_detach_without_entry() { | |||
| 863 | reset_notifications(); | 874 | reset_notifications(); |
| 864 | } | 875 | } |
| 865 | 876 | ||
| 877 | // DllMain docs (learn.microsoft.com/windows/win32/dlls/dllmain): "If fdwReason is DLL_PROCESS_ATTACH, lpvReserved is | ||
| 878 | // NULL for dynamic loads and non-NULL for static loads." Loader::load() from the host is the process-start graph (the | ||
| 879 | // executable and the DLLs it imports), a run-time LoadLibrary of a bundled DLL is a dynamic load. | ||
| 880 | void test_attach_reserved() { | ||
| 881 | reset_notifications(); | ||
| 882 | try { | ||
| 883 | rt::Loader loader(g_dir); | ||
| 884 | loader.load(write_image(make_lifecycle_image())); | ||
| 885 | loader.initialise(); | ||
| 886 | if (!g_attach_seen) failed("a static load's DllMain gets a non-NULL lpvReserved", "DllMain was not called"); | ||
| 887 | else if (!g_attach_reserved) failed("a static load's DllMain gets a non-NULL lpvReserved", "lpvReserved was NULL"); | ||
| 888 | else pass("a static load's DllMain gets a non-NULL lpvReserved"); | ||
| 889 | } catch (const std::exception& e) { | ||
| 890 | failed("a static load's DllMain gets a non-NULL lpvReserved", e.what()); | ||
| 891 | } | ||
| 892 | |||
| 893 | reset_notifications(); | ||
| 894 | try { | ||
| 895 | write_image_as(make_lifecycle_image(), "attach_reserved_dynamic.dll"); | ||
| 896 | rt::Loader loader(g_dir); | ||
| 897 | bool handled = false; | ||
| 898 | void* h = loader.load_runtime_module("attach_reserved_dynamic.dll", 0, handled); | ||
| 899 | if (!handled || !h) failed("a run-time load's DllMain gets a NULL lpvReserved", "the run-time load failed"); | ||
| 900 | else if (!g_attach_seen) failed("a run-time load's DllMain gets a NULL lpvReserved", "DllMain was not called"); | ||
| 901 | else if (g_attach_reserved) failed("a run-time load's DllMain gets a NULL lpvReserved", "lpvReserved was non-NULL"); | ||
| 902 | else pass("a run-time load's DllMain gets a NULL lpvReserved"); | ||
| 903 | } catch (const std::exception& e) { | ||
| 904 | failed("a run-time load's DllMain gets a NULL lpvReserved", e.what()); | ||
| 905 | } | ||
| 906 | } | ||
| 907 | |||
| 866 | void test_process_detach_dependencies() { | 908 | void test_process_detach_dependencies() { |
| 867 | reset_notifications(); | 909 | reset_notifications(); |
| 868 | auto dep = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000); | 910 | auto dep = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000); |
| @@ -1303,6 +1345,7 @@ int main() { | |||
| 1303 | test_process_detach(); | 1345 | test_process_detach(); |
| 1304 | test_process_detach_invalid_tls(); | 1346 | test_process_detach_invalid_tls(); |
| 1305 | test_process_detach_without_entry(); | 1347 | test_process_detach_without_entry(); |
| 1348 | test_attach_reserved(); | ||
| 1306 | test_process_detach_dependencies(); | 1349 | test_process_detach_dependencies(); |
| 1307 | test_failed_dependency_not_registered(); | 1350 | test_failed_dependency_not_registered(); |
| 1308 | test_delay_descriptor_fields(); | 1351 | test_delay_descriptor_fields(); |