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

Bump Explicit-layout value types with no fields to at minimum 1 byte size. #63975

Merged
merged 4 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/coreclr/vm/methodtablebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8648,6 +8648,14 @@ MethodTableBuilder::HandleExplicitLayout(
BuildMethodTableThrowException(IDS_CLASSLOAD_GENERAL);
}
}

if (!numInstanceFieldBytes.IsOverflow() && numInstanceFieldBytes.Value() == 0)
{
// If we calculate a 0-byte size here, we should have also calculated a 0-byte size
// in the initial layout algorithm.
_ASSERTE(GetLayoutInfo()->IsZeroSized());
numInstanceFieldBytes = S_UINT32(1);
}
}

// The GC requires that all valuetypes containing orefs be sized to a multiple of TARGET_POINTER_SIZE.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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;

[StructLayout(LayoutKind.Explicit)]
public struct S
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my education - does the same logic hold for zero-sized classes with explicit layout i.o.w. should we be also testing correct behavior in the presence of inheritance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe zero-sized classes are already bumped up to at least 1-byte elsewhere, but to make sure I've added some more test cases to make sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to specifically validate inheritance as it will end up down the same code path as the non-inheritance case.

{
}

[StructLayout(LayoutKind.Explicit)]
public struct S2
{
}

public class Test_explicitStruct_empty
{
// Mark as no-inlining so any test failures will show the right stack trace even after
// we consolidate test assemblies.
[Fact]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void EmptyExplicitStructCanBeLoadedAndCreated()
{
S s = new S();
}

[Fact]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void EmptyExplicitStructCanBeLoadedAndCreatedThroughReflection()
{
object s = Activator.CreateInstance(Type.GetType("S2"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<Compile Include="explicitStruct_empty.cs" />
</ItemGroup>
</Project>