Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to help uncover CrossGen2 scenario validation #65477

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/mono/mono/metadata/class-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1843,12 +1843,35 @@ class_has_ref_fields (MonoClass *klass)
return klass->has_ref_fields;
}

static gboolean
class_is_byreference (MonoClass* klass)
{
const char* klass_name_space = m_class_get_name_space (klass);
const char* klass_name = m_class_get_name (klass);
MonoImage* klass_image = m_class_get_image (klass);
gboolean in_corlib = klass_image == mono_defaults.corlib;

if (in_corlib &&
!strcmp (klass_name_space, "System") &&
!strcmp (klass_name, "ByReference`1")) {
return TRUE;
}

return FALSE;
}

static gboolean
type_has_ref_fields (MonoType *ftype)
{
if (m_type_is_byref (ftype) || (MONO_TYPE_ISSTRUCT (ftype) && class_has_ref_fields (mono_class_from_mono_type_internal (ftype))))
return TRUE;

/* Check for the ByReference`1 type */
if (MONO_TYPE_ISSTRUCT (ftype)) {
MonoClass* klass = mono_class_from_mono_type_internal (ftype);
return class_is_byreference (klass);
}

return FALSE;
}

Expand Down Expand Up @@ -1942,7 +1965,7 @@ validate_struct_fields_overlaps (guint8 *layout_check, int layout_size, MonoClas
if (mono_type_is_struct (ftype)) {
// recursively check the layout of the embedded struct
MonoClass *embedded_class = mono_class_from_mono_type_internal (ftype);
mono_class_setup_fields (embedded_class);
mono_class_setup_fields (embedded_class);

const int embedded_fields_count = mono_class_get_field_count (embedded_class);
int *embedded_offsets = g_new0 (int, embedded_fields_count);
Expand All @@ -1962,7 +1985,7 @@ validate_struct_fields_overlaps (guint8 *layout_check, int layout_size, MonoClas
} else {
int align = 0;
int size = mono_type_size (field->type, &align);
guint8 type = type_has_references (klass, ftype) ? 1 : m_type_is_byref (ftype) ? 2 : 3;
guint8 type = type_has_references (klass, ftype) ? 1 : (m_type_is_byref (ftype) || class_is_byreference (klass)) ? 2 : 3;

// Mark the bytes used by this fields type based on if it contains references or not.
// Make sure there are no overlaps between object and non-object fields.
Expand Down
1 change: 1 addition & 0 deletions src/tests/Loader/classloader/RefFields/InvalidCSharp.il
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
01 00 00 00
)
.field public int32& Field
.field public int32 Size
}

.class public explicit ansi sealed beforefieldinit InvalidCSharp.IntPtrOverlapWithInnerFieldType
Expand Down
35 changes: 33 additions & 2 deletions src/tests/Loader/classloader/RefFields/Validate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,52 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using InvalidCSharp;

using Xunit;

class Validate
{
[StructLayout(LayoutKind.Explicit)]
private ref struct Explicit
{
[FieldOffset(0)] public Span<byte> Bytes;
[FieldOffset(0)] public Guid Guid;
}

[Fact]
public static void Validate_Invalid_RefField_Fails()
{
Console.WriteLine($"{nameof(Validate_Invalid_RefField_Fails)}...");
Assert.Throws<TypeLoadException>(() => { var t = typeof(InvalidStructWithRefField); });
Assert.Throws<TypeLoadException>(() => { var t = typeof(InvalidRefFieldAlignment); });
Assert.Throws<TypeLoadException>(() => { var t = typeof(InvalidObjectRefRefFieldOverlap); });
Assert.Throws<TypeLoadException>(() => { var t = typeof(IntPtrRefFieldOverlap); });
Assert.Throws<TypeLoadException>(() => { var t = typeof(IntPtrOverlapWithInnerFieldType); });
Assert.Throws<TypeLoadException>(() =>
{
var t = new IntPtrRefFieldOverlap()
{
Field = IntPtr.Zero
};
return t.Field.ToString();
});
Assert.Throws<TypeLoadException>(() =>
{
var t = new IntPtrOverlapWithInnerFieldType()
{
Field = IntPtr.Zero
};
ref var i = ref t.Invalid;
return i.Size;
});
Assert.Throws<TypeLoadException>(() =>
{
var t = new Explicit()
{
Guid = Guid.NewGuid()
};
return t.Bytes.Length;
});
}

[Fact]
Expand Down