jbevain / cecil

Cecil is a library to inspect, modify and create .NET programs and libraries.
MIT License
2.77k stars 630 forks source link

Cannot read CustomAttribute with enum declared in generic type #804

Closed mrvoorhe closed 2 years ago

mrvoorhe commented 3 years ago
[Foo (Pan = GenericClassWithEnum<string>.MyEnum.One)]
class WithAttributeUsingNestedEnum {
}
System.NotImplementedException : GenericInst
   at Mono.Cecil.SignatureReader.ReadPrimitiveValue(ElementType type) in C:\Unity\dev\unity-cecil\Mono.Cecil\AssemblyReader.cs:line 3645
   at Mono.Cecil.SignatureReader.ReadCustomAttributeElementValue(TypeReference type) in C:\Unity\dev\unity-cecil\Mono.Cecil\AssemblyReader.cs:line 3611
   at Mono.Cecil.SignatureReader.ReadCustomAttributeElement(TypeReference type) in C:\Unity\dev\unity-cecil\Mono.Cecil\AssemblyReader.cs:line 3591
   at Mono.Cecil.SignatureReader.ReadCustomAttributeElement(TypeReference type) in C:\Unity\dev\unity-cecil\Mono.Cecil\AssemblyReader.cs:line 3591
   at Mono.Cecil.SignatureReader.ReadCustomAttributeFixedArgument(TypeReference type) in C:\Unity\dev\unity-cecil\Mono.Cecil\AssemblyReader.cs:line 3526
   at Mono.Cecil.SignatureReader.ReadCustomAttributeNamedArgument(Collection`1& fields, Collection`1& properties) in C:\Unity\dev\unity-cecil\Mono.Cecil\AssemblyReader.cs:line 3556
   at Mono.Cecil.SignatureReader.ReadCustomAttributeNamedArguments(UInt16 count, Collection`1& fields, Collection`1& properties) in C:\Unity\dev\unity-cecil\Mono.Cecil\AssemblyReader.cs:line 3534
   at Mono.Cecil.MetadataReader.ReadCustomAttributeSignature(CustomAttribute attribute) in C:\Unity\dev\unity-cecil\Mono.Cecil\AssemblyReader.cs:line 2597
   at Mono.Cecil.CustomAttribute.<Resolve>b__35_0(CustomAttribute attribute, MetadataReader reader) in C:\Unity\dev\unity-cecil\Mono.Cecil\CustomAttribute.cs:line 204
   at Mono.Cecil.ModuleDefinition.Read[TItem](TItem item, Action`2 read) in C:\Unity\dev\unity-cecil\Mono.Cecil\ModuleDefinition.cs:line 953
   at Mono.Cecil.CustomAttribute.Resolve() in C:\Unity\dev\unity-cecil\Mono.Cecil\CustomAttribute.cs:line 202
   at Mono.Cecil.CustomAttribute.get_Fields() in C:\Unity\dev\unity-cecil\Mono.Cecil\CustomAttribute.cs:line 126
   at Mono.Cecil.Tests.CustomAttributesTests.<>c.<EnumDeclaredInGenericType>b__25_0(ModuleDefinition module) in C:\Unity\dev\unity-cecil\Test\Mono.Cecil.Tests\CustomAttributesTests.cs:line 490
   at Mono.Cecil.Tests.TestRunner.RunTest() in C:\Unity\dev\unity-cecil\Test\Mono.Cecil.Tests\BaseTestFixture.cs:line 338
   at Mono.Cecil.Tests.BaseTestFixture.Run(TestCase testCase) in C:\Unity\dev\unity-cecil\Test\Mono.Cecil.Tests\BaseTestFixture.cs:line 143
   at Mono.Cecil.Tests.BaseTestFixture.TestCSharp(String file, Action`1 test, Boolean verify, Boolean readOnly, Type symbolReaderProvider, Type symbolWriterProvider, IAssemblyResolver assemblyResolver, Boolean applyWindowsRuntimeProjections, String sourceFilePath) in C:\Unity\dev\unity-cecil\Test\Mono.Cecil.Tests\BaseTestFixture.cs:line 132
   at Mono.Cecil.Tests.CustomAttributesTests.EnumDeclaredInGenericType() in C:\Unity\dev\unity-cecil\Test\Mono.Cecil.Tests\CustomAttributesTests.cs:line 485

Here is a branch with a unit test to reproduce https://github.com/Unity-Technologies/cecil/commits/master-cannot-read-enum-declared-in-generic-type

And the commit itself with the unit test https://github.com/Unity-Technologies/cecil/commit/53d375c553e77218601ab6c206472dc800fa8636

The asserts after the exception may not be exactly correctly

mrvoorhe commented 3 years ago

This was found as part of https://github.com/dotnet/linker/issues/2332

jbevain commented 3 years ago

Thanks for filing this with a repro, I'll have a look.

mrvoorhe commented 3 years ago

I tinkered a bit with trying to fix it but no luck yet. I added case ElementType.GenericInst: here https://github.com/jbevain/cecil/blob/master/Mono.Cecil/AssemblyReader.cs#L3605. That seemed to get further. Then the test would crash in ReadCustomAttributeFieldOrPropType.

I have another test with arrays I will commit. I'm sure you can fix this faster than I can, I'll let you take it from here.

mrvoorhe commented 3 years ago

Here is an additional test with an array https://github.com/Unity-Technologies/cecil/commit/35c63a43991956d68bac77f550540ae07a80e48e