Skip to content

Commit 7a98e3f

Browse files
committed
Merge in 'release/7.0' changes
2 parents 5a1974c + 7d81963 commit 7a98e3f

File tree

9 files changed

+143
-43
lines changed

9 files changed

+143
-43
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/mini/method-to-ir.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6554,8 +6554,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
65546554

65556555
if (cfg->llvm_only && cfg->interp && cfg->method == method && !cfg->deopt && !cfg->interp_entry_only) {
65566556
if (header->num_clauses) {
6557-
/* deopt is only disabled for gsharedvt */
6558-
g_assert (cfg->gsharedvt);
65596557
for (guint i = 0; i < header->num_clauses; ++i) {
65606558
MonoExceptionClause *clause = &header->clauses [i];
65616559
/* Finally clauses are checked after the remove_finally pass */

src/mono/mono/mini/mini.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,9 +3303,31 @@ mini_method_compile (MonoMethod *method, guint32 opts, JitFlags flags, int parts
33033303
}
33043304

33053305
if (cfg->llvm_only && cfg->interp && !cfg->interp_entry_only && header->num_clauses) {
3306-
cfg->deopt = TRUE;
3307-
/* Can't reconstruct inlined state */
3308-
cfg->disable_inline = TRUE;
3306+
gboolean can_deopt = TRUE;
3307+
/*
3308+
* Can't handle catch clauses inside finally clauses right now.
3309+
* When the ENDFINALLY opcode of the outer clause is encountered
3310+
* while executing the inner catch clause from run_with_il_state (),
3311+
* it will assert since it doesn't know where to continue execution.
3312+
*/
3313+
for (guint i = 0; i < cfg->header->num_clauses; ++i) {
3314+
for (guint j = 0; j < cfg->header->num_clauses; ++j) {
3315+
MonoExceptionClause *clause1 = &cfg->header->clauses [i];
3316+
MonoExceptionClause *clause2 = &cfg->header->clauses [j];
3317+
3318+
if (i != j && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
3319+
if (clause1->flags == MONO_EXCEPTION_CLAUSE_NONE && clause2->flags != MONO_EXCEPTION_CLAUSE_NONE) {
3320+
can_deopt = FALSE;
3321+
break;
3322+
}
3323+
}
3324+
}
3325+
}
3326+
if (can_deopt) {
3327+
cfg->deopt = TRUE;
3328+
/* Can't reconstruct inlined state */
3329+
cfg->disable_inline = TRUE;
3330+
}
33093331
}
33103332

33113333
#ifdef ENABLE_LLVM

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/mono/wasm/runtime/startup.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { preAllocatePThreadWorkerPool, instantiateWasmPThreadWorkerPool } from "
3131
let config: MonoConfigInternal = undefined as any;
3232
let configLoaded = false;
3333
let isCustomStartup = false;
34+
export const dotnetReady = createPromiseController<any>();
3435
export const afterConfigLoaded = createPromiseController<void>();
3536
export const afterInstantiateWasm = createPromiseController<void>();
3637
export const beforePreInit = createPromiseController<void>();
@@ -69,13 +70,17 @@ export function configure_emscripten_startup(module: DotnetModule, exportedAPI:
6970
// execution order == [5] ==
7071
module.postRun = [() => postRunAsync(userpostRun)];
7172
// execution order == [6] ==
72-
module.ready = module.ready.then(async () => {
73+
74+
module.ready.then(async () => {
7375
// wait for previous stage
7476
await afterPostRun.promise;
7577
// - here we resolve the promise returned by createDotnetRuntime export
76-
return exportedAPI;
7778
// - any code after createDotnetRuntime is executed now
79+
dotnetReady.promise_control.resolve(exportedAPI);
80+
}).catch(err => {
81+
dotnetReady.promise_control.reject(err);
7882
});
83+
module.ready = dotnetReady.promise;
7984
// execution order == [*] ==
8085
if (!module.onAbort) {
8186
module.onAbort = () => mono_on_abort;
@@ -220,6 +225,7 @@ async function postRunAsync(userpostRun: (() => void)[]) {
220225
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
221226
export function abort_startup(reason: any, should_exit: boolean): void {
222227
if (runtimeHelpers.diagnosticTracing) console.trace("MONO_WASM: abort_startup");
228+
dotnetReady.promise_control.reject(reason);
223229
afterInstantiateWasm.promise_control.reject(reason);
224230
beforePreInit.promise_control.reject(reason);
225231
afterPreInit.promise_control.reject(reason);

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)