Skip to content
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
1 change: 1 addition & 0 deletions src/coreclr/dlls/mscorrc/mscorrc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,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_EXPLICIT_SIZE "InlineArrayAttribute cannot be applied to a type with explicit size. Type: '%1'. Assembly: '%2'."

IDS_CLASSLOAD_BYREF_OF_BYREF "Could not create a ByRef of a ByRef. Type: '%1'. Assembly: '%2'."
IDS_CLASSLOAD_POINTER_OF_BYREF "Could not create a pointer to a ByRef. Type: '%1'. Assembly: '%2'."
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/dlls/mscorrc/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
#define IDS_CLASSLOAD_BYREF_OF_BYREF 0x17af
#define IDS_CLASSLOAD_POINTER_OF_BYREF 0x17b0

#define IDS_CLASSLOAD_INLINE_ARRAY_EXPLICIT_SIZE 0x17b1

#define IDS_INVALID_REDIM 0x17c3
#define IDS_INVALID_PINVOKE_CALLCONV 0x17c4
#define IDS_CLASSLOAD_NSTRUCT_EXPLICIT_OFFSET 0x17c7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ private static string GetFormatString(ExceptionStringID id)
return SR.ClassLoad_InlineArrayLength;
case ExceptionStringID.ClassLoadInlineArrayExplicit:
return SR.ClassLoad_InlineArrayExplicit;
case ExceptionStringID.ClassLoadInlineArrayExplicitSize:
return SR.ClassLoad_InlineArrayExplicitSize;
case ExceptionStringID.InvalidProgramDefault:
return SR.InvalidProgram_Default;
case ExceptionStringID.InvalidProgramSpecific:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum ExceptionStringID
ClassLoadInlineArrayFieldCount,
ClassLoadInlineArrayLength,
ClassLoadInlineArrayExplicit,
ClassLoadInlineArrayExplicitSize,

// MissingMethodException
MissingMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,12 @@ private static void AdjustForInlineArray(
ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadInlineArrayFieldCount, type);
}

var layoutMetadata = type.GetClassLayout();
if (layoutMetadata.Size != 0)
{
ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadInlineArrayExplicitSize, 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="ClassLoadInlineArrayExplicitSize" xml:space="preserve">
<value>InlineArrayAttribute cannot be applied to a type with explicit size. 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
6 changes: 6 additions & 0 deletions src/coreclr/vm/methodtablebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,12 @@ MethodTableBuilder::BuildMethodTableThrowing(
{
BuildMethodTableThrowException(IDS_CLASSLOAD_INLINE_ARRAY_EXPLICIT);
}

ULONG classSize;
if (SUCCEEDED(GetMDImport()->GetClassTotalSize(cl, &classSize)) && classSize != 0)
{
BuildMethodTableThrowException(IDS_CLASSLOAD_INLINE_ARRAY_EXPLICIT_SIZE);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4202,6 +4202,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_InlineArrayExplicitSize" xml:space="preserve">
<value>InlineArrayAttribute cannot be applied to a type with explicit size. 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
7 changes: 7 additions & 0 deletions src/mono/mono/metadata/class-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ mono_class_setup_fields (MonoClass *klass)
if (explicit_size)
instance_size += real_size;

if (explicit_size && real_size != 0 && m_class_is_inlinearray (klass)) {
if (mono_get_runtime_callbacks ()->mono_class_set_deferred_type_load_failure_callback)
mono_get_runtime_callbacks ()->mono_class_set_deferred_type_load_failure_callback (klass, "Inline array must not have explicit size.");
else
mono_class_set_type_load_failure (klass, "Inline array must not have explicit size.");
}

/*
* This function can recursively call itself.
* Prevent infinite recursion by using a list in TLS.
Expand Down
39 changes: 39 additions & 0 deletions src/tests/Loader/classloader/InlineArray/InlineArrayInvalid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,45 @@ public static void Explicit_Fails()
});
}

[Fact]
public static void ExplicitSize_FailsInSequential()
{
Console.WriteLine($"{nameof(ExplicitSize_FailsInSequential)}...");

Assert.Throws<TypeLoadException>(() => { var t = typeof(ExplicitSize); });

Assert.Throws<TypeLoadException>(() =>
{
return new ExplicitSize();
});
}

[Fact]
public static void ExplicitSize_FailsInAuto()
{
Console.WriteLine($"{nameof(ExplicitSize_FailsInAuto)}...");

Assert.Throws<TypeLoadException>(() => { var t = typeof(ExplicitSizeAuto); });

Assert.Throws<TypeLoadException>(() =>
{
return sizeof(ExplicitSizeAuto);
});
}

[Fact]
public static void ExplicitSize_FailsInGeneric()
{
Console.WriteLine($"{nameof(ExplicitSize_FailsInGeneric)}...");

Assert.Throws<TypeLoadException>(() => { var t = typeof(ExplicitSizeGeneric<long>); });

Assert.Throws<TypeLoadException>(() =>
{
return new ExplicitSizeGeneric<int>();
});
}

[Fact]
public static void ZeroLength_Fails()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- Make sure that invalid operations are not optimized out by Roslyn -->
<Optimize>false</Optimize>
<CLRTestPriority>0</CLRTestPriority>
</PropertyGroup>
<ItemGroup>
<Compile Include="InlineArrayInvalid.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,39 @@
.field [0] public valuetype [System.Runtime]System.Guid Guid
}

.class public sequential ansi sealed beforefieldinit ExplicitSize
extends [System.Runtime]System.ValueType
{
.custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
01 00 01 00 00 00 00 00
)

.size 256
.field [0] public valuetype [System.Runtime]System.Guid Guid
}

.class public auto ansi sealed beforefieldinit ExplicitSizeAuto
extends [System.Runtime]System.ValueType
{
.custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
01 00 01 00 00 00 00 00
)

.size 256
.field [0] public valuetype [System.Runtime]System.Guid Guid
}

.class public auto ansi sealed beforefieldinit ExplicitSizeGeneric`1<T>
extends [System.Runtime]System.ValueType
{
.custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
01 00 01 00 00 00 00 00
)

.size 256
.field [0] public valuetype [System.Runtime]System.Guid Guid
}

.class public sequential ansi sealed beforefieldinit ZeroLength
extends [System.Runtime]System.ValueType
{
Expand Down
Loading