Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Port IsBlittable fix and related changes from single-exe (#27746)
Browse files Browse the repository at this point in the history
* Port set of changes from single-exe branch

* Enable precompilation of marshalling IL stubs in crossgen2 #26767
* P/invoke pregeneration fixes #27389
* Fixes to array marshalling pregeneration #27425
* Fix IsBlittableType #27436

* Fix stack overflow issue and m_alignpad==0 assert

I've found that the changes from single-exe had issues - it was causing
stack overflow during compilation of a couple of runtime assemblies.
After I've fixed that, there was about 15 tests failing at runtime failing
with assert failure: m_alignpad == 0. This is a well known indication of a
problem where JIT and crossgen get different field offsets, resulting in
writing beyond the end of an object.
The problem was caused by incorrect usage of sequential layout for class
without LayoutSequential attribute.
I've added a commit with fix for those to this PR.
  • Loading branch information
janvorli authored Nov 7, 2019
1 parent 3ef9c7b commit 0e97c73
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 172 deletions.
14 changes: 14 additions & 0 deletions src/tools/crossgen2/Common/TypeSystem/Ecma/EcmaField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@ public override bool HasCustomAttribute(string attributeNamespace, string attrib
return !MetadataReader.GetCustomAttributeHandle(MetadataReader.GetFieldDefinition(_handle).GetCustomAttributes(),
attributeNamespace, attributeName).IsNil;
}

public override MarshalAsDescriptor GetMarshalAsDescriptor()
{
MetadataReader reader = MetadataReader;
FieldDefinition definition = reader.GetFieldDefinition(_handle);
if ((definition.Attributes & FieldAttributes.HasFieldMarshal) != 0)
{
BlobReader marshalAsReader = reader.GetBlobReader(definition.GetMarshallingDescriptor());
EcmaSignatureParser parser = new EcmaSignatureParser(_type.EcmaModule, marshalAsReader);
return parser.ParseMarshalAsDescriptor();
}

return null;
}
}

public static class EcmaFieldExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public MarshalAsDescriptor ParseMarshalAsDescriptor()
Debug.Assert(_reader.RemainingBytes != 0);

NativeTypeKind type = (NativeTypeKind)_reader.ReadByte();
NativeTypeKind arraySubType = NativeTypeKind.Invalid;
NativeTypeKind arraySubType = NativeTypeKind.Default;
uint? paramNum = null, numElem = null;

switch (type)
Expand Down
34 changes: 0 additions & 34 deletions src/tools/crossgen2/Common/TypeSystem/Ecma/EcmaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,40 +539,6 @@ public override ClassLayoutMetadata GetClassLayout()
return result;
}

public override MarshalAsDescriptor[] GetFieldMarshalAsDescriptors()
{
var fieldDefinitionHandles = _typeDefinition.GetFields();

MarshalAsDescriptor[] marshalAsDescriptors = new MarshalAsDescriptor[fieldDefinitionHandles.Count];
int index = 0;
foreach (var handle in fieldDefinitionHandles)
{
var fieldDefinition = MetadataReader.GetFieldDefinition(handle);

if ((fieldDefinition.Attributes & FieldAttributes.Static) != 0)
continue;

MarshalAsDescriptor marshalAsDescriptor = GetMarshalAsDescriptor(fieldDefinition);
marshalAsDescriptors[index++] = marshalAsDescriptor;
}

return marshalAsDescriptors;
}

private MarshalAsDescriptor GetMarshalAsDescriptor(FieldDefinition fieldDefinition)
{
if ((fieldDefinition.Attributes & FieldAttributes.HasFieldMarshal) == FieldAttributes.HasFieldMarshal)
{
MetadataReader metadataReader = MetadataReader;
BlobReader marshalAsReader = metadataReader.GetBlobReader(fieldDefinition.GetMarshallingDescriptor());
EcmaSignatureParser parser = new EcmaSignatureParser(EcmaModule, marshalAsReader);
MarshalAsDescriptor marshalAs = parser.ParseMarshalAsDescriptor();
Debug.Assert(marshalAs != null);
return marshalAs;
}
return null;
}

public override bool IsExplicitLayout
{
get
Expand Down
25 changes: 25 additions & 0 deletions src/tools/crossgen2/Common/TypeSystem/Interop/FieldDesc.Interop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.

namespace Internal.TypeSystem
{
partial class FieldDesc
{
/// <summary>
/// Returns description of how the field should be marshalled to native code.
/// </summary>
public virtual MarshalAsDescriptor GetMarshalAsDescriptor()
{
return null;
}
}

partial class FieldForInstantiatedType
{
public override MarshalAsDescriptor GetMarshalAsDescriptor()
{
return _fieldDef.GetMarshalAsDescriptor();
}
}
}
Loading

0 comments on commit 0e97c73

Please sign in to comment.