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

Disallow readonly instance fields in InlineArray structs #90262

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/coreclr/dlls/mscorrc/mscorrc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ BEGIN
IDS_CLASSLOAD_INLINE_ARRAY_FIELD_COUNT "InlineArrayAttribute requires that the target type has a single instance field. Type: '%1'. Assembly: '%2'."
IDS_CLASSLOAD_INLINE_ARRAY_LENGTH "InlineArrayAttribute requires that the length argument is greater than 0. Type: '%1'. Assembly: '%2'."
IDS_CLASSLOAD_INLINE_ARRAY_EXPLICIT "InlineArrayAttribute cannot be applied to a type with explicit layout. Type: '%1'. Assembly: '%2'."
IDS_CLASSLOAD_INLINE_ARRAY_READONLY "InlineArrayAttribute cannot be applied to a type with readonly instance fields. Type: '%1'. Assembly: '%2'."

IDS_INVALID_RECURSIVE_GENERIC_FIELD_LOAD "Could not load type '%1' from assembly '%2' because of an invalid self-referential generic field."

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/dlls/mscorrc/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
#define IDS_CLASSLOAD_INLINE_ARRAY_FIELD_COUNT 0x17ac
#define IDS_CLASSLOAD_INLINE_ARRAY_LENGTH 0x17ad
#define IDS_CLASSLOAD_INLINE_ARRAY_EXPLICIT 0x17ae
#define IDS_CLASSLOAD_INLINE_ARRAY_READONLY 0x17af

#define IDS_DEBUG_USERBREAKPOINT 0x17b6

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ private static string GetFormatString(ExceptionStringID id)
return SR.ClassLoad_RankTooLarge;
case ExceptionStringID.ClassLoadInlineArrayFieldCount:
return SR.ClassLoad_InlineArrayFieldCount;
case ExceptionStringID.ClassLoadInlineArrayReadOnly:
return SR.ClassLoad_InlineArrayReadOnly;
case ExceptionStringID.ClassLoadInlineArrayLength:
return SR.ClassLoad_InlineArrayLength;
case ExceptionStringID.ClassLoadInlineArrayExplicit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum ExceptionStringID
ClassLoadInlineArrayFieldCount,
ClassLoadInlineArrayLength,
ClassLoadInlineArrayExplicit,
ClassLoadInlineArrayReadOnly,

// MissingMethodException
MissingMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq;
using Debug = System.Diagnostics.Debug;

namespace Internal.TypeSystem
Expand Down Expand Up @@ -493,6 +494,14 @@ private static void AdjustForInlineArray(
ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadInlineArrayFieldCount, type);
}

foreach (var field in type.GetFields())
{
if (field.IsInitOnly && !field.IsStatic)
{
ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadInlineArrayReadOnly, type);
}
}

if (!instanceByteSizeAndAlignment.Size.IsIndeterminate)
{
long size = instanceByteSizeAndAlignment.Size.AsInt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
<data name="ClassLoadInlineArrayExplicit" xml:space="preserve">
<value>InlineArrayAttribute cannot be applied to a type with explicit layout. Type: '{0}'. Assembly: '{1}'.'</value>
</data>
<data name="ClassLoadInlineArrayReadOnly" xml:space="preserve">
<value>InlineArrayAttribute cannot be applied to a type with readonly instance fields. Type: '{0}'. Assembly: '{1}'.</value>
</data>
<data name="ClassLoadValueClassTooLarge" xml:space="preserve">
<value>Array of type '{0}' from assembly '{1}' cannot be created because base value type is too large</value>
</data>
Expand Down Expand Up @@ -195,4 +198,4 @@
<data name="AmbiguousMatchUnsafeAccessor" xml:space="preserve">
<value>Ambiguity in binding of UnsafeAccessorAttribute.</value>
</data>
</root>
</root>
6 changes: 6 additions & 0 deletions src/coreclr/vm/methodtablebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,12 @@ MethodTableBuilder::BuildMethodTableThrowing(
BuildMethodTableThrowException(IDS_CLASSLOAD_INLINE_ARRAY_FIELD_COUNT);
}

DWORD dwFieldAttrs = bmtMetaData->pFieldAttrs[0];
if (IsFdInitOnly(dwFieldAttrs))
{
BuildMethodTableThrowException(IDS_CLASSLOAD_INLINE_ARRAY_READONLY);
}

if (cbVal >= (sizeof(INT32) + 2))
{
INT32 repeat = GET_UNALIGNED_VAL32((byte*)pVal + 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4130,6 +4130,9 @@
<data name="ClassLoad_InlineArrayExplicit" xml:space="preserve">
<value>InlineArrayAttribute cannot be applied to a type with explicit layout. Type: '{0}'. Assembly: '{1}'.</value>
</data>
<data name="ClassLoad_InlineArrayReadOnly" xml:space="preserve">
<value>InlineArrayAttribute cannot be applied to a type with readonly instance fields. Type: '{0}'. Assembly: '{1}'.</value>
</data>
<data name="ClassLoad_ExplicitGeneric" xml:space="preserve">
<value>Could not load type '{0}' from assembly '{1}' because generic types cannot have explicit layout.</value>
</data>
Expand Down