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

Handle more Attribute Types when Filtering Assemblies #9373

Merged
merged 4 commits into from
Oct 8, 2024
Merged

Conversation

dellis1972
Copy link
Contributor

Fixes #9369

When System.Reflection.Metadata comes across an Attribute which has a generic, it does NOT return a TypeReferenceHandle. It returns a TypeSpecificationHandle.

Tying to cast the type results in the following error

System.InvalidCastException: Die angegebene Umwandlung ist ungültig.
error XAFLT7007:    bei System.Reflection.Throw.InvalidCast()
error XAFLT7007:    bei System.Reflection.Metadata.TypeReferenceHandle.op_Explicit(EntityHandle handle)
error XAFLT7007:    bei Xamarin.Android.Tasks.MetadataExtensions.GetCustomAttributeFullName(MetadataReader reader, CustomAttribute attribute)
error XAFLT7007:    bei Xamarin.Android.Tasks.FilterAssemblies.IsAndroidAssembly(AssemblyDefinition assembly, MetadataReader reader)
error XAFLT7007:    bei Xamarin.Android.Tasks.FilterAssemblies.ProcessAssembly(ITaskItem assemblyItem, List`1 output)
error XAFLT7007:    bei Xamarin.Android.Tasks.FilterAssemblies.RunTask()
error XAFLT7007:    bei Microsoft.Android.Build.Tasks.AndroidTask.Execute()

So we need to handle this case, and put in place a backup to handle an InvalidCastException so that we never hit this issue again.

Fixes #9369

When System.Reflection.Metadata comes across an Attribute
which has a generic, it does NOT return a `TypeReferenceHandle`.
It returns a `TypeSpecificationHandle`.

Tying to cast the type results in the following error

```
System.InvalidCastException: Die angegebene Umwandlung ist ungültig.
error XAFLT7007:    bei System.Reflection.Throw.InvalidCast()
error XAFLT7007:    bei System.Reflection.Metadata.TypeReferenceHandle.op_Explicit(EntityHandle handle)
error XAFLT7007:    bei Xamarin.Android.Tasks.MetadataExtensions.GetCustomAttributeFullName(MetadataReader reader, CustomAttribute attribute)
error XAFLT7007:    bei Xamarin.Android.Tasks.FilterAssemblies.IsAndroidAssembly(AssemblyDefinition assembly, MetadataReader reader)
error XAFLT7007:    bei Xamarin.Android.Tasks.FilterAssemblies.ProcessAssembly(ITaskItem assemblyItem, List`1 output)
error XAFLT7007:    bei Xamarin.Android.Tasks.FilterAssemblies.RunTask()
error XAFLT7007:    bei Microsoft.Android.Build.Tasks.AndroidTask.Execute()
```

So we need to handle this case, and put in place a backup to handle
an `InvalidCastException` so that we never hit this issue again.
dellis1972 and others added 2 commits October 7, 2024 17:03
Code formatting, and log the unknown/unsupported .Kind value in the `catch` block.
@jonpryor
Copy link
Member

jonpryor commented Oct 8, 2024

Draft commit message:

[Xamarin.Android.Build.Tasks] Support HandleKind.TypeSpecification (#9373)

Fixes: https://github.com/dotnet/android/issues/9369

[C# 11 added support for generic custom attributes][0]:

	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
	public class GenericTestAttribute<T> : Attribute {
	}

which means generic types can now be used as assembly-level attributes:

	[assembly: GenericTestAttribute<string>]

Unfortunately, attempting to do this would result in an
`InvalidCastException` from the `<FilterAssemblies/>` task:

	System.InvalidCastException: Die angegebene Umwandlung ist ungültig.
	error XAFLT7007:    bei System.Reflection.Throw.InvalidCast()
	error XAFLT7007:    bei System.Reflection.Metadata.TypeReferenceHandle.op_Explicit(EntityHandle handle)
	error XAFLT7007:    bei Xamarin.Android.Tasks.MetadataExtensions.GetCustomAttributeFullName(MetadataReader reader, CustomAttribute attribute)
	error XAFLT7007:    bei Xamarin.Android.Tasks.FilterAssemblies.IsAndroidAssembly(AssemblyDefinition assembly, MetadataReader reader)
	error XAFLT7007:    bei Xamarin.Android.Tasks.FilterAssemblies.ProcessAssembly(ITaskItem assemblyItem, List`1 output)
	error XAFLT7007:    bei Xamarin.Android.Tasks.FilterAssemblies.RunTask()
	error XAFLT7007:    bei Microsoft.Android.Build.Tasks.AndroidTask.Execute()

This happens because when System.Reflection.Metadata comes across an
Attribute which has a generic type parameter, it does *not* return a
[`TypeReferenceHandle`][1], but instead a
[`TypeSpecificationHandle`][2].

Update `MetadataExtensions.GetCustomAttributeFullName()` to support
[`HandleKind.TypeSpecification`][3]

[0]: https://learn.microsoft.com/dotnet/csharp/whats-new/csharp-11#generic-attributes
[1]: https://learn.microsoft.com/dotnet/api/system.reflection.metadata.typereferencehandle?view=net-8.0
[2]: https://learn.microsoft.com/dotnet/api/system.reflection.metadata.typespecificationhandle?view=net-8.0
[3]: https://learn.microsoft.com/dotnet/api/system.reflection.metadata.handlekind?view=net-8.0

@jonpryor jonpryor merged commit d806eb4 into main Oct 8, 2024
56 of 58 checks passed
@jonpryor jonpryor deleted the Issue9369 branch October 8, 2024 23:57
@github-actions github-actions bot locked and limited conversation to collaborators Nov 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MetadataExtensions.GetCustomAttributeFullName crashes on generic AssemblyAttribute
3 participants