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
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ private enum FieldLayoutTag : byte
Empty,
NonORef,
ORef,
ByRef,
}

private struct FieldLayoutInterval : IComparable<FieldLayoutInterval>
Expand Down Expand Up @@ -100,7 +101,7 @@ private void AddToFieldLayout(int offset, TypeDesc fieldType)

MetadataType mdType = (MetadataType)fieldType;
int fieldSize = mdType.InstanceByteCountUnaligned.AsInt;
if (!mdType.ContainsGCPointers)
if (!mdType.ContainsGCPointers && !mdType.IsByRefLike)
{
// Plain value type, mark the entire range as NonORef
SetFieldLayout(offset, fieldSize, FieldLayoutTag.NonORef);
Expand All @@ -109,27 +110,27 @@ private void AddToFieldLayout(int offset, TypeDesc fieldType)
{
if (offset % _pointerSize != 0)
{
// Misaligned struct with GC pointers
// Misaligned struct with GC pointers or ByRef
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
ThrowFieldLayoutError(offset);
}

List<FieldLayoutInterval> fieldORefMap = new List<FieldLayoutInterval>();
MarkORefLocations(mdType, fieldORefMap, offset: 0);
List<FieldLayoutInterval> fieldRefMap = new();
MarkNonORefLocations(mdType, fieldRefMap, offset: 0);

// Merge in fieldORefMap from structure specifying not attributed intervals as NonORef
// Merge in fieldRefMap from structure specifying not attributed intervals as NonORef
int lastGCRegionReportedEnd = 0;

foreach (var gcRegion in fieldORefMap)
foreach (var gcRegion in fieldRefMap)
{
SetFieldLayout(offset + lastGCRegionReportedEnd, gcRegion.Start - lastGCRegionReportedEnd, FieldLayoutTag.NonORef);
Debug.Assert(gcRegion.Tag == FieldLayoutTag.ORef);
Debug.Assert(gcRegion.Tag == FieldLayoutTag.ORef || gcRegion.Tag == FieldLayoutTag.ByRef);
SetFieldLayout(offset + gcRegion.Start, gcRegion.Size, gcRegion.Tag);
lastGCRegionReportedEnd = gcRegion.EndSentinel;
}

if (fieldORefMap.Count > 0)
if (fieldRefMap.Count > 0)
{
int trailingRegionStart = fieldORefMap[fieldORefMap.Count - 1].EndSentinel;
int trailingRegionStart = fieldRefMap[fieldRefMap.Count - 1].EndSentinel;
int trailingRegionSize = fieldSize - trailingRegionStart;
SetFieldLayout(offset + trailingRegionStart, trailingRegionSize, FieldLayoutTag.NonORef);
}
Expand All @@ -142,15 +143,15 @@ private void AddToFieldLayout(int offset, TypeDesc fieldType)
// Misaligned pointer field
ThrowFieldLayoutError(offset);
}
SetFieldLayout(offset, _pointerSize, FieldLayoutTag.NonORef);
SetFieldLayout(offset, _pointerSize, FieldLayoutTag.ByRef);
}
else
{
Debug.Assert(false, fieldType.ToString());
}
}

private void MarkORefLocations(MetadataType type, List<FieldLayoutInterval> orefMap, int offset)
private void MarkNonORefLocations(MetadataType type, List<FieldLayoutInterval> refMap, int offset)
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
{
// Recurse into struct fields
foreach (FieldDesc field in type.GetFields())
Expand All @@ -160,14 +161,18 @@ private void MarkORefLocations(MetadataType type, List<FieldLayoutInterval> oref
int fieldOffset = offset + field.Offset.AsInt;
if (field.FieldType.IsGCPointer)
{
SetFieldLayout(orefMap, offset, _pointerSize, FieldLayoutTag.ORef);
SetFieldLayout(refMap, offset, _pointerSize, FieldLayoutTag.ORef);
}
else if (field.FieldType.IsByRef || field.FieldType.IsByReferenceOfT)
{
SetFieldLayout(refMap, offset, _pointerSize, FieldLayoutTag.ByRef);
}
else if (field.FieldType.IsValueType)
{
MetadataType mdFieldType = (MetadataType)field.FieldType;
if (mdFieldType.ContainsGCPointers)
if (mdFieldType.ContainsGCPointers || mdFieldType.IsByRefLike)
{
MarkORefLocations(mdFieldType, orefMap, fieldOffset);
MarkNonORefLocations(mdFieldType, refMap, fieldOffset);
}
}
}
Expand Down