From 8d68062cc4dc34d63bd4e614d818f7c415a10251 Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Mon, 5 Aug 2019 08:51:55 -0700 Subject: [PATCH] Always load hostfxr.dll by absolute path (#12807) --- .../AspNetCoreModuleV2/CommonLib/HostFxr.cpp | 19 ++++++++++++------- .../AspNetCoreModuleV2/CommonLib/HostFxr.h | 1 - .../inprocessapplication.cpp | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxr.cpp b/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxr.cpp index b05a4d1a50..545791610b 100644 --- a/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxr.cpp +++ b/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxr.cpp @@ -30,16 +30,17 @@ void HostFxrErrorRedirector::HostFxrErrorRedirectorCallback(const WCHAR* message m_writeFunction->Append(std::wstring(message) + L"\r\n"); } -void HostFxr::Load() -{ - HMODULE hModule; - THROW_LAST_ERROR_IF(!GetModuleHandleEx(0, L"hostfxr.dll", &hModule)); - Load(hModule); -} - void HostFxr::Load(HMODULE moduleHandle) { + // A hostfxr may already be loaded here if we tried to start with an + // invalid configuration. Release hostfxr before loading it again. + if (m_hHostFxrDll != nullptr) + { + m_hHostFxrDll.release(); + } + m_hHostFxrDll = moduleHandle; + try { m_hostfxr_get_native_search_directories_fn = ModuleHelpers::GetKnownProcAddress(moduleHandle, "hostfxr_get_native_search_directories"); @@ -63,9 +64,13 @@ void HostFxr::Load(HMODULE moduleHandle) void HostFxr::Load(const std::wstring& location) { + // Make sure to always load hostfxr via an absolute path. + // If the process fails to start for whatever reason, a mismatched hostfxr + // may be already loaded in the process. try { HMODULE hModule; + LOG_INFOF(L"Loading hostfxr from location %s", location.c_str()); THROW_LAST_ERROR_IF_NULL(hModule = LoadLibraryW(location.c_str())); Load(hModule); } diff --git a/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxr.h b/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxr.h index 526222322e..ced4dd1940 100644 --- a/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxr.h +++ b/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxr.h @@ -60,7 +60,6 @@ public: { } - void Load(); void Load(HMODULE moduleHandle); void Load(const std::wstring& location); diff --git a/src/Servers/IIS/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.cpp b/src/Servers/IIS/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.cpp index 5707e4cd77..b671e7dba2 100644 --- a/src/Servers/IIS/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.cpp +++ b/src/Servers/IIS/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.cpp @@ -210,7 +210,7 @@ IN_PROCESS_APPLICATION::ExecuteApplication() hostFxrResolutionResult->GetArguments(context->m_argc, context->m_argv); THROW_IF_FAILED(SetEnvironmentVariablesOnWorkerProcess()); - context->m_hostFxr.Load(); + context->m_hostFxr.Load(hostFxrResolutionResult->GetHostFxrLocation()); } else {