Skip to content

Class types with layout that inherit from System.Object should be ManagedSequential #65447

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 3 commits into from
Feb 18, 2022
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
2 changes: 1 addition & 1 deletion src/coreclr/vm/classlayoutinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ VOID EEClassLayoutInfo::CollectLayoutFieldMetadataThrowing(
// Check if this type might be ManagedSequential. Only valuetypes marked Sequential can be
// ManagedSequential. Other issues checked below might also disqualify the type.
if ( (!fExplicitOffsets) && // Is it marked sequential?
(pParentMT && (pParentMT->IsValueTypeClass() || pParentMT->IsManagedSequential())) // Is it a valuetype or derived from a qualifying valuetype?
(pParentMT && (pParentMT->IsObjectClass() || pParentMT->IsValueTypeClass() || pParentMT->IsManagedSequential())) // Is it a valuetype or derived from a qualifying valuetype?
)
{
fDisqualifyFromManagedSequential = FALSE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit;

[SkipOnMono("This test suite tests CoreCLR and Crossgen2/NativeAOT-specific layout rules.")]
unsafe class ManagedSequential
{
[StructLayout(LayoutKind.Sequential)]
class LayoutClassObjectBase
{
public byte b1;
public long l1;
}
class AutoClassLayoutBase : LayoutClassObjectBase
{
public byte b2;
public long l2;
}

class AutoClassObjectBase
{
public byte b1;
public long l1;
}

[StructLayout(LayoutKind.Sequential)]
class LayoutClassLayoutBase : LayoutClassObjectBase
{
public byte b2;
public long l2;
}

[Fact]
public static void LayoutClassObjectBaseIsManagedSequential()
{
var o = new LayoutClassObjectBase();
// Validate that the long member is placed after the byte member, as is done with sequential layout in this case.
Assert.Equal(8, (int)Unsafe.ByteOffset(ref o.b1, ref Unsafe.As<long, byte>(ref o.l1)));
}

[Fact]
public static void LayoutClassLayoutBaseIsManagedSequential()
{
var o = new LayoutClassLayoutBase();
// Validate that the long member is placed after the byte member, as is done with sequential layout in this case.
Assert.Equal(8, (int)Unsafe.ByteOffset(ref o.b2, ref Unsafe.As<long, byte>(ref o.l2)));
}

[Fact]
public static void AutoClassLayoutBaseIsManagedSequential()
{
var o = new AutoClassLayoutBase();
// Validate that the long member is placed before the byte member, as is done with auto layout in this case.
Assert.Equal(-8, (int)Unsafe.ByteOffset(ref o.b2, ref Unsafe.As<long, byte>(ref o.l2)));
}

[Fact]
public static void AutoClassObjectBaseIsManagedSequential()
{
var o = new AutoClassObjectBase();
// Validate that the long member is placed before the byte member, as is done with auto layout in this case.
Assert.Equal(-8, (int)Unsafe.ByteOffset(ref o.b1, ref Unsafe.As<long, byte>(ref o.l1)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>