Skip to content

CustomAttribute.DecodeValue doesn't handle generic type instantiations #123878

@MichalStrehovsky

Description

@MichalStrehovsky

Following program throws BadImageFormatException because CustomAttributeDecoder.DecodeFixedArgumentType doesn't handle SignatureTypeCode.GenericTypeInstance.

using System;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;

unsafe class Program
{
    static void Main()
    {
        typeof(ClassWithGenericEnumAttribute).Assembly.TryGetRawMetadata(out byte* blob, out int length);
        var reader = new MetadataReader(blob, length);
        var def = reader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle(typeof(ClassWithGenericEnumAttribute).MetadataToken));
        foreach (var attr in def.GetCustomAttributes())
        {
            reader.GetCustomAttribute(attr).DecodeValue(new CustomAttributeTypeProvider());
        }
    }
}

internal class CustomAttributeTypeProvider : ICustomAttributeTypeProvider<object>
{
    public object GetPrimitiveType(PrimitiveTypeCode typeCode) => throw new NotImplementedException();
    public object GetSystemType() => throw new NotImplementedException();
    public object GetSZArrayType(object elementType) => throw new NotImplementedException();
    public object GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) => throw new NotImplementedException();
    public object GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) => throw new NotImplementedException();
    public object GetTypeFromSerializedName(string name) => throw new NotImplementedException();
    public PrimitiveTypeCode GetUnderlyingEnumType(object type) => throw new NotImplementedException();
    public bool IsSystemType(object type) => throw new NotImplementedException();
}

class GenericEnumAttributeWithFunctionPointer : Attribute
{
    public unsafe GenericEnumAttributeWithFunctionPointer(GenericClassForEnum<delegate*<void>[]>.E e) { }
}

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

[GenericEnumAttributeWithFunctionPointer(default)]
unsafe class ClassWithGenericEnumAttribute { }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions