Skip to content

Commit 7d81963

Browse files
[release/7.0] Allow interop resolvers to return self handle (#78779)
* Allow interop resolvers to return self handle * Disable new test on windows and monointerpreter * Add new test to monointerpreter ExcludeList * Try test with getppid on all platforms * Revert "Try test with getppid on all platforms" This reverts commit de8eced. Co-authored-by: Adeel <[email protected]>
1 parent c912890 commit 7d81963

File tree

6 files changed

+110
-36
lines changed

6 files changed

+110
-36
lines changed

src/mono/mono/metadata/native-library.c

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,40 @@ netcore_probe_for_module_nofail (MonoImage *image, const char *file_name, int fl
584584
return result;
585585
}
586586

587+
static MonoDl*
588+
netcore_lookup_self_native_handle (void)
589+
{
590+
ERROR_DECL (load_error);
591+
if (!internal_module)
592+
internal_module = mono_dl_open_self (load_error);
593+
594+
if (!internal_module)
595+
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "DllImport error loading library '__Internal': '%s'.", mono_error_get_message_without_fields (load_error));
596+
597+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via __Internal.");
598+
mono_error_cleanup (load_error);
599+
600+
return internal_module;
601+
}
602+
603+
static MonoDl* native_handle_lookup_wrapper (gpointer handle)
604+
{
605+
MonoDl *result = NULL;
606+
607+
if (!internal_module)
608+
netcore_lookup_self_native_handle ();
609+
610+
if (internal_module->handle == handle) {
611+
result = internal_module;
612+
} else {
613+
native_library_lock ();
614+
result = netcore_handle_lookup (handle);
615+
native_library_unlock ();
616+
}
617+
618+
return result;
619+
}
620+
587621
static MonoDl *
588622
netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, const char *scope, guint32 flags, MonoError *error)
589623
{
@@ -631,9 +665,7 @@ netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAsse
631665
mono_runtime_invoke_checked (resolve, NULL, args, error);
632666
goto_if_nok (error, leave);
633667

634-
native_library_lock ();
635-
result = netcore_handle_lookup (lib);
636-
native_library_unlock ();
668+
result = native_handle_lookup_wrapper (lib);
637669

638670
leave:
639671
HANDLE_FUNCTION_RETURN_VAL (result);
@@ -688,9 +720,7 @@ netcore_resolve_with_load (MonoAssemblyLoadContext *alc, const char *scope, Mono
688720
mono_runtime_invoke_checked (resolve, NULL, args, error);
689721
goto_if_nok (error, leave);
690722

691-
native_library_lock ();
692-
result = netcore_handle_lookup (lib);
693-
native_library_unlock ();
723+
result = native_handle_lookup_wrapper (lib);
694724

695725
leave:
696726
HANDLE_FUNCTION_RETURN_VAL (result);
@@ -755,9 +785,7 @@ netcore_resolve_with_resolving_event (MonoAssemblyLoadContext *alc, MonoAssembly
755785
mono_runtime_invoke_checked (resolve, NULL, args, error);
756786
goto_if_nok (error, leave);
757787

758-
native_library_lock ();
759-
result = netcore_handle_lookup (lib);
760-
native_library_unlock ();
788+
result = native_handle_lookup_wrapper (lib);
761789

762790
leave:
763791
HANDLE_FUNCTION_RETURN_VAL (result);
@@ -802,22 +830,6 @@ netcore_check_alc_cache (MonoAssemblyLoadContext *alc, const char *scope)
802830
return result;
803831
}
804832

805-
static MonoDl*
806-
netcore_lookup_self_native_handle (void)
807-
{
808-
ERROR_DECL (load_error);
809-
if (!internal_module)
810-
internal_module = mono_dl_open_self (load_error);
811-
812-
if (!internal_module)
813-
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "DllImport error loading library '__Internal': '%s'.", mono_error_get_message_without_fields (load_error));
814-
815-
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via __Internal.");
816-
mono_error_cleanup (load_error);
817-
818-
return internal_module;
819-
}
820-
821833
static MonoDl *
822834
netcore_lookup_native_library (MonoAssemblyLoadContext *alc, MonoImage *image, const char *scope, guint32 flags)
823835
{

src/mono/mono/utils/mono-dl.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ fix_libc_name (const char *name)
176176
* mono_dl_open_self:
177177
* \param error pointer to MonoError
178178
*
179-
* Returns a handle to the main program, on android x86 it's not possible to
180-
* call dl_open(null), it returns a null handle, so this function returns RTLD_DEFAULT
179+
* Returns a handle to the main program, on Android it's not possible to
180+
* call dl_open(null) with RTLD_LAZY, it returns a null handle, so this
181+
* function uses RTLD_NOW.
181182
* handle in this platform.
182183
* \p error points to MonoError where an error will be stored in
183184
* case of failure. The error needs to be cleared when done using it, \c mono_error_cleanup.
@@ -195,7 +196,7 @@ mono_dl_open_self (MonoError *error)
195196
return NULL;
196197
}
197198
mono_refcount_init (module, NULL);
198-
module->handle = RTLD_DEFAULT;
199+
module->handle = dlopen(NULL, RTLD_NOW);
199200
module->dl_fallback = NULL;
200201
module->full_name = NULL;
201202
return module;

src/native/libs/System.Native/pal_dynamicload.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,23 @@ void SystemNative_FreeLibrary(void* handle)
5656
dlclose(handle);
5757
}
5858

59-
#ifdef TARGET_ANDROID
60-
void* SystemNative_GetDefaultSearchOrderPseudoHandle(void)
61-
{
62-
return (void*)RTLD_DEFAULT;
63-
}
64-
#else
6559
static void* volatile g_defaultSearchOrderPseudoHandle = NULL;
6660
void* SystemNative_GetDefaultSearchOrderPseudoHandle(void)
6761
{
6862
// Read the value once from the volatile static to avoid reading from memory twice.
6963
void* defaultSearchOrderPseudoHandle = (void*)g_defaultSearchOrderPseudoHandle;
7064
if (defaultSearchOrderPseudoHandle == NULL)
7165
{
66+
#ifdef TARGET_ANDROID
67+
int flag = RTLD_NOW;
68+
#else
69+
int flag = RTLD_LAZY;
70+
#endif
71+
7272
// Assign back to the static as well as the local here.
7373
// We don't need to check for a race between two threads as the value returned by
7474
// dlopen here will always be the same in a given environment.
75-
g_defaultSearchOrderPseudoHandle = defaultSearchOrderPseudoHandle = dlopen(NULL, RTLD_LAZY);
75+
g_defaultSearchOrderPseudoHandle = defaultSearchOrderPseudoHandle = dlopen(NULL, flag);
7676
}
7777
return defaultSearchOrderPseudoHandle;
7878
}
79-
#endif
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Reflection;
8+
using System.Runtime.InteropServices;
9+
10+
using Xunit;
11+
12+
public static class MainProgramHandleTests
13+
{
14+
private static IntPtr s_handle;
15+
16+
static MainProgramHandleTests() => NativeLibrary.SetDllImportResolver(typeof(MainProgramHandleTests).Assembly,
17+
(string libraryName, Assembly asm, DllImportSearchPath? dllImportSearchPath) =>
18+
{
19+
if (libraryName == "Self")
20+
{
21+
s_handle = NativeLibrary.GetMainProgramHandle();
22+
Assert.NotEqual(IntPtr.Zero, s_handle);
23+
return s_handle;
24+
}
25+
26+
return IntPtr.Zero;
27+
});
28+
29+
public static int Main()
30+
{
31+
try
32+
{
33+
free(s_handle);
34+
}
35+
catch (Exception e)
36+
{
37+
Console.WriteLine($"Test Failure: {e}");
38+
return 101;
39+
}
40+
41+
return 100;
42+
}
43+
44+
[DllImport("Self")]
45+
private static extern void free(IntPtr arg);
46+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5+
<CLRTestTargetUnsupported Condition="'$(TargetsWindows)' == 'true' or '$(RuntimeVariant)' == 'monointerpreter'">true</CLRTestTargetUnsupported>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="MainProgramHandleTests.cs" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
12+
</ItemGroup>
13+
</Project>

src/tests/issues.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,6 +2690,9 @@
26902690
<ExcludeList Include = "$(XunitTestBinBase)/Interop/StructMarshalling/ReversePInvoke/MarshalSeqStruct/DelegatePInvoke/DelegatePInvokeTest/**">
26912691
<Issue>needs triage</Issue>
26922692
</ExcludeList>
2693+
<ExcludeList Include = "$(XunitTestBinBase)/Interop/NativeLibrary/MainProgramHandle/**">
2694+
<Issue>needs triage</Issue>
2695+
</ExcludeList>
26932696
<ExcludeList Include = "$(XunitTestBinBase)/JIT/Methodical/eh/basics/throwinfilter_il_d/**">
26942697
<Issue>https://github.com/dotnet/runtime/issues/47624</Issue>
26952698
</ExcludeList>

0 commit comments

Comments
 (0)