Skip to content

[Mono] System.Runtime.Tests fail with TypeLoadException  #94086

Closed

Description

@adamsitnik

In #88620 I've added a very simple code that performs following checks:

private static void ValidateArrayType(Type arrayType)
{
    if (!arrayType.IsArray)
        ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_HasToBeArrayClass, ExceptionArgument.arrayType);

    ValidateElementType(arrayType.GetElementType()!);
}

private static void ValidateElementType(Type elementType)
{
    while (elementType.IsArray)
    {
        elementType = elementType.GetElementType()!;
    }

    if (elementType.IsByRef || elementType.IsByRefLike)
        ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ByRefLikeArray);
    if (elementType == typeof(void))
        ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_VoidArray);
    if (elementType.ContainsGenericParameters)
        ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_OpenType);
}

The tests pass on CLR and NativeAOT, but fail on Mono:

    System.Tests.ArrayTests.CreateInstanceFromArrayType_NotSupportedArrayType_ThrowsNotSupportedException(elementType: typeof(System.Void)) [FAIL]
      Assert.Throws() Failure: Exception type was not an exact match
      Expected: typeof(System.NotSupportedException)
      Actual:   typeof(System.TypeLoadException)
      ---- System.TypeLoadException : Could not load array element class, due to: Error getting the interfaces of System.Void[] due to Error Loading class assembly:/root/helix/work/correlation/shared/Microsoft.NETCore.App/9.0.0/System.Private.CoreLib.dll type:IList`1 member:(null) assembly:/root/helix/work/correlation/shared/Microsoft.NETCore.App/9.0.0/System.Private.CoreLib.dll type:Void[] member:(null)
      Stack Trace:
        /_/src/libraries/System.Runtime/tests/System/ArrayTests.cs(1957,0): at System.Tests.ArrayTests.CreateInstanceFromArrayType_NotSupportedArrayType_ThrowsNotSupportedException(Type elementType)
        /_/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.Mono.cs(22,0): at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
        /_/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.cs(178,0): at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
        ----- Inner Stack Trace -----
        /_/src/mono/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs(287,0): at System.RuntimeTypeHandle.GetElementType(RuntimeType type)
        /_/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs(96,0): at System.RuntimeType.GetElementType()
        /_/src/libraries/System.Private.CoreLib/src/System/Array.cs(338,0): at System.Array.ValidateArrayType(Type arrayType)
        /_/src/libraries/System.Private.CoreLib/src/System/Array.cs(261,0): at System.Array.CreateInstanceFromArrayType(Type arrayType, Int32[] lengths)
        /_/src/libraries/System.Runtime/tests/System/ArrayTests.cs(1957,0): at System.Tests.ArrayTests.<>c__DisplayClass90_0.<CreateInstanceFromArrayType_NotSupportedArrayType_ThrowsNotSupportedException>b__1()
    System.Threading.Tests.PeriodicTimerTests.PeriodicTimer_NoActiveOperations_TimerNotRooted [SKIP]

What is really strange is that due to this failure, other tests that were passing so far started to fail as well:

    System.Tests.TypeTests.IsSZArray_TrueForSZArrayTypes [FAIL]
      System.TypeLoadException : Error getting the interfaces of System.Void[] due to Error Loading class assembly:/root/helix/work/correlation/shared/Microsoft.NETCore.App/9.0.0/System.Private.CoreLib.dll type:IList`1 member:(null)
      Stack Trace:
        /_/src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs(1900,0): at System.RuntimeType.MakeArrayType()
        /_/src/libraries/System.Runtime/tests/System/Type/TypeTests.cs(613,0): at System.Tests.TypeTests.<>c.<IsSZArray_TrueForSZArrayTypes>b__36_0(Type nonArrayBaseType)
        /_/src/libraries/System.Linq/src/System/Linq/Select.cs(238,0): at System.Linq.Enumerable.SelectListIterator`2[[System.Type, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Type, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
        /_/src/libraries/System.Runtime/tests/System/Type/TypeTests.cs(613,0): at System.Tests.TypeTests.IsSZArray_TrueForSZArrayTypes()
        /_/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.Mono.cs(22,0): at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
        /_/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.cs(57,0): at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
    System.Threading.Tests.PeriodicTimerTests.PeriodicTimer_ActiveOperations_TimerRooted [SKIP]

I was not able to repro it with a standalone app:

using System.Reflection;

namespace ArrayOfVoid
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"IsMono: {Type.GetType("Mono.RuntimeStructs") != null}");

            Console.WriteLine(typeof(void).MakeArrayType().IsArray);
            Console.WriteLine(typeof(void).MakeArrayType().MakeArrayType().IsArray);

            Console.WriteLine(typeof(void).MakeArrayType().GetElementType()!.IsArray);
            Console.WriteLine(typeof(void).MakeArrayType().MakeArrayType().GetElementType()!.IsArray);
            Console.WriteLine(typeof(void).MakeArrayType().MakeArrayType().GetElementType()!.GetElementType()!.IsArray);
        }
    }
}
dotnet publish -c Release -r win-x64 --self-contained /p:UseMonoRuntime=true
.\bin\Release\net8.0\win-x64\publish\ArrayOfVoid.exe
IsMono: True
True
True
False
True
False

This test is trying to create an array of arrays of void, I suspect that this is somehow triggering some edge case.

BTW some of the tests are marked with [ActiveIssue("https://github.com/dotnet/runtime/issues/52072" despite the fact that #52072 has been closed (cc @mdh1418)

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Oct 27, 2023
  2. added this to the 9.0.0 milestone on Oct 27, 2023
  3. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Oct 27, 2023
  4. marek-safar commented on Oct 27, 2023

    @marek-safar
    Contributor
  5. vargaz commented on Oct 27, 2023

    @vargaz
    Contributor

    Will look into it. AFAIK, void[] is not a valid type, so some operations on it will cause type load exceptions, mono might throw them in different places than coreclr.

  6. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Nov 16, 2023
  7. added a commit that references this issue on Nov 17, 2023
    8b04e1a
  8. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Nov 17, 2023
  9. locked and limited conversation to collaborators on Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions