From d9ab1c3ce2cdebb865c2283eb3e764d3b2caaa98 Mon Sep 17 00:00:00 2001 From: Sean Hall <r.sean.hall@gmail.com> Date: Tue, 25 Oct 2022 15:12:01 -0500 Subject: Remove support for .NET Core 3.x from dnchost. Related to 6942 --- src/ext/Bal/dnchost/coreclrhost.h | 137 ------------------- src/ext/Bal/dnchost/dnchost.cpp | 1 - src/ext/Bal/dnchost/dnchost.vcxproj | 1 - src/ext/Bal/dnchost/dncutil.cpp | 257 +++--------------------------------- src/ext/Bal/dnchost/dncutil.h | 6 - src/ext/Bal/dnchost/precomp.h | 1 - 6 files changed, 19 insertions(+), 384 deletions(-) delete mode 100644 src/ext/Bal/dnchost/coreclrhost.h (limited to 'src/ext') diff --git a/src/ext/Bal/dnchost/coreclrhost.h b/src/ext/Bal/dnchost/coreclrhost.h deleted file mode 100644 index 07f28735..00000000 --- a/src/ext/Bal/dnchost/coreclrhost.h +++ /dev/null @@ -1,137 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - - - -// ***** ABOUT THIS HEADER ***** -// ************************************************************************************** -// -// This is the version on 2019-12-22 from -// https://github.com/dotnet/runtime/blob/master/src/coreclr/src/coreclr/hosts/inc/coreclrhost.h -// -// ************************************************************************************** -// **************************** - - -// -// APIs for hosting CoreCLR -// - -#ifndef __CORECLR_HOST_H__ -#define __CORECLR_HOST_H__ - -#if defined(_WIN32) && defined(_M_IX86) -#define CORECLR_CALLING_CONVENTION __stdcall -#else -#define CORECLR_CALLING_CONVENTION -#endif - -// For each hosting API, we define a function prototype and a function pointer -// The prototype is useful for implicit linking against the dynamic coreclr -// library and the pointer for explicit dynamic loading (dlopen, LoadLibrary) -#define CORECLR_HOSTING_API(function, ...) \ - extern "C" int CORECLR_CALLING_CONVENTION function(__VA_ARGS__); \ - typedef int (CORECLR_CALLING_CONVENTION *function##_ptr)(__VA_ARGS__) - -// -// Initialize the CoreCLR. Creates and starts CoreCLR host and creates an app domain -// -// Parameters: -// exePath - Absolute path of the executable that invoked the ExecuteAssembly (the native host application) -// appDomainFriendlyName - Friendly name of the app domain that will be created to execute the assembly -// propertyCount - Number of properties (elements of the following two arguments) -// propertyKeys - Keys of properties of the app domain -// propertyValues - Values of properties of the app domain -// hostHandle - Output parameter, handle of the created host -// domainId - Output parameter, id of the created app domain -// -// Returns: -// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed -// -CORECLR_HOSTING_API(coreclr_initialize, - const char* exePath, - const char* appDomainFriendlyName, - int propertyCount, - const char** propertyKeys, - const char** propertyValues, - void** hostHandle, - unsigned int* domainId); - -// -// Shutdown CoreCLR. It unloads the app domain and stops the CoreCLR host. -// -// Parameters: -// hostHandle - Handle of the host -// domainId - Id of the domain -// -// Returns: -// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed -// -CORECLR_HOSTING_API(coreclr_shutdown, - void* hostHandle, - unsigned int domainId); - -// -// Shutdown CoreCLR. It unloads the app domain and stops the CoreCLR host. -// -// Parameters: -// hostHandle - Handle of the host -// domainId - Id of the domain -// latchedExitCode - Latched exit code after domain unloaded -// -// Returns: -// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed -// -CORECLR_HOSTING_API(coreclr_shutdown_2, - void* hostHandle, - unsigned int domainId, - int* latchedExitCode); - -// -// Create a native callable function pointer for a managed method. -// -// Parameters: -// hostHandle - Handle of the host -// domainId - Id of the domain -// entryPointAssemblyName - Name of the assembly which holds the custom entry point -// entryPointTypeName - Name of the type which holds the custom entry point -// entryPointMethodName - Name of the method which is the custom entry point -// delegate - Output parameter, the function stores a native callable function pointer to the delegate at the specified address -// -// Returns: -// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed -// -CORECLR_HOSTING_API(coreclr_create_delegate, - void* hostHandle, - unsigned int domainId, - const char* entryPointAssemblyName, - const char* entryPointTypeName, - const char* entryPointMethodName, - void** delegate); - -// -// Execute a managed assembly with given arguments -// -// Parameters: -// hostHandle - Handle of the host -// domainId - Id of the domain -// argc - Number of arguments passed to the executed assembly -// argv - Array of arguments passed to the executed assembly -// managedAssemblyPath - Path of the managed assembly to execute (or NULL if using a custom entrypoint). -// exitCode - Exit code returned by the executed assembly -// -// Returns: -// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed -// -CORECLR_HOSTING_API(coreclr_execute_assembly, - void* hostHandle, - unsigned int domainId, - int argc, - const char** argv, - const char* managedAssemblyPath, - unsigned int* exitCode); - -#undef CORECLR_HOSTING_API - -#endif // __CORECLR_HOST_H__ \ No newline at end of file diff --git a/src/ext/Bal/dnchost/dnchost.cpp b/src/ext/Bal/dnchost/dnchost.cpp index 8faf292c..1868e3f8 100644 --- a/src/ext/Bal/dnchost/dnchost.cpp +++ b/src/ext/Bal/dnchost/dnchost.cpp @@ -286,7 +286,6 @@ static HRESULT LoadManagedBootstrapperApplicationFactory( hr = DnchostCreateFactory( &pState->hostfxrState, pState->sczBaFactoryAssemblyName, - pState->sczBaFactoryAssemblyPath, &pState->pAppFactory); return hr; diff --git a/src/ext/Bal/dnchost/dnchost.vcxproj b/src/ext/Bal/dnchost/dnchost.vcxproj index b258ccae..fc96580a 100644 --- a/src/ext/Bal/dnchost/dnchost.vcxproj +++ b/src/ext/Bal/dnchost/dnchost.vcxproj @@ -63,7 +63,6 @@ </ItemGroup> <ItemGroup> - <ClInclude Include="coreclrhost.h" /> <ClInclude Include="dnchost.h" /> <ClInclude Include="dncutil.h" /> <ClInclude Include="precomp.h" /> diff --git a/src/ext/Bal/dnchost/dncutil.cpp b/src/ext/Bal/dnchost/dncutil.cpp index a8d4f4ff..d00b0ce6 100644 --- a/src/ext/Bal/dnchost/dncutil.cpp +++ b/src/ext/Bal/dnchost/dncutil.cpp @@ -2,9 +2,7 @@ #include "precomp.h" -#define DNC_ENTRY_TYPE "WixToolset.Dnc.Host.BootstrapperApplicationFactory" #define DNC_ENTRY_TYPEW L"WixToolset.Dnc.Host.BootstrapperApplicationFactory" -#define DNC_STATIC_ENTRY_METHOD "CreateBAFactory" #define DNC_STATIC_ENTRY_METHODW L"CreateBAFactory" #define DNC_STATIC_ENTRY_DELEGATEW L"WixToolset.Dnc.Host.StaticEntryDelegate" @@ -29,23 +27,7 @@ static HRESULT InitializeHostfxr( __in LPCWSTR wzRuntimeConfigPath ); static HRESULT InitializeCoreClr( - __in HOSTFXR_STATE* pState, - __in LPCWSTR wzNativeHostPath - ); -static HRESULT InitializeCoreClrPre5( - __in HOSTFXR_STATE* pState, - __in LPCWSTR wzNativeHostPath - ); -static HRESULT LoadCoreClr( - __in HOSTFXR_STATE* pState, - __in LPCWSTR wzCoreClrPath - ); -static HRESULT StartCoreClr( - __in HOSTFXR_STATE* pState, - __in LPCWSTR wzNativeHostPath, - __in DWORD cProperties, - __in LPCWSTR* propertyKeys, - __in LPCWSTR* propertyValues + __in HOSTFXR_STATE* pState ); @@ -70,7 +52,7 @@ HRESULT DnchostLoadRuntime( hr = InitializeHostfxr(pState, wzManagedHostPath, wzDepsJsonPath, wzRuntimeConfigPath); BalExitOnFailure(hr, "Failed to initialize hostfxr."); - hr = InitializeCoreClr(pState, wzNativeHostPath); + hr = InitializeCoreClr(pState); BalExitOnFailure(hr, "Failed to initialize coreclr."); LExit: @@ -80,7 +62,6 @@ LExit: HRESULT DnchostCreateFactory( __in HOSTFXR_STATE* pState, __in LPCWSTR wzBaFactoryAssemblyName, - __in LPCWSTR /*wzBaFactoryAssemblyPath*/, __out IBootstrapperApplicationFactory** ppAppFactory ) { @@ -90,37 +71,20 @@ HRESULT DnchostCreateFactory( LPWSTR sczEntryDelegate = NULL; LPSTR sczBaFactoryAssemblyName = NULL; - if (pState->pfnGetFunctionPointer) - { - hr = StrAllocFormatted(&sczEntryType, L"%ls,%ls", DNC_ENTRY_TYPEW, wzBaFactoryAssemblyName); - BalExitOnFailure(hr, "Failed to format entry type."); - - hr = StrAllocFormatted(&sczEntryDelegate, L"%ls,%ls", DNC_STATIC_ENTRY_DELEGATEW, wzBaFactoryAssemblyName); - BalExitOnFailure(hr, "Failed to format entry delegate."); - - hr = pState->pfnGetFunctionPointer( - sczEntryType, - DNC_STATIC_ENTRY_METHODW, - sczEntryDelegate, - NULL, - NULL, - reinterpret_cast<void**>(&pfnCreateBAFactory)); - BalExitOnFailure(hr, "Failed to create delegate through GetFunctionPointer."); - } - else - { - hr = StrAnsiAllocString(&sczBaFactoryAssemblyName, wzBaFactoryAssemblyName, 0, CP_UTF8); - BalExitOnFailure(hr, "Failed to convert assembly name to UTF8: %ls", wzBaFactoryAssemblyName); - - hr = pState->pfnCoreclrCreateDelegate( - pState->pClrHandle, - pState->dwDomainId, - sczBaFactoryAssemblyName, - DNC_ENTRY_TYPE, - DNC_STATIC_ENTRY_METHOD, - reinterpret_cast<void**>(&pfnCreateBAFactory)); - BalExitOnFailure(hr, "Failed to create delegate in app domain."); - } + hr = StrAllocFormatted(&sczEntryType, L"%ls,%ls", DNC_ENTRY_TYPEW, wzBaFactoryAssemblyName); + BalExitOnFailure(hr, "Failed to format entry type."); + + hr = StrAllocFormatted(&sczEntryDelegate, L"%ls,%ls", DNC_STATIC_ENTRY_DELEGATEW, wzBaFactoryAssemblyName); + BalExitOnFailure(hr, "Failed to format entry delegate."); + + hr = pState->pfnGetFunctionPointer( + sczEntryType, + DNC_STATIC_ENTRY_METHODW, + sczEntryDelegate, + NULL, + NULL, + reinterpret_cast<void**>(&pfnCreateBAFactory)); + BalExitOnFailure(hr, "Failed to create delegate through GetFunctionPointer."); *ppAppFactory = pfnCreateBAFactory(); @@ -182,9 +146,6 @@ static HRESULT LoadHostfxr( pState->pfnHostfxrInitializeForApp = reinterpret_cast<hostfxr_initialize_for_dotnet_command_line_fn>(::GetProcAddress(hHostfxr, "hostfxr_initialize_for_dotnet_command_line")); BalExitOnNullWithLastError(pState->pfnHostfxrInitializeForApp, hr, "Failed to get procedure address for hostfxr_initialize_for_dotnet_command_line."); - pState->pfnHostfxrGetRuntimeProperties = reinterpret_cast<hostfxr_get_runtime_properties_fn>(::GetProcAddress(hHostfxr, "hostfxr_get_runtime_properties")); - BalExitOnNullWithLastError(pState->pfnHostfxrGetRuntimeProperties, hr, "Failed to get procedure address for hostfxr_get_runtime_properties."); - pState->pfnHostfxrSetErrorWriter = reinterpret_cast<hostfxr_set_error_writer_fn>(::GetProcAddress(hHostfxr, "hostfxr_set_error_writer")); BalExitOnNullWithLastError(pState->pfnHostfxrSetErrorWriter, hr, "Failed to get procedure address for hostfxr_set_error_writer."); @@ -204,14 +165,7 @@ static void HOSTFXR_CALLTYPE DnchostErrorWriter( __in LPCWSTR wzMessage ) { - BOOTSTRAPPER_LOG_LEVEL level = BOOTSTRAPPER_LOG_LEVEL_ERROR; - - if (CSTR_EQUAL == ::CompareString(LOCALE_INVARIANT, 0, wzMessage, -1, L"The requested delegate type is not available in the target framework.", -1)) - { - level = BOOTSTRAPPER_LOG_LEVEL_DEBUG; - } - - BalLog(level, "error from hostfxr: %ls", wzMessage); + BalLog(BOOTSTRAPPER_LOG_LEVEL_ERROR, "error from hostfxr: %ls", wzMessage); } static HRESULT InitializeHostfxr( @@ -241,8 +195,7 @@ LExit: } static HRESULT InitializeCoreClr( - __in HOSTFXR_STATE* pState, - __in LPCWSTR wzNativeHostPath + __in HOSTFXR_STATE* pState ) { HRESULT hr = S_OK; @@ -251,7 +204,7 @@ static HRESULT InitializeCoreClr( if (InvalidArgFailure == hr || // old versions of hostfxr don't allow calling GetRuntimeDelegate from InitializeForApp. HostApiUnsupportedVersion == hr) // hdt_get_function_pointer was added in .NET 5. { - hr = InitializeCoreClrPre5(pState, wzNativeHostPath); + BalExitOnFailure(hr, "HostfxrGetRuntimeDelegate failed, most likely because the target framework is older than .NET 5."); } else { @@ -261,175 +214,3 @@ static HRESULT InitializeCoreClr( LExit: return hr; } - -static HRESULT InitializeCoreClrPre5( - __in HOSTFXR_STATE* pState, - __in LPCWSTR wzNativeHostPath - ) -{ - HRESULT hr = S_OK; - int32_t rc = 0; - LPCWSTR* rgPropertyKeys = NULL; - LPCWSTR* rgPropertyValues = NULL; - size_t cProperties = 0; - LPWSTR* rgDirectories = NULL; - UINT cDirectories = 0; - LPWSTR sczCoreClrPath = NULL; - - // We are not using hostfxr as it was intended to be used. We need to initialize hostfxr so that it properly initializes hostpolicy - - // there are pieces of the framework such as AssemblyDependencyResolver that won't work without that. We also need hostfxr to find a - // compatible framework for framework-dependent deployed BAs. We had to use hostfxr_initialize_for_dotnet_command_line since - // hostfxr_initialize_for_runtime_config doesn't currently (3.x) support self-contained deployed BAs. That means we're supposed to - // start the runtime through hostfxr_run_app, but that method shuts down the runtime before returning. We actually want to call - // hostfxr_get_runtime_delegate, but that method currently requires hostfxr to be initialized through - // hostfxr_initialize_for_runtime_config. So we're forced to locate coreclr.dll and manually load the runtime ourselves. - - // Unfortunately, that's not the only problem. hostfxr has global state that tracks whether it started the runtime. While we keep our - // hostfxr_handle open, everyone that calls the hostfxr_initialize_* methods will block until we have started the runtime through - // hostfxr or closed our handle. If we close the handle, then hostfxr could potentially try to load a second runtime into the - // process, which is not supported. We're going to just keep our handle open since no one else in the process should be trying to - // start the runtime anyway. - - rc = pState->pfnHostfxrGetRuntimeProperties(pState->hostContextHandle, &cProperties, rgPropertyKeys, rgPropertyValues); - if (HostApiBufferTooSmall != rc) - { - BalExitOnFailure(hr = rc, "HostfxrGetRuntimeProperties failed to return required size."); - } - - rgPropertyKeys = static_cast<LPCWSTR*>(MemAlloc(sizeof(LPWSTR) * cProperties, TRUE)); - rgPropertyValues = static_cast<LPCWSTR*>(MemAlloc(sizeof(LPWSTR) * cProperties, TRUE)); - if (!rgPropertyKeys || !rgPropertyValues) - { - BalExitOnFailure(hr = E_OUTOFMEMORY, "Failed to allocate buffers for runtime properties."); - } - - hr = pState->pfnHostfxrGetRuntimeProperties(pState->hostContextHandle, &cProperties, rgPropertyKeys, rgPropertyValues); - BalExitOnFailure(hr, "HostfxrGetRuntimeProperties failed."); - - for (DWORD i = 0; i < cProperties; ++i) - { - if (CSTR_EQUAL == ::CompareString(LOCALE_INVARIANT, 0, rgPropertyKeys[i], -1, L"NATIVE_DLL_SEARCH_DIRECTORIES", -1)) - { - hr = StrSplitAllocArray(&rgDirectories, &cDirectories, rgPropertyValues[i], L";"); - BalExitOnFailure(hr, "Failed to split NATIVE_DLL_SEARCH_DIRECTORIES '%ls'", rgPropertyValues[i]); - } - } - - for (DWORD i = 0; i < cDirectories; ++i) - { - Assert(rgDirectories); - - hr = PathConcat(rgDirectories[i], L"coreclr.dll", &sczCoreClrPath); - BalExitOnFailure(hr, "Failed to allocate path to coreclr."); - - if (::PathFileExists(sczCoreClrPath)) - { - break; - } - else - { - ReleaseNullStr(sczCoreClrPath); - } - } - - if (!sczCoreClrPath) - { - for (DWORD i = 0; i < cProperties; ++i) - { - BalLog(BOOTSTRAPPER_LOG_LEVEL_ERROR, "%ls: %ls", rgPropertyKeys[i], rgPropertyValues[i]); - } - BalExitWithRootFailure(hr, E_FILENOTFOUND, "Failed to locate coreclr.dll."); - } - - hr = LoadCoreClr(pState, sczCoreClrPath); - BalExitOnFailure(hr, "Failed to load coreclr."); - - hr = StartCoreClr(pState, wzNativeHostPath, (DWORD)cProperties, rgPropertyKeys, rgPropertyValues); - BalExitOnFailure(hr, "Failed to start coreclr."); - -LExit: - ReleaseMem(rgDirectories); - ReleaseMem(rgPropertyValues); - ReleaseMem(rgPropertyKeys); - ReleaseStr(sczCoreClrPath); - - return hr; -} - -static HRESULT LoadCoreClr( - __in HOSTFXR_STATE* pState, - __in LPCWSTR wzCoreClrPath - ) -{ - HRESULT hr = S_OK; - HMODULE hModule = NULL; - - hModule = ::LoadLibraryExW(wzCoreClrPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); - BalExitOnNullWithLastError(hModule, hr, "Failed to load coreclr.dll from '%ls'.", wzCoreClrPath); - - pState->pfnCoreclrInitialize = reinterpret_cast<coreclr_initialize_ptr>(::GetProcAddress(hModule, "coreclr_initialize")); - BalExitOnNullWithLastError(pState->pfnCoreclrInitialize, hr, "Failed to get procedure address for coreclr_initialize."); - - pState->pfnCoreclrCreateDelegate = reinterpret_cast<coreclr_create_delegate_ptr>(::GetProcAddress(hModule, "coreclr_create_delegate")); - BalExitOnNullWithLastError(pState->pfnCoreclrCreateDelegate, hr, "Failed to get procedure address for coreclr_create_delegate."); - -LExit: - // Never unload the module since coreclr doesn't support it. - - return hr; -} - -static HRESULT StartCoreClr( - __in HOSTFXR_STATE* pState, - __in LPCWSTR wzNativeHostPath, - __in DWORD cProperties, - __in LPCWSTR* propertyKeys, - __in LPCWSTR* propertyValues - ) -{ - HRESULT hr = S_OK; - LPSTR szNativeHostPath = NULL; - LPSTR* rgPropertyKeys = NULL; - LPSTR* rgPropertyValues = NULL; - - rgPropertyKeys = static_cast<LPSTR*>(MemAlloc(sizeof(LPSTR) * cProperties, TRUE)); - rgPropertyValues = static_cast<LPSTR*>(MemAlloc(sizeof(LPSTR) * cProperties, TRUE)); - if (!rgPropertyKeys || !rgPropertyValues) - { - BalExitOnFailure(hr = E_OUTOFMEMORY, "Failed to allocate buffers for runtime properties."); - } - - hr = StrAnsiAllocString(&szNativeHostPath, wzNativeHostPath, 0, CP_UTF8); - BalExitOnFailure(hr, "Failed to convert module path to UTF8: %ls", wzNativeHostPath); - - for (DWORD i = 0; i < cProperties; ++i) - { - hr = StrAnsiAllocString(&rgPropertyKeys[i], propertyKeys[i], 0, CP_UTF8); - BalExitOnFailure(hr, "Failed to convert property key to UTF8: %ls", propertyKeys[i]); - - hr = StrAnsiAllocString(&rgPropertyValues[i], propertyValues[i], 0, CP_UTF8); - BalExitOnFailure(hr, "Failed to convert property value to UTF8: %ls", propertyValues[i]); - } - - hr = pState->pfnCoreclrInitialize(szNativeHostPath, "MBA", cProperties, (LPCSTR*)rgPropertyKeys, (LPCSTR*)rgPropertyValues, &pState->pClrHandle, &pState->dwDomainId); - BalExitOnFailure(hr, "CoreclrInitialize failed."); - -LExit: - for (DWORD i = 0; i < cProperties; ++i) - { - if (rgPropertyKeys) - { - ReleaseStr(rgPropertyKeys[i]); - } - - if (rgPropertyValues) - { - ReleaseStr(rgPropertyValues[i]); - } - } - ReleaseMem(rgPropertyValues); - ReleaseMem(rgPropertyKeys); - ReleaseStr(szNativeHostPath); - - return hr; -} diff --git a/src/ext/Bal/dnchost/dncutil.h b/src/ext/Bal/dnchost/dncutil.h index f88e3f77..3fa81364 100644 --- a/src/ext/Bal/dnchost/dncutil.h +++ b/src/ext/Bal/dnchost/dncutil.h @@ -8,15 +8,10 @@ struct HOSTFXR_STATE LPWSTR sczHostfxrPath; hostfxr_handle hostContextHandle; hostfxr_initialize_for_dotnet_command_line_fn pfnHostfxrInitializeForApp; - hostfxr_get_runtime_properties_fn pfnHostfxrGetRuntimeProperties; hostfxr_set_error_writer_fn pfnHostfxrSetErrorWriter; hostfxr_close_fn pfnHostfxrClose; hostfxr_get_runtime_delegate_fn pfnHostfxrGetRuntimeDelegate; get_function_pointer_fn pfnGetFunctionPointer; - coreclr_initialize_ptr pfnCoreclrInitialize; - coreclr_create_delegate_ptr pfnCoreclrCreateDelegate; - void* pClrHandle; - UINT dwDomainId; }; HRESULT DnchostLoadRuntime( @@ -30,6 +25,5 @@ HRESULT DnchostLoadRuntime( HRESULT DnchostCreateFactory( __in HOSTFXR_STATE* pState, __in LPCWSTR wzBaFactoryAssemblyName, - __in LPCWSTR wzBaFactoryAssemblyPath, __out IBootstrapperApplicationFactory** ppAppFactory ); diff --git a/src/ext/Bal/dnchost/precomp.h b/src/ext/Bal/dnchost/precomp.h index 5f365fba..2166a23d 100644 --- a/src/ext/Bal/dnchost/precomp.h +++ b/src/ext/Bal/dnchost/precomp.h @@ -27,6 +27,5 @@ #include <preqba.h> -#include "coreclrhost.h" #include "dncutil.h" #include "dnchost.h" -- cgit v1.2.3-55-g6feb