Skip to content

Commit 46648f3

Browse files
authored
Fix size calculation for derived explicit layout types with no fields (#118245)
1 parent e58b345 commit 46648f3

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

src/coreclr/vm/classlayoutinfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ namespace
213213
LayoutRawFieldInfo* pfwalk = pFieldInfoArray;
214214
mdFieldDef fd;
215215
ULONG ulOffset;
216-
UINT32 calcTotalSize = 0;
216+
UINT32 calcTotalSize = parentSize;
217217
while (SUCCEEDED(hr = pInternalImport->GetClassLayoutNext(
218218
&classlayout,
219219
&fd,

src/tests/JIT/Methodical/structs/ExplicitLayout.cs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,67 @@ internal unsafe struct TestStruct
2626
}
2727
#pragma warning restore 618
2828

29+
[StructLayout(LayoutKind.Explicit)]
30+
public class ExplicitBase
31+
{
32+
[FieldOffset(8)] public object? m_objectField;
33+
[FieldOffset(0)] public double m_doubleField;
34+
35+
public double DoubleValue
36+
{
37+
get => m_doubleField;
38+
set => m_doubleField = value;
39+
}
40+
}
41+
42+
[StructLayout(LayoutKind.Explicit)]
43+
public class EmptyExplicitClassDerivingFromExplicitClass : ExplicitBase
44+
{
45+
}
46+
47+
public class AutoDerivingFromEmptyExplicitClass : EmptyExplicitClassDerivingFromExplicitClass
48+
{
49+
string MyStringField;
50+
51+
public AutoDerivingFromEmptyExplicitClass(string fieldValue = "Default Value")
52+
{
53+
MyStringField = fieldValue;
54+
}
55+
56+
public string GetMyStringField()
57+
{
58+
return MyStringField;
59+
}
60+
}
61+
2962
public class Program
3063
{
3164
[Fact]
3265
[OuterLoop]
33-
public static int TestEntrypoint()
66+
public static void ExplicitLayoutStruct()
3467
{
35-
int returnVal = 100;
36-
3768
TestStruct t = new TestStruct();
3869
t.Guid1 = Guid.NewGuid();
3970
t.Guid2 = t.Guid1;
4071

41-
if (t.Guid1 != t.Guid2)
42-
{
43-
Console.WriteLine("FAIL self-copy");
44-
returnVal = -1;
45-
}
72+
Assert.Equal(t.Guid1, t.Guid2);
4673

4774
TestStruct t2 = new TestStruct();
4875
Guid newGuid = Guid.NewGuid();
4976
t2.Guid1 = newGuid;
5077
t2.Guid2 = newGuid;
5178

52-
if (t2.Guid1 != t2.Guid2)
53-
{
54-
Console.WriteLine("FAIL other-copy");
55-
returnVal = -1;
56-
}
79+
Assert.Equal(t2.Guid1, t2.Guid2);
80+
}
81+
82+
[Fact]
83+
public static void EmptyExplicitClass()
84+
{
85+
AutoDerivingFromEmptyExplicitClass emptyDirectBase = new("AutoDerivingFromEmptyExplicitClass");
86+
87+
emptyDirectBase.DoubleValue = 17.0;
5788

58-
return returnVal;
89+
Assert.Equal("AutoDerivingFromEmptyExplicitClass", emptyDirectBase.GetMyStringField());
5990
}
6091
}
6192
}

0 commit comments

Comments
 (0)