From 54e137e582ea427e87b7f468d9bfadb56683d961 Mon Sep 17 00:00:00 2001 From: fanyang-mono Date: Thu, 18 Apr 2024 16:35:02 -0400 Subject: [PATCH 1/4] Enable tests for methods without type parameters --- src/mono/mono/metadata/marshal-lightweight.c | 3 +- .../UnsafeAccessorsTests.Generics.cs | 32 +++++++++++-------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/mono/mono/metadata/marshal-lightweight.c b/src/mono/mono/metadata/marshal-lightweight.c index 86f64eeb40bba..a4579c4188126 100644 --- a/src/mono/mono/metadata/marshal-lightweight.c +++ b/src/mono/mono/metadata/marshal-lightweight.c @@ -2322,7 +2322,6 @@ method_sig_from_accessor_sig (MonoMethodBuilder *mb, gboolean hasthis, MonoMetho ret->hasthis = hasthis; for (int i = 1; i < ret->param_count; i++) ret->params [i - 1] = ret->params [i]; - memset (&ret->params[ret->param_count - 1], 0, sizeof (MonoType)); // just in case ret->param_count--; return ret; } @@ -2467,7 +2466,7 @@ emit_unsafe_accessor_method_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor static void emit_unsafe_accessor_wrapper_ilgen (MonoMethodBuilder *mb, MonoMethod *accessor_method, MonoMethodSignature *sig, MonoGenericContext *ctx, MonoUnsafeAccessorKind kind, const char *member_name) { - if (accessor_method->is_inflated || accessor_method->is_generic || mono_class_is_ginst (accessor_method->klass) || ctx != NULL) { + if (accessor_method->is_generic || ctx != NULL) { mono_mb_emit_exception_full (mb, "System", "BadImageFormatException", "UnsafeAccessor_Generics"); return; } diff --git a/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs b/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs index e1029797bf12c..45c06b173737a 100644 --- a/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs +++ b/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs @@ -63,10 +63,9 @@ private void Add(Struct a) => } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/89439", TestRuntimes.Mono)] - public static void Verify_Generic_AccessStaticFieldClass() + public static void Verify_Generic_AccessStaticFieldClass_NonGenericMethod() { - Console.WriteLine($"Running {nameof(Verify_Generic_AccessStaticFieldClass)}"); + Console.WriteLine($"Running {nameof(Verify_Generic_AccessStaticFieldClass_NonGenericMethod)}"); Assert.Equal(typeof(int).ToString(), GetPrivateStaticFieldInt((MyList)null)); @@ -74,6 +73,22 @@ public static void Verify_Generic_AccessStaticFieldClass() Assert.Equal(typeof(Struct).ToString(), GetPrivateStaticFieldStruct((MyList)null)); + [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] + extern static ref string GetPrivateStaticFieldInt(MyList d); + + [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] + extern static ref string GetPrivateStaticFieldString(MyList d); + + [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] + extern static ref string GetPrivateStaticFieldStruct(MyList d); + } + + [Fact] + [ActiveIssue("https://github.com/dotnet/runtime/issues/89439", TestRuntimes.Mono)] + public static void Verify_Generic_AccessStaticFieldClass_GenericMethod() + { + Console.WriteLine($"Running {nameof(Verify_Generic_AccessStaticFieldClass_GenericMethod)}"); + { int expected = 10; MyList.SetStaticGenericField(expected); @@ -85,15 +100,6 @@ public static void Verify_Generic_AccessStaticFieldClass() Assert.Equal(expected, GetPrivateStaticField((MyList)null)); } - [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] - extern static ref string GetPrivateStaticFieldInt(MyList d); - - [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] - extern static ref string GetPrivateStaticFieldString(MyList d); - - [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] - extern static ref string GetPrivateStaticFieldStruct(MyList d); - [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticGenericFieldName)] extern static ref V GetPrivateStaticField(MyList d); } @@ -284,7 +290,6 @@ public static void Verify_Generic_CallCtor() } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/89439", TestRuntimes.Mono)] public static void Verify_Generic_GenericTypeNonGenericInstanceMethod() { Console.WriteLine($"Running {nameof(Verify_Generic_GenericTypeNonGenericInstanceMethod)}"); @@ -358,7 +363,6 @@ public static void Verify_Generic_GenericTypeGenericInstanceMethod() } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/89439", TestRuntimes.Mono)] public static void Verify_Generic_GenericTypeNonGenericStaticMethod() { Console.WriteLine($"Running {nameof(Verify_Generic_GenericTypeNonGenericStaticMethod)}"); From 51e1bd6a3986c1952946ff16c52824966e719154 Mon Sep 17 00:00:00 2001 From: fanyang-mono Date: Fri, 19 Apr 2024 11:31:58 -0400 Subject: [PATCH 2/4] Enable more field tests --- .../UnsafeAccessorsTests.Generics.cs | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs b/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs index 45c06b173737a..04f4b21c5ea5b 100644 --- a/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs +++ b/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs @@ -63,9 +63,9 @@ private void Add(Struct a) => } [Fact] - public static void Verify_Generic_AccessStaticFieldClass_NonGenericMethod() + public static void Verify_Generic_AccessStaticFieldClass() { - Console.WriteLine($"Running {nameof(Verify_Generic_AccessStaticFieldClass_NonGenericMethod)}"); + Console.WriteLine($"Running {nameof(Verify_Generic_AccessStaticFieldClass)}"); Assert.Equal(typeof(int).ToString(), GetPrivateStaticFieldInt((MyList)null)); @@ -73,22 +73,6 @@ public static void Verify_Generic_AccessStaticFieldClass_NonGenericMethod() Assert.Equal(typeof(Struct).ToString(), GetPrivateStaticFieldStruct((MyList)null)); - [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] - extern static ref string GetPrivateStaticFieldInt(MyList d); - - [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] - extern static ref string GetPrivateStaticFieldString(MyList d); - - [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] - extern static ref string GetPrivateStaticFieldStruct(MyList d); - } - - [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/89439", TestRuntimes.Mono)] - public static void Verify_Generic_AccessStaticFieldClass_GenericMethod() - { - Console.WriteLine($"Running {nameof(Verify_Generic_AccessStaticFieldClass_GenericMethod)}"); - { int expected = 10; MyList.SetStaticGenericField(expected); @@ -100,6 +84,15 @@ public static void Verify_Generic_AccessStaticFieldClass_GenericMethod() Assert.Equal(expected, GetPrivateStaticField((MyList)null)); } + [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] + extern static ref string GetPrivateStaticFieldInt(MyList d); + + [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] + extern static ref string GetPrivateStaticFieldString(MyList d); + + [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticFieldName)] + extern static ref string GetPrivateStaticFieldStruct(MyList d); + [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name=MyList.StaticGenericFieldName)] extern static ref V GetPrivateStaticField(MyList d); } From c0552644f1f544575ca85aad2fc90b09f1713375 Mon Sep 17 00:00:00 2001 From: fanyang-mono Date: Fri, 19 Apr 2024 11:37:21 -0400 Subject: [PATCH 3/4] Enable all field tests --- .../UnsafeAccessors/UnsafeAccessorsTests.Generics.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs b/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs index 04f4b21c5ea5b..e4fb9593dd3ed 100644 --- a/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs +++ b/src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs @@ -98,7 +98,6 @@ public static void Verify_Generic_AccessStaticFieldClass() } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/89439", TestRuntimes.Mono)] public static void Verify_Generic_AccessFieldClass() { Console.WriteLine($"Running {nameof(Verify_Generic_AccessFieldClass)}"); From ca84ec811bd7319315a420337efc7e6e79649617 Mon Sep 17 00:00:00 2001 From: fanyang-mono Date: Fri, 19 Apr 2024 14:30:34 -0400 Subject: [PATCH 4/4] Fix the type to get size for --- src/mono/mono/metadata/marshal-lightweight.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mono/mono/metadata/marshal-lightweight.c b/src/mono/mono/metadata/marshal-lightweight.c index a4579c4188126..6399b315cfd20 100644 --- a/src/mono/mono/metadata/marshal-lightweight.c +++ b/src/mono/mono/metadata/marshal-lightweight.c @@ -2322,6 +2322,7 @@ method_sig_from_accessor_sig (MonoMethodBuilder *mb, gboolean hasthis, MonoMetho ret->hasthis = hasthis; for (int i = 1; i < ret->param_count; i++) ret->params [i - 1] = ret->params [i]; + memset (&ret->params[ret->param_count - 1], 0, sizeof (MonoType*)); // just in case ret->param_count--; return ret; }