Skip to content

Commit e5f0c36

Browse files
authored
Add generic parameter checks for fields of generic types (#99911)
1 parent 3209346 commit e5f0c36

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/mono/mono/metadata/class-init.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,6 +2573,10 @@ mono_class_layout_fields (MonoClass *klass, int base_instance_size, int packing_
25732573
case MONO_TYPE_VALUETYPE:
25742574
case MONO_TYPE_GENERICINST:
25752575
field_class = mono_class_from_mono_type_internal (field->type);
2576+
if (mono_class_is_ginst (field_class) && !mono_verifier_class_is_valid_generic_instantiation (field_class)) {
2577+
mono_class_set_type_load_failure (klass, "Field '%s' is an invalid generic instantiation of type %s", field->name, mono_type_get_full_name (field_class));
2578+
return;
2579+
}
25762580
break;
25772581
default:
25782582
break;

src/tests/Loader/classloader/generics/ByRefLike/ValidateNegative.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static void AllowByRefLike_Substituted_For_NonByRefLike_Invalid()
1818
Console.WriteLine($"{nameof(AllowByRefLike_Substituted_For_NonByRefLike_Invalid)}...");
1919

2020
Assert.Throws<TypeLoadException>(() => { Exec.TypeSubstitutionInterfaceImplementationAllowByRefLikeIntoNonByRefLike(); });
21-
Assert.Throws<TypeLoadException>(() => { Exec.TypeSubstitutionFieldAllowByRefLikeIntoNonByRefLike(); });
2221
Assert.Throws<TypeLoadException>(() => { Exec.OverrideMethodNotByRefLike(); });
2322
}
2423

@@ -28,5 +27,6 @@ public static void AllowByRefLike_Substituted_For_NonByRefLike_Invalid_Class()
2827
Console.WriteLine($"{nameof(AllowByRefLike_Substituted_For_NonByRefLike_Invalid_Class)}...");
2928

3029
Assert.Throws<TypeLoadException>(() => { Exec.TypeSubstitutionInheritanceAllowByRefLikeIntoNonByRefLike(); });
30+
Assert.Throws<TypeLoadException>(() => { Exec.TypeSubstitutionFieldAllowByRefLikeIntoNonByRefLike(); });
3131
}
3232
}

0 commit comments

Comments
 (0)