This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add explicit layout checks to Crossgen2 #27054
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
src/tools/crossgen2/Common/TypeSystem/Common/ExplicitLayoutValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace Internal.TypeSystem | ||
{ | ||
public struct ExplicitLayoutValidator | ||
{ | ||
private enum FieldLayoutTag : byte | ||
{ | ||
Empty, | ||
NonORef, | ||
ORef, | ||
} | ||
|
||
private readonly int _pointerSize; | ||
|
||
private readonly FieldLayoutTag[] _fieldLayout; | ||
|
||
private readonly MetadataType _typeBeingValidated; | ||
|
||
private ExplicitLayoutValidator(MetadataType type, int typeSizeInBytes) | ||
{ | ||
_typeBeingValidated = type; | ||
_pointerSize = type.Context.Target.PointerSize; | ||
_fieldLayout = new FieldLayoutTag[typeSizeInBytes]; | ||
} | ||
|
||
public static void Validate(MetadataType type, ComputedInstanceFieldLayout layout) | ||
{ | ||
ExplicitLayoutValidator validator = new ExplicitLayoutValidator(type, layout.ByteCountUnaligned.AsInt); | ||
foreach (FieldAndOffset fieldAndOffset in layout.Offsets) | ||
{ | ||
validator.AddToFieldLayout(fieldAndOffset.Offset.AsInt, fieldAndOffset.Field.FieldType); | ||
} | ||
} | ||
|
||
private void AddToFieldLayout(int offset, TypeDesc fieldType) | ||
{ | ||
if (fieldType.IsGCPointer) | ||
{ | ||
if (offset % _pointerSize != 0) | ||
{ | ||
// Misaligned ORef | ||
ThrowFieldLayoutError(offset); | ||
} | ||
SetFieldLayout(offset, _pointerSize, FieldLayoutTag.ORef); | ||
} | ||
else if (fieldType.IsPointer || fieldType.IsFunctionPointer) | ||
{ | ||
SetFieldLayout(offset, _pointerSize, FieldLayoutTag.NonORef); | ||
} | ||
else if (fieldType.IsValueType) | ||
{ | ||
if (fieldType.IsByRefLike && offset % _pointerSize != 0) | ||
{ | ||
// Misaligned ByRefLike | ||
ThrowFieldLayoutError(offset); | ||
} | ||
|
||
MetadataType mdType = (MetadataType)fieldType; | ||
int fieldSize = mdType.InstanceByteCountUnaligned.AsInt; | ||
if (!mdType.ContainsGCPointers) | ||
{ | ||
// Plain value type, mark the entire range as NonORef | ||
SetFieldLayout(offset, fieldSize, FieldLayoutTag.NonORef); | ||
} | ||
else | ||
{ | ||
if (offset % _pointerSize != 0) | ||
{ | ||
// Misaligned struct with GC pointers | ||
ThrowFieldLayoutError(offset); | ||
} | ||
|
||
bool[] fieldORefMap = new bool[fieldSize]; | ||
MarkORefLocations(mdType, fieldORefMap, offset: 0); | ||
for (int index = 0; index < fieldSize; index++) | ||
{ | ||
SetFieldLayout(offset + index, fieldORefMap[index] ? FieldLayoutTag.ORef : FieldLayoutTag.NonORef); | ||
} | ||
} | ||
} | ||
else if (fieldType.IsByRef) | ||
{ | ||
if (offset % _pointerSize != 0) | ||
{ | ||
// Misaligned pointer field | ||
ThrowFieldLayoutError(offset); | ||
} | ||
SetFieldLayout(offset, _pointerSize, FieldLayoutTag.NonORef); | ||
} | ||
else | ||
{ | ||
Debug.Assert(false, fieldType.ToString()); | ||
} | ||
} | ||
|
||
private void MarkORefLocations(MetadataType type, bool[] orefMap, int offset) | ||
{ | ||
// Recurse into struct fields | ||
foreach (FieldDesc field in type.GetFields()) | ||
{ | ||
if (!field.IsStatic) | ||
{ | ||
int fieldOffset = offset + field.Offset.AsInt; | ||
if (field.FieldType.IsGCPointer) | ||
{ | ||
for (int index = 0; index < _pointerSize; index++) | ||
{ | ||
orefMap[fieldOffset + index] = true; | ||
} | ||
} | ||
else if (field.FieldType.IsValueType) | ||
{ | ||
MetadataType mdFieldType = (MetadataType)field.FieldType; | ||
if (mdFieldType.ContainsGCPointers) | ||
{ | ||
MarkORefLocations(mdFieldType, orefMap, fieldOffset); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void SetFieldLayout(int offset, int count, FieldLayoutTag tag) | ||
{ | ||
for (int index = 0; index < count; index++) | ||
{ | ||
SetFieldLayout(offset + index, tag); | ||
} | ||
} | ||
|
||
private void SetFieldLayout(int offset, FieldLayoutTag tag) | ||
{ | ||
FieldLayoutTag existingTag = _fieldLayout[offset]; | ||
if (existingTag != tag) | ||
{ | ||
if (existingTag != FieldLayoutTag.Empty) | ||
{ | ||
ThrowFieldLayoutError(offset); | ||
} | ||
_fieldLayout[offset] = tag; | ||
} | ||
} | ||
|
||
private void ThrowFieldLayoutError(int offset) | ||
{ | ||
ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadExplicitLayout, _typeBeingValidated, offset.ToStringInvariant()); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the field is a ByRef-like struct, it also needs to be aligned at pointer boundary. This should throw:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added alignment check for IsByRefLike within the IsValueType conditional block.