From 30887533f3fda32b56d9b19a5b7100da9e3788f7 Mon Sep 17 00:00:00 2001 From: Ryan Lucia Date: Thu, 29 Aug 2019 12:35:15 -0400 Subject: [PATCH] [netcore] Implement AssemblyLoadContext.LoadUnmanagedDllFromPath (mono/mono#16525) * [netcore] Implement AssemblyLoadContext.LoadUnmanagedDllFromPath * Fixes Commit migrated from https://github.com/mono/mono/commit/3b57849541b35a8f6cced2957b9a6fdc0680136f --- src/mono/mono/metadata/assembly-load-context.c | 32 ++++++++++++++++++++++ src/mono/mono/metadata/icall-def-netcore.h | 1 + .../System.Runtime.Loader/AssemblyLoadContext.cs | 6 ++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/mono/mono/metadata/assembly-load-context.c b/src/mono/mono/metadata/assembly-load-context.c index 344b586..4e5abba 100644 --- a/src/mono/mono/metadata/assembly-load-context.c +++ b/src/mono/mono/metadata/assembly-load-context.c @@ -5,6 +5,7 @@ #include "mono/metadata/assembly.h" #include "mono/metadata/domain-internals.h" +#include "mono/metadata/exception-internals.h" #include "mono/metadata/icall-decl.h" #include "mono/metadata/loader-internals.h" #include "mono/metadata/loaded-images-internals.h" @@ -105,6 +106,37 @@ ves_icall_System_Runtime_Loader_AssemblyLoadContext_GetLoadContextForAssembly (M return GUINT_TO_POINTER (alc->gchandle); } +gpointer +ves_icall_System_Runtime_Loader_AssemblyLoadContext_InternalLoadUnmanagedDllFromPath (MonoStringHandle fname, MonoError *error) +{ + gpointer res = NULL; + MonoDl *lib = NULL; + char *filename = NULL; + char *local_error = NULL; + + g_assert (!MONO_HANDLE_IS_NULL (fname)); // should have already been checked in managed, so we assert + + filename = mono_string_handle_to_utf8 (fname, error); + goto_if_nok (error, exit); + + g_assert (g_path_is_absolute (filename)); // again, checked in managed + + lib = mono_dl_open (filename, MONO_DL_LAZY, &local_error); + + if (lib == NULL) { + mono_error_set_file_not_found (error, filename, "%s", local_error); + goto exit; + } + + res = lib->handle; + +exit: + g_free (lib); + g_free (filename); + g_free (local_error); + return res; +} + gboolean mono_alc_is_default (MonoAssemblyLoadContext *alc) { diff --git a/src/mono/mono/metadata/icall-def-netcore.h b/src/mono/mono/metadata/icall-def-netcore.h index 9c7c2a3..da429c5 100644 --- a/src/mono/mono/metadata/icall-def-netcore.h +++ b/src/mono/mono/metadata/icall-def-netcore.h @@ -375,6 +375,7 @@ HANDLES(ALC_4, "InternalGetLoadedAssemblies", ves_icall_System_Runtime_Loader_As HANDLES(ALC_2, "InternalInitializeNativeALC", ves_icall_System_Runtime_Loader_AssemblyLoadContext_InternalInitializeNativeALC, gpointer, 3, (gpointer, MonoBoolean, MonoBoolean)) HANDLES(ALC_1, "InternalLoadFile", ves_icall_System_Runtime_Loader_AssemblyLoadContext_InternalLoadFile, MonoReflectionAssembly, 3, (gpointer, MonoString, MonoStackCrawlMark_ptr)) HANDLES(ALC_3, "InternalLoadFromStream", ves_icall_System_Runtime_Loader_AssemblyLoadContext_InternalLoadFromStream, MonoReflectionAssembly, 5, (gpointer, gpointer, gint32, gpointer, gint32)) +HANDLES(ALC_6, "InternalLoadUnmanagedDllFromPath", ves_icall_System_Runtime_Loader_AssemblyLoadContext_InternalLoadUnmanagedDllFromPath, gpointer, 1, (MonoString)) ICALL_TYPE(RUNIMPORT, "System.Runtime.RuntimeImports", RUNIMPORT_1) NOHANDLES(ICALL(RUNIMPORT_1, "RhBulkMoveWithWriteBarrier", ves_icall_System_Runtime_RuntimeImports_RhBulkMoveWithWriteBarrier)) diff --git a/src/mono/netcore/System.Private.CoreLib/src/System.Runtime.Loader/AssemblyLoadContext.cs b/src/mono/netcore/System.Private.CoreLib/src/System.Runtime.Loader/AssemblyLoadContext.cs index af157bf..0ac47c9 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System.Runtime.Loader/AssemblyLoadContext.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System.Runtime.Loader/AssemblyLoadContext.cs @@ -27,10 +27,8 @@ namespace System.Runtime.Loader { } - static IntPtr InternalLoadUnmanagedDllFromPath (string unmanagedDllPath) - { - throw new NotImplementedException (); - } + [MethodImplAttribute (MethodImplOptions.InternalCall)] + extern static IntPtr InternalLoadUnmanagedDllFromPath (string unmanagedDllPath); [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod Assembly InternalLoadFromPath (string assemblyPath, string nativeImagePath) -- 2.7.4