Skip to content

Commit

Permalink
[release/6.0] Override corehost_resolve_component_dependencies and …
Browse files Browse the repository at this point in the history
…`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 <vitek.karas@microsoft.com>
  • Loading branch information
3 people committed Oct 8, 2021
1 parent 9dfda4f commit fba3f13
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/installer/tests/Assets/TestProjects/AppWithSubDirs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
11 changes: 11 additions & 0 deletions src/native/corehost/hostpolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
5 changes: 0 additions & 5 deletions src/native/corehost/hostpolicy/hostpolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions src/native/corehost/hostpolicy/hostpolicy_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <error_codes.h>
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down

0 comments on commit fba3f13

Please sign in to comment.