aboutsummaryrefslogtreecommitdiff
path: root/src/ext/Bal/dnchost/dncutil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ext/Bal/dnchost/dncutil.cpp')
-rw-r--r--src/ext/Bal/dnchost/dncutil.cpp216
1 files changed, 0 insertions, 216 deletions
diff --git a/src/ext/Bal/dnchost/dncutil.cpp b/src/ext/Bal/dnchost/dncutil.cpp
deleted file mode 100644
index d00b0ce6..00000000
--- a/src/ext/Bal/dnchost/dncutil.cpp
+++ /dev/null
@@ -1,216 +0,0 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3#include "precomp.h"
4
5#define DNC_ENTRY_TYPEW L"WixToolset.Dnc.Host.BootstrapperApplicationFactory"
6#define DNC_STATIC_ENTRY_METHODW L"CreateBAFactory"
7#define DNC_STATIC_ENTRY_DELEGATEW L"WixToolset.Dnc.Host.StaticEntryDelegate"
8
9// https://github.com/dotnet/runtime/blob/master/src/installer/corehost/error_codes.h
10#define InvalidArgFailure 0x80008081
11#define HostApiBufferTooSmall 0x80008098
12#define HostApiUnsupportedVersion 0x800080a2
13
14// internal function declarations
15
16static HRESULT GetHostfxrPath(
17 __in HOSTFXR_STATE* pState,
18 __in LPCWSTR wzNativeHostPath
19 );
20static HRESULT LoadHostfxr(
21 __in HOSTFXR_STATE* pState
22 );
23static HRESULT InitializeHostfxr(
24 __in HOSTFXR_STATE* pState,
25 __in LPCWSTR wzManagedHostPath,
26 __in LPCWSTR wzDepsJsonPath,
27 __in LPCWSTR wzRuntimeConfigPath
28 );
29static HRESULT InitializeCoreClr(
30 __in HOSTFXR_STATE* pState
31 );
32
33
34// function definitions
35
36HRESULT DnchostLoadRuntime(
37 __in HOSTFXR_STATE* pState,
38 __in LPCWSTR wzNativeHostPath,
39 __in LPCWSTR wzManagedHostPath,
40 __in LPCWSTR wzDepsJsonPath,
41 __in LPCWSTR wzRuntimeConfigPath
42 )
43{
44 HRESULT hr = S_OK;
45
46 hr = GetHostfxrPath(pState, wzNativeHostPath);
47 BalExitOnFailure(hr, "Failed to find hostfxr.");
48
49 hr = LoadHostfxr(pState);
50 BalExitOnFailure(hr, "Failed to load hostfxr.");
51
52 hr = InitializeHostfxr(pState, wzManagedHostPath, wzDepsJsonPath, wzRuntimeConfigPath);
53 BalExitOnFailure(hr, "Failed to initialize hostfxr.");
54
55 hr = InitializeCoreClr(pState);
56 BalExitOnFailure(hr, "Failed to initialize coreclr.");
57
58LExit:
59 return hr;
60}
61
62HRESULT DnchostCreateFactory(
63 __in HOSTFXR_STATE* pState,
64 __in LPCWSTR wzBaFactoryAssemblyName,
65 __out IBootstrapperApplicationFactory** ppAppFactory
66 )
67{
68 HRESULT hr = S_OK;
69 PFNCREATEBAFACTORY pfnCreateBAFactory = NULL;
70 LPWSTR sczEntryType = NULL;
71 LPWSTR sczEntryDelegate = NULL;
72 LPSTR sczBaFactoryAssemblyName = NULL;
73
74 hr = StrAllocFormatted(&sczEntryType, L"%ls,%ls", DNC_ENTRY_TYPEW, wzBaFactoryAssemblyName);
75 BalExitOnFailure(hr, "Failed to format entry type.");
76
77 hr = StrAllocFormatted(&sczEntryDelegate, L"%ls,%ls", DNC_STATIC_ENTRY_DELEGATEW, wzBaFactoryAssemblyName);
78 BalExitOnFailure(hr, "Failed to format entry delegate.");
79
80 hr = pState->pfnGetFunctionPointer(
81 sczEntryType,
82 DNC_STATIC_ENTRY_METHODW,
83 sczEntryDelegate,
84 NULL,
85 NULL,
86 reinterpret_cast<void**>(&pfnCreateBAFactory));
87 BalExitOnFailure(hr, "Failed to create delegate through GetFunctionPointer.");
88
89 *ppAppFactory = pfnCreateBAFactory();
90
91LExit:
92 ReleaseStr(sczEntryType);
93 ReleaseStr(sczEntryDelegate);
94 ReleaseStr(sczBaFactoryAssemblyName);
95
96 return hr;
97}
98
99static HRESULT GetHostfxrPath(
100 __in HOSTFXR_STATE* pState,
101 __in LPCWSTR wzNativeHostPath
102 )
103{
104 HRESULT hr = S_OK;
105 get_hostfxr_parameters getHostfxrParameters = { };
106 int nrc = 0;
107 size_t cchHostFxrPath = MAX_PATH;
108
109 getHostfxrParameters.size = sizeof(get_hostfxr_parameters);
110 getHostfxrParameters.assembly_path = wzNativeHostPath;
111
112 // get_hostfxr_path does a full search on every call, so
113 // minimize the number of calls
114 // need to loop
115 for (;;)
116 {
117 cchHostFxrPath *= 2;
118 hr = StrAlloc(&pState->sczHostfxrPath, cchHostFxrPath);
119 BalExitOnFailure(hr, "Failed to allocate hostFxrPath.");
120
121 nrc = get_hostfxr_path(pState->sczHostfxrPath, &cchHostFxrPath, &getHostfxrParameters);
122 if (HostApiBufferTooSmall != nrc)
123 {
124 break;
125 }
126 }
127 if (0 != nrc)
128 {
129 BalExitOnFailure(hr = nrc, "GetHostfxrPath failed");
130 }
131
132LExit:
133 return hr;
134}
135
136static HRESULT LoadHostfxr(
137 __in HOSTFXR_STATE* pState
138 )
139{
140 HRESULT hr = S_OK;
141 HMODULE hHostfxr;
142
143 hHostfxr = ::LoadLibraryExW(pState->sczHostfxrPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
144 BalExitOnNullWithLastError(hHostfxr, hr, "Failed to load hostfxr from '%ls'.", pState->sczHostfxrPath);
145
146 pState->pfnHostfxrInitializeForApp = reinterpret_cast<hostfxr_initialize_for_dotnet_command_line_fn>(::GetProcAddress(hHostfxr, "hostfxr_initialize_for_dotnet_command_line"));
147 BalExitOnNullWithLastError(pState->pfnHostfxrInitializeForApp, hr, "Failed to get procedure address for hostfxr_initialize_for_dotnet_command_line.");
148
149 pState->pfnHostfxrSetErrorWriter = reinterpret_cast<hostfxr_set_error_writer_fn>(::GetProcAddress(hHostfxr, "hostfxr_set_error_writer"));
150 BalExitOnNullWithLastError(pState->pfnHostfxrSetErrorWriter, hr, "Failed to get procedure address for hostfxr_set_error_writer.");
151
152 pState->pfnHostfxrClose = reinterpret_cast<hostfxr_close_fn>(::GetProcAddress(hHostfxr, "hostfxr_close"));
153 BalExitOnNullWithLastError(pState->pfnHostfxrClose, hr, "Failed to get procedure address for hostfxr_close.");
154
155 pState->pfnHostfxrGetRuntimeDelegate = reinterpret_cast<hostfxr_get_runtime_delegate_fn>(::GetProcAddress(hHostfxr, "hostfxr_get_runtime_delegate"));
156 BalExitOnNullWithLastError(pState->pfnHostfxrGetRuntimeDelegate, hr, "Failed to get procedure address for hostfxr_get_runtime_delegate.");
157
158LExit:
159 // Never unload the module since it isn't meant to be unloaded.
160
161 return hr;
162}
163
164static void HOSTFXR_CALLTYPE DnchostErrorWriter(
165 __in LPCWSTR wzMessage
166 )
167{
168 BalLog(BOOTSTRAPPER_LOG_LEVEL_ERROR, "error from hostfxr: %ls", wzMessage);
169}
170
171static HRESULT InitializeHostfxr(
172 __in HOSTFXR_STATE* pState,
173 __in LPCWSTR wzManagedHostPath,
174 __in LPCWSTR wzDepsJsonPath,
175 __in LPCWSTR wzRuntimeConfigPath
176 )
177{
178 HRESULT hr = S_OK;
179
180 pState->pfnHostfxrSetErrorWriter(static_cast<hostfxr_error_writer_fn>(&DnchostErrorWriter));
181
182 LPCWSTR argv[] = {
183 L"exec",
184 L"--depsfile",
185 wzDepsJsonPath,
186 L"--runtimeconfig",
187 wzRuntimeConfigPath,
188 wzManagedHostPath,
189 };
190 hr = pState->pfnHostfxrInitializeForApp(sizeof(argv)/sizeof(LPWSTR), argv, NULL, &pState->hostContextHandle);
191 BalExitOnFailure(hr, "HostfxrInitializeForApp failed");
192
193LExit:
194 return hr;
195}
196
197static HRESULT InitializeCoreClr(
198 __in HOSTFXR_STATE* pState
199 )
200{
201 HRESULT hr = S_OK;
202
203 hr = pState->pfnHostfxrGetRuntimeDelegate(pState->hostContextHandle, hdt_get_function_pointer, reinterpret_cast<void**>(&pState->pfnGetFunctionPointer));
204 if (InvalidArgFailure == hr || // old versions of hostfxr don't allow calling GetRuntimeDelegate from InitializeForApp.
205 HostApiUnsupportedVersion == hr) // hdt_get_function_pointer was added in .NET 5.
206 {
207 BalExitOnFailure(hr, "HostfxrGetRuntimeDelegate failed, most likely because the target framework is older than .NET 5.");
208 }
209 else
210 {
211 BalExitOnFailure(hr, "HostfxrGetRuntimeDelegate failed");
212 }
213
214LExit:
215 return hr;
216}