Skip to content

Override corehost_resolve_component_dependencies and corehost_set_error_writer PInvokes in singlefile scenario. #60046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the extension needed for hostpolicy, but not for System.IO.Compression.Native on Windows and libhostpolicy on Unix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the differences between

internal const string HostPolicy = "hostpolicy.dll";

and

internal const string HostPolicy = "libhostpolicy";

hostpolicy seems to be using a naming approach that is different from System.IO.Compression.Native and I am not sure if there are reasons for that of if changing could introduce any additional risk.

It could be worth to consider removing the ".dll" in 7.0.
This fix will be likely ported to 6.0, so I think it should not be in this change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracking issue: #60080

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, Thanks!


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;
}
Copy link
Member

@am11 am11 Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An assert and a warning after this block might help us catch other cases in the future. Something like:

Suggested change
}
}
trace::warning(_X("Hostpolicy P/Invoke override for [%s] was not found in the bundle bundle."), entrypointName);
assert(false); // all cases should be handled above

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ok for any user to try calling methods that do not exist in hostpolicy. It is also (although very unlikely) that unrelated hostpolicy.dll may exist and user PInvokes something from it that is real. Also there could be additional overrides registered at higher level, - in theory.

Basically, while very unlikely, there could be scenarios when not finding an override for a hostpolicy PInvoke is expected and ok.

I think, as a matter of testing in controlled environment, an assert makes sense here, but not a warning, since, strictly speaking this is not a failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually even an assert could become a blocking nuisance if someone needs to use Checked/Debug build and there is a call to something that does not exist in hostpolicy.
Otherwise it should be a "method missing" exception that can be caught and handled/ignored.

I think this part should stay as-is.

}

#if defined(TARGET_OSX)
if (strcmp(libraryName, "libSystem.Security.Cryptography.Native.Apple") == 0)
Expand Down