Skip to content

Commit d3bf287

Browse files
authored
[mono] Disallow casts of bounded arrays to array special interfaces (#93616)
* [mono] Disallow casts of bounded arrays to array special interfaces Fixes #93597 * re-enable disabled tests * exclude lower bound regression test on NativeAOT
1 parent c519fd1 commit d3bf287

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

src/libraries/System.Runtime/tests/System/ArrayTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,6 @@ public static IEnumerable<object[]> Copy_Array_UnreliableConversion_CanPerform_T
12991299
}
13001300

13011301
[Theory]
1302-
[ActiveIssue("https://github.com/dotnet/runtime/issues/93597", TestRuntimes.Mono)]
13031302
[MemberData(nameof(Copy_SZArray_Reliable_TestData))]
13041303
[MemberData(nameof(Copy_SZArray_PrimitiveWidening_TestData))]
13051304
[MemberData(nameof(Copy_SZArray_UnreliableConversion_CanPerform_TestData))]
@@ -3233,7 +3232,6 @@ public static IEnumerable<object[]> Reverse_TestData()
32333232
}
32343233

32353234
[Theory]
3236-
[ActiveIssue("https://github.com/dotnet/runtime/issues/93597", TestRuntimes.Mono)]
32373235
[MemberData(nameof(Reverse_TestData))]
32383236
public static void Reverse_SZArray(Array array, int index, int length, Array expected)
32393237
{

src/mono/mono/metadata/class.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4100,7 +4100,7 @@ mono_class_is_assignable_from_general (MonoClass *klass, MonoClass *oklass, gboo
41004100
return;
41014101
}
41024102

4103-
if (m_class_is_array_special_interface (klass) && m_class_get_rank (oklass) == 1) {
4103+
if (m_class_is_array_special_interface (klass) && m_class_get_rank (oklass) == 1 && m_class_get_byval_arg (oklass)->type == MONO_TYPE_SZARRAY) {
41044104
if (mono_class_is_gtd (klass)) {
41054105
/* klass is an array special gtd like
41064106
* IList`1<>, and oklass is X[] for some X.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Runtime.CompilerServices;
6+
7+
public class ReproGH93597 {
8+
public static int Main() {
9+
var expected = new int[] {5,4,3,2,1};
10+
11+
const int LowerBound = 5;
12+
13+
var expectedNzlba = NonZeroLowerBoundArray(expected, LowerBound);
14+
15+
return Helper(expectedNzlba);
16+
return 100;
17+
}
18+
[MethodImpl(MethodImplOptions.NoInlining)]
19+
private static int Helper(Array a) {
20+
IEnumerable<int> ie = null;
21+
try {
22+
ie = (IEnumerable<int>)a;
23+
} catch (InvalidCastException) {
24+
Console.WriteLine ("caught ICE, good");
25+
return 100;
26+
}
27+
ie.GetEnumerator(); // mono crashes here
28+
return 101;
29+
}
30+
31+
32+
private static Array NonZeroLowerBoundArray(Array szArrayContents, int lowerBound)
33+
{
34+
Array array = Array.CreateInstance(szArrayContents.GetType().GetElementType(), new int[] { szArrayContents.Length }, new int[] { lowerBound });
35+
for (int i = 0; i < szArrayContents.Length; i++)
36+
{
37+
array.SetValue(szArrayContents.GetValue(i), i + lowerBound);
38+
}
39+
return array;
40+
}
41+
42+
}
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<ItemGroup>
3+
<Compile Include="GitHub_93597.cs" />
4+
</ItemGroup>
5+
</Project>

src/tests/issues.targets

+1
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@
11531153
<ExcludeList Include="$(XunitTestBinBase)/JIT/Methodical\VT\port\huge_gcref_d\*" />
11541154
<ExcludeList Include="$(XunitTestBinBase)/JIT/Methodical\VT\port\huge_gcref_r\*" />
11551155
<ExcludeList Include="$(XunitTestBinBase)/JIT/Performance/CodeQuality/Benchstones/MDBenchI/MDGeneralArray/MDGeneralArray/*" />
1156+
<ExcludeList Include="$(XunitTestBinBase)/Loader/classloader/regressions/GitHub_93597/GitHub_93597/*" />
11561157

11571158
<!-- Covariant returns -->
11581159
<!-- https://github.com/dotnet/runtimelab/issues/205 -->

0 commit comments

Comments
 (0)