Skip to content
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

Single-File host does not correctly redirect hostpolicy PInvokes #39907

Closed
vitek-karas opened this issue Jul 24, 2020 · 3 comments · Fixed by #40014
Closed

Single-File host does not correctly redirect hostpolicy PInvokes #39907

vitek-karas opened this issue Jul 24, 2020 · 3 comments · Fixed by #40014
Assignees
Milestone

Comments

@vitek-karas
Copy link
Member

vitek-karas commented Jul 24, 2020

There's a managed component AssemblyDependencyResolver which is implemented by PInvoking into hostpolicy. Since in the new single-file the hostpolicy doesn't exist as a separate module, we need to redirect the PInvokes to the main module, just like we do for other statically linked libraries.

Note that this needs to work on Windows as well - which is different from the case we already implement which is Linux only.

Repro:

using System.Runtime.Loader;

namespace app
{
    class Program
    {
        static void Main(string[] args)
        {
            AssemblyDependencyResolver resolver = new AssemblyDependencyResolver("path.dll");
        }
    }
}

Run as a single-file self-contained console app. i.e. dotnet publish -r win-x64 /p:PublishSingleFile=true. The result is:

Unhandled exception. System.InvalidOperationException: Cannot load hostpolicy library. AssemblyDependencyResolver is currently only supported if the runtime is hosted through hostpolicy library.
 ---> System.DllNotFoundException: Dll was not found.
   at Interop.HostPolicy.corehost_set_error_writer(IntPtr errorWriter)
   at System.Runtime.Loader.AssemblyDependencyResolver..ctor(String componentAssemblyPath)

It will work if published as FDD single-file i.e. dotnet publish -r win-x64 --self-contained false /p:PublishSingleFile=true - this will now fail with error about not finding path.dll.

I verified that it behaves the same on Linux with the latest RC from installers.

@ghost
Copy link

ghost commented Jul 24, 2020

Tagging subscribers to this area: @swaroop-sridhar, @agocke
See info in area-owners.md if you want to be subscribed.

@vitek-karas vitek-karas added this to the 5.0.0 milestone Jul 24, 2020
@vitek-karas
Copy link
Member Author

/cc @agocke @VSadov

@vitek-karas
Copy link
Member Author

I spent some time today looking into this:

  • On Linux this is relatively simple - just add libhostpolicy to the list of "Special" libraries in dllimport.cpp
  • On Windows this a bit more tricky:
    • We need to special case hostpolicy.dll in dllimport.cpp as well, but only when running with single-file host with hostpolicy included
    • The coreclr itself can't tell if single-file host has been used which has hostpolicy linked in. The best solution is to pass a new property from hostpolicy to runtime upon init and remember this in the runtime. One day when we switch to using superhost on Windows as well, this could go away.

I got it working in a hacky way on Windows as a proof of concept (the special cased library returns WszGetModuleHandle(NULL) as the handle to the executable). But then it showed more issues in hostpolicy, so AssemblyDependencyResolver is still broken. I'm looking into the hostpolicy code some more, there are likely additional changes necessary.

@vitek-karas vitek-karas self-assigned this Jul 28, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant