Skip to content

Conversation

@Venkad000
Copy link
Contributor

Avoid incorrectly resolving a MonoClass for MONO_TYPE_GENERICINST when loading custom attribute values.

load_cattr_value() previously unconditionally called m_type_data_get_klass(), which is invalid for GENERICINST and could lead to incorrect behavior when processing generic enum instances. This change defers class resolution for MONO_TYPE_GENERICINST and handles enum generic instances explicitly by extracting the underlying element type. This fixes a crash when decoding custom attributes involving generic types.

This crashes in Mono but doesn't in CoreClr

using System;
using System.Collections;
using System.Reflection;

using System.Runtime.InteropServices;

var attr = typeof(C).CustomAttributes.Single(d => d.AttributeType == typeof(A));
var arg = attr.ConstructorArguments.Single();
Console.WriteLine(arg.GetType());
class A : Attribute
{
    public unsafe A(B<delegate*<void>[]>.E e) { }
} 

class B<T>
{
    public enum E { }
}

[A(default)]
unsafe class C { }

Output on CoreClr:

System.Reflection.CustomAttributeTypedArgument

Output on Mono without fix:

MonoType with type 21 accessed by m_type_data_get_klass

=================================================================
	Native Crash Reporting
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
	Native stacktrace:
=================================================================
	0x7fa2a25ffd2f - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a25a3e0e - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a25ff5b1 - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a2a29040 - /lib64/libc.so.6 : 
	0x7fa2a2a82e5c - /lib64/libc.so.6 : 
	0x7fa2a2a28f0e - /lib64/libc.so.6 : gsignal
	0x7fa2a2a106d0 - /lib64/libc.so.6 : abort
	0x7fa2a26ac244 - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a26b9e06 - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a26ac606 - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a26ac73e - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a277445b - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a277408d - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a276ee74 - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a276e9c4 - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : mono_reflection_create_custom_attr_data_args
	0x7fa2a276fdbf - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x7fa2a2704f08 - /home/venkad/dotnet10.0.100-mono/shared/Microsoft.NETCore.App/10.0.0-dev/libcoreclr.so : 
	0x4037366b - Unknown

Output on Mono with fix:

System.Reflection.CustomAttributeTypedArgument

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 21, 2026
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 21, 2026
@lewing lewing added area-VM-meta-mono and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jan 21, 2026
Copilot AI added a commit that referenced this pull request Jan 21, 2026
Co-authored-by: lewing <24063+lewing@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-VM-meta-mono community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants