Cannot read CustomAttribute with enum declared in generic type #804

Closed
opened 2021-10-26 08:37:26 -05:00 by mrvoorhe · 4 comments
mrvoorhe commented 2021-10-26 08:37:26 -05:00 (Migrated from github.com)
[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 github.com/Unity-Technologies/cecil@53d375c553

The asserts after the exception may not be exactly correctly

``` [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 2021-10-26 08:38:19 -05:00 (Migrated from github.com)
This was found as part of https://github.com/dotnet/linker/issues/2332
jbevain commented 2021-10-26 11:03:46 -05:00 (Migrated from github.com)

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

Thanks for filing this with a repro, I'll have a look.
mrvoorhe commented 2021-10-26 12:22:46 -05:00 (Migrated from github.com)

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.

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 2021-10-26 12:24:56 -05:00 (Migrated from github.com)

Here is an additional test with an array github.com/Unity-Technologies/cecil@35c63a4399

Here is an additional test with an array https://github.com/Unity-Technologies/cecil/commit/35c63a43991956d68bac77f550540ae07a80e48e
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-jbevain/cecil#804
No description provided.