From 878682acb078200e477a388cf12662a215aafb07 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Mon, 4 Oct 2021 10:25:28 -0400 Subject: [PATCH 1/4] [CoreLib] Move ComponentActivator to the shared area --- .../System.Private.CoreLib/System.Private.CoreLib.csproj | 2 -- .../src/ILLink/ILLink.Descriptors.Shared.xml | 8 -------- .../src/ILLink/ILLink.Substitutions.xml | 3 --- .../src/ILLink/ILLink.Suppressions.LibraryBuild.xml | 7 ------- .../src/ILLink/ILLink.Descriptors.Shared.xml | 8 ++++++++ .../src/ILLink/ILLink.Substitutions.Shared.xml | 3 +++ .../src/ILLink/ILLink.Suppressions.LibraryBuild.xml | 7 +++++++ .../Runtime/InteropServices/ComponentActivator.cs | 0 .../InteropServices/IsolatedComponentLoadContext.cs | 0 .../src/System.Private.CoreLib.Shared.projitems | 2 ++ 10 files changed, 20 insertions(+), 20 deletions(-) rename src/{coreclr => libraries}/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs (100%) rename src/{coreclr => libraries}/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs (100%) diff --git a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj index 36b4d648bdfb2..b069dc0086067 100644 --- a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -111,9 +111,7 @@ - - diff --git a/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml b/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml index 4384581b1d132..1b4e3a1bd2acd 100644 --- a/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml +++ b/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml @@ -22,14 +22,6 @@ - - - - - - diff --git a/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml b/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml index fcbdde6d6494d..227fdc69d0d93 100644 --- a/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml +++ b/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml @@ -3,8 +3,5 @@ - - - diff --git a/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml b/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml index 610fb95a4250d..ae5bf9ff04e32 100644 --- a/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml +++ b/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml @@ -8,13 +8,6 @@ M:System.StartupHookProvider.ProcessStartupHooks() This warning is left in the product so developers get an ILLink warning when trimming an app with System.StartupHookProvider.IsSupported=true. - - ILLink - IL2026 - member - M:Internal.Runtime.InteropServices.ComponentActivator.GetFunctionPointer(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr) - This warning is left in the product so developers get an ILLink warning when trimming an app with Internal.Runtime.InteropServices.ComponentActivator.IsSupported=true. - ILLink IL2026 diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml index 5f0f0d2d916a9..0618f1644ea32 100644 --- a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml +++ b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml @@ -6,6 +6,14 @@ https://github.com/mono/linker/pull/649 --> + + + + + + diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml index 80bb81c443fa2..9765f3ab4f662 100644 --- a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml +++ b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml @@ -34,5 +34,8 @@ + + + diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml index 695f2477d9b08..64710f5c85410 100644 --- a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml +++ b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml @@ -8,5 +8,12 @@ M:System.Resources.ManifestBasedResourceGroveler.CreateResourceSet(System.IO.Stream,System.Reflection.Assembly) This warning is left in the product so developers get an ILLink warning when trimming an app with System.Resources.ResourceManager.AllowCustomResourceTypes=true. + + ILLink + IL2026 + member + M:Internal.Runtime.InteropServices.ComponentActivator.GetFunctionPointer(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr) + This warning is left in the product so developers get an ILLink warning when trimming an app with Internal.Runtime.InteropServices.ComponentActivator.IsSupported=true. + diff --git a/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs similarity index 100% rename from src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs rename to src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs diff --git a/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs similarity index 100% rename from src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs rename to src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 0af9bbaac7472..867e13262ba6a 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -57,6 +57,8 @@ + + From 5c30a82505071936507669f8b6f490b98c1bfb41 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Mon, 4 Oct 2021 12:52:50 -0400 Subject: [PATCH 2/4] [mono] Implement coreclr_create_delegate hosting API Related to https://github.com/dotnet/runtime/issues/59815 --- src/mono/mono/mini/main-core.c | 3 +- src/mono/mono/mini/monovm.c | 87 ++++++++++++++++++++++++++++++++++ src/mono/mono/mini/monovm.h | 5 ++ 3 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/mono/mono/mini/main-core.c b/src/mono/mono/mini/main-core.c index 41318fe661fba..11866090e1704 100644 --- a/src/mono/mono/mini/main-core.c +++ b/src/mono/mono/mini/main-core.c @@ -115,6 +115,5 @@ int STDAPICALLTYPE coreclr_create_delegate (void* hostHandle, unsigned int domai const char* entryPointAssemblyName, const char* entryPointTypeName, const char* entryPointMethodName, void** delegate) { - g_error ("Not implemented"); - return 0; + return monovm_create_delegate (entryPointAssemblyName, entryPointTypeName, entryPointMethodName, delegate); } diff --git a/src/mono/mono/mini/monovm.c b/src/mono/mono/mini/monovm.c index 2524a223a647b..e148596216599 100644 --- a/src/mono/mono/mini/monovm.c +++ b/src/mono/mono/mini/monovm.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -276,3 +277,89 @@ monovm_shutdown (int *latchedExitCode) return 0; } + +static int +monovm_create_delegate_impl (const char* assemblyName, const char* typeName, const char *methodName, void **delegate); + + +int +monovm_create_delegate (const char *assemblyName, const char *typeName, const char *methodName, void **delegate) +{ + int result; + /* monovm_create_delegate may be called instead of monovm_execute_assembly. Initialize the + * runtime if it isn't already. */ + if (!mono_get_root_domain()) + mini_init (assemblyName, "v4.0.30319"); + MONO_ENTER_GC_UNSAFE; + result = monovm_create_delegate_impl (assemblyName, typeName, methodName, delegate); + MONO_EXIT_GC_UNSAFE; + return result; +} + +int +monovm_create_delegate_impl (const char* assemblyName, const char* typeName, const char *methodName, void **delegate) +{ + // Load an assembly and a type and a method from the type, then return a pointer to a native + // callable version of the method. See CorHost2::CreateDelegate + // + // We have to do this in general, but the CoreCLR hostpolicy only calls this with a handful + // of methods from the [CoreLib]Internal.Runtime.InteropServices.ComponentActivator + // class. See hostpolicy.cpp get_delegate() + + ERROR_DECL (error); + + const int failure = 0x80004005; /* E_FAIL */ + + if (!delegate) + return failure; + *delegate = NULL; + + MonoAssemblyLoadContext *alc = mono_alc_get_default (); + + MonoImage *image; + if (!strcmp (MONO_ASSEMBLY_CORLIB_NAME, assemblyName)) { + image = mono_defaults.corlib; + } else { + MonoAssemblyName aname = {0}; + aname.name = assemblyName; + MonoAssemblyByNameRequest req; + mono_assembly_request_prepare_byname (&req, alc); + MonoImageOpenStatus status = MONO_IMAGE_OK; + MonoAssembly *assm = mono_assembly_request_byname (&aname, &req, &status); + + if (!assm || status != MONO_IMAGE_OK) + return failure; + image = assm->image; + } + + MonoType *t = mono_reflection_type_from_name_checked ((char*)typeName, alc, image, error); + goto_if_nok (error, fail); + + g_assert (t); + MonoClass *klass = mono_class_from_mono_type_internal (t); + + + MonoMethod *method = mono_class_get_method_from_name_checked (klass, methodName, -1, 0, error); + goto_if_nok (error, fail); + + if (!mono_method_has_unmanaged_callers_only_attribute (method)) { + mono_error_set_not_supported (error, "MonoVM only supports UnmanagedCallersOnly implementations of hostfxr_get_runtime_delegate delegate types"); + goto fail; + } + + MonoClass *delegate_klass = NULL; + MonoGCHandle target_handle = 0; + MonoMethod *wrapper = mono_marshal_get_managed_wrapper (method, delegate_klass, target_handle, error); + goto_if_nok (error, fail); + + gpointer addr = mono_compile_method_checked (wrapper, error); + goto_if_nok (error, fail); + + *delegate = addr; + return 0; + +fail: + g_warning ("coreclr_create_delegate: failed due to %s", mono_error_get_message (error)); + mono_error_cleanup (error); + return failure; +} diff --git a/src/mono/mono/mini/monovm.h b/src/mono/mono/mini/monovm.h index 09fc3d3850fa4..990307925019f 100644 --- a/src/mono/mono/mini/monovm.h +++ b/src/mono/mono/mini/monovm.h @@ -17,4 +17,9 @@ monovm_execute_assembly (int argc, const char **argv, const char *managedAssembl MONO_API int monovm_shutdown (int *latchedExitCode); +int +monovm_create_delegate (const char *assemblyName, const char *typeName, const char *methodName, void **delegate); + + + #endif // _MONO_MINI_MONOVM_H_ From f35e51749f599baa209f1fe6fe2d1df69a367fc6 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Mon, 4 Oct 2021 15:49:40 -0400 Subject: [PATCH 3/4] Mark IsolatedComponentLoadContext as unsupported on mobile and browser Should make ComponentActivator work without them --- .../Runtime/InteropServices/ComponentActivator.cs | 13 +++++++++++++ .../InteropServices/IsolatedComponentLoadContext.cs | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs index e276025cfd37b..37d8b610c04cb 100644 --- a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs +++ b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs @@ -7,6 +7,7 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Runtime.Loader; +using System.Runtime.Versioning; namespace Internal.Runtime.InteropServices { @@ -14,6 +15,10 @@ public static class ComponentActivator { private const string TrimIncompatibleWarningMessage = "Native hosting is not trim compatible and this warning will be seen if trimming is enabled."; + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] private static readonly Dictionary s_assemblyLoadContexts = new Dictionary(StringComparer.InvariantCulture); private static readonly Dictionary s_delegates = new Dictionary(); @@ -47,6 +52,10 @@ private static string MarshalToString(IntPtr arg, string argName) /// Extensibility parameter (currently unused) /// Pointer where to store the function pointer result [RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")] + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] [UnmanagedCallersOnly] public static unsafe int LoadAssemblyAndGetFunctionPointer(IntPtr assemblyPathNative, IntPtr typeNameNative, @@ -142,6 +151,10 @@ public static unsafe int GetFunctionPointer(IntPtr typeNameNative, } [RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")] + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] private static IsolatedComponentLoadContext GetIsolatedComponentLoadContext(string assemblyPath) { IsolatedComponentLoadContext? alc; diff --git a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs index b455e371cbb0b..bd789bf522807 100644 --- a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs +++ b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs @@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.Loader; +using System.Runtime.Versioning; namespace Internal.Runtime.InteropServices { @@ -13,6 +14,10 @@ namespace Internal.Runtime.InteropServices /// or IJW components loaded from native. It provides a load context that uses an to resolve the component's /// dependencies within the ALC and not pollute the default ALC. /// + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] internal sealed class IsolatedComponentLoadContext : AssemblyLoadContext { private readonly AssemblyDependencyResolver _resolver; From 0ee0d0ace8bd7f8d417809e63d04036bdae6d847 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Mon, 4 Oct 2021 16:25:01 -0400 Subject: [PATCH 4/4] Also unsupported on MacCatalyst --- .../src/Internal/Runtime/InteropServices/ComponentActivator.cs | 2 ++ .../Runtime/InteropServices/IsolatedComponentLoadContext.cs | 1 + 2 files changed, 3 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs index 37d8b610c04cb..8b35a94463c99 100644 --- a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs +++ b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs @@ -18,6 +18,7 @@ public static class ComponentActivator [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("maccatalyst")] [UnsupportedOSPlatform("tvos")] private static readonly Dictionary s_assemblyLoadContexts = new Dictionary(StringComparer.InvariantCulture); private static readonly Dictionary s_delegates = new Dictionary(); @@ -55,6 +56,7 @@ private static string MarshalToString(IntPtr arg, string argName) [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("maccatalyst")] [UnsupportedOSPlatform("tvos")] [UnmanagedCallersOnly] public static unsafe int LoadAssemblyAndGetFunctionPointer(IntPtr assemblyPathNative, diff --git a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs index bd789bf522807..94ed1f4441ba8 100644 --- a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs +++ b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/IsolatedComponentLoadContext.cs @@ -17,6 +17,7 @@ namespace Internal.Runtime.InteropServices [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("maccatalyst")] [UnsupportedOSPlatform("tvos")] internal sealed class IsolatedComponentLoadContext : AssemblyLoadContext {