Skip to content

Commit

Permalink
Add generic parameter checks for fields of generic types (#99911)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyang-mono committed Mar 19, 2024
1 parent 3209346 commit e5f0c36
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/mono/mono/metadata/class-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -2573,6 +2573,10 @@ mono_class_layout_fields (MonoClass *klass, int base_instance_size, int packing_
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_GENERICINST:
field_class = mono_class_from_mono_type_internal (field->type);
if (mono_class_is_ginst (field_class) && !mono_verifier_class_is_valid_generic_instantiation (field_class)) {
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));
return;
}
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static void AllowByRefLike_Substituted_For_NonByRefLike_Invalid()
Console.WriteLine($"{nameof(AllowByRefLike_Substituted_For_NonByRefLike_Invalid)}...");

Assert.Throws<TypeLoadException>(() => { Exec.TypeSubstitutionInterfaceImplementationAllowByRefLikeIntoNonByRefLike(); });
Assert.Throws<TypeLoadException>(() => { Exec.TypeSubstitutionFieldAllowByRefLikeIntoNonByRefLike(); });
Assert.Throws<TypeLoadException>(() => { Exec.OverrideMethodNotByRefLike(); });
}

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

Assert.Throws<TypeLoadException>(() => { Exec.TypeSubstitutionInheritanceAllowByRefLikeIntoNonByRefLike(); });
Assert.Throws<TypeLoadException>(() => { Exec.TypeSubstitutionFieldAllowByRefLikeIntoNonByRefLike(); });
}
}

0 comments on commit e5f0c36

Please sign in to comment.