runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[Mono] InvalidCastException thrown while doing an Array.Copy() from an Array of System.UInt32 to an Array of Enums

Open Venkad000 opened this issue 1 year ago • 5 comments

The test case link fails on Mono runtime throwing an InvalidCastException. The InvalidCastException is thrown in the function Enum.GetValue() (where T is an enum) when it tries to do an Array.Copy() from an array of System.Int32 to an array of T[]. Normally this wouldn't throw an exception, but an exception gets thrown in this case because T turns out to be of MONO_TYPE_GENERICINST whereas it would be expected to be of MONO_TYPE_VALUETYPE. Due to this disparity, there are no switch statements that would handle a MONO_TYPE_GENERICINST in the function https://github.com/dotnet/runtime/blob/bc6f4b0c8c4cbfb31878f350a5c6ca2efc96ff71/src/mono/mono/metadata/icall.c#L344 and gives an INVALID_CAST here

I have tried extracting this, but failed to reproduce the issue. This is what I tried doing.

using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

public class PoolableDbContext(DbContextOptions options) : DbContext(options)
{
    protected PoolableDbContext()
        : this(new DbContextOptions<PoolableDbContext>())
    {
    }
}

public class PrimitiveCollectionsContext(DbContextOptions options) : PoolableDbContext(options) {
    public DbSet<PrimitiveCollectionsEntity> PrimitiveCollectionsEntities { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseInMemoryDatabase("MyInMemoryDb");
    }
}

public class PrimitiveCollectionsEntity
{
    public int Id { get; set; }

    public required string String { get; set; }
    public int Int { get; set; }
    public DateTime DateTime { get; set; }
    public bool Bool { get; set; }
    public WrappedId WrappedId { get; set; }
    public MyEnum Enum { get; set; }
    public int? NullableInt { get; set; }
    public string? NullableString { get; set; }
    public WrappedId? NullableWrappedId { get; set; }
    public WrappedId? NullableWrappedIdWithNullableComparer { get; set; }

    public required string[] Strings { get; set; }
    public required int[] Ints { get; set; }
    public required DateTime[] DateTimes { get; set; }
    public required bool[] Bools { get; set; }
    public required MyEnum[] Enums { get; set; }
    public required int?[] NullableInts { get; set; }
    public required string?[] NullableStrings { get; set; }
}

public readonly record struct WrappedId(int Value);

public enum MyEnum { Value1, Value2, Value3, Value4 }


class Program
{
    static void Main()
    {
        PrimitiveCollectionsContext ss = new PrimitiveCollectionsContext(new DbContextOptions<PoolableDbContext>());
        ss.Set<PrimitiveCollectionsEntity>().Where(x => Enum.GetValues<MyEnum>().Cast<int>().Count(y => y == x.Int) > 0).ToListAsync();
    }
}

Venkad000 avatar Jun 10 '25 06:06 Venkad000

@@ -696,7 +696,44 @@ array_set_value_impl (MonoArray *arr, MonoObjectHandle value_handle, guint32 pos
                case MONO_TYPE_R8:
                        ASSIGN_REAL (gdouble);
                        break;
-               default:
+        case MONO_TYPE_GENERICINST:
+            switch(vt) {
+            case MONO_TYPE_CHAR:
+                ASSIGN_UNSIGNED (guint16);
+                break;
+            case MONO_TYPE_U1:
+                ASSIGN_UNSIGNED (guint8);
+                break;
+            case MONO_TYPE_U2:
+                ASSIGN_UNSIGNED (guint16);
+                break;
+            case MONO_TYPE_U4:
+                ASSIGN_UNSIGNED (guint32);
+                break;
+            case MONO_TYPE_U8:
+                ASSIGN_UNSIGNED (guint64);
+                break;
+            case MONO_TYPE_I1:
+                ASSIGN_SIGNED (gint8);
+                break;
+            case MONO_TYPE_I2:
+                ASSIGN_SIGNED (gint16);
+                break;
+            case MONO_TYPE_I4:
+                ASSIGN_SIGNED (gint32);
+                break;
+            case MONO_TYPE_I8:
+                ASSIGN_SIGNED (gint64);
+                break;
+            case MONO_TYPE_R4:
+                ASSIGN_REAL (gfloat);
+                break;
+            case MONO_TYPE_R8:
+                ASSIGN_REAL (gdouble);
+                break;
+            }
+            break;
+        default:
                        INVALID_CAST;
                }
        }

I added a case in https://github.com/dotnet/runtime/blob/bc6f4b0c8c4cbfb31878f350a5c6ca2efc96ff71/src/mono/mono/metadata/icall.c#L344 which doesn't raise the exception anymore. Would this be considered a correct implementation?

Venkad000 avatar Jun 10 '25 06:06 Venkad000

cc: @steveisok

saitama951 avatar Jun 10 '25 14:06 saitama951

/cc @vitek-karas

steveisok avatar Jul 28 '25 15:07 steveisok

@BrzVlad - does https://github.com/dotnet/runtime/issues/116462#issuecomment-2957901371 look reasonable?

steveisok avatar Aug 22 '25 17:08 steveisok

I can reproduce with the following diff applied to your code:

32c32
<     public SomeClass<object>.MyEnum Enum { get; set; }
---
>     public MyEnum Enum { get; set; }
42c42
<     public required SomeClass<object>.MyEnum[] Enums { get; set; }
---
>     public required MyEnum[] Enums { get; set; }
49,51c49,50
< public class SomeClass<T> {
<     public enum MyEnum { Value1, Value2, Value3, Value4 }
< }
---
> public enum MyEnum { Value1, Value2, Value3, Value4 }
> 
58c57
<         ss.Set<PrimitiveCollectionsEntity>().Where(x => Enum.GetValues<SomeClass<object>.MyEnum>().Cast<int>().Count(y => y == x.Int) > 0).ToListAsync();
---
>         ss.Set<PrimitiveCollectionsEntity>().Where(x => Enum.GetValues<MyEnum>().Cast<int>().Count(y => y == x.Int) > 0).ToListAsync();

The fix doesn't seem right. I am assuming you would want to obtain the underlying type of the generic instance instead and retry the switch. For example: https://github.com/dotnet/runtime/blob/b312158dc6a79c91109f176cba7a05cc6ed6655c/src/mono/mono/mini/interp/transform.c#L341

Still, I'm not 100% convinced this the complete fix. I suspect you might need to handle the enum checks above as well: https://github.com/dotnet/runtime/blob/b312158dc6a79c91109f176cba7a05cc6ed6655c/src/mono/mono/metadata/icall.c#L490

BrzVlad avatar Aug 26 '25 15:08 BrzVlad