Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,10 @@ public static void ThrowArgumentOutOfRangeException()
{
throw new ArgumentOutOfRangeException();
}

public static void ThrowInvalidOperationInlineArrayEqualsGetHashCode()
{
throw new InvalidOperationException(SR.InvalidOperation_InlineArrayEqualsGetHashCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ public override MethodIL EmitIL()

ILEmitter emitter = new ILEmitter();

if (_owningType is MetadataType mdType)
{
// Types marked as InlineArray aren't supported by
// the built-in Equals() or GetHashCode().
if (mdType.IsInlineArray)
{
var stream = emitter.NewCodeStream();
MethodDesc invalidOp = Context.GetHelperEntryPoint("ThrowHelpers", "ThrowInvalidOperationInlineArrayEqualsGetHashCode");
stream.EmitCallThrowHelper(emitter, invalidOp);
return emitter.Link(this);
}
}

TypeDesc methodTableType = Context.SystemModule.GetKnownType("Internal.Runtime", "MethodTable");
MethodDesc methodTableOfMethod = methodTableType.GetKnownMethod("Of", null);

Expand Down
8 changes: 6 additions & 2 deletions src/coreclr/vm/comutilnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,8 @@ BOOL CanCompareBitsOrUseFastGetHashCode(MethodTable* mt)
}

if (mt->ContainsPointers()
|| mt->IsNotTightlyPacked())
|| mt->IsNotTightlyPacked()
|| mt->GetClass()->IsInlineArray())
{
mt->SetHasCheckedCanCompareBitsOrUseFastGetHashCode();
return FALSE;
Expand Down Expand Up @@ -1677,14 +1678,17 @@ BOOL CanCompareBitsOrUseFastGetHashCode(MethodTable* mt)
return canCompareBitsOrUseFastGetHashCode;
}

extern "C" BOOL QCALLTYPE MethodTable_CanCompareBitsOrUseFastGetHashCode(MethodTable * mt)
extern "C" BOOL QCALLTYPE MethodTable_CanCompareBitsOrUseFastGetHashCode(MethodTable* mt)
{
QCALL_CONTRACT;

BOOL ret = FALSE;

BEGIN_QCALL;

if (mt->GetClass()->IsInlineArray())
COMPlusThrow(kInvalidOperationException, W("InvalidOperation_InlineArrayEqualsGetHashCode"));

ret = CanCompareBitsOrUseFastGetHashCode(mt);

END_QCALL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2641,6 +2641,9 @@
<data name="InvalidOperation_IComparerFailed" xml:space="preserve">
<value>Failed to compare two elements in the array.</value>
</data>
<data name="InvalidOperation_InlineArrayEqualsGetHashCode" xml:space="preserve">
<value>Calling built-in Equals() or GetHashCode() on type marked as InlineArray is invalid.</value>
</data>
<data name="InvalidOperation_MethodBaked" xml:space="preserve">
<value>Type definition of the method is complete.</value>
</data>
Expand Down
10 changes: 10 additions & 0 deletions src/mono/mono/metadata/icall.c
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,11 @@ ves_icall_System_ValueType_InternalGetHashCode (MonoObjectHandle this_obj, MonoA

klass = mono_handle_class (this_obj);

if (m_class_is_inlinearray (klass)) {
mono_error_set_invalid_operation (error, "Calling built-in GetHashCode() on type marked as InlineArray is invalid.");
return FALSE;
}

if (mono_class_num_fields (klass) == 0)
return result;

Expand Down Expand Up @@ -1327,6 +1332,11 @@ ves_icall_System_ValueType_Equals (MonoObjectHandle this_obj, MonoObjectHandle t

klass = mono_handle_class (this_obj);

if (m_class_is_inlinearray (klass)) {
mono_error_set_invalid_operation (error, "Calling built-in Equals() on type marked as InlineArray is invalid.");
return FALSE;
}

if (m_class_is_enumtype (klass) && mono_class_enum_basetype_internal (klass) && mono_class_enum_basetype_internal (klass)->type == MONO_TYPE_I4)
return *(gint32*)mono_handle_get_data_unsafe (this_obj) == *(gint32*)mono_handle_get_data_unsafe (that);

Expand Down
26 changes: 26 additions & 0 deletions src/tests/Loader/classloader/InlineArray/InlineArrayValid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,30 @@ public static void MonoGCDescOpt()
Assert.Equal(i + 1, holder.arr[i].s);
}
}

struct StructHasInlineArrayField
{
FourtyTwoBytes _field;
}

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

Assert.Throws<InvalidOperationException>(() =>
{
new FourtyTwoBytes().Equals(new FourtyTwoBytes());
});

Assert.Throws<InvalidOperationException>(() =>
{
new StructHasInlineArrayField().Equals(new StructHasInlineArrayField());
});

Assert.Throws<InvalidOperationException>(() =>
{
new FourtyTwoBytes().GetHashCode();
});
}
}