From b139c86079a5833e9cf48da3cc0c131e7dd13c7a Mon Sep 17 00:00:00 2001 From: lateralusX Date: Wed, 27 Jan 2021 15:15:55 +0100 Subject: [PATCH 1/3] Handle NativeLibrary.GetExport on libs loaded with Interop.Kernel32.LoadLibraryEx. https://github.com/dotnet/runtime/pull/47013 changed how kernel32.dll and Ws2_32.dll gets loaded on Windows. Instead of loading using NativeLibrary.Load these system libraries are now loaded directly using LoadLibraryEx, but symbols are still handled through NativeLibrary. This short-circuits some logic in Mono that assumes all libraries gets loaded through NativeLibrary.Load. Fix adds ability to use passed in HMODULE when not finding a matching library in our native library cache and use it directly in call to GetProcAddress on Windows, inline with CoreClr behavior. --- src/mono/mono/metadata/native-library.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/mono/mono/metadata/native-library.c b/src/mono/mono/metadata/native-library.c index 2753582d00ae6d..30ca7c4768ae08 100644 --- a/src/mono/mono/metadata/native-library.c +++ b/src/mono/mono/metadata/native-library.c @@ -1610,14 +1610,26 @@ ves_icall_System_Runtime_InteropServices_NativeLibrary_GetSymbol (gpointer lib, native_library_lock (); module = netcore_handle_lookup (lib); - if (!module) + if (!module) { +#ifdef HOST_WIN32 + // netcore calls NativeLibrary.GetExport on Windows system libraries loaded directly using Interop.Kernel32.LoadLibraryEx. + // Fallback accepting NativeLibrary.GetExport calls on libraries loaded directly by Interop.Kernel32.LoadLibraryEx. + // https://github.com/dotnet/runtime/pull/47013. + symbol = GetProcAddress ((HMODULE)lib, symbol_name); + if (!symbol) + mono_error_set_generic_error (error, "System", "EntryPointNotFoundException", "%p: %s", lib, symbol_name); +#else mono_error_set_generic_error (error, "System", "DllNotFoundException", "%p: %s", lib, symbol_name); +#endif + } goto_if_nok (error, leave); - mono_dl_symbol (module, symbol_name, &symbol); - if (!symbol) - mono_error_set_generic_error (error, "System", "EntryPointNotFoundException", "%s: %s", module->full_name, symbol_name); - goto_if_nok (error, leave); + if (!symbol) { + mono_dl_symbol (module, symbol_name, &symbol); + if (!symbol) + mono_error_set_generic_error (error, "System", "EntryPointNotFoundException", "%s: %s", module->full_name, symbol_name); + goto_if_nok (error, leave); + } leave: native_library_unlock (); From 9b533327c5a632767d00f79b1b3ed6937b0b9973 Mon Sep 17 00:00:00 2001 From: lateralusX Date: Mon, 1 Feb 2021 17:53:54 +0100 Subject: [PATCH 2/3] Handle NativeLibrary GetExport/Free using IntPtr library OS handle xplat. --- src/mono/mono/metadata/native-library.c | 47 +++++++++++-------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/src/mono/mono/metadata/native-library.c b/src/mono/mono/metadata/native-library.c index 30ca7c4768ae08..c69304ce26ff5f 100644 --- a/src/mono/mono/metadata/native-library.c +++ b/src/mono/mono/metadata/native-library.c @@ -1578,16 +1578,19 @@ ves_icall_System_Runtime_InteropServices_NativeLibrary_FreeLib (gpointer lib, Mo native_library_lock (); module = netcore_handle_lookup (lib); - if (!module) - goto leave; - - ref_count = mono_refcount_dec (module); - if (ref_count > 0) - goto leave; - - g_hash_table_remove (native_library_module_map, module->handle); - g_hash_table_add (native_library_module_blocklist, module); - mono_dl_close (module); + if (module) { + ref_count = mono_refcount_dec (module); + if (ref_count > 0) + goto leave; + + g_hash_table_remove (native_library_module_map, module->handle); + g_hash_table_add (native_library_module_blocklist, module); + mono_dl_close (module); + } else { + MonoDl raw_module = { 0 }; + raw_module.handle = lib; + mono_dl_close (&raw_module); + } leave: native_library_unlock (); @@ -1610,28 +1613,18 @@ ves_icall_System_Runtime_InteropServices_NativeLibrary_GetSymbol (gpointer lib, native_library_lock (); module = netcore_handle_lookup (lib); - if (!module) { -#ifdef HOST_WIN32 - // netcore calls NativeLibrary.GetExport on Windows system libraries loaded directly using Interop.Kernel32.LoadLibraryEx. - // Fallback accepting NativeLibrary.GetExport calls on libraries loaded directly by Interop.Kernel32.LoadLibraryEx. - // https://github.com/dotnet/runtime/pull/47013. - symbol = GetProcAddress ((HMODULE)lib, symbol_name); - if (!symbol) - mono_error_set_generic_error (error, "System", "EntryPointNotFoundException", "%p: %s", lib, symbol_name); -#else - mono_error_set_generic_error (error, "System", "DllNotFoundException", "%p: %s", lib, symbol_name); -#endif - } - goto_if_nok (error, leave); - - if (!symbol) { + if (module) { mono_dl_symbol (module, symbol_name, &symbol); if (!symbol) mono_error_set_generic_error (error, "System", "EntryPointNotFoundException", "%s: %s", module->full_name, symbol_name); - goto_if_nok (error, leave); + } else { + MonoDl raw_module = { 0 }; + raw_module.handle = lib; + mono_dl_symbol (&raw_module, symbol_name, &symbol); + if (!symbol) + mono_error_set_generic_error (error, "System", "EntryPointNotFoundException", "%p: %s", lib, symbol_name); } -leave: native_library_unlock (); leave_nolock: From ff81c206a7e569cbcdd2a58abc96d7c3ea735844 Mon Sep 17 00:00:00 2001 From: lateralusX Date: Tue, 2 Feb 2021 14:37:47 +0100 Subject: [PATCH 3/3] Disable additional System.Drawing.Common tests due to missing COM support. --- .../System.Drawing.Common/tests/IconTests.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Drawing.Common/tests/IconTests.cs b/src/libraries/System.Drawing.Common/tests/IconTests.cs index bb501fc108f074..2ad23d5f18d5fd 100644 --- a/src/libraries/System.Drawing.Common/tests/IconTests.cs +++ b/src/libraries/System.Drawing.Common/tests/IconTests.cs @@ -500,7 +500,7 @@ public void Save_NullOutputStreamIconData_ThrowsNullReferenceException() [ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)] [ConditionalFact(Helpers.IsDrawingSupported)] - [ActiveIssue("https://github.com/dotnet/runtime/issues/34591", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/47759", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] public void Save_NullOutputStreamNoIconData_ThrowsArgumentNullException() { using (var source = new Icon(Helpers.GetTestBitmapPath("48x48_multiple_entries_4bit.ico"))) @@ -526,6 +526,7 @@ public void Save_ClosedOutputStreamIconData_ThrowsException() } [ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/47759", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] [ConditionalFact(Helpers.IsDrawingSupported)] public void Save_ClosedOutputStreamNoIconData_DoesNothing() { @@ -688,7 +689,7 @@ private static Icon GetPngIcon() } [ConditionalFact(Helpers.IsDrawingSupported)] - [ActiveIssue("https://github.com/dotnet/runtime/issues/34591", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/47759", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] public void FromHandle_IconHandleOneTime_Success() { using (var icon1 = new Icon(Helpers.GetTestBitmapPath("16x16_one_entry_4bit.ico"))) @@ -701,7 +702,7 @@ public void FromHandle_IconHandleOneTime_Success() } [ConditionalFact(Helpers.IsDrawingSupported)] - [ActiveIssue("https://github.com/dotnet/runtime/issues/34591", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/47759", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] public void FromHandle_IconHandleMultipleTime_Success() { using (var icon1 = new Icon(Helpers.GetTestBitmapPath("16x16_one_entry_4bit.ico"))) @@ -722,7 +723,7 @@ public void FromHandle_IconHandleMultipleTime_Success() } [ConditionalFact(Helpers.IsDrawingSupported)] - [ActiveIssue("https://github.com/dotnet/runtime/issues/34591", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/47759", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] public void FromHandle_BitmapHandleOneTime_Success() { IntPtr handle; @@ -739,7 +740,7 @@ public void FromHandle_BitmapHandleOneTime_Success() } [ConditionalFact(Helpers.IsDrawingSupported)] - [ActiveIssue("https://github.com/dotnet/runtime/issues/34591", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/47759", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] public void FromHandle_BitmapHandleMultipleTime_Success() { IntPtr handle;