Skip to content

Commit

Permalink
Don't consider types with empty base classes blittable. They have a d…
Browse files Browse the repository at this point in the history
…ifferent managed and native size. (#46653)

* Don't consider types with empty base classes blittable. They have a different managed and native size.

* Add test.

* Update src/coreclr/vm/classlayoutinfo.cpp
  • Loading branch information
jkoritzinsky authored Jan 7, 2021
1 parent 92870e5 commit 15be630
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/coreclr/vm/classlayoutinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,12 @@ VOID EEClassLayoutInfo::CollectLayoutFieldMetadataThrowing(
DEBUGARG(szName)
);

// Type is blittable only if parent is also blittable
isBlittable = isBlittable && (fHasNonTrivialParent ? pParentMT->IsBlittable() : TRUE);
// Type is blittable only if parent is also blittable and is not empty.
if (isBlittable && fHasNonTrivialParent)
{
isBlittable = pParentMT->IsBlittable() // Check parent
&& (!pParentLayoutInfo || !pParentLayoutInfo->IsZeroSized()); // Ensure non-zero size
}
pEEClassLayoutInfoOut->SetIsBlittable(isBlittable);

S_UINT32 cbSortArraySize = S_UINT32(cTotalFields) * S_UINT32(sizeof(LayoutRawFieldInfo*));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public void SizeOf_Struct_With_GenericValueTypeField_ReturnsExpected()
Assert.Equal(8, Marshal.SizeOf<TestStructWithVector64>());
}

[Fact]
public void SizeOf_TypeWithEmptyBase_ReturnsExpected()
{
Assert.Equal(4, Marshal.SizeOf<DerivedClass>());
}

public static IEnumerable<object[]> SizeOf_InvalidType_TestData()
{
yield return new object[] { typeof(int).MakeByRefType(), null };
Expand Down Expand Up @@ -136,5 +142,16 @@ public struct TestStructWithVector64
{
public System.Runtime.Intrinsics.Vector64<double> v;
}

[StructLayout(LayoutKind.Sequential)]
public class EmptyClass
{
}

[StructLayout(LayoutKind.Sequential)]
public class DerivedClass : EmptyClass
{
public int i;
}
}
}

0 comments on commit 15be630

Please sign in to comment.