Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# 11.0 Generic Attributes cannot be parsed correctly #397

Closed
Washi1337 opened this issue Dec 28, 2022 · 0 comments · Fixed by #401
Closed

C# 11.0 Generic Attributes cannot be parsed correctly #397

Washi1337 opened this issue Dec 28, 2022 · 0 comments · Fixed by #401
Labels
bug dotnet Issues related to AsmResolver.DotNet
Milestone

Comments

@Washi1337
Copy link
Owner

AsmResolver Version

5.0.0

.NET Version

.NET 7.0

Operating System

Linux

Describe the Bug

With the introduction of generic custom attributes in C# 11.0, the runtime has changed the implementation of custom attribute signatures slightly to support instantiated type parameters. AsmResolver currently does not support this, and crashes upon encountering such a CA signature.

How To Reproduce

Compile and run the following program:

[Metadata<int>(1337)]
[Metadata<int>(Value = 1337)]
internal class Program
{
    public static void Main(string[] args)
    {
        var module = ModuleDefinition.FromFile(typeof(Program).Assembly.Location);
        var type = module.TopLevelTypes.First(t => t.Name == nameof(Program));

        foreach (var attribute in type.CustomAttributes)
        {
            Console.WriteLine(attribute.Constructor);

            Console.WriteLine("- Fixed arguments: ");
            for (var i = 0; i < attribute.Signature.FixedArguments.Count; i++)
                Console.WriteLine($"\t- {i}: {attribute.Signature.FixedArguments[i].Element}");

            Console.WriteLine("- Named arguments: ");
            foreach (var arg in attribute.Signature.NamedArguments)
                Console.WriteLine($"\t- {arg.MemberName}: {arg.Argument.Element}");
        }
    }
}

[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class MetadataAttribute<T> : Attribute
{
    public MetadataAttribute()
    {
        Value = default;
    }

    public MetadataAttribute(T value)
    {
        Value = value;
    }

    public T? Value
    {
        get;
        set;
    }
}

Expected Behavior

System.Void MetadataAttribute<System.Int32>::.ctor(!0)
- Fixed arguments: 
        - 0: 1337
- Named arguments: 
System.Void MetadataAttribute<System.Int32>::.ctor()
- Fixed arguments: 
- Named arguments: 
        - Value: 1337

Actual Behavior

System.Void MetadataAttribute`1<System.Int32>::.ctor(!0)
- Fixed arguments: 
Unhandled exception. System.NotSupportedException: Unsupported element type Var in custom attribute argument.
   at AsmResolver.ThrowErrorListener.RegisterException(Exception exception)
   at AsmResolver.DotNet.Serialized.ModuleReaderContext.RegisterException(Exception exception)
   at AsmResolver.ErrorListenerExtensions.NotSupported(IErrorListener self, String message)
   at AsmResolver.DotNet.Signatures.CustomAttributeArgumentReader.ReadValue(BlobReaderContext& context, BinaryStreamReader& reader, TypeSignature valueType)
   at AsmResolver.DotNet.Signatures.CustomAttributeArgument.FromReader(BlobReaderContext& context, TypeSignature argumentType, BinaryStreamReader& reader)
   at AsmResolver.DotNet.Signatures.SerializedCustomAttributeSignature.Initialize(IList`1 fixedArguments, IList`1 namedArguments)
   at AsmResolver.DotNet.Signatures.CustomAttributeSignature.EnsureIsInitialized()
   at AsmResolver.DotNet.Signatures.CustomAttributeSignature.get_FixedArguments()
   at Program.Main(String[] args)

Additional Context

No response

@Washi1337 Washi1337 added bug dotnet Issues related to AsmResolver.DotNet labels Dec 28, 2022
@Washi1337 Washi1337 added this to the 5.1.0 milestone Dec 28, 2022
@Washi1337 Washi1337 linked a pull request Jan 5, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug dotnet Issues related to AsmResolver.DotNet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant