diff options
Diffstat (limited to 'src/ext/Bal/dnchost/dncutil.cpp')
-rw-r--r-- | src/ext/Bal/dnchost/dncutil.cpp | 257 |
1 files changed, 19 insertions, 238 deletions
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 @@ | |||
2 | 2 | ||
3 | #include "precomp.h" | 3 | #include "precomp.h" |
4 | 4 | ||
5 | #define DNC_ENTRY_TYPE "WixToolset.Dnc.Host.BootstrapperApplicationFactory" | ||
6 | #define DNC_ENTRY_TYPEW L"WixToolset.Dnc.Host.BootstrapperApplicationFactory" | 5 | #define DNC_ENTRY_TYPEW L"WixToolset.Dnc.Host.BootstrapperApplicationFactory" |
7 | #define DNC_STATIC_ENTRY_METHOD "CreateBAFactory" | ||
8 | #define DNC_STATIC_ENTRY_METHODW L"CreateBAFactory" | 6 | #define DNC_STATIC_ENTRY_METHODW L"CreateBAFactory" |
9 | #define DNC_STATIC_ENTRY_DELEGATEW L"WixToolset.Dnc.Host.StaticEntryDelegate" | 7 | #define DNC_STATIC_ENTRY_DELEGATEW L"WixToolset.Dnc.Host.StaticEntryDelegate" |
10 | 8 | ||
@@ -29,23 +27,7 @@ static HRESULT InitializeHostfxr( | |||
29 | __in LPCWSTR wzRuntimeConfigPath | 27 | __in LPCWSTR wzRuntimeConfigPath |
30 | ); | 28 | ); |
31 | static HRESULT InitializeCoreClr( | 29 | static HRESULT InitializeCoreClr( |
32 | __in HOSTFXR_STATE* pState, | 30 | __in HOSTFXR_STATE* pState |
33 | __in LPCWSTR wzNativeHostPath | ||
34 | ); | ||
35 | static HRESULT InitializeCoreClrPre5( | ||
36 | __in HOSTFXR_STATE* pState, | ||
37 | __in LPCWSTR wzNativeHostPath | ||
38 | ); | ||
39 | static HRESULT LoadCoreClr( | ||
40 | __in HOSTFXR_STATE* pState, | ||
41 | __in LPCWSTR wzCoreClrPath | ||
42 | ); | ||
43 | static HRESULT StartCoreClr( | ||
44 | __in HOSTFXR_STATE* pState, | ||
45 | __in LPCWSTR wzNativeHostPath, | ||
46 | __in DWORD cProperties, | ||
47 | __in LPCWSTR* propertyKeys, | ||
48 | __in LPCWSTR* propertyValues | ||
49 | ); | 31 | ); |
50 | 32 | ||
51 | 33 | ||
@@ -70,7 +52,7 @@ HRESULT DnchostLoadRuntime( | |||
70 | hr = InitializeHostfxr(pState, wzManagedHostPath, wzDepsJsonPath, wzRuntimeConfigPath); | 52 | hr = InitializeHostfxr(pState, wzManagedHostPath, wzDepsJsonPath, wzRuntimeConfigPath); |
71 | BalExitOnFailure(hr, "Failed to initialize hostfxr."); | 53 | BalExitOnFailure(hr, "Failed to initialize hostfxr."); |
72 | 54 | ||
73 | hr = InitializeCoreClr(pState, wzNativeHostPath); | 55 | hr = InitializeCoreClr(pState); |
74 | BalExitOnFailure(hr, "Failed to initialize coreclr."); | 56 | BalExitOnFailure(hr, "Failed to initialize coreclr."); |
75 | 57 | ||
76 | LExit: | 58 | LExit: |
@@ -80,7 +62,6 @@ LExit: | |||
80 | HRESULT DnchostCreateFactory( | 62 | HRESULT DnchostCreateFactory( |
81 | __in HOSTFXR_STATE* pState, | 63 | __in HOSTFXR_STATE* pState, |
82 | __in LPCWSTR wzBaFactoryAssemblyName, | 64 | __in LPCWSTR wzBaFactoryAssemblyName, |
83 | __in LPCWSTR /*wzBaFactoryAssemblyPath*/, | ||
84 | __out IBootstrapperApplicationFactory** ppAppFactory | 65 | __out IBootstrapperApplicationFactory** ppAppFactory |
85 | ) | 66 | ) |
86 | { | 67 | { |
@@ -90,37 +71,20 @@ HRESULT DnchostCreateFactory( | |||
90 | LPWSTR sczEntryDelegate = NULL; | 71 | LPWSTR sczEntryDelegate = NULL; |
91 | LPSTR sczBaFactoryAssemblyName = NULL; | 72 | LPSTR sczBaFactoryAssemblyName = NULL; |
92 | 73 | ||
93 | if (pState->pfnGetFunctionPointer) | 74 | hr = StrAllocFormatted(&sczEntryType, L"%ls,%ls", DNC_ENTRY_TYPEW, wzBaFactoryAssemblyName); |
94 | { | 75 | BalExitOnFailure(hr, "Failed to format entry type."); |
95 | hr = StrAllocFormatted(&sczEntryType, L"%ls,%ls", DNC_ENTRY_TYPEW, wzBaFactoryAssemblyName); | 76 | |
96 | BalExitOnFailure(hr, "Failed to format entry type."); | 77 | hr = StrAllocFormatted(&sczEntryDelegate, L"%ls,%ls", DNC_STATIC_ENTRY_DELEGATEW, wzBaFactoryAssemblyName); |
97 | 78 | BalExitOnFailure(hr, "Failed to format entry delegate."); | |
98 | hr = StrAllocFormatted(&sczEntryDelegate, L"%ls,%ls", DNC_STATIC_ENTRY_DELEGATEW, wzBaFactoryAssemblyName); | 79 | |
99 | BalExitOnFailure(hr, "Failed to format entry delegate."); | 80 | hr = pState->pfnGetFunctionPointer( |
100 | 81 | sczEntryType, | |
101 | hr = pState->pfnGetFunctionPointer( | 82 | DNC_STATIC_ENTRY_METHODW, |
102 | sczEntryType, | 83 | sczEntryDelegate, |
103 | DNC_STATIC_ENTRY_METHODW, | 84 | NULL, |
104 | sczEntryDelegate, | 85 | NULL, |
105 | NULL, | 86 | reinterpret_cast<void**>(&pfnCreateBAFactory)); |
106 | NULL, | 87 | BalExitOnFailure(hr, "Failed to create delegate through GetFunctionPointer."); |
107 | reinterpret_cast<void**>(&pfnCreateBAFactory)); | ||
108 | BalExitOnFailure(hr, "Failed to create delegate through GetFunctionPointer."); | ||
109 | } | ||
110 | else | ||
111 | { | ||
112 | hr = StrAnsiAllocString(&sczBaFactoryAssemblyName, wzBaFactoryAssemblyName, 0, CP_UTF8); | ||
113 | BalExitOnFailure(hr, "Failed to convert assembly name to UTF8: %ls", wzBaFactoryAssemblyName); | ||
114 | |||
115 | hr = pState->pfnCoreclrCreateDelegate( | ||
116 | pState->pClrHandle, | ||
117 | pState->dwDomainId, | ||
118 | sczBaFactoryAssemblyName, | ||
119 | DNC_ENTRY_TYPE, | ||
120 | DNC_STATIC_ENTRY_METHOD, | ||
121 | reinterpret_cast<void**>(&pfnCreateBAFactory)); | ||
122 | BalExitOnFailure(hr, "Failed to create delegate in app domain."); | ||
123 | } | ||
124 | 88 | ||
125 | *ppAppFactory = pfnCreateBAFactory(); | 89 | *ppAppFactory = pfnCreateBAFactory(); |
126 | 90 | ||
@@ -182,9 +146,6 @@ static HRESULT LoadHostfxr( | |||
182 | pState->pfnHostfxrInitializeForApp = reinterpret_cast<hostfxr_initialize_for_dotnet_command_line_fn>(::GetProcAddress(hHostfxr, "hostfxr_initialize_for_dotnet_command_line")); | 146 | pState->pfnHostfxrInitializeForApp = reinterpret_cast<hostfxr_initialize_for_dotnet_command_line_fn>(::GetProcAddress(hHostfxr, "hostfxr_initialize_for_dotnet_command_line")); |
183 | BalExitOnNullWithLastError(pState->pfnHostfxrInitializeForApp, hr, "Failed to get procedure address for hostfxr_initialize_for_dotnet_command_line."); | 147 | BalExitOnNullWithLastError(pState->pfnHostfxrInitializeForApp, hr, "Failed to get procedure address for hostfxr_initialize_for_dotnet_command_line."); |
184 | 148 | ||
185 | pState->pfnHostfxrGetRuntimeProperties = reinterpret_cast<hostfxr_get_runtime_properties_fn>(::GetProcAddress(hHostfxr, "hostfxr_get_runtime_properties")); | ||
186 | BalExitOnNullWithLastError(pState->pfnHostfxrGetRuntimeProperties, hr, "Failed to get procedure address for hostfxr_get_runtime_properties."); | ||
187 | |||
188 | pState->pfnHostfxrSetErrorWriter = reinterpret_cast<hostfxr_set_error_writer_fn>(::GetProcAddress(hHostfxr, "hostfxr_set_error_writer")); | 149 | pState->pfnHostfxrSetErrorWriter = reinterpret_cast<hostfxr_set_error_writer_fn>(::GetProcAddress(hHostfxr, "hostfxr_set_error_writer")); |
189 | BalExitOnNullWithLastError(pState->pfnHostfxrSetErrorWriter, hr, "Failed to get procedure address for hostfxr_set_error_writer."); | 150 | BalExitOnNullWithLastError(pState->pfnHostfxrSetErrorWriter, hr, "Failed to get procedure address for hostfxr_set_error_writer."); |
190 | 151 | ||
@@ -204,14 +165,7 @@ static void HOSTFXR_CALLTYPE DnchostErrorWriter( | |||
204 | __in LPCWSTR wzMessage | 165 | __in LPCWSTR wzMessage |
205 | ) | 166 | ) |
206 | { | 167 | { |
207 | BOOTSTRAPPER_LOG_LEVEL level = BOOTSTRAPPER_LOG_LEVEL_ERROR; | 168 | BalLog(BOOTSTRAPPER_LOG_LEVEL_ERROR, "error from hostfxr: %ls", wzMessage); |
208 | |||
209 | if (CSTR_EQUAL == ::CompareString(LOCALE_INVARIANT, 0, wzMessage, -1, L"The requested delegate type is not available in the target framework.", -1)) | ||
210 | { | ||
211 | level = BOOTSTRAPPER_LOG_LEVEL_DEBUG; | ||
212 | } | ||
213 | |||
214 | BalLog(level, "error from hostfxr: %ls", wzMessage); | ||
215 | } | 169 | } |
216 | 170 | ||
217 | static HRESULT InitializeHostfxr( | 171 | static HRESULT InitializeHostfxr( |
@@ -241,8 +195,7 @@ LExit: | |||
241 | } | 195 | } |
242 | 196 | ||
243 | static HRESULT InitializeCoreClr( | 197 | static HRESULT InitializeCoreClr( |
244 | __in HOSTFXR_STATE* pState, | 198 | __in HOSTFXR_STATE* pState |
245 | __in LPCWSTR wzNativeHostPath | ||
246 | ) | 199 | ) |
247 | { | 200 | { |
248 | HRESULT hr = S_OK; | 201 | HRESULT hr = S_OK; |
@@ -251,7 +204,7 @@ static HRESULT InitializeCoreClr( | |||
251 | if (InvalidArgFailure == hr || // old versions of hostfxr don't allow calling GetRuntimeDelegate from InitializeForApp. | 204 | if (InvalidArgFailure == hr || // old versions of hostfxr don't allow calling GetRuntimeDelegate from InitializeForApp. |
252 | HostApiUnsupportedVersion == hr) // hdt_get_function_pointer was added in .NET 5. | 205 | HostApiUnsupportedVersion == hr) // hdt_get_function_pointer was added in .NET 5. |
253 | { | 206 | { |
254 | hr = InitializeCoreClrPre5(pState, wzNativeHostPath); | 207 | BalExitOnFailure(hr, "HostfxrGetRuntimeDelegate failed, most likely because the target framework is older than .NET 5."); |
255 | } | 208 | } |
256 | else | 209 | else |
257 | { | 210 | { |
@@ -261,175 +214,3 @@ static HRESULT InitializeCoreClr( | |||
261 | LExit: | 214 | LExit: |
262 | return hr; | 215 | return hr; |
263 | } | 216 | } |
264 | |||
265 | static HRESULT InitializeCoreClrPre5( | ||
266 | __in HOSTFXR_STATE* pState, | ||
267 | __in LPCWSTR wzNativeHostPath | ||
268 | ) | ||
269 | { | ||
270 | HRESULT hr = S_OK; | ||
271 | int32_t rc = 0; | ||
272 | LPCWSTR* rgPropertyKeys = NULL; | ||
273 | LPCWSTR* rgPropertyValues = NULL; | ||
274 | size_t cProperties = 0; | ||
275 | LPWSTR* rgDirectories = NULL; | ||
276 | UINT cDirectories = 0; | ||
277 | LPWSTR sczCoreClrPath = NULL; | ||
278 | |||
279 | // We are not using hostfxr as it was intended to be used. We need to initialize hostfxr so that it properly initializes hostpolicy - | ||
280 | // there are pieces of the framework such as AssemblyDependencyResolver that won't work without that. We also need hostfxr to find a | ||
281 | // compatible framework for framework-dependent deployed BAs. We had to use hostfxr_initialize_for_dotnet_command_line since | ||
282 | // hostfxr_initialize_for_runtime_config doesn't currently (3.x) support self-contained deployed BAs. That means we're supposed to | ||
283 | // start the runtime through hostfxr_run_app, but that method shuts down the runtime before returning. We actually want to call | ||
284 | // hostfxr_get_runtime_delegate, but that method currently requires hostfxr to be initialized through | ||
285 | // hostfxr_initialize_for_runtime_config. So we're forced to locate coreclr.dll and manually load the runtime ourselves. | ||
286 | |||
287 | // Unfortunately, that's not the only problem. hostfxr has global state that tracks whether it started the runtime. While we keep our | ||
288 | // hostfxr_handle open, everyone that calls the hostfxr_initialize_* methods will block until we have started the runtime through | ||
289 | // hostfxr or closed our handle. If we close the handle, then hostfxr could potentially try to load a second runtime into the | ||
290 | // 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 | ||
291 | // start the runtime anyway. | ||
292 | |||
293 | rc = pState->pfnHostfxrGetRuntimeProperties(pState->hostContextHandle, &cProperties, rgPropertyKeys, rgPropertyValues); | ||
294 | if (HostApiBufferTooSmall != rc) | ||
295 | { | ||
296 | BalExitOnFailure(hr = rc, "HostfxrGetRuntimeProperties failed to return required size."); | ||
297 | } | ||
298 | |||
299 | rgPropertyKeys = static_cast<LPCWSTR*>(MemAlloc(sizeof(LPWSTR) * cProperties, TRUE)); | ||
300 | rgPropertyValues = static_cast<LPCWSTR*>(MemAlloc(sizeof(LPWSTR) * cProperties, TRUE)); | ||
301 | if (!rgPropertyKeys || !rgPropertyValues) | ||
302 | { | ||
303 | BalExitOnFailure(hr = E_OUTOFMEMORY, "Failed to allocate buffers for runtime properties."); | ||
304 | } | ||
305 | |||
306 | hr = pState->pfnHostfxrGetRuntimeProperties(pState->hostContextHandle, &cProperties, rgPropertyKeys, rgPropertyValues); | ||
307 | BalExitOnFailure(hr, "HostfxrGetRuntimeProperties failed."); | ||
308 | |||
309 | for (DWORD i = 0; i < cProperties; ++i) | ||
310 | { | ||
311 | if (CSTR_EQUAL == ::CompareString(LOCALE_INVARIANT, 0, rgPropertyKeys[i], -1, L"NATIVE_DLL_SEARCH_DIRECTORIES", -1)) | ||
312 | { | ||
313 | hr = StrSplitAllocArray(&rgDirectories, &cDirectories, rgPropertyValues[i], L";"); | ||
314 | BalExitOnFailure(hr, "Failed to split NATIVE_DLL_SEARCH_DIRECTORIES '%ls'", rgPropertyValues[i]); | ||
315 | } | ||
316 | } | ||
317 | |||
318 | for (DWORD i = 0; i < cDirectories; ++i) | ||
319 | { | ||
320 | Assert(rgDirectories); | ||
321 | |||
322 | hr = PathConcat(rgDirectories[i], L"coreclr.dll", &sczCoreClrPath); | ||
323 | BalExitOnFailure(hr, "Failed to allocate path to coreclr."); | ||
324 | |||
325 | if (::PathFileExists(sczCoreClrPath)) | ||
326 | { | ||
327 | break; | ||
328 | } | ||
329 | else | ||
330 | { | ||
331 | ReleaseNullStr(sczCoreClrPath); | ||
332 | } | ||
333 | } | ||
334 | |||
335 | if (!sczCoreClrPath) | ||
336 | { | ||
337 | for (DWORD i = 0; i < cProperties; ++i) | ||
338 | { | ||
339 | BalLog(BOOTSTRAPPER_LOG_LEVEL_ERROR, "%ls: %ls", rgPropertyKeys[i], rgPropertyValues[i]); | ||
340 | } | ||
341 | BalExitWithRootFailure(hr, E_FILENOTFOUND, "Failed to locate coreclr.dll."); | ||
342 | } | ||
343 | |||
344 | hr = LoadCoreClr(pState, sczCoreClrPath); | ||
345 | BalExitOnFailure(hr, "Failed to load coreclr."); | ||
346 | |||
347 | hr = StartCoreClr(pState, wzNativeHostPath, (DWORD)cProperties, rgPropertyKeys, rgPropertyValues); | ||
348 | BalExitOnFailure(hr, "Failed to start coreclr."); | ||
349 | |||
350 | LExit: | ||
351 | ReleaseMem(rgDirectories); | ||
352 | ReleaseMem(rgPropertyValues); | ||
353 | ReleaseMem(rgPropertyKeys); | ||
354 | ReleaseStr(sczCoreClrPath); | ||
355 | |||
356 | return hr; | ||
357 | } | ||
358 | |||
359 | static HRESULT LoadCoreClr( | ||
360 | __in HOSTFXR_STATE* pState, | ||
361 | __in LPCWSTR wzCoreClrPath | ||
362 | ) | ||
363 | { | ||
364 | HRESULT hr = S_OK; | ||
365 | HMODULE hModule = NULL; | ||
366 | |||
367 | hModule = ::LoadLibraryExW(wzCoreClrPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | ||
368 | BalExitOnNullWithLastError(hModule, hr, "Failed to load coreclr.dll from '%ls'.", wzCoreClrPath); | ||
369 | |||
370 | pState->pfnCoreclrInitialize = reinterpret_cast<coreclr_initialize_ptr>(::GetProcAddress(hModule, "coreclr_initialize")); | ||
371 | BalExitOnNullWithLastError(pState->pfnCoreclrInitialize, hr, "Failed to get procedure address for coreclr_initialize."); | ||
372 | |||
373 | pState->pfnCoreclrCreateDelegate = reinterpret_cast<coreclr_create_delegate_ptr>(::GetProcAddress(hModule, "coreclr_create_delegate")); | ||
374 | BalExitOnNullWithLastError(pState->pfnCoreclrCreateDelegate, hr, "Failed to get procedure address for coreclr_create_delegate."); | ||
375 | |||
376 | LExit: | ||
377 | // Never unload the module since coreclr doesn't support it. | ||
378 | |||
379 | return hr; | ||
380 | } | ||
381 | |||
382 | static HRESULT StartCoreClr( | ||
383 | __in HOSTFXR_STATE* pState, | ||
384 | __in LPCWSTR wzNativeHostPath, | ||
385 | __in DWORD cProperties, | ||
386 | __in LPCWSTR* propertyKeys, | ||
387 | __in LPCWSTR* propertyValues | ||
388 | ) | ||
389 | { | ||
390 | HRESULT hr = S_OK; | ||
391 | LPSTR szNativeHostPath = NULL; | ||
392 | LPSTR* rgPropertyKeys = NULL; | ||
393 | LPSTR* rgPropertyValues = NULL; | ||
394 | |||
395 | rgPropertyKeys = static_cast<LPSTR*>(MemAlloc(sizeof(LPSTR) * cProperties, TRUE)); | ||
396 | rgPropertyValues = static_cast<LPSTR*>(MemAlloc(sizeof(LPSTR) * cProperties, TRUE)); | ||
397 | if (!rgPropertyKeys || !rgPropertyValues) | ||
398 | { | ||
399 | BalExitOnFailure(hr = E_OUTOFMEMORY, "Failed to allocate buffers for runtime properties."); | ||
400 | } | ||
401 | |||
402 | hr = StrAnsiAllocString(&szNativeHostPath, wzNativeHostPath, 0, CP_UTF8); | ||
403 | BalExitOnFailure(hr, "Failed to convert module path to UTF8: %ls", wzNativeHostPath); | ||
404 | |||
405 | for (DWORD i = 0; i < cProperties; ++i) | ||
406 | { | ||
407 | hr = StrAnsiAllocString(&rgPropertyKeys[i], propertyKeys[i], 0, CP_UTF8); | ||
408 | BalExitOnFailure(hr, "Failed to convert property key to UTF8: %ls", propertyKeys[i]); | ||
409 | |||
410 | hr = StrAnsiAllocString(&rgPropertyValues[i], propertyValues[i], 0, CP_UTF8); | ||
411 | BalExitOnFailure(hr, "Failed to convert property value to UTF8: %ls", propertyValues[i]); | ||
412 | } | ||
413 | |||
414 | hr = pState->pfnCoreclrInitialize(szNativeHostPath, "MBA", cProperties, (LPCSTR*)rgPropertyKeys, (LPCSTR*)rgPropertyValues, &pState->pClrHandle, &pState->dwDomainId); | ||
415 | BalExitOnFailure(hr, "CoreclrInitialize failed."); | ||
416 | |||
417 | LExit: | ||
418 | for (DWORD i = 0; i < cProperties; ++i) | ||
419 | { | ||
420 | if (rgPropertyKeys) | ||
421 | { | ||
422 | ReleaseStr(rgPropertyKeys[i]); | ||
423 | } | ||
424 | |||
425 | if (rgPropertyValues) | ||
426 | { | ||
427 | ReleaseStr(rgPropertyValues[i]); | ||
428 | } | ||
429 | } | ||
430 | ReleaseMem(rgPropertyValues); | ||
431 | ReleaseMem(rgPropertyKeys); | ||
432 | ReleaseStr(szNativeHostPath); | ||
433 | |||
434 | return hr; | ||
435 | } | ||