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

Error out when struct size is bigger than int.MaxValue #104393

Merged
merged 20 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
14 changes: 14 additions & 0 deletions src/coreclr/tools/Common/TypeSystem/Common/LayoutInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public string ToStringInvariant()
return new LayoutInt(checked(left._value - right._value));
}

public static LayoutInt AddThrowing(LayoutInt left, LayoutInt right, TypeDesc loadedType)
{
if (left.IsIndeterminate || right.IsIndeterminate)
return Indeterminate;

int result = left._value + right._value;
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved

// Overflow if both arguments have the opposite sign of the result
if (((left._value ^ result) & (right._value ^ result)) < 0)
ThrowHelper.ThrowTypeLoadException(loadedType);

return new LayoutInt(result);
}

public override bool Equals(object obj)
{
if (obj is LayoutInt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ protected ComputedInstanceFieldLayout ComputeSequentialFieldLayout(MetadataType

cumulativeInstanceFieldPos = AlignUpInstanceFieldOffset(cumulativeInstanceFieldPos, fieldSizeAndAlignment.Alignment, type.Context.Target);
offsets[fieldOrdinal] = new FieldAndOffset(field, cumulativeInstanceFieldPos + offsetBias);
cumulativeInstanceFieldPos = checked(cumulativeInstanceFieldPos + fieldSizeAndAlignment.Size);
cumulativeInstanceFieldPos = LayoutInt.AddThrowing(cumulativeInstanceFieldPos, fieldSizeAndAlignment.Size, type);

fieldOrdinal++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;

using Internal.TypeSystem;
Expand Down Expand Up @@ -301,11 +302,11 @@ public static bool CanCompareValueTypeBits(MetadataType type, MethodDesc objectE

private struct OverlappingFieldTracker
{
private bool[] _usedBytes;
private BitArray _usedBytes;

public OverlappingFieldTracker(MetadataType type)
{
_usedBytes = new bool[type.InstanceFieldSize.AsInt];
_usedBytes = new BitArray(type.InstanceFieldSize.AsInt);
}

public bool TrackField(FieldDesc field)
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/vm/methodtablebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3805,6 +3805,8 @@ VOID MethodTableBuilder::InitializeFieldDescs(FieldDesc *pFieldDescList,

DWORD dwR8Fields = 0; // Number of R8's the class has

UINT32 accumulatedSize = 0;

#ifdef FEATURE_64BIT_ALIGNMENT
// Track whether any field in this type requires 8-byte alignment
BOOL fFieldRequiresAlign8 = HasParent() ? GetParentMethodTable()->RequiresAlign8() : FALSE;
Expand Down Expand Up @@ -4311,6 +4313,9 @@ VOID MethodTableBuilder::InitializeFieldDescs(FieldDesc *pFieldDescList,
pszFieldName
);

if (pLayoutFieldInfo)
accumulatedSize += pLayoutFieldInfo->m_placement.m_size;
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved

// We're using FieldDesc::m_pMTOfEnclosingClass to temporarily store the field's size.
//
if (fIsByValue)
Expand Down Expand Up @@ -4468,6 +4473,7 @@ VOID MethodTableBuilder::InitializeFieldDescs(FieldDesc *pFieldDescList,
}
}
// We processed all fields
IfFailThrow((accumulatedSize > FIELD_OFFSET_LAST_REAL_OFFSET) ? COR_E_TYPELOAD : S_OK);

//#SelfReferencingStaticValueTypeField_Checks
if (bmtFP->fHasSelfReferencingStaticValueTypeField_WithRVA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,29 @@ public static void ManagedSequentialDisqualifiedClassDerivedFromManagedSequentia
// Validate that the byte member is placed immediately after the object member.
Assert.Equal(sizeof(nint), (int)Unsafe.ByteOffset(ref Unsafe.As<AutoClassObjectBase, byte>(ref o.o), ref o.b));
}

struct X
{
byte x;
BigArray a;
}

struct Y
{
BigArray a;
byte y;
}

[StructLayout(LayoutKind.Sequential, Size = int.MaxValue)]
struct BigArray
{
}

[Fact]
public static void TestLargeStructSize()
{
Assert.Equal(int.MaxValue, sizeof(BigArray));
Assert.Throws<TypeLoadException>(() => sizeof(X));
Assert.Throws<TypeLoadException>(() => sizeof(Y));
}
}
Loading