Skip to content

Commit 474fa50

Browse files
committed
check in Auto as well, including generic structs
1 parent 61ca2c0 commit 474fa50

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

src/coreclr/tools/Common/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,6 @@ protected ComputedInstanceFieldLayout ComputeSequentialFieldLayout(MetadataType
473473

474474
if (type.IsInlineArray)
475475
{
476-
// inline array cannot have explicit instance size
477-
if (layoutMetadata.Size != 0)
478-
{
479-
ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadInlineArrayExplicitSize, type);
480-
}
481-
482476
AdjustForInlineArray(type, numInstanceFields, ref instanceByteSizeAndAlignment, ref instanceSizeAndAlignment);
483477
}
484478

@@ -516,6 +510,12 @@ private static void AdjustForInlineArray(
516510
ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadInlineArrayFieldCount, type);
517511
}
518512

513+
var layoutMetadata = type.GetClassLayout();
514+
if (layoutMetadata.Size != 0)
515+
{
516+
ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadInlineArrayExplicitSize, type);
517+
}
518+
519519
if (!instanceByteSizeAndAlignment.Size.IsIndeterminate)
520520
{
521521
long size = instanceByteSizeAndAlignment.Size.AsInt;

src/coreclr/vm/methodtablebuilder.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,12 @@ MethodTableBuilder::BuildMethodTableThrowing(
17711771
{
17721772
BuildMethodTableThrowException(IDS_CLASSLOAD_INLINE_ARRAY_EXPLICIT);
17731773
}
1774+
1775+
ULONG classSize;
1776+
if (SUCCEEDED(GetMDImport()->GetClassTotalSize(cl, &classSize)) && classSize != 0)
1777+
{
1778+
BuildMethodTableThrowException(IDS_CLASSLOAD_INLINE_ARRAY_EXPLICIT_SIZE);
1779+
}
17741780
}
17751781
}
17761782
}
@@ -1793,13 +1799,6 @@ MethodTableBuilder::BuildMethodTableThrowing(
17931799
{
17941800
if ((int)bmtFP->NumInstanceFieldBytes != (INT64)bmtFP->NumInstanceFieldBytes)
17951801
BuildMethodTableThrowException(IDS_CLASSLOAD_FIELDTOOLARGE);
1796-
1797-
if (HasExplicitSize() &&
1798-
bmtLayout->layoutType == EEClassLayoutInfo::LayoutType::Sequential &&
1799-
GetHalfBakedClass()->IsInlineArray())
1800-
{
1801-
BuildMethodTableThrowException(IDS_CLASSLOAD_INLINE_ARRAY_EXPLICIT_SIZE);
1802-
}
18031802
}
18041803

18051804
if (CheckIfSIMDAndUpdateSize())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ mono_class_setup_fields (MonoClass *klass)
317317
if (explicit_size)
318318
instance_size += real_size;
319319

320-
if (explicit_size && layout == TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT && m_class_is_inlinearray (klass)) {
320+
if (explicit_size && real_size != 0 && m_class_is_inlinearray (klass)) {
321321
if (mono_get_runtime_callbacks ()->mono_class_set_deferred_type_load_failure_callback)
322322
mono_get_runtime_callbacks ()->mono_class_set_deferred_type_load_failure_callback (klass, "Inline array must not have explicit size.");
323323
else

src/tests/Loader/classloader/InlineArray/InlineArrayInvalid.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,41 @@ public static void Explicit_Fails()
2323
}
2424

2525
[Fact]
26-
public static void ExplicitSize_Fails()
26+
public static void ExplicitSize_FailsInSequential()
2727
{
28-
Console.WriteLine($"{nameof(ExplicitSize_Fails)}...");
29-
30-
// .size is ignored for auto layout
31-
Assert.True(sizeof(ExplicitSizeAuto) > 0);
28+
Console.WriteLine($"{nameof(ExplicitSize_FailsInSequential)}...");
3229

3330
Assert.Throws<TypeLoadException>(() => { var t = typeof(ExplicitSize); });
3431

3532
Assert.Throws<TypeLoadException>(() =>
3633
{
37-
return sizeof(ExplicitSize);
34+
return new ExplicitSize();
35+
});
36+
}
37+
38+
[Fact]
39+
public static void ExplicitSize_FailsInAuto()
40+
{
41+
Console.WriteLine($"{nameof(ExplicitSize_FailsInAuto)}...");
42+
43+
Assert.Throws<TypeLoadException>(() => { var t = typeof(ExplicitSizeAuto); });
44+
45+
Assert.Throws<TypeLoadException>(() =>
46+
{
47+
return sizeof(ExplicitSizeAuto);
48+
});
49+
}
50+
51+
[Fact]
52+
public static void ExplicitSize_FailsInGeneric()
53+
{
54+
Console.WriteLine($"{nameof(ExplicitSize_FailsInGeneric)}...");
55+
56+
Assert.Throws<TypeLoadException>(() => { var t = typeof(ExplicitSizeGeneric<long>); });
57+
58+
Assert.Throws<TypeLoadException>(() =>
59+
{
60+
return new ExplicitSizeGeneric<int>();
3861
});
3962
}
4063

src/tests/Loader/classloader/InlineArray/InvalidCSharpInlineArray.il

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@
3636
.field [0] public valuetype [System.Runtime]System.Guid Guid
3737
}
3838

39+
.class public auto ansi sealed beforefieldinit ExplicitSizeGeneric`1<T>
40+
extends [System.Runtime]System.ValueType
41+
{
42+
.custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
43+
01 00 01 00 00 00 00 00
44+
)
45+
46+
.size 256
47+
.field [0] public valuetype [System.Runtime]System.Guid Guid
48+
}
49+
3950
.class public sequential ansi sealed beforefieldinit ZeroLength
4051
extends [System.Runtime]System.ValueType
4152
{

0 commit comments

Comments
 (0)