From 67a478b874017b80e884a997460e061327be537a Mon Sep 17 00:00:00 2001
From: Adeel <3840695+am11@users.noreply.github.com>
Date: Wed, 16 Nov 2022 19:45:36 +0200
Subject: [PATCH 1/5] Allow interop resolvers to return self handle
---
src/mono/mono/metadata/native-library.c | 62 +++++++++++--------
src/mono/mono/utils/mono-dl.c | 9 +--
.../libs/System.Native/pal_dynamicload.c | 15 +++--
.../MainProgramHandleTests.cs | 46 ++++++++++++++
.../MainProgramHandleTests.csproj | 12 ++++
5 files changed, 107 insertions(+), 37 deletions(-)
create mode 100644 src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs
create mode 100644 src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
diff --git a/src/mono/mono/metadata/native-library.c b/src/mono/mono/metadata/native-library.c
index b2d85270cd3fc..164a3aef132f2 100644
--- a/src/mono/mono/metadata/native-library.c
+++ b/src/mono/mono/metadata/native-library.c
@@ -584,6 +584,40 @@ netcore_probe_for_module_nofail (MonoImage *image, const char *file_name, int fl
return result;
}
+static MonoDl*
+netcore_lookup_self_native_handle (void)
+{
+ ERROR_DECL (load_error);
+ if (!internal_module)
+ internal_module = mono_dl_open_self (load_error);
+
+ if (!internal_module)
+ mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "DllImport error loading library '__Internal': '%s'.", mono_error_get_message_without_fields (load_error));
+
+ mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via __Internal.");
+ mono_error_cleanup (load_error);
+
+ return internal_module;
+}
+
+static MonoDl* native_handle_lookup_wrapper (gpointer handle)
+{
+ MonoDl *result = NULL;
+
+ if (!internal_module)
+ netcore_lookup_self_native_handle ();
+
+ if (internal_module->handle == handle) {
+ result = internal_module;
+ } else {
+ native_library_lock ();
+ result = netcore_handle_lookup (handle);
+ native_library_unlock ();
+ }
+
+ return result;
+}
+
static MonoDl *
netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, const char *scope, guint32 flags, MonoError *error)
{
@@ -631,9 +665,7 @@ netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAsse
mono_runtime_invoke_checked (resolve, NULL, args, error);
goto_if_nok (error, leave);
- native_library_lock ();
- result = netcore_handle_lookup (lib);
- native_library_unlock ();
+ result = native_handle_lookup_wrapper (lib);
leave:
HANDLE_FUNCTION_RETURN_VAL (result);
@@ -688,9 +720,7 @@ netcore_resolve_with_load (MonoAssemblyLoadContext *alc, const char *scope, Mono
mono_runtime_invoke_checked (resolve, NULL, args, error);
goto_if_nok (error, leave);
- native_library_lock ();
- result = netcore_handle_lookup (lib);
- native_library_unlock ();
+ result = native_handle_lookup_wrapper (lib);
leave:
HANDLE_FUNCTION_RETURN_VAL (result);
@@ -755,9 +785,7 @@ netcore_resolve_with_resolving_event (MonoAssemblyLoadContext *alc, MonoAssembly
mono_runtime_invoke_checked (resolve, NULL, args, error);
goto_if_nok (error, leave);
- native_library_lock ();
- result = netcore_handle_lookup (lib);
- native_library_unlock ();
+ result = native_handle_lookup_wrapper (lib);
leave:
HANDLE_FUNCTION_RETURN_VAL (result);
@@ -802,22 +830,6 @@ netcore_check_alc_cache (MonoAssemblyLoadContext *alc, const char *scope)
return result;
}
-static MonoDl*
-netcore_lookup_self_native_handle (void)
-{
- ERROR_DECL (load_error);
- if (!internal_module)
- internal_module = mono_dl_open_self (load_error);
-
- if (!internal_module)
- mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "DllImport error loading library '__Internal': '%s'.", mono_error_get_message_without_fields (load_error));
-
- mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via __Internal.");
- mono_error_cleanup (load_error);
-
- return internal_module;
-}
-
static MonoDl *
netcore_lookup_native_library (MonoAssemblyLoadContext *alc, MonoImage *image, const char *scope, guint32 flags)
{
diff --git a/src/mono/mono/utils/mono-dl.c b/src/mono/mono/utils/mono-dl.c
index 512ce7205e606..bce99682e0c6d 100644
--- a/src/mono/mono/utils/mono-dl.c
+++ b/src/mono/mono/utils/mono-dl.c
@@ -176,8 +176,9 @@ fix_libc_name (const char *name)
* mono_dl_open_self:
* \param error pointer to MonoError
*
- * Returns a handle to the main program, on android x86 it's not possible to
- * call dl_open(null), it returns a null handle, so this function returns RTLD_DEFAULT
+ * Returns a handle to the main program, on Android it's not possible to
+ * call dl_open(null) with RTLD_LAZY, it returns a null handle, so this
+ * function uses RTLD_NOW.
* handle in this platform.
* \p error points to MonoError where an error will be stored in
* case of failure. The error needs to be cleared when done using it, \c mono_error_cleanup.
@@ -187,7 +188,7 @@ MonoDl*
mono_dl_open_self (MonoError *error)
{
-#if defined(TARGET_ANDROID) && !defined(WIN32)
+#if defined(TARGET_ANDROID)
MonoDl *module;
module = (MonoDl *) g_malloc (sizeof (MonoDl));
if (!module) {
@@ -195,7 +196,7 @@ mono_dl_open_self (MonoError *error)
return NULL;
}
mono_refcount_init (module, NULL);
- module->handle = RTLD_DEFAULT;
+ module->handle = dlopen(NULL, RTLD_NOW);
module->dl_fallback = NULL;
module->full_name = NULL;
return module;
diff --git a/src/native/libs/System.Native/pal_dynamicload.c b/src/native/libs/System.Native/pal_dynamicload.c
index 6aaf70f50fcdd..76f9c56678af5 100644
--- a/src/native/libs/System.Native/pal_dynamicload.c
+++ b/src/native/libs/System.Native/pal_dynamicload.c
@@ -56,12 +56,6 @@ void SystemNative_FreeLibrary(void* handle)
dlclose(handle);
}
-#ifdef TARGET_ANDROID
-void* SystemNative_GetDefaultSearchOrderPseudoHandle(void)
-{
- return (void*)RTLD_DEFAULT;
-}
-#else
static void* volatile g_defaultSearchOrderPseudoHandle = NULL;
void* SystemNative_GetDefaultSearchOrderPseudoHandle(void)
{
@@ -69,11 +63,16 @@ void* SystemNative_GetDefaultSearchOrderPseudoHandle(void)
void* defaultSearchOrderPseudoHandle = (void*)g_defaultSearchOrderPseudoHandle;
if (defaultSearchOrderPseudoHandle == NULL)
{
+#ifdef TARGET_ANDROID
+ int flag = RTLD_NOW;
+#else
+ int flag = RTLD_LAZY;
+#endif
+
// Assign back to the static as well as the local here.
// We don't need to check for a race between two threads as the value returned by
// dlopen here will always be the same in a given environment.
- g_defaultSearchOrderPseudoHandle = defaultSearchOrderPseudoHandle = dlopen(NULL, RTLD_LAZY);
+ g_defaultSearchOrderPseudoHandle = defaultSearchOrderPseudoHandle = dlopen(NULL, flag);
}
return defaultSearchOrderPseudoHandle;
}
-#endif
diff --git a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs
new file mode 100644
index 0000000000000..f0f3ce75fff7f
--- /dev/null
+++ b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs
@@ -0,0 +1,46 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Reflection;
+using System.Runtime.InteropServices;
+
+using Xunit;
+
+public static class MainProgramHandleTests
+{
+ private static IntPtr s_handle;
+
+ static MainProgramHandleTests() => NativeLibrary.SetDllImportResolver(typeof(MainProgramHandleTests).Assembly,
+ (string libraryName, Assembly asm, DllImportSearchPath? dllImportSearchPath) =>
+ {
+ if (libraryName == "Self")
+ {
+ s_handle = NativeLibrary.GetMainProgramHandle();
+ Assert.NotEqual(IntPtr.Zero, s_handle);
+ return s_handle;
+ }
+
+ return IntPtr.Zero;
+ });
+
+ public static int Main()
+ {
+ try
+ {
+ free(s_handle);
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine($"Test Failure: {e}");
+ return 101;
+ }
+
+ return 100;
+ }
+
+ [DllImport("Self")]
+ private static extern int free(IntPtr arg);
+}
diff --git a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
new file mode 100644
index 0000000000000..1a444cccfd87a
--- /dev/null
+++ b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
@@ -0,0 +1,12 @@
+
+
+ Exe
+ true
+
+
+
+
+
+
+
+
From 25dad431f92bd8f466eead10004dca87912e6d16 Mon Sep 17 00:00:00 2001
From: Adeel <3840695+am11@users.noreply.github.com>
Date: Wed, 16 Nov 2022 22:30:42 +0200
Subject: [PATCH 2/5] Disable new test on windows and monointerpreter
---
src/mono/mono/utils/mono-dl.c | 2 +-
.../MainProgramHandle/MainProgramHandleTests.csproj | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/mono/mono/utils/mono-dl.c b/src/mono/mono/utils/mono-dl.c
index bce99682e0c6d..c4e5d5eafad24 100644
--- a/src/mono/mono/utils/mono-dl.c
+++ b/src/mono/mono/utils/mono-dl.c
@@ -188,7 +188,7 @@ MonoDl*
mono_dl_open_self (MonoError *error)
{
-#if defined(TARGET_ANDROID)
+#if defined(TARGET_ANDROID) && !defined(WIN32)
MonoDl *module;
module = (MonoDl *) g_malloc (sizeof (MonoDl));
if (!module) {
diff --git a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
index 1a444cccfd87a..1ce6225d7853c 100644
--- a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
+++ b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
@@ -2,6 +2,7 @@
Exe
true
+ true
From 1fe65e4945aefcdd5e5b66697e3a45d5035aa2b6 Mon Sep 17 00:00:00 2001
From: Adeel <3840695+am11@users.noreply.github.com>
Date: Thu, 17 Nov 2022 04:36:37 +0200
Subject: [PATCH 3/5] Add new test to monointerpreter ExcludeList
---
src/tests/issues.targets | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/tests/issues.targets b/src/tests/issues.targets
index 6eaf2dcf8fb3c..3b6ef72bff560 100644
--- a/src/tests/issues.targets
+++ b/src/tests/issues.targets
@@ -2690,6 +2690,9 @@
needs triage
+
+ needs triage
+
https://github.com/dotnet/runtime/issues/47624
From 1cf68635a7e38884d97b7dc2f915f956a539a202 Mon Sep 17 00:00:00 2001
From: Adeel <3840695+am11@users.noreply.github.com>
Date: Thu, 17 Nov 2022 06:21:10 +0200
Subject: [PATCH 4/5] Try test with getppid on all platforms
---
.../MainProgramHandle/MainProgramHandleTests.cs | 13 ++++++-------
.../MainProgramHandle/MainProgramHandleTests.csproj | 1 -
src/tests/issues.targets | 3 ---
3 files changed, 6 insertions(+), 11 deletions(-)
diff --git a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs
index f0f3ce75fff7f..fa7203169796a 100644
--- a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs
+++ b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs
@@ -11,16 +11,14 @@
public static class MainProgramHandleTests
{
- private static IntPtr s_handle;
-
static MainProgramHandleTests() => NativeLibrary.SetDllImportResolver(typeof(MainProgramHandleTests).Assembly,
(string libraryName, Assembly asm, DllImportSearchPath? dllImportSearchPath) =>
{
if (libraryName == "Self")
{
- s_handle = NativeLibrary.GetMainProgramHandle();
- Assert.NotEqual(IntPtr.Zero, s_handle);
- return s_handle;
+ IntPtr handle = NativeLibrary.GetMainProgramHandle();
+ Assert.NotEqual(IntPtr.Zero, handle);
+ return handle;
}
return IntPtr.Zero;
@@ -30,7 +28,8 @@ public static int Main()
{
try
{
- free(s_handle);
+ int parentPid = getppid();
+ Console.WriteLine("Parent PID is: {0}", parentPid);
}
catch (Exception e)
{
@@ -42,5 +41,5 @@ public static int Main()
}
[DllImport("Self")]
- private static extern int free(IntPtr arg);
+ private static extern int getppid();
}
diff --git a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
index 1ce6225d7853c..1a444cccfd87a 100644
--- a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
+++ b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
@@ -2,7 +2,6 @@
Exe
true
- true
diff --git a/src/tests/issues.targets b/src/tests/issues.targets
index 3b6ef72bff560..6eaf2dcf8fb3c 100644
--- a/src/tests/issues.targets
+++ b/src/tests/issues.targets
@@ -2690,9 +2690,6 @@
needs triage
-
- needs triage
-
https://github.com/dotnet/runtime/issues/47624
From 9a392d54cb15f189cea2343e55de5b1efa4adfb7 Mon Sep 17 00:00:00 2001
From: Adeel <3840695+am11@users.noreply.github.com>
Date: Thu, 17 Nov 2022 15:56:08 +0200
Subject: [PATCH 5/5] Revert "Try test with getppid on all platforms"
This reverts commit de8eced8b79cafde878b22caf89887fbd44bb05d.
---
.../MainProgramHandle/MainProgramHandleTests.cs | 13 +++++++------
.../MainProgramHandle/MainProgramHandleTests.csproj | 1 +
src/tests/issues.targets | 3 +++
3 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs
index fa7203169796a..c526c2a8d2a0a 100644
--- a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs
+++ b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.cs
@@ -11,14 +11,16 @@
public static class MainProgramHandleTests
{
+ private static IntPtr s_handle;
+
static MainProgramHandleTests() => NativeLibrary.SetDllImportResolver(typeof(MainProgramHandleTests).Assembly,
(string libraryName, Assembly asm, DllImportSearchPath? dllImportSearchPath) =>
{
if (libraryName == "Self")
{
- IntPtr handle = NativeLibrary.GetMainProgramHandle();
- Assert.NotEqual(IntPtr.Zero, handle);
- return handle;
+ s_handle = NativeLibrary.GetMainProgramHandle();
+ Assert.NotEqual(IntPtr.Zero, s_handle);
+ return s_handle;
}
return IntPtr.Zero;
@@ -28,8 +30,7 @@ public static int Main()
{
try
{
- int parentPid = getppid();
- Console.WriteLine("Parent PID is: {0}", parentPid);
+ free(s_handle);
}
catch (Exception e)
{
@@ -41,5 +42,5 @@ public static int Main()
}
[DllImport("Self")]
- private static extern int getppid();
+ private static extern void free(IntPtr arg);
}
diff --git a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
index 1a444cccfd87a..1ce6225d7853c 100644
--- a/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
+++ b/src/tests/Interop/NativeLibrary/MainProgramHandle/MainProgramHandleTests.csproj
@@ -2,6 +2,7 @@
Exe
true
+ true
diff --git a/src/tests/issues.targets b/src/tests/issues.targets
index 6eaf2dcf8fb3c..3b6ef72bff560 100644
--- a/src/tests/issues.targets
+++ b/src/tests/issues.targets
@@ -2690,6 +2690,9 @@
needs triage
+
+ needs triage
+
https://github.com/dotnet/runtime/issues/47624