From fba3f131d8a2f783f6095b75fddaf0c9c97e51c4 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 8 Oct 2021 12:08:24 -0700 Subject: [PATCH] [release/6.0] Override `corehost_resolve_component_dependencies` and `corehost_set_error_writer` PInvokes in singlefile scenario. (#60111) * Override `corehost_resolve_component_dependencies` and `corehost_set_error_writer` PInvokes in singlefile scenario. * fix for GCC * Fixes the test failures when hostpolicy is used at runtime. Co-authored-by: vsadov <8218165+VSadov@users.noreply.github.com> Co-authored-by: vitek-karas --- .../TestProjects/AppWithSubDirs/Program.cs | 15 +++++++++++++++ .../AppHost.Bundle.Tests/BundleTestBase.cs | 4 ++-- src/native/corehost/hostpolicy.h | 11 +++++++++++ src/native/corehost/hostpolicy/hostpolicy.cpp | 5 ----- .../corehost/hostpolicy/hostpolicy_context.cpp | 18 ++++++++++++++++++ 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/installer/tests/Assets/TestProjects/AppWithSubDirs/Program.cs b/src/installer/tests/Assets/TestProjects/AppWithSubDirs/Program.cs index c1a31dec98665..0a5d0149fff21 100644 --- a/src/installer/tests/Assets/TestProjects/AppWithSubDirs/Program.cs +++ b/src/installer/tests/Assets/TestProjects/AppWithSubDirs/Program.cs @@ -12,6 +12,21 @@ public static void Main(string[] args) { string baseDir = Path.Combine(AppContext.BaseDirectory, "Sentence"); + // singlefile app redirects a number of known internal PInvokes to their implementations + // which are statically linked into the host. + // Use of File IO, Console APIs and the like uses and tests those mechanisms extensively. + // + // There are also two PInvokes in the hostpolicy itself. Test them here. + // We are not looking for a success, just that we can get to these methods - + // we should not see errors like "Unable to find an entry point". + try + { + new System.Runtime.Loader.AssemblyDependencyResolver("qwerty"); + } + catch (InvalidOperationException ex) when (ex.Message.Contains("Failed to locate managed application")) + { + } + string Part(string dir="", string subdir="", string subsubdir="") { return File.ReadAllText(Path.Combine(baseDir, dir, subdir, subsubdir, "word")); diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleTestBase.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleTestBase.cs index 18aba06f6d978..669b300cb838a 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleTestBase.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleTestBase.cs @@ -25,7 +25,7 @@ public static string UseSingleFileSelfContainedHost(TestProjectFixture testFixtu var publishedHostPath = BundleHelper.GetHostPath(testFixture); HostWriter.CreateAppHost(singleFileHost, publishedHostPath, - BundleHelper.GetAppPath(testFixture)); + BundleHelper.GetAppName(testFixture)); return publishedHostPath; } @@ -37,7 +37,7 @@ public static string UseFrameworkDependentHost(TestProjectFixture testFixture) var publishedHostPath = BundleHelper.GetHostPath(testFixture); HostWriter.CreateAppHost(appHost, publishedHostPath, - BundleHelper.GetAppPath(testFixture)); + BundleHelper.GetAppName(testFixture)); return publishedHostPath; } diff --git a/src/native/corehost/hostpolicy.h b/src/native/corehost/hostpolicy.h index 27ada5fb18fc1..8c152325c6a9b 100644 --- a/src/native/corehost/hostpolicy.h +++ b/src/native/corehost/hostpolicy.h @@ -37,4 +37,15 @@ typedef int(HOSTPOLICY_CALLTYPE *corehost_initialize_fn)( uint32_t options, corehost_context_contract *handle); +typedef void(HOSTPOLICY_CALLTYPE* corehost_resolve_component_dependencies_result_fn)( + const pal::char_t* assembly_paths, + const pal::char_t* native_search_paths, + const pal::char_t* resource_search_paths); + +SHARED_API corehost_error_writer_fn HOSTPOLICY_CALLTYPE corehost_set_error_writer(corehost_error_writer_fn error_writer); + +SHARED_API int HOSTPOLICY_CALLTYPE corehost_resolve_component_dependencies( + const pal::char_t* component_main_assembly_path, + corehost_resolve_component_dependencies_result_fn result); + #endif //__HOSTPOLICY_H__ diff --git a/src/native/corehost/hostpolicy/hostpolicy.cpp b/src/native/corehost/hostpolicy/hostpolicy.cpp index c19a444794ba4..635fb26095ca2 100644 --- a/src/native/corehost/hostpolicy/hostpolicy.cpp +++ b/src/native/corehost/hostpolicy/hostpolicy.cpp @@ -824,11 +824,6 @@ SHARED_API int HOSTPOLICY_CALLTYPE corehost_unload() return StatusCode::Success; } -typedef void(HOSTPOLICY_CALLTYPE *corehost_resolve_component_dependencies_result_fn)( - const pal::char_t* assembly_paths, - const pal::char_t* native_search_paths, - const pal::char_t* resource_search_paths); - SHARED_API int HOSTPOLICY_CALLTYPE corehost_resolve_component_dependencies( const pal::char_t *component_main_assembly_path, corehost_resolve_component_dependencies_result_fn result) diff --git a/src/native/corehost/hostpolicy/hostpolicy_context.cpp b/src/native/corehost/hostpolicy/hostpolicy_context.cpp index 2b9a953e28d53..e213e78e05b42 100644 --- a/src/native/corehost/hostpolicy/hostpolicy_context.cpp +++ b/src/native/corehost/hostpolicy/hostpolicy_context.cpp @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. #include "hostpolicy_context.h" +#include "hostpolicy.h" #include "deps_resolver.h" #include @@ -55,11 +56,15 @@ namespace const void* STDMETHODCALLTYPE pinvoke_override(const char* libraryName, const char* entrypointName) { #if defined(_WIN32) + const char* hostPolicyLib = "hostpolicy.dll"; + if (strcmp(libraryName, "System.IO.Compression.Native") == 0) { return CompressionResolveDllImport(entrypointName); } #else + const char* hostPolicyLib = "libhostpolicy"; + if (strcmp(libraryName, "libSystem.IO.Compression.Native") == 0) { return CompressionResolveDllImport(entrypointName); @@ -80,6 +85,19 @@ namespace return CryptoResolveDllImport(entrypointName); } #endif + // there are two PInvokes in the hostpolicy itself, redirect them here. + if (strcmp(libraryName, hostPolicyLib) == 0) + { + if (strcmp(entrypointName, "corehost_resolve_component_dependencies") == 0) + { + return (void*)corehost_resolve_component_dependencies; + } + + if (strcmp(entrypointName, "corehost_set_error_writer") == 0) + { + return (void*)corehost_set_error_writer; + } + } #if defined(TARGET_OSX) if (strcmp(libraryName, "libSystem.Security.Cryptography.Native.Apple") == 0)