Skip to content

Use adjusted parent layout size to handle a non-zero-sized class inheriting from a zero-sized class that inherits from another zero-sized class. #50861

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

Merged
merged 4 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions src/coreclr/vm/classlayoutinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ VOID EEClassLayoutInfo::CollectLayoutFieldMetadataThrowing(
pEEClassLayoutInfoOut->m_cbPackingSize = packingSize;

BOOL fParentHasLayout = pParentMT && pParentMT->HasLayout();
UINT32 cbAdjustedParentLayoutNativeSize = 0;
UINT32 cbAdjustedParentLayoutSize = 0;
EEClassLayoutInfo *pParentLayoutInfo = NULL;
if (fParentHasLayout)
{
Expand All @@ -622,11 +622,11 @@ VOID EEClassLayoutInfo::CollectLayoutFieldMetadataThrowing(
// got bumped up to a size of 1 for compatibility reasons, then
// we need to remove the padding, but ONLY for inheritance situations.
if (pParentLayoutInfo->IsZeroSized()) {
cbAdjustedParentLayoutNativeSize = 0;
cbAdjustedParentLayoutSize = 0;
}
else
{
cbAdjustedParentLayoutNativeSize = pParentMT->GetNumInstanceFieldBytes();
cbAdjustedParentLayoutSize = pParentMT->GetNumInstanceFieldBytes();
}
}

Expand Down Expand Up @@ -668,7 +668,7 @@ VOID EEClassLayoutInfo::CollectLayoutFieldMetadataThrowing(
}
CQuickArray<LayoutRawFieldInfo*> pSortArray;
pSortArray.ReSizeThrows(cbSortArraySize.Value());
SetOffsetsAndSortFields(pInternalImport, cl, pInfoArrayOut, cInstanceFields, fExplicitOffsets, cbAdjustedParentLayoutNativeSize, pModule, pSortArray.Ptr());
SetOffsetsAndSortFields(pInternalImport, cl, pInfoArrayOut, cInstanceFields, fExplicitOffsets, cbAdjustedParentLayoutSize, pModule, pSortArray.Ptr());

ULONG classSizeInMetadata = 0;
if (FAILED(pInternalImport->GetClassTotalSize(cl, &classSizeInMetadata)))
Expand All @@ -690,14 +690,13 @@ VOID EEClassLayoutInfo::CollectLayoutFieldMetadataThrowing(
}

BYTE parentManagedAlignmentRequirement = 0;
UINT32 parentSize = pParentMT->GetNumInstanceFieldBytes();
if (pParentMT && (pParentMT->IsManagedSequential() || (pParentMT->GetClass()->HasExplicitFieldOffsetLayout() && pParentMT->IsBlittable())))
{
parentManagedAlignmentRequirement = pParentLayoutInfo->m_ManagedLargestAlignmentRequirementOfAllMembers;
}

CalculateSizeAndFieldOffsets(
parentSize,
cbAdjustedParentLayoutSize,
cInstanceFields,
fExplicitOffsets,
pSortArray.Ptr(),
Expand Down
20 changes: 18 additions & 2 deletions src/tests/Interop/LayoutClass/LayoutClassTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class EmptyBase
{
}

[StructLayout(LayoutKind.Sequential)]
public class EmptyBase2 : EmptyBase
{
}

[StructLayout(LayoutKind.Sequential)]
public class SeqDerivedClass : EmptyBase
{
Expand All @@ -23,6 +28,17 @@ public SeqDerivedClass(int _a)
}
}

[StructLayout(LayoutKind.Sequential)]
public class SeqDerivedClass2 : EmptyBase2
{
public int a;

public SeqDerivedClass2(int _a)
{
a = _a;
}
}

[StructLayout(LayoutKind.Sequential)]
public sealed class SeqClass
{
Expand Down Expand Up @@ -185,8 +201,8 @@ public static void DerivedClassWithEmptyBase()
Console.WriteLine($"Running {nameof(DerivedClassWithEmptyBase)}...");

string s = "before";
var p = new SeqDerivedClass(42);
Assert.IsTrue(DerivedSeqLayoutClassByRef(p, 42));
Assert.IsTrue(DerivedSeqLayoutClassByRef(new SeqDerivedClass(42), 42));
Assert.IsTrue(DerivedSeqLayoutClassByRef(new SeqDerivedClass2(42), 42));
}

public static void ExplicitClass()
Expand Down